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 "mdt_internal.h"
71 static unsigned int max_mod_rpcs_per_client = 8;
72 CFS_MODULE_PARM(max_mod_rpcs_per_client, "i", uint, 0644,
73 "maximum number of modify RPCs in flight allowed per client");
75 mdl_mode_t mdt_mdl_lock_modes[] = {
76 [LCK_MINMODE] = MDL_MINMODE,
83 [LCK_GROUP] = MDL_GROUP
86 enum ldlm_mode mdt_dlm_lock_modes[] = {
87 [MDL_MINMODE] = LCK_MINMODE,
94 [MDL_GROUP] = LCK_GROUP
97 static struct mdt_device *mdt_dev(struct lu_device *d);
98 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
100 static const struct lu_object_operations mdt_obj_ops;
102 /* Slab for MDT object allocation */
103 static struct kmem_cache *mdt_object_kmem;
105 /* For HSM restore handles */
106 struct kmem_cache *mdt_hsm_cdt_kmem;
108 /* For HSM request handles */
109 struct kmem_cache *mdt_hsm_car_kmem;
111 static struct lu_kmem_descr mdt_caches[] = {
113 .ckd_cache = &mdt_object_kmem,
114 .ckd_name = "mdt_obj",
115 .ckd_size = sizeof(struct mdt_object)
118 .ckd_cache = &mdt_hsm_cdt_kmem,
119 .ckd_name = "mdt_cdt_restore_handle",
120 .ckd_size = sizeof(struct cdt_restore_handle)
123 .ckd_cache = &mdt_hsm_car_kmem,
124 .ckd_name = "mdt_cdt_agent_req",
125 .ckd_size = sizeof(struct cdt_agent_req)
132 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
136 return rep->lock_policy_res1 & op_flag;
139 void mdt_clear_disposition(struct mdt_thread_info *info,
140 struct ldlm_reply *rep, __u64 op_flag)
143 info->mti_opdata &= ~op_flag;
144 tgt_opdata_clear(info->mti_env, op_flag);
147 rep->lock_policy_res1 &= ~op_flag;
150 void mdt_set_disposition(struct mdt_thread_info *info,
151 struct ldlm_reply *rep, __u64 op_flag)
154 info->mti_opdata |= op_flag;
155 tgt_opdata_set(info->mti_env, op_flag);
158 rep->lock_policy_res1 |= op_flag;
161 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
163 lh->mlh_pdo_hash = 0;
164 lh->mlh_reg_mode = lm;
165 lh->mlh_rreg_mode = lm;
166 lh->mlh_type = MDT_REG_LOCK;
169 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
170 const struct lu_name *lname)
172 lh->mlh_reg_mode = lock_mode;
173 lh->mlh_pdo_mode = LCK_MINMODE;
174 lh->mlh_rreg_mode = lock_mode;
175 lh->mlh_type = MDT_PDO_LOCK;
177 if (lu_name_is_valid(lname)) {
178 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
180 /* XXX Workaround for LU-2856
182 * Zero is a valid return value of full_name_hash, but
183 * several users of mlh_pdo_hash assume a non-zero
184 * hash value. We therefore map zero onto an
185 * arbitrary, but consistent value (1) to avoid
186 * problems further down the road. */
187 if (unlikely(lh->mlh_pdo_hash == 0))
188 lh->mlh_pdo_hash = 1;
190 lh->mlh_pdo_hash = 0;
194 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
195 struct mdt_lock_handle *lh)
201 * Any dir access needs couple of locks:
203 * 1) on part of dir we gonna take lookup/modify;
205 * 2) on whole dir to protect it from concurrent splitting and/or to
206 * flush client's cache for readdir().
208 * so, for a given mode and object this routine decides what lock mode
209 * to use for lock #2:
211 * 1) if caller's gonna lookup in dir then we need to protect dir from
212 * being splitted only - LCK_CR
214 * 2) if caller's gonna modify dir then we need to protect dir from
215 * being splitted and to flush cache - LCK_CW
217 * 3) if caller's gonna modify dir and that dir seems ready for
218 * splitting then we need to protect it from any type of access
219 * (lookup/modify/split) - LCK_EX --bzzz
222 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
223 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
226 * Ask underlaying level its opinion about preferable PDO lock mode
227 * having access type passed as regular lock mode:
229 * - MDL_MINMODE means that lower layer does not want to specify lock
232 * - MDL_NL means that no PDO lock should be taken. This is used in some
233 * cases. Say, for non-splittable directories no need to use PDO locks
236 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
237 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
239 if (mode != MDL_MINMODE) {
240 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
243 * Lower layer does not want to specify locking mode. We do it
244 * our selves. No special protection is needed, just flush
245 * client's cache on modification and allow concurrent
248 switch (lh->mlh_reg_mode) {
250 lh->mlh_pdo_mode = LCK_EX;
253 lh->mlh_pdo_mode = LCK_CR;
256 lh->mlh_pdo_mode = LCK_CW;
259 CERROR("Not expected lock type (0x%x)\n",
260 (int)lh->mlh_reg_mode);
265 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
269 static int mdt_getstatus(struct tgt_session_info *tsi)
271 struct mdt_thread_info *info = tsi2mdt_info(tsi);
272 struct mdt_device *mdt = info->mti_mdt;
273 struct mdt_body *repbody;
278 rc = mdt_check_ucred(info);
280 GOTO(out, rc = err_serious(rc));
282 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
283 GOTO(out, rc = err_serious(-ENOMEM));
285 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
286 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
287 repbody->mbo_valid |= OBD_MD_FLID;
291 mdt_thread_info_fini(info);
295 static int mdt_statfs(struct tgt_session_info *tsi)
297 struct ptlrpc_request *req = tgt_ses_req(tsi);
298 struct mdt_thread_info *info = tsi2mdt_info(tsi);
299 struct md_device *next = info->mti_mdt->mdt_child;
300 struct ptlrpc_service_part *svcpt;
301 struct obd_statfs *osfs;
306 svcpt = req->rq_rqbd->rqbd_svcpt;
308 /* This will trigger a watchdog timeout */
309 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
310 (MDT_SERVICE_WATCHDOG_FACTOR *
311 at_get(&svcpt->scp_at_estimate)) + 1);
313 rc = mdt_check_ucred(info);
315 GOTO(out, rc = err_serious(rc));
317 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
318 GOTO(out, rc = err_serious(-ENOMEM));
320 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
322 GOTO(out, rc = -EPROTO);
324 /** statfs information are cached in the mdt_device */
325 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
326 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
327 /** statfs data is too old, get up-to-date one */
328 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
331 spin_lock(&info->mti_mdt->mdt_osfs_lock);
332 info->mti_mdt->mdt_osfs = *osfs;
333 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
334 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
336 /** use cached statfs data */
337 spin_lock(&info->mti_mdt->mdt_osfs_lock);
338 *osfs = info->mti_mdt->mdt_osfs;
339 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
343 mdt_counter_incr(req, LPROC_MDT_STATFS);
345 mdt_thread_info_fini(info);
349 #ifdef CONFIG_FS_POSIX_ACL
351 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
353 * \param info thread info object
354 * \param repbody reply to pack ACLs into
355 * \param o mdt object of file to examine
356 * \param nodemap nodemap of client to reply to
358 * \retval -errno error getting or parsing ACL from disk
360 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
361 struct mdt_object *o, struct lu_nodemap *nodemap)
363 const struct lu_env *env = info->mti_env;
364 struct md_object *next = mdt_object_child(o);
365 struct lu_buf *buf = &info->mti_buf;
368 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
369 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
371 if (buf->lb_len == 0)
374 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
376 if (rc == -ENODATA) {
377 repbody->mbo_aclsize = 0;
378 repbody->mbo_valid |= OBD_MD_FLACL;
380 } else if (rc == -EOPNOTSUPP) {
383 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
384 mdt_obd_name(info->mti_mdt),
385 PFID(mdt_object_fid(o)), rc);
388 rc = nodemap_map_acl(nodemap, buf->lb_buf,
389 rc, NODEMAP_FS_TO_CLIENT);
390 /* if all ACLs mapped out, rc is still >= 0 */
392 CERROR("%s: nodemap_map_acl unable to parse "DFID
393 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
394 PFID(mdt_object_fid(o)), rc);
396 repbody->mbo_aclsize = rc;
397 repbody->mbo_valid |= OBD_MD_FLACL;
405 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
406 const struct lu_attr *attr, const struct lu_fid *fid)
408 struct md_attr *ma = &info->mti_attr;
409 struct obd_export *exp = info->mti_exp;
410 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
412 LASSERT(ma->ma_valid & MA_INODE);
414 if (attr->la_valid & LA_ATIME) {
415 b->mbo_atime = attr->la_atime;
416 b->mbo_valid |= OBD_MD_FLATIME;
418 if (attr->la_valid & LA_MTIME) {
419 b->mbo_mtime = attr->la_mtime;
420 b->mbo_valid |= OBD_MD_FLMTIME;
422 if (attr->la_valid & LA_CTIME) {
423 b->mbo_ctime = attr->la_ctime;
424 b->mbo_valid |= OBD_MD_FLCTIME;
426 if (attr->la_valid & LA_FLAGS) {
427 b->mbo_flags = attr->la_flags;
428 b->mbo_valid |= OBD_MD_FLFLAGS;
430 if (attr->la_valid & LA_NLINK) {
431 b->mbo_nlink = attr->la_nlink;
432 b->mbo_valid |= OBD_MD_FLNLINK;
434 if (attr->la_valid & LA_UID) {
435 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
436 NODEMAP_FS_TO_CLIENT,
438 b->mbo_valid |= OBD_MD_FLUID;
440 if (attr->la_valid & LA_GID) {
441 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
442 NODEMAP_FS_TO_CLIENT,
444 b->mbo_valid |= OBD_MD_FLGID;
446 b->mbo_mode = attr->la_mode;
447 if (attr->la_valid & LA_MODE)
448 b->mbo_valid |= OBD_MD_FLMODE;
449 if (attr->la_valid & LA_TYPE)
450 b->mbo_valid |= OBD_MD_FLTYPE;
454 b->mbo_valid |= OBD_MD_FLID;
455 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid="LPX64"\n",
456 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
460 mdt_body_reverse_idmap(info, b);
462 if (!(attr->la_valid & LA_TYPE))
465 b->mbo_rdev = attr->la_rdev;
466 b->mbo_size = attr->la_size;
467 b->mbo_blocks = attr->la_blocks;
469 if (!S_ISREG(attr->la_mode)) {
470 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
471 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
472 /* means no objects are allocated on osts. */
473 LASSERT(!(ma->ma_valid & MA_LOV));
474 /* just ignore blocks occupied by extend attributes on MDS */
476 /* if no object is allocated on osts, the size on mds is valid.
478 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
479 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
480 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
481 /* A released file stores its size on MDS. */
482 /* But return 1 block for released file, unless tools like tar
483 * will consider it fully sparse. (LU-3864)
485 if (unlikely(b->mbo_size == 0))
489 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
492 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
493 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
494 PFID(fid), (unsigned long long)b->mbo_size);
497 static inline int mdt_body_has_lov(const struct lu_attr *la,
498 const struct mdt_body *body)
500 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
501 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
504 void mdt_client_compatibility(struct mdt_thread_info *info)
506 struct mdt_body *body;
507 struct ptlrpc_request *req = mdt_info_req(info);
508 struct obd_export *exp = req->rq_export;
509 struct md_attr *ma = &info->mti_attr;
510 struct lu_attr *la = &ma->ma_attr;
513 if (exp_connect_layout(exp))
514 /* the client can deal with 16-bit lmm_stripe_count */
517 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
519 if (!mdt_body_has_lov(la, body))
522 /* now we have a reply with a lov for a client not compatible with the
523 * layout lock so we have to clean the layout generation number */
524 if (S_ISREG(la->la_mode))
525 ma->ma_lmm->lmm_layout_gen = 0;
529 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
530 struct mdt_object *o)
532 const struct lu_env *env = info->mti_env;
535 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
544 /* Is it a directory? Let's check for the LMV as well */
545 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
546 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
550 rc2 = mo_xattr_get(env, mdt_object_child(o),
552 XATTR_NAME_DEFAULT_LMV);
554 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
562 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
565 const struct lu_env *env = info->mti_env;
569 LASSERT(info->mti_big_lmm_used == 0);
570 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
574 /* big_lmm may need to be grown */
575 if (info->mti_big_lmmsize < rc) {
576 int size = size_roundup_power2(rc);
578 if (info->mti_big_lmmsize > 0) {
579 /* free old buffer */
580 LASSERT(info->mti_big_lmm);
581 OBD_FREE_LARGE(info->mti_big_lmm,
582 info->mti_big_lmmsize);
583 info->mti_big_lmm = NULL;
584 info->mti_big_lmmsize = 0;
587 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
588 if (info->mti_big_lmm == NULL)
590 info->mti_big_lmmsize = size;
592 LASSERT(info->mti_big_lmmsize >= rc);
594 info->mti_buf.lb_buf = info->mti_big_lmm;
595 info->mti_buf.lb_len = info->mti_big_lmmsize;
596 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
601 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
602 struct md_attr *ma, const char *name)
604 struct md_object *next = mdt_object_child(o);
605 struct lu_buf *buf = &info->mti_buf;
608 if (strcmp(name, XATTR_NAME_LOV) == 0) {
609 buf->lb_buf = ma->ma_lmm;
610 buf->lb_len = ma->ma_lmm_size;
611 LASSERT(!(ma->ma_valid & MA_LOV));
612 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
613 buf->lb_buf = ma->ma_lmv;
614 buf->lb_len = ma->ma_lmv_size;
615 LASSERT(!(ma->ma_valid & MA_LMV));
616 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
617 buf->lb_buf = ma->ma_lmv;
618 buf->lb_len = ma->ma_lmv_size;
619 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
624 rc = mo_xattr_get(info->mti_env, next, buf, name);
628 if (strcmp(name, XATTR_NAME_LOV) == 0) {
629 if (info->mti_big_lmm_used)
630 ma->ma_lmm = info->mti_big_lmm;
632 /* NOT return LOV EA with hole to old client. */
633 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
634 LOV_PATTERN_F_HOLE) &&
635 !(exp_connect_flags(info->mti_exp) &
636 OBD_CONNECT_LFSCK)) {
639 ma->ma_lmm_size = rc;
640 ma->ma_valid |= MA_LOV;
642 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
643 if (info->mti_big_lmm_used)
644 ma->ma_lmv = info->mti_big_lmm;
646 ma->ma_lmv_size = rc;
647 ma->ma_valid |= MA_LMV;
648 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
649 ma->ma_lmv_size = rc;
650 ma->ma_valid |= MA_LMV_DEF;
653 /* Update mdt_max_mdsize so all clients will be aware that */
654 if (info->mti_mdt->mdt_max_mdsize < rc)
655 info->mti_mdt->mdt_max_mdsize = rc;
658 } else if (rc == -ENODATA) {
661 } else if (rc == -ERANGE) {
662 /* Default LMV has fixed size, so it must be able to fit
663 * in the original buffer */
664 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
666 rc = mdt_big_xattr_get(info, o, name);
668 info->mti_big_lmm_used = 1;
676 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
677 struct mdt_object *o, struct lu_fid *pfid)
679 struct lu_buf *buf = &info->mti_buf;
680 struct link_ea_header *leh;
681 struct link_ea_entry *lee;
685 buf->lb_buf = info->mti_big_lmm;
686 buf->lb_len = info->mti_big_lmmsize;
687 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
688 buf, XATTR_NAME_LINK);
689 /* ignore errors, MA_PFID won't be set and it is
690 * up to the caller to treat this as an error */
691 if (rc == -ERANGE || buf->lb_len == 0) {
692 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
693 buf->lb_buf = info->mti_big_lmm;
694 buf->lb_len = info->mti_big_lmmsize;
699 if (rc < sizeof(*leh)) {
700 CERROR("short LinkEA on "DFID": rc = %d\n",
701 PFID(mdt_object_fid(o)), rc);
705 leh = (struct link_ea_header *) buf->lb_buf;
706 lee = (struct link_ea_entry *)(leh + 1);
707 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
708 leh->leh_magic = LINK_EA_MAGIC;
709 leh->leh_reccount = __swab32(leh->leh_reccount);
710 leh->leh_len = __swab64(leh->leh_len);
712 if (leh->leh_magic != LINK_EA_MAGIC)
714 if (leh->leh_reccount == 0)
717 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
718 fid_be_to_cpu(pfid, pfid);
723 int mdt_attr_get_complex(struct mdt_thread_info *info,
724 struct mdt_object *o, struct md_attr *ma)
726 const struct lu_env *env = info->mti_env;
727 struct md_object *next = mdt_object_child(o);
728 struct lu_buf *buf = &info->mti_buf;
729 int need = ma->ma_need;
736 if (mdt_object_exists(o) == 0)
737 GOTO(out, rc = -ENOENT);
738 mode = lu_object_attr(&next->mo_lu);
740 if (need & MA_INODE) {
741 ma->ma_need = MA_INODE;
742 rc = mo_attr_get(env, next, ma);
745 ma->ma_valid |= MA_INODE;
748 if (need & MA_PFID) {
749 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
751 ma->ma_valid |= MA_PFID;
752 /* ignore this error, parent fid is not mandatory */
756 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
757 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
762 if (need & MA_LMV && S_ISDIR(mode)) {
763 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
768 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
769 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
774 if (need & MA_HSM && S_ISREG(mode)) {
775 buf->lb_buf = info->mti_xattr_buf;
776 buf->lb_len = sizeof(info->mti_xattr_buf);
777 CLASSERT(sizeof(struct hsm_attrs) <=
778 sizeof(info->mti_xattr_buf));
779 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
780 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
782 ma->ma_valid |= MA_HSM;
783 else if (rc2 < 0 && rc2 != -ENODATA)
787 #ifdef CONFIG_FS_POSIX_ACL
788 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
789 buf->lb_buf = ma->ma_acl;
790 buf->lb_len = ma->ma_acl_size;
791 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
793 ma->ma_acl_size = rc2;
794 ma->ma_valid |= MA_ACL_DEF;
795 } else if (rc2 == -ENODATA) {
804 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
805 rc, ma->ma_valid, ma->ma_lmm);
809 static int mdt_getattr_internal(struct mdt_thread_info *info,
810 struct mdt_object *o, int ma_need)
812 struct md_object *next = mdt_object_child(o);
813 const struct mdt_body *reqbody = info->mti_body;
814 struct ptlrpc_request *req = mdt_info_req(info);
815 struct md_attr *ma = &info->mti_attr;
816 struct lu_attr *la = &ma->ma_attr;
817 struct req_capsule *pill = info->mti_pill;
818 const struct lu_env *env = info->mti_env;
819 struct mdt_body *repbody;
820 struct lu_buf *buffer = &info->mti_buf;
821 struct obd_export *exp = info->mti_exp;
822 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
827 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
828 RETURN(err_serious(-ENOMEM));
830 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
834 if (mdt_object_remote(o)) {
835 /* This object is located on remote node.*/
836 /* Return -ENOTSUPP for old client */
837 if (!mdt_is_dne_client(req->rq_export))
838 GOTO(out, rc = -ENOTSUPP);
840 repbody->mbo_fid1 = *mdt_object_fid(o);
841 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
845 if (reqbody->mbo_eadatasize > 0) {
846 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
847 if (buffer->lb_buf == NULL)
848 GOTO(out, rc = -EPROTO);
849 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
852 buffer->lb_buf = NULL;
854 ma_need &= ~(MA_LOV | MA_LMV);
855 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
856 mdt_obd_name(info->mti_mdt),
857 req->rq_export->exp_client_uuid.uuid);
860 /* If it is dir object and client require MEA, then we got MEA */
861 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
862 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
863 /* Assumption: MDT_MD size is enough for lmv size. */
864 ma->ma_lmv = buffer->lb_buf;
865 ma->ma_lmv_size = buffer->lb_len;
866 ma->ma_need = MA_INODE;
867 if (ma->ma_lmv_size > 0) {
868 if (reqbody->mbo_valid & OBD_MD_MEA)
869 ma->ma_need |= MA_LMV;
870 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
871 ma->ma_need |= MA_LMV_DEF;
874 ma->ma_lmm = buffer->lb_buf;
875 ma->ma_lmm_size = buffer->lb_len;
876 ma->ma_need = MA_INODE | MA_HSM;
877 if (ma->ma_lmm_size > 0)
878 ma->ma_need |= MA_LOV;
881 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
882 reqbody->mbo_valid & OBD_MD_FLDIREA &&
883 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
884 /* get default stripe info for this dir. */
885 ma->ma_need |= MA_LOV_DEF;
887 ma->ma_need |= ma_need;
889 rc = mdt_attr_get_complex(info, o, ma);
891 CERROR("%s: getattr error for "DFID": rc = %d\n",
892 mdt_obd_name(info->mti_mdt),
893 PFID(mdt_object_fid(o)), rc);
897 /* if file is released, check if a restore is running */
898 if (ma->ma_valid & MA_HSM) {
899 repbody->mbo_valid |= OBD_MD_TSTATE;
900 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
901 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
902 repbody->mbo_t_state = MS_RESTORE;
905 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
907 /* the Lustre protocol supposes to return default striping
908 * on the user-visible root if explicitly requested */
909 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
910 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
911 struct lu_fid rootfid;
912 struct mdt_object *root;
913 struct mdt_device *mdt = info->mti_mdt;
915 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
918 root = mdt_object_find(env, mdt, &rootfid);
920 RETURN(PTR_ERR(root));
921 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
922 mdt_object_put(info->mti_env, root);
924 CERROR("%s: getattr error for "DFID": rc = %d\n",
925 mdt_obd_name(info->mti_mdt),
926 PFID(mdt_object_fid(o)), rc);
931 if (likely(ma->ma_valid & MA_INODE))
932 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
936 if (mdt_body_has_lov(la, reqbody)) {
937 if (ma->ma_valid & MA_LOV) {
938 LASSERT(ma->ma_lmm_size);
939 repbody->mbo_eadatasize = ma->ma_lmm_size;
940 if (S_ISDIR(la->la_mode))
941 repbody->mbo_valid |= OBD_MD_FLDIREA;
943 repbody->mbo_valid |= OBD_MD_FLEASIZE;
944 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
946 if (ma->ma_valid & MA_LMV) {
947 /* Return -ENOTSUPP for old client */
948 if (!mdt_is_striped_client(req->rq_export))
951 LASSERT(S_ISDIR(la->la_mode));
952 mdt_dump_lmv(D_INFO, ma->ma_lmv);
953 repbody->mbo_eadatasize = ma->ma_lmv_size;
954 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
956 if (ma->ma_valid & MA_LMV_DEF) {
957 /* Return -ENOTSUPP for old client */
958 if (!mdt_is_striped_client(req->rq_export))
960 LASSERT(S_ISDIR(la->la_mode));
961 repbody->mbo_eadatasize = ma->ma_lmv_size;
962 repbody->mbo_valid |= (OBD_MD_FLDIREA |
965 } else if (S_ISLNK(la->la_mode) &&
966 reqbody->mbo_valid & OBD_MD_LINKNAME) {
967 buffer->lb_buf = ma->ma_lmm;
968 /* eadatasize from client includes NULL-terminator, so
969 * there is no need to read it */
970 buffer->lb_len = reqbody->mbo_eadatasize - 1;
971 rc = mo_readlink(env, next, buffer);
972 if (unlikely(rc <= 0)) {
973 CERROR("%s: readlink failed for "DFID": rc = %d\n",
974 mdt_obd_name(info->mti_mdt),
975 PFID(mdt_object_fid(o)), rc);
978 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
980 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
982 repbody->mbo_valid |= OBD_MD_LINKNAME;
983 /* we need to report back size with NULL-terminator
984 * because client expects that */
985 repbody->mbo_eadatasize = rc + 1;
986 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
987 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
988 "on "DFID ", expected %d\n",
989 mdt_obd_name(info->mti_mdt),
990 rc, PFID(mdt_object_fid(o)),
991 reqbody->mbo_eadatasize - 1);
993 ((char *)ma->ma_lmm)[rc] = 0;
995 /* If the total CDEBUG() size is larger than a page, it
996 * will print a warning to the console, avoid this by
997 * printing just the last part of the symlink. */
998 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
999 print_limit < rc ? "..." : "", print_limit,
1000 (char *)ma->ma_lmm + rc - print_limit, rc);
1005 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1006 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1007 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1008 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1009 repbody->mbo_max_mdsize);
1012 if (exp_connect_rmtclient(info->mti_exp) &&
1013 reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
1014 void *buf = req_capsule_server_get(pill, &RMF_ACL);
1016 /* mdt_getattr_lock only */
1017 rc = mdt_pack_remote_perm(info, o, buf);
1019 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
1020 repbody->mbo_aclsize = 0;
1023 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
1024 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
1027 #ifdef CONFIG_FS_POSIX_ACL
1028 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1029 (reqbody->mbo_valid & OBD_MD_FLACL))
1030 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1035 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1040 static int mdt_getattr(struct tgt_session_info *tsi)
1042 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1043 struct mdt_object *obj = info->mti_object;
1044 struct req_capsule *pill = info->mti_pill;
1045 struct mdt_body *reqbody;
1046 struct mdt_body *repbody;
1050 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1052 LASSERT(obj != NULL);
1053 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1055 /* Unlike intent case where we need to pre-fill out buffers early on
1056 * in intent policy for ldlm reasons, here we can have a much better
1057 * guess at EA size by just reading it from disk.
1058 * Exceptions are readdir and (missing) directory striping */
1060 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1061 /* No easy way to know how long is the symlink, but it cannot
1062 * be more than PATH_MAX, so we allocate +1 */
1065 /* A special case for fs ROOT: getattr there might fetch
1066 * default EA for entire fs, not just for this dir!
1068 } else if (lu_fid_eq(mdt_object_fid(obj),
1069 &info->mti_mdt->mdt_md_root_fid) &&
1070 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1071 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1073 /* Should the default strping be bigger, mdt_fix_reply
1074 * will reallocate */
1075 rc = DEF_REP_MD_SIZE;
1077 /* Read the actual EA size from disk */
1078 rc = mdt_attr_get_eabuf_size(info, obj);
1082 GOTO(out, rc = err_serious(rc));
1084 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1086 rc = req_capsule_server_pack(pill);
1087 if (unlikely(rc != 0))
1088 GOTO(out, rc = err_serious(rc));
1090 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1091 LASSERT(repbody != NULL);
1092 repbody->mbo_eadatasize = 0;
1093 repbody->mbo_aclsize = 0;
1095 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1096 rc = mdt_init_ucred(info, reqbody);
1098 rc = mdt_check_ucred(info);
1100 GOTO(out_shrink, rc);
1102 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1104 rc = mdt_getattr_internal(info, obj, 0);
1105 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1106 mdt_exit_ucred(info);
1109 mdt_client_compatibility(info);
1110 rc2 = mdt_fix_reply(info);
1114 mdt_thread_info_fini(info);
1119 * Exchange MOF_LOV_CREATED flags between two objects after a
1120 * layout swap. No assumption is made on whether o1 or o2 have
1121 * created objects or not.
1123 * \param[in,out] o1 First swap layout object
1124 * \param[in,out] o2 Second swap layout object
1126 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1130 mutex_lock(&o1->mot_lov_mutex);
1131 mutex_lock(&o2->mot_lov_mutex);
1133 o1_flags = o1->mot_flags;
1134 o1->mot_flags = (o1->mot_flags & ~MOF_LOV_CREATED) |
1135 (o2->mot_flags & MOF_LOV_CREATED);
1137 o2->mot_flags = (o2->mot_flags & ~MOF_LOV_CREATED) |
1138 (o1_flags & MOF_LOV_CREATED);
1140 mutex_unlock(&o2->mot_lov_mutex);
1141 mutex_unlock(&o1->mot_lov_mutex);
1144 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1146 struct mdt_thread_info *info;
1147 struct ptlrpc_request *req = tgt_ses_req(tsi);
1148 struct obd_export *exp = req->rq_export;
1149 struct mdt_object *o1, *o2, *o;
1150 struct mdt_lock_handle *lh1, *lh2;
1151 struct mdc_swap_layouts *msl;
1155 /* client does not support layout lock, so layout swaping
1157 * FIXME: there is a problem for old clients which don't support
1158 * layout lock yet. If those clients have already opened the file
1159 * they won't be notified at all so that old layout may still be
1160 * used to do IO. This can be fixed after file release is landed by
1161 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1162 if (!exp_connect_layout(exp))
1163 RETURN(-EOPNOTSUPP);
1165 info = tsi2mdt_info(tsi);
1167 if (info->mti_dlm_req != NULL)
1168 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1170 o1 = info->mti_object;
1171 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1172 &info->mti_body->mbo_fid2);
1174 GOTO(out, rc = PTR_ERR(o));
1176 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1177 GOTO(put, rc = -ENOENT);
1179 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1180 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1186 /* permission check. Make sure the calling process having permission
1187 * to write both files. */
1188 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1193 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1198 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1200 GOTO(put, rc = -EPROTO);
1202 lh1 = &info->mti_lh[MDT_LH_NEW];
1203 mdt_lock_reg_init(lh1, LCK_EX);
1204 lh2 = &info->mti_lh[MDT_LH_OLD];
1205 mdt_lock_reg_init(lh2, LCK_EX);
1207 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1208 MDS_INODELOCK_XATTR);
1212 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1213 MDS_INODELOCK_XATTR);
1217 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1218 mdt_object_child(o2), msl->msl_flags);
1222 mdt_swap_lov_flag(o1, o2);
1225 mdt_object_unlock(info, o2, lh2, rc);
1227 mdt_object_unlock(info, o1, lh1, rc);
1229 mdt_object_put(info->mti_env, o);
1231 mdt_thread_info_fini(info);
1235 static int mdt_raw_lookup(struct mdt_thread_info *info,
1236 struct mdt_object *parent,
1237 const struct lu_name *lname,
1238 struct ldlm_reply *ldlm_rep)
1240 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1244 LASSERT(!info->mti_cross_ref);
1246 /* Only got the fid of this obj by name */
1247 fid_zero(child_fid);
1248 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1249 lname, child_fid, &info->mti_spec);
1251 struct mdt_body *repbody;
1253 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1254 repbody->mbo_fid1 = *child_fid;
1255 repbody->mbo_valid = OBD_MD_FLID;
1256 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1257 } else if (rc == -ENOENT) {
1258 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1265 * UPDATE lock should be taken against parent, and be released before exit;
1266 * child_bits lock should be taken against child, and be returned back:
1267 * (1)normal request should release the child lock;
1268 * (2)intent request will grant the lock to client.
1270 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1271 struct mdt_lock_handle *lhc,
1273 struct ldlm_reply *ldlm_rep)
1275 struct ptlrpc_request *req = mdt_info_req(info);
1276 struct mdt_body *reqbody = NULL;
1277 struct mdt_object *parent = info->mti_object;
1278 struct mdt_object *child;
1279 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1280 struct lu_name *lname = NULL;
1281 struct mdt_lock_handle *lhp = NULL;
1282 struct ldlm_lock *lock;
1289 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1290 LASSERT(ergo(is_resent,
1291 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1296 if (info->mti_cross_ref) {
1297 /* Only getattr on the child. Parent is on another node. */
1298 mdt_set_disposition(info, ldlm_rep,
1299 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1301 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1303 PFID(mdt_object_fid(child)), ldlm_rep);
1305 rc = mdt_check_resent_lock(info, child, lhc);
1308 } else if (rc > 0) {
1309 mdt_lock_handle_init(lhc);
1310 mdt_lock_reg_init(lhc, LCK_PR);
1313 * Object's name is on another MDS, no lookup or layout
1314 * lock is needed here but update lock is.
1316 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1317 MDS_INODELOCK_LAYOUT);
1318 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1320 rc = mdt_object_lock(info, child, lhc, child_bits);
1325 /* Finally, we can get attr for child. */
1326 if (!mdt_object_exists(child)) {
1327 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1329 "remote object doesn't exist.\n");
1330 mdt_object_unlock(info, child, lhc, 1);
1334 rc = mdt_getattr_internal(info, child, 0);
1335 if (unlikely(rc != 0))
1336 mdt_object_unlock(info, child, lhc, 1);
1341 lname = &info->mti_name;
1342 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1344 if (lu_name_is_valid(lname)) {
1345 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1346 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1347 PNAME(lname), ldlm_rep);
1349 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1350 if (unlikely(reqbody == NULL))
1351 RETURN(err_serious(-EPROTO));
1353 *child_fid = reqbody->mbo_fid2;
1355 if (unlikely(!fid_is_sane(child_fid)))
1356 RETURN(err_serious(-EINVAL));
1358 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1360 PFID(mdt_object_fid(parent)),
1361 PFID(&reqbody->mbo_fid2), ldlm_rep);
1364 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1366 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1367 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1369 "Parent doesn't exist!\n");
1373 if (mdt_object_remote(parent)) {
1374 CERROR("%s: parent "DFID" is on remote target\n",
1375 mdt_obd_name(info->mti_mdt),
1376 PFID(mdt_object_fid(parent)));
1380 if (lu_name_is_valid(lname)) {
1381 /* Always allow to lookup ".." */
1382 if (unlikely(lname->ln_namelen == 2 &&
1383 lname->ln_name[0] == '.' &&
1384 lname->ln_name[1] == '.'))
1385 info->mti_spec.sp_permitted = 1;
1387 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1388 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1393 /* step 1: lock parent only if parent is a directory */
1394 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1395 lhp = &info->mti_lh[MDT_LH_PARENT];
1396 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1397 rc = mdt_object_lock(info, parent, lhp,
1398 MDS_INODELOCK_UPDATE);
1399 if (unlikely(rc != 0))
1403 /* step 2: lookup child's fid by name */
1404 fid_zero(child_fid);
1405 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1406 child_fid, &info->mti_spec);
1408 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1411 GOTO(out_parent, rc);
1414 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1417 *step 3: find the child object by fid & lock it.
1418 * regardless if it is local or remote.
1420 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1421 * set parent dir fid the same as child fid in getattr by fid case
1422 * we should not lu_object_find() the object again, could lead
1423 * to hung if there is a concurrent unlink destroyed the object.
1425 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1426 mdt_object_get(info->mti_env, parent);
1429 child = mdt_object_find(info->mti_env, info->mti_mdt,
1433 if (unlikely(IS_ERR(child)))
1434 GOTO(out_parent, rc = PTR_ERR(child));
1436 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1437 if (!mdt_object_exists(child)) {
1438 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1440 "Object doesn't exist!\n");
1441 GOTO(out_child, rc = -ENOENT);
1444 rc = mdt_check_resent_lock(info, child, lhc);
1446 GOTO(out_child, rc);
1447 } else if (rc > 0) {
1448 mdt_lock_handle_init(lhc);
1449 mdt_lock_reg_init(lhc, LCK_PR);
1452 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1453 mdt_object_exists(child) && !mdt_object_remote(child)) {
1454 struct md_attr *ma = &info->mti_attr;
1457 ma->ma_need = MA_INODE;
1458 rc = mdt_attr_get_complex(info, child, ma);
1459 if (unlikely(rc != 0))
1460 GOTO(out_child, rc);
1462 /* If the file has not been changed for some time, we
1463 * return not only a LOOKUP lock, but also an UPDATE
1464 * lock and this might save us RPC on later STAT. For
1465 * directories, it also let negative dentry cache start
1466 * working for this dir. */
1467 if (ma->ma_valid & MA_INODE &&
1468 ma->ma_attr.la_valid & LA_CTIME &&
1469 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1470 ma->ma_attr.la_ctime < cfs_time_current_sec())
1471 child_bits |= MDS_INODELOCK_UPDATE;
1474 /* layout lock must be granted in a best-effort way
1475 * for IT operations */
1476 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1477 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1478 exp_connect_layout(info->mti_exp) &&
1479 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1480 !mdt_object_remote(child) && ldlm_rep != NULL) {
1481 /* try to grant layout lock for regular file. */
1487 child_bits |= MDS_INODELOCK_LAYOUT;
1488 /* try layout lock, it may fail to be granted due to
1489 * contention at LOOKUP or UPDATE */
1490 if (!mdt_object_lock_try(info, child, lhc,
1492 child_bits &= ~MDS_INODELOCK_LAYOUT;
1493 LASSERT(child_bits != 0);
1494 rc = mdt_object_lock(info, child, lhc,
1500 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1501 * client will enqueue the lock to the remote MDT */
1502 if (mdt_object_remote(child))
1503 child_bits &= ~MDS_INODELOCK_UPDATE;
1504 rc = mdt_object_lock(info, child, lhc, child_bits);
1506 if (unlikely(rc != 0))
1507 GOTO(out_child, rc);
1510 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1512 /* finally, we can get attr for child. */
1513 rc = mdt_getattr_internal(info, child, ma_need);
1514 if (unlikely(rc != 0)) {
1515 mdt_object_unlock(info, child, lhc, 1);
1517 /* Debugging code. */
1518 LDLM_DEBUG(lock, "Returning lock to client");
1519 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1520 &lock->l_resource->lr_name),
1521 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1522 PLDLMRES(lock->l_resource),
1523 PFID(mdt_object_fid(child)));
1526 LDLM_LOCK_PUT(lock);
1530 mdt_object_put(info->mti_env, child);
1533 mdt_object_unlock(info, parent, lhp, 1);
1537 /* normal handler: should release the child lock */
1538 static int mdt_getattr_name(struct tgt_session_info *tsi)
1540 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1541 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1542 struct mdt_body *reqbody;
1543 struct mdt_body *repbody;
1547 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1548 LASSERT(reqbody != NULL);
1549 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1550 LASSERT(repbody != NULL);
1552 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1553 repbody->mbo_eadatasize = 0;
1554 repbody->mbo_aclsize = 0;
1556 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1558 GOTO(out_shrink, rc);
1560 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1561 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1562 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1563 lhc->mlh_reg_lh.cookie = 0;
1565 mdt_exit_ucred(info);
1568 mdt_client_compatibility(info);
1569 rc2 = mdt_fix_reply(info);
1572 mdt_thread_info_fini(info);
1576 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1577 void *karg, void __user *uarg);
1579 static int mdt_set_info(struct tgt_session_info *tsi)
1581 struct ptlrpc_request *req = tgt_ses_req(tsi);
1584 int keylen, vallen, rc = 0;
1588 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1590 DEBUG_REQ(D_HA, req, "no set_info key");
1591 RETURN(err_serious(-EFAULT));
1594 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1597 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1599 DEBUG_REQ(D_HA, req, "no set_info val");
1600 RETURN(err_serious(-EFAULT));
1603 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1606 /* Swab any part of val you need to here */
1607 if (KEY_IS(KEY_READ_ONLY)) {
1608 spin_lock(&req->rq_export->exp_lock);
1610 *exp_connect_flags_ptr(req->rq_export) |=
1613 *exp_connect_flags_ptr(req->rq_export) &=
1614 ~OBD_CONNECT_RDONLY;
1615 spin_unlock(&req->rq_export->exp_lock);
1616 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1617 struct changelog_setinfo *cs = val;
1619 if (vallen != sizeof(*cs)) {
1620 CERROR("%s: bad changelog_clear setinfo size %d\n",
1621 tgt_name(tsi->tsi_tgt), vallen);
1624 if (ptlrpc_req_need_swab(req)) {
1625 __swab64s(&cs->cs_recno);
1626 __swab32s(&cs->cs_id);
1629 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1631 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1633 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1640 static int mdt_readpage(struct tgt_session_info *tsi)
1642 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1643 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1644 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1645 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1646 struct mdt_body *repbody;
1652 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1653 RETURN(err_serious(-ENOMEM));
1655 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1656 if (repbody == NULL || reqbody == NULL)
1657 RETURN(err_serious(-EFAULT));
1660 * prepare @rdpg before calling lower layers and transfer itself. Here
1661 * reqbody->size contains offset of where to start to read and
1662 * reqbody->nlink contains number bytes to read.
1664 rdpg->rp_hash = reqbody->mbo_size;
1665 if (rdpg->rp_hash != reqbody->mbo_size) {
1666 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1667 rdpg->rp_hash, reqbody->mbo_size);
1671 rdpg->rp_attrs = reqbody->mbo_mode;
1672 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1673 rdpg->rp_attrs |= LUDA_64BITHASH;
1674 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1675 exp_max_brw_size(tsi->tsi_exp));
1676 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1678 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1679 if (rdpg->rp_pages == NULL)
1682 for (i = 0; i < rdpg->rp_npages; ++i) {
1683 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1684 if (rdpg->rp_pages[i] == NULL)
1685 GOTO(free_rdpg, rc = -ENOMEM);
1688 /* call lower layers to fill allocated pages with directory data */
1689 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1691 GOTO(free_rdpg, rc);
1693 /* send pages to client */
1694 rc = tgt_sendpage(tsi, rdpg, rc);
1699 for (i = 0; i < rdpg->rp_npages; i++)
1700 if (rdpg->rp_pages[i] != NULL)
1701 __free_page(rdpg->rp_pages[i]);
1702 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1704 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1710 static int mdt_reint_internal(struct mdt_thread_info *info,
1711 struct mdt_lock_handle *lhc,
1714 struct req_capsule *pill = info->mti_pill;
1715 struct mdt_body *repbody;
1720 rc = mdt_reint_unpack(info, op);
1722 CERROR("Can't unpack reint, rc %d\n", rc);
1723 RETURN(err_serious(rc));
1726 /* for replay (no_create) lmm is not needed, client has it already */
1727 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1728 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1731 /* llog cookies are always 0, the field is kept for compatibility */
1732 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1733 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1735 rc = req_capsule_server_pack(pill);
1737 CERROR("Can't pack response, rc %d\n", rc);
1738 RETURN(err_serious(rc));
1741 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1742 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1744 repbody->mbo_eadatasize = 0;
1745 repbody->mbo_aclsize = 0;
1748 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1750 /* for replay no cookkie / lmm need, because client have this already */
1751 if (info->mti_spec.no_create)
1752 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1753 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1755 rc = mdt_init_ucred_reint(info);
1757 GOTO(out_shrink, rc);
1759 rc = mdt_fix_attr_ucred(info, op);
1761 GOTO(out_ucred, rc = err_serious(rc));
1763 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1765 GOTO(out_ucred, rc);
1766 } else if (rc == 1) {
1767 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1768 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1769 GOTO(out_ucred, rc);
1771 rc = mdt_reint_rec(info, lhc);
1774 mdt_exit_ucred(info);
1776 mdt_client_compatibility(info);
1777 rc2 = mdt_fix_reply(info);
1783 static long mdt_reint_opcode(struct ptlrpc_request *req,
1784 const struct req_format **fmt)
1786 struct mdt_device *mdt;
1787 struct mdt_rec_reint *rec;
1790 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1792 opc = rec->rr_opcode;
1793 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1794 if (opc < REINT_MAX && fmt[opc] != NULL)
1795 req_capsule_extend(&req->rq_pill, fmt[opc]);
1797 mdt = mdt_exp2dev(req->rq_export);
1798 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1799 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1800 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1801 opc = err_serious(-EFAULT);
1804 opc = err_serious(-EFAULT);
1809 static int mdt_reint(struct tgt_session_info *tsi)
1813 static const struct req_format *reint_fmts[REINT_MAX] = {
1814 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1815 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1816 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1817 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1818 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1819 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1820 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1821 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1822 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1827 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1829 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1831 * No lock possible here from client to pass it to reint code
1834 rc = mdt_reint_internal(info, NULL, opc);
1835 mdt_thread_info_fini(info);
1840 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1844 /* this should sync the whole device */
1845 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1847 struct dt_device *dt = mdt->mdt_bottom;
1851 rc = dt->dd_ops->dt_sync(env, dt);
1855 /* this should sync this object */
1856 static int mdt_object_sync(struct mdt_thread_info *info)
1858 struct md_object *next;
1862 if (!mdt_object_exists(info->mti_object)) {
1863 CWARN("%s: non existing object "DFID": rc = %d\n",
1864 mdt_obd_name(info->mti_mdt),
1865 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1868 next = mdt_object_child(info->mti_object);
1869 rc = mo_object_sync(info->mti_env, next);
1874 static int mdt_sync(struct tgt_session_info *tsi)
1876 struct ptlrpc_request *req = tgt_ses_req(tsi);
1877 struct req_capsule *pill = tsi->tsi_pill;
1878 struct mdt_body *body;
1883 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1884 RETURN(err_serious(-ENOMEM));
1886 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
1887 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1889 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1891 /* sync an object */
1892 rc = mdt_object_sync(info);
1894 const struct lu_fid *fid;
1895 struct lu_attr *la = &info->mti_attr.ma_attr;
1897 info->mti_attr.ma_need = MA_INODE;
1898 info->mti_attr.ma_valid = 0;
1899 rc = mdt_attr_get_complex(info, info->mti_object,
1902 body = req_capsule_server_get(pill,
1904 fid = mdt_object_fid(info->mti_object);
1905 mdt_pack_attr2body(info, body, la, fid);
1908 mdt_thread_info_fini(info);
1911 mdt_counter_incr(req, LPROC_MDT_SYNC);
1917 * Handle quota control requests to consult current usage/limit, but also
1918 * to configure quota enforcement
1920 static int mdt_quotactl(struct tgt_session_info *tsi)
1922 struct obd_export *exp = tsi->tsi_exp;
1923 struct req_capsule *pill = tsi->tsi_pill;
1924 struct obd_quotactl *oqctl, *repoqc;
1926 struct mdt_device *mdt = mdt_exp2dev(exp);
1927 struct lu_device *qmt = mdt->mdt_qmt_dev;
1928 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
1931 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1933 RETURN(err_serious(-EPROTO));
1935 rc = req_capsule_server_pack(pill);
1937 RETURN(err_serious(rc));
1939 switch (oqctl->qc_cmd) {
1940 /* master quotactl */
1943 if (!nodemap_can_setquota(nodemap))
1948 RETURN(-EOPNOTSUPP);
1949 /* slave quotactl */
1954 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1958 /* map uid/gid for remote client */
1960 if (exp_connect_rmtclient(exp)) {
1961 struct lustre_idmap_table *idmap;
1963 idmap = exp->exp_mdt_data.med_idmap;
1965 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1966 oqctl->qc_cmd != Q_GETINFO))
1969 if (oqctl->qc_type == USRQUOTA)
1970 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1972 else if (oqctl->qc_type == GRPQUOTA)
1973 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1978 if (id == CFS_IDMAP_NOTFOUND) {
1979 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1984 if (oqctl->qc_type == USRQUOTA)
1985 id = nodemap_map_id(nodemap, NODEMAP_UID,
1986 NODEMAP_CLIENT_TO_FS, id);
1987 else if (oqctl->qc_type == GRPQUOTA)
1988 id = nodemap_map_id(nodemap, NODEMAP_UID,
1989 NODEMAP_CLIENT_TO_FS, id);
1991 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1993 RETURN(err_serious(-EFAULT));
1995 if (oqctl->qc_id != id)
1996 swap(oqctl->qc_id, id);
1998 switch (oqctl->qc_cmd) {
2004 /* forward quotactl request to QMT */
2005 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2010 /* slave quotactl */
2011 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2016 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2020 if (oqctl->qc_id != id)
2021 swap(oqctl->qc_id, id);
2027 /** clone llog ctxt from child (mdd)
2028 * This allows remote llog (replicator) access.
2029 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2030 * context was originally set up, or we can handle them directly.
2031 * I choose the latter, but that means I need any llog
2032 * contexts set up by child to be accessable by the mdt. So we clone the
2033 * context into our context list here.
2035 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2038 struct md_device *next = mdt->mdt_child;
2039 struct llog_ctxt *ctxt;
2042 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2045 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2046 if (rc || ctxt == NULL) {
2050 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2052 CERROR("Can't set mdt ctxt %d\n", rc);
2057 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2058 struct mdt_device *mdt, int idx)
2060 struct llog_ctxt *ctxt;
2062 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2065 /* Put once for the get we just did, and once for the clone */
2066 llog_ctxt_put(ctxt);
2067 llog_ctxt_put(ctxt);
2072 * sec context handlers
2074 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2078 rc = mdt_handle_idmap(tsi);
2080 struct ptlrpc_request *req = tgt_ses_req(tsi);
2083 opc = lustre_msg_get_opc(req->rq_reqmsg);
2084 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2085 sptlrpc_svc_ctx_invalidate(req);
2088 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2094 * quota request handlers
2096 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2098 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2099 struct lu_device *qmt = mdt->mdt_qmt_dev;
2104 RETURN(err_serious(-EOPNOTSUPP));
2106 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2110 struct mdt_object *mdt_object_new(const struct lu_env *env,
2111 struct mdt_device *d,
2112 const struct lu_fid *f)
2114 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2115 struct lu_object *o;
2116 struct mdt_object *m;
2119 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2120 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2121 if (unlikely(IS_ERR(o)))
2122 m = (struct mdt_object *)o;
2128 struct mdt_object *mdt_object_find(const struct lu_env *env,
2129 struct mdt_device *d,
2130 const struct lu_fid *f)
2132 struct lu_object *o;
2133 struct mdt_object *m;
2136 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2137 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2138 if (unlikely(IS_ERR(o)))
2139 m = (struct mdt_object *)o;
2147 * Asyncronous commit for mdt device.
2149 * Pass asynchonous commit call down the MDS stack.
2151 * \param env environment
2152 * \param mdt the mdt device
2154 static void mdt_device_commit_async(const struct lu_env *env,
2155 struct mdt_device *mdt)
2157 struct dt_device *dt = mdt->mdt_bottom;
2161 rc = dt->dd_ops->dt_commit_async(env, dt);
2162 if (unlikely(rc != 0))
2163 CWARN("%s: async commit start failed: rc = %d\n",
2164 mdt_obd_name(mdt), rc);
2165 atomic_inc(&mdt->mdt_async_commit_count);
2170 * Mark the lock as "synchonous".
2172 * Mark the lock to deffer transaction commit to the unlock time.
2174 * \param lock the lock to mark as "synchonous"
2176 * \see mdt_is_lock_sync
2177 * \see mdt_save_lock
2179 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2181 lock->l_ast_data = (void*)1;
2185 * Check whehter the lock "synchonous" or not.
2187 * \param lock the lock to check
2188 * \retval 1 the lock is "synchonous"
2189 * \retval 0 the lock isn't "synchronous"
2191 * \see mdt_set_lock_sync
2192 * \see mdt_save_lock
2194 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2196 return lock->l_ast_data != NULL;
2200 * Blocking AST for mdt locks.
2202 * Starts transaction commit if in case of COS lock conflict or
2203 * deffers such a commit to the mdt_save_lock.
2205 * \param lock the lock which blocks a request or cancelling lock
2206 * \param desc unused
2207 * \param data unused
2208 * \param flag indicates whether this cancelling or blocking callback
2210 * \see ldlm_blocking_ast_nocheck
2212 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2213 void *data, int flag)
2215 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2216 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2220 if (flag == LDLM_CB_CANCELING)
2223 lock_res_and_lock(lock);
2224 if (lock->l_blocking_ast != mdt_blocking_ast) {
2225 unlock_res_and_lock(lock);
2228 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2229 lock->l_blocking_lock != NULL) {
2230 if (mdt_cos_is_enabled(mdt) &&
2231 lock->l_client_cookie !=
2232 lock->l_blocking_lock->l_client_cookie)
2233 mdt_set_lock_sync(lock);
2234 else if (mdt_slc_is_enabled(mdt) &&
2235 ldlm_is_cos_incompat(lock->l_blocking_lock))
2236 mdt_set_lock_sync(lock);
2238 rc = ldlm_blocking_ast_nocheck(lock);
2240 /* There is no lock conflict if l_blocking_lock == NULL,
2241 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2242 * when the last reference to a local lock was released */
2243 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2246 rc = lu_env_init(&env, LCT_LOCAL);
2247 if (unlikely(rc != 0))
2248 CWARN("%s: lu_env initialization failed, cannot "
2249 "start asynchronous commit: rc = %d\n",
2252 mdt_device_commit_async(&env, mdt);
2259 * Blocking AST for cross-MDT lock
2261 * Discard lock from uncommitted_slc_locks and cancel it.
2263 * \param lock the lock which blocks a request or cancelling lock
2264 * \param desc unused
2265 * \param data unused
2266 * \param flag indicates whether this cancelling or blocking callback
2267 * \retval 0 on success
2268 * \retval negative number on error
2270 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2271 void *data, int flag)
2273 struct lustre_handle lockh;
2278 case LDLM_CB_BLOCKING:
2279 ldlm_lock2handle(lock, &lockh);
2280 rc = ldlm_cli_cancel(&lockh,
2281 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2283 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2287 case LDLM_CB_CANCELING:
2288 LDLM_DEBUG(lock, "Revoke remote lock\n");
2289 /* discard slc lock here so that it can be cleaned anytime,
2290 * especially for cleanup_resource() */
2291 tgt_discard_slc_lock(lock);
2300 int mdt_check_resent_lock(struct mdt_thread_info *info,
2301 struct mdt_object *mo,
2302 struct mdt_lock_handle *lhc)
2304 /* the lock might already be gotten in ldlm_handle_enqueue() */
2305 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2306 struct ptlrpc_request *req = mdt_info_req(info);
2307 struct ldlm_lock *lock;
2309 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2310 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2312 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2313 * a resend case, however, it could be already destroyed
2314 * due to client eviction or a raced cancel RPC. */
2315 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
2316 lhc->mlh_reg_lh.cookie);
2320 if (!fid_res_name_eq(mdt_object_fid(mo),
2321 &lock->l_resource->lr_name)) {
2322 CWARN("%s: Although resent, but still not "
2323 "get child lock:"DFID"\n",
2324 info->mti_exp->exp_obd->obd_name,
2325 PFID(mdt_object_fid(mo)));
2326 LDLM_LOCK_PUT(lock);
2329 LDLM_LOCK_PUT(lock);
2335 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2336 const struct lu_fid *fid, struct lustre_handle *lh,
2337 enum ldlm_mode mode, __u64 ibits, bool nonblock)
2339 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2340 union ldlm_policy_data *policy = &mti->mti_policy;
2341 struct ldlm_res_id *res_id = &mti->mti_res_id;
2345 LASSERT(mdt_object_remote(o));
2347 fid_build_reg_res_name(fid, res_id);
2349 memset(einfo, 0, sizeof(*einfo));
2350 einfo->ei_type = LDLM_IBITS;
2351 einfo->ei_mode = mode;
2352 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2353 einfo->ei_cb_cp = ldlm_completion_ast;
2354 einfo->ei_enq_slave = 0;
2355 einfo->ei_res_id = res_id;
2357 einfo->ei_nonblock = 1;
2359 memset(policy, 0, sizeof(*policy));
2360 policy->l_inodebits.bits = ibits;
2362 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2367 static int mdt_object_local_lock(struct mdt_thread_info *info,
2368 struct mdt_object *o,
2369 struct mdt_lock_handle *lh, __u64 ibits,
2370 bool nonblock, bool cos_incompat)
2372 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2373 union ldlm_policy_data *policy = &info->mti_policy;
2374 struct ldlm_res_id *res_id = &info->mti_res_id;
2379 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2380 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2381 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2382 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2385 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2386 lh->mlh_reg_mode == LCK_EX);
2387 dlmflags |= LDLM_FL_COS_INCOMPAT;
2388 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2389 dlmflags |= LDLM_FL_COS_ENABLED;
2392 /* Only enqueue LOOKUP lock for remote object */
2393 if (mdt_object_remote(o))
2394 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2396 if (lh->mlh_type == MDT_PDO_LOCK) {
2397 /* check for exists after object is locked */
2398 if (mdt_object_exists(o) == 0) {
2399 /* Non-existent object shouldn't have PDO lock */
2402 /* Non-dir object shouldn't have PDO lock */
2403 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2408 memset(policy, 0, sizeof(*policy));
2409 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2411 dlmflags |= LDLM_FL_ATOMIC_CB;
2413 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2416 * Take PDO lock on whole directory and build correct @res_id for lock
2417 * on part of directory.
2419 if (lh->mlh_pdo_hash != 0) {
2420 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2421 mdt_lock_pdo_mode(info, o, lh);
2422 if (lh->mlh_pdo_mode != LCK_NL) {
2424 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2425 * is never going to be sent to client and we do not
2426 * want it slowed down due to possible cancels.
2428 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2429 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2430 policy, res_id, dlmflags,
2431 info->mti_exp == NULL ? NULL :
2432 &info->mti_exp->exp_handle.h_cookie);
2433 if (unlikely(rc != 0))
2434 GOTO(out_unlock, rc);
2438 * Finish res_id initializing by name hash marking part of
2439 * directory which is taking modification.
2441 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2444 policy->l_inodebits.bits = ibits;
2447 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2448 * going to be sent to client. If it is - mdt_intent_policy() path will
2449 * fix it up and turn FL_LOCAL flag off.
2451 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2452 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2453 info->mti_exp == NULL ? NULL :
2454 &info->mti_exp->exp_handle.h_cookie);
2457 mdt_object_unlock(info, o, lh, 1);
2458 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2459 lh->mlh_pdo_hash != 0 &&
2460 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2461 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2467 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2468 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2471 struct mdt_lock_handle *local_lh = NULL;
2475 if (!mdt_object_remote(o)) {
2476 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2481 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2482 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2483 MDS_INODELOCK_XATTR);
2485 /* Only enqueue LOOKUP lock for remote object */
2486 if (ibits & MDS_INODELOCK_LOOKUP) {
2487 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2488 nonblock, cos_incompat);
2495 if (ibits & MDS_INODELOCK_UPDATE) {
2496 /* Sigh, PDO needs to enqueue 2 locks right now, but
2497 * enqueue RPC can only request 1 lock, to avoid extra
2498 * RPC, so it will instead enqueue EX lock for remote
2499 * object anyway XXX*/
2500 if (lh->mlh_type == MDT_PDO_LOCK &&
2501 lh->mlh_pdo_hash != 0) {
2502 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2503 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2504 PFID(mdt_object_fid(o)));
2505 lh->mlh_pdo_hash = 0;
2506 lh->mlh_rreg_mode = LCK_EX;
2507 lh->mlh_type = MDT_REG_LOCK;
2509 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2512 MDS_INODELOCK_UPDATE, nonblock);
2513 if (rc != ELDLM_OK) {
2514 if (local_lh != NULL)
2515 mdt_object_unlock(info, o, local_lh, rc);
2523 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2524 struct mdt_lock_handle *lh, __u64 ibits)
2526 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2529 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2530 struct mdt_lock_handle *lh, __u64 ibits,
2533 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2534 return mdt_object_lock_internal(info, o, lh, ibits, false,
2538 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2539 struct mdt_lock_handle *lh, __u64 ibits)
2541 struct mdt_lock_handle tmp = *lh;
2544 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2551 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2552 struct mdt_object *o, struct mdt_lock_handle *lh,
2553 __u64 ibits, bool cos_incompat)
2555 struct mdt_lock_handle tmp = *lh;
2558 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2559 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2567 * Save a lock within request object.
2569 * Keep the lock referenced until whether client ACK or transaction
2570 * commit happens or release the lock immediately depending on input
2571 * parameters. If COS is ON, a write lock is converted to COS lock
2574 * \param info thead info object
2575 * \param h lock handle
2576 * \param mode lock mode
2577 * \param decref force immediate lock releasing
2579 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2580 enum ldlm_mode mode, int decref)
2584 if (lustre_handle_is_used(h)) {
2585 if (decref || !info->mti_has_trans ||
2586 !(mode & (LCK_PW | LCK_EX))) {
2587 mdt_fid_unlock(h, mode);
2589 struct mdt_device *mdt = info->mti_mdt;
2590 struct ldlm_lock *lock = ldlm_handle2lock(h);
2591 struct ptlrpc_request *req = mdt_info_req(info);
2594 cos = (mdt_cos_is_enabled(mdt) ||
2595 mdt_slc_is_enabled(mdt));
2597 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2600 /* there is no request if mdt_object_unlock() is called
2601 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2602 if (likely(req != NULL)) {
2603 CDEBUG(D_HA, "request = %p reply state = %p"
2604 " transno = "LPD64"\n", req,
2605 req->rq_reply_state, req->rq_transno);
2607 ldlm_lock_downgrade(lock, LCK_COS);
2610 ptlrpc_save_lock(req, h, mode, cos);
2612 mdt_fid_unlock(h, mode);
2614 if (mdt_is_lock_sync(lock)) {
2615 CDEBUG(D_HA, "found sync-lock,"
2616 " async commit started\n");
2617 mdt_device_commit_async(info->mti_env,
2620 LDLM_LOCK_PUT(lock);
2629 * Save cross-MDT lock in uncommitted_slc_locks
2631 * Keep the lock referenced until transaction commit happens or release the lock
2632 * immediately depending on input parameters.
2634 * \param info thead info object
2635 * \param h lock handle
2636 * \param mode lock mode
2637 * \param decref force immediate lock releasing
2639 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2640 struct lustre_handle *h, enum ldlm_mode mode,
2645 if (lustre_handle_is_used(h)) {
2646 if (decref || !info->mti_has_trans ||
2647 !(mode & (LCK_PW | LCK_EX))) {
2648 ldlm_lock_decref_and_cancel(h, mode);
2650 struct ldlm_lock *lock = ldlm_handle2lock(h);
2651 struct ptlrpc_request *req = mdt_info_req(info);
2653 LASSERT(req != NULL);
2654 tgt_save_slc_lock(lock, req->rq_transno);
2655 ldlm_lock_decref(h, mode);
2664 * Unlock mdt object.
2666 * Immeditely release the regular lock and the PDO lock or save the
2667 * lock in request and keep them referenced until client ACK or
2668 * transaction commit.
2670 * \param info thread info object
2671 * \param o mdt object
2672 * \param lh mdt lock handle referencing regular and PDO locks
2673 * \param decref force immediate lock releasing
2675 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2676 struct mdt_lock_handle *lh, int decref)
2680 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2681 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2682 mdt_save_remote_lock(info, &lh->mlh_rreg_lh, lh->mlh_rreg_mode, decref);
2687 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2688 const struct lu_fid *f,
2689 struct mdt_lock_handle *lh,
2692 struct mdt_object *o;
2694 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2698 rc = mdt_object_lock(info, o, lh, ibits);
2700 mdt_object_put(info->mti_env, o);
2707 void mdt_object_unlock_put(struct mdt_thread_info * info,
2708 struct mdt_object * o,
2709 struct mdt_lock_handle *lh,
2712 mdt_object_unlock(info, o, lh, decref);
2713 mdt_object_put(info->mti_env, o);
2717 * Generic code handling requests that have struct mdt_body passed in:
2719 * - extract mdt_body from request and save it in @info, if present;
2721 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2724 * - if HABEO_CORPUS flag is set for this request type check whether object
2725 * actually exists on storage (lu_object_exists()).
2728 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2730 const struct mdt_body *body;
2731 struct mdt_object *obj;
2732 const struct lu_env *env;
2733 struct req_capsule *pill;
2737 env = info->mti_env;
2738 pill = info->mti_pill;
2740 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2744 if (!(body->mbo_valid & OBD_MD_FLID))
2747 if (!fid_is_sane(&body->mbo_fid1)) {
2748 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2752 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2754 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2755 mdt_object_put(env, obj);
2758 info->mti_object = obj;
2767 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2769 struct req_capsule *pill = info->mti_pill;
2773 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2774 rc = mdt_body_unpack(info, flags);
2778 if (rc == 0 && (flags & HABEO_REFERO)) {
2780 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2781 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2783 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2784 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2787 rc = req_capsule_server_pack(pill);
2792 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2794 lh->mlh_type = MDT_NUL_LOCK;
2795 lh->mlh_reg_lh.cookie = 0ull;
2796 lh->mlh_reg_mode = LCK_MINMODE;
2797 lh->mlh_pdo_lh.cookie = 0ull;
2798 lh->mlh_pdo_mode = LCK_MINMODE;
2799 lh->mlh_rreg_lh.cookie = 0ull;
2800 lh->mlh_rreg_mode = LCK_MINMODE;
2803 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2805 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2806 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2810 * Initialize fields of struct mdt_thread_info. Other fields are left in
2811 * uninitialized state, because it's too expensive to zero out whole
2812 * mdt_thread_info (> 1K) on each request arrival.
2814 void mdt_thread_info_init(struct ptlrpc_request *req,
2815 struct mdt_thread_info *info)
2819 info->mti_pill = &req->rq_pill;
2822 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2823 mdt_lock_handle_init(&info->mti_lh[i]);
2825 /* mdt device: it can be NULL while CONNECT */
2826 if (req->rq_export) {
2827 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2828 info->mti_exp = req->rq_export;
2830 info->mti_mdt = NULL;
2831 info->mti_env = req->rq_svc_thread->t_env;
2832 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2834 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2835 info->mti_big_buf = LU_BUF_NULL;
2836 info->mti_body = NULL;
2837 info->mti_object = NULL;
2838 info->mti_dlm_req = NULL;
2839 info->mti_has_trans = 0;
2840 info->mti_cross_ref = 0;
2841 info->mti_opdata = 0;
2842 info->mti_big_lmm_used = 0;
2844 info->mti_spec.no_create = 0;
2845 info->mti_spec.sp_rm_entry = 0;
2846 info->mti_spec.sp_permitted = 0;
2847 info->mti_spec.sp_migrate_close = 0;
2849 info->mti_spec.u.sp_ea.eadata = NULL;
2850 info->mti_spec.u.sp_ea.eadatalen = 0;
2853 void mdt_thread_info_fini(struct mdt_thread_info *info)
2857 if (info->mti_object != NULL) {
2858 mdt_object_put(info->mti_env, info->mti_object);
2859 info->mti_object = NULL;
2862 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2863 mdt_lock_handle_fini(&info->mti_lh[i]);
2864 info->mti_env = NULL;
2865 info->mti_pill = NULL;
2866 info->mti_exp = NULL;
2868 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2869 lu_buf_free(&info->mti_big_buf);
2872 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2874 struct mdt_thread_info *mti;
2876 mti = mdt_th_info(tsi->tsi_env);
2877 LASSERT(mti != NULL);
2879 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2880 if (tsi->tsi_corpus != NULL) {
2881 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2882 lu_object_get(tsi->tsi_corpus);
2884 mti->mti_body = tsi->tsi_mdt_body;
2885 mti->mti_dlm_req = tsi->tsi_dlm_req;
2890 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2892 struct ptlrpc_request *req = tgt_ses_req(tsi);
2897 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
2899 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
2900 set_current_state(TASK_UNINTERRUPTIBLE);
2901 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
2904 rc = tgt_connect(tsi);
2908 rc = mdt_init_idmap(tsi);
2913 obd_disconnect(class_export_get(req->rq_export));
2932 static int mdt_intent_getattr(enum mdt_it_code opcode,
2933 struct mdt_thread_info *info,
2934 struct ldlm_lock **,
2937 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2938 struct mdt_thread_info *info,
2939 struct ldlm_lock **lockp,
2942 static int mdt_intent_layout(enum mdt_it_code opcode,
2943 struct mdt_thread_info *info,
2944 struct ldlm_lock **,
2946 static int mdt_intent_reint(enum mdt_it_code opcode,
2947 struct mdt_thread_info *info,
2948 struct ldlm_lock **,
2951 static struct mdt_it_flavor {
2952 const struct req_format *it_fmt;
2954 int (*it_act)(enum mdt_it_code ,
2955 struct mdt_thread_info *,
2956 struct ldlm_lock **,
2959 } mdt_it_flavor[] = {
2961 .it_fmt = &RQF_LDLM_INTENT,
2962 /*.it_flags = HABEO_REFERO,*/
2964 .it_act = mdt_intent_reint,
2965 .it_reint = REINT_OPEN
2968 .it_fmt = &RQF_LDLM_INTENT,
2970 * OCREAT is not a MUTABOR request as if the file
2972 * We do the extra check of OBD_CONNECT_RDONLY in
2973 * mdt_reint_open() when we really need to create
2977 .it_act = mdt_intent_reint,
2978 .it_reint = REINT_OPEN
2981 .it_fmt = &RQF_LDLM_INTENT,
2982 .it_flags = MUTABOR,
2983 .it_act = mdt_intent_reint,
2984 .it_reint = REINT_CREATE
2986 [MDT_IT_GETATTR] = {
2987 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2988 .it_flags = HABEO_REFERO,
2989 .it_act = mdt_intent_getattr
2991 [MDT_IT_READDIR] = {
2997 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2998 .it_flags = HABEO_REFERO,
2999 .it_act = mdt_intent_getattr
3002 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3003 .it_flags = MUTABOR,
3005 .it_reint = REINT_UNLINK
3009 .it_flags = MUTABOR,
3012 [MDT_IT_GETXATTR] = {
3013 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3014 .it_flags = HABEO_CORPUS,
3015 .it_act = mdt_intent_getxattr
3018 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3020 .it_act = mdt_intent_layout
3025 mdt_intent_lock_replace(struct mdt_thread_info *info,
3026 struct ldlm_lock **lockp,
3027 struct mdt_lock_handle *lh,
3030 struct ptlrpc_request *req = mdt_info_req(info);
3031 struct ldlm_lock *lock = *lockp;
3032 struct ldlm_lock *new_lock;
3034 /* If possible resent found a lock, @lh is set to its handle */
3035 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3037 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3038 lh->mlh_reg_lh.cookie = 0;
3042 LASSERTF(new_lock != NULL,
3043 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3046 * If we've already given this lock to a client once, then we should
3047 * have no readers or writers. Otherwise, we should have one reader
3048 * _or_ writer ref (which will be zeroed below) before returning the
3051 if (new_lock->l_export == req->rq_export) {
3052 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3054 LASSERT(new_lock->l_export == NULL);
3055 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3060 if (new_lock->l_export == req->rq_export) {
3062 * Already gave this to the client, which means that we
3063 * reconstructed a reply.
3065 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3068 LDLM_LOCK_RELEASE(new_lock);
3069 lh->mlh_reg_lh.cookie = 0;
3070 RETURN(ELDLM_LOCK_REPLACED);
3074 * Fixup the lock to be given to the client.
3076 lock_res_and_lock(new_lock);
3077 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3078 * possible blocking AST. */
3079 while (new_lock->l_readers > 0) {
3080 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3081 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3082 new_lock->l_readers--;
3084 while (new_lock->l_writers > 0) {
3085 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3086 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3087 new_lock->l_writers--;
3090 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3091 new_lock->l_blocking_ast = lock->l_blocking_ast;
3092 new_lock->l_completion_ast = lock->l_completion_ast;
3093 new_lock->l_remote_handle = lock->l_remote_handle;
3094 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3096 unlock_res_and_lock(new_lock);
3098 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3099 &new_lock->l_remote_handle,
3100 &new_lock->l_exp_hash);
3102 LDLM_LOCK_RELEASE(new_lock);
3103 lh->mlh_reg_lh.cookie = 0;
3105 RETURN(ELDLM_LOCK_REPLACED);
3108 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3109 struct ldlm_lock *new_lock,
3110 struct mdt_lock_handle *lh,
3113 struct ptlrpc_request *req = mdt_info_req(info);
3114 struct ldlm_request *dlmreq;
3116 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3119 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3121 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3122 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3124 if (flags & LDLM_FL_RESENT) {
3125 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3126 lh->mlh_reg_mode = new_lock->l_granted_mode;
3128 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3129 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3130 lh->mlh_reg_lh.cookie);
3135 * If the xid matches, then we know this is a resent request, and allow
3136 * it. (It's probably an OPEN, for which we don't send a lock.
3138 if (req_can_reconstruct(req, NULL))
3142 * This remote handle isn't enqueued, so we never received or processed
3143 * this request. Clear MSG_RESENT, because it can be handled like any
3144 * normal request now.
3146 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3148 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3149 dlmreq->lock_handle[0].cookie);
3152 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3153 struct mdt_thread_info *info,
3154 struct ldlm_lock **lockp,
3157 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3158 struct ldlm_reply *ldlm_rep = NULL;
3162 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3163 * (for the resend case) or a new lock. Below we will use it to
3164 * replace the original lock.
3166 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3167 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3168 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3169 rc = mdt_object_lock(info, info->mti_object, lhc,
3170 MDS_INODELOCK_XATTR);
3175 grc = mdt_getxattr(info);
3177 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3179 if (mdt_info_req(info)->rq_repmsg != NULL)
3180 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3181 if (ldlm_rep == NULL)
3182 RETURN(err_serious(-EFAULT));
3184 ldlm_rep->lock_policy_res2 = grc;
3189 static int mdt_intent_getattr(enum mdt_it_code opcode,
3190 struct mdt_thread_info *info,
3191 struct ldlm_lock **lockp,
3194 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3196 struct ldlm_reply *ldlm_rep;
3197 struct mdt_body *reqbody;
3198 struct mdt_body *repbody;
3202 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3205 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3208 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3209 repbody->mbo_eadatasize = 0;
3210 repbody->mbo_aclsize = 0;
3214 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3216 case MDT_IT_GETATTR:
3217 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3221 CERROR("Unsupported intent (%d)\n", opcode);
3222 GOTO(out_shrink, rc = -EINVAL);
3225 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3227 GOTO(out_shrink, rc);
3229 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3230 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3232 /* Get lock from request for possible resent case. */
3233 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3235 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3236 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3238 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3239 ldlm_rep->lock_policy_res2 = 0;
3240 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3241 ldlm_rep->lock_policy_res2) {
3242 lhc->mlh_reg_lh.cookie = 0ull;
3243 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3246 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3249 mdt_exit_ucred(info);
3251 mdt_client_compatibility(info);
3252 rc2 = mdt_fix_reply(info);
3258 static int mdt_intent_layout(enum mdt_it_code opcode,
3259 struct mdt_thread_info *info,
3260 struct ldlm_lock **lockp,
3263 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3264 struct layout_intent *layout;
3266 struct mdt_object *obj = NULL;
3267 int layout_size = 0;
3271 if (opcode != MDT_IT_LAYOUT) {
3272 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3277 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3281 if (layout->li_opc != LAYOUT_INTENT_ACCESS) {
3282 CERROR("%s: Unsupported layout intent opc %d\n",
3283 mdt_obd_name(info->mti_mdt), layout->li_opc);
3287 fid = &info->mti_tmp_fid2;
3288 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3290 /* Get lock from request for possible resent case. */
3291 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3293 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3295 GOTO(out, rc = PTR_ERR(obj));
3297 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3298 layout_size = mdt_attr_get_eabuf_size(info, obj);
3299 if (layout_size < 0)
3300 GOTO(out_obj, rc = layout_size);
3302 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3303 info->mti_mdt->mdt_max_mdsize = layout_size;
3306 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3307 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3309 rc = req_capsule_server_pack(info->mti_pill);
3313 mdt_object_put(info->mti_env, obj);
3315 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3316 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3319 lhc->mlh_reg_lh.cookie = 0;
3324 static int mdt_intent_reint(enum mdt_it_code opcode,
3325 struct mdt_thread_info *info,
3326 struct ldlm_lock **lockp,
3329 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3330 struct ldlm_reply *rep = NULL;
3334 static const struct req_format *intent_fmts[REINT_MAX] = {
3335 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3336 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3341 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3345 if (mdt_it_flavor[opcode].it_reint != opc) {
3346 CERROR("Reint code %ld doesn't match intent: %d\n",
3348 RETURN(err_serious(-EPROTO));
3351 /* Get lock from request for possible resent case. */
3352 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3354 rc = mdt_reint_internal(info, lhc, opc);
3356 /* Check whether the reply has been packed successfully. */
3357 if (mdt_info_req(info)->rq_repmsg != NULL)
3358 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3360 RETURN(err_serious(-EFAULT));
3362 /* MDC expects this in any case */
3364 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3366 /* the open lock or the lock for cross-ref object should be
3367 * returned to the client */
3368 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3369 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3370 rep->lock_policy_res2 = 0;
3371 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3375 rep->lock_policy_res2 = clear_serious(rc);
3377 if (rep->lock_policy_res2 == -ENOENT &&
3378 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3379 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3380 rep->lock_policy_res2 = 0;
3382 lhc->mlh_reg_lh.cookie = 0ull;
3383 if (rc == -ENOTCONN || rc == -ENODEV ||
3384 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3386 * If it is the disconnect error (ENODEV & ENOCONN), the error
3387 * will be returned by rq_status, and client at ptlrpc layer
3388 * will detect this, then disconnect, reconnect the import
3389 * immediately, instead of impacting the following the rpc.
3394 * For other cases, the error will be returned by intent, and client
3395 * will retrieve the result from intent.
3397 RETURN(ELDLM_LOCK_ABORTED);
3400 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3408 case IT_OPEN|IT_CREAT:
3415 rc = MDT_IT_READDIR;
3418 rc = MDT_IT_GETATTR;
3430 rc = MDT_IT_GETXATTR;
3435 case IT_QUOTA_DQACQ:
3440 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3447 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3448 struct mdt_thread_info *info,
3449 struct ldlm_lock **lockp, __u64 flags)
3451 struct req_capsule *pill = info->mti_pill;
3452 struct ptlrpc_request *req = mdt_info_req(info);
3453 struct mdt_it_flavor *flv;
3458 opc = mdt_intent_code(itopc);
3462 if (opc == MDT_IT_QUOTA) {
3463 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3466 RETURN(-EOPNOTSUPP);
3468 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3469 /* pass the request to quota master */
3470 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3471 mdt_info_req(info), lockp,
3476 flv = &mdt_it_flavor[opc];
3477 if (flv->it_fmt != NULL)
3478 req_capsule_extend(pill, flv->it_fmt);
3480 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3484 if (flv->it_flags & MUTABOR &&
3485 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3488 if (flv->it_act != NULL) {
3489 struct ldlm_reply *rep;
3491 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3493 /* execute policy */
3494 rc = flv->it_act(opc, info, lockp, flags);
3496 /* Check whether the reply has been packed successfully. */
3497 if (req->rq_repmsg != NULL) {
3498 rep = req_capsule_server_get(info->mti_pill,
3500 rep->lock_policy_res2 =
3501 ptlrpc_status_hton(rep->lock_policy_res2);
3508 static int mdt_intent_policy(struct ldlm_namespace *ns,
3509 struct ldlm_lock **lockp, void *req_cookie,
3510 enum ldlm_mode mode, __u64 flags, void *data)
3512 struct tgt_session_info *tsi;
3513 struct mdt_thread_info *info;
3514 struct ptlrpc_request *req = req_cookie;
3515 struct ldlm_intent *it;
3516 struct req_capsule *pill;
3521 LASSERT(req != NULL);
3523 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3525 info = tsi2mdt_info(tsi);
3526 LASSERT(info != NULL);
3527 pill = info->mti_pill;
3528 LASSERT(pill->rc_req == req);
3530 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3531 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3532 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3534 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3538 /* Lock without inodebits makes no sense and will oops
3539 * later in ldlm. Let's check it now to see if we have
3540 * ibits corrupted somewhere in mdt_intent_opc().
3541 * The case for client miss to set ibits has been
3542 * processed by others. */
3543 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3544 lr_type == LDLM_IBITS,
3545 info->mti_dlm_req->lock_desc.\
3546 l_policy_data.l_inodebits.bits != 0));
3548 rc = err_serious(-EFAULT);
3550 /* No intent was provided */
3551 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3552 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3553 rc = req_capsule_server_pack(pill);
3555 rc = err_serious(rc);
3557 mdt_thread_info_fini(info);
3561 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3563 struct seq_server_site *ss = mdt_seq_site(mdt);
3565 if (ss->ss_node_id == 0)
3568 if (ss->ss_client_seq != NULL) {
3569 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3570 ss->ss_client_seq->lcs_exp = NULL;
3573 if (ss->ss_server_fld != NULL) {
3574 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3575 ss->ss_server_fld->lsf_control_exp = NULL;
3579 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3581 struct seq_server_site *ss = mdt_seq_site(mdt);
3586 if (ss->ss_server_seq != NULL)
3587 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3590 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3592 mdt_seq_fini_cli(mdt);
3593 mdt_deregister_seq_exp(mdt);
3595 return seq_site_fini(env, mdt_seq_site(mdt));
3599 * It will retrieve its FLDB entries from MDT0, and it only happens
3600 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3601 * and it needs to refresh FLDB from the MDT0.
3603 static int mdt_register_lwp_callback(void *data)
3606 struct mdt_device *mdt = data;
3607 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3611 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3613 rc = lu_env_init(&env, LCT_MD_THREAD);
3615 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3619 /* Allocate new sequence now to avoid creating local transaction
3620 * in the normal transaction process */
3621 rc = seq_server_check_and_alloc_super(&env,
3622 mdt_seq_site(mdt)->ss_server_seq);
3627 rc = fld_update_from_controller(&env, fld);
3629 CERROR("%s: cannot update controller: rc = %d\n",
3630 mdt_obd_name(mdt), rc);
3639 static int mdt_register_seq_exp(struct mdt_device *mdt)
3641 struct seq_server_site *ss = mdt_seq_site(mdt);
3642 char *lwp_name = NULL;
3645 if (ss->ss_node_id == 0)
3648 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3649 if (lwp_name == NULL)
3650 GOTO(out_free, rc = -ENOMEM);
3652 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3656 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3661 rc = lustre_register_lwp_item(lwp_name,
3662 &ss->ss_server_fld->lsf_control_exp,
3663 mdt_register_lwp_callback, mdt);
3665 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3666 ss->ss_client_seq->lcs_exp = NULL;
3670 if (lwp_name != NULL)
3671 OBD_FREE(lwp_name, MAX_OBD_NAME);
3677 * Init client sequence manager which is used by local MDS to talk to sequence
3678 * controller on remote node.
3680 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3682 struct seq_server_site *ss = mdt_seq_site(mdt);
3687 /* check if this is adding the first MDC and controller is not yet
3689 OBD_ALLOC_PTR(ss->ss_client_seq);
3690 if (ss->ss_client_seq == NULL)
3693 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3694 if (prefix == NULL) {
3695 OBD_FREE_PTR(ss->ss_client_seq);
3696 ss->ss_client_seq = NULL;
3700 /* Note: seq_client_fini will be called in seq_site_fini */
3701 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3702 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3703 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3705 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3707 OBD_FREE_PTR(ss->ss_client_seq);
3708 ss->ss_client_seq = NULL;
3712 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3717 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3719 struct seq_server_site *ss;
3723 ss = mdt_seq_site(mdt);
3724 /* init sequence controller server(MDT0) */
3725 if (ss->ss_node_id == 0) {
3726 OBD_ALLOC_PTR(ss->ss_control_seq);
3727 if (ss->ss_control_seq == NULL)
3730 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3731 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3734 GOTO(out_seq_fini, rc);
3737 /* Init normal sequence server */
3738 OBD_ALLOC_PTR(ss->ss_server_seq);
3739 if (ss->ss_server_seq == NULL)
3740 GOTO(out_seq_fini, rc = -ENOMEM);
3742 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3743 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3745 GOTO(out_seq_fini, rc);
3747 /* init seq client for seq server to talk to seq controller(MDT0) */
3748 rc = mdt_seq_init_cli(env, mdt);
3750 GOTO(out_seq_fini, rc);
3752 if (ss->ss_node_id != 0)
3753 /* register controller export through lwp */
3754 rc = mdt_register_seq_exp(mdt);
3759 mdt_seq_fini(env, mdt);
3767 static int mdt_fld_fini(const struct lu_env *env,
3768 struct mdt_device *m)
3770 struct seq_server_site *ss = mdt_seq_site(m);
3773 if (ss && ss->ss_server_fld) {
3774 fld_server_fini(env, ss->ss_server_fld);
3775 OBD_FREE_PTR(ss->ss_server_fld);
3776 ss->ss_server_fld = NULL;
3782 static int mdt_fld_init(const struct lu_env *env,
3784 struct mdt_device *m)
3786 struct seq_server_site *ss;
3790 ss = mdt_seq_site(m);
3792 OBD_ALLOC_PTR(ss->ss_server_fld);
3793 if (ss->ss_server_fld == NULL)
3794 RETURN(rc = -ENOMEM);
3796 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3799 OBD_FREE_PTR(ss->ss_server_fld);
3800 ss->ss_server_fld = NULL;
3807 static void mdt_stack_pre_fini(const struct lu_env *env,
3808 struct mdt_device *m, struct lu_device *top)
3810 struct lustre_cfg_bufs *bufs;
3811 struct lustre_cfg *lcfg;
3812 struct mdt_thread_info *info;
3817 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3818 LASSERT(info != NULL);
3820 bufs = &info->mti_u.bufs;
3822 LASSERT(m->mdt_child_exp);
3823 LASSERT(m->mdt_child_exp->exp_obd);
3825 /* process cleanup, pass mdt obd name to get obd umount flags */
3826 /* XXX: this is needed because all layers are referenced by
3827 * objects (some of them are pinned by osd, for example *
3828 * the proper solution should be a model where object used
3829 * by osd only doesn't have mdt/mdd slices -bzzz */
3830 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3831 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3832 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3836 top->ld_ops->ldo_process_config(env, top, lcfg);
3837 lustre_cfg_free(lcfg);
3841 static void mdt_stack_fini(const struct lu_env *env,
3842 struct mdt_device *m, struct lu_device *top)
3844 struct obd_device *obd = mdt2obd_dev(m);
3845 struct lustre_cfg_bufs *bufs;
3846 struct lustre_cfg *lcfg;
3847 struct mdt_thread_info *info;
3851 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3852 LASSERT(info != NULL);
3854 lu_dev_del_linkage(top->ld_site, top);
3856 lu_site_purge(env, top->ld_site, -1);
3858 bufs = &info->mti_u.bufs;
3859 /* process cleanup, pass mdt obd name to get obd umount flags */
3860 /* another purpose is to let all layers to release their objects */
3861 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3866 lustre_cfg_bufs_set_string(bufs, 1, flags);
3867 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3872 top->ld_ops->ldo_process_config(env, top, lcfg);
3873 lustre_cfg_free(lcfg);
3875 lu_site_purge(env, top->ld_site, -1);
3877 m->mdt_child = NULL;
3878 m->mdt_bottom = NULL;
3880 obd_disconnect(m->mdt_child_exp);
3881 m->mdt_child_exp = NULL;
3883 obd_disconnect(m->mdt_bottom_exp);
3884 m->mdt_child_exp = NULL;
3887 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3888 const char *next, struct obd_export **exp)
3890 struct obd_connect_data *data = NULL;
3891 struct obd_device *obd;
3895 OBD_ALLOC_PTR(data);
3897 GOTO(out, rc = -ENOMEM);
3899 obd = class_name2obd(next);
3901 CERROR("%s: can't locate next device: %s\n",
3902 mdt_obd_name(m), next);
3903 GOTO(out, rc = -ENOTCONN);
3906 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3907 data->ocd_version = LUSTRE_VERSION_CODE;
3909 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3911 CERROR("%s: cannot connect to next dev %s (%d)\n",
3912 mdt_obd_name(m), next, rc);
3922 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3923 struct lustre_cfg *cfg)
3925 char *dev = lustre_cfg_string(cfg, 0);
3926 int rc, name_size, uuid_size;
3927 char *name, *uuid, *p;
3928 struct lustre_cfg_bufs *bufs;
3929 struct lustre_cfg *lcfg;
3930 struct obd_device *obd;
3931 struct lustre_profile *lprof;
3932 struct lu_site *site;
3935 /* in 1.8 we had the only device in the stack - MDS.
3936 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3937 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3938 * to generate names and setup MDT, MDD. MDT will be using
3939 * generated name to connect to MDD. for MDD the next device
3940 * will be LOD with name taken from so called "profile" which
3941 * is generated by mount_option line
3943 * 1.8 MGS generates config. commands like this:
3944 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3945 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3946 * 2.0 MGS generates config. commands like this:
3947 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3948 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3949 * 3:lustre-MDT0000-mdtlov 4:f
3951 * we generate MDD name from MDT one, just replacing T with D
3953 * after all the preparations, the logical equivalent will be
3954 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3955 * 3:lustre-MDT0000-mdtlov 4:f
3956 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3957 * 3:lustre-MDD0000 4:f
3959 * notice we build the stack from down to top: MDD first, then MDT */
3961 name_size = MAX_OBD_NAME;
3962 uuid_size = MAX_OBD_NAME;
3964 OBD_ALLOC(name, name_size);
3965 OBD_ALLOC(uuid, uuid_size);
3966 if (name == NULL || uuid == NULL)
3967 GOTO(cleanup_mem, rc = -ENOMEM);
3969 OBD_ALLOC_PTR(bufs);
3971 GOTO(cleanup_mem, rc = -ENOMEM);
3974 p = strstr(name, "-MDT");
3976 GOTO(free_bufs, rc = -ENOMEM);
3979 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3981 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3982 if (lprof == NULL || lprof->lp_dt == NULL) {
3983 CERROR("can't find the profile: %s\n",
3984 lustre_cfg_string(cfg, 0));
3985 GOTO(free_bufs, rc = -EINVAL);
3988 lustre_cfg_bufs_reset(bufs, name);
3989 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3990 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3991 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3993 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3995 GOTO(put_profile, rc = -ENOMEM);
3997 rc = class_attach(lcfg);
3999 GOTO(lcfg_cleanup, rc);
4001 obd = class_name2obd(name);
4003 CERROR("Can not find obd %s (%s in config)\n",
4004 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4005 GOTO(lcfg_cleanup, rc = -EINVAL);
4008 lustre_cfg_free(lcfg);
4010 lustre_cfg_bufs_reset(bufs, name);
4011 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4012 lustre_cfg_bufs_set_string(bufs, 2, dev);
4013 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4015 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4017 GOTO(class_detach, rc = -ENOMEM);
4019 rc = class_setup(obd, lcfg);
4021 GOTO(class_detach, rc);
4023 /* connect to MDD we just setup */
4024 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4026 GOTO(class_detach, rc);
4028 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4030 LASSERT(mdt_lu_site(mdt) == NULL);
4031 mdt->mdt_lu_dev.ld_site = site;
4032 site->ls_top_dev = &mdt->mdt_lu_dev;
4033 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4035 /* now connect to bottom OSD */
4036 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4037 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4039 GOTO(class_detach, rc);
4041 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4043 rc = lu_env_refill((struct lu_env *)env);
4045 CERROR("Failure to refill session: '%d'\n", rc);
4047 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4052 class_detach(obd, lcfg);
4054 lustre_cfg_free(lcfg);
4056 class_put_profile(lprof);
4061 OBD_FREE(name, name_size);
4063 OBD_FREE(uuid, uuid_size);
4067 /* setup quota master target on MDT0 */
4068 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4069 struct lustre_cfg *cfg)
4071 struct obd_device *obd;
4072 char *dev = lustre_cfg_string(cfg, 0);
4073 char *qmtname, *uuid, *p;
4074 struct lustre_cfg_bufs *bufs;
4075 struct lustre_cfg *lcfg;
4076 struct lustre_profile *lprof;
4077 struct obd_connect_data *data;
4081 LASSERT(mdt->mdt_qmt_exp == NULL);
4082 LASSERT(mdt->mdt_qmt_dev == NULL);
4084 /* quota master is on MDT0 only for now */
4085 if (mdt->mdt_seq_site.ss_node_id != 0)
4088 /* MGS generates config commands which look as follows:
4089 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4090 * 3:lustre-MDT0000-mdtlov 4:f
4092 * We generate the QMT name from the MDT one, just replacing MD with QM
4093 * after all the preparations, the logical equivalent will be:
4094 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4095 * 3:lustre-MDT0000-osd 4:f */
4096 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4097 OBD_ALLOC(uuid, UUID_MAX);
4098 OBD_ALLOC_PTR(bufs);
4099 OBD_ALLOC_PTR(data);
4100 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4101 GOTO(cleanup_mem, rc = -ENOMEM);
4103 strcpy(qmtname, dev);
4104 p = strstr(qmtname, "-MDT");
4106 GOTO(cleanup_mem, rc = -ENOMEM);
4107 /* replace MD with QM */
4111 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4113 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4114 if (lprof == NULL || lprof->lp_dt == NULL) {
4115 CERROR("can't find profile for %s\n",
4116 lustre_cfg_string(cfg, 0));
4117 GOTO(cleanup_mem, rc = -EINVAL);
4120 lustre_cfg_bufs_reset(bufs, qmtname);
4121 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4122 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4123 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4125 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4127 GOTO(put_profile, rc = -ENOMEM);
4129 rc = class_attach(lcfg);
4131 GOTO(lcfg_cleanup, rc);
4133 obd = class_name2obd(qmtname);
4135 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4136 lustre_cfg_string(cfg, 0));
4137 GOTO(lcfg_cleanup, rc = -EINVAL);
4140 lustre_cfg_free(lcfg);
4142 lustre_cfg_bufs_reset(bufs, qmtname);
4143 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4144 lustre_cfg_bufs_set_string(bufs, 2, dev);
4146 /* for quota, the next device should be the OSD device */
4147 lustre_cfg_bufs_set_string(bufs, 3,
4148 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4150 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4152 GOTO(class_detach, rc = -ENOMEM);
4154 rc = class_setup(obd, lcfg);
4156 GOTO(class_detach, rc);
4158 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4160 /* configure local quota objects */
4161 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4165 GOTO(class_cleanup, rc);
4167 /* connect to quota master target */
4168 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4169 data->ocd_version = LUSTRE_VERSION_CODE;
4170 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4173 CERROR("cannot connect to quota master device %s (%d)\n",
4175 GOTO(class_cleanup, rc);
4181 class_manual_cleanup(obd);
4182 mdt->mdt_qmt_dev = NULL;
4186 class_detach(obd, lcfg);
4188 lustre_cfg_free(lcfg);
4190 class_put_profile(lprof);
4195 OBD_FREE(qmtname, MAX_OBD_NAME);
4197 OBD_FREE(uuid, UUID_MAX);
4203 /* Shutdown quota master target associated with mdt */
4204 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4208 if (mdt->mdt_qmt_exp == NULL)
4210 LASSERT(mdt->mdt_qmt_dev != NULL);
4212 /* the qmt automatically shuts down when the mdt disconnects */
4213 obd_disconnect(mdt->mdt_qmt_exp);
4214 mdt->mdt_qmt_exp = NULL;
4215 mdt->mdt_qmt_dev = NULL;
4219 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4220 * for now. This will be removed along with converting rest of MDT code
4221 * to use tgt_session_info */
4222 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4224 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4227 rc = mdt_getxattr(info);
4229 mdt_thread_info_fini(info);
4233 static struct tgt_handler mdt_tgt_handlers[] = {
4234 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4235 0, MDS_CONNECT, mdt_tgt_connect,
4236 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4237 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4238 0, MDS_DISCONNECT, tgt_disconnect,
4239 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4240 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4241 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4242 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4243 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4244 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4245 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4246 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4248 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4249 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4250 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4251 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4252 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4253 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4254 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4255 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4257 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4258 mdt_hsm_ct_register),
4259 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4260 mdt_hsm_ct_unregister),
4261 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4263 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4265 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4266 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4268 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4273 static struct tgt_handler mdt_sec_ctx_ops[] = {
4274 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4275 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4276 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4279 static struct tgt_handler mdt_quota_ops[] = {
4280 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4283 static struct tgt_opc_slice mdt_common_slice[] = {
4285 .tos_opc_start = MDS_FIRST_OPC,
4286 .tos_opc_end = MDS_LAST_OPC,
4287 .tos_hs = mdt_tgt_handlers
4290 .tos_opc_start = OBD_FIRST_OPC,
4291 .tos_opc_end = OBD_LAST_OPC,
4292 .tos_hs = tgt_obd_handlers
4295 .tos_opc_start = LDLM_FIRST_OPC,
4296 .tos_opc_end = LDLM_LAST_OPC,
4297 .tos_hs = tgt_dlm_handlers
4300 .tos_opc_start = SEC_FIRST_OPC,
4301 .tos_opc_end = SEC_LAST_OPC,
4302 .tos_hs = mdt_sec_ctx_ops
4305 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4306 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4307 .tos_hs = tgt_out_handlers
4310 .tos_opc_start = FLD_FIRST_OPC,
4311 .tos_opc_end = FLD_LAST_OPC,
4312 .tos_hs = fld_handlers
4315 .tos_opc_start = SEQ_FIRST_OPC,
4316 .tos_opc_end = SEQ_LAST_OPC,
4317 .tos_hs = seq_handlers
4320 .tos_opc_start = QUOTA_DQACQ,
4321 .tos_opc_end = QUOTA_LAST_OPC,
4322 .tos_hs = mdt_quota_ops
4325 .tos_opc_start = LLOG_FIRST_OPC,
4326 .tos_opc_end = LLOG_LAST_OPC,
4327 .tos_hs = tgt_llog_handlers
4330 .tos_opc_start = LFSCK_FIRST_OPC,
4331 .tos_opc_end = LFSCK_LAST_OPC,
4332 .tos_hs = tgt_lfsck_handlers
4340 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4342 struct md_device *next = m->mdt_child;
4343 struct lu_device *d = &m->mdt_lu_dev;
4344 struct obd_device *obd = mdt2obd_dev(m);
4345 struct lfsck_stop stop;
4348 stop.ls_status = LS_PAUSED;
4350 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4352 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4353 target_recovery_fini(obd);
4354 ping_evictor_stop();
4356 if (m->mdt_opts.mo_coordinator)
4357 mdt_hsm_cdt_stop(m);
4359 mdt_hsm_cdt_fini(m);
4361 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4362 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4364 if (m->mdt_namespace != NULL)
4365 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4366 d->ld_obd->obd_force);
4368 obd_exports_barrier(obd);
4369 obd_zombie_barrier();
4373 tgt_fini(env, &m->mdt_lut);
4374 mdt_fs_cleanup(env, m);
4375 upcall_cache_cleanup(m->mdt_identity_cache);
4376 m->mdt_identity_cache = NULL;
4378 if (m->mdt_namespace != NULL) {
4379 ldlm_namespace_free_post(m->mdt_namespace);
4380 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4383 mdt_quota_fini(env, m);
4385 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4387 mdt_seq_fini(env, m);
4388 mdt_fld_fini(env, m);
4393 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4395 LASSERT(atomic_read(&d->ld_ref) == 0);
4397 server_put_mount(mdt_obd_name(m), true);
4402 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4404 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4405 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4407 struct mdt_thread_info *info;
4408 struct obd_device *obd;
4409 const char *dev = lustre_cfg_string(cfg, 0);
4410 const char *num = lustre_cfg_string(cfg, 2);
4411 struct lustre_mount_info *lmi = NULL;
4412 struct lustre_sb_info *lsi;
4414 struct seq_server_site *ss_site;
4415 const char *identity_upcall = "NONE";
4416 struct md_device *next;
4422 lu_device_init(&m->mdt_lu_dev, ldt);
4424 * Environment (env) might be missing mdt_thread_key values at that
4425 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4428 * Usually device allocation path doesn't use module key values, but
4429 * mdt has to do a lot of work here, so allocate key value.
4431 rc = lu_env_refill((struct lu_env *)env);
4435 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4436 LASSERT(info != NULL);
4438 obd = class_name2obd(dev);
4439 LASSERT(obd != NULL);
4441 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4442 m->mdt_opts.mo_evict_tgt_nids = 1;
4443 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4445 /* default is coordinator off, it is started through conf_param
4447 m->mdt_opts.mo_coordinator = 0;
4449 lmi = server_get_mount(dev);
4451 CERROR("Cannot get mount info for %s!\n", dev);
4454 lsi = s2lsi(lmi->lmi_sb);
4455 /* CMD is supported only in IAM mode */
4457 node_id = simple_strtol(num, NULL, 10);
4458 obd->u.obt.obt_magic = OBT_MAGIC;
4459 if (lsi->lsi_lmd != NULL &&
4460 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4461 m->mdt_skip_lfsck = 1;
4464 m->mdt_squash.rsi_uid = 0;
4465 m->mdt_squash.rsi_gid = 0;
4466 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4467 init_rwsem(&m->mdt_squash.rsi_sem);
4468 spin_lock_init(&m->mdt_osfs_lock);
4469 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4470 m->mdt_enable_remote_dir = 0;
4471 m->mdt_enable_remote_dir_gid = 0;
4473 atomic_set(&m->mdt_mds_mds_conns, 0);
4474 atomic_set(&m->mdt_async_commit_count, 0);
4476 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4477 m->mdt_lu_dev.ld_obd = obd;
4478 /* Set this lu_device to obd for error handling purposes. */
4479 obd->obd_lu_dev = &m->mdt_lu_dev;
4481 /* init the stack */
4482 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4484 CERROR("%s: Can't init device stack, rc %d\n",
4485 mdt_obd_name(m), rc);
4490 ss_site = mdt_seq_site(m);
4491 s->ld_seq_site = ss_site;
4494 /* set server index */
4495 ss_site->ss_node_id = node_id;
4497 /* failover is the default
4498 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4499 * assumed whose ss_node_id == 0 XXX
4501 obd->obd_replayable = 1;
4502 /* No connection accepted until configurations will finish */
4503 obd->obd_no_conn = 1;
4505 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4506 char *str = lustre_cfg_string(cfg, 4);
4507 if (strchr(str, 'n')) {
4508 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4509 obd->obd_replayable = 0;
4513 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4515 GOTO(err_fini_stack, rc);
4517 rc = mdt_seq_init(env, m);
4519 GOTO(err_fini_fld, rc);
4521 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4522 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4523 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4524 LDLM_NAMESPACE_SERVER,
4525 LDLM_NAMESPACE_GREEDY,
4527 if (m->mdt_namespace == NULL)
4528 GOTO(err_fini_seq, rc = -ENOMEM);
4530 m->mdt_namespace->ns_lvbp = m;
4531 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4533 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4534 /* set obd_namespace for compatibility with old code */
4535 obd->obd_namespace = m->mdt_namespace;
4537 rc = mdt_hsm_cdt_init(m);
4539 CERROR("%s: error initializing coordinator, rc %d\n",
4540 mdt_obd_name(m), rc);
4541 GOTO(err_free_ns, rc);
4544 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4545 OBD_FAIL_MDS_ALL_REQUEST_NET,
4546 OBD_FAIL_MDS_ALL_REPLY_NET);
4548 GOTO(err_free_hsm, rc);
4550 rc = mdt_fs_setup(env, m, obd, lsi);
4554 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4556 next = m->mdt_child;
4557 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4560 GOTO(err_fs_cleanup, rc);
4562 if (mntopts & MNTOPT_USERXATTR)
4563 m->mdt_opts.mo_user_xattr = 1;
4565 m->mdt_opts.mo_user_xattr = 0;
4567 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4569 GOTO(err_fs_cleanup, rc);
4571 if (mntopts & MNTOPT_ACL)
4572 m->mdt_opts.mo_acl = 1;
4574 m->mdt_opts.mo_acl = 0;
4576 /* XXX: to support suppgid for ACL, we enable identity_upcall
4577 * by default, otherwise, maybe got unexpected -EACCESS. */
4578 if (m->mdt_opts.mo_acl)
4579 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4581 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4583 &mdt_identity_upcall_cache_ops);
4584 if (IS_ERR(m->mdt_identity_cache)) {
4585 rc = PTR_ERR(m->mdt_identity_cache);
4586 m->mdt_identity_cache = NULL;
4587 GOTO(err_fs_cleanup, rc);
4590 rc = mdt_procfs_init(m, dev);
4592 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4593 GOTO(err_recovery, rc);
4596 rc = mdt_quota_init(env, m, cfg);
4598 GOTO(err_procfs, rc);
4600 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4601 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4602 "mdt_ldlm_client", m->mdt_ldlm_client);
4604 ping_evictor_start();
4606 /* recovery will be started upon mdt_prepare()
4607 * when the whole stack is complete and ready
4608 * to serve the requests */
4610 /* Reduce the initial timeout on an MDS because it doesn't need such
4611 * a long timeout as an OST does. Adaptive timeouts will adjust this
4612 * value appropriately. */
4613 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4614 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4620 target_recovery_fini(obd);
4621 upcall_cache_cleanup(m->mdt_identity_cache);
4622 m->mdt_identity_cache = NULL;
4624 mdt_fs_cleanup(env, m);
4626 tgt_fini(env, &m->mdt_lut);
4628 mdt_hsm_cdt_fini(m);
4630 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4631 obd->obd_namespace = m->mdt_namespace = NULL;
4633 mdt_seq_fini(env, m);
4635 mdt_fld_fini(env, m);
4637 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4640 server_put_mount(dev, true);
4644 /* For interoperability, the left element is old parameter, the right one
4645 * is the new version of the parameter, if some parameter is deprecated,
4646 * the new version should be set as NULL. */
4647 static struct cfg_interop_param mdt_interop_param[] = {
4648 { "mdt.group_upcall", NULL },
4649 { "mdt.quota_type", NULL },
4650 { "mdd.quota_type", NULL },
4651 { "mdt.rootsquash", "mdt.root_squash" },
4652 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4656 /* used by MGS to process specific configurations */
4657 static int mdt_process_config(const struct lu_env *env,
4658 struct lu_device *d, struct lustre_cfg *cfg)
4660 struct mdt_device *m = mdt_dev(d);
4661 struct md_device *md_next = m->mdt_child;
4662 struct lu_device *next = md2lu_dev(md_next);
4666 switch (cfg->lcfg_command) {
4668 struct obd_device *obd = d->ld_obd;
4670 /* For interoperability */
4671 struct cfg_interop_param *ptr = NULL;
4672 struct lustre_cfg *old_cfg = NULL;
4675 param = lustre_cfg_string(cfg, 1);
4676 if (param == NULL) {
4677 CERROR("param is empty\n");
4682 ptr = class_find_old_param(param, mdt_interop_param);
4684 if (ptr->new_param == NULL) {
4686 CWARN("For interoperability, skip this %s."
4687 " It is obsolete.\n", ptr->old_param);
4691 CWARN("Found old param %s, changed it to %s.\n",
4692 ptr->old_param, ptr->new_param);
4695 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4702 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4704 if (rc > 0 || rc == -ENOSYS) {
4705 /* is it an HSM var ? */
4706 rc = class_process_proc_param(PARAM_HSM,
4707 hsm_cdt_get_proc_vars(),
4709 if (rc > 0 || rc == -ENOSYS)
4710 /* we don't understand; pass it on */
4711 rc = next->ld_ops->ldo_process_config(env, next,
4715 if (old_cfg != NULL)
4716 lustre_cfg_free(cfg);
4721 /* others are passed further */
4722 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4728 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4729 const struct lu_object_header *hdr,
4730 struct lu_device *d)
4732 struct mdt_object *mo;
4736 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4738 struct lu_object *o;
4739 struct lu_object_header *h;
4742 h = &mo->mot_header;
4743 lu_object_header_init(h);
4744 lu_object_init(o, h, d);
4745 lu_object_add_top(h, o);
4746 o->lo_ops = &mdt_obj_ops;
4747 spin_lock_init(&mo->mot_write_lock);
4748 mutex_init(&mo->mot_lov_mutex);
4749 init_rwsem(&mo->mot_open_sem);
4755 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4756 const struct lu_object_conf *unused)
4758 struct mdt_device *d = mdt_dev(o->lo_dev);
4759 struct lu_device *under;
4760 struct lu_object *below;
4764 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4765 PFID(lu_object_fid(o)));
4767 under = &d->mdt_child->md_lu_dev;
4768 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4769 if (below != NULL) {
4770 lu_object_add(o, below);
4777 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4779 struct mdt_object *mo = mdt_obj(o);
4780 struct lu_object_header *h;
4784 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4785 PFID(lu_object_fid(o)));
4787 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4788 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4791 lu_object_header_fini(h);
4792 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4797 static int mdt_object_print(const struct lu_env *env, void *cookie,
4798 lu_printer_t p, const struct lu_object *o)
4800 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4802 return (*p)(env, cookie,
4803 LUSTRE_MDT_NAME"-object@%p(flags=%d, writecount=%d)",
4804 mdto, mdto->mot_flags, mdto->mot_write_count);
4807 static int mdt_prepare(const struct lu_env *env,
4808 struct lu_device *pdev,
4809 struct lu_device *cdev)
4811 struct mdt_device *mdt = mdt_dev(cdev);
4812 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4813 struct obd_device *obd = cdev->ld_obd;
4820 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4824 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4828 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4832 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4833 /* The LFSCK instance is registered just now, so it must be there when
4834 * register the namespace to such instance. */
4835 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4837 if (mdt->mdt_seq_site.ss_node_id == 0) {
4838 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4839 &mdt->mdt_md_root_fid);
4844 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4846 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4847 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4848 LASSERT(obd->obd_no_conn);
4849 spin_lock(&obd->obd_dev_lock);
4850 obd->obd_no_conn = 0;
4851 spin_unlock(&obd->obd_dev_lock);
4853 if (obd->obd_recovering == 0)
4854 mdt_postrecov(env, mdt);
4859 const struct lu_device_operations mdt_lu_ops = {
4860 .ldo_object_alloc = mdt_object_alloc,
4861 .ldo_process_config = mdt_process_config,
4862 .ldo_prepare = mdt_prepare,
4865 static const struct lu_object_operations mdt_obj_ops = {
4866 .loo_object_init = mdt_object_init,
4867 .loo_object_free = mdt_object_free,
4868 .loo_object_print = mdt_object_print
4871 static int mdt_obd_set_info_async(const struct lu_env *env,
4872 struct obd_export *exp,
4873 __u32 keylen, void *key,
4874 __u32 vallen, void *val,
4875 struct ptlrpc_request_set *set)
4881 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4882 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4890 * Match client and server connection feature flags.
4892 * Compute the compatibility flags for a connection request based on
4893 * features mutually supported by client and server.
4895 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4896 * must not be updated here, otherwise a partially initialized value may
4897 * be exposed. After the connection request is successfully processed,
4898 * the top-level MDT connect request handler atomically updates the export
4899 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4900 * reply. \see mdt_connect().
4902 * \param exp the obd_export associated with this client/target pair
4903 * \param mdt the target device for the connection
4904 * \param data stores data for this connect request
4907 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4908 * \retval -EBADE client and server feature requirements are incompatible
4910 static int mdt_connect_internal(struct obd_export *exp,
4911 struct mdt_device *mdt,
4912 struct obd_connect_data *data)
4914 LASSERT(data != NULL);
4916 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4917 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4919 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
4920 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
4921 CWARN("%s: client %s does not support ibits lock, either "
4922 "very old or an invalid client: flags "LPX64"\n",
4923 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
4924 data->ocd_connect_flags);
4928 if (!mdt->mdt_opts.mo_acl)
4929 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4931 if (!mdt->mdt_opts.mo_user_xattr)
4932 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4934 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4935 data->ocd_brw_size = min(data->ocd_brw_size,
4936 (__u32)MD_MAX_BRW_SIZE);
4937 if (data->ocd_brw_size == 0) {
4938 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4939 " ocd_version: %x ocd_grant: %d "
4940 "ocd_index: %u ocd_brw_size is "
4941 "unexpectedly zero, network data "
4942 "corruption? Refusing connection of this"
4945 exp->exp_client_uuid.uuid,
4946 exp, data->ocd_connect_flags, data->ocd_version,
4947 data->ocd_grant, data->ocd_index);
4952 /* NB: Disregard the rule against updating
4953 * exp_connect_data.ocd_connect_flags in this case, since
4954 * tgt_client_new() needs to know if this is a lightweight
4955 * connection, and it is safe to expose this flag before
4956 * connection processing completes. */
4957 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4958 spin_lock(&exp->exp_lock);
4959 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4960 spin_unlock(&exp->exp_lock);
4963 data->ocd_version = LUSTRE_VERSION_CODE;
4965 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4966 CWARN("%s: MDS requires FID support, but client not\n",
4971 if (OCD_HAS_FLAG(data, PINGLESS)) {
4972 if (ptlrpc_pinger_suppress_pings()) {
4973 spin_lock(&exp->exp_obd->obd_dev_lock);
4974 list_del_init(&exp->exp_obd_chain_timed);
4975 spin_unlock(&exp->exp_obd->obd_dev_lock);
4977 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4981 data->ocd_max_easize = mdt->mdt_max_ea_size;
4983 /* NB: Disregard the rule against updating
4984 * exp_connect_data.ocd_connect_flags in this case, since
4985 * tgt_client_new() needs to know if this is client supports
4986 * multiple modify RPCs, and it is safe to expose this flag before
4987 * connection processing completes. */
4988 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
4989 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
4990 spin_lock(&exp->exp_lock);
4991 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
4992 spin_unlock(&exp->exp_lock);
4998 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
4999 struct mdt_thread_info *info,
5000 struct mdt_file_data *mfd)
5002 struct lu_context ses;
5006 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5011 lu_context_enter(&ses);
5013 mdt_ucred(info)->uc_valid = UCRED_OLD;
5014 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5016 lu_context_exit(&ses);
5017 lu_context_fini(&ses);
5023 static int mdt_export_cleanup(struct obd_export *exp)
5025 struct list_head closing_list;
5026 struct mdt_export_data *med = &exp->exp_mdt_data;
5027 struct obd_device *obd = exp->exp_obd;
5028 struct mdt_device *mdt;
5029 struct mdt_thread_info *info;
5031 struct mdt_file_data *mfd, *n;
5035 INIT_LIST_HEAD(&closing_list);
5036 spin_lock(&med->med_open_lock);
5037 while (!list_empty(&med->med_open_head)) {
5038 struct list_head *tmp = med->med_open_head.next;
5039 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5041 /* Remove mfd handle so it can't be found again.
5042 * We are consuming the mfd_list reference here. */
5043 class_handle_unhash(&mfd->mfd_handle);
5044 list_move_tail(&mfd->mfd_list, &closing_list);
5046 spin_unlock(&med->med_open_lock);
5047 mdt = mdt_dev(obd->obd_lu_dev);
5048 LASSERT(mdt != NULL);
5050 rc = lu_env_init(&env, LCT_MD_THREAD);
5054 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5055 LASSERT(info != NULL);
5056 memset(info, 0, sizeof *info);
5057 info->mti_env = &env;
5058 info->mti_mdt = mdt;
5059 info->mti_exp = exp;
5061 if (!list_empty(&closing_list)) {
5062 struct md_attr *ma = &info->mti_attr;
5064 /* Close any open files (which may also cause orphan
5066 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5067 list_del_init(&mfd->mfd_list);
5068 ma->ma_need = ma->ma_valid = 0;
5070 /* This file is being closed due to an eviction, it
5071 * could have been modified and now dirty regarding to
5072 * HSM archive, check this!
5073 * The logic here is to mark a file dirty if there's a
5074 * chance it was dirtied before the client was evicted,
5075 * so that we don't have to wait for a release attempt
5076 * before finding out the file was actually dirty and
5077 * fail the release. Aggressively marking it dirty here
5078 * will cause the policy engine to attempt to
5079 * re-archive it; when rearchiving, we can compare the
5080 * current version to the HSM data_version and make the
5081 * archive request into a noop if it's not actually
5084 if (mfd->mfd_mode & FMODE_WRITE)
5085 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5087 /* Don't unlink orphan on failover umount, LU-184 */
5088 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5089 ma->ma_valid = MA_FLAGS;
5090 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5092 mdt_mfd_close(info, mfd);
5095 info->mti_mdt = NULL;
5096 /* cleanup client slot early */
5097 /* Do not erase record for recoverable client. */
5098 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5099 tgt_client_del(&env, exp);
5105 static inline void mdt_enable_slc(struct mdt_device *mdt)
5107 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5108 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5111 static inline void mdt_disable_slc(struct mdt_device *mdt)
5113 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5114 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5117 static int mdt_obd_disconnect(struct obd_export *exp)
5123 class_export_get(exp);
5125 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5126 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5127 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5129 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5130 mdt_disable_slc(mdt);
5133 rc = server_disconnect_export(exp);
5135 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5137 rc = mdt_export_cleanup(exp);
5138 nodemap_del_member(exp);
5139 class_export_put(exp);
5143 /* mds_connect copy */
5144 static int mdt_obd_connect(const struct lu_env *env,
5145 struct obd_export **exp, struct obd_device *obd,
5146 struct obd_uuid *cluuid,
5147 struct obd_connect_data *data,
5150 struct obd_export *lexp;
5151 struct lustre_handle conn = { 0 };
5152 struct mdt_device *mdt;
5154 lnet_nid_t *client_nid = localdata;
5157 LASSERT(env != NULL);
5158 if (!exp || !obd || !cluuid)
5161 mdt = mdt_dev(obd->obd_lu_dev);
5163 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5164 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5165 atomic_inc(&mdt->mdt_mds_mds_conns);
5166 mdt_enable_slc(mdt);
5170 * first, check whether the stack is ready to handle requests
5171 * XXX: probably not very appropriate method is used now
5172 * at some point we should find a better one
5174 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5175 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5176 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5177 rc = obd_get_info(env, mdt->mdt_child_exp,
5178 sizeof(KEY_OSP_CONNECTED),
5179 KEY_OSP_CONNECTED, NULL, NULL);
5182 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5185 rc = class_connect(&conn, obd, cluuid);
5189 lexp = class_conn2export(&conn);
5190 LASSERT(lexp != NULL);
5192 rc = nodemap_add_member(*client_nid, lexp);
5193 if (rc != 0 && rc != -EEXIST)
5196 rc = mdt_connect_internal(lexp, mdt, data);
5198 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5201 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5202 rc = tgt_client_new(env, lexp);
5204 mdt_export_stats_init(obd, lexp, localdata);
5208 class_disconnect(lexp);
5209 nodemap_del_member(lexp);
5218 static int mdt_obd_reconnect(const struct lu_env *env,
5219 struct obd_export *exp, struct obd_device *obd,
5220 struct obd_uuid *cluuid,
5221 struct obd_connect_data *data,
5224 lnet_nid_t *client_nid = localdata;
5228 if (exp == NULL || obd == NULL || cluuid == NULL)
5231 rc = nodemap_add_member(*client_nid, exp);
5232 if (rc != 0 && rc != -EEXIST)
5235 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5237 mdt_export_stats_init(obd, exp, localdata);
5239 nodemap_del_member(exp);
5244 /* FIXME: Can we avoid using these two interfaces? */
5245 static int mdt_init_export(struct obd_export *exp)
5247 struct mdt_export_data *med = &exp->exp_mdt_data;
5251 INIT_LIST_HEAD(&med->med_open_head);
5252 spin_lock_init(&med->med_open_lock);
5253 mutex_init(&med->med_idmap_mutex);
5254 med->med_idmap = NULL;
5255 spin_lock(&exp->exp_lock);
5256 exp->exp_connecting = 1;
5257 spin_unlock(&exp->exp_lock);
5259 /* self-export doesn't need client data and ldlm initialization */
5260 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5261 &exp->exp_client_uuid)))
5264 rc = tgt_client_alloc(exp);
5268 rc = ldlm_init_export(exp);
5275 tgt_client_free(exp);
5277 CERROR("%s: Failed to initialize export: rc = %d\n",
5278 exp->exp_obd->obd_name, rc);
5282 static int mdt_destroy_export(struct obd_export *exp)
5286 if (exp_connect_rmtclient(exp))
5287 mdt_cleanup_idmap(&exp->exp_mdt_data);
5289 target_destroy_export(exp);
5290 /* destroy can be called from failed obd_setup, so
5291 * checking uuid is safer than obd_self_export */
5292 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5293 &exp->exp_client_uuid)))
5296 ldlm_destroy_export(exp);
5297 tgt_client_free(exp);
5299 LASSERT(list_empty(&exp->exp_outstanding_replies));
5300 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5305 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5306 struct linkea_data *ldata)
5310 LASSERT(ldata->ld_buf->lb_buf != NULL);
5312 if (!mdt_object_exists(mdt_obj))
5315 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5316 ldata->ld_buf, XATTR_NAME_LINK);
5317 if (rc == -ERANGE) {
5318 /* Buf was too small, figure out what we need. */
5319 lu_buf_free(ldata->ld_buf);
5320 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5321 ldata->ld_buf, XATTR_NAME_LINK);
5324 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5325 if (ldata->ld_buf->lb_buf == NULL)
5327 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5328 ldata->ld_buf, XATTR_NAME_LINK);
5333 return linkea_init(ldata);
5337 * Given an MDT object, try to look up the full path to the object.
5338 * Part of the MDT layer implementation of lfs fid2path.
5340 * \param[in] info Per-thread common data shared by MDT level handlers.
5341 * \param[in] obj Object to do path lookup of
5342 * \param[in,out] fp User-provided struct to store path information
5344 * \retval 0 Lookup successful, path information stored in fp
5345 * \retval -EAGAIN Lookup failed, usually because object is being moved
5346 * \retval negative errno if there was a problem
5348 static int mdt_path_current(struct mdt_thread_info *info,
5349 struct mdt_object *obj,
5350 struct getinfo_fid2path *fp)
5352 struct mdt_device *mdt = info->mti_mdt;
5353 struct mdt_object *mdt_obj;
5354 struct link_ea_header *leh;
5355 struct link_ea_entry *lee;
5356 struct lu_name *tmpname = &info->mti_name;
5357 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5358 struct lu_buf *buf = &info->mti_big_buf;
5361 struct linkea_data ldata = { NULL };
5366 /* temp buffer for path element, the buffer will be finally freed
5367 * in mdt_thread_info_fini */
5368 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5369 if (buf->lb_buf == NULL)
5373 ptr = fp->gf_path + fp->gf_pathlen - 1;
5376 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5378 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5379 * so checking root_fid can only happen on MDT0. */
5380 while (!lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid)) {
5381 struct lu_buf lmv_buf;
5383 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5384 if (IS_ERR(mdt_obj))
5385 GOTO(out, rc = PTR_ERR(mdt_obj));
5387 if (!mdt_object_exists(mdt_obj)) {
5388 mdt_object_put(info->mti_env, mdt_obj);
5389 GOTO(out, rc = -ENOENT);
5392 if (mdt_object_remote(mdt_obj)) {
5393 mdt_object_put(info->mti_env, mdt_obj);
5394 GOTO(remote_out, rc = -EREMOTE);
5397 rc = mdt_links_read(info, mdt_obj, &ldata);
5399 mdt_object_put(info->mti_env, mdt_obj);
5404 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5405 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5406 /* If set, use link #linkno for path lookup, otherwise use
5407 link #0. Only do this for the final path element. */
5408 if (first && fp->gf_linkno < leh->leh_reccount) {
5410 for (count = 0; count < fp->gf_linkno; count++) {
5411 lee = (struct link_ea_entry *)
5412 ((char *)lee + reclen);
5413 linkea_entry_unpack(lee, &reclen, tmpname,
5416 if (fp->gf_linkno < leh->leh_reccount - 1)
5417 /* indicate to user there are more links */
5421 lmv_buf.lb_buf = info->mti_xattr_buf;
5422 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5423 /* Check if it is slave stripes */
5424 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5425 &lmv_buf, XATTR_NAME_LMV);
5426 mdt_object_put(info->mti_env, mdt_obj);
5428 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5430 /* For slave stripes, get its master */
5431 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5432 fp->gf_fid = *tmpfid;
5435 } else if (rc < 0 && rc != -ENODATA) {
5441 /* Pack the name in the end of the buffer */
5442 ptr -= tmpname->ln_namelen;
5443 if (ptr - 1 <= fp->gf_path)
5444 GOTO(out, rc = -EOVERFLOW);
5445 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5448 /* keep the last resolved fid to the client, so the
5449 * client will build the left path on another MDT for
5451 fp->gf_fid = *tmpfid;
5457 ptr++; /* skip leading / */
5458 memmove(fp->gf_path, ptr, fp->gf_path + fp->gf_pathlen - ptr);
5465 * Given an MDT object, use mdt_path_current to get the path.
5466 * Essentially a wrapper to retry mdt_path_current a set number of times
5467 * if -EAGAIN is returned (usually because an object is being moved).
5469 * Part of the MDT layer implementation of lfs fid2path.
5471 * \param[in] info Per-thread common data shared by mdt level handlers.
5472 * \param[in] obj Object to do path lookup of
5473 * \param[in,out] fp User-provided struct for arguments and to store path
5476 * \retval 0 Lookup successful, path information stored in fp
5477 * \retval negative errno if there was a problem
5479 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5480 struct getinfo_fid2path *fp)
5482 struct mdt_device *mdt = info->mti_mdt;
5487 if (fp->gf_pathlen < 3)
5490 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5491 fp->gf_path[0] = '\0';
5495 /* Retry multiple times in case file is being moved */
5496 while (tries-- && rc == -EAGAIN)
5497 rc = mdt_path_current(info, obj, fp);
5503 * Get the full path of the provided FID, as of changelog record recno.
5505 * This checks sanity and looks up object for user provided FID
5506 * before calling the actual path lookup code.
5508 * Part of the MDT layer implementation of lfs fid2path.
5510 * \param[in] info Per-thread common data shared by mdt level handlers.
5511 * \param[in,out] fp User-provided struct for arguments and to store path
5514 * \retval 0 Lookup successful, path information and recno stored in fp
5515 * \retval -ENOENT, object does not exist
5516 * \retval negative errno if there was a problem
5518 static int mdt_fid2path(struct mdt_thread_info *info,
5519 struct getinfo_fid2path *fp)
5521 struct mdt_device *mdt = info->mti_mdt;
5522 struct mdt_object *obj;
5526 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5527 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5529 if (!fid_is_sane(&fp->gf_fid))
5532 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5533 CWARN("%s: "DFID" is invalid, sequence should be "
5534 ">= "LPX64"\n", mdt_obd_name(mdt),
5535 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5539 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5542 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5543 PFID(&fp->gf_fid), rc);
5547 if (mdt_object_remote(obj))
5549 else if (!mdt_object_exists(obj))
5555 mdt_object_put(info->mti_env, obj);
5556 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5557 PFID(&fp->gf_fid), rc);
5561 rc = mdt_path(info, obj, fp);
5563 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5564 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5566 mdt_object_put(info->mti_env, obj);
5571 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5572 void *val, int vallen)
5574 struct getinfo_fid2path *fpout, *fpin;
5577 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5580 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5581 lustre_swab_fid2path(fpin);
5583 memcpy(fpout, fpin, sizeof(*fpin));
5584 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5587 rc = mdt_fid2path(info, fpout);
5591 int mdt_get_info(struct tgt_session_info *tsi)
5601 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5603 CDEBUG(D_IOCTL, "No GETINFO key\n");
5604 RETURN(err_serious(-EFAULT));
5606 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5609 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5610 if (vallen == NULL) {
5611 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5612 tgt_name(tsi->tsi_tgt));
5613 RETURN(err_serious(-EFAULT));
5616 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5618 rc = req_capsule_server_pack(tsi->tsi_pill);
5620 RETURN(err_serious(rc));
5622 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5623 if (valout == NULL) {
5624 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5625 tgt_name(tsi->tsi_tgt));
5626 RETURN(err_serious(-EFAULT));
5629 if (KEY_IS(KEY_FID2PATH)) {
5630 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5632 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5633 mdt_thread_info_fini(info);
5640 /* Pass the ioc down */
5641 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5642 unsigned int cmd, int len, void *data)
5644 struct lu_context ioctl_session;
5645 struct md_device *next = mdt->mdt_child;
5649 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5652 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5653 lu_context_enter(&ioctl_session);
5654 env->le_ses = &ioctl_session;
5656 LASSERT(next->md_ops->mdo_iocontrol);
5657 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5659 lu_context_exit(&ioctl_session);
5660 lu_context_fini(&ioctl_session);
5664 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5666 struct obd_ioctl_data *data = karg;
5669 struct mdt_object *obj;
5670 struct mdt_lock_handle *lh;
5674 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5675 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5678 fid = (struct lu_fid *)data->ioc_inlbuf1;
5680 if (!fid_is_sane(fid))
5683 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5685 lh = &mti->mti_lh[MDT_LH_PARENT];
5686 mdt_lock_reg_init(lh, LCK_CR);
5688 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5690 RETURN(PTR_ERR(obj));
5692 if (mdt_object_remote(obj)) {
5695 * before calling version get the correct MDS should be
5696 * fid, this is error to find remote object here
5698 CERROR("nonlocal object "DFID"\n", PFID(fid));
5699 } else if (!mdt_object_exists(obj)) {
5700 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5703 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5704 *(__u64 *)data->ioc_inlbuf2 = version;
5707 mdt_object_unlock_put(mti, obj, lh, 1);
5711 /* ioctls on obd dev */
5712 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5713 void *karg, void __user *uarg)
5716 struct obd_device *obd = exp->exp_obd;
5717 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5718 struct dt_device *dt = mdt->mdt_bottom;
5722 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5723 rc = lu_env_init(&env, LCT_MD_THREAD);
5729 rc = mdt_device_sync(&env, mdt);
5731 case OBD_IOC_SET_READONLY:
5732 rc = dt->dd_ops->dt_ro(&env, dt);
5734 case OBD_IOC_ABORT_RECOVERY:
5735 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5736 obd->obd_abort_recovery = 1;
5737 target_stop_recovery_thread(obd);
5740 case OBD_IOC_CHANGELOG_REG:
5741 case OBD_IOC_CHANGELOG_DEREG:
5742 case OBD_IOC_CHANGELOG_CLEAR:
5743 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5745 case OBD_IOC_START_LFSCK: {
5746 struct md_device *next = mdt->mdt_child;
5747 struct obd_ioctl_data *data = karg;
5748 struct lfsck_start_param lsp;
5750 if (unlikely(data == NULL)) {
5755 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5756 lsp.lsp_index_valid = 0;
5757 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5760 case OBD_IOC_STOP_LFSCK: {
5761 struct md_device *next = mdt->mdt_child;
5762 struct obd_ioctl_data *data = karg;
5763 struct lfsck_stop stop;
5765 stop.ls_status = LS_STOPPED;
5766 /* Old lfsck utils may pass NULL @stop. */
5767 if (data->ioc_inlbuf1 == NULL)
5771 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5773 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5776 case OBD_IOC_GET_OBJ_VERSION: {
5777 struct mdt_thread_info *mti;
5778 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5779 memset(mti, 0, sizeof *mti);
5780 mti->mti_env = &env;
5784 rc = mdt_ioc_version_get(mti, karg);
5787 case OBD_IOC_CATLOGLIST: {
5788 struct mdt_thread_info *mti;
5790 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5791 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5792 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5793 &mti->mti_tmp_fid1);
5798 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5799 mdt_obd_name(mdt), cmd, rc);
5806 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5808 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5812 if (!mdt->mdt_skip_lfsck) {
5813 struct lfsck_start_param lsp;
5815 lsp.lsp_start = NULL;
5816 lsp.lsp_index_valid = 0;
5817 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5818 OBD_IOC_START_LFSCK,
5820 if (rc != 0 && rc != -EALREADY)
5821 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5822 mdt_obd_name(mdt), rc);
5825 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5829 static int mdt_obd_postrecov(struct obd_device *obd)
5834 rc = lu_env_init(&env, LCT_MD_THREAD);
5837 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5842 static struct obd_ops mdt_obd_device_ops = {
5843 .o_owner = THIS_MODULE,
5844 .o_set_info_async = mdt_obd_set_info_async,
5845 .o_connect = mdt_obd_connect,
5846 .o_reconnect = mdt_obd_reconnect,
5847 .o_disconnect = mdt_obd_disconnect,
5848 .o_init_export = mdt_init_export,
5849 .o_destroy_export = mdt_destroy_export,
5850 .o_iocontrol = mdt_iocontrol,
5851 .o_postrecov = mdt_obd_postrecov,
5854 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5855 struct lu_device *d)
5857 struct mdt_device *m = mdt_dev(d);
5864 static struct lu_device *mdt_device_free(const struct lu_env *env,
5865 struct lu_device *d)
5867 struct mdt_device *m = mdt_dev(d);
5870 lu_device_fini(&m->mdt_lu_dev);
5876 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5877 struct lu_device_type *t,
5878 struct lustre_cfg *cfg)
5880 struct lu_device *l;
5881 struct mdt_device *m;
5888 rc = mdt_init0(env, m, t, cfg);
5890 mdt_device_free(env, l);
5895 l = ERR_PTR(-ENOMEM);
5899 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5900 LU_KEY_INIT(mdt, struct mdt_thread_info);
5902 static void mdt_key_fini(const struct lu_context *ctx,
5903 struct lu_context_key *key, void* data)
5905 struct mdt_thread_info *info = data;
5907 if (info->mti_big_lmm) {
5908 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5909 info->mti_big_lmm = NULL;
5910 info->mti_big_lmmsize = 0;
5915 /* context key: mdt_thread_key */
5916 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5918 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5920 return lu_ucred(info->mti_env);
5923 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5925 return lu_ucred_check(info->mti_env);
5929 * Enable/disable COS (Commit On Sharing).
5931 * Set/Clear the COS flag in mdt options.
5933 * \param mdt mdt device
5934 * \param val 0 disables COS, other values enable COS
5936 void mdt_enable_cos(struct mdt_device *mdt, int val)
5941 mdt->mdt_opts.mo_cos = !!val;
5942 rc = lu_env_init(&env, LCT_LOCAL);
5943 if (unlikely(rc != 0)) {
5944 CWARN("%s: lu_env initialization failed, cannot "
5945 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
5948 mdt_device_sync(&env, mdt);
5953 * Check COS (Commit On Sharing) status.
5955 * Return COS flag status.
5957 * \param mdt mdt device
5959 int mdt_cos_is_enabled(struct mdt_device *mdt)
5961 return mdt->mdt_opts.mo_cos != 0;
5964 static struct lu_device_type_operations mdt_device_type_ops = {
5965 .ldto_device_alloc = mdt_device_alloc,
5966 .ldto_device_free = mdt_device_free,
5967 .ldto_device_fini = mdt_device_fini
5970 static struct lu_device_type mdt_device_type = {
5971 .ldt_tags = LU_DEVICE_MD,
5972 .ldt_name = LUSTRE_MDT_NAME,
5973 .ldt_ops = &mdt_device_type_ops,
5974 .ldt_ctx_tags = LCT_MD_THREAD
5977 static int __init mdt_init(void)
5981 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5982 FID_NOBRACE_LEN + 1);
5983 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5985 rc = lu_kmem_init(mdt_caches);
5989 rc = mds_mod_init();
5993 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
5994 LUSTRE_MDT_NAME, &mdt_device_type);
5999 lu_kmem_fini(mdt_caches);
6006 static void __exit mdt_exit(void)
6008 class_unregister_type(LUSTRE_MDT_NAME);
6010 lu_kmem_fini(mdt_caches);
6013 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6014 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6015 MODULE_VERSION(LUSTRE_VERSION_STRING);
6016 MODULE_LICENSE("GPL");
6018 module_init(mdt_init);
6019 module_exit(mdt_exit);