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 * struct OBD_{ALLOC,FREE}*()
55 #include <obd_support.h>
56 #include <lustre_ioctl.h>
57 /* struct ptlrpc_request */
58 #include <lustre_net.h>
59 /* struct obd_export */
60 #include <lustre_export.h>
61 /* struct obd_device */
64 #include <dt_object.h>
65 #include <lustre_mds.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
70 #include <lustre_quota.h>
71 #include <lustre_lfsck.h>
72 #include <lustre_nodemap.h>
74 static unsigned int max_mod_rpcs_per_client = 8;
75 CFS_MODULE_PARM(max_mod_rpcs_per_client, "i", uint, 0644,
76 "maximum number of modify RPCs in flight allowed per client");
78 mdl_mode_t mdt_mdl_lock_modes[] = {
79 [LCK_MINMODE] = MDL_MINMODE,
86 [LCK_GROUP] = MDL_GROUP
89 enum ldlm_mode mdt_dlm_lock_modes[] = {
90 [MDL_MINMODE] = LCK_MINMODE,
97 [MDL_GROUP] = LCK_GROUP
100 static struct mdt_device *mdt_dev(struct lu_device *d);
101 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
103 static const struct lu_object_operations mdt_obj_ops;
105 /* Slab for MDT object allocation */
106 static struct kmem_cache *mdt_object_kmem;
108 /* For HSM restore handles */
109 struct kmem_cache *mdt_hsm_cdt_kmem;
111 /* For HSM request handles */
112 struct kmem_cache *mdt_hsm_car_kmem;
114 static struct lu_kmem_descr mdt_caches[] = {
116 .ckd_cache = &mdt_object_kmem,
117 .ckd_name = "mdt_obj",
118 .ckd_size = sizeof(struct mdt_object)
121 .ckd_cache = &mdt_hsm_cdt_kmem,
122 .ckd_name = "mdt_cdt_restore_handle",
123 .ckd_size = sizeof(struct cdt_restore_handle)
126 .ckd_cache = &mdt_hsm_car_kmem,
127 .ckd_name = "mdt_cdt_agent_req",
128 .ckd_size = sizeof(struct cdt_agent_req)
135 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
139 return rep->lock_policy_res1 & op_flag;
142 void mdt_clear_disposition(struct mdt_thread_info *info,
143 struct ldlm_reply *rep, __u64 op_flag)
146 info->mti_opdata &= ~op_flag;
147 tgt_opdata_clear(info->mti_env, op_flag);
150 rep->lock_policy_res1 &= ~op_flag;
153 void mdt_set_disposition(struct mdt_thread_info *info,
154 struct ldlm_reply *rep, __u64 op_flag)
157 info->mti_opdata |= op_flag;
158 tgt_opdata_set(info->mti_env, op_flag);
161 rep->lock_policy_res1 |= op_flag;
164 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
166 lh->mlh_pdo_hash = 0;
167 lh->mlh_reg_mode = lm;
168 lh->mlh_rreg_mode = lm;
169 lh->mlh_type = MDT_REG_LOCK;
172 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
173 const struct lu_name *lname)
175 lh->mlh_reg_mode = lock_mode;
176 lh->mlh_rreg_mode = lock_mode;
177 lh->mlh_type = MDT_PDO_LOCK;
179 if (lu_name_is_valid(lname)) {
180 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
182 /* XXX Workaround for LU-2856
184 * Zero is a valid return value of full_name_hash, but
185 * several users of mlh_pdo_hash assume a non-zero
186 * hash value. We therefore map zero onto an
187 * arbitrary, but consistent value (1) to avoid
188 * problems further down the road. */
189 if (unlikely(lh->mlh_pdo_hash == 0))
190 lh->mlh_pdo_hash = 1;
192 lh->mlh_pdo_hash = 0;
196 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
197 struct mdt_lock_handle *lh)
203 * Any dir access needs couple of locks:
205 * 1) on part of dir we gonna take lookup/modify;
207 * 2) on whole dir to protect it from concurrent splitting and/or to
208 * flush client's cache for readdir().
210 * so, for a given mode and object this routine decides what lock mode
211 * to use for lock #2:
213 * 1) if caller's gonna lookup in dir then we need to protect dir from
214 * being splitted only - LCK_CR
216 * 2) if caller's gonna modify dir then we need to protect dir from
217 * being splitted and to flush cache - LCK_CW
219 * 3) if caller's gonna modify dir and that dir seems ready for
220 * splitting then we need to protect it from any type of access
221 * (lookup/modify/split) - LCK_EX --bzzz
224 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
225 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
228 * Ask underlaying level its opinion about preferable PDO lock mode
229 * having access type passed as regular lock mode:
231 * - MDL_MINMODE means that lower layer does not want to specify lock
234 * - MDL_NL means that no PDO lock should be taken. This is used in some
235 * cases. Say, for non-splittable directories no need to use PDO locks
238 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
239 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
241 if (mode != MDL_MINMODE) {
242 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
245 * Lower layer does not want to specify locking mode. We do it
246 * our selves. No special protection is needed, just flush
247 * client's cache on modification and allow concurrent
250 switch (lh->mlh_reg_mode) {
252 lh->mlh_pdo_mode = LCK_EX;
255 lh->mlh_pdo_mode = LCK_CR;
258 lh->mlh_pdo_mode = LCK_CW;
261 CERROR("Not expected lock type (0x%x)\n",
262 (int)lh->mlh_reg_mode);
267 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
271 static int mdt_getstatus(struct tgt_session_info *tsi)
273 struct mdt_thread_info *info = tsi2mdt_info(tsi);
274 struct mdt_device *mdt = info->mti_mdt;
275 struct mdt_body *repbody;
280 rc = mdt_check_ucred(info);
282 GOTO(out, rc = err_serious(rc));
284 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
285 GOTO(out, rc = err_serious(-ENOMEM));
287 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
288 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
289 repbody->mbo_valid |= OBD_MD_FLID;
293 mdt_thread_info_fini(info);
297 static int mdt_statfs(struct tgt_session_info *tsi)
299 struct ptlrpc_request *req = tgt_ses_req(tsi);
300 struct mdt_thread_info *info = tsi2mdt_info(tsi);
301 struct md_device *next = info->mti_mdt->mdt_child;
302 struct ptlrpc_service_part *svcpt;
303 struct obd_statfs *osfs;
308 svcpt = req->rq_rqbd->rqbd_svcpt;
310 /* This will trigger a watchdog timeout */
311 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
312 (MDT_SERVICE_WATCHDOG_FACTOR *
313 at_get(&svcpt->scp_at_estimate)) + 1);
315 rc = mdt_check_ucred(info);
317 GOTO(out, rc = err_serious(rc));
319 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
320 GOTO(out, rc = err_serious(-ENOMEM));
322 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
324 GOTO(out, rc = -EPROTO);
326 /** statfs information are cached in the mdt_device */
327 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
328 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
329 /** statfs data is too old, get up-to-date one */
330 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
333 spin_lock(&info->mti_mdt->mdt_osfs_lock);
334 info->mti_mdt->mdt_osfs = *osfs;
335 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
336 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
338 /** use cached statfs data */
339 spin_lock(&info->mti_mdt->mdt_osfs_lock);
340 *osfs = info->mti_mdt->mdt_osfs;
341 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
345 mdt_counter_incr(req, LPROC_MDT_STATFS);
347 mdt_thread_info_fini(info);
351 #ifdef CONFIG_FS_POSIX_ACL
353 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
355 * \param info thread info object
356 * \param repbody reply to pack ACLs into
357 * \param o mdt object of file to examine
358 * \param nodemap nodemap of client to reply to
360 * \retval -errno error getting or parsing ACL from disk
362 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
363 struct mdt_object *o, struct lu_nodemap *nodemap)
365 const struct lu_env *env = info->mti_env;
366 struct md_object *next = mdt_object_child(o);
367 struct lu_buf *buf = &info->mti_buf;
370 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
371 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
373 if (buf->lb_len == 0)
376 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
378 if (rc == -ENODATA) {
379 repbody->mbo_aclsize = 0;
380 repbody->mbo_valid |= OBD_MD_FLACL;
382 } else if (rc == -EOPNOTSUPP) {
385 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
386 mdt_obd_name(info->mti_mdt),
387 PFID(mdt_object_fid(o)), rc);
390 rc = nodemap_map_acl(nodemap, buf->lb_buf,
391 rc, NODEMAP_FS_TO_CLIENT);
392 /* if all ACLs mapped out, rc is still >= 0 */
394 CERROR("%s: nodemap_map_acl unable to parse "DFID
395 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
396 PFID(mdt_object_fid(o)), rc);
398 repbody->mbo_aclsize = rc;
399 repbody->mbo_valid |= OBD_MD_FLACL;
407 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
408 const struct lu_attr *attr, const struct lu_fid *fid)
410 struct md_attr *ma = &info->mti_attr;
411 struct obd_export *exp = info->mti_exp;
412 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
414 LASSERT(ma->ma_valid & MA_INODE);
416 if (attr->la_valid & LA_ATIME) {
417 b->mbo_atime = attr->la_atime;
418 b->mbo_valid |= OBD_MD_FLATIME;
420 if (attr->la_valid & LA_MTIME) {
421 b->mbo_mtime = attr->la_mtime;
422 b->mbo_valid |= OBD_MD_FLMTIME;
424 if (attr->la_valid & LA_CTIME) {
425 b->mbo_ctime = attr->la_ctime;
426 b->mbo_valid |= OBD_MD_FLCTIME;
428 if (attr->la_valid & LA_FLAGS) {
429 b->mbo_flags = attr->la_flags;
430 b->mbo_valid |= OBD_MD_FLFLAGS;
432 if (attr->la_valid & LA_NLINK) {
433 b->mbo_nlink = attr->la_nlink;
434 b->mbo_valid |= OBD_MD_FLNLINK;
436 if (attr->la_valid & LA_UID) {
437 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
438 NODEMAP_FS_TO_CLIENT,
440 b->mbo_valid |= OBD_MD_FLUID;
442 if (attr->la_valid & LA_GID) {
443 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
444 NODEMAP_FS_TO_CLIENT,
446 b->mbo_valid |= OBD_MD_FLGID;
448 b->mbo_mode = attr->la_mode;
449 if (attr->la_valid & LA_MODE)
450 b->mbo_valid |= OBD_MD_FLMODE;
451 if (attr->la_valid & LA_TYPE)
452 b->mbo_valid |= OBD_MD_FLTYPE;
456 b->mbo_valid |= OBD_MD_FLID;
457 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid="LPX64"\n",
458 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
462 mdt_body_reverse_idmap(info, b);
464 if (!(attr->la_valid & LA_TYPE))
467 b->mbo_rdev = attr->la_rdev;
468 b->mbo_size = attr->la_size;
469 b->mbo_blocks = attr->la_blocks;
471 if (!S_ISREG(attr->la_mode)) {
472 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
473 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
474 /* means no objects are allocated on osts. */
475 LASSERT(!(ma->ma_valid & MA_LOV));
476 /* just ignore blocks occupied by extend attributes on MDS */
478 /* if no object is allocated on osts, the size on mds is valid.
480 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
481 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
482 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
483 /* A released file stores its size on MDS. */
484 /* But return 1 block for released file, unless tools like tar
485 * will consider it fully sparse. (LU-3864)
487 if (unlikely(b->mbo_size == 0))
491 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
494 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
495 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
496 PFID(fid), (unsigned long long)b->mbo_size);
499 static inline int mdt_body_has_lov(const struct lu_attr *la,
500 const struct mdt_body *body)
502 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
503 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
506 void mdt_client_compatibility(struct mdt_thread_info *info)
508 struct mdt_body *body;
509 struct ptlrpc_request *req = mdt_info_req(info);
510 struct obd_export *exp = req->rq_export;
511 struct md_attr *ma = &info->mti_attr;
512 struct lu_attr *la = &ma->ma_attr;
515 if (exp_connect_layout(exp))
516 /* the client can deal with 16-bit lmm_stripe_count */
519 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
521 if (!mdt_body_has_lov(la, body))
524 /* now we have a reply with a lov for a client not compatible with the
525 * layout lock so we have to clean the layout generation number */
526 if (S_ISREG(la->la_mode))
527 ma->ma_lmm->lmm_layout_gen = 0;
531 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
532 struct mdt_object *o)
534 const struct lu_env *env = info->mti_env;
537 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
546 /* Is it a directory? Let's check for the LMV as well */
547 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
548 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
552 rc2 = mo_xattr_get(env, mdt_object_child(o),
554 XATTR_NAME_DEFAULT_LMV);
556 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
564 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
567 const struct lu_env *env = info->mti_env;
571 LASSERT(info->mti_big_lmm_used == 0);
572 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
576 /* big_lmm may need to be grown */
577 if (info->mti_big_lmmsize < rc) {
578 int size = size_roundup_power2(rc);
580 if (info->mti_big_lmmsize > 0) {
581 /* free old buffer */
582 LASSERT(info->mti_big_lmm);
583 OBD_FREE_LARGE(info->mti_big_lmm,
584 info->mti_big_lmmsize);
585 info->mti_big_lmm = NULL;
586 info->mti_big_lmmsize = 0;
589 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
590 if (info->mti_big_lmm == NULL)
592 info->mti_big_lmmsize = size;
594 LASSERT(info->mti_big_lmmsize >= rc);
596 info->mti_buf.lb_buf = info->mti_big_lmm;
597 info->mti_buf.lb_len = info->mti_big_lmmsize;
598 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
603 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
604 struct md_attr *ma, const char *name)
606 struct md_object *next = mdt_object_child(o);
607 struct lu_buf *buf = &info->mti_buf;
610 if (strcmp(name, XATTR_NAME_LOV) == 0) {
611 buf->lb_buf = ma->ma_lmm;
612 buf->lb_len = ma->ma_lmm_size;
613 LASSERT(!(ma->ma_valid & MA_LOV));
614 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
615 buf->lb_buf = ma->ma_lmv;
616 buf->lb_len = ma->ma_lmv_size;
617 LASSERT(!(ma->ma_valid & MA_LMV));
618 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
619 buf->lb_buf = ma->ma_lmv;
620 buf->lb_len = ma->ma_lmv_size;
621 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
626 rc = mo_xattr_get(info->mti_env, next, buf, name);
630 if (strcmp(name, XATTR_NAME_LOV) == 0) {
631 if (info->mti_big_lmm_used)
632 ma->ma_lmm = info->mti_big_lmm;
634 /* NOT return LOV EA with hole to old client. */
635 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
636 LOV_PATTERN_F_HOLE) &&
637 !(exp_connect_flags(info->mti_exp) &
638 OBD_CONNECT_LFSCK)) {
641 ma->ma_lmm_size = rc;
642 ma->ma_valid |= MA_LOV;
644 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
645 if (info->mti_big_lmm_used)
646 ma->ma_lmv = info->mti_big_lmm;
648 ma->ma_lmv_size = rc;
649 ma->ma_valid |= MA_LMV;
650 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
651 ma->ma_lmv_size = rc;
652 ma->ma_valid |= MA_LMV_DEF;
655 /* Update mdt_max_mdsize so all clients will be aware that */
656 if (info->mti_mdt->mdt_max_mdsize < rc)
657 info->mti_mdt->mdt_max_mdsize = rc;
660 } else if (rc == -ENODATA) {
663 } else if (rc == -ERANGE) {
664 /* Default LMV has fixed size, so it must be able to fit
665 * in the original buffer */
666 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
668 rc = mdt_big_xattr_get(info, o, name);
670 info->mti_big_lmm_used = 1;
678 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
679 struct mdt_object *o, struct lu_fid *pfid)
681 struct lu_buf *buf = &info->mti_buf;
682 struct link_ea_header *leh;
683 struct link_ea_entry *lee;
687 buf->lb_buf = info->mti_big_lmm;
688 buf->lb_len = info->mti_big_lmmsize;
689 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
690 buf, XATTR_NAME_LINK);
691 /* ignore errors, MA_PFID won't be set and it is
692 * up to the caller to treat this as an error */
693 if (rc == -ERANGE || buf->lb_len == 0) {
694 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
695 buf->lb_buf = info->mti_big_lmm;
696 buf->lb_len = info->mti_big_lmmsize;
701 if (rc < sizeof(*leh)) {
702 CERROR("short LinkEA on "DFID": rc = %d\n",
703 PFID(mdt_object_fid(o)), rc);
707 leh = (struct link_ea_header *) buf->lb_buf;
708 lee = (struct link_ea_entry *)(leh + 1);
709 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
710 leh->leh_magic = LINK_EA_MAGIC;
711 leh->leh_reccount = __swab32(leh->leh_reccount);
712 leh->leh_len = __swab64(leh->leh_len);
714 if (leh->leh_magic != LINK_EA_MAGIC)
716 if (leh->leh_reccount == 0)
719 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
720 fid_be_to_cpu(pfid, pfid);
725 int mdt_attr_get_complex(struct mdt_thread_info *info,
726 struct mdt_object *o, struct md_attr *ma)
728 const struct lu_env *env = info->mti_env;
729 struct md_object *next = mdt_object_child(o);
730 struct lu_buf *buf = &info->mti_buf;
731 int need = ma->ma_need;
738 if (mdt_object_exists(o) == 0)
739 GOTO(out, rc = -ENOENT);
740 mode = lu_object_attr(&next->mo_lu);
742 if (need & MA_INODE) {
743 ma->ma_need = MA_INODE;
744 rc = mo_attr_get(env, next, ma);
747 ma->ma_valid |= MA_INODE;
750 if (need & MA_PFID) {
751 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
753 ma->ma_valid |= MA_PFID;
754 /* ignore this error, parent fid is not mandatory */
758 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
759 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
764 if (need & MA_LMV && S_ISDIR(mode)) {
765 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
770 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
771 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
776 if (need & MA_HSM && S_ISREG(mode)) {
777 buf->lb_buf = info->mti_xattr_buf;
778 buf->lb_len = sizeof(info->mti_xattr_buf);
779 CLASSERT(sizeof(struct hsm_attrs) <=
780 sizeof(info->mti_xattr_buf));
781 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
782 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
784 ma->ma_valid |= MA_HSM;
785 else if (rc2 < 0 && rc2 != -ENODATA)
789 #ifdef CONFIG_FS_POSIX_ACL
790 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
791 buf->lb_buf = ma->ma_acl;
792 buf->lb_len = ma->ma_acl_size;
793 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
795 ma->ma_acl_size = rc2;
796 ma->ma_valid |= MA_ACL_DEF;
797 } else if (rc2 == -ENODATA) {
806 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
807 rc, ma->ma_valid, ma->ma_lmm);
811 static int mdt_getattr_internal(struct mdt_thread_info *info,
812 struct mdt_object *o, int ma_need)
814 struct md_object *next = mdt_object_child(o);
815 const struct mdt_body *reqbody = info->mti_body;
816 struct ptlrpc_request *req = mdt_info_req(info);
817 struct md_attr *ma = &info->mti_attr;
818 struct lu_attr *la = &ma->ma_attr;
819 struct req_capsule *pill = info->mti_pill;
820 const struct lu_env *env = info->mti_env;
821 struct mdt_body *repbody;
822 struct lu_buf *buffer = &info->mti_buf;
823 struct obd_export *exp = info->mti_exp;
824 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
829 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
830 RETURN(err_serious(-ENOMEM));
832 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
836 if (mdt_object_remote(o)) {
837 /* This object is located on remote node.*/
838 /* Return -ENOTSUPP for old client */
839 if (!mdt_is_dne_client(req->rq_export))
840 GOTO(out, rc = -ENOTSUPP);
842 repbody->mbo_fid1 = *mdt_object_fid(o);
843 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
847 if (reqbody->mbo_eadatasize > 0) {
848 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
849 if (buffer->lb_buf == NULL)
850 GOTO(out, rc = -EPROTO);
851 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
854 buffer->lb_buf = NULL;
856 ma_need &= ~(MA_LOV | MA_LMV);
857 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
858 mdt_obd_name(info->mti_mdt),
859 req->rq_export->exp_client_uuid.uuid);
862 /* If it is dir object and client require MEA, then we got MEA */
863 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
864 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
865 /* Assumption: MDT_MD size is enough for lmv size. */
866 ma->ma_lmv = buffer->lb_buf;
867 ma->ma_lmv_size = buffer->lb_len;
868 ma->ma_need = MA_INODE;
869 if (ma->ma_lmv_size > 0) {
870 if (reqbody->mbo_valid & OBD_MD_MEA)
871 ma->ma_need |= MA_LMV;
872 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
873 ma->ma_need |= MA_LMV_DEF;
876 ma->ma_lmm = buffer->lb_buf;
877 ma->ma_lmm_size = buffer->lb_len;
878 ma->ma_need = MA_INODE | MA_HSM;
879 if (ma->ma_lmm_size > 0)
880 ma->ma_need |= MA_LOV;
883 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
884 reqbody->mbo_valid & OBD_MD_FLDIREA &&
885 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
886 /* get default stripe info for this dir. */
887 ma->ma_need |= MA_LOV_DEF;
889 ma->ma_need |= ma_need;
891 rc = mdt_attr_get_complex(info, o, ma);
893 CERROR("%s: getattr error for "DFID": rc = %d\n",
894 mdt_obd_name(info->mti_mdt),
895 PFID(mdt_object_fid(o)), rc);
899 /* if file is released, check if a restore is running */
900 if (ma->ma_valid & MA_HSM) {
901 repbody->mbo_valid |= OBD_MD_TSTATE;
902 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
903 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
904 repbody->mbo_t_state = MS_RESTORE;
907 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
909 /* the Lustre protocol supposes to return default striping
910 * on the user-visible root if explicitly requested */
911 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
912 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
913 struct lu_fid rootfid;
914 struct mdt_object *root;
915 struct mdt_device *mdt = info->mti_mdt;
917 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
920 root = mdt_object_find(env, mdt, &rootfid);
922 RETURN(PTR_ERR(root));
923 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
924 mdt_object_put(info->mti_env, root);
926 CERROR("%s: getattr error for "DFID": rc = %d\n",
927 mdt_obd_name(info->mti_mdt),
928 PFID(mdt_object_fid(o)), rc);
933 if (likely(ma->ma_valid & MA_INODE))
934 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
938 if (mdt_body_has_lov(la, reqbody)) {
939 if (ma->ma_valid & MA_LOV) {
940 LASSERT(ma->ma_lmm_size);
941 repbody->mbo_eadatasize = ma->ma_lmm_size;
942 if (S_ISDIR(la->la_mode))
943 repbody->mbo_valid |= OBD_MD_FLDIREA;
945 repbody->mbo_valid |= OBD_MD_FLEASIZE;
946 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
948 if (ma->ma_valid & MA_LMV) {
949 /* Return -ENOTSUPP for old client */
950 if (!mdt_is_striped_client(req->rq_export))
953 LASSERT(S_ISDIR(la->la_mode));
954 mdt_dump_lmv(D_INFO, ma->ma_lmv);
955 repbody->mbo_eadatasize = ma->ma_lmv_size;
956 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
958 if (ma->ma_valid & MA_LMV_DEF) {
959 /* Return -ENOTSUPP for old client */
960 if (!mdt_is_striped_client(req->rq_export))
962 LASSERT(S_ISDIR(la->la_mode));
963 repbody->mbo_eadatasize = ma->ma_lmv_size;
964 repbody->mbo_valid |= (OBD_MD_FLDIREA |
967 } else if (S_ISLNK(la->la_mode) &&
968 reqbody->mbo_valid & OBD_MD_LINKNAME) {
969 buffer->lb_buf = ma->ma_lmm;
970 /* eadatasize from client includes NULL-terminator, so
971 * there is no need to read it */
972 buffer->lb_len = reqbody->mbo_eadatasize - 1;
973 rc = mo_readlink(env, next, buffer);
974 if (unlikely(rc <= 0)) {
975 CERROR("%s: readlink failed for "DFID": rc = %d\n",
976 mdt_obd_name(info->mti_mdt),
977 PFID(mdt_object_fid(o)), rc);
980 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
982 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
984 repbody->mbo_valid |= OBD_MD_LINKNAME;
985 /* we need to report back size with NULL-terminator
986 * because client expects that */
987 repbody->mbo_eadatasize = rc + 1;
988 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
989 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
990 "on "DFID ", expected %d\n",
991 mdt_obd_name(info->mti_mdt),
992 rc, PFID(mdt_object_fid(o)),
993 reqbody->mbo_eadatasize - 1);
995 ((char *)ma->ma_lmm)[rc] = 0;
997 /* If the total CDEBUG() size is larger than a page, it
998 * will print a warning to the console, avoid this by
999 * printing just the last part of the symlink. */
1000 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1001 print_limit < rc ? "..." : "", print_limit,
1002 (char *)ma->ma_lmm + rc - print_limit, rc);
1007 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1008 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1009 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1010 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1011 repbody->mbo_max_mdsize);
1014 if (exp_connect_rmtclient(info->mti_exp) &&
1015 reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
1016 void *buf = req_capsule_server_get(pill, &RMF_ACL);
1018 /* mdt_getattr_lock only */
1019 rc = mdt_pack_remote_perm(info, o, buf);
1021 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
1022 repbody->mbo_aclsize = 0;
1025 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
1026 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
1029 #ifdef CONFIG_FS_POSIX_ACL
1030 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1031 (reqbody->mbo_valid & OBD_MD_FLACL))
1032 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1037 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1042 static int mdt_getattr(struct tgt_session_info *tsi)
1044 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1045 struct mdt_object *obj = info->mti_object;
1046 struct req_capsule *pill = info->mti_pill;
1047 struct mdt_body *reqbody;
1048 struct mdt_body *repbody;
1052 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1054 LASSERT(obj != NULL);
1055 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1057 /* Unlike intent case where we need to pre-fill out buffers early on
1058 * in intent policy for ldlm reasons, here we can have a much better
1059 * guess at EA size by just reading it from disk.
1060 * Exceptions are readdir and (missing) directory striping */
1062 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1063 /* No easy way to know how long is the symlink, but it cannot
1064 * be more than PATH_MAX, so we allocate +1 */
1067 /* A special case for fs ROOT: getattr there might fetch
1068 * default EA for entire fs, not just for this dir!
1070 } else if (lu_fid_eq(mdt_object_fid(obj),
1071 &info->mti_mdt->mdt_md_root_fid) &&
1072 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1073 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1075 /* Should the default strping be bigger, mdt_fix_reply
1076 * will reallocate */
1077 rc = DEF_REP_MD_SIZE;
1079 /* Read the actual EA size from disk */
1080 rc = mdt_attr_get_eabuf_size(info, obj);
1084 GOTO(out, rc = err_serious(rc));
1086 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1088 rc = req_capsule_server_pack(pill);
1089 if (unlikely(rc != 0))
1090 GOTO(out, rc = err_serious(rc));
1092 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1093 LASSERT(repbody != NULL);
1094 repbody->mbo_eadatasize = 0;
1095 repbody->mbo_aclsize = 0;
1097 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1098 rc = mdt_init_ucred(info, reqbody);
1100 rc = mdt_check_ucred(info);
1102 GOTO(out_shrink, rc);
1104 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1106 rc = mdt_getattr_internal(info, obj, 0);
1107 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1108 mdt_exit_ucred(info);
1111 mdt_client_compatibility(info);
1112 rc2 = mdt_fix_reply(info);
1116 mdt_thread_info_fini(info);
1121 * Exchange MOF_LOV_CREATED flags between two objects after a
1122 * layout swap. No assumption is made on whether o1 or o2 have
1123 * created objects or not.
1125 * \param[in,out] o1 First swap layout object
1126 * \param[in,out] o2 Second swap layout object
1128 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1132 mutex_lock(&o1->mot_lov_mutex);
1133 mutex_lock(&o2->mot_lov_mutex);
1135 o1_flags = o1->mot_flags;
1136 o1->mot_flags = (o1->mot_flags & ~MOF_LOV_CREATED) |
1137 (o2->mot_flags & MOF_LOV_CREATED);
1139 o2->mot_flags = (o2->mot_flags & ~MOF_LOV_CREATED) |
1140 (o1_flags & MOF_LOV_CREATED);
1142 mutex_unlock(&o2->mot_lov_mutex);
1143 mutex_unlock(&o1->mot_lov_mutex);
1146 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1148 struct mdt_thread_info *info;
1149 struct ptlrpc_request *req = tgt_ses_req(tsi);
1150 struct obd_export *exp = req->rq_export;
1151 struct mdt_object *o1, *o2, *o;
1152 struct mdt_lock_handle *lh1, *lh2;
1153 struct mdc_swap_layouts *msl;
1157 /* client does not support layout lock, so layout swaping
1159 * FIXME: there is a problem for old clients which don't support
1160 * layout lock yet. If those clients have already opened the file
1161 * they won't be notified at all so that old layout may still be
1162 * used to do IO. This can be fixed after file release is landed by
1163 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1164 if (!exp_connect_layout(exp))
1165 RETURN(-EOPNOTSUPP);
1167 info = tsi2mdt_info(tsi);
1169 if (info->mti_dlm_req != NULL)
1170 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1172 o1 = info->mti_object;
1173 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1174 &info->mti_body->mbo_fid2);
1176 GOTO(out, rc = PTR_ERR(o));
1178 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1179 GOTO(put, rc = -ENOENT);
1181 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1182 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1188 /* permission check. Make sure the calling process having permission
1189 * to write both files. */
1190 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1195 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1200 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1202 GOTO(put, rc = -EPROTO);
1204 lh1 = &info->mti_lh[MDT_LH_NEW];
1205 mdt_lock_reg_init(lh1, LCK_EX);
1206 lh2 = &info->mti_lh[MDT_LH_OLD];
1207 mdt_lock_reg_init(lh2, LCK_EX);
1209 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1210 MDS_INODELOCK_XATTR);
1214 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1215 MDS_INODELOCK_XATTR);
1219 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1220 mdt_object_child(o2), msl->msl_flags);
1224 mdt_swap_lov_flag(o1, o2);
1227 mdt_object_unlock(info, o2, lh2, rc);
1229 mdt_object_unlock(info, o1, lh1, rc);
1231 mdt_object_put(info->mti_env, o);
1233 mdt_thread_info_fini(info);
1237 static int mdt_raw_lookup(struct mdt_thread_info *info,
1238 struct mdt_object *parent,
1239 const struct lu_name *lname,
1240 struct ldlm_reply *ldlm_rep)
1242 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1246 LASSERT(!info->mti_cross_ref);
1248 /* Only got the fid of this obj by name */
1249 fid_zero(child_fid);
1250 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1251 lname, child_fid, &info->mti_spec);
1253 struct mdt_body *repbody;
1255 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1256 repbody->mbo_fid1 = *child_fid;
1257 repbody->mbo_valid = OBD_MD_FLID;
1258 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1259 } else if (rc == -ENOENT) {
1260 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1267 * UPDATE lock should be taken against parent, and be released before exit;
1268 * child_bits lock should be taken against child, and be returned back:
1269 * (1)normal request should release the child lock;
1270 * (2)intent request will grant the lock to client.
1272 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1273 struct mdt_lock_handle *lhc,
1275 struct ldlm_reply *ldlm_rep)
1277 struct ptlrpc_request *req = mdt_info_req(info);
1278 struct mdt_body *reqbody = NULL;
1279 struct mdt_object *parent = info->mti_object;
1280 struct mdt_object *child;
1281 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1282 struct lu_name *lname = NULL;
1283 struct mdt_lock_handle *lhp = NULL;
1284 struct ldlm_lock *lock;
1291 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1292 LASSERT(ergo(is_resent,
1293 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1298 if (info->mti_cross_ref) {
1299 /* Only getattr on the child. Parent is on another node. */
1300 mdt_set_disposition(info, ldlm_rep,
1301 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1303 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1305 PFID(mdt_object_fid(child)), ldlm_rep);
1307 rc = mdt_check_resent_lock(info, child, lhc);
1310 } else if (rc > 0) {
1311 mdt_lock_handle_init(lhc);
1312 mdt_lock_reg_init(lhc, LCK_PR);
1315 * Object's name is on another MDS, no lookup or layout
1316 * lock is needed here but update lock is.
1318 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1319 MDS_INODELOCK_LAYOUT);
1320 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1322 rc = mdt_object_lock(info, child, lhc, child_bits);
1327 /* Finally, we can get attr for child. */
1328 if (!mdt_object_exists(child)) {
1329 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1331 "remote object doesn't exist.\n");
1332 mdt_object_unlock(info, child, lhc, 1);
1336 rc = mdt_getattr_internal(info, child, 0);
1337 if (unlikely(rc != 0))
1338 mdt_object_unlock(info, child, lhc, 1);
1343 lname = &info->mti_name;
1344 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1346 if (lu_name_is_valid(lname)) {
1347 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1348 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1349 PNAME(lname), ldlm_rep);
1351 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1352 if (unlikely(reqbody == NULL))
1353 RETURN(err_serious(-EPROTO));
1355 *child_fid = reqbody->mbo_fid2;
1357 if (unlikely(!fid_is_sane(child_fid)))
1358 RETURN(err_serious(-EINVAL));
1360 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1362 PFID(mdt_object_fid(parent)),
1363 PFID(&reqbody->mbo_fid2), ldlm_rep);
1366 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1368 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1369 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1371 "Parent doesn't exist!\n");
1375 if (mdt_object_remote(parent)) {
1376 CERROR("%s: parent "DFID" is on remote target\n",
1377 mdt_obd_name(info->mti_mdt),
1378 PFID(mdt_object_fid(parent)));
1382 if (lu_name_is_valid(lname)) {
1383 /* Always allow to lookup ".." */
1384 if (unlikely(lname->ln_namelen == 2 &&
1385 lname->ln_name[0] == '.' &&
1386 lname->ln_name[1] == '.'))
1387 info->mti_spec.sp_permitted = 1;
1389 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1390 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1395 /* step 1: lock parent only if parent is a directory */
1396 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1397 lhp = &info->mti_lh[MDT_LH_PARENT];
1398 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1399 rc = mdt_object_lock(info, parent, lhp,
1400 MDS_INODELOCK_UPDATE);
1401 if (unlikely(rc != 0))
1405 /* step 2: lookup child's fid by name */
1406 fid_zero(child_fid);
1407 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1408 child_fid, &info->mti_spec);
1410 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1413 GOTO(out_parent, rc);
1416 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1419 *step 3: find the child object by fid & lock it.
1420 * regardless if it is local or remote.
1422 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1423 * set parent dir fid the same as child fid in getattr by fid case
1424 * we should not lu_object_find() the object again, could lead
1425 * to hung if there is a concurrent unlink destroyed the object.
1427 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1428 mdt_object_get(info->mti_env, parent);
1431 child = mdt_object_find(info->mti_env, info->mti_mdt,
1435 if (unlikely(IS_ERR(child)))
1436 GOTO(out_parent, rc = PTR_ERR(child));
1438 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1439 if (!mdt_object_exists(child)) {
1440 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1442 "Object doesn't exist!\n");
1443 GOTO(out_child, rc = -ENOENT);
1446 rc = mdt_check_resent_lock(info, child, lhc);
1448 GOTO(out_child, rc);
1449 } else if (rc > 0) {
1450 mdt_lock_handle_init(lhc);
1451 mdt_lock_reg_init(lhc, LCK_PR);
1454 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1455 mdt_object_exists(child) && !mdt_object_remote(child)) {
1456 struct md_attr *ma = &info->mti_attr;
1459 ma->ma_need = MA_INODE;
1460 rc = mdt_attr_get_complex(info, child, ma);
1461 if (unlikely(rc != 0))
1462 GOTO(out_child, rc);
1464 /* If the file has not been changed for some time, we
1465 * return not only a LOOKUP lock, but also an UPDATE
1466 * lock and this might save us RPC on later STAT. For
1467 * directories, it also let negative dentry cache start
1468 * working for this dir. */
1469 if (ma->ma_valid & MA_INODE &&
1470 ma->ma_attr.la_valid & LA_CTIME &&
1471 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1472 ma->ma_attr.la_ctime < cfs_time_current_sec())
1473 child_bits |= MDS_INODELOCK_UPDATE;
1476 /* layout lock must be granted in a best-effort way
1477 * for IT operations */
1478 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1479 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1480 exp_connect_layout(info->mti_exp) &&
1481 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1482 !mdt_object_remote(child) && ldlm_rep != NULL) {
1483 /* try to grant layout lock for regular file. */
1489 child_bits |= MDS_INODELOCK_LAYOUT;
1490 /* try layout lock, it may fail to be granted due to
1491 * contention at LOOKUP or UPDATE */
1492 if (!mdt_object_lock_try(info, child, lhc,
1494 child_bits &= ~MDS_INODELOCK_LAYOUT;
1495 LASSERT(child_bits != 0);
1496 rc = mdt_object_lock(info, child, lhc,
1502 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1503 * client will enqueue the lock to the remote MDT */
1504 if (mdt_object_remote(child))
1505 child_bits &= ~MDS_INODELOCK_UPDATE;
1506 rc = mdt_object_lock(info, child, lhc, child_bits);
1508 if (unlikely(rc != 0))
1509 GOTO(out_child, rc);
1512 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1514 /* finally, we can get attr for child. */
1515 rc = mdt_getattr_internal(info, child, ma_need);
1516 if (unlikely(rc != 0)) {
1517 mdt_object_unlock(info, child, lhc, 1);
1519 /* Debugging code. */
1520 LDLM_DEBUG(lock, "Returning lock to client");
1521 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1522 &lock->l_resource->lr_name),
1523 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1524 PLDLMRES(lock->l_resource),
1525 PFID(mdt_object_fid(child)));
1528 LDLM_LOCK_PUT(lock);
1532 mdt_object_put(info->mti_env, child);
1535 mdt_object_unlock(info, parent, lhp, 1);
1539 /* normal handler: should release the child lock */
1540 static int mdt_getattr_name(struct tgt_session_info *tsi)
1542 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1543 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1544 struct mdt_body *reqbody;
1545 struct mdt_body *repbody;
1549 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1550 LASSERT(reqbody != NULL);
1551 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1552 LASSERT(repbody != NULL);
1554 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1555 repbody->mbo_eadatasize = 0;
1556 repbody->mbo_aclsize = 0;
1558 rc = mdt_init_ucred(info, reqbody);
1560 GOTO(out_shrink, rc);
1562 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1563 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1564 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1565 lhc->mlh_reg_lh.cookie = 0;
1567 mdt_exit_ucred(info);
1570 mdt_client_compatibility(info);
1571 rc2 = mdt_fix_reply(info);
1574 mdt_thread_info_fini(info);
1578 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1579 void *karg, void *uarg);
1581 static int mdt_set_info(struct tgt_session_info *tsi)
1583 struct ptlrpc_request *req = tgt_ses_req(tsi);
1586 int keylen, vallen, rc = 0;
1590 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1592 DEBUG_REQ(D_HA, req, "no set_info key");
1593 RETURN(err_serious(-EFAULT));
1596 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1599 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1601 DEBUG_REQ(D_HA, req, "no set_info val");
1602 RETURN(err_serious(-EFAULT));
1605 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1608 /* Swab any part of val you need to here */
1609 if (KEY_IS(KEY_READ_ONLY)) {
1610 spin_lock(&req->rq_export->exp_lock);
1612 *exp_connect_flags_ptr(req->rq_export) |=
1615 *exp_connect_flags_ptr(req->rq_export) &=
1616 ~OBD_CONNECT_RDONLY;
1617 spin_unlock(&req->rq_export->exp_lock);
1618 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1619 struct changelog_setinfo *cs = val;
1621 if (vallen != sizeof(*cs)) {
1622 CERROR("%s: bad changelog_clear setinfo size %d\n",
1623 tgt_name(tsi->tsi_tgt), vallen);
1626 if (ptlrpc_req_need_swab(req)) {
1627 __swab64s(&cs->cs_recno);
1628 __swab32s(&cs->cs_id);
1631 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1633 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1635 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1642 static int mdt_readpage(struct tgt_session_info *tsi)
1644 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1645 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1646 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1647 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1648 struct mdt_body *repbody;
1654 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1655 RETURN(err_serious(-ENOMEM));
1657 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1658 if (repbody == NULL || reqbody == NULL)
1659 RETURN(err_serious(-EFAULT));
1662 * prepare @rdpg before calling lower layers and transfer itself. Here
1663 * reqbody->size contains offset of where to start to read and
1664 * reqbody->nlink contains number bytes to read.
1666 rdpg->rp_hash = reqbody->mbo_size;
1667 if (rdpg->rp_hash != reqbody->mbo_size) {
1668 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1669 rdpg->rp_hash, reqbody->mbo_size);
1673 rdpg->rp_attrs = reqbody->mbo_mode;
1674 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1675 rdpg->rp_attrs |= LUDA_64BITHASH;
1676 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1677 exp_max_brw_size(tsi->tsi_exp));
1678 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1680 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1681 if (rdpg->rp_pages == NULL)
1684 for (i = 0; i < rdpg->rp_npages; ++i) {
1685 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1686 if (rdpg->rp_pages[i] == NULL)
1687 GOTO(free_rdpg, rc = -ENOMEM);
1690 /* call lower layers to fill allocated pages with directory data */
1691 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1693 GOTO(free_rdpg, rc);
1695 /* send pages to client */
1696 rc = tgt_sendpage(tsi, rdpg, rc);
1701 for (i = 0; i < rdpg->rp_npages; i++)
1702 if (rdpg->rp_pages[i] != NULL)
1703 __free_page(rdpg->rp_pages[i]);
1704 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1706 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1712 static int mdt_reint_internal(struct mdt_thread_info *info,
1713 struct mdt_lock_handle *lhc,
1716 struct req_capsule *pill = info->mti_pill;
1717 struct mdt_body *repbody;
1722 rc = mdt_reint_unpack(info, op);
1724 CERROR("Can't unpack reint, rc %d\n", rc);
1725 RETURN(err_serious(rc));
1728 /* for replay (no_create) lmm is not needed, client has it already */
1729 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1730 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1733 /* llog cookies are always 0, the field is kept for compatibility */
1734 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1735 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1737 rc = req_capsule_server_pack(pill);
1739 CERROR("Can't pack response, rc %d\n", rc);
1740 RETURN(err_serious(rc));
1743 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1744 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1746 repbody->mbo_eadatasize = 0;
1747 repbody->mbo_aclsize = 0;
1750 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1752 /* for replay no cookkie / lmm need, because client have this already */
1753 if (info->mti_spec.no_create)
1754 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1755 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1757 rc = mdt_init_ucred_reint(info);
1759 GOTO(out_shrink, rc);
1761 rc = mdt_fix_attr_ucred(info, op);
1763 GOTO(out_ucred, rc = err_serious(rc));
1765 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1767 GOTO(out_ucred, rc);
1768 } else if (rc == 1) {
1769 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1770 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1771 GOTO(out_ucred, rc);
1773 rc = mdt_reint_rec(info, lhc);
1776 mdt_exit_ucred(info);
1778 mdt_client_compatibility(info);
1779 rc2 = mdt_fix_reply(info);
1785 static long mdt_reint_opcode(struct ptlrpc_request *req,
1786 const struct req_format **fmt)
1788 struct mdt_device *mdt;
1789 struct mdt_rec_reint *rec;
1792 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1794 opc = rec->rr_opcode;
1795 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1796 if (opc < REINT_MAX && fmt[opc] != NULL)
1797 req_capsule_extend(&req->rq_pill, fmt[opc]);
1799 mdt = mdt_exp2dev(req->rq_export);
1800 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1801 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1802 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1803 opc = err_serious(-EFAULT);
1806 opc = err_serious(-EFAULT);
1811 static int mdt_reint(struct tgt_session_info *tsi)
1815 static const struct req_format *reint_fmts[REINT_MAX] = {
1816 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1817 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1818 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1819 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1820 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1821 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1822 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1823 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1824 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1829 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1831 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1833 * No lock possible here from client to pass it to reint code
1836 rc = mdt_reint_internal(info, NULL, opc);
1837 mdt_thread_info_fini(info);
1842 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1846 /* this should sync the whole device */
1847 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1849 struct dt_device *dt = mdt->mdt_bottom;
1853 rc = dt->dd_ops->dt_sync(env, dt);
1857 /* this should sync this object */
1858 static int mdt_object_sync(struct mdt_thread_info *info)
1860 struct md_object *next;
1864 if (!mdt_object_exists(info->mti_object)) {
1865 CWARN("%s: non existing object "DFID": rc = %d\n",
1866 mdt_obd_name(info->mti_mdt),
1867 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1870 next = mdt_object_child(info->mti_object);
1871 rc = mo_object_sync(info->mti_env, next);
1876 static int mdt_sync(struct tgt_session_info *tsi)
1878 struct ptlrpc_request *req = tgt_ses_req(tsi);
1879 struct req_capsule *pill = tsi->tsi_pill;
1880 struct mdt_body *body;
1885 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1886 RETURN(err_serious(-ENOMEM));
1888 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
1889 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1891 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1893 /* sync an object */
1894 rc = mdt_object_sync(info);
1896 const struct lu_fid *fid;
1897 struct lu_attr *la = &info->mti_attr.ma_attr;
1899 info->mti_attr.ma_need = MA_INODE;
1900 info->mti_attr.ma_valid = 0;
1901 rc = mdt_attr_get_complex(info, info->mti_object,
1904 body = req_capsule_server_get(pill,
1906 fid = mdt_object_fid(info->mti_object);
1907 mdt_pack_attr2body(info, body, la, fid);
1910 mdt_thread_info_fini(info);
1913 mdt_counter_incr(req, LPROC_MDT_SYNC);
1919 * Handle quota control requests to consult current usage/limit, but also
1920 * to configure quota enforcement
1922 static int mdt_quotactl(struct tgt_session_info *tsi)
1924 struct obd_export *exp = tsi->tsi_exp;
1925 struct req_capsule *pill = tsi->tsi_pill;
1926 struct obd_quotactl *oqctl, *repoqc;
1928 struct mdt_device *mdt = mdt_exp2dev(exp);
1929 struct lu_device *qmt = mdt->mdt_qmt_dev;
1930 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
1933 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1935 RETURN(err_serious(-EPROTO));
1937 rc = req_capsule_server_pack(pill);
1939 RETURN(err_serious(rc));
1941 switch (oqctl->qc_cmd) {
1942 /* master quotactl */
1945 if (!nodemap_can_setquota(nodemap))
1950 RETURN(-EOPNOTSUPP);
1951 /* slave quotactl */
1956 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1960 /* map uid/gid for remote client */
1962 if (exp_connect_rmtclient(exp)) {
1963 struct lustre_idmap_table *idmap;
1965 idmap = exp->exp_mdt_data.med_idmap;
1967 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1968 oqctl->qc_cmd != Q_GETINFO))
1971 if (oqctl->qc_type == USRQUOTA)
1972 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1974 else if (oqctl->qc_type == GRPQUOTA)
1975 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1980 if (id == CFS_IDMAP_NOTFOUND) {
1981 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1986 if (oqctl->qc_type == USRQUOTA)
1987 id = nodemap_map_id(nodemap, NODEMAP_UID,
1988 NODEMAP_CLIENT_TO_FS, id);
1989 else if (oqctl->qc_type == GRPQUOTA)
1990 id = nodemap_map_id(nodemap, NODEMAP_UID,
1991 NODEMAP_CLIENT_TO_FS, id);
1993 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1995 RETURN(err_serious(-EFAULT));
1997 if (oqctl->qc_id != id)
1998 swap(oqctl->qc_id, id);
2000 switch (oqctl->qc_cmd) {
2006 /* forward quotactl request to QMT */
2007 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2012 /* slave quotactl */
2013 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2018 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2022 if (oqctl->qc_id != id)
2023 swap(oqctl->qc_id, id);
2029 /** clone llog ctxt from child (mdd)
2030 * This allows remote llog (replicator) access.
2031 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2032 * context was originally set up, or we can handle them directly.
2033 * I choose the latter, but that means I need any llog
2034 * contexts set up by child to be accessable by the mdt. So we clone the
2035 * context into our context list here.
2037 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2040 struct md_device *next = mdt->mdt_child;
2041 struct llog_ctxt *ctxt;
2044 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2047 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2048 if (rc || ctxt == NULL) {
2052 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2054 CERROR("Can't set mdt ctxt %d\n", rc);
2059 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2060 struct mdt_device *mdt, int idx)
2062 struct llog_ctxt *ctxt;
2064 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2067 /* Put once for the get we just did, and once for the clone */
2068 llog_ctxt_put(ctxt);
2069 llog_ctxt_put(ctxt);
2074 * sec context handlers
2076 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2080 rc = mdt_handle_idmap(tsi);
2082 struct ptlrpc_request *req = tgt_ses_req(tsi);
2085 opc = lustre_msg_get_opc(req->rq_reqmsg);
2086 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2087 sptlrpc_svc_ctx_invalidate(req);
2090 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2096 * quota request handlers
2098 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2100 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2101 struct lu_device *qmt = mdt->mdt_qmt_dev;
2106 RETURN(err_serious(-EOPNOTSUPP));
2108 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2112 struct mdt_object *mdt_object_new(const struct lu_env *env,
2113 struct mdt_device *d,
2114 const struct lu_fid *f)
2116 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2117 struct lu_object *o;
2118 struct mdt_object *m;
2121 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2122 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2123 if (unlikely(IS_ERR(o)))
2124 m = (struct mdt_object *)o;
2130 struct mdt_object *mdt_object_find(const struct lu_env *env,
2131 struct mdt_device *d,
2132 const struct lu_fid *f)
2134 struct lu_object *o;
2135 struct mdt_object *m;
2138 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2139 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2140 if (unlikely(IS_ERR(o)))
2141 m = (struct mdt_object *)o;
2149 * Asyncronous commit for mdt device.
2151 * Pass asynchonous commit call down the MDS stack.
2153 * \param env environment
2154 * \param mdt the mdt device
2156 static void mdt_device_commit_async(const struct lu_env *env,
2157 struct mdt_device *mdt)
2159 struct dt_device *dt = mdt->mdt_bottom;
2162 rc = dt->dd_ops->dt_commit_async(env, dt);
2163 if (unlikely(rc != 0))
2164 CWARN("%s: async commit start failed: rc = %d\n",
2165 mdt_obd_name(mdt), rc);
2169 * Mark the lock as "synchonous".
2171 * Mark the lock to deffer transaction commit to the unlock time.
2173 * \param lock the lock to mark as "synchonous"
2175 * \see mdt_is_lock_sync
2176 * \see mdt_save_lock
2178 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2180 lock->l_ast_data = (void*)1;
2184 * Check whehter the lock "synchonous" or not.
2186 * \param lock the lock to check
2187 * \retval 1 the lock is "synchonous"
2188 * \retval 0 the lock isn't "synchronous"
2190 * \see mdt_set_lock_sync
2191 * \see mdt_save_lock
2193 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2195 return lock->l_ast_data != NULL;
2199 * Blocking AST for mdt locks.
2201 * Starts transaction commit if in case of COS lock conflict or
2202 * deffers such a commit to the mdt_save_lock.
2204 * \param lock the lock which blocks a request or cancelling lock
2205 * \param desc unused
2206 * \param data unused
2207 * \param flag indicates whether this cancelling or blocking callback
2209 * \see ldlm_blocking_ast_nocheck
2211 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2212 void *data, int flag)
2214 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2215 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2219 if (flag == LDLM_CB_CANCELING)
2221 lock_res_and_lock(lock);
2222 if (lock->l_blocking_ast != mdt_blocking_ast) {
2223 unlock_res_and_lock(lock);
2226 if (mdt_cos_is_enabled(mdt) &&
2227 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2228 lock->l_blocking_lock != NULL &&
2229 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2230 mdt_set_lock_sync(lock);
2232 rc = ldlm_blocking_ast_nocheck(lock);
2234 /* There is no lock conflict if l_blocking_lock == NULL,
2235 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2236 * when the last reference to a local lock was released */
2237 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2240 rc = lu_env_init(&env, LCT_LOCAL);
2241 if (unlikely(rc != 0))
2242 CWARN("%s: lu_env initialization failed, cannot "
2243 "start asynchronous commit: rc = %d\n",
2246 mdt_device_commit_async(&env, mdt);
2252 /* Used for cross-MDT lock */
2253 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2254 void *data, int flag)
2256 struct lustre_handle lockh;
2260 case LDLM_CB_BLOCKING:
2261 ldlm_lock2handle(lock, &lockh);
2262 rc = ldlm_cli_cancel(&lockh,
2263 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2265 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2269 case LDLM_CB_CANCELING:
2270 LDLM_DEBUG(lock, "Revoke remote lock\n");
2278 int mdt_check_resent_lock(struct mdt_thread_info *info,
2279 struct mdt_object *mo,
2280 struct mdt_lock_handle *lhc)
2282 /* the lock might already be gotten in ldlm_handle_enqueue() */
2283 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2284 struct ptlrpc_request *req = mdt_info_req(info);
2285 struct ldlm_lock *lock;
2287 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2288 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2290 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2291 * a resend case, however, it could be already destroyed
2292 * due to client eviction or a raced cancel RPC. */
2293 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
2294 lhc->mlh_reg_lh.cookie);
2298 if (!fid_res_name_eq(mdt_object_fid(mo),
2299 &lock->l_resource->lr_name)) {
2300 CWARN("%s: Although resent, but still not "
2301 "get child lock:"DFID"\n",
2302 info->mti_exp->exp_obd->obd_name,
2303 PFID(mdt_object_fid(mo)));
2304 LDLM_LOCK_PUT(lock);
2307 LDLM_LOCK_PUT(lock);
2313 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2314 const struct lu_fid *fid, struct lustre_handle *lh,
2315 enum ldlm_mode mode, __u64 ibits, bool nonblock)
2317 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2318 union ldlm_policy_data *policy = &mti->mti_policy;
2319 struct ldlm_res_id *res_id = &mti->mti_res_id;
2323 LASSERT(mdt_object_remote(o));
2325 fid_build_reg_res_name(fid, res_id);
2327 memset(einfo, 0, sizeof(*einfo));
2328 einfo->ei_type = LDLM_IBITS;
2329 einfo->ei_mode = mode;
2330 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2331 einfo->ei_cb_cp = ldlm_completion_ast;
2332 einfo->ei_enq_slave = 0;
2333 einfo->ei_res_id = res_id;
2335 einfo->ei_nonblock = 1;
2337 memset(policy, 0, sizeof(*policy));
2338 policy->l_inodebits.bits = ibits;
2340 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2345 static int mdt_object_local_lock(struct mdt_thread_info *info,
2346 struct mdt_object *o,
2347 struct mdt_lock_handle *lh, __u64 ibits,
2350 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2351 union ldlm_policy_data *policy = &info->mti_policy;
2352 struct ldlm_res_id *res_id = &info->mti_res_id;
2357 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2358 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2359 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2360 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2362 /* Only enqueue LOOKUP lock for remote object */
2363 if (mdt_object_remote(o))
2364 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2366 if (lh->mlh_type == MDT_PDO_LOCK) {
2367 /* check for exists after object is locked */
2368 if (mdt_object_exists(o) == 0) {
2369 /* Non-existent object shouldn't have PDO lock */
2372 /* Non-dir object shouldn't have PDO lock */
2373 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2378 memset(policy, 0, sizeof(*policy));
2379 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2381 dlmflags = LDLM_FL_ATOMIC_CB;
2383 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2386 * Take PDO lock on whole directory and build correct @res_id for lock
2387 * on part of directory.
2389 if (lh->mlh_pdo_hash != 0) {
2390 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2391 mdt_lock_pdo_mode(info, o, lh);
2392 if (lh->mlh_pdo_mode != LCK_NL) {
2394 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2395 * is never going to be sent to client and we do not
2396 * want it slowed down due to possible cancels.
2398 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2399 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2400 policy, res_id, dlmflags,
2401 info->mti_exp == NULL ? NULL :
2402 &info->mti_exp->exp_handle.h_cookie);
2403 if (unlikely(rc != 0))
2404 GOTO(out_unlock, rc);
2408 * Finish res_id initializing by name hash marking part of
2409 * directory which is taking modification.
2411 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2414 policy->l_inodebits.bits = ibits;
2417 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2418 * going to be sent to client. If it is - mdt_intent_policy() path will
2419 * fix it up and turn FL_LOCAL flag off.
2421 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2422 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2423 info->mti_exp == NULL ? NULL :
2424 &info->mti_exp->exp_handle.h_cookie);
2427 mdt_object_unlock(info, o, lh, 1);
2428 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2429 lh->mlh_pdo_hash != 0 &&
2430 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2431 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2437 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2438 struct mdt_lock_handle *lh, __u64 ibits,
2441 struct mdt_lock_handle *local_lh = NULL;
2445 if (!mdt_object_remote(o))
2446 return mdt_object_local_lock(info, o, lh, ibits, nonblock);
2448 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2449 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2450 MDS_INODELOCK_XATTR);
2452 /* Only enqueue LOOKUP lock for remote object */
2453 if (ibits & MDS_INODELOCK_LOOKUP) {
2454 rc = mdt_object_local_lock(info, o, lh,
2455 MDS_INODELOCK_LOOKUP,
2463 if (ibits & MDS_INODELOCK_UPDATE) {
2464 /* Sigh, PDO needs to enqueue 2 locks right now, but
2465 * enqueue RPC can only request 1 lock, to avoid extra
2466 * RPC, so it will instead enqueue EX lock for remote
2467 * object anyway XXX*/
2468 if (lh->mlh_type == MDT_PDO_LOCK &&
2469 lh->mlh_pdo_hash != 0) {
2470 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2471 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2472 PFID(mdt_object_fid(o)));
2473 lh->mlh_pdo_hash = 0;
2474 lh->mlh_rreg_mode = LCK_EX;
2475 lh->mlh_type = MDT_REG_LOCK;
2477 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2480 MDS_INODELOCK_UPDATE, nonblock);
2481 if (rc != ELDLM_OK) {
2482 if (local_lh != NULL)
2483 mdt_object_unlock(info, o, local_lh, rc);
2491 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2492 struct mdt_lock_handle *lh, __u64 ibits)
2494 return mdt_object_lock_internal(info, o, lh, ibits, false);
2497 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2498 struct mdt_lock_handle *lh, __u64 ibits)
2500 struct mdt_lock_handle tmp = *lh;
2503 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true);
2511 * Save a lock within request object.
2513 * Keep the lock referenced until whether client ACK or transaction
2514 * commit happens or release the lock immediately depending on input
2515 * parameters. If COS is ON, a write lock is converted to COS lock
2518 * \param info thead info object
2519 * \param h lock handle
2520 * \param mode lock mode
2521 * \param decref force immediate lock releasing
2523 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2524 enum ldlm_mode mode, int decref)
2528 if (lustre_handle_is_used(h)) {
2529 if (decref || !info->mti_has_trans ||
2530 !(mode & (LCK_PW | LCK_EX))) {
2531 mdt_fid_unlock(h, mode);
2533 struct mdt_device *mdt = info->mti_mdt;
2534 struct ldlm_lock *lock = ldlm_handle2lock(h);
2535 struct ptlrpc_request *req = mdt_info_req(info);
2538 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2540 /* there is no request if mdt_object_unlock() is called
2541 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2542 if (likely(req != NULL)) {
2543 CDEBUG(D_HA, "request = %p reply state = %p"
2544 " transno = "LPD64"\n", req,
2545 req->rq_reply_state, req->rq_transno);
2546 if (mdt_cos_is_enabled(mdt)) {
2548 ldlm_lock_downgrade(lock, LCK_COS);
2551 ptlrpc_save_lock(req, h, mode, no_ack);
2553 ldlm_lock_decref(h, mode);
2555 if (mdt_is_lock_sync(lock)) {
2556 CDEBUG(D_HA, "found sync-lock,"
2557 " async commit started\n");
2558 mdt_device_commit_async(info->mti_env,
2561 LDLM_LOCK_PUT(lock);
2570 * Unlock mdt object.
2572 * Immeditely release the regular lock and the PDO lock or save the
2573 * lock in request and keep them referenced until client ACK or
2574 * transaction commit.
2576 * \param info thread info object
2577 * \param o mdt object
2578 * \param lh mdt lock handle referencing regular and PDO locks
2579 * \param decref force immediate lock releasing
2581 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2582 struct mdt_lock_handle *lh, int decref)
2586 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2587 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2589 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2590 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2595 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2596 const struct lu_fid *f,
2597 struct mdt_lock_handle *lh,
2600 struct mdt_object *o;
2602 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2606 rc = mdt_object_lock(info, o, lh, ibits);
2608 mdt_object_put(info->mti_env, o);
2615 void mdt_object_unlock_put(struct mdt_thread_info * info,
2616 struct mdt_object * o,
2617 struct mdt_lock_handle *lh,
2620 mdt_object_unlock(info, o, lh, decref);
2621 mdt_object_put(info->mti_env, o);
2625 * Generic code handling requests that have struct mdt_body passed in:
2627 * - extract mdt_body from request and save it in @info, if present;
2629 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2632 * - if HABEO_CORPUS flag is set for this request type check whether object
2633 * actually exists on storage (lu_object_exists()).
2636 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2638 const struct mdt_body *body;
2639 struct mdt_object *obj;
2640 const struct lu_env *env;
2641 struct req_capsule *pill;
2645 env = info->mti_env;
2646 pill = info->mti_pill;
2648 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2652 if (!(body->mbo_valid & OBD_MD_FLID))
2655 if (!fid_is_sane(&body->mbo_fid1)) {
2656 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2660 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2662 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2663 mdt_object_put(env, obj);
2666 info->mti_object = obj;
2675 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2677 struct req_capsule *pill = info->mti_pill;
2681 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2682 rc = mdt_body_unpack(info, flags);
2686 if (rc == 0 && (flags & HABEO_REFERO)) {
2688 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2689 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2691 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2692 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2695 rc = req_capsule_server_pack(pill);
2700 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2702 lh->mlh_type = MDT_NUL_LOCK;
2703 lh->mlh_reg_lh.cookie = 0ull;
2704 lh->mlh_reg_mode = LCK_MINMODE;
2705 lh->mlh_pdo_lh.cookie = 0ull;
2706 lh->mlh_pdo_mode = LCK_MINMODE;
2707 lh->mlh_rreg_lh.cookie = 0ull;
2708 lh->mlh_rreg_mode = LCK_MINMODE;
2711 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2713 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2714 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2718 * Initialize fields of struct mdt_thread_info. Other fields are left in
2719 * uninitialized state, because it's too expensive to zero out whole
2720 * mdt_thread_info (> 1K) on each request arrival.
2722 void mdt_thread_info_init(struct ptlrpc_request *req,
2723 struct mdt_thread_info *info)
2727 info->mti_pill = &req->rq_pill;
2730 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2731 mdt_lock_handle_init(&info->mti_lh[i]);
2733 /* mdt device: it can be NULL while CONNECT */
2734 if (req->rq_export) {
2735 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2736 info->mti_exp = req->rq_export;
2738 info->mti_mdt = NULL;
2739 info->mti_env = req->rq_svc_thread->t_env;
2740 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2742 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2743 info->mti_big_buf = LU_BUF_NULL;
2744 info->mti_body = NULL;
2745 info->mti_object = NULL;
2746 info->mti_dlm_req = NULL;
2747 info->mti_has_trans = 0;
2748 info->mti_cross_ref = 0;
2749 info->mti_opdata = 0;
2750 info->mti_big_lmm_used = 0;
2752 info->mti_spec.no_create = 0;
2753 info->mti_spec.sp_rm_entry = 0;
2754 info->mti_spec.sp_permitted = 0;
2755 info->mti_spec.sp_migrate_close = 0;
2757 info->mti_spec.u.sp_ea.eadata = NULL;
2758 info->mti_spec.u.sp_ea.eadatalen = 0;
2761 void mdt_thread_info_fini(struct mdt_thread_info *info)
2765 if (info->mti_object != NULL) {
2766 mdt_object_put(info->mti_env, info->mti_object);
2767 info->mti_object = NULL;
2770 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2771 mdt_lock_handle_fini(&info->mti_lh[i]);
2772 info->mti_env = NULL;
2773 info->mti_pill = NULL;
2774 info->mti_exp = NULL;
2776 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2777 lu_buf_free(&info->mti_big_buf);
2780 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2782 struct mdt_thread_info *mti;
2784 mti = mdt_th_info(tsi->tsi_env);
2785 LASSERT(mti != NULL);
2787 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2788 if (tsi->tsi_corpus != NULL) {
2789 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2790 lu_object_get(tsi->tsi_corpus);
2792 mti->mti_body = tsi->tsi_mdt_body;
2793 mti->mti_dlm_req = tsi->tsi_dlm_req;
2798 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2800 struct ptlrpc_request *req = tgt_ses_req(tsi);
2805 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
2807 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
2808 set_current_state(TASK_UNINTERRUPTIBLE);
2809 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
2812 rc = tgt_connect(tsi);
2816 rc = mdt_init_idmap(tsi);
2821 obd_disconnect(class_export_get(req->rq_export));
2840 static int mdt_intent_getattr(enum mdt_it_code opcode,
2841 struct mdt_thread_info *info,
2842 struct ldlm_lock **,
2845 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2846 struct mdt_thread_info *info,
2847 struct ldlm_lock **lockp,
2850 static int mdt_intent_layout(enum mdt_it_code opcode,
2851 struct mdt_thread_info *info,
2852 struct ldlm_lock **,
2854 static int mdt_intent_reint(enum mdt_it_code opcode,
2855 struct mdt_thread_info *info,
2856 struct ldlm_lock **,
2859 static struct mdt_it_flavor {
2860 const struct req_format *it_fmt;
2862 int (*it_act)(enum mdt_it_code ,
2863 struct mdt_thread_info *,
2864 struct ldlm_lock **,
2867 } mdt_it_flavor[] = {
2869 .it_fmt = &RQF_LDLM_INTENT,
2870 /*.it_flags = HABEO_REFERO,*/
2872 .it_act = mdt_intent_reint,
2873 .it_reint = REINT_OPEN
2876 .it_fmt = &RQF_LDLM_INTENT,
2878 * OCREAT is not a MUTABOR request as if the file
2880 * We do the extra check of OBD_CONNECT_RDONLY in
2881 * mdt_reint_open() when we really need to create
2885 .it_act = mdt_intent_reint,
2886 .it_reint = REINT_OPEN
2889 .it_fmt = &RQF_LDLM_INTENT,
2890 .it_flags = MUTABOR,
2891 .it_act = mdt_intent_reint,
2892 .it_reint = REINT_CREATE
2894 [MDT_IT_GETATTR] = {
2895 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2896 .it_flags = HABEO_REFERO,
2897 .it_act = mdt_intent_getattr
2899 [MDT_IT_READDIR] = {
2905 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2906 .it_flags = HABEO_REFERO,
2907 .it_act = mdt_intent_getattr
2910 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2911 .it_flags = MUTABOR,
2913 .it_reint = REINT_UNLINK
2917 .it_flags = MUTABOR,
2920 [MDT_IT_GETXATTR] = {
2921 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2922 .it_flags = HABEO_CORPUS,
2923 .it_act = mdt_intent_getxattr
2926 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2928 .it_act = mdt_intent_layout
2933 mdt_intent_lock_replace(struct mdt_thread_info *info,
2934 struct ldlm_lock **lockp,
2935 struct mdt_lock_handle *lh,
2938 struct ptlrpc_request *req = mdt_info_req(info);
2939 struct ldlm_lock *lock = *lockp;
2940 struct ldlm_lock *new_lock;
2942 /* If possible resent found a lock, @lh is set to its handle */
2943 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2945 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2946 lh->mlh_reg_lh.cookie = 0;
2950 LASSERTF(new_lock != NULL,
2951 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2954 * If we've already given this lock to a client once, then we should
2955 * have no readers or writers. Otherwise, we should have one reader
2956 * _or_ writer ref (which will be zeroed below) before returning the
2959 if (new_lock->l_export == req->rq_export) {
2960 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2962 LASSERT(new_lock->l_export == NULL);
2963 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2968 if (new_lock->l_export == req->rq_export) {
2970 * Already gave this to the client, which means that we
2971 * reconstructed a reply.
2973 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2976 LDLM_LOCK_RELEASE(new_lock);
2977 lh->mlh_reg_lh.cookie = 0;
2978 RETURN(ELDLM_LOCK_REPLACED);
2982 * Fixup the lock to be given to the client.
2984 lock_res_and_lock(new_lock);
2985 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2986 * possible blocking AST. */
2987 while (new_lock->l_readers > 0) {
2988 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2989 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2990 new_lock->l_readers--;
2992 while (new_lock->l_writers > 0) {
2993 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2994 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2995 new_lock->l_writers--;
2998 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2999 new_lock->l_blocking_ast = lock->l_blocking_ast;
3000 new_lock->l_completion_ast = lock->l_completion_ast;
3001 new_lock->l_remote_handle = lock->l_remote_handle;
3002 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3004 unlock_res_and_lock(new_lock);
3006 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3007 &new_lock->l_remote_handle,
3008 &new_lock->l_exp_hash);
3010 LDLM_LOCK_RELEASE(new_lock);
3011 lh->mlh_reg_lh.cookie = 0;
3013 RETURN(ELDLM_LOCK_REPLACED);
3016 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3017 struct ldlm_lock *new_lock,
3018 struct mdt_lock_handle *lh,
3021 struct ptlrpc_request *req = mdt_info_req(info);
3022 struct ldlm_request *dlmreq;
3024 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3027 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3029 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3030 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3032 if (flags & LDLM_FL_RESENT) {
3033 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3034 lh->mlh_reg_mode = new_lock->l_granted_mode;
3036 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3037 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3038 lh->mlh_reg_lh.cookie);
3043 * If the xid matches, then we know this is a resent request, and allow
3044 * it. (It's probably an OPEN, for which we don't send a lock.
3046 if (req_can_reconstruct(req, NULL))
3050 * This remote handle isn't enqueued, so we never received or processed
3051 * this request. Clear MSG_RESENT, because it can be handled like any
3052 * normal request now.
3054 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3056 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3057 dlmreq->lock_handle[0].cookie);
3060 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3061 struct mdt_thread_info *info,
3062 struct ldlm_lock **lockp,
3065 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3066 struct ldlm_reply *ldlm_rep = NULL;
3070 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3071 * (for the resend case) or a new lock. Below we will use it to
3072 * replace the original lock.
3074 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3075 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3076 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3077 rc = mdt_object_lock(info, info->mti_object, lhc,
3078 MDS_INODELOCK_XATTR);
3083 grc = mdt_getxattr(info);
3085 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3087 if (mdt_info_req(info)->rq_repmsg != NULL)
3088 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3089 if (ldlm_rep == NULL)
3090 RETURN(err_serious(-EFAULT));
3092 ldlm_rep->lock_policy_res2 = grc;
3097 static int mdt_intent_getattr(enum mdt_it_code opcode,
3098 struct mdt_thread_info *info,
3099 struct ldlm_lock **lockp,
3102 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3104 struct ldlm_reply *ldlm_rep;
3105 struct mdt_body *reqbody;
3106 struct mdt_body *repbody;
3110 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3113 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3116 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3117 repbody->mbo_eadatasize = 0;
3118 repbody->mbo_aclsize = 0;
3122 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3124 case MDT_IT_GETATTR:
3125 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3129 CERROR("Unsupported intent (%d)\n", opcode);
3130 GOTO(out_shrink, rc = -EINVAL);
3133 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3135 GOTO(out_shrink, rc);
3137 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3138 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3140 /* Get lock from request for possible resent case. */
3141 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3143 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3144 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3146 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3147 ldlm_rep->lock_policy_res2 = 0;
3148 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3149 ldlm_rep->lock_policy_res2) {
3150 lhc->mlh_reg_lh.cookie = 0ull;
3151 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3154 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3157 mdt_exit_ucred(info);
3159 mdt_client_compatibility(info);
3160 rc2 = mdt_fix_reply(info);
3166 static int mdt_intent_layout(enum mdt_it_code opcode,
3167 struct mdt_thread_info *info,
3168 struct ldlm_lock **lockp,
3171 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3172 struct layout_intent *layout;
3174 struct mdt_object *obj = NULL;
3178 if (opcode != MDT_IT_LAYOUT) {
3179 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3184 fid = &info->mti_tmp_fid2;
3185 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3187 /* Get lock from request for possible resent case. */
3188 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3190 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3192 RETURN(PTR_ERR(obj));
3194 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3195 /* get the length of lsm */
3196 rc = mdt_attr_get_eabuf_size(info, obj);
3198 mdt_object_put(info->mti_env, obj);
3202 if (rc > info->mti_mdt->mdt_max_mdsize)
3203 info->mti_mdt->mdt_max_mdsize = rc;
3206 mdt_object_put(info->mti_env, obj);
3208 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3209 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
3210 rc = req_capsule_server_pack(info->mti_pill);
3214 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
3215 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3217 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3218 LASSERT(layout != NULL);
3219 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3220 /* return to normal/resent ldlm handling */
3223 CERROR("%s: Unsupported layout intent (%d)\n",
3224 mdt_obd_name(info->mti_mdt), layout->li_opc);
3228 static int mdt_intent_reint(enum mdt_it_code opcode,
3229 struct mdt_thread_info *info,
3230 struct ldlm_lock **lockp,
3233 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3234 struct ldlm_reply *rep = NULL;
3238 static const struct req_format *intent_fmts[REINT_MAX] = {
3239 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3240 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3245 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3249 if (mdt_it_flavor[opcode].it_reint != opc) {
3250 CERROR("Reint code %ld doesn't match intent: %d\n",
3252 RETURN(err_serious(-EPROTO));
3255 /* Get lock from request for possible resent case. */
3256 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3258 rc = mdt_reint_internal(info, lhc, opc);
3260 /* Check whether the reply has been packed successfully. */
3261 if (mdt_info_req(info)->rq_repmsg != NULL)
3262 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3264 RETURN(err_serious(-EFAULT));
3266 /* MDC expects this in any case */
3268 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3270 /* the open lock or the lock for cross-ref object should be
3271 * returned to the client */
3272 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3273 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3274 rep->lock_policy_res2 = 0;
3275 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3279 rep->lock_policy_res2 = clear_serious(rc);
3281 if (rep->lock_policy_res2 == -ENOENT &&
3282 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3283 rep->lock_policy_res2 = 0;
3285 lhc->mlh_reg_lh.cookie = 0ull;
3286 if (rc == -ENOTCONN || rc == -ENODEV ||
3287 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3289 * If it is the disconnect error (ENODEV & ENOCONN), the error
3290 * will be returned by rq_status, and client at ptlrpc layer
3291 * will detect this, then disconnect, reconnect the import
3292 * immediately, instead of impacting the following the rpc.
3297 * For other cases, the error will be returned by intent, and client
3298 * will retrieve the result from intent.
3300 RETURN(ELDLM_LOCK_ABORTED);
3303 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3311 case IT_OPEN|IT_CREAT:
3318 rc = MDT_IT_READDIR;
3321 rc = MDT_IT_GETATTR;
3333 rc = MDT_IT_GETXATTR;
3338 case IT_QUOTA_DQACQ:
3343 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3350 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3351 struct mdt_thread_info *info,
3352 struct ldlm_lock **lockp, __u64 flags)
3354 struct req_capsule *pill = info->mti_pill;
3355 struct ptlrpc_request *req = mdt_info_req(info);
3356 struct mdt_it_flavor *flv;
3361 opc = mdt_intent_code(itopc);
3365 if (opc == MDT_IT_QUOTA) {
3366 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3369 RETURN(-EOPNOTSUPP);
3371 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3372 /* pass the request to quota master */
3373 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3374 mdt_info_req(info), lockp,
3379 flv = &mdt_it_flavor[opc];
3380 if (flv->it_fmt != NULL)
3381 req_capsule_extend(pill, flv->it_fmt);
3383 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3387 if (flv->it_flags & MUTABOR &&
3388 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3391 if (flv->it_act != NULL) {
3392 struct ldlm_reply *rep;
3394 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3396 /* execute policy */
3397 rc = flv->it_act(opc, info, lockp, flags);
3399 /* Check whether the reply has been packed successfully. */
3400 if (req->rq_repmsg != NULL) {
3401 rep = req_capsule_server_get(info->mti_pill,
3403 rep->lock_policy_res2 =
3404 ptlrpc_status_hton(rep->lock_policy_res2);
3411 static int mdt_intent_policy(struct ldlm_namespace *ns,
3412 struct ldlm_lock **lockp, void *req_cookie,
3413 enum ldlm_mode mode, __u64 flags, void *data)
3415 struct tgt_session_info *tsi;
3416 struct mdt_thread_info *info;
3417 struct ptlrpc_request *req = req_cookie;
3418 struct ldlm_intent *it;
3419 struct req_capsule *pill;
3424 LASSERT(req != NULL);
3426 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3428 info = tsi2mdt_info(tsi);
3429 LASSERT(info != NULL);
3430 pill = info->mti_pill;
3431 LASSERT(pill->rc_req == req);
3433 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3434 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3435 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3437 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3441 /* Lock without inodebits makes no sense and will oops
3442 * later in ldlm. Let's check it now to see if we have
3443 * ibits corrupted somewhere in mdt_intent_opc().
3444 * The case for client miss to set ibits has been
3445 * processed by others. */
3446 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3447 lr_type == LDLM_IBITS,
3448 info->mti_dlm_req->lock_desc.\
3449 l_policy_data.l_inodebits.bits != 0));
3451 rc = err_serious(-EFAULT);
3453 /* No intent was provided */
3454 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3455 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3456 rc = req_capsule_server_pack(pill);
3458 rc = err_serious(rc);
3460 mdt_thread_info_fini(info);
3464 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3466 struct seq_server_site *ss = mdt_seq_site(mdt);
3468 if (ss->ss_node_id == 0)
3471 if (ss->ss_client_seq != NULL) {
3472 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3473 ss->ss_client_seq->lcs_exp = NULL;
3476 if (ss->ss_server_fld != NULL) {
3477 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3478 ss->ss_server_fld->lsf_control_exp = NULL;
3482 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3484 struct seq_server_site *ss = mdt_seq_site(mdt);
3489 if (ss->ss_server_seq != NULL)
3490 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3493 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3495 mdt_seq_fini_cli(mdt);
3496 mdt_deregister_seq_exp(mdt);
3498 return seq_site_fini(env, mdt_seq_site(mdt));
3502 * It will retrieve its FLDB entries from MDT0, and it only happens
3503 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3504 * and it needs to refresh FLDB from the MDT0.
3506 static int mdt_register_lwp_callback(void *data)
3509 struct mdt_device *mdt = data;
3510 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3514 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3516 rc = lu_env_init(&env, LCT_MD_THREAD);
3518 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3522 /* Allocate new sequence now to avoid creating local transaction
3523 * in the normal transaction process */
3524 rc = seq_server_check_and_alloc_super(&env,
3525 mdt_seq_site(mdt)->ss_server_seq);
3530 rc = fld_update_from_controller(&env, fld);
3532 CERROR("%s: cannot update controller: rc = %d\n",
3533 mdt_obd_name(mdt), rc);
3542 static int mdt_register_seq_exp(struct mdt_device *mdt)
3544 struct seq_server_site *ss = mdt_seq_site(mdt);
3545 char *lwp_name = NULL;
3548 if (ss->ss_node_id == 0)
3551 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3552 if (lwp_name == NULL)
3553 GOTO(out_free, rc = -ENOMEM);
3555 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3559 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3564 rc = lustre_register_lwp_item(lwp_name,
3565 &ss->ss_server_fld->lsf_control_exp,
3566 mdt_register_lwp_callback, mdt);
3568 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3569 ss->ss_client_seq->lcs_exp = NULL;
3573 if (lwp_name != NULL)
3574 OBD_FREE(lwp_name, MAX_OBD_NAME);
3580 * Init client sequence manager which is used by local MDS to talk to sequence
3581 * controller on remote node.
3583 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3585 struct seq_server_site *ss = mdt_seq_site(mdt);
3590 /* check if this is adding the first MDC and controller is not yet
3592 OBD_ALLOC_PTR(ss->ss_client_seq);
3593 if (ss->ss_client_seq == NULL)
3596 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3597 if (prefix == NULL) {
3598 OBD_FREE_PTR(ss->ss_client_seq);
3599 ss->ss_client_seq = NULL;
3603 /* Note: seq_client_fini will be called in seq_site_fini */
3604 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3605 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3606 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3608 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3610 OBD_FREE_PTR(ss->ss_client_seq);
3611 ss->ss_client_seq = NULL;
3615 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3620 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3622 struct seq_server_site *ss;
3626 ss = mdt_seq_site(mdt);
3627 /* init sequence controller server(MDT0) */
3628 if (ss->ss_node_id == 0) {
3629 OBD_ALLOC_PTR(ss->ss_control_seq);
3630 if (ss->ss_control_seq == NULL)
3633 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3634 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3637 GOTO(out_seq_fini, rc);
3640 /* Init normal sequence server */
3641 OBD_ALLOC_PTR(ss->ss_server_seq);
3642 if (ss->ss_server_seq == NULL)
3643 GOTO(out_seq_fini, rc = -ENOMEM);
3645 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3646 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3648 GOTO(out_seq_fini, rc);
3650 /* init seq client for seq server to talk to seq controller(MDT0) */
3651 rc = mdt_seq_init_cli(env, mdt);
3653 GOTO(out_seq_fini, rc);
3655 if (ss->ss_node_id != 0)
3656 /* register controller export through lwp */
3657 rc = mdt_register_seq_exp(mdt);
3662 mdt_seq_fini(env, mdt);
3670 static int mdt_fld_fini(const struct lu_env *env,
3671 struct mdt_device *m)
3673 struct seq_server_site *ss = mdt_seq_site(m);
3676 if (ss && ss->ss_server_fld) {
3677 fld_server_fini(env, ss->ss_server_fld);
3678 OBD_FREE_PTR(ss->ss_server_fld);
3679 ss->ss_server_fld = NULL;
3685 static int mdt_fld_init(const struct lu_env *env,
3687 struct mdt_device *m)
3689 struct seq_server_site *ss;
3693 ss = mdt_seq_site(m);
3695 OBD_ALLOC_PTR(ss->ss_server_fld);
3696 if (ss->ss_server_fld == NULL)
3697 RETURN(rc = -ENOMEM);
3699 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3702 OBD_FREE_PTR(ss->ss_server_fld);
3703 ss->ss_server_fld = NULL;
3710 static void mdt_stack_pre_fini(const struct lu_env *env,
3711 struct mdt_device *m, struct lu_device *top)
3713 struct lustre_cfg_bufs *bufs;
3714 struct lustre_cfg *lcfg;
3715 struct mdt_thread_info *info;
3720 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3721 LASSERT(info != NULL);
3723 bufs = &info->mti_u.bufs;
3725 LASSERT(m->mdt_child_exp);
3726 LASSERT(m->mdt_child_exp->exp_obd);
3728 /* process cleanup, pass mdt obd name to get obd umount flags */
3729 /* XXX: this is needed because all layers are referenced by
3730 * objects (some of them are pinned by osd, for example *
3731 * the proper solution should be a model where object used
3732 * by osd only doesn't have mdt/mdd slices -bzzz */
3733 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3734 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3735 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3739 top->ld_ops->ldo_process_config(env, top, lcfg);
3740 lustre_cfg_free(lcfg);
3744 static void mdt_stack_fini(const struct lu_env *env,
3745 struct mdt_device *m, struct lu_device *top)
3747 struct obd_device *obd = mdt2obd_dev(m);
3748 struct lustre_cfg_bufs *bufs;
3749 struct lustre_cfg *lcfg;
3750 struct mdt_thread_info *info;
3754 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3755 LASSERT(info != NULL);
3757 lu_dev_del_linkage(top->ld_site, top);
3759 lu_site_purge(env, top->ld_site, -1);
3761 bufs = &info->mti_u.bufs;
3762 /* process cleanup, pass mdt obd name to get obd umount flags */
3763 /* another purpose is to let all layers to release their objects */
3764 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3769 lustre_cfg_bufs_set_string(bufs, 1, flags);
3770 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3775 top->ld_ops->ldo_process_config(env, top, lcfg);
3776 lustre_cfg_free(lcfg);
3778 lu_site_purge(env, top->ld_site, -1);
3780 m->mdt_child = NULL;
3781 m->mdt_bottom = NULL;
3783 obd_disconnect(m->mdt_child_exp);
3784 m->mdt_child_exp = NULL;
3786 obd_disconnect(m->mdt_bottom_exp);
3787 m->mdt_child_exp = NULL;
3790 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3791 const char *next, struct obd_export **exp)
3793 struct obd_connect_data *data = NULL;
3794 struct obd_device *obd;
3798 OBD_ALLOC_PTR(data);
3800 GOTO(out, rc = -ENOMEM);
3802 obd = class_name2obd(next);
3804 CERROR("%s: can't locate next device: %s\n",
3805 mdt_obd_name(m), next);
3806 GOTO(out, rc = -ENOTCONN);
3809 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3810 data->ocd_version = LUSTRE_VERSION_CODE;
3812 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3814 CERROR("%s: cannot connect to next dev %s (%d)\n",
3815 mdt_obd_name(m), next, rc);
3825 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3826 struct lustre_cfg *cfg)
3828 char *dev = lustre_cfg_string(cfg, 0);
3829 int rc, name_size, uuid_size;
3830 char *name, *uuid, *p;
3831 struct lustre_cfg_bufs *bufs;
3832 struct lustre_cfg *lcfg;
3833 struct obd_device *obd;
3834 struct lustre_profile *lprof;
3835 struct lu_site *site;
3838 /* in 1.8 we had the only device in the stack - MDS.
3839 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3840 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3841 * to generate names and setup MDT, MDD. MDT will be using
3842 * generated name to connect to MDD. for MDD the next device
3843 * will be LOD with name taken from so called "profile" which
3844 * is generated by mount_option line
3846 * 1.8 MGS generates config. commands like this:
3847 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3848 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3849 * 2.0 MGS generates config. commands like this:
3850 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3851 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3852 * 3:lustre-MDT0000-mdtlov 4:f
3854 * we generate MDD name from MDT one, just replacing T with D
3856 * after all the preparations, the logical equivalent will be
3857 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3858 * 3:lustre-MDT0000-mdtlov 4:f
3859 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3860 * 3:lustre-MDD0000 4:f
3862 * notice we build the stack from down to top: MDD first, then MDT */
3864 name_size = MAX_OBD_NAME;
3865 uuid_size = MAX_OBD_NAME;
3867 OBD_ALLOC(name, name_size);
3868 OBD_ALLOC(uuid, uuid_size);
3869 if (name == NULL || uuid == NULL)
3870 GOTO(cleanup_mem, rc = -ENOMEM);
3872 OBD_ALLOC_PTR(bufs);
3874 GOTO(cleanup_mem, rc = -ENOMEM);
3877 p = strstr(name, "-MDT");
3879 GOTO(free_bufs, rc = -ENOMEM);
3882 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3884 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3885 if (lprof == NULL || lprof->lp_dt == NULL) {
3886 CERROR("can't find the profile: %s\n",
3887 lustre_cfg_string(cfg, 0));
3888 GOTO(free_bufs, rc = -EINVAL);
3891 lustre_cfg_bufs_reset(bufs, name);
3892 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3893 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3894 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3896 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3898 GOTO(put_profile, rc = -ENOMEM);
3900 rc = class_attach(lcfg);
3902 GOTO(lcfg_cleanup, rc);
3904 obd = class_name2obd(name);
3906 CERROR("Can not find obd %s (%s in config)\n",
3907 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
3908 GOTO(lcfg_cleanup, rc = -EINVAL);
3911 lustre_cfg_free(lcfg);
3913 lustre_cfg_bufs_reset(bufs, name);
3914 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3915 lustre_cfg_bufs_set_string(bufs, 2, dev);
3916 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3918 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3920 GOTO(class_detach, rc = -ENOMEM);
3922 rc = class_setup(obd, lcfg);
3924 GOTO(class_detach, rc);
3926 /* connect to MDD we just setup */
3927 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
3929 GOTO(class_detach, rc);
3931 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
3933 LASSERT(mdt_lu_site(mdt) == NULL);
3934 mdt->mdt_lu_dev.ld_site = site;
3935 site->ls_top_dev = &mdt->mdt_lu_dev;
3936 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
3938 /* now connect to bottom OSD */
3939 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
3940 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
3942 GOTO(class_detach, rc);
3944 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
3946 rc = lu_env_refill((struct lu_env *)env);
3948 CERROR("Failure to refill session: '%d'\n", rc);
3950 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
3955 class_detach(obd, lcfg);
3957 lustre_cfg_free(lcfg);
3959 class_put_profile(lprof);
3964 OBD_FREE(name, name_size);
3966 OBD_FREE(uuid, uuid_size);
3970 /* setup quota master target on MDT0 */
3971 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
3972 struct lustre_cfg *cfg)
3974 struct obd_device *obd;
3975 char *dev = lustre_cfg_string(cfg, 0);
3976 char *qmtname, *uuid, *p;
3977 struct lustre_cfg_bufs *bufs;
3978 struct lustre_cfg *lcfg;
3979 struct lustre_profile *lprof;
3980 struct obd_connect_data *data;
3984 LASSERT(mdt->mdt_qmt_exp == NULL);
3985 LASSERT(mdt->mdt_qmt_dev == NULL);
3987 /* quota master is on MDT0 only for now */
3988 if (mdt->mdt_seq_site.ss_node_id != 0)
3991 /* MGS generates config commands which look as follows:
3992 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3993 * 3:lustre-MDT0000-mdtlov 4:f
3995 * We generate the QMT name from the MDT one, just replacing MD with QM
3996 * after all the preparations, the logical equivalent will be:
3997 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
3998 * 3:lustre-MDT0000-osd 4:f */
3999 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4000 OBD_ALLOC(uuid, UUID_MAX);
4001 OBD_ALLOC_PTR(bufs);
4002 OBD_ALLOC_PTR(data);
4003 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4004 GOTO(cleanup_mem, rc = -ENOMEM);
4006 strcpy(qmtname, dev);
4007 p = strstr(qmtname, "-MDT");
4009 GOTO(cleanup_mem, rc = -ENOMEM);
4010 /* replace MD with QM */
4014 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4016 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4017 if (lprof == NULL || lprof->lp_dt == NULL) {
4018 CERROR("can't find profile for %s\n",
4019 lustre_cfg_string(cfg, 0));
4020 GOTO(cleanup_mem, rc = -EINVAL);
4023 lustre_cfg_bufs_reset(bufs, qmtname);
4024 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4025 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4026 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4028 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4030 GOTO(put_profile, rc = -ENOMEM);
4032 rc = class_attach(lcfg);
4034 GOTO(lcfg_cleanup, rc);
4036 obd = class_name2obd(qmtname);
4038 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4039 lustre_cfg_string(cfg, 0));
4040 GOTO(lcfg_cleanup, rc = -EINVAL);
4043 lustre_cfg_free(lcfg);
4045 lustre_cfg_bufs_reset(bufs, qmtname);
4046 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4047 lustre_cfg_bufs_set_string(bufs, 2, dev);
4049 /* for quota, the next device should be the OSD device */
4050 lustre_cfg_bufs_set_string(bufs, 3,
4051 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4053 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4055 GOTO(class_detach, rc = -ENOMEM);
4057 rc = class_setup(obd, lcfg);
4059 GOTO(class_detach, rc);
4061 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4063 /* configure local quota objects */
4064 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4068 GOTO(class_cleanup, rc);
4070 /* connect to quota master target */
4071 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4072 data->ocd_version = LUSTRE_VERSION_CODE;
4073 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4076 CERROR("cannot connect to quota master device %s (%d)\n",
4078 GOTO(class_cleanup, rc);
4084 class_manual_cleanup(obd);
4085 mdt->mdt_qmt_dev = NULL;
4089 class_detach(obd, lcfg);
4091 lustre_cfg_free(lcfg);
4093 class_put_profile(lprof);
4098 OBD_FREE(qmtname, MAX_OBD_NAME);
4100 OBD_FREE(uuid, UUID_MAX);
4106 /* Shutdown quota master target associated with mdt */
4107 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4111 if (mdt->mdt_qmt_exp == NULL)
4113 LASSERT(mdt->mdt_qmt_dev != NULL);
4115 /* the qmt automatically shuts down when the mdt disconnects */
4116 obd_disconnect(mdt->mdt_qmt_exp);
4117 mdt->mdt_qmt_exp = NULL;
4118 mdt->mdt_qmt_dev = NULL;
4122 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4123 * for now. This will be removed along with converting rest of MDT code
4124 * to use tgt_session_info */
4125 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4127 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4130 rc = mdt_getxattr(info);
4132 mdt_thread_info_fini(info);
4136 static struct tgt_handler mdt_tgt_handlers[] = {
4137 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4138 0, MDS_CONNECT, mdt_tgt_connect,
4139 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4140 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4141 0, MDS_DISCONNECT, tgt_disconnect,
4142 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4143 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4144 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4145 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4146 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4147 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4148 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4149 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4151 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4152 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4153 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4154 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4155 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4156 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4157 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4158 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4160 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4161 mdt_hsm_ct_register),
4162 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4163 mdt_hsm_ct_unregister),
4164 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4166 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4168 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4169 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4171 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4176 static struct tgt_handler mdt_sec_ctx_ops[] = {
4177 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4178 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4179 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4182 static struct tgt_handler mdt_quota_ops[] = {
4183 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4186 static struct tgt_opc_slice mdt_common_slice[] = {
4188 .tos_opc_start = MDS_FIRST_OPC,
4189 .tos_opc_end = MDS_LAST_OPC,
4190 .tos_hs = mdt_tgt_handlers
4193 .tos_opc_start = OBD_FIRST_OPC,
4194 .tos_opc_end = OBD_LAST_OPC,
4195 .tos_hs = tgt_obd_handlers
4198 .tos_opc_start = LDLM_FIRST_OPC,
4199 .tos_opc_end = LDLM_LAST_OPC,
4200 .tos_hs = tgt_dlm_handlers
4203 .tos_opc_start = SEC_FIRST_OPC,
4204 .tos_opc_end = SEC_LAST_OPC,
4205 .tos_hs = mdt_sec_ctx_ops
4208 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4209 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4210 .tos_hs = tgt_out_handlers
4213 .tos_opc_start = FLD_FIRST_OPC,
4214 .tos_opc_end = FLD_LAST_OPC,
4215 .tos_hs = fld_handlers
4218 .tos_opc_start = SEQ_FIRST_OPC,
4219 .tos_opc_end = SEQ_LAST_OPC,
4220 .tos_hs = seq_handlers
4223 .tos_opc_start = QUOTA_DQACQ,
4224 .tos_opc_end = QUOTA_LAST_OPC,
4225 .tos_hs = mdt_quota_ops
4228 .tos_opc_start = LLOG_FIRST_OPC,
4229 .tos_opc_end = LLOG_LAST_OPC,
4230 .tos_hs = tgt_llog_handlers
4233 .tos_opc_start = LFSCK_FIRST_OPC,
4234 .tos_opc_end = LFSCK_LAST_OPC,
4235 .tos_hs = tgt_lfsck_handlers
4243 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4245 struct md_device *next = m->mdt_child;
4246 struct lu_device *d = &m->mdt_lu_dev;
4247 struct obd_device *obd = mdt2obd_dev(m);
4248 struct lfsck_stop stop;
4251 stop.ls_status = LS_PAUSED;
4253 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4255 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4256 target_recovery_fini(obd);
4257 ping_evictor_stop();
4259 if (m->mdt_opts.mo_coordinator)
4260 mdt_hsm_cdt_stop(m);
4262 mdt_hsm_cdt_fini(m);
4264 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4265 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4267 if (m->mdt_namespace != NULL)
4268 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4269 d->ld_obd->obd_force);
4271 obd_exports_barrier(obd);
4272 obd_zombie_barrier();
4276 tgt_fini(env, &m->mdt_lut);
4277 mdt_fs_cleanup(env, m);
4278 upcall_cache_cleanup(m->mdt_identity_cache);
4279 m->mdt_identity_cache = NULL;
4281 if (m->mdt_namespace != NULL) {
4282 ldlm_namespace_free_post(m->mdt_namespace);
4283 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4286 mdt_quota_fini(env, m);
4288 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4290 mdt_seq_fini(env, m);
4291 mdt_fld_fini(env, m);
4296 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4298 LASSERT(atomic_read(&d->ld_ref) == 0);
4300 server_put_mount(mdt_obd_name(m), true);
4305 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4307 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4308 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4310 struct mdt_thread_info *info;
4311 struct obd_device *obd;
4312 const char *dev = lustre_cfg_string(cfg, 0);
4313 const char *num = lustre_cfg_string(cfg, 2);
4314 struct lustre_mount_info *lmi = NULL;
4315 struct lustre_sb_info *lsi;
4317 struct seq_server_site *ss_site;
4318 const char *identity_upcall = "NONE";
4319 struct md_device *next;
4325 lu_device_init(&m->mdt_lu_dev, ldt);
4327 * Environment (env) might be missing mdt_thread_key values at that
4328 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4331 * Usually device allocation path doesn't use module key values, but
4332 * mdt has to do a lot of work here, so allocate key value.
4334 rc = lu_env_refill((struct lu_env *)env);
4338 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4339 LASSERT(info != NULL);
4341 obd = class_name2obd(dev);
4342 LASSERT(obd != NULL);
4344 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4345 m->mdt_opts.mo_evict_tgt_nids = 1;
4346 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4348 /* default is coordinator off, it is started through conf_param
4350 m->mdt_opts.mo_coordinator = 0;
4352 lmi = server_get_mount(dev);
4354 CERROR("Cannot get mount info for %s!\n", dev);
4357 lsi = s2lsi(lmi->lmi_sb);
4358 /* CMD is supported only in IAM mode */
4360 node_id = simple_strtol(num, NULL, 10);
4361 obd->u.obt.obt_magic = OBT_MAGIC;
4362 if (lsi->lsi_lmd != NULL &&
4363 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4364 m->mdt_skip_lfsck = 1;
4367 m->mdt_squash.rsi_uid = 0;
4368 m->mdt_squash.rsi_gid = 0;
4369 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4370 init_rwsem(&m->mdt_squash.rsi_sem);
4371 spin_lock_init(&m->mdt_osfs_lock);
4372 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4373 m->mdt_enable_remote_dir = 0;
4374 m->mdt_enable_remote_dir_gid = 0;
4376 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4377 m->mdt_lu_dev.ld_obd = obd;
4378 /* Set this lu_device to obd for error handling purposes. */
4379 obd->obd_lu_dev = &m->mdt_lu_dev;
4381 /* init the stack */
4382 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4384 CERROR("%s: Can't init device stack, rc %d\n",
4385 mdt_obd_name(m), rc);
4390 ss_site = mdt_seq_site(m);
4391 s->ld_seq_site = ss_site;
4394 /* set server index */
4395 ss_site->ss_node_id = node_id;
4397 /* failover is the default
4398 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4399 * assumed whose ss_node_id == 0 XXX
4401 obd->obd_replayable = 1;
4402 /* No connection accepted until configurations will finish */
4403 obd->obd_no_conn = 1;
4405 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4406 char *str = lustre_cfg_string(cfg, 4);
4407 if (strchr(str, 'n')) {
4408 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4409 obd->obd_replayable = 0;
4413 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4415 GOTO(err_fini_stack, rc);
4417 rc = mdt_seq_init(env, m);
4419 GOTO(err_fini_fld, rc);
4421 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4422 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4423 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4424 LDLM_NAMESPACE_SERVER,
4425 LDLM_NAMESPACE_GREEDY,
4427 if (m->mdt_namespace == NULL)
4428 GOTO(err_fini_seq, rc = -ENOMEM);
4430 m->mdt_namespace->ns_lvbp = m;
4431 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4433 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4434 /* set obd_namespace for compatibility with old code */
4435 obd->obd_namespace = m->mdt_namespace;
4437 rc = mdt_hsm_cdt_init(m);
4439 CERROR("%s: error initializing coordinator, rc %d\n",
4440 mdt_obd_name(m), rc);
4441 GOTO(err_free_ns, rc);
4444 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4445 OBD_FAIL_MDS_ALL_REQUEST_NET,
4446 OBD_FAIL_MDS_ALL_REPLY_NET);
4448 GOTO(err_free_hsm, rc);
4450 rc = mdt_fs_setup(env, m, obd, lsi);
4454 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4456 next = m->mdt_child;
4457 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4460 GOTO(err_fs_cleanup, rc);
4462 if (mntopts & MNTOPT_USERXATTR)
4463 m->mdt_opts.mo_user_xattr = 1;
4465 m->mdt_opts.mo_user_xattr = 0;
4467 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4469 GOTO(err_fs_cleanup, rc);
4471 if (mntopts & MNTOPT_ACL)
4472 m->mdt_opts.mo_acl = 1;
4474 m->mdt_opts.mo_acl = 0;
4476 /* XXX: to support suppgid for ACL, we enable identity_upcall
4477 * by default, otherwise, maybe got unexpected -EACCESS. */
4478 if (m->mdt_opts.mo_acl)
4479 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4481 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4483 &mdt_identity_upcall_cache_ops);
4484 if (IS_ERR(m->mdt_identity_cache)) {
4485 rc = PTR_ERR(m->mdt_identity_cache);
4486 m->mdt_identity_cache = NULL;
4487 GOTO(err_fs_cleanup, rc);
4490 rc = mdt_procfs_init(m, dev);
4492 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4493 GOTO(err_recovery, rc);
4496 rc = mdt_quota_init(env, m, cfg);
4498 GOTO(err_procfs, rc);
4500 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4501 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4502 "mdt_ldlm_client", m->mdt_ldlm_client);
4504 ping_evictor_start();
4506 /* recovery will be started upon mdt_prepare()
4507 * when the whole stack is complete and ready
4508 * to serve the requests */
4510 /* Reduce the initial timeout on an MDS because it doesn't need such
4511 * a long timeout as an OST does. Adaptive timeouts will adjust this
4512 * value appropriately. */
4513 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4514 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4520 target_recovery_fini(obd);
4521 upcall_cache_cleanup(m->mdt_identity_cache);
4522 m->mdt_identity_cache = NULL;
4524 mdt_fs_cleanup(env, m);
4526 tgt_fini(env, &m->mdt_lut);
4528 mdt_hsm_cdt_fini(m);
4530 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4531 obd->obd_namespace = m->mdt_namespace = NULL;
4533 mdt_seq_fini(env, m);
4535 mdt_fld_fini(env, m);
4537 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4540 server_put_mount(dev, true);
4544 /* For interoperability, the left element is old parameter, the right one
4545 * is the new version of the parameter, if some parameter is deprecated,
4546 * the new version should be set as NULL. */
4547 static struct cfg_interop_param mdt_interop_param[] = {
4548 { "mdt.group_upcall", NULL },
4549 { "mdt.quota_type", NULL },
4550 { "mdd.quota_type", NULL },
4551 { "mdt.rootsquash", "mdt.root_squash" },
4552 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4556 /* used by MGS to process specific configurations */
4557 static int mdt_process_config(const struct lu_env *env,
4558 struct lu_device *d, struct lustre_cfg *cfg)
4560 struct mdt_device *m = mdt_dev(d);
4561 struct md_device *md_next = m->mdt_child;
4562 struct lu_device *next = md2lu_dev(md_next);
4566 switch (cfg->lcfg_command) {
4568 struct obd_device *obd = d->ld_obd;
4570 /* For interoperability */
4571 struct cfg_interop_param *ptr = NULL;
4572 struct lustre_cfg *old_cfg = NULL;
4575 param = lustre_cfg_string(cfg, 1);
4576 if (param == NULL) {
4577 CERROR("param is empty\n");
4582 ptr = class_find_old_param(param, mdt_interop_param);
4584 if (ptr->new_param == NULL) {
4586 CWARN("For interoperability, skip this %s."
4587 " It is obsolete.\n", ptr->old_param);
4591 CWARN("Found old param %s, changed it to %s.\n",
4592 ptr->old_param, ptr->new_param);
4595 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4602 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4604 if (rc > 0 || rc == -ENOSYS) {
4605 /* is it an HSM var ? */
4606 rc = class_process_proc_param(PARAM_HSM,
4607 hsm_cdt_get_proc_vars(),
4609 if (rc > 0 || rc == -ENOSYS)
4610 /* we don't understand; pass it on */
4611 rc = next->ld_ops->ldo_process_config(env, next,
4615 if (old_cfg != NULL)
4616 lustre_cfg_free(cfg);
4621 /* others are passed further */
4622 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4628 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4629 const struct lu_object_header *hdr,
4630 struct lu_device *d)
4632 struct mdt_object *mo;
4636 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4638 struct lu_object *o;
4639 struct lu_object_header *h;
4642 h = &mo->mot_header;
4643 lu_object_header_init(h);
4644 lu_object_init(o, h, d);
4645 lu_object_add_top(h, o);
4646 o->lo_ops = &mdt_obj_ops;
4647 spin_lock_init(&mo->mot_write_lock);
4648 mutex_init(&mo->mot_lov_mutex);
4649 init_rwsem(&mo->mot_open_sem);
4655 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4656 const struct lu_object_conf *unused)
4658 struct mdt_device *d = mdt_dev(o->lo_dev);
4659 struct lu_device *under;
4660 struct lu_object *below;
4664 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4665 PFID(lu_object_fid(o)));
4667 under = &d->mdt_child->md_lu_dev;
4668 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4669 if (below != NULL) {
4670 lu_object_add(o, below);
4677 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4679 struct mdt_object *mo = mdt_obj(o);
4680 struct lu_object_header *h;
4684 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4685 PFID(lu_object_fid(o)));
4687 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4688 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4691 lu_object_header_fini(h);
4692 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4697 static int mdt_object_print(const struct lu_env *env, void *cookie,
4698 lu_printer_t p, const struct lu_object *o)
4700 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4702 return (*p)(env, cookie,
4703 LUSTRE_MDT_NAME"-object@%p(flags=%d, writecount=%d)",
4704 mdto, mdto->mot_flags, mdto->mot_write_count);
4707 static int mdt_prepare(const struct lu_env *env,
4708 struct lu_device *pdev,
4709 struct lu_device *cdev)
4711 struct mdt_device *mdt = mdt_dev(cdev);
4712 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4713 struct obd_device *obd = cdev->ld_obd;
4720 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4724 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4728 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4732 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4733 /* The LFSCK instance is registered just now, so it must be there when
4734 * register the namespace to such instance. */
4735 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4737 if (mdt->mdt_seq_site.ss_node_id == 0) {
4738 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4739 &mdt->mdt_md_root_fid);
4744 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4746 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4747 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4748 LASSERT(obd->obd_no_conn);
4749 spin_lock(&obd->obd_dev_lock);
4750 obd->obd_no_conn = 0;
4751 spin_unlock(&obd->obd_dev_lock);
4753 if (obd->obd_recovering == 0)
4754 mdt_postrecov(env, mdt);
4759 const struct lu_device_operations mdt_lu_ops = {
4760 .ldo_object_alloc = mdt_object_alloc,
4761 .ldo_process_config = mdt_process_config,
4762 .ldo_prepare = mdt_prepare,
4765 static const struct lu_object_operations mdt_obj_ops = {
4766 .loo_object_init = mdt_object_init,
4767 .loo_object_free = mdt_object_free,
4768 .loo_object_print = mdt_object_print
4771 static int mdt_obd_set_info_async(const struct lu_env *env,
4772 struct obd_export *exp,
4773 __u32 keylen, void *key,
4774 __u32 vallen, void *val,
4775 struct ptlrpc_request_set *set)
4781 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4782 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4790 * Match client and server connection feature flags.
4792 * Compute the compatibility flags for a connection request based on
4793 * features mutually supported by client and server.
4795 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4796 * must not be updated here, otherwise a partially initialized value may
4797 * be exposed. After the connection request is successfully processed,
4798 * the top-level MDT connect request handler atomically updates the export
4799 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4800 * reply. \see mdt_connect().
4802 * \param exp the obd_export associated with this client/target pair
4803 * \param mdt the target device for the connection
4804 * \param data stores data for this connect request
4807 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4808 * \retval -EBADE client and server feature requirements are incompatible
4810 static int mdt_connect_internal(struct obd_export *exp,
4811 struct mdt_device *mdt,
4812 struct obd_connect_data *data)
4814 LASSERT(data != NULL);
4816 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4817 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4819 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
4820 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
4821 CWARN("%s: client %s does not support ibits lock, either "
4822 "very old or an invalid client: flags "LPX64"\n",
4823 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
4824 data->ocd_connect_flags);
4828 if (!mdt->mdt_opts.mo_acl)
4829 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4831 if (!mdt->mdt_opts.mo_user_xattr)
4832 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4834 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4835 data->ocd_brw_size = min(data->ocd_brw_size,
4836 (__u32)MD_MAX_BRW_SIZE);
4837 if (data->ocd_brw_size == 0) {
4838 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4839 " ocd_version: %x ocd_grant: %d "
4840 "ocd_index: %u ocd_brw_size is "
4841 "unexpectedly zero, network data "
4842 "corruption? Refusing connection of this"
4845 exp->exp_client_uuid.uuid,
4846 exp, data->ocd_connect_flags, data->ocd_version,
4847 data->ocd_grant, data->ocd_index);
4852 /* NB: Disregard the rule against updating
4853 * exp_connect_data.ocd_connect_flags in this case, since
4854 * tgt_client_new() needs to know if this is a lightweight
4855 * connection, and it is safe to expose this flag before
4856 * connection processing completes. */
4857 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4858 spin_lock(&exp->exp_lock);
4859 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4860 spin_unlock(&exp->exp_lock);
4863 data->ocd_version = LUSTRE_VERSION_CODE;
4865 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4866 CWARN("%s: MDS requires FID support, but client not\n",
4871 if (OCD_HAS_FLAG(data, PINGLESS)) {
4872 if (ptlrpc_pinger_suppress_pings()) {
4873 spin_lock(&exp->exp_obd->obd_dev_lock);
4874 list_del_init(&exp->exp_obd_chain_timed);
4875 spin_unlock(&exp->exp_obd->obd_dev_lock);
4877 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4881 data->ocd_max_easize = mdt->mdt_max_ea_size;
4883 /* NB: Disregard the rule against updating
4884 * exp_connect_data.ocd_connect_flags in this case, since
4885 * tgt_client_new() needs to know if this is client supports
4886 * multiple modify RPCs, and it is safe to expose this flag before
4887 * connection processing completes. */
4888 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
4889 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
4890 spin_lock(&exp->exp_lock);
4891 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
4892 spin_unlock(&exp->exp_lock);
4898 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
4899 struct mdt_thread_info *info,
4900 struct mdt_file_data *mfd)
4902 struct lu_context ses;
4906 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
4911 lu_context_enter(&ses);
4913 mdt_ucred(info)->uc_valid = UCRED_OLD;
4914 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
4916 lu_context_exit(&ses);
4917 lu_context_fini(&ses);
4923 static int mdt_export_cleanup(struct obd_export *exp)
4925 struct list_head closing_list;
4926 struct mdt_export_data *med = &exp->exp_mdt_data;
4927 struct obd_device *obd = exp->exp_obd;
4928 struct mdt_device *mdt;
4929 struct mdt_thread_info *info;
4931 struct mdt_file_data *mfd, *n;
4935 INIT_LIST_HEAD(&closing_list);
4936 spin_lock(&med->med_open_lock);
4937 while (!list_empty(&med->med_open_head)) {
4938 struct list_head *tmp = med->med_open_head.next;
4939 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
4941 /* Remove mfd handle so it can't be found again.
4942 * We are consuming the mfd_list reference here. */
4943 class_handle_unhash(&mfd->mfd_handle);
4944 list_move_tail(&mfd->mfd_list, &closing_list);
4946 spin_unlock(&med->med_open_lock);
4947 mdt = mdt_dev(obd->obd_lu_dev);
4948 LASSERT(mdt != NULL);
4950 rc = lu_env_init(&env, LCT_MD_THREAD);
4954 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4955 LASSERT(info != NULL);
4956 memset(info, 0, sizeof *info);
4957 info->mti_env = &env;
4958 info->mti_mdt = mdt;
4959 info->mti_exp = exp;
4961 if (!list_empty(&closing_list)) {
4962 struct md_attr *ma = &info->mti_attr;
4964 /* Close any open files (which may also cause orphan
4966 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
4967 list_del_init(&mfd->mfd_list);
4968 ma->ma_need = ma->ma_valid = 0;
4970 /* This file is being closed due to an eviction, it
4971 * could have been modified and now dirty regarding to
4972 * HSM archive, check this!
4973 * The logic here is to mark a file dirty if there's a
4974 * chance it was dirtied before the client was evicted,
4975 * so that we don't have to wait for a release attempt
4976 * before finding out the file was actually dirty and
4977 * fail the release. Aggressively marking it dirty here
4978 * will cause the policy engine to attempt to
4979 * re-archive it; when rearchiving, we can compare the
4980 * current version to the HSM data_version and make the
4981 * archive request into a noop if it's not actually
4984 if (mfd->mfd_mode & FMODE_WRITE)
4985 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
4987 /* Don't unlink orphan on failover umount, LU-184 */
4988 if (exp->exp_flags & OBD_OPT_FAILOVER) {
4989 ma->ma_valid = MA_FLAGS;
4990 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
4992 mdt_mfd_close(info, mfd);
4995 info->mti_mdt = NULL;
4996 /* cleanup client slot early */
4997 /* Do not erase record for recoverable client. */
4998 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
4999 tgt_client_del(&env, exp);
5005 static int mdt_obd_disconnect(struct obd_export *exp)
5011 class_export_get(exp);
5013 rc = server_disconnect_export(exp);
5015 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5017 rc = mdt_export_cleanup(exp);
5018 nodemap_del_member(exp);
5019 class_export_put(exp);
5023 /* mds_connect copy */
5024 static int mdt_obd_connect(const struct lu_env *env,
5025 struct obd_export **exp, struct obd_device *obd,
5026 struct obd_uuid *cluuid,
5027 struct obd_connect_data *data,
5030 struct obd_export *lexp;
5031 struct lustre_handle conn = { 0 };
5032 struct mdt_device *mdt;
5034 lnet_nid_t *client_nid = localdata;
5037 LASSERT(env != NULL);
5038 if (!exp || !obd || !cluuid)
5041 mdt = mdt_dev(obd->obd_lu_dev);
5044 * first, check whether the stack is ready to handle requests
5045 * XXX: probably not very appropriate method is used now
5046 * at some point we should find a better one
5048 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5049 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5050 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5051 rc = obd_get_info(env, mdt->mdt_child_exp,
5052 sizeof(KEY_OSP_CONNECTED),
5053 KEY_OSP_CONNECTED, NULL, NULL);
5056 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5059 rc = class_connect(&conn, obd, cluuid);
5063 lexp = class_conn2export(&conn);
5064 LASSERT(lexp != NULL);
5066 rc = nodemap_add_member(*client_nid, lexp);
5067 if (rc != 0 && rc != -EEXIST)
5070 rc = mdt_connect_internal(lexp, mdt, data);
5072 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5075 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5076 rc = tgt_client_new(env, lexp);
5078 mdt_export_stats_init(obd, lexp, localdata);
5082 class_disconnect(lexp);
5083 nodemap_del_member(lexp);
5092 static int mdt_obd_reconnect(const struct lu_env *env,
5093 struct obd_export *exp, struct obd_device *obd,
5094 struct obd_uuid *cluuid,
5095 struct obd_connect_data *data,
5098 lnet_nid_t *client_nid = localdata;
5102 if (exp == NULL || obd == NULL || cluuid == NULL)
5105 rc = nodemap_add_member(*client_nid, exp);
5106 if (rc != 0 && rc != -EEXIST)
5109 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5111 mdt_export_stats_init(obd, exp, localdata);
5113 nodemap_del_member(exp);
5118 /* FIXME: Can we avoid using these two interfaces? */
5119 static int mdt_init_export(struct obd_export *exp)
5121 struct mdt_export_data *med = &exp->exp_mdt_data;
5125 INIT_LIST_HEAD(&med->med_open_head);
5126 spin_lock_init(&med->med_open_lock);
5127 mutex_init(&med->med_idmap_mutex);
5128 med->med_idmap = NULL;
5129 spin_lock(&exp->exp_lock);
5130 exp->exp_connecting = 1;
5131 spin_unlock(&exp->exp_lock);
5133 /* self-export doesn't need client data and ldlm initialization */
5134 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5135 &exp->exp_client_uuid)))
5138 rc = tgt_client_alloc(exp);
5142 rc = ldlm_init_export(exp);
5149 tgt_client_free(exp);
5151 CERROR("%s: Failed to initialize export: rc = %d\n",
5152 exp->exp_obd->obd_name, rc);
5156 static int mdt_destroy_export(struct obd_export *exp)
5160 if (exp_connect_rmtclient(exp))
5161 mdt_cleanup_idmap(&exp->exp_mdt_data);
5163 target_destroy_export(exp);
5164 /* destroy can be called from failed obd_setup, so
5165 * checking uuid is safer than obd_self_export */
5166 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5167 &exp->exp_client_uuid)))
5170 ldlm_destroy_export(exp);
5171 tgt_client_free(exp);
5173 LASSERT(list_empty(&exp->exp_outstanding_replies));
5174 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5179 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5180 struct linkea_data *ldata)
5184 LASSERT(ldata->ld_buf->lb_buf != NULL);
5186 if (!mdt_object_exists(mdt_obj))
5189 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5190 ldata->ld_buf, XATTR_NAME_LINK);
5191 if (rc == -ERANGE) {
5192 /* Buf was too small, figure out what we need. */
5193 lu_buf_free(ldata->ld_buf);
5194 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5195 ldata->ld_buf, XATTR_NAME_LINK);
5198 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5199 if (ldata->ld_buf->lb_buf == NULL)
5201 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5202 ldata->ld_buf, XATTR_NAME_LINK);
5207 return linkea_init(ldata);
5211 * Given an MDT object, try to look up the full path to the object.
5212 * Part of the MDT layer implementation of lfs fid2path.
5214 * \param[in] info Per-thread common data shared by MDT level handlers.
5215 * \param[in] obj Object to do path lookup of
5216 * \param[in,out] fp User-provided struct to store path information
5218 * \retval 0 Lookup successful, path information stored in fp
5219 * \retval -EAGAIN Lookup failed, usually because object is being moved
5220 * \retval negative errno if there was a problem
5222 static int mdt_path_current(struct mdt_thread_info *info,
5223 struct mdt_object *obj,
5224 struct getinfo_fid2path *fp)
5226 struct mdt_device *mdt = info->mti_mdt;
5227 struct mdt_object *mdt_obj;
5228 struct link_ea_header *leh;
5229 struct link_ea_entry *lee;
5230 struct lu_name *tmpname = &info->mti_name;
5231 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5232 struct lu_buf *buf = &info->mti_big_buf;
5235 struct linkea_data ldata = { NULL };
5240 /* temp buffer for path element, the buffer will be finally freed
5241 * in mdt_thread_info_fini */
5242 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5243 if (buf->lb_buf == NULL)
5247 ptr = fp->gf_path + fp->gf_pathlen - 1;
5250 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5252 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5253 * so checking root_fid can only happen on MDT0. */
5254 while (!lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid)) {
5255 struct lu_buf lmv_buf;
5257 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5258 if (IS_ERR(mdt_obj))
5259 GOTO(out, rc = PTR_ERR(mdt_obj));
5261 if (!mdt_object_exists(mdt_obj)) {
5262 mdt_object_put(info->mti_env, mdt_obj);
5263 GOTO(out, rc = -ENOENT);
5266 if (mdt_object_remote(mdt_obj)) {
5267 mdt_object_put(info->mti_env, mdt_obj);
5268 GOTO(remote_out, rc = -EREMOTE);
5271 rc = mdt_links_read(info, mdt_obj, &ldata);
5273 mdt_object_put(info->mti_env, mdt_obj);
5278 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5279 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5280 /* If set, use link #linkno for path lookup, otherwise use
5281 link #0. Only do this for the final path element. */
5282 if (first && fp->gf_linkno < leh->leh_reccount) {
5284 for (count = 0; count < fp->gf_linkno; count++) {
5285 lee = (struct link_ea_entry *)
5286 ((char *)lee + reclen);
5287 linkea_entry_unpack(lee, &reclen, tmpname,
5290 if (fp->gf_linkno < leh->leh_reccount - 1)
5291 /* indicate to user there are more links */
5295 lmv_buf.lb_buf = info->mti_xattr_buf;
5296 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5297 /* Check if it is slave stripes */
5298 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5299 &lmv_buf, XATTR_NAME_LMV);
5300 mdt_object_put(info->mti_env, mdt_obj);
5302 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5304 /* For slave stripes, get its master */
5305 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5306 fp->gf_fid = *tmpfid;
5309 } else if (rc < 0 && rc != -ENODATA) {
5315 /* Pack the name in the end of the buffer */
5316 ptr -= tmpname->ln_namelen;
5317 if (ptr - 1 <= fp->gf_path)
5318 GOTO(out, rc = -EOVERFLOW);
5319 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5322 /* keep the last resolved fid to the client, so the
5323 * client will build the left path on another MDT for
5325 fp->gf_fid = *tmpfid;
5331 ptr++; /* skip leading / */
5332 memmove(fp->gf_path, ptr, fp->gf_path + fp->gf_pathlen - ptr);
5339 * Given an MDT object, use mdt_path_current to get the path.
5340 * Essentially a wrapper to retry mdt_path_current a set number of times
5341 * if -EAGAIN is returned (usually because an object is being moved).
5343 * Part of the MDT layer implementation of lfs fid2path.
5345 * \param[in] info Per-thread common data shared by mdt level handlers.
5346 * \param[in] obj Object to do path lookup of
5347 * \param[in,out] fp User-provided struct for arguments and to store path
5350 * \retval 0 Lookup successful, path information stored in fp
5351 * \retval negative errno if there was a problem
5353 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5354 struct getinfo_fid2path *fp)
5356 struct mdt_device *mdt = info->mti_mdt;
5361 if (fp->gf_pathlen < 3)
5364 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5365 fp->gf_path[0] = '\0';
5369 /* Retry multiple times in case file is being moved */
5370 while (tries-- && rc == -EAGAIN)
5371 rc = mdt_path_current(info, obj, fp);
5377 * Get the full path of the provided FID, as of changelog record recno.
5379 * This checks sanity and looks up object for user provided FID
5380 * before calling the actual path lookup code.
5382 * Part of the MDT layer implementation of lfs fid2path.
5384 * \param[in] info Per-thread common data shared by mdt level handlers.
5385 * \param[in,out] fp User-provided struct for arguments and to store path
5388 * \retval 0 Lookup successful, path information and recno stored in fp
5389 * \retval -ENOENT, object does not exist
5390 * \retval negative errno if there was a problem
5392 static int mdt_fid2path(struct mdt_thread_info *info,
5393 struct getinfo_fid2path *fp)
5395 struct mdt_device *mdt = info->mti_mdt;
5396 struct mdt_object *obj;
5400 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5401 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5403 if (!fid_is_sane(&fp->gf_fid))
5406 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5407 CWARN("%s: "DFID" is invalid, sequence should be "
5408 ">= "LPX64"\n", mdt_obd_name(mdt),
5409 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5413 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5416 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5417 PFID(&fp->gf_fid), rc);
5421 if (mdt_object_remote(obj))
5423 else if (!mdt_object_exists(obj))
5429 mdt_object_put(info->mti_env, obj);
5430 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5431 PFID(&fp->gf_fid), rc);
5435 rc = mdt_path(info, obj, fp);
5437 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5438 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5440 mdt_object_put(info->mti_env, obj);
5445 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5446 void *val, int vallen)
5448 struct getinfo_fid2path *fpout, *fpin;
5451 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5454 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5455 lustre_swab_fid2path(fpin);
5457 memcpy(fpout, fpin, sizeof(*fpin));
5458 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5461 rc = mdt_fid2path(info, fpout);
5465 int mdt_get_info(struct tgt_session_info *tsi)
5475 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5477 CDEBUG(D_IOCTL, "No GETINFO key\n");
5478 RETURN(err_serious(-EFAULT));
5480 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5483 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5484 if (vallen == NULL) {
5485 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5486 tgt_name(tsi->tsi_tgt));
5487 RETURN(err_serious(-EFAULT));
5490 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5492 rc = req_capsule_server_pack(tsi->tsi_pill);
5494 RETURN(err_serious(rc));
5496 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5497 if (valout == NULL) {
5498 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5499 tgt_name(tsi->tsi_tgt));
5500 RETURN(err_serious(-EFAULT));
5503 if (KEY_IS(KEY_FID2PATH)) {
5504 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5506 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5507 mdt_thread_info_fini(info);
5514 /* Pass the ioc down */
5515 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5516 unsigned int cmd, int len, void *data)
5518 struct lu_context ioctl_session;
5519 struct md_device *next = mdt->mdt_child;
5523 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5526 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5527 lu_context_enter(&ioctl_session);
5528 env->le_ses = &ioctl_session;
5530 LASSERT(next->md_ops->mdo_iocontrol);
5531 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5533 lu_context_exit(&ioctl_session);
5534 lu_context_fini(&ioctl_session);
5538 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5540 struct obd_ioctl_data *data = karg;
5543 struct mdt_object *obj;
5544 struct mdt_lock_handle *lh;
5548 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5549 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5552 fid = (struct lu_fid *)data->ioc_inlbuf1;
5554 if (!fid_is_sane(fid))
5557 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5559 lh = &mti->mti_lh[MDT_LH_PARENT];
5560 mdt_lock_reg_init(lh, LCK_CR);
5562 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5564 RETURN(PTR_ERR(obj));
5566 if (mdt_object_remote(obj)) {
5569 * before calling version get the correct MDS should be
5570 * fid, this is error to find remote object here
5572 CERROR("nonlocal object "DFID"\n", PFID(fid));
5573 } else if (!mdt_object_exists(obj)) {
5574 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5577 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5578 *(__u64 *)data->ioc_inlbuf2 = version;
5581 mdt_object_unlock_put(mti, obj, lh, 1);
5585 /* ioctls on obd dev */
5586 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5587 void *karg, void *uarg)
5590 struct obd_device *obd = exp->exp_obd;
5591 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5592 struct dt_device *dt = mdt->mdt_bottom;
5596 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5597 rc = lu_env_init(&env, LCT_MD_THREAD);
5603 rc = mdt_device_sync(&env, mdt);
5605 case OBD_IOC_SET_READONLY:
5606 rc = dt->dd_ops->dt_ro(&env, dt);
5608 case OBD_IOC_ABORT_RECOVERY:
5609 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5610 obd->obd_force_abort_recovery = 1;
5611 target_stop_recovery_thread(obd);
5614 case OBD_IOC_CHANGELOG_REG:
5615 case OBD_IOC_CHANGELOG_DEREG:
5616 case OBD_IOC_CHANGELOG_CLEAR:
5617 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5619 case OBD_IOC_START_LFSCK: {
5620 struct md_device *next = mdt->mdt_child;
5621 struct obd_ioctl_data *data = karg;
5622 struct lfsck_start_param lsp;
5624 if (unlikely(data == NULL)) {
5629 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5630 lsp.lsp_index_valid = 0;
5631 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5634 case OBD_IOC_STOP_LFSCK: {
5635 struct md_device *next = mdt->mdt_child;
5636 struct obd_ioctl_data *data = karg;
5637 struct lfsck_stop stop;
5639 stop.ls_status = LS_STOPPED;
5640 /* Old lfsck utils may pass NULL @stop. */
5641 if (data->ioc_inlbuf1 == NULL)
5645 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5647 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5650 case OBD_IOC_GET_OBJ_VERSION: {
5651 struct mdt_thread_info *mti;
5652 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5653 memset(mti, 0, sizeof *mti);
5654 mti->mti_env = &env;
5658 rc = mdt_ioc_version_get(mti, karg);
5661 case OBD_IOC_CATLOGLIST: {
5662 struct mdt_thread_info *mti;
5664 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5665 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5666 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5667 &mti->mti_tmp_fid1);
5672 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5673 mdt_obd_name(mdt), cmd, rc);
5680 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5682 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5686 if (!mdt->mdt_skip_lfsck) {
5687 struct lfsck_start_param lsp;
5689 lsp.lsp_start = NULL;
5690 lsp.lsp_index_valid = 0;
5691 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5692 OBD_IOC_START_LFSCK,
5694 if (rc != 0 && rc != -EALREADY)
5695 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5696 mdt_obd_name(mdt), rc);
5699 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5703 static int mdt_obd_postrecov(struct obd_device *obd)
5708 rc = lu_env_init(&env, LCT_MD_THREAD);
5711 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5716 static struct obd_ops mdt_obd_device_ops = {
5717 .o_owner = THIS_MODULE,
5718 .o_set_info_async = mdt_obd_set_info_async,
5719 .o_connect = mdt_obd_connect,
5720 .o_reconnect = mdt_obd_reconnect,
5721 .o_disconnect = mdt_obd_disconnect,
5722 .o_init_export = mdt_init_export,
5723 .o_destroy_export = mdt_destroy_export,
5724 .o_iocontrol = mdt_iocontrol,
5725 .o_postrecov = mdt_obd_postrecov,
5728 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5729 struct lu_device *d)
5731 struct mdt_device *m = mdt_dev(d);
5738 static struct lu_device *mdt_device_free(const struct lu_env *env,
5739 struct lu_device *d)
5741 struct mdt_device *m = mdt_dev(d);
5744 lu_device_fini(&m->mdt_lu_dev);
5750 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5751 struct lu_device_type *t,
5752 struct lustre_cfg *cfg)
5754 struct lu_device *l;
5755 struct mdt_device *m;
5762 rc = mdt_init0(env, m, t, cfg);
5764 mdt_device_free(env, l);
5769 l = ERR_PTR(-ENOMEM);
5773 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5774 LU_KEY_INIT(mdt, struct mdt_thread_info);
5776 static void mdt_key_fini(const struct lu_context *ctx,
5777 struct lu_context_key *key, void* data)
5779 struct mdt_thread_info *info = data;
5781 if (info->mti_big_lmm) {
5782 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5783 info->mti_big_lmm = NULL;
5784 info->mti_big_lmmsize = 0;
5789 /* context key: mdt_thread_key */
5790 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5792 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5794 return lu_ucred(info->mti_env);
5797 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5799 return lu_ucred_check(info->mti_env);
5803 * Enable/disable COS (Commit On Sharing).
5805 * Set/Clear the COS flag in mdt options.
5807 * \param mdt mdt device
5808 * \param val 0 disables COS, other values enable COS
5810 void mdt_enable_cos(struct mdt_device *mdt, int val)
5815 mdt->mdt_opts.mo_cos = !!val;
5816 rc = lu_env_init(&env, LCT_LOCAL);
5817 if (unlikely(rc != 0)) {
5818 CWARN("%s: lu_env initialization failed, cannot "
5819 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
5822 mdt_device_sync(&env, mdt);
5827 * Check COS (Commit On Sharing) status.
5829 * Return COS flag status.
5831 * \param mdt mdt device
5833 int mdt_cos_is_enabled(struct mdt_device *mdt)
5835 return mdt->mdt_opts.mo_cos != 0;
5838 static struct lu_device_type_operations mdt_device_type_ops = {
5839 .ldto_device_alloc = mdt_device_alloc,
5840 .ldto_device_free = mdt_device_free,
5841 .ldto_device_fini = mdt_device_fini
5844 static struct lu_device_type mdt_device_type = {
5845 .ldt_tags = LU_DEVICE_MD,
5846 .ldt_name = LUSTRE_MDT_NAME,
5847 .ldt_ops = &mdt_device_type_ops,
5848 .ldt_ctx_tags = LCT_MD_THREAD
5851 static int __init mdt_init(void)
5855 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5856 FID_NOBRACE_LEN + 1);
5857 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5859 rc = lu_kmem_init(mdt_caches);
5863 rc = mds_mod_init();
5867 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
5868 LUSTRE_MDT_NAME, &mdt_device_type);
5873 lu_kmem_fini(mdt_caches);
5880 static void __exit mdt_exit(void)
5882 class_unregister_type(LUSTRE_MDT_NAME);
5884 lu_kmem_fini(mdt_caches);
5887 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
5888 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5889 MODULE_VERSION(LUSTRE_VERSION_STRING);
5890 MODULE_LICENSE("GPL");
5892 module_init(mdt_init);
5893 module_exit(mdt_exit);