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, 2013, 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>
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 ldlm_mode_t mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
96 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
134 return (rep->lock_policy_res1 & flag);
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, int flag)
141 info->mti_opdata &= ~flag;
142 tgt_opdata_clear(info->mti_env, flag);
145 rep->lock_policy_res1 &= ~flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, int flag)
152 info->mti_opdata |= flag;
153 tgt_opdata_set(info->mti_env, flag);
156 rep->lock_policy_res1 |= flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
168 const struct lu_name *lname)
170 lh->mlh_reg_mode = lock_mode;
171 lh->mlh_rreg_mode = lock_mode;
172 lh->mlh_type = MDT_PDO_LOCK;
174 if (lu_name_is_valid(lname)) {
175 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
177 /* XXX Workaround for LU-2856
179 * Zero is a valid return value of full_name_hash, but
180 * several users of mlh_pdo_hash assume a non-zero
181 * hash value. We therefore map zero onto an
182 * arbitrary, but consistent value (1) to avoid
183 * problems further down the road. */
184 if (unlikely(lh->mlh_pdo_hash == 0))
185 lh->mlh_pdo_hash = 1;
187 lh->mlh_pdo_hash = 0;
191 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
192 struct mdt_lock_handle *lh)
198 * Any dir access needs couple of locks:
200 * 1) on part of dir we gonna take lookup/modify;
202 * 2) on whole dir to protect it from concurrent splitting and/or to
203 * flush client's cache for readdir().
205 * so, for a given mode and object this routine decides what lock mode
206 * to use for lock #2:
208 * 1) if caller's gonna lookup in dir then we need to protect dir from
209 * being splitted only - LCK_CR
211 * 2) if caller's gonna modify dir then we need to protect dir from
212 * being splitted and to flush cache - LCK_CW
214 * 3) if caller's gonna modify dir and that dir seems ready for
215 * splitting then we need to protect it from any type of access
216 * (lookup/modify/split) - LCK_EX --bzzz
219 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
220 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
223 * Ask underlaying level its opinion about preferable PDO lock mode
224 * having access type passed as regular lock mode:
226 * - MDL_MINMODE means that lower layer does not want to specify lock
229 * - MDL_NL means that no PDO lock should be taken. This is used in some
230 * cases. Say, for non-splittable directories no need to use PDO locks
233 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
234 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
236 if (mode != MDL_MINMODE) {
237 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
240 * Lower layer does not want to specify locking mode. We do it
241 * our selves. No special protection is needed, just flush
242 * client's cache on modification and allow concurrent
245 switch (lh->mlh_reg_mode) {
247 lh->mlh_pdo_mode = LCK_EX;
250 lh->mlh_pdo_mode = LCK_CR;
253 lh->mlh_pdo_mode = LCK_CW;
256 CERROR("Not expected lock type (0x%x)\n",
257 (int)lh->mlh_reg_mode);
262 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
266 static int mdt_getstatus(struct tgt_session_info *tsi)
268 struct mdt_thread_info *info = tsi2mdt_info(tsi);
269 struct mdt_device *mdt = info->mti_mdt;
270 struct mdt_body *repbody;
275 rc = mdt_check_ucred(info);
277 GOTO(out, rc = err_serious(rc));
279 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
280 GOTO(out, rc = err_serious(-ENOMEM));
282 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
283 repbody->fid1 = mdt->mdt_md_root_fid;
284 repbody->valid |= OBD_MD_FLID;
286 if (tsi->tsi_tgt->lut_mds_capa &&
287 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
288 struct mdt_object *root;
289 struct lustre_capa *capa;
291 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
293 GOTO(out, rc = PTR_ERR(root));
295 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
297 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
298 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
300 mdt_object_put(info->mti_env, root);
302 repbody->valid |= OBD_MD_FLMDSCAPA;
306 mdt_thread_info_fini(info);
310 static int mdt_statfs(struct tgt_session_info *tsi)
312 struct ptlrpc_request *req = tgt_ses_req(tsi);
313 struct mdt_thread_info *info = tsi2mdt_info(tsi);
314 struct md_device *next = info->mti_mdt->mdt_child;
315 struct ptlrpc_service_part *svcpt;
316 struct obd_statfs *osfs;
321 svcpt = req->rq_rqbd->rqbd_svcpt;
323 /* This will trigger a watchdog timeout */
324 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
325 (MDT_SERVICE_WATCHDOG_FACTOR *
326 at_get(&svcpt->scp_at_estimate)) + 1);
328 rc = mdt_check_ucred(info);
330 GOTO(out, rc = err_serious(rc));
332 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
333 GOTO(out, rc = err_serious(-ENOMEM));
335 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
337 GOTO(out, rc = -EPROTO);
339 /** statfs information are cached in the mdt_device */
340 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
341 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
342 /** statfs data is too old, get up-to-date one */
343 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
346 spin_lock(&info->mti_mdt->mdt_osfs_lock);
347 info->mti_mdt->mdt_osfs = *osfs;
348 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
349 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
351 /** use cached statfs data */
352 spin_lock(&info->mti_mdt->mdt_osfs_lock);
353 *osfs = info->mti_mdt->mdt_osfs;
354 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
358 mdt_counter_incr(req, LPROC_MDT_STATFS);
360 mdt_thread_info_fini(info);
365 * Pack SOM attributes into the reply.
366 * Call under a DLM UPDATE lock.
368 static void mdt_pack_size2body(struct mdt_thread_info *info,
369 struct mdt_object *mo)
372 struct md_attr *ma = &info->mti_attr;
374 LASSERT(ma->ma_attr.la_valid & LA_MODE);
375 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
377 /* Check if Size-on-MDS is supported, if this is a regular file,
378 * if SOM is enabled on the object and if SOM cache exists and valid.
379 * Otherwise do not pack Size-on-MDS attributes to the reply. */
380 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
381 !S_ISREG(ma->ma_attr.la_mode) ||
382 !mdt_object_is_som_enabled(mo) ||
383 !(ma->ma_valid & MA_SOM))
386 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
387 b->size = ma->ma_som->msd_size;
388 b->blocks = ma->ma_som->msd_blocks;
391 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
392 const struct lu_attr *attr, const struct lu_fid *fid)
394 struct md_attr *ma = &info->mti_attr;
396 LASSERT(ma->ma_valid & MA_INODE);
398 b->atime = attr->la_atime;
399 b->mtime = attr->la_mtime;
400 b->ctime = attr->la_ctime;
401 b->mode = attr->la_mode;
402 b->size = attr->la_size;
403 b->blocks = attr->la_blocks;
404 b->uid = attr->la_uid;
405 b->gid = attr->la_gid;
406 b->flags = attr->la_flags;
407 b->nlink = attr->la_nlink;
408 b->rdev = attr->la_rdev;
410 /*XXX should pack the reply body according to lu_valid*/
411 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
412 OBD_MD_FLGID | OBD_MD_FLTYPE |
413 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
414 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
416 if (!S_ISREG(attr->la_mode)) {
417 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
418 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
419 /* means no objects are allocated on osts. */
420 LASSERT(!(ma->ma_valid & MA_LOV));
421 /* just ignore blocks occupied by extend attributes on MDS */
423 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
424 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
425 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
426 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
427 /* A released file stores its size on MDS. */
428 /* But return 1 block for released file, unless tools like tar
429 * will consider it fully sparse. (LU-3864)
431 if (unlikely(b->size == 0))
435 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
440 b->valid |= OBD_MD_FLID;
441 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
442 PFID(fid), b->nlink, b->mode, b->size);
446 mdt_body_reverse_idmap(info, b);
448 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
449 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
450 PFID(fid), (unsigned long long)b->size);
453 static inline int mdt_body_has_lov(const struct lu_attr *la,
454 const struct mdt_body *body)
456 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
457 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
460 void mdt_client_compatibility(struct mdt_thread_info *info)
462 struct mdt_body *body;
463 struct ptlrpc_request *req = mdt_info_req(info);
464 struct obd_export *exp = req->rq_export;
465 struct md_attr *ma = &info->mti_attr;
466 struct lu_attr *la = &ma->ma_attr;
469 if (exp_connect_layout(exp))
470 /* the client can deal with 16-bit lmm_stripe_count */
473 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
475 if (!mdt_body_has_lov(la, body))
478 /* now we have a reply with a lov for a client not compatible with the
479 * layout lock so we have to clean the layout generation number */
480 if (S_ISREG(la->la_mode))
481 ma->ma_lmm->lmm_layout_gen = 0;
485 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
487 const struct lu_env *env = info->mti_env;
490 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
499 /* Is it a directory? Let's check for the LMV as well */
500 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
501 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
503 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
511 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
514 const struct lu_env *env = info->mti_env;
518 LASSERT(info->mti_big_lmm_used == 0);
519 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
523 /* big_lmm may need to be grown */
524 if (info->mti_big_lmmsize < rc) {
525 int size = size_roundup_power2(rc);
527 if (info->mti_big_lmmsize > 0) {
528 /* free old buffer */
529 LASSERT(info->mti_big_lmm);
530 OBD_FREE_LARGE(info->mti_big_lmm,
531 info->mti_big_lmmsize);
532 info->mti_big_lmm = NULL;
533 info->mti_big_lmmsize = 0;
536 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
537 if (info->mti_big_lmm == NULL)
539 info->mti_big_lmmsize = size;
541 LASSERT(info->mti_big_lmmsize >= rc);
543 info->mti_buf.lb_buf = info->mti_big_lmm;
544 info->mti_buf.lb_len = info->mti_big_lmmsize;
545 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
550 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
551 struct md_attr *ma, const char *name)
553 struct md_object *next = mdt_object_child(o);
554 struct lu_buf *buf = &info->mti_buf;
557 if (strcmp(name, XATTR_NAME_LOV) == 0) {
558 buf->lb_buf = ma->ma_lmm;
559 buf->lb_len = ma->ma_lmm_size;
560 LASSERT(!(ma->ma_valid & MA_LOV));
561 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
562 buf->lb_buf = ma->ma_lmv;
563 buf->lb_len = ma->ma_lmv_size;
564 LASSERT(!(ma->ma_valid & MA_LMV));
565 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
566 buf->lb_buf = ma->ma_lmv;
567 buf->lb_len = ma->ma_lmv_size;
568 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
573 rc = mo_xattr_get(info->mti_env, next, buf, name);
575 if (strcmp(name, XATTR_NAME_LOV) == 0) {
576 ma->ma_lmm_size = rc;
577 ma->ma_valid |= MA_LOV;
578 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
579 ma->ma_lmv_size = rc;
580 ma->ma_valid |= MA_LMV;
581 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
582 ma->ma_lmv_size = rc;
583 ma->ma_valid |= MA_LMV_DEF;
587 } else if (rc == -ENODATA) {
590 } else if (rc == -ERANGE) {
591 /* Default LMV has fixed size, so it must be able to fit
592 * in the original buffer */
593 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
595 rc = mdt_big_xattr_get(info, o, name);
597 info->mti_big_lmm_used = 1;
598 if (!strcmp(name, XATTR_NAME_LOV)) {
599 ma->ma_valid |= MA_LOV;
600 ma->ma_lmm = info->mti_big_lmm;
601 ma->ma_lmm_size = rc;
602 } else if (!strcmp(name, XATTR_NAME_LMV)) {
603 ma->ma_valid |= MA_LMV;
604 ma->ma_lmv = info->mti_big_lmm;
605 ma->ma_lmv_size = rc;
610 /* update mdt_max_mdsize so all clients
611 * will be aware about that */
612 if (info->mti_mdt->mdt_max_mdsize < rc)
613 info->mti_mdt->mdt_max_mdsize = rc;
621 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
622 struct mdt_object *o, struct lu_fid *pfid)
624 struct lu_buf *buf = &info->mti_buf;
625 struct link_ea_header *leh;
626 struct link_ea_entry *lee;
630 buf->lb_buf = info->mti_big_lmm;
631 buf->lb_len = info->mti_big_lmmsize;
632 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
633 buf, XATTR_NAME_LINK);
634 /* ignore errors, MA_PFID won't be set and it is
635 * up to the caller to treat this as an error */
636 if (rc == -ERANGE || buf->lb_len == 0) {
637 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
638 buf->lb_buf = info->mti_big_lmm;
639 buf->lb_len = info->mti_big_lmmsize;
644 if (rc < sizeof(*leh)) {
645 CERROR("short LinkEA on "DFID": rc = %d\n",
646 PFID(mdt_object_fid(o)), rc);
650 leh = (struct link_ea_header *) buf->lb_buf;
651 lee = (struct link_ea_entry *)(leh + 1);
652 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
653 leh->leh_magic = LINK_EA_MAGIC;
654 leh->leh_reccount = __swab32(leh->leh_reccount);
655 leh->leh_len = __swab64(leh->leh_len);
657 if (leh->leh_magic != LINK_EA_MAGIC)
659 if (leh->leh_reccount == 0)
662 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
663 fid_be_to_cpu(pfid, pfid);
668 int mdt_attr_get_complex(struct mdt_thread_info *info,
669 struct mdt_object *o, struct md_attr *ma)
671 const struct lu_env *env = info->mti_env;
672 struct md_object *next = mdt_object_child(o);
673 struct lu_buf *buf = &info->mti_buf;
674 u32 mode = lu_object_attr(&next->mo_lu);
675 int need = ma->ma_need;
681 if (need & MA_INODE) {
682 ma->ma_need = MA_INODE;
683 rc = mo_attr_get(env, next, ma);
686 ma->ma_valid |= MA_INODE;
689 if (need & MA_PFID) {
690 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
692 ma->ma_valid |= MA_PFID;
693 /* ignore this error, parent fid is not mandatory */
697 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
698 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
703 if (need & MA_LMV && S_ISDIR(mode)) {
704 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
709 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
710 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
715 if (need & MA_SOM && S_ISREG(mode)) {
716 buf->lb_buf = info->mti_xattr_buf;
717 buf->lb_len = sizeof(info->mti_xattr_buf);
718 CLASSERT(sizeof(struct som_attrs) <=
719 sizeof(info->mti_xattr_buf));
720 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
721 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
723 ma->ma_valid |= MA_SOM;
724 else if (rc2 < 0 && rc2 != -ENODATA)
728 if (need & MA_HSM && S_ISREG(mode)) {
729 buf->lb_buf = info->mti_xattr_buf;
730 buf->lb_len = sizeof(info->mti_xattr_buf);
731 CLASSERT(sizeof(struct hsm_attrs) <=
732 sizeof(info->mti_xattr_buf));
733 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
734 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
736 ma->ma_valid |= MA_HSM;
737 else if (rc2 < 0 && rc2 != -ENODATA)
741 #ifdef CONFIG_FS_POSIX_ACL
742 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
743 buf->lb_buf = ma->ma_acl;
744 buf->lb_len = ma->ma_acl_size;
745 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
747 ma->ma_acl_size = rc2;
748 ma->ma_valid |= MA_ACL_DEF;
749 } else if (rc2 == -ENODATA) {
758 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
759 rc, ma->ma_valid, ma->ma_lmm);
763 static int mdt_getattr_internal(struct mdt_thread_info *info,
764 struct mdt_object *o, int ma_need)
766 struct md_object *next = mdt_object_child(o);
767 const struct mdt_body *reqbody = info->mti_body;
768 struct ptlrpc_request *req = mdt_info_req(info);
769 struct md_attr *ma = &info->mti_attr;
770 struct lu_attr *la = &ma->ma_attr;
771 struct req_capsule *pill = info->mti_pill;
772 const struct lu_env *env = info->mti_env;
773 struct mdt_body *repbody;
774 struct lu_buf *buffer = &info->mti_buf;
779 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
780 RETURN(err_serious(-ENOMEM));
782 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
786 if (mdt_object_remote(o)) {
787 /* This object is located on remote node.*/
788 /* Return -ENOTSUPP for old client */
789 if (!mdt_is_dne_client(req->rq_export))
790 GOTO(out, rc = -ENOTSUPP);
792 repbody->fid1 = *mdt_object_fid(o);
793 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
797 if (reqbody->eadatasize > 0) {
798 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
799 if (buffer->lb_buf == NULL)
800 GOTO(out, rc = -EPROTO);
801 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
804 buffer->lb_buf = NULL;
806 ma_need &= ~(MA_LOV | MA_LMV);
807 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
808 mdt_obd_name(info->mti_mdt),
809 req->rq_export->exp_client_uuid.uuid);
812 /* If it is dir object and client require MEA, then we got MEA */
813 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
814 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
815 /* Assumption: MDT_MD size is enough for lmv size. */
816 ma->ma_lmv = buffer->lb_buf;
817 ma->ma_lmv_size = buffer->lb_len;
818 ma->ma_need = MA_INODE;
819 if (ma->ma_lmv_size > 0) {
820 if (reqbody->valid & OBD_MD_MEA)
821 ma->ma_need |= MA_LMV;
822 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
823 ma->ma_need |= MA_LMV_DEF;
826 ma->ma_lmm = buffer->lb_buf;
827 ma->ma_lmm_size = buffer->lb_len;
828 ma->ma_need = MA_INODE | MA_HSM;
829 if (ma->ma_lmm_size > 0)
830 ma->ma_need |= MA_LOV;
833 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
834 reqbody->valid & OBD_MD_FLDIREA &&
835 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
836 /* get default stripe info for this dir. */
837 ma->ma_need |= MA_LOV_DEF;
839 ma->ma_need |= ma_need;
840 if (ma->ma_need & MA_SOM)
841 ma->ma_som = &info->mti_u.som.data;
843 rc = mdt_attr_get_complex(info, o, ma);
845 CERROR("%s: getattr error for "DFID": rc = %d\n",
846 mdt_obd_name(info->mti_mdt),
847 PFID(mdt_object_fid(o)), rc);
851 /* if file is released, check if a restore is running */
852 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
853 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
854 repbody->t_state = MS_RESTORE;
855 repbody->valid |= OBD_MD_TSTATE;
858 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
860 /* the Lustre protocol supposes to return default striping
861 * on the user-visible root if explicitly requested */
862 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
863 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
864 struct lu_fid rootfid;
865 struct mdt_object *root;
866 struct mdt_device *mdt = info->mti_mdt;
868 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
871 root = mdt_object_find(env, mdt, &rootfid);
873 RETURN(PTR_ERR(root));
874 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
875 mdt_object_put(info->mti_env, root);
877 CERROR("%s: getattr error for "DFID": rc = %d\n",
878 mdt_obd_name(info->mti_mdt),
879 PFID(mdt_object_fid(o)), rc);
884 if (likely(ma->ma_valid & MA_INODE))
885 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
889 if (mdt_body_has_lov(la, reqbody)) {
890 if (ma->ma_valid & MA_LOV) {
891 LASSERT(ma->ma_lmm_size);
892 repbody->eadatasize = ma->ma_lmm_size;
893 if (S_ISDIR(la->la_mode))
894 repbody->valid |= OBD_MD_FLDIREA;
896 repbody->valid |= OBD_MD_FLEASIZE;
897 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
899 if (ma->ma_valid & MA_LMV) {
900 /* Return -ENOTSUPP for old client */
901 if (!mdt_is_striped_client(req->rq_export))
904 LASSERT(S_ISDIR(la->la_mode));
905 mdt_dump_lmv(D_INFO, ma->ma_lmv);
906 repbody->eadatasize = ma->ma_lmv_size;
907 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
909 if (ma->ma_valid & MA_LMV_DEF) {
910 /* Return -ENOTSUPP for old client */
911 if (!mdt_is_striped_client(req->rq_export))
913 LASSERT(S_ISDIR(la->la_mode));
914 repbody->eadatasize = ma->ma_lmv_size;
915 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
917 } else if (S_ISLNK(la->la_mode) &&
918 reqbody->valid & OBD_MD_LINKNAME) {
919 buffer->lb_buf = ma->ma_lmm;
920 /* eadatasize from client includes NULL-terminator, so
921 * there is no need to read it */
922 buffer->lb_len = reqbody->eadatasize - 1;
923 rc = mo_readlink(env, next, buffer);
924 if (unlikely(rc <= 0)) {
925 CERROR("%s: readlink failed for "DFID": rc = %d\n",
926 mdt_obd_name(info->mti_mdt),
927 PFID(mdt_object_fid(o)), rc);
930 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
932 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
934 repbody->valid |= OBD_MD_LINKNAME;
935 /* we need to report back size with NULL-terminator
936 * because client expects that */
937 repbody->eadatasize = rc + 1;
938 if (repbody->eadatasize != reqbody->eadatasize)
939 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
940 "on "DFID ", expected %d\n",
941 mdt_obd_name(info->mti_mdt),
942 rc, PFID(mdt_object_fid(o)),
943 reqbody->eadatasize - 1);
945 ((char *)ma->ma_lmm)[rc] = 0;
947 /* If the total CDEBUG() size is larger than a page, it
948 * will print a warning to the console, avoid this by
949 * printing just the last part of the symlink. */
950 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
951 print_limit < rc ? "..." : "", print_limit,
952 (char *)ma->ma_lmm + rc - print_limit, rc);
957 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
958 repbody->max_cookiesize = 0;
959 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
960 repbody->valid |= OBD_MD_FLMODEASIZE;
961 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
962 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
963 repbody->max_cookiesize);
966 if (exp_connect_rmtclient(info->mti_exp) &&
967 reqbody->valid & OBD_MD_FLRMTPERM) {
968 void *buf = req_capsule_server_get(pill, &RMF_ACL);
970 /* mdt_getattr_lock only */
971 rc = mdt_pack_remote_perm(info, o, buf);
973 repbody->valid &= ~OBD_MD_FLRMTPERM;
974 repbody->aclsize = 0;
977 repbody->valid |= OBD_MD_FLRMTPERM;
978 repbody->aclsize = sizeof(struct mdt_remote_perm);
981 #ifdef CONFIG_FS_POSIX_ACL
982 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
983 (reqbody->valid & OBD_MD_FLACL)) {
984 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
985 buffer->lb_len = req_capsule_get_size(pill,
986 &RMF_ACL, RCL_SERVER);
987 if (buffer->lb_len > 0) {
988 rc = mo_xattr_get(env, next, buffer,
989 XATTR_NAME_ACL_ACCESS);
991 if (rc == -ENODATA) {
992 repbody->aclsize = 0;
993 repbody->valid |= OBD_MD_FLACL;
995 } else if (rc == -EOPNOTSUPP) {
998 CERROR("%s: unable to read "DFID
1000 mdt_obd_name(info->mti_mdt),
1001 PFID(mdt_object_fid(o)), rc);
1004 repbody->aclsize = rc;
1005 repbody->valid |= OBD_MD_FLACL;
1012 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
1013 info->mti_mdt->mdt_lut.lut_mds_capa &&
1014 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1015 struct lustre_capa *capa;
1017 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1019 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1020 rc = mo_capa_get(env, next, capa, 0);
1023 repbody->valid |= OBD_MD_FLMDSCAPA;
1028 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1033 static int mdt_renew_capa(struct mdt_thread_info *info)
1035 struct mdt_object *obj = info->mti_object;
1036 struct mdt_body *body;
1037 struct lustre_capa *capa, *c;
1041 /* if object doesn't exist, or server has disabled capability,
1042 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1045 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1046 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1049 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1050 LASSERT(body != NULL);
1052 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1055 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1059 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1061 body->valid |= OBD_MD_FLOSSCAPA;
1065 static int mdt_getattr(struct tgt_session_info *tsi)
1067 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1068 struct mdt_object *obj = info->mti_object;
1069 struct req_capsule *pill = info->mti_pill;
1070 struct mdt_body *reqbody;
1071 struct mdt_body *repbody;
1075 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1078 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1079 rc = req_capsule_server_pack(pill);
1081 RETURN(err_serious(rc));
1082 rc = mdt_renew_capa(info);
1083 GOTO(out_shrink, rc);
1086 LASSERT(obj != NULL);
1087 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1089 /* Unlike intent case where we need to pre-fill out buffers early on
1090 * in intent policy for ldlm reasons, here we can have a much better
1091 * guess at EA size by just reading it from disk.
1092 * Exceptions are readdir and (missing) directory striping */
1094 if (reqbody->valid & OBD_MD_LINKNAME) {
1095 /* No easy way to know how long is the symlink, but it cannot
1096 * be more than PATH_MAX, so we allocate +1 */
1099 /* A special case for fs ROOT: getattr there might fetch
1100 * default EA for entire fs, not just for this dir!
1102 } else if (lu_fid_eq(mdt_object_fid(obj),
1103 &info->mti_mdt->mdt_md_root_fid) &&
1104 (reqbody->valid & OBD_MD_FLDIREA) &&
1105 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1107 /* Should the default strping be bigger, mdt_fix_reply
1108 * will reallocate */
1109 rc = DEF_REP_MD_SIZE;
1111 /* Read the actual EA size from disk */
1112 rc = mdt_attr_get_eabuf_size(info, obj);
1116 GOTO(out_shrink, rc);
1118 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1120 rc = req_capsule_server_pack(pill);
1121 if (unlikely(rc != 0))
1122 GOTO(out, rc = err_serious(rc));
1124 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1125 LASSERT(repbody != NULL);
1126 repbody->eadatasize = 0;
1127 repbody->aclsize = 0;
1129 if (reqbody->valid & OBD_MD_FLRMTPERM)
1130 rc = mdt_init_ucred(info, reqbody);
1132 rc = mdt_check_ucred(info);
1134 GOTO(out_shrink, rc);
1136 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1139 * Don't check capability at all, because rename might getattr for
1140 * remote obj, and at that time no capability is available.
1142 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1143 rc = mdt_getattr_internal(info, obj, 0);
1144 if (reqbody->valid & OBD_MD_FLRMTPERM)
1145 mdt_exit_ucred(info);
1148 mdt_client_compatibility(info);
1149 rc2 = mdt_fix_reply(info);
1153 mdt_thread_info_fini(info);
1157 static int mdt_is_subdir(struct tgt_session_info *tsi)
1159 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1160 struct mdt_object *o = info->mti_object;
1161 struct req_capsule *pill = info->mti_pill;
1162 const struct mdt_body *body = info->mti_body;
1163 struct mdt_body *repbody;
1169 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1172 * We save last checked parent fid to @repbody->fid1 for remote
1175 LASSERT(fid_is_sane(&body->fid2));
1176 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1177 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1178 &body->fid2, &repbody->fid1);
1179 if (rc == 0 || rc == -EREMOTE)
1180 repbody->valid |= OBD_MD_FLID;
1182 mdt_thread_info_fini(info);
1186 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1188 struct mdt_thread_info *info;
1189 struct ptlrpc_request *req = tgt_ses_req(tsi);
1190 struct obd_export *exp = req->rq_export;
1191 struct mdt_object *o1, *o2, *o;
1192 struct mdt_lock_handle *lh1, *lh2;
1193 struct mdc_swap_layouts *msl;
1197 /* client does not support layout lock, so layout swaping
1199 * FIXME: there is a problem for old clients which don't support
1200 * layout lock yet. If those clients have already opened the file
1201 * they won't be notified at all so that old layout may still be
1202 * used to do IO. This can be fixed after file release is landed by
1203 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1204 if (!exp_connect_layout(exp))
1205 RETURN(-EOPNOTSUPP);
1207 info = tsi2mdt_info(tsi);
1209 if (info->mti_dlm_req != NULL)
1210 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1212 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1213 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1214 req_capsule_client_get(info->mti_pill,
1217 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1218 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1219 req_capsule_client_get(info->mti_pill,
1222 o1 = info->mti_object;
1223 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1224 &info->mti_body->fid2);
1226 GOTO(out, rc = PTR_ERR(o));
1228 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1229 GOTO(put, rc = -ENOENT);
1231 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1232 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1238 /* permission check. Make sure the calling process having permission
1239 * to write both files. */
1240 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1245 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1250 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1252 GOTO(put, rc = -EPROTO);
1254 lh1 = &info->mti_lh[MDT_LH_NEW];
1255 mdt_lock_reg_init(lh1, LCK_EX);
1256 lh2 = &info->mti_lh[MDT_LH_OLD];
1257 mdt_lock_reg_init(lh2, LCK_EX);
1259 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1260 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1264 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1265 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1269 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1270 mdt_object_child(o2), msl->msl_flags);
1273 mdt_object_unlock(info, o2, lh2, rc);
1275 mdt_object_unlock(info, o1, lh1, rc);
1277 mdt_object_put(info->mti_env, o);
1279 mdt_thread_info_fini(info);
1283 static int mdt_raw_lookup(struct mdt_thread_info *info,
1284 struct mdt_object *parent,
1285 const struct lu_name *lname,
1286 struct ldlm_reply *ldlm_rep)
1288 struct md_object *next = mdt_object_child(info->mti_object);
1289 const struct mdt_body *reqbody = info->mti_body;
1290 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1291 struct mdt_body *repbody;
1295 if (reqbody->valid != OBD_MD_FLID)
1298 LASSERT(!info->mti_cross_ref);
1300 /* Only got the fid of this obj by name */
1301 fid_zero(child_fid);
1302 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1305 /* XXX is raw_lookup possible as intent operation? */
1308 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1311 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1313 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1316 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1317 repbody->fid1 = *child_fid;
1318 repbody->valid = OBD_MD_FLID;
1324 * UPDATE lock should be taken against parent, and be release before exit;
1325 * child_bits lock should be taken against child, and be returned back:
1326 * (1)normal request should release the child lock;
1327 * (2)intent request will grant the lock to client.
1329 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1330 struct mdt_lock_handle *lhc,
1332 struct ldlm_reply *ldlm_rep)
1334 struct ptlrpc_request *req = mdt_info_req(info);
1335 struct mdt_body *reqbody = NULL;
1336 struct mdt_object *parent = info->mti_object;
1337 struct mdt_object *child;
1338 struct md_object *next = mdt_object_child(parent);
1339 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1340 struct lu_name *lname = NULL;
1341 struct mdt_lock_handle *lhp = NULL;
1342 struct ldlm_lock *lock;
1349 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1350 LASSERT(ergo(is_resent,
1351 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1353 LASSERT(parent != NULL);
1355 if (info->mti_cross_ref) {
1356 /* Only getattr on the child. Parent is on another node. */
1357 mdt_set_disposition(info, ldlm_rep,
1358 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1360 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1362 PFID(mdt_object_fid(child)), ldlm_rep);
1365 /* Do not take lock for resent case. */
1366 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1367 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1368 lhc->mlh_reg_lh.cookie);
1369 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1370 &lock->l_resource->lr_name));
1371 LDLM_LOCK_PUT(lock);
1373 mdt_lock_handle_init(lhc);
1374 mdt_lock_reg_init(lhc, LCK_PR);
1377 * Object's name is on another MDS, no lookup or layout
1378 * lock is needed here but update lock is.
1380 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1381 MDS_INODELOCK_LAYOUT);
1382 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1384 rc = mdt_object_lock(info, child, lhc, child_bits,
1390 /* Finally, we can get attr for child. */
1391 if (!mdt_object_exists(child)) {
1392 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1394 "remote object doesn't exist.\n");
1395 mdt_object_unlock(info, child, lhc, 1);
1399 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1400 rc = mdt_getattr_internal(info, child, 0);
1401 if (unlikely(rc != 0))
1402 mdt_object_unlock(info, child, lhc, 1);
1407 lname = &info->mti_name;
1408 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1410 if (lu_name_is_valid(lname)) {
1411 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1412 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1413 PNAME(lname), ldlm_rep);
1415 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1416 if (unlikely(reqbody == NULL))
1417 RETURN(err_serious(-EPROTO));
1419 *child_fid = reqbody->fid2;
1421 if (unlikely(!fid_is_sane(child_fid)))
1422 RETURN(err_serious(-EINVAL));
1424 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1426 PFID(mdt_object_fid(parent)),
1427 PFID(&reqbody->fid2), ldlm_rep);
1430 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1432 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1433 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1435 "Parent doesn't exist!\n");
1439 if (mdt_object_remote(parent)) {
1440 CERROR("%s: parent "DFID" is on remote target\n",
1441 mdt_obd_name(info->mti_mdt),
1442 PFID(mdt_object_fid(parent)));
1446 if (lu_name_is_valid(lname)) {
1447 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1454 /* step 1: lock parent only if parent is a directory */
1455 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1456 lhp = &info->mti_lh[MDT_LH_PARENT];
1457 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1458 rc = mdt_object_lock(info, parent, lhp,
1459 MDS_INODELOCK_UPDATE,
1461 if (unlikely(rc != 0))
1465 /* step 2: lookup child's fid by name */
1466 fid_zero(child_fid);
1467 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1470 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1473 GOTO(out_parent, rc);
1476 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1479 *step 3: find the child object by fid & lock it.
1480 * regardless if it is local or remote.
1482 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1483 * set parent dir fid the same as child fid in getattr by fid case
1484 * we should not lu_object_find() the object again, could lead
1485 * to hung if there is a concurrent unlink destroyed the object.
1487 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1488 mdt_object_get(info->mti_env, parent);
1491 child = mdt_object_find(info->mti_env, info->mti_mdt,
1495 if (unlikely(IS_ERR(child)))
1496 GOTO(out_parent, rc = PTR_ERR(child));
1498 /* Do not take lock for resent case. */
1499 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1500 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1501 lhc->mlh_reg_lh.cookie);
1503 if (!fid_res_name_eq(mdt_object_fid(child),
1504 &lock->l_resource->lr_name)) {
1505 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1506 &lock->l_resource->lr_name),
1507 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1508 PLDLMRES(lock->l_resource),
1509 PFID(mdt_object_fid(parent)));
1510 CWARN("Although resent, but still not get child lock"
1511 "parent:"DFID" child:"DFID"\n",
1512 PFID(mdt_object_fid(parent)),
1513 PFID(mdt_object_fid(child)));
1514 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1515 LDLM_LOCK_PUT(lock);
1518 LDLM_LOCK_PUT(lock);
1521 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1522 mdt_lock_handle_init(lhc);
1523 mdt_lock_reg_init(lhc, LCK_PR);
1526 if (!mdt_object_exists(child)) {
1527 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1529 "Object doesn't exist!\n");
1530 GOTO(out_child, rc = -ENOENT);
1533 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1534 mdt_object_exists(child) && !mdt_object_remote(child)) {
1535 struct md_attr *ma = &info->mti_attr;
1538 ma->ma_need = MA_INODE;
1539 rc = mdt_attr_get_complex(info, child, ma);
1540 if (unlikely(rc != 0))
1541 GOTO(out_child, rc);
1543 /* If the file has not been changed for some time, we
1544 * return not only a LOOKUP lock, but also an UPDATE
1545 * lock and this might save us RPC on later STAT. For
1546 * directories, it also let negative dentry cache start
1547 * working for this dir. */
1548 if (ma->ma_valid & MA_INODE &&
1549 ma->ma_attr.la_valid & LA_CTIME &&
1550 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1551 ma->ma_attr.la_ctime < cfs_time_current_sec())
1552 child_bits |= MDS_INODELOCK_UPDATE;
1555 /* layout lock must be granted in a best-effort way
1556 * for IT operations */
1557 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1558 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1559 exp_connect_layout(info->mti_exp) &&
1560 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1561 !mdt_object_remote(child) && ldlm_rep != NULL) {
1562 /* try to grant layout lock for regular file. */
1568 child_bits |= MDS_INODELOCK_LAYOUT;
1569 /* try layout lock, it may fail to be granted due to
1570 * contention at LOOKUP or UPDATE */
1571 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1573 child_bits &= ~MDS_INODELOCK_LAYOUT;
1574 LASSERT(child_bits != 0);
1575 rc = mdt_object_lock(info, child, lhc,
1576 child_bits, MDT_CROSS_LOCK);
1581 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1582 * client will enqueue the lock to the remote MDT */
1583 if (mdt_object_remote(child))
1584 child_bits &= ~MDS_INODELOCK_UPDATE;
1585 rc = mdt_object_lock(info, child, lhc, child_bits,
1588 if (unlikely(rc != 0))
1589 GOTO(out_child, rc);
1592 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1593 /* Get MA_SOM attributes if update lock is given. */
1595 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1596 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1599 /* finally, we can get attr for child. */
1600 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1601 rc = mdt_getattr_internal(info, child, ma_need);
1602 if (unlikely(rc != 0)) {
1603 mdt_object_unlock(info, child, lhc, 1);
1605 /* Debugging code. */
1606 LDLM_DEBUG(lock, "Returning lock to client");
1607 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1608 &lock->l_resource->lr_name),
1609 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1610 PLDLMRES(lock->l_resource),
1611 PFID(mdt_object_fid(child)));
1612 if (mdt_object_exists(child) && !mdt_object_remote(child))
1613 mdt_pack_size2body(info, child);
1616 LDLM_LOCK_PUT(lock);
1620 mdt_object_put(info->mti_env, child);
1623 mdt_object_unlock(info, parent, lhp, 1);
1627 /* normal handler: should release the child lock */
1628 static int mdt_getattr_name(struct tgt_session_info *tsi)
1630 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1631 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1632 struct mdt_body *reqbody;
1633 struct mdt_body *repbody;
1637 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1638 LASSERT(reqbody != NULL);
1639 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1640 LASSERT(repbody != NULL);
1642 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1643 repbody->eadatasize = 0;
1644 repbody->aclsize = 0;
1646 rc = mdt_init_ucred(info, reqbody);
1648 GOTO(out_shrink, rc);
1650 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1651 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1652 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1653 lhc->mlh_reg_lh.cookie = 0;
1655 mdt_exit_ucred(info);
1658 mdt_client_compatibility(info);
1659 rc2 = mdt_fix_reply(info);
1662 mdt_thread_info_fini(info);
1666 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1667 void *karg, void *uarg);
1669 static int mdt_set_info(struct tgt_session_info *tsi)
1671 struct ptlrpc_request *req = tgt_ses_req(tsi);
1674 int keylen, vallen, rc = 0;
1678 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1680 DEBUG_REQ(D_HA, req, "no set_info key");
1681 RETURN(err_serious(-EFAULT));
1684 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1687 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1689 DEBUG_REQ(D_HA, req, "no set_info val");
1690 RETURN(err_serious(-EFAULT));
1693 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1696 /* Swab any part of val you need to here */
1697 if (KEY_IS(KEY_READ_ONLY)) {
1698 spin_lock(&req->rq_export->exp_lock);
1700 *exp_connect_flags_ptr(req->rq_export) |=
1703 *exp_connect_flags_ptr(req->rq_export) &=
1704 ~OBD_CONNECT_RDONLY;
1705 spin_unlock(&req->rq_export->exp_lock);
1706 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1707 struct changelog_setinfo *cs = val;
1709 if (vallen != sizeof(*cs)) {
1710 CERROR("%s: bad changelog_clear setinfo size %d\n",
1711 tgt_name(tsi->tsi_tgt), vallen);
1714 if (ptlrpc_req_need_swab(req)) {
1715 __swab64s(&cs->cs_recno);
1716 __swab32s(&cs->cs_id);
1719 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1727 static int mdt_readpage(struct tgt_session_info *tsi)
1729 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1730 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1731 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1732 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1733 struct mdt_body *repbody;
1739 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1740 RETURN(err_serious(-ENOMEM));
1742 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1743 if (repbody == NULL || reqbody == NULL)
1744 RETURN(err_serious(-EFAULT));
1747 * prepare @rdpg before calling lower layers and transfer itself. Here
1748 * reqbody->size contains offset of where to start to read and
1749 * reqbody->nlink contains number bytes to read.
1751 rdpg->rp_hash = reqbody->size;
1752 if (rdpg->rp_hash != reqbody->size) {
1753 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1754 rdpg->rp_hash, reqbody->size);
1758 rdpg->rp_attrs = reqbody->mode;
1759 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1760 rdpg->rp_attrs |= LUDA_64BITHASH;
1761 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1762 exp_max_brw_size(tsi->tsi_exp));
1763 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1765 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1766 if (rdpg->rp_pages == NULL)
1769 for (i = 0; i < rdpg->rp_npages; ++i) {
1770 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1771 if (rdpg->rp_pages[i] == NULL)
1772 GOTO(free_rdpg, rc = -ENOMEM);
1775 /* call lower layers to fill allocated pages with directory data */
1776 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1778 GOTO(free_rdpg, rc);
1780 /* send pages to client */
1781 rc = tgt_sendpage(tsi, rdpg, rc);
1786 for (i = 0; i < rdpg->rp_npages; i++)
1787 if (rdpg->rp_pages[i] != NULL)
1788 __free_page(rdpg->rp_pages[i]);
1789 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1791 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1797 static int mdt_reint_internal(struct mdt_thread_info *info,
1798 struct mdt_lock_handle *lhc,
1801 struct req_capsule *pill = info->mti_pill;
1802 struct mdt_body *repbody;
1807 rc = mdt_reint_unpack(info, op);
1809 CERROR("Can't unpack reint, rc %d\n", rc);
1810 RETURN(err_serious(rc));
1813 /* for replay (no_create) lmm is not needed, client has it already */
1814 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1815 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1818 /* llog cookies are always 0, the field is kept for compatibility */
1819 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1820 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1822 rc = req_capsule_server_pack(pill);
1824 CERROR("Can't pack response, rc %d\n", rc);
1825 RETURN(err_serious(rc));
1828 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1829 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1831 repbody->eadatasize = 0;
1832 repbody->aclsize = 0;
1835 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1837 /* for replay no cookkie / lmm need, because client have this already */
1838 if (info->mti_spec.no_create)
1839 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1840 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1842 rc = mdt_init_ucred_reint(info);
1844 GOTO(out_shrink, rc);
1846 rc = mdt_fix_attr_ucred(info, op);
1848 GOTO(out_ucred, rc = err_serious(rc));
1850 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1851 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1852 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1853 GOTO(out_ucred, rc);
1855 rc = mdt_reint_rec(info, lhc);
1858 mdt_exit_ucred(info);
1860 mdt_client_compatibility(info);
1861 rc2 = mdt_fix_reply(info);
1867 static long mdt_reint_opcode(struct ptlrpc_request *req,
1868 const struct req_format **fmt)
1870 struct mdt_device *mdt;
1871 struct mdt_rec_reint *rec;
1874 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1876 opc = rec->rr_opcode;
1877 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1878 if (opc < REINT_MAX && fmt[opc] != NULL)
1879 req_capsule_extend(&req->rq_pill, fmt[opc]);
1881 mdt = mdt_exp2dev(req->rq_export);
1882 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1883 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1884 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1885 opc = err_serious(-EFAULT);
1888 opc = err_serious(-EFAULT);
1893 static int mdt_reint(struct tgt_session_info *tsi)
1897 static const struct req_format *reint_fmts[REINT_MAX] = {
1898 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1899 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1900 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1901 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1902 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1903 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1904 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1905 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1906 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1911 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1913 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1915 * No lock possible here from client to pass it to reint code
1918 rc = mdt_reint_internal(info, NULL, opc);
1919 mdt_thread_info_fini(info);
1924 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1928 /* this should sync the whole device */
1929 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1931 struct dt_device *dt = mdt->mdt_bottom;
1935 rc = dt->dd_ops->dt_sync(env, dt);
1939 /* this should sync this object */
1940 static int mdt_object_sync(struct mdt_thread_info *info)
1942 struct md_object *next;
1946 if (!mdt_object_exists(info->mti_object)) {
1947 CWARN("Non existing object "DFID"!\n",
1948 PFID(mdt_object_fid(info->mti_object)));
1951 next = mdt_object_child(info->mti_object);
1952 rc = mo_object_sync(info->mti_env, next);
1957 static int mdt_sync(struct tgt_session_info *tsi)
1959 struct ptlrpc_request *req = tgt_ses_req(tsi);
1960 struct req_capsule *pill = tsi->tsi_pill;
1961 struct mdt_body *body;
1966 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1967 RETURN(err_serious(-ENOMEM));
1969 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1970 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1972 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1974 /* sync an object */
1975 rc = mdt_object_sync(info);
1977 const struct lu_fid *fid;
1978 struct lu_attr *la = &info->mti_attr.ma_attr;
1980 info->mti_attr.ma_need = MA_INODE;
1981 info->mti_attr.ma_valid = 0;
1982 rc = mdt_attr_get_complex(info, info->mti_object,
1985 body = req_capsule_server_get(pill,
1987 fid = mdt_object_fid(info->mti_object);
1988 mdt_pack_attr2body(info, body, la, fid);
1991 mdt_thread_info_fini(info);
1994 mdt_counter_incr(req, LPROC_MDT_SYNC);
2000 * Handle quota control requests to consult current usage/limit, but also
2001 * to configure quota enforcement
2003 static int mdt_quotactl(struct tgt_session_info *tsi)
2005 struct obd_export *exp = tsi->tsi_exp;
2006 struct req_capsule *pill = tsi->tsi_pill;
2007 struct obd_quotactl *oqctl, *repoqc;
2009 struct mdt_device *mdt = mdt_exp2dev(exp);
2010 struct lu_device *qmt = mdt->mdt_qmt_dev;
2013 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2015 RETURN(err_serious(-EPROTO));
2017 rc = req_capsule_server_pack(pill);
2019 RETURN(err_serious(rc));
2021 switch (oqctl->qc_cmd) {
2023 case LUSTRE_Q_INVALIDATE:
2024 case LUSTRE_Q_FINVALIDATE:
2028 /* deprecated, not used any more */
2029 RETURN(-EOPNOTSUPP);
2030 /* master quotactl */
2036 RETURN(-EOPNOTSUPP);
2037 /* slave quotactl */
2042 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2046 /* map uid/gid for remote client */
2048 if (exp_connect_rmtclient(exp)) {
2049 struct lustre_idmap_table *idmap;
2051 idmap = exp->exp_mdt_data.med_idmap;
2053 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2054 oqctl->qc_cmd != Q_GETINFO))
2057 if (oqctl->qc_type == USRQUOTA)
2058 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2060 else if (oqctl->qc_type == GRPQUOTA)
2061 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2066 if (id == CFS_IDMAP_NOTFOUND) {
2067 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2072 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2074 RETURN(err_serious(-EFAULT));
2076 if (oqctl->qc_id != id)
2077 swap(oqctl->qc_id, id);
2079 switch (oqctl->qc_cmd) {
2085 /* forward quotactl request to QMT */
2086 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2091 /* slave quotactl */
2092 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2097 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2101 if (oqctl->qc_id != id)
2102 swap(oqctl->qc_id, id);
2108 /** clone llog ctxt from child (mdd)
2109 * This allows remote llog (replicator) access.
2110 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2111 * context was originally set up, or we can handle them directly.
2112 * I choose the latter, but that means I need any llog
2113 * contexts set up by child to be accessable by the mdt. So we clone the
2114 * context into our context list here.
2116 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2119 struct md_device *next = mdt->mdt_child;
2120 struct llog_ctxt *ctxt;
2123 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2126 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2127 if (rc || ctxt == NULL) {
2131 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2133 CERROR("Can't set mdt ctxt %d\n", rc);
2138 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2139 struct mdt_device *mdt, int idx)
2141 struct llog_ctxt *ctxt;
2143 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2146 /* Put once for the get we just did, and once for the clone */
2147 llog_ctxt_put(ctxt);
2148 llog_ctxt_put(ctxt);
2153 * sec context handlers
2155 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2159 rc = mdt_handle_idmap(tsi);
2161 struct ptlrpc_request *req = tgt_ses_req(tsi);
2164 opc = lustre_msg_get_opc(req->rq_reqmsg);
2165 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2166 sptlrpc_svc_ctx_invalidate(req);
2169 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2175 * quota request handlers
2177 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2179 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2180 struct lu_device *qmt = mdt->mdt_qmt_dev;
2185 RETURN(err_serious(-EOPNOTSUPP));
2187 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2191 struct mdt_object *mdt_object_new(const struct lu_env *env,
2192 struct mdt_device *d,
2193 const struct lu_fid *f)
2195 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2196 struct lu_object *o;
2197 struct mdt_object *m;
2200 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2201 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2202 if (unlikely(IS_ERR(o)))
2203 m = (struct mdt_object *)o;
2209 struct mdt_object *mdt_object_find(const struct lu_env *env,
2210 struct mdt_device *d,
2211 const struct lu_fid *f)
2213 struct lu_object *o;
2214 struct mdt_object *m;
2217 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2218 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2219 if (unlikely(IS_ERR(o)))
2220 m = (struct mdt_object *)o;
2228 * Asyncronous commit for mdt device.
2230 * Pass asynchonous commit call down the MDS stack.
2232 * \param env environment
2233 * \param mdt the mdt device
2235 static void mdt_device_commit_async(const struct lu_env *env,
2236 struct mdt_device *mdt)
2238 struct dt_device *dt = mdt->mdt_bottom;
2241 rc = dt->dd_ops->dt_commit_async(env, dt);
2242 if (unlikely(rc != 0))
2243 CWARN("async commit start failed with rc = %d", rc);
2247 * Mark the lock as "synchonous".
2249 * Mark the lock to deffer transaction commit to the unlock time.
2251 * \param lock the lock to mark as "synchonous"
2253 * \see mdt_is_lock_sync
2254 * \see mdt_save_lock
2256 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2258 lock->l_ast_data = (void*)1;
2262 * Check whehter the lock "synchonous" or not.
2264 * \param lock the lock to check
2265 * \retval 1 the lock is "synchonous"
2266 * \retval 0 the lock isn't "synchronous"
2268 * \see mdt_set_lock_sync
2269 * \see mdt_save_lock
2271 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2273 return lock->l_ast_data != NULL;
2277 * Blocking AST for mdt locks.
2279 * Starts transaction commit if in case of COS lock conflict or
2280 * deffers such a commit to the mdt_save_lock.
2282 * \param lock the lock which blocks a request or cancelling lock
2283 * \param desc unused
2284 * \param data unused
2285 * \param flag indicates whether this cancelling or blocking callback
2287 * \see ldlm_blocking_ast_nocheck
2289 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2290 void *data, int flag)
2292 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2293 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2297 if (flag == LDLM_CB_CANCELING)
2299 lock_res_and_lock(lock);
2300 if (lock->l_blocking_ast != mdt_blocking_ast) {
2301 unlock_res_and_lock(lock);
2304 if (mdt_cos_is_enabled(mdt) &&
2305 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2306 lock->l_blocking_lock != NULL &&
2307 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2308 mdt_set_lock_sync(lock);
2310 rc = ldlm_blocking_ast_nocheck(lock);
2312 /* There is no lock conflict if l_blocking_lock == NULL,
2313 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2314 * when the last reference to a local lock was released */
2315 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2318 rc = lu_env_init(&env, LCT_LOCAL);
2319 if (unlikely(rc != 0))
2320 CWARN("lu_env initialization failed with rc = %d,"
2321 "cannot start asynchronous commit\n", rc);
2323 mdt_device_commit_async(&env, mdt);
2329 /* Used for cross-MDT lock */
2330 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2331 void *data, int flag)
2333 struct lustre_handle lockh;
2337 case LDLM_CB_BLOCKING:
2338 ldlm_lock2handle(lock, &lockh);
2339 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2341 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2345 case LDLM_CB_CANCELING:
2346 LDLM_DEBUG(lock, "Revoke remote lock\n");
2354 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2355 struct mdt_object *o, const struct lu_fid *fid,
2356 struct lustre_handle *lh, ldlm_mode_t mode,
2359 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2360 ldlm_policy_data_t *policy = &mti->mti_policy;
2361 struct ldlm_res_id *res_id = &mti->mti_res_id;
2365 LASSERT(mdt_object_remote(o));
2367 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2369 fid_build_reg_res_name(fid, res_id);
2371 memset(einfo, 0, sizeof(*einfo));
2372 einfo->ei_type = LDLM_IBITS;
2373 einfo->ei_mode = mode;
2374 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2375 einfo->ei_cb_cp = ldlm_completion_ast;
2376 einfo->ei_enq_slave = 0;
2377 einfo->ei_res_id = res_id;
2379 memset(policy, 0, sizeof(*policy));
2380 policy->l_inodebits.bits = ibits;
2382 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2387 static int mdt_object_local_lock(struct mdt_thread_info *info,
2388 struct mdt_object *o,
2389 struct mdt_lock_handle *lh, __u64 ibits,
2390 bool nonblock, int locality)
2392 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2393 ldlm_policy_data_t *policy = &info->mti_policy;
2394 struct ldlm_res_id *res_id = &info->mti_res_id;
2399 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2400 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2401 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2402 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2404 /* Only enqueue LOOKUP lock for remote object */
2405 if (mdt_object_remote(o))
2406 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2408 if (lh->mlh_type == MDT_PDO_LOCK) {
2409 /* check for exists after object is locked */
2410 if (mdt_object_exists(o) == 0) {
2411 /* Non-existent object shouldn't have PDO lock */
2414 /* Non-dir object shouldn't have PDO lock */
2415 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2420 memset(policy, 0, sizeof(*policy));
2421 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2423 dlmflags = LDLM_FL_ATOMIC_CB;
2425 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2428 * Take PDO lock on whole directory and build correct @res_id for lock
2429 * on part of directory.
2431 if (lh->mlh_pdo_hash != 0) {
2432 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2433 mdt_lock_pdo_mode(info, o, lh);
2434 if (lh->mlh_pdo_mode != LCK_NL) {
2436 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2437 * is never going to be sent to client and we do not
2438 * want it slowed down due to possible cancels.
2440 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2441 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2442 policy, res_id, dlmflags,
2443 info->mti_exp == NULL ? NULL :
2444 &info->mti_exp->exp_handle.h_cookie);
2450 * Finish res_id initializing by name hash marking part of
2451 * directory which is taking modification.
2453 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2456 policy->l_inodebits.bits = ibits;
2459 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2460 * going to be sent to client. If it is - mdt_intent_policy() path will
2461 * fix it up and turn FL_LOCAL flag off.
2463 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2464 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2465 info->mti_exp == NULL ? NULL :
2466 &info->mti_exp->exp_handle.h_cookie);
2468 mdt_object_unlock(info, o, lh, 1);
2469 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2470 lh->mlh_pdo_hash != 0 &&
2471 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2472 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2479 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2480 struct mdt_lock_handle *lh, __u64 ibits,
2481 bool nonblock, int locality)
2486 if (!mdt_object_remote(o))
2487 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2490 if (locality == MDT_LOCAL_LOCK) {
2491 CERROR("%s: try to get local lock for remote object"
2492 DFID".\n", mdt_obd_name(info->mti_mdt),
2493 PFID(mdt_object_fid(o)));
2497 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2498 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2499 MDS_INODELOCK_XATTR);
2500 if (ibits & MDS_INODELOCK_UPDATE) {
2501 /* Sigh, PDO needs to enqueue 2 locks right now, but
2502 * enqueue RPC can only request 1 lock, to avoid extra
2503 * RPC, so it will instead enqueue EX lock for remote
2504 * object anyway XXX*/
2505 if (lh->mlh_type == MDT_PDO_LOCK &&
2506 lh->mlh_pdo_hash != 0) {
2507 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2508 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2509 PFID(mdt_object_fid(o)));
2510 lh->mlh_pdo_hash = 0;
2511 lh->mlh_rreg_mode = LCK_EX;
2512 lh->mlh_type = MDT_REG_LOCK;
2514 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2517 MDS_INODELOCK_UPDATE);
2522 /* Only enqueue LOOKUP lock for remote object */
2523 if (ibits & MDS_INODELOCK_LOOKUP) {
2524 rc = mdt_object_local_lock(info, o, lh,
2525 MDS_INODELOCK_LOOKUP,
2526 nonblock, locality);
2534 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2535 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2537 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2540 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2541 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2543 struct mdt_lock_handle tmp = *lh;
2546 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2554 * Save a lock within request object.
2556 * Keep the lock referenced until whether client ACK or transaction
2557 * commit happens or release the lock immediately depending on input
2558 * parameters. If COS is ON, a write lock is converted to COS lock
2561 * \param info thead info object
2562 * \param h lock handle
2563 * \param mode lock mode
2564 * \param decref force immediate lock releasing
2567 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2568 ldlm_mode_t mode, int decref)
2572 if (lustre_handle_is_used(h)) {
2573 if (decref || !info->mti_has_trans ||
2574 !(mode & (LCK_PW | LCK_EX))){
2575 mdt_fid_unlock(h, mode);
2577 struct mdt_device *mdt = info->mti_mdt;
2578 struct ldlm_lock *lock = ldlm_handle2lock(h);
2579 struct ptlrpc_request *req = mdt_info_req(info);
2582 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2584 /* there is no request if mdt_object_unlock() is called
2585 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2586 if (likely(req != NULL)) {
2587 CDEBUG(D_HA, "request = %p reply state = %p"
2588 " transno = "LPD64"\n", req,
2589 req->rq_reply_state, req->rq_transno);
2590 if (mdt_cos_is_enabled(mdt)) {
2592 ldlm_lock_downgrade(lock, LCK_COS);
2595 ptlrpc_save_lock(req, h, mode, no_ack);
2597 ldlm_lock_decref(h, mode);
2599 if (mdt_is_lock_sync(lock)) {
2600 CDEBUG(D_HA, "found sync-lock,"
2601 " async commit started\n");
2602 mdt_device_commit_async(info->mti_env,
2605 LDLM_LOCK_PUT(lock);
2614 * Unlock mdt object.
2616 * Immeditely release the regular lock and the PDO lock or save the
2617 * lock in reqeuest and keep them referenced until client ACK or
2618 * transaction commit.
2620 * \param info thread info object
2621 * \param o mdt object
2622 * \param lh mdt lock handle referencing regular and PDO locks
2623 * \param decref force immediate lock releasing
2625 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2626 struct mdt_lock_handle *lh, int decref)
2630 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2631 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2633 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2634 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2639 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2640 const struct lu_fid *f,
2641 struct mdt_lock_handle *lh,
2644 struct mdt_object *o;
2646 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2650 rc = mdt_object_lock(info, o, lh, ibits,
2653 mdt_object_put(info->mti_env, o);
2660 void mdt_object_unlock_put(struct mdt_thread_info * info,
2661 struct mdt_object * o,
2662 struct mdt_lock_handle *lh,
2665 mdt_object_unlock(info, o, lh, decref);
2666 mdt_object_put(info->mti_env, o);
2670 * Generic code handling requests that have struct mdt_body passed in:
2672 * - extract mdt_body from request and save it in @info, if present;
2674 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2677 * - if HABEO_CORPUS flag is set for this request type check whether object
2678 * actually exists on storage (lu_object_exists()).
2681 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2683 const struct mdt_body *body;
2684 struct mdt_object *obj;
2685 const struct lu_env *env;
2686 struct req_capsule *pill;
2690 env = info->mti_env;
2691 pill = info->mti_pill;
2693 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2697 if (!(body->valid & OBD_MD_FLID))
2700 if (!fid_is_sane(&body->fid1)) {
2701 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2706 * Do not get size or any capa fields before we check that request
2707 * contains capa actually. There are some requests which do not, for
2708 * instance MDS_IS_SUBDIR.
2710 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2711 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2712 mdt_set_capainfo(info, 0, &body->fid1,
2713 req_capsule_client_get(pill, &RMF_CAPA1));
2715 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2717 if ((flags & HABEO_CORPUS) &&
2718 !mdt_object_exists(obj)) {
2719 mdt_object_put(env, obj);
2720 /* for capability renew ENOENT will be handled in
2722 if (body->valid & OBD_MD_FLOSSCAPA)
2727 info->mti_object = obj;
2736 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2738 struct req_capsule *pill = info->mti_pill;
2742 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2743 rc = mdt_body_unpack(info, flags);
2747 if (rc == 0 && (flags & HABEO_REFERO)) {
2749 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2750 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2752 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2753 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2756 rc = req_capsule_server_pack(pill);
2761 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2763 struct md_device *next = m->mdt_child;
2765 return next->md_ops->mdo_init_capa_ctxt(env, next,
2766 m->mdt_lut.lut_mds_capa,
2767 m->mdt_capa_timeout,
2772 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2774 lh->mlh_type = MDT_NUL_LOCK;
2775 lh->mlh_reg_lh.cookie = 0ull;
2776 lh->mlh_reg_mode = LCK_MINMODE;
2777 lh->mlh_pdo_lh.cookie = 0ull;
2778 lh->mlh_pdo_mode = LCK_MINMODE;
2779 lh->mlh_rreg_lh.cookie = 0ull;
2780 lh->mlh_rreg_mode = LCK_MINMODE;
2783 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2785 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2786 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2790 * Initialize fields of struct mdt_thread_info. Other fields are left in
2791 * uninitialized state, because it's too expensive to zero out whole
2792 * mdt_thread_info (> 1K) on each request arrival.
2794 void mdt_thread_info_init(struct ptlrpc_request *req,
2795 struct mdt_thread_info *info)
2799 info->mti_pill = &req->rq_pill;
2802 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2803 mdt_lock_handle_init(&info->mti_lh[i]);
2805 /* mdt device: it can be NULL while CONNECT */
2806 if (req->rq_export) {
2807 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2808 info->mti_exp = req->rq_export;
2810 info->mti_mdt = NULL;
2811 info->mti_env = req->rq_svc_thread->t_env;
2812 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2814 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2815 info->mti_big_buf = LU_BUF_NULL;
2816 info->mti_body = NULL;
2817 info->mti_object = NULL;
2818 info->mti_dlm_req = NULL;
2819 info->mti_has_trans = 0;
2820 info->mti_cross_ref = 0;
2821 info->mti_opdata = 0;
2822 info->mti_big_lmm_used = 0;
2824 info->mti_spec.no_create = 0;
2825 info->mti_spec.sp_rm_entry = 0;
2827 info->mti_spec.u.sp_ea.eadata = NULL;
2828 info->mti_spec.u.sp_ea.eadatalen = 0;
2831 void mdt_thread_info_fini(struct mdt_thread_info *info)
2835 if (info->mti_object != NULL) {
2836 mdt_object_put(info->mti_env, info->mti_object);
2837 info->mti_object = NULL;
2840 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2841 mdt_lock_handle_fini(&info->mti_lh[i]);
2842 info->mti_env = NULL;
2843 info->mti_pill = NULL;
2844 info->mti_exp = NULL;
2846 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2847 lu_buf_free(&info->mti_big_buf);
2850 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2852 struct mdt_thread_info *mti;
2853 struct lustre_capa *lc;
2855 mti = mdt_th_info(tsi->tsi_env);
2856 LASSERT(mti != NULL);
2858 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2859 if (tsi->tsi_corpus != NULL) {
2860 struct req_capsule *pill = tsi->tsi_pill;
2862 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2863 lu_object_get(tsi->tsi_corpus);
2866 * XXX: must be part of tgt_mdt_body_unpack but moved here
2867 * due to mdt_set_capainfo().
2869 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2870 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2871 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2872 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2875 mti->mti_body = tsi->tsi_mdt_body;
2876 mti->mti_dlm_req = tsi->tsi_dlm_req;
2881 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2883 struct ptlrpc_request *req = tgt_ses_req(tsi);
2888 rc = tgt_connect(tsi);
2892 rc = mdt_init_idmap(tsi);
2897 obd_disconnect(class_export_get(req->rq_export));
2916 static int mdt_intent_getattr(enum mdt_it_code opcode,
2917 struct mdt_thread_info *info,
2918 struct ldlm_lock **,
2921 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2922 struct mdt_thread_info *info,
2923 struct ldlm_lock **lockp,
2926 static int mdt_intent_layout(enum mdt_it_code opcode,
2927 struct mdt_thread_info *info,
2928 struct ldlm_lock **,
2930 static int mdt_intent_reint(enum mdt_it_code opcode,
2931 struct mdt_thread_info *info,
2932 struct ldlm_lock **,
2935 static struct mdt_it_flavor {
2936 const struct req_format *it_fmt;
2938 int (*it_act)(enum mdt_it_code ,
2939 struct mdt_thread_info *,
2940 struct ldlm_lock **,
2943 } mdt_it_flavor[] = {
2945 .it_fmt = &RQF_LDLM_INTENT,
2946 /*.it_flags = HABEO_REFERO,*/
2948 .it_act = mdt_intent_reint,
2949 .it_reint = REINT_OPEN
2952 .it_fmt = &RQF_LDLM_INTENT,
2954 * OCREAT is not a MUTABOR request as if the file
2956 * We do the extra check of OBD_CONNECT_RDONLY in
2957 * mdt_reint_open() when we really need to create
2961 .it_act = mdt_intent_reint,
2962 .it_reint = REINT_OPEN
2965 .it_fmt = &RQF_LDLM_INTENT,
2966 .it_flags = MUTABOR,
2967 .it_act = mdt_intent_reint,
2968 .it_reint = REINT_CREATE
2970 [MDT_IT_GETATTR] = {
2971 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2972 .it_flags = HABEO_REFERO,
2973 .it_act = mdt_intent_getattr
2975 [MDT_IT_READDIR] = {
2981 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2982 .it_flags = HABEO_REFERO,
2983 .it_act = mdt_intent_getattr
2986 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2987 .it_flags = MUTABOR,
2989 .it_reint = REINT_UNLINK
2993 .it_flags = MUTABOR,
2996 [MDT_IT_GETXATTR] = {
2997 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2998 .it_flags = HABEO_CORPUS,
2999 .it_act = mdt_intent_getxattr
3002 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3004 .it_act = mdt_intent_layout
3009 mdt_intent_lock_replace(struct mdt_thread_info *info, struct ldlm_lock **lockp,
3010 struct ldlm_lock *new_lock, struct mdt_lock_handle *lh,
3013 struct ptlrpc_request *req = mdt_info_req(info);
3014 struct ldlm_lock *lock = *lockp;
3017 * Get new lock only for cases when possible resent did not find any
3020 if (new_lock == NULL)
3021 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3023 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3024 lh->mlh_reg_lh.cookie = 0;
3028 LASSERTF(new_lock != NULL,
3029 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3032 * If we've already given this lock to a client once, then we should
3033 * have no readers or writers. Otherwise, we should have one reader
3034 * _or_ writer ref (which will be zeroed below) before returning the
3037 if (new_lock->l_export == req->rq_export) {
3038 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3040 LASSERT(new_lock->l_export == NULL);
3041 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3046 if (new_lock->l_export == req->rq_export) {
3048 * Already gave this to the client, which means that we
3049 * reconstructed a reply.
3051 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3053 lh->mlh_reg_lh.cookie = 0;
3054 RETURN(ELDLM_LOCK_REPLACED);
3058 * Fixup the lock to be given to the client.
3060 lock_res_and_lock(new_lock);
3061 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3062 * possible blocking AST. */
3063 while (new_lock->l_readers > 0) {
3064 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3065 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3066 new_lock->l_readers--;
3068 while (new_lock->l_writers > 0) {
3069 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3070 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3071 new_lock->l_writers--;
3074 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3075 new_lock->l_blocking_ast = lock->l_blocking_ast;
3076 new_lock->l_completion_ast = lock->l_completion_ast;
3077 new_lock->l_remote_handle = lock->l_remote_handle;
3078 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3080 unlock_res_and_lock(new_lock);
3082 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3083 &new_lock->l_remote_handle,
3084 &new_lock->l_exp_hash);
3086 LDLM_LOCK_RELEASE(new_lock);
3087 lh->mlh_reg_lh.cookie = 0;
3089 RETURN(ELDLM_LOCK_REPLACED);
3092 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3093 struct ldlm_lock *new_lock,
3094 struct ldlm_lock **old_lock,
3095 struct mdt_lock_handle *lh,
3096 enum mdt_it_code opcode)
3098 struct ptlrpc_request *req = mdt_info_req(info);
3099 struct obd_export *exp = req->rq_export;
3100 struct lustre_handle remote_hdl;
3101 struct ldlm_request *dlmreq;
3102 struct ldlm_lock *lock;
3104 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3107 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3108 remote_hdl = dlmreq->lock_handle[0];
3109 /* If the client does not require open lock, it does not need to
3110 * search lock in exp_lock_hash, since the server thread will
3111 * make sure the lock will be released, and the resend request
3112 * can always re-enqueue the lock */
3113 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3114 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3115 /* In the function below, .hs_keycmp resolves to
3116 * ldlm_export_lock_keycmp() */
3117 /* coverity[overrun-buffer-val] */
3118 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3120 lock_res_and_lock(lock);
3121 if (lock != new_lock) {
3122 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3123 lh->mlh_reg_mode = lock->l_granted_mode;
3125 LDLM_DEBUG(lock, "Restoring lock cookie");
3126 DEBUG_REQ(D_DLMTRACE, req,
3127 "restoring lock cookie "LPX64,
3128 lh->mlh_reg_lh.cookie);
3130 *old_lock = LDLM_LOCK_GET(lock);
3131 cfs_hash_put(exp->exp_lock_hash,
3133 unlock_res_and_lock(lock);
3136 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3137 unlock_res_and_lock(lock);
3141 * If the xid matches, then we know this is a resent request, and allow
3142 * it. (It's probably an OPEN, for which we don't send a lock.
3144 if (req_xid_is_last(req))
3148 * This remote handle isn't enqueued, so we never received or processed
3149 * this request. Clear MSG_RESENT, because it can be handled like any
3150 * normal request now.
3152 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3154 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3158 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3159 struct mdt_thread_info *info,
3160 struct ldlm_lock **lockp,
3163 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3164 struct ldlm_reply *ldlm_rep = NULL;
3168 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3169 * (for the resend case) or a new lock. Below we will use it to
3170 * replace the original lock.
3172 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);