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->mbo_fid1 = mdt->mdt_md_root_fid;
284 repbody->mbo_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->mbo_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->mbo_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->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
387 b->mbo_size = ma->ma_som->msd_size;
388 b->mbo_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->mbo_atime = attr->la_atime;
399 b->mbo_mtime = attr->la_mtime;
400 b->mbo_ctime = attr->la_ctime;
401 b->mbo_mode = attr->la_mode;
402 b->mbo_size = attr->la_size;
403 b->mbo_blocks = attr->la_blocks;
404 b->mbo_uid = attr->la_uid;
405 b->mbo_gid = attr->la_gid;
406 b->mbo_flags = attr->la_flags;
407 b->mbo_nlink = attr->la_nlink;
408 b->mbo_rdev = attr->la_rdev;
410 /* XXX should pack the reply body according to lu_valid */
411 b->mbo_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->mbo_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.
425 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
426 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
427 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
428 /* A released file stores its size on MDS. */
429 /* But return 1 block for released file, unless tools like tar
430 * will consider it fully sparse. (LU-3864)
432 if (unlikely(b->mbo_size == 0))
436 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
441 b->mbo_valid |= OBD_MD_FLID;
442 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
443 PFID(fid), b->mbo_nlink,
444 b->mbo_mode, b->mbo_size);
448 mdt_body_reverse_idmap(info, b);
450 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
451 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
452 PFID(fid), (unsigned long long)b->mbo_size);
455 static inline int mdt_body_has_lov(const struct lu_attr *la,
456 const struct mdt_body *body)
458 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
459 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
462 void mdt_client_compatibility(struct mdt_thread_info *info)
464 struct mdt_body *body;
465 struct ptlrpc_request *req = mdt_info_req(info);
466 struct obd_export *exp = req->rq_export;
467 struct md_attr *ma = &info->mti_attr;
468 struct lu_attr *la = &ma->ma_attr;
471 if (exp_connect_layout(exp))
472 /* the client can deal with 16-bit lmm_stripe_count */
475 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
477 if (!mdt_body_has_lov(la, body))
480 /* now we have a reply with a lov for a client not compatible with the
481 * layout lock so we have to clean the layout generation number */
482 if (S_ISREG(la->la_mode))
483 ma->ma_lmm->lmm_layout_gen = 0;
487 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
489 const struct lu_env *env = info->mti_env;
492 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
501 /* Is it a directory? Let's check for the LMV as well */
502 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
503 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
507 rc2 = mo_xattr_get(env, mdt_object_child(o),
509 XATTR_NAME_DEFAULT_LMV);
511 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
519 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
522 const struct lu_env *env = info->mti_env;
526 LASSERT(info->mti_big_lmm_used == 0);
527 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
531 /* big_lmm may need to be grown */
532 if (info->mti_big_lmmsize < rc) {
533 int size = size_roundup_power2(rc);
535 if (info->mti_big_lmmsize > 0) {
536 /* free old buffer */
537 LASSERT(info->mti_big_lmm);
538 OBD_FREE_LARGE(info->mti_big_lmm,
539 info->mti_big_lmmsize);
540 info->mti_big_lmm = NULL;
541 info->mti_big_lmmsize = 0;
544 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
545 if (info->mti_big_lmm == NULL)
547 info->mti_big_lmmsize = size;
549 LASSERT(info->mti_big_lmmsize >= rc);
551 info->mti_buf.lb_buf = info->mti_big_lmm;
552 info->mti_buf.lb_len = info->mti_big_lmmsize;
553 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
558 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
559 struct md_attr *ma, const char *name)
561 struct md_object *next = mdt_object_child(o);
562 struct lu_buf *buf = &info->mti_buf;
565 if (strcmp(name, XATTR_NAME_LOV) == 0) {
566 buf->lb_buf = ma->ma_lmm;
567 buf->lb_len = ma->ma_lmm_size;
568 LASSERT(!(ma->ma_valid & MA_LOV));
569 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
570 buf->lb_buf = ma->ma_lmv;
571 buf->lb_len = ma->ma_lmv_size;
572 LASSERT(!(ma->ma_valid & MA_LMV));
573 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
574 buf->lb_buf = ma->ma_lmv;
575 buf->lb_len = ma->ma_lmv_size;
576 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
581 rc = mo_xattr_get(info->mti_env, next, buf, name);
585 if (strcmp(name, XATTR_NAME_LOV) == 0) {
586 if (info->mti_big_lmm_used)
587 ma->ma_lmm = info->mti_big_lmm;
589 /* NOT return LOV EA with hole to old client. */
590 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
591 LOV_PATTERN_F_HOLE) &&
592 !(exp_connect_flags(info->mti_exp) &
593 OBD_CONNECT_LFSCK)) {
596 ma->ma_lmm_size = rc;
597 ma->ma_valid |= MA_LOV;
599 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
600 if (info->mti_big_lmm_used)
601 ma->ma_lmv = info->mti_big_lmm;
603 ma->ma_lmv_size = rc;
604 ma->ma_valid |= MA_LMV;
605 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
606 ma->ma_lmv_size = rc;
607 ma->ma_valid |= MA_LMV_DEF;
610 /* Update mdt_max_mdsize so all clients will be aware that */
611 if (info->mti_mdt->mdt_max_mdsize < rc)
612 info->mti_mdt->mdt_max_mdsize = rc;
615 } else if (rc == -ENODATA) {
618 } else if (rc == -ERANGE) {
619 /* Default LMV has fixed size, so it must be able to fit
620 * in the original buffer */
621 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
623 rc = mdt_big_xattr_get(info, o, name);
625 info->mti_big_lmm_used = 1;
633 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
634 struct mdt_object *o, struct lu_fid *pfid)
636 struct lu_buf *buf = &info->mti_buf;
637 struct link_ea_header *leh;
638 struct link_ea_entry *lee;
642 buf->lb_buf = info->mti_big_lmm;
643 buf->lb_len = info->mti_big_lmmsize;
644 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
645 buf, XATTR_NAME_LINK);
646 /* ignore errors, MA_PFID won't be set and it is
647 * up to the caller to treat this as an error */
648 if (rc == -ERANGE || buf->lb_len == 0) {
649 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
650 buf->lb_buf = info->mti_big_lmm;
651 buf->lb_len = info->mti_big_lmmsize;
656 if (rc < sizeof(*leh)) {
657 CERROR("short LinkEA on "DFID": rc = %d\n",
658 PFID(mdt_object_fid(o)), rc);
662 leh = (struct link_ea_header *) buf->lb_buf;
663 lee = (struct link_ea_entry *)(leh + 1);
664 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
665 leh->leh_magic = LINK_EA_MAGIC;
666 leh->leh_reccount = __swab32(leh->leh_reccount);
667 leh->leh_len = __swab64(leh->leh_len);
669 if (leh->leh_magic != LINK_EA_MAGIC)
671 if (leh->leh_reccount == 0)
674 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
675 fid_be_to_cpu(pfid, pfid);
680 int mdt_attr_get_complex(struct mdt_thread_info *info,
681 struct mdt_object *o, struct md_attr *ma)
683 const struct lu_env *env = info->mti_env;
684 struct md_object *next = mdt_object_child(o);
685 struct lu_buf *buf = &info->mti_buf;
686 int need = ma->ma_need;
693 if (mdt_object_exists(o) == 0)
694 GOTO(out, rc = -ENOENT);
695 mode = lu_object_attr(&next->mo_lu);
697 if (need & MA_INODE) {
698 ma->ma_need = MA_INODE;
699 rc = mo_attr_get(env, next, ma);
702 ma->ma_valid |= MA_INODE;
705 if (need & MA_PFID) {
706 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
708 ma->ma_valid |= MA_PFID;
709 /* ignore this error, parent fid is not mandatory */
713 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
714 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
719 if (need & MA_LMV && S_ISDIR(mode)) {
720 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
725 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
726 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
731 if (need & MA_SOM && S_ISREG(mode)) {
732 buf->lb_buf = info->mti_xattr_buf;
733 buf->lb_len = sizeof(info->mti_xattr_buf);
734 CLASSERT(sizeof(struct som_attrs) <=
735 sizeof(info->mti_xattr_buf));
736 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
737 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
739 ma->ma_valid |= MA_SOM;
740 else if (rc2 < 0 && rc2 != -ENODATA)
744 if (need & MA_HSM && S_ISREG(mode)) {
745 buf->lb_buf = info->mti_xattr_buf;
746 buf->lb_len = sizeof(info->mti_xattr_buf);
747 CLASSERT(sizeof(struct hsm_attrs) <=
748 sizeof(info->mti_xattr_buf));
749 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
750 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
752 ma->ma_valid |= MA_HSM;
753 else if (rc2 < 0 && rc2 != -ENODATA)
757 #ifdef CONFIG_FS_POSIX_ACL
758 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
759 buf->lb_buf = ma->ma_acl;
760 buf->lb_len = ma->ma_acl_size;
761 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
763 ma->ma_acl_size = rc2;
764 ma->ma_valid |= MA_ACL_DEF;
765 } else if (rc2 == -ENODATA) {
774 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
775 rc, ma->ma_valid, ma->ma_lmm);
779 static int mdt_getattr_internal(struct mdt_thread_info *info,
780 struct mdt_object *o, int ma_need)
782 struct md_object *next = mdt_object_child(o);
783 const struct mdt_body *reqbody = info->mti_body;
784 struct ptlrpc_request *req = mdt_info_req(info);
785 struct md_attr *ma = &info->mti_attr;
786 struct lu_attr *la = &ma->ma_attr;
787 struct req_capsule *pill = info->mti_pill;
788 const struct lu_env *env = info->mti_env;
789 struct mdt_body *repbody;
790 struct lu_buf *buffer = &info->mti_buf;
795 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
796 RETURN(err_serious(-ENOMEM));
798 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
802 if (mdt_object_remote(o)) {
803 /* This object is located on remote node.*/
804 /* Return -ENOTSUPP for old client */
805 if (!mdt_is_dne_client(req->rq_export))
806 GOTO(out, rc = -ENOTSUPP);
808 repbody->mbo_fid1 = *mdt_object_fid(o);
809 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
813 if (reqbody->mbo_eadatasize > 0) {
814 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
815 if (buffer->lb_buf == NULL)
816 GOTO(out, rc = -EPROTO);
817 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
820 buffer->lb_buf = NULL;
822 ma_need &= ~(MA_LOV | MA_LMV);
823 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
824 mdt_obd_name(info->mti_mdt),
825 req->rq_export->exp_client_uuid.uuid);
828 /* If it is dir object and client require MEA, then we got MEA */
829 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
830 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
831 /* Assumption: MDT_MD size is enough for lmv size. */
832 ma->ma_lmv = buffer->lb_buf;
833 ma->ma_lmv_size = buffer->lb_len;
834 ma->ma_need = MA_INODE;
835 if (ma->ma_lmv_size > 0) {
836 if (reqbody->mbo_valid & OBD_MD_MEA)
837 ma->ma_need |= MA_LMV;
838 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
839 ma->ma_need |= MA_LMV_DEF;
842 ma->ma_lmm = buffer->lb_buf;
843 ma->ma_lmm_size = buffer->lb_len;
844 ma->ma_need = MA_INODE | MA_HSM;
845 if (ma->ma_lmm_size > 0)
846 ma->ma_need |= MA_LOV;
849 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
850 reqbody->mbo_valid & OBD_MD_FLDIREA &&
851 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
852 /* get default stripe info for this dir. */
853 ma->ma_need |= MA_LOV_DEF;
855 ma->ma_need |= ma_need;
856 if (ma->ma_need & MA_SOM)
857 ma->ma_som = &info->mti_u.som.data;
859 rc = mdt_attr_get_complex(info, o, ma);
861 CERROR("%s: getattr error for "DFID": rc = %d\n",
862 mdt_obd_name(info->mti_mdt),
863 PFID(mdt_object_fid(o)), rc);
867 /* if file is released, check if a restore is running */
868 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
869 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
870 repbody->mbo_t_state = MS_RESTORE;
871 repbody->mbo_valid |= OBD_MD_TSTATE;
874 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
876 /* the Lustre protocol supposes to return default striping
877 * on the user-visible root if explicitly requested */
878 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
879 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
880 struct lu_fid rootfid;
881 struct mdt_object *root;
882 struct mdt_device *mdt = info->mti_mdt;
884 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
887 root = mdt_object_find(env, mdt, &rootfid);
889 RETURN(PTR_ERR(root));
890 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
891 mdt_object_put(info->mti_env, root);
893 CERROR("%s: getattr error for "DFID": rc = %d\n",
894 mdt_obd_name(info->mti_mdt),
895 PFID(mdt_object_fid(o)), rc);
900 if (likely(ma->ma_valid & MA_INODE))
901 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
905 if (mdt_body_has_lov(la, reqbody)) {
906 if (ma->ma_valid & MA_LOV) {
907 LASSERT(ma->ma_lmm_size);
908 repbody->mbo_eadatasize = ma->ma_lmm_size;
909 if (S_ISDIR(la->la_mode))
910 repbody->mbo_valid |= OBD_MD_FLDIREA;
912 repbody->mbo_valid |= OBD_MD_FLEASIZE;
913 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
915 if (ma->ma_valid & MA_LMV) {
916 /* Return -ENOTSUPP for old client */
917 if (!mdt_is_striped_client(req->rq_export))
920 LASSERT(S_ISDIR(la->la_mode));
921 mdt_dump_lmv(D_INFO, ma->ma_lmv);
922 repbody->mbo_eadatasize = ma->ma_lmv_size;
923 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
925 if (ma->ma_valid & MA_LMV_DEF) {
926 /* Return -ENOTSUPP for old client */
927 if (!mdt_is_striped_client(req->rq_export))
929 LASSERT(S_ISDIR(la->la_mode));
930 repbody->mbo_eadatasize = ma->ma_lmv_size;
931 repbody->mbo_valid |= (OBD_MD_FLDIREA |
934 } else if (S_ISLNK(la->la_mode) &&
935 reqbody->mbo_valid & OBD_MD_LINKNAME) {
936 buffer->lb_buf = ma->ma_lmm;
937 /* eadatasize from client includes NULL-terminator, so
938 * there is no need to read it */
939 buffer->lb_len = reqbody->mbo_eadatasize - 1;
940 rc = mo_readlink(env, next, buffer);
941 if (unlikely(rc <= 0)) {
942 CERROR("%s: readlink failed for "DFID": rc = %d\n",
943 mdt_obd_name(info->mti_mdt),
944 PFID(mdt_object_fid(o)), rc);
947 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
949 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
951 repbody->mbo_valid |= OBD_MD_LINKNAME;
952 /* we need to report back size with NULL-terminator
953 * because client expects that */
954 repbody->mbo_eadatasize = rc + 1;
955 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
956 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
957 "on "DFID ", expected %d\n",
958 mdt_obd_name(info->mti_mdt),
959 rc, PFID(mdt_object_fid(o)),
960 reqbody->mbo_eadatasize - 1);
962 ((char *)ma->ma_lmm)[rc] = 0;
964 /* If the total CDEBUG() size is larger than a page, it
965 * will print a warning to the console, avoid this by
966 * printing just the last part of the symlink. */
967 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
968 print_limit < rc ? "..." : "", print_limit,
969 (char *)ma->ma_lmm + rc - print_limit, rc);
974 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
975 repbody->mbo_max_cookiesize = 0;
976 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
977 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
978 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
979 "MAX_COOKIE to : %d:%d\n", repbody->mbo_max_mdsize,
980 repbody->mbo_max_cookiesize);
983 if (exp_connect_rmtclient(info->mti_exp) &&
984 reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
985 void *buf = req_capsule_server_get(pill, &RMF_ACL);
987 /* mdt_getattr_lock only */
988 rc = mdt_pack_remote_perm(info, o, buf);
990 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
991 repbody->mbo_aclsize = 0;
994 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
995 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
998 #ifdef CONFIG_FS_POSIX_ACL
999 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1000 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1001 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
1002 buffer->lb_len = req_capsule_get_size(pill,
1003 &RMF_ACL, RCL_SERVER);
1004 if (buffer->lb_len > 0) {
1005 rc = mo_xattr_get(env, next, buffer,
1006 XATTR_NAME_ACL_ACCESS);
1008 if (rc == -ENODATA) {
1009 repbody->mbo_aclsize = 0;
1010 repbody->mbo_valid |= OBD_MD_FLACL;
1012 } else if (rc == -EOPNOTSUPP) {
1015 CERROR("%s: unable to read "DFID
1017 mdt_obd_name(info->mti_mdt),
1018 PFID(mdt_object_fid(o)), rc);
1021 repbody->mbo_aclsize = rc;
1022 repbody->mbo_valid |= OBD_MD_FLACL;
1029 if (reqbody->mbo_valid & OBD_MD_FLMDSCAPA &&
1030 info->mti_mdt->mdt_lut.lut_mds_capa &&
1031 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1032 struct lustre_capa *capa;
1034 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1036 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1037 rc = mo_capa_get(env, next, capa, 0);
1040 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
1045 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1050 static int mdt_renew_capa(struct mdt_thread_info *info)
1052 struct mdt_object *obj = info->mti_object;
1053 struct mdt_body *body;
1054 struct lustre_capa *capa, *c;
1058 /* if object doesn't exist, or server has disabled capability,
1059 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1062 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1063 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1066 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1067 LASSERT(body != NULL);
1069 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1072 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1076 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1078 body->mbo_valid |= OBD_MD_FLOSSCAPA;
1082 static int mdt_getattr(struct tgt_session_info *tsi)
1084 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1085 struct mdt_object *obj = info->mti_object;
1086 struct req_capsule *pill = info->mti_pill;
1087 struct mdt_body *reqbody;
1088 struct mdt_body *repbody;
1092 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1095 if (reqbody->mbo_valid & OBD_MD_FLOSSCAPA) {
1096 rc = req_capsule_server_pack(pill);
1098 RETURN(err_serious(rc));
1099 rc = mdt_renew_capa(info);
1100 GOTO(out_shrink, rc);
1103 LASSERT(obj != NULL);
1104 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1106 /* Unlike intent case where we need to pre-fill out buffers early on
1107 * in intent policy for ldlm reasons, here we can have a much better
1108 * guess at EA size by just reading it from disk.
1109 * Exceptions are readdir and (missing) directory striping */
1111 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1112 /* No easy way to know how long is the symlink, but it cannot
1113 * be more than PATH_MAX, so we allocate +1 */
1116 /* A special case for fs ROOT: getattr there might fetch
1117 * default EA for entire fs, not just for this dir!
1119 } else if (lu_fid_eq(mdt_object_fid(obj),
1120 &info->mti_mdt->mdt_md_root_fid) &&
1121 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1122 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1124 /* Should the default strping be bigger, mdt_fix_reply
1125 * will reallocate */
1126 rc = DEF_REP_MD_SIZE;
1128 /* Read the actual EA size from disk */
1129 rc = mdt_attr_get_eabuf_size(info, obj);
1133 GOTO(out, rc = err_serious(rc));
1135 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1137 rc = req_capsule_server_pack(pill);
1138 if (unlikely(rc != 0))
1139 GOTO(out, rc = err_serious(rc));
1141 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1142 LASSERT(repbody != NULL);
1143 repbody->mbo_eadatasize = 0;
1144 repbody->mbo_aclsize = 0;
1146 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1147 rc = mdt_init_ucred(info, reqbody);
1149 rc = mdt_check_ucred(info);
1151 GOTO(out_shrink, rc);
1153 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1156 * Don't check capability at all, because rename might getattr for
1157 * remote obj, and at that time no capability is available.
1159 mdt_set_capainfo(info, 1, &reqbody->mbo_fid1, BYPASS_CAPA);
1160 rc = mdt_getattr_internal(info, obj, 0);
1161 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1162 mdt_exit_ucred(info);
1165 mdt_client_compatibility(info);
1166 rc2 = mdt_fix_reply(info);
1170 mdt_thread_info_fini(info);
1174 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1176 struct mdt_thread_info *info;
1177 struct ptlrpc_request *req = tgt_ses_req(tsi);
1178 struct obd_export *exp = req->rq_export;
1179 struct mdt_object *o1, *o2, *o;
1180 struct mdt_lock_handle *lh1, *lh2;
1181 struct mdc_swap_layouts *msl;
1185 /* client does not support layout lock, so layout swaping
1187 * FIXME: there is a problem for old clients which don't support
1188 * layout lock yet. If those clients have already opened the file
1189 * they won't be notified at all so that old layout may still be
1190 * used to do IO. This can be fixed after file release is landed by
1191 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1192 if (!exp_connect_layout(exp))
1193 RETURN(-EOPNOTSUPP);
1195 info = tsi2mdt_info(tsi);
1197 if (info->mti_dlm_req != NULL)
1198 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1200 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1201 mdt_set_capainfo(info, 0, &info->mti_body->mbo_fid1,
1202 req_capsule_client_get(info->mti_pill,
1205 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1206 mdt_set_capainfo(info, 1, &info->mti_body->mbo_fid2,
1207 req_capsule_client_get(info->mti_pill,
1210 o1 = info->mti_object;
1211 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1212 &info->mti_body->mbo_fid2);
1214 GOTO(out, rc = PTR_ERR(o));
1216 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1217 GOTO(put, rc = -ENOENT);
1219 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1220 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1226 /* permission check. Make sure the calling process having permission
1227 * to write both files. */
1228 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1233 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1238 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1240 GOTO(put, rc = -EPROTO);
1242 lh1 = &info->mti_lh[MDT_LH_NEW];
1243 mdt_lock_reg_init(lh1, LCK_EX);
1244 lh2 = &info->mti_lh[MDT_LH_OLD];
1245 mdt_lock_reg_init(lh2, LCK_EX);
1247 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1248 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1252 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1253 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1257 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1258 mdt_object_child(o2), msl->msl_flags);
1261 mdt_object_unlock(info, o2, lh2, rc);
1263 mdt_object_unlock(info, o1, lh1, rc);
1265 mdt_object_put(info->mti_env, o);
1267 mdt_thread_info_fini(info);
1271 static int mdt_raw_lookup(struct mdt_thread_info *info,
1272 struct mdt_object *parent,
1273 const struct lu_name *lname,
1274 struct ldlm_reply *ldlm_rep)
1276 struct md_object *next = mdt_object_child(info->mti_object);
1277 const struct mdt_body *reqbody = info->mti_body;
1278 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1279 struct mdt_body *repbody;
1283 if (reqbody->mbo_valid != OBD_MD_FLID)
1286 LASSERT(!info->mti_cross_ref);
1288 /* Only got the fid of this obj by name */
1289 fid_zero(child_fid);
1290 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1293 /* XXX is raw_lookup possible as intent operation? */
1296 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1299 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1301 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1304 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1305 repbody->mbo_fid1 = *child_fid;
1306 repbody->mbo_valid = OBD_MD_FLID;
1312 * UPDATE lock should be taken against parent, and be release before exit;
1313 * child_bits lock should be taken against child, and be returned back:
1314 * (1)normal request should release the child lock;
1315 * (2)intent request will grant the lock to client.
1317 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1318 struct mdt_lock_handle *lhc,
1320 struct ldlm_reply *ldlm_rep)
1322 struct ptlrpc_request *req = mdt_info_req(info);
1323 struct mdt_body *reqbody = NULL;
1324 struct mdt_object *parent = info->mti_object;
1325 struct mdt_object *child;
1326 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1327 struct lu_name *lname = NULL;
1328 struct mdt_lock_handle *lhp = NULL;
1329 struct ldlm_lock *lock;
1336 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1337 LASSERT(ergo(is_resent,
1338 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1343 if (info->mti_cross_ref) {
1344 /* Only getattr on the child. Parent is on another node. */
1345 mdt_set_disposition(info, ldlm_rep,
1346 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1348 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1350 PFID(mdt_object_fid(child)), ldlm_rep);
1352 rc = mdt_check_resent_lock(info, child, lhc);
1355 } else if (rc > 0) {
1356 mdt_lock_handle_init(lhc);
1357 mdt_lock_reg_init(lhc, LCK_PR);
1360 * Object's name is on another MDS, no lookup or layout
1361 * lock is needed here but update lock is.
1363 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1364 MDS_INODELOCK_LAYOUT);
1365 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1367 rc = mdt_object_lock(info, child, lhc, child_bits,
1373 /* Finally, we can get attr for child. */
1374 if (!mdt_object_exists(child)) {
1375 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1377 "remote object doesn't exist.\n");
1378 mdt_object_unlock(info, child, lhc, 1);
1382 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1383 rc = mdt_getattr_internal(info, child, 0);
1384 if (unlikely(rc != 0))
1385 mdt_object_unlock(info, child, lhc, 1);
1390 lname = &info->mti_name;
1391 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1393 if (lu_name_is_valid(lname)) {
1394 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1395 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1396 PNAME(lname), ldlm_rep);
1398 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1399 if (unlikely(reqbody == NULL))
1400 RETURN(err_serious(-EPROTO));
1402 *child_fid = reqbody->mbo_fid2;
1404 if (unlikely(!fid_is_sane(child_fid)))
1405 RETURN(err_serious(-EINVAL));
1407 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1409 PFID(mdt_object_fid(parent)),
1410 PFID(&reqbody->mbo_fid2), ldlm_rep);
1413 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1415 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1416 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1418 "Parent doesn't exist!\n");
1422 if (mdt_object_remote(parent)) {
1423 CERROR("%s: parent "DFID" is on remote target\n",
1424 mdt_obd_name(info->mti_mdt),
1425 PFID(mdt_object_fid(parent)));
1429 if (lu_name_is_valid(lname)) {
1430 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1437 /* step 1: lock parent only if parent is a directory */
1438 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1439 lhp = &info->mti_lh[MDT_LH_PARENT];
1440 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1441 rc = mdt_object_lock(info, parent, lhp,
1442 MDS_INODELOCK_UPDATE,
1444 if (unlikely(rc != 0))
1448 /* step 2: lookup child's fid by name */
1449 fid_zero(child_fid);
1450 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1451 child_fid, &info->mti_spec);
1453 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1456 GOTO(out_parent, rc);
1459 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1462 *step 3: find the child object by fid & lock it.
1463 * regardless if it is local or remote.
1465 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1466 * set parent dir fid the same as child fid in getattr by fid case
1467 * we should not lu_object_find() the object again, could lead
1468 * to hung if there is a concurrent unlink destroyed the object.
1470 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1471 mdt_object_get(info->mti_env, parent);
1474 child = mdt_object_find(info->mti_env, info->mti_mdt,
1478 if (unlikely(IS_ERR(child)))
1479 GOTO(out_parent, rc = PTR_ERR(child));
1481 rc = mdt_check_resent_lock(info, child, lhc);
1483 GOTO(out_child, rc);
1484 } else if (rc > 0) {
1485 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1486 mdt_lock_handle_init(lhc);
1487 mdt_lock_reg_init(lhc, LCK_PR);
1490 if (!mdt_object_exists(child)) {
1491 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1493 "Object doesn't exist!\n");
1494 GOTO(out_child, rc = -ENOENT);
1497 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1498 mdt_object_exists(child) && !mdt_object_remote(child)) {
1499 struct md_attr *ma = &info->mti_attr;
1502 ma->ma_need = MA_INODE;
1503 rc = mdt_attr_get_complex(info, child, ma);
1504 if (unlikely(rc != 0))
1505 GOTO(out_child, rc);
1507 /* If the file has not been changed for some time, we
1508 * return not only a LOOKUP lock, but also an UPDATE
1509 * lock and this might save us RPC on later STAT. For
1510 * directories, it also let negative dentry cache start
1511 * working for this dir. */
1512 if (ma->ma_valid & MA_INODE &&
1513 ma->ma_attr.la_valid & LA_CTIME &&
1514 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1515 ma->ma_attr.la_ctime < cfs_time_current_sec())
1516 child_bits |= MDS_INODELOCK_UPDATE;
1519 /* layout lock must be granted in a best-effort way
1520 * for IT operations */
1521 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1522 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1523 exp_connect_layout(info->mti_exp) &&
1524 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1525 !mdt_object_remote(child) && ldlm_rep != NULL) {
1526 /* try to grant layout lock for regular file. */
1532 child_bits |= MDS_INODELOCK_LAYOUT;
1533 /* try layout lock, it may fail to be granted due to
1534 * contention at LOOKUP or UPDATE */
1535 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1537 child_bits &= ~MDS_INODELOCK_LAYOUT;
1538 LASSERT(child_bits != 0);
1539 rc = mdt_object_lock(info, child, lhc,
1540 child_bits, MDT_CROSS_LOCK);
1545 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1546 * client will enqueue the lock to the remote MDT */
1547 if (mdt_object_remote(child))
1548 child_bits &= ~MDS_INODELOCK_UPDATE;
1549 rc = mdt_object_lock(info, child, lhc, child_bits,
1552 if (unlikely(rc != 0))
1553 GOTO(out_child, rc);
1556 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1557 /* Get MA_SOM attributes if update lock is given. */
1559 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1560 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1563 /* finally, we can get attr for child. */
1564 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1565 rc = mdt_getattr_internal(info, child, ma_need);
1566 if (unlikely(rc != 0)) {
1567 mdt_object_unlock(info, child, lhc, 1);
1569 /* Debugging code. */
1570 LDLM_DEBUG(lock, "Returning lock to client");
1571 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1572 &lock->l_resource->lr_name),
1573 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1574 PLDLMRES(lock->l_resource),
1575 PFID(mdt_object_fid(child)));
1576 if (mdt_object_exists(child) && !mdt_object_remote(child))
1577 mdt_pack_size2body(info, child);
1580 LDLM_LOCK_PUT(lock);
1584 mdt_object_put(info->mti_env, child);
1587 mdt_object_unlock(info, parent, lhp, 1);
1591 /* normal handler: should release the child lock */
1592 static int mdt_getattr_name(struct tgt_session_info *tsi)
1594 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1595 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1596 struct mdt_body *reqbody;
1597 struct mdt_body *repbody;
1601 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1602 LASSERT(reqbody != NULL);
1603 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1604 LASSERT(repbody != NULL);
1606 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1607 repbody->mbo_eadatasize = 0;
1608 repbody->mbo_aclsize = 0;
1610 rc = mdt_init_ucred(info, reqbody);
1612 GOTO(out_shrink, rc);
1614 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1615 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1616 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1617 lhc->mlh_reg_lh.cookie = 0;
1619 mdt_exit_ucred(info);
1622 mdt_client_compatibility(info);
1623 rc2 = mdt_fix_reply(info);
1626 mdt_thread_info_fini(info);
1630 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1631 void *karg, void *uarg);
1633 static int mdt_set_info(struct tgt_session_info *tsi)
1635 struct ptlrpc_request *req = tgt_ses_req(tsi);
1638 int keylen, vallen, rc = 0;
1642 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1644 DEBUG_REQ(D_HA, req, "no set_info key");
1645 RETURN(err_serious(-EFAULT));
1648 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1651 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1653 DEBUG_REQ(D_HA, req, "no set_info val");
1654 RETURN(err_serious(-EFAULT));
1657 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1660 /* Swab any part of val you need to here */
1661 if (KEY_IS(KEY_READ_ONLY)) {
1662 spin_lock(&req->rq_export->exp_lock);
1664 *exp_connect_flags_ptr(req->rq_export) |=
1667 *exp_connect_flags_ptr(req->rq_export) &=
1668 ~OBD_CONNECT_RDONLY;
1669 spin_unlock(&req->rq_export->exp_lock);
1670 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1671 struct changelog_setinfo *cs = val;
1673 if (vallen != sizeof(*cs)) {
1674 CERROR("%s: bad changelog_clear setinfo size %d\n",
1675 tgt_name(tsi->tsi_tgt), vallen);
1678 if (ptlrpc_req_need_swab(req)) {
1679 __swab64s(&cs->cs_recno);
1680 __swab32s(&cs->cs_id);
1683 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1691 static int mdt_readpage(struct tgt_session_info *tsi)
1693 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1694 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1695 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1696 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1697 struct mdt_body *repbody;
1703 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1704 RETURN(err_serious(-ENOMEM));
1706 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1707 if (repbody == NULL || reqbody == NULL)
1708 RETURN(err_serious(-EFAULT));
1711 * prepare @rdpg before calling lower layers and transfer itself. Here
1712 * reqbody->size contains offset of where to start to read and
1713 * reqbody->nlink contains number bytes to read.
1715 rdpg->rp_hash = reqbody->mbo_size;
1716 if (rdpg->rp_hash != reqbody->mbo_size) {
1717 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1718 rdpg->rp_hash, reqbody->mbo_size);
1722 rdpg->rp_attrs = reqbody->mbo_mode;
1723 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1724 rdpg->rp_attrs |= LUDA_64BITHASH;
1725 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1726 exp_max_brw_size(tsi->tsi_exp));
1727 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1729 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1730 if (rdpg->rp_pages == NULL)
1733 for (i = 0; i < rdpg->rp_npages; ++i) {
1734 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1735 if (rdpg->rp_pages[i] == NULL)
1736 GOTO(free_rdpg, rc = -ENOMEM);
1739 /* call lower layers to fill allocated pages with directory data */
1740 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1742 GOTO(free_rdpg, rc);
1744 /* send pages to client */
1745 rc = tgt_sendpage(tsi, rdpg, rc);
1750 for (i = 0; i < rdpg->rp_npages; i++)
1751 if (rdpg->rp_pages[i] != NULL)
1752 __free_page(rdpg->rp_pages[i]);
1753 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1755 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1761 static int mdt_reint_internal(struct mdt_thread_info *info,
1762 struct mdt_lock_handle *lhc,
1765 struct req_capsule *pill = info->mti_pill;
1766 struct mdt_body *repbody;
1771 rc = mdt_reint_unpack(info, op);
1773 CERROR("Can't unpack reint, rc %d\n", rc);
1774 RETURN(err_serious(rc));
1777 /* for replay (no_create) lmm is not needed, client has it already */
1778 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1779 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1782 /* llog cookies are always 0, the field is kept for compatibility */
1783 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1784 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1786 rc = req_capsule_server_pack(pill);
1788 CERROR("Can't pack response, rc %d\n", rc);
1789 RETURN(err_serious(rc));
1792 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1793 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1795 repbody->mbo_eadatasize = 0;
1796 repbody->mbo_aclsize = 0;
1799 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1801 /* for replay no cookkie / lmm need, because client have this already */
1802 if (info->mti_spec.no_create)
1803 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1804 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1806 rc = mdt_init_ucred_reint(info);
1808 GOTO(out_shrink, rc);
1810 rc = mdt_fix_attr_ucred(info, op);
1812 GOTO(out_ucred, rc = err_serious(rc));
1814 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1815 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1816 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1817 GOTO(out_ucred, rc);
1819 rc = mdt_reint_rec(info, lhc);
1822 mdt_exit_ucred(info);
1824 mdt_client_compatibility(info);
1825 rc2 = mdt_fix_reply(info);
1831 static long mdt_reint_opcode(struct ptlrpc_request *req,
1832 const struct req_format **fmt)
1834 struct mdt_device *mdt;
1835 struct mdt_rec_reint *rec;
1838 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1840 opc = rec->rr_opcode;
1841 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1842 if (opc < REINT_MAX && fmt[opc] != NULL)
1843 req_capsule_extend(&req->rq_pill, fmt[opc]);
1845 mdt = mdt_exp2dev(req->rq_export);
1846 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1847 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1848 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1849 opc = err_serious(-EFAULT);
1852 opc = err_serious(-EFAULT);
1857 static int mdt_reint(struct tgt_session_info *tsi)
1861 static const struct req_format *reint_fmts[REINT_MAX] = {
1862 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1863 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1864 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1865 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1866 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1867 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1868 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1869 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1870 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1875 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1877 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1879 * No lock possible here from client to pass it to reint code
1882 rc = mdt_reint_internal(info, NULL, opc);
1883 mdt_thread_info_fini(info);
1888 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1892 /* this should sync the whole device */
1893 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1895 struct dt_device *dt = mdt->mdt_bottom;
1899 rc = dt->dd_ops->dt_sync(env, dt);
1903 /* this should sync this object */
1904 static int mdt_object_sync(struct mdt_thread_info *info)
1906 struct md_object *next;
1910 if (!mdt_object_exists(info->mti_object)) {
1911 CWARN("%s: non existing object "DFID": rc = %d\n",
1912 mdt_obd_name(info->mti_mdt),
1913 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1916 next = mdt_object_child(info->mti_object);
1917 rc = mo_object_sync(info->mti_env, next);
1922 static int mdt_sync(struct tgt_session_info *tsi)
1924 struct ptlrpc_request *req = tgt_ses_req(tsi);
1925 struct req_capsule *pill = tsi->tsi_pill;
1926 struct mdt_body *body;
1931 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1932 RETURN(err_serious(-ENOMEM));
1934 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
1935 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1937 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1939 /* sync an object */
1940 rc = mdt_object_sync(info);
1942 const struct lu_fid *fid;
1943 struct lu_attr *la = &info->mti_attr.ma_attr;
1945 info->mti_attr.ma_need = MA_INODE;
1946 info->mti_attr.ma_valid = 0;
1947 rc = mdt_attr_get_complex(info, info->mti_object,
1950 body = req_capsule_server_get(pill,
1952 fid = mdt_object_fid(info->mti_object);
1953 mdt_pack_attr2body(info, body, la, fid);
1956 mdt_thread_info_fini(info);
1959 mdt_counter_incr(req, LPROC_MDT_SYNC);
1965 * Handle quota control requests to consult current usage/limit, but also
1966 * to configure quota enforcement
1968 static int mdt_quotactl(struct tgt_session_info *tsi)
1970 struct obd_export *exp = tsi->tsi_exp;
1971 struct req_capsule *pill = tsi->tsi_pill;
1972 struct obd_quotactl *oqctl, *repoqc;
1974 struct mdt_device *mdt = mdt_exp2dev(exp);
1975 struct lu_device *qmt = mdt->mdt_qmt_dev;
1978 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1980 RETURN(err_serious(-EPROTO));
1982 rc = req_capsule_server_pack(pill);
1984 RETURN(err_serious(rc));
1986 switch (oqctl->qc_cmd) {
1988 case LUSTRE_Q_INVALIDATE:
1989 case LUSTRE_Q_FINVALIDATE:
1993 /* deprecated, not used any more */
1994 RETURN(-EOPNOTSUPP);
1995 /* master quotactl */
2001 RETURN(-EOPNOTSUPP);
2002 /* slave quotactl */
2007 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2011 /* map uid/gid for remote client */
2013 if (exp_connect_rmtclient(exp)) {
2014 struct lustre_idmap_table *idmap;
2016 idmap = exp->exp_mdt_data.med_idmap;
2018 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2019 oqctl->qc_cmd != Q_GETINFO))
2022 if (oqctl->qc_type == USRQUOTA)
2023 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2025 else if (oqctl->qc_type == GRPQUOTA)
2026 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2031 if (id == CFS_IDMAP_NOTFOUND) {
2032 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2037 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2039 RETURN(err_serious(-EFAULT));
2041 if (oqctl->qc_id != id)
2042 swap(oqctl->qc_id, id);
2044 switch (oqctl->qc_cmd) {
2050 /* forward quotactl request to QMT */
2051 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2056 /* slave quotactl */
2057 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2062 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2066 if (oqctl->qc_id != id)
2067 swap(oqctl->qc_id, id);
2073 /** clone llog ctxt from child (mdd)
2074 * This allows remote llog (replicator) access.
2075 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2076 * context was originally set up, or we can handle them directly.
2077 * I choose the latter, but that means I need any llog
2078 * contexts set up by child to be accessable by the mdt. So we clone the
2079 * context into our context list here.
2081 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2084 struct md_device *next = mdt->mdt_child;
2085 struct llog_ctxt *ctxt;
2088 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2091 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2092 if (rc || ctxt == NULL) {
2096 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2098 CERROR("Can't set mdt ctxt %d\n", rc);
2103 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2104 struct mdt_device *mdt, int idx)
2106 struct llog_ctxt *ctxt;
2108 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2111 /* Put once for the get we just did, and once for the clone */
2112 llog_ctxt_put(ctxt);
2113 llog_ctxt_put(ctxt);
2118 * sec context handlers
2120 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2124 rc = mdt_handle_idmap(tsi);
2126 struct ptlrpc_request *req = tgt_ses_req(tsi);
2129 opc = lustre_msg_get_opc(req->rq_reqmsg);
2130 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2131 sptlrpc_svc_ctx_invalidate(req);
2134 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2140 * quota request handlers
2142 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2144 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2145 struct lu_device *qmt = mdt->mdt_qmt_dev;
2150 RETURN(err_serious(-EOPNOTSUPP));
2152 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2156 struct mdt_object *mdt_object_new(const struct lu_env *env,
2157 struct mdt_device *d,
2158 const struct lu_fid *f)
2160 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2161 struct lu_object *o;
2162 struct mdt_object *m;
2165 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2166 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2167 if (unlikely(IS_ERR(o)))
2168 m = (struct mdt_object *)o;
2174 struct mdt_object *mdt_object_find(const struct lu_env *env,
2175 struct mdt_device *d,
2176 const struct lu_fid *f)
2178 struct lu_object *o;
2179 struct mdt_object *m;
2182 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2183 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2184 if (unlikely(IS_ERR(o)))
2185 m = (struct mdt_object *)o;
2193 * Asyncronous commit for mdt device.
2195 * Pass asynchonous commit call down the MDS stack.
2197 * \param env environment
2198 * \param mdt the mdt device
2200 static void mdt_device_commit_async(const struct lu_env *env,
2201 struct mdt_device *mdt)
2203 struct dt_device *dt = mdt->mdt_bottom;
2206 rc = dt->dd_ops->dt_commit_async(env, dt);
2207 if (unlikely(rc != 0))
2208 CWARN("%s: async commit start failed: rc = %d\n",
2209 mdt_obd_name(mdt), rc);
2213 * Mark the lock as "synchonous".
2215 * Mark the lock to deffer transaction commit to the unlock time.
2217 * \param lock the lock to mark as "synchonous"
2219 * \see mdt_is_lock_sync
2220 * \see mdt_save_lock
2222 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2224 lock->l_ast_data = (void*)1;
2228 * Check whehter the lock "synchonous" or not.
2230 * \param lock the lock to check
2231 * \retval 1 the lock is "synchonous"
2232 * \retval 0 the lock isn't "synchronous"
2234 * \see mdt_set_lock_sync
2235 * \see mdt_save_lock
2237 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2239 return lock->l_ast_data != NULL;
2243 * Blocking AST for mdt locks.
2245 * Starts transaction commit if in case of COS lock conflict or
2246 * deffers such a commit to the mdt_save_lock.
2248 * \param lock the lock which blocks a request or cancelling lock
2249 * \param desc unused
2250 * \param data unused
2251 * \param flag indicates whether this cancelling or blocking callback
2253 * \see ldlm_blocking_ast_nocheck
2255 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2256 void *data, int flag)
2258 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2259 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2263 if (flag == LDLM_CB_CANCELING)
2265 lock_res_and_lock(lock);
2266 if (lock->l_blocking_ast != mdt_blocking_ast) {
2267 unlock_res_and_lock(lock);
2270 if (mdt_cos_is_enabled(mdt) &&
2271 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2272 lock->l_blocking_lock != NULL &&
2273 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2274 mdt_set_lock_sync(lock);
2276 rc = ldlm_blocking_ast_nocheck(lock);
2278 /* There is no lock conflict if l_blocking_lock == NULL,
2279 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2280 * when the last reference to a local lock was released */
2281 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2284 rc = lu_env_init(&env, LCT_LOCAL);
2285 if (unlikely(rc != 0))
2286 CWARN("%s: lu_env initialization failed, cannot "
2287 "start asynchronous commit: rc = %d\n",
2290 mdt_device_commit_async(&env, mdt);
2296 /* Used for cross-MDT lock */
2297 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2298 void *data, int flag)
2300 struct lustre_handle lockh;
2304 case LDLM_CB_BLOCKING:
2305 ldlm_lock2handle(lock, &lockh);
2306 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2308 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2312 case LDLM_CB_CANCELING:
2313 LDLM_DEBUG(lock, "Revoke remote lock\n");
2321 int mdt_check_resent_lock(struct mdt_thread_info *info,
2322 struct mdt_object *mo,
2323 struct mdt_lock_handle *lhc)
2325 /* the lock might already be gotten in ldlm_handle_enqueue() */
2326 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2327 struct ptlrpc_request *req = mdt_info_req(info);
2328 struct ldlm_lock *lock;
2330 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2331 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2333 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2334 * a resend case, however, it could be already destroyed
2335 * due to client eviction or a raced cancel RPC. */
2336 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
2337 lhc->mlh_reg_lh.cookie);
2341 if (!fid_res_name_eq(mdt_object_fid(mo),
2342 &lock->l_resource->lr_name)) {
2343 CWARN("%s: Although resent, but still not "
2344 "get child lock:"DFID"\n",
2345 info->mti_exp->exp_obd->obd_name,
2346 PFID(mdt_object_fid(mo)));
2347 LDLM_LOCK_PUT(lock);
2350 LDLM_LOCK_PUT(lock);
2356 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2357 struct mdt_object *o, const struct lu_fid *fid,
2358 struct lustre_handle *lh, ldlm_mode_t mode,
2361 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2362 ldlm_policy_data_t *policy = &mti->mti_policy;
2363 struct ldlm_res_id *res_id = &mti->mti_res_id;
2367 LASSERT(mdt_object_remote(o));
2369 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2371 fid_build_reg_res_name(fid, res_id);
2373 memset(einfo, 0, sizeof(*einfo));
2374 einfo->ei_type = LDLM_IBITS;
2375 einfo->ei_mode = mode;
2376 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2377 einfo->ei_cb_cp = ldlm_completion_ast;
2378 einfo->ei_enq_slave = 0;
2379 einfo->ei_res_id = res_id;
2381 memset(policy, 0, sizeof(*policy));
2382 policy->l_inodebits.bits = ibits;
2384 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2389 static int mdt_object_local_lock(struct mdt_thread_info *info,
2390 struct mdt_object *o,
2391 struct mdt_lock_handle *lh, __u64 ibits,
2392 bool nonblock, int locality)
2394 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2395 ldlm_policy_data_t *policy = &info->mti_policy;
2396 struct ldlm_res_id *res_id = &info->mti_res_id;
2401 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2402 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2403 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2404 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2406 /* Only enqueue LOOKUP lock for remote object */
2407 if (mdt_object_remote(o))
2408 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2410 if (lh->mlh_type == MDT_PDO_LOCK) {
2411 /* check for exists after object is locked */
2412 if (mdt_object_exists(o) == 0) {
2413 /* Non-existent object shouldn't have PDO lock */
2416 /* Non-dir object shouldn't have PDO lock */
2417 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2422 memset(policy, 0, sizeof(*policy));
2423 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2425 dlmflags = LDLM_FL_ATOMIC_CB;
2427 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2430 * Take PDO lock on whole directory and build correct @res_id for lock
2431 * on part of directory.
2433 if (lh->mlh_pdo_hash != 0) {
2434 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2435 mdt_lock_pdo_mode(info, o, lh);
2436 if (lh->mlh_pdo_mode != LCK_NL) {
2438 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2439 * is never going to be sent to client and we do not
2440 * want it slowed down due to possible cancels.
2442 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2443 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2444 policy, res_id, dlmflags,
2445 info->mti_exp == NULL ? NULL :
2446 &info->mti_exp->exp_handle.h_cookie);
2447 if (unlikely(rc != 0))
2448 GOTO(out_unlock, rc);
2452 * Finish res_id initializing by name hash marking part of
2453 * directory which is taking modification.
2455 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2458 policy->l_inodebits.bits = ibits;
2461 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2462 * going to be sent to client. If it is - mdt_intent_policy() path will
2463 * fix it up and turn FL_LOCAL flag off.
2465 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2466 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2467 info->mti_exp == NULL ? NULL :
2468 &info->mti_exp->exp_handle.h_cookie);
2471 mdt_object_unlock(info, o, lh, 1);
2472 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2473 lh->mlh_pdo_hash != 0 &&
2474 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2475 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2481 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2482 struct mdt_lock_handle *lh, __u64 ibits,
2483 bool nonblock, int locality)
2488 if (!mdt_object_remote(o))
2489 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2492 if (locality == MDT_LOCAL_LOCK) {
2493 CERROR("%s: try to get local lock for remote object"
2494 DFID".\n", mdt_obd_name(info->mti_mdt),
2495 PFID(mdt_object_fid(o)));
2499 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2500 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2501 MDS_INODELOCK_XATTR);
2502 if (ibits & MDS_INODELOCK_UPDATE) {
2503 /* Sigh, PDO needs to enqueue 2 locks right now, but
2504 * enqueue RPC can only request 1 lock, to avoid extra
2505 * RPC, so it will instead enqueue EX lock for remote
2506 * object anyway XXX*/
2507 if (lh->mlh_type == MDT_PDO_LOCK &&
2508 lh->mlh_pdo_hash != 0) {
2509 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2510 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2511 PFID(mdt_object_fid(o)));
2512 lh->mlh_pdo_hash = 0;
2513 lh->mlh_rreg_mode = LCK_EX;
2514 lh->mlh_type = MDT_REG_LOCK;
2516 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2519 MDS_INODELOCK_UPDATE);
2524 /* Only enqueue LOOKUP lock for remote object */
2525 if (ibits & MDS_INODELOCK_LOOKUP) {
2526 rc = mdt_object_local_lock(info, o, lh,
2527 MDS_INODELOCK_LOOKUP,
2528 nonblock, locality);
2536 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2537 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2539 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2542 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2543 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2545 struct mdt_lock_handle tmp = *lh;
2548 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2556 * Save a lock within request object.
2558 * Keep the lock referenced until whether client ACK or transaction
2559 * commit happens or release the lock immediately depending on input
2560 * parameters. If COS is ON, a write lock is converted to COS lock
2563 * \param info thead info object
2564 * \param h lock handle
2565 * \param mode lock mode
2566 * \param decref force immediate lock releasing
2569 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2570 ldlm_mode_t mode, int decref)
2574 if (lustre_handle_is_used(h)) {
2575 if (decref || !info->mti_has_trans ||
2576 !(mode & (LCK_PW | LCK_EX))){
2577 mdt_fid_unlock(h, mode);
2579 struct mdt_device *mdt = info->mti_mdt;
2580 struct ldlm_lock *lock = ldlm_handle2lock(h);
2581 struct ptlrpc_request *req = mdt_info_req(info);
2584 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2586 /* there is no request if mdt_object_unlock() is called
2587 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2588 if (likely(req != NULL)) {
2589 CDEBUG(D_HA, "request = %p reply state = %p"
2590 " transno = "LPD64"\n", req,
2591 req->rq_reply_state, req->rq_transno);
2592 if (mdt_cos_is_enabled(mdt)) {
2594 ldlm_lock_downgrade(lock, LCK_COS);
2597 ptlrpc_save_lock(req, h, mode, no_ack);
2599 ldlm_lock_decref(h, mode);
2601 if (mdt_is_lock_sync(lock)) {
2602 CDEBUG(D_HA, "found sync-lock,"
2603 " async commit started\n");
2604 mdt_device_commit_async(info->mti_env,
2607 LDLM_LOCK_PUT(lock);
2616 * Unlock mdt object.
2618 * Immeditely release the regular lock and the PDO lock or save the
2619 * lock in reqeuest and keep them referenced until client ACK or
2620 * transaction commit.
2622 * \param info thread info object
2623 * \param o mdt object
2624 * \param lh mdt lock handle referencing regular and PDO locks
2625 * \param decref force immediate lock releasing
2627 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2628 struct mdt_lock_handle *lh, int decref)
2632 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2633 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2635 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2636 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2641 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2642 const struct lu_fid *f,
2643 struct mdt_lock_handle *lh,
2646 struct mdt_object *o;
2648 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2652 rc = mdt_object_lock(info, o, lh, ibits,
2655 mdt_object_put(info->mti_env, o);
2662 void mdt_object_unlock_put(struct mdt_thread_info * info,
2663 struct mdt_object * o,
2664 struct mdt_lock_handle *lh,
2667 mdt_object_unlock(info, o, lh, decref);
2668 mdt_object_put(info->mti_env, o);
2672 * Generic code handling requests that have struct mdt_body passed in:
2674 * - extract mdt_body from request and save it in @info, if present;
2676 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2679 * - if HABEO_CORPUS flag is set for this request type check whether object
2680 * actually exists on storage (lu_object_exists()).
2683 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2685 const struct mdt_body *body;
2686 struct mdt_object *obj;
2687 const struct lu_env *env;
2688 struct req_capsule *pill;
2692 env = info->mti_env;
2693 pill = info->mti_pill;
2695 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2699 if (!(body->mbo_valid & OBD_MD_FLID))
2702 if (!fid_is_sane(&body->mbo_fid1)) {
2703 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2708 * Do not get size or any capa fields before we check that request
2709 * contains capa actually. There are some requests which do not, for
2710 * instance MDS_IS_SUBDIR.
2712 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2713 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2714 mdt_set_capainfo(info, 0, &body->mbo_fid1,
2715 req_capsule_client_get(pill, &RMF_CAPA1));
2717 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2719 if ((flags & HABEO_CORPUS) &&
2720 !mdt_object_exists(obj)) {
2721 mdt_object_put(env, obj);
2722 /* for capability renew ENOENT will be handled in
2724 if (body->mbo_valid & OBD_MD_FLOSSCAPA)
2729 info->mti_object = obj;
2738 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2740 struct req_capsule *pill = info->mti_pill;
2744 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2745 rc = mdt_body_unpack(info, flags);
2749 if (rc == 0 && (flags & HABEO_REFERO)) {
2751 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2752 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2754 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2755 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2758 rc = req_capsule_server_pack(pill);
2763 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2765 struct md_device *next = m->mdt_child;
2767 return next->md_ops->mdo_init_capa_ctxt(env, next,
2768 m->mdt_lut.lut_mds_capa,
2769 m->mdt_capa_timeout,
2774 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2776 lh->mlh_type = MDT_NUL_LOCK;
2777 lh->mlh_reg_lh.cookie = 0ull;
2778 lh->mlh_reg_mode = LCK_MINMODE;
2779 lh->mlh_pdo_lh.cookie = 0ull;
2780 lh->mlh_pdo_mode = LCK_MINMODE;
2781 lh->mlh_rreg_lh.cookie = 0ull;
2782 lh->mlh_rreg_mode = LCK_MINMODE;
2785 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2787 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2788 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2792 * Initialize fields of struct mdt_thread_info. Other fields are left in
2793 * uninitialized state, because it's too expensive to zero out whole
2794 * mdt_thread_info (> 1K) on each request arrival.
2796 void mdt_thread_info_init(struct ptlrpc_request *req,
2797 struct mdt_thread_info *info)
2801 info->mti_pill = &req->rq_pill;
2804 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2805 mdt_lock_handle_init(&info->mti_lh[i]);
2807 /* mdt device: it can be NULL while CONNECT */
2808 if (req->rq_export) {
2809 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2810 info->mti_exp = req->rq_export;
2812 info->mti_mdt = NULL;
2813 info->mti_env = req->rq_svc_thread->t_env;
2814 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2816 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2817 info->mti_big_buf = LU_BUF_NULL;
2818 info->mti_body = NULL;
2819 info->mti_object = NULL;
2820 info->mti_dlm_req = NULL;
2821 info->mti_has_trans = 0;
2822 info->mti_cross_ref = 0;
2823 info->mti_opdata = 0;
2824 info->mti_big_lmm_used = 0;
2826 info->mti_spec.no_create = 0;
2827 info->mti_spec.sp_rm_entry = 0;
2829 info->mti_spec.u.sp_ea.eadata = NULL;
2830 info->mti_spec.u.sp_ea.eadatalen = 0;
2833 void mdt_thread_info_fini(struct mdt_thread_info *info)
2837 if (info->mti_object != NULL) {
2838 mdt_object_put(info->mti_env, info->mti_object);
2839 info->mti_object = NULL;
2842 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2843 mdt_lock_handle_fini(&info->mti_lh[i]);
2844 info->mti_env = NULL;
2845 info->mti_pill = NULL;
2846 info->mti_exp = NULL;
2848 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2849 lu_buf_free(&info->mti_big_buf);
2852 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2854 struct mdt_thread_info *mti;
2855 struct lustre_capa *lc;
2857 mti = mdt_th_info(tsi->tsi_env);
2858 LASSERT(mti != NULL);
2860 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2861 if (tsi->tsi_corpus != NULL) {
2862 struct req_capsule *pill = tsi->tsi_pill;
2864 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2865 lu_object_get(tsi->tsi_corpus);
2868 * XXX: must be part of tgt_mdt_body_unpack but moved here
2869 * due to mdt_set_capainfo().
2871 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2872 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2873 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2874 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->mbo_fid1,
2878 mti->mti_body = tsi->tsi_mdt_body;
2879 mti->mti_dlm_req = tsi->tsi_dlm_req;
2884 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2886 struct ptlrpc_request *req = tgt_ses_req(tsi);
2891 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
2893 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
2894 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE, HZ * 3);
2896 rc = tgt_connect(tsi);
2900 rc = mdt_init_idmap(tsi);
2905 obd_disconnect(class_export_get(req->rq_export));
2924 static int mdt_intent_getattr(enum mdt_it_code opcode,
2925 struct mdt_thread_info *info,
2926 struct ldlm_lock **,
2929 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2930 struct mdt_thread_info *info,
2931 struct ldlm_lock **lockp,
2934 static int mdt_intent_layout(enum mdt_it_code opcode,
2935 struct mdt_thread_info *info,
2936 struct ldlm_lock **,
2938 static int mdt_intent_reint(enum mdt_it_code opcode,
2939 struct mdt_thread_info *info,
2940 struct ldlm_lock **,
2943 static struct mdt_it_flavor {
2944 const struct req_format *it_fmt;
2946 int (*it_act)(enum mdt_it_code ,
2947 struct mdt_thread_info *,
2948 struct ldlm_lock **,
2951 } mdt_it_flavor[] = {
2953 .it_fmt = &RQF_LDLM_INTENT,
2954 /*.it_flags = HABEO_REFERO,*/
2956 .it_act = mdt_intent_reint,
2957 .it_reint = REINT_OPEN
2960 .it_fmt = &RQF_LDLM_INTENT,
2962 * OCREAT is not a MUTABOR request as if the file
2964 * We do the extra check of OBD_CONNECT_RDONLY in
2965 * mdt_reint_open() when we really need to create
2969 .it_act = mdt_intent_reint,
2970 .it_reint = REINT_OPEN
2973 .it_fmt = &RQF_LDLM_INTENT,
2974 .it_flags = MUTABOR,
2975 .it_act = mdt_intent_reint,
2976 .it_reint = REINT_CREATE
2978 [MDT_IT_GETATTR] = {
2979 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2980 .it_flags = HABEO_REFERO,
2981 .it_act = mdt_intent_getattr
2983 [MDT_IT_READDIR] = {
2989 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2990 .it_flags = HABEO_REFERO,
2991 .it_act = mdt_intent_getattr
2994 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2995 .it_flags = MUTABOR,
2997 .it_reint = REINT_UNLINK
3001 .it_flags = MUTABOR,
3004 [MDT_IT_GETXATTR] = {
3005 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3006 .it_flags = HABEO_CORPUS,
3007 .it_act = mdt_intent_getxattr
3010 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3012 .it_act = mdt_intent_layout
3017 mdt_intent_lock_replace(struct mdt_thread_info *info,
3018 struct ldlm_lock **lockp,
3019 struct mdt_lock_handle *lh,
3022 struct ptlrpc_request *req = mdt_info_req(info);
3023 struct ldlm_lock *lock = *lockp;
3024 struct ldlm_lock *new_lock;
3026 /* If possible resent found a lock, @lh is set to its handle */
3027 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3029 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3030 lh->mlh_reg_lh.cookie = 0;
3034 LASSERTF(new_lock != NULL,
3035 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3038 * If we've already given this lock to a client once, then we should
3039 * have no readers or writers. Otherwise, we should have one reader
3040 * _or_ writer ref (which will be zeroed below) before returning the
3043 if (new_lock->l_export == req->rq_export) {
3044 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3046 LASSERT(new_lock->l_export == NULL);
3047 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3052 if (new_lock->l_export == req->rq_export) {
3054 * Already gave this to the client, which means that we
3055 * reconstructed a reply.
3057 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3060 LDLM_LOCK_RELEASE(new_lock);
3061 lh->mlh_reg_lh.cookie = 0;
3062 RETURN(ELDLM_LOCK_REPLACED);
3066 * Fixup the lock to be given to the client.
3068 lock_res_and_lock(new_lock);
3069 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3070 * possible blocking AST. */
3071 while (new_lock->l_readers > 0) {
3072 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3073 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3074 new_lock->l_readers--;
3076 while (new_lock->l_writers > 0) {
3077 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3078 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3079 new_lock->l_writers--;
3082 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3083 new_lock->l_blocking_ast = lock->l_blocking_ast;
3084 new_lock->l_completion_ast = lock->l_completion_ast;
3085 new_lock->l_remote_handle = lock->l_remote_handle;
3086 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3088 unlock_res_and_lock(new_lock);
3090 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3091 &new_lock->l_remote_handle,
3092 &new_lock->l_exp_hash);
3094 LDLM_LOCK_RELEASE(new_lock);
3095 lh->mlh_reg_lh.cookie = 0;
3097 RETURN(ELDLM_LOCK_REPLACED);
3100 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3101 struct ldlm_lock *new_lock,
3102 struct mdt_lock_handle *lh,
3105 struct ptlrpc_request *req = mdt_info_req(info);
3106 struct ldlm_request *dlmreq;
3108 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3111 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3113 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3114 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3116 if (flags & LDLM_FL_RESENT) {
3117 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3118 lh->mlh_reg_mode = new_lock->l_granted_mode;
3120 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3121 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3122 lh->mlh_reg_lh.cookie);
3127 * If the xid matches, then we know this is a resent request, and allow
3128 * it. (It's probably an OPEN, for which we don't send a lock.
3130 if (req_xid_is_last(req))
3134 * This remote handle isn't enqueued, so we never received or processed
3135 * this request. Clear MSG_RESENT, because it can be handled like any
3136 * normal request now.
3138 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3140 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3141 dlmreq->lock_handle[0].cookie);
3144 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3145 struct mdt_thread_info *info,
3146 struct ldlm_lock **lockp,
3149 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3150 struct ldlm_reply *ldlm_rep = NULL;
3154 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3155 * (for the resend case) or a new lock. Below we will use it to
3156 * replace the original lock.
3158 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3159 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3160 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3161 rc = mdt_object_lock(info, info->mti_object, lhc,
3162 MDS_INODELOCK_XATTR,
3168 grc = mdt_getxattr(info);
3170 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3172 if (mdt_info_req(info)->rq_repmsg != NULL)
3173 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3174 if (ldlm_rep == NULL)
3175 RETURN(err_serious(-EFAULT));
3177 ldlm_rep->lock_policy_res2 = grc;