4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_net.h>
57 #include <lustre_nodemap.h>
58 #include <lustre_mds.h>
59 #include <lustre_param.h>
60 #include <lustre_quota.h>
61 #include <lustre_swab.h>
63 #include <obd_support.h>
64 #include <lustre_barrier.h>
66 #include <llog_swab.h>
68 #include "mdt_internal.h"
71 static unsigned int max_mod_rpcs_per_client = 8;
72 module_param(max_mod_rpcs_per_client, uint, 0644);
73 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
75 mdl_mode_t mdt_mdl_lock_modes[] = {
76 [LCK_MINMODE] = MDL_MINMODE,
83 [LCK_GROUP] = MDL_GROUP
86 enum ldlm_mode mdt_dlm_lock_modes[] = {
87 [MDL_MINMODE] = LCK_MINMODE,
94 [MDL_GROUP] = LCK_GROUP
97 static struct mdt_device *mdt_dev(struct lu_device *d);
98 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
100 static const struct lu_object_operations mdt_obj_ops;
102 /* Slab for MDT object allocation */
103 static struct kmem_cache *mdt_object_kmem;
105 /* For HSM restore handles */
106 struct kmem_cache *mdt_hsm_cdt_kmem;
108 /* For HSM request handles */
109 struct kmem_cache *mdt_hsm_car_kmem;
111 static struct lu_kmem_descr mdt_caches[] = {
113 .ckd_cache = &mdt_object_kmem,
114 .ckd_name = "mdt_obj",
115 .ckd_size = sizeof(struct mdt_object)
118 .ckd_cache = &mdt_hsm_cdt_kmem,
119 .ckd_name = "mdt_cdt_restore_handle",
120 .ckd_size = sizeof(struct cdt_restore_handle)
123 .ckd_cache = &mdt_hsm_car_kmem,
124 .ckd_name = "mdt_cdt_agent_req",
125 .ckd_size = sizeof(struct cdt_agent_req)
132 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
136 return rep->lock_policy_res1 & op_flag;
139 void mdt_clear_disposition(struct mdt_thread_info *info,
140 struct ldlm_reply *rep, __u64 op_flag)
143 info->mti_opdata &= ~op_flag;
144 tgt_opdata_clear(info->mti_env, op_flag);
147 rep->lock_policy_res1 &= ~op_flag;
150 void mdt_set_disposition(struct mdt_thread_info *info,
151 struct ldlm_reply *rep, __u64 op_flag)
154 info->mti_opdata |= op_flag;
155 tgt_opdata_set(info->mti_env, op_flag);
158 rep->lock_policy_res1 |= op_flag;
161 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
163 lh->mlh_pdo_hash = 0;
164 lh->mlh_reg_mode = lm;
165 lh->mlh_rreg_mode = lm;
166 lh->mlh_type = MDT_REG_LOCK;
169 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
170 const struct lu_name *lname)
172 lh->mlh_reg_mode = lock_mode;
173 lh->mlh_pdo_mode = LCK_MINMODE;
174 lh->mlh_rreg_mode = lock_mode;
175 lh->mlh_type = MDT_PDO_LOCK;
177 if (lu_name_is_valid(lname)) {
178 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
180 /* XXX Workaround for LU-2856
182 * Zero is a valid return value of full_name_hash, but
183 * several users of mlh_pdo_hash assume a non-zero
184 * hash value. We therefore map zero onto an
185 * arbitrary, but consistent value (1) to avoid
186 * problems further down the road. */
187 if (unlikely(lh->mlh_pdo_hash == 0))
188 lh->mlh_pdo_hash = 1;
190 lh->mlh_pdo_hash = 0;
194 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
195 struct mdt_lock_handle *lh)
201 * Any dir access needs couple of locks:
203 * 1) on part of dir we gonna take lookup/modify;
205 * 2) on whole dir to protect it from concurrent splitting and/or to
206 * flush client's cache for readdir().
208 * so, for a given mode and object this routine decides what lock mode
209 * to use for lock #2:
211 * 1) if caller's gonna lookup in dir then we need to protect dir from
212 * being splitted only - LCK_CR
214 * 2) if caller's gonna modify dir then we need to protect dir from
215 * being splitted and to flush cache - LCK_CW
217 * 3) if caller's gonna modify dir and that dir seems ready for
218 * splitting then we need to protect it from any type of access
219 * (lookup/modify/split) - LCK_EX --bzzz
222 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
223 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
226 * Ask underlaying level its opinion about preferable PDO lock mode
227 * having access type passed as regular lock mode:
229 * - MDL_MINMODE means that lower layer does not want to specify lock
232 * - MDL_NL means that no PDO lock should be taken. This is used in some
233 * cases. Say, for non-splittable directories no need to use PDO locks
236 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
237 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
239 if (mode != MDL_MINMODE) {
240 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
243 * Lower layer does not want to specify locking mode. We do it
244 * our selves. No special protection is needed, just flush
245 * client's cache on modification and allow concurrent
248 switch (lh->mlh_reg_mode) {
250 lh->mlh_pdo_mode = LCK_EX;
253 lh->mlh_pdo_mode = LCK_CR;
256 lh->mlh_pdo_mode = LCK_CW;
259 CERROR("Not expected lock type (0x%x)\n",
260 (int)lh->mlh_reg_mode);
265 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
269 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
272 struct mdt_device *mdt = info->mti_mdt;
273 struct lu_name *lname = &info->mti_name;
275 struct mdt_object *parent;
279 LASSERT(!info->mti_cross_ref);
281 OBD_ALLOC(name, NAME_MAX + 1);
284 lname->ln_name = name;
287 * We may want to allow this to mount a completely separate
288 * fileset from the MDT in the future, but keeping it to
289 * ROOT/ only for now avoid potential security issues.
291 *fid = mdt->mdt_md_root_fid;
293 while (rc == 0 && fileset != NULL && *fileset != '\0') {
294 const char *s1 = fileset;
300 while (*s2 != '/' && *s2 != '\0')
308 lname->ln_namelen = s2 - s1;
309 if (lname->ln_namelen > NAME_MAX) {
314 /* reject .. as a path component */
315 if (lname->ln_namelen == 2 &&
316 strncmp(s1, "..", 2) == 0) {
321 strncpy(name, s1, lname->ln_namelen);
322 name[lname->ln_namelen] = '\0';
324 parent = mdt_object_find(info->mti_env, mdt, fid);
325 if (IS_ERR(parent)) {
326 rc = PTR_ERR(parent);
329 /* Only got the fid of this obj by name */
331 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
332 fid, &info->mti_spec);
333 mdt_object_put(info->mti_env, parent);
336 parent = mdt_object_find(info->mti_env, mdt, fid);
338 rc = PTR_ERR(parent);
340 mode = lu_object_attr(&parent->mot_obj);
341 mdt_object_put(info->mti_env, parent);
347 OBD_FREE(name, NAME_MAX + 1);
352 static int mdt_get_root(struct tgt_session_info *tsi)
354 struct mdt_thread_info *info = tsi2mdt_info(tsi);
355 struct mdt_device *mdt = info->mti_mdt;
356 struct mdt_body *repbody;
357 char *fileset = NULL, *buffer = NULL;
359 struct obd_export *exp = info->mti_exp;
360 char *nodemap_fileset;
364 rc = mdt_check_ucred(info);
366 GOTO(out, rc = err_serious(rc));
368 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
369 GOTO(out, rc = err_serious(-ENOMEM));
371 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
372 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
373 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
375 GOTO(out, rc = err_serious(-EFAULT));
378 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
379 if (nodemap_fileset && nodemap_fileset[0]) {
380 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
382 /* consider fileset from client as a sub-fileset
383 * of the nodemap one */
384 OBD_ALLOC(buffer, PATH_MAX + 1);
386 GOTO(out, rc = err_serious(-ENOMEM));
387 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
388 nodemap_fileset, fileset) >= PATH_MAX + 1)
389 GOTO(out, rc = err_serious(-EINVAL));
392 /* enforce fileset as specified in the nodemap */
393 fileset = nodemap_fileset;
398 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
399 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
401 GOTO(out, rc = err_serious(rc));
403 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
405 repbody->mbo_valid |= OBD_MD_FLID;
409 mdt_thread_info_fini(info);
411 OBD_FREE(buffer, PATH_MAX+1);
415 static int mdt_statfs(struct tgt_session_info *tsi)
417 struct ptlrpc_request *req = tgt_ses_req(tsi);
418 struct mdt_thread_info *info = tsi2mdt_info(tsi);
419 struct md_device *next = info->mti_mdt->mdt_child;
420 struct ptlrpc_service_part *svcpt;
421 struct obd_statfs *osfs;
426 svcpt = req->rq_rqbd->rqbd_svcpt;
428 /* This will trigger a watchdog timeout */
429 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
430 (MDT_SERVICE_WATCHDOG_FACTOR *
431 at_get(&svcpt->scp_at_estimate)) + 1);
433 rc = mdt_check_ucred(info);
435 GOTO(out, rc = err_serious(rc));
437 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
438 GOTO(out, rc = err_serious(-ENOMEM));
440 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
442 GOTO(out, rc = -EPROTO);
444 /** statfs information are cached in the mdt_device */
445 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
446 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
447 /** statfs data is too old, get up-to-date one */
448 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
451 spin_lock(&info->mti_mdt->mdt_lock);
452 info->mti_mdt->mdt_osfs = *osfs;
453 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
454 spin_unlock(&info->mti_mdt->mdt_lock);
456 /** use cached statfs data */
457 spin_lock(&info->mti_mdt->mdt_lock);
458 *osfs = info->mti_mdt->mdt_osfs;
459 spin_unlock(&info->mti_mdt->mdt_lock);
463 mdt_counter_incr(req, LPROC_MDT_STATFS);
465 mdt_thread_info_fini(info);
469 #ifdef CONFIG_FS_POSIX_ACL
471 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
473 * \param info thread info object
474 * \param repbody reply to pack ACLs into
475 * \param o mdt object of file to examine
476 * \param nodemap nodemap of client to reply to
478 * \retval -errno error getting or parsing ACL from disk
480 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
481 struct mdt_object *o, struct lu_nodemap *nodemap)
483 const struct lu_env *env = info->mti_env;
484 struct md_object *next = mdt_object_child(o);
485 struct lu_buf *buf = &info->mti_buf;
488 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
489 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
491 if (buf->lb_len == 0)
494 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
496 if (rc == -ENODATA) {
497 repbody->mbo_aclsize = 0;
498 repbody->mbo_valid |= OBD_MD_FLACL;
500 } else if (rc == -EOPNOTSUPP) {
503 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
504 mdt_obd_name(info->mti_mdt),
505 PFID(mdt_object_fid(o)), rc);
508 rc = nodemap_map_acl(nodemap, buf->lb_buf,
509 rc, NODEMAP_FS_TO_CLIENT);
510 /* if all ACLs mapped out, rc is still >= 0 */
512 CERROR("%s: nodemap_map_acl unable to parse "DFID
513 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
514 PFID(mdt_object_fid(o)), rc);
516 repbody->mbo_aclsize = rc;
517 repbody->mbo_valid |= OBD_MD_FLACL;
525 /* XXX Look into layout in MDT layer. */
526 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
528 struct lov_comp_md_v1 *comp_v1;
529 struct lov_mds_md *v1;
532 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
533 comp_v1 = (struct lov_comp_md_v1 *)lmm;
535 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
536 v1 = (struct lov_mds_md *)((char *)comp_v1 +
537 comp_v1->lcm_entries[i].lcme_offset);
538 /* We don't support partial release for now */
539 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
544 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
549 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
550 const struct lu_attr *attr, const struct lu_fid *fid)
552 struct md_attr *ma = &info->mti_attr;
553 struct obd_export *exp = info->mti_exp;
554 struct lu_nodemap *nodemap = NULL;
556 LASSERT(ma->ma_valid & MA_INODE);
558 if (attr->la_valid & LA_ATIME) {
559 b->mbo_atime = attr->la_atime;
560 b->mbo_valid |= OBD_MD_FLATIME;
562 if (attr->la_valid & LA_MTIME) {
563 b->mbo_mtime = attr->la_mtime;
564 b->mbo_valid |= OBD_MD_FLMTIME;
566 if (attr->la_valid & LA_CTIME) {
567 b->mbo_ctime = attr->la_ctime;
568 b->mbo_valid |= OBD_MD_FLCTIME;
570 if (attr->la_valid & LA_FLAGS) {
571 b->mbo_flags = attr->la_flags;
572 b->mbo_valid |= OBD_MD_FLFLAGS;
574 if (attr->la_valid & LA_NLINK) {
575 b->mbo_nlink = attr->la_nlink;
576 b->mbo_valid |= OBD_MD_FLNLINK;
578 if (attr->la_valid & (LA_UID|LA_GID)) {
579 nodemap = nodemap_get_from_exp(exp);
583 if (attr->la_valid & LA_UID) {
584 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
585 NODEMAP_FS_TO_CLIENT,
587 b->mbo_valid |= OBD_MD_FLUID;
589 if (attr->la_valid & LA_GID) {
590 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
591 NODEMAP_FS_TO_CLIENT,
593 b->mbo_valid |= OBD_MD_FLGID;
596 if (attr->la_valid & LA_PROJID) {
597 /* TODO, nodemap for project id */
598 b->mbo_projid = attr->la_projid;
599 b->mbo_valid |= OBD_MD_FLPROJID;
602 b->mbo_mode = attr->la_mode;
603 if (attr->la_valid & LA_MODE)
604 b->mbo_valid |= OBD_MD_FLMODE;
605 if (attr->la_valid & LA_TYPE)
606 b->mbo_valid |= OBD_MD_FLTYPE;
610 b->mbo_valid |= OBD_MD_FLID;
611 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
612 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
615 if (!(attr->la_valid & LA_TYPE))
618 b->mbo_rdev = attr->la_rdev;
619 b->mbo_size = attr->la_size;
620 b->mbo_blocks = attr->la_blocks;
622 if (!S_ISREG(attr->la_mode)) {
623 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
624 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
625 /* means no objects are allocated on osts. */
626 LASSERT(!(ma->ma_valid & MA_LOV));
627 /* just ignore blocks occupied by extend attributes on MDS */
629 /* if no object is allocated on osts, the size on mds is valid.
631 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
632 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
633 mdt_hsm_is_released(ma->ma_lmm)) {
634 /* A released file stores its size on MDS. */
635 /* But return 1 block for released file, unless tools like tar
636 * will consider it fully sparse. (LU-3864)
638 if (unlikely(b->mbo_size == 0))
642 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
645 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
646 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
647 PFID(fid), (unsigned long long)b->mbo_size);
650 if (!IS_ERR_OR_NULL(nodemap))
651 nodemap_putref(nodemap);
654 static inline int mdt_body_has_lov(const struct lu_attr *la,
655 const struct mdt_body *body)
657 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
658 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
661 void mdt_client_compatibility(struct mdt_thread_info *info)
663 struct mdt_body *body;
664 struct ptlrpc_request *req = mdt_info_req(info);
665 struct obd_export *exp = req->rq_export;
666 struct md_attr *ma = &info->mti_attr;
667 struct lu_attr *la = &ma->ma_attr;
670 if (exp_connect_layout(exp))
671 /* the client can deal with 16-bit lmm_stripe_count */
674 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
676 if (!mdt_body_has_lov(la, body))
679 /* now we have a reply with a lov for a client not compatible with the
680 * layout lock so we have to clean the layout generation number */
681 if (S_ISREG(la->la_mode))
682 ma->ma_lmm->lmm_layout_gen = 0;
686 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
687 struct mdt_object *o)
689 const struct lu_env *env = info->mti_env;
692 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
701 /* Is it a directory? Let's check for the LMV as well */
702 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
703 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
707 rc2 = mo_xattr_get(env, mdt_object_child(o),
709 XATTR_NAME_DEFAULT_LMV);
711 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
719 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
722 const struct lu_env *env = info->mti_env;
726 LASSERT(info->mti_big_lmm_used == 0);
727 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
731 /* big_lmm may need to be grown */
732 if (info->mti_big_lmmsize < rc) {
733 int size = size_roundup_power2(rc);
735 if (info->mti_big_lmmsize > 0) {
736 /* free old buffer */
737 LASSERT(info->mti_big_lmm);
738 OBD_FREE_LARGE(info->mti_big_lmm,
739 info->mti_big_lmmsize);
740 info->mti_big_lmm = NULL;
741 info->mti_big_lmmsize = 0;
744 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
745 if (info->mti_big_lmm == NULL)
747 info->mti_big_lmmsize = size;
749 LASSERT(info->mti_big_lmmsize >= rc);
751 info->mti_buf.lb_buf = info->mti_big_lmm;
752 info->mti_buf.lb_len = info->mti_big_lmmsize;
753 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
758 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
759 struct md_attr *ma, const char *name)
761 struct md_object *next = mdt_object_child(o);
762 struct lu_buf *buf = &info->mti_buf;
765 if (strcmp(name, XATTR_NAME_LOV) == 0) {
766 buf->lb_buf = ma->ma_lmm;
767 buf->lb_len = ma->ma_lmm_size;
768 LASSERT(!(ma->ma_valid & MA_LOV));
769 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
770 buf->lb_buf = ma->ma_lmv;
771 buf->lb_len = ma->ma_lmv_size;
772 LASSERT(!(ma->ma_valid & MA_LMV));
773 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
774 buf->lb_buf = ma->ma_lmv;
775 buf->lb_len = ma->ma_lmv_size;
776 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
781 rc = mo_xattr_get(info->mti_env, next, buf, name);
785 if (strcmp(name, XATTR_NAME_LOV) == 0) {
786 if (info->mti_big_lmm_used)
787 ma->ma_lmm = info->mti_big_lmm;
789 /* NOT return LOV EA with hole to old client. */
790 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
791 LOV_PATTERN_F_HOLE) &&
792 !(exp_connect_flags(info->mti_exp) &
793 OBD_CONNECT_LFSCK)) {
796 ma->ma_lmm_size = rc;
797 ma->ma_valid |= MA_LOV;
799 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
800 if (info->mti_big_lmm_used)
801 ma->ma_lmv = info->mti_big_lmm;
803 ma->ma_lmv_size = rc;
804 ma->ma_valid |= MA_LMV;
805 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
806 ma->ma_lmv_size = rc;
807 ma->ma_valid |= MA_LMV_DEF;
810 /* Update mdt_max_mdsize so all clients will be aware that */
811 if (info->mti_mdt->mdt_max_mdsize < rc)
812 info->mti_mdt->mdt_max_mdsize = rc;
815 } else if (rc == -ENODATA) {
818 } else if (rc == -ERANGE) {
819 /* Default LMV has fixed size, so it must be able to fit
820 * in the original buffer */
821 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
823 rc = mdt_big_xattr_get(info, o, name);
825 info->mti_big_lmm_used = 1;
833 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
834 struct mdt_object *o, struct lu_fid *pfid)
836 struct lu_buf *buf = &info->mti_buf;
837 struct link_ea_header *leh;
838 struct link_ea_entry *lee;
842 buf->lb_buf = info->mti_big_lmm;
843 buf->lb_len = info->mti_big_lmmsize;
844 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
845 buf, XATTR_NAME_LINK);
846 /* ignore errors, MA_PFID won't be set and it is
847 * up to the caller to treat this as an error */
848 if (rc == -ERANGE || buf->lb_len == 0) {
849 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
850 buf->lb_buf = info->mti_big_lmm;
851 buf->lb_len = info->mti_big_lmmsize;
856 if (rc < sizeof(*leh)) {
857 CERROR("short LinkEA on "DFID": rc = %d\n",
858 PFID(mdt_object_fid(o)), rc);
862 leh = (struct link_ea_header *) buf->lb_buf;
863 lee = (struct link_ea_entry *)(leh + 1);
864 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
865 leh->leh_magic = LINK_EA_MAGIC;
866 leh->leh_reccount = __swab32(leh->leh_reccount);
867 leh->leh_len = __swab64(leh->leh_len);
869 if (leh->leh_magic != LINK_EA_MAGIC)
871 if (leh->leh_reccount == 0)
874 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
875 fid_be_to_cpu(pfid, pfid);
880 int mdt_attr_get_complex(struct mdt_thread_info *info,
881 struct mdt_object *o, struct md_attr *ma)
883 const struct lu_env *env = info->mti_env;
884 struct md_object *next = mdt_object_child(o);
885 struct lu_buf *buf = &info->mti_buf;
886 int need = ma->ma_need;
893 if (mdt_object_exists(o) == 0)
894 GOTO(out, rc = -ENOENT);
895 mode = lu_object_attr(&next->mo_lu);
897 if (need & MA_INODE) {
898 ma->ma_need = MA_INODE;
899 rc = mo_attr_get(env, next, ma);
902 ma->ma_valid |= MA_INODE;
905 if (need & MA_PFID) {
906 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
908 ma->ma_valid |= MA_PFID;
909 /* ignore this error, parent fid is not mandatory */
913 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
914 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
919 if (need & MA_LMV && S_ISDIR(mode)) {
920 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
925 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
926 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
931 if (need & MA_HSM && S_ISREG(mode)) {
932 buf->lb_buf = info->mti_xattr_buf;
933 buf->lb_len = sizeof(info->mti_xattr_buf);
934 CLASSERT(sizeof(struct hsm_attrs) <=
935 sizeof(info->mti_xattr_buf));
936 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
937 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
939 ma->ma_valid |= MA_HSM;
940 else if (rc2 < 0 && rc2 != -ENODATA)
944 #ifdef CONFIG_FS_POSIX_ACL
945 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
946 buf->lb_buf = ma->ma_acl;
947 buf->lb_len = ma->ma_acl_size;
948 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
950 ma->ma_acl_size = rc2;
951 ma->ma_valid |= MA_ACL_DEF;
952 } else if (rc2 == -ENODATA) {
961 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
962 rc, ma->ma_valid, ma->ma_lmm);
966 static int mdt_getattr_internal(struct mdt_thread_info *info,
967 struct mdt_object *o, int ma_need)
969 struct md_object *next = mdt_object_child(o);
970 const struct mdt_body *reqbody = info->mti_body;
971 struct ptlrpc_request *req = mdt_info_req(info);
972 struct md_attr *ma = &info->mti_attr;
973 struct lu_attr *la = &ma->ma_attr;
974 struct req_capsule *pill = info->mti_pill;
975 const struct lu_env *env = info->mti_env;
976 struct mdt_body *repbody;
977 struct lu_buf *buffer = &info->mti_buf;
978 struct obd_export *exp = info->mti_exp;
982 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
983 RETURN(err_serious(-ENOMEM));
985 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
989 if (mdt_object_remote(o)) {
990 /* This object is located on remote node.*/
991 /* Return -ENOTSUPP for old client */
992 if (!mdt_is_dne_client(req->rq_export))
993 GOTO(out, rc = -ENOTSUPP);
995 repbody->mbo_fid1 = *mdt_object_fid(o);
996 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1000 if (reqbody->mbo_eadatasize > 0) {
1001 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1002 if (buffer->lb_buf == NULL)
1003 GOTO(out, rc = -EPROTO);
1004 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1007 buffer->lb_buf = NULL;
1009 ma_need &= ~(MA_LOV | MA_LMV);
1010 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1011 mdt_obd_name(info->mti_mdt),
1012 req->rq_export->exp_client_uuid.uuid);
1015 /* If it is dir object and client require MEA, then we got MEA */
1016 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1017 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1018 /* Assumption: MDT_MD size is enough for lmv size. */
1019 ma->ma_lmv = buffer->lb_buf;
1020 ma->ma_lmv_size = buffer->lb_len;
1021 ma->ma_need = MA_INODE;
1022 if (ma->ma_lmv_size > 0) {
1023 if (reqbody->mbo_valid & OBD_MD_MEA)
1024 ma->ma_need |= MA_LMV;
1025 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1026 ma->ma_need |= MA_LMV_DEF;
1029 ma->ma_lmm = buffer->lb_buf;
1030 ma->ma_lmm_size = buffer->lb_len;
1031 ma->ma_need = MA_INODE | MA_HSM;
1032 if (ma->ma_lmm_size > 0)
1033 ma->ma_need |= MA_LOV;
1036 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1037 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1038 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1039 /* get default stripe info for this dir. */
1040 ma->ma_need |= MA_LOV_DEF;
1042 ma->ma_need |= ma_need;
1044 rc = mdt_attr_get_complex(info, o, ma);
1046 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1047 "%s: getattr error for "DFID": rc = %d\n",
1048 mdt_obd_name(info->mti_mdt),
1049 PFID(mdt_object_fid(o)), rc);
1053 /* if file is released, check if a restore is running */
1054 if (ma->ma_valid & MA_HSM) {
1055 repbody->mbo_valid |= OBD_MD_TSTATE;
1056 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1057 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1058 repbody->mbo_t_state = MS_RESTORE;
1061 if (likely(ma->ma_valid & MA_INODE))
1062 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1066 if (mdt_body_has_lov(la, reqbody)) {
1067 if (ma->ma_valid & MA_LOV) {
1068 LASSERT(ma->ma_lmm_size);
1069 repbody->mbo_eadatasize = ma->ma_lmm_size;
1070 if (S_ISDIR(la->la_mode))
1071 repbody->mbo_valid |= OBD_MD_FLDIREA;
1073 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1074 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1076 if (ma->ma_valid & MA_LMV) {
1077 /* Return -ENOTSUPP for old client */
1078 if (!mdt_is_striped_client(req->rq_export))
1081 LASSERT(S_ISDIR(la->la_mode));
1082 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1083 repbody->mbo_eadatasize = ma->ma_lmv_size;
1084 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1086 if (ma->ma_valid & MA_LMV_DEF) {
1087 /* Return -ENOTSUPP for old client */
1088 if (!mdt_is_striped_client(req->rq_export))
1090 LASSERT(S_ISDIR(la->la_mode));
1091 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1092 repbody->mbo_eadatasize = ma->ma_lmv_size;
1093 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1094 OBD_MD_DEFAULT_MEA);
1096 } else if (S_ISLNK(la->la_mode) &&
1097 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1098 buffer->lb_buf = ma->ma_lmm;
1099 /* eadatasize from client includes NULL-terminator, so
1100 * there is no need to read it */
1101 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1102 rc = mo_readlink(env, next, buffer);
1103 if (unlikely(rc <= 0)) {
1104 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1105 mdt_obd_name(info->mti_mdt),
1106 PFID(mdt_object_fid(o)), rc);
1109 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1111 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1113 repbody->mbo_valid |= OBD_MD_LINKNAME;
1114 /* we need to report back size with NULL-terminator
1115 * because client expects that */
1116 repbody->mbo_eadatasize = rc + 1;
1117 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1118 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1119 "on "DFID ", expected %d\n",
1120 mdt_obd_name(info->mti_mdt),
1121 rc, PFID(mdt_object_fid(o)),
1122 reqbody->mbo_eadatasize - 1);
1123 /* NULL terminate */
1124 ((char *)ma->ma_lmm)[rc] = 0;
1126 /* If the total CDEBUG() size is larger than a page, it
1127 * will print a warning to the console, avoid this by
1128 * printing just the last part of the symlink. */
1129 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1130 print_limit < rc ? "..." : "", print_limit,
1131 (char *)ma->ma_lmm + rc - print_limit, rc);
1136 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1137 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1138 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1139 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1140 repbody->mbo_max_mdsize);
1143 #ifdef CONFIG_FS_POSIX_ACL
1144 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1145 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1146 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1147 if (IS_ERR(nodemap))
1148 RETURN(PTR_ERR(nodemap));
1150 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1151 nodemap_putref(nodemap);
1157 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1162 static int mdt_getattr(struct tgt_session_info *tsi)
1164 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1165 struct mdt_object *obj = info->mti_object;
1166 struct req_capsule *pill = info->mti_pill;
1167 struct mdt_body *reqbody;
1168 struct mdt_body *repbody;
1172 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1174 LASSERT(obj != NULL);
1175 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1177 /* Unlike intent case where we need to pre-fill out buffers early on
1178 * in intent policy for ldlm reasons, here we can have a much better
1179 * guess at EA size by just reading it from disk.
1180 * Exceptions are readdir and (missing) directory striping */
1182 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1183 /* No easy way to know how long is the symlink, but it cannot
1184 * be more than PATH_MAX, so we allocate +1 */
1187 /* A special case for fs ROOT: getattr there might fetch
1188 * default EA for entire fs, not just for this dir!
1190 } else if (lu_fid_eq(mdt_object_fid(obj),
1191 &info->mti_mdt->mdt_md_root_fid) &&
1192 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1193 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1195 /* Should the default strping be bigger, mdt_fix_reply
1196 * will reallocate */
1197 rc = DEF_REP_MD_SIZE;
1199 /* Read the actual EA size from disk */
1200 rc = mdt_attr_get_eabuf_size(info, obj);
1204 GOTO(out, rc = err_serious(rc));
1206 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1208 rc = req_capsule_server_pack(pill);
1209 if (unlikely(rc != 0))
1210 GOTO(out, rc = err_serious(rc));
1212 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1213 LASSERT(repbody != NULL);
1214 repbody->mbo_eadatasize = 0;
1215 repbody->mbo_aclsize = 0;
1217 rc = mdt_check_ucred(info);
1219 GOTO(out_shrink, rc);
1221 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1223 rc = mdt_getattr_internal(info, obj, 0);
1226 mdt_client_compatibility(info);
1227 rc2 = mdt_fix_reply(info);
1231 mdt_thread_info_fini(info);
1236 * Handler of layout intent RPC requiring the layout modification
1238 * \param[in] info thread environment
1239 * \param[in] obj object
1240 * \param[in] layout layout intent
1241 * \param[in] buf buffer containing client's lovea, could be empty
1243 * \retval 0 on success
1244 * \retval < 0 error code
1246 static int mdt_layout_change(struct mdt_thread_info *info,
1247 struct mdt_object *obj,
1248 struct layout_intent *layout,
1249 const struct lu_buf *buf)
1251 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1255 CDEBUG(D_INFO, "got layout change request from client: "
1256 "opc:%u flags:%#x extent[%#llx,%#llx)\n",
1257 layout->li_opc, layout->li_flags,
1258 layout->li_start, layout->li_end);
1259 if (layout->li_start >= layout->li_end) {
1260 CERROR("Recieved an invalid layout change range [%llu, %llu) "
1261 "for "DFID"\n", layout->li_start, layout->li_end,
1262 PFID(mdt_object_fid(obj)));
1266 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1267 GOTO(out, rc = -EINVAL);
1269 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1274 /* take layout lock to prepare layout change */
1275 mdt_lock_reg_init(lh, LCK_EX);
1276 rc = mdt_object_lock(info, obj, lh,
1277 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
1281 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout,
1284 mdt_object_unlock(info, obj, lh, 1);
1290 * Exchange MOF_LOV_CREATED flags between two objects after a
1291 * layout swap. No assumption is made on whether o1 or o2 have
1292 * created objects or not.
1294 * \param[in,out] o1 First swap layout object
1295 * \param[in,out] o2 Second swap layout object
1297 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1299 unsigned int o1_lov_created = o1->mot_lov_created;
1301 mutex_lock(&o1->mot_lov_mutex);
1302 mutex_lock(&o2->mot_lov_mutex);
1304 o1->mot_lov_created = o2->mot_lov_created;
1305 o2->mot_lov_created = o1_lov_created;
1307 mutex_unlock(&o2->mot_lov_mutex);
1308 mutex_unlock(&o1->mot_lov_mutex);
1311 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1313 struct mdt_thread_info *info;
1314 struct ptlrpc_request *req = tgt_ses_req(tsi);
1315 struct obd_export *exp = req->rq_export;
1316 struct mdt_object *o1, *o2, *o;
1317 struct mdt_lock_handle *lh1, *lh2;
1318 struct mdc_swap_layouts *msl;
1322 /* client does not support layout lock, so layout swaping
1324 * FIXME: there is a problem for old clients which don't support
1325 * layout lock yet. If those clients have already opened the file
1326 * they won't be notified at all so that old layout may still be
1327 * used to do IO. This can be fixed after file release is landed by
1328 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1329 if (!exp_connect_layout(exp))
1330 RETURN(-EOPNOTSUPP);
1332 info = tsi2mdt_info(tsi);
1334 if (info->mti_dlm_req != NULL)
1335 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1337 o1 = info->mti_object;
1338 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1339 &info->mti_body->mbo_fid2);
1341 GOTO(out, rc = PTR_ERR(o));
1343 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1344 GOTO(put, rc = -ENOENT);
1346 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1347 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1353 /* permission check. Make sure the calling process having permission
1354 * to write both files. */
1355 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1360 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1365 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1367 GOTO(put, rc = -EPROTO);
1369 lh1 = &info->mti_lh[MDT_LH_NEW];
1370 mdt_lock_reg_init(lh1, LCK_EX);
1371 lh2 = &info->mti_lh[MDT_LH_OLD];
1372 mdt_lock_reg_init(lh2, LCK_EX);
1374 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1375 MDS_INODELOCK_XATTR);
1379 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1380 MDS_INODELOCK_XATTR);
1384 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1385 mdt_object_child(o2), msl->msl_flags);
1389 mdt_swap_lov_flag(o1, o2);
1392 mdt_object_unlock(info, o2, lh2, rc);
1394 mdt_object_unlock(info, o1, lh1, rc);
1396 mdt_object_put(info->mti_env, o);
1398 mdt_thread_info_fini(info);
1402 static int mdt_raw_lookup(struct mdt_thread_info *info,
1403 struct mdt_object *parent,
1404 const struct lu_name *lname,
1405 struct ldlm_reply *ldlm_rep)
1407 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1411 LASSERT(!info->mti_cross_ref);
1413 /* Only got the fid of this obj by name */
1414 fid_zero(child_fid);
1415 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1416 lname, child_fid, &info->mti_spec);
1418 struct mdt_body *repbody;
1420 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1421 repbody->mbo_fid1 = *child_fid;
1422 repbody->mbo_valid = OBD_MD_FLID;
1423 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1424 } else if (rc == -ENOENT) {
1425 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1432 * UPDATE lock should be taken against parent, and be released before exit;
1433 * child_bits lock should be taken against child, and be returned back:
1434 * (1)normal request should release the child lock;
1435 * (2)intent request will grant the lock to client.
1437 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1438 struct mdt_lock_handle *lhc,
1440 struct ldlm_reply *ldlm_rep)
1442 struct ptlrpc_request *req = mdt_info_req(info);
1443 struct mdt_body *reqbody = NULL;
1444 struct mdt_object *parent = info->mti_object;
1445 struct mdt_object *child;
1446 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1447 struct lu_name *lname = NULL;
1448 struct mdt_lock_handle *lhp = NULL;
1449 struct ldlm_lock *lock;
1456 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1457 LASSERT(ergo(is_resent,
1458 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1463 if (info->mti_cross_ref) {
1464 /* Only getattr on the child. Parent is on another node. */
1465 mdt_set_disposition(info, ldlm_rep,
1466 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1468 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1470 PFID(mdt_object_fid(child)), ldlm_rep);
1472 rc = mdt_check_resent_lock(info, child, lhc);
1475 } else if (rc > 0) {
1476 mdt_lock_handle_init(lhc);
1477 mdt_lock_reg_init(lhc, LCK_PR);
1480 * Object's name is on another MDS, no lookup or layout
1481 * lock is needed here but update lock is.
1483 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1484 MDS_INODELOCK_LAYOUT);
1485 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1487 rc = mdt_object_lock(info, child, lhc, child_bits);
1492 /* Finally, we can get attr for child. */
1493 if (!mdt_object_exists(child)) {
1494 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1496 "remote object doesn't exist.");
1497 mdt_object_unlock(info, child, lhc, 1);
1501 rc = mdt_getattr_internal(info, child, 0);
1502 if (unlikely(rc != 0))
1503 mdt_object_unlock(info, child, lhc, 1);
1508 lname = &info->mti_name;
1509 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1511 if (lu_name_is_valid(lname)) {
1512 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1513 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1514 PNAME(lname), ldlm_rep);
1516 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1517 if (unlikely(reqbody == NULL))
1518 RETURN(err_serious(-EPROTO));
1520 *child_fid = reqbody->mbo_fid2;
1522 if (unlikely(!fid_is_sane(child_fid)))
1523 RETURN(err_serious(-EINVAL));
1525 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1527 PFID(mdt_object_fid(parent)),
1528 PFID(&reqbody->mbo_fid2), ldlm_rep);
1531 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1533 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1534 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1536 "Parent doesn't exist!");
1540 if (mdt_object_remote(parent)) {
1541 CERROR("%s: parent "DFID" is on remote target\n",
1542 mdt_obd_name(info->mti_mdt),
1543 PFID(mdt_object_fid(parent)));
1547 if (lu_name_is_valid(lname)) {
1548 /* Always allow to lookup ".." */
1549 if (unlikely(lname->ln_namelen == 2 &&
1550 lname->ln_name[0] == '.' &&
1551 lname->ln_name[1] == '.'))
1552 info->mti_spec.sp_permitted = 1;
1554 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1555 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1560 /* step 1: lock parent only if parent is a directory */
1561 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1562 lhp = &info->mti_lh[MDT_LH_PARENT];
1563 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1564 rc = mdt_object_lock(info, parent, lhp,
1565 MDS_INODELOCK_UPDATE);
1566 if (unlikely(rc != 0))
1570 /* step 2: lookup child's fid by name */
1571 fid_zero(child_fid);
1572 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1573 child_fid, &info->mti_spec);
1575 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1578 GOTO(out_parent, rc);
1581 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1584 *step 3: find the child object by fid & lock it.
1585 * regardless if it is local or remote.
1587 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1588 * set parent dir fid the same as child fid in getattr by fid case
1589 * we should not lu_object_find() the object again, could lead
1590 * to hung if there is a concurrent unlink destroyed the object.
1592 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1593 mdt_object_get(info->mti_env, parent);
1596 child = mdt_object_find(info->mti_env, info->mti_mdt,
1600 if (unlikely(IS_ERR(child)))
1601 GOTO(out_parent, rc = PTR_ERR(child));
1603 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1604 if (!mdt_object_exists(child)) {
1605 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1607 "Object doesn't exist!");
1608 GOTO(out_child, rc = -ENOENT);
1611 rc = mdt_check_resent_lock(info, child, lhc);
1613 GOTO(out_child, rc);
1614 } else if (rc > 0) {
1615 mdt_lock_handle_init(lhc);
1616 mdt_lock_reg_init(lhc, LCK_PR);
1619 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1620 mdt_object_exists(child) && !mdt_object_remote(child)) {
1621 struct md_attr *ma = &info->mti_attr;
1624 ma->ma_need = MA_INODE;
1625 rc = mdt_attr_get_complex(info, child, ma);
1626 if (unlikely(rc != 0))
1627 GOTO(out_child, rc);
1629 /* If the file has not been changed for some time, we
1630 * return not only a LOOKUP lock, but also an UPDATE
1631 * lock and this might save us RPC on later STAT. For
1632 * directories, it also let negative dentry cache start
1633 * working for this dir. */
1634 if (ma->ma_valid & MA_INODE &&
1635 ma->ma_attr.la_valid & LA_CTIME &&
1636 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1637 ma->ma_attr.la_ctime < cfs_time_current_sec())
1638 child_bits |= MDS_INODELOCK_UPDATE;
1641 /* layout lock must be granted in a best-effort way
1642 * for IT operations */
1643 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1644 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1645 exp_connect_layout(info->mti_exp) &&
1646 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1647 !mdt_object_remote(child) && ldlm_rep != NULL) {
1648 /* try to grant layout lock for regular file. */
1654 child_bits |= MDS_INODELOCK_LAYOUT;
1655 /* try layout lock, it may fail to be granted due to
1656 * contention at LOOKUP or UPDATE */
1657 if (!mdt_object_lock_try(info, child, lhc,
1659 child_bits &= ~MDS_INODELOCK_LAYOUT;
1660 LASSERT(child_bits != 0);
1661 rc = mdt_object_lock(info, child, lhc,
1667 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1668 * client will enqueue the lock to the remote MDT */
1669 if (mdt_object_remote(child))
1670 child_bits &= ~MDS_INODELOCK_UPDATE;
1671 rc = mdt_object_lock(info, child, lhc, child_bits);
1673 if (unlikely(rc != 0))
1674 GOTO(out_child, rc);
1677 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1679 /* finally, we can get attr for child. */
1680 rc = mdt_getattr_internal(info, child, ma_need);
1681 if (unlikely(rc != 0)) {
1682 mdt_object_unlock(info, child, lhc, 1);
1684 /* Debugging code. */
1685 LDLM_DEBUG(lock, "Returning lock to client");
1686 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1687 &lock->l_resource->lr_name),
1688 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1689 PLDLMRES(lock->l_resource),
1690 PFID(mdt_object_fid(child)));
1693 LDLM_LOCK_PUT(lock);
1697 mdt_object_put(info->mti_env, child);
1700 mdt_object_unlock(info, parent, lhp, 1);
1704 /* normal handler: should release the child lock */
1705 static int mdt_getattr_name(struct tgt_session_info *tsi)
1707 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1708 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1709 struct mdt_body *reqbody;
1710 struct mdt_body *repbody;
1714 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1715 LASSERT(reqbody != NULL);
1716 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1717 LASSERT(repbody != NULL);
1719 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1720 repbody->mbo_eadatasize = 0;
1721 repbody->mbo_aclsize = 0;
1723 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1725 GOTO(out_shrink, rc);
1727 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1728 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1729 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1730 lhc->mlh_reg_lh.cookie = 0;
1732 mdt_exit_ucred(info);
1735 mdt_client_compatibility(info);
1736 rc2 = mdt_fix_reply(info);
1739 mdt_thread_info_fini(info);
1743 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1744 void *karg, void __user *uarg);
1746 static int mdt_set_info(struct tgt_session_info *tsi)
1748 struct ptlrpc_request *req = tgt_ses_req(tsi);
1751 int keylen, vallen, rc = 0;
1755 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1757 DEBUG_REQ(D_HA, req, "no set_info key");
1758 RETURN(err_serious(-EFAULT));
1761 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1764 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1766 DEBUG_REQ(D_HA, req, "no set_info val");
1767 RETURN(err_serious(-EFAULT));
1770 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1773 /* Swab any part of val you need to here */
1774 if (KEY_IS(KEY_READ_ONLY)) {
1775 spin_lock(&req->rq_export->exp_lock);
1777 *exp_connect_flags_ptr(req->rq_export) |=
1780 *exp_connect_flags_ptr(req->rq_export) &=
1781 ~OBD_CONNECT_RDONLY;
1782 spin_unlock(&req->rq_export->exp_lock);
1783 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1784 struct changelog_setinfo *cs = val;
1786 if (vallen != sizeof(*cs)) {
1787 CERROR("%s: bad changelog_clear setinfo size %d\n",
1788 tgt_name(tsi->tsi_tgt), vallen);
1791 if (ptlrpc_req_need_swab(req)) {
1792 __swab64s(&cs->cs_recno);
1793 __swab32s(&cs->cs_id);
1796 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1798 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1800 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1807 static int mdt_readpage(struct tgt_session_info *tsi)
1809 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1810 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1811 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1812 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1813 struct mdt_body *repbody;
1819 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1820 RETURN(err_serious(-ENOMEM));
1822 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1823 if (repbody == NULL || reqbody == NULL)
1824 RETURN(err_serious(-EFAULT));
1827 * prepare @rdpg before calling lower layers and transfer itself. Here
1828 * reqbody->size contains offset of where to start to read and
1829 * reqbody->nlink contains number bytes to read.
1831 rdpg->rp_hash = reqbody->mbo_size;
1832 if (rdpg->rp_hash != reqbody->mbo_size) {
1833 CERROR("Invalid hash: %#llx != %#llx\n",
1834 rdpg->rp_hash, reqbody->mbo_size);
1838 rdpg->rp_attrs = reqbody->mbo_mode;
1839 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1840 rdpg->rp_attrs |= LUDA_64BITHASH;
1841 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1842 exp_max_brw_size(tsi->tsi_exp));
1843 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1845 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1846 if (rdpg->rp_pages == NULL)
1849 for (i = 0; i < rdpg->rp_npages; ++i) {
1850 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1851 if (rdpg->rp_pages[i] == NULL)
1852 GOTO(free_rdpg, rc = -ENOMEM);
1855 /* call lower layers to fill allocated pages with directory data */
1856 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1858 GOTO(free_rdpg, rc);
1860 /* send pages to client */
1861 rc = tgt_sendpage(tsi, rdpg, rc);
1866 for (i = 0; i < rdpg->rp_npages; i++)
1867 if (rdpg->rp_pages[i] != NULL)
1868 __free_page(rdpg->rp_pages[i]);
1869 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1871 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1877 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1879 struct lu_ucred *uc = mdt_ucred_check(info);
1880 struct lu_attr *attr = &info->mti_attr.ma_attr;
1885 if (op != REINT_SETATTR) {
1886 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1887 attr->la_uid = uc->uc_fsuid;
1888 /* for S_ISGID, inherit gid from his parent, such work will be
1889 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1890 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1891 attr->la_gid = uc->uc_fsgid;
1897 static int mdt_reint_internal(struct mdt_thread_info *info,
1898 struct mdt_lock_handle *lhc,
1901 struct req_capsule *pill = info->mti_pill;
1902 struct mdt_body *repbody;
1907 rc = mdt_reint_unpack(info, op);
1909 CERROR("Can't unpack reint, rc %d\n", rc);
1910 RETURN(err_serious(rc));
1913 /* for replay (no_create) lmm is not needed, client has it already */
1914 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1915 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1918 /* llog cookies are always 0, the field is kept for compatibility */
1919 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1920 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1922 rc = req_capsule_server_pack(pill);
1924 CERROR("Can't pack response, rc %d\n", rc);
1925 RETURN(err_serious(rc));
1928 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1929 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1931 repbody->mbo_eadatasize = 0;
1932 repbody->mbo_aclsize = 0;
1935 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1937 /* for replay no cookkie / lmm need, because client have this already */
1938 if (info->mti_spec.no_create)
1939 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1940 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1942 rc = mdt_init_ucred_reint(info);
1944 GOTO(out_shrink, rc);
1946 rc = mdt_fix_attr_ucred(info, op);
1948 GOTO(out_ucred, rc = err_serious(rc));
1950 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1952 GOTO(out_ucred, rc);
1953 } else if (rc == 1) {
1954 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1955 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1956 GOTO(out_ucred, rc);
1958 rc = mdt_reint_rec(info, lhc);
1961 mdt_exit_ucred(info);
1963 mdt_client_compatibility(info);
1964 rc2 = mdt_fix_reply(info);
1970 static long mdt_reint_opcode(struct ptlrpc_request *req,
1971 const struct req_format **fmt)
1973 struct mdt_device *mdt;
1974 struct mdt_rec_reint *rec;
1977 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1979 opc = rec->rr_opcode;
1980 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1981 if (opc < REINT_MAX && fmt[opc] != NULL)
1982 req_capsule_extend(&req->rq_pill, fmt[opc]);
1984 mdt = mdt_exp2dev(req->rq_export);
1985 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1986 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1987 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1988 opc = err_serious(-EFAULT);
1991 opc = err_serious(-EFAULT);
1996 static int mdt_reint(struct tgt_session_info *tsi)
2000 static const struct req_format *reint_fmts[REINT_MAX] = {
2001 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2002 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2003 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2004 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2005 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2006 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2007 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2008 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2009 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
2014 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2016 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2018 * No lock possible here from client to pass it to reint code
2021 rc = mdt_reint_internal(info, NULL, opc);
2022 mdt_thread_info_fini(info);
2027 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2031 /* this should sync the whole device */
2032 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2034 struct dt_device *dt = mdt->mdt_bottom;
2038 rc = dt->dd_ops->dt_sync(env, dt);
2042 /* this should sync this object */
2043 static int mdt_object_sync(struct mdt_thread_info *info)
2045 struct md_object *next;
2049 if (!mdt_object_exists(info->mti_object)) {
2050 CWARN("%s: non existing object "DFID": rc = %d\n",
2051 mdt_obd_name(info->mti_mdt),
2052 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
2055 next = mdt_object_child(info->mti_object);
2056 rc = mo_object_sync(info->mti_env, next);
2061 static int mdt_sync(struct tgt_session_info *tsi)
2063 struct ptlrpc_request *req = tgt_ses_req(tsi);
2064 struct req_capsule *pill = tsi->tsi_pill;
2065 struct mdt_body *body;
2070 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2071 RETURN(err_serious(-ENOMEM));
2073 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2074 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2076 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2078 /* sync an object */
2079 rc = mdt_object_sync(info);
2081 const struct lu_fid *fid;
2082 struct lu_attr *la = &info->mti_attr.ma_attr;
2084 info->mti_attr.ma_need = MA_INODE;
2085 info->mti_attr.ma_valid = 0;
2086 rc = mdt_attr_get_complex(info, info->mti_object,
2089 body = req_capsule_server_get(pill,
2091 fid = mdt_object_fid(info->mti_object);
2092 mdt_pack_attr2body(info, body, la, fid);
2095 mdt_thread_info_fini(info);
2098 mdt_counter_incr(req, LPROC_MDT_SYNC);
2104 * Handle quota control requests to consult current usage/limit, but also
2105 * to configure quota enforcement
2107 static int mdt_quotactl(struct tgt_session_info *tsi)
2109 struct obd_export *exp = tsi->tsi_exp;
2110 struct req_capsule *pill = tsi->tsi_pill;
2111 struct obd_quotactl *oqctl, *repoqc;
2113 struct mdt_device *mdt = mdt_exp2dev(exp);
2114 struct lu_device *qmt = mdt->mdt_qmt_dev;
2115 struct lu_nodemap *nodemap;
2118 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2120 RETURN(err_serious(-EPROTO));
2122 rc = req_capsule_server_pack(pill);
2124 RETURN(err_serious(rc));
2126 nodemap = nodemap_get_from_exp(exp);
2127 if (IS_ERR(nodemap))
2128 RETURN(PTR_ERR(nodemap));
2130 switch (oqctl->qc_cmd) {
2131 /* master quotactl */
2134 if (!nodemap_can_setquota(nodemap))
2135 GOTO(out_nodemap, rc = -EPERM);
2139 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2140 /* slave quotactl */
2145 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2146 GOTO(out_nodemap, rc = -EFAULT);
2150 switch (oqctl->qc_type) {
2152 id = nodemap_map_id(nodemap, NODEMAP_UID,
2153 NODEMAP_CLIENT_TO_FS, id);
2156 id = nodemap_map_id(nodemap, NODEMAP_GID,
2157 NODEMAP_CLIENT_TO_FS, id);
2160 /* todo: check/map project id */
2164 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2166 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2168 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2170 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2171 barrier_exit(tsi->tsi_tgt->lut_bottom);
2173 if (oqctl->qc_id != id)
2174 swap(oqctl->qc_id, id);
2176 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2177 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2178 RETURN(-EINPROGRESS);
2181 switch (oqctl->qc_cmd) {
2187 /* forward quotactl request to QMT */
2188 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2193 /* slave quotactl */
2194 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2199 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2200 GOTO(out_nodemap, rc = -EFAULT);
2203 if (oqctl->qc_id != id)
2204 swap(oqctl->qc_id, id);
2211 nodemap_putref(nodemap);
2216 /** clone llog ctxt from child (mdd)
2217 * This allows remote llog (replicator) access.
2218 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2219 * context was originally set up, or we can handle them directly.
2220 * I choose the latter, but that means I need any llog
2221 * contexts set up by child to be accessable by the mdt. So we clone the
2222 * context into our context list here.
2224 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2227 struct md_device *next = mdt->mdt_child;
2228 struct llog_ctxt *ctxt;
2231 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2234 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2235 if (rc || ctxt == NULL) {
2239 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2241 CERROR("Can't set mdt ctxt %d\n", rc);
2246 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2247 struct mdt_device *mdt, int idx)
2249 struct llog_ctxt *ctxt;
2251 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2254 /* Put once for the get we just did, and once for the clone */
2255 llog_ctxt_put(ctxt);
2256 llog_ctxt_put(ctxt);
2261 * sec context handlers
2263 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2265 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2271 * quota request handlers
2273 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2275 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2276 struct lu_device *qmt = mdt->mdt_qmt_dev;
2281 RETURN(err_serious(-EOPNOTSUPP));
2283 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2287 struct mdt_object *mdt_object_new(const struct lu_env *env,
2288 struct mdt_device *d,
2289 const struct lu_fid *f)
2291 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2292 struct lu_object *o;
2293 struct mdt_object *m;
2296 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2297 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2298 if (unlikely(IS_ERR(o)))
2299 m = (struct mdt_object *)o;
2305 struct mdt_object *mdt_object_find(const struct lu_env *env,
2306 struct mdt_device *d,
2307 const struct lu_fid *f)
2309 struct lu_object *o;
2310 struct mdt_object *m;
2313 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2314 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2315 if (unlikely(IS_ERR(o)))
2316 m = (struct mdt_object *)o;
2324 * Asyncronous commit for mdt device.
2326 * Pass asynchonous commit call down the MDS stack.
2328 * \param env environment
2329 * \param mdt the mdt device
2331 static void mdt_device_commit_async(const struct lu_env *env,
2332 struct mdt_device *mdt)
2334 struct dt_device *dt = mdt->mdt_bottom;
2338 rc = dt->dd_ops->dt_commit_async(env, dt);
2339 if (unlikely(rc != 0))
2340 CWARN("%s: async commit start failed: rc = %d\n",
2341 mdt_obd_name(mdt), rc);
2342 atomic_inc(&mdt->mdt_async_commit_count);
2347 * Mark the lock as "synchonous".
2349 * Mark the lock to deffer transaction commit to the unlock time.
2351 * \param lock the lock to mark as "synchonous"
2353 * \see mdt_is_lock_sync
2354 * \see mdt_save_lock
2356 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2358 lock->l_ast_data = (void*)1;
2362 * Check whehter the lock "synchonous" or not.
2364 * \param lock the lock to check
2365 * \retval 1 the lock is "synchonous"
2366 * \retval 0 the lock isn't "synchronous"
2368 * \see mdt_set_lock_sync
2369 * \see mdt_save_lock
2371 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2373 return lock->l_ast_data != NULL;
2377 * Blocking AST for mdt locks.
2379 * Starts transaction commit if in case of COS lock conflict or
2380 * deffers such a commit to the mdt_save_lock.
2382 * \param lock the lock which blocks a request or cancelling lock
2383 * \param desc unused
2384 * \param data unused
2385 * \param flag indicates whether this cancelling or blocking callback
2387 * \see ldlm_blocking_ast_nocheck
2389 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2390 void *data, int flag)
2392 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2393 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2394 bool commit_async = false;
2398 if (flag == LDLM_CB_CANCELING)
2401 lock_res_and_lock(lock);
2402 if (lock->l_blocking_ast != mdt_blocking_ast) {
2403 unlock_res_and_lock(lock);
2406 /* There is no lock conflict if l_blocking_lock == NULL,
2407 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2408 * when the last reference to a local lock was released */
2409 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2410 lock->l_blocking_lock != NULL) {
2411 if (mdt_cos_is_enabled(mdt)) {
2412 if (lock->l_client_cookie !=
2413 lock->l_blocking_lock->l_client_cookie)
2414 mdt_set_lock_sync(lock);
2415 } else if (mdt_slc_is_enabled(mdt) &&
2416 ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2417 mdt_set_lock_sync(lock);
2419 * we may do extra commit here, but there is a small
2420 * window to miss a commit: lock was unlocked (saved),
2421 * then a conflict lock queued and we come here, but
2422 * REP-ACK not received, so lock was not converted to
2424 * Fortunately this window is quite small, so the
2425 * extra commit should be rare (not to say distributed
2426 * operation is rare too).
2428 commit_async = true;
2430 } else if (lock->l_req_mode == LCK_COS &&
2431 lock->l_blocking_lock != NULL) {
2432 commit_async = true;
2435 rc = ldlm_blocking_ast_nocheck(lock);
2440 rc = lu_env_init(&env, LCT_LOCAL);
2441 if (unlikely(rc != 0))
2442 CWARN("%s: lu_env initialization failed, cannot "
2443 "start asynchronous commit: rc = %d\n",
2446 mdt_device_commit_async(&env, mdt);
2453 * Blocking AST for cross-MDT lock
2455 * Discard lock from uncommitted_slc_locks and cancel it.
2457 * \param lock the lock which blocks a request or cancelling lock
2458 * \param desc unused
2459 * \param data unused
2460 * \param flag indicates whether this cancelling or blocking callback
2461 * \retval 0 on success
2462 * \retval negative number on error
2464 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2465 void *data, int flag)
2471 case LDLM_CB_BLOCKING: {
2472 struct lustre_handle lockh;
2474 ldlm_lock2handle(lock, &lockh);
2475 rc = ldlm_cli_cancel(&lockh,
2476 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2478 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2483 case LDLM_CB_CANCELING: {
2484 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2485 struct mdt_device *mdt =
2486 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2488 LDLM_DEBUG(lock, "Revoke remote lock\n");
2490 /* discard slc lock here so that it can be cleaned anytime,
2491 * especially for cleanup_resource() */
2492 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2494 /* once we cache lock, l_ast_data is set to mdt_object */
2495 if (lock->l_ast_data != NULL) {
2496 struct mdt_object *mo = lock->l_ast_data;
2499 rc = lu_env_init(&env, LCT_MD_THREAD);
2500 if (unlikely(rc != 0)) {
2501 CWARN("%s: lu_env initialization failed, object"
2502 "%p "DFID" is leaked!\n",
2504 PFID(mdt_object_fid(mo)));
2508 if (lock->l_policy_data.l_inodebits.bits &
2509 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2510 rc = mo_invalidate(&env, mdt_object_child(mo));
2511 mo->mot_cache_attr = 0;
2513 mdt_object_put(&env, mo);
2525 int mdt_check_resent_lock(struct mdt_thread_info *info,
2526 struct mdt_object *mo,
2527 struct mdt_lock_handle *lhc)
2529 /* the lock might already be gotten in ldlm_handle_enqueue() */
2530 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2531 struct ptlrpc_request *req = mdt_info_req(info);
2532 struct ldlm_lock *lock;
2534 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2535 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2537 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2538 * a resend case, however, it could be already destroyed
2539 * due to client eviction or a raced cancel RPC. */
2540 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2541 lhc->mlh_reg_lh.cookie);
2545 if (!fid_res_name_eq(mdt_object_fid(mo),
2546 &lock->l_resource->lr_name)) {
2547 CWARN("%s: Although resent, but still not "
2548 "get child lock:"DFID"\n",
2549 info->mti_exp->exp_obd->obd_name,
2550 PFID(mdt_object_fid(mo)));
2551 LDLM_LOCK_PUT(lock);
2554 LDLM_LOCK_PUT(lock);
2560 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2561 const struct lu_fid *fid, struct lustre_handle *lh,
2562 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2565 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2566 union ldlm_policy_data *policy = &mti->mti_policy;
2567 struct ldlm_res_id *res_id = &mti->mti_res_id;
2571 LASSERT(mdt_object_remote(o));
2573 fid_build_reg_res_name(fid, res_id);
2575 memset(einfo, 0, sizeof(*einfo));
2576 einfo->ei_type = LDLM_IBITS;
2577 einfo->ei_mode = mode;
2578 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2579 einfo->ei_cb_cp = ldlm_completion_ast;
2580 einfo->ei_enq_slave = 0;
2581 einfo->ei_res_id = res_id;
2583 einfo->ei_nonblock = 1;
2586 * if we cache lock, couple lock with mdt_object, so that object
2587 * can be easily found in lock ASTs.
2589 mdt_object_get(mti->mti_env, o);
2590 einfo->ei_cbdata = o;
2593 memset(policy, 0, sizeof(*policy));
2594 policy->l_inodebits.bits = ibits;
2596 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2598 if (rc < 0 && cache) {
2599 mdt_object_put(mti->mti_env, o);
2600 einfo->ei_cbdata = NULL;
2605 static int mdt_object_local_lock(struct mdt_thread_info *info,
2606 struct mdt_object *o,
2607 struct mdt_lock_handle *lh, __u64 ibits,
2608 bool nonblock, bool cos_incompat)
2610 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2611 union ldlm_policy_data *policy = &info->mti_policy;
2612 struct ldlm_res_id *res_id = &info->mti_res_id;
2617 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2618 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2619 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2620 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2623 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2624 lh->mlh_reg_mode == LCK_EX);
2625 dlmflags |= LDLM_FL_COS_INCOMPAT;
2626 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2627 dlmflags |= LDLM_FL_COS_ENABLED;
2630 /* Only enqueue LOOKUP lock for remote object */
2631 if (mdt_object_remote(o))
2632 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2634 if (lh->mlh_type == MDT_PDO_LOCK) {
2635 /* check for exists after object is locked */
2636 if (mdt_object_exists(o) == 0) {
2637 /* Non-existent object shouldn't have PDO lock */
2640 /* Non-dir object shouldn't have PDO lock */
2641 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2646 memset(policy, 0, sizeof(*policy));
2647 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2649 dlmflags |= LDLM_FL_ATOMIC_CB;
2651 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2654 * Take PDO lock on whole directory and build correct @res_id for lock
2655 * on part of directory.
2657 if (lh->mlh_pdo_hash != 0) {
2658 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2659 mdt_lock_pdo_mode(info, o, lh);
2660 if (lh->mlh_pdo_mode != LCK_NL) {
2662 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2663 * is never going to be sent to client and we do not
2664 * want it slowed down due to possible cancels.
2666 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2667 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2668 policy, res_id, dlmflags,
2669 info->mti_exp == NULL ? NULL :
2670 &info->mti_exp->exp_handle.h_cookie);
2671 if (unlikely(rc != 0))
2672 GOTO(out_unlock, rc);
2676 * Finish res_id initializing by name hash marking part of
2677 * directory which is taking modification.
2679 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2682 policy->l_inodebits.bits = ibits;
2685 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2686 * going to be sent to client. If it is - mdt_intent_policy() path will
2687 * fix it up and turn FL_LOCAL flag off.
2689 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2690 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2691 info->mti_exp == NULL ? NULL :
2692 &info->mti_exp->exp_handle.h_cookie);
2695 mdt_object_unlock(info, o, lh, 1);
2696 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2697 lh->mlh_pdo_hash != 0 &&
2698 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2699 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2705 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2706 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2709 struct mdt_lock_handle *local_lh = NULL;
2713 if (!mdt_object_remote(o)) {
2714 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2719 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2720 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2721 MDS_INODELOCK_XATTR);
2723 /* Only enqueue LOOKUP lock for remote object */
2724 if (ibits & MDS_INODELOCK_LOOKUP) {
2725 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2726 nonblock, cos_incompat);
2733 if (ibits & MDS_INODELOCK_UPDATE) {
2734 /* Sigh, PDO needs to enqueue 2 locks right now, but
2735 * enqueue RPC can only request 1 lock, to avoid extra
2736 * RPC, so it will instead enqueue EX lock for remote
2737 * object anyway XXX*/
2738 if (lh->mlh_type == MDT_PDO_LOCK &&
2739 lh->mlh_pdo_hash != 0) {
2740 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2741 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2742 PFID(mdt_object_fid(o)));
2743 lh->mlh_pdo_hash = 0;
2744 lh->mlh_rreg_mode = LCK_EX;
2745 lh->mlh_type = MDT_REG_LOCK;
2747 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2750 MDS_INODELOCK_UPDATE, nonblock,
2752 if (rc != ELDLM_OK) {
2753 if (local_lh != NULL)
2754 mdt_object_unlock(info, o, local_lh, rc);
2762 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2763 struct mdt_lock_handle *lh, __u64 ibits)
2765 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2768 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2769 struct mdt_lock_handle *lh, __u64 ibits,
2772 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2773 return mdt_object_lock_internal(info, o, lh, ibits, false,
2777 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2778 struct mdt_lock_handle *lh, __u64 ibits)
2780 struct mdt_lock_handle tmp = *lh;
2783 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2790 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2791 struct mdt_object *o, struct mdt_lock_handle *lh,
2792 __u64 ibits, bool cos_incompat)
2794 struct mdt_lock_handle tmp = *lh;
2797 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2798 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2806 * Save a lock within request object.
2808 * Keep the lock referenced until whether client ACK or transaction
2809 * commit happens or release the lock immediately depending on input
2810 * parameters. If COS is ON, a write lock is converted to COS lock
2813 * \param info thead info object
2814 * \param h lock handle
2815 * \param mode lock mode
2816 * \param decref force immediate lock releasing
2818 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2819 enum ldlm_mode mode, int decref)
2823 if (lustre_handle_is_used(h)) {
2824 if (decref || !info->mti_has_trans ||
2825 !(mode & (LCK_PW | LCK_EX))) {
2826 mdt_fid_unlock(h, mode);
2828 struct mdt_device *mdt = info->mti_mdt;
2829 struct ldlm_lock *lock = ldlm_handle2lock(h);
2830 struct ptlrpc_request *req = mdt_info_req(info);
2831 bool cos = mdt_cos_is_enabled(mdt);
2832 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
2834 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2837 /* there is no request if mdt_object_unlock() is called
2838 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2839 if (likely(req != NULL)) {
2840 LDLM_DEBUG(lock, "save lock request %p reply "
2841 "state %p transno %lld\n", req,
2842 req->rq_reply_state, req->rq_transno);
2844 ldlm_lock_downgrade(lock, LCK_COS);
2847 ptlrpc_save_lock(req, h, mode, cos,
2850 mdt_fid_unlock(h, mode);
2852 if (mdt_is_lock_sync(lock)) {
2853 CDEBUG(D_HA, "found sync-lock,"
2854 " async commit started\n");
2855 mdt_device_commit_async(info->mti_env,
2858 LDLM_LOCK_PUT(lock);
2867 * Save cross-MDT lock in uncommitted_slc_locks
2869 * Keep the lock referenced until transaction commit happens or release the lock
2870 * immediately depending on input parameters.
2872 * \param info thead info object
2873 * \param h lock handle
2874 * \param mode lock mode
2875 * \param decref force immediate lock releasing
2877 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2878 struct mdt_object *o, struct lustre_handle *h,
2879 enum ldlm_mode mode, int decref)
2883 if (lustre_handle_is_used(h)) {
2884 struct ldlm_lock *lock = ldlm_handle2lock(h);
2887 (lock->l_policy_data.l_inodebits.bits &
2888 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2889 mo_invalidate(info->mti_env, mdt_object_child(o));
2891 if (decref || !info->mti_has_trans ||
2892 !(mode & (LCK_PW | LCK_EX))) {
2893 ldlm_lock_decref_and_cancel(h, mode);
2894 LDLM_LOCK_PUT(lock);
2896 struct ptlrpc_request *req = mdt_info_req(info);
2898 LASSERT(req != NULL);
2899 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2901 ldlm_lock_decref(h, mode);
2910 * Unlock mdt object.
2912 * Immeditely release the regular lock and the PDO lock or save the
2913 * lock in request and keep them referenced until client ACK or
2914 * transaction commit.
2916 * \param info thread info object
2917 * \param o mdt object
2918 * \param lh mdt lock handle referencing regular and PDO locks
2919 * \param decref force immediate lock releasing
2921 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2923 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2924 struct mdt_lock_handle *lh, int decref)
2928 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2929 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2930 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2936 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2937 const struct lu_fid *f,
2938 struct mdt_lock_handle *lh,
2941 struct mdt_object *o;
2943 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2947 rc = mdt_object_lock(info, o, lh, ibits);
2949 mdt_object_put(info->mti_env, o);
2956 void mdt_object_unlock_put(struct mdt_thread_info * info,
2957 struct mdt_object * o,
2958 struct mdt_lock_handle *lh,
2961 mdt_object_unlock(info, o, lh, decref);
2962 mdt_object_put(info->mti_env, o);
2966 * Generic code handling requests that have struct mdt_body passed in:
2968 * - extract mdt_body from request and save it in @info, if present;
2970 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2973 * - if HABEO_CORPUS flag is set for this request type check whether object
2974 * actually exists on storage (lu_object_exists()).
2977 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2979 const struct mdt_body *body;
2980 struct mdt_object *obj;
2981 const struct lu_env *env;
2982 struct req_capsule *pill;
2986 env = info->mti_env;
2987 pill = info->mti_pill;
2989 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2993 if (!(body->mbo_valid & OBD_MD_FLID))
2996 if (!fid_is_sane(&body->mbo_fid1)) {
2997 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3001 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3003 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3004 mdt_object_put(env, obj);
3007 info->mti_object = obj;
3016 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3018 struct req_capsule *pill = info->mti_pill;
3022 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3023 rc = mdt_body_unpack(info, flags);
3027 if (rc == 0 && (flags & HABEO_REFERO)) {
3029 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3030 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3032 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3033 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3036 rc = req_capsule_server_pack(pill);
3041 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3043 lh->mlh_type = MDT_NUL_LOCK;
3044 lh->mlh_reg_lh.cookie = 0ull;
3045 lh->mlh_reg_mode = LCK_MINMODE;
3046 lh->mlh_pdo_lh.cookie = 0ull;
3047 lh->mlh_pdo_mode = LCK_MINMODE;
3048 lh->mlh_rreg_lh.cookie = 0ull;
3049 lh->mlh_rreg_mode = LCK_MINMODE;
3052 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3054 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3055 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3059 * Initialize fields of struct mdt_thread_info. Other fields are left in
3060 * uninitialized state, because it's too expensive to zero out whole
3061 * mdt_thread_info (> 1K) on each request arrival.
3063 void mdt_thread_info_init(struct ptlrpc_request *req,
3064 struct mdt_thread_info *info)
3068 info->mti_pill = &req->rq_pill;
3071 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3072 mdt_lock_handle_init(&info->mti_lh[i]);
3074 /* mdt device: it can be NULL while CONNECT */
3075 if (req->rq_export) {
3076 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3077 info->mti_exp = req->rq_export;
3079 info->mti_mdt = NULL;
3080 info->mti_env = req->rq_svc_thread->t_env;
3081 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3083 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3084 info->mti_big_buf = LU_BUF_NULL;
3085 info->mti_body = NULL;
3086 info->mti_object = NULL;
3087 info->mti_dlm_req = NULL;
3088 info->mti_has_trans = 0;
3089 info->mti_cross_ref = 0;
3090 info->mti_opdata = 0;
3091 info->mti_big_lmm_used = 0;
3093 info->mti_spec.no_create = 0;
3094 info->mti_spec.sp_rm_entry = 0;
3095 info->mti_spec.sp_permitted = 0;
3096 info->mti_spec.sp_migrate_close = 0;
3098 info->mti_spec.u.sp_ea.eadata = NULL;
3099 info->mti_spec.u.sp_ea.eadatalen = 0;
3102 void mdt_thread_info_fini(struct mdt_thread_info *info)
3106 if (info->mti_object != NULL) {
3107 mdt_object_put(info->mti_env, info->mti_object);
3108 info->mti_object = NULL;
3111 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3112 mdt_lock_handle_fini(&info->mti_lh[i]);
3113 info->mti_env = NULL;
3114 info->mti_pill = NULL;
3115 info->mti_exp = NULL;
3117 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3118 lu_buf_free(&info->mti_big_buf);
3121 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3123 struct mdt_thread_info *mti;
3125 mti = mdt_th_info(tsi->tsi_env);
3126 LASSERT(mti != NULL);
3128 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3129 if (tsi->tsi_corpus != NULL) {
3130 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3131 lu_object_get(tsi->tsi_corpus);
3133 mti->mti_body = tsi->tsi_mdt_body;
3134 mti->mti_dlm_req = tsi->tsi_dlm_req;
3139 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3141 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3143 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3144 set_current_state(TASK_UNINTERRUPTIBLE);
3145 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3148 return tgt_connect(tsi);
3166 static int mdt_intent_getattr(enum mdt_it_code opcode,
3167 struct mdt_thread_info *info,
3168 struct ldlm_lock **,
3171 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3172 struct mdt_thread_info *info,
3173 struct ldlm_lock **lockp,
3176 static int mdt_intent_layout(enum mdt_it_code opcode,
3177 struct mdt_thread_info *info,
3178 struct ldlm_lock **,
3180 static int mdt_intent_reint(enum mdt_it_code opcode,
3181 struct mdt_thread_info *info,
3182 struct ldlm_lock **,
3185 static struct mdt_it_flavor {
3186 const struct req_format *it_fmt;
3188 int (*it_act)(enum mdt_it_code ,
3189 struct mdt_thread_info *,
3190 struct ldlm_lock **,
3193 } mdt_it_flavor[] = {
3195 .it_fmt = &RQF_LDLM_INTENT,
3196 /*.it_flags = HABEO_REFERO,*/
3198 .it_act = mdt_intent_reint,
3199 .it_reint = REINT_OPEN
3202 .it_fmt = &RQF_LDLM_INTENT,
3204 * OCREAT is not a MUTABOR request as if the file
3206 * We do the extra check of OBD_CONNECT_RDONLY in
3207 * mdt_reint_open() when we really need to create
3211 .it_act = mdt_intent_reint,
3212 .it_reint = REINT_OPEN
3215 .it_fmt = &RQF_LDLM_INTENT,
3216 .it_flags = MUTABOR,
3217 .it_act = mdt_intent_reint,
3218 .it_reint = REINT_CREATE
3220 [MDT_IT_GETATTR] = {
3221 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3222 .it_flags = HABEO_REFERO,
3223 .it_act = mdt_intent_getattr
3225 [MDT_IT_READDIR] = {
3231 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3232 .it_flags = HABEO_REFERO,
3233 .it_act = mdt_intent_getattr
3236 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3237 .it_flags = MUTABOR,
3239 .it_reint = REINT_UNLINK
3243 .it_flags = MUTABOR,
3246 [MDT_IT_GETXATTR] = {
3247 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3248 .it_flags = HABEO_CORPUS,
3249 .it_act = mdt_intent_getxattr
3252 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3254 .it_act = mdt_intent_layout
3259 mdt_intent_lock_replace(struct mdt_thread_info *info,
3260 struct ldlm_lock **lockp,
3261 struct mdt_lock_handle *lh,
3262 __u64 flags, int result)
3264 struct ptlrpc_request *req = mdt_info_req(info);
3265 struct ldlm_lock *lock = *lockp;
3266 struct ldlm_lock *new_lock;
3268 /* If possible resent found a lock, @lh is set to its handle */
3269 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3271 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3272 lh->mlh_reg_lh.cookie = 0;
3276 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3277 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3278 * a resend case, however, it could be already destroyed
3279 * due to client eviction or a raced cancel RPC. */
3280 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3281 lh->mlh_reg_lh.cookie);
3282 lh->mlh_reg_lh.cookie = 0;
3286 LASSERTF(new_lock != NULL,
3287 "lockh %#llx flags %#llx : rc = %d\n",
3288 lh->mlh_reg_lh.cookie, flags, result);
3291 * If we've already given this lock to a client once, then we should
3292 * have no readers or writers. Otherwise, we should have one reader
3293 * _or_ writer ref (which will be zeroed below) before returning the
3296 if (new_lock->l_export == req->rq_export) {
3297 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3299 LASSERT(new_lock->l_export == NULL);
3300 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3305 if (new_lock->l_export == req->rq_export) {
3307 * Already gave this to the client, which means that we
3308 * reconstructed a reply.
3310 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3313 LDLM_LOCK_RELEASE(new_lock);
3314 lh->mlh_reg_lh.cookie = 0;
3315 RETURN(ELDLM_LOCK_REPLACED);
3319 * Fixup the lock to be given to the client.
3321 lock_res_and_lock(new_lock);
3322 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3323 * possible blocking AST. */
3324 while (new_lock->l_readers > 0) {
3325 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3326 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3327 new_lock->l_readers--;
3329 while (new_lock->l_writers > 0) {
3330 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3331 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3332 new_lock->l_writers--;
3335 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3336 new_lock->l_blocking_ast = lock->l_blocking_ast;
3337 new_lock->l_completion_ast = lock->l_completion_ast;
3338 new_lock->l_remote_handle = lock->l_remote_handle;
3339 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3341 unlock_res_and_lock(new_lock);
3343 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3344 &new_lock->l_remote_handle,
3345 &new_lock->l_exp_hash);
3347 LDLM_LOCK_RELEASE(new_lock);
3348 lh->mlh_reg_lh.cookie = 0;
3350 RETURN(ELDLM_LOCK_REPLACED);
3353 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3354 struct ldlm_lock *new_lock,
3355 struct mdt_lock_handle *lh,
3358 struct ptlrpc_request *req = mdt_info_req(info);
3359 struct ldlm_request *dlmreq;
3361 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3364 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3366 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3367 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3369 if (flags & LDLM_FL_RESENT) {
3370 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3371 lh->mlh_reg_mode = new_lock->l_granted_mode;
3373 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3374 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3375 lh->mlh_reg_lh.cookie);
3380 * If the xid matches, then we know this is a resent request, and allow
3381 * it. (It's probably an OPEN, for which we don't send a lock.
3383 if (req_can_reconstruct(req, NULL))
3387 * This remote handle isn't enqueued, so we never received or processed
3388 * this request. Clear MSG_RESENT, because it can be handled like any
3389 * normal request now.
3391 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3393 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3394 dlmreq->lock_handle[0].cookie);
3397 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3398 struct mdt_thread_info *info,
3399 struct ldlm_lock **lockp,
3402 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3403 struct ldlm_reply *ldlm_rep = NULL;
3408 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3409 * (for the resend case) or a new lock. Below we will use it to
3410 * replace the original lock.
3412 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3413 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3414 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3415 rc = mdt_object_lock(info, info->mti_object, lhc,
3416 MDS_INODELOCK_XATTR);
3421 rc = mdt_getxattr(info);
3423 if (mdt_info_req(info)->rq_repmsg != NULL)
3424 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3426 if (ldlm_rep == NULL ||
3427 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3428 mdt_object_unlock(info, info->mti_object, lhc, 1);
3429 RETURN(err_serious(-EFAULT));
3432 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3434 /* This is left for interop instead of adding a new interop flag.
3436 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3437 if (ldlm_rep->lock_policy_res2) {
3438 mdt_object_unlock(info, info->mti_object, lhc, 1);
3439 RETURN(ELDLM_LOCK_ABORTED);
3443 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3447 static int mdt_intent_getattr(enum mdt_it_code opcode,
3448 struct mdt_thread_info *info,
3449 struct ldlm_lock **lockp,
3452 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3454 struct ldlm_reply *ldlm_rep;
3455 struct mdt_body *reqbody;
3456 struct mdt_body *repbody;
3460 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3463 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3466 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3467 repbody->mbo_eadatasize = 0;
3468 repbody->mbo_aclsize = 0;
3472 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3474 case MDT_IT_GETATTR:
3475 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3479 CERROR("Unsupported intent (%d)\n", opcode);
3480 GOTO(out_shrink, rc = -EINVAL);
3483 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3485 GOTO(out_shrink, rc);
3487 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3488 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3490 /* Get lock from request for possible resent case. */
3491 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3493 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3494 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3496 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3497 ldlm_rep->lock_policy_res2 = 0;
3498 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3499 ldlm_rep->lock_policy_res2) {
3500 lhc->mlh_reg_lh.cookie = 0ull;
3501 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3504 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3507 mdt_exit_ucred(info);
3509 mdt_client_compatibility(info);
3510 rc2 = mdt_fix_reply(info);
3516 static int mdt_intent_layout(enum mdt_it_code opcode,
3517 struct mdt_thread_info *info,
3518 struct ldlm_lock **lockp,
3521 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3522 struct layout_intent *layout;
3524 struct mdt_object *obj = NULL;
3525 bool layout_change = false;
3526 int layout_size = 0;
3530 if (opcode != MDT_IT_LAYOUT) {
3531 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3536 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3540 switch (layout->li_opc) {
3541 case LAYOUT_INTENT_TRUNC:
3542 case LAYOUT_INTENT_WRITE:
3543 layout_change = true;
3545 case LAYOUT_INTENT_ACCESS:
3547 case LAYOUT_INTENT_READ:
3548 case LAYOUT_INTENT_GLIMPSE:
3549 case LAYOUT_INTENT_RELEASE:
3550 case LAYOUT_INTENT_RESTORE:
3551 CERROR("%s: Unsupported layout intent opc %d\n",
3552 mdt_obd_name(info->mti_mdt), layout->li_opc);
3556 CERROR("%s: Unknown layout intent opc %d\n",
3557 mdt_obd_name(info->mti_mdt), layout->li_opc);
3564 fid = &info->mti_tmp_fid2;
3565 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3567 /* Get lock from request for possible resent case. */
3568 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3570 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3572 GOTO(out, rc = PTR_ERR(obj));
3574 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3575 layout_size = mdt_attr_get_eabuf_size(info, obj);
3576 if (layout_size < 0)
3577 GOTO(out_obj, rc = layout_size);
3579 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3580 info->mti_mdt->mdt_max_mdsize = layout_size;
3584 * set reply buffer size, so that ldlm_handle_enqueue0()->
3585 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
3587 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3588 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3590 rc = req_capsule_server_pack(info->mti_pill);
3595 if (layout_change) {
3596 struct lu_buf *buf = &info->mti_buf;
3599 * mdt_layout_change is a reint operation, when the request
3600 * is resent, layout write shouldn't reprocess it again.
3602 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
3604 GOTO(out_obj, rc = rc < 0 ? rc : 0);
3607 * There is another resent case: the client's job has been
3608 * done by another client, referring lod_declare_layout_change
3609 * -EALREADY case, and it became a operation w/o transaction,
3610 * so we should not do the layout change, otherwise
3611 * mdt_layout_change() will try to cancel the granted server
3612 * CR lock whose remote counterpart is still in hold on the
3613 * client, and a deadlock ensues.
3615 rc = mdt_check_resent_lock(info, obj, lhc);
3621 if (unlikely(req_is_replay(mdt_info_req(info)))) {
3622 buf->lb_buf = req_capsule_client_get(info->mti_pill,
3624 buf->lb_len = req_capsule_get_size(info->mti_pill,
3625 &RMF_EADATA, RCL_CLIENT);
3627 * If it's a replay of layout write intent RPC, the
3628 * client has saved the extended lovea when
3629 * it get reply then.
3631 if (buf->lb_len > 0)
3632 mdt_fix_lov_magic(info, buf->lb_buf);
3636 * Instantiate some layout components, if @buf contains
3637 * lovea, then it's a replay of the layout intent write
3640 rc = mdt_layout_change(info, obj, layout, buf);
3645 mdt_object_put(info->mti_env, obj);
3647 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3648 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3651 lhc->mlh_reg_lh.cookie = 0;
3656 static int mdt_intent_reint(enum mdt_it_code opcode,
3657 struct mdt_thread_info *info,
3658 struct ldlm_lock **lockp,
3661 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3662 struct ldlm_reply *rep = NULL;
3666 static const struct req_format *intent_fmts[REINT_MAX] = {
3667 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3668 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3673 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3677 if (mdt_it_flavor[opcode].it_reint != opc) {
3678 CERROR("Reint code %ld doesn't match intent: %d\n",
3680 RETURN(err_serious(-EPROTO));
3683 /* Get lock from request for possible resent case. */
3684 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3686 rc = mdt_reint_internal(info, lhc, opc);
3688 /* Check whether the reply has been packed successfully. */
3689 if (mdt_info_req(info)->rq_repmsg != NULL)
3690 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3692 RETURN(err_serious(-EFAULT));
3694 /* MDC expects this in any case */
3696 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3698 /* the open lock or the lock for cross-ref object should be
3699 * returned to the client */
3700 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3701 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3702 rep->lock_policy_res2 = 0;
3703 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3707 rep->lock_policy_res2 = clear_serious(rc);
3709 if (rep->lock_policy_res2 == -ENOENT &&
3710 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3711 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3712 rep->lock_policy_res2 = 0;
3714 lhc->mlh_reg_lh.cookie = 0ull;
3715 if (rc == -ENOTCONN || rc == -ENODEV ||
3716 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3718 * If it is the disconnect error (ENODEV & ENOCONN), the error
3719 * will be returned by rq_status, and client at ptlrpc layer
3720 * will detect this, then disconnect, reconnect the import
3721 * immediately, instead of impacting the following the rpc.
3726 * For other cases, the error will be returned by intent, and client
3727 * will retrieve the result from intent.
3729 RETURN(ELDLM_LOCK_ABORTED);
3732 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3740 case IT_OPEN|IT_CREAT:
3747 rc = MDT_IT_READDIR;
3750 rc = MDT_IT_GETATTR;
3762 rc = MDT_IT_GETXATTR;
3767 case IT_QUOTA_DQACQ:
3772 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3779 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3780 struct mdt_thread_info *info,
3781 struct ldlm_lock **lockp, __u64 flags)
3783 struct req_capsule *pill = info->mti_pill;
3784 struct ptlrpc_request *req = mdt_info_req(info);
3785 struct mdt_it_flavor *flv;
3790 opc = mdt_intent_code(itopc);
3794 if (opc == MDT_IT_QUOTA) {
3795 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3798 RETURN(-EOPNOTSUPP);
3800 if (mdt_rdonly(req->rq_export))
3803 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3804 /* pass the request to quota master */
3805 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3806 mdt_info_req(info), lockp,
3811 flv = &mdt_it_flavor[opc];
3812 if (flv->it_fmt != NULL)
3813 req_capsule_extend(pill, flv->it_fmt);
3815 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3819 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3822 if (flv->it_act != NULL) {
3823 struct ldlm_reply *rep;
3825 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3827 /* execute policy */
3828 rc = flv->it_act(opc, info, lockp, flags);
3830 /* Check whether the reply has been packed successfully. */
3831 if (req->rq_repmsg != NULL) {
3832 rep = req_capsule_server_get(info->mti_pill,
3834 rep->lock_policy_res2 =
3835 ptlrpc_status_hton(rep->lock_policy_res2);
3842 static int mdt_intent_policy(struct ldlm_namespace *ns,
3843 struct ldlm_lock **lockp, void *req_cookie,
3844 enum ldlm_mode mode, __u64 flags, void *data)
3846 struct tgt_session_info *tsi;
3847 struct mdt_thread_info *info;
3848 struct ptlrpc_request *req = req_cookie;
3849 struct ldlm_intent *it;
3850 struct req_capsule *pill;
3855 LASSERT(req != NULL);
3857 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3859 info = tsi2mdt_info(tsi);
3860 LASSERT(info != NULL);
3861 pill = info->mti_pill;
3862 LASSERT(pill->rc_req == req);
3864 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3865 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3866 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3868 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3872 /* Lock without inodebits makes no sense and will oops
3873 * later in ldlm. Let's check it now to see if we have
3874 * ibits corrupted somewhere in mdt_intent_opc().
3875 * The case for client miss to set ibits has been
3876 * processed by others. */
3877 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3878 lr_type == LDLM_IBITS,
3879 info->mti_dlm_req->lock_desc.\
3880 l_policy_data.l_inodebits.bits != 0));
3882 rc = err_serious(-EFAULT);
3884 /* No intent was provided */
3885 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3886 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3887 rc = req_capsule_server_pack(pill);
3889 rc = err_serious(rc);
3891 mdt_thread_info_fini(info);
3895 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3897 struct seq_server_site *ss = mdt_seq_site(mdt);
3899 if (ss->ss_node_id == 0)
3902 if (ss->ss_client_seq != NULL) {
3903 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3904 ss->ss_client_seq->lcs_exp = NULL;
3907 if (ss->ss_server_fld != NULL) {
3908 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3909 ss->ss_server_fld->lsf_control_exp = NULL;
3913 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3915 struct seq_server_site *ss = mdt_seq_site(mdt);
3920 if (ss->ss_server_seq != NULL)
3921 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3924 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3926 mdt_seq_fini_cli(mdt);
3927 mdt_deregister_seq_exp(mdt);
3929 return seq_site_fini(env, mdt_seq_site(mdt));
3933 * It will retrieve its FLDB entries from MDT0, and it only happens
3934 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3935 * and it needs to refresh FLDB from the MDT0.
3937 static int mdt_register_lwp_callback(void *data)
3940 struct mdt_device *mdt = data;
3941 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3945 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3947 rc = lu_env_init(&env, LCT_MD_THREAD);
3949 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3953 /* Allocate new sequence now to avoid creating local transaction
3954 * in the normal transaction process */
3955 rc = seq_server_check_and_alloc_super(&env,
3956 mdt_seq_site(mdt)->ss_server_seq);
3961 rc = fld_update_from_controller(&env, fld);
3963 CERROR("%s: cannot update controller: rc = %d\n",
3964 mdt_obd_name(mdt), rc);
3973 static int mdt_register_seq_exp(struct mdt_device *mdt)
3975 struct seq_server_site *ss = mdt_seq_site(mdt);
3976 char *lwp_name = NULL;
3979 if (ss->ss_node_id == 0)
3982 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3983 if (lwp_name == NULL)
3984 GOTO(out_free, rc = -ENOMEM);
3986 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3990 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3995 rc = lustre_register_lwp_item(lwp_name,
3996 &ss->ss_server_fld->lsf_control_exp,
3997 mdt_register_lwp_callback, mdt);
3999 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4000 ss->ss_client_seq->lcs_exp = NULL;
4004 if (lwp_name != NULL)
4005 OBD_FREE(lwp_name, MAX_OBD_NAME);
4011 * Init client sequence manager which is used by local MDS to talk to sequence
4012 * controller on remote node.
4014 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4016 struct seq_server_site *ss = mdt_seq_site(mdt);
4021 /* check if this is adding the first MDC and controller is not yet
4023 OBD_ALLOC_PTR(ss->ss_client_seq);
4024 if (ss->ss_client_seq == NULL)
4027 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4028 if (prefix == NULL) {
4029 OBD_FREE_PTR(ss->ss_client_seq);
4030 ss->ss_client_seq = NULL;
4034 /* Note: seq_client_fini will be called in seq_site_fini */
4035 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4036 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4037 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4039 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4041 OBD_FREE_PTR(ss->ss_client_seq);
4042 ss->ss_client_seq = NULL;
4046 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
4051 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4053 struct seq_server_site *ss;
4057 ss = mdt_seq_site(mdt);
4058 /* init sequence controller server(MDT0) */
4059 if (ss->ss_node_id == 0) {
4060 OBD_ALLOC_PTR(ss->ss_control_seq);
4061 if (ss->ss_control_seq == NULL)
4064 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4065 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4068 GOTO(out_seq_fini, rc);
4071 /* Init normal sequence server */
4072 OBD_ALLOC_PTR(ss->ss_server_seq);
4073 if (ss->ss_server_seq == NULL)
4074 GOTO(out_seq_fini, rc = -ENOMEM);
4076 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4077 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4079 GOTO(out_seq_fini, rc);
4081 /* init seq client for seq server to talk to seq controller(MDT0) */
4082 rc = mdt_seq_init_cli(env, mdt);
4084 GOTO(out_seq_fini, rc);
4086 if (ss->ss_node_id != 0)
4087 /* register controller export through lwp */
4088 rc = mdt_register_seq_exp(mdt);
4093 mdt_seq_fini(env, mdt);
4101 static int mdt_fld_fini(const struct lu_env *env,
4102 struct mdt_device *m)
4104 struct seq_server_site *ss = mdt_seq_site(m);
4107 if (ss && ss->ss_server_fld) {
4108 fld_server_fini(env, ss->ss_server_fld);
4109 OBD_FREE_PTR(ss->ss_server_fld);
4110 ss->ss_server_fld = NULL;
4116 static int mdt_fld_init(const struct lu_env *env,
4118 struct mdt_device *m)
4120 struct seq_server_site *ss;
4124 ss = mdt_seq_site(m);
4126 OBD_ALLOC_PTR(ss->ss_server_fld);
4127 if (ss->ss_server_fld == NULL)
4128 RETURN(rc = -ENOMEM);
4130 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4133 OBD_FREE_PTR(ss->ss_server_fld);
4134 ss->ss_server_fld = NULL;
4141 static void mdt_stack_pre_fini(const struct lu_env *env,
4142 struct mdt_device *m, struct lu_device *top)
4144 struct lustre_cfg_bufs *bufs;
4145 struct lustre_cfg *lcfg;
4146 struct mdt_thread_info *info;
4151 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4152 LASSERT(info != NULL);
4154 bufs = &info->mti_u.bufs;
4156 LASSERT(m->mdt_child_exp);
4157 LASSERT(m->mdt_child_exp->exp_obd);
4159 /* process cleanup, pass mdt obd name to get obd umount flags */
4160 /* XXX: this is needed because all layers are referenced by
4161 * objects (some of them are pinned by osd, for example *
4162 * the proper solution should be a model where object used
4163 * by osd only doesn't have mdt/mdd slices -bzzz */
4164 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4165 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4166 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4170 top->ld_ops->ldo_process_config(env, top, lcfg);
4171 lustre_cfg_free(lcfg);
4175 static void mdt_stack_fini(const struct lu_env *env,
4176 struct mdt_device *m, struct lu_device *top)
4178 struct obd_device *obd = mdt2obd_dev(m);
4179 struct lustre_cfg_bufs *bufs;
4180 struct lustre_cfg *lcfg;
4181 struct mdt_thread_info *info;
4185 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4186 LASSERT(info != NULL);
4188 lu_dev_del_linkage(top->ld_site, top);
4190 lu_site_purge(env, top->ld_site, -1);
4192 bufs = &info->mti_u.bufs;
4193 /* process cleanup, pass mdt obd name to get obd umount flags */
4194 /* another purpose is to let all layers to release their objects */
4195 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4200 lustre_cfg_bufs_set_string(bufs, 1, flags);
4201 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4206 top->ld_ops->ldo_process_config(env, top, lcfg);
4207 lustre_cfg_free(lcfg);
4209 lu_site_purge(env, top->ld_site, -1);
4211 m->mdt_child = NULL;
4212 m->mdt_bottom = NULL;
4214 obd_disconnect(m->mdt_child_exp);
4215 m->mdt_child_exp = NULL;
4217 obd_disconnect(m->mdt_bottom_exp);
4218 m->mdt_child_exp = NULL;
4221 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4222 const char *next, struct obd_export **exp)
4224 struct obd_connect_data *data = NULL;
4225 struct obd_device *obd;
4229 OBD_ALLOC_PTR(data);
4231 GOTO(out, rc = -ENOMEM);
4233 obd = class_name2obd(next);
4235 CERROR("%s: can't locate next device: %s\n",
4236 mdt_obd_name(m), next);
4237 GOTO(out, rc = -ENOTCONN);
4240 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4241 data->ocd_version = LUSTRE_VERSION_CODE;
4243 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4245 CERROR("%s: cannot connect to next dev %s (%d)\n",
4246 mdt_obd_name(m), next, rc);
4256 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4257 struct lustre_cfg *cfg)
4259 char *dev = lustre_cfg_string(cfg, 0);
4260 int rc, name_size, uuid_size;
4261 char *name, *uuid, *p;
4262 struct lustre_cfg_bufs *bufs;
4263 struct lustre_cfg *lcfg;
4264 struct obd_device *obd;
4265 struct lustre_profile *lprof;
4266 struct lu_site *site;
4269 /* in 1.8 we had the only device in the stack - MDS.
4270 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4271 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4272 * to generate names and setup MDT, MDD. MDT will be using
4273 * generated name to connect to MDD. for MDD the next device
4274 * will be LOD with name taken from so called "profile" which
4275 * is generated by mount_option line
4277 * 1.8 MGS generates config. commands like this:
4278 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4279 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4280 * 2.0 MGS generates config. commands like this:
4281 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4282 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4283 * 3:lustre-MDT0000-mdtlov 4:f
4285 * we generate MDD name from MDT one, just replacing T with D
4287 * after all the preparations, the logical equivalent will be
4288 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4289 * 3:lustre-MDT0000-mdtlov 4:f
4290 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4291 * 3:lustre-MDD0000 4:f
4293 * notice we build the stack from down to top: MDD first, then MDT */
4295 name_size = MAX_OBD_NAME;
4296 uuid_size = MAX_OBD_NAME;
4298 OBD_ALLOC(name, name_size);
4299 OBD_ALLOC(uuid, uuid_size);
4300 if (name == NULL || uuid == NULL)
4301 GOTO(cleanup_mem, rc = -ENOMEM);
4303 OBD_ALLOC_PTR(bufs);
4305 GOTO(cleanup_mem, rc = -ENOMEM);
4308 p = strstr(name, "-MDT");
4310 GOTO(free_bufs, rc = -ENOMEM);
4313 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4315 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4316 if (lprof == NULL || lprof->lp_dt == NULL) {
4317 CERROR("can't find the profile: %s\n",
4318 lustre_cfg_string(cfg, 0));
4319 GOTO(free_bufs, rc = -EINVAL);
4322 lustre_cfg_bufs_reset(bufs, name);
4323 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4324 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4325 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4327 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4329 GOTO(put_profile, rc = -ENOMEM);
4331 rc = class_attach(lcfg);
4333 GOTO(lcfg_cleanup, rc);
4335 obd = class_name2obd(name);
4337 CERROR("Can not find obd %s (%s in config)\n",
4338 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4339 GOTO(lcfg_cleanup, rc = -EINVAL);
4342 lustre_cfg_free(lcfg);
4344 lustre_cfg_bufs_reset(bufs, name);
4345 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4346 lustre_cfg_bufs_set_string(bufs, 2, dev);
4347 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4349 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4351 GOTO(class_detach, rc = -ENOMEM);
4353 rc = class_setup(obd, lcfg);
4355 GOTO(class_detach, rc);
4357 /* connect to MDD we just setup */
4358 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4360 GOTO(class_detach, rc);
4362 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4364 LASSERT(mdt_lu_site(mdt) == NULL);
4365 mdt->mdt_lu_dev.ld_site = site;
4366 site->ls_top_dev = &mdt->mdt_lu_dev;
4367 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4369 /* now connect to bottom OSD */
4370 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4371 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4373 GOTO(class_detach, rc);
4375 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4377 rc = lu_env_refill((struct lu_env *)env);
4379 CERROR("Failure to refill session: '%d'\n", rc);
4381 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4386 class_detach(obd, lcfg);
4388 lustre_cfg_free(lcfg);
4390 class_put_profile(lprof);
4395 OBD_FREE(name, name_size);
4397 OBD_FREE(uuid, uuid_size);
4401 /* setup quota master target on MDT0 */
4402 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4403 struct lustre_cfg *cfg)
4405 struct obd_device *obd;
4406 char *dev = lustre_cfg_string(cfg, 0);
4407 char *qmtname, *uuid, *p;
4408 struct lustre_cfg_bufs *bufs;
4409 struct lustre_cfg *lcfg;
4410 struct lustre_profile *lprof;
4411 struct obd_connect_data *data;
4415 LASSERT(mdt->mdt_qmt_exp == NULL);
4416 LASSERT(mdt->mdt_qmt_dev == NULL);
4418 /* quota master is on MDT0 only for now */
4419 if (mdt->mdt_seq_site.ss_node_id != 0)
4422 /* MGS generates config commands which look as follows:
4423 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4424 * 3:lustre-MDT0000-mdtlov 4:f
4426 * We generate the QMT name from the MDT one, just replacing MD with QM
4427 * after all the preparations, the logical equivalent will be:
4428 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4429 * 3:lustre-MDT0000-osd 4:f */
4430 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4431 OBD_ALLOC(uuid, UUID_MAX);
4432 OBD_ALLOC_PTR(bufs);
4433 OBD_ALLOC_PTR(data);
4434 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4435 GOTO(cleanup_mem, rc = -ENOMEM);
4437 strcpy(qmtname, dev);
4438 p = strstr(qmtname, "-MDT");
4440 GOTO(cleanup_mem, rc = -ENOMEM);
4441 /* replace MD with QM */
4445 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4447 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4448 if (lprof == NULL || lprof->lp_dt == NULL) {
4449 CERROR("can't find profile for %s\n",
4450 lustre_cfg_string(cfg, 0));
4451 GOTO(cleanup_mem, rc = -EINVAL);
4454 lustre_cfg_bufs_reset(bufs, qmtname);
4455 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4456 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4457 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4459 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4461 GOTO(put_profile, rc = -ENOMEM);
4463 rc = class_attach(lcfg);
4465 GOTO(lcfg_cleanup, rc);
4467 obd = class_name2obd(qmtname);
4469 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4470 lustre_cfg_string(cfg, 0));
4471 GOTO(lcfg_cleanup, rc = -EINVAL);
4474 lustre_cfg_free(lcfg);
4476 lustre_cfg_bufs_reset(bufs, qmtname);
4477 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4478 lustre_cfg_bufs_set_string(bufs, 2, dev);
4480 /* for quota, the next device should be the OSD device */
4481 lustre_cfg_bufs_set_string(bufs, 3,
4482 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4484 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4486 GOTO(class_detach, rc = -ENOMEM);
4488 rc = class_setup(obd, lcfg);
4490 GOTO(class_detach, rc);
4492 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4494 /* configure local quota objects */
4495 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4499 GOTO(class_cleanup, rc);
4501 /* connect to quota master target */
4502 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4503 data->ocd_version = LUSTRE_VERSION_CODE;
4504 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4507 CERROR("cannot connect to quota master device %s (%d)\n",
4509 GOTO(class_cleanup, rc);
4515 class_manual_cleanup(obd);
4516 mdt->mdt_qmt_dev = NULL;
4520 class_detach(obd, lcfg);
4522 lustre_cfg_free(lcfg);
4524 class_put_profile(lprof);
4529 OBD_FREE(qmtname, MAX_OBD_NAME);
4531 OBD_FREE(uuid, UUID_MAX);
4537 /* Shutdown quota master target associated with mdt */
4538 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4542 if (mdt->mdt_qmt_exp == NULL)
4544 LASSERT(mdt->mdt_qmt_dev != NULL);
4546 /* the qmt automatically shuts down when the mdt disconnects */
4547 obd_disconnect(mdt->mdt_qmt_exp);
4548 mdt->mdt_qmt_exp = NULL;
4549 mdt->mdt_qmt_dev = NULL;
4553 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4554 * for now. This will be removed along with converting rest of MDT code
4555 * to use tgt_session_info */
4556 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4558 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4561 rc = mdt_getxattr(info);
4563 mdt_thread_info_fini(info);
4567 static struct tgt_handler mdt_tgt_handlers[] = {
4568 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4569 0, MDS_CONNECT, mdt_tgt_connect,
4570 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4571 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4572 0, MDS_DISCONNECT, tgt_disconnect,
4573 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4574 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4575 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4576 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4577 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4578 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4579 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4580 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4582 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4583 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4584 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4585 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4586 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4587 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4588 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4589 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4591 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4592 mdt_hsm_ct_register),
4593 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4594 mdt_hsm_ct_unregister),
4595 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4597 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4599 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4600 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4602 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4607 static struct tgt_handler mdt_sec_ctx_ops[] = {
4608 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4609 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4610 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4613 static struct tgt_handler mdt_quota_ops[] = {
4614 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4617 static struct tgt_opc_slice mdt_common_slice[] = {
4619 .tos_opc_start = MDS_FIRST_OPC,
4620 .tos_opc_end = MDS_LAST_OPC,
4621 .tos_hs = mdt_tgt_handlers
4624 .tos_opc_start = OBD_FIRST_OPC,
4625 .tos_opc_end = OBD_LAST_OPC,
4626 .tos_hs = tgt_obd_handlers
4629 .tos_opc_start = LDLM_FIRST_OPC,
4630 .tos_opc_end = LDLM_LAST_OPC,
4631 .tos_hs = tgt_dlm_handlers
4634 .tos_opc_start = SEC_FIRST_OPC,
4635 .tos_opc_end = SEC_LAST_OPC,
4636 .tos_hs = mdt_sec_ctx_ops
4639 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4640 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4641 .tos_hs = tgt_out_handlers
4644 .tos_opc_start = FLD_FIRST_OPC,
4645 .tos_opc_end = FLD_LAST_OPC,
4646 .tos_hs = fld_handlers
4649 .tos_opc_start = SEQ_FIRST_OPC,
4650 .tos_opc_end = SEQ_LAST_OPC,
4651 .tos_hs = seq_handlers
4654 .tos_opc_start = QUOTA_DQACQ,
4655 .tos_opc_end = QUOTA_LAST_OPC,
4656 .tos_hs = mdt_quota_ops
4659 .tos_opc_start = LLOG_FIRST_OPC,
4660 .tos_opc_end = LLOG_LAST_OPC,
4661 .tos_hs = tgt_llog_handlers
4664 .tos_opc_start = LFSCK_FIRST_OPC,
4665 .tos_opc_end = LFSCK_LAST_OPC,
4666 .tos_hs = tgt_lfsck_handlers
4674 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4676 struct md_device *next = m->mdt_child;
4677 struct lu_device *d = &m->mdt_lu_dev;
4678 struct obd_device *obd = mdt2obd_dev(m);
4679 struct lfsck_stop stop;
4682 stop.ls_status = LS_PAUSED;
4684 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4686 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4687 ping_evictor_stop();
4689 if (m->mdt_opts.mo_coordinator)
4690 mdt_hsm_cdt_stop(m);
4692 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4693 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4695 if (m->mdt_namespace != NULL)
4696 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4697 d->ld_obd->obd_force);
4699 obd_exports_barrier(obd);
4700 obd_zombie_barrier();
4702 mdt_quota_fini(env, m);
4704 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4706 /* Calling the cleanup functions in the same order as in the mdt_init0
4711 target_recovery_fini(obd);
4712 upcall_cache_cleanup(m->mdt_identity_cache);
4713 m->mdt_identity_cache = NULL;
4715 mdt_fs_cleanup(env, m);
4717 tgt_fini(env, &m->mdt_lut);
4719 mdt_hsm_cdt_fini(m);
4721 if (m->mdt_namespace != NULL) {
4722 ldlm_namespace_free_post(m->mdt_namespace);
4723 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4726 if (m->mdt_md_root != NULL) {
4727 mdt_object_put(env, m->mdt_md_root);
4728 m->mdt_md_root = NULL;
4731 mdt_seq_fini(env, m);
4733 mdt_fld_fini(env, m);
4738 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4740 LASSERT(atomic_read(&d->ld_ref) == 0);
4742 server_put_mount(mdt_obd_name(m), true);
4747 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4749 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4750 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4752 struct mdt_thread_info *info;
4753 struct obd_device *obd;
4754 const char *dev = lustre_cfg_string(cfg, 0);
4755 const char *num = lustre_cfg_string(cfg, 2);
4756 struct lustre_mount_info *lmi = NULL;
4757 struct lustre_sb_info *lsi;
4759 struct seq_server_site *ss_site;
4760 const char *identity_upcall = "NONE";
4761 struct md_device *next;
4767 lu_device_init(&m->mdt_lu_dev, ldt);
4769 * Environment (env) might be missing mdt_thread_key values at that
4770 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4773 * Usually device allocation path doesn't use module key values, but
4774 * mdt has to do a lot of work here, so allocate key value.
4776 rc = lu_env_refill((struct lu_env *)env);
4780 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4781 LASSERT(info != NULL);
4783 obd = class_name2obd(dev);
4784 LASSERT(obd != NULL);
4786 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4787 m->mdt_opts.mo_evict_tgt_nids = 1;
4788 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4790 /* default is coordinator off, it is started through conf_param
4792 m->mdt_opts.mo_coordinator = 0;
4794 lmi = server_get_mount(dev);
4796 CERROR("Cannot get mount info for %s!\n", dev);
4799 lsi = s2lsi(lmi->lmi_sb);
4800 /* CMD is supported only in IAM mode */
4802 node_id = simple_strtol(num, NULL, 10);
4803 obd->u.obt.obt_magic = OBT_MAGIC;
4804 if (lsi->lsi_lmd != NULL &&
4805 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4806 m->mdt_skip_lfsck = 1;
4809 m->mdt_squash.rsi_uid = 0;
4810 m->mdt_squash.rsi_gid = 0;
4811 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4812 init_rwsem(&m->mdt_squash.rsi_sem);
4813 spin_lock_init(&m->mdt_lock);
4814 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4815 m->mdt_enable_remote_dir = 0;
4816 m->mdt_enable_remote_dir_gid = 0;
4818 atomic_set(&m->mdt_mds_mds_conns, 0);
4819 atomic_set(&m->mdt_async_commit_count, 0);
4821 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4822 m->mdt_lu_dev.ld_obd = obd;
4823 /* Set this lu_device to obd for error handling purposes. */
4824 obd->obd_lu_dev = &m->mdt_lu_dev;
4826 /* init the stack */
4827 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4829 CERROR("%s: Can't init device stack, rc %d\n",
4830 mdt_obd_name(m), rc);
4835 ss_site = mdt_seq_site(m);
4836 s->ld_seq_site = ss_site;
4839 /* set server index */
4840 ss_site->ss_node_id = node_id;
4842 /* failover is the default
4843 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4844 * assumed whose ss_node_id == 0 XXX
4846 obd->obd_replayable = 1;
4847 /* No connection accepted until configurations will finish */
4848 obd->obd_no_conn = 1;
4850 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4851 char *str = lustre_cfg_string(cfg, 4);
4852 if (strchr(str, 'n')) {
4853 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4854 obd->obd_replayable = 0;
4858 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4860 GOTO(err_fini_stack, rc);
4862 rc = mdt_seq_init(env, m);
4864 GOTO(err_fini_fld, rc);
4866 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4867 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4868 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4869 LDLM_NAMESPACE_SERVER,
4870 LDLM_NAMESPACE_GREEDY,
4872 if (m->mdt_namespace == NULL)
4873 GOTO(err_fini_seq, rc = -ENOMEM);
4875 m->mdt_namespace->ns_lvbp = m;
4876 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4878 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4879 /* set obd_namespace for compatibility with old code */
4880 obd->obd_namespace = m->mdt_namespace;
4882 rc = mdt_hsm_cdt_init(m);
4884 CERROR("%s: error initializing coordinator, rc %d\n",
4885 mdt_obd_name(m), rc);
4886 GOTO(err_free_ns, rc);
4889 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4890 OBD_FAIL_MDS_ALL_REQUEST_NET,
4891 OBD_FAIL_MDS_ALL_REPLY_NET);
4893 GOTO(err_free_hsm, rc);
4895 rc = mdt_fs_setup(env, m, obd, lsi);
4899 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
4901 next = m->mdt_child;
4902 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4905 GOTO(err_fs_cleanup, rc);
4907 if (mntopts & MNTOPT_USERXATTR)
4908 m->mdt_opts.mo_user_xattr = 1;
4910 m->mdt_opts.mo_user_xattr = 0;
4912 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4914 GOTO(err_fs_cleanup, rc);
4916 if (mntopts & MNTOPT_ACL)
4917 m->mdt_opts.mo_acl = 1;
4919 m->mdt_opts.mo_acl = 0;
4921 /* XXX: to support suppgid for ACL, we enable identity_upcall
4922 * by default, otherwise, maybe got unexpected -EACCESS. */
4923 if (m->mdt_opts.mo_acl)
4924 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4926 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4928 &mdt_identity_upcall_cache_ops);
4929 if (IS_ERR(m->mdt_identity_cache)) {
4930 rc = PTR_ERR(m->mdt_identity_cache);
4931 m->mdt_identity_cache = NULL;
4932 GOTO(err_fs_cleanup, rc);
4935 rc = mdt_procfs_init(m, dev);
4937 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4938 GOTO(err_recovery, rc);
4941 rc = mdt_quota_init(env, m, cfg);
4943 GOTO(err_procfs, rc);
4945 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4946 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4947 "mdt_ldlm_client", m->mdt_ldlm_client);
4949 ping_evictor_start();
4951 /* recovery will be started upon mdt_prepare()
4952 * when the whole stack is complete and ready
4953 * to serve the requests */
4955 /* Reduce the initial timeout on an MDS because it doesn't need such
4956 * a long timeout as an OST does. Adaptive timeouts will adjust this
4957 * value appropriately. */
4958 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4959 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4965 target_recovery_fini(obd);
4966 upcall_cache_cleanup(m->mdt_identity_cache);
4967 m->mdt_identity_cache = NULL;
4969 mdt_fs_cleanup(env, m);
4971 tgt_fini(env, &m->mdt_lut);
4973 mdt_hsm_cdt_fini(m);
4975 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4976 obd->obd_namespace = m->mdt_namespace = NULL;
4978 mdt_seq_fini(env, m);
4980 mdt_fld_fini(env, m);
4982 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4985 server_put_mount(dev, true);
4989 /* For interoperability, the left element is old parameter, the right one
4990 * is the new version of the parameter, if some parameter is deprecated,
4991 * the new version should be set as NULL. */
4992 static struct cfg_interop_param mdt_interop_param[] = {
4993 { "mdt.group_upcall", NULL },
4994 { "mdt.quota_type", NULL },
4995 { "mdd.quota_type", NULL },
4996 { "mdt.som", NULL },
4997 { "mdt.rootsquash", "mdt.root_squash" },
4998 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5002 /* used by MGS to process specific configurations */
5003 static int mdt_process_config(const struct lu_env *env,
5004 struct lu_device *d, struct lustre_cfg *cfg)
5006 struct mdt_device *m = mdt_dev(d);
5007 struct md_device *md_next = m->mdt_child;
5008 struct lu_device *next = md2lu_dev(md_next);
5012 switch (cfg->lcfg_command) {
5014 struct obd_device *obd = d->ld_obd;
5016 /* For interoperability */
5017 struct cfg_interop_param *ptr = NULL;
5018 struct lustre_cfg *old_cfg = NULL;
5021 param = lustre_cfg_string(cfg, 1);
5022 if (param == NULL) {
5023 CERROR("param is empty\n");
5028 ptr = class_find_old_param(param, mdt_interop_param);
5030 if (ptr->new_param == NULL) {
5032 CWARN("For interoperability, skip this %s."
5033 " It is obsolete.\n", ptr->old_param);
5037 CWARN("Found old param %s, changed it to %s.\n",
5038 ptr->old_param, ptr->new_param);
5041 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5048 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
5050 if (rc > 0 || rc == -ENOSYS) {
5051 /* is it an HSM var ? */
5052 rc = class_process_proc_param(PARAM_HSM,
5053 hsm_cdt_get_proc_vars(),
5055 if (rc > 0 || rc == -ENOSYS)
5056 /* we don't understand; pass it on */
5057 rc = next->ld_ops->ldo_process_config(env, next,
5061 if (old_cfg != NULL)
5062 lustre_cfg_free(cfg);
5067 /* others are passed further */
5068 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5074 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5075 const struct lu_object_header *hdr,
5076 struct lu_device *d)
5078 struct mdt_object *mo;
5082 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5084 struct lu_object *o;
5085 struct lu_object_header *h;
5088 h = &mo->mot_header;
5089 lu_object_header_init(h);
5090 lu_object_init(o, h, d);
5091 lu_object_add_top(h, o);
5092 o->lo_ops = &mdt_obj_ops;
5093 spin_lock_init(&mo->mot_write_lock);
5094 mutex_init(&mo->mot_lov_mutex);
5095 init_rwsem(&mo->mot_open_sem);
5096 atomic_set(&mo->mot_open_count, 0);
5102 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5103 const struct lu_object_conf *unused)
5105 struct mdt_device *d = mdt_dev(o->lo_dev);
5106 struct lu_device *under;
5107 struct lu_object *below;
5111 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5112 PFID(lu_object_fid(o)));
5114 under = &d->mdt_child->md_lu_dev;
5115 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5116 if (below != NULL) {
5117 lu_object_add(o, below);
5124 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5126 struct mdt_object *mo = mdt_obj(o);
5127 struct lu_object_header *h;
5131 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5132 PFID(lu_object_fid(o)));
5134 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5135 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5138 lu_object_header_fini(h);
5139 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5144 static int mdt_object_print(const struct lu_env *env, void *cookie,
5145 lu_printer_t p, const struct lu_object *o)
5147 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5149 return (*p)(env, cookie,
5150 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
5151 mdto, mdto->mot_lov_created ? "lov_created" : "",
5152 mdto->mot_cache_attr ? "cache_attr" : "",
5153 mdto->mot_write_count);
5156 static int mdt_prepare(const struct lu_env *env,
5157 struct lu_device *pdev,
5158 struct lu_device *cdev)
5160 struct mdt_device *mdt = mdt_dev(cdev);
5161 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5162 struct obd_device *obd = cdev->ld_obd;
5169 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5173 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5177 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5181 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5182 /* The LFSCK instance is registered just now, so it must be there when
5183 * register the namespace to such instance. */
5184 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5186 if (mdt->mdt_seq_site.ss_node_id == 0) {
5187 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5188 &mdt->mdt_md_root_fid);
5193 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5195 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5196 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5197 LASSERT(obd->obd_no_conn);
5198 spin_lock(&obd->obd_dev_lock);
5199 obd->obd_no_conn = 0;
5200 spin_unlock(&obd->obd_dev_lock);
5202 if (obd->obd_recovering == 0)
5203 mdt_postrecov(env, mdt);
5208 const struct lu_device_operations mdt_lu_ops = {
5209 .ldo_object_alloc = mdt_object_alloc,
5210 .ldo_process_config = mdt_process_config,
5211 .ldo_prepare = mdt_prepare,
5214 static const struct lu_object_operations mdt_obj_ops = {
5215 .loo_object_init = mdt_object_init,
5216 .loo_object_free = mdt_object_free,
5217 .loo_object_print = mdt_object_print
5220 static int mdt_obd_set_info_async(const struct lu_env *env,
5221 struct obd_export *exp,
5222 __u32 keylen, void *key,
5223 __u32 vallen, void *val,
5224 struct ptlrpc_request_set *set)
5230 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5231 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5239 * Match client and server connection feature flags.
5241 * Compute the compatibility flags for a connection request based on
5242 * features mutually supported by client and server.
5244 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5245 * must not be updated here, otherwise a partially initialized value may
5246 * be exposed. After the connection request is successfully processed,
5247 * the top-level MDT connect request handler atomically updates the export
5248 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5249 * reply. \see mdt_connect().
5251 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5252 * full struct obd_connect_data. So care must be taken when accessing fields
5253 * that are not present in struct obd_connect_data_v1. See LU-16.
5255 * \param exp the obd_export associated with this client/target pair
5256 * \param mdt the target device for the connection
5257 * \param data stores data for this connect request
5260 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5261 * \retval -EBADE client and server feature requirements are incompatible
5263 static int mdt_connect_internal(struct obd_export *exp,
5264 struct mdt_device *mdt,
5265 struct obd_connect_data *data)
5267 LASSERT(data != NULL);
5269 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5271 if (mdt->mdt_bottom->dd_rdonly &&
5272 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5273 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5276 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5277 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5279 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5281 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5282 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5283 CWARN("%s: client %s does not support ibits lock, either "
5284 "very old or an invalid client: flags %#llx\n",
5285 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5286 data->ocd_connect_flags);
5290 if (!mdt->mdt_opts.mo_acl)
5291 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5293 if (!mdt->mdt_opts.mo_user_xattr)
5294 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5296 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
5297 data->ocd_brw_size = min(data->ocd_brw_size, MD_MAX_BRW_SIZE);
5298 if (data->ocd_brw_size == 0) {
5299 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
5300 "ocd_version: %x ocd_grant: %d ocd_index: %u "
5301 "ocd_brw_size unexpectedly zero, network data "
5302 "corruption? Refusing to connect this client\n",
5304 exp->exp_client_uuid.uuid,
5305 exp, data->ocd_connect_flags, data->ocd_version,
5306 data->ocd_grant, data->ocd_index);
5311 /* NB: Disregard the rule against updating
5312 * exp_connect_data.ocd_connect_flags in this case, since
5313 * tgt_client_new() needs to know if this is a lightweight
5314 * connection, and it is safe to expose this flag before
5315 * connection processing completes. */
5316 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5317 spin_lock(&exp->exp_lock);
5318 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5319 spin_unlock(&exp->exp_lock);
5322 data->ocd_version = LUSTRE_VERSION_CODE;
5324 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5325 CWARN("%s: MDS requires FID support, but client not\n",
5330 if (OCD_HAS_FLAG(data, PINGLESS)) {
5331 if (ptlrpc_pinger_suppress_pings()) {
5332 spin_lock(&exp->exp_obd->obd_dev_lock);
5333 list_del_init(&exp->exp_obd_chain_timed);
5334 spin_unlock(&exp->exp_obd->obd_dev_lock);
5336 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5340 data->ocd_max_easize = mdt->mdt_max_ea_size;
5342 /* NB: Disregard the rule against updating
5343 * exp_connect_data.ocd_connect_flags in this case, since
5344 * tgt_client_new() needs to know if this is client supports
5345 * multiple modify RPCs, and it is safe to expose this flag before
5346 * connection processing completes. */
5347 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5348 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5349 spin_lock(&exp->exp_lock);
5350 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5351 spin_unlock(&exp->exp_lock);
5357 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5358 struct mdt_thread_info *info,
5359 struct mdt_file_data *mfd)
5361 struct lu_context ses;
5365 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5370 lu_context_enter(&ses);
5372 mdt_ucred(info)->uc_valid = UCRED_OLD;
5373 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5375 lu_context_exit(&ses);
5376 lu_context_fini(&ses);
5382 static int mdt_export_cleanup(struct obd_export *exp)
5384 struct list_head closing_list;
5385 struct mdt_export_data *med = &exp->exp_mdt_data;
5386 struct obd_device *obd = exp->exp_obd;
5387 struct mdt_device *mdt;
5388 struct mdt_thread_info *info;
5390 struct mdt_file_data *mfd, *n;
5394 INIT_LIST_HEAD(&closing_list);
5395 spin_lock(&med->med_open_lock);
5396 while (!list_empty(&med->med_open_head)) {
5397 struct list_head *tmp = med->med_open_head.next;
5398 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5400 /* Remove mfd handle so it can't be found again.
5401 * We are consuming the mfd_list reference here. */
5402 class_handle_unhash(&mfd->mfd_handle);
5403 list_move_tail(&mfd->mfd_list, &closing_list);
5405 spin_unlock(&med->med_open_lock);
5406 mdt = mdt_dev(obd->obd_lu_dev);
5407 LASSERT(mdt != NULL);
5409 rc = lu_env_init(&env, LCT_MD_THREAD);
5413 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5414 LASSERT(info != NULL);
5415 memset(info, 0, sizeof *info);
5416 info->mti_env = &env;
5417 info->mti_mdt = mdt;
5418 info->mti_exp = exp;
5420 if (!list_empty(&closing_list)) {
5421 struct md_attr *ma = &info->mti_attr;
5423 /* Close any open files (which may also cause orphan
5425 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5426 list_del_init(&mfd->mfd_list);
5427 ma->ma_need = ma->ma_valid = 0;
5429 /* This file is being closed due to an eviction, it
5430 * could have been modified and now dirty regarding to
5431 * HSM archive, check this!
5432 * The logic here is to mark a file dirty if there's a
5433 * chance it was dirtied before the client was evicted,
5434 * so that we don't have to wait for a release attempt
5435 * before finding out the file was actually dirty and
5436 * fail the release. Aggressively marking it dirty here
5437 * will cause the policy engine to attempt to
5438 * re-archive it; when rearchiving, we can compare the
5439 * current version to the HSM data_version and make the
5440 * archive request into a noop if it's not actually
5443 if (mfd->mfd_mode & FMODE_WRITE)
5444 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5446 /* Don't unlink orphan on failover umount, LU-184 */
5447 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5448 ma->ma_valid = MA_FLAGS;
5449 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5451 mdt_mfd_close(info, mfd);
5454 info->mti_mdt = NULL;
5455 /* cleanup client slot early */
5456 /* Do not erase record for recoverable client. */
5457 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5458 tgt_client_del(&env, exp);
5464 static inline void mdt_enable_slc(struct mdt_device *mdt)
5466 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5467 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5470 static inline void mdt_disable_slc(struct mdt_device *mdt)
5472 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5473 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5476 static int mdt_obd_disconnect(struct obd_export *exp)
5482 class_export_get(exp);
5484 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5485 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5486 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5488 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5489 mdt_disable_slc(mdt);
5492 rc = server_disconnect_export(exp);
5494 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5496 rc = mdt_export_cleanup(exp);
5497 nodemap_del_member(exp);
5498 class_export_put(exp);
5502 /* mds_connect copy */
5503 static int mdt_obd_connect(const struct lu_env *env,
5504 struct obd_export **exp, struct obd_device *obd,
5505 struct obd_uuid *cluuid,
5506 struct obd_connect_data *data,
5509 struct obd_export *lexp;
5510 struct lustre_handle conn = { 0 };
5511 struct mdt_device *mdt;
5513 lnet_nid_t *client_nid = localdata;
5516 LASSERT(env != NULL);
5517 LASSERT(data != NULL);
5519 if (!exp || !obd || !cluuid)
5522 mdt = mdt_dev(obd->obd_lu_dev);
5524 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5525 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5526 atomic_inc(&mdt->mdt_mds_mds_conns);
5527 mdt_enable_slc(mdt);
5531 * first, check whether the stack is ready to handle requests
5532 * XXX: probably not very appropriate method is used now
5533 * at some point we should find a better one
5535 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5536 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5537 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5538 rc = obd_get_info(env, mdt->mdt_child_exp,
5539 sizeof(KEY_OSP_CONNECTED),
5540 KEY_OSP_CONNECTED, NULL, NULL);
5543 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5546 rc = class_connect(&conn, obd, cluuid);
5550 lexp = class_conn2export(&conn);
5551 LASSERT(lexp != NULL);
5553 rc = nodemap_add_member(*client_nid, lexp);
5554 if (rc != 0 && rc != -EEXIST)
5557 rc = mdt_connect_internal(lexp, mdt, data);
5559 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5562 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5563 rc = tgt_client_new(env, lexp);
5565 mdt_export_stats_init(obd, lexp, localdata);
5569 class_disconnect(lexp);
5570 nodemap_del_member(lexp);
5574 /* Because we do not want this export to be evicted by pinger,
5575 * let's not add this export to the timed chain list. */
5576 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5577 spin_lock(&lexp->exp_obd->obd_dev_lock);
5578 list_del_init(&lexp->exp_obd_chain_timed);
5579 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5586 static int mdt_obd_reconnect(const struct lu_env *env,
5587 struct obd_export *exp, struct obd_device *obd,
5588 struct obd_uuid *cluuid,
5589 struct obd_connect_data *data,
5592 lnet_nid_t *client_nid = localdata;
5596 if (exp == NULL || obd == NULL || cluuid == NULL)
5599 rc = nodemap_add_member(*client_nid, exp);
5600 if (rc != 0 && rc != -EEXIST)
5603 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5605 mdt_export_stats_init(obd, exp, localdata);
5607 nodemap_del_member(exp);
5612 /* FIXME: Can we avoid using these two interfaces? */
5613 static int mdt_init_export(struct obd_export *exp)
5615 struct mdt_export_data *med = &exp->exp_mdt_data;
5619 INIT_LIST_HEAD(&med->med_open_head);
5620 spin_lock_init(&med->med_open_lock);
5621 spin_lock(&exp->exp_lock);
5622 exp->exp_connecting = 1;
5623 spin_unlock(&exp->exp_lock);
5625 /* self-export doesn't need client data and ldlm initialization */
5626 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5627 &exp->exp_client_uuid)))
5630 rc = tgt_client_alloc(exp);
5634 rc = ldlm_init_export(exp);
5641 tgt_client_free(exp);
5643 CERROR("%s: Failed to initialize export: rc = %d\n",
5644 exp->exp_obd->obd_name, rc);
5648 static int mdt_destroy_export(struct obd_export *exp)
5652 target_destroy_export(exp);
5653 /* destroy can be called from failed obd_setup, so
5654 * checking uuid is safer than obd_self_export */
5655 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5656 &exp->exp_client_uuid)))
5659 ldlm_destroy_export(exp);
5660 tgt_client_free(exp);
5662 LASSERT(list_empty(&exp->exp_outstanding_replies));
5663 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5668 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5669 struct linkea_data *ldata)
5673 LASSERT(ldata->ld_buf->lb_buf != NULL);
5675 if (!mdt_object_exists(mdt_obj))
5678 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5679 ldata->ld_buf, XATTR_NAME_LINK);
5680 if (rc == -ERANGE) {
5681 /* Buf was too small, figure out what we need. */
5682 lu_buf_free(ldata->ld_buf);
5683 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5684 ldata->ld_buf, XATTR_NAME_LINK);
5687 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5688 if (ldata->ld_buf->lb_buf == NULL)
5690 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5691 ldata->ld_buf, XATTR_NAME_LINK);
5696 return linkea_init_with_rec(ldata);
5700 * Given an MDT object, try to look up the full path to the object.
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] obj Object to do path lookup of
5705 * \param[in,out] fp User-provided struct to store path information
5706 * \param[in] root_fid Root FID of current path should reach
5708 * \retval 0 Lookup successful, path information stored in fp
5709 * \retval -EAGAIN Lookup failed, usually because object is being moved
5710 * \retval negative errno if there was a problem
5712 static int mdt_path_current(struct mdt_thread_info *info,
5713 struct mdt_object *obj,
5714 struct getinfo_fid2path *fp,
5715 struct lu_fid *root_fid)
5717 struct mdt_device *mdt = info->mti_mdt;
5718 struct mdt_object *mdt_obj;
5719 struct link_ea_header *leh;
5720 struct link_ea_entry *lee;
5721 struct lu_name *tmpname = &info->mti_name;
5722 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5723 struct lu_buf *buf = &info->mti_big_buf;
5726 struct linkea_data ldata = { NULL };
5731 /* temp buffer for path element, the buffer will be finally freed
5732 * in mdt_thread_info_fini */
5733 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5734 if (buf->lb_buf == NULL)
5738 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5741 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5743 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5744 struct lu_buf lmv_buf;
5746 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5747 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5748 GOTO(out, rc = -ENOENT);
5750 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5752 mdt_object_get(info->mti_env, mdt_obj);
5754 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5755 if (IS_ERR(mdt_obj))
5756 GOTO(out, rc = PTR_ERR(mdt_obj));
5759 if (!mdt_object_exists(mdt_obj)) {
5760 mdt_object_put(info->mti_env, mdt_obj);
5761 GOTO(out, rc = -ENOENT);
5764 if (mdt_object_remote(mdt_obj)) {
5765 mdt_object_put(info->mti_env, mdt_obj);
5766 GOTO(remote_out, rc = -EREMOTE);
5769 rc = mdt_links_read(info, mdt_obj, &ldata);
5771 mdt_object_put(info->mti_env, mdt_obj);
5776 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5777 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5778 /* If set, use link #linkno for path lookup, otherwise use
5779 link #0. Only do this for the final path element. */
5780 if (first && fp->gf_linkno < leh->leh_reccount) {
5782 for (count = 0; count < fp->gf_linkno; count++) {
5783 lee = (struct link_ea_entry *)
5784 ((char *)lee + reclen);
5785 linkea_entry_unpack(lee, &reclen, tmpname,
5788 if (fp->gf_linkno < leh->leh_reccount - 1)
5789 /* indicate to user there are more links */
5793 lmv_buf.lb_buf = info->mti_xattr_buf;
5794 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5795 /* Check if it is slave stripes */
5796 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5797 &lmv_buf, XATTR_NAME_LMV);
5798 mdt_object_put(info->mti_env, mdt_obj);
5800 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5802 /* For slave stripes, get its master */
5803 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5804 fp->gf_fid = *tmpfid;
5807 } else if (rc < 0 && rc != -ENODATA) {
5813 /* Pack the name in the end of the buffer */
5814 ptr -= tmpname->ln_namelen;
5815 if (ptr - 1 <= fp->gf_u.gf_path)
5816 GOTO(out, rc = -EOVERFLOW);
5817 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5820 /* keep the last resolved fid to the client, so the
5821 * client will build the left path on another MDT for
5823 fp->gf_fid = *tmpfid;
5829 ptr++; /* skip leading / */
5830 memmove(fp->gf_u.gf_path, ptr,
5831 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5838 * Given an MDT object, use mdt_path_current to get the path.
5839 * Essentially a wrapper to retry mdt_path_current a set number of times
5840 * if -EAGAIN is returned (usually because an object is being moved).
5842 * Part of the MDT layer implementation of lfs fid2path.
5844 * \param[in] info Per-thread common data shared by mdt level handlers.
5845 * \param[in] obj Object to do path lookup of
5846 * \param[in,out] fp User-provided struct for arguments and to store path
5849 * \retval 0 Lookup successful, path information stored in fp
5850 * \retval negative errno if there was a problem
5852 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5853 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5855 struct mdt_device *mdt = info->mti_mdt;
5860 if (fp->gf_pathlen < 3)
5863 if (root_fid == NULL)
5864 root_fid = &mdt->mdt_md_root_fid;
5866 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5867 fp->gf_u.gf_path[0] = '\0';
5871 /* Retry multiple times in case file is being moved */
5872 while (tries-- && rc == -EAGAIN)
5873 rc = mdt_path_current(info, obj, fp, root_fid);
5879 * Get the full path of the provided FID, as of changelog record recno.
5881 * This checks sanity and looks up object for user provided FID
5882 * before calling the actual path lookup code.
5884 * Part of the MDT layer implementation of lfs fid2path.
5886 * \param[in] info Per-thread common data shared by mdt level handlers.
5887 * \param[in,out] fp User-provided struct for arguments and to store path
5890 * \retval 0 Lookup successful, path information and recno stored in fp
5891 * \retval -ENOENT, object does not exist
5892 * \retval negative errno if there was a problem
5894 static int mdt_fid2path(struct mdt_thread_info *info,
5895 struct lu_fid *root_fid,
5896 struct getinfo_fid2path *fp)
5898 struct mdt_device *mdt = info->mti_mdt;
5899 struct mdt_object *obj;
5903 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5904 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5906 if (!fid_is_sane(&fp->gf_fid))
5909 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5910 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5911 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5912 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5916 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5919 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5920 PFID(&fp->gf_fid), rc);
5924 if (mdt_object_remote(obj))
5926 else if (!mdt_object_exists(obj))
5932 mdt_object_put(info->mti_env, obj);
5933 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5934 PFID(&fp->gf_fid), rc);
5938 rc = mdt_path(info, obj, fp, root_fid);
5940 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
5941 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5942 fp->gf_recno, fp->gf_linkno);
5944 mdt_object_put(info->mti_env, obj);
5949 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5950 void *val, int vallen)
5952 struct getinfo_fid2path *fpout, *fpin;
5953 struct lu_fid *root_fid = NULL;
5956 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5959 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5960 lustre_swab_fid2path(fpin);
5962 memcpy(fpout, fpin, sizeof(*fpin));
5963 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5966 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5967 sizeof(struct lu_fid)) {
5968 /* client sent its root FID, which is normally fileset FID */
5969 root_fid = fpin->gf_u.gf_root_fid;
5970 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5971 lustre_swab_lu_fid(root_fid);
5973 if (root_fid != NULL && !fid_is_sane(root_fid))
5977 rc = mdt_fid2path(info, root_fid, fpout);
5981 int mdt_get_info(struct tgt_session_info *tsi)
5991 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5993 CDEBUG(D_IOCTL, "No GETINFO key\n");
5994 RETURN(err_serious(-EFAULT));
5996 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5999 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
6000 if (vallen == NULL) {
6001 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
6002 tgt_name(tsi->tsi_tgt));
6003 RETURN(err_serious(-EFAULT));
6006 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6008 rc = req_capsule_server_pack(tsi->tsi_pill);
6010 RETURN(err_serious(rc));
6012 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
6013 if (valout == NULL) {
6014 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
6015 tgt_name(tsi->tsi_tgt));
6016 RETURN(err_serious(-EFAULT));
6019 if (KEY_IS(KEY_FID2PATH)) {
6020 struct mdt_thread_info *info = tsi2mdt_info(tsi);
6022 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
6023 mdt_thread_info_fini(info);
6030 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6032 struct obd_ioctl_data *data = karg;
6035 struct mdt_object *obj;
6036 struct mdt_lock_handle *lh;
6040 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6041 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6044 fid = (struct lu_fid *)data->ioc_inlbuf1;
6046 if (!fid_is_sane(fid))
6049 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6051 lh = &mti->mti_lh[MDT_LH_PARENT];
6052 mdt_lock_reg_init(lh, LCK_CR);
6054 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6056 RETURN(PTR_ERR(obj));
6058 if (mdt_object_remote(obj)) {
6061 * before calling version get the correct MDS should be
6062 * fid, this is error to find remote object here
6064 CERROR("nonlocal object "DFID"\n", PFID(fid));
6065 } else if (!mdt_object_exists(obj)) {
6066 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6069 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6070 *(__u64 *)data->ioc_inlbuf2 = version;
6073 mdt_object_unlock_put(mti, obj, lh, 1);
6077 /* ioctls on obd dev */
6078 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6079 void *karg, void __user *uarg)
6082 struct obd_device *obd = exp->exp_obd;
6083 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6084 struct dt_device *dt = mdt->mdt_bottom;
6088 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6089 rc = lu_env_init(&env, LCT_MD_THREAD);
6095 rc = mdt_device_sync(&env, mdt);
6097 case OBD_IOC_SET_READONLY:
6098 rc = dt_sync(&env, dt);
6100 rc = dt_ro(&env, dt);
6102 case OBD_IOC_ABORT_RECOVERY:
6103 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6104 obd->obd_abort_recovery = 1;
6105 target_stop_recovery_thread(obd);
6108 case OBD_IOC_CHANGELOG_REG:
6109 case OBD_IOC_CHANGELOG_DEREG:
6110 case OBD_IOC_CHANGELOG_CLEAR:
6111 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
6115 case OBD_IOC_START_LFSCK: {
6116 struct md_device *next = mdt->mdt_child;
6117 struct obd_ioctl_data *data = karg;
6118 struct lfsck_start_param lsp;
6120 if (unlikely(data == NULL)) {
6125 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6126 lsp.lsp_index_valid = 0;
6127 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6130 case OBD_IOC_STOP_LFSCK: {
6131 struct md_device *next = mdt->mdt_child;
6132 struct obd_ioctl_data *data = karg;
6133 struct lfsck_stop stop;
6135 stop.ls_status = LS_STOPPED;
6136 /* Old lfsck utils may pass NULL @stop. */
6137 if (data->ioc_inlbuf1 == NULL)
6141 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
6143 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
6146 case OBD_IOC_QUERY_LFSCK: {
6147 struct md_device *next = mdt->mdt_child;
6148 struct obd_ioctl_data *data = karg;
6150 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6154 case OBD_IOC_GET_OBJ_VERSION: {
6155 struct mdt_thread_info *mti;
6156 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6157 memset(mti, 0, sizeof *mti);
6158 mti->mti_env = &env;
6162 rc = mdt_ioc_version_get(mti, karg);
6165 case OBD_IOC_CATLOGLIST: {
6166 struct mdt_thread_info *mti;
6168 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6169 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6170 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6171 &mti->mti_tmp_fid1);
6176 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6177 mdt_obd_name(mdt), cmd, rc);
6184 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6186 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6190 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6191 struct lfsck_start_param lsp;
6193 lsp.lsp_start = NULL;
6194 lsp.lsp_index_valid = 0;
6195 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6196 OBD_IOC_START_LFSCK,
6198 if (rc != 0 && rc != -EALREADY)
6199 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6200 mdt_obd_name(mdt), rc);
6203 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6207 static int mdt_obd_postrecov(struct obd_device *obd)
6212 rc = lu_env_init(&env, LCT_MD_THREAD);
6215 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6220 static struct obd_ops mdt_obd_device_ops = {
6221 .o_owner = THIS_MODULE,
6222 .o_set_info_async = mdt_obd_set_info_async,
6223 .o_connect = mdt_obd_connect,
6224 .o_reconnect = mdt_obd_reconnect,
6225 .o_disconnect = mdt_obd_disconnect,
6226 .o_init_export = mdt_init_export,
6227 .o_destroy_export = mdt_destroy_export,
6228 .o_iocontrol = mdt_iocontrol,
6229 .o_postrecov = mdt_obd_postrecov,
6232 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6233 struct lu_device *d)
6235 struct mdt_device *m = mdt_dev(d);
6242 static struct lu_device *mdt_device_free(const struct lu_env *env,
6243 struct lu_device *d)
6245 struct mdt_device *m = mdt_dev(d);
6248 lu_device_fini(&m->mdt_lu_dev);
6254 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6255 struct lu_device_type *t,
6256 struct lustre_cfg *cfg)
6258 struct lu_device *l;
6259 struct mdt_device *m;
6266 rc = mdt_init0(env, m, t, cfg);
6268 mdt_device_free(env, l);
6273 l = ERR_PTR(-ENOMEM);
6277 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6278 LU_KEY_INIT(mdt, struct mdt_thread_info);
6280 static void mdt_key_fini(const struct lu_context *ctx,
6281 struct lu_context_key *key, void* data)
6283 struct mdt_thread_info *info = data;
6285 if (info->mti_big_lmm) {
6286 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6287 info->mti_big_lmm = NULL;
6288 info->mti_big_lmmsize = 0;
6293 /* context key: mdt_thread_key */
6294 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6296 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6298 return lu_ucred(info->mti_env);
6301 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6303 return lu_ucred_check(info->mti_env);
6307 * Enable/disable COS (Commit On Sharing).
6309 * Set/Clear the COS flag in mdt options.
6311 * \param mdt mdt device
6312 * \param val 0 disables COS, other values enable COS
6314 void mdt_enable_cos(struct mdt_device *mdt, int val)
6319 mdt->mdt_opts.mo_cos = !!val;
6320 rc = lu_env_init(&env, LCT_LOCAL);
6321 if (unlikely(rc != 0)) {
6322 CWARN("%s: lu_env initialization failed, cannot "
6323 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6326 mdt_device_sync(&env, mdt);
6331 * Check COS (Commit On Sharing) status.
6333 * Return COS flag status.
6335 * \param mdt mdt device
6337 int mdt_cos_is_enabled(struct mdt_device *mdt)
6339 return mdt->mdt_opts.mo_cos != 0;
6342 static struct lu_device_type_operations mdt_device_type_ops = {
6343 .ldto_device_alloc = mdt_device_alloc,
6344 .ldto_device_free = mdt_device_free,
6345 .ldto_device_fini = mdt_device_fini
6348 static struct lu_device_type mdt_device_type = {
6349 .ldt_tags = LU_DEVICE_MD,
6350 .ldt_name = LUSTRE_MDT_NAME,
6351 .ldt_ops = &mdt_device_type_ops,
6352 .ldt_ctx_tags = LCT_MD_THREAD
6355 static int __init mdt_init(void)
6359 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6360 FID_NOBRACE_LEN + 1);
6361 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6363 rc = lu_kmem_init(mdt_caches);
6367 rc = mds_mod_init();
6371 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6372 LUSTRE_MDT_NAME, &mdt_device_type);
6377 lu_kmem_fini(mdt_caches);
6384 static void __exit mdt_exit(void)
6386 class_unregister_type(LUSTRE_MDT_NAME);
6388 lu_kmem_fini(mdt_caches);
6391 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6392 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6393 MODULE_VERSION(LUSTRE_VERSION_STRING);
6394 MODULE_LICENSE("GPL");
6396 module_init(mdt_init);
6397 module_exit(mdt_exit);