4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 #include <dt_object.h>
54 #include <lustre_acl.h>
55 #include <lustre_export.h>
56 #include <lustre_ioctl.h>
57 #include <lustre_lfsck.h>
58 #include <lustre_log.h>
59 #include <lustre_net.h>
60 #include <lustre_nodemap.h>
61 #include <lustre_mds.h>
62 #include <lustre_param.h>
63 #include <lustre_quota.h>
64 #include <lustre_swab.h>
66 #include <obd_support.h>
68 #include <llog_swab.h>
70 #include "mdt_internal.h"
73 static unsigned int max_mod_rpcs_per_client = 8;
74 module_param(max_mod_rpcs_per_client, uint, 0644);
75 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
77 mdl_mode_t mdt_mdl_lock_modes[] = {
78 [LCK_MINMODE] = MDL_MINMODE,
85 [LCK_GROUP] = MDL_GROUP
88 enum ldlm_mode mdt_dlm_lock_modes[] = {
89 [MDL_MINMODE] = LCK_MINMODE,
96 [MDL_GROUP] = LCK_GROUP
99 static struct mdt_device *mdt_dev(struct lu_device *d);
100 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
102 static const struct lu_object_operations mdt_obj_ops;
104 /* Slab for MDT object allocation */
105 static struct kmem_cache *mdt_object_kmem;
107 /* For HSM restore handles */
108 struct kmem_cache *mdt_hsm_cdt_kmem;
110 /* For HSM request handles */
111 struct kmem_cache *mdt_hsm_car_kmem;
113 static struct lu_kmem_descr mdt_caches[] = {
115 .ckd_cache = &mdt_object_kmem,
116 .ckd_name = "mdt_obj",
117 .ckd_size = sizeof(struct mdt_object)
120 .ckd_cache = &mdt_hsm_cdt_kmem,
121 .ckd_name = "mdt_cdt_restore_handle",
122 .ckd_size = sizeof(struct cdt_restore_handle)
125 .ckd_cache = &mdt_hsm_car_kmem,
126 .ckd_name = "mdt_cdt_agent_req",
127 .ckd_size = sizeof(struct cdt_agent_req)
134 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
138 return rep->lock_policy_res1 & op_flag;
141 void mdt_clear_disposition(struct mdt_thread_info *info,
142 struct ldlm_reply *rep, __u64 op_flag)
145 info->mti_opdata &= ~op_flag;
146 tgt_opdata_clear(info->mti_env, op_flag);
149 rep->lock_policy_res1 &= ~op_flag;
152 void mdt_set_disposition(struct mdt_thread_info *info,
153 struct ldlm_reply *rep, __u64 op_flag)
156 info->mti_opdata |= op_flag;
157 tgt_opdata_set(info->mti_env, op_flag);
160 rep->lock_policy_res1 |= op_flag;
163 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
165 lh->mlh_pdo_hash = 0;
166 lh->mlh_reg_mode = lm;
167 lh->mlh_rreg_mode = lm;
168 lh->mlh_type = MDT_REG_LOCK;
171 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
172 const struct lu_name *lname)
174 lh->mlh_reg_mode = lock_mode;
175 lh->mlh_pdo_mode = LCK_MINMODE;
176 lh->mlh_rreg_mode = lock_mode;
177 lh->mlh_type = MDT_PDO_LOCK;
179 if (lu_name_is_valid(lname)) {
180 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
182 /* XXX Workaround for LU-2856
184 * Zero is a valid return value of full_name_hash, but
185 * several users of mlh_pdo_hash assume a non-zero
186 * hash value. We therefore map zero onto an
187 * arbitrary, but consistent value (1) to avoid
188 * problems further down the road. */
189 if (unlikely(lh->mlh_pdo_hash == 0))
190 lh->mlh_pdo_hash = 1;
192 lh->mlh_pdo_hash = 0;
196 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
197 struct mdt_lock_handle *lh)
203 * Any dir access needs couple of locks:
205 * 1) on part of dir we gonna take lookup/modify;
207 * 2) on whole dir to protect it from concurrent splitting and/or to
208 * flush client's cache for readdir().
210 * so, for a given mode and object this routine decides what lock mode
211 * to use for lock #2:
213 * 1) if caller's gonna lookup in dir then we need to protect dir from
214 * being splitted only - LCK_CR
216 * 2) if caller's gonna modify dir then we need to protect dir from
217 * being splitted and to flush cache - LCK_CW
219 * 3) if caller's gonna modify dir and that dir seems ready for
220 * splitting then we need to protect it from any type of access
221 * (lookup/modify/split) - LCK_EX --bzzz
224 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
225 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
228 * Ask underlaying level its opinion about preferable PDO lock mode
229 * having access type passed as regular lock mode:
231 * - MDL_MINMODE means that lower layer does not want to specify lock
234 * - MDL_NL means that no PDO lock should be taken. This is used in some
235 * cases. Say, for non-splittable directories no need to use PDO locks
238 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
239 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
241 if (mode != MDL_MINMODE) {
242 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
245 * Lower layer does not want to specify locking mode. We do it
246 * our selves. No special protection is needed, just flush
247 * client's cache on modification and allow concurrent
250 switch (lh->mlh_reg_mode) {
252 lh->mlh_pdo_mode = LCK_EX;
255 lh->mlh_pdo_mode = LCK_CR;
258 lh->mlh_pdo_mode = LCK_CW;
261 CERROR("Not expected lock type (0x%x)\n",
262 (int)lh->mlh_reg_mode);
267 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
271 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
274 struct mdt_device *mdt = info->mti_mdt;
275 struct lu_name *lname = &info->mti_name;
277 struct mdt_object *parent;
281 LASSERT(!info->mti_cross_ref);
283 OBD_ALLOC(name, NAME_MAX + 1);
286 lname->ln_name = name;
289 * We may want to allow this to mount a completely separate
290 * fileset from the MDT in the future, but keeping it to
291 * ROOT/ only for now avoid potential security issues.
293 *fid = mdt->mdt_md_root_fid;
295 while (rc == 0 && fileset != NULL && *fileset != '\0') {
296 const char *s1 = fileset;
302 while (*s2 != '/' && *s2 != '\0')
310 lname->ln_namelen = s2 - s1;
311 if (lname->ln_namelen > NAME_MAX) {
316 /* reject .. as a path component */
317 if (lname->ln_namelen == 2 &&
318 strncmp(s1, "..", 2) == 0) {
323 strncpy(name, s1, lname->ln_namelen);
324 name[lname->ln_namelen] = '\0';
326 parent = mdt_object_find(info->mti_env, mdt, fid);
327 if (IS_ERR(parent)) {
328 rc = PTR_ERR(parent);
331 /* Only got the fid of this obj by name */
333 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
334 fid, &info->mti_spec);
335 mdt_object_put(info->mti_env, parent);
338 parent = mdt_object_find(info->mti_env, mdt, fid);
340 rc = PTR_ERR(parent);
342 mode = lu_object_attr(&parent->mot_obj);
343 mdt_object_put(info->mti_env, parent);
349 OBD_FREE(name, NAME_MAX + 1);
354 static int mdt_get_root(struct tgt_session_info *tsi)
356 struct mdt_thread_info *info = tsi2mdt_info(tsi);
357 struct mdt_device *mdt = info->mti_mdt;
358 struct mdt_body *repbody;
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));
377 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
379 GOTO(out, rc = err_serious(rc));
381 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
383 repbody->mbo_valid |= OBD_MD_FLID;
387 mdt_thread_info_fini(info);
391 static int mdt_statfs(struct tgt_session_info *tsi)
393 struct ptlrpc_request *req = tgt_ses_req(tsi);
394 struct mdt_thread_info *info = tsi2mdt_info(tsi);
395 struct md_device *next = info->mti_mdt->mdt_child;
396 struct ptlrpc_service_part *svcpt;
397 struct obd_statfs *osfs;
402 svcpt = req->rq_rqbd->rqbd_svcpt;
404 /* This will trigger a watchdog timeout */
405 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
406 (MDT_SERVICE_WATCHDOG_FACTOR *
407 at_get(&svcpt->scp_at_estimate)) + 1);
409 rc = mdt_check_ucred(info);
411 GOTO(out, rc = err_serious(rc));
413 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
414 GOTO(out, rc = err_serious(-ENOMEM));
416 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
418 GOTO(out, rc = -EPROTO);
420 /** statfs information are cached in the mdt_device */
421 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
422 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
423 /** statfs data is too old, get up-to-date one */
424 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
427 spin_lock(&info->mti_mdt->mdt_osfs_lock);
428 info->mti_mdt->mdt_osfs = *osfs;
429 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
430 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
432 /** use cached statfs data */
433 spin_lock(&info->mti_mdt->mdt_osfs_lock);
434 *osfs = info->mti_mdt->mdt_osfs;
435 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
439 mdt_counter_incr(req, LPROC_MDT_STATFS);
441 mdt_thread_info_fini(info);
445 #ifdef CONFIG_FS_POSIX_ACL
447 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
449 * \param info thread info object
450 * \param repbody reply to pack ACLs into
451 * \param o mdt object of file to examine
452 * \param nodemap nodemap of client to reply to
454 * \retval -errno error getting or parsing ACL from disk
456 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
457 struct mdt_object *o, struct lu_nodemap *nodemap)
459 const struct lu_env *env = info->mti_env;
460 struct md_object *next = mdt_object_child(o);
461 struct lu_buf *buf = &info->mti_buf;
464 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
465 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
467 if (buf->lb_len == 0)
470 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
472 if (rc == -ENODATA) {
473 repbody->mbo_aclsize = 0;
474 repbody->mbo_valid |= OBD_MD_FLACL;
476 } else if (rc == -EOPNOTSUPP) {
479 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
480 mdt_obd_name(info->mti_mdt),
481 PFID(mdt_object_fid(o)), rc);
484 rc = nodemap_map_acl(nodemap, buf->lb_buf,
485 rc, NODEMAP_FS_TO_CLIENT);
486 /* if all ACLs mapped out, rc is still >= 0 */
488 CERROR("%s: nodemap_map_acl unable to parse "DFID
489 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
490 PFID(mdt_object_fid(o)), rc);
492 repbody->mbo_aclsize = rc;
493 repbody->mbo_valid |= OBD_MD_FLACL;
501 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
502 const struct lu_attr *attr, const struct lu_fid *fid)
504 struct md_attr *ma = &info->mti_attr;
505 struct obd_export *exp = info->mti_exp;
506 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
508 LASSERT(ma->ma_valid & MA_INODE);
510 if (attr->la_valid & LA_ATIME) {
511 b->mbo_atime = attr->la_atime;
512 b->mbo_valid |= OBD_MD_FLATIME;
514 if (attr->la_valid & LA_MTIME) {
515 b->mbo_mtime = attr->la_mtime;
516 b->mbo_valid |= OBD_MD_FLMTIME;
518 if (attr->la_valid & LA_CTIME) {
519 b->mbo_ctime = attr->la_ctime;
520 b->mbo_valid |= OBD_MD_FLCTIME;
522 if (attr->la_valid & LA_FLAGS) {
523 b->mbo_flags = attr->la_flags;
524 b->mbo_valid |= OBD_MD_FLFLAGS;
526 if (attr->la_valid & LA_NLINK) {
527 b->mbo_nlink = attr->la_nlink;
528 b->mbo_valid |= OBD_MD_FLNLINK;
530 if (attr->la_valid & LA_UID) {
531 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
532 NODEMAP_FS_TO_CLIENT,
534 b->mbo_valid |= OBD_MD_FLUID;
536 if (attr->la_valid & LA_GID) {
537 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
538 NODEMAP_FS_TO_CLIENT,
540 b->mbo_valid |= OBD_MD_FLGID;
542 b->mbo_mode = attr->la_mode;
543 if (attr->la_valid & LA_MODE)
544 b->mbo_valid |= OBD_MD_FLMODE;
545 if (attr->la_valid & LA_TYPE)
546 b->mbo_valid |= OBD_MD_FLTYPE;
550 b->mbo_valid |= OBD_MD_FLID;
551 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid="LPX64"\n",
552 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
556 mdt_body_reverse_idmap(info, b);
558 if (!(attr->la_valid & LA_TYPE))
561 b->mbo_rdev = attr->la_rdev;
562 b->mbo_size = attr->la_size;
563 b->mbo_blocks = attr->la_blocks;
565 if (!S_ISREG(attr->la_mode)) {
566 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
567 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
568 /* means no objects are allocated on osts. */
569 LASSERT(!(ma->ma_valid & MA_LOV));
570 /* just ignore blocks occupied by extend attributes on MDS */
572 /* if no object is allocated on osts, the size on mds is valid.
574 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
575 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
576 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
577 /* A released file stores its size on MDS. */
578 /* But return 1 block for released file, unless tools like tar
579 * will consider it fully sparse. (LU-3864)
581 if (unlikely(b->mbo_size == 0))
585 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
588 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
589 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
590 PFID(fid), (unsigned long long)b->mbo_size);
593 static inline int mdt_body_has_lov(const struct lu_attr *la,
594 const struct mdt_body *body)
596 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
597 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
600 void mdt_client_compatibility(struct mdt_thread_info *info)
602 struct mdt_body *body;
603 struct ptlrpc_request *req = mdt_info_req(info);
604 struct obd_export *exp = req->rq_export;
605 struct md_attr *ma = &info->mti_attr;
606 struct lu_attr *la = &ma->ma_attr;
609 if (exp_connect_layout(exp))
610 /* the client can deal with 16-bit lmm_stripe_count */
613 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
615 if (!mdt_body_has_lov(la, body))
618 /* now we have a reply with a lov for a client not compatible with the
619 * layout lock so we have to clean the layout generation number */
620 if (S_ISREG(la->la_mode))
621 ma->ma_lmm->lmm_layout_gen = 0;
625 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
626 struct mdt_object *o)
628 const struct lu_env *env = info->mti_env;
631 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
640 /* Is it a directory? Let's check for the LMV as well */
641 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
642 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
646 rc2 = mo_xattr_get(env, mdt_object_child(o),
648 XATTR_NAME_DEFAULT_LMV);
650 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
658 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
661 const struct lu_env *env = info->mti_env;
665 LASSERT(info->mti_big_lmm_used == 0);
666 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
670 /* big_lmm may need to be grown */
671 if (info->mti_big_lmmsize < rc) {
672 int size = size_roundup_power2(rc);
674 if (info->mti_big_lmmsize > 0) {
675 /* free old buffer */
676 LASSERT(info->mti_big_lmm);
677 OBD_FREE_LARGE(info->mti_big_lmm,
678 info->mti_big_lmmsize);
679 info->mti_big_lmm = NULL;
680 info->mti_big_lmmsize = 0;
683 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
684 if (info->mti_big_lmm == NULL)
686 info->mti_big_lmmsize = size;
688 LASSERT(info->mti_big_lmmsize >= rc);
690 info->mti_buf.lb_buf = info->mti_big_lmm;
691 info->mti_buf.lb_len = info->mti_big_lmmsize;
692 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
697 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
698 struct md_attr *ma, const char *name)
700 struct md_object *next = mdt_object_child(o);
701 struct lu_buf *buf = &info->mti_buf;
704 if (strcmp(name, XATTR_NAME_LOV) == 0) {
705 buf->lb_buf = ma->ma_lmm;
706 buf->lb_len = ma->ma_lmm_size;
707 LASSERT(!(ma->ma_valid & MA_LOV));
708 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
709 buf->lb_buf = ma->ma_lmv;
710 buf->lb_len = ma->ma_lmv_size;
711 LASSERT(!(ma->ma_valid & MA_LMV));
712 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
713 buf->lb_buf = ma->ma_lmv;
714 buf->lb_len = ma->ma_lmv_size;
715 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
720 rc = mo_xattr_get(info->mti_env, next, buf, name);
724 if (strcmp(name, XATTR_NAME_LOV) == 0) {
725 if (info->mti_big_lmm_used)
726 ma->ma_lmm = info->mti_big_lmm;
728 /* NOT return LOV EA with hole to old client. */
729 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
730 LOV_PATTERN_F_HOLE) &&
731 !(exp_connect_flags(info->mti_exp) &
732 OBD_CONNECT_LFSCK)) {
735 ma->ma_lmm_size = rc;
736 ma->ma_valid |= MA_LOV;
738 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
739 if (info->mti_big_lmm_used)
740 ma->ma_lmv = info->mti_big_lmm;
742 ma->ma_lmv_size = rc;
743 ma->ma_valid |= MA_LMV;
744 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
745 ma->ma_lmv_size = rc;
746 ma->ma_valid |= MA_LMV_DEF;
749 /* Update mdt_max_mdsize so all clients will be aware that */
750 if (info->mti_mdt->mdt_max_mdsize < rc)
751 info->mti_mdt->mdt_max_mdsize = rc;
754 } else if (rc == -ENODATA) {
757 } else if (rc == -ERANGE) {
758 /* Default LMV has fixed size, so it must be able to fit
759 * in the original buffer */
760 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
762 rc = mdt_big_xattr_get(info, o, name);
764 info->mti_big_lmm_used = 1;
772 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
773 struct mdt_object *o, struct lu_fid *pfid)
775 struct lu_buf *buf = &info->mti_buf;
776 struct link_ea_header *leh;
777 struct link_ea_entry *lee;
781 buf->lb_buf = info->mti_big_lmm;
782 buf->lb_len = info->mti_big_lmmsize;
783 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
784 buf, XATTR_NAME_LINK);
785 /* ignore errors, MA_PFID won't be set and it is
786 * up to the caller to treat this as an error */
787 if (rc == -ERANGE || buf->lb_len == 0) {
788 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
789 buf->lb_buf = info->mti_big_lmm;
790 buf->lb_len = info->mti_big_lmmsize;
795 if (rc < sizeof(*leh)) {
796 CERROR("short LinkEA on "DFID": rc = %d\n",
797 PFID(mdt_object_fid(o)), rc);
801 leh = (struct link_ea_header *) buf->lb_buf;
802 lee = (struct link_ea_entry *)(leh + 1);
803 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
804 leh->leh_magic = LINK_EA_MAGIC;
805 leh->leh_reccount = __swab32(leh->leh_reccount);
806 leh->leh_len = __swab64(leh->leh_len);
808 if (leh->leh_magic != LINK_EA_MAGIC)
810 if (leh->leh_reccount == 0)
813 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
814 fid_be_to_cpu(pfid, pfid);
819 int mdt_attr_get_complex(struct mdt_thread_info *info,
820 struct mdt_object *o, struct md_attr *ma)
822 const struct lu_env *env = info->mti_env;
823 struct md_object *next = mdt_object_child(o);
824 struct lu_buf *buf = &info->mti_buf;
825 int need = ma->ma_need;
832 if (mdt_object_exists(o) == 0)
833 GOTO(out, rc = -ENOENT);
834 mode = lu_object_attr(&next->mo_lu);
836 if (need & MA_INODE) {
837 ma->ma_need = MA_INODE;
838 rc = mo_attr_get(env, next, ma);
841 ma->ma_valid |= MA_INODE;
844 if (need & MA_PFID) {
845 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
847 ma->ma_valid |= MA_PFID;
848 /* ignore this error, parent fid is not mandatory */
852 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
853 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
858 if (need & MA_LMV && S_ISDIR(mode)) {
859 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
864 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
865 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
870 if (need & MA_HSM && S_ISREG(mode)) {
871 buf->lb_buf = info->mti_xattr_buf;
872 buf->lb_len = sizeof(info->mti_xattr_buf);
873 CLASSERT(sizeof(struct hsm_attrs) <=
874 sizeof(info->mti_xattr_buf));
875 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
876 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
878 ma->ma_valid |= MA_HSM;
879 else if (rc2 < 0 && rc2 != -ENODATA)
883 #ifdef CONFIG_FS_POSIX_ACL
884 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
885 buf->lb_buf = ma->ma_acl;
886 buf->lb_len = ma->ma_acl_size;
887 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
889 ma->ma_acl_size = rc2;
890 ma->ma_valid |= MA_ACL_DEF;
891 } else if (rc2 == -ENODATA) {
900 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
901 rc, ma->ma_valid, ma->ma_lmm);
905 static int mdt_getattr_internal(struct mdt_thread_info *info,
906 struct mdt_object *o, int ma_need)
908 struct md_object *next = mdt_object_child(o);
909 const struct mdt_body *reqbody = info->mti_body;
910 struct ptlrpc_request *req = mdt_info_req(info);
911 struct md_attr *ma = &info->mti_attr;
912 struct lu_attr *la = &ma->ma_attr;
913 struct req_capsule *pill = info->mti_pill;
914 const struct lu_env *env = info->mti_env;
915 struct mdt_body *repbody;
916 struct lu_buf *buffer = &info->mti_buf;
917 struct obd_export *exp = info->mti_exp;
918 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
923 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
924 RETURN(err_serious(-ENOMEM));
926 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
930 if (mdt_object_remote(o)) {
931 /* This object is located on remote node.*/
932 /* Return -ENOTSUPP for old client */
933 if (!mdt_is_dne_client(req->rq_export))
934 GOTO(out, rc = -ENOTSUPP);
936 repbody->mbo_fid1 = *mdt_object_fid(o);
937 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
941 if (reqbody->mbo_eadatasize > 0) {
942 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
943 if (buffer->lb_buf == NULL)
944 GOTO(out, rc = -EPROTO);
945 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
948 buffer->lb_buf = NULL;
950 ma_need &= ~(MA_LOV | MA_LMV);
951 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
952 mdt_obd_name(info->mti_mdt),
953 req->rq_export->exp_client_uuid.uuid);
956 /* If it is dir object and client require MEA, then we got MEA */
957 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
958 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
959 /* Assumption: MDT_MD size is enough for lmv size. */
960 ma->ma_lmv = buffer->lb_buf;
961 ma->ma_lmv_size = buffer->lb_len;
962 ma->ma_need = MA_INODE;
963 if (ma->ma_lmv_size > 0) {
964 if (reqbody->mbo_valid & OBD_MD_MEA)
965 ma->ma_need |= MA_LMV;
966 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
967 ma->ma_need |= MA_LMV_DEF;
970 ma->ma_lmm = buffer->lb_buf;
971 ma->ma_lmm_size = buffer->lb_len;
972 ma->ma_need = MA_INODE | MA_HSM;
973 if (ma->ma_lmm_size > 0)
974 ma->ma_need |= MA_LOV;
977 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
978 reqbody->mbo_valid & OBD_MD_FLDIREA &&
979 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
980 /* get default stripe info for this dir. */
981 ma->ma_need |= MA_LOV_DEF;
983 ma->ma_need |= ma_need;
985 rc = mdt_attr_get_complex(info, o, ma);
987 CERROR("%s: getattr error for "DFID": rc = %d\n",
988 mdt_obd_name(info->mti_mdt),
989 PFID(mdt_object_fid(o)), rc);
993 /* if file is released, check if a restore is running */
994 if (ma->ma_valid & MA_HSM) {
995 repbody->mbo_valid |= OBD_MD_TSTATE;
996 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
997 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
998 repbody->mbo_t_state = MS_RESTORE;
1001 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
1003 /* the Lustre protocol supposes to return default striping
1004 * on the user-visible root if explicitly requested */
1005 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
1006 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
1007 struct lu_fid rootfid;
1008 struct mdt_object *root;
1009 struct mdt_device *mdt = info->mti_mdt;
1011 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
1014 root = mdt_object_find(env, mdt, &rootfid);
1016 RETURN(PTR_ERR(root));
1017 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
1018 mdt_object_put(info->mti_env, root);
1020 CERROR("%s: getattr error for "DFID": rc = %d\n",
1021 mdt_obd_name(info->mti_mdt),
1022 PFID(mdt_object_fid(o)), rc);
1027 if (likely(ma->ma_valid & MA_INODE))
1028 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1032 if (mdt_body_has_lov(la, reqbody)) {
1033 if (ma->ma_valid & MA_LOV) {
1034 LASSERT(ma->ma_lmm_size);
1035 repbody->mbo_eadatasize = ma->ma_lmm_size;
1036 if (S_ISDIR(la->la_mode))
1037 repbody->mbo_valid |= OBD_MD_FLDIREA;
1039 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1040 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1042 if (ma->ma_valid & MA_LMV) {
1043 /* Return -ENOTSUPP for old client */
1044 if (!mdt_is_striped_client(req->rq_export))
1047 LASSERT(S_ISDIR(la->la_mode));
1048 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1049 repbody->mbo_eadatasize = ma->ma_lmv_size;
1050 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1052 if (ma->ma_valid & MA_LMV_DEF) {
1053 /* Return -ENOTSUPP for old client */
1054 if (!mdt_is_striped_client(req->rq_export))
1056 LASSERT(S_ISDIR(la->la_mode));
1057 repbody->mbo_eadatasize = ma->ma_lmv_size;
1058 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1059 OBD_MD_DEFAULT_MEA);
1061 } else if (S_ISLNK(la->la_mode) &&
1062 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1063 buffer->lb_buf = ma->ma_lmm;
1064 /* eadatasize from client includes NULL-terminator, so
1065 * there is no need to read it */
1066 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1067 rc = mo_readlink(env, next, buffer);
1068 if (unlikely(rc <= 0)) {
1069 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1070 mdt_obd_name(info->mti_mdt),
1071 PFID(mdt_object_fid(o)), rc);
1074 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
1076 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1078 repbody->mbo_valid |= OBD_MD_LINKNAME;
1079 /* we need to report back size with NULL-terminator
1080 * because client expects that */
1081 repbody->mbo_eadatasize = rc + 1;
1082 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1083 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1084 "on "DFID ", expected %d\n",
1085 mdt_obd_name(info->mti_mdt),
1086 rc, PFID(mdt_object_fid(o)),
1087 reqbody->mbo_eadatasize - 1);
1088 /* NULL terminate */
1089 ((char *)ma->ma_lmm)[rc] = 0;
1091 /* If the total CDEBUG() size is larger than a page, it
1092 * will print a warning to the console, avoid this by
1093 * printing just the last part of the symlink. */
1094 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1095 print_limit < rc ? "..." : "", print_limit,
1096 (char *)ma->ma_lmm + rc - print_limit, rc);
1101 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1102 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1103 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1104 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1105 repbody->mbo_max_mdsize);
1108 if (exp_connect_rmtclient(info->mti_exp) &&
1109 reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
1110 void *buf = req_capsule_server_get(pill, &RMF_ACL);
1112 /* mdt_getattr_lock only */
1113 rc = mdt_pack_remote_perm(info, o, buf);
1115 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
1116 repbody->mbo_aclsize = 0;
1119 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
1120 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
1123 #ifdef CONFIG_FS_POSIX_ACL
1124 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1125 (reqbody->mbo_valid & OBD_MD_FLACL))
1126 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1131 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1136 static int mdt_getattr(struct tgt_session_info *tsi)
1138 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1139 struct mdt_object *obj = info->mti_object;
1140 struct req_capsule *pill = info->mti_pill;
1141 struct mdt_body *reqbody;
1142 struct mdt_body *repbody;
1146 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1148 LASSERT(obj != NULL);
1149 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1151 /* Unlike intent case where we need to pre-fill out buffers early on
1152 * in intent policy for ldlm reasons, here we can have a much better
1153 * guess at EA size by just reading it from disk.
1154 * Exceptions are readdir and (missing) directory striping */
1156 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1157 /* No easy way to know how long is the symlink, but it cannot
1158 * be more than PATH_MAX, so we allocate +1 */
1161 /* A special case for fs ROOT: getattr there might fetch
1162 * default EA for entire fs, not just for this dir!
1164 } else if (lu_fid_eq(mdt_object_fid(obj),
1165 &info->mti_mdt->mdt_md_root_fid) &&
1166 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1167 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1169 /* Should the default strping be bigger, mdt_fix_reply
1170 * will reallocate */
1171 rc = DEF_REP_MD_SIZE;
1173 /* Read the actual EA size from disk */
1174 rc = mdt_attr_get_eabuf_size(info, obj);
1178 GOTO(out, rc = err_serious(rc));
1180 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1182 rc = req_capsule_server_pack(pill);
1183 if (unlikely(rc != 0))
1184 GOTO(out, rc = err_serious(rc));
1186 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1187 LASSERT(repbody != NULL);
1188 repbody->mbo_eadatasize = 0;
1189 repbody->mbo_aclsize = 0;
1191 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1192 rc = mdt_init_ucred(info, reqbody);
1194 rc = mdt_check_ucred(info);
1196 GOTO(out_shrink, rc);
1198 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1200 rc = mdt_getattr_internal(info, obj, 0);
1201 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1202 mdt_exit_ucred(info);
1205 mdt_client_compatibility(info);
1206 rc2 = mdt_fix_reply(info);
1210 mdt_thread_info_fini(info);
1215 * Exchange MOF_LOV_CREATED flags between two objects after a
1216 * layout swap. No assumption is made on whether o1 or o2 have
1217 * created objects or not.
1219 * \param[in,out] o1 First swap layout object
1220 * \param[in,out] o2 Second swap layout object
1222 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1226 mutex_lock(&o1->mot_lov_mutex);
1227 mutex_lock(&o2->mot_lov_mutex);
1229 o1_flags = o1->mot_flags;
1230 o1->mot_flags = (o1->mot_flags & ~MOF_LOV_CREATED) |
1231 (o2->mot_flags & MOF_LOV_CREATED);
1233 o2->mot_flags = (o2->mot_flags & ~MOF_LOV_CREATED) |
1234 (o1_flags & MOF_LOV_CREATED);
1236 mutex_unlock(&o2->mot_lov_mutex);
1237 mutex_unlock(&o1->mot_lov_mutex);
1240 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1242 struct mdt_thread_info *info;
1243 struct ptlrpc_request *req = tgt_ses_req(tsi);
1244 struct obd_export *exp = req->rq_export;
1245 struct mdt_object *o1, *o2, *o;
1246 struct mdt_lock_handle *lh1, *lh2;
1247 struct mdc_swap_layouts *msl;
1251 /* client does not support layout lock, so layout swaping
1253 * FIXME: there is a problem for old clients which don't support
1254 * layout lock yet. If those clients have already opened the file
1255 * they won't be notified at all so that old layout may still be
1256 * used to do IO. This can be fixed after file release is landed by
1257 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1258 if (!exp_connect_layout(exp))
1259 RETURN(-EOPNOTSUPP);
1261 info = tsi2mdt_info(tsi);
1263 if (info->mti_dlm_req != NULL)
1264 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1266 o1 = info->mti_object;
1267 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1268 &info->mti_body->mbo_fid2);
1270 GOTO(out, rc = PTR_ERR(o));
1272 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1273 GOTO(put, rc = -ENOENT);
1275 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1276 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1282 /* permission check. Make sure the calling process having permission
1283 * to write both files. */
1284 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1289 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1294 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1296 GOTO(put, rc = -EPROTO);
1298 lh1 = &info->mti_lh[MDT_LH_NEW];
1299 mdt_lock_reg_init(lh1, LCK_EX);
1300 lh2 = &info->mti_lh[MDT_LH_OLD];
1301 mdt_lock_reg_init(lh2, LCK_EX);
1303 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1304 MDS_INODELOCK_XATTR);
1308 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1309 MDS_INODELOCK_XATTR);
1313 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1314 mdt_object_child(o2), msl->msl_flags);
1318 mdt_swap_lov_flag(o1, o2);
1321 mdt_object_unlock(info, o2, lh2, rc);
1323 mdt_object_unlock(info, o1, lh1, rc);
1325 mdt_object_put(info->mti_env, o);
1327 mdt_thread_info_fini(info);
1331 static int mdt_raw_lookup(struct mdt_thread_info *info,
1332 struct mdt_object *parent,
1333 const struct lu_name *lname,
1334 struct ldlm_reply *ldlm_rep)
1336 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1340 LASSERT(!info->mti_cross_ref);
1342 /* Only got the fid of this obj by name */
1343 fid_zero(child_fid);
1344 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1345 lname, child_fid, &info->mti_spec);
1347 struct mdt_body *repbody;
1349 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1350 repbody->mbo_fid1 = *child_fid;
1351 repbody->mbo_valid = OBD_MD_FLID;
1352 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1353 } else if (rc == -ENOENT) {
1354 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1361 * UPDATE lock should be taken against parent, and be released before exit;
1362 * child_bits lock should be taken against child, and be returned back:
1363 * (1)normal request should release the child lock;
1364 * (2)intent request will grant the lock to client.
1366 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1367 struct mdt_lock_handle *lhc,
1369 struct ldlm_reply *ldlm_rep)
1371 struct ptlrpc_request *req = mdt_info_req(info);
1372 struct mdt_body *reqbody = NULL;
1373 struct mdt_object *parent = info->mti_object;
1374 struct mdt_object *child;
1375 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1376 struct lu_name *lname = NULL;
1377 struct mdt_lock_handle *lhp = NULL;
1378 struct ldlm_lock *lock;
1385 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1386 LASSERT(ergo(is_resent,
1387 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1392 if (info->mti_cross_ref) {
1393 /* Only getattr on the child. Parent is on another node. */
1394 mdt_set_disposition(info, ldlm_rep,
1395 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1397 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1399 PFID(mdt_object_fid(child)), ldlm_rep);
1401 rc = mdt_check_resent_lock(info, child, lhc);
1404 } else if (rc > 0) {
1405 mdt_lock_handle_init(lhc);
1406 mdt_lock_reg_init(lhc, LCK_PR);
1409 * Object's name is on another MDS, no lookup or layout
1410 * lock is needed here but update lock is.
1412 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1413 MDS_INODELOCK_LAYOUT);
1414 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1416 rc = mdt_object_lock(info, child, lhc, child_bits);
1421 /* Finally, we can get attr for child. */
1422 if (!mdt_object_exists(child)) {
1423 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1425 "remote object doesn't exist.");
1426 mdt_object_unlock(info, child, lhc, 1);
1430 rc = mdt_getattr_internal(info, child, 0);
1431 if (unlikely(rc != 0))
1432 mdt_object_unlock(info, child, lhc, 1);
1437 lname = &info->mti_name;
1438 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1440 if (lu_name_is_valid(lname)) {
1441 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1442 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1443 PNAME(lname), ldlm_rep);
1445 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1446 if (unlikely(reqbody == NULL))
1447 RETURN(err_serious(-EPROTO));
1449 *child_fid = reqbody->mbo_fid2;
1451 if (unlikely(!fid_is_sane(child_fid)))
1452 RETURN(err_serious(-EINVAL));
1454 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1456 PFID(mdt_object_fid(parent)),
1457 PFID(&reqbody->mbo_fid2), ldlm_rep);
1460 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1462 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1463 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1465 "Parent doesn't exist!");
1469 if (mdt_object_remote(parent)) {
1470 CERROR("%s: parent "DFID" is on remote target\n",
1471 mdt_obd_name(info->mti_mdt),
1472 PFID(mdt_object_fid(parent)));
1476 if (lu_name_is_valid(lname)) {
1477 /* Always allow to lookup ".." */
1478 if (unlikely(lname->ln_namelen == 2 &&
1479 lname->ln_name[0] == '.' &&
1480 lname->ln_name[1] == '.'))
1481 info->mti_spec.sp_permitted = 1;
1483 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1484 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1489 /* step 1: lock parent only if parent is a directory */
1490 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1491 lhp = &info->mti_lh[MDT_LH_PARENT];
1492 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1493 rc = mdt_object_lock(info, parent, lhp,
1494 MDS_INODELOCK_UPDATE);
1495 if (unlikely(rc != 0))
1499 /* step 2: lookup child's fid by name */
1500 fid_zero(child_fid);
1501 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1502 child_fid, &info->mti_spec);
1504 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1507 GOTO(out_parent, rc);
1510 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1513 *step 3: find the child object by fid & lock it.
1514 * regardless if it is local or remote.
1516 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1517 * set parent dir fid the same as child fid in getattr by fid case
1518 * we should not lu_object_find() the object again, could lead
1519 * to hung if there is a concurrent unlink destroyed the object.
1521 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1522 mdt_object_get(info->mti_env, parent);
1525 child = mdt_object_find(info->mti_env, info->mti_mdt,
1529 if (unlikely(IS_ERR(child)))
1530 GOTO(out_parent, rc = PTR_ERR(child));
1532 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1533 if (!mdt_object_exists(child)) {
1534 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1536 "Object doesn't exist!");
1537 GOTO(out_child, rc = -ENOENT);
1540 rc = mdt_check_resent_lock(info, child, lhc);
1542 GOTO(out_child, rc);
1543 } else if (rc > 0) {
1544 mdt_lock_handle_init(lhc);
1545 mdt_lock_reg_init(lhc, LCK_PR);
1548 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1549 mdt_object_exists(child) && !mdt_object_remote(child)) {
1550 struct md_attr *ma = &info->mti_attr;
1553 ma->ma_need = MA_INODE;
1554 rc = mdt_attr_get_complex(info, child, ma);
1555 if (unlikely(rc != 0))
1556 GOTO(out_child, rc);
1558 /* If the file has not been changed for some time, we
1559 * return not only a LOOKUP lock, but also an UPDATE
1560 * lock and this might save us RPC on later STAT. For
1561 * directories, it also let negative dentry cache start
1562 * working for this dir. */
1563 if (ma->ma_valid & MA_INODE &&
1564 ma->ma_attr.la_valid & LA_CTIME &&
1565 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1566 ma->ma_attr.la_ctime < cfs_time_current_sec())
1567 child_bits |= MDS_INODELOCK_UPDATE;
1570 /* layout lock must be granted in a best-effort way
1571 * for IT operations */
1572 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1573 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1574 exp_connect_layout(info->mti_exp) &&
1575 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1576 !mdt_object_remote(child) && ldlm_rep != NULL) {
1577 /* try to grant layout lock for regular file. */
1583 child_bits |= MDS_INODELOCK_LAYOUT;
1584 /* try layout lock, it may fail to be granted due to
1585 * contention at LOOKUP or UPDATE */
1586 if (!mdt_object_lock_try(info, child, lhc,
1588 child_bits &= ~MDS_INODELOCK_LAYOUT;
1589 LASSERT(child_bits != 0);
1590 rc = mdt_object_lock(info, child, lhc,
1596 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1597 * client will enqueue the lock to the remote MDT */
1598 if (mdt_object_remote(child))
1599 child_bits &= ~MDS_INODELOCK_UPDATE;
1600 rc = mdt_object_lock(info, child, lhc, child_bits);
1602 if (unlikely(rc != 0))
1603 GOTO(out_child, rc);
1606 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1608 /* finally, we can get attr for child. */
1609 rc = mdt_getattr_internal(info, child, ma_need);
1610 if (unlikely(rc != 0)) {
1611 mdt_object_unlock(info, child, lhc, 1);
1613 /* Debugging code. */
1614 LDLM_DEBUG(lock, "Returning lock to client");
1615 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1616 &lock->l_resource->lr_name),
1617 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1618 PLDLMRES(lock->l_resource),
1619 PFID(mdt_object_fid(child)));
1622 LDLM_LOCK_PUT(lock);
1626 mdt_object_put(info->mti_env, child);
1629 mdt_object_unlock(info, parent, lhp, 1);
1633 /* normal handler: should release the child lock */
1634 static int mdt_getattr_name(struct tgt_session_info *tsi)
1636 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1637 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1638 struct mdt_body *reqbody;
1639 struct mdt_body *repbody;
1643 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1644 LASSERT(reqbody != NULL);
1645 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1646 LASSERT(repbody != NULL);
1648 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1649 repbody->mbo_eadatasize = 0;
1650 repbody->mbo_aclsize = 0;
1652 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1654 GOTO(out_shrink, rc);
1656 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1657 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1658 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1659 lhc->mlh_reg_lh.cookie = 0;
1661 mdt_exit_ucred(info);
1664 mdt_client_compatibility(info);
1665 rc2 = mdt_fix_reply(info);
1668 mdt_thread_info_fini(info);
1672 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1673 void *karg, void __user *uarg);
1675 static int mdt_set_info(struct tgt_session_info *tsi)
1677 struct ptlrpc_request *req = tgt_ses_req(tsi);
1680 int keylen, vallen, rc = 0;
1684 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1686 DEBUG_REQ(D_HA, req, "no set_info key");
1687 RETURN(err_serious(-EFAULT));
1690 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1693 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1695 DEBUG_REQ(D_HA, req, "no set_info val");
1696 RETURN(err_serious(-EFAULT));
1699 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1702 /* Swab any part of val you need to here */
1703 if (KEY_IS(KEY_READ_ONLY)) {
1704 spin_lock(&req->rq_export->exp_lock);
1706 *exp_connect_flags_ptr(req->rq_export) |=
1709 *exp_connect_flags_ptr(req->rq_export) &=
1710 ~OBD_CONNECT_RDONLY;
1711 spin_unlock(&req->rq_export->exp_lock);
1712 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1713 struct changelog_setinfo *cs = val;
1715 if (vallen != sizeof(*cs)) {
1716 CERROR("%s: bad changelog_clear setinfo size %d\n",
1717 tgt_name(tsi->tsi_tgt), vallen);
1720 if (ptlrpc_req_need_swab(req)) {
1721 __swab64s(&cs->cs_recno);
1722 __swab32s(&cs->cs_id);
1725 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1727 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1729 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1736 static int mdt_readpage(struct tgt_session_info *tsi)
1738 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1739 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1740 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1741 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1742 struct mdt_body *repbody;
1748 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1749 RETURN(err_serious(-ENOMEM));
1751 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1752 if (repbody == NULL || reqbody == NULL)
1753 RETURN(err_serious(-EFAULT));
1756 * prepare @rdpg before calling lower layers and transfer itself. Here
1757 * reqbody->size contains offset of where to start to read and
1758 * reqbody->nlink contains number bytes to read.
1760 rdpg->rp_hash = reqbody->mbo_size;
1761 if (rdpg->rp_hash != reqbody->mbo_size) {
1762 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1763 rdpg->rp_hash, reqbody->mbo_size);
1767 rdpg->rp_attrs = reqbody->mbo_mode;
1768 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1769 rdpg->rp_attrs |= LUDA_64BITHASH;
1770 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1771 exp_max_brw_size(tsi->tsi_exp));
1772 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1774 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1775 if (rdpg->rp_pages == NULL)
1778 for (i = 0; i < rdpg->rp_npages; ++i) {
1779 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1780 if (rdpg->rp_pages[i] == NULL)
1781 GOTO(free_rdpg, rc = -ENOMEM);
1784 /* call lower layers to fill allocated pages with directory data */
1785 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1787 GOTO(free_rdpg, rc);
1789 /* send pages to client */
1790 rc = tgt_sendpage(tsi, rdpg, rc);
1795 for (i = 0; i < rdpg->rp_npages; i++)
1796 if (rdpg->rp_pages[i] != NULL)
1797 __free_page(rdpg->rp_pages[i]);
1798 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1800 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1806 static int mdt_reint_internal(struct mdt_thread_info *info,
1807 struct mdt_lock_handle *lhc,
1810 struct req_capsule *pill = info->mti_pill;
1811 struct mdt_body *repbody;
1816 rc = mdt_reint_unpack(info, op);
1818 CERROR("Can't unpack reint, rc %d\n", rc);
1819 RETURN(err_serious(rc));
1822 /* for replay (no_create) lmm is not needed, client has it already */
1823 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1824 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1827 /* llog cookies are always 0, the field is kept for compatibility */
1828 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1829 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1831 rc = req_capsule_server_pack(pill);
1833 CERROR("Can't pack response, rc %d\n", rc);
1834 RETURN(err_serious(rc));
1837 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1838 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1840 repbody->mbo_eadatasize = 0;
1841 repbody->mbo_aclsize = 0;
1844 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1846 /* for replay no cookkie / lmm need, because client have this already */
1847 if (info->mti_spec.no_create)
1848 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1849 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1851 rc = mdt_init_ucred_reint(info);
1853 GOTO(out_shrink, rc);
1855 rc = mdt_fix_attr_ucred(info, op);
1857 GOTO(out_ucred, rc = err_serious(rc));
1859 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1861 GOTO(out_ucred, rc);
1862 } else if (rc == 1) {
1863 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1864 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1865 GOTO(out_ucred, rc);
1867 rc = mdt_reint_rec(info, lhc);
1870 mdt_exit_ucred(info);
1872 mdt_client_compatibility(info);
1873 rc2 = mdt_fix_reply(info);
1879 static long mdt_reint_opcode(struct ptlrpc_request *req,
1880 const struct req_format **fmt)
1882 struct mdt_device *mdt;
1883 struct mdt_rec_reint *rec;
1886 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1888 opc = rec->rr_opcode;
1889 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1890 if (opc < REINT_MAX && fmt[opc] != NULL)
1891 req_capsule_extend(&req->rq_pill, fmt[opc]);
1893 mdt = mdt_exp2dev(req->rq_export);
1894 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1895 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1896 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1897 opc = err_serious(-EFAULT);
1900 opc = err_serious(-EFAULT);
1905 static int mdt_reint(struct tgt_session_info *tsi)
1909 static const struct req_format *reint_fmts[REINT_MAX] = {
1910 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1911 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1912 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1913 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1914 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1915 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1916 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1917 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1918 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1923 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1925 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1927 * No lock possible here from client to pass it to reint code
1930 rc = mdt_reint_internal(info, NULL, opc);
1931 mdt_thread_info_fini(info);
1936 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1940 /* this should sync the whole device */
1941 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1943 struct dt_device *dt = mdt->mdt_bottom;
1947 rc = dt->dd_ops->dt_sync(env, dt);
1951 /* this should sync this object */
1952 static int mdt_object_sync(struct mdt_thread_info *info)
1954 struct md_object *next;
1958 if (!mdt_object_exists(info->mti_object)) {
1959 CWARN("%s: non existing object "DFID": rc = %d\n",
1960 mdt_obd_name(info->mti_mdt),
1961 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1964 next = mdt_object_child(info->mti_object);
1965 rc = mo_object_sync(info->mti_env, next);
1970 static int mdt_sync(struct tgt_session_info *tsi)
1972 struct ptlrpc_request *req = tgt_ses_req(tsi);
1973 struct req_capsule *pill = tsi->tsi_pill;
1974 struct mdt_body *body;
1979 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1980 RETURN(err_serious(-ENOMEM));
1982 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
1983 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1985 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1987 /* sync an object */
1988 rc = mdt_object_sync(info);
1990 const struct lu_fid *fid;
1991 struct lu_attr *la = &info->mti_attr.ma_attr;
1993 info->mti_attr.ma_need = MA_INODE;
1994 info->mti_attr.ma_valid = 0;
1995 rc = mdt_attr_get_complex(info, info->mti_object,
1998 body = req_capsule_server_get(pill,
2000 fid = mdt_object_fid(info->mti_object);
2001 mdt_pack_attr2body(info, body, la, fid);
2004 mdt_thread_info_fini(info);
2007 mdt_counter_incr(req, LPROC_MDT_SYNC);
2013 * Handle quota control requests to consult current usage/limit, but also
2014 * to configure quota enforcement
2016 static int mdt_quotactl(struct tgt_session_info *tsi)
2018 struct obd_export *exp = tsi->tsi_exp;
2019 struct req_capsule *pill = tsi->tsi_pill;
2020 struct obd_quotactl *oqctl, *repoqc;
2022 struct mdt_device *mdt = mdt_exp2dev(exp);
2023 struct lu_device *qmt = mdt->mdt_qmt_dev;
2024 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
2027 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2029 RETURN(err_serious(-EPROTO));
2031 rc = req_capsule_server_pack(pill);
2033 RETURN(err_serious(rc));
2035 switch (oqctl->qc_cmd) {
2036 /* master quotactl */
2039 if (!nodemap_can_setquota(nodemap))
2044 RETURN(-EOPNOTSUPP);
2045 /* slave quotactl */
2050 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2054 /* map uid/gid for remote client */
2056 if (exp_connect_rmtclient(exp)) {
2057 struct lustre_idmap_table *idmap;
2059 idmap = exp->exp_mdt_data.med_idmap;
2061 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2062 oqctl->qc_cmd != Q_GETINFO))
2065 if (oqctl->qc_type == USRQUOTA)
2066 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2068 else if (oqctl->qc_type == GRPQUOTA)
2069 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2074 if (id == CFS_IDMAP_NOTFOUND) {
2075 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2080 if (oqctl->qc_type == USRQUOTA)
2081 id = nodemap_map_id(nodemap, NODEMAP_UID,
2082 NODEMAP_CLIENT_TO_FS, id);
2083 else if (oqctl->qc_type == GRPQUOTA)
2084 id = nodemap_map_id(nodemap, NODEMAP_UID,
2085 NODEMAP_CLIENT_TO_FS, id);
2087 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2089 RETURN(err_serious(-EFAULT));
2091 if (oqctl->qc_id != id)
2092 swap(oqctl->qc_id, id);
2094 switch (oqctl->qc_cmd) {
2100 /* forward quotactl request to QMT */
2101 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2106 /* slave quotactl */
2107 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2112 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2116 if (oqctl->qc_id != id)
2117 swap(oqctl->qc_id, id);
2123 /** clone llog ctxt from child (mdd)
2124 * This allows remote llog (replicator) access.
2125 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2126 * context was originally set up, or we can handle them directly.
2127 * I choose the latter, but that means I need any llog
2128 * contexts set up by child to be accessable by the mdt. So we clone the
2129 * context into our context list here.
2131 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2134 struct md_device *next = mdt->mdt_child;
2135 struct llog_ctxt *ctxt;
2138 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2141 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2142 if (rc || ctxt == NULL) {
2146 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2148 CERROR("Can't set mdt ctxt %d\n", rc);
2153 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2154 struct mdt_device *mdt, int idx)
2156 struct llog_ctxt *ctxt;
2158 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2161 /* Put once for the get we just did, and once for the clone */
2162 llog_ctxt_put(ctxt);
2163 llog_ctxt_put(ctxt);
2168 * sec context handlers
2170 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2174 rc = mdt_handle_idmap(tsi);
2176 struct ptlrpc_request *req = tgt_ses_req(tsi);
2179 opc = lustre_msg_get_opc(req->rq_reqmsg);
2180 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2181 sptlrpc_svc_ctx_invalidate(req);
2184 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2190 * quota request handlers
2192 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2194 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2195 struct lu_device *qmt = mdt->mdt_qmt_dev;
2200 RETURN(err_serious(-EOPNOTSUPP));
2202 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2206 struct mdt_object *mdt_object_new(const struct lu_env *env,
2207 struct mdt_device *d,
2208 const struct lu_fid *f)
2210 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2211 struct lu_object *o;
2212 struct mdt_object *m;
2215 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2216 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2217 if (unlikely(IS_ERR(o)))
2218 m = (struct mdt_object *)o;
2224 struct mdt_object *mdt_object_find(const struct lu_env *env,
2225 struct mdt_device *d,
2226 const struct lu_fid *f)
2228 struct lu_object *o;
2229 struct mdt_object *m;
2232 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2233 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2234 if (unlikely(IS_ERR(o)))
2235 m = (struct mdt_object *)o;
2243 * Asyncronous commit for mdt device.
2245 * Pass asynchonous commit call down the MDS stack.
2247 * \param env environment
2248 * \param mdt the mdt device
2250 static void mdt_device_commit_async(const struct lu_env *env,
2251 struct mdt_device *mdt)
2253 struct dt_device *dt = mdt->mdt_bottom;
2257 rc = dt->dd_ops->dt_commit_async(env, dt);
2258 if (unlikely(rc != 0))
2259 CWARN("%s: async commit start failed: rc = %d\n",
2260 mdt_obd_name(mdt), rc);
2261 atomic_inc(&mdt->mdt_async_commit_count);
2266 * Mark the lock as "synchonous".
2268 * Mark the lock to deffer transaction commit to the unlock time.
2270 * \param lock the lock to mark as "synchonous"
2272 * \see mdt_is_lock_sync
2273 * \see mdt_save_lock
2275 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2277 lock->l_ast_data = (void*)1;
2281 * Check whehter the lock "synchonous" or not.
2283 * \param lock the lock to check
2284 * \retval 1 the lock is "synchonous"
2285 * \retval 0 the lock isn't "synchronous"
2287 * \see mdt_set_lock_sync
2288 * \see mdt_save_lock
2290 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2292 return lock->l_ast_data != NULL;
2296 * Blocking AST for mdt locks.
2298 * Starts transaction commit if in case of COS lock conflict or
2299 * deffers such a commit to the mdt_save_lock.
2301 * \param lock the lock which blocks a request or cancelling lock
2302 * \param desc unused
2303 * \param data unused
2304 * \param flag indicates whether this cancelling or blocking callback
2306 * \see ldlm_blocking_ast_nocheck
2308 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2309 void *data, int flag)
2311 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2312 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2316 if (flag == LDLM_CB_CANCELING)
2319 lock_res_and_lock(lock);
2320 if (lock->l_blocking_ast != mdt_blocking_ast) {
2321 unlock_res_and_lock(lock);
2324 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2325 lock->l_blocking_lock != NULL) {
2326 if (mdt_cos_is_enabled(mdt) &&
2327 lock->l_client_cookie !=
2328 lock->l_blocking_lock->l_client_cookie)
2329 mdt_set_lock_sync(lock);
2330 else if (mdt_slc_is_enabled(mdt) &&
2331 ldlm_is_cos_incompat(lock->l_blocking_lock))
2332 mdt_set_lock_sync(lock);
2334 rc = ldlm_blocking_ast_nocheck(lock);
2336 /* There is no lock conflict if l_blocking_lock == NULL,
2337 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2338 * when the last reference to a local lock was released */
2339 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2342 rc = lu_env_init(&env, LCT_LOCAL);
2343 if (unlikely(rc != 0))
2344 CWARN("%s: lu_env initialization failed, cannot "
2345 "start asynchronous commit: rc = %d\n",
2348 mdt_device_commit_async(&env, mdt);
2355 * Blocking AST for cross-MDT lock
2357 * Discard lock from uncommitted_slc_locks and cancel it.
2359 * \param lock the lock which blocks a request or cancelling lock
2360 * \param desc unused
2361 * \param data unused
2362 * \param flag indicates whether this cancelling or blocking callback
2363 * \retval 0 on success
2364 * \retval negative number on error
2366 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2367 void *data, int flag)
2369 struct lustre_handle lockh;
2374 case LDLM_CB_BLOCKING:
2375 ldlm_lock2handle(lock, &lockh);
2376 rc = ldlm_cli_cancel(&lockh,
2377 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2379 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2383 case LDLM_CB_CANCELING:
2384 LDLM_DEBUG(lock, "Revoke remote lock");
2385 /* discard slc lock here so that it can be cleaned anytime,
2386 * especially for cleanup_resource() */
2387 tgt_discard_slc_lock(lock);
2396 int mdt_check_resent_lock(struct mdt_thread_info *info,
2397 struct mdt_object *mo,
2398 struct mdt_lock_handle *lhc)
2400 /* the lock might already be gotten in ldlm_handle_enqueue() */
2401 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2402 struct ptlrpc_request *req = mdt_info_req(info);
2403 struct ldlm_lock *lock;
2405 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2406 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2408 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2409 * a resend case, however, it could be already destroyed
2410 * due to client eviction or a raced cancel RPC. */
2411 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64,
2412 lhc->mlh_reg_lh.cookie);
2416 if (!fid_res_name_eq(mdt_object_fid(mo),
2417 &lock->l_resource->lr_name)) {
2418 CWARN("%s: Although resent, but still not "
2419 "get child lock:"DFID"\n",
2420 info->mti_exp->exp_obd->obd_name,
2421 PFID(mdt_object_fid(mo)));
2422 LDLM_LOCK_PUT(lock);
2425 LDLM_LOCK_PUT(lock);
2431 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2432 const struct lu_fid *fid, struct lustre_handle *lh,
2433 enum ldlm_mode mode, __u64 ibits, bool nonblock)
2435 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2436 union ldlm_policy_data *policy = &mti->mti_policy;
2437 struct ldlm_res_id *res_id = &mti->mti_res_id;
2441 LASSERT(mdt_object_remote(o));
2443 fid_build_reg_res_name(fid, res_id);
2445 memset(einfo, 0, sizeof(*einfo));
2446 einfo->ei_type = LDLM_IBITS;
2447 einfo->ei_mode = mode;
2448 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2449 einfo->ei_cb_cp = ldlm_completion_ast;
2450 einfo->ei_enq_slave = 0;
2451 einfo->ei_res_id = res_id;
2453 einfo->ei_nonblock = 1;
2455 memset(policy, 0, sizeof(*policy));
2456 policy->l_inodebits.bits = ibits;
2458 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2463 static int mdt_object_local_lock(struct mdt_thread_info *info,
2464 struct mdt_object *o,
2465 struct mdt_lock_handle *lh, __u64 ibits,
2466 bool nonblock, bool cos_incompat)
2468 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2469 union ldlm_policy_data *policy = &info->mti_policy;
2470 struct ldlm_res_id *res_id = &info->mti_res_id;
2475 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2476 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2477 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2478 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2481 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2482 lh->mlh_reg_mode == LCK_EX);
2483 dlmflags |= LDLM_FL_COS_INCOMPAT;
2484 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2485 dlmflags |= LDLM_FL_COS_ENABLED;
2488 /* Only enqueue LOOKUP lock for remote object */
2489 if (mdt_object_remote(o))
2490 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2492 if (lh->mlh_type == MDT_PDO_LOCK) {
2493 /* check for exists after object is locked */
2494 if (mdt_object_exists(o) == 0) {
2495 /* Non-existent object shouldn't have PDO lock */
2498 /* Non-dir object shouldn't have PDO lock */
2499 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2504 memset(policy, 0, sizeof(*policy));
2505 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2507 dlmflags |= LDLM_FL_ATOMIC_CB;
2509 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2512 * Take PDO lock on whole directory and build correct @res_id for lock
2513 * on part of directory.
2515 if (lh->mlh_pdo_hash != 0) {
2516 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2517 mdt_lock_pdo_mode(info, o, lh);
2518 if (lh->mlh_pdo_mode != LCK_NL) {
2520 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2521 * is never going to be sent to client and we do not
2522 * want it slowed down due to possible cancels.
2524 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2525 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2526 policy, res_id, dlmflags,
2527 info->mti_exp == NULL ? NULL :
2528 &info->mti_exp->exp_handle.h_cookie);
2529 if (unlikely(rc != 0))
2530 GOTO(out_unlock, rc);
2534 * Finish res_id initializing by name hash marking part of
2535 * directory which is taking modification.
2537 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2540 policy->l_inodebits.bits = ibits;
2543 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2544 * going to be sent to client. If it is - mdt_intent_policy() path will
2545 * fix it up and turn FL_LOCAL flag off.
2547 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2548 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2549 info->mti_exp == NULL ? NULL :
2550 &info->mti_exp->exp_handle.h_cookie);
2553 mdt_object_unlock(info, o, lh, 1);
2554 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2555 lh->mlh_pdo_hash != 0 &&
2556 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2557 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2563 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2564 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2567 struct mdt_lock_handle *local_lh = NULL;
2571 if (!mdt_object_remote(o)) {
2572 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2577 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2578 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2579 MDS_INODELOCK_XATTR);
2581 /* Only enqueue LOOKUP lock for remote object */
2582 if (ibits & MDS_INODELOCK_LOOKUP) {
2583 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2584 nonblock, cos_incompat);
2591 if (ibits & MDS_INODELOCK_UPDATE) {
2592 /* Sigh, PDO needs to enqueue 2 locks right now, but
2593 * enqueue RPC can only request 1 lock, to avoid extra
2594 * RPC, so it will instead enqueue EX lock for remote
2595 * object anyway XXX*/
2596 if (lh->mlh_type == MDT_PDO_LOCK &&
2597 lh->mlh_pdo_hash != 0) {
2598 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2599 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2600 PFID(mdt_object_fid(o)));
2601 lh->mlh_pdo_hash = 0;
2602 lh->mlh_rreg_mode = LCK_EX;
2603 lh->mlh_type = MDT_REG_LOCK;
2605 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2608 MDS_INODELOCK_UPDATE, nonblock);
2609 if (rc != ELDLM_OK) {
2610 if (local_lh != NULL)
2611 mdt_object_unlock(info, o, local_lh, rc);
2619 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2620 struct mdt_lock_handle *lh, __u64 ibits)
2622 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2625 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2626 struct mdt_lock_handle *lh, __u64 ibits,
2629 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2630 return mdt_object_lock_internal(info, o, lh, ibits, false,
2634 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2635 struct mdt_lock_handle *lh, __u64 ibits)
2637 struct mdt_lock_handle tmp = *lh;
2640 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2647 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2648 struct mdt_object *o, struct mdt_lock_handle *lh,
2649 __u64 ibits, bool cos_incompat)
2651 struct mdt_lock_handle tmp = *lh;
2654 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2655 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2663 * Save a lock within request object.
2665 * Keep the lock referenced until whether client ACK or transaction
2666 * commit happens or release the lock immediately depending on input
2667 * parameters. If COS is ON, a write lock is converted to COS lock
2670 * \param info thead info object
2671 * \param h lock handle
2672 * \param mode lock mode
2673 * \param decref force immediate lock releasing
2675 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2676 enum ldlm_mode mode, int decref)
2680 if (lustre_handle_is_used(h)) {
2681 if (decref || !info->mti_has_trans ||
2682 !(mode & (LCK_PW | LCK_EX))) {
2683 mdt_fid_unlock(h, mode);
2685 struct mdt_device *mdt = info->mti_mdt;
2686 struct ldlm_lock *lock = ldlm_handle2lock(h);
2687 struct ptlrpc_request *req = mdt_info_req(info);
2690 cos = (mdt_cos_is_enabled(mdt) ||
2691 mdt_slc_is_enabled(mdt));
2693 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2696 /* there is no request if mdt_object_unlock() is called
2697 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2698 if (likely(req != NULL)) {
2699 CDEBUG(D_HA, "request = %p reply state = %p"
2700 " transno = "LPD64"\n", req,
2701 req->rq_reply_state, req->rq_transno);
2703 ldlm_lock_downgrade(lock, LCK_COS);
2706 ptlrpc_save_lock(req, h, mode, cos);
2708 mdt_fid_unlock(h, mode);
2710 if (mdt_is_lock_sync(lock)) {
2711 CDEBUG(D_HA, "found sync-lock,"
2712 " async commit started\n");
2713 mdt_device_commit_async(info->mti_env,
2716 LDLM_LOCK_PUT(lock);
2725 * Save cross-MDT lock in uncommitted_slc_locks
2727 * Keep the lock referenced until transaction commit happens or release the lock
2728 * immediately depending on input parameters.
2730 * \param info thead info object
2731 * \param h lock handle
2732 * \param mode lock mode
2733 * \param decref force immediate lock releasing
2735 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2736 struct lustre_handle *h, enum ldlm_mode mode,
2741 if (lustre_handle_is_used(h)) {
2742 if (decref || !info->mti_has_trans ||
2743 !(mode & (LCK_PW | LCK_EX))) {
2744 ldlm_lock_decref_and_cancel(h, mode);
2746 struct ldlm_lock *lock = ldlm_handle2lock(h);
2747 struct ptlrpc_request *req = mdt_info_req(info);
2749 LASSERT(req != NULL);
2750 tgt_save_slc_lock(lock, req->rq_transno);
2751 ldlm_lock_decref(h, mode);
2760 * Unlock mdt object.
2762 * Immeditely release the regular lock and the PDO lock or save the
2763 * lock in request and keep them referenced until client ACK or
2764 * transaction commit.
2766 * \param info thread info object
2767 * \param o mdt object
2768 * \param lh mdt lock handle referencing regular and PDO locks
2769 * \param decref force immediate lock releasing
2771 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2772 struct mdt_lock_handle *lh, int decref)
2776 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2777 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2778 mdt_save_remote_lock(info, &lh->mlh_rreg_lh, lh->mlh_rreg_mode, decref);
2783 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2784 const struct lu_fid *f,
2785 struct mdt_lock_handle *lh,
2788 struct mdt_object *o;
2790 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2794 rc = mdt_object_lock(info, o, lh, ibits);
2796 mdt_object_put(info->mti_env, o);
2803 void mdt_object_unlock_put(struct mdt_thread_info * info,
2804 struct mdt_object * o,
2805 struct mdt_lock_handle *lh,
2808 mdt_object_unlock(info, o, lh, decref);
2809 mdt_object_put(info->mti_env, o);
2813 * Generic code handling requests that have struct mdt_body passed in:
2815 * - extract mdt_body from request and save it in @info, if present;
2817 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2820 * - if HABEO_CORPUS flag is set for this request type check whether object
2821 * actually exists on storage (lu_object_exists()).
2824 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2826 const struct mdt_body *body;
2827 struct mdt_object *obj;
2828 const struct lu_env *env;
2829 struct req_capsule *pill;
2833 env = info->mti_env;
2834 pill = info->mti_pill;
2836 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2840 if (!(body->mbo_valid & OBD_MD_FLID))
2843 if (!fid_is_sane(&body->mbo_fid1)) {
2844 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2848 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2850 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2851 mdt_object_put(env, obj);
2854 info->mti_object = obj;
2863 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2865 struct req_capsule *pill = info->mti_pill;
2869 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2870 rc = mdt_body_unpack(info, flags);
2874 if (rc == 0 && (flags & HABEO_REFERO)) {
2876 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2877 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2879 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2880 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2883 rc = req_capsule_server_pack(pill);
2888 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2890 lh->mlh_type = MDT_NUL_LOCK;
2891 lh->mlh_reg_lh.cookie = 0ull;
2892 lh->mlh_reg_mode = LCK_MINMODE;
2893 lh->mlh_pdo_lh.cookie = 0ull;
2894 lh->mlh_pdo_mode = LCK_MINMODE;
2895 lh->mlh_rreg_lh.cookie = 0ull;
2896 lh->mlh_rreg_mode = LCK_MINMODE;
2899 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2901 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2902 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2906 * Initialize fields of struct mdt_thread_info. Other fields are left in
2907 * uninitialized state, because it's too expensive to zero out whole
2908 * mdt_thread_info (> 1K) on each request arrival.
2910 void mdt_thread_info_init(struct ptlrpc_request *req,
2911 struct mdt_thread_info *info)
2915 info->mti_pill = &req->rq_pill;
2918 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2919 mdt_lock_handle_init(&info->mti_lh[i]);
2921 /* mdt device: it can be NULL while CONNECT */
2922 if (req->rq_export) {
2923 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2924 info->mti_exp = req->rq_export;
2926 info->mti_mdt = NULL;
2927 info->mti_env = req->rq_svc_thread->t_env;
2928 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2930 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2931 info->mti_big_buf = LU_BUF_NULL;
2932 info->mti_body = NULL;
2933 info->mti_object = NULL;
2934 info->mti_dlm_req = NULL;
2935 info->mti_has_trans = 0;
2936 info->mti_cross_ref = 0;
2937 info->mti_opdata = 0;
2938 info->mti_big_lmm_used = 0;
2940 info->mti_spec.no_create = 0;
2941 info->mti_spec.sp_rm_entry = 0;
2942 info->mti_spec.sp_permitted = 0;
2943 info->mti_spec.sp_migrate_close = 0;
2945 info->mti_spec.u.sp_ea.eadata = NULL;
2946 info->mti_spec.u.sp_ea.eadatalen = 0;
2949 void mdt_thread_info_fini(struct mdt_thread_info *info)
2953 if (info->mti_object != NULL) {
2954 mdt_object_put(info->mti_env, info->mti_object);
2955 info->mti_object = NULL;
2958 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2959 mdt_lock_handle_fini(&info->mti_lh[i]);
2960 info->mti_env = NULL;
2961 info->mti_pill = NULL;
2962 info->mti_exp = NULL;
2964 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2965 lu_buf_free(&info->mti_big_buf);
2968 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2970 struct mdt_thread_info *mti;
2972 mti = mdt_th_info(tsi->tsi_env);
2973 LASSERT(mti != NULL);
2975 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2976 if (tsi->tsi_corpus != NULL) {
2977 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2978 lu_object_get(tsi->tsi_corpus);
2980 mti->mti_body = tsi->tsi_mdt_body;
2981 mti->mti_dlm_req = tsi->tsi_dlm_req;
2986 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2988 struct ptlrpc_request *req = tgt_ses_req(tsi);
2993 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
2995 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
2996 set_current_state(TASK_UNINTERRUPTIBLE);
2997 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3000 rc = tgt_connect(tsi);
3004 rc = mdt_init_idmap(tsi);
3009 obd_disconnect(class_export_get(req->rq_export));
3028 static int mdt_intent_getattr(enum mdt_it_code opcode,
3029 struct mdt_thread_info *info,
3030 struct ldlm_lock **,
3033 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3034 struct mdt_thread_info *info,
3035 struct ldlm_lock **lockp,
3038 static int mdt_intent_layout(enum mdt_it_code opcode,
3039 struct mdt_thread_info *info,
3040 struct ldlm_lock **,
3042 static int mdt_intent_reint(enum mdt_it_code opcode,
3043 struct mdt_thread_info *info,
3044 struct ldlm_lock **,
3047 static struct mdt_it_flavor {
3048 const struct req_format *it_fmt;
3050 int (*it_act)(enum mdt_it_code ,
3051 struct mdt_thread_info *,
3052 struct ldlm_lock **,
3055 } mdt_it_flavor[] = {
3057 .it_fmt = &RQF_LDLM_INTENT,
3058 /*.it_flags = HABEO_REFERO,*/
3060 .it_act = mdt_intent_reint,
3061 .it_reint = REINT_OPEN
3064 .it_fmt = &RQF_LDLM_INTENT,
3066 * OCREAT is not a MUTABOR request as if the file
3068 * We do the extra check of OBD_CONNECT_RDONLY in
3069 * mdt_reint_open() when we really need to create
3073 .it_act = mdt_intent_reint,
3074 .it_reint = REINT_OPEN
3077 .it_fmt = &RQF_LDLM_INTENT,
3078 .it_flags = MUTABOR,
3079 .it_act = mdt_intent_reint,
3080 .it_reint = REINT_CREATE
3082 [MDT_IT_GETATTR] = {
3083 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3084 .it_flags = HABEO_REFERO,
3085 .it_act = mdt_intent_getattr
3087 [MDT_IT_READDIR] = {
3093 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3094 .it_flags = HABEO_REFERO,
3095 .it_act = mdt_intent_getattr
3098 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3099 .it_flags = MUTABOR,
3101 .it_reint = REINT_UNLINK
3105 .it_flags = MUTABOR,
3108 [MDT_IT_GETXATTR] = {
3109 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3110 .it_flags = HABEO_CORPUS,
3111 .it_act = mdt_intent_getxattr
3114 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3116 .it_act = mdt_intent_layout
3121 mdt_intent_lock_replace(struct mdt_thread_info *info,
3122 struct ldlm_lock **lockp,
3123 struct mdt_lock_handle *lh,
3126 struct ptlrpc_request *req = mdt_info_req(info);
3127 struct ldlm_lock *lock = *lockp;
3128 struct ldlm_lock *new_lock;
3130 /* If possible resent found a lock, @lh is set to its handle */
3131 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3133 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3134 lh->mlh_reg_lh.cookie = 0;
3138 LASSERTF(new_lock != NULL,
3139 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3142 * If we've already given this lock to a client once, then we should
3143 * have no readers or writers. Otherwise, we should have one reader
3144 * _or_ writer ref (which will be zeroed below) before returning the
3147 if (new_lock->l_export == req->rq_export) {
3148 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3150 LASSERT(new_lock->l_export == NULL);
3151 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3156 if (new_lock->l_export == req->rq_export) {
3158 * Already gave this to the client, which means that we
3159 * reconstructed a reply.
3161 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3164 LDLM_LOCK_RELEASE(new_lock);
3165 lh->mlh_reg_lh.cookie = 0;
3166 RETURN(ELDLM_LOCK_REPLACED);
3170 * Fixup the lock to be given to the client.
3172 lock_res_and_lock(new_lock);
3173 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3174 * possible blocking AST. */
3175 while (new_lock->l_readers > 0) {
3176 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3177 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3178 new_lock->l_readers--;
3180 while (new_lock->l_writers > 0) {
3181 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3182 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3183 new_lock->l_writers--;
3186 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3187 new_lock->l_blocking_ast = lock->l_blocking_ast;
3188 new_lock->l_completion_ast = lock->l_completion_ast;
3189 new_lock->l_remote_handle = lock->l_remote_handle;
3190 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3192 unlock_res_and_lock(new_lock);
3194 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3195 &new_lock->l_remote_handle,
3196 &new_lock->l_exp_hash);
3198 LDLM_LOCK_RELEASE(new_lock);
3199 lh->mlh_reg_lh.cookie = 0;
3201 RETURN(ELDLM_LOCK_REPLACED);
3204 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3205 struct ldlm_lock *new_lock,
3206 struct mdt_lock_handle *lh,
3209 struct ptlrpc_request *req = mdt_info_req(info);
3210 struct ldlm_request *dlmreq;
3212 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3215 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3217 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3218 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3220 if (flags & LDLM_FL_RESENT) {
3221 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3222 lh->mlh_reg_mode = new_lock->l_granted_mode;
3224 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3225 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3226 lh->mlh_reg_lh.cookie);
3231 * If the xid matches, then we know this is a resent request, and allow
3232 * it. (It's probably an OPEN, for which we don't send a lock.
3234 if (req_can_reconstruct(req, NULL))
3238 * This remote handle isn't enqueued, so we never received or processed
3239 * this request. Clear MSG_RESENT, because it can be handled like any
3240 * normal request now.
3242 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3244 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3245 dlmreq->lock_handle[0].cookie);
3248 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3249 struct mdt_thread_info *info,
3250 struct ldlm_lock **lockp,
3253 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3254 struct ldlm_reply *ldlm_rep = NULL;
3258 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3259 * (for the resend case) or a new lock. Below we will use it to
3260 * replace the original lock.
3262 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3263 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3264 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3265 rc = mdt_object_lock(info, info->mti_object, lhc,
3266 MDS_INODELOCK_XATTR);
3271 grc = mdt_getxattr(info);
3273 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3275 if (mdt_info_req(info)->rq_repmsg != NULL)
3276 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3277 if (ldlm_rep == NULL)
3278 RETURN(err_serious(-EFAULT));
3280 ldlm_rep->lock_policy_res2 = grc;
3285 static int mdt_intent_getattr(enum mdt_it_code opcode,
3286 struct mdt_thread_info *info,
3287 struct ldlm_lock **lockp,
3290 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3292 struct ldlm_reply *ldlm_rep;
3293 struct mdt_body *reqbody;
3294 struct mdt_body *repbody;
3298 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3301 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3304 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3305 repbody->mbo_eadatasize = 0;
3306 repbody->mbo_aclsize = 0;
3310 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3312 case MDT_IT_GETATTR:
3313 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3317 CERROR("Unsupported intent (%d)\n", opcode);
3318 GOTO(out_shrink, rc = -EINVAL);
3321 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3323 GOTO(out_shrink, rc);
3325 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3326 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3328 /* Get lock from request for possible resent case. */
3329 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3331 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3332 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3334 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3335 ldlm_rep->lock_policy_res2 = 0;
3336 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3337 ldlm_rep->lock_policy_res2) {
3338 lhc->mlh_reg_lh.cookie = 0ull;
3339 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3342 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3345 mdt_exit_ucred(info);
3347 mdt_client_compatibility(info);
3348 rc2 = mdt_fix_reply(info);
3354 static int mdt_intent_layout(enum mdt_it_code opcode,
3355 struct mdt_thread_info *info,
3356 struct ldlm_lock **lockp,
3359 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3360 struct layout_intent *layout;
3362 struct mdt_object *obj = NULL;
3363 int layout_size = 0;
3367 if (opcode != MDT_IT_LAYOUT) {
3368 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3373 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3377 if (layout->li_opc != LAYOUT_INTENT_ACCESS) {
3378 CERROR("%s: Unsupported layout intent opc %d\n",
3379 mdt_obd_name(info->mti_mdt), layout->li_opc);
3383 fid = &info->mti_tmp_fid2;
3384 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3386 /* Get lock from request for possible resent case. */
3387 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3389 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3391 GOTO(out, rc = PTR_ERR(obj));
3393 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3394 layout_size = mdt_attr_get_eabuf_size(info, obj);
3395 if (layout_size < 0)
3396 GOTO(out_obj, rc = layout_size);
3398 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3399 info->mti_mdt->mdt_max_mdsize = layout_size;
3402 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3403 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3405 rc = req_capsule_server_pack(info->mti_pill);
3409 mdt_object_put(info->mti_env, obj);
3411 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3412 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3415 lhc->mlh_reg_lh.cookie = 0;
3420 static int mdt_intent_reint(enum mdt_it_code opcode,
3421 struct mdt_thread_info *info,
3422 struct ldlm_lock **lockp,
3425 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3426 struct ldlm_reply *rep = NULL;
3430 static const struct req_format *intent_fmts[REINT_MAX] = {
3431 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3432 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3437 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3441 if (mdt_it_flavor[opcode].it_reint != opc) {
3442 CERROR("Reint code %ld doesn't match intent: %d\n",
3444 RETURN(err_serious(-EPROTO));
3447 /* Get lock from request for possible resent case. */
3448 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3450 rc = mdt_reint_internal(info, lhc, opc);
3452 /* Check whether the reply has been packed successfully. */
3453 if (mdt_info_req(info)->rq_repmsg != NULL)
3454 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3456 RETURN(err_serious(-EFAULT));
3458 /* MDC expects this in any case */
3460 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3462 /* the open lock or the lock for cross-ref object should be
3463 * returned to the client */
3464 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3465 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3466 rep->lock_policy_res2 = 0;
3467 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3471 rep->lock_policy_res2 = clear_serious(rc);
3473 if (rep->lock_policy_res2 == -ENOENT &&
3474 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3475 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3476 rep->lock_policy_res2 = 0;
3478 lhc->mlh_reg_lh.cookie = 0ull;
3479 if (rc == -ENOTCONN || rc == -ENODEV ||
3480 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3482 * If it is the disconnect error (ENODEV & ENOCONN), the error
3483 * will be returned by rq_status, and client at ptlrpc layer
3484 * will detect this, then disconnect, reconnect the import
3485 * immediately, instead of impacting the following the rpc.
3490 * For other cases, the error will be returned by intent, and client
3491 * will retrieve the result from intent.
3493 RETURN(ELDLM_LOCK_ABORTED);
3496 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3504 case IT_OPEN|IT_CREAT:
3511 rc = MDT_IT_READDIR;
3514 rc = MDT_IT_GETATTR;
3526 rc = MDT_IT_GETXATTR;
3531 case IT_QUOTA_DQACQ:
3536 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3543 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3544 struct mdt_thread_info *info,
3545 struct ldlm_lock **lockp, __u64 flags)
3547 struct req_capsule *pill = info->mti_pill;
3548 struct ptlrpc_request *req = mdt_info_req(info);
3549 struct mdt_it_flavor *flv;
3554 opc = mdt_intent_code(itopc);
3558 if (opc == MDT_IT_QUOTA) {
3559 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3562 RETURN(-EOPNOTSUPP);
3564 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3565 /* pass the request to quota master */
3566 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3567 mdt_info_req(info), lockp,
3572 flv = &mdt_it_flavor[opc];
3573 if (flv->it_fmt != NULL)
3574 req_capsule_extend(pill, flv->it_fmt);
3576 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3580 if (flv->it_flags & MUTABOR &&
3581 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3584 if (flv->it_act != NULL) {
3585 struct ldlm_reply *rep;
3587 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3589 /* execute policy */
3590 rc = flv->it_act(opc, info, lockp, flags);
3592 /* Check whether the reply has been packed successfully. */
3593 if (req->rq_repmsg != NULL) {
3594 rep = req_capsule_server_get(info->mti_pill,
3596 rep->lock_policy_res2 =
3597 ptlrpc_status_hton(rep->lock_policy_res2);
3604 static int mdt_intent_policy(struct ldlm_namespace *ns,
3605 struct ldlm_lock **lockp, void *req_cookie,
3606 enum ldlm_mode mode, __u64 flags, void *data)
3608 struct tgt_session_info *tsi;
3609 struct mdt_thread_info *info;
3610 struct ptlrpc_request *req = req_cookie;
3611 struct ldlm_intent *it;
3612 struct req_capsule *pill;
3617 LASSERT(req != NULL);
3619 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3621 info = tsi2mdt_info(tsi);
3622 LASSERT(info != NULL);
3623 pill = info->mti_pill;
3624 LASSERT(pill->rc_req == req);
3626 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3627 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3628 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3630 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3634 /* Lock without inodebits makes no sense and will oops
3635 * later in ldlm. Let's check it now to see if we have
3636 * ibits corrupted somewhere in mdt_intent_opc().
3637 * The case for client miss to set ibits has been
3638 * processed by others. */
3639 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3640 lr_type == LDLM_IBITS,
3641 info->mti_dlm_req->lock_desc.\
3642 l_policy_data.l_inodebits.bits != 0));
3644 rc = err_serious(-EFAULT);
3646 /* No intent was provided */
3647 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3648 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3649 rc = req_capsule_server_pack(pill);
3651 rc = err_serious(rc);
3653 mdt_thread_info_fini(info);
3657 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3659 struct seq_server_site *ss = mdt_seq_site(mdt);
3661 if (ss->ss_node_id == 0)
3664 if (ss->ss_client_seq != NULL) {
3665 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3666 ss->ss_client_seq->lcs_exp = NULL;
3669 if (ss->ss_server_fld != NULL) {
3670 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3671 ss->ss_server_fld->lsf_control_exp = NULL;
3675 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3677 struct seq_server_site *ss = mdt_seq_site(mdt);
3682 if (ss->ss_server_seq != NULL)
3683 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3686 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3688 mdt_seq_fini_cli(mdt);
3689 mdt_deregister_seq_exp(mdt);
3691 return seq_site_fini(env, mdt_seq_site(mdt));
3695 * It will retrieve its FLDB entries from MDT0, and it only happens
3696 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3697 * and it needs to refresh FLDB from the MDT0.
3699 static int mdt_register_lwp_callback(void *data)
3702 struct mdt_device *mdt = data;
3703 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3707 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3709 rc = lu_env_init(&env, LCT_MD_THREAD);
3711 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3715 /* Allocate new sequence now to avoid creating local transaction
3716 * in the normal transaction process */
3717 rc = seq_server_check_and_alloc_super(&env,
3718 mdt_seq_site(mdt)->ss_server_seq);
3723 rc = fld_update_from_controller(&env, fld);
3725 CERROR("%s: cannot update controller: rc = %d\n",
3726 mdt_obd_name(mdt), rc);
3735 static int mdt_register_seq_exp(struct mdt_device *mdt)
3737 struct seq_server_site *ss = mdt_seq_site(mdt);
3738 char *lwp_name = NULL;
3741 if (ss->ss_node_id == 0)
3744 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3745 if (lwp_name == NULL)
3746 GOTO(out_free, rc = -ENOMEM);
3748 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3752 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3757 rc = lustre_register_lwp_item(lwp_name,
3758 &ss->ss_server_fld->lsf_control_exp,
3759 mdt_register_lwp_callback, mdt);
3761 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3762 ss->ss_client_seq->lcs_exp = NULL;
3766 if (lwp_name != NULL)
3767 OBD_FREE(lwp_name, MAX_OBD_NAME);
3773 * Init client sequence manager which is used by local MDS to talk to sequence
3774 * controller on remote node.
3776 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3778 struct seq_server_site *ss = mdt_seq_site(mdt);
3783 /* check if this is adding the first MDC and controller is not yet
3785 OBD_ALLOC_PTR(ss->ss_client_seq);
3786 if (ss->ss_client_seq == NULL)
3789 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3790 if (prefix == NULL) {
3791 OBD_FREE_PTR(ss->ss_client_seq);
3792 ss->ss_client_seq = NULL;
3796 /* Note: seq_client_fini will be called in seq_site_fini */
3797 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3798 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3799 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3801 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3803 OBD_FREE_PTR(ss->ss_client_seq);
3804 ss->ss_client_seq = NULL;
3808 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3813 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3815 struct seq_server_site *ss;
3819 ss = mdt_seq_site(mdt);
3820 /* init sequence controller server(MDT0) */
3821 if (ss->ss_node_id == 0) {
3822 OBD_ALLOC_PTR(ss->ss_control_seq);
3823 if (ss->ss_control_seq == NULL)
3826 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3827 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3830 GOTO(out_seq_fini, rc);
3833 /* Init normal sequence server */
3834 OBD_ALLOC_PTR(ss->ss_server_seq);
3835 if (ss->ss_server_seq == NULL)
3836 GOTO(out_seq_fini, rc = -ENOMEM);
3838 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3839 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3841 GOTO(out_seq_fini, rc);
3843 /* init seq client for seq server to talk to seq controller(MDT0) */
3844 rc = mdt_seq_init_cli(env, mdt);
3846 GOTO(out_seq_fini, rc);
3848 if (ss->ss_node_id != 0)
3849 /* register controller export through lwp */
3850 rc = mdt_register_seq_exp(mdt);
3855 mdt_seq_fini(env, mdt);
3863 static int mdt_fld_fini(const struct lu_env *env,
3864 struct mdt_device *m)
3866 struct seq_server_site *ss = mdt_seq_site(m);
3869 if (ss && ss->ss_server_fld) {
3870 fld_server_fini(env, ss->ss_server_fld);
3871 OBD_FREE_PTR(ss->ss_server_fld);
3872 ss->ss_server_fld = NULL;
3878 static int mdt_fld_init(const struct lu_env *env,
3880 struct mdt_device *m)
3882 struct seq_server_site *ss;
3886 ss = mdt_seq_site(m);
3888 OBD_ALLOC_PTR(ss->ss_server_fld);
3889 if (ss->ss_server_fld == NULL)
3890 RETURN(rc = -ENOMEM);
3892 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3895 OBD_FREE_PTR(ss->ss_server_fld);
3896 ss->ss_server_fld = NULL;
3903 static void mdt_stack_pre_fini(const struct lu_env *env,
3904 struct mdt_device *m, struct lu_device *top)
3906 struct lustre_cfg_bufs *bufs;
3907 struct lustre_cfg *lcfg;
3908 struct mdt_thread_info *info;
3913 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3914 LASSERT(info != NULL);
3916 bufs = &info->mti_u.bufs;
3918 LASSERT(m->mdt_child_exp);
3919 LASSERT(m->mdt_child_exp->exp_obd);
3921 /* process cleanup, pass mdt obd name to get obd umount flags */
3922 /* XXX: this is needed because all layers are referenced by
3923 * objects (some of them are pinned by osd, for example *
3924 * the proper solution should be a model where object used
3925 * by osd only doesn't have mdt/mdd slices -bzzz */
3926 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3927 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3928 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3932 top->ld_ops->ldo_process_config(env, top, lcfg);
3933 lustre_cfg_free(lcfg);
3937 static void mdt_stack_fini(const struct lu_env *env,
3938 struct mdt_device *m, struct lu_device *top)
3940 struct obd_device *obd = mdt2obd_dev(m);
3941 struct lustre_cfg_bufs *bufs;
3942 struct lustre_cfg *lcfg;
3943 struct mdt_thread_info *info;
3947 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3948 LASSERT(info != NULL);
3950 lu_dev_del_linkage(top->ld_site, top);
3952 lu_site_purge(env, top->ld_site, -1);
3954 bufs = &info->mti_u.bufs;
3955 /* process cleanup, pass mdt obd name to get obd umount flags */
3956 /* another purpose is to let all layers to release their objects */
3957 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3962 lustre_cfg_bufs_set_string(bufs, 1, flags);
3963 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3968 top->ld_ops->ldo_process_config(env, top, lcfg);
3969 lustre_cfg_free(lcfg);
3971 lu_site_purge(env, top->ld_site, -1);
3973 m->mdt_child = NULL;
3974 m->mdt_bottom = NULL;
3976 obd_disconnect(m->mdt_child_exp);
3977 m->mdt_child_exp = NULL;
3979 obd_disconnect(m->mdt_bottom_exp);
3980 m->mdt_child_exp = NULL;
3983 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3984 const char *next, struct obd_export **exp)
3986 struct obd_connect_data *data = NULL;
3987 struct obd_device *obd;
3991 OBD_ALLOC_PTR(data);
3993 GOTO(out, rc = -ENOMEM);
3995 obd = class_name2obd(next);
3997 CERROR("%s: can't locate next device: %s\n",
3998 mdt_obd_name(m), next);
3999 GOTO(out, rc = -ENOTCONN);
4002 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4003 data->ocd_version = LUSTRE_VERSION_CODE;
4005 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4007 CERROR("%s: cannot connect to next dev %s (%d)\n",
4008 mdt_obd_name(m), next, rc);
4018 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4019 struct lustre_cfg *cfg)
4021 char *dev = lustre_cfg_string(cfg, 0);
4022 int rc, name_size, uuid_size;
4023 char *name, *uuid, *p;
4024 struct lustre_cfg_bufs *bufs;
4025 struct lustre_cfg *lcfg;
4026 struct obd_device *obd;
4027 struct lustre_profile *lprof;
4028 struct lu_site *site;
4031 /* in 1.8 we had the only device in the stack - MDS.
4032 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4033 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4034 * to generate names and setup MDT, MDD. MDT will be using
4035 * generated name to connect to MDD. for MDD the next device
4036 * will be LOD with name taken from so called "profile" which
4037 * is generated by mount_option line
4039 * 1.8 MGS generates config. commands like this:
4040 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4041 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4042 * 2.0 MGS generates config. commands like this:
4043 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4044 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4045 * 3:lustre-MDT0000-mdtlov 4:f
4047 * we generate MDD name from MDT one, just replacing T with D
4049 * after all the preparations, the logical equivalent will be
4050 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4051 * 3:lustre-MDT0000-mdtlov 4:f
4052 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4053 * 3:lustre-MDD0000 4:f
4055 * notice we build the stack from down to top: MDD first, then MDT */
4057 name_size = MAX_OBD_NAME;
4058 uuid_size = MAX_OBD_NAME;
4060 OBD_ALLOC(name, name_size);
4061 OBD_ALLOC(uuid, uuid_size);
4062 if (name == NULL || uuid == NULL)
4063 GOTO(cleanup_mem, rc = -ENOMEM);
4065 OBD_ALLOC_PTR(bufs);
4067 GOTO(cleanup_mem, rc = -ENOMEM);
4070 p = strstr(name, "-MDT");
4072 GOTO(free_bufs, rc = -ENOMEM);
4075 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4077 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4078 if (lprof == NULL || lprof->lp_dt == NULL) {
4079 CERROR("can't find the profile: %s\n",
4080 lustre_cfg_string(cfg, 0));
4081 GOTO(free_bufs, rc = -EINVAL);
4084 lustre_cfg_bufs_reset(bufs, name);
4085 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4086 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4087 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4089 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4091 GOTO(put_profile, rc = -ENOMEM);
4093 rc = class_attach(lcfg);
4095 GOTO(lcfg_cleanup, rc);
4097 obd = class_name2obd(name);
4099 CERROR("Can not find obd %s (%s in config)\n",
4100 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4101 GOTO(lcfg_cleanup, rc = -EINVAL);
4104 lustre_cfg_free(lcfg);
4106 lustre_cfg_bufs_reset(bufs, name);
4107 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4108 lustre_cfg_bufs_set_string(bufs, 2, dev);
4109 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4111 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4113 GOTO(class_detach, rc = -ENOMEM);
4115 rc = class_setup(obd, lcfg);
4117 GOTO(class_detach, rc);
4119 /* connect to MDD we just setup */
4120 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4122 GOTO(class_detach, rc);
4124 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4126 LASSERT(mdt_lu_site(mdt) == NULL);
4127 mdt->mdt_lu_dev.ld_site = site;
4128 site->ls_top_dev = &mdt->mdt_lu_dev;
4129 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4131 /* now connect to bottom OSD */
4132 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4133 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4135 GOTO(class_detach, rc);
4137 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4139 rc = lu_env_refill((struct lu_env *)env);
4141 CERROR("Failure to refill session: '%d'\n", rc);
4143 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4148 class_detach(obd, lcfg);
4150 lustre_cfg_free(lcfg);
4152 class_put_profile(lprof);
4157 OBD_FREE(name, name_size);
4159 OBD_FREE(uuid, uuid_size);
4163 /* setup quota master target on MDT0 */
4164 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4165 struct lustre_cfg *cfg)
4167 struct obd_device *obd;
4168 char *dev = lustre_cfg_string(cfg, 0);
4169 char *qmtname, *uuid, *p;
4170 struct lustre_cfg_bufs *bufs;
4171 struct lustre_cfg *lcfg;
4172 struct lustre_profile *lprof;
4173 struct obd_connect_data *data;
4177 LASSERT(mdt->mdt_qmt_exp == NULL);
4178 LASSERT(mdt->mdt_qmt_dev == NULL);
4180 /* quota master is on MDT0 only for now */
4181 if (mdt->mdt_seq_site.ss_node_id != 0)
4184 /* MGS generates config commands which look as follows:
4185 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4186 * 3:lustre-MDT0000-mdtlov 4:f
4188 * We generate the QMT name from the MDT one, just replacing MD with QM
4189 * after all the preparations, the logical equivalent will be:
4190 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4191 * 3:lustre-MDT0000-osd 4:f */
4192 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4193 OBD_ALLOC(uuid, UUID_MAX);
4194 OBD_ALLOC_PTR(bufs);
4195 OBD_ALLOC_PTR(data);
4196 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4197 GOTO(cleanup_mem, rc = -ENOMEM);
4199 strcpy(qmtname, dev);
4200 p = strstr(qmtname, "-MDT");
4202 GOTO(cleanup_mem, rc = -ENOMEM);
4203 /* replace MD with QM */
4207 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4209 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4210 if (lprof == NULL || lprof->lp_dt == NULL) {
4211 CERROR("can't find profile for %s\n",
4212 lustre_cfg_string(cfg, 0));
4213 GOTO(cleanup_mem, rc = -EINVAL);
4216 lustre_cfg_bufs_reset(bufs, qmtname);
4217 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4218 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4219 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4221 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4223 GOTO(put_profile, rc = -ENOMEM);
4225 rc = class_attach(lcfg);
4227 GOTO(lcfg_cleanup, rc);
4229 obd = class_name2obd(qmtname);
4231 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4232 lustre_cfg_string(cfg, 0));
4233 GOTO(lcfg_cleanup, rc = -EINVAL);
4236 lustre_cfg_free(lcfg);
4238 lustre_cfg_bufs_reset(bufs, qmtname);
4239 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4240 lustre_cfg_bufs_set_string(bufs, 2, dev);
4242 /* for quota, the next device should be the OSD device */
4243 lustre_cfg_bufs_set_string(bufs, 3,
4244 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4246 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4248 GOTO(class_detach, rc = -ENOMEM);
4250 rc = class_setup(obd, lcfg);
4252 GOTO(class_detach, rc);
4254 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4256 /* configure local quota objects */
4257 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4261 GOTO(class_cleanup, rc);
4263 /* connect to quota master target */
4264 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4265 data->ocd_version = LUSTRE_VERSION_CODE;
4266 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4269 CERROR("cannot connect to quota master device %s (%d)\n",
4271 GOTO(class_cleanup, rc);
4277 class_manual_cleanup(obd);
4278 mdt->mdt_qmt_dev = NULL;
4282 class_detach(obd, lcfg);
4284 lustre_cfg_free(lcfg);
4286 class_put_profile(lprof);
4291 OBD_FREE(qmtname, MAX_OBD_NAME);
4293 OBD_FREE(uuid, UUID_MAX);
4299 /* Shutdown quota master target associated with mdt */
4300 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4304 if (mdt->mdt_qmt_exp == NULL)
4306 LASSERT(mdt->mdt_qmt_dev != NULL);
4308 /* the qmt automatically shuts down when the mdt disconnects */
4309 obd_disconnect(mdt->mdt_qmt_exp);
4310 mdt->mdt_qmt_exp = NULL;
4311 mdt->mdt_qmt_dev = NULL;
4315 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4316 * for now. This will be removed along with converting rest of MDT code
4317 * to use tgt_session_info */
4318 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4320 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4323 rc = mdt_getxattr(info);
4325 mdt_thread_info_fini(info);
4329 static struct tgt_handler mdt_tgt_handlers[] = {
4330 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4331 0, MDS_CONNECT, mdt_tgt_connect,
4332 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4333 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4334 0, MDS_DISCONNECT, tgt_disconnect,
4335 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4336 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4337 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4338 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4339 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4340 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4341 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4342 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4344 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4345 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4346 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4347 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4348 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4349 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4350 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4351 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4353 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4354 mdt_hsm_ct_register),
4355 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4356 mdt_hsm_ct_unregister),
4357 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4359 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4361 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4362 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4364 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4369 static struct tgt_handler mdt_sec_ctx_ops[] = {
4370 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4371 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4372 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4375 static struct tgt_handler mdt_quota_ops[] = {
4376 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4379 static struct tgt_opc_slice mdt_common_slice[] = {
4381 .tos_opc_start = MDS_FIRST_OPC,
4382 .tos_opc_end = MDS_LAST_OPC,
4383 .tos_hs = mdt_tgt_handlers
4386 .tos_opc_start = OBD_FIRST_OPC,
4387 .tos_opc_end = OBD_LAST_OPC,
4388 .tos_hs = tgt_obd_handlers
4391 .tos_opc_start = LDLM_FIRST_OPC,
4392 .tos_opc_end = LDLM_LAST_OPC,
4393 .tos_hs = tgt_dlm_handlers
4396 .tos_opc_start = SEC_FIRST_OPC,
4397 .tos_opc_end = SEC_LAST_OPC,
4398 .tos_hs = mdt_sec_ctx_ops
4401 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4402 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4403 .tos_hs = tgt_out_handlers
4406 .tos_opc_start = FLD_FIRST_OPC,
4407 .tos_opc_end = FLD_LAST_OPC,
4408 .tos_hs = fld_handlers
4411 .tos_opc_start = SEQ_FIRST_OPC,
4412 .tos_opc_end = SEQ_LAST_OPC,
4413 .tos_hs = seq_handlers
4416 .tos_opc_start = QUOTA_DQACQ,
4417 .tos_opc_end = QUOTA_LAST_OPC,
4418 .tos_hs = mdt_quota_ops
4421 .tos_opc_start = LLOG_FIRST_OPC,
4422 .tos_opc_end = LLOG_LAST_OPC,
4423 .tos_hs = tgt_llog_handlers
4426 .tos_opc_start = LFSCK_FIRST_OPC,
4427 .tos_opc_end = LFSCK_LAST_OPC,
4428 .tos_hs = tgt_lfsck_handlers
4436 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4438 struct md_device *next = m->mdt_child;
4439 struct lu_device *d = &m->mdt_lu_dev;
4440 struct obd_device *obd = mdt2obd_dev(m);
4441 struct lfsck_stop stop;
4444 stop.ls_status = LS_PAUSED;
4446 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4448 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4449 target_recovery_fini(obd);
4450 ping_evictor_stop();
4452 if (m->mdt_opts.mo_coordinator)
4453 mdt_hsm_cdt_stop(m);
4455 mdt_hsm_cdt_fini(m);
4457 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4458 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4460 if (m->mdt_namespace != NULL)
4461 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4462 d->ld_obd->obd_force);
4464 obd_exports_barrier(obd);
4465 obd_zombie_barrier();
4469 tgt_fini(env, &m->mdt_lut);
4470 mdt_fs_cleanup(env, m);
4471 upcall_cache_cleanup(m->mdt_identity_cache);
4472 m->mdt_identity_cache = NULL;
4474 if (m->mdt_namespace != NULL) {
4475 ldlm_namespace_free_post(m->mdt_namespace);
4476 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4479 mdt_quota_fini(env, m);
4481 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4483 mdt_seq_fini(env, m);
4484 mdt_fld_fini(env, m);
4489 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4491 LASSERT(atomic_read(&d->ld_ref) == 0);
4493 server_put_mount(mdt_obd_name(m), true);
4498 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4500 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4501 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4503 struct mdt_thread_info *info;
4504 struct obd_device *obd;
4505 const char *dev = lustre_cfg_string(cfg, 0);
4506 const char *num = lustre_cfg_string(cfg, 2);
4507 struct lustre_mount_info *lmi = NULL;
4508 struct lustre_sb_info *lsi;
4510 struct seq_server_site *ss_site;
4511 const char *identity_upcall = "NONE";
4512 struct md_device *next;
4518 lu_device_init(&m->mdt_lu_dev, ldt);
4520 * Environment (env) might be missing mdt_thread_key values at that
4521 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4524 * Usually device allocation path doesn't use module key values, but
4525 * mdt has to do a lot of work here, so allocate key value.
4527 rc = lu_env_refill((struct lu_env *)env);
4531 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4532 LASSERT(info != NULL);
4534 obd = class_name2obd(dev);
4535 LASSERT(obd != NULL);
4537 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4538 m->mdt_opts.mo_evict_tgt_nids = 1;
4539 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4541 /* default is coordinator off, it is started through conf_param
4543 m->mdt_opts.mo_coordinator = 0;
4545 lmi = server_get_mount(dev);
4547 CERROR("Cannot get mount info for %s!\n", dev);
4550 lsi = s2lsi(lmi->lmi_sb);
4551 /* CMD is supported only in IAM mode */
4553 node_id = simple_strtol(num, NULL, 10);
4554 obd->u.obt.obt_magic = OBT_MAGIC;
4555 if (lsi->lsi_lmd != NULL &&
4556 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4557 m->mdt_skip_lfsck = 1;
4560 m->mdt_squash.rsi_uid = 0;
4561 m->mdt_squash.rsi_gid = 0;
4562 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4563 init_rwsem(&m->mdt_squash.rsi_sem);
4564 spin_lock_init(&m->mdt_osfs_lock);
4565 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4566 m->mdt_enable_remote_dir = 0;
4567 m->mdt_enable_remote_dir_gid = 0;
4569 atomic_set(&m->mdt_mds_mds_conns, 0);
4570 atomic_set(&m->mdt_async_commit_count, 0);
4572 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4573 m->mdt_lu_dev.ld_obd = obd;
4574 /* Set this lu_device to obd for error handling purposes. */
4575 obd->obd_lu_dev = &m->mdt_lu_dev;
4577 /* init the stack */
4578 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4580 CERROR("%s: Can't init device stack, rc %d\n",
4581 mdt_obd_name(m), rc);
4586 ss_site = mdt_seq_site(m);
4587 s->ld_seq_site = ss_site;
4590 /* set server index */
4591 ss_site->ss_node_id = node_id;
4593 /* failover is the default
4594 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4595 * assumed whose ss_node_id == 0 XXX
4597 obd->obd_replayable = 1;
4598 /* No connection accepted until configurations will finish */
4599 obd->obd_no_conn = 1;
4601 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4602 char *str = lustre_cfg_string(cfg, 4);
4603 if (strchr(str, 'n')) {
4604 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4605 obd->obd_replayable = 0;
4609 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4611 GOTO(err_fini_stack, rc);
4613 rc = mdt_seq_init(env, m);
4615 GOTO(err_fini_fld, rc);
4617 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4618 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4619 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4620 LDLM_NAMESPACE_SERVER,
4621 LDLM_NAMESPACE_GREEDY,
4623 if (m->mdt_namespace == NULL)
4624 GOTO(err_fini_seq, rc = -ENOMEM);
4626 m->mdt_namespace->ns_lvbp = m;
4627 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4629 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4630 /* set obd_namespace for compatibility with old code */
4631 obd->obd_namespace = m->mdt_namespace;
4633 rc = mdt_hsm_cdt_init(m);
4635 CERROR("%s: error initializing coordinator, rc %d\n",
4636 mdt_obd_name(m), rc);
4637 GOTO(err_free_ns, rc);
4640 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4641 OBD_FAIL_MDS_ALL_REQUEST_NET,
4642 OBD_FAIL_MDS_ALL_REPLY_NET);
4644 GOTO(err_free_hsm, rc);
4646 rc = mdt_fs_setup(env, m, obd, lsi);
4650 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4652 next = m->mdt_child;
4653 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4656 GOTO(err_fs_cleanup, rc);
4658 if (mntopts & MNTOPT_USERXATTR)
4659 m->mdt_opts.mo_user_xattr = 1;
4661 m->mdt_opts.mo_user_xattr = 0;
4663 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4665 GOTO(err_fs_cleanup, rc);
4667 if (mntopts & MNTOPT_ACL)
4668 m->mdt_opts.mo_acl = 1;
4670 m->mdt_opts.mo_acl = 0;
4672 /* XXX: to support suppgid for ACL, we enable identity_upcall
4673 * by default, otherwise, maybe got unexpected -EACCESS. */
4674 if (m->mdt_opts.mo_acl)
4675 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4677 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4679 &mdt_identity_upcall_cache_ops);
4680 if (IS_ERR(m->mdt_identity_cache)) {
4681 rc = PTR_ERR(m->mdt_identity_cache);
4682 m->mdt_identity_cache = NULL;
4683 GOTO(err_fs_cleanup, rc);
4686 rc = mdt_procfs_init(m, dev);
4688 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4689 GOTO(err_recovery, rc);
4692 rc = mdt_quota_init(env, m, cfg);
4694 GOTO(err_procfs, rc);
4696 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4697 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4698 "mdt_ldlm_client", m->mdt_ldlm_client);
4700 ping_evictor_start();
4702 /* recovery will be started upon mdt_prepare()
4703 * when the whole stack is complete and ready
4704 * to serve the requests */
4706 /* Reduce the initial timeout on an MDS because it doesn't need such
4707 * a long timeout as an OST does. Adaptive timeouts will adjust this
4708 * value appropriately. */
4709 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4710 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4716 target_recovery_fini(obd);
4717 upcall_cache_cleanup(m->mdt_identity_cache);
4718 m->mdt_identity_cache = NULL;
4720 mdt_fs_cleanup(env, m);
4722 tgt_fini(env, &m->mdt_lut);
4724 mdt_hsm_cdt_fini(m);
4726 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4727 obd->obd_namespace = m->mdt_namespace = NULL;
4729 mdt_seq_fini(env, m);
4731 mdt_fld_fini(env, m);
4733 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4736 server_put_mount(dev, true);
4740 /* For interoperability, the left element is old parameter, the right one
4741 * is the new version of the parameter, if some parameter is deprecated,
4742 * the new version should be set as NULL. */
4743 static struct cfg_interop_param mdt_interop_param[] = {
4744 { "mdt.group_upcall", NULL },
4745 { "mdt.quota_type", NULL },
4746 { "mdd.quota_type", NULL },
4747 { "mdt.som", NULL },
4748 { "mdt.rootsquash", "mdt.root_squash" },
4749 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4753 /* used by MGS to process specific configurations */
4754 static int mdt_process_config(const struct lu_env *env,
4755 struct lu_device *d, struct lustre_cfg *cfg)
4757 struct mdt_device *m = mdt_dev(d);
4758 struct md_device *md_next = m->mdt_child;
4759 struct lu_device *next = md2lu_dev(md_next);
4763 switch (cfg->lcfg_command) {
4765 struct obd_device *obd = d->ld_obd;
4767 /* For interoperability */
4768 struct cfg_interop_param *ptr = NULL;
4769 struct lustre_cfg *old_cfg = NULL;
4772 param = lustre_cfg_string(cfg, 1);
4773 if (param == NULL) {
4774 CERROR("param is empty\n");
4779 ptr = class_find_old_param(param, mdt_interop_param);
4781 if (ptr->new_param == NULL) {
4783 CWARN("For interoperability, skip this %s."
4784 " It is obsolete.\n", ptr->old_param);
4788 CWARN("Found old param %s, changed it to %s.\n",
4789 ptr->old_param, ptr->new_param);
4792 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4799 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4801 if (rc > 0 || rc == -ENOSYS) {
4802 /* is it an HSM var ? */
4803 rc = class_process_proc_param(PARAM_HSM,
4804 hsm_cdt_get_proc_vars(),
4806 if (rc > 0 || rc == -ENOSYS)
4807 /* we don't understand; pass it on */
4808 rc = next->ld_ops->ldo_process_config(env, next,
4812 if (old_cfg != NULL)
4813 lustre_cfg_free(cfg);
4818 /* others are passed further */
4819 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4825 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4826 const struct lu_object_header *hdr,
4827 struct lu_device *d)
4829 struct mdt_object *mo;
4833 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4835 struct lu_object *o;
4836 struct lu_object_header *h;
4839 h = &mo->mot_header;
4840 lu_object_header_init(h);
4841 lu_object_init(o, h, d);
4842 lu_object_add_top(h, o);
4843 o->lo_ops = &mdt_obj_ops;
4844 spin_lock_init(&mo->mot_write_lock);
4845 mutex_init(&mo->mot_lov_mutex);
4846 init_rwsem(&mo->mot_open_sem);
4852 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4853 const struct lu_object_conf *unused)
4855 struct mdt_device *d = mdt_dev(o->lo_dev);
4856 struct lu_device *under;
4857 struct lu_object *below;
4861 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4862 PFID(lu_object_fid(o)));
4864 under = &d->mdt_child->md_lu_dev;
4865 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4866 if (below != NULL) {
4867 lu_object_add(o, below);
4874 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4876 struct mdt_object *mo = mdt_obj(o);
4877 struct lu_object_header *h;
4881 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4882 PFID(lu_object_fid(o)));
4884 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4885 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4888 lu_object_header_fini(h);
4889 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4894 static int mdt_object_print(const struct lu_env *env, void *cookie,
4895 lu_printer_t p, const struct lu_object *o)
4897 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4899 return (*p)(env, cookie,
4900 LUSTRE_MDT_NAME"-object@%p(flags=%d, writecount=%d)",
4901 mdto, mdto->mot_flags, mdto->mot_write_count);
4904 static int mdt_prepare(const struct lu_env *env,
4905 struct lu_device *pdev,
4906 struct lu_device *cdev)
4908 struct mdt_device *mdt = mdt_dev(cdev);
4909 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4910 struct obd_device *obd = cdev->ld_obd;
4917 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4921 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4925 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4929 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4930 /* The LFSCK instance is registered just now, so it must be there when
4931 * register the namespace to such instance. */
4932 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4934 if (mdt->mdt_seq_site.ss_node_id == 0) {
4935 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4936 &mdt->mdt_md_root_fid);
4941 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4943 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4944 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4945 LASSERT(obd->obd_no_conn);
4946 spin_lock(&obd->obd_dev_lock);
4947 obd->obd_no_conn = 0;
4948 spin_unlock(&obd->obd_dev_lock);
4950 if (obd->obd_recovering == 0)
4951 mdt_postrecov(env, mdt);
4956 const struct lu_device_operations mdt_lu_ops = {
4957 .ldo_object_alloc = mdt_object_alloc,
4958 .ldo_process_config = mdt_process_config,
4959 .ldo_prepare = mdt_prepare,
4962 static const struct lu_object_operations mdt_obj_ops = {
4963 .loo_object_init = mdt_object_init,
4964 .loo_object_free = mdt_object_free,
4965 .loo_object_print = mdt_object_print
4968 static int mdt_obd_set_info_async(const struct lu_env *env,
4969 struct obd_export *exp,
4970 __u32 keylen, void *key,
4971 __u32 vallen, void *val,
4972 struct ptlrpc_request_set *set)
4978 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4979 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4987 * Match client and server connection feature flags.
4989 * Compute the compatibility flags for a connection request based on
4990 * features mutually supported by client and server.
4992 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4993 * must not be updated here, otherwise a partially initialized value may
4994 * be exposed. After the connection request is successfully processed,
4995 * the top-level MDT connect request handler atomically updates the export
4996 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4997 * reply. \see mdt_connect().
4999 * \param exp the obd_export associated with this client/target pair
5000 * \param mdt the target device for the connection
5001 * \param data stores data for this connect request
5004 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5005 * \retval -EBADE client and server feature requirements are incompatible
5007 static int mdt_connect_internal(struct obd_export *exp,
5008 struct mdt_device *mdt,
5009 struct obd_connect_data *data)
5011 LASSERT(data != NULL);
5013 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5014 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5016 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5017 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5018 CWARN("%s: client %s does not support ibits lock, either "
5019 "very old or an invalid client: flags "LPX64"\n",
5020 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5021 data->ocd_connect_flags);
5025 if (!mdt->mdt_opts.mo_acl)
5026 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5028 if (!mdt->mdt_opts.mo_user_xattr)
5029 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5031 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5032 data->ocd_brw_size = min(data->ocd_brw_size,
5033 (__u32)MD_MAX_BRW_SIZE);
5034 if (data->ocd_brw_size == 0) {
5035 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5036 " ocd_version: %x ocd_grant: %d "
5037 "ocd_index: %u ocd_brw_size is "
5038 "unexpectedly zero, network data "
5039 "corruption? Refusing connection of this"
5042 exp->exp_client_uuid.uuid,
5043 exp, data->ocd_connect_flags, data->ocd_version,
5044 data->ocd_grant, data->ocd_index);
5049 /* NB: Disregard the rule against updating
5050 * exp_connect_data.ocd_connect_flags in this case, since
5051 * tgt_client_new() needs to know if this is a lightweight
5052 * connection, and it is safe to expose this flag before
5053 * connection processing completes. */
5054 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5055 spin_lock(&exp->exp_lock);
5056 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5057 spin_unlock(&exp->exp_lock);
5060 data->ocd_version = LUSTRE_VERSION_CODE;
5062 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5063 CWARN("%s: MDS requires FID support, but client not\n",
5068 if (OCD_HAS_FLAG(data, PINGLESS)) {
5069 if (ptlrpc_pinger_suppress_pings()) {
5070 spin_lock(&exp->exp_obd->obd_dev_lock);
5071 list_del_init(&exp->exp_obd_chain_timed);
5072 spin_unlock(&exp->exp_obd->obd_dev_lock);
5074 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5078 data->ocd_max_easize = mdt->mdt_max_ea_size;
5080 /* NB: Disregard the rule against updating
5081 * exp_connect_data.ocd_connect_flags in this case, since
5082 * tgt_client_new() needs to know if this is client supports
5083 * multiple modify RPCs, and it is safe to expose this flag before
5084 * connection processing completes. */
5085 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5086 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5087 spin_lock(&exp->exp_lock);
5088 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5089 spin_unlock(&exp->exp_lock);
5095 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5096 struct mdt_thread_info *info,
5097 struct mdt_file_data *mfd)
5099 struct lu_context ses;
5103 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5108 lu_context_enter(&ses);
5110 mdt_ucred(info)->uc_valid = UCRED_OLD;
5111 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5113 lu_context_exit(&ses);
5114 lu_context_fini(&ses);
5120 static int mdt_export_cleanup(struct obd_export *exp)
5122 struct list_head closing_list;
5123 struct mdt_export_data *med = &exp->exp_mdt_data;
5124 struct obd_device *obd = exp->exp_obd;
5125 struct mdt_device *mdt;
5126 struct mdt_thread_info *info;
5128 struct mdt_file_data *mfd, *n;
5132 INIT_LIST_HEAD(&closing_list);
5133 spin_lock(&med->med_open_lock);
5134 while (!list_empty(&med->med_open_head)) {
5135 struct list_head *tmp = med->med_open_head.next;
5136 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5138 /* Remove mfd handle so it can't be found again.
5139 * We are consuming the mfd_list reference here. */
5140 class_handle_unhash(&mfd->mfd_handle);
5141 list_move_tail(&mfd->mfd_list, &closing_list);
5143 spin_unlock(&med->med_open_lock);
5144 mdt = mdt_dev(obd->obd_lu_dev);
5145 LASSERT(mdt != NULL);
5147 rc = lu_env_init(&env, LCT_MD_THREAD);
5151 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5152 LASSERT(info != NULL);
5153 memset(info, 0, sizeof *info);
5154 info->mti_env = &env;
5155 info->mti_mdt = mdt;
5156 info->mti_exp = exp;
5158 if (!list_empty(&closing_list)) {
5159 struct md_attr *ma = &info->mti_attr;
5161 /* Close any open files (which may also cause orphan
5163 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5164 list_del_init(&mfd->mfd_list);
5165 ma->ma_need = ma->ma_valid = 0;
5167 /* This file is being closed due to an eviction, it
5168 * could have been modified and now dirty regarding to
5169 * HSM archive, check this!
5170 * The logic here is to mark a file dirty if there's a
5171 * chance it was dirtied before the client was evicted,
5172 * so that we don't have to wait for a release attempt
5173 * before finding out the file was actually dirty and
5174 * fail the release. Aggressively marking it dirty here
5175 * will cause the policy engine to attempt to
5176 * re-archive it; when rearchiving, we can compare the
5177 * current version to the HSM data_version and make the
5178 * archive request into a noop if it's not actually
5181 if (mfd->mfd_mode & FMODE_WRITE)
5182 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5184 /* Don't unlink orphan on failover umount, LU-184 */
5185 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5186 ma->ma_valid = MA_FLAGS;
5187 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5189 mdt_mfd_close(info, mfd);
5192 info->mti_mdt = NULL;
5193 /* cleanup client slot early */
5194 /* Do not erase record for recoverable client. */
5195 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5196 tgt_client_del(&env, exp);
5202 static inline void mdt_enable_slc(struct mdt_device *mdt)
5204 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5205 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5208 static inline void mdt_disable_slc(struct mdt_device *mdt)
5210 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5211 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5214 static int mdt_obd_disconnect(struct obd_export *exp)
5220 class_export_get(exp);
5222 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5223 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5224 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5226 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5227 mdt_disable_slc(mdt);
5230 rc = server_disconnect_export(exp);
5232 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5234 rc = mdt_export_cleanup(exp);
5235 nodemap_del_member(exp);
5236 class_export_put(exp);
5240 /* mds_connect copy */
5241 static int mdt_obd_connect(const struct lu_env *env,
5242 struct obd_export **exp, struct obd_device *obd,
5243 struct obd_uuid *cluuid,
5244 struct obd_connect_data *data,
5247 struct obd_export *lexp;
5248 struct lustre_handle conn = { 0 };
5249 struct mdt_device *mdt;
5251 lnet_nid_t *client_nid = localdata;
5254 LASSERT(env != NULL);
5255 if (!exp || !obd || !cluuid)
5258 mdt = mdt_dev(obd->obd_lu_dev);
5260 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5261 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5262 atomic_inc(&mdt->mdt_mds_mds_conns);
5263 mdt_enable_slc(mdt);
5267 * first, check whether the stack is ready to handle requests
5268 * XXX: probably not very appropriate method is used now
5269 * at some point we should find a better one
5271 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5272 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5273 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5274 rc = obd_get_info(env, mdt->mdt_child_exp,
5275 sizeof(KEY_OSP_CONNECTED),
5276 KEY_OSP_CONNECTED, NULL, NULL);
5279 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5282 rc = class_connect(&conn, obd, cluuid);
5286 lexp = class_conn2export(&conn);
5287 LASSERT(lexp != NULL);
5289 rc = nodemap_add_member(*client_nid, lexp);
5290 if (rc != 0 && rc != -EEXIST)
5293 rc = mdt_connect_internal(lexp, mdt, data);
5295 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5298 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5299 rc = tgt_client_new(env, lexp);
5301 mdt_export_stats_init(obd, lexp, localdata);
5305 class_disconnect(lexp);
5306 nodemap_del_member(lexp);
5310 /* Because we do not want this export to be evicted by pinger,
5311 * let's not add this export to the timed chain list. */
5312 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5313 spin_lock(&lexp->exp_obd->obd_dev_lock);
5314 list_del_init(&lexp->exp_obd_chain_timed);
5315 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5322 static int mdt_obd_reconnect(const struct lu_env *env,
5323 struct obd_export *exp, struct obd_device *obd,
5324 struct obd_uuid *cluuid,
5325 struct obd_connect_data *data,
5328 lnet_nid_t *client_nid = localdata;
5332 if (exp == NULL || obd == NULL || cluuid == NULL)
5335 rc = nodemap_add_member(*client_nid, exp);
5336 if (rc != 0 && rc != -EEXIST)
5339 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5341 mdt_export_stats_init(obd, exp, localdata);
5343 nodemap_del_member(exp);
5348 /* FIXME: Can we avoid using these two interfaces? */
5349 static int mdt_init_export(struct obd_export *exp)
5351 struct mdt_export_data *med = &exp->exp_mdt_data;
5355 INIT_LIST_HEAD(&med->med_open_head);
5356 spin_lock_init(&med->med_open_lock);
5357 mutex_init(&med->med_idmap_mutex);
5358 med->med_idmap = NULL;
5359 spin_lock(&exp->exp_lock);
5360 exp->exp_connecting = 1;
5361 spin_unlock(&exp->exp_lock);
5363 /* self-export doesn't need client data and ldlm initialization */
5364 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5365 &exp->exp_client_uuid)))
5368 rc = tgt_client_alloc(exp);
5372 rc = ldlm_init_export(exp);
5379 tgt_client_free(exp);
5381 CERROR("%s: Failed to initialize export: rc = %d\n",
5382 exp->exp_obd->obd_name, rc);
5386 static int mdt_destroy_export(struct obd_export *exp)
5390 if (exp_connect_rmtclient(exp))
5391 mdt_cleanup_idmap(&exp->exp_mdt_data);
5393 target_destroy_export(exp);
5394 /* destroy can be called from failed obd_setup, so
5395 * checking uuid is safer than obd_self_export */
5396 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5397 &exp->exp_client_uuid)))
5400 ldlm_destroy_export(exp);
5401 tgt_client_free(exp);
5403 LASSERT(list_empty(&exp->exp_outstanding_replies));
5404 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5409 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5410 struct linkea_data *ldata)
5414 LASSERT(ldata->ld_buf->lb_buf != NULL);
5416 if (!mdt_object_exists(mdt_obj))
5419 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5420 ldata->ld_buf, XATTR_NAME_LINK);
5421 if (rc == -ERANGE) {
5422 /* Buf was too small, figure out what we need. */
5423 lu_buf_free(ldata->ld_buf);
5424 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5425 ldata->ld_buf, XATTR_NAME_LINK);
5428 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5429 if (ldata->ld_buf->lb_buf == NULL)
5431 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5432 ldata->ld_buf, XATTR_NAME_LINK);
5437 return linkea_init(ldata);
5441 * Given an MDT object, try to look up the full path to the object.
5442 * Part of the MDT layer implementation of lfs fid2path.
5444 * \param[in] info Per-thread common data shared by MDT level handlers.
5445 * \param[in] obj Object to do path lookup of
5446 * \param[in,out] fp User-provided struct to store path information
5447 * \param[in] root_fid Root FID of current path should reach
5449 * \retval 0 Lookup successful, path information stored in fp
5450 * \retval -EAGAIN Lookup failed, usually because object is being moved
5451 * \retval negative errno if there was a problem
5453 static int mdt_path_current(struct mdt_thread_info *info,
5454 struct mdt_object *obj,
5455 struct getinfo_fid2path *fp,
5456 struct lu_fid *root_fid)
5458 struct mdt_device *mdt = info->mti_mdt;
5459 struct mdt_object *mdt_obj;
5460 struct link_ea_header *leh;
5461 struct link_ea_entry *lee;
5462 struct lu_name *tmpname = &info->mti_name;
5463 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5464 struct lu_buf *buf = &info->mti_big_buf;
5467 struct linkea_data ldata = { NULL };
5472 /* temp buffer for path element, the buffer will be finally freed
5473 * in mdt_thread_info_fini */
5474 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5475 if (buf->lb_buf == NULL)
5479 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5482 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5484 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5485 struct lu_buf lmv_buf;
5487 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5488 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5489 GOTO(out, rc = -ENOENT);
5491 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5492 if (IS_ERR(mdt_obj))
5493 GOTO(out, rc = PTR_ERR(mdt_obj));
5495 if (!mdt_object_exists(mdt_obj)) {
5496 mdt_object_put(info->mti_env, mdt_obj);
5497 GOTO(out, rc = -ENOENT);
5500 if (mdt_object_remote(mdt_obj)) {
5501 mdt_object_put(info->mti_env, mdt_obj);
5502 GOTO(remote_out, rc = -EREMOTE);
5505 rc = mdt_links_read(info, mdt_obj, &ldata);
5507 mdt_object_put(info->mti_env, mdt_obj);
5512 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5513 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5514 /* If set, use link #linkno for path lookup, otherwise use
5515 link #0. Only do this for the final path element. */
5516 if (first && fp->gf_linkno < leh->leh_reccount) {
5518 for (count = 0; count < fp->gf_linkno; count++) {
5519 lee = (struct link_ea_entry *)
5520 ((char *)lee + reclen);
5521 linkea_entry_unpack(lee, &reclen, tmpname,
5524 if (fp->gf_linkno < leh->leh_reccount - 1)
5525 /* indicate to user there are more links */
5529 lmv_buf.lb_buf = info->mti_xattr_buf;
5530 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5531 /* Check if it is slave stripes */
5532 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5533 &lmv_buf, XATTR_NAME_LMV);
5534 mdt_object_put(info->mti_env, mdt_obj);
5536 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5538 /* For slave stripes, get its master */
5539 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5540 fp->gf_fid = *tmpfid;
5543 } else if (rc < 0 && rc != -ENODATA) {
5549 /* Pack the name in the end of the buffer */
5550 ptr -= tmpname->ln_namelen;
5551 if (ptr - 1 <= fp->gf_u.gf_path)
5552 GOTO(out, rc = -EOVERFLOW);
5553 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5556 /* keep the last resolved fid to the client, so the
5557 * client will build the left path on another MDT for
5559 fp->gf_fid = *tmpfid;
5565 ptr++; /* skip leading / */
5566 memmove(fp->gf_u.gf_path, ptr,
5567 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5574 * Given an MDT object, use mdt_path_current to get the path.
5575 * Essentially a wrapper to retry mdt_path_current a set number of times
5576 * if -EAGAIN is returned (usually because an object is being moved).
5578 * Part of the MDT layer implementation of lfs fid2path.
5580 * \param[in] info Per-thread common data shared by mdt level handlers.
5581 * \param[in] obj Object to do path lookup of
5582 * \param[in,out] fp User-provided struct for arguments and to store path
5585 * \retval 0 Lookup successful, path information stored in fp
5586 * \retval negative errno if there was a problem
5588 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5589 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5591 struct mdt_device *mdt = info->mti_mdt;
5596 if (fp->gf_pathlen < 3)
5599 if (root_fid == NULL)
5600 root_fid = &mdt->mdt_md_root_fid;
5602 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5603 fp->gf_u.gf_path[0] = '\0';
5607 /* Retry multiple times in case file is being moved */
5608 while (tries-- && rc == -EAGAIN)
5609 rc = mdt_path_current(info, obj, fp, root_fid);
5615 * Get the full path of the provided FID, as of changelog record recno.
5617 * This checks sanity and looks up object for user provided FID
5618 * before calling the actual path lookup code.
5620 * Part of the MDT layer implementation of lfs fid2path.
5622 * \param[in] info Per-thread common data shared by mdt level handlers.
5623 * \param[in,out] fp User-provided struct for arguments and to store path
5626 * \retval 0 Lookup successful, path information and recno stored in fp
5627 * \retval -ENOENT, object does not exist
5628 * \retval negative errno if there was a problem
5630 static int mdt_fid2path(struct mdt_thread_info *info,
5631 struct lu_fid *root_fid,
5632 struct getinfo_fid2path *fp)
5634 struct mdt_device *mdt = info->mti_mdt;
5635 struct mdt_object *obj;
5639 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5640 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5642 if (!fid_is_sane(&fp->gf_fid))
5645 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5646 CWARN("%s: "DFID" is invalid, sequence should be "
5647 ">= "LPX64"\n", mdt_obd_name(mdt),
5648 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5652 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5655 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5656 PFID(&fp->gf_fid), rc);
5660 if (mdt_object_remote(obj))
5662 else if (!mdt_object_exists(obj))
5668 mdt_object_put(info->mti_env, obj);
5669 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5670 PFID(&fp->gf_fid), rc);
5674 rc = mdt_path(info, obj, fp, root_fid);
5676 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5677 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5678 fp->gf_recno, fp->gf_linkno);
5680 mdt_object_put(info->mti_env, obj);
5685 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5686 void *val, int vallen)
5688 struct getinfo_fid2path *fpout, *fpin;
5689 struct lu_fid *root_fid = NULL;
5692 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5695 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5696 lustre_swab_fid2path(fpin);
5698 memcpy(fpout, fpin, sizeof(*fpin));
5699 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5702 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5703 sizeof(struct lu_fid)) {
5704 /* client sent its root FID, which is normally fileset FID */
5705 root_fid = fpin->gf_u.gf_root_fid;
5706 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5707 lustre_swab_lu_fid(root_fid);
5709 if (root_fid != NULL && !fid_is_sane(root_fid))
5713 rc = mdt_fid2path(info, root_fid, fpout);
5717 int mdt_get_info(struct tgt_session_info *tsi)
5727 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5729 CDEBUG(D_IOCTL, "No GETINFO key\n");
5730 RETURN(err_serious(-EFAULT));
5732 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5735 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5736 if (vallen == NULL) {
5737 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5738 tgt_name(tsi->tsi_tgt));
5739 RETURN(err_serious(-EFAULT));
5742 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5744 rc = req_capsule_server_pack(tsi->tsi_pill);
5746 RETURN(err_serious(rc));
5748 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5749 if (valout == NULL) {
5750 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5751 tgt_name(tsi->tsi_tgt));
5752 RETURN(err_serious(-EFAULT));
5755 if (KEY_IS(KEY_FID2PATH)) {
5756 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5758 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
5759 mdt_thread_info_fini(info);
5766 /* Pass the ioc down */
5767 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5768 unsigned int cmd, int len, void *data)
5770 struct lu_context ioctl_session;
5771 struct md_device *next = mdt->mdt_child;
5775 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5778 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5779 lu_context_enter(&ioctl_session);
5780 env->le_ses = &ioctl_session;
5782 LASSERT(next->md_ops->mdo_iocontrol);
5783 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5785 lu_context_exit(&ioctl_session);
5786 lu_context_fini(&ioctl_session);
5790 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5792 struct obd_ioctl_data *data = karg;
5795 struct mdt_object *obj;
5796 struct mdt_lock_handle *lh;
5800 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5801 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5804 fid = (struct lu_fid *)data->ioc_inlbuf1;
5806 if (!fid_is_sane(fid))
5809 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5811 lh = &mti->mti_lh[MDT_LH_PARENT];
5812 mdt_lock_reg_init(lh, LCK_CR);
5814 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5816 RETURN(PTR_ERR(obj));
5818 if (mdt_object_remote(obj)) {
5821 * before calling version get the correct MDS should be
5822 * fid, this is error to find remote object here
5824 CERROR("nonlocal object "DFID"\n", PFID(fid));
5825 } else if (!mdt_object_exists(obj)) {
5826 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5829 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5830 *(__u64 *)data->ioc_inlbuf2 = version;
5833 mdt_object_unlock_put(mti, obj, lh, 1);
5837 /* ioctls on obd dev */
5838 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5839 void *karg, void __user *uarg)
5842 struct obd_device *obd = exp->exp_obd;
5843 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5844 struct dt_device *dt = mdt->mdt_bottom;
5848 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5849 rc = lu_env_init(&env, LCT_MD_THREAD);
5855 rc = mdt_device_sync(&env, mdt);
5857 case OBD_IOC_SET_READONLY:
5858 rc = dt->dd_ops->dt_ro(&env, dt);
5860 case OBD_IOC_ABORT_RECOVERY:
5861 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5862 obd->obd_abort_recovery = 1;
5863 target_stop_recovery_thread(obd);
5866 case OBD_IOC_CHANGELOG_REG:
5867 case OBD_IOC_CHANGELOG_DEREG:
5868 case OBD_IOC_CHANGELOG_CLEAR:
5869 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5871 case OBD_IOC_START_LFSCK: {
5872 struct md_device *next = mdt->mdt_child;
5873 struct obd_ioctl_data *data = karg;
5874 struct lfsck_start_param lsp;
5876 if (unlikely(data == NULL)) {
5881 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5882 lsp.lsp_index_valid = 0;
5883 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5886 case OBD_IOC_STOP_LFSCK: {
5887 struct md_device *next = mdt->mdt_child;
5888 struct obd_ioctl_data *data = karg;
5889 struct lfsck_stop stop;
5891 stop.ls_status = LS_STOPPED;
5892 /* Old lfsck utils may pass NULL @stop. */
5893 if (data->ioc_inlbuf1 == NULL)
5897 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5899 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5902 case OBD_IOC_QUERY_LFSCK: {
5903 struct md_device *next = mdt->mdt_child;
5904 struct obd_ioctl_data *data = karg;
5906 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
5910 case OBD_IOC_GET_OBJ_VERSION: {
5911 struct mdt_thread_info *mti;
5912 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5913 memset(mti, 0, sizeof *mti);
5914 mti->mti_env = &env;
5918 rc = mdt_ioc_version_get(mti, karg);
5921 case OBD_IOC_CATLOGLIST: {
5922 struct mdt_thread_info *mti;
5924 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5925 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5926 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5927 &mti->mti_tmp_fid1);
5932 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5933 mdt_obd_name(mdt), cmd, rc);
5940 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5942 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5946 if (!mdt->mdt_skip_lfsck) {
5947 struct lfsck_start_param lsp;
5949 lsp.lsp_start = NULL;
5950 lsp.lsp_index_valid = 0;
5951 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5952 OBD_IOC_START_LFSCK,
5954 if (rc != 0 && rc != -EALREADY)
5955 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5956 mdt_obd_name(mdt), rc);
5959 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5963 static int mdt_obd_postrecov(struct obd_device *obd)
5968 rc = lu_env_init(&env, LCT_MD_THREAD);
5971 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5976 static struct obd_ops mdt_obd_device_ops = {
5977 .o_owner = THIS_MODULE,
5978 .o_set_info_async = mdt_obd_set_info_async,
5979 .o_connect = mdt_obd_connect,
5980 .o_reconnect = mdt_obd_reconnect,
5981 .o_disconnect = mdt_obd_disconnect,
5982 .o_init_export = mdt_init_export,
5983 .o_destroy_export = mdt_destroy_export,
5984 .o_iocontrol = mdt_iocontrol,
5985 .o_postrecov = mdt_obd_postrecov,
5988 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5989 struct lu_device *d)
5991 struct mdt_device *m = mdt_dev(d);
5998 static struct lu_device *mdt_device_free(const struct lu_env *env,
5999 struct lu_device *d)
6001 struct mdt_device *m = mdt_dev(d);
6004 lu_device_fini(&m->mdt_lu_dev);
6010 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6011 struct lu_device_type *t,
6012 struct lustre_cfg *cfg)
6014 struct lu_device *l;
6015 struct mdt_device *m;
6022 rc = mdt_init0(env, m, t, cfg);
6024 mdt_device_free(env, l);
6029 l = ERR_PTR(-ENOMEM);
6033 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6034 LU_KEY_INIT(mdt, struct mdt_thread_info);
6036 static void mdt_key_fini(const struct lu_context *ctx,
6037 struct lu_context_key *key, void* data)
6039 struct mdt_thread_info *info = data;
6041 if (info->mti_big_lmm) {
6042 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6043 info->mti_big_lmm = NULL;
6044 info->mti_big_lmmsize = 0;
6049 /* context key: mdt_thread_key */
6050 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6052 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6054 return lu_ucred(info->mti_env);
6057 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6059 return lu_ucred_check(info->mti_env);
6063 * Enable/disable COS (Commit On Sharing).
6065 * Set/Clear the COS flag in mdt options.
6067 * \param mdt mdt device
6068 * \param val 0 disables COS, other values enable COS
6070 void mdt_enable_cos(struct mdt_device *mdt, int val)
6075 mdt->mdt_opts.mo_cos = !!val;
6076 rc = lu_env_init(&env, LCT_LOCAL);
6077 if (unlikely(rc != 0)) {
6078 CWARN("%s: lu_env initialization failed, cannot "
6079 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6082 mdt_device_sync(&env, mdt);
6087 * Check COS (Commit On Sharing) status.
6089 * Return COS flag status.
6091 * \param mdt mdt device
6093 int mdt_cos_is_enabled(struct mdt_device *mdt)
6095 return mdt->mdt_opts.mo_cos != 0;
6098 static struct lu_device_type_operations mdt_device_type_ops = {
6099 .ldto_device_alloc = mdt_device_alloc,
6100 .ldto_device_free = mdt_device_free,
6101 .ldto_device_fini = mdt_device_fini
6104 static struct lu_device_type mdt_device_type = {
6105 .ldt_tags = LU_DEVICE_MD,
6106 .ldt_name = LUSTRE_MDT_NAME,
6107 .ldt_ops = &mdt_device_type_ops,
6108 .ldt_ctx_tags = LCT_MD_THREAD
6111 static int __init mdt_init(void)
6115 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6116 FID_NOBRACE_LEN + 1);
6117 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6119 rc = lu_kmem_init(mdt_caches);
6123 rc = mds_mod_init();
6127 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6128 LUSTRE_MDT_NAME, &mdt_device_type);
6133 lu_kmem_fini(mdt_caches);
6140 static void __exit mdt_exit(void)
6142 class_unregister_type(LUSTRE_MDT_NAME);
6144 lu_kmem_fini(mdt_caches);
6147 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6148 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6149 MODULE_VERSION(LUSTRE_VERSION_STRING);
6150 MODULE_LICENSE("GPL");
6152 module_init(mdt_init);
6153 module_exit(mdt_exit);