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 static 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 static 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 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
486 const struct lu_env *env = info->mti_env;
489 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
498 /* Is it a directory? Let's check for the LMV as well */
499 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
500 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
502 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
510 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
513 const struct lu_env *env = info->mti_env;
517 LASSERT(info->mti_big_lmm_used == 0);
518 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
522 /* big_lmm may need to be grown */
523 if (info->mti_big_lmmsize < rc) {
524 int size = size_roundup_power2(rc);
526 if (info->mti_big_lmmsize > 0) {
527 /* free old buffer */
528 LASSERT(info->mti_big_lmm);
529 OBD_FREE_LARGE(info->mti_big_lmm,
530 info->mti_big_lmmsize);
531 info->mti_big_lmm = NULL;
532 info->mti_big_lmmsize = 0;
535 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
536 if (info->mti_big_lmm == NULL)
538 info->mti_big_lmmsize = size;
540 LASSERT(info->mti_big_lmmsize >= rc);
542 info->mti_buf.lb_buf = info->mti_big_lmm;
543 info->mti_buf.lb_len = info->mti_big_lmmsize;
544 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
549 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
550 struct md_attr *ma, const char *name)
552 struct md_object *next = mdt_object_child(o);
553 struct lu_buf *buf = &info->mti_buf;
556 if (strcmp(name, XATTR_NAME_LOV) == 0) {
557 buf->lb_buf = ma->ma_lmm;
558 buf->lb_len = ma->ma_lmm_size;
559 LASSERT(!(ma->ma_valid & MA_LOV));
560 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
561 buf->lb_buf = ma->ma_lmv;
562 buf->lb_len = ma->ma_lmv_size;
563 LASSERT(!(ma->ma_valid & MA_LMV));
564 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
565 buf->lb_buf = ma->ma_lmv;
566 buf->lb_len = ma->ma_lmv_size;
567 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
572 rc = mo_xattr_get(info->mti_env, next, buf, name);
574 if (strcmp(name, XATTR_NAME_LOV) == 0) {
575 ma->ma_lmm_size = rc;
576 ma->ma_valid |= MA_LOV;
577 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
578 ma->ma_lmv_size = rc;
579 ma->ma_valid |= MA_LMV;
580 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
581 ma->ma_lmv_size = rc;
582 ma->ma_valid |= MA_LMV_DEF;
586 } else if (rc == -ENODATA) {
589 } else if (rc == -ERANGE) {
590 /* Default LMV has fixed size, so it must be able to fit
591 * in the original buffer */
592 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
594 rc = mdt_big_xattr_get(info, o, name);
596 info->mti_big_lmm_used = 1;
597 if (!strcmp(name, XATTR_NAME_LOV)) {
598 ma->ma_valid |= MA_LOV;
599 ma->ma_lmm = info->mti_big_lmm;
600 ma->ma_lmm_size = rc;
601 } else if (!strcmp(name, XATTR_NAME_LMV)) {
602 ma->ma_valid |= MA_LMV;
603 ma->ma_lmv = info->mti_big_lmm;
604 ma->ma_lmv_size = rc;
609 /* update mdt_max_mdsize so all clients
610 * will be aware about that */
611 if (info->mti_mdt->mdt_max_mdsize < rc)
612 info->mti_mdt->mdt_max_mdsize = rc;
620 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
621 struct mdt_object *o, struct lu_fid *pfid)
623 struct lu_buf *buf = &info->mti_buf;
624 struct link_ea_header *leh;
625 struct link_ea_entry *lee;
629 buf->lb_buf = info->mti_big_lmm;
630 buf->lb_len = info->mti_big_lmmsize;
631 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
632 buf, XATTR_NAME_LINK);
633 /* ignore errors, MA_PFID won't be set and it is
634 * up to the caller to treat this as an error */
635 if (rc == -ERANGE || buf->lb_len == 0) {
636 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
637 buf->lb_buf = info->mti_big_lmm;
638 buf->lb_len = info->mti_big_lmmsize;
643 if (rc < sizeof(*leh)) {
644 CERROR("short LinkEA on "DFID": rc = %d\n",
645 PFID(mdt_object_fid(o)), rc);
649 leh = (struct link_ea_header *) buf->lb_buf;
650 lee = (struct link_ea_entry *)(leh + 1);
651 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
652 leh->leh_magic = LINK_EA_MAGIC;
653 leh->leh_reccount = __swab32(leh->leh_reccount);
654 leh->leh_len = __swab64(leh->leh_len);
656 if (leh->leh_magic != LINK_EA_MAGIC)
658 if (leh->leh_reccount == 0)
661 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
662 fid_be_to_cpu(pfid, pfid);
667 int mdt_attr_get_complex(struct mdt_thread_info *info,
668 struct mdt_object *o, struct md_attr *ma)
670 const struct lu_env *env = info->mti_env;
671 struct md_object *next = mdt_object_child(o);
672 struct lu_buf *buf = &info->mti_buf;
673 u32 mode = lu_object_attr(&next->mo_lu);
674 int need = ma->ma_need;
680 if (need & MA_INODE) {
681 ma->ma_need = MA_INODE;
682 rc = mo_attr_get(env, next, ma);
685 ma->ma_valid |= MA_INODE;
688 if (need & MA_PFID) {
689 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
691 ma->ma_valid |= MA_PFID;
692 /* ignore this error, parent fid is not mandatory */
696 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
697 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
702 if (need & MA_LMV && S_ISDIR(mode)) {
703 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
708 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
709 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
714 if (need & MA_SOM && S_ISREG(mode)) {
715 buf->lb_buf = info->mti_xattr_buf;
716 buf->lb_len = sizeof(info->mti_xattr_buf);
717 CLASSERT(sizeof(struct som_attrs) <=
718 sizeof(info->mti_xattr_buf));
719 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
720 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
722 ma->ma_valid |= MA_SOM;
723 else if (rc2 < 0 && rc2 != -ENODATA)
727 if (need & MA_HSM && S_ISREG(mode)) {
728 buf->lb_buf = info->mti_xattr_buf;
729 buf->lb_len = sizeof(info->mti_xattr_buf);
730 CLASSERT(sizeof(struct hsm_attrs) <=
731 sizeof(info->mti_xattr_buf));
732 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
733 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
735 ma->ma_valid |= MA_HSM;
736 else if (rc2 < 0 && rc2 != -ENODATA)
740 #ifdef CONFIG_FS_POSIX_ACL
741 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
742 buf->lb_buf = ma->ma_acl;
743 buf->lb_len = ma->ma_acl_size;
744 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
746 ma->ma_acl_size = rc2;
747 ma->ma_valid |= MA_ACL_DEF;
748 } else if (rc2 == -ENODATA) {
757 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
758 rc, ma->ma_valid, ma->ma_lmm);
762 static int mdt_getattr_internal(struct mdt_thread_info *info,
763 struct mdt_object *o, int ma_need)
765 struct md_object *next = mdt_object_child(o);
766 const struct mdt_body *reqbody = info->mti_body;
767 struct ptlrpc_request *req = mdt_info_req(info);
768 struct md_attr *ma = &info->mti_attr;
769 struct lu_attr *la = &ma->ma_attr;
770 struct req_capsule *pill = info->mti_pill;
771 const struct lu_env *env = info->mti_env;
772 struct mdt_body *repbody;
773 struct lu_buf *buffer = &info->mti_buf;
778 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
779 RETURN(err_serious(-ENOMEM));
781 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
785 if (mdt_object_remote(o)) {
786 /* This object is located on remote node.*/
787 /* Return -EIO for old client */
788 if (!mdt_is_dne_client(req->rq_export))
789 GOTO(out, rc = -EIO);
791 repbody->fid1 = *mdt_object_fid(o);
792 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
796 if (reqbody->eadatasize > 0) {
797 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
798 if (buffer->lb_buf == NULL)
799 GOTO(out, rc = -EPROTO);
800 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
803 buffer->lb_buf = NULL;
805 ma_need &= ~(MA_LOV | MA_LMV);
806 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
807 mdt_obd_name(info->mti_mdt),
808 req->rq_export->exp_client_uuid.uuid);
811 /* If it is dir object and client require MEA, then we got MEA */
812 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
813 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
814 /* Assumption: MDT_MD size is enough for lmv size. */
815 ma->ma_lmv = buffer->lb_buf;
816 ma->ma_lmv_size = buffer->lb_len;
817 ma->ma_need = MA_INODE;
818 if (ma->ma_lmv_size > 0) {
819 if (reqbody->valid & OBD_MD_MEA)
820 ma->ma_need |= MA_LMV;
821 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
822 ma->ma_need |= MA_LMV_DEF;
825 ma->ma_lmm = buffer->lb_buf;
826 ma->ma_lmm_size = buffer->lb_len;
827 ma->ma_need = MA_INODE | MA_HSM;
828 if (ma->ma_lmm_size > 0)
829 ma->ma_need |= MA_LOV;
832 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
833 reqbody->valid & OBD_MD_FLDIREA &&
834 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
835 /* get default stripe info for this dir. */
836 ma->ma_need |= MA_LOV_DEF;
838 ma->ma_need |= ma_need;
839 if (ma->ma_need & MA_SOM)
840 ma->ma_som = &info->mti_u.som.data;
842 rc = mdt_attr_get_complex(info, o, ma);
844 CERROR("%s: getattr error for "DFID": rc = %d\n",
845 mdt_obd_name(info->mti_mdt),
846 PFID(mdt_object_fid(o)), rc);
850 /* if file is released, check if a restore is running */
851 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
852 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
853 repbody->t_state = MS_RESTORE;
854 repbody->valid |= OBD_MD_TSTATE;
857 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
859 /* the Lustre protocol supposes to return default striping
860 * on the user-visible root if explicitly requested */
861 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
862 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
863 struct lu_fid rootfid;
864 struct mdt_object *root;
865 struct mdt_device *mdt = info->mti_mdt;
867 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
870 root = mdt_object_find(env, mdt, &rootfid);
872 RETURN(PTR_ERR(root));
873 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
874 mdt_object_put(info->mti_env, root);
876 CERROR("%s: getattr error for "DFID": rc = %d\n",
877 mdt_obd_name(info->mti_mdt),
878 PFID(mdt_object_fid(o)), rc);
883 if (likely(ma->ma_valid & MA_INODE))
884 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
888 if (mdt_body_has_lov(la, reqbody)) {
889 if (ma->ma_valid & MA_LOV) {
890 LASSERT(ma->ma_lmm_size);
891 repbody->eadatasize = ma->ma_lmm_size;
892 if (S_ISDIR(la->la_mode))
893 repbody->valid |= OBD_MD_FLDIREA;
895 repbody->valid |= OBD_MD_FLEASIZE;
896 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
898 if (ma->ma_valid & MA_LMV) {
899 LASSERT(S_ISDIR(la->la_mode));
900 mdt_dump_lmv(D_INFO, ma->ma_lmv);
901 repbody->eadatasize = ma->ma_lmv_size;
902 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
904 if (ma->ma_valid & MA_LMV_DEF) {
905 LASSERT(S_ISDIR(la->la_mode));
906 repbody->eadatasize = ma->ma_lmv_size;
907 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
909 } else if (S_ISLNK(la->la_mode) &&
910 reqbody->valid & OBD_MD_LINKNAME) {
911 buffer->lb_buf = ma->ma_lmm;
912 /* eadatasize from client includes NULL-terminator, so
913 * there is no need to read it */
914 buffer->lb_len = reqbody->eadatasize - 1;
915 rc = mo_readlink(env, next, buffer);
916 if (unlikely(rc <= 0)) {
917 CERROR("%s: readlink failed for "DFID": rc = %d\n",
918 mdt_obd_name(info->mti_mdt),
919 PFID(mdt_object_fid(o)), rc);
922 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
924 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
926 repbody->valid |= OBD_MD_LINKNAME;
927 /* we need to report back size with NULL-terminator
928 * because client expects that */
929 repbody->eadatasize = rc + 1;
930 if (repbody->eadatasize != reqbody->eadatasize)
931 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
932 "on "DFID ", expected %d\n",
933 mdt_obd_name(info->mti_mdt),
934 rc, PFID(mdt_object_fid(o)),
935 reqbody->eadatasize - 1);
937 ((char *)ma->ma_lmm)[rc] = 0;
939 /* If the total CDEBUG() size is larger than a page, it
940 * will print a warning to the console, avoid this by
941 * printing just the last part of the symlink. */
942 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
943 print_limit < rc ? "..." : "", print_limit,
944 (char *)ma->ma_lmm + rc - print_limit, rc);
949 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
950 repbody->max_cookiesize = 0;
951 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
952 repbody->valid |= OBD_MD_FLMODEASIZE;
953 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
954 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
955 repbody->max_cookiesize);
958 if (exp_connect_rmtclient(info->mti_exp) &&
959 reqbody->valid & OBD_MD_FLRMTPERM) {
960 void *buf = req_capsule_server_get(pill, &RMF_ACL);
962 /* mdt_getattr_lock only */
963 rc = mdt_pack_remote_perm(info, o, buf);
965 repbody->valid &= ~OBD_MD_FLRMTPERM;
966 repbody->aclsize = 0;
969 repbody->valid |= OBD_MD_FLRMTPERM;
970 repbody->aclsize = sizeof(struct mdt_remote_perm);
973 #ifdef CONFIG_FS_POSIX_ACL
974 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
975 (reqbody->valid & OBD_MD_FLACL)) {
976 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
977 buffer->lb_len = req_capsule_get_size(pill,
978 &RMF_ACL, RCL_SERVER);
979 if (buffer->lb_len > 0) {
980 rc = mo_xattr_get(env, next, buffer,
981 XATTR_NAME_ACL_ACCESS);
983 if (rc == -ENODATA) {
984 repbody->aclsize = 0;
985 repbody->valid |= OBD_MD_FLACL;
987 } else if (rc == -EOPNOTSUPP) {
990 CERROR("%s: unable to read "DFID
992 mdt_obd_name(info->mti_mdt),
993 PFID(mdt_object_fid(o)), rc);
996 repbody->aclsize = rc;
997 repbody->valid |= OBD_MD_FLACL;
1004 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
1005 info->mti_mdt->mdt_lut.lut_mds_capa &&
1006 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1007 struct lustre_capa *capa;
1009 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1011 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1012 rc = mo_capa_get(env, next, capa, 0);
1015 repbody->valid |= OBD_MD_FLMDSCAPA;
1020 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1025 static int mdt_renew_capa(struct mdt_thread_info *info)
1027 struct mdt_object *obj = info->mti_object;
1028 struct mdt_body *body;
1029 struct lustre_capa *capa, *c;
1033 /* if object doesn't exist, or server has disabled capability,
1034 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1037 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1038 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1041 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1042 LASSERT(body != NULL);
1044 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1047 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1051 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1053 body->valid |= OBD_MD_FLOSSCAPA;
1057 static int mdt_getattr(struct tgt_session_info *tsi)
1059 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1060 struct mdt_object *obj = info->mti_object;
1061 struct req_capsule *pill = info->mti_pill;
1062 struct mdt_body *reqbody;
1063 struct mdt_body *repbody;
1067 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1070 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1071 rc = req_capsule_server_pack(pill);
1073 RETURN(err_serious(rc));
1074 rc = mdt_renew_capa(info);
1075 GOTO(out_shrink, rc);
1078 LASSERT(obj != NULL);
1079 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1081 /* Unlike intent case where we need to pre-fill out buffers early on
1082 * in intent policy for ldlm reasons, here we can have a much better
1083 * guess at EA size by just reading it from disk.
1084 * Exceptions are readdir and (missing) directory striping */
1086 if (reqbody->valid & OBD_MD_LINKNAME) {
1087 /* No easy way to know how long is the symlink, but it cannot
1088 * be more than PATH_MAX, so we allocate +1 */
1091 /* A special case for fs ROOT: getattr there might fetch
1092 * default EA for entire fs, not just for this dir!
1094 } else if (lu_fid_eq(mdt_object_fid(obj),
1095 &info->mti_mdt->mdt_md_root_fid) &&
1096 (reqbody->valid & OBD_MD_FLDIREA) &&
1097 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1099 /* Should the default strping be bigger, mdt_fix_reply
1100 * will reallocate */
1101 rc = DEF_REP_MD_SIZE;
1103 /* Read the actual EA size from disk */
1104 rc = mdt_attr_get_eabuf_size(info, obj);
1108 GOTO(out_shrink, rc);
1110 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1112 rc = req_capsule_server_pack(pill);
1113 if (unlikely(rc != 0))
1114 GOTO(out, rc = err_serious(rc));
1116 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1117 LASSERT(repbody != NULL);
1118 repbody->eadatasize = 0;
1119 repbody->aclsize = 0;
1121 if (reqbody->valid & OBD_MD_FLRMTPERM)
1122 rc = mdt_init_ucred(info, reqbody);
1124 rc = mdt_check_ucred(info);
1126 GOTO(out_shrink, rc);
1128 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1131 * Don't check capability at all, because rename might getattr for
1132 * remote obj, and at that time no capability is available.
1134 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1135 rc = mdt_getattr_internal(info, obj, 0);
1136 if (reqbody->valid & OBD_MD_FLRMTPERM)
1137 mdt_exit_ucred(info);
1140 mdt_client_compatibility(info);
1141 rc2 = mdt_fix_reply(info);
1145 mdt_thread_info_fini(info);
1149 static int mdt_is_subdir(struct tgt_session_info *tsi)
1151 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1152 struct mdt_object *o = info->mti_object;
1153 struct req_capsule *pill = info->mti_pill;
1154 const struct mdt_body *body = info->mti_body;
1155 struct mdt_body *repbody;
1161 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1164 * We save last checked parent fid to @repbody->fid1 for remote
1167 LASSERT(fid_is_sane(&body->fid2));
1168 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1169 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1170 &body->fid2, &repbody->fid1);
1171 if (rc == 0 || rc == -EREMOTE)
1172 repbody->valid |= OBD_MD_FLID;
1174 mdt_thread_info_fini(info);
1178 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1180 struct mdt_thread_info *info;
1181 struct ptlrpc_request *req = tgt_ses_req(tsi);
1182 struct obd_export *exp = req->rq_export;
1183 struct mdt_object *o1, *o2, *o;
1184 struct mdt_lock_handle *lh1, *lh2;
1185 struct mdc_swap_layouts *msl;
1189 /* client does not support layout lock, so layout swaping
1191 * FIXME: there is a problem for old clients which don't support
1192 * layout lock yet. If those clients have already opened the file
1193 * they won't be notified at all so that old layout may still be
1194 * used to do IO. This can be fixed after file release is landed by
1195 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1196 if (!exp_connect_layout(exp))
1197 RETURN(-EOPNOTSUPP);
1199 info = tsi2mdt_info(tsi);
1201 if (info->mti_dlm_req != NULL)
1202 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1204 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1205 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1206 req_capsule_client_get(info->mti_pill,
1209 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1210 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1211 req_capsule_client_get(info->mti_pill,
1214 o1 = info->mti_object;
1215 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1216 &info->mti_body->fid2);
1218 GOTO(out, rc = PTR_ERR(o));
1220 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1221 GOTO(put, rc = -ENOENT);
1223 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1224 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1230 /* permission check. Make sure the calling process having permission
1231 * to write both files. */
1232 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1237 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1242 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1244 GOTO(put, rc = -EPROTO);
1246 lh1 = &info->mti_lh[MDT_LH_NEW];
1247 mdt_lock_reg_init(lh1, LCK_EX);
1248 lh2 = &info->mti_lh[MDT_LH_OLD];
1249 mdt_lock_reg_init(lh2, LCK_EX);
1251 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1252 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1256 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1257 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1261 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1262 mdt_object_child(o2), msl->msl_flags);
1265 mdt_object_unlock(info, o2, lh2, rc);
1267 mdt_object_unlock(info, o1, lh1, rc);
1269 mdt_object_put(info->mti_env, o);
1271 mdt_thread_info_fini(info);
1275 static int mdt_raw_lookup(struct mdt_thread_info *info,
1276 struct mdt_object *parent,
1277 const struct lu_name *lname,
1278 struct ldlm_reply *ldlm_rep)
1280 struct md_object *next = mdt_object_child(info->mti_object);
1281 const struct mdt_body *reqbody = info->mti_body;
1282 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1283 struct mdt_body *repbody;
1287 if (reqbody->valid != OBD_MD_FLID)
1290 LASSERT(!info->mti_cross_ref);
1292 /* Only got the fid of this obj by name */
1293 fid_zero(child_fid);
1294 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1297 /* XXX is raw_lookup possible as intent operation? */
1300 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1303 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1305 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1308 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1309 repbody->fid1 = *child_fid;
1310 repbody->valid = OBD_MD_FLID;
1316 * UPDATE lock should be taken against parent, and be release before exit;
1317 * child_bits lock should be taken against child, and be returned back:
1318 * (1)normal request should release the child lock;
1319 * (2)intent request will grant the lock to client.
1321 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1322 struct mdt_lock_handle *lhc,
1324 struct ldlm_reply *ldlm_rep)
1326 struct ptlrpc_request *req = mdt_info_req(info);
1327 struct mdt_body *reqbody = NULL;
1328 struct mdt_object *parent = info->mti_object;
1329 struct mdt_object *child;
1330 struct md_object *next = mdt_object_child(parent);
1331 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1332 struct lu_name *lname = NULL;
1333 struct mdt_lock_handle *lhp = NULL;
1334 struct ldlm_lock *lock;
1341 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1342 LASSERT(ergo(is_resent,
1343 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1345 LASSERT(parent != NULL);
1347 if (info->mti_cross_ref) {
1348 /* Only getattr on the child. Parent is on another node. */
1349 mdt_set_disposition(info, ldlm_rep,
1350 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1352 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1354 PFID(mdt_object_fid(child)), ldlm_rep);
1357 /* Do not take lock for resent case. */
1358 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1359 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1360 lhc->mlh_reg_lh.cookie);
1361 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1362 &lock->l_resource->lr_name));
1363 LDLM_LOCK_PUT(lock);
1365 mdt_lock_handle_init(lhc);
1366 mdt_lock_reg_init(lhc, LCK_PR);
1369 * Object's name is on another MDS, no lookup or layout
1370 * lock is needed here but update lock is.
1372 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1373 MDS_INODELOCK_LAYOUT);
1374 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1376 rc = mdt_object_lock(info, child, lhc, child_bits,
1382 /* Finally, we can get attr for child. */
1383 if (!mdt_object_exists(child)) {
1384 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1386 "remote object doesn't exist.\n");
1387 mdt_object_unlock(info, child, lhc, 1);
1391 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1392 rc = mdt_getattr_internal(info, child, 0);
1393 if (unlikely(rc != 0))
1394 mdt_object_unlock(info, child, lhc, 1);
1399 lname = &info->mti_name;
1400 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1402 if (lu_name_is_valid(lname)) {
1403 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1404 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1405 PNAME(lname), ldlm_rep);
1407 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1408 if (unlikely(reqbody == NULL))
1409 RETURN(err_serious(-EPROTO));
1411 *child_fid = reqbody->fid2;
1413 if (unlikely(!fid_is_sane(child_fid)))
1414 RETURN(err_serious(-EINVAL));
1416 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1418 PFID(mdt_object_fid(parent)),
1419 PFID(&reqbody->fid2), ldlm_rep);
1422 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1424 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1425 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1427 "Parent doesn't exist!\n");
1431 if (mdt_object_remote(parent)) {
1432 CERROR("%s: parent "DFID" is on remote target\n",
1433 mdt_obd_name(info->mti_mdt),
1434 PFID(mdt_object_fid(parent)));
1438 if (lu_name_is_valid(lname)) {
1439 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1446 /* step 1: lock parent only if parent is a directory */
1447 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1448 lhp = &info->mti_lh[MDT_LH_PARENT];
1449 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1450 rc = mdt_object_lock(info, parent, lhp,
1451 MDS_INODELOCK_UPDATE,
1453 if (unlikely(rc != 0))
1457 /* step 2: lookup child's fid by name */
1458 fid_zero(child_fid);
1459 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1462 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1465 GOTO(out_parent, rc);
1468 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1471 *step 3: find the child object by fid & lock it.
1472 * regardless if it is local or remote.
1474 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1475 * set parent dir fid the same as child fid in getattr by fid case
1476 * we should not lu_object_find() the object again, could lead
1477 * to hung if there is a concurrent unlink destroyed the object.
1479 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1480 mdt_object_get(info->mti_env, parent);
1483 child = mdt_object_find(info->mti_env, info->mti_mdt,
1487 if (unlikely(IS_ERR(child)))
1488 GOTO(out_parent, rc = PTR_ERR(child));
1490 /* Do not take lock for resent case. */
1491 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1492 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1493 lhc->mlh_reg_lh.cookie);
1495 if (!fid_res_name_eq(mdt_object_fid(child),
1496 &lock->l_resource->lr_name)) {
1497 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1498 &lock->l_resource->lr_name),
1499 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1500 PLDLMRES(lock->l_resource),
1501 PFID(mdt_object_fid(parent)));
1502 CWARN("Although resent, but still not get child lock"
1503 "parent:"DFID" child:"DFID"\n",
1504 PFID(mdt_object_fid(parent)),
1505 PFID(mdt_object_fid(child)));
1506 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1507 LDLM_LOCK_PUT(lock);
1510 LDLM_LOCK_PUT(lock);
1513 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1514 mdt_lock_handle_init(lhc);
1515 mdt_lock_reg_init(lhc, LCK_PR);
1518 if (!mdt_object_exists(child)) {
1519 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1521 "Object doesn't exist!\n");
1522 GOTO(out_child, rc = -ENOENT);
1525 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1526 mdt_object_exists(child) && !mdt_object_remote(child)) {
1527 struct md_attr *ma = &info->mti_attr;
1530 ma->ma_need = MA_INODE;
1531 rc = mdt_attr_get_complex(info, child, ma);
1532 if (unlikely(rc != 0))
1533 GOTO(out_child, rc);
1535 /* If the file has not been changed for some time, we
1536 * return not only a LOOKUP lock, but also an UPDATE
1537 * lock and this might save us RPC on later STAT. For
1538 * directories, it also let negative dentry cache start
1539 * working for this dir. */
1540 if (ma->ma_valid & MA_INODE &&
1541 ma->ma_attr.la_valid & LA_CTIME &&
1542 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1543 ma->ma_attr.la_ctime < cfs_time_current_sec())
1544 child_bits |= MDS_INODELOCK_UPDATE;
1547 /* layout lock must be granted in a best-effort way
1548 * for IT operations */
1549 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1550 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1551 exp_connect_layout(info->mti_exp) &&
1552 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1553 !mdt_object_remote(child) && ldlm_rep != NULL) {
1554 /* try to grant layout lock for regular file. */
1560 child_bits |= MDS_INODELOCK_LAYOUT;
1561 /* try layout lock, it may fail to be granted due to
1562 * contention at LOOKUP or UPDATE */
1563 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1565 child_bits &= ~MDS_INODELOCK_LAYOUT;
1566 LASSERT(child_bits != 0);
1567 rc = mdt_object_lock(info, child, lhc,
1568 child_bits, MDT_CROSS_LOCK);
1573 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1574 * client will enqueue the lock to the remote MDT */
1575 if (mdt_object_remote(child))
1576 child_bits &= ~MDS_INODELOCK_UPDATE;
1577 rc = mdt_object_lock(info, child, lhc, child_bits,
1580 if (unlikely(rc != 0))
1581 GOTO(out_child, rc);
1584 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1585 /* Get MA_SOM attributes if update lock is given. */
1587 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1588 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1591 /* finally, we can get attr for child. */
1592 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1593 rc = mdt_getattr_internal(info, child, ma_need);
1594 if (unlikely(rc != 0)) {
1595 mdt_object_unlock(info, child, lhc, 1);
1597 /* Debugging code. */
1598 LDLM_DEBUG(lock, "Returning lock to client");
1599 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1600 &lock->l_resource->lr_name),
1601 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1602 PLDLMRES(lock->l_resource),
1603 PFID(mdt_object_fid(child)));
1604 if (mdt_object_exists(child) && !mdt_object_remote(child))
1605 mdt_pack_size2body(info, child);
1608 LDLM_LOCK_PUT(lock);
1612 mdt_object_put(info->mti_env, child);
1615 mdt_object_unlock(info, parent, lhp, 1);
1619 /* normal handler: should release the child lock */
1620 static int mdt_getattr_name(struct tgt_session_info *tsi)
1622 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1623 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1624 struct mdt_body *reqbody;
1625 struct mdt_body *repbody;
1629 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1630 LASSERT(reqbody != NULL);
1631 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1632 LASSERT(repbody != NULL);
1634 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1635 repbody->eadatasize = 0;
1636 repbody->aclsize = 0;
1638 rc = mdt_init_ucred(info, reqbody);
1640 GOTO(out_shrink, rc);
1642 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1643 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1644 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1645 lhc->mlh_reg_lh.cookie = 0;
1647 mdt_exit_ucred(info);
1650 mdt_client_compatibility(info);
1651 rc2 = mdt_fix_reply(info);
1654 mdt_thread_info_fini(info);
1658 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1659 void *karg, void *uarg);
1661 static int mdt_set_info(struct tgt_session_info *tsi)
1663 struct ptlrpc_request *req = tgt_ses_req(tsi);
1666 int keylen, vallen, rc = 0;
1670 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1672 DEBUG_REQ(D_HA, req, "no set_info key");
1673 RETURN(err_serious(-EFAULT));
1676 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1679 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1681 DEBUG_REQ(D_HA, req, "no set_info val");
1682 RETURN(err_serious(-EFAULT));
1685 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1688 /* Swab any part of val you need to here */
1689 if (KEY_IS(KEY_READ_ONLY)) {
1690 spin_lock(&req->rq_export->exp_lock);
1692 *exp_connect_flags_ptr(req->rq_export) |=
1695 *exp_connect_flags_ptr(req->rq_export) &=
1696 ~OBD_CONNECT_RDONLY;
1697 spin_unlock(&req->rq_export->exp_lock);
1698 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1699 struct changelog_setinfo *cs = val;
1701 if (vallen != sizeof(*cs)) {
1702 CERROR("%s: bad changelog_clear setinfo size %d\n",
1703 tgt_name(tsi->tsi_tgt), vallen);
1706 if (ptlrpc_req_need_swab(req)) {
1707 __swab64s(&cs->cs_recno);
1708 __swab32s(&cs->cs_id);
1711 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1719 static int mdt_readpage(struct tgt_session_info *tsi)
1721 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1722 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1723 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1724 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1725 struct mdt_body *repbody;
1731 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1732 RETURN(err_serious(-ENOMEM));
1734 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1735 if (repbody == NULL || reqbody == NULL)
1736 RETURN(err_serious(-EFAULT));
1739 * prepare @rdpg before calling lower layers and transfer itself. Here
1740 * reqbody->size contains offset of where to start to read and
1741 * reqbody->nlink contains number bytes to read.
1743 rdpg->rp_hash = reqbody->size;
1744 if (rdpg->rp_hash != reqbody->size) {
1745 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1746 rdpg->rp_hash, reqbody->size);
1750 rdpg->rp_attrs = reqbody->mode;
1751 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1752 rdpg->rp_attrs |= LUDA_64BITHASH;
1753 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1754 exp_max_brw_size(tsi->tsi_exp));
1755 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1757 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1758 if (rdpg->rp_pages == NULL)
1761 for (i = 0; i < rdpg->rp_npages; ++i) {
1762 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1763 if (rdpg->rp_pages[i] == NULL)
1764 GOTO(free_rdpg, rc = -ENOMEM);
1767 /* call lower layers to fill allocated pages with directory data */
1768 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1770 GOTO(free_rdpg, rc);
1772 /* send pages to client */
1773 rc = tgt_sendpage(tsi, rdpg, rc);
1778 for (i = 0; i < rdpg->rp_npages; i++)
1779 if (rdpg->rp_pages[i] != NULL)
1780 __free_page(rdpg->rp_pages[i]);
1781 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1783 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1789 static int mdt_reint_internal(struct mdt_thread_info *info,
1790 struct mdt_lock_handle *lhc,
1793 struct req_capsule *pill = info->mti_pill;
1794 struct mdt_body *repbody;
1799 rc = mdt_reint_unpack(info, op);
1801 CERROR("Can't unpack reint, rc %d\n", rc);
1802 RETURN(err_serious(rc));
1805 /* for replay (no_create) lmm is not needed, client has it already */
1806 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1807 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1810 /* llog cookies are always 0, the field is kept for compatibility */
1811 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1812 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1814 rc = req_capsule_server_pack(pill);
1816 CERROR("Can't pack response, rc %d\n", rc);
1817 RETURN(err_serious(rc));
1820 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1821 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1823 repbody->eadatasize = 0;
1824 repbody->aclsize = 0;
1827 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1829 /* for replay no cookkie / lmm need, because client have this already */
1830 if (info->mti_spec.no_create)
1831 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1832 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1834 rc = mdt_init_ucred_reint(info);
1836 GOTO(out_shrink, rc);
1838 rc = mdt_fix_attr_ucred(info, op);
1840 GOTO(out_ucred, rc = err_serious(rc));
1842 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1843 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1844 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1845 GOTO(out_ucred, rc);
1847 rc = mdt_reint_rec(info, lhc);
1850 mdt_exit_ucred(info);
1852 mdt_client_compatibility(info);
1853 rc2 = mdt_fix_reply(info);
1859 static long mdt_reint_opcode(struct ptlrpc_request *req,
1860 const struct req_format **fmt)
1862 struct mdt_device *mdt;
1863 struct mdt_rec_reint *rec;
1866 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1868 opc = rec->rr_opcode;
1869 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1870 if (opc < REINT_MAX && fmt[opc] != NULL)
1871 req_capsule_extend(&req->rq_pill, fmt[opc]);
1873 mdt = mdt_exp2dev(req->rq_export);
1874 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1875 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1876 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1877 opc = err_serious(-EFAULT);
1880 opc = err_serious(-EFAULT);
1885 static int mdt_reint(struct tgt_session_info *tsi)
1889 static const struct req_format *reint_fmts[REINT_MAX] = {
1890 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1891 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1892 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1893 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1894 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1895 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1896 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1897 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1898 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1903 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1905 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1907 * No lock possible here from client to pass it to reint code
1910 rc = mdt_reint_internal(info, NULL, opc);
1911 mdt_thread_info_fini(info);
1916 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1920 /* this should sync the whole device */
1921 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1923 struct dt_device *dt = mdt->mdt_bottom;
1927 rc = dt->dd_ops->dt_sync(env, dt);
1931 /* this should sync this object */
1932 static int mdt_object_sync(struct mdt_thread_info *info)
1934 struct md_object *next;
1938 if (!mdt_object_exists(info->mti_object)) {
1939 CWARN("Non existing object "DFID"!\n",
1940 PFID(mdt_object_fid(info->mti_object)));
1943 next = mdt_object_child(info->mti_object);
1944 rc = mo_object_sync(info->mti_env, next);
1949 static int mdt_sync(struct tgt_session_info *tsi)
1951 struct ptlrpc_request *req = tgt_ses_req(tsi);
1952 struct req_capsule *pill = tsi->tsi_pill;
1953 struct mdt_body *body;
1958 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1959 RETURN(err_serious(-ENOMEM));
1961 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1962 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1964 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1966 /* sync an object */
1967 rc = mdt_object_sync(info);
1969 const struct lu_fid *fid;
1970 struct lu_attr *la = &info->mti_attr.ma_attr;
1972 info->mti_attr.ma_need = MA_INODE;
1973 info->mti_attr.ma_valid = 0;
1974 rc = mdt_attr_get_complex(info, info->mti_object,
1977 body = req_capsule_server_get(pill,
1979 fid = mdt_object_fid(info->mti_object);
1980 mdt_pack_attr2body(info, body, la, fid);
1983 mdt_thread_info_fini(info);
1986 mdt_counter_incr(req, LPROC_MDT_SYNC);
1992 * Handle quota control requests to consult current usage/limit, but also
1993 * to configure quota enforcement
1995 static int mdt_quotactl(struct tgt_session_info *tsi)
1997 struct obd_export *exp = tsi->tsi_exp;
1998 struct req_capsule *pill = tsi->tsi_pill;
1999 struct obd_quotactl *oqctl, *repoqc;
2001 struct mdt_device *mdt = mdt_exp2dev(exp);
2002 struct lu_device *qmt = mdt->mdt_qmt_dev;
2005 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2007 RETURN(err_serious(-EPROTO));
2009 rc = req_capsule_server_pack(pill);
2011 RETURN(err_serious(rc));
2013 switch (oqctl->qc_cmd) {
2015 case LUSTRE_Q_INVALIDATE:
2016 case LUSTRE_Q_FINVALIDATE:
2020 /* deprecated, not used any more */
2021 RETURN(-EOPNOTSUPP);
2022 /* master quotactl */
2028 RETURN(-EOPNOTSUPP);
2029 /* slave quotactl */
2034 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2038 /* map uid/gid for remote client */
2040 if (exp_connect_rmtclient(exp)) {
2041 struct lustre_idmap_table *idmap;
2043 idmap = exp->exp_mdt_data.med_idmap;
2045 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2046 oqctl->qc_cmd != Q_GETINFO))
2049 if (oqctl->qc_type == USRQUOTA)
2050 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2052 else if (oqctl->qc_type == GRPQUOTA)
2053 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2058 if (id == CFS_IDMAP_NOTFOUND) {
2059 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2064 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2066 RETURN(err_serious(-EFAULT));
2068 if (oqctl->qc_id != id)
2069 swap(oqctl->qc_id, id);
2071 switch (oqctl->qc_cmd) {
2077 /* forward quotactl request to QMT */
2078 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2083 /* slave quotactl */
2084 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2089 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2093 if (oqctl->qc_id != id)
2094 swap(oqctl->qc_id, id);
2100 /** clone llog ctxt from child (mdd)
2101 * This allows remote llog (replicator) access.
2102 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2103 * context was originally set up, or we can handle them directly.
2104 * I choose the latter, but that means I need any llog
2105 * contexts set up by child to be accessable by the mdt. So we clone the
2106 * context into our context list here.
2108 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2111 struct md_device *next = mdt->mdt_child;
2112 struct llog_ctxt *ctxt;
2115 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2118 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2119 if (rc || ctxt == NULL) {
2123 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2125 CERROR("Can't set mdt ctxt %d\n", rc);
2130 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2131 struct mdt_device *mdt, int idx)
2133 struct llog_ctxt *ctxt;
2135 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2138 /* Put once for the get we just did, and once for the clone */
2139 llog_ctxt_put(ctxt);
2140 llog_ctxt_put(ctxt);
2145 * sec context handlers
2147 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2151 rc = mdt_handle_idmap(tsi);
2153 struct ptlrpc_request *req = tgt_ses_req(tsi);
2156 opc = lustre_msg_get_opc(req->rq_reqmsg);
2157 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2158 sptlrpc_svc_ctx_invalidate(req);
2161 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2167 * quota request handlers
2169 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2171 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2172 struct lu_device *qmt = mdt->mdt_qmt_dev;
2177 RETURN(err_serious(-EOPNOTSUPP));
2179 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2183 struct mdt_object *mdt_object_new(const struct lu_env *env,
2184 struct mdt_device *d,
2185 const struct lu_fid *f)
2187 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2188 struct lu_object *o;
2189 struct mdt_object *m;
2192 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2193 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2194 if (unlikely(IS_ERR(o)))
2195 m = (struct mdt_object *)o;
2201 struct mdt_object *mdt_object_find(const struct lu_env *env,
2202 struct mdt_device *d,
2203 const struct lu_fid *f)
2205 struct lu_object *o;
2206 struct mdt_object *m;
2209 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2210 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2211 if (unlikely(IS_ERR(o)))
2212 m = (struct mdt_object *)o;
2220 * Asyncronous commit for mdt device.
2222 * Pass asynchonous commit call down the MDS stack.
2224 * \param env environment
2225 * \param mdt the mdt device
2227 static void mdt_device_commit_async(const struct lu_env *env,
2228 struct mdt_device *mdt)
2230 struct dt_device *dt = mdt->mdt_bottom;
2233 rc = dt->dd_ops->dt_commit_async(env, dt);
2234 if (unlikely(rc != 0))
2235 CWARN("async commit start failed with rc = %d", rc);
2239 * Mark the lock as "synchonous".
2241 * Mark the lock to deffer transaction commit to the unlock time.
2243 * \param lock the lock to mark as "synchonous"
2245 * \see mdt_is_lock_sync
2246 * \see mdt_save_lock
2248 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2250 lock->l_ast_data = (void*)1;
2254 * Check whehter the lock "synchonous" or not.
2256 * \param lock the lock to check
2257 * \retval 1 the lock is "synchonous"
2258 * \retval 0 the lock isn't "synchronous"
2260 * \see mdt_set_lock_sync
2261 * \see mdt_save_lock
2263 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2265 return lock->l_ast_data != NULL;
2269 * Blocking AST for mdt locks.
2271 * Starts transaction commit if in case of COS lock conflict or
2272 * deffers such a commit to the mdt_save_lock.
2274 * \param lock the lock which blocks a request or cancelling lock
2275 * \param desc unused
2276 * \param data unused
2277 * \param flag indicates whether this cancelling or blocking callback
2279 * \see ldlm_blocking_ast_nocheck
2281 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2282 void *data, int flag)
2284 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2285 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2289 if (flag == LDLM_CB_CANCELING)
2291 lock_res_and_lock(lock);
2292 if (lock->l_blocking_ast != mdt_blocking_ast) {
2293 unlock_res_and_lock(lock);
2296 if (mdt_cos_is_enabled(mdt) &&
2297 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2298 lock->l_blocking_lock != NULL &&
2299 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2300 mdt_set_lock_sync(lock);
2302 rc = ldlm_blocking_ast_nocheck(lock);
2304 /* There is no lock conflict if l_blocking_lock == NULL,
2305 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2306 * when the last reference to a local lock was released */
2307 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2310 rc = lu_env_init(&env, LCT_LOCAL);
2311 if (unlikely(rc != 0))
2312 CWARN("lu_env initialization failed with rc = %d,"
2313 "cannot start asynchronous commit\n", rc);
2315 mdt_device_commit_async(&env, mdt);
2321 /* Used for cross-MDT lock */
2322 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2323 void *data, int flag)
2325 struct lustre_handle lockh;
2329 case LDLM_CB_BLOCKING:
2330 ldlm_lock2handle(lock, &lockh);
2331 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2333 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2337 case LDLM_CB_CANCELING:
2338 LDLM_DEBUG(lock, "Revoke remote lock\n");
2346 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2347 struct mdt_object *o, const struct lu_fid *fid,
2348 struct lustre_handle *lh, ldlm_mode_t mode,
2351 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2352 ldlm_policy_data_t *policy = &mti->mti_policy;
2353 struct ldlm_res_id *res_id = &mti->mti_res_id;
2357 LASSERT(mdt_object_remote(o));
2359 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2361 fid_build_reg_res_name(fid, res_id);
2363 memset(einfo, 0, sizeof(*einfo));
2364 einfo->ei_type = LDLM_IBITS;
2365 einfo->ei_mode = mode;
2366 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2367 einfo->ei_cb_cp = ldlm_completion_ast;
2368 einfo->ei_enq_slave = 0;
2369 einfo->ei_res_id = res_id;
2371 memset(policy, 0, sizeof(*policy));
2372 policy->l_inodebits.bits = ibits;
2374 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2379 static int mdt_object_local_lock(struct mdt_thread_info *info,
2380 struct mdt_object *o,
2381 struct mdt_lock_handle *lh, __u64 ibits,
2382 bool nonblock, int locality)
2384 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2385 ldlm_policy_data_t *policy = &info->mti_policy;
2386 struct ldlm_res_id *res_id = &info->mti_res_id;
2391 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2392 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2393 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2394 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2396 /* Only enqueue LOOKUP lock for remote object */
2397 if (mdt_object_remote(o))
2398 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2400 if (lh->mlh_type == MDT_PDO_LOCK) {
2401 /* check for exists after object is locked */
2402 if (mdt_object_exists(o) == 0) {
2403 /* Non-existent object shouldn't have PDO lock */
2406 /* Non-dir object shouldn't have PDO lock */
2407 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2412 memset(policy, 0, sizeof(*policy));
2413 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2415 dlmflags = LDLM_FL_ATOMIC_CB;
2417 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2420 * Take PDO lock on whole directory and build correct @res_id for lock
2421 * on part of directory.
2423 if (lh->mlh_pdo_hash != 0) {
2424 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2425 mdt_lock_pdo_mode(info, o, lh);
2426 if (lh->mlh_pdo_mode != LCK_NL) {
2428 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2429 * is never going to be sent to client and we do not
2430 * want it slowed down due to possible cancels.
2432 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2433 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2434 policy, res_id, dlmflags,
2435 info->mti_exp == NULL ? NULL :
2436 &info->mti_exp->exp_handle.h_cookie);
2442 * Finish res_id initializing by name hash marking part of
2443 * directory which is taking modification.
2445 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2448 policy->l_inodebits.bits = ibits;
2451 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2452 * going to be sent to client. If it is - mdt_intent_policy() path will
2453 * fix it up and turn FL_LOCAL flag off.
2455 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2456 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2457 info->mti_exp == NULL ? NULL :
2458 &info->mti_exp->exp_handle.h_cookie);
2460 mdt_object_unlock(info, o, lh, 1);
2461 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2462 lh->mlh_pdo_hash != 0 &&
2463 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2464 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2471 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2472 struct mdt_lock_handle *lh, __u64 ibits,
2473 bool nonblock, int locality)
2478 if (!mdt_object_remote(o))
2479 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2482 if (locality == MDT_LOCAL_LOCK) {
2483 CERROR("%s: try to get local lock for remote object"
2484 DFID".\n", mdt_obd_name(info->mti_mdt),
2485 PFID(mdt_object_fid(o)));
2489 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2490 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2491 MDS_INODELOCK_XATTR);
2492 if (ibits & MDS_INODELOCK_UPDATE) {
2493 /* Sigh, PDO needs to enqueue 2 locks right now, but
2494 * enqueue RPC can only request 1 lock, to avoid extra
2495 * RPC, so it will instead enqueue EX lock for remote
2496 * object anyway XXX*/
2497 if (lh->mlh_type == MDT_PDO_LOCK &&
2498 lh->mlh_pdo_hash != 0) {
2499 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2500 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2501 PFID(mdt_object_fid(o)));
2502 lh->mlh_pdo_hash = 0;
2503 lh->mlh_rreg_mode = LCK_EX;
2504 lh->mlh_type = MDT_REG_LOCK;
2506 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2509 MDS_INODELOCK_UPDATE);
2514 /* Only enqueue LOOKUP lock for remote object */
2515 if (ibits & MDS_INODELOCK_LOOKUP) {
2516 rc = mdt_object_local_lock(info, o, lh,
2517 MDS_INODELOCK_LOOKUP,
2518 nonblock, locality);
2526 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2527 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2529 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2532 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2533 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2535 struct mdt_lock_handle tmp = *lh;
2538 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2546 * Save a lock within request object.
2548 * Keep the lock referenced until whether client ACK or transaction
2549 * commit happens or release the lock immediately depending on input
2550 * parameters. If COS is ON, a write lock is converted to COS lock
2553 * \param info thead info object
2554 * \param h lock handle
2555 * \param mode lock mode
2556 * \param decref force immediate lock releasing
2559 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2560 ldlm_mode_t mode, int decref)
2564 if (lustre_handle_is_used(h)) {
2565 if (decref || !info->mti_has_trans ||
2566 !(mode & (LCK_PW | LCK_EX))){
2567 mdt_fid_unlock(h, mode);
2569 struct mdt_device *mdt = info->mti_mdt;
2570 struct ldlm_lock *lock = ldlm_handle2lock(h);
2571 struct ptlrpc_request *req = mdt_info_req(info);
2574 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2576 /* there is no request if mdt_object_unlock() is called
2577 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2578 if (likely(req != NULL)) {
2579 CDEBUG(D_HA, "request = %p reply state = %p"
2580 " transno = "LPD64"\n", req,
2581 req->rq_reply_state, req->rq_transno);
2582 if (mdt_cos_is_enabled(mdt)) {
2584 ldlm_lock_downgrade(lock, LCK_COS);
2587 ptlrpc_save_lock(req, h, mode, no_ack);
2589 ldlm_lock_decref(h, mode);
2591 if (mdt_is_lock_sync(lock)) {
2592 CDEBUG(D_HA, "found sync-lock,"
2593 " async commit started\n");
2594 mdt_device_commit_async(info->mti_env,
2597 LDLM_LOCK_PUT(lock);
2606 * Unlock mdt object.
2608 * Immeditely release the regular lock and the PDO lock or save the
2609 * lock in reqeuest and keep them referenced until client ACK or
2610 * transaction commit.
2612 * \param info thread info object
2613 * \param o mdt object
2614 * \param lh mdt lock handle referencing regular and PDO locks
2615 * \param decref force immediate lock releasing
2617 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2618 struct mdt_lock_handle *lh, int decref)
2622 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2623 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2625 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2626 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2631 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2632 const struct lu_fid *f,
2633 struct mdt_lock_handle *lh,
2636 struct mdt_object *o;
2638 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2642 rc = mdt_object_lock(info, o, lh, ibits,
2645 mdt_object_put(info->mti_env, o);
2652 void mdt_object_unlock_put(struct mdt_thread_info * info,
2653 struct mdt_object * o,
2654 struct mdt_lock_handle *lh,
2657 mdt_object_unlock(info, o, lh, decref);
2658 mdt_object_put(info->mti_env, o);
2662 * Generic code handling requests that have struct mdt_body passed in:
2664 * - extract mdt_body from request and save it in @info, if present;
2666 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2669 * - if HABEO_CORPUS flag is set for this request type check whether object
2670 * actually exists on storage (lu_object_exists()).
2673 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2675 const struct mdt_body *body;
2676 struct mdt_object *obj;
2677 const struct lu_env *env;
2678 struct req_capsule *pill;
2682 env = info->mti_env;
2683 pill = info->mti_pill;
2685 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2689 if (!(body->valid & OBD_MD_FLID))
2692 if (!fid_is_sane(&body->fid1)) {
2693 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2698 * Do not get size or any capa fields before we check that request
2699 * contains capa actually. There are some requests which do not, for
2700 * instance MDS_IS_SUBDIR.
2702 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2703 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2704 mdt_set_capainfo(info, 0, &body->fid1,
2705 req_capsule_client_get(pill, &RMF_CAPA1));
2707 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2709 if ((flags & HABEO_CORPUS) &&
2710 !mdt_object_exists(obj)) {
2711 mdt_object_put(env, obj);
2712 /* for capability renew ENOENT will be handled in
2714 if (body->valid & OBD_MD_FLOSSCAPA)
2719 info->mti_object = obj;
2728 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2730 struct req_capsule *pill = info->mti_pill;
2734 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2735 rc = mdt_body_unpack(info, flags);
2739 if (rc == 0 && (flags & HABEO_REFERO)) {
2741 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2742 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2744 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2745 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2748 rc = req_capsule_server_pack(pill);
2753 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2755 struct md_device *next = m->mdt_child;
2757 return next->md_ops->mdo_init_capa_ctxt(env, next,
2758 m->mdt_lut.lut_mds_capa,
2759 m->mdt_capa_timeout,
2764 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2766 lh->mlh_type = MDT_NUL_LOCK;
2767 lh->mlh_reg_lh.cookie = 0ull;
2768 lh->mlh_reg_mode = LCK_MINMODE;
2769 lh->mlh_pdo_lh.cookie = 0ull;
2770 lh->mlh_pdo_mode = LCK_MINMODE;
2771 lh->mlh_rreg_lh.cookie = 0ull;
2772 lh->mlh_rreg_mode = LCK_MINMODE;
2775 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2777 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2778 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2782 * Initialize fields of struct mdt_thread_info. Other fields are left in
2783 * uninitialized state, because it's too expensive to zero out whole
2784 * mdt_thread_info (> 1K) on each request arrival.
2786 void mdt_thread_info_init(struct ptlrpc_request *req,
2787 struct mdt_thread_info *info)
2791 info->mti_pill = &req->rq_pill;
2794 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2795 mdt_lock_handle_init(&info->mti_lh[i]);
2797 /* mdt device: it can be NULL while CONNECT */
2798 if (req->rq_export) {
2799 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2800 info->mti_exp = req->rq_export;
2802 info->mti_mdt = NULL;
2803 info->mti_env = req->rq_svc_thread->t_env;
2804 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2806 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2807 info->mti_big_buf = LU_BUF_NULL;
2808 info->mti_body = NULL;
2809 info->mti_object = NULL;
2810 info->mti_dlm_req = NULL;
2811 info->mti_has_trans = 0;
2812 info->mti_cross_ref = 0;
2813 info->mti_opdata = 0;
2814 info->mti_big_lmm_used = 0;
2816 info->mti_spec.no_create = 0;
2817 info->mti_spec.sp_rm_entry = 0;
2819 info->mti_spec.u.sp_ea.eadata = NULL;
2820 info->mti_spec.u.sp_ea.eadatalen = 0;
2823 void mdt_thread_info_fini(struct mdt_thread_info *info)
2827 if (info->mti_object != NULL) {
2828 mdt_object_put(info->mti_env, info->mti_object);
2829 info->mti_object = NULL;
2832 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2833 mdt_lock_handle_fini(&info->mti_lh[i]);
2834 info->mti_env = NULL;
2835 info->mti_pill = NULL;
2836 info->mti_exp = NULL;
2838 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2839 lu_buf_free(&info->mti_big_buf);
2842 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2844 struct mdt_thread_info *mti;
2845 struct lustre_capa *lc;
2847 mti = mdt_th_info(tsi->tsi_env);
2848 LASSERT(mti != NULL);
2850 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2851 if (tsi->tsi_corpus != NULL) {
2852 struct req_capsule *pill = tsi->tsi_pill;
2854 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2855 lu_object_get(tsi->tsi_corpus);
2858 * XXX: must be part of tgt_mdt_body_unpack but moved here
2859 * due to mdt_set_capainfo().
2861 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2862 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2863 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2864 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2867 mti->mti_body = tsi->tsi_mdt_body;
2868 mti->mti_dlm_req = tsi->tsi_dlm_req;
2873 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2875 struct ptlrpc_request *req = tgt_ses_req(tsi);
2880 rc = tgt_connect(tsi);
2884 rc = mdt_init_idmap(tsi);
2889 obd_disconnect(class_export_get(req->rq_export));
2908 static int mdt_intent_getattr(enum mdt_it_code opcode,
2909 struct mdt_thread_info *info,
2910 struct ldlm_lock **,
2913 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2914 struct mdt_thread_info *info,
2915 struct ldlm_lock **lockp,
2918 static int mdt_intent_layout(enum mdt_it_code opcode,
2919 struct mdt_thread_info *info,
2920 struct ldlm_lock **,
2922 static int mdt_intent_reint(enum mdt_it_code opcode,
2923 struct mdt_thread_info *info,
2924 struct ldlm_lock **,
2927 static struct mdt_it_flavor {
2928 const struct req_format *it_fmt;
2930 int (*it_act)(enum mdt_it_code ,
2931 struct mdt_thread_info *,
2932 struct ldlm_lock **,
2935 } mdt_it_flavor[] = {
2937 .it_fmt = &RQF_LDLM_INTENT,
2938 /*.it_flags = HABEO_REFERO,*/
2940 .it_act = mdt_intent_reint,
2941 .it_reint = REINT_OPEN
2944 .it_fmt = &RQF_LDLM_INTENT,
2946 * OCREAT is not a MUTABOR request as if the file
2948 * We do the extra check of OBD_CONNECT_RDONLY in
2949 * mdt_reint_open() when we really need to create
2953 .it_act = mdt_intent_reint,
2954 .it_reint = REINT_OPEN
2957 .it_fmt = &RQF_LDLM_INTENT,
2958 .it_flags = MUTABOR,
2959 .it_act = mdt_intent_reint,
2960 .it_reint = REINT_CREATE
2962 [MDT_IT_GETATTR] = {
2963 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2964 .it_flags = HABEO_REFERO,
2965 .it_act = mdt_intent_getattr
2967 [MDT_IT_READDIR] = {
2973 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2974 .it_flags = HABEO_REFERO,
2975 .it_act = mdt_intent_getattr
2978 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2979 .it_flags = MUTABOR,
2981 .it_reint = REINT_UNLINK
2985 .it_flags = MUTABOR,
2988 [MDT_IT_GETXATTR] = {
2989 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2990 .it_flags = HABEO_CORPUS,
2991 .it_act = mdt_intent_getxattr
2994 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2996 .it_act = mdt_intent_layout
3001 mdt_intent_lock_replace(struct mdt_thread_info *info, struct ldlm_lock **lockp,
3002 struct ldlm_lock *new_lock, struct mdt_lock_handle *lh,
3005 struct ptlrpc_request *req = mdt_info_req(info);
3006 struct ldlm_lock *lock = *lockp;
3009 * Get new lock only for cases when possible resent did not find any
3012 if (new_lock == NULL)
3013 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3015 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3016 lh->mlh_reg_lh.cookie = 0;
3020 LASSERTF(new_lock != NULL,
3021 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3024 * If we've already given this lock to a client once, then we should
3025 * have no readers or writers. Otherwise, we should have one reader
3026 * _or_ writer ref (which will be zeroed below) before returning the
3029 if (new_lock->l_export == req->rq_export) {
3030 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3032 LASSERT(new_lock->l_export == NULL);
3033 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3038 if (new_lock->l_export == req->rq_export) {
3040 * Already gave this to the client, which means that we
3041 * reconstructed a reply.
3043 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3045 lh->mlh_reg_lh.cookie = 0;
3046 RETURN(ELDLM_LOCK_REPLACED);
3050 * Fixup the lock to be given to the client.
3052 lock_res_and_lock(new_lock);
3053 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3054 * possible blocking AST. */
3055 while (new_lock->l_readers > 0) {
3056 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3057 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3058 new_lock->l_readers--;
3060 while (new_lock->l_writers > 0) {
3061 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3062 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3063 new_lock->l_writers--;
3066 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3067 new_lock->l_blocking_ast = lock->l_blocking_ast;
3068 new_lock->l_completion_ast = lock->l_completion_ast;
3069 new_lock->l_remote_handle = lock->l_remote_handle;
3070 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3072 unlock_res_and_lock(new_lock);
3074 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3075 &new_lock->l_remote_handle,
3076 &new_lock->l_exp_hash);
3078 LDLM_LOCK_RELEASE(new_lock);
3079 lh->mlh_reg_lh.cookie = 0;
3081 RETURN(ELDLM_LOCK_REPLACED);
3084 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3085 struct ldlm_lock *new_lock,
3086 struct ldlm_lock **old_lock,
3087 struct mdt_lock_handle *lh,
3088 enum mdt_it_code opcode)
3090 struct ptlrpc_request *req = mdt_info_req(info);
3091 struct obd_export *exp = req->rq_export;
3092 struct lustre_handle remote_hdl;
3093 struct ldlm_request *dlmreq;
3094 struct ldlm_lock *lock;
3096 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3099 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3100 remote_hdl = dlmreq->lock_handle[0];
3101 /* If the client does not require open lock, it does not need to
3102 * search lock in exp_lock_hash, since the server thread will
3103 * make sure the lock will be released, and the resend request
3104 * can always re-enqueue the lock */
3105 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3106 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3107 /* In the function below, .hs_keycmp resolves to
3108 * ldlm_export_lock_keycmp() */
3109 /* coverity[overrun-buffer-val] */
3110 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3112 lock_res_and_lock(lock);
3113 if (lock != new_lock) {
3114 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3115 lh->mlh_reg_mode = lock->l_granted_mode;
3117 LDLM_DEBUG(lock, "Restoring lock cookie");
3118 DEBUG_REQ(D_DLMTRACE, req,
3119 "restoring lock cookie "LPX64,
3120 lh->mlh_reg_lh.cookie);
3122 *old_lock = LDLM_LOCK_GET(lock);
3123 cfs_hash_put(exp->exp_lock_hash,
3125 unlock_res_and_lock(lock);
3128 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3129 unlock_res_and_lock(lock);
3133 * If the xid matches, then we know this is a resent request, and allow
3134 * it. (It's probably an OPEN, for which we don't send a lock.
3136 if (req_xid_is_last(req))
3140 * This remote handle isn't enqueued, so we never received or processed
3141 * this request. Clear MSG_RESENT, because it can be handled like any
3142 * normal request now.
3144 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3146 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3150 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3151 struct mdt_thread_info *info,
3152 struct ldlm_lock **lockp,
3155 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3156 struct ldlm_reply *ldlm_rep = NULL;
3160 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3161 * (for the resend case) or a new lock. Below we will use it to
3162 * replace the original lock.
3164 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3165 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3166 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3167 rc = mdt_object_lock(info, info->mti_object, lhc,
3168 MDS_INODELOCK_XATTR,