4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 * struct OBD_{ALLOC,FREE}*()
55 #include <obd_support.h>
56 #include <lustre_ioctl.h>
57 /* struct ptlrpc_request */
58 #include <lustre_net.h>
59 /* struct obd_export */
60 #include <lustre_export.h>
61 /* struct obd_device */
64 #include <dt_object.h>
65 #include <lustre_mds.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
70 #include <lustre_quota.h>
71 #include <lustre_lfsck.h>
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 ldlm_mode_t mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
96 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
134 return (rep->lock_policy_res1 & flag);
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, int flag)
141 info->mti_opdata &= ~flag;
142 tgt_opdata_clear(info->mti_env, flag);
145 rep->lock_policy_res1 &= ~flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, int flag)
152 info->mti_opdata |= flag;
153 tgt_opdata_set(info->mti_env, flag);
156 rep->lock_policy_res1 |= flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
168 const struct lu_name *lname)
170 lh->mlh_reg_mode = lock_mode;
171 lh->mlh_rreg_mode = lock_mode;
172 lh->mlh_type = MDT_PDO_LOCK;
174 if (lu_name_is_valid(lname)) {
175 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
177 /* XXX Workaround for LU-2856
179 * Zero is a valid return value of full_name_hash, but
180 * several users of mlh_pdo_hash assume a non-zero
181 * hash value. We therefore map zero onto an
182 * arbitrary, but consistent value (1) to avoid
183 * problems further down the road. */
184 if (unlikely(lh->mlh_pdo_hash == 0))
185 lh->mlh_pdo_hash = 1;
187 lh->mlh_pdo_hash = 0;
191 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
192 struct mdt_lock_handle *lh)
198 * Any dir access needs couple of locks:
200 * 1) on part of dir we gonna take lookup/modify;
202 * 2) on whole dir to protect it from concurrent splitting and/or to
203 * flush client's cache for readdir().
205 * so, for a given mode and object this routine decides what lock mode
206 * to use for lock #2:
208 * 1) if caller's gonna lookup in dir then we need to protect dir from
209 * being splitted only - LCK_CR
211 * 2) if caller's gonna modify dir then we need to protect dir from
212 * being splitted and to flush cache - LCK_CW
214 * 3) if caller's gonna modify dir and that dir seems ready for
215 * splitting then we need to protect it from any type of access
216 * (lookup/modify/split) - LCK_EX --bzzz
219 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
220 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
223 * Ask underlaying level its opinion about preferable PDO lock mode
224 * having access type passed as regular lock mode:
226 * - MDL_MINMODE means that lower layer does not want to specify lock
229 * - MDL_NL means that no PDO lock should be taken. This is used in some
230 * cases. Say, for non-splittable directories no need to use PDO locks
233 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
234 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
236 if (mode != MDL_MINMODE) {
237 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
240 * Lower layer does not want to specify locking mode. We do it
241 * our selves. No special protection is needed, just flush
242 * client's cache on modification and allow concurrent
245 switch (lh->mlh_reg_mode) {
247 lh->mlh_pdo_mode = LCK_EX;
250 lh->mlh_pdo_mode = LCK_CR;
253 lh->mlh_pdo_mode = LCK_CW;
256 CERROR("Not expected lock type (0x%x)\n",
257 (int)lh->mlh_reg_mode);
262 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
266 static int mdt_getstatus(struct tgt_session_info *tsi)
268 struct mdt_thread_info *info = tsi2mdt_info(tsi);
269 struct mdt_device *mdt = info->mti_mdt;
270 struct mdt_body *repbody;
275 rc = mdt_check_ucred(info);
277 GOTO(out, rc = err_serious(rc));
279 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
280 GOTO(out, rc = err_serious(-ENOMEM));
282 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
283 repbody->fid1 = mdt->mdt_md_root_fid;
284 repbody->valid |= OBD_MD_FLID;
286 if (tsi->tsi_tgt->lut_mds_capa &&
287 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
288 struct mdt_object *root;
289 struct lustre_capa *capa;
291 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
293 GOTO(out, rc = PTR_ERR(root));
295 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
297 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
298 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
300 mdt_object_put(info->mti_env, root);
302 repbody->valid |= OBD_MD_FLMDSCAPA;
306 mdt_thread_info_fini(info);
310 static int mdt_statfs(struct tgt_session_info *tsi)
312 struct ptlrpc_request *req = tgt_ses_req(tsi);
313 struct mdt_thread_info *info = tsi2mdt_info(tsi);
314 struct md_device *next = info->mti_mdt->mdt_child;
315 struct ptlrpc_service_part *svcpt;
316 struct obd_statfs *osfs;
321 svcpt = req->rq_rqbd->rqbd_svcpt;
323 /* This will trigger a watchdog timeout */
324 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
325 (MDT_SERVICE_WATCHDOG_FACTOR *
326 at_get(&svcpt->scp_at_estimate)) + 1);
328 rc = mdt_check_ucred(info);
330 GOTO(out, rc = err_serious(rc));
332 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
333 GOTO(out, rc = err_serious(-ENOMEM));
335 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
337 GOTO(out, rc = -EPROTO);
339 /** statfs information are cached in the mdt_device */
340 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
341 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
342 /** statfs data is too old, get up-to-date one */
343 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
346 spin_lock(&info->mti_mdt->mdt_osfs_lock);
347 info->mti_mdt->mdt_osfs = *osfs;
348 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
349 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
351 /** use cached statfs data */
352 spin_lock(&info->mti_mdt->mdt_osfs_lock);
353 *osfs = info->mti_mdt->mdt_osfs;
354 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
358 mdt_counter_incr(req, LPROC_MDT_STATFS);
360 mdt_thread_info_fini(info);
365 * Pack SOM attributes into the reply.
366 * Call under a DLM UPDATE lock.
368 static void mdt_pack_size2body(struct mdt_thread_info *info,
369 struct mdt_object *mo)
372 struct md_attr *ma = &info->mti_attr;
374 LASSERT(ma->ma_attr.la_valid & LA_MODE);
375 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
377 /* Check if Size-on-MDS is supported, if this is a regular file,
378 * if SOM is enabled on the object and if SOM cache exists and valid.
379 * Otherwise do not pack Size-on-MDS attributes to the reply. */
380 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
381 !S_ISREG(ma->ma_attr.la_mode) ||
382 !mdt_object_is_som_enabled(mo) ||
383 !(ma->ma_valid & MA_SOM))
386 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
387 b->size = ma->ma_som->msd_size;
388 b->blocks = ma->ma_som->msd_blocks;
391 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
392 const struct lu_attr *attr, const struct lu_fid *fid)
394 struct md_attr *ma = &info->mti_attr;
396 LASSERT(ma->ma_valid & MA_INODE);
398 b->atime = attr->la_atime;
399 b->mtime = attr->la_mtime;
400 b->ctime = attr->la_ctime;
401 b->mode = attr->la_mode;
402 b->size = attr->la_size;
403 b->blocks = attr->la_blocks;
404 b->uid = attr->la_uid;
405 b->gid = attr->la_gid;
406 b->flags = attr->la_flags;
407 b->nlink = attr->la_nlink;
408 b->rdev = attr->la_rdev;
410 /*XXX should pack the reply body according to lu_valid*/
411 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
412 OBD_MD_FLGID | OBD_MD_FLTYPE |
413 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
414 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
416 if (!S_ISREG(attr->la_mode)) {
417 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
418 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
419 /* means no objects are allocated on osts. */
420 LASSERT(!(ma->ma_valid & MA_LOV));
421 /* just ignore blocks occupied by extend attributes on MDS */
423 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
424 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
425 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
426 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
427 /* A released file stores its size on MDS. */
428 /* But return 1 block for released file, unless tools like tar
429 * will consider it fully sparse. (LU-3864)
431 if (unlikely(b->size == 0))
435 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
440 b->valid |= OBD_MD_FLID;
441 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
442 PFID(fid), b->nlink, b->mode, b->size);
446 mdt_body_reverse_idmap(info, b);
448 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
449 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
450 PFID(fid), (unsigned long long)b->size);
453 static inline int mdt_body_has_lov(const struct lu_attr *la,
454 const struct mdt_body *body)
456 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
457 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
460 void mdt_client_compatibility(struct mdt_thread_info *info)
462 struct mdt_body *body;
463 struct ptlrpc_request *req = mdt_info_req(info);
464 struct obd_export *exp = req->rq_export;
465 struct md_attr *ma = &info->mti_attr;
466 struct lu_attr *la = &ma->ma_attr;
469 if (exp_connect_layout(exp))
470 /* the client can deal with 16-bit lmm_stripe_count */
473 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
475 if (!mdt_body_has_lov(la, body))
478 /* now we have a reply with a lov for a client not compatible with the
479 * layout lock so we have to clean the layout generation number */
480 if (S_ISREG(la->la_mode))
481 ma->ma_lmm->lmm_layout_gen = 0;
485 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
487 const struct lu_env *env = info->mti_env;
490 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
499 /* Is it a directory? Let's check for the LMV as well */
500 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
501 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
503 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
511 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
514 const struct lu_env *env = info->mti_env;
518 LASSERT(info->mti_big_lmm_used == 0);
519 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
523 /* big_lmm may need to be grown */
524 if (info->mti_big_lmmsize < rc) {
525 int size = size_roundup_power2(rc);
527 if (info->mti_big_lmmsize > 0) {
528 /* free old buffer */
529 LASSERT(info->mti_big_lmm);
530 OBD_FREE_LARGE(info->mti_big_lmm,
531 info->mti_big_lmmsize);
532 info->mti_big_lmm = NULL;
533 info->mti_big_lmmsize = 0;
536 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
537 if (info->mti_big_lmm == NULL)
539 info->mti_big_lmmsize = size;
541 LASSERT(info->mti_big_lmmsize >= rc);
543 info->mti_buf.lb_buf = info->mti_big_lmm;
544 info->mti_buf.lb_len = info->mti_big_lmmsize;
545 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
550 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
551 struct md_attr *ma, const char *name)
553 struct md_object *next = mdt_object_child(o);
554 struct lu_buf *buf = &info->mti_buf;
557 if (strcmp(name, XATTR_NAME_LOV) == 0) {
558 buf->lb_buf = ma->ma_lmm;
559 buf->lb_len = ma->ma_lmm_size;
560 LASSERT(!(ma->ma_valid & MA_LOV));
561 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
562 buf->lb_buf = ma->ma_lmv;
563 buf->lb_len = ma->ma_lmv_size;
564 LASSERT(!(ma->ma_valid & MA_LMV));
565 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
566 buf->lb_buf = ma->ma_lmv;
567 buf->lb_len = ma->ma_lmv_size;
568 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
573 rc = mo_xattr_get(info->mti_env, next, buf, name);
575 if (strcmp(name, XATTR_NAME_LOV) == 0) {
576 ma->ma_lmm_size = rc;
577 ma->ma_valid |= MA_LOV;
578 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
579 ma->ma_lmv_size = rc;
580 ma->ma_valid |= MA_LMV;
581 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
582 ma->ma_lmv_size = rc;
583 ma->ma_valid |= MA_LMV_DEF;
587 } else if (rc == -ENODATA) {
590 } else if (rc == -ERANGE) {
591 /* Default LMV has fixed size, so it must be able to fit
592 * in the original buffer */
593 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
595 rc = mdt_big_xattr_get(info, o, name);
597 info->mti_big_lmm_used = 1;
598 if (!strcmp(name, XATTR_NAME_LOV)) {
599 ma->ma_valid |= MA_LOV;
600 ma->ma_lmm = info->mti_big_lmm;
601 ma->ma_lmm_size = rc;
602 } else if (!strcmp(name, XATTR_NAME_LMV)) {
603 ma->ma_valid |= MA_LMV;
604 ma->ma_lmv = info->mti_big_lmm;
605 ma->ma_lmv_size = rc;
610 /* update mdt_max_mdsize so all clients
611 * will be aware about that */
612 if (info->mti_mdt->mdt_max_mdsize < rc)
613 info->mti_mdt->mdt_max_mdsize = rc;
621 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
622 struct mdt_object *o, struct lu_fid *pfid)
624 struct lu_buf *buf = &info->mti_buf;
625 struct link_ea_header *leh;
626 struct link_ea_entry *lee;
630 buf->lb_buf = info->mti_big_lmm;
631 buf->lb_len = info->mti_big_lmmsize;
632 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
633 buf, XATTR_NAME_LINK);
634 /* ignore errors, MA_PFID won't be set and it is
635 * up to the caller to treat this as an error */
636 if (rc == -ERANGE || buf->lb_len == 0) {
637 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
638 buf->lb_buf = info->mti_big_lmm;
639 buf->lb_len = info->mti_big_lmmsize;
644 if (rc < sizeof(*leh)) {
645 CERROR("short LinkEA on "DFID": rc = %d\n",
646 PFID(mdt_object_fid(o)), rc);
650 leh = (struct link_ea_header *) buf->lb_buf;
651 lee = (struct link_ea_entry *)(leh + 1);
652 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
653 leh->leh_magic = LINK_EA_MAGIC;
654 leh->leh_reccount = __swab32(leh->leh_reccount);
655 leh->leh_len = __swab64(leh->leh_len);
657 if (leh->leh_magic != LINK_EA_MAGIC)
659 if (leh->leh_reccount == 0)
662 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
663 fid_be_to_cpu(pfid, pfid);
668 int mdt_attr_get_complex(struct mdt_thread_info *info,
669 struct mdt_object *o, struct md_attr *ma)
671 const struct lu_env *env = info->mti_env;
672 struct md_object *next = mdt_object_child(o);
673 struct lu_buf *buf = &info->mti_buf;
674 u32 mode = lu_object_attr(&next->mo_lu);
675 int need = ma->ma_need;
681 if (need & MA_INODE) {
682 ma->ma_need = MA_INODE;
683 rc = mo_attr_get(env, next, ma);
686 ma->ma_valid |= MA_INODE;
689 if (need & MA_PFID) {
690 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
692 ma->ma_valid |= MA_PFID;
693 /* ignore this error, parent fid is not mandatory */
697 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
698 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
703 if (need & MA_LMV && S_ISDIR(mode)) {
704 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
709 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
710 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
715 if (need & MA_SOM && S_ISREG(mode)) {
716 buf->lb_buf = info->mti_xattr_buf;
717 buf->lb_len = sizeof(info->mti_xattr_buf);
718 CLASSERT(sizeof(struct som_attrs) <=
719 sizeof(info->mti_xattr_buf));
720 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
721 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
723 ma->ma_valid |= MA_SOM;
724 else if (rc2 < 0 && rc2 != -ENODATA)
728 if (need & MA_HSM && S_ISREG(mode)) {
729 buf->lb_buf = info->mti_xattr_buf;
730 buf->lb_len = sizeof(info->mti_xattr_buf);
731 CLASSERT(sizeof(struct hsm_attrs) <=
732 sizeof(info->mti_xattr_buf));
733 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
734 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
736 ma->ma_valid |= MA_HSM;
737 else if (rc2 < 0 && rc2 != -ENODATA)
741 #ifdef CONFIG_FS_POSIX_ACL
742 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
743 buf->lb_buf = ma->ma_acl;
744 buf->lb_len = ma->ma_acl_size;
745 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
747 ma->ma_acl_size = rc2;
748 ma->ma_valid |= MA_ACL_DEF;
749 } else if (rc2 == -ENODATA) {
758 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
759 rc, ma->ma_valid, ma->ma_lmm);
763 static int mdt_getattr_internal(struct mdt_thread_info *info,
764 struct mdt_object *o, int ma_need)
766 struct md_object *next = mdt_object_child(o);
767 const struct mdt_body *reqbody = info->mti_body;
768 struct ptlrpc_request *req = mdt_info_req(info);
769 struct md_attr *ma = &info->mti_attr;
770 struct lu_attr *la = &ma->ma_attr;
771 struct req_capsule *pill = info->mti_pill;
772 const struct lu_env *env = info->mti_env;
773 struct mdt_body *repbody;
774 struct lu_buf *buffer = &info->mti_buf;
779 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
780 RETURN(err_serious(-ENOMEM));
782 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
786 if (mdt_object_remote(o)) {
787 /* This object is located on remote node.*/
788 /* Return -EIO for old client */
789 if (!mdt_is_dne_client(req->rq_export))
790 GOTO(out, rc = -EIO);
792 repbody->fid1 = *mdt_object_fid(o);
793 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
797 if (reqbody->eadatasize > 0) {
798 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
799 if (buffer->lb_buf == NULL)
800 GOTO(out, rc = -EPROTO);
801 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
804 buffer->lb_buf = NULL;
806 ma_need &= ~(MA_LOV | MA_LMV);
807 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
808 mdt_obd_name(info->mti_mdt),
809 req->rq_export->exp_client_uuid.uuid);
812 /* If it is dir object and client require MEA, then we got MEA */
813 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
814 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
815 /* Assumption: MDT_MD size is enough for lmv size. */
816 ma->ma_lmv = buffer->lb_buf;
817 ma->ma_lmv_size = buffer->lb_len;
818 ma->ma_need = MA_INODE;
819 if (ma->ma_lmv_size > 0) {
820 if (reqbody->valid & OBD_MD_MEA)
821 ma->ma_need |= MA_LMV;
822 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
823 ma->ma_need |= MA_LMV_DEF;
826 ma->ma_lmm = buffer->lb_buf;
827 ma->ma_lmm_size = buffer->lb_len;
828 ma->ma_need = MA_INODE | MA_HSM;
829 if (ma->ma_lmm_size > 0)
830 ma->ma_need |= MA_LOV;
833 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
834 reqbody->valid & OBD_MD_FLDIREA &&
835 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
836 /* get default stripe info for this dir. */
837 ma->ma_need |= MA_LOV_DEF;
839 ma->ma_need |= ma_need;
840 if (ma->ma_need & MA_SOM)
841 ma->ma_som = &info->mti_u.som.data;
843 rc = mdt_attr_get_complex(info, o, ma);
845 CERROR("%s: getattr error for "DFID": rc = %d\n",
846 mdt_obd_name(info->mti_mdt),
847 PFID(mdt_object_fid(o)), rc);
851 /* if file is released, check if a restore is running */
852 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
853 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
854 repbody->t_state = MS_RESTORE;
855 repbody->valid |= OBD_MD_TSTATE;
858 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
860 /* the Lustre protocol supposes to return default striping
861 * on the user-visible root if explicitly requested */
862 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
863 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
864 struct lu_fid rootfid;
865 struct mdt_object *root;
866 struct mdt_device *mdt = info->mti_mdt;
868 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
871 root = mdt_object_find(env, mdt, &rootfid);
873 RETURN(PTR_ERR(root));
874 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
875 mdt_object_put(info->mti_env, root);
877 CERROR("%s: getattr error for "DFID": rc = %d\n",
878 mdt_obd_name(info->mti_mdt),
879 PFID(mdt_object_fid(o)), rc);
884 if (likely(ma->ma_valid & MA_INODE))
885 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
889 if (mdt_body_has_lov(la, reqbody)) {
890 if (ma->ma_valid & MA_LOV) {
891 LASSERT(ma->ma_lmm_size);
892 repbody->eadatasize = ma->ma_lmm_size;
893 if (S_ISDIR(la->la_mode))
894 repbody->valid |= OBD_MD_FLDIREA;
896 repbody->valid |= OBD_MD_FLEASIZE;
897 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
899 if (ma->ma_valid & MA_LMV) {
900 LASSERT(S_ISDIR(la->la_mode));
901 mdt_dump_lmv(D_INFO, ma->ma_lmv);
902 repbody->eadatasize = ma->ma_lmv_size;
903 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
905 if (ma->ma_valid & MA_LMV_DEF) {
906 LASSERT(S_ISDIR(la->la_mode));
907 repbody->eadatasize = ma->ma_lmv_size;
908 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
910 } else if (S_ISLNK(la->la_mode) &&
911 reqbody->valid & OBD_MD_LINKNAME) {
912 buffer->lb_buf = ma->ma_lmm;
913 /* eadatasize from client includes NULL-terminator, so
914 * there is no need to read it */
915 buffer->lb_len = reqbody->eadatasize - 1;
916 rc = mo_readlink(env, next, buffer);
917 if (unlikely(rc <= 0)) {
918 CERROR("%s: readlink failed for "DFID": rc = %d\n",
919 mdt_obd_name(info->mti_mdt),
920 PFID(mdt_object_fid(o)), rc);
923 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
925 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
927 repbody->valid |= OBD_MD_LINKNAME;
928 /* we need to report back size with NULL-terminator
929 * because client expects that */
930 repbody->eadatasize = rc + 1;
931 if (repbody->eadatasize != reqbody->eadatasize)
932 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
933 "on "DFID ", expected %d\n",
934 mdt_obd_name(info->mti_mdt),
935 rc, PFID(mdt_object_fid(o)),
936 reqbody->eadatasize - 1);
938 ((char *)ma->ma_lmm)[rc] = 0;
940 /* If the total CDEBUG() size is larger than a page, it
941 * will print a warning to the console, avoid this by
942 * printing just the last part of the symlink. */
943 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
944 print_limit < rc ? "..." : "", print_limit,
945 (char *)ma->ma_lmm + rc - print_limit, rc);
950 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
951 repbody->max_cookiesize = 0;
952 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
953 repbody->valid |= OBD_MD_FLMODEASIZE;
954 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
955 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
956 repbody->max_cookiesize);
959 if (exp_connect_rmtclient(info->mti_exp) &&
960 reqbody->valid & OBD_MD_FLRMTPERM) {
961 void *buf = req_capsule_server_get(pill, &RMF_ACL);
963 /* mdt_getattr_lock only */
964 rc = mdt_pack_remote_perm(info, o, buf);
966 repbody->valid &= ~OBD_MD_FLRMTPERM;
967 repbody->aclsize = 0;
970 repbody->valid |= OBD_MD_FLRMTPERM;
971 repbody->aclsize = sizeof(struct mdt_remote_perm);
974 #ifdef CONFIG_FS_POSIX_ACL
975 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
976 (reqbody->valid & OBD_MD_FLACL)) {
977 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
978 buffer->lb_len = req_capsule_get_size(pill,
979 &RMF_ACL, RCL_SERVER);
980 if (buffer->lb_len > 0) {
981 rc = mo_xattr_get(env, next, buffer,
982 XATTR_NAME_ACL_ACCESS);
984 if (rc == -ENODATA) {
985 repbody->aclsize = 0;
986 repbody->valid |= OBD_MD_FLACL;
988 } else if (rc == -EOPNOTSUPP) {
991 CERROR("%s: unable to read "DFID
993 mdt_obd_name(info->mti_mdt),
994 PFID(mdt_object_fid(o)), rc);
997 repbody->aclsize = rc;
998 repbody->valid |= OBD_MD_FLACL;
1005 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
1006 info->mti_mdt->mdt_lut.lut_mds_capa &&
1007 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1008 struct lustre_capa *capa;
1010 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1012 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1013 rc = mo_capa_get(env, next, capa, 0);
1016 repbody->valid |= OBD_MD_FLMDSCAPA;
1021 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1026 static int mdt_renew_capa(struct mdt_thread_info *info)
1028 struct mdt_object *obj = info->mti_object;
1029 struct mdt_body *body;
1030 struct lustre_capa *capa, *c;
1034 /* if object doesn't exist, or server has disabled capability,
1035 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1038 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1039 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1042 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1043 LASSERT(body != NULL);
1045 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1048 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1052 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1054 body->valid |= OBD_MD_FLOSSCAPA;
1058 static int mdt_getattr(struct tgt_session_info *tsi)
1060 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1061 struct mdt_object *obj = info->mti_object;
1062 struct req_capsule *pill = info->mti_pill;
1063 struct mdt_body *reqbody;
1064 struct mdt_body *repbody;
1068 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1071 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1072 rc = req_capsule_server_pack(pill);
1074 RETURN(err_serious(rc));
1075 rc = mdt_renew_capa(info);
1076 GOTO(out_shrink, rc);
1079 LASSERT(obj != NULL);
1080 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1082 /* Unlike intent case where we need to pre-fill out buffers early on
1083 * in intent policy for ldlm reasons, here we can have a much better
1084 * guess at EA size by just reading it from disk.
1085 * Exceptions are readdir and (missing) directory striping */
1087 if (reqbody->valid & OBD_MD_LINKNAME) {
1088 /* No easy way to know how long is the symlink, but it cannot
1089 * be more than PATH_MAX, so we allocate +1 */
1092 /* A special case for fs ROOT: getattr there might fetch
1093 * default EA for entire fs, not just for this dir!
1095 } else if (lu_fid_eq(mdt_object_fid(obj),
1096 &info->mti_mdt->mdt_md_root_fid) &&
1097 (reqbody->valid & OBD_MD_FLDIREA) &&
1098 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1100 /* Should the default strping be bigger, mdt_fix_reply
1101 * will reallocate */
1102 rc = DEF_REP_MD_SIZE;
1104 /* Read the actual EA size from disk */
1105 rc = mdt_attr_get_eabuf_size(info, obj);
1109 GOTO(out_shrink, rc);
1111 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1113 rc = req_capsule_server_pack(pill);
1114 if (unlikely(rc != 0))
1115 GOTO(out, rc = err_serious(rc));
1117 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1118 LASSERT(repbody != NULL);
1119 repbody->eadatasize = 0;
1120 repbody->aclsize = 0;
1122 if (reqbody->valid & OBD_MD_FLRMTPERM)
1123 rc = mdt_init_ucred(info, reqbody);
1125 rc = mdt_check_ucred(info);
1127 GOTO(out_shrink, rc);
1129 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1132 * Don't check capability at all, because rename might getattr for
1133 * remote obj, and at that time no capability is available.
1135 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1136 rc = mdt_getattr_internal(info, obj, 0);
1137 if (reqbody->valid & OBD_MD_FLRMTPERM)
1138 mdt_exit_ucred(info);
1141 mdt_client_compatibility(info);
1142 rc2 = mdt_fix_reply(info);
1146 mdt_thread_info_fini(info);
1150 static int mdt_is_subdir(struct tgt_session_info *tsi)
1152 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1153 struct mdt_object *o = info->mti_object;
1154 struct req_capsule *pill = info->mti_pill;
1155 const struct mdt_body *body = info->mti_body;
1156 struct mdt_body *repbody;
1162 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1165 * We save last checked parent fid to @repbody->fid1 for remote
1168 LASSERT(fid_is_sane(&body->fid2));
1169 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1170 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1171 &body->fid2, &repbody->fid1);
1172 if (rc == 0 || rc == -EREMOTE)
1173 repbody->valid |= OBD_MD_FLID;
1175 mdt_thread_info_fini(info);
1179 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1181 struct mdt_thread_info *info;
1182 struct ptlrpc_request *req = tgt_ses_req(tsi);
1183 struct obd_export *exp = req->rq_export;
1184 struct mdt_object *o1, *o2, *o;
1185 struct mdt_lock_handle *lh1, *lh2;
1186 struct mdc_swap_layouts *msl;
1190 /* client does not support layout lock, so layout swaping
1192 * FIXME: there is a problem for old clients which don't support
1193 * layout lock yet. If those clients have already opened the file
1194 * they won't be notified at all so that old layout may still be
1195 * used to do IO. This can be fixed after file release is landed by
1196 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1197 if (!exp_connect_layout(exp))
1198 RETURN(-EOPNOTSUPP);
1200 info = tsi2mdt_info(tsi);
1202 if (info->mti_dlm_req != NULL)
1203 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1205 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1206 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1207 req_capsule_client_get(info->mti_pill,
1210 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1211 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1212 req_capsule_client_get(info->mti_pill,
1215 o1 = info->mti_object;
1216 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1217 &info->mti_body->fid2);
1219 GOTO(out, rc = PTR_ERR(o));
1221 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1222 GOTO(put, rc = -ENOENT);
1224 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1225 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1231 /* permission check. Make sure the calling process having permission
1232 * to write both files. */
1233 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1238 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1243 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1245 GOTO(put, rc = -EPROTO);
1247 lh1 = &info->mti_lh[MDT_LH_NEW];
1248 mdt_lock_reg_init(lh1, LCK_EX);
1249 lh2 = &info->mti_lh[MDT_LH_OLD];
1250 mdt_lock_reg_init(lh2, LCK_EX);
1252 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1253 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1257 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1258 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1262 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1263 mdt_object_child(o2), msl->msl_flags);
1266 mdt_object_unlock(info, o2, lh2, rc);
1268 mdt_object_unlock(info, o1, lh1, rc);
1270 mdt_object_put(info->mti_env, o);
1272 mdt_thread_info_fini(info);
1276 static int mdt_raw_lookup(struct mdt_thread_info *info,
1277 struct mdt_object *parent,
1278 const struct lu_name *lname,
1279 struct ldlm_reply *ldlm_rep)
1281 struct md_object *next = mdt_object_child(info->mti_object);
1282 const struct mdt_body *reqbody = info->mti_body;
1283 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1284 struct mdt_body *repbody;
1288 if (reqbody->valid != OBD_MD_FLID)
1291 LASSERT(!info->mti_cross_ref);
1293 /* Only got the fid of this obj by name */
1294 fid_zero(child_fid);
1295 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1298 /* XXX is raw_lookup possible as intent operation? */
1301 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1304 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1306 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1309 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1310 repbody->fid1 = *child_fid;
1311 repbody->valid = OBD_MD_FLID;
1317 * UPDATE lock should be taken against parent, and be release before exit;
1318 * child_bits lock should be taken against child, and be returned back:
1319 * (1)normal request should release the child lock;
1320 * (2)intent request will grant the lock to client.
1322 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1323 struct mdt_lock_handle *lhc,
1325 struct ldlm_reply *ldlm_rep)
1327 struct ptlrpc_request *req = mdt_info_req(info);
1328 struct mdt_body *reqbody = NULL;
1329 struct mdt_object *parent = info->mti_object;
1330 struct mdt_object *child;
1331 struct md_object *next = mdt_object_child(parent);
1332 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1333 struct lu_name *lname = NULL;
1334 struct mdt_lock_handle *lhp = NULL;
1335 struct ldlm_lock *lock;
1342 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1343 LASSERT(ergo(is_resent,
1344 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1346 LASSERT(parent != NULL);
1348 if (info->mti_cross_ref) {
1349 /* Only getattr on the child. Parent is on another node. */
1350 mdt_set_disposition(info, ldlm_rep,
1351 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1353 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1355 PFID(mdt_object_fid(child)), ldlm_rep);
1358 /* Do not take lock for resent case. */
1359 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1360 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1361 lhc->mlh_reg_lh.cookie);
1362 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1363 &lock->l_resource->lr_name));
1364 LDLM_LOCK_PUT(lock);
1366 mdt_lock_handle_init(lhc);
1367 mdt_lock_reg_init(lhc, LCK_PR);
1370 * Object's name is on another MDS, no lookup or layout
1371 * lock is needed here but update lock is.
1373 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1374 MDS_INODELOCK_LAYOUT);
1375 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1377 rc = mdt_object_lock(info, child, lhc, child_bits,
1383 /* Finally, we can get attr for child. */
1384 if (!mdt_object_exists(child)) {
1385 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1387 "remote object doesn't exist.\n");
1388 mdt_object_unlock(info, child, lhc, 1);
1392 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1393 rc = mdt_getattr_internal(info, child, 0);
1394 if (unlikely(rc != 0))
1395 mdt_object_unlock(info, child, lhc, 1);
1400 lname = &info->mti_name;
1401 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1403 if (lu_name_is_valid(lname)) {
1404 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1405 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1406 PNAME(lname), ldlm_rep);
1408 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1409 if (unlikely(reqbody == NULL))
1410 RETURN(err_serious(-EPROTO));
1412 *child_fid = reqbody->fid2;
1414 if (unlikely(!fid_is_sane(child_fid)))
1415 RETURN(err_serious(-EINVAL));
1417 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1419 PFID(mdt_object_fid(parent)),
1420 PFID(&reqbody->fid2), ldlm_rep);
1423 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1425 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1426 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1428 "Parent doesn't exist!\n");
1432 if (mdt_object_remote(parent)) {
1433 CERROR("%s: parent "DFID" is on remote target\n",
1434 mdt_obd_name(info->mti_mdt),
1435 PFID(mdt_object_fid(parent)));
1439 if (lu_name_is_valid(lname)) {
1440 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1447 /* step 1: lock parent only if parent is a directory */
1448 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1449 lhp = &info->mti_lh[MDT_LH_PARENT];
1450 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1451 rc = mdt_object_lock(info, parent, lhp,
1452 MDS_INODELOCK_UPDATE,
1454 if (unlikely(rc != 0))
1458 /* step 2: lookup child's fid by name */
1459 fid_zero(child_fid);
1460 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1463 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1466 GOTO(out_parent, rc);
1469 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1472 *step 3: find the child object by fid & lock it.
1473 * regardless if it is local or remote.
1475 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1476 * set parent dir fid the same as child fid in getattr by fid case
1477 * we should not lu_object_find() the object again, could lead
1478 * to hung if there is a concurrent unlink destroyed the object.
1480 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1481 mdt_object_get(info->mti_env, parent);
1484 child = mdt_object_find(info->mti_env, info->mti_mdt,
1488 if (unlikely(IS_ERR(child)))
1489 GOTO(out_parent, rc = PTR_ERR(child));
1491 /* Do not take lock for resent case. */
1492 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1493 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1494 lhc->mlh_reg_lh.cookie);
1496 if (!fid_res_name_eq(mdt_object_fid(child),
1497 &lock->l_resource->lr_name)) {
1498 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1499 &lock->l_resource->lr_name),
1500 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1501 PLDLMRES(lock->l_resource),
1502 PFID(mdt_object_fid(parent)));
1503 CWARN("Although resent, but still not get child lock"
1504 "parent:"DFID" child:"DFID"\n",
1505 PFID(mdt_object_fid(parent)),
1506 PFID(mdt_object_fid(child)));
1507 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1508 LDLM_LOCK_PUT(lock);
1511 LDLM_LOCK_PUT(lock);
1514 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1515 mdt_lock_handle_init(lhc);
1516 mdt_lock_reg_init(lhc, LCK_PR);
1519 if (!mdt_object_exists(child)) {
1520 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1522 "Object doesn't exist!\n");
1523 GOTO(out_child, rc = -ENOENT);
1526 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1527 mdt_object_exists(child) && !mdt_object_remote(child)) {
1528 struct md_attr *ma = &info->mti_attr;
1531 ma->ma_need = MA_INODE;
1532 rc = mdt_attr_get_complex(info, child, ma);
1533 if (unlikely(rc != 0))
1534 GOTO(out_child, rc);
1536 /* If the file has not been changed for some time, we
1537 * return not only a LOOKUP lock, but also an UPDATE
1538 * lock and this might save us RPC on later STAT. For
1539 * directories, it also let negative dentry cache start
1540 * working for this dir. */
1541 if (ma->ma_valid & MA_INODE &&
1542 ma->ma_attr.la_valid & LA_CTIME &&
1543 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1544 ma->ma_attr.la_ctime < cfs_time_current_sec())
1545 child_bits |= MDS_INODELOCK_UPDATE;
1548 /* layout lock must be granted in a best-effort way
1549 * for IT operations */
1550 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1551 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1552 exp_connect_layout(info->mti_exp) &&
1553 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1554 !mdt_object_remote(child) && ldlm_rep != NULL) {
1555 /* try to grant layout lock for regular file. */
1561 child_bits |= MDS_INODELOCK_LAYOUT;
1562 /* try layout lock, it may fail to be granted due to
1563 * contention at LOOKUP or UPDATE */
1564 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1566 child_bits &= ~MDS_INODELOCK_LAYOUT;
1567 LASSERT(child_bits != 0);
1568 rc = mdt_object_lock(info, child, lhc,
1569 child_bits, MDT_CROSS_LOCK);
1574 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1575 * client will enqueue the lock to the remote MDT */
1576 if (mdt_object_remote(child))
1577 child_bits &= ~MDS_INODELOCK_UPDATE;
1578 rc = mdt_object_lock(info, child, lhc, child_bits,
1581 if (unlikely(rc != 0))
1582 GOTO(out_child, rc);
1585 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1586 /* Get MA_SOM attributes if update lock is given. */
1588 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1589 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1592 /* finally, we can get attr for child. */
1593 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1594 rc = mdt_getattr_internal(info, child, ma_need);
1595 if (unlikely(rc != 0)) {
1596 mdt_object_unlock(info, child, lhc, 1);
1598 /* Debugging code. */
1599 LDLM_DEBUG(lock, "Returning lock to client");
1600 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1601 &lock->l_resource->lr_name),
1602 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1603 PLDLMRES(lock->l_resource),
1604 PFID(mdt_object_fid(child)));
1605 if (mdt_object_exists(child) && !mdt_object_remote(child))
1606 mdt_pack_size2body(info, child);
1609 LDLM_LOCK_PUT(lock);
1613 mdt_object_put(info->mti_env, child);
1616 mdt_object_unlock(info, parent, lhp, 1);
1620 /* normal handler: should release the child lock */
1621 static int mdt_getattr_name(struct tgt_session_info *tsi)
1623 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1624 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1625 struct mdt_body *reqbody;
1626 struct mdt_body *repbody;
1630 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1631 LASSERT(reqbody != NULL);
1632 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1633 LASSERT(repbody != NULL);
1635 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1636 repbody->eadatasize = 0;
1637 repbody->aclsize = 0;
1639 rc = mdt_init_ucred(info, reqbody);
1641 GOTO(out_shrink, rc);
1643 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1644 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1645 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1646 lhc->mlh_reg_lh.cookie = 0;
1648 mdt_exit_ucred(info);
1651 mdt_client_compatibility(info);
1652 rc2 = mdt_fix_reply(info);
1655 mdt_thread_info_fini(info);
1659 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1660 void *karg, void *uarg);
1662 static int mdt_set_info(struct tgt_session_info *tsi)
1664 struct ptlrpc_request *req = tgt_ses_req(tsi);
1667 int keylen, vallen, rc = 0;
1671 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1673 DEBUG_REQ(D_HA, req, "no set_info key");
1674 RETURN(err_serious(-EFAULT));
1677 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1680 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1682 DEBUG_REQ(D_HA, req, "no set_info val");
1683 RETURN(err_serious(-EFAULT));
1686 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1689 /* Swab any part of val you need to here */
1690 if (KEY_IS(KEY_READ_ONLY)) {
1691 spin_lock(&req->rq_export->exp_lock);
1693 *exp_connect_flags_ptr(req->rq_export) |=
1696 *exp_connect_flags_ptr(req->rq_export) &=
1697 ~OBD_CONNECT_RDONLY;
1698 spin_unlock(&req->rq_export->exp_lock);
1699 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1700 struct changelog_setinfo *cs = val;
1702 if (vallen != sizeof(*cs)) {
1703 CERROR("%s: bad changelog_clear setinfo size %d\n",
1704 tgt_name(tsi->tsi_tgt), vallen);
1707 if (ptlrpc_req_need_swab(req)) {
1708 __swab64s(&cs->cs_recno);
1709 __swab32s(&cs->cs_id);
1712 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1720 static int mdt_readpage(struct tgt_session_info *tsi)
1722 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1723 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1724 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1725 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1726 struct mdt_body *repbody;
1732 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1733 RETURN(err_serious(-ENOMEM));
1735 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1736 if (repbody == NULL || reqbody == NULL)
1737 RETURN(err_serious(-EFAULT));
1740 * prepare @rdpg before calling lower layers and transfer itself. Here
1741 * reqbody->size contains offset of where to start to read and
1742 * reqbody->nlink contains number bytes to read.
1744 rdpg->rp_hash = reqbody->size;
1745 if (rdpg->rp_hash != reqbody->size) {
1746 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1747 rdpg->rp_hash, reqbody->size);
1751 rdpg->rp_attrs = reqbody->mode;
1752 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1753 rdpg->rp_attrs |= LUDA_64BITHASH;
1754 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1755 exp_max_brw_size(tsi->tsi_exp));
1756 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1758 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1759 if (rdpg->rp_pages == NULL)
1762 for (i = 0; i < rdpg->rp_npages; ++i) {
1763 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1764 if (rdpg->rp_pages[i] == NULL)
1765 GOTO(free_rdpg, rc = -ENOMEM);
1768 /* call lower layers to fill allocated pages with directory data */
1769 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1771 GOTO(free_rdpg, rc);
1773 /* send pages to client */
1774 rc = tgt_sendpage(tsi, rdpg, rc);
1779 for (i = 0; i < rdpg->rp_npages; i++)
1780 if (rdpg->rp_pages[i] != NULL)
1781 __free_page(rdpg->rp_pages[i]);
1782 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1784 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1790 static int mdt_reint_internal(struct mdt_thread_info *info,
1791 struct mdt_lock_handle *lhc,
1794 struct req_capsule *pill = info->mti_pill;
1795 struct mdt_body *repbody;
1800 rc = mdt_reint_unpack(info, op);
1802 CERROR("Can't unpack reint, rc %d\n", rc);
1803 RETURN(err_serious(rc));
1806 /* for replay (no_create) lmm is not needed, client has it already */
1807 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1808 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1811 /* llog cookies are always 0, the field is kept for compatibility */
1812 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1813 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1815 rc = req_capsule_server_pack(pill);
1817 CERROR("Can't pack response, rc %d\n", rc);
1818 RETURN(err_serious(rc));
1821 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1822 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1824 repbody->eadatasize = 0;
1825 repbody->aclsize = 0;
1828 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1830 /* for replay no cookkie / lmm need, because client have this already */
1831 if (info->mti_spec.no_create)
1832 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1833 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1835 rc = mdt_init_ucred_reint(info);
1837 GOTO(out_shrink, rc);
1839 rc = mdt_fix_attr_ucred(info, op);
1841 GOTO(out_ucred, rc = err_serious(rc));
1843 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1844 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1845 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1846 GOTO(out_ucred, rc);
1848 rc = mdt_reint_rec(info, lhc);
1851 mdt_exit_ucred(info);
1853 mdt_client_compatibility(info);
1854 rc2 = mdt_fix_reply(info);
1860 static long mdt_reint_opcode(struct ptlrpc_request *req,
1861 const struct req_format **fmt)
1863 struct mdt_device *mdt;
1864 struct mdt_rec_reint *rec;
1867 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1869 opc = rec->rr_opcode;
1870 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1871 if (opc < REINT_MAX && fmt[opc] != NULL)
1872 req_capsule_extend(&req->rq_pill, fmt[opc]);
1874 mdt = mdt_exp2dev(req->rq_export);
1875 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1876 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1877 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1878 opc = err_serious(-EFAULT);
1881 opc = err_serious(-EFAULT);
1886 static int mdt_reint(struct tgt_session_info *tsi)
1890 static const struct req_format *reint_fmts[REINT_MAX] = {
1891 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1892 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1893 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1894 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1895 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1896 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1897 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1898 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1899 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1904 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1906 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1908 * No lock possible here from client to pass it to reint code
1911 rc = mdt_reint_internal(info, NULL, opc);
1912 mdt_thread_info_fini(info);
1917 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1921 /* this should sync the whole device */
1922 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1924 struct dt_device *dt = mdt->mdt_bottom;
1928 rc = dt->dd_ops->dt_sync(env, dt);
1932 /* this should sync this object */
1933 static int mdt_object_sync(struct mdt_thread_info *info)
1935 struct md_object *next;
1939 if (!mdt_object_exists(info->mti_object)) {
1940 CWARN("Non existing object "DFID"!\n",
1941 PFID(mdt_object_fid(info->mti_object)));
1944 next = mdt_object_child(info->mti_object);
1945 rc = mo_object_sync(info->mti_env, next);
1950 static int mdt_sync(struct tgt_session_info *tsi)
1952 struct ptlrpc_request *req = tgt_ses_req(tsi);
1953 struct req_capsule *pill = tsi->tsi_pill;
1954 struct mdt_body *body;
1959 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1960 RETURN(err_serious(-ENOMEM));
1962 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1963 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1965 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1967 /* sync an object */
1968 rc = mdt_object_sync(info);
1970 const struct lu_fid *fid;
1971 struct lu_attr *la = &info->mti_attr.ma_attr;
1973 info->mti_attr.ma_need = MA_INODE;
1974 info->mti_attr.ma_valid = 0;
1975 rc = mdt_attr_get_complex(info, info->mti_object,
1978 body = req_capsule_server_get(pill,
1980 fid = mdt_object_fid(info->mti_object);
1981 mdt_pack_attr2body(info, body, la, fid);
1984 mdt_thread_info_fini(info);
1987 mdt_counter_incr(req, LPROC_MDT_SYNC);
1993 * Handle quota control requests to consult current usage/limit, but also
1994 * to configure quota enforcement
1996 static int mdt_quotactl(struct tgt_session_info *tsi)
1998 struct obd_export *exp = tsi->tsi_exp;
1999 struct req_capsule *pill = tsi->tsi_pill;
2000 struct obd_quotactl *oqctl, *repoqc;
2002 struct mdt_device *mdt = mdt_exp2dev(exp);
2003 struct lu_device *qmt = mdt->mdt_qmt_dev;
2006 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2008 RETURN(err_serious(-EPROTO));
2010 rc = req_capsule_server_pack(pill);
2012 RETURN(err_serious(rc));
2014 switch (oqctl->qc_cmd) {
2016 case LUSTRE_Q_INVALIDATE:
2017 case LUSTRE_Q_FINVALIDATE:
2021 /* deprecated, not used any more */
2022 RETURN(-EOPNOTSUPP);
2023 /* master quotactl */
2029 RETURN(-EOPNOTSUPP);
2030 /* slave quotactl */
2035 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2039 /* map uid/gid for remote client */
2041 if (exp_connect_rmtclient(exp)) {
2042 struct lustre_idmap_table *idmap;
2044 idmap = exp->exp_mdt_data.med_idmap;
2046 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2047 oqctl->qc_cmd != Q_GETINFO))
2050 if (oqctl->qc_type == USRQUOTA)
2051 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2053 else if (oqctl->qc_type == GRPQUOTA)
2054 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2059 if (id == CFS_IDMAP_NOTFOUND) {
2060 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2065 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2067 RETURN(err_serious(-EFAULT));
2069 if (oqctl->qc_id != id)
2070 swap(oqctl->qc_id, id);
2072 switch (oqctl->qc_cmd) {
2078 /* forward quotactl request to QMT */
2079 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2084 /* slave quotactl */
2085 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2090 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2094 if (oqctl->qc_id != id)
2095 swap(oqctl->qc_id, id);
2101 /** clone llog ctxt from child (mdd)
2102 * This allows remote llog (replicator) access.
2103 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2104 * context was originally set up, or we can handle them directly.
2105 * I choose the latter, but that means I need any llog
2106 * contexts set up by child to be accessable by the mdt. So we clone the
2107 * context into our context list here.
2109 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2112 struct md_device *next = mdt->mdt_child;
2113 struct llog_ctxt *ctxt;
2116 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2119 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2120 if (rc || ctxt == NULL) {
2124 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2126 CERROR("Can't set mdt ctxt %d\n", rc);
2131 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2132 struct mdt_device *mdt, int idx)
2134 struct llog_ctxt *ctxt;
2136 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2139 /* Put once for the get we just did, and once for the clone */
2140 llog_ctxt_put(ctxt);
2141 llog_ctxt_put(ctxt);
2146 * sec context handlers
2148 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2152 rc = mdt_handle_idmap(tsi);
2154 struct ptlrpc_request *req = tgt_ses_req(tsi);
2157 opc = lustre_msg_get_opc(req->rq_reqmsg);
2158 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2159 sptlrpc_svc_ctx_invalidate(req);
2162 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2168 * quota request handlers
2170 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2172 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2173 struct lu_device *qmt = mdt->mdt_qmt_dev;
2178 RETURN(err_serious(-EOPNOTSUPP));
2180 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2184 struct mdt_object *mdt_object_new(const struct lu_env *env,
2185 struct mdt_device *d,
2186 const struct lu_fid *f)
2188 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2189 struct lu_object *o;
2190 struct mdt_object *m;
2193 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2194 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2195 if (unlikely(IS_ERR(o)))
2196 m = (struct mdt_object *)o;
2202 struct mdt_object *mdt_object_find(const struct lu_env *env,
2203 struct mdt_device *d,
2204 const struct lu_fid *f)
2206 struct lu_object *o;
2207 struct mdt_object *m;
2210 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2211 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2212 if (unlikely(IS_ERR(o)))
2213 m = (struct mdt_object *)o;
2221 * Asyncronous commit for mdt device.
2223 * Pass asynchonous commit call down the MDS stack.
2225 * \param env environment
2226 * \param mdt the mdt device
2228 static void mdt_device_commit_async(const struct lu_env *env,
2229 struct mdt_device *mdt)
2231 struct dt_device *dt = mdt->mdt_bottom;
2234 rc = dt->dd_ops->dt_commit_async(env, dt);
2235 if (unlikely(rc != 0))
2236 CWARN("async commit start failed with rc = %d", rc);
2240 * Mark the lock as "synchonous".
2242 * Mark the lock to deffer transaction commit to the unlock time.
2244 * \param lock the lock to mark as "synchonous"
2246 * \see mdt_is_lock_sync
2247 * \see mdt_save_lock
2249 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2251 lock->l_ast_data = (void*)1;
2255 * Check whehter the lock "synchonous" or not.
2257 * \param lock the lock to check
2258 * \retval 1 the lock is "synchonous"
2259 * \retval 0 the lock isn't "synchronous"
2261 * \see mdt_set_lock_sync
2262 * \see mdt_save_lock
2264 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2266 return lock->l_ast_data != NULL;
2270 * Blocking AST for mdt locks.
2272 * Starts transaction commit if in case of COS lock conflict or
2273 * deffers such a commit to the mdt_save_lock.
2275 * \param lock the lock which blocks a request or cancelling lock
2276 * \param desc unused
2277 * \param data unused
2278 * \param flag indicates whether this cancelling or blocking callback
2280 * \see ldlm_blocking_ast_nocheck
2282 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2283 void *data, int flag)
2285 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2286 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2290 if (flag == LDLM_CB_CANCELING)
2292 lock_res_and_lock(lock);
2293 if (lock->l_blocking_ast != mdt_blocking_ast) {
2294 unlock_res_and_lock(lock);
2297 if (mdt_cos_is_enabled(mdt) &&
2298 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2299 lock->l_blocking_lock != NULL &&
2300 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2301 mdt_set_lock_sync(lock);
2303 rc = ldlm_blocking_ast_nocheck(lock);
2305 /* There is no lock conflict if l_blocking_lock == NULL,
2306 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2307 * when the last reference to a local lock was released */
2308 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2311 rc = lu_env_init(&env, LCT_LOCAL);
2312 if (unlikely(rc != 0))
2313 CWARN("lu_env initialization failed with rc = %d,"
2314 "cannot start asynchronous commit\n", rc);
2316 mdt_device_commit_async(&env, mdt);
2322 /* Used for cross-MDT lock */
2323 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2324 void *data, int flag)
2326 struct lustre_handle lockh;
2330 case LDLM_CB_BLOCKING:
2331 ldlm_lock2handle(lock, &lockh);
2332 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2334 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2338 case LDLM_CB_CANCELING:
2339 LDLM_DEBUG(lock, "Revoke remote lock\n");
2347 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2348 struct mdt_object *o, const struct lu_fid *fid,
2349 struct lustre_handle *lh, ldlm_mode_t mode,
2352 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2353 ldlm_policy_data_t *policy = &mti->mti_policy;
2354 struct ldlm_res_id *res_id = &mti->mti_res_id;
2358 LASSERT(mdt_object_remote(o));
2360 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2362 fid_build_reg_res_name(fid, res_id);
2364 memset(einfo, 0, sizeof(*einfo));
2365 einfo->ei_type = LDLM_IBITS;
2366 einfo->ei_mode = mode;
2367 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2368 einfo->ei_cb_cp = ldlm_completion_ast;
2369 einfo->ei_enq_slave = 0;
2370 einfo->ei_res_id = res_id;
2372 memset(policy, 0, sizeof(*policy));
2373 policy->l_inodebits.bits = ibits;
2375 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2380 static int mdt_object_local_lock(struct mdt_thread_info *info,
2381 struct mdt_object *o,
2382 struct mdt_lock_handle *lh, __u64 ibits,
2383 bool nonblock, int locality)
2385 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2386 ldlm_policy_data_t *policy = &info->mti_policy;
2387 struct ldlm_res_id *res_id = &info->mti_res_id;
2392 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2393 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2394 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2395 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2397 /* Only enqueue LOOKUP lock for remote object */
2398 if (mdt_object_remote(o))
2399 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2401 if (lh->mlh_type == MDT_PDO_LOCK) {
2402 /* check for exists after object is locked */
2403 if (mdt_object_exists(o) == 0) {
2404 /* Non-existent object shouldn't have PDO lock */
2407 /* Non-dir object shouldn't have PDO lock */
2408 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2413 memset(policy, 0, sizeof(*policy));
2414 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2416 dlmflags = LDLM_FL_ATOMIC_CB;
2418 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2421 * Take PDO lock on whole directory and build correct @res_id for lock
2422 * on part of directory.
2424 if (lh->mlh_pdo_hash != 0) {
2425 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2426 mdt_lock_pdo_mode(info, o, lh);
2427 if (lh->mlh_pdo_mode != LCK_NL) {
2429 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2430 * is never going to be sent to client and we do not
2431 * want it slowed down due to possible cancels.
2433 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2434 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2435 policy, res_id, dlmflags,
2436 info->mti_exp == NULL ? NULL :
2437 &info->mti_exp->exp_handle.h_cookie);
2443 * Finish res_id initializing by name hash marking part of
2444 * directory which is taking modification.
2446 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2449 policy->l_inodebits.bits = ibits;
2452 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2453 * going to be sent to client. If it is - mdt_intent_policy() path will
2454 * fix it up and turn FL_LOCAL flag off.
2456 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2457 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2458 info->mti_exp == NULL ? NULL :
2459 &info->mti_exp->exp_handle.h_cookie);
2461 mdt_object_unlock(info, o, lh, 1);
2462 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2463 lh->mlh_pdo_hash != 0 &&
2464 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2465 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2472 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2473 struct mdt_lock_handle *lh, __u64 ibits,
2474 bool nonblock, int locality)
2479 if (!mdt_object_remote(o))
2480 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2483 if (locality == MDT_LOCAL_LOCK) {
2484 CERROR("%s: try to get local lock for remote object"
2485 DFID".\n", mdt_obd_name(info->mti_mdt),
2486 PFID(mdt_object_fid(o)));
2490 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2491 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2492 MDS_INODELOCK_XATTR);
2493 if (ibits & MDS_INODELOCK_UPDATE) {
2494 /* Sigh, PDO needs to enqueue 2 locks right now, but
2495 * enqueue RPC can only request 1 lock, to avoid extra
2496 * RPC, so it will instead enqueue EX lock for remote
2497 * object anyway XXX*/
2498 if (lh->mlh_type == MDT_PDO_LOCK &&
2499 lh->mlh_pdo_hash != 0) {
2500 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2501 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2502 PFID(mdt_object_fid(o)));
2503 lh->mlh_pdo_hash = 0;
2504 lh->mlh_rreg_mode = LCK_EX;
2505 lh->mlh_type = MDT_REG_LOCK;
2507 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2510 MDS_INODELOCK_UPDATE);
2515 /* Only enqueue LOOKUP lock for remote object */
2516 if (ibits & MDS_INODELOCK_LOOKUP) {
2517 rc = mdt_object_local_lock(info, o, lh,
2518 MDS_INODELOCK_LOOKUP,
2519 nonblock, locality);
2527 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2528 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2530 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2533 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2534 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2536 struct mdt_lock_handle tmp = *lh;
2539 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2547 * Save a lock within request object.
2549 * Keep the lock referenced until whether client ACK or transaction
2550 * commit happens or release the lock immediately depending on input
2551 * parameters. If COS is ON, a write lock is converted to COS lock
2554 * \param info thead info object
2555 * \param h lock handle
2556 * \param mode lock mode
2557 * \param decref force immediate lock releasing
2560 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2561 ldlm_mode_t mode, int decref)
2565 if (lustre_handle_is_used(h)) {
2566 if (decref || !info->mti_has_trans ||
2567 !(mode & (LCK_PW | LCK_EX))){
2568 mdt_fid_unlock(h, mode);
2570 struct mdt_device *mdt = info->mti_mdt;
2571 struct ldlm_lock *lock = ldlm_handle2lock(h);
2572 struct ptlrpc_request *req = mdt_info_req(info);
2575 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2577 /* there is no request if mdt_object_unlock() is called
2578 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2579 if (likely(req != NULL)) {
2580 CDEBUG(D_HA, "request = %p reply state = %p"
2581 " transno = "LPD64"\n", req,
2582 req->rq_reply_state, req->rq_transno);
2583 if (mdt_cos_is_enabled(mdt)) {
2585 ldlm_lock_downgrade(lock, LCK_COS);
2588 ptlrpc_save_lock(req, h, mode, no_ack);
2590 ldlm_lock_decref(h, mode);
2592 if (mdt_is_lock_sync(lock)) {
2593 CDEBUG(D_HA, "found sync-lock,"
2594 " async commit started\n");
2595 mdt_device_commit_async(info->mti_env,
2598 LDLM_LOCK_PUT(lock);
2607 * Unlock mdt object.
2609 * Immeditely release the regular lock and the PDO lock or save the
2610 * lock in reqeuest and keep them referenced until client ACK or
2611 * transaction commit.
2613 * \param info thread info object
2614 * \param o mdt object
2615 * \param lh mdt lock handle referencing regular and PDO locks
2616 * \param decref force immediate lock releasing
2618 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2619 struct mdt_lock_handle *lh, int decref)
2623 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2624 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2626 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2627 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2632 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2633 const struct lu_fid *f,
2634 struct mdt_lock_handle *lh,
2637 struct mdt_object *o;
2639 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2643 rc = mdt_object_lock(info, o, lh, ibits,
2646 mdt_object_put(info->mti_env, o);
2653 void mdt_object_unlock_put(struct mdt_thread_info * info,
2654 struct mdt_object * o,
2655 struct mdt_lock_handle *lh,
2658 mdt_object_unlock(info, o, lh, decref);
2659 mdt_object_put(info->mti_env, o);
2663 * Generic code handling requests that have struct mdt_body passed in:
2665 * - extract mdt_body from request and save it in @info, if present;
2667 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2670 * - if HABEO_CORPUS flag is set for this request type check whether object
2671 * actually exists on storage (lu_object_exists()).
2674 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2676 const struct mdt_body *body;
2677 struct mdt_object *obj;
2678 const struct lu_env *env;
2679 struct req_capsule *pill;
2683 env = info->mti_env;
2684 pill = info->mti_pill;
2686 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2690 if (!(body->valid & OBD_MD_FLID))
2693 if (!fid_is_sane(&body->fid1)) {
2694 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2699 * Do not get size or any capa fields before we check that request
2700 * contains capa actually. There are some requests which do not, for
2701 * instance MDS_IS_SUBDIR.
2703 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2704 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2705 mdt_set_capainfo(info, 0, &body->fid1,
2706 req_capsule_client_get(pill, &RMF_CAPA1));
2708 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2710 if ((flags & HABEO_CORPUS) &&
2711 !mdt_object_exists(obj)) {
2712 mdt_object_put(env, obj);
2713 /* for capability renew ENOENT will be handled in
2715 if (body->valid & OBD_MD_FLOSSCAPA)
2720 info->mti_object = obj;
2729 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2731 struct req_capsule *pill = info->mti_pill;
2735 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2736 rc = mdt_body_unpack(info, flags);
2740 if (rc == 0 && (flags & HABEO_REFERO)) {
2742 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2743 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2745 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2746 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2749 rc = req_capsule_server_pack(pill);
2754 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2756 struct md_device *next = m->mdt_child;
2758 return next->md_ops->mdo_init_capa_ctxt(env, next,
2759 m->mdt_lut.lut_mds_capa,
2760 m->mdt_capa_timeout,
2765 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2767 lh->mlh_type = MDT_NUL_LOCK;
2768 lh->mlh_reg_lh.cookie = 0ull;
2769 lh->mlh_reg_mode = LCK_MINMODE;
2770 lh->mlh_pdo_lh.cookie = 0ull;
2771 lh->mlh_pdo_mode = LCK_MINMODE;
2772 lh->mlh_rreg_lh.cookie = 0ull;
2773 lh->mlh_rreg_mode = LCK_MINMODE;
2776 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2778 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2779 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2783 * Initialize fields of struct mdt_thread_info. Other fields are left in
2784 * uninitialized state, because it's too expensive to zero out whole
2785 * mdt_thread_info (> 1K) on each request arrival.
2787 void mdt_thread_info_init(struct ptlrpc_request *req,
2788 struct mdt_thread_info *info)
2792 info->mti_pill = &req->rq_pill;
2795 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2796 mdt_lock_handle_init(&info->mti_lh[i]);
2798 /* mdt device: it can be NULL while CONNECT */
2799 if (req->rq_export) {
2800 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2801 info->mti_exp = req->rq_export;
2803 info->mti_mdt = NULL;
2804 info->mti_env = req->rq_svc_thread->t_env;
2805 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2807 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2808 info->mti_big_buf = LU_BUF_NULL;
2809 info->mti_body = NULL;
2810 info->mti_object = NULL;
2811 info->mti_dlm_req = NULL;
2812 info->mti_has_trans = 0;
2813 info->mti_cross_ref = 0;
2814 info->mti_opdata = 0;
2815 info->mti_big_lmm_used = 0;
2817 info->mti_spec.no_create = 0;
2818 info->mti_spec.sp_rm_entry = 0;
2820 info->mti_spec.u.sp_ea.eadata = NULL;
2821 info->mti_spec.u.sp_ea.eadatalen = 0;
2824 void mdt_thread_info_fini(struct mdt_thread_info *info)
2828 if (info->mti_object != NULL) {
2829 mdt_object_put(info->mti_env, info->mti_object);
2830 info->mti_object = NULL;
2833 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2834 mdt_lock_handle_fini(&info->mti_lh[i]);
2835 info->mti_env = NULL;
2836 info->mti_pill = NULL;
2837 info->mti_exp = NULL;
2839 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2840 lu_buf_free(&info->mti_big_buf);
2843 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2845 struct mdt_thread_info *mti;
2846 struct lustre_capa *lc;
2848 mti = mdt_th_info(tsi->tsi_env);
2849 LASSERT(mti != NULL);
2851 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2852 if (tsi->tsi_corpus != NULL) {
2853 struct req_capsule *pill = tsi->tsi_pill;
2855 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2856 lu_object_get(tsi->tsi_corpus);
2859 * XXX: must be part of tgt_mdt_body_unpack but moved here
2860 * due to mdt_set_capainfo().
2862 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2863 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2864 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2865 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2868 mti->mti_body = tsi->tsi_mdt_body;
2869 mti->mti_dlm_req = tsi->tsi_dlm_req;
2874 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2876 struct ptlrpc_request *req = tgt_ses_req(tsi);
2881 rc = tgt_connect(tsi);
2885 rc = mdt_init_idmap(tsi);
2890 obd_disconnect(class_export_get(req->rq_export));
2909 static int mdt_intent_getattr(enum mdt_it_code opcode,
2910 struct mdt_thread_info *info,
2911 struct ldlm_lock **,
2914 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2915 struct mdt_thread_info *info,
2916 struct ldlm_lock **lockp,
2919 static int mdt_intent_layout(enum mdt_it_code opcode,
2920 struct mdt_thread_info *info,
2921 struct ldlm_lock **,
2923 static int mdt_intent_reint(enum mdt_it_code opcode,
2924 struct mdt_thread_info *info,
2925 struct ldlm_lock **,
2928 static struct mdt_it_flavor {
2929 const struct req_format *it_fmt;
2931 int (*it_act)(enum mdt_it_code ,
2932 struct mdt_thread_info *,
2933 struct ldlm_lock **,
2936 } mdt_it_flavor[] = {
2938 .it_fmt = &RQF_LDLM_INTENT,
2939 /*.it_flags = HABEO_REFERO,*/
2941 .it_act = mdt_intent_reint,
2942 .it_reint = REINT_OPEN
2945 .it_fmt = &RQF_LDLM_INTENT,
2947 * OCREAT is not a MUTABOR request as if the file
2949 * We do the extra check of OBD_CONNECT_RDONLY in
2950 * mdt_reint_open() when we really need to create
2954 .it_act = mdt_intent_reint,
2955 .it_reint = REINT_OPEN
2958 .it_fmt = &RQF_LDLM_INTENT,
2959 .it_flags = MUTABOR,
2960 .it_act = mdt_intent_reint,
2961 .it_reint = REINT_CREATE
2963 [MDT_IT_GETATTR] = {
2964 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2965 .it_flags = HABEO_REFERO,
2966 .it_act = mdt_intent_getattr
2968 [MDT_IT_READDIR] = {
2974 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2975 .it_flags = HABEO_REFERO,
2976 .it_act = mdt_intent_getattr
2979 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2980 .it_flags = MUTABOR,
2982 .it_reint = REINT_UNLINK
2986 .it_flags = MUTABOR,
2989 [MDT_IT_GETXATTR] = {
2990 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2991 .it_flags = HABEO_CORPUS,
2992 .it_act = mdt_intent_getxattr
2995 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2997 .it_act = mdt_intent_layout
3002 mdt_intent_lock_replace(struct mdt_thread_info *info, struct ldlm_lock **lockp,
3003 struct ldlm_lock *new_lock, struct mdt_lock_handle *lh,
3006 struct ptlrpc_request *req = mdt_info_req(info);
3007 struct ldlm_lock *lock = *lockp;
3010 * Get new lock only for cases when possible resent did not find any
3013 if (new_lock == NULL)
3014 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3016 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3017 lh->mlh_reg_lh.cookie = 0;
3021 LASSERTF(new_lock != NULL,
3022 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3025 * If we've already given this lock to a client once, then we should
3026 * have no readers or writers. Otherwise, we should have one reader
3027 * _or_ writer ref (which will be zeroed below) before returning the
3030 if (new_lock->l_export == req->rq_export) {
3031 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3033 LASSERT(new_lock->l_export == NULL);
3034 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3039 if (new_lock->l_export == req->rq_export) {
3041 * Already gave this to the client, which means that we
3042 * reconstructed a reply.
3044 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3046 lh->mlh_reg_lh.cookie = 0;
3047 RETURN(ELDLM_LOCK_REPLACED);
3051 * Fixup the lock to be given to the client.
3053 lock_res_and_lock(new_lock);
3054 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3055 * possible blocking AST. */
3056 while (new_lock->l_readers > 0) {
3057 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3058 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3059 new_lock->l_readers--;
3061 while (new_lock->l_writers > 0) {
3062 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3063 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3064 new_lock->l_writers--;
3067 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3068 new_lock->l_blocking_ast = lock->l_blocking_ast;
3069 new_lock->l_completion_ast = lock->l_completion_ast;
3070 new_lock->l_remote_handle = lock->l_remote_handle;
3071 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3073 unlock_res_and_lock(new_lock);
3075 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3076 &new_lock->l_remote_handle,
3077 &new_lock->l_exp_hash);
3079 LDLM_LOCK_RELEASE(new_lock);
3080 lh->mlh_reg_lh.cookie = 0;
3082 RETURN(ELDLM_LOCK_REPLACED);
3085 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3086 struct ldlm_lock *new_lock,
3087 struct ldlm_lock **old_lock,
3088 struct mdt_lock_handle *lh,
3089 enum mdt_it_code opcode)
3091 struct ptlrpc_request *req = mdt_info_req(info);
3092 struct obd_export *exp = req->rq_export;
3093 struct lustre_handle remote_hdl;
3094 struct ldlm_request *dlmreq;
3095 struct ldlm_lock *lock;
3097 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3100 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3101 remote_hdl = dlmreq->lock_handle[0];
3102 /* If the client does not require open lock, it does not need to
3103 * search lock in exp_lock_hash, since the server thread will
3104 * make sure the lock will be released, and the resend request
3105 * can always re-enqueue the lock */
3106 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3107 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3108 /* In the function below, .hs_keycmp resolves to
3109 * ldlm_export_lock_keycmp() */
3110 /* coverity[overrun-buffer-val] */
3111 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3113 lock_res_and_lock(lock);
3114 if (lock != new_lock) {
3115 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3116 lh->mlh_reg_mode = lock->l_granted_mode;
3118 LDLM_DEBUG(lock, "Restoring lock cookie");
3119 DEBUG_REQ(D_DLMTRACE, req,
3120 "restoring lock cookie "LPX64,
3121 lh->mlh_reg_lh.cookie);
3123 *old_lock = LDLM_LOCK_GET(lock);
3124 cfs_hash_put(exp->exp_lock_hash,
3126 unlock_res_and_lock(lock);
3129 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3130 unlock_res_and_lock(lock);
3134 * If the xid matches, then we know this is a resent request, and allow
3135 * it. (It's probably an OPEN, for which we don't send a lock.
3137 if (req_xid_is_last(req))
3141 * This remote handle isn't enqueued, so we never received or processed
3142 * this request. Clear MSG_RESENT, because it can be handled like any
3143 * normal request now.
3145 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3147 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3151 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3152 struct mdt_thread_info *info,
3153 struct ldlm_lock **lockp,
3156 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3157 struct ldlm_reply *ldlm_rep = NULL;
3161 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3162 * (for the resend case) or a new lock. Below we will use it to
3163 * replace the original lock.
3165 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3166 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3167 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3168 rc = mdt_object_lock(info, info->mti_object, lhc,
3169 MDS_INODELOCK_XATTR,
3175 grc = mdt_getxattr(info);
3177 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3179 if (mdt_info_req(info)->rq_repmsg != NULL)
3180 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3181 if (ldlm_rep == NULL)
3182 RETURN(err_serious(-EFAULT));
3184 ldlm_rep->lock_policy_res2 = grc;
3189 static int mdt_intent_getattr(enum mdt_it_code opcode,
3190 struct mdt_thread_info *info,
3191 struct ldlm_lock **lockp,
3194 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3195 struct ldlm_lock *new_lock = NULL;
3197 struct ldlm_reply *ldlm_rep;
3198 struct mdt_body *reqbody;
3199 struct mdt_body *repbody;
3203 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3206 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3209 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3210 repbody->eadatasize = 0;
3211 repbody->aclsize = 0;
3215 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3217 case MDT_IT_GETATTR:
3218 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3222 CERROR("Unsupported intent (%d)\n", opcode);
3223 GOTO(out_shrink, rc = -EINVAL);
3226 rc = mdt_init_ucred(info, reqbody);
3228 GOTO(out_shrink, rc);
3230 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3231 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3233 /* Get lock from request for possible resent case. */
3234 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3236 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3237 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3239 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3240 ldlm_rep->lock_policy_res2 = 0;
3241 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3242 ldlm_rep->lock_policy_res2) {
3243 lhc->mlh_reg_lh.cookie = 0ull;
3244 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3247 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3250 mdt_exit_ucred(info);
3252 mdt_client_compatibility(info);
3253 rc2 = mdt_fix_reply(info);
3259 static int mdt_intent_layout(enum mdt_it_code opcode,
3260 struct mdt_thread_info *info,
3261 struct ldlm_lock **lockp,
3264 struct layout_intent *layout;
3266 struct mdt_object *obj = NULL;
3270 if (opcode != MDT_IT_LAYOUT) {
3271 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3276 fid = &info->mti_tmp_fid2;
3277 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3279 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3281 RETURN(PTR_ERR(obj));
3283 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3284 /* get the length of lsm */
3285 rc = mdt_attr_get_eabuf_size(info, obj);
3289 if (rc > info->mti_mdt->mdt_max_mdsize)
3290 info->mti_mdt->mdt_max_mdsize = rc;
3293 mdt_object_put(info->mti_env, obj);
3295 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3296 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
3297 rc = req_capsule_server_pack(info->mti_pill);
3301 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3302 LASSERT(layout != NULL);
3303 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3304 /* return to normal ldlm handling */
3307 CERROR("%s: Unsupported layout intent (%d)\n",
3308 mdt_obd_name(info->mti_mdt), layout->li_opc);
3312 static int mdt_intent_reint(enum mdt_it_code opcode,
3313 struct mdt_thread_info *info,
3314 struct ldlm_lock **lockp,
3317 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3318 struct ldlm_reply *rep = NULL;
3322 static const struct req_format *intent_fmts[REINT_MAX] = {
3323 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3324 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3329 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3333 if (mdt_it_flavor[opcode].it_reint != opc) {
3334 CERROR("Reint code %ld doesn't match intent: %d\n",
3336 RETURN(err_serious(-EPROTO));
3339 /* Get lock from request for possible resent case. */
3340 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3342 rc = mdt_reint_internal(info, lhc, opc);
3344 /* Check whether the reply has been packed successfully. */
3345 if (mdt_info_req(info)->rq_repmsg != NULL)
3346 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3348 RETURN(err_serious(-EFAULT));
3350 /* MDC expects this in any case */
3352 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3354 /* the open lock or the lock for cross-ref object should be
3355 * returned to the client */
3356 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3357 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3358 rep->lock_policy_res2 = 0;
3359 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3363 rep->lock_policy_res2 = clear_serious(rc);
3365 if (rep->lock_policy_res2 == -ENOENT &&
3366 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3367 rep->lock_policy_res2 = 0;
3369 if (rc == -ENOTCONN || rc == -ENODEV ||
3370 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3372 * If it is the disconnect error (ENODEV & ENOCONN), the error
3373 * will be returned by rq_status, and client at ptlrpc layer
3374 * will detect this, then disconnect, reconnect the import
3375 * immediately, instead of impacting the following the rpc.
3377 lhc->mlh_reg_lh.cookie = 0ull;
3381 * For other cases, the error will be returned by intent.
3382 * and client will retrieve the result from intent.
3385 * FIXME: when open lock is finished, that should be
3388 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3389 LASSERTF(rc == 0, "Error occurred but lock handle "
3390 "is still in use, rc = %d\n", rc);
3391 rep->lock_policy_res2 = 0;
3392 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3395 lhc->mlh_reg_lh.cookie = 0ull;
3396 RETURN(ELDLM_LOCK_ABORTED);
3401 static int mdt_intent_code(long itcode)
3409 case IT_OPEN|IT_CREAT:
3416 rc = MDT_IT_READDIR;
3419 rc = MDT_IT_GETATTR;
3431 rc = MDT_IT_GETXATTR;
3436 case IT_QUOTA_DQACQ:
3441 CERROR("Unknown intent opcode: %ld\n", itcode);
3448 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3449 struct ldlm_lock **lockp, __u64 flags)
3451 struct req_capsule *pill;
3452 struct mdt_it_flavor *flv;
3457 opc = mdt_intent_code(itopc);
3461 pill = info->mti_pill;
3463 if (opc == MDT_IT_QUOTA) {
3464 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3467 RETURN(-EOPNOTSUPP);
3469 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3470 /* pass the request to quota master */
3471 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3472 mdt_info_req(info), lockp,
3477 flv = &mdt_it_flavor[opc];
3478 if (flv->it_fmt != NULL)
3479 req_capsule_extend(pill, flv->it_fmt);
3481 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3483 struct ptlrpc_request *req = mdt_info_req(info);
3484 if (flv->it_flags & MUTABOR &&
3485 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3488 if (rc == 0 && flv->it_act != NULL) {
3489 struct ldlm_reply *rep;
3491 /* execute policy */
3492 rc = flv->it_act(opc, info, lockp, flags);
3494 /* Check whether the reply has been packed successfully. */
3495 if (mdt_info_req(info)->rq_repmsg != NULL) {
3496 rep = req_capsule_server_get(info->mti_pill,
3498 rep->lock_policy_res2 =
3499 ptlrpc_status_hton(rep->lock_policy_res2);
3507 static int mdt_intent_policy(struct ldlm_namespace *ns,
3508 struct ldlm_lock **lockp, void *req_cookie,
3509 ldlm_mode_t mode, __u64 flags, void *data)
3511 struct tgt_session_info *tsi;
3512 struct mdt_thread_info *info;
3513 struct ptlrpc_request *req = req_cookie;
3514 struct ldlm_intent *it;
3515 struct req_capsule *pill;
3520 LASSERT(req != NULL);
3522 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3524 info = tsi2mdt_info(tsi);
3525 LASSERT(info != NULL);
3526 pill = info->mti_pill;
3527 LASSERT(pill->rc_req == req);
3529 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3530 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3531 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3533 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3537 /* Lock without inodebits makes no sense and will oops
3538 * later in ldlm. Let's check it now to see if we have
3539 * ibits corrupted somewhere in mdt_intent_opc().
3540 * The case for client miss to set ibits has been
3541 * processed by others. */
3542 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3543 lr_type == LDLM_IBITS,
3544 info->mti_dlm_req->lock_desc.\
3545 l_policy_data.l_inodebits.bits != 0));
3547 rc = err_serious(-EFAULT);
3549 /* No intent was provided */
3550 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3551 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3552 rc = req_capsule_server_pack(pill);
3554 rc = err_serious(rc);
3556 mdt_thread_info_fini(info);
3560 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3562 struct seq_server_site *ss = mdt_seq_site(mdt);
3564 if (ss->ss_node_id == 0)
3567 if (ss->ss_client_seq != NULL) {
3568 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3569 ss->ss_client_seq->lcs_exp = NULL;
3572 if (ss->ss_server_fld != NULL) {
3573 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3574 ss->ss_server_fld->lsf_control_exp = NULL;
3578 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3580 struct seq_server_site *ss = mdt_seq_site(mdt);
3585 if (ss->ss_server_seq != NULL)
3586 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3589 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3591 mdt_seq_fini_cli(mdt);
3592 mdt_deregister_seq_exp(mdt);
3594 return seq_site_fini(env, mdt_seq_site(mdt));
3598 * It will retrieve its FLDB entries from MDT0, and it only happens
3599 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3600 * and it needs to refresh FLDB from the MDT0.
3602 static int mdt_register_lwp_callback(void *data)
3605 struct mdt_device *mdt = data;
3606 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3610 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3612 if (!likely(fld->lsf_new))
3615 rc = lu_env_init(&env, LCT_MD_THREAD);
3617 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3621 rc = fld_update_from_controller(&env, fld);
3623 CERROR("%s: cannot update controller: rc = %d\n",
3624 mdt_obd_name(mdt), rc);
3632 static int mdt_register_seq_exp(struct mdt_device *mdt)
3634 struct seq_server_site *ss = mdt_seq_site(mdt);
3635 char *lwp_name = NULL;
3638 if (ss->ss_node_id == 0)
3641 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3642 if (lwp_name == NULL)
3643 GOTO(out_free, rc = -ENOMEM);
3645 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3649 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3654 rc = lustre_register_lwp_item(lwp_name,
3655 &ss->ss_server_fld->lsf_control_exp,
3656 mdt_register_lwp_callback, mdt);
3658 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3659 ss->ss_client_seq->lcs_exp = NULL;
3663 if (lwp_name != NULL)
3664 OBD_FREE(lwp_name, MAX_OBD_NAME);
3670 * Init client sequence manager which is used by local MDS to talk to sequence
3671 * controller on remote node.
3673 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3675 struct seq_server_site *ss = mdt_seq_site(mdt);
3680 /* check if this is adding the first MDC and controller is not yet
3682 OBD_ALLOC_PTR(ss->ss_client_seq);
3683 if (ss->ss_client_seq == NULL)