4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_net.h>
57 #include <lustre_nodemap.h>
58 #include <lustre_mds.h>
59 #include <lustre_param.h>
60 #include <lustre_quota.h>
61 #include <lustre_swab.h>
63 #include <obd_support.h>
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 = full_name_hash(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 if (layout->li_start >= layout->li_end) {
1256 CERROR("Recieved an invalid layout change range [%llu, %llu) "
1257 "for "DFID"\n", layout->li_start, layout->li_end,
1258 PFID(mdt_object_fid(obj)));
1262 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1263 GOTO(out, rc = -EINVAL);
1265 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1270 /* take layout lock to prepare layout change */
1271 mdt_lock_reg_init(lh, LCK_EX);
1272 rc = mdt_object_lock(info, obj, lh,
1273 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
1277 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout,
1280 mdt_object_unlock(info, obj, lh, 1);
1286 * Exchange MOF_LOV_CREATED flags between two objects after a
1287 * layout swap. No assumption is made on whether o1 or o2 have
1288 * created objects or not.
1290 * \param[in,out] o1 First swap layout object
1291 * \param[in,out] o2 Second swap layout object
1293 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1295 unsigned int o1_lov_created = o1->mot_lov_created;
1297 mutex_lock(&o1->mot_lov_mutex);
1298 mutex_lock(&o2->mot_lov_mutex);
1300 o1->mot_lov_created = o2->mot_lov_created;
1301 o2->mot_lov_created = o1_lov_created;
1303 mutex_unlock(&o2->mot_lov_mutex);
1304 mutex_unlock(&o1->mot_lov_mutex);
1307 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1309 struct mdt_thread_info *info;
1310 struct ptlrpc_request *req = tgt_ses_req(tsi);
1311 struct obd_export *exp = req->rq_export;
1312 struct mdt_object *o1, *o2, *o;
1313 struct mdt_lock_handle *lh1, *lh2;
1314 struct mdc_swap_layouts *msl;
1318 /* client does not support layout lock, so layout swaping
1320 * FIXME: there is a problem for old clients which don't support
1321 * layout lock yet. If those clients have already opened the file
1322 * they won't be notified at all so that old layout may still be
1323 * used to do IO. This can be fixed after file release is landed by
1324 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1325 if (!exp_connect_layout(exp))
1326 RETURN(-EOPNOTSUPP);
1328 info = tsi2mdt_info(tsi);
1330 if (info->mti_dlm_req != NULL)
1331 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1333 o1 = info->mti_object;
1334 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1335 &info->mti_body->mbo_fid2);
1337 GOTO(out, rc = PTR_ERR(o));
1339 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1340 GOTO(put, rc = -ENOENT);
1342 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1343 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1349 /* permission check. Make sure the calling process having permission
1350 * to write both files. */
1351 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1356 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1361 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1363 GOTO(put, rc = -EPROTO);
1365 lh1 = &info->mti_lh[MDT_LH_NEW];
1366 mdt_lock_reg_init(lh1, LCK_EX);
1367 lh2 = &info->mti_lh[MDT_LH_OLD];
1368 mdt_lock_reg_init(lh2, LCK_EX);
1370 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1371 MDS_INODELOCK_XATTR);
1375 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1376 MDS_INODELOCK_XATTR);
1380 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1381 mdt_object_child(o2), msl->msl_flags);
1385 mdt_swap_lov_flag(o1, o2);
1388 mdt_object_unlock(info, o2, lh2, rc);
1390 mdt_object_unlock(info, o1, lh1, rc);
1392 mdt_object_put(info->mti_env, o);
1394 mdt_thread_info_fini(info);
1398 static int mdt_raw_lookup(struct mdt_thread_info *info,
1399 struct mdt_object *parent,
1400 const struct lu_name *lname,
1401 struct ldlm_reply *ldlm_rep)
1403 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1407 LASSERT(!info->mti_cross_ref);
1409 /* Only got the fid of this obj by name */
1410 fid_zero(child_fid);
1411 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1412 lname, child_fid, &info->mti_spec);
1414 struct mdt_body *repbody;
1416 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1417 repbody->mbo_fid1 = *child_fid;
1418 repbody->mbo_valid = OBD_MD_FLID;
1419 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1420 } else if (rc == -ENOENT) {
1421 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1428 * UPDATE lock should be taken against parent, and be released before exit;
1429 * child_bits lock should be taken against child, and be returned back:
1430 * (1)normal request should release the child lock;
1431 * (2)intent request will grant the lock to client.
1433 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1434 struct mdt_lock_handle *lhc,
1436 struct ldlm_reply *ldlm_rep)
1438 struct ptlrpc_request *req = mdt_info_req(info);
1439 struct mdt_body *reqbody = NULL;
1440 struct mdt_object *parent = info->mti_object;
1441 struct mdt_object *child;
1442 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1443 struct lu_name *lname = NULL;
1444 struct mdt_lock_handle *lhp = NULL;
1445 struct ldlm_lock *lock;
1452 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1453 LASSERT(ergo(is_resent,
1454 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1459 if (info->mti_cross_ref) {
1460 /* Only getattr on the child. Parent is on another node. */
1461 mdt_set_disposition(info, ldlm_rep,
1462 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1464 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1466 PFID(mdt_object_fid(child)), ldlm_rep);
1468 rc = mdt_check_resent_lock(info, child, lhc);
1471 } else if (rc > 0) {
1472 mdt_lock_handle_init(lhc);
1473 mdt_lock_reg_init(lhc, LCK_PR);
1476 * Object's name is on another MDS, no lookup or layout
1477 * lock is needed here but update lock is.
1479 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1480 MDS_INODELOCK_LAYOUT);
1481 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1483 rc = mdt_object_lock(info, child, lhc, child_bits);
1488 /* Finally, we can get attr for child. */
1489 if (!mdt_object_exists(child)) {
1490 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1492 "remote object doesn't exist.");
1493 mdt_object_unlock(info, child, lhc, 1);
1497 rc = mdt_getattr_internal(info, child, 0);
1498 if (unlikely(rc != 0))
1499 mdt_object_unlock(info, child, lhc, 1);
1504 lname = &info->mti_name;
1505 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1507 if (lu_name_is_valid(lname)) {
1508 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1509 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1510 PNAME(lname), ldlm_rep);
1512 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1513 if (unlikely(reqbody == NULL))
1514 RETURN(err_serious(-EPROTO));
1516 *child_fid = reqbody->mbo_fid2;
1518 if (unlikely(!fid_is_sane(child_fid)))
1519 RETURN(err_serious(-EINVAL));
1521 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1523 PFID(mdt_object_fid(parent)),
1524 PFID(&reqbody->mbo_fid2), ldlm_rep);
1527 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1529 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1530 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1532 "Parent doesn't exist!");
1536 if (mdt_object_remote(parent)) {
1537 CERROR("%s: parent "DFID" is on remote target\n",
1538 mdt_obd_name(info->mti_mdt),
1539 PFID(mdt_object_fid(parent)));
1543 if (lu_name_is_valid(lname)) {
1544 /* Always allow to lookup ".." */
1545 if (unlikely(lname->ln_namelen == 2 &&
1546 lname->ln_name[0] == '.' &&
1547 lname->ln_name[1] == '.'))
1548 info->mti_spec.sp_permitted = 1;
1550 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1551 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1556 /* step 1: lock parent only if parent is a directory */
1557 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1558 lhp = &info->mti_lh[MDT_LH_PARENT];
1559 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1560 rc = mdt_object_lock(info, parent, lhp,
1561 MDS_INODELOCK_UPDATE);
1562 if (unlikely(rc != 0))
1566 /* step 2: lookup child's fid by name */
1567 fid_zero(child_fid);
1568 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1569 child_fid, &info->mti_spec);
1571 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1574 GOTO(out_parent, rc);
1577 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1580 *step 3: find the child object by fid & lock it.
1581 * regardless if it is local or remote.
1583 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1584 * set parent dir fid the same as child fid in getattr by fid case
1585 * we should not lu_object_find() the object again, could lead
1586 * to hung if there is a concurrent unlink destroyed the object.
1588 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1589 mdt_object_get(info->mti_env, parent);
1592 child = mdt_object_find(info->mti_env, info->mti_mdt,
1596 if (unlikely(IS_ERR(child)))
1597 GOTO(out_parent, rc = PTR_ERR(child));
1599 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1600 if (!mdt_object_exists(child)) {
1601 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1603 "Object doesn't exist!");
1604 GOTO(out_child, rc = -ENOENT);
1607 rc = mdt_check_resent_lock(info, child, lhc);
1609 GOTO(out_child, rc);
1610 } else if (rc > 0) {
1611 mdt_lock_handle_init(lhc);
1612 mdt_lock_reg_init(lhc, LCK_PR);
1615 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1616 mdt_object_exists(child) && !mdt_object_remote(child)) {
1617 struct md_attr *ma = &info->mti_attr;
1620 ma->ma_need = MA_INODE;
1621 rc = mdt_attr_get_complex(info, child, ma);
1622 if (unlikely(rc != 0))
1623 GOTO(out_child, rc);
1625 /* If the file has not been changed for some time, we
1626 * return not only a LOOKUP lock, but also an UPDATE
1627 * lock and this might save us RPC on later STAT. For
1628 * directories, it also let negative dentry cache start
1629 * working for this dir. */
1630 if (ma->ma_valid & MA_INODE &&
1631 ma->ma_attr.la_valid & LA_CTIME &&
1632 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1633 ma->ma_attr.la_ctime < cfs_time_current_sec())
1634 child_bits |= MDS_INODELOCK_UPDATE;
1637 /* layout lock must be granted in a best-effort way
1638 * for IT operations */
1639 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1640 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1641 exp_connect_layout(info->mti_exp) &&
1642 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1643 !mdt_object_remote(child) && ldlm_rep != NULL) {
1644 /* try to grant layout lock for regular file. */
1650 child_bits |= MDS_INODELOCK_LAYOUT;
1651 /* try layout lock, it may fail to be granted due to
1652 * contention at LOOKUP or UPDATE */
1653 if (!mdt_object_lock_try(info, child, lhc,
1655 child_bits &= ~MDS_INODELOCK_LAYOUT;
1656 LASSERT(child_bits != 0);
1657 rc = mdt_object_lock(info, child, lhc,
1663 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1664 * client will enqueue the lock to the remote MDT */
1665 if (mdt_object_remote(child))
1666 child_bits &= ~MDS_INODELOCK_UPDATE;
1667 rc = mdt_object_lock(info, child, lhc, child_bits);
1669 if (unlikely(rc != 0))
1670 GOTO(out_child, rc);
1673 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1675 /* finally, we can get attr for child. */
1676 rc = mdt_getattr_internal(info, child, ma_need);
1677 if (unlikely(rc != 0)) {
1678 mdt_object_unlock(info, child, lhc, 1);
1680 /* Debugging code. */
1681 LDLM_DEBUG(lock, "Returning lock to client");
1682 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1683 &lock->l_resource->lr_name),
1684 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1685 PLDLMRES(lock->l_resource),
1686 PFID(mdt_object_fid(child)));
1689 LDLM_LOCK_PUT(lock);
1693 mdt_object_put(info->mti_env, child);
1696 mdt_object_unlock(info, parent, lhp, 1);
1700 /* normal handler: should release the child lock */
1701 static int mdt_getattr_name(struct tgt_session_info *tsi)
1703 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1704 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1705 struct mdt_body *reqbody;
1706 struct mdt_body *repbody;
1710 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1711 LASSERT(reqbody != NULL);
1712 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1713 LASSERT(repbody != NULL);
1715 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1716 repbody->mbo_eadatasize = 0;
1717 repbody->mbo_aclsize = 0;
1719 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1721 GOTO(out_shrink, rc);
1723 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1724 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1725 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1726 lhc->mlh_reg_lh.cookie = 0;
1728 mdt_exit_ucred(info);
1731 mdt_client_compatibility(info);
1732 rc2 = mdt_fix_reply(info);
1735 mdt_thread_info_fini(info);
1739 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1740 void *karg, void __user *uarg);
1742 static int mdt_set_info(struct tgt_session_info *tsi)
1744 struct ptlrpc_request *req = tgt_ses_req(tsi);
1747 int keylen, vallen, rc = 0;
1751 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1753 DEBUG_REQ(D_HA, req, "no set_info key");
1754 RETURN(err_serious(-EFAULT));
1757 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1760 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1762 DEBUG_REQ(D_HA, req, "no set_info val");
1763 RETURN(err_serious(-EFAULT));
1766 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1769 /* Swab any part of val you need to here */
1770 if (KEY_IS(KEY_READ_ONLY)) {
1771 spin_lock(&req->rq_export->exp_lock);
1773 *exp_connect_flags_ptr(req->rq_export) |=
1776 *exp_connect_flags_ptr(req->rq_export) &=
1777 ~OBD_CONNECT_RDONLY;
1778 spin_unlock(&req->rq_export->exp_lock);
1779 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1780 struct changelog_setinfo *cs = val;
1782 if (vallen != sizeof(*cs)) {
1783 CERROR("%s: bad changelog_clear setinfo size %d\n",
1784 tgt_name(tsi->tsi_tgt), vallen);
1787 if (ptlrpc_req_need_swab(req)) {
1788 __swab64s(&cs->cs_recno);
1789 __swab32s(&cs->cs_id);
1792 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1794 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1796 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1803 static int mdt_readpage(struct tgt_session_info *tsi)
1805 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1806 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1807 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1808 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1809 struct mdt_body *repbody;
1815 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1816 RETURN(err_serious(-ENOMEM));
1818 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1819 if (repbody == NULL || reqbody == NULL)
1820 RETURN(err_serious(-EFAULT));
1823 * prepare @rdpg before calling lower layers and transfer itself. Here
1824 * reqbody->size contains offset of where to start to read and
1825 * reqbody->nlink contains number bytes to read.
1827 rdpg->rp_hash = reqbody->mbo_size;
1828 if (rdpg->rp_hash != reqbody->mbo_size) {
1829 CERROR("Invalid hash: %#llx != %#llx\n",
1830 rdpg->rp_hash, reqbody->mbo_size);
1834 rdpg->rp_attrs = reqbody->mbo_mode;
1835 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1836 rdpg->rp_attrs |= LUDA_64BITHASH;
1837 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1838 exp_max_brw_size(tsi->tsi_exp));
1839 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1841 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1842 if (rdpg->rp_pages == NULL)
1845 for (i = 0; i < rdpg->rp_npages; ++i) {
1846 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1847 if (rdpg->rp_pages[i] == NULL)
1848 GOTO(free_rdpg, rc = -ENOMEM);
1851 /* call lower layers to fill allocated pages with directory data */
1852 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1854 GOTO(free_rdpg, rc);
1856 /* send pages to client */
1857 rc = tgt_sendpage(tsi, rdpg, rc);
1862 for (i = 0; i < rdpg->rp_npages; i++)
1863 if (rdpg->rp_pages[i] != NULL)
1864 __free_page(rdpg->rp_pages[i]);
1865 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1867 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1873 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1875 struct lu_ucred *uc = mdt_ucred_check(info);
1876 struct lu_attr *attr = &info->mti_attr.ma_attr;
1881 if (op != REINT_SETATTR) {
1882 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1883 attr->la_uid = uc->uc_fsuid;
1884 /* for S_ISGID, inherit gid from his parent, such work will be
1885 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1886 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1887 attr->la_gid = uc->uc_fsgid;
1893 static int mdt_reint_internal(struct mdt_thread_info *info,
1894 struct mdt_lock_handle *lhc,
1897 struct req_capsule *pill = info->mti_pill;
1898 struct mdt_body *repbody;
1903 rc = mdt_reint_unpack(info, op);
1905 CERROR("Can't unpack reint, rc %d\n", rc);
1906 RETURN(err_serious(rc));
1909 /* for replay (no_create) lmm is not needed, client has it already */
1910 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1911 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1914 /* llog cookies are always 0, the field is kept for compatibility */
1915 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1916 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1918 rc = req_capsule_server_pack(pill);
1920 CERROR("Can't pack response, rc %d\n", rc);
1921 RETURN(err_serious(rc));
1924 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1925 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1927 repbody->mbo_eadatasize = 0;
1928 repbody->mbo_aclsize = 0;
1931 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1933 /* for replay no cookkie / lmm need, because client have this already */
1934 if (info->mti_spec.no_create)
1935 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1936 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1938 rc = mdt_init_ucred_reint(info);
1940 GOTO(out_shrink, rc);
1942 rc = mdt_fix_attr_ucred(info, op);
1944 GOTO(out_ucred, rc = err_serious(rc));
1946 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1948 GOTO(out_ucred, rc);
1949 } else if (rc == 1) {
1950 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1951 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1952 GOTO(out_ucred, rc);
1954 rc = mdt_reint_rec(info, lhc);
1957 mdt_exit_ucred(info);
1959 mdt_client_compatibility(info);
1960 rc2 = mdt_fix_reply(info);
1966 static long mdt_reint_opcode(struct ptlrpc_request *req,
1967 const struct req_format **fmt)
1969 struct mdt_device *mdt;
1970 struct mdt_rec_reint *rec;
1973 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1975 opc = rec->rr_opcode;
1976 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1977 if (opc < REINT_MAX && fmt[opc] != NULL)
1978 req_capsule_extend(&req->rq_pill, fmt[opc]);
1980 mdt = mdt_exp2dev(req->rq_export);
1981 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1982 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1983 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1984 opc = err_serious(-EFAULT);
1987 opc = err_serious(-EFAULT);
1992 static int mdt_reint(struct tgt_session_info *tsi)
1996 static const struct req_format *reint_fmts[REINT_MAX] = {
1997 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1998 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1999 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2000 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2001 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2002 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2003 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2004 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2005 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
2010 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2012 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2014 * No lock possible here from client to pass it to reint code
2017 rc = mdt_reint_internal(info, NULL, opc);
2018 mdt_thread_info_fini(info);
2023 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2027 /* this should sync the whole device */
2028 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2030 struct dt_device *dt = mdt->mdt_bottom;
2034 rc = dt->dd_ops->dt_sync(env, dt);
2038 /* this should sync this object */
2039 static int mdt_object_sync(struct mdt_thread_info *info)
2041 struct md_object *next;
2045 if (!mdt_object_exists(info->mti_object)) {
2046 CWARN("%s: non existing object "DFID": rc = %d\n",
2047 mdt_obd_name(info->mti_mdt),
2048 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
2051 next = mdt_object_child(info->mti_object);
2052 rc = mo_object_sync(info->mti_env, next);
2057 static int mdt_sync(struct tgt_session_info *tsi)
2059 struct ptlrpc_request *req = tgt_ses_req(tsi);
2060 struct req_capsule *pill = tsi->tsi_pill;
2061 struct mdt_body *body;
2066 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2067 RETURN(err_serious(-ENOMEM));
2069 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2070 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2072 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2074 /* sync an object */
2075 rc = mdt_object_sync(info);
2077 const struct lu_fid *fid;
2078 struct lu_attr *la = &info->mti_attr.ma_attr;
2080 info->mti_attr.ma_need = MA_INODE;
2081 info->mti_attr.ma_valid = 0;
2082 rc = mdt_attr_get_complex(info, info->mti_object,
2085 body = req_capsule_server_get(pill,
2087 fid = mdt_object_fid(info->mti_object);
2088 mdt_pack_attr2body(info, body, la, fid);
2091 mdt_thread_info_fini(info);
2094 mdt_counter_incr(req, LPROC_MDT_SYNC);
2100 * Handle quota control requests to consult current usage/limit, but also
2101 * to configure quota enforcement
2103 static int mdt_quotactl(struct tgt_session_info *tsi)
2105 struct obd_export *exp = tsi->tsi_exp;
2106 struct req_capsule *pill = tsi->tsi_pill;
2107 struct obd_quotactl *oqctl, *repoqc;
2109 struct mdt_device *mdt = mdt_exp2dev(exp);
2110 struct lu_device *qmt = mdt->mdt_qmt_dev;
2111 struct lu_nodemap *nodemap;
2114 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2116 RETURN(err_serious(-EPROTO));
2118 rc = req_capsule_server_pack(pill);
2120 RETURN(err_serious(rc));
2122 nodemap = nodemap_get_from_exp(exp);
2123 if (IS_ERR(nodemap))
2124 RETURN(PTR_ERR(nodemap));
2126 switch (oqctl->qc_cmd) {
2127 /* master quotactl */
2130 if (!nodemap_can_setquota(nodemap))
2131 GOTO(out_nodemap, rc = -EPERM);
2135 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2136 /* slave quotactl */
2141 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2142 GOTO(out_nodemap, rc = -EFAULT);
2146 switch (oqctl->qc_type) {
2148 id = nodemap_map_id(nodemap, NODEMAP_UID,
2149 NODEMAP_CLIENT_TO_FS, id);
2152 id = nodemap_map_id(nodemap, NODEMAP_UID,
2153 NODEMAP_CLIENT_TO_FS, id);
2156 /* todo: check/map project id */
2160 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2162 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2164 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2166 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2167 barrier_exit(tsi->tsi_tgt->lut_bottom);
2169 if (oqctl->qc_id != id)
2170 swap(oqctl->qc_id, id);
2172 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2173 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2174 RETURN(-EINPROGRESS);
2177 switch (oqctl->qc_cmd) {
2183 /* forward quotactl request to QMT */
2184 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2189 /* slave quotactl */
2190 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2195 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2196 GOTO(out_nodemap, rc = -EFAULT);
2199 if (oqctl->qc_id != id)
2200 swap(oqctl->qc_id, id);
2207 nodemap_putref(nodemap);
2212 /** clone llog ctxt from child (mdd)
2213 * This allows remote llog (replicator) access.
2214 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2215 * context was originally set up, or we can handle them directly.
2216 * I choose the latter, but that means I need any llog
2217 * contexts set up by child to be accessable by the mdt. So we clone the
2218 * context into our context list here.
2220 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2223 struct md_device *next = mdt->mdt_child;
2224 struct llog_ctxt *ctxt;
2227 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2230 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2231 if (rc || ctxt == NULL) {
2235 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2237 CERROR("Can't set mdt ctxt %d\n", rc);
2242 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2243 struct mdt_device *mdt, int idx)
2245 struct llog_ctxt *ctxt;
2247 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2250 /* Put once for the get we just did, and once for the clone */
2251 llog_ctxt_put(ctxt);
2252 llog_ctxt_put(ctxt);
2257 * sec context handlers
2259 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2261 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2267 * quota request handlers
2269 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2271 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2272 struct lu_device *qmt = mdt->mdt_qmt_dev;
2277 RETURN(err_serious(-EOPNOTSUPP));
2279 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2283 struct mdt_object *mdt_object_new(const struct lu_env *env,
2284 struct mdt_device *d,
2285 const struct lu_fid *f)
2287 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2288 struct lu_object *o;
2289 struct mdt_object *m;
2292 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2293 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2294 if (unlikely(IS_ERR(o)))
2295 m = (struct mdt_object *)o;
2301 struct mdt_object *mdt_object_find(const struct lu_env *env,
2302 struct mdt_device *d,
2303 const struct lu_fid *f)
2305 struct lu_object *o;
2306 struct mdt_object *m;
2309 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2310 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2311 if (unlikely(IS_ERR(o)))
2312 m = (struct mdt_object *)o;
2320 * Asyncronous commit for mdt device.
2322 * Pass asynchonous commit call down the MDS stack.
2324 * \param env environment
2325 * \param mdt the mdt device
2327 static void mdt_device_commit_async(const struct lu_env *env,
2328 struct mdt_device *mdt)
2330 struct dt_device *dt = mdt->mdt_bottom;
2334 rc = dt->dd_ops->dt_commit_async(env, dt);
2335 if (unlikely(rc != 0))
2336 CWARN("%s: async commit start failed: rc = %d\n",
2337 mdt_obd_name(mdt), rc);
2338 atomic_inc(&mdt->mdt_async_commit_count);
2343 * Mark the lock as "synchonous".
2345 * Mark the lock to deffer transaction commit to the unlock time.
2347 * \param lock the lock to mark as "synchonous"
2349 * \see mdt_is_lock_sync
2350 * \see mdt_save_lock
2352 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2354 lock->l_ast_data = (void*)1;
2358 * Check whehter the lock "synchonous" or not.
2360 * \param lock the lock to check
2361 * \retval 1 the lock is "synchonous"
2362 * \retval 0 the lock isn't "synchronous"
2364 * \see mdt_set_lock_sync
2365 * \see mdt_save_lock
2367 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2369 return lock->l_ast_data != NULL;
2373 * Blocking AST for mdt locks.
2375 * Starts transaction commit if in case of COS lock conflict or
2376 * deffers such a commit to the mdt_save_lock.
2378 * \param lock the lock which blocks a request or cancelling lock
2379 * \param desc unused
2380 * \param data unused
2381 * \param flag indicates whether this cancelling or blocking callback
2383 * \see ldlm_blocking_ast_nocheck
2385 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2386 void *data, int flag)
2388 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2389 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2393 if (flag == LDLM_CB_CANCELING)
2396 lock_res_and_lock(lock);
2397 if (lock->l_blocking_ast != mdt_blocking_ast) {
2398 unlock_res_and_lock(lock);
2401 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2402 lock->l_blocking_lock != NULL) {
2403 if (mdt_cos_is_enabled(mdt) &&
2404 lock->l_client_cookie !=
2405 lock->l_blocking_lock->l_client_cookie)
2406 mdt_set_lock_sync(lock);
2407 else if (mdt_slc_is_enabled(mdt) &&
2408 ldlm_is_cos_incompat(lock->l_blocking_lock))
2409 mdt_set_lock_sync(lock);
2411 rc = ldlm_blocking_ast_nocheck(lock);
2413 /* There is no lock conflict if l_blocking_lock == NULL,
2414 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2415 * when the last reference to a local lock was released */
2416 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2419 rc = lu_env_init(&env, LCT_LOCAL);
2420 if (unlikely(rc != 0))
2421 CWARN("%s: lu_env initialization failed, cannot "
2422 "start asynchronous commit: rc = %d\n",
2425 mdt_device_commit_async(&env, mdt);
2432 * Blocking AST for cross-MDT lock
2434 * Discard lock from uncommitted_slc_locks and cancel it.
2436 * \param lock the lock which blocks a request or cancelling lock
2437 * \param desc unused
2438 * \param data unused
2439 * \param flag indicates whether this cancelling or blocking callback
2440 * \retval 0 on success
2441 * \retval negative number on error
2443 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2444 void *data, int flag)
2450 case LDLM_CB_BLOCKING: {
2451 struct lustre_handle lockh;
2453 ldlm_lock2handle(lock, &lockh);
2454 rc = ldlm_cli_cancel(&lockh,
2455 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2457 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2462 case LDLM_CB_CANCELING: {
2463 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2464 struct mdt_device *mdt =
2465 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2467 LDLM_DEBUG(lock, "Revoke remote lock\n");
2469 /* discard slc lock here so that it can be cleaned anytime,
2470 * especially for cleanup_resource() */
2471 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2473 /* once we cache lock, l_ast_data is set to mdt_object */
2474 if (lock->l_ast_data != NULL) {
2475 struct mdt_object *mo = lock->l_ast_data;
2478 rc = lu_env_init(&env, LCT_MD_THREAD);
2479 if (unlikely(rc != 0)) {
2480 CWARN("%s: lu_env initialization failed, object"
2481 "%p "DFID" is leaked!\n",
2483 PFID(mdt_object_fid(mo)));
2487 if (lock->l_policy_data.l_inodebits.bits &
2488 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2489 rc = mo_invalidate(&env, mdt_object_child(mo));
2490 mo->mot_cache_attr = 0;
2492 mdt_object_put(&env, mo);
2504 int mdt_check_resent_lock(struct mdt_thread_info *info,
2505 struct mdt_object *mo,
2506 struct mdt_lock_handle *lhc)
2508 /* the lock might already be gotten in ldlm_handle_enqueue() */
2509 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2510 struct ptlrpc_request *req = mdt_info_req(info);
2511 struct ldlm_lock *lock;
2513 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2514 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2516 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2517 * a resend case, however, it could be already destroyed
2518 * due to client eviction or a raced cancel RPC. */
2519 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2520 lhc->mlh_reg_lh.cookie);
2524 if (!fid_res_name_eq(mdt_object_fid(mo),
2525 &lock->l_resource->lr_name)) {
2526 CWARN("%s: Although resent, but still not "
2527 "get child lock:"DFID"\n",
2528 info->mti_exp->exp_obd->obd_name,
2529 PFID(mdt_object_fid(mo)));
2530 LDLM_LOCK_PUT(lock);
2533 LDLM_LOCK_PUT(lock);
2539 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2540 const struct lu_fid *fid, struct lustre_handle *lh,
2541 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2544 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2545 union ldlm_policy_data *policy = &mti->mti_policy;
2546 struct ldlm_res_id *res_id = &mti->mti_res_id;
2550 LASSERT(mdt_object_remote(o));
2552 fid_build_reg_res_name(fid, res_id);
2554 memset(einfo, 0, sizeof(*einfo));
2555 einfo->ei_type = LDLM_IBITS;
2556 einfo->ei_mode = mode;
2557 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2558 einfo->ei_cb_cp = ldlm_completion_ast;
2559 einfo->ei_enq_slave = 0;
2560 einfo->ei_res_id = res_id;
2562 einfo->ei_nonblock = 1;
2565 * if we cache lock, couple lock with mdt_object, so that object
2566 * can be easily found in lock ASTs.
2568 mdt_object_get(mti->mti_env, o);
2569 einfo->ei_cbdata = o;
2572 memset(policy, 0, sizeof(*policy));
2573 policy->l_inodebits.bits = ibits;
2575 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2577 if (rc < 0 && cache) {
2578 mdt_object_put(mti->mti_env, o);
2579 einfo->ei_cbdata = NULL;
2584 static int mdt_object_local_lock(struct mdt_thread_info *info,
2585 struct mdt_object *o,
2586 struct mdt_lock_handle *lh, __u64 ibits,
2587 bool nonblock, bool cos_incompat)
2589 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2590 union ldlm_policy_data *policy = &info->mti_policy;
2591 struct ldlm_res_id *res_id = &info->mti_res_id;
2596 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2597 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2598 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2599 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2602 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2603 lh->mlh_reg_mode == LCK_EX);
2604 dlmflags |= LDLM_FL_COS_INCOMPAT;
2605 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2606 dlmflags |= LDLM_FL_COS_ENABLED;
2609 /* Only enqueue LOOKUP lock for remote object */
2610 if (mdt_object_remote(o))
2611 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2613 if (lh->mlh_type == MDT_PDO_LOCK) {
2614 /* check for exists after object is locked */
2615 if (mdt_object_exists(o) == 0) {
2616 /* Non-existent object shouldn't have PDO lock */
2619 /* Non-dir object shouldn't have PDO lock */
2620 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2625 memset(policy, 0, sizeof(*policy));
2626 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2628 dlmflags |= LDLM_FL_ATOMIC_CB;
2630 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2633 * Take PDO lock on whole directory and build correct @res_id for lock
2634 * on part of directory.
2636 if (lh->mlh_pdo_hash != 0) {
2637 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2638 mdt_lock_pdo_mode(info, o, lh);
2639 if (lh->mlh_pdo_mode != LCK_NL) {
2641 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2642 * is never going to be sent to client and we do not
2643 * want it slowed down due to possible cancels.
2645 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2646 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2647 policy, res_id, dlmflags,
2648 info->mti_exp == NULL ? NULL :
2649 &info->mti_exp->exp_handle.h_cookie);
2650 if (unlikely(rc != 0))
2651 GOTO(out_unlock, rc);
2655 * Finish res_id initializing by name hash marking part of
2656 * directory which is taking modification.
2658 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2661 policy->l_inodebits.bits = ibits;
2664 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2665 * going to be sent to client. If it is - mdt_intent_policy() path will
2666 * fix it up and turn FL_LOCAL flag off.
2668 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2669 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2670 info->mti_exp == NULL ? NULL :
2671 &info->mti_exp->exp_handle.h_cookie);
2674 mdt_object_unlock(info, o, lh, 1);
2675 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2676 lh->mlh_pdo_hash != 0 &&
2677 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2678 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2684 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2685 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2688 struct mdt_lock_handle *local_lh = NULL;
2692 if (!mdt_object_remote(o)) {
2693 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2698 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2699 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2700 MDS_INODELOCK_XATTR);
2702 /* Only enqueue LOOKUP lock for remote object */
2703 if (ibits & MDS_INODELOCK_LOOKUP) {
2704 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2705 nonblock, cos_incompat);
2712 if (ibits & MDS_INODELOCK_UPDATE) {
2713 /* Sigh, PDO needs to enqueue 2 locks right now, but
2714 * enqueue RPC can only request 1 lock, to avoid extra
2715 * RPC, so it will instead enqueue EX lock for remote
2716 * object anyway XXX*/
2717 if (lh->mlh_type == MDT_PDO_LOCK &&
2718 lh->mlh_pdo_hash != 0) {
2719 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2720 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2721 PFID(mdt_object_fid(o)));
2722 lh->mlh_pdo_hash = 0;
2723 lh->mlh_rreg_mode = LCK_EX;
2724 lh->mlh_type = MDT_REG_LOCK;
2726 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2729 MDS_INODELOCK_UPDATE, nonblock,
2731 if (rc != ELDLM_OK) {
2732 if (local_lh != NULL)
2733 mdt_object_unlock(info, o, local_lh, rc);
2741 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2742 struct mdt_lock_handle *lh, __u64 ibits)
2744 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2747 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2748 struct mdt_lock_handle *lh, __u64 ibits,
2751 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2752 return mdt_object_lock_internal(info, o, lh, ibits, false,
2756 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2757 struct mdt_lock_handle *lh, __u64 ibits)
2759 struct mdt_lock_handle tmp = *lh;
2762 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2769 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2770 struct mdt_object *o, struct mdt_lock_handle *lh,
2771 __u64 ibits, bool cos_incompat)
2773 struct mdt_lock_handle tmp = *lh;
2776 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2777 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2785 * Save a lock within request object.
2787 * Keep the lock referenced until whether client ACK or transaction
2788 * commit happens or release the lock immediately depending on input
2789 * parameters. If COS is ON, a write lock is converted to COS lock
2792 * \param info thead info object
2793 * \param h lock handle
2794 * \param mode lock mode
2795 * \param decref force immediate lock releasing
2797 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2798 enum ldlm_mode mode, int decref)
2802 if (lustre_handle_is_used(h)) {
2803 if (decref || !info->mti_has_trans ||
2804 !(mode & (LCK_PW | LCK_EX))) {
2805 mdt_fid_unlock(h, mode);
2807 struct mdt_device *mdt = info->mti_mdt;
2808 struct ldlm_lock *lock = ldlm_handle2lock(h);
2809 struct ptlrpc_request *req = mdt_info_req(info);
2812 cos = (mdt_cos_is_enabled(mdt) ||
2813 mdt_slc_is_enabled(mdt));
2815 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2818 /* there is no request if mdt_object_unlock() is called
2819 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2820 if (likely(req != NULL)) {
2821 CDEBUG(D_HA, "request = %p reply state = %p"
2822 " transno = %lld\n", req,
2823 req->rq_reply_state, req->rq_transno);
2825 ldlm_lock_downgrade(lock, LCK_COS);
2828 ptlrpc_save_lock(req, h, mode, cos);
2830 mdt_fid_unlock(h, mode);
2832 if (mdt_is_lock_sync(lock)) {
2833 CDEBUG(D_HA, "found sync-lock,"
2834 " async commit started\n");
2835 mdt_device_commit_async(info->mti_env,
2838 LDLM_LOCK_PUT(lock);
2847 * Save cross-MDT lock in uncommitted_slc_locks
2849 * Keep the lock referenced until transaction commit happens or release the lock
2850 * immediately depending on input parameters.
2852 * \param info thead info object
2853 * \param h lock handle
2854 * \param mode lock mode
2855 * \param decref force immediate lock releasing
2857 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2858 struct mdt_object *o, struct lustre_handle *h,
2859 enum ldlm_mode mode, int decref)
2863 if (lustre_handle_is_used(h)) {
2864 struct ldlm_lock *lock = ldlm_handle2lock(h);
2867 (lock->l_policy_data.l_inodebits.bits &
2868 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2869 mo_invalidate(info->mti_env, mdt_object_child(o));
2871 if (decref || !info->mti_has_trans ||
2872 !(mode & (LCK_PW | LCK_EX))) {
2873 ldlm_lock_decref_and_cancel(h, mode);
2874 LDLM_LOCK_PUT(lock);
2876 struct ptlrpc_request *req = mdt_info_req(info);
2878 LASSERT(req != NULL);
2879 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2881 ldlm_lock_decref(h, mode);
2890 * Unlock mdt object.
2892 * Immeditely release the regular lock and the PDO lock or save the
2893 * lock in request and keep them referenced until client ACK or
2894 * transaction commit.
2896 * \param info thread info object
2897 * \param o mdt object
2898 * \param lh mdt lock handle referencing regular and PDO locks
2899 * \param decref force immediate lock releasing
2901 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2903 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2904 struct mdt_lock_handle *lh, int decref)
2908 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2909 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2910 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2916 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2917 const struct lu_fid *f,
2918 struct mdt_lock_handle *lh,
2921 struct mdt_object *o;
2923 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2927 rc = mdt_object_lock(info, o, lh, ibits);
2929 mdt_object_put(info->mti_env, o);
2936 void mdt_object_unlock_put(struct mdt_thread_info * info,
2937 struct mdt_object * o,
2938 struct mdt_lock_handle *lh,
2941 mdt_object_unlock(info, o, lh, decref);
2942 mdt_object_put(info->mti_env, o);
2946 * Generic code handling requests that have struct mdt_body passed in:
2948 * - extract mdt_body from request and save it in @info, if present;
2950 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2953 * - if HABEO_CORPUS flag is set for this request type check whether object
2954 * actually exists on storage (lu_object_exists()).
2957 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2959 const struct mdt_body *body;
2960 struct mdt_object *obj;
2961 const struct lu_env *env;
2962 struct req_capsule *pill;
2966 env = info->mti_env;
2967 pill = info->mti_pill;
2969 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2973 if (!(body->mbo_valid & OBD_MD_FLID))
2976 if (!fid_is_sane(&body->mbo_fid1)) {
2977 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2981 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2983 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2984 mdt_object_put(env, obj);
2987 info->mti_object = obj;
2996 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2998 struct req_capsule *pill = info->mti_pill;
3002 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3003 rc = mdt_body_unpack(info, flags);
3007 if (rc == 0 && (flags & HABEO_REFERO)) {
3009 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3010 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3012 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3013 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3016 rc = req_capsule_server_pack(pill);
3021 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3023 lh->mlh_type = MDT_NUL_LOCK;
3024 lh->mlh_reg_lh.cookie = 0ull;
3025 lh->mlh_reg_mode = LCK_MINMODE;
3026 lh->mlh_pdo_lh.cookie = 0ull;
3027 lh->mlh_pdo_mode = LCK_MINMODE;
3028 lh->mlh_rreg_lh.cookie = 0ull;
3029 lh->mlh_rreg_mode = LCK_MINMODE;
3032 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3034 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3035 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3039 * Initialize fields of struct mdt_thread_info. Other fields are left in
3040 * uninitialized state, because it's too expensive to zero out whole
3041 * mdt_thread_info (> 1K) on each request arrival.
3043 void mdt_thread_info_init(struct ptlrpc_request *req,
3044 struct mdt_thread_info *info)
3048 info->mti_pill = &req->rq_pill;
3051 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3052 mdt_lock_handle_init(&info->mti_lh[i]);
3054 /* mdt device: it can be NULL while CONNECT */
3055 if (req->rq_export) {
3056 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3057 info->mti_exp = req->rq_export;
3059 info->mti_mdt = NULL;
3060 info->mti_env = req->rq_svc_thread->t_env;
3061 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3063 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3064 info->mti_big_buf = LU_BUF_NULL;
3065 info->mti_body = NULL;
3066 info->mti_object = NULL;
3067 info->mti_dlm_req = NULL;
3068 info->mti_has_trans = 0;
3069 info->mti_cross_ref = 0;
3070 info->mti_opdata = 0;
3071 info->mti_big_lmm_used = 0;
3073 info->mti_spec.no_create = 0;
3074 info->mti_spec.sp_rm_entry = 0;
3075 info->mti_spec.sp_permitted = 0;
3076 info->mti_spec.sp_migrate_close = 0;
3078 info->mti_spec.u.sp_ea.eadata = NULL;
3079 info->mti_spec.u.sp_ea.eadatalen = 0;
3082 void mdt_thread_info_fini(struct mdt_thread_info *info)
3086 if (info->mti_object != NULL) {
3087 mdt_object_put(info->mti_env, info->mti_object);
3088 info->mti_object = NULL;
3091 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3092 mdt_lock_handle_fini(&info->mti_lh[i]);
3093 info->mti_env = NULL;
3094 info->mti_pill = NULL;
3095 info->mti_exp = NULL;
3097 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3098 lu_buf_free(&info->mti_big_buf);
3101 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3103 struct mdt_thread_info *mti;
3105 mti = mdt_th_info(tsi->tsi_env);
3106 LASSERT(mti != NULL);
3108 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3109 if (tsi->tsi_corpus != NULL) {
3110 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3111 lu_object_get(tsi->tsi_corpus);
3113 mti->mti_body = tsi->tsi_mdt_body;
3114 mti->mti_dlm_req = tsi->tsi_dlm_req;
3119 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3121 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3123 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3124 set_current_state(TASK_UNINTERRUPTIBLE);
3125 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3128 return tgt_connect(tsi);
3146 static int mdt_intent_getattr(enum mdt_it_code opcode,
3147 struct mdt_thread_info *info,
3148 struct ldlm_lock **,
3151 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3152 struct mdt_thread_info *info,
3153 struct ldlm_lock **lockp,
3156 static int mdt_intent_layout(enum mdt_it_code opcode,
3157 struct mdt_thread_info *info,
3158 struct ldlm_lock **,
3160 static int mdt_intent_reint(enum mdt_it_code opcode,
3161 struct mdt_thread_info *info,
3162 struct ldlm_lock **,
3165 static struct mdt_it_flavor {
3166 const struct req_format *it_fmt;
3168 int (*it_act)(enum mdt_it_code ,
3169 struct mdt_thread_info *,
3170 struct ldlm_lock **,
3173 } mdt_it_flavor[] = {
3175 .it_fmt = &RQF_LDLM_INTENT,
3176 /*.it_flags = HABEO_REFERO,*/
3178 .it_act = mdt_intent_reint,
3179 .it_reint = REINT_OPEN
3182 .it_fmt = &RQF_LDLM_INTENT,
3184 * OCREAT is not a MUTABOR request as if the file
3186 * We do the extra check of OBD_CONNECT_RDONLY in
3187 * mdt_reint_open() when we really need to create
3191 .it_act = mdt_intent_reint,
3192 .it_reint = REINT_OPEN
3195 .it_fmt = &RQF_LDLM_INTENT,
3196 .it_flags = MUTABOR,
3197 .it_act = mdt_intent_reint,
3198 .it_reint = REINT_CREATE
3200 [MDT_IT_GETATTR] = {
3201 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3202 .it_flags = HABEO_REFERO,
3203 .it_act = mdt_intent_getattr
3205 [MDT_IT_READDIR] = {
3211 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3212 .it_flags = HABEO_REFERO,
3213 .it_act = mdt_intent_getattr
3216 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3217 .it_flags = MUTABOR,
3219 .it_reint = REINT_UNLINK
3223 .it_flags = MUTABOR,
3226 [MDT_IT_GETXATTR] = {
3227 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3228 .it_flags = HABEO_CORPUS,
3229 .it_act = mdt_intent_getxattr
3232 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3234 .it_act = mdt_intent_layout
3239 mdt_intent_lock_replace(struct mdt_thread_info *info,
3240 struct ldlm_lock **lockp,
3241 struct mdt_lock_handle *lh,
3242 __u64 flags, int result)
3244 struct ptlrpc_request *req = mdt_info_req(info);
3245 struct ldlm_lock *lock = *lockp;
3246 struct ldlm_lock *new_lock;
3248 /* If possible resent found a lock, @lh is set to its handle */
3249 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3251 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3252 lh->mlh_reg_lh.cookie = 0;
3256 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3257 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3258 * a resend case, however, it could be already destroyed
3259 * due to client eviction or a raced cancel RPC. */
3260 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3261 lh->mlh_reg_lh.cookie);
3262 lh->mlh_reg_lh.cookie = 0;
3266 LASSERTF(new_lock != NULL,
3267 "lockh %#llx flags %#llx : rc = %d\n",
3268 lh->mlh_reg_lh.cookie, flags, result);
3271 * If we've already given this lock to a client once, then we should
3272 * have no readers or writers. Otherwise, we should have one reader
3273 * _or_ writer ref (which will be zeroed below) before returning the
3276 if (new_lock->l_export == req->rq_export) {
3277 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3279 LASSERT(new_lock->l_export == NULL);
3280 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3285 if (new_lock->l_export == req->rq_export) {
3287 * Already gave this to the client, which means that we
3288 * reconstructed a reply.
3290 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3293 LDLM_LOCK_RELEASE(new_lock);
3294 lh->mlh_reg_lh.cookie = 0;
3295 RETURN(ELDLM_LOCK_REPLACED);
3299 * Fixup the lock to be given to the client.
3301 lock_res_and_lock(new_lock);
3302 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3303 * possible blocking AST. */
3304 while (new_lock->l_readers > 0) {
3305 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3306 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3307 new_lock->l_readers--;
3309 while (new_lock->l_writers > 0) {
3310 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3311 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3312 new_lock->l_writers--;
3315 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3316 new_lock->l_blocking_ast = lock->l_blocking_ast;
3317 new_lock->l_completion_ast = lock->l_completion_ast;
3318 new_lock->l_remote_handle = lock->l_remote_handle;
3319 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3321 unlock_res_and_lock(new_lock);
3323 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3324 &new_lock->l_remote_handle,
3325 &new_lock->l_exp_hash);
3327 LDLM_LOCK_RELEASE(new_lock);
3328 lh->mlh_reg_lh.cookie = 0;
3330 RETURN(ELDLM_LOCK_REPLACED);
3333 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3334 struct ldlm_lock *new_lock,
3335 struct mdt_lock_handle *lh,
3338 struct ptlrpc_request *req = mdt_info_req(info);
3339 struct ldlm_request *dlmreq;
3341 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3344 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3346 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3347 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3349 if (flags & LDLM_FL_RESENT) {
3350 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3351 lh->mlh_reg_mode = new_lock->l_granted_mode;
3353 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3354 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3355 lh->mlh_reg_lh.cookie);
3360 * If the xid matches, then we know this is a resent request, and allow
3361 * it. (It's probably an OPEN, for which we don't send a lock.
3363 if (req_can_reconstruct(req, NULL))
3367 * This remote handle isn't enqueued, so we never received or processed
3368 * this request. Clear MSG_RESENT, because it can be handled like any
3369 * normal request now.
3371 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3373 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3374 dlmreq->lock_handle[0].cookie);
3377 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3378 struct mdt_thread_info *info,
3379 struct ldlm_lock **lockp,
3382 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3383 struct ldlm_reply *ldlm_rep = NULL;
3388 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3389 * (for the resend case) or a new lock. Below we will use it to
3390 * replace the original lock.
3392 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3393 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3394 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3395 rc = mdt_object_lock(info, info->mti_object, lhc,
3396 MDS_INODELOCK_XATTR);
3401 rc = mdt_getxattr(info);
3403 if (mdt_info_req(info)->rq_repmsg != NULL)
3404 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3406 if (ldlm_rep == NULL ||
3407 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3408 mdt_object_unlock(info, info->mti_object, lhc, 1);
3409 RETURN(err_serious(-EFAULT));
3412 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3414 /* This is left for interop instead of adding a new interop flag.
3416 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3417 if (ldlm_rep->lock_policy_res2) {
3418 mdt_object_unlock(info, info->mti_object, lhc, 1);
3419 RETURN(ELDLM_LOCK_ABORTED);
3423 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3427 static int mdt_intent_getattr(enum mdt_it_code opcode,
3428 struct mdt_thread_info *info,
3429 struct ldlm_lock **lockp,
3432 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3434 struct ldlm_reply *ldlm_rep;
3435 struct mdt_body *reqbody;
3436 struct mdt_body *repbody;
3440 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3443 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3446 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3447 repbody->mbo_eadatasize = 0;
3448 repbody->mbo_aclsize = 0;
3452 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3454 case MDT_IT_GETATTR:
3455 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3459 CERROR("Unsupported intent (%d)\n", opcode);
3460 GOTO(out_shrink, rc = -EINVAL);
3463 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3465 GOTO(out_shrink, rc);
3467 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3468 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3470 /* Get lock from request for possible resent case. */
3471 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3473 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3474 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3476 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3477 ldlm_rep->lock_policy_res2 = 0;
3478 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3479 ldlm_rep->lock_policy_res2) {
3480 lhc->mlh_reg_lh.cookie = 0ull;
3481 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3484 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3487 mdt_exit_ucred(info);
3489 mdt_client_compatibility(info);
3490 rc2 = mdt_fix_reply(info);
3496 static int mdt_intent_layout(enum mdt_it_code opcode,
3497 struct mdt_thread_info *info,
3498 struct ldlm_lock **lockp,
3501 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3502 struct layout_intent *layout;
3504 struct mdt_object *obj = NULL;
3505 bool layout_change = false;
3506 int layout_size = 0;
3510 if (opcode != MDT_IT_LAYOUT) {
3511 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3516 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3520 switch (layout->li_opc) {
3521 case LAYOUT_INTENT_TRUNC:
3522 case LAYOUT_INTENT_WRITE:
3523 layout_change = true;
3525 case LAYOUT_INTENT_ACCESS:
3527 case LAYOUT_INTENT_READ:
3528 case LAYOUT_INTENT_GLIMPSE:
3529 case LAYOUT_INTENT_RELEASE:
3530 case LAYOUT_INTENT_RESTORE:
3531 CERROR("%s: Unsupported layout intent opc %d\n",
3532 mdt_obd_name(info->mti_mdt), layout->li_opc);
3536 CERROR("%s: Unknown layout intent opc %d\n",
3537 mdt_obd_name(info->mti_mdt), layout->li_opc);
3544 fid = &info->mti_tmp_fid2;
3545 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3547 /* Get lock from request for possible resent case. */
3548 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3550 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3552 GOTO(out, rc = PTR_ERR(obj));
3554 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3555 layout_size = mdt_attr_get_eabuf_size(info, obj);
3556 if (layout_size < 0)
3557 GOTO(out_obj, rc = layout_size);
3559 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3560 info->mti_mdt->mdt_max_mdsize = layout_size;
3564 * set reply buffer size, so that ldlm_handle_enqueue0()->
3565 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
3567 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3568 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3570 rc = req_capsule_server_pack(info->mti_pill);
3575 if (layout_change) {
3576 struct lu_buf *buf = &info->mti_buf;
3580 if (unlikely(req_is_replay(mdt_info_req(info)))) {
3581 buf->lb_buf = req_capsule_client_get(info->mti_pill,
3583 buf->lb_len = req_capsule_get_size(info->mti_pill,
3584 &RMF_EADATA, RCL_CLIENT);
3586 * If it's a replay of layout write intent RPC, the
3587 * client has saved the extended lovea when
3588 * it get reply then.
3590 if (buf->lb_len > 0)
3591 mdt_fix_lov_magic(info, buf->lb_buf);
3595 * Instantiate some layout components, if @buf contains
3596 * lovea, then it's a replay of the layout intent write
3599 rc = mdt_layout_change(info, obj, layout, buf);
3604 mdt_object_put(info->mti_env, obj);
3606 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3607 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3610 lhc->mlh_reg_lh.cookie = 0;
3615 static int mdt_intent_reint(enum mdt_it_code opcode,
3616 struct mdt_thread_info *info,
3617 struct ldlm_lock **lockp,
3620 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3621 struct ldlm_reply *rep = NULL;
3625 static const struct req_format *intent_fmts[REINT_MAX] = {
3626 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3627 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3632 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3636 if (mdt_it_flavor[opcode].it_reint != opc) {
3637 CERROR("Reint code %ld doesn't match intent: %d\n",
3639 RETURN(err_serious(-EPROTO));
3642 /* Get lock from request for possible resent case. */
3643 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3645 rc = mdt_reint_internal(info, lhc, opc);
3647 /* Check whether the reply has been packed successfully. */
3648 if (mdt_info_req(info)->rq_repmsg != NULL)
3649 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3651 RETURN(err_serious(-EFAULT));
3653 /* MDC expects this in any case */
3655 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3657 /* the open lock or the lock for cross-ref object should be
3658 * returned to the client */
3659 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3660 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3661 rep->lock_policy_res2 = 0;
3662 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3666 rep->lock_policy_res2 = clear_serious(rc);
3668 if (rep->lock_policy_res2 == -ENOENT &&
3669 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3670 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3671 rep->lock_policy_res2 = 0;
3673 lhc->mlh_reg_lh.cookie = 0ull;
3674 if (rc == -ENOTCONN || rc == -ENODEV ||
3675 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3677 * If it is the disconnect error (ENODEV & ENOCONN), the error
3678 * will be returned by rq_status, and client at ptlrpc layer
3679 * will detect this, then disconnect, reconnect the import
3680 * immediately, instead of impacting the following the rpc.
3685 * For other cases, the error will be returned by intent, and client
3686 * will retrieve the result from intent.
3688 RETURN(ELDLM_LOCK_ABORTED);
3691 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3699 case IT_OPEN|IT_CREAT:
3706 rc = MDT_IT_READDIR;
3709 rc = MDT_IT_GETATTR;
3721 rc = MDT_IT_GETXATTR;
3726 case IT_QUOTA_DQACQ:
3731 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3738 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3739 struct mdt_thread_info *info,
3740 struct ldlm_lock **lockp, __u64 flags)
3742 struct req_capsule *pill = info->mti_pill;
3743 struct ptlrpc_request *req = mdt_info_req(info);
3744 struct mdt_it_flavor *flv;
3749 opc = mdt_intent_code(itopc);
3753 if (opc == MDT_IT_QUOTA) {
3754 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3757 RETURN(-EOPNOTSUPP);
3759 if (mdt_rdonly(req->rq_export))
3762 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3763 /* pass the request to quota master */
3764 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3765 mdt_info_req(info), lockp,
3770 flv = &mdt_it_flavor[opc];
3771 if (flv->it_fmt != NULL)
3772 req_capsule_extend(pill, flv->it_fmt);
3774 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3778 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3781 if (flv->it_act != NULL) {
3782 struct ldlm_reply *rep;
3784 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3786 /* execute policy */
3787 rc = flv->it_act(opc, info, lockp, flags);
3789 /* Check whether the reply has been packed successfully. */
3790 if (req->rq_repmsg != NULL) {
3791 rep = req_capsule_server_get(info->mti_pill,
3793 rep->lock_policy_res2 =
3794 ptlrpc_status_hton(rep->lock_policy_res2);
3801 static int mdt_intent_policy(struct ldlm_namespace *ns,
3802 struct ldlm_lock **lockp, void *req_cookie,
3803 enum ldlm_mode mode, __u64 flags, void *data)
3805 struct tgt_session_info *tsi;
3806 struct mdt_thread_info *info;
3807 struct ptlrpc_request *req = req_cookie;
3808 struct ldlm_intent *it;
3809 struct req_capsule *pill;
3814 LASSERT(req != NULL);
3816 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3818 info = tsi2mdt_info(tsi);
3819 LASSERT(info != NULL);
3820 pill = info->mti_pill;
3821 LASSERT(pill->rc_req == req);
3823 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3824 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3825 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3827 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3831 /* Lock without inodebits makes no sense and will oops
3832 * later in ldlm. Let's check it now to see if we have
3833 * ibits corrupted somewhere in mdt_intent_opc().
3834 * The case for client miss to set ibits has been
3835 * processed by others. */
3836 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3837 lr_type == LDLM_IBITS,
3838 info->mti_dlm_req->lock_desc.\
3839 l_policy_data.l_inodebits.bits != 0));
3841 rc = err_serious(-EFAULT);
3843 /* No intent was provided */
3844 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3845 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3846 rc = req_capsule_server_pack(pill);
3848 rc = err_serious(rc);
3850 mdt_thread_info_fini(info);
3854 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3856 struct seq_server_site *ss = mdt_seq_site(mdt);
3858 if (ss->ss_node_id == 0)
3861 if (ss->ss_client_seq != NULL) {
3862 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3863 ss->ss_client_seq->lcs_exp = NULL;
3866 if (ss->ss_server_fld != NULL) {
3867 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3868 ss->ss_server_fld->lsf_control_exp = NULL;
3872 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3874 struct seq_server_site *ss = mdt_seq_site(mdt);
3879 if (ss->ss_server_seq != NULL)
3880 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3883 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3885 mdt_seq_fini_cli(mdt);
3886 mdt_deregister_seq_exp(mdt);
3888 return seq_site_fini(env, mdt_seq_site(mdt));
3892 * It will retrieve its FLDB entries from MDT0, and it only happens
3893 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3894 * and it needs to refresh FLDB from the MDT0.
3896 static int mdt_register_lwp_callback(void *data)
3899 struct mdt_device *mdt = data;
3900 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3904 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3906 rc = lu_env_init(&env, LCT_MD_THREAD);
3908 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3912 /* Allocate new sequence now to avoid creating local transaction
3913 * in the normal transaction process */
3914 rc = seq_server_check_and_alloc_super(&env,
3915 mdt_seq_site(mdt)->ss_server_seq);
3920 rc = fld_update_from_controller(&env, fld);
3922 CERROR("%s: cannot update controller: rc = %d\n",
3923 mdt_obd_name(mdt), rc);
3932 static int mdt_register_seq_exp(struct mdt_device *mdt)
3934 struct seq_server_site *ss = mdt_seq_site(mdt);
3935 char *lwp_name = NULL;
3938 if (ss->ss_node_id == 0)
3941 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3942 if (lwp_name == NULL)
3943 GOTO(out_free, rc = -ENOMEM);
3945 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3949 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3954 rc = lustre_register_lwp_item(lwp_name,
3955 &ss->ss_server_fld->lsf_control_exp,
3956 mdt_register_lwp_callback, mdt);
3958 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3959 ss->ss_client_seq->lcs_exp = NULL;
3963 if (lwp_name != NULL)
3964 OBD_FREE(lwp_name, MAX_OBD_NAME);
3970 * Init client sequence manager which is used by local MDS to talk to sequence
3971 * controller on remote node.
3973 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3975 struct seq_server_site *ss = mdt_seq_site(mdt);
3980 /* check if this is adding the first MDC and controller is not yet
3982 OBD_ALLOC_PTR(ss->ss_client_seq);
3983 if (ss->ss_client_seq == NULL)
3986 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3987 if (prefix == NULL) {
3988 OBD_FREE_PTR(ss->ss_client_seq);
3989 ss->ss_client_seq = NULL;
3993 /* Note: seq_client_fini will be called in seq_site_fini */
3994 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3995 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3996 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3998 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4000 OBD_FREE_PTR(ss->ss_client_seq);
4001 ss->ss_client_seq = NULL;
4005 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
4010 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4012 struct seq_server_site *ss;
4016 ss = mdt_seq_site(mdt);
4017 /* init sequence controller server(MDT0) */
4018 if (ss->ss_node_id == 0) {
4019 OBD_ALLOC_PTR(ss->ss_control_seq);
4020 if (ss->ss_control_seq == NULL)
4023 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4024 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4027 GOTO(out_seq_fini, rc);
4030 /* Init normal sequence server */
4031 OBD_ALLOC_PTR(ss->ss_server_seq);
4032 if (ss->ss_server_seq == NULL)
4033 GOTO(out_seq_fini, rc = -ENOMEM);
4035 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4036 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4038 GOTO(out_seq_fini, rc);
4040 /* init seq client for seq server to talk to seq controller(MDT0) */
4041 rc = mdt_seq_init_cli(env, mdt);
4043 GOTO(out_seq_fini, rc);
4045 if (ss->ss_node_id != 0)
4046 /* register controller export through lwp */
4047 rc = mdt_register_seq_exp(mdt);
4052 mdt_seq_fini(env, mdt);
4060 static int mdt_fld_fini(const struct lu_env *env,
4061 struct mdt_device *m)
4063 struct seq_server_site *ss = mdt_seq_site(m);
4066 if (ss && ss->ss_server_fld) {
4067 fld_server_fini(env, ss->ss_server_fld);
4068 OBD_FREE_PTR(ss->ss_server_fld);
4069 ss->ss_server_fld = NULL;
4075 static int mdt_fld_init(const struct lu_env *env,
4077 struct mdt_device *m)
4079 struct seq_server_site *ss;
4083 ss = mdt_seq_site(m);
4085 OBD_ALLOC_PTR(ss->ss_server_fld);
4086 if (ss->ss_server_fld == NULL)
4087 RETURN(rc = -ENOMEM);
4089 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4092 OBD_FREE_PTR(ss->ss_server_fld);
4093 ss->ss_server_fld = NULL;
4100 static void mdt_stack_pre_fini(const struct lu_env *env,
4101 struct mdt_device *m, struct lu_device *top)
4103 struct lustre_cfg_bufs *bufs;
4104 struct lustre_cfg *lcfg;
4105 struct mdt_thread_info *info;
4110 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4111 LASSERT(info != NULL);
4113 bufs = &info->mti_u.bufs;
4115 LASSERT(m->mdt_child_exp);
4116 LASSERT(m->mdt_child_exp->exp_obd);
4118 /* process cleanup, pass mdt obd name to get obd umount flags */
4119 /* XXX: this is needed because all layers are referenced by
4120 * objects (some of them are pinned by osd, for example *
4121 * the proper solution should be a model where object used
4122 * by osd only doesn't have mdt/mdd slices -bzzz */
4123 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4124 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4125 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4129 top->ld_ops->ldo_process_config(env, top, lcfg);
4130 lustre_cfg_free(lcfg);
4134 static void mdt_stack_fini(const struct lu_env *env,
4135 struct mdt_device *m, struct lu_device *top)
4137 struct obd_device *obd = mdt2obd_dev(m);
4138 struct lustre_cfg_bufs *bufs;
4139 struct lustre_cfg *lcfg;
4140 struct mdt_thread_info *info;
4144 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4145 LASSERT(info != NULL);
4147 lu_dev_del_linkage(top->ld_site, top);
4149 lu_site_purge(env, top->ld_site, -1);
4151 bufs = &info->mti_u.bufs;
4152 /* process cleanup, pass mdt obd name to get obd umount flags */
4153 /* another purpose is to let all layers to release their objects */
4154 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4159 lustre_cfg_bufs_set_string(bufs, 1, flags);
4160 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4165 top->ld_ops->ldo_process_config(env, top, lcfg);
4166 lustre_cfg_free(lcfg);
4168 lu_site_purge(env, top->ld_site, -1);
4170 m->mdt_child = NULL;
4171 m->mdt_bottom = NULL;
4173 obd_disconnect(m->mdt_child_exp);
4174 m->mdt_child_exp = NULL;
4176 obd_disconnect(m->mdt_bottom_exp);
4177 m->mdt_child_exp = NULL;
4180 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4181 const char *next, struct obd_export **exp)
4183 struct obd_connect_data *data = NULL;
4184 struct obd_device *obd;
4188 OBD_ALLOC_PTR(data);
4190 GOTO(out, rc = -ENOMEM);
4192 obd = class_name2obd(next);
4194 CERROR("%s: can't locate next device: %s\n",
4195 mdt_obd_name(m), next);
4196 GOTO(out, rc = -ENOTCONN);
4199 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4200 data->ocd_version = LUSTRE_VERSION_CODE;
4202 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4204 CERROR("%s: cannot connect to next dev %s (%d)\n",
4205 mdt_obd_name(m), next, rc);
4215 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4216 struct lustre_cfg *cfg)
4218 char *dev = lustre_cfg_string(cfg, 0);
4219 int rc, name_size, uuid_size;
4220 char *name, *uuid, *p;
4221 struct lustre_cfg_bufs *bufs;
4222 struct lustre_cfg *lcfg;
4223 struct obd_device *obd;
4224 struct lustre_profile *lprof;
4225 struct lu_site *site;
4228 /* in 1.8 we had the only device in the stack - MDS.
4229 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4230 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4231 * to generate names and setup MDT, MDD. MDT will be using
4232 * generated name to connect to MDD. for MDD the next device
4233 * will be LOD with name taken from so called "profile" which
4234 * is generated by mount_option line
4236 * 1.8 MGS generates config. commands like this:
4237 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4238 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4239 * 2.0 MGS generates config. commands like this:
4240 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4241 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4242 * 3:lustre-MDT0000-mdtlov 4:f
4244 * we generate MDD name from MDT one, just replacing T with D
4246 * after all the preparations, the logical equivalent will be
4247 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4248 * 3:lustre-MDT0000-mdtlov 4:f
4249 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4250 * 3:lustre-MDD0000 4:f
4252 * notice we build the stack from down to top: MDD first, then MDT */
4254 name_size = MAX_OBD_NAME;
4255 uuid_size = MAX_OBD_NAME;
4257 OBD_ALLOC(name, name_size);
4258 OBD_ALLOC(uuid, uuid_size);
4259 if (name == NULL || uuid == NULL)
4260 GOTO(cleanup_mem, rc = -ENOMEM);
4262 OBD_ALLOC_PTR(bufs);
4264 GOTO(cleanup_mem, rc = -ENOMEM);
4267 p = strstr(name, "-MDT");
4269 GOTO(free_bufs, rc = -ENOMEM);
4272 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4274 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4275 if (lprof == NULL || lprof->lp_dt == NULL) {
4276 CERROR("can't find the profile: %s\n",
4277 lustre_cfg_string(cfg, 0));
4278 GOTO(free_bufs, rc = -EINVAL);
4281 lustre_cfg_bufs_reset(bufs, name);
4282 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4283 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4284 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4286 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4288 GOTO(put_profile, rc = -ENOMEM);
4290 rc = class_attach(lcfg);
4292 GOTO(lcfg_cleanup, rc);
4294 obd = class_name2obd(name);
4296 CERROR("Can not find obd %s (%s in config)\n",
4297 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4298 GOTO(lcfg_cleanup, rc = -EINVAL);
4301 lustre_cfg_free(lcfg);
4303 lustre_cfg_bufs_reset(bufs, name);
4304 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4305 lustre_cfg_bufs_set_string(bufs, 2, dev);
4306 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4308 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4310 GOTO(class_detach, rc = -ENOMEM);
4312 rc = class_setup(obd, lcfg);
4314 GOTO(class_detach, rc);
4316 /* connect to MDD we just setup */
4317 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4319 GOTO(class_detach, rc);
4321 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4323 LASSERT(mdt_lu_site(mdt) == NULL);
4324 mdt->mdt_lu_dev.ld_site = site;
4325 site->ls_top_dev = &mdt->mdt_lu_dev;
4326 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4328 /* now connect to bottom OSD */
4329 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4330 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4332 GOTO(class_detach, rc);
4334 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4336 rc = lu_env_refill((struct lu_env *)env);
4338 CERROR("Failure to refill session: '%d'\n", rc);
4340 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4345 class_detach(obd, lcfg);
4347 lustre_cfg_free(lcfg);
4349 class_put_profile(lprof);
4354 OBD_FREE(name, name_size);
4356 OBD_FREE(uuid, uuid_size);
4360 /* setup quota master target on MDT0 */
4361 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4362 struct lustre_cfg *cfg)
4364 struct obd_device *obd;
4365 char *dev = lustre_cfg_string(cfg, 0);
4366 char *qmtname, *uuid, *p;
4367 struct lustre_cfg_bufs *bufs;
4368 struct lustre_cfg *lcfg;
4369 struct lustre_profile *lprof;
4370 struct obd_connect_data *data;
4374 LASSERT(mdt->mdt_qmt_exp == NULL);
4375 LASSERT(mdt->mdt_qmt_dev == NULL);
4377 /* quota master is on MDT0 only for now */
4378 if (mdt->mdt_seq_site.ss_node_id != 0)
4381 /* MGS generates config commands which look as follows:
4382 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4383 * 3:lustre-MDT0000-mdtlov 4:f
4385 * We generate the QMT name from the MDT one, just replacing MD with QM
4386 * after all the preparations, the logical equivalent will be:
4387 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4388 * 3:lustre-MDT0000-osd 4:f */
4389 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4390 OBD_ALLOC(uuid, UUID_MAX);
4391 OBD_ALLOC_PTR(bufs);
4392 OBD_ALLOC_PTR(data);
4393 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4394 GOTO(cleanup_mem, rc = -ENOMEM);
4396 strcpy(qmtname, dev);
4397 p = strstr(qmtname, "-MDT");
4399 GOTO(cleanup_mem, rc = -ENOMEM);
4400 /* replace MD with QM */
4404 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4406 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4407 if (lprof == NULL || lprof->lp_dt == NULL) {
4408 CERROR("can't find profile for %s\n",
4409 lustre_cfg_string(cfg, 0));
4410 GOTO(cleanup_mem, rc = -EINVAL);
4413 lustre_cfg_bufs_reset(bufs, qmtname);
4414 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4415 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4416 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4418 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4420 GOTO(put_profile, rc = -ENOMEM);
4422 rc = class_attach(lcfg);
4424 GOTO(lcfg_cleanup, rc);
4426 obd = class_name2obd(qmtname);
4428 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4429 lustre_cfg_string(cfg, 0));
4430 GOTO(lcfg_cleanup, rc = -EINVAL);
4433 lustre_cfg_free(lcfg);
4435 lustre_cfg_bufs_reset(bufs, qmtname);
4436 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4437 lustre_cfg_bufs_set_string(bufs, 2, dev);
4439 /* for quota, the next device should be the OSD device */
4440 lustre_cfg_bufs_set_string(bufs, 3,
4441 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4443 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4445 GOTO(class_detach, rc = -ENOMEM);
4447 rc = class_setup(obd, lcfg);
4449 GOTO(class_detach, rc);
4451 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4453 /* configure local quota objects */
4454 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4458 GOTO(class_cleanup, rc);
4460 /* connect to quota master target */
4461 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4462 data->ocd_version = LUSTRE_VERSION_CODE;
4463 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4466 CERROR("cannot connect to quota master device %s (%d)\n",
4468 GOTO(class_cleanup, rc);
4474 class_manual_cleanup(obd);
4475 mdt->mdt_qmt_dev = NULL;
4479 class_detach(obd, lcfg);
4481 lustre_cfg_free(lcfg);
4483 class_put_profile(lprof);
4488 OBD_FREE(qmtname, MAX_OBD_NAME);
4490 OBD_FREE(uuid, UUID_MAX);
4496 /* Shutdown quota master target associated with mdt */
4497 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4501 if (mdt->mdt_qmt_exp == NULL)
4503 LASSERT(mdt->mdt_qmt_dev != NULL);
4505 /* the qmt automatically shuts down when the mdt disconnects */
4506 obd_disconnect(mdt->mdt_qmt_exp);
4507 mdt->mdt_qmt_exp = NULL;
4508 mdt->mdt_qmt_dev = NULL;
4512 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4513 * for now. This will be removed along with converting rest of MDT code
4514 * to use tgt_session_info */
4515 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4517 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4520 rc = mdt_getxattr(info);
4522 mdt_thread_info_fini(info);
4526 static struct tgt_handler mdt_tgt_handlers[] = {
4527 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4528 0, MDS_CONNECT, mdt_tgt_connect,
4529 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4530 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4531 0, MDS_DISCONNECT, tgt_disconnect,
4532 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4533 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4534 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4535 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4536 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4537 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4538 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4539 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4541 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4542 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4543 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4544 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4545 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4546 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4547 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4548 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4550 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4551 mdt_hsm_ct_register),
4552 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4553 mdt_hsm_ct_unregister),
4554 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4556 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4558 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4559 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4561 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4566 static struct tgt_handler mdt_sec_ctx_ops[] = {
4567 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4568 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4569 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4572 static struct tgt_handler mdt_quota_ops[] = {
4573 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4576 static struct tgt_opc_slice mdt_common_slice[] = {
4578 .tos_opc_start = MDS_FIRST_OPC,
4579 .tos_opc_end = MDS_LAST_OPC,
4580 .tos_hs = mdt_tgt_handlers
4583 .tos_opc_start = OBD_FIRST_OPC,
4584 .tos_opc_end = OBD_LAST_OPC,
4585 .tos_hs = tgt_obd_handlers
4588 .tos_opc_start = LDLM_FIRST_OPC,
4589 .tos_opc_end = LDLM_LAST_OPC,
4590 .tos_hs = tgt_dlm_handlers
4593 .tos_opc_start = SEC_FIRST_OPC,
4594 .tos_opc_end = SEC_LAST_OPC,
4595 .tos_hs = mdt_sec_ctx_ops
4598 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4599 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4600 .tos_hs = tgt_out_handlers
4603 .tos_opc_start = FLD_FIRST_OPC,
4604 .tos_opc_end = FLD_LAST_OPC,
4605 .tos_hs = fld_handlers
4608 .tos_opc_start = SEQ_FIRST_OPC,
4609 .tos_opc_end = SEQ_LAST_OPC,
4610 .tos_hs = seq_handlers
4613 .tos_opc_start = QUOTA_DQACQ,
4614 .tos_opc_end = QUOTA_LAST_OPC,
4615 .tos_hs = mdt_quota_ops
4618 .tos_opc_start = LLOG_FIRST_OPC,
4619 .tos_opc_end = LLOG_LAST_OPC,
4620 .tos_hs = tgt_llog_handlers
4623 .tos_opc_start = LFSCK_FIRST_OPC,
4624 .tos_opc_end = LFSCK_LAST_OPC,
4625 .tos_hs = tgt_lfsck_handlers
4633 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4635 struct md_device *next = m->mdt_child;
4636 struct lu_device *d = &m->mdt_lu_dev;
4637 struct obd_device *obd = mdt2obd_dev(m);
4638 struct lfsck_stop stop;
4641 stop.ls_status = LS_PAUSED;
4643 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4645 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4646 ping_evictor_stop();
4648 if (m->mdt_opts.mo_coordinator)
4649 mdt_hsm_cdt_stop(m);
4651 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4652 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4654 if (m->mdt_namespace != NULL)
4655 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4656 d->ld_obd->obd_force);
4658 obd_exports_barrier(obd);
4659 obd_zombie_barrier();
4661 mdt_quota_fini(env, m);
4663 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4665 /* Calling the cleanup functions in the same order as in the mdt_init0
4670 target_recovery_fini(obd);
4671 upcall_cache_cleanup(m->mdt_identity_cache);
4672 m->mdt_identity_cache = NULL;
4674 mdt_fs_cleanup(env, m);
4676 tgt_fini(env, &m->mdt_lut);
4678 mdt_hsm_cdt_fini(m);
4680 if (m->mdt_namespace != NULL) {
4681 ldlm_namespace_free_post(m->mdt_namespace);
4682 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4685 if (m->mdt_md_root != NULL) {
4686 mdt_object_put(env, m->mdt_md_root);
4687 m->mdt_md_root = NULL;
4690 mdt_seq_fini(env, m);
4692 mdt_fld_fini(env, m);
4697 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4699 LASSERT(atomic_read(&d->ld_ref) == 0);
4701 server_put_mount(mdt_obd_name(m), true);
4706 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4708 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4709 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4711 struct mdt_thread_info *info;
4712 struct obd_device *obd;
4713 const char *dev = lustre_cfg_string(cfg, 0);
4714 const char *num = lustre_cfg_string(cfg, 2);
4715 struct lustre_mount_info *lmi = NULL;
4716 struct lustre_sb_info *lsi;
4718 struct seq_server_site *ss_site;
4719 const char *identity_upcall = "NONE";
4720 struct md_device *next;
4726 lu_device_init(&m->mdt_lu_dev, ldt);
4728 * Environment (env) might be missing mdt_thread_key values at that
4729 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4732 * Usually device allocation path doesn't use module key values, but
4733 * mdt has to do a lot of work here, so allocate key value.
4735 rc = lu_env_refill((struct lu_env *)env);
4739 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4740 LASSERT(info != NULL);
4742 obd = class_name2obd(dev);
4743 LASSERT(obd != NULL);
4745 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4746 m->mdt_opts.mo_evict_tgt_nids = 1;
4747 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4749 /* default is coordinator off, it is started through conf_param
4751 m->mdt_opts.mo_coordinator = 0;
4753 lmi = server_get_mount(dev);
4755 CERROR("Cannot get mount info for %s!\n", dev);
4758 lsi = s2lsi(lmi->lmi_sb);
4759 /* CMD is supported only in IAM mode */
4761 node_id = simple_strtol(num, NULL, 10);
4762 obd->u.obt.obt_magic = OBT_MAGIC;
4763 if (lsi->lsi_lmd != NULL &&
4764 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4765 m->mdt_skip_lfsck = 1;
4768 m->mdt_squash.rsi_uid = 0;
4769 m->mdt_squash.rsi_gid = 0;
4770 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4771 init_rwsem(&m->mdt_squash.rsi_sem);
4772 spin_lock_init(&m->mdt_lock);
4773 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4774 m->mdt_enable_remote_dir = 0;
4775 m->mdt_enable_remote_dir_gid = 0;
4777 atomic_set(&m->mdt_mds_mds_conns, 0);
4778 atomic_set(&m->mdt_async_commit_count, 0);
4780 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4781 m->mdt_lu_dev.ld_obd = obd;
4782 /* Set this lu_device to obd for error handling purposes. */
4783 obd->obd_lu_dev = &m->mdt_lu_dev;
4785 /* init the stack */
4786 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4788 CERROR("%s: Can't init device stack, rc %d\n",
4789 mdt_obd_name(m), rc);
4794 ss_site = mdt_seq_site(m);
4795 s->ld_seq_site = ss_site;
4798 /* set server index */
4799 ss_site->ss_node_id = node_id;
4801 /* failover is the default
4802 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4803 * assumed whose ss_node_id == 0 XXX
4805 obd->obd_replayable = 1;
4806 /* No connection accepted until configurations will finish */
4807 obd->obd_no_conn = 1;
4809 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4810 char *str = lustre_cfg_string(cfg, 4);
4811 if (strchr(str, 'n')) {
4812 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4813 obd->obd_replayable = 0;
4817 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4819 GOTO(err_fini_stack, rc);
4821 rc = mdt_seq_init(env, m);
4823 GOTO(err_fini_fld, rc);
4825 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4826 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4827 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4828 LDLM_NAMESPACE_SERVER,
4829 LDLM_NAMESPACE_GREEDY,
4831 if (m->mdt_namespace == NULL)
4832 GOTO(err_fini_seq, rc = -ENOMEM);
4834 m->mdt_namespace->ns_lvbp = m;
4835 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4837 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4838 /* set obd_namespace for compatibility with old code */
4839 obd->obd_namespace = m->mdt_namespace;
4841 rc = mdt_hsm_cdt_init(m);
4843 CERROR("%s: error initializing coordinator, rc %d\n",
4844 mdt_obd_name(m), rc);
4845 GOTO(err_free_ns, rc);
4848 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4849 OBD_FAIL_MDS_ALL_REQUEST_NET,
4850 OBD_FAIL_MDS_ALL_REPLY_NET);
4852 GOTO(err_free_hsm, rc);
4854 rc = mdt_fs_setup(env, m, obd, lsi);
4858 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
4860 next = m->mdt_child;
4861 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4864 GOTO(err_fs_cleanup, rc);
4866 if (mntopts & MNTOPT_USERXATTR)
4867 m->mdt_opts.mo_user_xattr = 1;
4869 m->mdt_opts.mo_user_xattr = 0;
4871 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4873 GOTO(err_fs_cleanup, rc);
4875 if (mntopts & MNTOPT_ACL)
4876 m->mdt_opts.mo_acl = 1;
4878 m->mdt_opts.mo_acl = 0;
4880 /* XXX: to support suppgid for ACL, we enable identity_upcall
4881 * by default, otherwise, maybe got unexpected -EACCESS. */
4882 if (m->mdt_opts.mo_acl)
4883 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4885 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4887 &mdt_identity_upcall_cache_ops);
4888 if (IS_ERR(m->mdt_identity_cache)) {
4889 rc = PTR_ERR(m->mdt_identity_cache);
4890 m->mdt_identity_cache = NULL;
4891 GOTO(err_fs_cleanup, rc);
4894 rc = mdt_procfs_init(m, dev);
4896 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4897 GOTO(err_recovery, rc);
4900 rc = mdt_quota_init(env, m, cfg);
4902 GOTO(err_procfs, rc);
4904 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4905 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4906 "mdt_ldlm_client", m->mdt_ldlm_client);
4908 ping_evictor_start();
4910 /* recovery will be started upon mdt_prepare()
4911 * when the whole stack is complete and ready
4912 * to serve the requests */
4914 /* Reduce the initial timeout on an MDS because it doesn't need such
4915 * a long timeout as an OST does. Adaptive timeouts will adjust this
4916 * value appropriately. */
4917 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4918 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4924 target_recovery_fini(obd);
4925 upcall_cache_cleanup(m->mdt_identity_cache);
4926 m->mdt_identity_cache = NULL;
4928 mdt_fs_cleanup(env, m);
4930 tgt_fini(env, &m->mdt_lut);
4932 mdt_hsm_cdt_fini(m);
4934 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4935 obd->obd_namespace = m->mdt_namespace = NULL;
4937 mdt_seq_fini(env, m);
4939 mdt_fld_fini(env, m);
4941 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4944 server_put_mount(dev, true);
4948 /* For interoperability, the left element is old parameter, the right one
4949 * is the new version of the parameter, if some parameter is deprecated,
4950 * the new version should be set as NULL. */
4951 static struct cfg_interop_param mdt_interop_param[] = {
4952 { "mdt.group_upcall", NULL },
4953 { "mdt.quota_type", NULL },
4954 { "mdd.quota_type", NULL },
4955 { "mdt.som", NULL },
4956 { "mdt.rootsquash", "mdt.root_squash" },
4957 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4961 /* used by MGS to process specific configurations */
4962 static int mdt_process_config(const struct lu_env *env,
4963 struct lu_device *d, struct lustre_cfg *cfg)
4965 struct mdt_device *m = mdt_dev(d);
4966 struct md_device *md_next = m->mdt_child;
4967 struct lu_device *next = md2lu_dev(md_next);
4971 switch (cfg->lcfg_command) {
4973 struct obd_device *obd = d->ld_obd;
4975 /* For interoperability */
4976 struct cfg_interop_param *ptr = NULL;
4977 struct lustre_cfg *old_cfg = NULL;
4980 param = lustre_cfg_string(cfg, 1);
4981 if (param == NULL) {
4982 CERROR("param is empty\n");
4987 ptr = class_find_old_param(param, mdt_interop_param);
4989 if (ptr->new_param == NULL) {
4991 CWARN("For interoperability, skip this %s."
4992 " It is obsolete.\n", ptr->old_param);
4996 CWARN("Found old param %s, changed it to %s.\n",
4997 ptr->old_param, ptr->new_param);
5000 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5007 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
5009 if (rc > 0 || rc == -ENOSYS) {
5010 /* is it an HSM var ? */
5011 rc = class_process_proc_param(PARAM_HSM,
5012 hsm_cdt_get_proc_vars(),
5014 if (rc > 0 || rc == -ENOSYS)
5015 /* we don't understand; pass it on */
5016 rc = next->ld_ops->ldo_process_config(env, next,
5020 if (old_cfg != NULL)
5021 lustre_cfg_free(cfg);
5026 /* others are passed further */
5027 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5033 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5034 const struct lu_object_header *hdr,
5035 struct lu_device *d)
5037 struct mdt_object *mo;
5041 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5043 struct lu_object *o;
5044 struct lu_object_header *h;
5047 h = &mo->mot_header;
5048 lu_object_header_init(h);
5049 lu_object_init(o, h, d);
5050 lu_object_add_top(h, o);
5051 o->lo_ops = &mdt_obj_ops;
5052 spin_lock_init(&mo->mot_write_lock);
5053 mutex_init(&mo->mot_lov_mutex);
5054 init_rwsem(&mo->mot_open_sem);
5055 atomic_set(&mo->mot_open_count, 0);
5061 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5062 const struct lu_object_conf *unused)
5064 struct mdt_device *d = mdt_dev(o->lo_dev);
5065 struct lu_device *under;
5066 struct lu_object *below;
5070 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5071 PFID(lu_object_fid(o)));
5073 under = &d->mdt_child->md_lu_dev;
5074 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5075 if (below != NULL) {
5076 lu_object_add(o, below);
5083 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5085 struct mdt_object *mo = mdt_obj(o);
5086 struct lu_object_header *h;
5090 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5091 PFID(lu_object_fid(o)));
5093 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5094 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5097 lu_object_header_fini(h);
5098 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5103 static int mdt_object_print(const struct lu_env *env, void *cookie,
5104 lu_printer_t p, const struct lu_object *o)
5106 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5108 return (*p)(env, cookie,
5109 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
5110 mdto, mdto->mot_lov_created ? "lov_created" : "",
5111 mdto->mot_cache_attr ? "cache_attr" : "",
5112 mdto->mot_write_count);
5115 static int mdt_prepare(const struct lu_env *env,
5116 struct lu_device *pdev,
5117 struct lu_device *cdev)
5119 struct mdt_device *mdt = mdt_dev(cdev);
5120 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5121 struct obd_device *obd = cdev->ld_obd;
5128 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5132 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5136 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5140 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5141 /* The LFSCK instance is registered just now, so it must be there when
5142 * register the namespace to such instance. */
5143 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5145 if (mdt->mdt_seq_site.ss_node_id == 0) {
5146 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5147 &mdt->mdt_md_root_fid);
5152 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5154 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5155 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5156 LASSERT(obd->obd_no_conn);
5157 spin_lock(&obd->obd_dev_lock);
5158 obd->obd_no_conn = 0;
5159 spin_unlock(&obd->obd_dev_lock);
5161 if (obd->obd_recovering == 0)
5162 mdt_postrecov(env, mdt);
5167 const struct lu_device_operations mdt_lu_ops = {
5168 .ldo_object_alloc = mdt_object_alloc,
5169 .ldo_process_config = mdt_process_config,
5170 .ldo_prepare = mdt_prepare,
5173 static const struct lu_object_operations mdt_obj_ops = {
5174 .loo_object_init = mdt_object_init,
5175 .loo_object_free = mdt_object_free,
5176 .loo_object_print = mdt_object_print
5179 static int mdt_obd_set_info_async(const struct lu_env *env,
5180 struct obd_export *exp,
5181 __u32 keylen, void *key,
5182 __u32 vallen, void *val,
5183 struct ptlrpc_request_set *set)
5189 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5190 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5198 * Match client and server connection feature flags.
5200 * Compute the compatibility flags for a connection request based on
5201 * features mutually supported by client and server.
5203 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5204 * must not be updated here, otherwise a partially initialized value may
5205 * be exposed. After the connection request is successfully processed,
5206 * the top-level MDT connect request handler atomically updates the export
5207 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5208 * reply. \see mdt_connect().
5210 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5211 * full struct obd_connect_data. So care must be taken when accessing fields
5212 * that are not present in struct obd_connect_data_v1. See LU-16.
5214 * \param exp the obd_export associated with this client/target pair
5215 * \param mdt the target device for the connection
5216 * \param data stores data for this connect request
5219 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5220 * \retval -EBADE client and server feature requirements are incompatible
5222 static int mdt_connect_internal(struct obd_export *exp,
5223 struct mdt_device *mdt,
5224 struct obd_connect_data *data)
5226 LASSERT(data != NULL);
5228 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5230 if (mdt->mdt_bottom->dd_rdonly &&
5231 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5232 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5235 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5236 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5238 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5240 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5241 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5242 CWARN("%s: client %s does not support ibits lock, either "
5243 "very old or an invalid client: flags %#llx\n",
5244 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5245 data->ocd_connect_flags);
5249 if (!mdt->mdt_opts.mo_acl)
5250 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5252 if (!mdt->mdt_opts.mo_user_xattr)
5253 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5255 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5256 data->ocd_brw_size = min(data->ocd_brw_size,
5257 (__u32)MD_MAX_BRW_SIZE);
5258 if (data->ocd_brw_size == 0) {
5259 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx"
5260 " ocd_version: %x ocd_grant: %d "
5261 "ocd_index: %u ocd_brw_size is "
5262 "unexpectedly zero, network data "
5263 "corruption? Refusing connection of this"
5266 exp->exp_client_uuid.uuid,
5267 exp, data->ocd_connect_flags, data->ocd_version,
5268 data->ocd_grant, data->ocd_index);
5273 /* NB: Disregard the rule against updating
5274 * exp_connect_data.ocd_connect_flags in this case, since
5275 * tgt_client_new() needs to know if this is a lightweight
5276 * connection, and it is safe to expose this flag before
5277 * connection processing completes. */
5278 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5279 spin_lock(&exp->exp_lock);
5280 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5281 spin_unlock(&exp->exp_lock);
5284 data->ocd_version = LUSTRE_VERSION_CODE;
5286 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5287 CWARN("%s: MDS requires FID support, but client not\n",
5292 if (OCD_HAS_FLAG(data, PINGLESS)) {
5293 if (ptlrpc_pinger_suppress_pings()) {
5294 spin_lock(&exp->exp_obd->obd_dev_lock);
5295 list_del_init(&exp->exp_obd_chain_timed);
5296 spin_unlock(&exp->exp_obd->obd_dev_lock);
5298 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5302 data->ocd_max_easize = mdt->mdt_max_ea_size;
5304 /* NB: Disregard the rule against updating
5305 * exp_connect_data.ocd_connect_flags in this case, since
5306 * tgt_client_new() needs to know if this is client supports
5307 * multiple modify RPCs, and it is safe to expose this flag before
5308 * connection processing completes. */
5309 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5310 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5311 spin_lock(&exp->exp_lock);
5312 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5313 spin_unlock(&exp->exp_lock);
5319 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5320 struct mdt_thread_info *info,
5321 struct mdt_file_data *mfd)
5323 struct lu_context ses;
5327 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5332 lu_context_enter(&ses);
5334 mdt_ucred(info)->uc_valid = UCRED_OLD;
5335 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5337 lu_context_exit(&ses);
5338 lu_context_fini(&ses);
5344 static int mdt_export_cleanup(struct obd_export *exp)
5346 struct list_head closing_list;
5347 struct mdt_export_data *med = &exp->exp_mdt_data;
5348 struct obd_device *obd = exp->exp_obd;
5349 struct mdt_device *mdt;
5350 struct mdt_thread_info *info;
5352 struct mdt_file_data *mfd, *n;
5356 INIT_LIST_HEAD(&closing_list);
5357 spin_lock(&med->med_open_lock);
5358 while (!list_empty(&med->med_open_head)) {
5359 struct list_head *tmp = med->med_open_head.next;
5360 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5362 /* Remove mfd handle so it can't be found again.
5363 * We are consuming the mfd_list reference here. */
5364 class_handle_unhash(&mfd->mfd_handle);
5365 list_move_tail(&mfd->mfd_list, &closing_list);
5367 spin_unlock(&med->med_open_lock);
5368 mdt = mdt_dev(obd->obd_lu_dev);
5369 LASSERT(mdt != NULL);
5371 rc = lu_env_init(&env, LCT_MD_THREAD);
5375 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5376 LASSERT(info != NULL);
5377 memset(info, 0, sizeof *info);
5378 info->mti_env = &env;
5379 info->mti_mdt = mdt;
5380 info->mti_exp = exp;
5382 if (!list_empty(&closing_list)) {
5383 struct md_attr *ma = &info->mti_attr;
5385 /* Close any open files (which may also cause orphan
5387 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5388 list_del_init(&mfd->mfd_list);
5389 ma->ma_need = ma->ma_valid = 0;
5391 /* This file is being closed due to an eviction, it
5392 * could have been modified and now dirty regarding to
5393 * HSM archive, check this!
5394 * The logic here is to mark a file dirty if there's a
5395 * chance it was dirtied before the client was evicted,
5396 * so that we don't have to wait for a release attempt
5397 * before finding out the file was actually dirty and
5398 * fail the release. Aggressively marking it dirty here
5399 * will cause the policy engine to attempt to
5400 * re-archive it; when rearchiving, we can compare the
5401 * current version to the HSM data_version and make the
5402 * archive request into a noop if it's not actually
5405 if (mfd->mfd_mode & FMODE_WRITE)
5406 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5408 /* Don't unlink orphan on failover umount, LU-184 */
5409 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5410 ma->ma_valid = MA_FLAGS;
5411 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5413 mdt_mfd_close(info, mfd);
5416 info->mti_mdt = NULL;
5417 /* cleanup client slot early */
5418 /* Do not erase record for recoverable client. */
5419 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5420 tgt_client_del(&env, exp);
5426 static inline void mdt_enable_slc(struct mdt_device *mdt)
5428 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5429 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5432 static inline void mdt_disable_slc(struct mdt_device *mdt)
5434 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5435 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5438 static int mdt_obd_disconnect(struct obd_export *exp)
5444 class_export_get(exp);
5446 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5447 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5448 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5450 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5451 mdt_disable_slc(mdt);
5454 rc = server_disconnect_export(exp);
5456 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5458 rc = mdt_export_cleanup(exp);
5459 nodemap_del_member(exp);
5460 class_export_put(exp);
5464 /* mds_connect copy */
5465 static int mdt_obd_connect(const struct lu_env *env,
5466 struct obd_export **exp, struct obd_device *obd,
5467 struct obd_uuid *cluuid,
5468 struct obd_connect_data *data,
5471 struct obd_export *lexp;
5472 struct lustre_handle conn = { 0 };
5473 struct mdt_device *mdt;
5475 lnet_nid_t *client_nid = localdata;
5478 LASSERT(env != NULL);
5479 LASSERT(data != NULL);
5481 if (!exp || !obd || !cluuid)
5484 mdt = mdt_dev(obd->obd_lu_dev);
5486 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5487 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5488 atomic_inc(&mdt->mdt_mds_mds_conns);
5489 mdt_enable_slc(mdt);
5493 * first, check whether the stack is ready to handle requests
5494 * XXX: probably not very appropriate method is used now
5495 * at some point we should find a better one
5497 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5498 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5499 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5500 rc = obd_get_info(env, mdt->mdt_child_exp,
5501 sizeof(KEY_OSP_CONNECTED),
5502 KEY_OSP_CONNECTED, NULL, NULL);
5505 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5508 rc = class_connect(&conn, obd, cluuid);
5512 lexp = class_conn2export(&conn);
5513 LASSERT(lexp != NULL);
5515 rc = nodemap_add_member(*client_nid, lexp);
5516 if (rc != 0 && rc != -EEXIST)
5519 rc = mdt_connect_internal(lexp, mdt, data);
5521 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5524 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5525 rc = tgt_client_new(env, lexp);
5527 mdt_export_stats_init(obd, lexp, localdata);
5531 class_disconnect(lexp);
5532 nodemap_del_member(lexp);
5536 /* Because we do not want this export to be evicted by pinger,
5537 * let's not add this export to the timed chain list. */
5538 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5539 spin_lock(&lexp->exp_obd->obd_dev_lock);
5540 list_del_init(&lexp->exp_obd_chain_timed);
5541 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5548 static int mdt_obd_reconnect(const struct lu_env *env,
5549 struct obd_export *exp, struct obd_device *obd,
5550 struct obd_uuid *cluuid,
5551 struct obd_connect_data *data,
5554 lnet_nid_t *client_nid = localdata;
5558 if (exp == NULL || obd == NULL || cluuid == NULL)
5561 rc = nodemap_add_member(*client_nid, exp);
5562 if (rc != 0 && rc != -EEXIST)
5565 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5567 mdt_export_stats_init(obd, exp, localdata);
5569 nodemap_del_member(exp);
5574 /* FIXME: Can we avoid using these two interfaces? */
5575 static int mdt_init_export(struct obd_export *exp)
5577 struct mdt_export_data *med = &exp->exp_mdt_data;
5581 INIT_LIST_HEAD(&med->med_open_head);
5582 spin_lock_init(&med->med_open_lock);
5583 spin_lock(&exp->exp_lock);
5584 exp->exp_connecting = 1;
5585 spin_unlock(&exp->exp_lock);
5587 /* self-export doesn't need client data and ldlm initialization */
5588 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5589 &exp->exp_client_uuid)))
5592 rc = tgt_client_alloc(exp);
5596 rc = ldlm_init_export(exp);
5603 tgt_client_free(exp);
5605 CERROR("%s: Failed to initialize export: rc = %d\n",
5606 exp->exp_obd->obd_name, rc);
5610 static int mdt_destroy_export(struct obd_export *exp)
5614 target_destroy_export(exp);
5615 /* destroy can be called from failed obd_setup, so
5616 * checking uuid is safer than obd_self_export */
5617 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5618 &exp->exp_client_uuid)))
5621 ldlm_destroy_export(exp);
5622 tgt_client_free(exp);
5624 LASSERT(list_empty(&exp->exp_outstanding_replies));
5625 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5630 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5631 struct linkea_data *ldata)
5635 LASSERT(ldata->ld_buf->lb_buf != NULL);
5637 if (!mdt_object_exists(mdt_obj))
5640 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5641 ldata->ld_buf, XATTR_NAME_LINK);
5642 if (rc == -ERANGE) {
5643 /* Buf was too small, figure out what we need. */
5644 lu_buf_free(ldata->ld_buf);
5645 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5646 ldata->ld_buf, XATTR_NAME_LINK);
5649 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5650 if (ldata->ld_buf->lb_buf == NULL)
5652 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5653 ldata->ld_buf, XATTR_NAME_LINK);
5658 return linkea_init_with_rec(ldata);
5662 * Given an MDT object, try to look up the full path to the object.
5663 * Part of the MDT layer implementation of lfs fid2path.
5665 * \param[in] info Per-thread common data shared by MDT level handlers.
5666 * \param[in] obj Object to do path lookup of
5667 * \param[in,out] fp User-provided struct to store path information
5668 * \param[in] root_fid Root FID of current path should reach
5670 * \retval 0 Lookup successful, path information stored in fp
5671 * \retval -EAGAIN Lookup failed, usually because object is being moved
5672 * \retval negative errno if there was a problem
5674 static int mdt_path_current(struct mdt_thread_info *info,
5675 struct mdt_object *obj,
5676 struct getinfo_fid2path *fp,
5677 struct lu_fid *root_fid)
5679 struct mdt_device *mdt = info->mti_mdt;
5680 struct mdt_object *mdt_obj;
5681 struct link_ea_header *leh;
5682 struct link_ea_entry *lee;
5683 struct lu_name *tmpname = &info->mti_name;
5684 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5685 struct lu_buf *buf = &info->mti_big_buf;
5688 struct linkea_data ldata = { NULL };
5693 /* temp buffer for path element, the buffer will be finally freed
5694 * in mdt_thread_info_fini */
5695 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5696 if (buf->lb_buf == NULL)
5700 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5703 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5705 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5706 struct lu_buf lmv_buf;
5708 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5709 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5710 GOTO(out, rc = -ENOENT);
5712 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5714 mdt_object_get(info->mti_env, mdt_obj);
5716 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5717 if (IS_ERR(mdt_obj))
5718 GOTO(out, rc = PTR_ERR(mdt_obj));
5721 if (!mdt_object_exists(mdt_obj)) {
5722 mdt_object_put(info->mti_env, mdt_obj);
5723 GOTO(out, rc = -ENOENT);
5726 if (mdt_object_remote(mdt_obj)) {
5727 mdt_object_put(info->mti_env, mdt_obj);
5728 GOTO(remote_out, rc = -EREMOTE);
5731 rc = mdt_links_read(info, mdt_obj, &ldata);
5733 mdt_object_put(info->mti_env, mdt_obj);
5738 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5739 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5740 /* If set, use link #linkno for path lookup, otherwise use
5741 link #0. Only do this for the final path element. */
5742 if (first && fp->gf_linkno < leh->leh_reccount) {
5744 for (count = 0; count < fp->gf_linkno; count++) {
5745 lee = (struct link_ea_entry *)
5746 ((char *)lee + reclen);
5747 linkea_entry_unpack(lee, &reclen, tmpname,
5750 if (fp->gf_linkno < leh->leh_reccount - 1)
5751 /* indicate to user there are more links */
5755 lmv_buf.lb_buf = info->mti_xattr_buf;
5756 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5757 /* Check if it is slave stripes */
5758 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5759 &lmv_buf, XATTR_NAME_LMV);
5760 mdt_object_put(info->mti_env, mdt_obj);
5762 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5764 /* For slave stripes, get its master */
5765 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5766 fp->gf_fid = *tmpfid;
5769 } else if (rc < 0 && rc != -ENODATA) {
5775 /* Pack the name in the end of the buffer */
5776 ptr -= tmpname->ln_namelen;
5777 if (ptr - 1 <= fp->gf_u.gf_path)
5778 GOTO(out, rc = -EOVERFLOW);
5779 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5782 /* keep the last resolved fid to the client, so the
5783 * client will build the left path on another MDT for
5785 fp->gf_fid = *tmpfid;
5791 ptr++; /* skip leading / */
5792 memmove(fp->gf_u.gf_path, ptr,
5793 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5800 * Given an MDT object, use mdt_path_current to get the path.
5801 * Essentially a wrapper to retry mdt_path_current a set number of times
5802 * if -EAGAIN is returned (usually because an object is being moved).
5804 * Part of the MDT layer implementation of lfs fid2path.
5806 * \param[in] info Per-thread common data shared by mdt level handlers.
5807 * \param[in] obj Object to do path lookup of
5808 * \param[in,out] fp User-provided struct for arguments and to store path
5811 * \retval 0 Lookup successful, path information stored in fp
5812 * \retval negative errno if there was a problem
5814 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5815 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5817 struct mdt_device *mdt = info->mti_mdt;
5822 if (fp->gf_pathlen < 3)
5825 if (root_fid == NULL)
5826 root_fid = &mdt->mdt_md_root_fid;
5828 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5829 fp->gf_u.gf_path[0] = '\0';
5833 /* Retry multiple times in case file is being moved */
5834 while (tries-- && rc == -EAGAIN)
5835 rc = mdt_path_current(info, obj, fp, root_fid);
5841 * Get the full path of the provided FID, as of changelog record recno.
5843 * This checks sanity and looks up object for user provided FID
5844 * before calling the actual path lookup code.
5846 * Part of the MDT layer implementation of lfs fid2path.
5848 * \param[in] info Per-thread common data shared by mdt level handlers.
5849 * \param[in,out] fp User-provided struct for arguments and to store path
5852 * \retval 0 Lookup successful, path information and recno stored in fp
5853 * \retval -ENOENT, object does not exist
5854 * \retval negative errno if there was a problem
5856 static int mdt_fid2path(struct mdt_thread_info *info,
5857 struct lu_fid *root_fid,
5858 struct getinfo_fid2path *fp)
5860 struct mdt_device *mdt = info->mti_mdt;
5861 struct mdt_object *obj;
5865 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5866 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5868 if (!fid_is_sane(&fp->gf_fid))
5871 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5872 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5873 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5874 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5878 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5881 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5882 PFID(&fp->gf_fid), rc);
5886 if (mdt_object_remote(obj))
5888 else if (!mdt_object_exists(obj))
5894 mdt_object_put(info->mti_env, obj);
5895 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5896 PFID(&fp->gf_fid), rc);
5900 rc = mdt_path(info, obj, fp, root_fid);
5902 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
5903 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5904 fp->gf_recno, fp->gf_linkno);
5906 mdt_object_put(info->mti_env, obj);
5911 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5912 void *val, int vallen)
5914 struct getinfo_fid2path *fpout, *fpin;
5915 struct lu_fid *root_fid = NULL;
5918 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5921 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5922 lustre_swab_fid2path(fpin);
5924 memcpy(fpout, fpin, sizeof(*fpin));
5925 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5928 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5929 sizeof(struct lu_fid)) {
5930 /* client sent its root FID, which is normally fileset FID */
5931 root_fid = fpin->gf_u.gf_root_fid;
5932 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5933 lustre_swab_lu_fid(root_fid);
5935 if (root_fid != NULL && !fid_is_sane(root_fid))
5939 rc = mdt_fid2path(info, root_fid, fpout);
5943 int mdt_get_info(struct tgt_session_info *tsi)
5953 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5955 CDEBUG(D_IOCTL, "No GETINFO key\n");
5956 RETURN(err_serious(-EFAULT));
5958 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5961 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5962 if (vallen == NULL) {
5963 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5964 tgt_name(tsi->tsi_tgt));
5965 RETURN(err_serious(-EFAULT));
5968 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5970 rc = req_capsule_server_pack(tsi->tsi_pill);
5972 RETURN(err_serious(rc));
5974 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5975 if (valout == NULL) {
5976 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5977 tgt_name(tsi->tsi_tgt));
5978 RETURN(err_serious(-EFAULT));
5981 if (KEY_IS(KEY_FID2PATH)) {
5982 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5984 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
5985 mdt_thread_info_fini(info);
5992 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5994 struct obd_ioctl_data *data = karg;
5997 struct mdt_object *obj;
5998 struct mdt_lock_handle *lh;
6002 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6003 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6006 fid = (struct lu_fid *)data->ioc_inlbuf1;
6008 if (!fid_is_sane(fid))
6011 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6013 lh = &mti->mti_lh[MDT_LH_PARENT];
6014 mdt_lock_reg_init(lh, LCK_CR);
6016 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6018 RETURN(PTR_ERR(obj));
6020 if (mdt_object_remote(obj)) {
6023 * before calling version get the correct MDS should be
6024 * fid, this is error to find remote object here
6026 CERROR("nonlocal object "DFID"\n", PFID(fid));
6027 } else if (!mdt_object_exists(obj)) {
6028 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6031 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6032 *(__u64 *)data->ioc_inlbuf2 = version;
6035 mdt_object_unlock_put(mti, obj, lh, 1);
6039 /* ioctls on obd dev */
6040 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6041 void *karg, void __user *uarg)
6044 struct obd_device *obd = exp->exp_obd;
6045 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6046 struct dt_device *dt = mdt->mdt_bottom;
6050 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6051 rc = lu_env_init(&env, LCT_MD_THREAD);
6057 rc = mdt_device_sync(&env, mdt);
6059 case OBD_IOC_SET_READONLY:
6060 rc = dt_sync(&env, dt);
6062 rc = dt_ro(&env, dt);
6064 case OBD_IOC_ABORT_RECOVERY:
6065 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6066 obd->obd_abort_recovery = 1;
6067 target_stop_recovery_thread(obd);
6070 case OBD_IOC_CHANGELOG_REG:
6071 case OBD_IOC_CHANGELOG_DEREG:
6072 case OBD_IOC_CHANGELOG_CLEAR:
6073 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
6077 case OBD_IOC_START_LFSCK: {
6078 struct md_device *next = mdt->mdt_child;
6079 struct obd_ioctl_data *data = karg;
6080 struct lfsck_start_param lsp;
6082 if (unlikely(data == NULL)) {
6087 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6088 lsp.lsp_index_valid = 0;
6089 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6092 case OBD_IOC_STOP_LFSCK: {
6093 struct md_device *next = mdt->mdt_child;
6094 struct obd_ioctl_data *data = karg;
6095 struct lfsck_stop stop;
6097 stop.ls_status = LS_STOPPED;
6098 /* Old lfsck utils may pass NULL @stop. */
6099 if (data->ioc_inlbuf1 == NULL)
6103 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
6105 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
6108 case OBD_IOC_QUERY_LFSCK: {
6109 struct md_device *next = mdt->mdt_child;
6110 struct obd_ioctl_data *data = karg;
6112 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6116 case OBD_IOC_GET_OBJ_VERSION: {
6117 struct mdt_thread_info *mti;
6118 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6119 memset(mti, 0, sizeof *mti);
6120 mti->mti_env = &env;
6124 rc = mdt_ioc_version_get(mti, karg);
6127 case OBD_IOC_CATLOGLIST: {
6128 struct mdt_thread_info *mti;
6130 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6131 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6132 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6133 &mti->mti_tmp_fid1);
6138 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6139 mdt_obd_name(mdt), cmd, rc);
6146 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6148 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6152 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6153 struct lfsck_start_param lsp;
6155 lsp.lsp_start = NULL;
6156 lsp.lsp_index_valid = 0;
6157 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6158 OBD_IOC_START_LFSCK,
6160 if (rc != 0 && rc != -EALREADY)
6161 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6162 mdt_obd_name(mdt), rc);
6165 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6169 static int mdt_obd_postrecov(struct obd_device *obd)
6174 rc = lu_env_init(&env, LCT_MD_THREAD);
6177 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6182 static struct obd_ops mdt_obd_device_ops = {
6183 .o_owner = THIS_MODULE,
6184 .o_set_info_async = mdt_obd_set_info_async,
6185 .o_connect = mdt_obd_connect,
6186 .o_reconnect = mdt_obd_reconnect,
6187 .o_disconnect = mdt_obd_disconnect,
6188 .o_init_export = mdt_init_export,
6189 .o_destroy_export = mdt_destroy_export,
6190 .o_iocontrol = mdt_iocontrol,
6191 .o_postrecov = mdt_obd_postrecov,
6194 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6195 struct lu_device *d)
6197 struct mdt_device *m = mdt_dev(d);
6204 static struct lu_device *mdt_device_free(const struct lu_env *env,
6205 struct lu_device *d)
6207 struct mdt_device *m = mdt_dev(d);
6210 lu_device_fini(&m->mdt_lu_dev);
6216 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6217 struct lu_device_type *t,
6218 struct lustre_cfg *cfg)
6220 struct lu_device *l;
6221 struct mdt_device *m;
6228 rc = mdt_init0(env, m, t, cfg);
6230 mdt_device_free(env, l);
6235 l = ERR_PTR(-ENOMEM);
6239 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6240 LU_KEY_INIT(mdt, struct mdt_thread_info);
6242 static void mdt_key_fini(const struct lu_context *ctx,
6243 struct lu_context_key *key, void* data)
6245 struct mdt_thread_info *info = data;
6247 if (info->mti_big_lmm) {
6248 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6249 info->mti_big_lmm = NULL;
6250 info->mti_big_lmmsize = 0;
6255 /* context key: mdt_thread_key */
6256 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6258 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6260 return lu_ucred(info->mti_env);
6263 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6265 return lu_ucred_check(info->mti_env);
6269 * Enable/disable COS (Commit On Sharing).
6271 * Set/Clear the COS flag in mdt options.
6273 * \param mdt mdt device
6274 * \param val 0 disables COS, other values enable COS
6276 void mdt_enable_cos(struct mdt_device *mdt, int val)
6281 mdt->mdt_opts.mo_cos = !!val;
6282 rc = lu_env_init(&env, LCT_LOCAL);
6283 if (unlikely(rc != 0)) {
6284 CWARN("%s: lu_env initialization failed, cannot "
6285 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6288 mdt_device_sync(&env, mdt);
6293 * Check COS (Commit On Sharing) status.
6295 * Return COS flag status.
6297 * \param mdt mdt device
6299 int mdt_cos_is_enabled(struct mdt_device *mdt)
6301 return mdt->mdt_opts.mo_cos != 0;
6304 static struct lu_device_type_operations mdt_device_type_ops = {
6305 .ldto_device_alloc = mdt_device_alloc,
6306 .ldto_device_free = mdt_device_free,
6307 .ldto_device_fini = mdt_device_fini
6310 static struct lu_device_type mdt_device_type = {
6311 .ldt_tags = LU_DEVICE_MD,
6312 .ldt_name = LUSTRE_MDT_NAME,
6313 .ldt_ops = &mdt_device_type_ops,
6314 .ldt_ctx_tags = LCT_MD_THREAD
6317 static int __init mdt_init(void)
6321 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6322 FID_NOBRACE_LEN + 1);
6323 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6325 rc = lu_kmem_init(mdt_caches);
6329 rc = mds_mod_init();
6333 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6334 LUSTRE_MDT_NAME, &mdt_device_type);
6339 lu_kmem_fini(mdt_caches);
6346 static void __exit mdt_exit(void)
6348 class_unregister_type(LUSTRE_MDT_NAME);
6350 lu_kmem_fini(mdt_caches);
6353 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6354 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6355 MODULE_VERSION(LUSTRE_VERSION_STRING);
6356 MODULE_LICENSE("GPL");
6358 module_init(mdt_init);
6359 module_exit(mdt_exit);