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, 2014, 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 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 ldlm_mode_t mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
104 /* For HSM restore handles */
105 struct kmem_cache *mdt_hsm_cdt_kmem;
107 /* For HSM request handles */
108 struct kmem_cache *mdt_hsm_car_kmem;
110 static struct lu_kmem_descr mdt_caches[] = {
112 .ckd_cache = &mdt_object_kmem,
113 .ckd_name = "mdt_obj",
114 .ckd_size = sizeof(struct mdt_object)
117 .ckd_cache = &mdt_hsm_cdt_kmem,
118 .ckd_name = "mdt_cdt_restore_handle",
119 .ckd_size = sizeof(struct cdt_restore_handle)
122 .ckd_cache = &mdt_hsm_car_kmem,
123 .ckd_name = "mdt_cdt_agent_req",
124 .ckd_size = sizeof(struct cdt_agent_req)
131 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
135 return rep->lock_policy_res1 & op_flag;
138 void mdt_clear_disposition(struct mdt_thread_info *info,
139 struct ldlm_reply *rep, __u64 op_flag)
142 info->mti_opdata &= ~op_flag;
143 tgt_opdata_clear(info->mti_env, op_flag);
146 rep->lock_policy_res1 &= ~op_flag;
149 void mdt_set_disposition(struct mdt_thread_info *info,
150 struct ldlm_reply *rep, __u64 op_flag)
153 info->mti_opdata |= op_flag;
154 tgt_opdata_set(info->mti_env, op_flag);
157 rep->lock_policy_res1 |= op_flag;
160 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
162 lh->mlh_pdo_hash = 0;
163 lh->mlh_reg_mode = lm;
164 lh->mlh_rreg_mode = lm;
165 lh->mlh_type = MDT_REG_LOCK;
168 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
169 const struct lu_name *lname)
171 lh->mlh_reg_mode = lock_mode;
172 lh->mlh_rreg_mode = lock_mode;
173 lh->mlh_type = MDT_PDO_LOCK;
175 if (lu_name_is_valid(lname)) {
176 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
178 /* XXX Workaround for LU-2856
180 * Zero is a valid return value of full_name_hash, but
181 * several users of mlh_pdo_hash assume a non-zero
182 * hash value. We therefore map zero onto an
183 * arbitrary, but consistent value (1) to avoid
184 * problems further down the road. */
185 if (unlikely(lh->mlh_pdo_hash == 0))
186 lh->mlh_pdo_hash = 1;
188 lh->mlh_pdo_hash = 0;
192 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
193 struct mdt_lock_handle *lh)
199 * Any dir access needs couple of locks:
201 * 1) on part of dir we gonna take lookup/modify;
203 * 2) on whole dir to protect it from concurrent splitting and/or to
204 * flush client's cache for readdir().
206 * so, for a given mode and object this routine decides what lock mode
207 * to use for lock #2:
209 * 1) if caller's gonna lookup in dir then we need to protect dir from
210 * being splitted only - LCK_CR
212 * 2) if caller's gonna modify dir then we need to protect dir from
213 * being splitted and to flush cache - LCK_CW
215 * 3) if caller's gonna modify dir and that dir seems ready for
216 * splitting then we need to protect it from any type of access
217 * (lookup/modify/split) - LCK_EX --bzzz
220 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
221 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
224 * Ask underlaying level its opinion about preferable PDO lock mode
225 * having access type passed as regular lock mode:
227 * - MDL_MINMODE means that lower layer does not want to specify lock
230 * - MDL_NL means that no PDO lock should be taken. This is used in some
231 * cases. Say, for non-splittable directories no need to use PDO locks
234 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
235 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
237 if (mode != MDL_MINMODE) {
238 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
241 * Lower layer does not want to specify locking mode. We do it
242 * our selves. No special protection is needed, just flush
243 * client's cache on modification and allow concurrent
246 switch (lh->mlh_reg_mode) {
248 lh->mlh_pdo_mode = LCK_EX;
251 lh->mlh_pdo_mode = LCK_CR;
254 lh->mlh_pdo_mode = LCK_CW;
257 CERROR("Not expected lock type (0x%x)\n",
258 (int)lh->mlh_reg_mode);
263 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
267 static int mdt_getstatus(struct tgt_session_info *tsi)
269 struct mdt_thread_info *info = tsi2mdt_info(tsi);
270 struct mdt_device *mdt = info->mti_mdt;
271 struct mdt_body *repbody;
276 rc = mdt_check_ucred(info);
278 GOTO(out, rc = err_serious(rc));
280 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
281 GOTO(out, rc = err_serious(-ENOMEM));
283 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
284 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
285 repbody->mbo_valid |= OBD_MD_FLID;
289 mdt_thread_info_fini(info);
293 static int mdt_statfs(struct tgt_session_info *tsi)
295 struct ptlrpc_request *req = tgt_ses_req(tsi);
296 struct mdt_thread_info *info = tsi2mdt_info(tsi);
297 struct md_device *next = info->mti_mdt->mdt_child;
298 struct ptlrpc_service_part *svcpt;
299 struct obd_statfs *osfs;
304 svcpt = req->rq_rqbd->rqbd_svcpt;
306 /* This will trigger a watchdog timeout */
307 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
308 (MDT_SERVICE_WATCHDOG_FACTOR *
309 at_get(&svcpt->scp_at_estimate)) + 1);
311 rc = mdt_check_ucred(info);
313 GOTO(out, rc = err_serious(rc));
315 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
316 GOTO(out, rc = err_serious(-ENOMEM));
318 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
320 GOTO(out, rc = -EPROTO);
322 /** statfs information are cached in the mdt_device */
323 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
324 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
325 /** statfs data is too old, get up-to-date one */
326 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
329 spin_lock(&info->mti_mdt->mdt_osfs_lock);
330 info->mti_mdt->mdt_osfs = *osfs;
331 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
332 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
334 /** use cached statfs data */
335 spin_lock(&info->mti_mdt->mdt_osfs_lock);
336 *osfs = info->mti_mdt->mdt_osfs;
337 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
341 mdt_counter_incr(req, LPROC_MDT_STATFS);
343 mdt_thread_info_fini(info);
347 #ifdef CONFIG_FS_POSIX_ACL
349 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
351 * \param info thread info object
352 * \param repbody reply to pack ACLs into
353 * \param o mdt object of file to examine
354 * \param nodemap nodemap of client to reply to
356 * \retval -errno error getting or parsing ACL from disk
358 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
359 struct mdt_object *o, struct lu_nodemap *nodemap)
361 const struct lu_env *env = info->mti_env;
362 struct md_object *next = mdt_object_child(o);
363 struct lu_buf *buf = &info->mti_buf;
366 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
367 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
369 if (buf->lb_len == 0)
372 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
374 if (rc == -ENODATA) {
375 repbody->mbo_aclsize = 0;
376 repbody->mbo_valid |= OBD_MD_FLACL;
378 } else if (rc == -EOPNOTSUPP) {
381 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
382 mdt_obd_name(info->mti_mdt),
383 PFID(mdt_object_fid(o)), rc);
386 rc = nodemap_map_acl(nodemap, buf->lb_buf,
387 rc, NODEMAP_FS_TO_CLIENT);
388 /* if all ACLs mapped out, rc is still >= 0 */
390 CERROR("%s: nodemap_map_acl unable to parse "DFID
391 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
392 PFID(mdt_object_fid(o)), rc);
394 repbody->mbo_aclsize = rc;
395 repbody->mbo_valid |= OBD_MD_FLACL;
403 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
404 const struct lu_attr *attr, const struct lu_fid *fid)
406 struct md_attr *ma = &info->mti_attr;
407 struct obd_export *exp = info->mti_exp;
408 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
410 LASSERT(ma->ma_valid & MA_INODE);
412 if (attr->la_valid & LA_ATIME) {
413 b->mbo_atime = attr->la_atime;
414 b->mbo_valid |= OBD_MD_FLATIME;
416 if (attr->la_valid & LA_MTIME) {
417 b->mbo_mtime = attr->la_mtime;
418 b->mbo_valid |= OBD_MD_FLMTIME;
420 if (attr->la_valid & LA_CTIME) {
421 b->mbo_ctime = attr->la_ctime;
422 b->mbo_valid |= OBD_MD_FLCTIME;
424 if (attr->la_valid & LA_FLAGS) {
425 b->mbo_flags = attr->la_flags;
426 b->mbo_valid |= OBD_MD_FLFLAGS;
428 if (attr->la_valid & LA_NLINK) {
429 b->mbo_nlink = attr->la_nlink;
430 b->mbo_valid |= OBD_MD_FLNLINK;
432 if (attr->la_valid & LA_UID) {
433 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
434 NODEMAP_FS_TO_CLIENT,
436 b->mbo_valid |= OBD_MD_FLUID;
438 if (attr->la_valid & LA_GID) {
439 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
440 NODEMAP_FS_TO_CLIENT,
442 b->mbo_valid |= OBD_MD_FLGID;
444 b->mbo_mode = attr->la_mode;
445 if (attr->la_valid & LA_MODE)
446 b->mbo_valid |= OBD_MD_FLMODE;
447 if (attr->la_valid & LA_TYPE)
448 b->mbo_valid |= OBD_MD_FLTYPE;
452 b->mbo_valid |= OBD_MD_FLID;
453 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid="LPX64"\n",
454 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
458 mdt_body_reverse_idmap(info, b);
460 if (!(attr->la_valid & LA_TYPE))
463 b->mbo_rdev = attr->la_rdev;
464 b->mbo_size = attr->la_size;
465 b->mbo_blocks = attr->la_blocks;
467 if (!S_ISREG(attr->la_mode)) {
468 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
469 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
470 /* means no objects are allocated on osts. */
471 LASSERT(!(ma->ma_valid & MA_LOV));
472 /* just ignore blocks occupied by extend attributes on MDS */
474 /* if no object is allocated on osts, the size on mds is valid.
476 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
477 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
478 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
479 /* A released file stores its size on MDS. */
480 /* But return 1 block for released file, unless tools like tar
481 * will consider it fully sparse. (LU-3864)
483 if (unlikely(b->mbo_size == 0))
487 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
490 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
491 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
492 PFID(fid), (unsigned long long)b->mbo_size);
495 static inline int mdt_body_has_lov(const struct lu_attr *la,
496 const struct mdt_body *body)
498 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
499 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
502 void mdt_client_compatibility(struct mdt_thread_info *info)
504 struct mdt_body *body;
505 struct ptlrpc_request *req = mdt_info_req(info);
506 struct obd_export *exp = req->rq_export;
507 struct md_attr *ma = &info->mti_attr;
508 struct lu_attr *la = &ma->ma_attr;
511 if (exp_connect_layout(exp))
512 /* the client can deal with 16-bit lmm_stripe_count */
515 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
517 if (!mdt_body_has_lov(la, body))
520 /* now we have a reply with a lov for a client not compatible with the
521 * layout lock so we have to clean the layout generation number */
522 if (S_ISREG(la->la_mode))
523 ma->ma_lmm->lmm_layout_gen = 0;
527 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
528 struct mdt_object *o)
530 const struct lu_env *env = info->mti_env;
533 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
542 /* Is it a directory? Let's check for the LMV as well */
543 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
544 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
548 rc2 = mo_xattr_get(env, mdt_object_child(o),
550 XATTR_NAME_DEFAULT_LMV);
552 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
560 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
563 const struct lu_env *env = info->mti_env;
567 LASSERT(info->mti_big_lmm_used == 0);
568 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
572 /* big_lmm may need to be grown */
573 if (info->mti_big_lmmsize < rc) {
574 int size = size_roundup_power2(rc);
576 if (info->mti_big_lmmsize > 0) {
577 /* free old buffer */
578 LASSERT(info->mti_big_lmm);
579 OBD_FREE_LARGE(info->mti_big_lmm,
580 info->mti_big_lmmsize);
581 info->mti_big_lmm = NULL;
582 info->mti_big_lmmsize = 0;
585 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
586 if (info->mti_big_lmm == NULL)
588 info->mti_big_lmmsize = size;
590 LASSERT(info->mti_big_lmmsize >= rc);
592 info->mti_buf.lb_buf = info->mti_big_lmm;
593 info->mti_buf.lb_len = info->mti_big_lmmsize;
594 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
599 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
600 struct md_attr *ma, const char *name)
602 struct md_object *next = mdt_object_child(o);
603 struct lu_buf *buf = &info->mti_buf;
606 if (strcmp(name, XATTR_NAME_LOV) == 0) {
607 buf->lb_buf = ma->ma_lmm;
608 buf->lb_len = ma->ma_lmm_size;
609 LASSERT(!(ma->ma_valid & MA_LOV));
610 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
611 buf->lb_buf = ma->ma_lmv;
612 buf->lb_len = ma->ma_lmv_size;
613 LASSERT(!(ma->ma_valid & MA_LMV));
614 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
615 buf->lb_buf = ma->ma_lmv;
616 buf->lb_len = ma->ma_lmv_size;
617 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
622 rc = mo_xattr_get(info->mti_env, next, buf, name);
626 if (strcmp(name, XATTR_NAME_LOV) == 0) {
627 if (info->mti_big_lmm_used)
628 ma->ma_lmm = info->mti_big_lmm;
630 /* NOT return LOV EA with hole to old client. */
631 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
632 LOV_PATTERN_F_HOLE) &&
633 !(exp_connect_flags(info->mti_exp) &
634 OBD_CONNECT_LFSCK)) {
637 ma->ma_lmm_size = rc;
638 ma->ma_valid |= MA_LOV;
640 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
641 if (info->mti_big_lmm_used)
642 ma->ma_lmv = info->mti_big_lmm;
644 ma->ma_lmv_size = rc;
645 ma->ma_valid |= MA_LMV;
646 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
647 ma->ma_lmv_size = rc;
648 ma->ma_valid |= MA_LMV_DEF;
651 /* Update mdt_max_mdsize so all clients will be aware that */
652 if (info->mti_mdt->mdt_max_mdsize < rc)
653 info->mti_mdt->mdt_max_mdsize = rc;
656 } else if (rc == -ENODATA) {
659 } else if (rc == -ERANGE) {
660 /* Default LMV has fixed size, so it must be able to fit
661 * in the original buffer */
662 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
664 rc = mdt_big_xattr_get(info, o, name);
666 info->mti_big_lmm_used = 1;
674 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
675 struct mdt_object *o, struct lu_fid *pfid)
677 struct lu_buf *buf = &info->mti_buf;
678 struct link_ea_header *leh;
679 struct link_ea_entry *lee;
683 buf->lb_buf = info->mti_big_lmm;
684 buf->lb_len = info->mti_big_lmmsize;
685 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
686 buf, XATTR_NAME_LINK);
687 /* ignore errors, MA_PFID won't be set and it is
688 * up to the caller to treat this as an error */
689 if (rc == -ERANGE || buf->lb_len == 0) {
690 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
691 buf->lb_buf = info->mti_big_lmm;
692 buf->lb_len = info->mti_big_lmmsize;
697 if (rc < sizeof(*leh)) {
698 CERROR("short LinkEA on "DFID": rc = %d\n",
699 PFID(mdt_object_fid(o)), rc);
703 leh = (struct link_ea_header *) buf->lb_buf;
704 lee = (struct link_ea_entry *)(leh + 1);
705 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
706 leh->leh_magic = LINK_EA_MAGIC;
707 leh->leh_reccount = __swab32(leh->leh_reccount);
708 leh->leh_len = __swab64(leh->leh_len);
710 if (leh->leh_magic != LINK_EA_MAGIC)
712 if (leh->leh_reccount == 0)
715 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
716 fid_be_to_cpu(pfid, pfid);
721 int mdt_attr_get_complex(struct mdt_thread_info *info,
722 struct mdt_object *o, struct md_attr *ma)
724 const struct lu_env *env = info->mti_env;
725 struct md_object *next = mdt_object_child(o);
726 struct lu_buf *buf = &info->mti_buf;
727 int need = ma->ma_need;
734 if (mdt_object_exists(o) == 0)
735 GOTO(out, rc = -ENOENT);
736 mode = lu_object_attr(&next->mo_lu);
738 if (need & MA_INODE) {
739 ma->ma_need = MA_INODE;
740 rc = mo_attr_get(env, next, ma);
743 ma->ma_valid |= MA_INODE;
746 if (need & MA_PFID) {
747 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
749 ma->ma_valid |= MA_PFID;
750 /* ignore this error, parent fid is not mandatory */
754 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
755 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
760 if (need & MA_LMV && S_ISDIR(mode)) {
761 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
766 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
767 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
772 if (need & MA_HSM && S_ISREG(mode)) {
773 buf->lb_buf = info->mti_xattr_buf;
774 buf->lb_len = sizeof(info->mti_xattr_buf);
775 CLASSERT(sizeof(struct hsm_attrs) <=
776 sizeof(info->mti_xattr_buf));
777 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
778 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
780 ma->ma_valid |= MA_HSM;
781 else if (rc2 < 0 && rc2 != -ENODATA)
785 #ifdef CONFIG_FS_POSIX_ACL
786 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
787 buf->lb_buf = ma->ma_acl;
788 buf->lb_len = ma->ma_acl_size;
789 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
791 ma->ma_acl_size = rc2;
792 ma->ma_valid |= MA_ACL_DEF;
793 } else if (rc2 == -ENODATA) {
802 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
803 rc, ma->ma_valid, ma->ma_lmm);
807 static int mdt_getattr_internal(struct mdt_thread_info *info,
808 struct mdt_object *o, int ma_need)
810 struct md_object *next = mdt_object_child(o);
811 const struct mdt_body *reqbody = info->mti_body;
812 struct ptlrpc_request *req = mdt_info_req(info);
813 struct md_attr *ma = &info->mti_attr;
814 struct lu_attr *la = &ma->ma_attr;
815 struct req_capsule *pill = info->mti_pill;
816 const struct lu_env *env = info->mti_env;
817 struct mdt_body *repbody;
818 struct lu_buf *buffer = &info->mti_buf;
819 struct obd_export *exp = info->mti_exp;
820 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
825 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
826 RETURN(err_serious(-ENOMEM));
828 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
832 if (mdt_object_remote(o)) {
833 /* This object is located on remote node.*/
834 /* Return -ENOTSUPP for old client */
835 if (!mdt_is_dne_client(req->rq_export))
836 GOTO(out, rc = -ENOTSUPP);
838 repbody->mbo_fid1 = *mdt_object_fid(o);
839 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
843 if (reqbody->mbo_eadatasize > 0) {
844 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
845 if (buffer->lb_buf == NULL)
846 GOTO(out, rc = -EPROTO);
847 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
850 buffer->lb_buf = NULL;
852 ma_need &= ~(MA_LOV | MA_LMV);
853 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
854 mdt_obd_name(info->mti_mdt),
855 req->rq_export->exp_client_uuid.uuid);
858 /* If it is dir object and client require MEA, then we got MEA */
859 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
860 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
861 /* Assumption: MDT_MD size is enough for lmv size. */
862 ma->ma_lmv = buffer->lb_buf;
863 ma->ma_lmv_size = buffer->lb_len;
864 ma->ma_need = MA_INODE;
865 if (ma->ma_lmv_size > 0) {
866 if (reqbody->mbo_valid & OBD_MD_MEA)
867 ma->ma_need |= MA_LMV;
868 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
869 ma->ma_need |= MA_LMV_DEF;
872 ma->ma_lmm = buffer->lb_buf;
873 ma->ma_lmm_size = buffer->lb_len;
874 ma->ma_need = MA_INODE | MA_HSM;
875 if (ma->ma_lmm_size > 0)
876 ma->ma_need |= MA_LOV;
879 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
880 reqbody->mbo_valid & OBD_MD_FLDIREA &&
881 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
882 /* get default stripe info for this dir. */
883 ma->ma_need |= MA_LOV_DEF;
885 ma->ma_need |= ma_need;
887 rc = mdt_attr_get_complex(info, o, ma);
889 CERROR("%s: getattr error for "DFID": rc = %d\n",
890 mdt_obd_name(info->mti_mdt),
891 PFID(mdt_object_fid(o)), rc);
895 /* if file is released, check if a restore is running */
896 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
897 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
898 repbody->mbo_t_state = MS_RESTORE;
899 repbody->mbo_valid |= OBD_MD_TSTATE;
902 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
904 /* the Lustre protocol supposes to return default striping
905 * on the user-visible root if explicitly requested */
906 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
907 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
908 struct lu_fid rootfid;
909 struct mdt_object *root;
910 struct mdt_device *mdt = info->mti_mdt;
912 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
915 root = mdt_object_find(env, mdt, &rootfid);
917 RETURN(PTR_ERR(root));
918 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
919 mdt_object_put(info->mti_env, root);
921 CERROR("%s: getattr error for "DFID": rc = %d\n",
922 mdt_obd_name(info->mti_mdt),
923 PFID(mdt_object_fid(o)), rc);
928 if (likely(ma->ma_valid & MA_INODE))
929 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
933 if (mdt_body_has_lov(la, reqbody)) {
934 if (ma->ma_valid & MA_LOV) {
935 LASSERT(ma->ma_lmm_size);
936 repbody->mbo_eadatasize = ma->ma_lmm_size;
937 if (S_ISDIR(la->la_mode))
938 repbody->mbo_valid |= OBD_MD_FLDIREA;
940 repbody->mbo_valid |= OBD_MD_FLEASIZE;
941 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
943 if (ma->ma_valid & MA_LMV) {
944 /* Return -ENOTSUPP for old client */
945 if (!mdt_is_striped_client(req->rq_export))
948 LASSERT(S_ISDIR(la->la_mode));
949 mdt_dump_lmv(D_INFO, ma->ma_lmv);
950 repbody->mbo_eadatasize = ma->ma_lmv_size;
951 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
953 if (ma->ma_valid & MA_LMV_DEF) {
954 /* Return -ENOTSUPP for old client */
955 if (!mdt_is_striped_client(req->rq_export))
957 LASSERT(S_ISDIR(la->la_mode));
958 repbody->mbo_eadatasize = ma->ma_lmv_size;
959 repbody->mbo_valid |= (OBD_MD_FLDIREA |
962 } else if (S_ISLNK(la->la_mode) &&
963 reqbody->mbo_valid & OBD_MD_LINKNAME) {
964 buffer->lb_buf = ma->ma_lmm;
965 /* eadatasize from client includes NULL-terminator, so
966 * there is no need to read it */
967 buffer->lb_len = reqbody->mbo_eadatasize - 1;
968 rc = mo_readlink(env, next, buffer);
969 if (unlikely(rc <= 0)) {
970 CERROR("%s: readlink failed for "DFID": rc = %d\n",
971 mdt_obd_name(info->mti_mdt),
972 PFID(mdt_object_fid(o)), rc);
975 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
977 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
979 repbody->mbo_valid |= OBD_MD_LINKNAME;
980 /* we need to report back size with NULL-terminator
981 * because client expects that */
982 repbody->mbo_eadatasize = rc + 1;
983 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
984 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
985 "on "DFID ", expected %d\n",
986 mdt_obd_name(info->mti_mdt),
987 rc, PFID(mdt_object_fid(o)),
988 reqbody->mbo_eadatasize - 1);
990 ((char *)ma->ma_lmm)[rc] = 0;
992 /* If the total CDEBUG() size is larger than a page, it
993 * will print a warning to the console, avoid this by
994 * printing just the last part of the symlink. */
995 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
996 print_limit < rc ? "..." : "", print_limit,
997 (char *)ma->ma_lmm + rc - print_limit, rc);
1002 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1003 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1004 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1005 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1006 repbody->mbo_max_mdsize);
1009 if (exp_connect_rmtclient(info->mti_exp) &&
1010 reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
1011 void *buf = req_capsule_server_get(pill, &RMF_ACL);
1013 /* mdt_getattr_lock only */
1014 rc = mdt_pack_remote_perm(info, o, buf);
1016 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
1017 repbody->mbo_aclsize = 0;
1020 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
1021 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
1024 #ifdef CONFIG_FS_POSIX_ACL
1025 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1026 (reqbody->mbo_valid & OBD_MD_FLACL))
1027 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1032 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1037 static int mdt_getattr(struct tgt_session_info *tsi)
1039 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1040 struct mdt_object *obj = info->mti_object;
1041 struct req_capsule *pill = info->mti_pill;
1042 struct mdt_body *reqbody;
1043 struct mdt_body *repbody;
1047 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1049 LASSERT(obj != NULL);
1050 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1052 /* Unlike intent case where we need to pre-fill out buffers early on
1053 * in intent policy for ldlm reasons, here we can have a much better
1054 * guess at EA size by just reading it from disk.
1055 * Exceptions are readdir and (missing) directory striping */
1057 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1058 /* No easy way to know how long is the symlink, but it cannot
1059 * be more than PATH_MAX, so we allocate +1 */
1062 /* A special case for fs ROOT: getattr there might fetch
1063 * default EA for entire fs, not just for this dir!
1065 } else if (lu_fid_eq(mdt_object_fid(obj),
1066 &info->mti_mdt->mdt_md_root_fid) &&
1067 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1068 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1070 /* Should the default strping be bigger, mdt_fix_reply
1071 * will reallocate */
1072 rc = DEF_REP_MD_SIZE;
1074 /* Read the actual EA size from disk */
1075 rc = mdt_attr_get_eabuf_size(info, obj);
1079 GOTO(out, rc = err_serious(rc));
1081 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1083 rc = req_capsule_server_pack(pill);
1084 if (unlikely(rc != 0))
1085 GOTO(out, rc = err_serious(rc));
1087 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1088 LASSERT(repbody != NULL);
1089 repbody->mbo_eadatasize = 0;
1090 repbody->mbo_aclsize = 0;
1092 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1093 rc = mdt_init_ucred(info, reqbody);
1095 rc = mdt_check_ucred(info);
1097 GOTO(out_shrink, rc);
1099 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1101 rc = mdt_getattr_internal(info, obj, 0);
1102 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1103 mdt_exit_ucred(info);
1106 mdt_client_compatibility(info);
1107 rc2 = mdt_fix_reply(info);
1111 mdt_thread_info_fini(info);
1116 * Exchange MOF_LOV_CREATED flags between two objects after a
1117 * layout swap. No assumption is made on whether o1 or o2 have
1118 * created objects or not.
1120 * \param[in,out] o1 First swap layout object
1121 * \param[in,out] o2 Second swap layout object
1123 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1127 mutex_lock(&o1->mot_lov_mutex);
1128 mutex_lock(&o2->mot_lov_mutex);
1130 o1_flags = o1->mot_flags;
1131 o1->mot_flags = (o1->mot_flags & ~MOF_LOV_CREATED) |
1132 (o2->mot_flags & MOF_LOV_CREATED);
1134 o2->mot_flags = (o2->mot_flags & ~MOF_LOV_CREATED) |
1135 (o1_flags & MOF_LOV_CREATED);
1137 mutex_unlock(&o2->mot_lov_mutex);
1138 mutex_unlock(&o1->mot_lov_mutex);
1141 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1143 struct mdt_thread_info *info;
1144 struct ptlrpc_request *req = tgt_ses_req(tsi);
1145 struct obd_export *exp = req->rq_export;
1146 struct mdt_object *o1, *o2, *o;
1147 struct mdt_lock_handle *lh1, *lh2;
1148 struct mdc_swap_layouts *msl;
1152 /* client does not support layout lock, so layout swaping
1154 * FIXME: there is a problem for old clients which don't support
1155 * layout lock yet. If those clients have already opened the file
1156 * they won't be notified at all so that old layout may still be
1157 * used to do IO. This can be fixed after file release is landed by
1158 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1159 if (!exp_connect_layout(exp))
1160 RETURN(-EOPNOTSUPP);
1162 info = tsi2mdt_info(tsi);
1164 if (info->mti_dlm_req != NULL)
1165 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1167 o1 = info->mti_object;
1168 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1169 &info->mti_body->mbo_fid2);
1171 GOTO(out, rc = PTR_ERR(o));
1173 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1174 GOTO(put, rc = -ENOENT);
1176 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1177 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1183 /* permission check. Make sure the calling process having permission
1184 * to write both files. */
1185 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1190 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1195 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1197 GOTO(put, rc = -EPROTO);
1199 lh1 = &info->mti_lh[MDT_LH_NEW];
1200 mdt_lock_reg_init(lh1, LCK_EX);
1201 lh2 = &info->mti_lh[MDT_LH_OLD];
1202 mdt_lock_reg_init(lh2, LCK_EX);
1204 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1205 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1209 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1210 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1214 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1215 mdt_object_child(o2), msl->msl_flags);
1219 mdt_swap_lov_flag(o1, o2);
1222 mdt_object_unlock(info, o2, lh2, rc);
1224 mdt_object_unlock(info, o1, lh1, rc);
1226 mdt_object_put(info->mti_env, o);
1228 mdt_thread_info_fini(info);
1232 static int mdt_raw_lookup(struct mdt_thread_info *info,
1233 struct mdt_object *parent,
1234 const struct lu_name *lname,
1235 struct ldlm_reply *ldlm_rep)
1237 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1241 LASSERT(!info->mti_cross_ref);
1243 /* Only got the fid of this obj by name */
1244 fid_zero(child_fid);
1245 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1246 lname, child_fid, &info->mti_spec);
1248 struct mdt_body *repbody;
1250 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1251 repbody->mbo_fid1 = *child_fid;
1252 repbody->mbo_valid = OBD_MD_FLID;
1253 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1254 } else if (rc == -ENOENT) {
1255 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1262 * UPDATE lock should be taken against parent, and be released before exit;
1263 * child_bits lock should be taken against child, and be returned back:
1264 * (1)normal request should release the child lock;
1265 * (2)intent request will grant the lock to client.
1267 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1268 struct mdt_lock_handle *lhc,
1270 struct ldlm_reply *ldlm_rep)
1272 struct ptlrpc_request *req = mdt_info_req(info);
1273 struct mdt_body *reqbody = NULL;
1274 struct mdt_object *parent = info->mti_object;
1275 struct mdt_object *child;
1276 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1277 struct lu_name *lname = NULL;
1278 struct mdt_lock_handle *lhp = NULL;
1279 struct ldlm_lock *lock;
1286 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1287 LASSERT(ergo(is_resent,
1288 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1293 if (info->mti_cross_ref) {
1294 /* Only getattr on the child. Parent is on another node. */
1295 mdt_set_disposition(info, ldlm_rep,
1296 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1298 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1300 PFID(mdt_object_fid(child)), ldlm_rep);
1302 rc = mdt_check_resent_lock(info, child, lhc);
1305 } else if (rc > 0) {
1306 mdt_lock_handle_init(lhc);
1307 mdt_lock_reg_init(lhc, LCK_PR);
1310 * Object's name is on another MDS, no lookup or layout
1311 * lock is needed here but update lock is.
1313 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1314 MDS_INODELOCK_LAYOUT);
1315 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1317 rc = mdt_object_lock(info, child, lhc, child_bits,
1323 /* Finally, we can get attr for child. */
1324 if (!mdt_object_exists(child)) {
1325 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1327 "remote object doesn't exist.\n");
1328 mdt_object_unlock(info, child, lhc, 1);
1332 rc = mdt_getattr_internal(info, child, 0);
1333 if (unlikely(rc != 0))
1334 mdt_object_unlock(info, child, lhc, 1);
1339 lname = &info->mti_name;
1340 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1342 if (lu_name_is_valid(lname)) {
1343 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1344 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1345 PNAME(lname), ldlm_rep);
1347 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1348 if (unlikely(reqbody == NULL))
1349 RETURN(err_serious(-EPROTO));
1351 *child_fid = reqbody->mbo_fid2;
1353 if (unlikely(!fid_is_sane(child_fid)))
1354 RETURN(err_serious(-EINVAL));
1356 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1358 PFID(mdt_object_fid(parent)),
1359 PFID(&reqbody->mbo_fid2), ldlm_rep);
1362 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1364 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1365 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1367 "Parent doesn't exist!\n");
1371 if (mdt_object_remote(parent)) {
1372 CERROR("%s: parent "DFID" is on remote target\n",
1373 mdt_obd_name(info->mti_mdt),
1374 PFID(mdt_object_fid(parent)));
1378 if (lu_name_is_valid(lname)) {
1379 /* Always allow to lookup ".." */
1380 if (unlikely(lname->ln_namelen == 2 &&
1381 lname->ln_name[0] == '.' &&
1382 lname->ln_name[1] == '.'))
1383 info->mti_spec.sp_permitted = 1;
1385 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1386 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1391 /* step 1: lock parent only if parent is a directory */
1392 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1393 lhp = &info->mti_lh[MDT_LH_PARENT];
1394 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1395 rc = mdt_object_lock(info, parent, lhp,
1396 MDS_INODELOCK_UPDATE,
1398 if (unlikely(rc != 0))
1402 /* step 2: lookup child's fid by name */
1403 fid_zero(child_fid);
1404 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1405 child_fid, &info->mti_spec);
1407 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1410 GOTO(out_parent, rc);
1413 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1416 *step 3: find the child object by fid & lock it.
1417 * regardless if it is local or remote.
1419 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1420 * set parent dir fid the same as child fid in getattr by fid case
1421 * we should not lu_object_find() the object again, could lead
1422 * to hung if there is a concurrent unlink destroyed the object.
1424 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1425 mdt_object_get(info->mti_env, parent);
1428 child = mdt_object_find(info->mti_env, info->mti_mdt,
1432 if (unlikely(IS_ERR(child)))
1433 GOTO(out_parent, rc = PTR_ERR(child));
1435 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1436 rc = mdt_check_resent_lock(info, child, lhc);
1438 GOTO(out_child, rc);
1439 } else if (rc > 0) {
1440 mdt_lock_handle_init(lhc);
1441 mdt_lock_reg_init(lhc, LCK_PR);
1444 if (!mdt_object_exists(child)) {
1445 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1447 "Object doesn't exist!\n");
1448 GOTO(out_child, rc = -ENOENT);
1451 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1452 mdt_object_exists(child) && !mdt_object_remote(child)) {
1453 struct md_attr *ma = &info->mti_attr;
1456 ma->ma_need = MA_INODE;
1457 rc = mdt_attr_get_complex(info, child, ma);
1458 if (unlikely(rc != 0))
1459 GOTO(out_child, rc);
1461 /* If the file has not been changed for some time, we
1462 * return not only a LOOKUP lock, but also an UPDATE
1463 * lock and this might save us RPC on later STAT. For
1464 * directories, it also let negative dentry cache start
1465 * working for this dir. */
1466 if (ma->ma_valid & MA_INODE &&
1467 ma->ma_attr.la_valid & LA_CTIME &&
1468 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1469 ma->ma_attr.la_ctime < cfs_time_current_sec())
1470 child_bits |= MDS_INODELOCK_UPDATE;
1473 /* layout lock must be granted in a best-effort way
1474 * for IT operations */
1475 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1476 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1477 exp_connect_layout(info->mti_exp) &&
1478 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1479 !mdt_object_remote(child) && ldlm_rep != NULL) {
1480 /* try to grant layout lock for regular file. */
1486 child_bits |= MDS_INODELOCK_LAYOUT;
1487 /* try layout lock, it may fail to be granted due to
1488 * contention at LOOKUP or UPDATE */
1489 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1491 child_bits &= ~MDS_INODELOCK_LAYOUT;
1492 LASSERT(child_bits != 0);
1493 rc = mdt_object_lock(info, child, lhc,
1494 child_bits, MDT_CROSS_LOCK);
1499 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1500 * client will enqueue the lock to the remote MDT */
1501 if (mdt_object_remote(child))
1502 child_bits &= ~MDS_INODELOCK_UPDATE;
1503 rc = mdt_object_lock(info, child, lhc, child_bits,
1506 if (unlikely(rc != 0))
1507 GOTO(out_child, rc);
1510 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1512 /* finally, we can get attr for child. */
1513 rc = mdt_getattr_internal(info, child, ma_need);
1514 if (unlikely(rc != 0)) {
1515 mdt_object_unlock(info, child, lhc, 1);
1517 /* Debugging code. */
1518 LDLM_DEBUG(lock, "Returning lock to client");
1519 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1520 &lock->l_resource->lr_name),
1521 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1522 PLDLMRES(lock->l_resource),
1523 PFID(mdt_object_fid(child)));
1526 LDLM_LOCK_PUT(lock);
1530 mdt_object_put(info->mti_env, child);
1533 mdt_object_unlock(info, parent, lhp, 1);
1537 /* normal handler: should release the child lock */
1538 static int mdt_getattr_name(struct tgt_session_info *tsi)
1540 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1541 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1542 struct mdt_body *reqbody;
1543 struct mdt_body *repbody;
1547 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1548 LASSERT(reqbody != NULL);
1549 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1550 LASSERT(repbody != NULL);
1552 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1553 repbody->mbo_eadatasize = 0;
1554 repbody->mbo_aclsize = 0;
1556 rc = mdt_init_ucred(info, reqbody);
1558 GOTO(out_shrink, rc);
1560 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1561 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1562 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1563 lhc->mlh_reg_lh.cookie = 0;
1565 mdt_exit_ucred(info);
1568 mdt_client_compatibility(info);
1569 rc2 = mdt_fix_reply(info);
1572 mdt_thread_info_fini(info);
1576 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1577 void *karg, void *uarg);
1579 static int mdt_set_info(struct tgt_session_info *tsi)
1581 struct ptlrpc_request *req = tgt_ses_req(tsi);
1584 int keylen, vallen, rc = 0;
1588 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1590 DEBUG_REQ(D_HA, req, "no set_info key");
1591 RETURN(err_serious(-EFAULT));
1594 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1597 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1599 DEBUG_REQ(D_HA, req, "no set_info val");
1600 RETURN(err_serious(-EFAULT));
1603 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1606 /* Swab any part of val you need to here */
1607 if (KEY_IS(KEY_READ_ONLY)) {
1608 spin_lock(&req->rq_export->exp_lock);
1610 *exp_connect_flags_ptr(req->rq_export) |=
1613 *exp_connect_flags_ptr(req->rq_export) &=
1614 ~OBD_CONNECT_RDONLY;
1615 spin_unlock(&req->rq_export->exp_lock);
1616 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1617 struct changelog_setinfo *cs = val;
1619 if (vallen != sizeof(*cs)) {
1620 CERROR("%s: bad changelog_clear setinfo size %d\n",
1621 tgt_name(tsi->tsi_tgt), vallen);
1624 if (ptlrpc_req_need_swab(req)) {
1625 __swab64s(&cs->cs_recno);
1626 __swab32s(&cs->cs_id);
1629 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1637 static int mdt_readpage(struct tgt_session_info *tsi)
1639 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1640 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1641 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1642 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1643 struct mdt_body *repbody;
1649 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1650 RETURN(err_serious(-ENOMEM));
1652 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1653 if (repbody == NULL || reqbody == NULL)
1654 RETURN(err_serious(-EFAULT));
1657 * prepare @rdpg before calling lower layers and transfer itself. Here
1658 * reqbody->size contains offset of where to start to read and
1659 * reqbody->nlink contains number bytes to read.
1661 rdpg->rp_hash = reqbody->mbo_size;
1662 if (rdpg->rp_hash != reqbody->mbo_size) {
1663 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1664 rdpg->rp_hash, reqbody->mbo_size);
1668 rdpg->rp_attrs = reqbody->mbo_mode;
1669 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1670 rdpg->rp_attrs |= LUDA_64BITHASH;
1671 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1672 exp_max_brw_size(tsi->tsi_exp));
1673 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1675 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1676 if (rdpg->rp_pages == NULL)
1679 for (i = 0; i < rdpg->rp_npages; ++i) {
1680 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1681 if (rdpg->rp_pages[i] == NULL)
1682 GOTO(free_rdpg, rc = -ENOMEM);
1685 /* call lower layers to fill allocated pages with directory data */
1686 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1688 GOTO(free_rdpg, rc);
1690 /* send pages to client */
1691 rc = tgt_sendpage(tsi, rdpg, rc);
1696 for (i = 0; i < rdpg->rp_npages; i++)
1697 if (rdpg->rp_pages[i] != NULL)
1698 __free_page(rdpg->rp_pages[i]);
1699 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1701 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1707 static int mdt_reint_internal(struct mdt_thread_info *info,
1708 struct mdt_lock_handle *lhc,
1711 struct req_capsule *pill = info->mti_pill;
1712 struct mdt_body *repbody;
1717 rc = mdt_reint_unpack(info, op);
1719 CERROR("Can't unpack reint, rc %d\n", rc);
1720 RETURN(err_serious(rc));
1723 /* for replay (no_create) lmm is not needed, client has it already */
1724 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1725 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1728 /* llog cookies are always 0, the field is kept for compatibility */
1729 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1730 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1732 rc = req_capsule_server_pack(pill);
1734 CERROR("Can't pack response, rc %d\n", rc);
1735 RETURN(err_serious(rc));
1738 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1739 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1741 repbody->mbo_eadatasize = 0;
1742 repbody->mbo_aclsize = 0;
1745 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1747 /* for replay no cookkie / lmm need, because client have this already */
1748 if (info->mti_spec.no_create)
1749 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1750 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1752 rc = mdt_init_ucred_reint(info);
1754 GOTO(out_shrink, rc);
1756 rc = mdt_fix_attr_ucred(info, op);
1758 GOTO(out_ucred, rc = err_serious(rc));
1760 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1761 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1762 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1763 GOTO(out_ucred, rc);
1765 rc = mdt_reint_rec(info, lhc);
1768 mdt_exit_ucred(info);
1770 mdt_client_compatibility(info);
1771 rc2 = mdt_fix_reply(info);
1777 static long mdt_reint_opcode(struct ptlrpc_request *req,
1778 const struct req_format **fmt)
1780 struct mdt_device *mdt;
1781 struct mdt_rec_reint *rec;
1784 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1786 opc = rec->rr_opcode;
1787 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1788 if (opc < REINT_MAX && fmt[opc] != NULL)
1789 req_capsule_extend(&req->rq_pill, fmt[opc]);
1791 mdt = mdt_exp2dev(req->rq_export);
1792 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1793 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1794 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1795 opc = err_serious(-EFAULT);
1798 opc = err_serious(-EFAULT);
1803 static int mdt_reint(struct tgt_session_info *tsi)
1807 static const struct req_format *reint_fmts[REINT_MAX] = {
1808 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1809 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1810 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1811 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1812 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1813 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1814 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1815 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1816 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1821 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1823 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1825 * No lock possible here from client to pass it to reint code
1828 rc = mdt_reint_internal(info, NULL, opc);
1829 mdt_thread_info_fini(info);
1834 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1838 /* this should sync the whole device */
1839 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1841 struct dt_device *dt = mdt->mdt_bottom;
1845 rc = dt->dd_ops->dt_sync(env, dt);
1849 /* this should sync this object */
1850 static int mdt_object_sync(struct mdt_thread_info *info)
1852 struct md_object *next;
1856 if (!mdt_object_exists(info->mti_object)) {
1857 CWARN("%s: non existing object "DFID": rc = %d\n",
1858 mdt_obd_name(info->mti_mdt),
1859 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1862 next = mdt_object_child(info->mti_object);
1863 rc = mo_object_sync(info->mti_env, next);
1868 static int mdt_sync(struct tgt_session_info *tsi)
1870 struct ptlrpc_request *req = tgt_ses_req(tsi);
1871 struct req_capsule *pill = tsi->tsi_pill;
1872 struct mdt_body *body;
1877 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1878 RETURN(err_serious(-ENOMEM));
1880 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
1881 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1883 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1885 /* sync an object */
1886 rc = mdt_object_sync(info);
1888 const struct lu_fid *fid;
1889 struct lu_attr *la = &info->mti_attr.ma_attr;
1891 info->mti_attr.ma_need = MA_INODE;
1892 info->mti_attr.ma_valid = 0;
1893 rc = mdt_attr_get_complex(info, info->mti_object,
1896 body = req_capsule_server_get(pill,
1898 fid = mdt_object_fid(info->mti_object);
1899 mdt_pack_attr2body(info, body, la, fid);
1902 mdt_thread_info_fini(info);
1905 mdt_counter_incr(req, LPROC_MDT_SYNC);
1911 * Handle quota control requests to consult current usage/limit, but also
1912 * to configure quota enforcement
1914 static int mdt_quotactl(struct tgt_session_info *tsi)
1916 struct obd_export *exp = tsi->tsi_exp;
1917 struct req_capsule *pill = tsi->tsi_pill;
1918 struct obd_quotactl *oqctl, *repoqc;
1920 struct mdt_device *mdt = mdt_exp2dev(exp);
1921 struct lu_device *qmt = mdt->mdt_qmt_dev;
1922 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
1925 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1927 RETURN(err_serious(-EPROTO));
1929 rc = req_capsule_server_pack(pill);
1931 RETURN(err_serious(rc));
1933 switch (oqctl->qc_cmd) {
1934 /* master quotactl */
1937 if (!nodemap_can_setquota(nodemap))
1942 RETURN(-EOPNOTSUPP);
1943 /* slave quotactl */
1948 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1952 /* map uid/gid for remote client */
1954 if (exp_connect_rmtclient(exp)) {
1955 struct lustre_idmap_table *idmap;
1957 idmap = exp->exp_mdt_data.med_idmap;
1959 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1960 oqctl->qc_cmd != Q_GETINFO))
1963 if (oqctl->qc_type == USRQUOTA)
1964 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1966 else if (oqctl->qc_type == GRPQUOTA)
1967 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1972 if (id == CFS_IDMAP_NOTFOUND) {
1973 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1978 if (oqctl->qc_type == USRQUOTA)
1979 id = nodemap_map_id(nodemap, NODEMAP_UID,
1980 NODEMAP_CLIENT_TO_FS, id);
1981 else if (oqctl->qc_type == GRPQUOTA)
1982 id = nodemap_map_id(nodemap, NODEMAP_UID,
1983 NODEMAP_CLIENT_TO_FS, id);
1985 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1987 RETURN(err_serious(-EFAULT));
1989 if (oqctl->qc_id != id)
1990 swap(oqctl->qc_id, id);
1992 switch (oqctl->qc_cmd) {
1998 /* forward quotactl request to QMT */
1999 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2004 /* slave quotactl */
2005 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2010 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2014 if (oqctl->qc_id != id)
2015 swap(oqctl->qc_id, id);
2021 /** clone llog ctxt from child (mdd)
2022 * This allows remote llog (replicator) access.
2023 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2024 * context was originally set up, or we can handle them directly.
2025 * I choose the latter, but that means I need any llog
2026 * contexts set up by child to be accessable by the mdt. So we clone the
2027 * context into our context list here.
2029 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2032 struct md_device *next = mdt->mdt_child;
2033 struct llog_ctxt *ctxt;
2036 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2039 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2040 if (rc || ctxt == NULL) {
2044 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2046 CERROR("Can't set mdt ctxt %d\n", rc);
2051 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2052 struct mdt_device *mdt, int idx)
2054 struct llog_ctxt *ctxt;
2056 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2059 /* Put once for the get we just did, and once for the clone */
2060 llog_ctxt_put(ctxt);
2061 llog_ctxt_put(ctxt);
2066 * sec context handlers
2068 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2072 rc = mdt_handle_idmap(tsi);
2074 struct ptlrpc_request *req = tgt_ses_req(tsi);
2077 opc = lustre_msg_get_opc(req->rq_reqmsg);
2078 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2079 sptlrpc_svc_ctx_invalidate(req);
2082 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2088 * quota request handlers
2090 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2092 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2093 struct lu_device *qmt = mdt->mdt_qmt_dev;
2098 RETURN(err_serious(-EOPNOTSUPP));
2100 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2104 struct mdt_object *mdt_object_new(const struct lu_env *env,
2105 struct mdt_device *d,
2106 const struct lu_fid *f)
2108 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2109 struct lu_object *o;
2110 struct mdt_object *m;
2113 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2114 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2115 if (unlikely(IS_ERR(o)))
2116 m = (struct mdt_object *)o;
2122 struct mdt_object *mdt_object_find(const struct lu_env *env,
2123 struct mdt_device *d,
2124 const struct lu_fid *f)
2126 struct lu_object *o;
2127 struct mdt_object *m;
2130 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2131 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2132 if (unlikely(IS_ERR(o)))
2133 m = (struct mdt_object *)o;
2141 * Asyncronous commit for mdt device.
2143 * Pass asynchonous commit call down the MDS stack.
2145 * \param env environment
2146 * \param mdt the mdt device
2148 static void mdt_device_commit_async(const struct lu_env *env,
2149 struct mdt_device *mdt)
2151 struct dt_device *dt = mdt->mdt_bottom;
2154 rc = dt->dd_ops->dt_commit_async(env, dt);
2155 if (unlikely(rc != 0))
2156 CWARN("%s: async commit start failed: rc = %d\n",
2157 mdt_obd_name(mdt), rc);
2161 * Mark the lock as "synchonous".
2163 * Mark the lock to deffer transaction commit to the unlock time.
2165 * \param lock the lock to mark as "synchonous"
2167 * \see mdt_is_lock_sync
2168 * \see mdt_save_lock
2170 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2172 lock->l_ast_data = (void*)1;
2176 * Check whehter the lock "synchonous" or not.
2178 * \param lock the lock to check
2179 * \retval 1 the lock is "synchonous"
2180 * \retval 0 the lock isn't "synchronous"
2182 * \see mdt_set_lock_sync
2183 * \see mdt_save_lock
2185 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2187 return lock->l_ast_data != NULL;
2191 * Blocking AST for mdt locks.
2193 * Starts transaction commit if in case of COS lock conflict or
2194 * deffers such a commit to the mdt_save_lock.
2196 * \param lock the lock which blocks a request or cancelling lock
2197 * \param desc unused
2198 * \param data unused
2199 * \param flag indicates whether this cancelling or blocking callback
2201 * \see ldlm_blocking_ast_nocheck
2203 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2204 void *data, int flag)
2206 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2207 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2211 if (flag == LDLM_CB_CANCELING)
2213 lock_res_and_lock(lock);
2214 if (lock->l_blocking_ast != mdt_blocking_ast) {
2215 unlock_res_and_lock(lock);
2218 if (mdt_cos_is_enabled(mdt) &&
2219 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2220 lock->l_blocking_lock != NULL &&
2221 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2222 mdt_set_lock_sync(lock);
2224 rc = ldlm_blocking_ast_nocheck(lock);
2226 /* There is no lock conflict if l_blocking_lock == NULL,
2227 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2228 * when the last reference to a local lock was released */
2229 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2232 rc = lu_env_init(&env, LCT_LOCAL);
2233 if (unlikely(rc != 0))
2234 CWARN("%s: lu_env initialization failed, cannot "
2235 "start asynchronous commit: rc = %d\n",
2238 mdt_device_commit_async(&env, mdt);
2244 /* Used for cross-MDT lock */
2245 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2246 void *data, int flag)
2248 struct lustre_handle lockh;
2252 case LDLM_CB_BLOCKING:
2253 ldlm_lock2handle(lock, &lockh);
2254 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2256 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2260 case LDLM_CB_CANCELING:
2261 LDLM_DEBUG(lock, "Revoke remote lock\n");
2269 int mdt_check_resent_lock(struct mdt_thread_info *info,
2270 struct mdt_object *mo,
2271 struct mdt_lock_handle *lhc)
2273 /* the lock might already be gotten in ldlm_handle_enqueue() */
2274 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2275 struct ptlrpc_request *req = mdt_info_req(info);
2276 struct ldlm_lock *lock;
2278 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2279 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2281 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2282 * a resend case, however, it could be already destroyed
2283 * due to client eviction or a raced cancel RPC. */
2284 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
2285 lhc->mlh_reg_lh.cookie);
2289 if (!fid_res_name_eq(mdt_object_fid(mo),
2290 &lock->l_resource->lr_name)) {
2291 CWARN("%s: Although resent, but still not "
2292 "get child lock:"DFID"\n",
2293 info->mti_exp->exp_obd->obd_name,
2294 PFID(mdt_object_fid(mo)));
2295 LDLM_LOCK_PUT(lock);
2298 LDLM_LOCK_PUT(lock);
2304 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2305 struct mdt_object *o, const struct lu_fid *fid,
2306 struct lustre_handle *lh, ldlm_mode_t mode,
2309 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2310 ldlm_policy_data_t *policy = &mti->mti_policy;
2311 struct ldlm_res_id *res_id = &mti->mti_res_id;
2315 LASSERT(mdt_object_remote(o));
2317 fid_build_reg_res_name(fid, res_id);
2319 memset(einfo, 0, sizeof(*einfo));
2320 einfo->ei_type = LDLM_IBITS;
2321 einfo->ei_mode = mode;
2322 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2323 einfo->ei_cb_cp = ldlm_completion_ast;
2324 einfo->ei_enq_slave = 0;
2325 einfo->ei_res_id = res_id;
2327 memset(policy, 0, sizeof(*policy));
2328 policy->l_inodebits.bits = ibits;
2330 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2335 static int mdt_object_local_lock(struct mdt_thread_info *info,
2336 struct mdt_object *o,
2337 struct mdt_lock_handle *lh, __u64 ibits,
2338 bool nonblock, int locality)
2340 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2341 ldlm_policy_data_t *policy = &info->mti_policy;
2342 struct ldlm_res_id *res_id = &info->mti_res_id;
2347 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2348 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2349 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2350 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2352 /* Only enqueue LOOKUP lock for remote object */
2353 if (mdt_object_remote(o))
2354 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2356 if (lh->mlh_type == MDT_PDO_LOCK) {
2357 /* check for exists after object is locked */
2358 if (mdt_object_exists(o) == 0) {
2359 /* Non-existent object shouldn't have PDO lock */
2362 /* Non-dir object shouldn't have PDO lock */
2363 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2368 memset(policy, 0, sizeof(*policy));
2369 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2371 dlmflags = LDLM_FL_ATOMIC_CB;
2373 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2376 * Take PDO lock on whole directory and build correct @res_id for lock
2377 * on part of directory.
2379 if (lh->mlh_pdo_hash != 0) {
2380 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2381 mdt_lock_pdo_mode(info, o, lh);
2382 if (lh->mlh_pdo_mode != LCK_NL) {
2384 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2385 * is never going to be sent to client and we do not
2386 * want it slowed down due to possible cancels.
2388 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2389 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2390 policy, res_id, dlmflags,
2391 info->mti_exp == NULL ? NULL :
2392 &info->mti_exp->exp_handle.h_cookie);
2393 if (unlikely(rc != 0))
2394 GOTO(out_unlock, rc);
2398 * Finish res_id initializing by name hash marking part of
2399 * directory which is taking modification.
2401 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2404 policy->l_inodebits.bits = ibits;
2407 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2408 * going to be sent to client. If it is - mdt_intent_policy() path will
2409 * fix it up and turn FL_LOCAL flag off.
2411 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2412 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2413 info->mti_exp == NULL ? NULL :
2414 &info->mti_exp->exp_handle.h_cookie);
2417 mdt_object_unlock(info, o, lh, 1);
2418 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2419 lh->mlh_pdo_hash != 0 &&
2420 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2421 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2427 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2428 struct mdt_lock_handle *lh, __u64 ibits,
2429 bool nonblock, int locality)
2434 if (!mdt_object_remote(o))
2435 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2438 if (locality == MDT_LOCAL_LOCK) {
2439 CERROR("%s: try to get local lock for remote object"
2440 DFID".\n", mdt_obd_name(info->mti_mdt),
2441 PFID(mdt_object_fid(o)));
2445 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2446 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2447 MDS_INODELOCK_XATTR);
2448 if (ibits & MDS_INODELOCK_UPDATE) {
2449 /* Sigh, PDO needs to enqueue 2 locks right now, but
2450 * enqueue RPC can only request 1 lock, to avoid extra
2451 * RPC, so it will instead enqueue EX lock for remote
2452 * object anyway XXX*/
2453 if (lh->mlh_type == MDT_PDO_LOCK &&
2454 lh->mlh_pdo_hash != 0) {
2455 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2456 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2457 PFID(mdt_object_fid(o)));
2458 lh->mlh_pdo_hash = 0;
2459 lh->mlh_rreg_mode = LCK_EX;
2460 lh->mlh_type = MDT_REG_LOCK;
2462 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2465 MDS_INODELOCK_UPDATE);
2470 /* Only enqueue LOOKUP lock for remote object */
2471 if (ibits & MDS_INODELOCK_LOOKUP) {
2472 rc = mdt_object_local_lock(info, o, lh,
2473 MDS_INODELOCK_LOOKUP,
2474 nonblock, locality);
2482 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2483 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2485 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2488 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2489 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2491 struct mdt_lock_handle tmp = *lh;
2494 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2502 * Save a lock within request object.
2504 * Keep the lock referenced until whether client ACK or transaction
2505 * commit happens or release the lock immediately depending on input
2506 * parameters. If COS is ON, a write lock is converted to COS lock
2509 * \param info thead info object
2510 * \param h lock handle
2511 * \param mode lock mode
2512 * \param decref force immediate lock releasing
2515 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2516 ldlm_mode_t mode, int decref)
2520 if (lustre_handle_is_used(h)) {
2521 if (decref || !info->mti_has_trans ||
2522 !(mode & (LCK_PW | LCK_EX))){
2523 mdt_fid_unlock(h, mode);
2525 struct mdt_device *mdt = info->mti_mdt;
2526 struct ldlm_lock *lock = ldlm_handle2lock(h);
2527 struct ptlrpc_request *req = mdt_info_req(info);
2530 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2532 /* there is no request if mdt_object_unlock() is called
2533 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2534 if (likely(req != NULL)) {
2535 CDEBUG(D_HA, "request = %p reply state = %p"
2536 " transno = "LPD64"\n", req,
2537 req->rq_reply_state, req->rq_transno);
2538 if (mdt_cos_is_enabled(mdt)) {
2540 ldlm_lock_downgrade(lock, LCK_COS);
2543 ptlrpc_save_lock(req, h, mode, no_ack);
2545 ldlm_lock_decref(h, mode);
2547 if (mdt_is_lock_sync(lock)) {
2548 CDEBUG(D_HA, "found sync-lock,"
2549 " async commit started\n");
2550 mdt_device_commit_async(info->mti_env,
2553 LDLM_LOCK_PUT(lock);
2562 * Unlock mdt object.
2564 * Immeditely release the regular lock and the PDO lock or save the
2565 * lock in request and keep them referenced until client ACK or
2566 * transaction commit.
2568 * \param info thread info object
2569 * \param o mdt object
2570 * \param lh mdt lock handle referencing regular and PDO locks
2571 * \param decref force immediate lock releasing
2573 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2574 struct mdt_lock_handle *lh, int decref)
2578 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2579 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2581 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2582 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2587 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2588 const struct lu_fid *f,
2589 struct mdt_lock_handle *lh,
2592 struct mdt_object *o;
2594 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2598 rc = mdt_object_lock(info, o, lh, ibits,
2601 mdt_object_put(info->mti_env, o);
2608 void mdt_object_unlock_put(struct mdt_thread_info * info,
2609 struct mdt_object * o,
2610 struct mdt_lock_handle *lh,
2613 mdt_object_unlock(info, o, lh, decref);
2614 mdt_object_put(info->mti_env, o);
2618 * Generic code handling requests that have struct mdt_body passed in:
2620 * - extract mdt_body from request and save it in @info, if present;
2622 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2625 * - if HABEO_CORPUS flag is set for this request type check whether object
2626 * actually exists on storage (lu_object_exists()).
2629 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2631 const struct mdt_body *body;
2632 struct mdt_object *obj;
2633 const struct lu_env *env;
2634 struct req_capsule *pill;
2638 env = info->mti_env;
2639 pill = info->mti_pill;
2641 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2645 if (!(body->mbo_valid & OBD_MD_FLID))
2648 if (!fid_is_sane(&body->mbo_fid1)) {
2649 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2653 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2655 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2656 mdt_object_put(env, obj);
2659 info->mti_object = obj;
2668 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2670 struct req_capsule *pill = info->mti_pill;
2674 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2675 rc = mdt_body_unpack(info, flags);
2679 if (rc == 0 && (flags & HABEO_REFERO)) {
2681 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2682 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2684 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2685 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2688 rc = req_capsule_server_pack(pill);
2693 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2695 lh->mlh_type = MDT_NUL_LOCK;
2696 lh->mlh_reg_lh.cookie = 0ull;
2697 lh->mlh_reg_mode = LCK_MINMODE;
2698 lh->mlh_pdo_lh.cookie = 0ull;
2699 lh->mlh_pdo_mode = LCK_MINMODE;
2700 lh->mlh_rreg_lh.cookie = 0ull;
2701 lh->mlh_rreg_mode = LCK_MINMODE;
2704 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2706 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2707 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2711 * Initialize fields of struct mdt_thread_info. Other fields are left in
2712 * uninitialized state, because it's too expensive to zero out whole
2713 * mdt_thread_info (> 1K) on each request arrival.
2715 void mdt_thread_info_init(struct ptlrpc_request *req,
2716 struct mdt_thread_info *info)
2720 info->mti_pill = &req->rq_pill;
2723 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2724 mdt_lock_handle_init(&info->mti_lh[i]);
2726 /* mdt device: it can be NULL while CONNECT */
2727 if (req->rq_export) {
2728 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2729 info->mti_exp = req->rq_export;
2731 info->mti_mdt = NULL;
2732 info->mti_env = req->rq_svc_thread->t_env;
2733 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2735 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2736 info->mti_big_buf = LU_BUF_NULL;
2737 info->mti_body = NULL;
2738 info->mti_object = NULL;
2739 info->mti_dlm_req = NULL;
2740 info->mti_has_trans = 0;
2741 info->mti_cross_ref = 0;
2742 info->mti_opdata = 0;
2743 info->mti_big_lmm_used = 0;
2745 info->mti_spec.no_create = 0;
2746 info->mti_spec.sp_rm_entry = 0;
2747 info->mti_spec.sp_permitted = 0;
2749 info->mti_spec.u.sp_ea.eadata = NULL;
2750 info->mti_spec.u.sp_ea.eadatalen = 0;
2753 void mdt_thread_info_fini(struct mdt_thread_info *info)
2757 if (info->mti_object != NULL) {
2758 mdt_object_put(info->mti_env, info->mti_object);
2759 info->mti_object = NULL;
2762 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2763 mdt_lock_handle_fini(&info->mti_lh[i]);
2764 info->mti_env = NULL;
2765 info->mti_pill = NULL;
2766 info->mti_exp = NULL;
2768 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2769 lu_buf_free(&info->mti_big_buf);
2772 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2774 struct mdt_thread_info *mti;
2776 mti = mdt_th_info(tsi->tsi_env);
2777 LASSERT(mti != NULL);
2779 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2780 if (tsi->tsi_corpus != NULL) {
2781 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2782 lu_object_get(tsi->tsi_corpus);
2784 mti->mti_body = tsi->tsi_mdt_body;
2785 mti->mti_dlm_req = tsi->tsi_dlm_req;
2790 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2792 struct ptlrpc_request *req = tgt_ses_req(tsi);
2797 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
2799 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
2800 set_current_state(TASK_UNINTERRUPTIBLE);
2801 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
2804 rc = tgt_connect(tsi);
2808 rc = mdt_init_idmap(tsi);
2813 obd_disconnect(class_export_get(req->rq_export));
2832 static int mdt_intent_getattr(enum mdt_it_code opcode,
2833 struct mdt_thread_info *info,
2834 struct ldlm_lock **,
2837 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2838 struct mdt_thread_info *info,
2839 struct ldlm_lock **lockp,
2842 static int mdt_intent_layout(enum mdt_it_code opcode,
2843 struct mdt_thread_info *info,
2844 struct ldlm_lock **,
2846 static int mdt_intent_reint(enum mdt_it_code opcode,
2847 struct mdt_thread_info *info,
2848 struct ldlm_lock **,
2851 static struct mdt_it_flavor {
2852 const struct req_format *it_fmt;
2854 int (*it_act)(enum mdt_it_code ,
2855 struct mdt_thread_info *,
2856 struct ldlm_lock **,
2859 } mdt_it_flavor[] = {
2861 .it_fmt = &RQF_LDLM_INTENT,
2862 /*.it_flags = HABEO_REFERO,*/
2864 .it_act = mdt_intent_reint,
2865 .it_reint = REINT_OPEN
2868 .it_fmt = &RQF_LDLM_INTENT,
2870 * OCREAT is not a MUTABOR request as if the file
2872 * We do the extra check of OBD_CONNECT_RDONLY in
2873 * mdt_reint_open() when we really need to create
2877 .it_act = mdt_intent_reint,
2878 .it_reint = REINT_OPEN
2881 .it_fmt = &RQF_LDLM_INTENT,
2882 .it_flags = MUTABOR,
2883 .it_act = mdt_intent_reint,
2884 .it_reint = REINT_CREATE
2886 [MDT_IT_GETATTR] = {
2887 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2888 .it_flags = HABEO_REFERO,
2889 .it_act = mdt_intent_getattr
2891 [MDT_IT_READDIR] = {
2897 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2898 .it_flags = HABEO_REFERO,
2899 .it_act = mdt_intent_getattr
2902 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2903 .it_flags = MUTABOR,
2905 .it_reint = REINT_UNLINK
2909 .it_flags = MUTABOR,
2912 [MDT_IT_GETXATTR] = {
2913 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2914 .it_flags = HABEO_CORPUS,
2915 .it_act = mdt_intent_getxattr
2918 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2920 .it_act = mdt_intent_layout
2925 mdt_intent_lock_replace(struct mdt_thread_info *info,
2926 struct ldlm_lock **lockp,
2927 struct mdt_lock_handle *lh,
2930 struct ptlrpc_request *req = mdt_info_req(info);
2931 struct ldlm_lock *lock = *lockp;
2932 struct ldlm_lock *new_lock;
2934 /* If possible resent found a lock, @lh is set to its handle */
2935 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2937 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2938 lh->mlh_reg_lh.cookie = 0;
2942 LASSERTF(new_lock != NULL,
2943 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2946 * If we've already given this lock to a client once, then we should
2947 * have no readers or writers. Otherwise, we should have one reader
2948 * _or_ writer ref (which will be zeroed below) before returning the
2951 if (new_lock->l_export == req->rq_export) {
2952 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2954 LASSERT(new_lock->l_export == NULL);
2955 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2960 if (new_lock->l_export == req->rq_export) {
2962 * Already gave this to the client, which means that we
2963 * reconstructed a reply.
2965 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2968 LDLM_LOCK_RELEASE(new_lock);
2969 lh->mlh_reg_lh.cookie = 0;
2970 RETURN(ELDLM_LOCK_REPLACED);
2974 * Fixup the lock to be given to the client.
2976 lock_res_and_lock(new_lock);
2977 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2978 * possible blocking AST. */
2979 while (new_lock->l_readers > 0) {
2980 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2981 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2982 new_lock->l_readers--;
2984 while (new_lock->l_writers > 0) {
2985 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2986 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2987 new_lock->l_writers--;
2990 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2991 new_lock->l_blocking_ast = lock->l_blocking_ast;
2992 new_lock->l_completion_ast = lock->l_completion_ast;
2993 new_lock->l_remote_handle = lock->l_remote_handle;
2994 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2996 unlock_res_and_lock(new_lock);
2998 cfs_hash_add(new_lock->l_export->exp_lock_hash,
2999 &new_lock->l_remote_handle,
3000 &new_lock->l_exp_hash);
3002 LDLM_LOCK_RELEASE(new_lock);
3003 lh->mlh_reg_lh.cookie = 0;
3005 RETURN(ELDLM_LOCK_REPLACED);
3008 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3009 struct ldlm_lock *new_lock,
3010 struct mdt_lock_handle *lh,
3013 struct ptlrpc_request *req = mdt_info_req(info);
3014 struct ldlm_request *dlmreq;
3016 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3019 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3021 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3022 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3024 if (flags & LDLM_FL_RESENT) {
3025 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3026 lh->mlh_reg_mode = new_lock->l_granted_mode;
3028 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3029 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3030 lh->mlh_reg_lh.cookie);
3035 * If the xid matches, then we know this is a resent request, and allow
3036 * it. (It's probably an OPEN, for which we don't send a lock.
3038 if (req_xid_is_last(req))
3042 * This remote handle isn't enqueued, so we never received or processed
3043 * this request. Clear MSG_RESENT, because it can be handled like any
3044 * normal request now.
3046 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3048 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3049 dlmreq->lock_handle[0].cookie);
3052 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3053 struct mdt_thread_info *info,
3054 struct ldlm_lock **lockp,
3057 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3058 struct ldlm_reply *ldlm_rep = NULL;
3062 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3063 * (for the resend case) or a new lock. Below we will use it to
3064 * replace the original lock.
3066 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3067 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3068 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3069 rc = mdt_object_lock(info, info->mti_object, lhc,
3070 MDS_INODELOCK_XATTR,
3076 grc = mdt_getxattr(info);
3078 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3080 if (mdt_info_req(info)->rq_repmsg != NULL)
3081 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3082 if (ldlm_rep == NULL)
3083 RETURN(err_serious(-EFAULT));
3085 ldlm_rep->lock_policy_res2 = grc;
3090 static int mdt_intent_getattr(enum mdt_it_code opcode,
3091 struct mdt_thread_info *info,
3092 struct ldlm_lock **lockp,
3095 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3097 struct ldlm_reply *ldlm_rep;
3098 struct mdt_body *reqbody;
3099 struct mdt_body *repbody;
3103 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3106 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3109 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3110 repbody->mbo_eadatasize = 0;
3111 repbody->mbo_aclsize = 0;
3115 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3117 case MDT_IT_GETATTR:
3118 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3122 CERROR("Unsupported intent (%d)\n", opcode);
3123 GOTO(out_shrink, rc = -EINVAL);
3126 rc = mdt_init_ucred(info, reqbody);
3128 GOTO(out_shrink, rc);
3130 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3131 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3133 /* Get lock from request for possible resent case. */
3134 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3136 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3137 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3139 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3140 ldlm_rep->lock_policy_res2 = 0;
3141 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3142 ldlm_rep->lock_policy_res2) {
3143 lhc->mlh_reg_lh.cookie = 0ull;
3144 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3147 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3150 mdt_exit_ucred(info);
3152 mdt_client_compatibility(info);
3153 rc2 = mdt_fix_reply(info);
3159 static int mdt_intent_layout(enum mdt_it_code opcode,
3160 struct mdt_thread_info *info,
3161 struct ldlm_lock **lockp,
3164 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3165 struct layout_intent *layout;
3167 struct mdt_object *obj = NULL;
3171 if (opcode != MDT_IT_LAYOUT) {
3172 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3177 fid = &info->mti_tmp_fid2;
3178 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3180 /* Get lock from request for possible resent case. */
3181 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3183 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3185 RETURN(PTR_ERR(obj));
3187 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3188 /* get the length of lsm */
3189 rc = mdt_attr_get_eabuf_size(info, obj);
3191 mdt_object_put(info->mti_env, obj);
3195 if (rc > info->mti_mdt->mdt_max_mdsize)