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 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_log.h>
66 #include "mdt_internal.h"
67 #include <lustre_acl.h>
68 #include <lustre_param.h>
69 #include <lustre_quota.h>
70 #include <lustre_lfsck.h>
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73 [LCK_MINMODE] = MDL_MINMODE,
80 [LCK_GROUP] = MDL_GROUP
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84 [MDL_MINMODE] = LCK_MINMODE,
91 [MDL_GROUP] = LCK_GROUP
94 static struct mdt_device *mdt_dev(struct lu_device *d);
95 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
97 static const struct lu_object_operations mdt_obj_ops;
99 /* Slab for MDT object allocation */
100 static struct kmem_cache *mdt_object_kmem;
102 /* For HSM restore handles */
103 struct kmem_cache *mdt_hsm_cdt_kmem;
105 /* For HSM request handles */
106 struct kmem_cache *mdt_hsm_car_kmem;
108 static struct lu_kmem_descr mdt_caches[] = {
110 .ckd_cache = &mdt_object_kmem,
111 .ckd_name = "mdt_obj",
112 .ckd_size = sizeof(struct mdt_object)
115 .ckd_cache = &mdt_hsm_cdt_kmem,
116 .ckd_name = "mdt_cdt_restore_handle",
117 .ckd_size = sizeof(struct cdt_restore_handle)
120 .ckd_cache = &mdt_hsm_car_kmem,
121 .ckd_name = "mdt_cdt_agent_req",
122 .ckd_size = sizeof(struct cdt_agent_req)
129 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
133 return (rep->lock_policy_res1 & flag);
136 void mdt_clear_disposition(struct mdt_thread_info *info,
137 struct ldlm_reply *rep, int flag)
140 info->mti_opdata &= ~flag;
141 tgt_opdata_clear(info->mti_env, flag);
144 rep->lock_policy_res1 &= ~flag;
147 void mdt_set_disposition(struct mdt_thread_info *info,
148 struct ldlm_reply *rep, int flag)
151 info->mti_opdata |= flag;
152 tgt_opdata_set(info->mti_env, flag);
155 rep->lock_policy_res1 |= flag;
158 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
160 lh->mlh_pdo_hash = 0;
161 lh->mlh_reg_mode = lm;
162 lh->mlh_rreg_mode = lm;
163 lh->mlh_type = MDT_REG_LOCK;
166 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
167 const struct lu_name *lname)
169 lh->mlh_reg_mode = lock_mode;
170 lh->mlh_rreg_mode = lock_mode;
171 lh->mlh_type = MDT_PDO_LOCK;
173 if (lu_name_is_valid(lname)) {
174 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
176 /* XXX Workaround for LU-2856
178 * Zero is a valid return value of full_name_hash, but
179 * several users of mlh_pdo_hash assume a non-zero
180 * hash value. We therefore map zero onto an
181 * arbitrary, but consistent value (1) to avoid
182 * problems further down the road. */
183 if (unlikely(lh->mlh_pdo_hash == 0))
184 lh->mlh_pdo_hash = 1;
186 lh->mlh_pdo_hash = 0;
190 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
191 struct mdt_lock_handle *lh)
197 * Any dir access needs couple of locks:
199 * 1) on part of dir we gonna take lookup/modify;
201 * 2) on whole dir to protect it from concurrent splitting and/or to
202 * flush client's cache for readdir().
204 * so, for a given mode and object this routine decides what lock mode
205 * to use for lock #2:
207 * 1) if caller's gonna lookup in dir then we need to protect dir from
208 * being splitted only - LCK_CR
210 * 2) if caller's gonna modify dir then we need to protect dir from
211 * being splitted and to flush cache - LCK_CW
213 * 3) if caller's gonna modify dir and that dir seems ready for
214 * splitting then we need to protect it from any type of access
215 * (lookup/modify/split) - LCK_EX --bzzz
218 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
219 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
222 * Ask underlaying level its opinion about preferable PDO lock mode
223 * having access type passed as regular lock mode:
225 * - MDL_MINMODE means that lower layer does not want to specify lock
228 * - MDL_NL means that no PDO lock should be taken. This is used in some
229 * cases. Say, for non-splittable directories no need to use PDO locks
232 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
233 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
235 if (mode != MDL_MINMODE) {
236 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
239 * Lower layer does not want to specify locking mode. We do it
240 * our selves. No special protection is needed, just flush
241 * client's cache on modification and allow concurrent
244 switch (lh->mlh_reg_mode) {
246 lh->mlh_pdo_mode = LCK_EX;
249 lh->mlh_pdo_mode = LCK_CR;
252 lh->mlh_pdo_mode = LCK_CW;
255 CERROR("Not expected lock type (0x%x)\n",
256 (int)lh->mlh_reg_mode);
261 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
265 int mdt_getstatus(struct tgt_session_info *tsi)
267 struct mdt_thread_info *info = tsi2mdt_info(tsi);
268 struct mdt_device *mdt = info->mti_mdt;
269 struct mdt_body *repbody;
274 rc = mdt_check_ucred(info);
276 GOTO(out, rc = err_serious(rc));
278 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
279 GOTO(out, rc = err_serious(-ENOMEM));
281 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
282 repbody->fid1 = mdt->mdt_md_root_fid;
283 repbody->valid |= OBD_MD_FLID;
285 if (tsi->tsi_tgt->lut_mds_capa &&
286 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
287 struct mdt_object *root;
288 struct lustre_capa *capa;
290 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
292 GOTO(out, rc = PTR_ERR(root));
294 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
296 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
297 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
299 mdt_object_put(info->mti_env, root);
301 repbody->valid |= OBD_MD_FLMDSCAPA;
305 mdt_thread_info_fini(info);
309 int mdt_statfs(struct tgt_session_info *tsi)
311 struct ptlrpc_request *req = tgt_ses_req(tsi);
312 struct mdt_thread_info *info = tsi2mdt_info(tsi);
313 struct md_device *next = info->mti_mdt->mdt_child;
314 struct ptlrpc_service_part *svcpt;
315 struct obd_statfs *osfs;
320 svcpt = req->rq_rqbd->rqbd_svcpt;
322 /* This will trigger a watchdog timeout */
323 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
324 (MDT_SERVICE_WATCHDOG_FACTOR *
325 at_get(&svcpt->scp_at_estimate)) + 1);
327 rc = mdt_check_ucred(info);
329 GOTO(out, rc = err_serious(rc));
331 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
332 GOTO(out, rc = err_serious(-ENOMEM));
334 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
336 GOTO(out, rc = -EPROTO);
338 /** statfs information are cached in the mdt_device */
339 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
340 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
341 /** statfs data is too old, get up-to-date one */
342 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
345 spin_lock(&info->mti_mdt->mdt_osfs_lock);
346 info->mti_mdt->mdt_osfs = *osfs;
347 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
348 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
350 /** use cached statfs data */
351 spin_lock(&info->mti_mdt->mdt_osfs_lock);
352 *osfs = info->mti_mdt->mdt_osfs;
353 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
357 mdt_counter_incr(req, LPROC_MDT_STATFS);
359 mdt_thread_info_fini(info);
364 * Pack SOM attributes into the reply.
365 * Call under a DLM UPDATE lock.
367 static void mdt_pack_size2body(struct mdt_thread_info *info,
368 struct mdt_object *mo)
371 struct md_attr *ma = &info->mti_attr;
373 LASSERT(ma->ma_attr.la_valid & LA_MODE);
374 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
376 /* Check if Size-on-MDS is supported, if this is a regular file,
377 * if SOM is enabled on the object and if SOM cache exists and valid.
378 * Otherwise do not pack Size-on-MDS attributes to the reply. */
379 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
380 !S_ISREG(ma->ma_attr.la_mode) ||
381 !mdt_object_is_som_enabled(mo) ||
382 !(ma->ma_valid & MA_SOM))
385 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
386 b->size = ma->ma_som->msd_size;
387 b->blocks = ma->ma_som->msd_blocks;
390 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
391 const struct lu_attr *attr, const struct lu_fid *fid)
393 struct md_attr *ma = &info->mti_attr;
395 LASSERT(ma->ma_valid & MA_INODE);
397 b->atime = attr->la_atime;
398 b->mtime = attr->la_mtime;
399 b->ctime = attr->la_ctime;
400 b->mode = attr->la_mode;
401 b->size = attr->la_size;
402 b->blocks = attr->la_blocks;
403 b->uid = attr->la_uid;
404 b->gid = attr->la_gid;
405 b->flags = attr->la_flags;
406 b->nlink = attr->la_nlink;
407 b->rdev = attr->la_rdev;
409 /*XXX should pack the reply body according to lu_valid*/
410 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
411 OBD_MD_FLGID | OBD_MD_FLTYPE |
412 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
413 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
415 if (!S_ISREG(attr->la_mode)) {
416 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
417 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
418 /* means no objects are allocated on osts. */
419 LASSERT(!(ma->ma_valid & MA_LOV));
420 /* just ignore blocks occupied by extend attributes on MDS */
422 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
423 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
424 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
425 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
426 /* A released file stores its size on MDS. */
427 /* But return 1 block for released file, unless tools like tar
428 * will consider it fully sparse. (LU-3864)
430 if (unlikely(b->size == 0))
434 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
439 b->valid |= OBD_MD_FLID;
440 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
441 PFID(fid), b->nlink, b->mode, b->size);
445 mdt_body_reverse_idmap(info, b);
447 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
448 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
449 PFID(fid), (unsigned long long)b->size);
452 static inline int mdt_body_has_lov(const struct lu_attr *la,
453 const struct mdt_body *body)
455 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
456 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
459 void mdt_client_compatibility(struct mdt_thread_info *info)
461 struct mdt_body *body;
462 struct ptlrpc_request *req = mdt_info_req(info);
463 struct obd_export *exp = req->rq_export;
464 struct md_attr *ma = &info->mti_attr;
465 struct lu_attr *la = &ma->ma_attr;
468 if (exp_connect_layout(exp))
469 /* the client can deal with 16-bit lmm_stripe_count */
472 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
474 if (!mdt_body_has_lov(la, body))
477 /* now we have a reply with a lov for a client not compatible with the
478 * layout lock so we have to clean the layout generation number */
479 if (S_ISREG(la->la_mode))
480 ma->ma_lmm->lmm_layout_gen = 0;
484 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
487 const struct lu_env *env = info->mti_env;
491 LASSERT(info->mti_big_lmm_used == 0);
492 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
496 /* big_lmm may need to be grown */
497 if (info->mti_big_lmmsize < rc) {
498 int size = size_roundup_power2(rc);
500 if (info->mti_big_lmmsize > 0) {
501 /* free old buffer */
502 LASSERT(info->mti_big_lmm);
503 OBD_FREE_LARGE(info->mti_big_lmm,
504 info->mti_big_lmmsize);
505 info->mti_big_lmm = NULL;
506 info->mti_big_lmmsize = 0;
509 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
510 if (info->mti_big_lmm == NULL)
512 info->mti_big_lmmsize = size;
514 LASSERT(info->mti_big_lmmsize >= rc);
516 info->mti_buf.lb_buf = info->mti_big_lmm;
517 info->mti_buf.lb_len = info->mti_big_lmmsize;
518 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
523 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
524 struct md_attr *ma, const char *name)
526 struct md_object *next = mdt_object_child(o);
527 struct lu_buf *buf = &info->mti_buf;
530 if (strcmp(name, XATTR_NAME_LOV) == 0) {
531 buf->lb_buf = ma->ma_lmm;
532 buf->lb_len = ma->ma_lmm_size;
533 LASSERT(!(ma->ma_valid & MA_LOV));
534 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
535 buf->lb_buf = ma->ma_lmv;
536 buf->lb_len = ma->ma_lmv_size;
537 LASSERT(!(ma->ma_valid & MA_LMV));
538 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
539 buf->lb_buf = ma->ma_lmv;
540 buf->lb_len = ma->ma_lmv_size;
541 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
546 rc = mo_xattr_get(info->mti_env, next, buf, name);
548 if (strcmp(name, XATTR_NAME_LOV) == 0) {
549 ma->ma_lmm_size = rc;
550 ma->ma_valid |= MA_LOV;
551 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
552 ma->ma_lmv_size = rc;
553 ma->ma_valid |= MA_LMV;
554 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
555 ma->ma_lmv_size = rc;
556 ma->ma_valid |= MA_LMV_DEF;
560 } else if (rc == -ENODATA) {
563 } else if (rc == -ERANGE) {
564 /* Default LMV has fixed size, so it must be able to fit
565 * in the original buffer */
566 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
568 rc = mdt_big_xattr_get(info, o, name);
570 info->mti_big_lmm_used = 1;
571 if (!strcmp(name, XATTR_NAME_LOV)) {
572 ma->ma_valid |= MA_LOV;
573 ma->ma_lmm = info->mti_big_lmm;
574 ma->ma_lmm_size = rc;
575 } else if (!strcmp(name, XATTR_NAME_LMV)) {
576 ma->ma_valid |= MA_LMV;
577 ma->ma_lmv = info->mti_big_lmm;
578 ma->ma_lmv_size = rc;
583 /* update mdt_max_mdsize so all clients
584 * will be aware about that */
585 if (info->mti_mdt->mdt_max_mdsize < rc)
586 info->mti_mdt->mdt_max_mdsize = rc;
594 int mdt_attr_get_pfid(struct mdt_thread_info *info,
595 struct mdt_object *o, struct lu_fid *pfid)
597 struct lu_buf *buf = &info->mti_buf;
598 struct link_ea_header *leh;
599 struct link_ea_entry *lee;
603 buf->lb_buf = info->mti_big_lmm;
604 buf->lb_len = info->mti_big_lmmsize;
605 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
606 buf, XATTR_NAME_LINK);
607 /* ignore errors, MA_PFID won't be set and it is
608 * up to the caller to treat this as an error */
609 if (rc == -ERANGE || buf->lb_len == 0) {
610 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
611 buf->lb_buf = info->mti_big_lmm;
612 buf->lb_len = info->mti_big_lmmsize;
617 if (rc < sizeof(*leh)) {
618 CERROR("short LinkEA on "DFID": rc = %d\n",
619 PFID(mdt_object_fid(o)), rc);
623 leh = (struct link_ea_header *) buf->lb_buf;
624 lee = (struct link_ea_entry *)(leh + 1);
625 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
626 leh->leh_magic = LINK_EA_MAGIC;
627 leh->leh_reccount = __swab32(leh->leh_reccount);
628 leh->leh_len = __swab64(leh->leh_len);
630 if (leh->leh_magic != LINK_EA_MAGIC)
632 if (leh->leh_reccount == 0)
635 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
636 fid_be_to_cpu(pfid, pfid);
641 int mdt_attr_get_complex(struct mdt_thread_info *info,
642 struct mdt_object *o, struct md_attr *ma)
644 const struct lu_env *env = info->mti_env;
645 struct md_object *next = mdt_object_child(o);
646 struct lu_buf *buf = &info->mti_buf;
647 u32 mode = lu_object_attr(&next->mo_lu);
648 int need = ma->ma_need;
654 if (need & MA_INODE) {
655 ma->ma_need = MA_INODE;
656 rc = mo_attr_get(env, next, ma);
659 ma->ma_valid |= MA_INODE;
662 if (need & MA_PFID) {
663 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
665 ma->ma_valid |= MA_PFID;
666 /* ignore this error, parent fid is not mandatory */
670 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
671 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
676 if (need & MA_LMV && S_ISDIR(mode)) {
677 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
682 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
683 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
688 if (need & MA_SOM && S_ISREG(mode)) {
689 buf->lb_buf = info->mti_xattr_buf;
690 buf->lb_len = sizeof(info->mti_xattr_buf);
691 CLASSERT(sizeof(struct som_attrs) <=
692 sizeof(info->mti_xattr_buf));
693 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
694 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
696 ma->ma_valid |= MA_SOM;
697 else if (rc2 < 0 && rc2 != -ENODATA)
701 if (need & MA_HSM && S_ISREG(mode)) {
702 buf->lb_buf = info->mti_xattr_buf;
703 buf->lb_len = sizeof(info->mti_xattr_buf);
704 CLASSERT(sizeof(struct hsm_attrs) <=
705 sizeof(info->mti_xattr_buf));
706 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
707 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
709 ma->ma_valid |= MA_HSM;
710 else if (rc2 < 0 && rc2 != -ENODATA)
714 #ifdef CONFIG_FS_POSIX_ACL
715 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
716 buf->lb_buf = ma->ma_acl;
717 buf->lb_len = ma->ma_acl_size;
718 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
720 ma->ma_acl_size = rc2;
721 ma->ma_valid |= MA_ACL_DEF;
722 } else if (rc2 == -ENODATA) {
731 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
732 rc, ma->ma_valid, ma->ma_lmm);
736 static int mdt_getattr_internal(struct mdt_thread_info *info,
737 struct mdt_object *o, int ma_need)
739 struct md_object *next = mdt_object_child(o);
740 const struct mdt_body *reqbody = info->mti_body;
741 struct ptlrpc_request *req = mdt_info_req(info);
742 struct md_attr *ma = &info->mti_attr;
743 struct lu_attr *la = &ma->ma_attr;
744 struct req_capsule *pill = info->mti_pill;
745 const struct lu_env *env = info->mti_env;
746 struct mdt_body *repbody;
747 struct lu_buf *buffer = &info->mti_buf;
752 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
753 RETURN(err_serious(-ENOMEM));
755 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
759 if (mdt_object_remote(o)) {
760 /* This object is located on remote node.*/
761 /* Return -EIO for old client */
762 if (!mdt_is_dne_client(req->rq_export))
763 GOTO(out, rc = -EIO);
765 repbody->fid1 = *mdt_object_fid(o);
766 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
770 buffer->lb_len = reqbody->eadatasize;
771 if (buffer->lb_len > 0) {
772 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
773 if (buffer->lb_buf == NULL)
774 GOTO(out, rc = -EPROTO);
776 buffer->lb_buf = NULL;
777 ma_need &= ~(MA_LOV | MA_LMV);
778 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
779 mdt_obd_name(info->mti_mdt),
780 req->rq_export->exp_client_uuid.uuid);
783 /* If it is dir object and client require MEA, then we got MEA */
784 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
785 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
786 /* Assumption: MDT_MD size is enough for lmv size. */
787 ma->ma_lmv = buffer->lb_buf;
788 ma->ma_lmv_size = buffer->lb_len;
789 ma->ma_need = MA_INODE;
790 if (ma->ma_lmv_size > 0) {
791 if (reqbody->valid & OBD_MD_MEA)
792 ma->ma_need |= MA_LMV;
793 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
794 ma->ma_need |= MA_LMV_DEF;
797 ma->ma_lmm = buffer->lb_buf;
798 ma->ma_lmm_size = buffer->lb_len;
799 ma->ma_need = MA_INODE | MA_HSM;
800 if (ma->ma_lmm_size > 0)
801 ma->ma_need |= MA_LOV;
804 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
805 reqbody->valid & OBD_MD_FLDIREA &&
806 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
807 /* get default stripe info for this dir. */
808 ma->ma_need |= MA_LOV_DEF;
810 ma->ma_need |= ma_need;
811 if (ma->ma_need & MA_SOM)
812 ma->ma_som = &info->mti_u.som.data;
814 rc = mdt_attr_get_complex(info, o, ma);
816 CERROR("%s: getattr error for "DFID": rc = %d\n",
817 mdt_obd_name(info->mti_mdt),
818 PFID(mdt_object_fid(o)), rc);
822 /* if file is released, check if a restore is running */
823 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
824 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
825 repbody->t_state = MS_RESTORE;
826 repbody->valid |= OBD_MD_TSTATE;
829 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
831 /* the Lustre protocol supposes to return default striping
832 * on the user-visible root if explicitly requested */
833 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
834 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
835 struct lu_fid rootfid;
836 struct mdt_object *root;
837 struct mdt_device *mdt = info->mti_mdt;
839 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
842 root = mdt_object_find(env, mdt, &rootfid);
844 RETURN(PTR_ERR(root));
845 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
846 mdt_object_put(info->mti_env, root);
848 CERROR("%s: getattr error for "DFID": rc = %d\n",
849 mdt_obd_name(info->mti_mdt),
850 PFID(mdt_object_fid(o)), rc);
855 if (likely(ma->ma_valid & MA_INODE))
856 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
860 if (mdt_body_has_lov(la, reqbody)) {
861 if (ma->ma_valid & MA_LOV) {
862 LASSERT(ma->ma_lmm_size);
863 mdt_dump_lmm(D_INFO, ma->ma_lmm);
864 repbody->eadatasize = ma->ma_lmm_size;
865 if (S_ISDIR(la->la_mode))
866 repbody->valid |= OBD_MD_FLDIREA;
868 repbody->valid |= OBD_MD_FLEASIZE;
870 if (ma->ma_valid & MA_LMV) {
871 LASSERT(S_ISDIR(la->la_mode));
872 mdt_dump_lmv(D_INFO, ma->ma_lmv);
873 repbody->eadatasize = ma->ma_lmv_size;
874 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
876 if (ma->ma_valid & MA_LMV_DEF) {
877 LASSERT(S_ISDIR(la->la_mode));
878 repbody->eadatasize = ma->ma_lmv_size;
879 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
881 } else if (S_ISLNK(la->la_mode) &&
882 reqbody->valid & OBD_MD_LINKNAME) {
883 buffer->lb_buf = ma->ma_lmm;
884 /* eadatasize from client includes NULL-terminator, so
885 * there is no need to read it */
886 buffer->lb_len = reqbody->eadatasize - 1;
887 rc = mo_readlink(env, next, buffer);
888 if (unlikely(rc <= 0)) {
889 CERROR("%s: readlink failed for "DFID": rc = %d\n",
890 mdt_obd_name(info->mti_mdt),
891 PFID(mdt_object_fid(o)), rc);
894 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
896 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
898 repbody->valid |= OBD_MD_LINKNAME;
899 /* we need to report back size with NULL-terminator
900 * because client expects that */
901 repbody->eadatasize = rc + 1;
902 if (repbody->eadatasize != reqbody->eadatasize)
903 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
904 "on "DFID ", expected %d\n",
905 mdt_obd_name(info->mti_mdt),
906 rc, PFID(mdt_object_fid(o)),
907 reqbody->eadatasize - 1);
909 ((char *)ma->ma_lmm)[rc] = 0;
911 /* If the total CDEBUG() size is larger than a page, it
912 * will print a warning to the console, avoid this by
913 * printing just the last part of the symlink. */
914 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
915 print_limit < rc ? "..." : "", print_limit,
916 (char *)ma->ma_lmm + rc - print_limit, rc);
921 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
922 repbody->max_cookiesize = 0;
923 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
924 repbody->valid |= OBD_MD_FLMODEASIZE;
925 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
926 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
927 repbody->max_cookiesize);
930 if (exp_connect_rmtclient(info->mti_exp) &&
931 reqbody->valid & OBD_MD_FLRMTPERM) {
932 void *buf = req_capsule_server_get(pill, &RMF_ACL);
934 /* mdt_getattr_lock only */
935 rc = mdt_pack_remote_perm(info, o, buf);
937 repbody->valid &= ~OBD_MD_FLRMTPERM;
938 repbody->aclsize = 0;
941 repbody->valid |= OBD_MD_FLRMTPERM;
942 repbody->aclsize = sizeof(struct mdt_remote_perm);
945 #ifdef CONFIG_FS_POSIX_ACL
946 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
947 (reqbody->valid & OBD_MD_FLACL)) {
948 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
949 buffer->lb_len = req_capsule_get_size(pill,
950 &RMF_ACL, RCL_SERVER);
951 if (buffer->lb_len > 0) {
952 rc = mo_xattr_get(env, next, buffer,
953 XATTR_NAME_ACL_ACCESS);
955 if (rc == -ENODATA) {
956 repbody->aclsize = 0;
957 repbody->valid |= OBD_MD_FLACL;
959 } else if (rc == -EOPNOTSUPP) {
962 CERROR("%s: unable to read "DFID
964 mdt_obd_name(info->mti_mdt),
965 PFID(mdt_object_fid(o)), rc);
968 repbody->aclsize = rc;
969 repbody->valid |= OBD_MD_FLACL;
976 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
977 info->mti_mdt->mdt_lut.lut_mds_capa &&
978 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
979 struct lustre_capa *capa;
981 capa = req_capsule_server_get(pill, &RMF_CAPA1);
983 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
984 rc = mo_capa_get(env, next, capa, 0);
987 repbody->valid |= OBD_MD_FLMDSCAPA;
992 mdt_counter_incr(req, LPROC_MDT_GETATTR);
997 static int mdt_renew_capa(struct mdt_thread_info *info)
999 struct mdt_object *obj = info->mti_object;
1000 struct mdt_body *body;
1001 struct lustre_capa *capa, *c;
1005 /* if object doesn't exist, or server has disabled capability,
1006 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1009 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1010 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1013 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1014 LASSERT(body != NULL);
1016 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1019 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1023 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1025 body->valid |= OBD_MD_FLOSSCAPA;
1029 int mdt_getattr(struct tgt_session_info *tsi)
1031 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1032 struct mdt_object *obj = info->mti_object;
1033 struct req_capsule *pill = info->mti_pill;
1034 struct mdt_body *reqbody;
1035 struct mdt_body *repbody;
1040 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1043 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1044 rc = req_capsule_server_pack(pill);
1046 RETURN(err_serious(rc));
1047 rc = mdt_renew_capa(info);
1048 GOTO(out_shrink, rc);
1051 LASSERT(obj != NULL);
1052 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1054 mode = lu_object_attr(&obj->mot_obj);
1056 /* old clients may not report needed easize, use max value then */
1057 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1058 reqbody->eadatasize == 0 ?
1059 info->mti_mdt->mdt_max_mdsize :
1060 reqbody->eadatasize);
1062 rc = req_capsule_server_pack(pill);
1063 if (unlikely(rc != 0))
1064 GOTO(out, rc = err_serious(rc));
1066 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1067 LASSERT(repbody != NULL);
1068 repbody->eadatasize = 0;
1069 repbody->aclsize = 0;
1071 if (reqbody->valid & OBD_MD_FLRMTPERM)
1072 rc = mdt_init_ucred(info, reqbody);
1074 rc = mdt_check_ucred(info);
1076 GOTO(out_shrink, rc);
1078 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1081 * Don't check capability at all, because rename might getattr for
1082 * remote obj, and at that time no capability is available.
1084 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1085 rc = mdt_getattr_internal(info, obj, 0);
1086 if (reqbody->valid & OBD_MD_FLRMTPERM)
1087 mdt_exit_ucred(info);
1090 mdt_client_compatibility(info);
1091 rc2 = mdt_fix_reply(info);
1095 mdt_thread_info_fini(info);
1099 int mdt_is_subdir(struct tgt_session_info *tsi)
1101 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1102 struct mdt_object *o = info->mti_object;
1103 struct req_capsule *pill = info->mti_pill;
1104 const struct mdt_body *body = info->mti_body;
1105 struct mdt_body *repbody;
1111 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1114 * We save last checked parent fid to @repbody->fid1 for remote
1117 LASSERT(fid_is_sane(&body->fid2));
1118 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1119 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1120 &body->fid2, &repbody->fid1);
1121 if (rc == 0 || rc == -EREMOTE)
1122 repbody->valid |= OBD_MD_FLID;
1124 mdt_thread_info_fini(info);
1128 int mdt_swap_layouts(struct tgt_session_info *tsi)
1130 struct mdt_thread_info *info;
1131 struct ptlrpc_request *req = tgt_ses_req(tsi);
1132 struct obd_export *exp = req->rq_export;
1133 struct mdt_object *o1, *o2, *o;
1134 struct mdt_lock_handle *lh1, *lh2;
1135 struct mdc_swap_layouts *msl;
1139 /* client does not support layout lock, so layout swaping
1141 * FIXME: there is a problem for old clients which don't support
1142 * layout lock yet. If those clients have already opened the file
1143 * they won't be notified at all so that old layout may still be
1144 * used to do IO. This can be fixed after file release is landed by
1145 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1146 if (!exp_connect_layout(exp))
1147 RETURN(-EOPNOTSUPP);
1149 info = tsi2mdt_info(tsi);
1150 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1151 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1152 req_capsule_client_get(info->mti_pill,
1155 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1156 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1157 req_capsule_client_get(info->mti_pill,
1160 o1 = info->mti_object;
1161 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1162 &info->mti_body->fid2);
1164 GOTO(out, rc = PTR_ERR(o));
1166 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1167 GOTO(put, rc = -ENOENT);
1169 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1170 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1176 /* permission check. Make sure the calling process having permission
1177 * to write both files. */
1178 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1183 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1188 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1190 GOTO(put, rc = -EPROTO);
1192 lh1 = &info->mti_lh[MDT_LH_NEW];
1193 mdt_lock_reg_init(lh1, LCK_EX);
1194 lh2 = &info->mti_lh[MDT_LH_OLD];
1195 mdt_lock_reg_init(lh2, LCK_EX);
1197 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1198 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1202 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1203 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1207 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1208 mdt_object_child(o2), msl->msl_flags);
1211 mdt_object_unlock(info, o2, lh2, rc);
1213 mdt_object_unlock(info, o1, lh1, rc);
1215 mdt_object_put(info->mti_env, o);
1217 mdt_thread_info_fini(info);
1221 static int mdt_raw_lookup(struct mdt_thread_info *info,
1222 struct mdt_object *parent,
1223 const struct lu_name *lname,
1224 struct ldlm_reply *ldlm_rep)
1226 struct md_object *next = mdt_object_child(info->mti_object);
1227 const struct mdt_body *reqbody = info->mti_body;
1228 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1229 struct mdt_body *repbody;
1233 if (reqbody->valid != OBD_MD_FLID)
1236 LASSERT(!info->mti_cross_ref);
1238 /* Only got the fid of this obj by name */
1239 fid_zero(child_fid);
1240 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1243 /* XXX is raw_lookup possible as intent operation? */
1246 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1249 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1251 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1254 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1255 repbody->fid1 = *child_fid;
1256 repbody->valid = OBD_MD_FLID;
1262 * UPDATE lock should be taken against parent, and be release before exit;
1263 * child_bits lock should be taken against child, and be returned back:
1264 * (1)normal request should release the child lock;
1265 * (2)intent request will grant the lock to client.
1267 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1268 struct mdt_lock_handle *lhc,
1270 struct ldlm_reply *ldlm_rep)
1272 struct ptlrpc_request *req = mdt_info_req(info);
1273 struct mdt_body *reqbody = NULL;
1274 struct mdt_object *parent = info->mti_object;
1275 struct mdt_object *child;
1276 struct md_object *next = mdt_object_child(parent);
1277 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1278 struct lu_name *lname = NULL;
1279 struct mdt_lock_handle *lhp = NULL;
1280 struct ldlm_lock *lock;
1281 struct ldlm_res_id *res_id;
1288 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1289 LASSERT(ergo(is_resent,
1290 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1292 LASSERT(parent != NULL);
1294 lname = &info->mti_name;
1295 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1297 if (!info->mti_cross_ref) {
1299 * XXX: Check for anonymous name is for getattr by fid
1300 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1301 * that is the name must contain at least one character and
1302 * the terminating '\0'.
1304 if (!lu_name_is_valid(lname)) {
1305 reqbody = req_capsule_client_get(info->mti_pill,
1307 if (unlikely(reqbody == NULL))
1308 RETURN(err_serious(-EFAULT));
1310 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1311 RETURN(err_serious(-EINVAL));
1313 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1315 PFID(mdt_object_fid(parent)),
1316 PFID(&reqbody->fid2), ldlm_rep);
1318 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1319 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1320 PNAME(lname), ldlm_rep);
1324 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1326 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1327 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1329 "Parent doesn't exist!\n");
1331 } else if (!info->mti_cross_ref) {
1332 LASSERTF(!mdt_object_remote(parent),
1333 "Parent "DFID" is on remote server\n",
1334 PFID(mdt_object_fid(parent)));
1337 if (lu_name_is_valid(lname)) {
1338 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1346 if (info->mti_cross_ref) {
1347 /* Only getattr on the child. Parent is on another node. */
1348 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1350 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1351 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1354 /* Do not take lock for resent case. */
1355 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1356 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1357 lhc->mlh_reg_lh.cookie);
1358 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1359 &lock->l_resource->lr_name));
1360 LDLM_LOCK_PUT(lock);
1363 mdt_lock_handle_init(lhc);
1364 mdt_lock_reg_init(lhc, LCK_PR);
1367 * Object's name is on another MDS, no lookup or layout
1368 * lock is needed here but update lock is.
1370 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1371 MDS_INODELOCK_LAYOUT);
1372 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1374 rc = mdt_object_lock(info, child, lhc, child_bits,
1378 /* Finally, we can get attr for child. */
1379 if (!mdt_object_exists(child)) {
1380 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1382 "remote object doesn't exist.\n");
1383 mdt_object_unlock(info, child, lhc, 1);
1387 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1389 rc = mdt_getattr_internal(info, child, 0);
1390 if (unlikely(rc != 0))
1391 mdt_object_unlock(info, child, lhc, 1);
1396 if (lu_name_is_valid(lname)) {
1397 /* step 1: lock parent only if parent is a directory */
1398 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1399 lhp = &info->mti_lh[MDT_LH_PARENT];
1400 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1401 rc = mdt_object_lock(info, parent, lhp,
1402 MDS_INODELOCK_UPDATE,
1404 if (unlikely(rc != 0))
1408 /* step 2: lookup child's fid by name */
1409 fid_zero(child_fid);
1410 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1415 mdt_set_disposition(info, ldlm_rep,
1417 GOTO(out_parent, rc);
1419 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1421 *child_fid = reqbody->fid2;
1422 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1426 *step 3: find the child object by fid & lock it.
1427 * regardless if it is local or remote.
1429 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1430 * set parent dir fid the same as child fid in getattr by fid case
1431 * we should not lu_object_find() the object again, could lead
1432 * to hung if there is a concurrent unlink destroyed the object.
1434 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1435 mdt_object_get(info->mti_env, parent);
1438 child = mdt_object_find(info->mti_env, info->mti_mdt,
1442 if (unlikely(IS_ERR(child)))
1443 GOTO(out_parent, rc = PTR_ERR(child));
1445 /* Do not take lock for resent case. */
1446 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1447 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1448 lhc->mlh_reg_lh.cookie);
1450 res_id = &lock->l_resource->lr_name;
1451 if (!fid_res_name_eq(mdt_object_fid(child),
1452 &lock->l_resource->lr_name)) {
1453 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1454 &lock->l_resource->lr_name),
1455 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1456 PLDLMRES(lock->l_resource),
1457 PFID(mdt_object_fid(parent)));
1458 CWARN("Although resent, but still not get child lock"
1459 "parent:"DFID" child:"DFID"\n",
1460 PFID(mdt_object_fid(parent)),
1461 PFID(mdt_object_fid(child)));
1462 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1463 LDLM_LOCK_PUT(lock);
1466 LDLM_LOCK_PUT(lock);
1469 bool try_layout = false;
1472 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1473 mdt_lock_handle_init(lhc);
1474 mdt_lock_reg_init(lhc, LCK_PR);
1476 if (!mdt_object_exists(child)) {
1477 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1479 "Object doesn't exist!\n");
1480 GOTO(out_child, rc = -ENOENT);
1483 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1484 mdt_object_exists(child) && !mdt_object_remote(child)) {
1485 struct md_attr *ma = &info->mti_attr;
1488 ma->ma_need = MA_INODE;
1489 rc = mdt_attr_get_complex(info, child, ma);
1490 if (unlikely(rc != 0))
1491 GOTO(out_child, rc);
1493 /* If the file has not been changed for some time, we
1494 * return not only a LOOKUP lock, but also an UPDATE
1495 * lock and this might save us RPC on later STAT. For
1496 * directories, it also let negative dentry cache start
1497 * working for this dir. */
1498 if (ma->ma_valid & MA_INODE &&
1499 ma->ma_attr.la_valid & LA_CTIME &&
1500 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1501 ma->ma_attr.la_ctime < cfs_time_current_sec())
1502 child_bits |= MDS_INODELOCK_UPDATE;
1505 /* layout lock must be granted in a best-effort way
1506 * for IT operations */
1507 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1508 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1509 exp_connect_layout(info->mti_exp) &&
1510 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1511 !mdt_object_remote(child) && ldlm_rep != NULL) {
1512 /* try to grant layout lock for regular file. */
1518 child_bits |= MDS_INODELOCK_LAYOUT;
1519 /* try layout lock, it may fail to be granted due to
1520 * contention at LOOKUP or UPDATE */
1521 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1523 child_bits &= ~MDS_INODELOCK_LAYOUT;
1524 LASSERT(child_bits != 0);
1525 rc = mdt_object_lock(info, child, lhc,
1526 child_bits, MDT_CROSS_LOCK);
1531 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1532 * client will enqueue the lock to the remote MDT */
1533 if (mdt_object_remote(child))
1534 child_bits &= ~MDS_INODELOCK_UPDATE;
1535 rc = mdt_object_lock(info, child, lhc, child_bits,
1538 if (unlikely(rc != 0))
1539 GOTO(out_child, rc);
1542 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1543 /* Get MA_SOM attributes if update lock is given. */
1545 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1546 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1549 /* finally, we can get attr for child. */
1550 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1551 rc = mdt_getattr_internal(info, child, ma_need);
1552 if (unlikely(rc != 0)) {
1553 mdt_object_unlock(info, child, lhc, 1);
1555 /* Debugging code. */
1556 res_id = &lock->l_resource->lr_name;
1557 LDLM_DEBUG(lock, "Returning lock to client");
1558 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1559 &lock->l_resource->lr_name),
1560 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1561 PLDLMRES(lock->l_resource),
1562 PFID(mdt_object_fid(child)));
1563 if (mdt_object_exists(child) && !mdt_object_remote(child))
1564 mdt_pack_size2body(info, child);
1567 LDLM_LOCK_PUT(lock);
1571 mdt_object_put(info->mti_env, child);
1574 mdt_object_unlock(info, parent, lhp, 1);
1578 /* normal handler: should release the child lock */
1579 int mdt_getattr_name(struct tgt_session_info *tsi)
1581 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1582 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1583 struct mdt_body *reqbody;
1584 struct mdt_body *repbody;
1588 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1589 LASSERT(reqbody != NULL);
1590 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1591 LASSERT(repbody != NULL);
1593 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1594 repbody->eadatasize = 0;
1595 repbody->aclsize = 0;
1597 rc = mdt_init_ucred(info, reqbody);
1599 GOTO(out_shrink, rc);
1601 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1602 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1603 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1604 lhc->mlh_reg_lh.cookie = 0;
1606 mdt_exit_ucred(info);
1609 mdt_client_compatibility(info);
1610 rc2 = mdt_fix_reply(info);
1613 mdt_thread_info_fini(info);
1617 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1618 void *karg, void *uarg);
1620 int mdt_set_info(struct tgt_session_info *tsi)
1622 struct ptlrpc_request *req = tgt_ses_req(tsi);
1625 int keylen, vallen, rc = 0;
1629 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1631 DEBUG_REQ(D_HA, req, "no set_info key");
1632 RETURN(err_serious(-EFAULT));
1635 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1638 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1640 DEBUG_REQ(D_HA, req, "no set_info val");
1641 RETURN(err_serious(-EFAULT));
1644 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1647 /* Swab any part of val you need to here */
1648 if (KEY_IS(KEY_READ_ONLY)) {
1649 spin_lock(&req->rq_export->exp_lock);
1651 *exp_connect_flags_ptr(req->rq_export) |=
1654 *exp_connect_flags_ptr(req->rq_export) &=
1655 ~OBD_CONNECT_RDONLY;
1656 spin_unlock(&req->rq_export->exp_lock);
1657 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1658 struct changelog_setinfo *cs = val;
1660 if (vallen != sizeof(*cs)) {
1661 CERROR("%s: bad changelog_clear setinfo size %d\n",
1662 tgt_name(tsi->tsi_tgt), vallen);
1665 if (ptlrpc_req_need_swab(req)) {
1666 __swab64s(&cs->cs_recno);
1667 __swab32s(&cs->cs_id);
1670 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1678 int mdt_readpage(struct tgt_session_info *tsi)
1680 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1681 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1682 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1683 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1684 struct mdt_body *repbody;
1690 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1691 RETURN(err_serious(-ENOMEM));
1693 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1694 if (repbody == NULL || reqbody == NULL)
1695 RETURN(err_serious(-EFAULT));
1698 * prepare @rdpg before calling lower layers and transfer itself. Here
1699 * reqbody->size contains offset of where to start to read and
1700 * reqbody->nlink contains number bytes to read.
1702 rdpg->rp_hash = reqbody->size;
1703 if (rdpg->rp_hash != reqbody->size) {
1704 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1705 rdpg->rp_hash, reqbody->size);
1709 rdpg->rp_attrs = reqbody->mode;
1710 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1711 rdpg->rp_attrs |= LUDA_64BITHASH;
1712 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1713 exp_max_brw_size(tsi->tsi_exp));
1714 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1716 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1717 if (rdpg->rp_pages == NULL)
1720 for (i = 0; i < rdpg->rp_npages; ++i) {
1721 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1722 if (rdpg->rp_pages[i] == NULL)
1723 GOTO(free_rdpg, rc = -ENOMEM);
1726 /* call lower layers to fill allocated pages with directory data */
1727 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1729 GOTO(free_rdpg, rc);
1731 /* send pages to client */
1732 rc = tgt_sendpage(tsi, rdpg, rc);
1737 for (i = 0; i < rdpg->rp_npages; i++)
1738 if (rdpg->rp_pages[i] != NULL)
1739 __free_page(rdpg->rp_pages[i]);
1740 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1742 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1748 static int mdt_reint_internal(struct mdt_thread_info *info,
1749 struct mdt_lock_handle *lhc,
1752 struct req_capsule *pill = info->mti_pill;
1753 struct mdt_body *repbody;
1758 rc = mdt_reint_unpack(info, op);
1760 CERROR("Can't unpack reint, rc %d\n", rc);
1761 RETURN(err_serious(rc));
1764 /* for replay (no_create) lmm is not needed, client has it already */
1765 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1766 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1767 info->mti_rr.rr_eadatalen);
1769 /* llog cookies are always 0, the field is kept for compatibility */
1770 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1771 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1773 rc = req_capsule_server_pack(pill);
1775 CERROR("Can't pack response, rc %d\n", rc);
1776 RETURN(err_serious(rc));
1779 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1780 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1782 repbody->eadatasize = 0;
1783 repbody->aclsize = 0;
1786 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1788 /* for replay no cookkie / lmm need, because client have this already */
1789 if (info->mti_spec.no_create)
1790 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1791 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1793 rc = mdt_init_ucred_reint(info);
1795 GOTO(out_shrink, rc);
1797 rc = mdt_fix_attr_ucred(info, op);
1799 GOTO(out_ucred, rc = err_serious(rc));
1801 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1802 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1803 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1804 GOTO(out_ucred, rc);
1806 rc = mdt_reint_rec(info, lhc);
1809 mdt_exit_ucred(info);
1811 mdt_client_compatibility(info);
1812 rc2 = mdt_fix_reply(info);
1818 static long mdt_reint_opcode(struct ptlrpc_request *req,
1819 const struct req_format **fmt)
1821 struct mdt_device *mdt;
1822 struct mdt_rec_reint *rec;
1825 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1827 opc = rec->rr_opcode;
1828 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1829 if (opc < REINT_MAX && fmt[opc] != NULL)
1830 req_capsule_extend(&req->rq_pill, fmt[opc]);
1832 mdt = mdt_exp2dev(req->rq_export);
1833 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1834 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1835 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1836 opc = err_serious(-EFAULT);
1839 opc = err_serious(-EFAULT);
1844 int mdt_reint(struct tgt_session_info *tsi)
1848 static const struct req_format *reint_fmts[REINT_MAX] = {
1849 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1850 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1851 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1852 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1853 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1854 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1855 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1856 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1857 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1862 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1864 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1866 * No lock possible here from client to pass it to reint code
1869 rc = mdt_reint_internal(info, NULL, opc);
1870 mdt_thread_info_fini(info);
1875 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1879 /* this should sync the whole device */
1880 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1882 struct dt_device *dt = mdt->mdt_bottom;
1886 rc = dt->dd_ops->dt_sync(env, dt);
1890 /* this should sync this object */
1891 static int mdt_object_sync(struct mdt_thread_info *info)
1893 struct md_object *next;
1897 if (!mdt_object_exists(info->mti_object)) {
1898 CWARN("Non existing object "DFID"!\n",
1899 PFID(mdt_object_fid(info->mti_object)));
1902 next = mdt_object_child(info->mti_object);
1903 rc = mo_object_sync(info->mti_env, next);
1908 int mdt_sync(struct tgt_session_info *tsi)
1910 struct ptlrpc_request *req = tgt_ses_req(tsi);
1911 struct req_capsule *pill = tsi->tsi_pill;
1912 struct mdt_body *body;
1917 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1918 RETURN(err_serious(-ENOMEM));
1920 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1921 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1923 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1925 /* sync an object */
1926 rc = mdt_object_sync(info);
1928 const struct lu_fid *fid;
1929 struct lu_attr *la = &info->mti_attr.ma_attr;
1931 info->mti_attr.ma_need = MA_INODE;
1932 info->mti_attr.ma_valid = 0;
1933 rc = mdt_attr_get_complex(info, info->mti_object,
1936 body = req_capsule_server_get(pill,
1938 fid = mdt_object_fid(info->mti_object);
1939 mdt_pack_attr2body(info, body, la, fid);
1942 mdt_thread_info_fini(info);
1945 mdt_counter_incr(req, LPROC_MDT_SYNC);
1951 * Handle quota control requests to consult current usage/limit, but also
1952 * to configure quota enforcement
1954 int mdt_quotactl(struct tgt_session_info *tsi)
1956 struct obd_export *exp = tsi->tsi_exp;
1957 struct req_capsule *pill = tsi->tsi_pill;
1958 struct obd_quotactl *oqctl, *repoqc;
1960 struct mdt_device *mdt = mdt_exp2dev(exp);
1961 struct lu_device *qmt = mdt->mdt_qmt_dev;
1964 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1966 RETURN(err_serious(-EPROTO));
1968 rc = req_capsule_server_pack(pill);
1970 RETURN(err_serious(rc));
1972 switch (oqctl->qc_cmd) {
1974 case LUSTRE_Q_INVALIDATE:
1975 case LUSTRE_Q_FINVALIDATE:
1979 /* deprecated, not used any more */
1980 RETURN(-EOPNOTSUPP);
1981 /* master quotactl */
1987 RETURN(-EOPNOTSUPP);
1988 /* slave quotactl */
1993 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1997 /* map uid/gid for remote client */
1999 if (exp_connect_rmtclient(exp)) {
2000 struct lustre_idmap_table *idmap;
2002 idmap = exp->exp_mdt_data.med_idmap;
2004 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2005 oqctl->qc_cmd != Q_GETINFO))
2008 if (oqctl->qc_type == USRQUOTA)
2009 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2011 else if (oqctl->qc_type == GRPQUOTA)
2012 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2017 if (id == CFS_IDMAP_NOTFOUND) {
2018 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2023 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2025 RETURN(err_serious(-EFAULT));
2027 if (oqctl->qc_id != id)
2028 swap(oqctl->qc_id, id);
2030 switch (oqctl->qc_cmd) {
2036 /* forward quotactl request to QMT */
2037 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2042 /* slave quotactl */
2043 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2048 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2052 if (oqctl->qc_id != id)
2053 swap(oqctl->qc_id, id);
2059 /** clone llog ctxt from child (mdd)
2060 * This allows remote llog (replicator) access.
2061 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2062 * context was originally set up, or we can handle them directly.
2063 * I choose the latter, but that means I need any llog
2064 * contexts set up by child to be accessable by the mdt. So we clone the
2065 * context into our context list here.
2067 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2070 struct md_device *next = mdt->mdt_child;
2071 struct llog_ctxt *ctxt;
2074 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2077 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2078 if (rc || ctxt == NULL) {
2082 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2084 CERROR("Can't set mdt ctxt %d\n", rc);
2089 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2090 struct mdt_device *mdt, int idx)
2092 struct llog_ctxt *ctxt;
2094 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2097 /* Put once for the get we just did, and once for the clone */
2098 llog_ctxt_put(ctxt);
2099 llog_ctxt_put(ctxt);
2104 * sec context handlers
2106 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2110 rc = mdt_handle_idmap(tsi);
2112 struct ptlrpc_request *req = tgt_ses_req(tsi);
2115 opc = lustre_msg_get_opc(req->rq_reqmsg);
2116 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2117 sptlrpc_svc_ctx_invalidate(req);
2120 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2126 * quota request handlers
2128 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2130 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2131 struct lu_device *qmt = mdt->mdt_qmt_dev;
2136 RETURN(err_serious(-EOPNOTSUPP));
2138 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2142 struct mdt_object *mdt_object_new(const struct lu_env *env,
2143 struct mdt_device *d,
2144 const struct lu_fid *f)
2146 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2147 struct lu_object *o;
2148 struct mdt_object *m;
2151 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2152 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2153 if (unlikely(IS_ERR(o)))
2154 m = (struct mdt_object *)o;
2160 struct mdt_object *mdt_object_find(const struct lu_env *env,
2161 struct mdt_device *d,
2162 const struct lu_fid *f)
2164 struct lu_object *o;
2165 struct mdt_object *m;
2168 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2169 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2170 if (unlikely(IS_ERR(o)))
2171 m = (struct mdt_object *)o;
2179 * Asyncronous commit for mdt device.
2181 * Pass asynchonous commit call down the MDS stack.
2183 * \param env environment
2184 * \param mdt the mdt device
2186 static void mdt_device_commit_async(const struct lu_env *env,
2187 struct mdt_device *mdt)
2189 struct dt_device *dt = mdt->mdt_bottom;
2192 rc = dt->dd_ops->dt_commit_async(env, dt);
2193 if (unlikely(rc != 0))
2194 CWARN("async commit start failed with rc = %d", rc);
2198 * Mark the lock as "synchonous".
2200 * Mark the lock to deffer transaction commit to the unlock time.
2202 * \param lock the lock to mark as "synchonous"
2204 * \see mdt_is_lock_sync
2205 * \see mdt_save_lock
2207 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2209 lock->l_ast_data = (void*)1;
2213 * Check whehter the lock "synchonous" or not.
2215 * \param lock the lock to check
2216 * \retval 1 the lock is "synchonous"
2217 * \retval 0 the lock isn't "synchronous"
2219 * \see mdt_set_lock_sync
2220 * \see mdt_save_lock
2222 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2224 return lock->l_ast_data != NULL;
2228 * Blocking AST for mdt locks.
2230 * Starts transaction commit if in case of COS lock conflict or
2231 * deffers such a commit to the mdt_save_lock.
2233 * \param lock the lock which blocks a request or cancelling lock
2234 * \param desc unused
2235 * \param data unused
2236 * \param flag indicates whether this cancelling or blocking callback
2238 * \see ldlm_blocking_ast_nocheck
2240 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2241 void *data, int flag)
2243 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2244 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2248 if (flag == LDLM_CB_CANCELING)
2250 lock_res_and_lock(lock);
2251 if (lock->l_blocking_ast != mdt_blocking_ast) {
2252 unlock_res_and_lock(lock);
2255 if (mdt_cos_is_enabled(mdt) &&
2256 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2257 lock->l_blocking_lock != NULL &&
2258 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2259 mdt_set_lock_sync(lock);
2261 rc = ldlm_blocking_ast_nocheck(lock);
2263 /* There is no lock conflict if l_blocking_lock == NULL,
2264 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2265 * when the last reference to a local lock was released */
2266 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2269 rc = lu_env_init(&env, LCT_LOCAL);
2270 if (unlikely(rc != 0))
2271 CWARN("lu_env initialization failed with rc = %d,"
2272 "cannot start asynchronous commit\n", rc);
2274 mdt_device_commit_async(&env, mdt);
2280 /* Used for cross-MDT lock */
2281 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2282 void *data, int flag)
2284 struct lustre_handle lockh;
2288 case LDLM_CB_BLOCKING:
2289 ldlm_lock2handle(lock, &lockh);
2290 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2292 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2296 case LDLM_CB_CANCELING:
2297 LDLM_DEBUG(lock, "Revoke remote lock\n");
2305 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2306 struct mdt_object *o, const struct lu_fid *fid,
2307 struct lustre_handle *lh, ldlm_mode_t mode,
2310 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2311 ldlm_policy_data_t *policy = &mti->mti_policy;
2312 struct ldlm_res_id *res_id = &mti->mti_res_id;
2316 LASSERT(mdt_object_remote(o));
2318 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2320 fid_build_reg_res_name(fid, res_id);
2322 memset(einfo, 0, sizeof(*einfo));
2323 einfo->ei_type = LDLM_IBITS;
2324 einfo->ei_mode = mode;
2325 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2326 einfo->ei_cb_cp = ldlm_completion_ast;
2327 einfo->ei_enq_slave = 0;
2328 einfo->ei_res_id = res_id;
2330 memset(policy, 0, sizeof(*policy));
2331 policy->l_inodebits.bits = ibits;
2333 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2338 static int mdt_object_local_lock(struct mdt_thread_info *info,
2339 struct mdt_object *o,
2340 struct mdt_lock_handle *lh, __u64 ibits,
2341 bool nonblock, int locality)
2343 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2344 ldlm_policy_data_t *policy = &info->mti_policy;
2345 struct ldlm_res_id *res_id = &info->mti_res_id;
2350 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2351 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2352 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2353 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2355 /* Only enqueue LOOKUP lock for remote object */
2356 if (mdt_object_remote(o))
2357 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2359 if (lh->mlh_type == MDT_PDO_LOCK) {
2360 /* check for exists after object is locked */
2361 if (mdt_object_exists(o) == 0) {
2362 /* Non-existent object shouldn't have PDO lock */
2365 /* Non-dir object shouldn't have PDO lock */
2366 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2371 memset(policy, 0, sizeof(*policy));
2372 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2374 dlmflags = LDLM_FL_ATOMIC_CB;
2376 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2379 * Take PDO lock on whole directory and build correct @res_id for lock
2380 * on part of directory.
2382 if (lh->mlh_pdo_hash != 0) {
2383 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2384 mdt_lock_pdo_mode(info, o, lh);
2385 if (lh->mlh_pdo_mode != LCK_NL) {
2387 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2388 * is never going to be sent to client and we do not
2389 * want it slowed down due to possible cancels.
2391 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2392 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2393 policy, res_id, dlmflags,
2394 &info->mti_exp->exp_handle.h_cookie);
2400 * Finish res_id initializing by name hash marking part of
2401 * directory which is taking modification.
2403 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2406 policy->l_inodebits.bits = ibits;
2409 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2410 * going to be sent to client. If it is - mdt_intent_policy() path will
2411 * fix it up and turn FL_LOCAL flag off.
2413 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2414 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2415 &info->mti_exp->exp_handle.h_cookie);
2417 mdt_object_unlock(info, o, lh, 1);
2418 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2419 lh->mlh_pdo_hash != 0 &&
2420 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2421 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2427 int mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2428 struct mdt_lock_handle *lh, __u64 ibits,
2429 bool nonblock, int locality)
2434 if (!mdt_object_remote(o))
2435 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2438 if (locality == MDT_LOCAL_LOCK) {
2439 CERROR("%s: try to get local lock for remote object"
2440 DFID".\n", mdt_obd_name(info->mti_mdt),
2441 PFID(mdt_object_fid(o)));
2445 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2446 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2447 MDS_INODELOCK_XATTR);
2448 if (ibits & MDS_INODELOCK_UPDATE) {
2449 /* Sigh, PDO needs to enqueue 2 locks right now, but
2450 * enqueue RPC can only request 1 lock, to avoid extra
2451 * RPC, so it will instead enqueue EX lock for remote
2452 * object anyway XXX*/
2453 if (lh->mlh_type == MDT_PDO_LOCK &&
2454 lh->mlh_pdo_hash != 0) {
2455 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2456 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2457 PFID(mdt_object_fid(o)));
2458 lh->mlh_pdo_hash = 0;
2459 lh->mlh_rreg_mode = LCK_EX;
2460 lh->mlh_type = MDT_REG_LOCK;
2462 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2465 MDS_INODELOCK_UPDATE);
2470 /* Only enqueue LOOKUP lock for remote object */
2471 if (ibits & MDS_INODELOCK_LOOKUP) {
2472 rc = mdt_object_local_lock(info, o, lh,
2473 MDS_INODELOCK_LOOKUP,
2474 nonblock, locality);
2482 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2483 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2485 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2488 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2489 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2491 struct mdt_lock_handle tmp = *lh;
2494 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2502 * Save a lock within request object.
2504 * Keep the lock referenced until whether client ACK or transaction
2505 * commit happens or release the lock immediately depending on input
2506 * parameters. If COS is ON, a write lock is converted to COS lock
2509 * \param info thead info object
2510 * \param h lock handle
2511 * \param mode lock mode
2512 * \param decref force immediate lock releasing
2515 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2516 ldlm_mode_t mode, int decref)
2520 if (lustre_handle_is_used(h)) {
2521 if (decref || !info->mti_has_trans ||
2522 !(mode & (LCK_PW | LCK_EX))){
2523 mdt_fid_unlock(h, mode);
2525 struct mdt_device *mdt = info->mti_mdt;
2526 struct ldlm_lock *lock = ldlm_handle2lock(h);
2527 struct ptlrpc_request *req = mdt_info_req(info);
2530 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2532 /* there is no request if mdt_object_unlock() is called
2533 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2534 if (likely(req != NULL)) {
2535 CDEBUG(D_HA, "request = %p reply state = %p"
2536 " transno = "LPD64"\n", req,
2537 req->rq_reply_state, req->rq_transno);
2538 if (mdt_cos_is_enabled(mdt)) {
2540 ldlm_lock_downgrade(lock, LCK_COS);
2543 ptlrpc_save_lock(req, h, mode, no_ack);
2545 ldlm_lock_decref(h, mode);
2547 if (mdt_is_lock_sync(lock)) {
2548 CDEBUG(D_HA, "found sync-lock,"
2549 " async commit started\n");
2550 mdt_device_commit_async(info->mti_env,
2553 LDLM_LOCK_PUT(lock);
2562 * Unlock mdt object.
2564 * Immeditely release the regular lock and the PDO lock or save the
2565 * lock in reqeuest and keep them referenced until client ACK or
2566 * transaction commit.
2568 * \param info thread info object
2569 * \param o mdt object
2570 * \param lh mdt lock handle referencing regular and PDO locks
2571 * \param decref force immediate lock releasing
2573 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2574 struct mdt_lock_handle *lh, int decref)
2578 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2579 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2581 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2582 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2587 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2588 const struct lu_fid *f,
2589 struct mdt_lock_handle *lh,
2592 struct mdt_object *o;
2594 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2598 rc = mdt_object_lock(info, o, lh, ibits,
2601 mdt_object_put(info->mti_env, o);
2608 void mdt_object_unlock_put(struct mdt_thread_info * info,
2609 struct mdt_object * o,
2610 struct mdt_lock_handle *lh,
2613 mdt_object_unlock(info, o, lh, decref);
2614 mdt_object_put(info->mti_env, o);
2618 * Generic code handling requests that have struct mdt_body passed in:
2620 * - extract mdt_body from request and save it in @info, if present;
2622 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2625 * - if HABEO_CORPUS flag is set for this request type check whether object
2626 * actually exists on storage (lu_object_exists()).
2629 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2631 const struct mdt_body *body;
2632 struct mdt_object *obj;
2633 const struct lu_env *env;
2634 struct req_capsule *pill;
2638 env = info->mti_env;
2639 pill = info->mti_pill;
2641 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2645 if (!(body->valid & OBD_MD_FLID))
2648 if (!fid_is_sane(&body->fid1)) {
2649 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2654 * Do not get size or any capa fields before we check that request
2655 * contains capa actually. There are some requests which do not, for
2656 * instance MDS_IS_SUBDIR.
2658 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2659 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2660 mdt_set_capainfo(info, 0, &body->fid1,
2661 req_capsule_client_get(pill, &RMF_CAPA1));
2663 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2665 if ((flags & HABEO_CORPUS) &&
2666 !mdt_object_exists(obj)) {
2667 mdt_object_put(env, obj);
2668 /* for capability renew ENOENT will be handled in
2670 if (body->valid & OBD_MD_FLOSSCAPA)
2675 info->mti_object = obj;
2684 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2686 struct req_capsule *pill = info->mti_pill;
2690 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2691 rc = mdt_body_unpack(info, flags);
2695 if (rc == 0 && (flags & HABEO_REFERO)) {
2697 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2698 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2699 info->mti_body->eadatasize);
2700 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2701 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2704 rc = req_capsule_server_pack(pill);
2709 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2711 struct md_device *next = m->mdt_child;
2713 return next->md_ops->mdo_init_capa_ctxt(env, next,
2714 m->mdt_lut.lut_mds_capa,
2715 m->mdt_capa_timeout,
2720 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2722 lh->mlh_type = MDT_NUL_LOCK;
2723 lh->mlh_reg_lh.cookie = 0ull;
2724 lh->mlh_reg_mode = LCK_MINMODE;
2725 lh->mlh_pdo_lh.cookie = 0ull;
2726 lh->mlh_pdo_mode = LCK_MINMODE;
2727 lh->mlh_rreg_lh.cookie = 0ull;
2728 lh->mlh_rreg_mode = LCK_MINMODE;
2731 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2733 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2734 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2738 * Initialize fields of struct mdt_thread_info. Other fields are left in
2739 * uninitialized state, because it's too expensive to zero out whole
2740 * mdt_thread_info (> 1K) on each request arrival.
2742 void mdt_thread_info_init(struct ptlrpc_request *req,
2743 struct mdt_thread_info *info)
2747 info->mti_pill = &req->rq_pill;
2750 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2751 mdt_lock_handle_init(&info->mti_lh[i]);
2753 /* mdt device: it can be NULL while CONNECT */
2754 if (req->rq_export) {
2755 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2756 info->mti_exp = req->rq_export;
2758 info->mti_mdt = NULL;
2759 info->mti_env = req->rq_svc_thread->t_env;
2760 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2762 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2763 info->mti_big_buf = LU_BUF_NULL;
2764 info->mti_body = NULL;
2765 info->mti_object = NULL;
2766 info->mti_dlm_req = NULL;
2767 info->mti_has_trans = 0;
2768 info->mti_cross_ref = 0;
2769 info->mti_opdata = 0;
2770 info->mti_big_lmm_used = 0;
2772 info->mti_spec.no_create = 0;
2773 info->mti_spec.sp_rm_entry = 0;
2775 info->mti_spec.u.sp_ea.eadata = NULL;
2776 info->mti_spec.u.sp_ea.eadatalen = 0;
2779 void mdt_thread_info_fini(struct mdt_thread_info *info)
2783 if (info->mti_object != NULL) {
2784 mdt_object_put(info->mti_env, info->mti_object);
2785 info->mti_object = NULL;
2788 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2789 mdt_lock_handle_fini(&info->mti_lh[i]);
2790 info->mti_env = NULL;
2791 info->mti_pill = NULL;
2792 info->mti_exp = NULL;
2794 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2795 lu_buf_free(&info->mti_big_buf);
2798 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2800 struct mdt_thread_info *mti;
2801 struct lustre_capa *lc;
2803 mti = mdt_th_info(tsi->tsi_env);
2804 LASSERT(mti != NULL);
2806 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2807 if (tsi->tsi_corpus != NULL) {
2808 struct req_capsule *pill = tsi->tsi_pill;
2810 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2811 lu_object_get(tsi->tsi_corpus);
2814 * XXX: must be part of tgt_mdt_body_unpack but moved here
2815 * due to mdt_set_capainfo().
2817 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2818 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2819 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2820 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2823 mti->mti_body = tsi->tsi_mdt_body;
2824 mti->mti_dlm_req = tsi->tsi_dlm_req;
2829 int mdt_tgt_connect(struct tgt_session_info *tsi)
2831 struct ptlrpc_request *req = tgt_ses_req(tsi);
2836 rc = tgt_connect(tsi);
2840 rc = mdt_init_idmap(tsi);
2845 obd_disconnect(class_export_get(req->rq_export));
2864 static int mdt_intent_getattr(enum mdt_it_code opcode,
2865 struct mdt_thread_info *info,
2866 struct ldlm_lock **,
2869 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2870 struct mdt_thread_info *info,
2871 struct ldlm_lock **lockp,
2874 static int mdt_intent_layout(enum mdt_it_code opcode,
2875 struct mdt_thread_info *info,
2876 struct ldlm_lock **,
2878 static int mdt_intent_reint(enum mdt_it_code opcode,
2879 struct mdt_thread_info *info,
2880 struct ldlm_lock **,
2883 static struct mdt_it_flavor {
2884 const struct req_format *it_fmt;
2886 int (*it_act)(enum mdt_it_code ,
2887 struct mdt_thread_info *,
2888 struct ldlm_lock **,
2891 } mdt_it_flavor[] = {
2893 .it_fmt = &RQF_LDLM_INTENT,
2894 /*.it_flags = HABEO_REFERO,*/
2896 .it_act = mdt_intent_reint,
2897 .it_reint = REINT_OPEN
2900 .it_fmt = &RQF_LDLM_INTENT,
2902 * OCREAT is not a MUTABOR request as if the file
2904 * We do the extra check of OBD_CONNECT_RDONLY in
2905 * mdt_reint_open() when we really need to create
2909 .it_act = mdt_intent_reint,
2910 .it_reint = REINT_OPEN
2913 .it_fmt = &RQF_LDLM_INTENT,
2914 .it_flags = MUTABOR,
2915 .it_act = mdt_intent_reint,
2916 .it_reint = REINT_CREATE
2918 [MDT_IT_GETATTR] = {
2919 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2920 .it_flags = HABEO_REFERO,
2921 .it_act = mdt_intent_getattr
2923 [MDT_IT_READDIR] = {
2929 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2930 .it_flags = HABEO_REFERO,
2931 .it_act = mdt_intent_getattr
2934 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2935 .it_flags = MUTABOR,
2937 .it_reint = REINT_UNLINK
2941 .it_flags = MUTABOR,
2944 [MDT_IT_GETXATTR] = {
2945 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2946 .it_flags = HABEO_CORPUS,
2947 .it_act = mdt_intent_getxattr
2950 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2952 .it_act = mdt_intent_layout
2956 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2957 struct ldlm_lock **lockp,
2958 struct ldlm_lock *new_lock,
2959 struct mdt_lock_handle *lh,
2962 struct ptlrpc_request *req = mdt_info_req(info);
2963 struct ldlm_lock *lock = *lockp;
2966 * Get new lock only for cases when possible resent did not find any
2969 if (new_lock == NULL)
2970 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2972 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2973 lh->mlh_reg_lh.cookie = 0;
2977 LASSERTF(new_lock != NULL,
2978 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2981 * If we've already given this lock to a client once, then we should
2982 * have no readers or writers. Otherwise, we should have one reader
2983 * _or_ writer ref (which will be zeroed below) before returning the
2986 if (new_lock->l_export == req->rq_export) {
2987 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2989 LASSERT(new_lock->l_export == NULL);
2990 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2995 if (new_lock->l_export == req->rq_export) {
2997 * Already gave this to the client, which means that we
2998 * reconstructed a reply.
3000 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3002 lh->mlh_reg_lh.cookie = 0;
3003 RETURN(ELDLM_LOCK_REPLACED);
3007 * Fixup the lock to be given to the client.
3009 lock_res_and_lock(new_lock);
3010 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3011 * possible blocking AST. */
3012 while (new_lock->l_readers > 0) {
3013 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3014 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3015 new_lock->l_readers--;
3017 while (new_lock->l_writers > 0) {
3018 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3019 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3020 new_lock->l_writers--;
3023 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3024 new_lock->l_blocking_ast = lock->l_blocking_ast;
3025 new_lock->l_completion_ast = lock->l_completion_ast;
3026 new_lock->l_remote_handle = lock->l_remote_handle;
3027 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3029 unlock_res_and_lock(new_lock);
3031 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3032 &new_lock->l_remote_handle,
3033 &new_lock->l_exp_hash);
3035 LDLM_LOCK_RELEASE(new_lock);
3036 lh->mlh_reg_lh.cookie = 0;
3038 RETURN(ELDLM_LOCK_REPLACED);
3041 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3042 struct ldlm_lock *new_lock,
3043 struct ldlm_lock **old_lock,
3044 struct mdt_lock_handle *lh,
3045 enum mdt_it_code opcode)
3047 struct ptlrpc_request *req = mdt_info_req(info);
3048 struct obd_export *exp = req->rq_export;
3049 struct lustre_handle remote_hdl;
3050 struct ldlm_request *dlmreq;
3051 struct ldlm_lock *lock;
3053 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3056 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3057 remote_hdl = dlmreq->lock_handle[0];
3058 /* If the client does not require open lock, it does not need to
3059 * search lock in exp_lock_hash, since the server thread will
3060 * make sure the lock will be released, and the resend request
3061 * can always re-enqueue the lock */
3062 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3063 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3064 /* In the function below, .hs_keycmp resolves to
3065 * ldlm_export_lock_keycmp() */
3066 /* coverity[overrun-buffer-val] */
3067 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3069 lock_res_and_lock(lock);
3070 if (lock != new_lock) {
3071 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3072 lh->mlh_reg_mode = lock->l_granted_mode;
3074 LDLM_DEBUG(lock, "Restoring lock cookie");
3075 DEBUG_REQ(D_DLMTRACE, req,
3076 "restoring lock cookie "LPX64,
3077 lh->mlh_reg_lh.cookie);
3079 *old_lock = LDLM_LOCK_GET(lock);
3080 cfs_hash_put(exp->exp_lock_hash,
3082 unlock_res_and_lock(lock);
3085 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3086 unlock_res_and_lock(lock);
3090 * If the xid matches, then we know this is a resent request, and allow
3091 * it. (It's probably an OPEN, for which we don't send a lock.
3093 if (req_xid_is_last(req))
3097 * This remote handle isn't enqueued, so we never received or processed
3098 * this request. Clear MSG_RESENT, because it can be handled like any
3099 * normal request now.
3101 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3103 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3107 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3108 struct mdt_thread_info *info,
3109 struct ldlm_lock **lockp,
3112 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3113 struct ldlm_reply *ldlm_rep = NULL;
3117 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3118 * (for the resend case) or a new lock. Below we will use it to
3119 * replace the original lock.
3121 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3122 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3123 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3124 rc = mdt_object_lock(info, info->mti_object, lhc,
3125 MDS_INODELOCK_XATTR,
3131 grc = mdt_getxattr(info);
3133 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3135 if (mdt_info_req(info)->rq_repmsg != NULL)
3136 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3137 if (ldlm_rep == NULL)
3138 RETURN(err_serious(-EFAULT));
3140 ldlm_rep->lock_policy_res2 = grc;
3145 static int mdt_intent_getattr(enum mdt_it_code opcode,
3146 struct mdt_thread_info *info,
3147 struct ldlm_lock **lockp,
3150 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3151 struct ldlm_lock *new_lock = NULL;
3153 struct ldlm_reply *ldlm_rep;
3154 struct ptlrpc_request *req;
3155 struct mdt_body *reqbody;
3156 struct mdt_body *repbody;
3160 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3163 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3166 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3167 repbody->eadatasize = 0;