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_linkea.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 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 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 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
488 const struct lu_env *env = info->mti_env;
492 LASSERT(info->mti_big_lmm_used == 0);
493 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
497 /* big_lmm may need to be grown */
498 if (info->mti_big_lmmsize < rc) {
499 int size = size_roundup_power2(rc);
501 if (info->mti_big_lmmsize > 0) {
502 /* free old buffer */
503 LASSERT(info->mti_big_lmm);
504 OBD_FREE_LARGE(info->mti_big_lmm,
505 info->mti_big_lmmsize);
506 info->mti_big_lmm = NULL;
507 info->mti_big_lmmsize = 0;
510 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
511 if (info->mti_big_lmm == NULL)
513 info->mti_big_lmmsize = size;
515 LASSERT(info->mti_big_lmmsize >= rc);
517 info->mti_buf.lb_buf = info->mti_big_lmm;
518 info->mti_buf.lb_len = info->mti_big_lmmsize;
519 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
524 int mdt_attr_get_lov(struct mdt_thread_info *info,
525 struct mdt_object *o, struct md_attr *ma)
527 struct md_object *next = mdt_object_child(o);
528 struct lu_buf *buf = &info->mti_buf;
531 buf->lb_buf = ma->ma_lmm;
532 buf->lb_len = ma->ma_lmm_size;
533 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
536 ma->ma_lmm_size = rc;
537 ma->ma_valid |= MA_LOV;
539 } else if (rc == -ENODATA) {
542 } else if (rc == -ERANGE) {
543 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
545 info->mti_big_lmm_used = 1;
546 ma->ma_valid |= MA_LOV;
547 ma->ma_lmm = info->mti_big_lmm;
548 ma->ma_lmm_size = rc;
549 /* update mdt_max_mdsize so all clients
550 * will be aware about that */
551 if (info->mti_mdt->mdt_max_mdsize < rc)
552 info->mti_mdt->mdt_max_mdsize = rc;
560 int mdt_attr_get_pfid(struct mdt_thread_info *info,
561 struct mdt_object *o, struct lu_fid *pfid)
563 struct lu_buf *buf = &info->mti_buf;
564 struct link_ea_header *leh;
565 struct link_ea_entry *lee;
569 buf->lb_buf = info->mti_big_lmm;
570 buf->lb_len = info->mti_big_lmmsize;
571 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
572 buf, XATTR_NAME_LINK);
573 /* ignore errors, MA_PFID won't be set and it is
574 * up to the caller to treat this as an error */
575 if (rc == -ERANGE || buf->lb_len == 0) {
576 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
577 buf->lb_buf = info->mti_big_lmm;
578 buf->lb_len = info->mti_big_lmmsize;
583 if (rc < sizeof(*leh)) {
584 CERROR("short LinkEA on "DFID": rc = %d\n",
585 PFID(mdt_object_fid(o)), rc);
589 leh = (struct link_ea_header *) buf->lb_buf;
590 lee = (struct link_ea_entry *)(leh + 1);
591 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
592 leh->leh_magic = LINK_EA_MAGIC;
593 leh->leh_reccount = __swab32(leh->leh_reccount);
594 leh->leh_len = __swab64(leh->leh_len);
596 if (leh->leh_magic != LINK_EA_MAGIC)
598 if (leh->leh_reccount == 0)
601 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
602 fid_be_to_cpu(pfid, pfid);
607 int mdt_attr_get_complex(struct mdt_thread_info *info,
608 struct mdt_object *o, struct md_attr *ma)
610 const struct lu_env *env = info->mti_env;
611 struct md_object *next = mdt_object_child(o);
612 struct lu_buf *buf = &info->mti_buf;
613 u32 mode = lu_object_attr(&next->mo_lu);
614 int need = ma->ma_need;
620 if (need & MA_INODE) {
621 ma->ma_need = MA_INODE;
622 rc = mo_attr_get(env, next, ma);
625 ma->ma_valid |= MA_INODE;
628 if (need & MA_PFID) {
629 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
631 ma->ma_valid |= MA_PFID;
632 /* ignore this error, parent fid is not mandatory */
636 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
637 rc = mdt_attr_get_lov(info, o, ma);
642 if (need & MA_LMV && S_ISDIR(mode)) {
643 buf->lb_buf = ma->ma_lmv;
644 buf->lb_len = ma->ma_lmv_size;
645 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
647 ma->ma_lmv_size = rc2;
648 ma->ma_valid |= MA_LMV;
649 } else if (rc2 == -ENODATA) {
656 if (need & MA_SOM && S_ISREG(mode)) {
657 buf->lb_buf = info->mti_xattr_buf;
658 buf->lb_len = sizeof(info->mti_xattr_buf);
659 CLASSERT(sizeof(struct som_attrs) <=
660 sizeof(info->mti_xattr_buf));
661 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
662 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
664 ma->ma_valid |= MA_SOM;
665 else if (rc2 < 0 && rc2 != -ENODATA)
669 if (need & MA_HSM && S_ISREG(mode)) {
670 buf->lb_buf = info->mti_xattr_buf;
671 buf->lb_len = sizeof(info->mti_xattr_buf);
672 CLASSERT(sizeof(struct hsm_attrs) <=
673 sizeof(info->mti_xattr_buf));
674 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
675 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
677 ma->ma_valid |= MA_HSM;
678 else if (rc2 < 0 && rc2 != -ENODATA)
682 #ifdef CONFIG_FS_POSIX_ACL
683 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
684 buf->lb_buf = ma->ma_acl;
685 buf->lb_len = ma->ma_acl_size;
686 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
688 ma->ma_acl_size = rc2;
689 ma->ma_valid |= MA_ACL_DEF;
690 } else if (rc2 == -ENODATA) {
699 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
700 rc, ma->ma_valid, ma->ma_lmm);
704 static int mdt_getattr_internal(struct mdt_thread_info *info,
705 struct mdt_object *o, int ma_need)
707 struct md_object *next = mdt_object_child(o);
708 const struct mdt_body *reqbody = info->mti_body;
709 struct ptlrpc_request *req = mdt_info_req(info);
710 struct md_attr *ma = &info->mti_attr;
711 struct lu_attr *la = &ma->ma_attr;
712 struct req_capsule *pill = info->mti_pill;
713 const struct lu_env *env = info->mti_env;
714 struct mdt_body *repbody;
715 struct lu_buf *buffer = &info->mti_buf;
720 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
721 RETURN(err_serious(-ENOMEM));
723 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
727 if (mdt_object_remote(o)) {
728 /* This object is located on remote node.*/
729 /* Return -EIO for old client */
730 if (!mdt_is_dne_client(req->rq_export))
731 GOTO(out, rc = -EIO);
733 repbody->fid1 = *mdt_object_fid(o);
734 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
738 buffer->lb_len = reqbody->eadatasize;
739 if (buffer->lb_len > 0) {
740 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
741 if (buffer->lb_buf == NULL)
742 GOTO(out, rc = -EPROTO);
744 buffer->lb_buf = NULL;
745 ma_need &= ~(MA_LOV | MA_LMV);
746 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
747 mdt_obd_name(info->mti_mdt),
748 req->rq_export->exp_client_uuid.uuid);
751 /* If it is dir object and client require MEA, then we got MEA */
752 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
753 reqbody->valid & OBD_MD_MEA) {
754 /* Assumption: MDT_MD size is enough for lmv size. */
755 ma->ma_lmv = buffer->lb_buf;
756 ma->ma_lmv_size = buffer->lb_len;
757 ma->ma_need = MA_INODE;
758 if (ma->ma_lmv_size > 0)
759 ma->ma_need |= MA_LMV;
761 ma->ma_lmm = buffer->lb_buf;
762 ma->ma_lmm_size = buffer->lb_len;
763 ma->ma_need = MA_INODE | MA_HSM;
764 if (ma->ma_lmm_size > 0)
765 ma->ma_need |= MA_LOV;
768 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
769 reqbody->valid & OBD_MD_FLDIREA &&
770 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
771 /* get default stripe info for this dir. */
772 ma->ma_need |= MA_LOV_DEF;
774 ma->ma_need |= ma_need;
775 if (ma->ma_need & MA_SOM)
776 ma->ma_som = &info->mti_u.som.data;
778 rc = mdt_attr_get_complex(info, o, ma);
780 CERROR("%s: getattr error for "DFID": rc = %d\n",
781 mdt_obd_name(info->mti_mdt),
782 PFID(mdt_object_fid(o)), rc);
786 /* if file is released, check if a restore is running */
787 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
788 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
789 repbody->t_state = MS_RESTORE;
790 repbody->valid |= OBD_MD_TSTATE;
793 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
795 /* the Lustre protocol supposes to return default striping
796 * on the user-visible root if explicitly requested */
797 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
798 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
799 struct lu_fid rootfid;
800 struct mdt_object *root;
801 struct mdt_device *mdt = info->mti_mdt;
803 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
806 root = mdt_object_find(env, mdt, &rootfid);
808 RETURN(PTR_ERR(root));
809 rc = mdt_attr_get_lov(info, root, ma);
810 mdt_object_put(info->mti_env, root);
812 CERROR("%s: getattr error for "DFID": rc = %d\n",
813 mdt_obd_name(info->mti_mdt),
814 PFID(mdt_object_fid(o)), rc);
819 if (likely(ma->ma_valid & MA_INODE))
820 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
824 if (mdt_body_has_lov(la, reqbody)) {
825 if (ma->ma_valid & MA_LOV) {
826 LASSERT(ma->ma_lmm_size);
827 mdt_dump_lmm(D_INFO, ma->ma_lmm);
828 repbody->eadatasize = ma->ma_lmm_size;
829 if (S_ISDIR(la->la_mode))
830 repbody->valid |= OBD_MD_FLDIREA;
832 repbody->valid |= OBD_MD_FLEASIZE;
834 if (ma->ma_valid & MA_LMV) {
835 LASSERT(S_ISDIR(la->la_mode));
836 repbody->eadatasize = ma->ma_lmv_size;
837 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
839 } else if (S_ISLNK(la->la_mode) &&
840 reqbody->valid & OBD_MD_LINKNAME) {
841 buffer->lb_buf = ma->ma_lmm;
842 /* eadatasize from client includes NULL-terminator, so
843 * there is no need to read it */
844 buffer->lb_len = reqbody->eadatasize - 1;
845 rc = mo_readlink(env, next, buffer);
846 if (unlikely(rc <= 0)) {
847 CERROR("%s: readlink failed for "DFID": rc = %d\n",
848 mdt_obd_name(info->mti_mdt),
849 PFID(mdt_object_fid(o)), rc);
852 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
854 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
856 repbody->valid |= OBD_MD_LINKNAME;
857 /* we need to report back size with NULL-terminator
858 * because client expects that */
859 repbody->eadatasize = rc + 1;
860 if (repbody->eadatasize != reqbody->eadatasize)
861 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
862 "on "DFID ", expected %d\n",
863 mdt_obd_name(info->mti_mdt),
864 rc, PFID(mdt_object_fid(o)),
865 reqbody->eadatasize - 1);
867 ((char *)ma->ma_lmm)[rc] = 0;
869 /* If the total CDEBUG() size is larger than a page, it
870 * will print a warning to the console, avoid this by
871 * printing just the last part of the symlink. */
872 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
873 print_limit < rc ? "..." : "", print_limit,
874 (char *)ma->ma_lmm + rc - print_limit, rc);
879 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
880 repbody->max_cookiesize = 0;
881 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
882 repbody->valid |= OBD_MD_FLMODEASIZE;
883 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
884 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
885 repbody->max_cookiesize);
888 if (exp_connect_rmtclient(info->mti_exp) &&
889 reqbody->valid & OBD_MD_FLRMTPERM) {
890 void *buf = req_capsule_server_get(pill, &RMF_ACL);
892 /* mdt_getattr_lock only */
893 rc = mdt_pack_remote_perm(info, o, buf);
895 repbody->valid &= ~OBD_MD_FLRMTPERM;
896 repbody->aclsize = 0;
899 repbody->valid |= OBD_MD_FLRMTPERM;
900 repbody->aclsize = sizeof(struct mdt_remote_perm);
903 #ifdef CONFIG_FS_POSIX_ACL
904 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
905 (reqbody->valid & OBD_MD_FLACL)) {
906 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
907 buffer->lb_len = req_capsule_get_size(pill,
908 &RMF_ACL, RCL_SERVER);
909 if (buffer->lb_len > 0) {
910 rc = mo_xattr_get(env, next, buffer,
911 XATTR_NAME_ACL_ACCESS);
913 if (rc == -ENODATA) {
914 repbody->aclsize = 0;
915 repbody->valid |= OBD_MD_FLACL;
917 } else if (rc == -EOPNOTSUPP) {
920 CERROR("%s: unable to read "DFID
922 mdt_obd_name(info->mti_mdt),
923 PFID(mdt_object_fid(o)), rc);
926 repbody->aclsize = rc;
927 repbody->valid |= OBD_MD_FLACL;
934 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
935 info->mti_mdt->mdt_lut.lut_mds_capa &&
936 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
937 struct lustre_capa *capa;
939 capa = req_capsule_server_get(pill, &RMF_CAPA1);
941 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
942 rc = mo_capa_get(env, next, capa, 0);
945 repbody->valid |= OBD_MD_FLMDSCAPA;
950 mdt_counter_incr(req, LPROC_MDT_GETATTR);
955 static int mdt_renew_capa(struct mdt_thread_info *info)
957 struct mdt_object *obj = info->mti_object;
958 struct mdt_body *body;
959 struct lustre_capa *capa, *c;
963 /* if object doesn't exist, or server has disabled capability,
964 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
967 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
968 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
971 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
972 LASSERT(body != NULL);
974 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
977 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
981 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
983 body->valid |= OBD_MD_FLOSSCAPA;
987 int mdt_getattr(struct tgt_session_info *tsi)
989 struct mdt_thread_info *info = tsi2mdt_info(tsi);
990 struct mdt_object *obj = info->mti_object;
991 struct req_capsule *pill = info->mti_pill;
992 struct mdt_body *reqbody;
993 struct mdt_body *repbody;
998 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1001 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1002 rc = req_capsule_server_pack(pill);
1004 RETURN(err_serious(rc));
1005 rc = mdt_renew_capa(info);
1006 GOTO(out_shrink, rc);
1009 LASSERT(obj != NULL);
1010 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1012 mode = lu_object_attr(&obj->mot_obj);
1014 /* old clients may not report needed easize, use max value then */
1015 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1016 reqbody->eadatasize == 0 ?
1017 info->mti_mdt->mdt_max_mdsize :
1018 reqbody->eadatasize);
1020 rc = req_capsule_server_pack(pill);
1021 if (unlikely(rc != 0))
1022 GOTO(out, rc = err_serious(rc));
1024 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1025 LASSERT(repbody != NULL);
1026 repbody->eadatasize = 0;
1027 repbody->aclsize = 0;
1029 if (reqbody->valid & OBD_MD_FLRMTPERM)
1030 rc = mdt_init_ucred(info, reqbody);
1032 rc = mdt_check_ucred(info);
1034 GOTO(out_shrink, rc);
1036 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1039 * Don't check capability at all, because rename might getattr for
1040 * remote obj, and at that time no capability is available.
1042 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1043 rc = mdt_getattr_internal(info, obj, 0);
1044 if (reqbody->valid & OBD_MD_FLRMTPERM)
1045 mdt_exit_ucred(info);
1048 mdt_client_compatibility(info);
1049 rc2 = mdt_fix_reply(info);
1053 mdt_thread_info_fini(info);
1057 int mdt_is_subdir(struct tgt_session_info *tsi)
1059 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1060 struct mdt_object *o = info->mti_object;
1061 struct req_capsule *pill = info->mti_pill;
1062 const struct mdt_body *body = info->mti_body;
1063 struct mdt_body *repbody;
1069 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1072 * We save last checked parent fid to @repbody->fid1 for remote
1075 LASSERT(fid_is_sane(&body->fid2));
1076 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1077 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1078 &body->fid2, &repbody->fid1);
1079 if (rc == 0 || rc == -EREMOTE)
1080 repbody->valid |= OBD_MD_FLID;
1082 mdt_thread_info_fini(info);
1086 int mdt_swap_layouts(struct tgt_session_info *tsi)
1088 struct mdt_thread_info *info;
1089 struct ptlrpc_request *req = tgt_ses_req(tsi);
1090 struct obd_export *exp = req->rq_export;
1091 struct mdt_object *o1, *o2, *o;
1092 struct mdt_lock_handle *lh1, *lh2;
1093 struct mdc_swap_layouts *msl;
1097 /* client does not support layout lock, so layout swaping
1099 * FIXME: there is a problem for old clients which don't support
1100 * layout lock yet. If those clients have already opened the file
1101 * they won't be notified at all so that old layout may still be
1102 * used to do IO. This can be fixed after file release is landed by
1103 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1104 if (!exp_connect_layout(exp))
1105 RETURN(-EOPNOTSUPP);
1107 info = tsi2mdt_info(tsi);
1108 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1109 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1110 req_capsule_client_get(info->mti_pill,
1113 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1114 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1115 req_capsule_client_get(info->mti_pill,
1118 o1 = info->mti_object;
1119 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1120 &info->mti_body->fid2);
1122 GOTO(out, rc = PTR_ERR(o));
1124 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1125 GOTO(put, rc = -ENOENT);
1127 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1128 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1134 /* permission check. Make sure the calling process having permission
1135 * to write both files. */
1136 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1141 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1146 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1148 GOTO(put, rc = -EPROTO);
1150 lh1 = &info->mti_lh[MDT_LH_NEW];
1151 mdt_lock_reg_init(lh1, LCK_EX);
1152 lh2 = &info->mti_lh[MDT_LH_OLD];
1153 mdt_lock_reg_init(lh2, LCK_EX);
1155 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1156 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1160 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1161 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1165 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1166 mdt_object_child(o2), msl->msl_flags);
1169 mdt_object_unlock(info, o2, lh2, rc);
1171 mdt_object_unlock(info, o1, lh1, rc);
1173 mdt_object_put(info->mti_env, o);
1175 mdt_thread_info_fini(info);
1179 static int mdt_raw_lookup(struct mdt_thread_info *info,
1180 struct mdt_object *parent,
1181 const struct lu_name *lname,
1182 struct ldlm_reply *ldlm_rep)
1184 struct md_object *next = mdt_object_child(info->mti_object);
1185 const struct mdt_body *reqbody = info->mti_body;
1186 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1187 struct mdt_body *repbody;
1191 if (reqbody->valid != OBD_MD_FLID)
1194 LASSERT(!info->mti_cross_ref);
1196 /* Only got the fid of this obj by name */
1197 fid_zero(child_fid);
1198 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1201 /* XXX is raw_lookup possible as intent operation? */
1204 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1207 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1209 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1212 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1213 repbody->fid1 = *child_fid;
1214 repbody->valid = OBD_MD_FLID;
1220 * UPDATE lock should be taken against parent, and be release before exit;
1221 * child_bits lock should be taken against child, and be returned back:
1222 * (1)normal request should release the child lock;
1223 * (2)intent request will grant the lock to client.
1225 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1226 struct mdt_lock_handle *lhc,
1228 struct ldlm_reply *ldlm_rep)
1230 struct ptlrpc_request *req = mdt_info_req(info);
1231 struct mdt_body *reqbody = NULL;
1232 struct mdt_object *parent = info->mti_object;
1233 struct mdt_object *child;
1234 struct md_object *next = mdt_object_child(parent);
1235 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1236 struct lu_name *lname = NULL;
1237 struct mdt_lock_handle *lhp = NULL;
1238 struct ldlm_lock *lock;
1239 struct ldlm_res_id *res_id;
1246 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1247 LASSERT(ergo(is_resent,
1248 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1250 LASSERT(parent != NULL);
1252 lname = &info->mti_name;
1253 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1255 if (!info->mti_cross_ref) {
1257 * XXX: Check for anonymous name is for getattr by fid
1258 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1259 * that is the name must contain at least one character and
1260 * the terminating '\0'.
1262 if (!lu_name_is_valid(lname)) {
1263 reqbody = req_capsule_client_get(info->mti_pill,
1265 if (unlikely(reqbody == NULL))
1266 RETURN(err_serious(-EFAULT));
1268 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1269 RETURN(err_serious(-EINVAL));
1271 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1273 PFID(mdt_object_fid(parent)),
1274 PFID(&reqbody->fid2), ldlm_rep);
1276 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1277 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1278 PNAME(lname), ldlm_rep);
1282 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1284 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1285 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1287 "Parent doesn't exist!\n");
1289 } else if (!info->mti_cross_ref) {
1290 LASSERTF(!mdt_object_remote(parent),
1291 "Parent "DFID" is on remote server\n",
1292 PFID(mdt_object_fid(parent)));
1295 if (lu_name_is_valid(lname)) {
1296 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1304 if (info->mti_cross_ref) {
1305 /* Only getattr on the child. Parent is on another node. */
1306 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1308 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1309 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1312 /* Do not take lock for resent case. */
1313 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1314 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1315 lhc->mlh_reg_lh.cookie);
1316 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1317 &lock->l_resource->lr_name));
1318 LDLM_LOCK_PUT(lock);
1321 mdt_lock_handle_init(lhc);
1322 mdt_lock_reg_init(lhc, LCK_PR);
1325 * Object's name is on another MDS, no lookup or layout
1326 * lock is needed here but update lock is.
1328 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1329 MDS_INODELOCK_LAYOUT);
1330 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1332 rc = mdt_object_lock(info, child, lhc, child_bits,
1336 /* Finally, we can get attr for child. */
1337 if (!mdt_object_exists(child)) {
1338 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1340 "remote object doesn't exist.\n");
1341 mdt_object_unlock(info, child, lhc, 1);
1345 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1347 rc = mdt_getattr_internal(info, child, 0);
1348 if (unlikely(rc != 0))
1349 mdt_object_unlock(info, child, lhc, 1);
1354 if (lu_name_is_valid(lname)) {
1355 /* step 1: lock parent only if parent is a directory */
1356 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1357 lhp = &info->mti_lh[MDT_LH_PARENT];
1358 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1359 rc = mdt_object_lock(info, parent, lhp,
1360 MDS_INODELOCK_UPDATE,
1362 if (unlikely(rc != 0))
1366 /* step 2: lookup child's fid by name */
1367 fid_zero(child_fid);
1368 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1373 mdt_set_disposition(info, ldlm_rep,
1375 GOTO(out_parent, rc);
1377 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1379 *child_fid = reqbody->fid2;
1380 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1384 *step 3: find the child object by fid & lock it.
1385 * regardless if it is local or remote.
1387 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1388 * set parent dir fid the same as child fid in getattr by fid case
1389 * we should not lu_object_find() the object again, could lead
1390 * to hung if there is a concurrent unlink destroyed the object.
1392 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1393 mdt_object_get(info->mti_env, parent);
1396 child = mdt_object_find(info->mti_env, info->mti_mdt,
1400 if (unlikely(IS_ERR(child)))
1401 GOTO(out_parent, rc = PTR_ERR(child));
1403 /* Do not take lock for resent case. */
1404 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1405 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1406 lhc->mlh_reg_lh.cookie);
1408 res_id = &lock->l_resource->lr_name;
1409 if (!fid_res_name_eq(mdt_object_fid(child),
1410 &lock->l_resource->lr_name)) {
1411 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1412 &lock->l_resource->lr_name),
1413 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1414 PLDLMRES(lock->l_resource),
1415 PFID(mdt_object_fid(parent)));
1416 CWARN("Although resent, but still not get child lock"
1417 "parent:"DFID" child:"DFID"\n",
1418 PFID(mdt_object_fid(parent)),
1419 PFID(mdt_object_fid(child)));
1420 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1421 LDLM_LOCK_PUT(lock);
1424 LDLM_LOCK_PUT(lock);
1427 bool try_layout = false;
1430 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1431 mdt_lock_handle_init(lhc);
1432 mdt_lock_reg_init(lhc, LCK_PR);
1434 if (!mdt_object_exists(child)) {
1435 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1437 "Object doesn't exist!\n");
1438 GOTO(out_child, rc = -ENOENT);
1441 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1442 mdt_object_exists(child) && !mdt_object_remote(child)) {
1443 struct md_attr *ma = &info->mti_attr;
1446 ma->ma_need = MA_INODE;
1447 rc = mdt_attr_get_complex(info, child, ma);
1448 if (unlikely(rc != 0))
1449 GOTO(out_child, rc);
1451 /* If the file has not been changed for some time, we
1452 * return not only a LOOKUP lock, but also an UPDATE
1453 * lock and this might save us RPC on later STAT. For
1454 * directories, it also let negative dentry cache start
1455 * working for this dir. */
1456 if (ma->ma_valid & MA_INODE &&
1457 ma->ma_attr.la_valid & LA_CTIME &&
1458 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1459 ma->ma_attr.la_ctime < cfs_time_current_sec())
1460 child_bits |= MDS_INODELOCK_UPDATE;
1463 /* layout lock must be granted in a best-effort way
1464 * for IT operations */
1465 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1466 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1467 exp_connect_layout(info->mti_exp) &&
1468 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1469 !mdt_object_remote(child) && ldlm_rep != NULL) {
1470 /* try to grant layout lock for regular file. */
1476 child_bits |= MDS_INODELOCK_LAYOUT;
1477 /* try layout lock, it may fail to be granted due to
1478 * contention at LOOKUP or UPDATE */
1479 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1481 child_bits &= ~MDS_INODELOCK_LAYOUT;
1482 LASSERT(child_bits != 0);
1483 rc = mdt_object_lock(info, child, lhc,
1484 child_bits, MDT_CROSS_LOCK);
1489 rc = mdt_object_lock(info, child, lhc, child_bits,
1492 if (unlikely(rc != 0))
1493 GOTO(out_child, rc);
1496 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1497 /* Get MA_SOM attributes if update lock is given. */
1499 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1500 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1503 /* finally, we can get attr for child. */
1504 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1505 rc = mdt_getattr_internal(info, child, ma_need);
1506 if (unlikely(rc != 0)) {
1507 mdt_object_unlock(info, child, lhc, 1);
1509 /* Debugging code. */
1510 res_id = &lock->l_resource->lr_name;
1511 LDLM_DEBUG(lock, "Returning lock to client");
1512 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1513 &lock->l_resource->lr_name),
1514 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1515 PLDLMRES(lock->l_resource),
1516 PFID(mdt_object_fid(child)));
1517 if (mdt_object_exists(child) && !mdt_object_remote(child))
1518 mdt_pack_size2body(info, child);
1521 LDLM_LOCK_PUT(lock);
1525 mdt_object_put(info->mti_env, child);
1528 mdt_object_unlock(info, parent, lhp, 1);
1532 /* normal handler: should release the child lock */
1533 int mdt_getattr_name(struct tgt_session_info *tsi)
1535 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1536 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1537 struct mdt_body *reqbody;
1538 struct mdt_body *repbody;
1542 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1543 LASSERT(reqbody != NULL);
1544 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1545 LASSERT(repbody != NULL);
1547 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1548 repbody->eadatasize = 0;
1549 repbody->aclsize = 0;
1551 rc = mdt_init_ucred(info, reqbody);
1553 GOTO(out_shrink, rc);
1555 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1556 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1557 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1558 lhc->mlh_reg_lh.cookie = 0;
1560 mdt_exit_ucred(info);
1563 mdt_client_compatibility(info);
1564 rc2 = mdt_fix_reply(info);
1567 mdt_thread_info_fini(info);
1571 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1572 void *karg, void *uarg);
1574 int mdt_set_info(struct tgt_session_info *tsi)
1576 struct ptlrpc_request *req = tgt_ses_req(tsi);
1579 int keylen, vallen, rc = 0;
1583 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1585 DEBUG_REQ(D_HA, req, "no set_info key");
1586 RETURN(err_serious(-EFAULT));
1589 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1592 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1594 DEBUG_REQ(D_HA, req, "no set_info val");
1595 RETURN(err_serious(-EFAULT));
1598 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1601 /* Swab any part of val you need to here */
1602 if (KEY_IS(KEY_READ_ONLY)) {
1603 spin_lock(&req->rq_export->exp_lock);
1605 *exp_connect_flags_ptr(req->rq_export) |=
1608 *exp_connect_flags_ptr(req->rq_export) &=
1609 ~OBD_CONNECT_RDONLY;
1610 spin_unlock(&req->rq_export->exp_lock);
1611 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1612 struct changelog_setinfo *cs = val;
1614 if (vallen != sizeof(*cs)) {
1615 CERROR("%s: bad changelog_clear setinfo size %d\n",
1616 tgt_name(tsi->tsi_tgt), vallen);
1619 if (ptlrpc_req_need_swab(req)) {
1620 __swab64s(&cs->cs_recno);
1621 __swab32s(&cs->cs_id);
1624 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1632 int mdt_readpage(struct tgt_session_info *tsi)
1634 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1635 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1636 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1637 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1638 struct mdt_body *repbody;
1644 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1645 RETURN(err_serious(-ENOMEM));
1647 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1648 if (repbody == NULL || reqbody == NULL)
1649 RETURN(err_serious(-EFAULT));
1652 * prepare @rdpg before calling lower layers and transfer itself. Here
1653 * reqbody->size contains offset of where to start to read and
1654 * reqbody->nlink contains number bytes to read.
1656 rdpg->rp_hash = reqbody->size;
1657 if (rdpg->rp_hash != reqbody->size) {
1658 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1659 rdpg->rp_hash, reqbody->size);
1663 rdpg->rp_attrs = reqbody->mode;
1664 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1665 rdpg->rp_attrs |= LUDA_64BITHASH;
1666 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1667 exp_max_brw_size(tsi->tsi_exp));
1668 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1670 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1671 if (rdpg->rp_pages == NULL)
1674 for (i = 0; i < rdpg->rp_npages; ++i) {
1675 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1676 if (rdpg->rp_pages[i] == NULL)
1677 GOTO(free_rdpg, rc = -ENOMEM);
1680 /* call lower layers to fill allocated pages with directory data */
1681 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1683 GOTO(free_rdpg, rc);
1685 /* send pages to client */
1686 rc = tgt_sendpage(tsi, rdpg, rc);
1691 for (i = 0; i < rdpg->rp_npages; i++)
1692 if (rdpg->rp_pages[i] != NULL)
1693 __free_page(rdpg->rp_pages[i]);
1694 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1696 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1702 static int mdt_reint_internal(struct mdt_thread_info *info,
1703 struct mdt_lock_handle *lhc,
1706 struct req_capsule *pill = info->mti_pill;
1707 struct mdt_body *repbody;
1712 rc = mdt_reint_unpack(info, op);
1714 CERROR("Can't unpack reint, rc %d\n", rc);
1715 RETURN(err_serious(rc));
1718 /* for replay (no_create) lmm is not needed, client has it already */
1719 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1720 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1721 info->mti_rr.rr_eadatalen);
1723 /* llog cookies are always 0, the field is kept for compatibility */
1724 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1725 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1727 rc = req_capsule_server_pack(pill);
1729 CERROR("Can't pack response, rc %d\n", rc);
1730 RETURN(err_serious(rc));
1733 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1734 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1736 repbody->eadatasize = 0;
1737 repbody->aclsize = 0;
1740 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1742 /* for replay no cookkie / lmm need, because client have this already */
1743 if (info->mti_spec.no_create)
1744 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1745 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1747 rc = mdt_init_ucred_reint(info);
1749 GOTO(out_shrink, rc);
1751 rc = mdt_fix_attr_ucred(info, op);
1753 GOTO(out_ucred, rc = err_serious(rc));
1755 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1756 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1757 GOTO(out_ucred, rc);
1759 rc = mdt_reint_rec(info, lhc);
1762 mdt_exit_ucred(info);
1764 mdt_client_compatibility(info);
1765 rc2 = mdt_fix_reply(info);
1771 static long mdt_reint_opcode(struct ptlrpc_request *req,
1772 const struct req_format **fmt)
1774 struct mdt_device *mdt;
1775 struct mdt_rec_reint *rec;
1778 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1780 opc = rec->rr_opcode;
1781 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1782 if (opc < REINT_MAX && fmt[opc] != NULL)
1783 req_capsule_extend(&req->rq_pill, fmt[opc]);
1785 mdt = mdt_exp2dev(req->rq_export);
1786 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1787 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1788 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1789 opc = err_serious(-EFAULT);
1792 opc = err_serious(-EFAULT);
1797 int mdt_reint(struct tgt_session_info *tsi)
1801 static const struct req_format *reint_fmts[REINT_MAX] = {
1802 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1803 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1804 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1805 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1806 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1807 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1808 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1809 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1814 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1816 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1818 * No lock possible here from client to pass it to reint code
1821 rc = mdt_reint_internal(info, NULL, opc);
1822 mdt_thread_info_fini(info);
1827 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1831 /* this should sync the whole device */
1832 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1834 struct dt_device *dt = mdt->mdt_bottom;
1838 rc = dt->dd_ops->dt_sync(env, dt);
1842 /* this should sync this object */
1843 static int mdt_object_sync(struct mdt_thread_info *info)
1845 struct md_object *next;
1849 if (!mdt_object_exists(info->mti_object)) {
1850 CWARN("Non existing object "DFID"!\n",
1851 PFID(mdt_object_fid(info->mti_object)));
1854 next = mdt_object_child(info->mti_object);
1855 rc = mo_object_sync(info->mti_env, next);
1860 int mdt_sync(struct tgt_session_info *tsi)
1862 struct ptlrpc_request *req = tgt_ses_req(tsi);
1863 struct req_capsule *pill = tsi->tsi_pill;
1864 struct mdt_body *body;
1869 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1870 RETURN(err_serious(-ENOMEM));
1872 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1873 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1875 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1877 /* sync an object */
1878 rc = mdt_object_sync(info);
1880 const struct lu_fid *fid;
1881 struct lu_attr *la = &info->mti_attr.ma_attr;
1883 info->mti_attr.ma_need = MA_INODE;
1884 info->mti_attr.ma_valid = 0;
1885 rc = mdt_attr_get_complex(info, info->mti_object,
1888 body = req_capsule_server_get(pill,
1890 fid = mdt_object_fid(info->mti_object);
1891 mdt_pack_attr2body(info, body, la, fid);
1894 mdt_thread_info_fini(info);
1897 mdt_counter_incr(req, LPROC_MDT_SYNC);
1903 * Handle quota control requests to consult current usage/limit, but also
1904 * to configure quota enforcement
1906 int mdt_quotactl(struct tgt_session_info *tsi)
1908 struct obd_export *exp = tsi->tsi_exp;
1909 struct req_capsule *pill = tsi->tsi_pill;
1910 struct obd_quotactl *oqctl, *repoqc;
1912 struct mdt_device *mdt = mdt_exp2dev(exp);
1913 struct lu_device *qmt = mdt->mdt_qmt_dev;
1916 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1918 RETURN(err_serious(-EPROTO));
1920 rc = req_capsule_server_pack(pill);
1922 RETURN(err_serious(rc));
1924 switch (oqctl->qc_cmd) {
1926 case LUSTRE_Q_INVALIDATE:
1927 case LUSTRE_Q_FINVALIDATE:
1931 /* deprecated, not used any more */
1932 RETURN(-EOPNOTSUPP);
1933 /* master quotactl */
1939 RETURN(-EOPNOTSUPP);
1940 /* slave quotactl */
1945 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1949 /* map uid/gid for remote client */
1951 if (exp_connect_rmtclient(exp)) {
1952 struct lustre_idmap_table *idmap;
1954 idmap = exp->exp_mdt_data.med_idmap;
1956 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1957 oqctl->qc_cmd != Q_GETINFO))
1960 if (oqctl->qc_type == USRQUOTA)
1961 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1963 else if (oqctl->qc_type == GRPQUOTA)
1964 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1969 if (id == CFS_IDMAP_NOTFOUND) {
1970 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1975 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1977 RETURN(err_serious(-EFAULT));
1979 if (oqctl->qc_id != id)
1980 swap(oqctl->qc_id, id);
1982 switch (oqctl->qc_cmd) {
1988 /* forward quotactl request to QMT */
1989 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
1994 /* slave quotactl */
1995 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2000 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2004 if (oqctl->qc_id != id)
2005 swap(oqctl->qc_id, id);
2011 /** clone llog ctxt from child (mdd)
2012 * This allows remote llog (replicator) access.
2013 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2014 * context was originally set up, or we can handle them directly.
2015 * I choose the latter, but that means I need any llog
2016 * contexts set up by child to be accessable by the mdt. So we clone the
2017 * context into our context list here.
2019 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2022 struct md_device *next = mdt->mdt_child;
2023 struct llog_ctxt *ctxt;
2026 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2029 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2030 if (rc || ctxt == NULL) {
2034 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2036 CERROR("Can't set mdt ctxt %d\n", rc);
2041 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2042 struct mdt_device *mdt, int idx)
2044 struct llog_ctxt *ctxt;
2046 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2049 /* Put once for the get we just did, and once for the clone */
2050 llog_ctxt_put(ctxt);
2051 llog_ctxt_put(ctxt);
2056 * sec context handlers
2058 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2062 rc = mdt_handle_idmap(tsi);
2064 struct ptlrpc_request *req = tgt_ses_req(tsi);
2067 opc = lustre_msg_get_opc(req->rq_reqmsg);
2068 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2069 sptlrpc_svc_ctx_invalidate(req);
2072 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2078 * quota request handlers
2080 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2082 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2083 struct lu_device *qmt = mdt->mdt_qmt_dev;
2088 RETURN(err_serious(-EOPNOTSUPP));
2090 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2094 struct mdt_object *mdt_object_new(const struct lu_env *env,
2095 struct mdt_device *d,
2096 const struct lu_fid *f)
2098 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2099 struct lu_object *o;
2100 struct mdt_object *m;
2103 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2104 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2105 if (unlikely(IS_ERR(o)))
2106 m = (struct mdt_object *)o;
2112 struct mdt_object *mdt_object_find(const struct lu_env *env,
2113 struct mdt_device *d,
2114 const struct lu_fid *f)
2116 struct lu_object *o;
2117 struct mdt_object *m;
2120 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2121 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2122 if (unlikely(IS_ERR(o)))
2123 m = (struct mdt_object *)o;
2131 * Asyncronous commit for mdt device.
2133 * Pass asynchonous commit call down the MDS stack.
2135 * \param env environment
2136 * \param mdt the mdt device
2138 static void mdt_device_commit_async(const struct lu_env *env,
2139 struct mdt_device *mdt)
2141 struct dt_device *dt = mdt->mdt_bottom;
2144 rc = dt->dd_ops->dt_commit_async(env, dt);
2145 if (unlikely(rc != 0))
2146 CWARN("async commit start failed with rc = %d", rc);
2150 * Mark the lock as "synchonous".
2152 * Mark the lock to deffer transaction commit to the unlock time.
2154 * \param lock the lock to mark as "synchonous"
2156 * \see mdt_is_lock_sync
2157 * \see mdt_save_lock
2159 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2161 lock->l_ast_data = (void*)1;
2165 * Check whehter the lock "synchonous" or not.
2167 * \param lock the lock to check
2168 * \retval 1 the lock is "synchonous"
2169 * \retval 0 the lock isn't "synchronous"
2171 * \see mdt_set_lock_sync
2172 * \see mdt_save_lock
2174 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2176 return lock->l_ast_data != NULL;
2180 * Blocking AST for mdt locks.
2182 * Starts transaction commit if in case of COS lock conflict or
2183 * deffers such a commit to the mdt_save_lock.
2185 * \param lock the lock which blocks a request or cancelling lock
2186 * \param desc unused
2187 * \param data unused
2188 * \param flag indicates whether this cancelling or blocking callback
2190 * \see ldlm_blocking_ast_nocheck
2192 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2193 void *data, int flag)
2195 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2196 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2200 if (flag == LDLM_CB_CANCELING)
2202 lock_res_and_lock(lock);
2203 if (lock->l_blocking_ast != mdt_blocking_ast) {
2204 unlock_res_and_lock(lock);
2207 if (mdt_cos_is_enabled(mdt) &&
2208 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2209 lock->l_blocking_lock != NULL &&
2210 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2211 mdt_set_lock_sync(lock);
2213 rc = ldlm_blocking_ast_nocheck(lock);
2215 /* There is no lock conflict if l_blocking_lock == NULL,
2216 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2217 * when the last reference to a local lock was released */
2218 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2221 rc = lu_env_init(&env, LCT_LOCAL);
2222 if (unlikely(rc != 0))
2223 CWARN("lu_env initialization failed with rc = %d,"
2224 "cannot start asynchronous commit\n", rc);
2226 mdt_device_commit_async(&env, mdt);
2232 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2233 void *data, int flag)
2235 struct lustre_handle lockh;
2239 case LDLM_CB_BLOCKING:
2240 ldlm_lock2handle(lock, &lockh);
2241 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2243 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2247 case LDLM_CB_CANCELING:
2248 LDLM_DEBUG(lock, "Revoke remote lock\n");
2256 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2257 struct mdt_object *o, struct lustre_handle *lh,
2258 ldlm_mode_t mode, __u64 ibits)
2260 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2261 ldlm_policy_data_t *policy = &mti->mti_policy;
2265 LASSERT(mdt_object_remote(o));
2267 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2269 memset(einfo, 0, sizeof(*einfo));
2270 einfo->ei_type = LDLM_IBITS;
2271 einfo->ei_mode = mode;
2272 einfo->ei_cb_bl = mdt_md_blocking_ast;
2273 einfo->ei_cb_cp = ldlm_completion_ast;
2275 memset(policy, 0, sizeof(*policy));
2276 policy->l_inodebits.bits = ibits;
2278 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2283 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2284 struct mdt_lock_handle *lh, __u64 ibits,
2285 bool nonblock, int locality)
2287 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2288 ldlm_policy_data_t *policy = &info->mti_policy;
2289 struct ldlm_res_id *res_id = &info->mti_res_id;
2294 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2295 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2296 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2297 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2299 if (mdt_object_remote(o)) {
2300 if (locality == MDT_CROSS_LOCK) {
2301 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2302 MDS_INODELOCK_LAYOUT);
2303 ibits |= MDS_INODELOCK_LOOKUP;
2306 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2307 MDS_INODELOCK_LAYOUT)),
2308 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2309 mdt_obd_name(info->mti_mdt), ibits,
2310 PFID(mdt_object_fid(o)));
2311 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2313 /* No PDO lock on remote object */
2314 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2317 if (lh->mlh_type == MDT_PDO_LOCK) {
2318 /* check for exists after object is locked */
2319 if (mdt_object_exists(o) == 0) {
2320 /* Non-existent object shouldn't have PDO lock */
2323 /* Non-dir object shouldn't have PDO lock */
2324 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2329 memset(policy, 0, sizeof(*policy));
2330 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2332 dlmflags = LDLM_FL_ATOMIC_CB;
2334 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2337 * Take PDO lock on whole directory and build correct @res_id for lock
2338 * on part of directory.
2340 if (lh->mlh_pdo_hash != 0) {
2341 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2342 mdt_lock_pdo_mode(info, o, lh);
2343 if (lh->mlh_pdo_mode != LCK_NL) {
2345 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2346 * is never going to be sent to client and we do not
2347 * want it slowed down due to possible cancels.
2349 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2350 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2351 policy, res_id, dlmflags,
2352 &info->mti_exp->exp_handle.h_cookie);
2358 * Finish res_id initializing by name hash marking part of
2359 * directory which is taking modification.
2361 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2364 policy->l_inodebits.bits = ibits;
2367 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2368 * going to be sent to client. If it is - mdt_intent_policy() path will
2369 * fix it up and turn FL_LOCAL flag off.
2371 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2372 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2373 &info->mti_exp->exp_handle.h_cookie);
2375 mdt_object_unlock(info, o, lh, 1);
2376 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2377 lh->mlh_pdo_hash != 0 &&
2378 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2379 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2385 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2386 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2388 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2391 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2392 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2394 struct mdt_lock_handle tmp = *lh;
2397 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2405 * Save a lock within request object.
2407 * Keep the lock referenced until whether client ACK or transaction
2408 * commit happens or release the lock immediately depending on input
2409 * parameters. If COS is ON, a write lock is converted to COS lock
2412 * \param info thead info object
2413 * \param h lock handle
2414 * \param mode lock mode
2415 * \param decref force immediate lock releasing
2418 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2419 ldlm_mode_t mode, int decref)
2423 if (lustre_handle_is_used(h)) {
2424 if (decref || !info->mti_has_trans ||
2425 !(mode & (LCK_PW | LCK_EX))){
2426 mdt_fid_unlock(h, mode);
2428 struct mdt_device *mdt = info->mti_mdt;
2429 struct ldlm_lock *lock = ldlm_handle2lock(h);
2430 struct ptlrpc_request *req = mdt_info_req(info);
2433 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2435 /* there is no request if mdt_object_unlock() is called
2436 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2437 if (likely(req != NULL)) {
2438 CDEBUG(D_HA, "request = %p reply state = %p"
2439 " transno = "LPD64"\n", req,
2440 req->rq_reply_state, req->rq_transno);
2441 if (mdt_cos_is_enabled(mdt)) {
2443 ldlm_lock_downgrade(lock, LCK_COS);
2446 ptlrpc_save_lock(req, h, mode, no_ack);
2448 ldlm_lock_decref(h, mode);
2450 if (mdt_is_lock_sync(lock)) {
2451 CDEBUG(D_HA, "found sync-lock,"
2452 " async commit started\n");
2453 mdt_device_commit_async(info->mti_env,
2456 LDLM_LOCK_PUT(lock);
2465 * Unlock mdt object.
2467 * Immeditely release the regular lock and the PDO lock or save the
2468 * lock in reqeuest and keep them referenced until client ACK or
2469 * transaction commit.
2471 * \param info thread info object
2472 * \param o mdt object
2473 * \param lh mdt lock handle referencing regular and PDO locks
2474 * \param decref force immediate lock releasing
2476 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2477 struct mdt_lock_handle *lh, int decref)
2481 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2482 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2484 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2485 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2490 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2491 const struct lu_fid *f,
2492 struct mdt_lock_handle *lh,
2495 struct mdt_object *o;
2497 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2501 rc = mdt_object_lock(info, o, lh, ibits,
2504 mdt_object_put(info->mti_env, o);
2511 void mdt_object_unlock_put(struct mdt_thread_info * info,
2512 struct mdt_object * o,
2513 struct mdt_lock_handle *lh,
2516 mdt_object_unlock(info, o, lh, decref);
2517 mdt_object_put(info->mti_env, o);
2521 * Generic code handling requests that have struct mdt_body passed in:
2523 * - extract mdt_body from request and save it in @info, if present;
2525 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2528 * - if HABEO_CORPUS flag is set for this request type check whether object
2529 * actually exists on storage (lu_object_exists()).
2532 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2534 const struct mdt_body *body;
2535 struct mdt_object *obj;
2536 const struct lu_env *env;
2537 struct req_capsule *pill;
2541 env = info->mti_env;
2542 pill = info->mti_pill;
2544 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2548 if (!(body->valid & OBD_MD_FLID))
2551 if (!fid_is_sane(&body->fid1)) {
2552 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2557 * Do not get size or any capa fields before we check that request
2558 * contains capa actually. There are some requests which do not, for
2559 * instance MDS_IS_SUBDIR.
2561 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2562 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2563 mdt_set_capainfo(info, 0, &body->fid1,
2564 req_capsule_client_get(pill, &RMF_CAPA1));
2566 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2568 if ((flags & HABEO_CORPUS) &&
2569 !mdt_object_exists(obj)) {
2570 mdt_object_put(env, obj);
2571 /* for capability renew ENOENT will be handled in
2573 if (body->valid & OBD_MD_FLOSSCAPA)
2578 info->mti_object = obj;
2587 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2589 struct req_capsule *pill = info->mti_pill;
2593 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2594 rc = mdt_body_unpack(info, flags);
2598 if (rc == 0 && (flags & HABEO_REFERO)) {
2600 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2601 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2602 info->mti_body->eadatasize);
2603 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2604 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2607 rc = req_capsule_server_pack(pill);
2612 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2614 struct md_device *next = m->mdt_child;
2616 return next->md_ops->mdo_init_capa_ctxt(env, next,
2617 m->mdt_lut.lut_mds_capa,
2618 m->mdt_capa_timeout,
2623 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2625 lh->mlh_type = MDT_NUL_LOCK;
2626 lh->mlh_reg_lh.cookie = 0ull;
2627 lh->mlh_reg_mode = LCK_MINMODE;
2628 lh->mlh_pdo_lh.cookie = 0ull;
2629 lh->mlh_pdo_mode = LCK_MINMODE;
2630 lh->mlh_rreg_lh.cookie = 0ull;
2631 lh->mlh_rreg_mode = LCK_MINMODE;
2634 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2636 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2637 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2641 * Initialize fields of struct mdt_thread_info. Other fields are left in
2642 * uninitialized state, because it's too expensive to zero out whole
2643 * mdt_thread_info (> 1K) on each request arrival.
2645 void mdt_thread_info_init(struct ptlrpc_request *req,
2646 struct mdt_thread_info *info)
2650 info->mti_pill = &req->rq_pill;
2653 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2654 mdt_lock_handle_init(&info->mti_lh[i]);
2656 /* mdt device: it can be NULL while CONNECT */
2657 if (req->rq_export) {
2658 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2659 info->mti_exp = req->rq_export;
2661 info->mti_mdt = NULL;
2662 info->mti_env = req->rq_svc_thread->t_env;
2663 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2665 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2666 info->mti_big_buf = LU_BUF_NULL;
2667 info->mti_body = NULL;
2668 info->mti_object = NULL;
2669 info->mti_dlm_req = NULL;
2670 info->mti_has_trans = 0;
2671 info->mti_cross_ref = 0;
2672 info->mti_opdata = 0;
2673 info->mti_big_lmm_used = 0;
2675 info->mti_spec.no_create = 0;
2676 info->mti_spec.sp_rm_entry = 0;
2679 void mdt_thread_info_fini(struct mdt_thread_info *info)
2683 if (info->mti_object != NULL) {
2684 mdt_object_put(info->mti_env, info->mti_object);
2685 info->mti_object = NULL;
2688 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2689 mdt_lock_handle_fini(&info->mti_lh[i]);
2690 info->mti_env = NULL;
2691 info->mti_pill = NULL;
2692 info->mti_exp = NULL;
2694 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2695 lu_buf_free(&info->mti_big_buf);
2698 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2700 struct mdt_thread_info *mti;
2701 struct lustre_capa *lc;
2703 mti = mdt_th_info(tsi->tsi_env);
2704 LASSERT(mti != NULL);
2706 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2707 if (tsi->tsi_corpus != NULL) {
2708 struct req_capsule *pill = tsi->tsi_pill;
2710 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2711 lu_object_get(tsi->tsi_corpus);
2714 * XXX: must be part of tgt_mdt_body_unpack but moved here
2715 * due to mdt_set_capainfo().
2717 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2718 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2719 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2720 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2723 mti->mti_body = tsi->tsi_mdt_body;
2724 mti->mti_dlm_req = tsi->tsi_dlm_req;
2729 int mdt_tgt_connect(struct tgt_session_info *tsi)
2731 struct ptlrpc_request *req = tgt_ses_req(tsi);
2736 rc = tgt_connect(tsi);
2740 rc = mdt_init_idmap(tsi);
2745 obd_disconnect(class_export_get(req->rq_export));
2764 static int mdt_intent_getattr(enum mdt_it_code opcode,
2765 struct mdt_thread_info *info,
2766 struct ldlm_lock **,
2769 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2770 struct mdt_thread_info *info,
2771 struct ldlm_lock **lockp,
2774 static int mdt_intent_layout(enum mdt_it_code opcode,
2775 struct mdt_thread_info *info,
2776 struct ldlm_lock **,
2778 static int mdt_intent_reint(enum mdt_it_code opcode,
2779 struct mdt_thread_info *info,
2780 struct ldlm_lock **,
2783 static struct mdt_it_flavor {
2784 const struct req_format *it_fmt;
2786 int (*it_act)(enum mdt_it_code ,
2787 struct mdt_thread_info *,
2788 struct ldlm_lock **,
2791 } mdt_it_flavor[] = {
2793 .it_fmt = &RQF_LDLM_INTENT,
2794 /*.it_flags = HABEO_REFERO,*/
2796 .it_act = mdt_intent_reint,
2797 .it_reint = REINT_OPEN
2800 .it_fmt = &RQF_LDLM_INTENT,
2802 * OCREAT is not a MUTABOR request as if the file
2804 * We do the extra check of OBD_CONNECT_RDONLY in
2805 * mdt_reint_open() when we really need to create
2809 .it_act = mdt_intent_reint,
2810 .it_reint = REINT_OPEN
2813 .it_fmt = &RQF_LDLM_INTENT,
2814 .it_flags = MUTABOR,
2815 .it_act = mdt_intent_reint,
2816 .it_reint = REINT_CREATE
2818 [MDT_IT_GETATTR] = {
2819 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2820 .it_flags = HABEO_REFERO,
2821 .it_act = mdt_intent_getattr
2823 [MDT_IT_READDIR] = {
2829 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2830 .it_flags = HABEO_REFERO,
2831 .it_act = mdt_intent_getattr
2834 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2835 .it_flags = MUTABOR,
2837 .it_reint = REINT_UNLINK
2841 .it_flags = MUTABOR,
2844 [MDT_IT_GETXATTR] = {
2845 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2846 .it_flags = HABEO_CORPUS,
2847 .it_act = mdt_intent_getxattr
2850 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2852 .it_act = mdt_intent_layout
2856 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2857 struct ldlm_lock **lockp,
2858 struct ldlm_lock *new_lock,
2859 struct mdt_lock_handle *lh,
2862 struct ptlrpc_request *req = mdt_info_req(info);
2863 struct ldlm_lock *lock = *lockp;
2866 * Get new lock only for cases when possible resent did not find any
2869 if (new_lock == NULL)
2870 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2872 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2873 lh->mlh_reg_lh.cookie = 0;
2877 LASSERTF(new_lock != NULL,
2878 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2881 * If we've already given this lock to a client once, then we should
2882 * have no readers or writers. Otherwise, we should have one reader
2883 * _or_ writer ref (which will be zeroed below) before returning the
2886 if (new_lock->l_export == req->rq_export) {
2887 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2889 LASSERT(new_lock->l_export == NULL);
2890 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2895 if (new_lock->l_export == req->rq_export) {
2897 * Already gave this to the client, which means that we
2898 * reconstructed a reply.
2900 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2902 lh->mlh_reg_lh.cookie = 0;
2903 RETURN(ELDLM_LOCK_REPLACED);
2907 * Fixup the lock to be given to the client.
2909 lock_res_and_lock(new_lock);
2910 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2911 * possible blocking AST. */
2912 while (new_lock->l_readers > 0) {
2913 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2914 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2915 new_lock->l_readers--;
2917 while (new_lock->l_writers > 0) {
2918 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2919 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2920 new_lock->l_writers--;
2923 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2924 new_lock->l_blocking_ast = lock->l_blocking_ast;
2925 new_lock->l_completion_ast = lock->l_completion_ast;
2926 new_lock->l_remote_handle = lock->l_remote_handle;
2927 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2929 unlock_res_and_lock(new_lock);
2931 cfs_hash_add(new_lock->l_export->exp_lock_hash,
2932 &new_lock->l_remote_handle,
2933 &new_lock->l_exp_hash);
2935 LDLM_LOCK_RELEASE(new_lock);
2936 lh->mlh_reg_lh.cookie = 0;
2938 RETURN(ELDLM_LOCK_REPLACED);
2941 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2942 struct ldlm_lock *new_lock,
2943 struct ldlm_lock **old_lock,
2944 struct mdt_lock_handle *lh,
2945 enum mdt_it_code opcode)
2947 struct ptlrpc_request *req = mdt_info_req(info);
2948 struct obd_export *exp = req->rq_export;
2949 struct lustre_handle remote_hdl;
2950 struct ldlm_request *dlmreq;
2951 struct ldlm_lock *lock;
2953 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2956 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2957 remote_hdl = dlmreq->lock_handle[0];
2958 /* If the client does not require open lock, it does not need to
2959 * search lock in exp_lock_hash, since the server thread will
2960 * make sure the lock will be released, and the resend request
2961 * can always re-enqueue the lock */
2962 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
2963 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
2964 /* In the function below, .hs_keycmp resolves to
2965 * ldlm_export_lock_keycmp() */
2966 /* coverity[overrun-buffer-val] */
2967 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
2969 lock_res_and_lock(lock);
2970 if (lock != new_lock) {
2971 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2972 lh->mlh_reg_mode = lock->l_granted_mode;
2974 LDLM_DEBUG(lock, "Restoring lock cookie");
2975 DEBUG_REQ(D_DLMTRACE, req,
2976 "restoring lock cookie "LPX64,
2977 lh->mlh_reg_lh.cookie);
2979 *old_lock = LDLM_LOCK_GET(lock);
2980 cfs_hash_put(exp->exp_lock_hash,
2982 unlock_res_and_lock(lock);
2985 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2986 unlock_res_and_lock(lock);
2990 * If the xid matches, then we know this is a resent request, and allow
2991 * it. (It's probably an OPEN, for which we don't send a lock.
2993 if (req_xid_is_last(req))
2997 * This remote handle isn't enqueued, so we never received or processed
2998 * this request. Clear MSG_RESENT, because it can be handled like any
2999 * normal request now.
3001 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3003 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3007 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3008 struct mdt_thread_info *info,
3009 struct ldlm_lock **lockp,
3012 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3013 struct ldlm_reply *ldlm_rep = NULL;
3017 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3018 * (for the resend case) or a new lock. Below we will use it to
3019 * replace the original lock.
3021 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3022 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3023 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3024 rc = mdt_object_lock(info, info->mti_object, lhc,
3025 MDS_INODELOCK_XATTR,
3031 grc = mdt_getxattr(info);
3033 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3035 if (mdt_info_req(info)->rq_repmsg != NULL)
3036 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3037 if (ldlm_rep == NULL)
3038 RETURN(err_serious(-EFAULT));
3040 ldlm_rep->lock_policy_res2 = grc;
3045 static int mdt_intent_getattr(enum mdt_it_code opcode,
3046 struct mdt_thread_info *info,
3047 struct ldlm_lock **lockp,
3050 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3051 struct ldlm_lock *new_lock = NULL;
3053 struct ldlm_reply *ldlm_rep;
3054 struct ptlrpc_request *req;
3055 struct mdt_body *reqbody;
3056 struct mdt_body *repbody;
3060 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3063 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3066 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3067 repbody->eadatasize = 0;
3068 repbody->aclsize = 0;
3072 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3074 case MDT_IT_GETATTR:
3075 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3079 CERROR("Unsupported intent (%d)\n", opcode);
3080 GOTO(out_shrink, rc = -EINVAL);
3083 rc = mdt_init_ucred(info, reqbody);
3085 GOTO(out_shrink, rc);
3087 req = info->mti_pill->rc_req;
3088 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3089 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3091 /* Get lock from request for possible resent case. */
3092 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3094 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3095 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3097 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3098 ldlm_rep->lock_policy_res2 = 0;
3099 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3100 ldlm_rep->lock_policy_res2) {
3101 lhc->mlh_reg_lh.cookie = 0ull;
3102 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3105 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3108 mdt_exit_ucred(info);
3110 mdt_client_compatibility(info);
3111 rc2 = mdt_fix_reply(info);
3117 static int mdt_intent_layout(enum mdt_it_code opcode,
3118 struct mdt_thread_info *info,
3119 struct ldlm_lock **lockp,
3122 struct layout_intent *layout;
3124 struct mdt_object *obj = NULL;
3125 struct md_object *child = NULL;
3129 if (opcode != MDT_IT_LAYOUT) {
3130 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3135 fid = &info->mti_tmp_fid2;
3136 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3138 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3140 RETURN(PTR_ERR(obj));
3142 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3143 child = mdt_object_child(obj);
3145 /* get the length of lsm */
3146 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3149 if (rc > info->mti_mdt->mdt_max_mdsize)
3150 info->mti_mdt->mdt_max_mdsize = rc;
3153 mdt_object_put(info->mti_env, obj);
3155 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3156 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3157 ldlm_lvbo_size(*lockp));
3158 rc = req_capsule_server_pack(info->mti_pill);
3162 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3163 LASSERT(layout != NULL);
3164 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3165 /* return to normal ldlm handling */
3168 CERROR("%s: Unsupported layout intent (%d)\n",
3169 mdt_obd_name(info->mti_mdt), layout->li_opc);
3173 static int mdt_intent_reint(enum mdt_it_code opcode,
3174 struct mdt_thread_info *info,
3175 struct ldlm_lock **lockp,
3178 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3179 struct ldlm_reply *rep = NULL;
3183 static const struct req_format *intent_fmts[REINT_MAX] = {
3184 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3185 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3190 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3194 if (mdt_it_flavor[opcode].it_reint != opc) {
3195 CERROR("Reint code %ld doesn't match intent: %d\n",
3197 RETURN(err_serious(-EPROTO));
3200 /* Get lock from request for possible resent case. */
3201 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3203 rc = mdt_reint_internal(info, lhc, opc);
3205 /* Check whether the reply has been packed successfully. */
3206 if (mdt_info_req(info)->rq_repmsg != NULL)
3207 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3209 RETURN(err_serious(-EFAULT));
3211 /* MDC expects this in any case */
3213 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3215 /* the open lock or the lock for cross-ref object should be
3216 * returned to the client */
3217 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3218 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3219 rep->lock_policy_res2 = 0;
3220 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3224 rep->lock_policy_res2 = clear_serious(rc);
3226 if (rep->lock_policy_res2 == -ENOENT &&
3227 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3228 rep->lock_policy_res2 = 0;
3230 if (rc == -ENOTCONN || rc == -ENODEV ||
3231 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3233 * If it is the disconnect error (ENODEV & ENOCONN), the error
3234 * will be returned by rq_status, and client at ptlrpc layer
3235 * will detect this, then disconnect, reconnect the import
3236 * immediately, instead of impacting the following the rpc.
3238 lhc->mlh_reg_lh.cookie = 0ull;
3242 * For other cases, the error will be returned by intent.
3243 * and client will retrieve the result from intent.
3246 * FIXME: when open lock is finished, that should be
3249 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3250 LASSERTF(rc == 0, "Error occurred but lock handle "
3251 "is still in use, rc = %d\n", rc);
3252 rep->lock_policy_res2 = 0;
3253 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3256 lhc->mlh_reg_lh.cookie = 0ull;
3257 RETURN(ELDLM_LOCK_ABORTED);
3262 static int mdt_intent_code(long itcode)
3270 case IT_OPEN|IT_CREAT:
3277 rc = MDT_IT_READDIR;
3280 rc = MDT_IT_GETATTR;
3292 rc = MDT_IT_GETXATTR;
3297 case IT_QUOTA_DQACQ:
3302 CERROR("Unknown intent opcode: %ld\n", itcode);
3309 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3310 struct ldlm_lock **lockp, __u64 flags)
3312 struct req_capsule *pill;
3313 struct mdt_it_flavor *flv;
3318 opc = mdt_intent_code(itopc);
3322 pill = info->mti_pill;
3324 if (opc == MDT_IT_QUOTA) {
3325 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3328 RETURN(-EOPNOTSUPP);
3330 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3331 /* pass the request to quota master */
3332 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3333 mdt_info_req(info), lockp,
3338 flv = &mdt_it_flavor[opc];
3339 if (flv->it_fmt != NULL)
3340 req_capsule_extend(pill, flv->it_fmt);
3342 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3344 struct ptlrpc_request *req = mdt_info_req(info);
3345 if (flv->it_flags & MUTABOR &&
3346 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3349 if (rc == 0 && flv->it_act != NULL) {
3350 struct ldlm_reply *rep;
3352 /* execute policy */
3353 rc = flv->it_act(opc, info, lockp, flags);
3355 /* Check whether the reply has been packed successfully. */
3356 if (mdt_info_req(info)->rq_repmsg != NULL) {
3357 rep = req_capsule_server_get(info->mti_pill,
3359 rep->lock_policy_res2 =
3360 ptlrpc_status_hton(rep->lock_policy_res2);
3368 static int mdt_intent_policy(struct ldlm_namespace *ns,
3369 struct ldlm_lock **lockp, void *req_cookie,
3370 ldlm_mode_t mode, __u64 flags, void *data)
3372 struct tgt_session_info *tsi;
3373 struct mdt_thread_info *info;
3374 struct ptlrpc_request *req = req_cookie;
3375 struct ldlm_intent *it;
3376 struct req_capsule *pill;
3381 LASSERT(req != NULL);
3383 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3385 info = tsi2mdt_info(tsi);
3386 LASSERT(info != NULL);
3387 pill = info->mti_pill;
3388 LASSERT(pill->rc_req == req);
3390 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3391 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3392 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3394 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3398 /* Lock without inodebits makes no sense and will oops
3399 * later in ldlm. Let's check it now to see if we have
3400 * ibits corrupted somewhere in mdt_intent_opc().
3401 * The case for client miss to set ibits has been
3402 * processed by others. */
3403 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3404 lr_type == LDLM_IBITS,
3405 info->mti_dlm_req->lock_desc.\
3406 l_policy_data.l_inodebits.bits != 0));
3408 rc = err_serious(-EFAULT);
3410 /* No intent was provided */
3411 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3412 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3413 rc = req_capsule_server_pack(pill);
3415 rc = err_serious(rc);
3417 mdt_thread_info_fini(info);
3421 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3423 struct seq_server_site *ss = mdt_seq_site(mdt);
3425 if (ss->ss_node_id == 0)
3428 if (ss->ss_client_seq != NULL) {
3429 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3430 ss->ss_client_seq->lcs_exp = NULL;
3433 if (ss->ss_server_fld != NULL) {
3434 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3435 ss->ss_server_fld->lsf_control_exp = NULL;
3439 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3441 struct seq_server_site *ss = mdt_seq_site(mdt);
3446 if (ss->ss_server_seq == NULL)
3447 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3452 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3454 mdt_seq_fini_cli(mdt);
3455 mdt_deregister_seq_exp(mdt);
3457 return seq_site_fini(env, mdt_seq_site(mdt));
3461 * It will retrieve its FLDB entries from MDT0, and it only happens
3462 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3463 * and it needs to refresh FLDB from the MDT0.
3465 static int mdt_register_lwp_callback(void *data)
3468 struct mdt_device *mdt = data;
3469 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3473 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3475 if (!likely(fld->lsf_new))
3478 rc = lu_env_init(&env, LCT_MD_THREAD);
3480 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3484 rc = fld_update_from_controller(&env, fld);
3486 CERROR("%s: cannot update controller: rc = %d\n",
3487 mdt_obd_name(mdt), rc);
3495 static int mdt_register_seq_exp(struct mdt_device *mdt)
3497 struct seq_server_site *ss = mdt_seq_site(mdt);
3498 char *lwp_name = NULL;
3501 if (ss->ss_node_id == 0)
3504 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3505 if (lwp_name == NULL)
3506 GOTO(out_free, rc = -ENOMEM);
3508 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3512 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3517 rc = lustre_register_lwp_item(lwp_name,
3518 &ss->ss_server_fld->lsf_control_exp,
3519 mdt_register_lwp_callback, mdt);
3521 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3522 ss->ss_client_seq->lcs_exp = NULL;
3526 if (lwp_name != NULL)
3527 OBD_FREE(lwp_name, MAX_OBD_NAME);
3533 * Init client sequence manager which is used by local MDS to talk to sequence
3534 * controller on remote node.
3536 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3538 struct seq_server_site *ss = mdt_seq_site(mdt);
3543 /* check if this is adding the first MDC and controller is not yet
3545 OBD_ALLOC_PTR(ss->ss_client_seq);
3546 if (ss->ss_client_seq == NULL)
3549 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3550 if (prefix == NULL) {
3551 OBD_FREE_PTR(ss->ss_client_seq);
3552 ss->ss_client_seq = NULL;
3556 /* Note: seq_client_fini will be called in seq_site_fini */
3557 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3558 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3559 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3561 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3563 OBD_FREE_PTR(ss->ss_client_seq);
3564 ss->ss_client_seq = NULL;
3568 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3573 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3575 struct seq_server_site *ss;
3579 ss = mdt_seq_site(mdt);
3580 /* init sequence controller server(MDT0) */
3581 if (ss->ss_node_id == 0) {
3582 OBD_ALLOC_PTR(ss->ss_control_seq);
3583 if (ss->ss_control_seq == NULL)
3586 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3587 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3590 GOTO(out_seq_fini, rc);
3593 /* Init normal sequence server */
3594 OBD_ALLOC_PTR(ss->ss_server_seq);
3595 if (ss->ss_server_seq == NULL)
3596 GOTO(out_seq_fini, rc = -ENOMEM);
3598 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3599 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3601 GOTO(out_seq_fini, rc);
3603 /* init seq client for seq server to talk to seq controller(MDT0) */
3604 rc = mdt_seq_init_cli(env, mdt);
3606 GOTO(out_seq_fini, rc);
3608 if (ss->ss_node_id != 0)
3609 /* register controler export through lwp */
3610 rc = mdt_register_seq_exp(mdt);
3615 mdt_seq_fini(env, mdt);
3623 static int mdt_fld_fini(const struct lu_env *env,
3624 struct mdt_device *m)
3626 struct seq_server_site *ss = mdt_seq_site(m);
3629 if (ss && ss->ss_server_fld) {
3630 fld_server_fini(env, ss->ss_server_fld);
3631 OBD_FREE_PTR(ss->ss_server_fld);
3632 ss->ss_server_fld = NULL;
3638 static int mdt_fld_init(const struct lu_env *env,
3640 struct mdt_device *m)
3642 struct seq_server_site *ss;
3646 ss = mdt_seq_site(m);
3648 OBD_ALLOC_PTR(ss->ss_server_fld);
3649 if (ss->ss_server_fld == NULL)
3650 RETURN(rc = -ENOMEM);
3652 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3655 OBD_FREE_PTR(ss->ss_server_fld);
3656 ss->ss_server_fld = NULL;
3663 static void mdt_stack_pre_fini(const struct lu_env *env,
3664 struct mdt_device *m, struct lu_device *top)
3666 struct obd_device *obd;
3667 struct lustre_cfg_bufs *bufs;
3668 struct lustre_cfg *lcfg;
3669 struct mdt_thread_info *info;
3674 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3675 LASSERT(info != NULL);
3677 bufs = &info->mti_u.bufs;
3679 LASSERT(m->mdt_child_exp);
3680 LASSERT(m->mdt_child_exp->exp_obd);
3681 obd = m->mdt_child_exp->exp_obd;
3683 /* process cleanup, pass mdt obd name to get obd umount flags */
3684 /* XXX: this is needed because all layers are referenced by
3685 * objects (some of them are pinned by osd, for example *
3686 * the proper solution should be a model where object used
3687 * by osd only doesn't have mdt/mdd slices -bzzz */
3688 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3689 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3690 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3692 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3695 top->ld_ops->ldo_process_config(env, top, lcfg);
3696 lustre_cfg_free(lcfg);
3700 static void mdt_stack_fini(const struct lu_env *env,
3701 struct mdt_device *m, struct lu_device *top)
3703 struct obd_device *obd = mdt2obd_dev(m);
3704 struct lustre_cfg_bufs *bufs;
3705 struct lustre_cfg *lcfg;
3706 struct mdt_thread_info *info;
3710 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3711 LASSERT(info != NULL);
3713 lu_dev_del_linkage(top->ld_site, top);
3715 lu_site_purge(env, top->ld_site, -1);
3717 bufs = &info->mti_u.bufs;
3718 /* process cleanup, pass mdt obd name to get obd umount flags */
3719 /* another purpose is to let all layers to release their objects */
3720 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3725 lustre_cfg_bufs_set_string(bufs, 1, flags);
3726 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3728 CERROR("Cannot alloc lcfg!\n");
3732 top->ld_ops->ldo_process_config(env, top, lcfg);
3733 lustre_cfg_free(lcfg);
3735 lu_site_purge(env, top->ld_site, -1);
3737 m->mdt_child = NULL;
3738 m->mdt_bottom = NULL;
3740 obd_disconnect(m->mdt_child_exp);
3741 m->mdt_child_exp = NULL;
3743 obd_disconnect(m->mdt_bottom_exp);
3744 m->mdt_child_exp = NULL;
3747 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3748 const char *next, struct obd_export **exp)
3750 struct obd_connect_data *data = NULL;
3751 struct obd_device *obd;
3755 OBD_ALLOC_PTR(data);
3757 GOTO(out, rc = -ENOMEM);
3759 obd = class_name2obd(next);
3761 CERROR("%s: can't locate next device: %s\n",
3762 mdt_obd_name(m), next);
3763 GOTO(out, rc = -ENOTCONN);
3766 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3767 data->ocd_version = LUSTRE_VERSION_CODE;
3769 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3771 CERROR("%s: cannot connect to next dev %s (%d)\n",
3772 mdt_obd_name(m), next, rc);
3782 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3783 struct lustre_cfg *cfg)
3785 char *dev = lustre_cfg_string(cfg, 0);
3786 int rc, name_size, uuid_size;
3787 char *name, *uuid, *p;
3788 struct lustre_cfg_bufs *bufs;
3789 struct lustre_cfg *lcfg;
3790 struct obd_device *obd;
3791 struct lustre_profile *lprof;
3792 struct lu_site *site;
3795 /* in 1.8 we had the only device in the stack - MDS.
3796 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3797 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3798 * to generate names and setup MDT, MDD. MDT will be using
3799 * generated name to connect to MDD. for MDD the next device
3800 * will be LOD with name taken from so called "profile" which
3801 * is generated by mount_option line
3803 * 1.8 MGS generates config. commands like this:
3804 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3805 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3806 * 2.0 MGS generates config. commands like this:
3807 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3808 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3809 * 3:lustre-MDT0000-mdtlov 4:f
3811 * we generate MDD name from MDT one, just replacing T with D
3813 * after all the preparations, the logical equivalent will be
3814 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3815 * 3:lustre-MDT0000-mdtlov 4:f
3816 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3817 * 3:lustre-MDD0000 4:f
3819 * notice we build the stack from down to top: MDD first, then MDT */
3821 name_size = MAX_OBD_NAME;
3822 uuid_size = MAX_OBD_NAME;
3824 OBD_ALLOC(name, name_size);
3825 OBD_ALLOC(uuid, uuid_size);
3826 if (name == NULL || uuid == NULL)
3827 GOTO(cleanup_mem, rc = -ENOMEM);
3829 OBD_ALLOC_PTR(bufs);
3831 GOTO(cleanup_mem, rc = -ENOMEM);
3834 p = strstr(name, "-MDT");
3836 GOTO(free_bufs, rc = -ENOMEM);
3839 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3841 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3842 if (lprof == NULL || lprof->lp_dt == NULL) {
3843 CERROR("can't find the profile: %s\n",
3844 lustre_cfg_string(cfg, 0));
3845 GOTO(free_bufs, rc = -EINVAL);
3848 lustre_cfg_bufs_reset(bufs, name);
3849 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3850 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3851 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3853 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3855 GOTO(free_bufs, rc = -ENOMEM);
3857 rc = class_attach(lcfg);
3859 GOTO(lcfg_cleanup, rc);
3861 obd = class_name2obd(name);
3863 CERROR("Can not find obd %s (%s in config)\n",
3864 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
3865 GOTO(class_detach, rc = -EINVAL);
3868 lustre_cfg_free(lcfg);
3870 lustre_cfg_bufs_reset(bufs, name);
3871 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3872 lustre_cfg_bufs_set_string(bufs, 2, dev);
3873 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3875 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3877 rc = class_setup(obd, lcfg);
3879 GOTO(class_detach, rc);
3881 /* connect to MDD we just setup */
3882 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
3884 GOTO(class_detach, rc);
3886 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
3888 LASSERT(mdt_lu_site(mdt) == NULL);
3889 mdt->mdt_lu_dev.ld_site = site;
3890 site->ls_top_dev = &mdt->mdt_lu_dev;
3891 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
3894 /* now connect to bottom OSD */
3895 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
3896 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
3898 GOTO(class_detach, rc);
3900 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
3903 rc = lu_env_refill((struct lu_env *)env);
3905 CERROR("Failure to refill session: '%d'\n", rc);
3907 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
3912 class_detach(obd, lcfg);
3914 lustre_cfg_free(lcfg);
3919 OBD_FREE(name, name_size);
3921 OBD_FREE(uuid, uuid_size);
3925 /* setup quota master target on MDT0 */
3926 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
3927 struct lustre_cfg *cfg)
3929 struct obd_device *obd;
3930 char *dev = lustre_cfg_string(cfg, 0);
3931 char *qmtname, *uuid, *p;
3932 struct lustre_cfg_bufs *bufs;
3933 struct lustre_cfg *lcfg;
3934 struct lustre_profile *lprof;
3935 struct obd_connect_data *data;
3939 LASSERT(mdt->mdt_qmt_exp == NULL);
3940 LASSERT(mdt->mdt_qmt_dev == NULL);
3942 /* quota master is on MDT0 only for now */
3943 if (mdt->mdt_seq_site.ss_node_id != 0)
3946 /* MGS generates config commands which look as follows:
3947 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3948 * 3:lustre-MDT0000-mdtlov 4:f
3950 * We generate the QMT name from the MDT one, just replacing MD with QM
3951 * after all the preparations, the logical equivalent will be:
3952 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
3953 * 3:lustre-MDT0000-osd 4:f */
3954 OBD_ALLOC(qmtname, MAX_OBD_NAME);
3955 OBD_ALLOC(uuid, UUID_MAX);
3956 OBD_ALLOC_PTR(bufs);
3957 OBD_ALLOC_PTR(data);
3958 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
3959 GOTO(cleanup_mem, rc = -ENOMEM);
3961 strcpy(qmtname, dev);
3962 p = strstr(qmtname, "-MDT");
3964 GOTO(cleanup_mem, rc = -ENOMEM);
3965 /* replace MD with QM */
3969 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
3971 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3972 if (lprof == NULL || lprof->lp_dt == NULL) {
3973 CERROR("can't find profile for %s\n",
3974 lustre_cfg_string(cfg, 0));
3975 GOTO(cleanup_mem, rc = -EINVAL);
3978 lustre_cfg_bufs_reset(bufs, qmtname);
3979 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
3980 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3981 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3983 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3985 GOTO(cleanup_mem, rc = -ENOMEM);
3987 rc = class_attach(lcfg);
3989 GOTO(lcfg_cleanup, rc);
3991 obd = class_name2obd(qmtname);
3993 CERROR("Can not find obd %s (%s in config)\n", qmtname,
3994 lustre_cfg_string(cfg, 0));
3995 GOTO(class_detach, rc = -EINVAL);
3998 lustre_cfg_free(lcfg);
4000 lustre_cfg_bufs_reset(bufs, qmtname);
4001 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4002 lustre_cfg_bufs_set_string(bufs, 2, dev);
4004 /* for quota, the next device should be the OSD device */
4005 lustre_cfg_bufs_set_string(bufs, 3,
4006 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4008 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4010 rc = class_setup(obd, lcfg);
4012 GOTO(class_detach, rc);
4014 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4016 /* configure local quota objects */
4017 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4021 GOTO(class_cleanup, rc);
4023 /* connect to quota master target */
4024 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4025 data->ocd_version = LUSTRE_VERSION_CODE;
4026 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4029 CERROR("cannot connect to quota master device %s (%d)\n",
4031 GOTO(class_cleanup, rc);
4037 class_manual_cleanup(obd);
4038 mdt->mdt_qmt_dev = NULL;
4042 class_detach(obd, lcfg);
4044 lustre_cfg_free(lcfg);
4049 OBD_FREE(qmtname, MAX_OBD_NAME);
4051 OBD_FREE(uuid, UUID_MAX);
4057 /* Shutdown quota master target associated with mdt */
4058 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4062 if (mdt->mdt_qmt_exp == NULL)
4064 LASSERT(mdt->mdt_qmt_dev != NULL);
4066 /* the qmt automatically shuts down when the mdt disconnects */
4067 obd_disconnect(mdt->mdt_qmt_exp);
4068 mdt->mdt_qmt_exp = NULL;
4069 mdt->mdt_qmt_dev = NULL;
4073 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4074 * for now. This will be removed along with converting rest of MDT code
4075 * to use tgt_session_info */
4076 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4078 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4081 rc = mdt_getxattr(info);
4083 mdt_thread_info_fini(info);
4087 static struct tgt_handler mdt_tgt_handlers[] = {
4088 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4089 0, MDS_CONNECT, mdt_tgt_connect,
4090 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4091 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4092 0, MDS_DISCONNECT, tgt_disconnect,
4093 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4094 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4095 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4096 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4097 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4098 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4099 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4100 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4102 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4103 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4104 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4105 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4106 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4108 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4109 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4110 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4111 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4112 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4114 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4115 mdt_hsm_ct_register),
4116 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4117 mdt_hsm_ct_unregister),
4118 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4120 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4122 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4123 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4125 TGT_MDT_HDL(HABEO_CORPUS|HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
4129 static struct tgt_handler mdt_sec_ctx_ops[] = {
4130 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4131 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4132 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4135 static struct tgt_handler mdt_quota_ops[] = {
4136 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4139 static struct tgt_opc_slice mdt_common_slice[] = {
4141 .tos_opc_start = MDS_FIRST_OPC,
4142 .tos_opc_end = MDS_LAST_OPC,
4143 .tos_hs = mdt_tgt_handlers
4146 .tos_opc_start = OBD_FIRST_OPC,
4147 .tos_opc_end = OBD_LAST_OPC,
4148 .tos_hs = tgt_obd_handlers
4151 .tos_opc_start = LDLM_FIRST_OPC,
4152 .tos_opc_end = LDLM_LAST_OPC,
4153 .tos_hs = tgt_dlm_handlers
4156 .tos_opc_start = SEC_FIRST_OPC,
4157 .tos_opc_end = SEC_LAST_OPC,
4158 .tos_hs = mdt_sec_ctx_ops
4161 .tos_opc_start = UPDATE_OBJ,
4162 .tos_opc_end = UPDATE_LAST_OPC,
4163 .tos_hs = tgt_out_handlers
4166 .tos_opc_start = FLD_FIRST_OPC,
4167 .tos_opc_end = FLD_LAST_OPC,
4168 .tos_hs = fld_handlers
4171 .tos_opc_start = SEQ_FIRST_OPC,
4172 .tos_opc_end = SEQ_LAST_OPC,
4173 .tos_hs = seq_handlers
4176 .tos_opc_start = QUOTA_DQACQ,
4177 .tos_opc_end = QUOTA_LAST_OPC,
4178 .tos_hs = mdt_quota_ops
4181 .tos_opc_start = LLOG_FIRST_OPC,
4182 .tos_opc_end = LLOG_LAST_OPC,
4183 .tos_hs = tgt_llog_handlers
4186 .tos_opc_start = LFSCK_FIRST_OPC,
4187 .tos_opc_end = LFSCK_LAST_OPC,
4188 .tos_hs = tgt_lfsck_handlers
4196 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4198 struct md_device *next = m->mdt_child;
4199 struct lu_device *d = &m->mdt_lu_dev;
4200 struct obd_device *obd = mdt2obd_dev(m);
4201 struct lfsck_stop stop;
4204 target_recovery_fini(obd);
4205 ping_evictor_stop();
4206 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4208 if (m->mdt_opts.mo_coordinator)
4209 mdt_hsm_cdt_stop(m);
4211 mdt_hsm_cdt_fini(m);
4213 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4214 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4215 obd_exports_barrier(obd);
4216 obd_zombie_barrier();
4220 tgt_fini(env, &m->mdt_lut);
4221 mdt_fs_cleanup(env, m);
4222 upcall_cache_cleanup(m->mdt_identity_cache);
4223 m->mdt_identity_cache = NULL;
4225 if (m->mdt_namespace != NULL) {
4226 ldlm_namespace_free(m->mdt_namespace, NULL,
4227 d->ld_obd->obd_force);
4228 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4231 mdt_quota_fini(env, m);
4233 cfs_free_nidlist(&m->mdt_nosquash_nids);
4234 if (m->mdt_nosquash_str) {
4235 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4236 m->mdt_nosquash_str = NULL;
4237 m->mdt_nosquash_strlen = 0;
4240 stop.ls_status = LS_PAUSED;
4242 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4244 mdt_seq_fini(env, m);
4245 mdt_fld_fini(env, m);
4247 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4248 cfs_timer_disarm(&m->mdt_ck_timer);
4249 mdt_ck_thread_stop(m);
4254 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4256 LASSERT(atomic_read(&d->ld_ref) == 0);
4258 server_put_mount(mdt_obd_name(m));
4263 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4265 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4266 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4268 struct mdt_thread_info *info;
4269 struct obd_device *obd;
4270 const char *dev = lustre_cfg_string(cfg, 0);
4271 const char *num = lustre_cfg_string(cfg, 2);
4272 struct lustre_mount_info *lmi = NULL;
4273 struct lustre_sb_info *lsi;
4275 struct seq_server_site *ss_site;
4276 const char *identity_upcall = "NONE";
4277 struct md_device *next;
4283 lu_device_init(&m->mdt_lu_dev, ldt);
4285 * Environment (env) might be missing mdt_thread_key values at that
4286 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4289 * Usually device allocation path doesn't use module key values, but
4290 * mdt has to do a lot of work here, so allocate key value.
4292 rc = lu_env_refill((struct lu_env *)env);
4296 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4297 LASSERT(info != NULL);
4299 obd = class_name2obd(dev);
4300 LASSERT(obd != NULL);
4302 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4304 m->mdt_som_conf = 0;
4306 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4308 /* default is coordinator off, it is started through conf_param
4310 m->mdt_opts.mo_coordinator = 0;
4312 lmi = server_get_mount(dev);
4314 CERROR("Cannot get mount info for %s!\n", dev);
4317 lsi = s2lsi(lmi->lmi_sb);
4318 /* CMD is supported only in IAM mode */
4320 node_id = simple_strtol(num, NULL, 10);
4321 obd->u.obt.obt_magic = OBT_MAGIC;
4324 spin_lock_init(&m->mdt_ioepoch_lock);
4325 m->mdt_capa_timeout = CAPA_TIMEOUT;
4326 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4327 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4328 m->mdt_squash_uid = 0;
4329 m->mdt_squash_gid = 0;
4330 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4331 m->mdt_nosquash_str = NULL;
4332 m->mdt_nosquash_strlen = 0;
4333 init_rwsem(&m->mdt_squash_sem);
4334 spin_lock_init(&m->mdt_osfs_lock);
4335 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4336 m->mdt_enable_remote_dir = 0;
4337 m->mdt_enable_remote_dir_gid = 0;
4339 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4340 m->mdt_lu_dev.ld_obd = obd;
4341 /* Set this lu_device to obd for error handling purposes. */
4342 obd->obd_lu_dev = &m->mdt_lu_dev;
4344 /* init the stack */
4345 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4347 CERROR("%s: Can't init device stack, rc %d\n",
4348 mdt_obd_name(m), rc);
4353 ss_site = mdt_seq_site(m);
4354 s->ld_seq_site = ss_site;
4357 /* set server index */
4358 ss_site->ss_node_id = node_id;
4360 /* failover is the default
4361 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4362 * assumed whose ss_node_id == 0 XXX
4364 obd->obd_replayable = 1;
4365 /* No connection accepted until configurations will finish */
4366 obd->obd_no_conn = 1;
4368 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4369 char *str = lustre_cfg_string(cfg, 4);
4370 if (strchr(str, 'n')) {
4371 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4372 obd->obd_replayable = 0;
4376 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4378 GOTO(err_fini_stack, rc);
4380 rc = mdt_seq_init(env, m);
4382 GOTO(err_fini_fld, rc);
4384 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4385 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4386 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4387 LDLM_NAMESPACE_SERVER,
4388 LDLM_NAMESPACE_GREEDY,
4390 if (m->mdt_namespace == NULL)
4391 GOTO(err_fini_seq, rc = -ENOMEM);
4393 m->mdt_namespace->ns_lvbp = m;
4394 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4396 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4397 /* set obd_namespace for compatibility with old code */
4398 obd->obd_namespace = m->mdt_namespace;
4400 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4402 rc = mdt_hsm_cdt_init(m);
4404 CERROR("%s: error initializing coordinator, rc %d\n",
4405 mdt_obd_name(m), rc);
4406 GOTO(err_free_ns, rc);
4409 rc = mdt_ck_thread_start(m);
4411 GOTO(err_free_hsm, rc);
4413 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4414 OBD_FAIL_MDS_ALL_REQUEST_NET,
4415 OBD_FAIL_MDS_ALL_REPLY_NET);
4419 rc = mdt_fs_setup(env, m, obd, lsi);
4423 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4425 next = m->mdt_child;
4426 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4429 GOTO(err_fs_cleanup, rc);
4431 if (mntopts & MNTOPT_USERXATTR)
4432 m->mdt_opts.mo_user_xattr = 1;
4434 m->mdt_opts.mo_user_xattr = 0;
4436 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4438 GOTO(err_fs_cleanup, rc);
4440 if (mntopts & MNTOPT_ACL)
4441 m->mdt_opts.mo_acl = 1;
4443 m->mdt_opts.mo_acl = 0;
4445 /* XXX: to support suppgid for ACL, we enable identity_upcall
4446 * by default, otherwise, maybe got unexpected -EACCESS. */
4447 if (m->mdt_opts.mo_acl)
4448 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4450 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4452 &mdt_identity_upcall_cache_ops);
4453 if (IS_ERR(m->mdt_identity_cache)) {
4454 rc = PTR_ERR(m->mdt_identity_cache);
4455 m->mdt_identity_cache = NULL;
4456 GOTO(err_fs_cleanup, rc);
4459 rc = mdt_procfs_init(m, dev);
4461 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4462 GOTO(err_recovery, rc);
4465 rc = mdt_quota_init(env, m, cfg);
4467 GOTO(err_procfs, rc);
4469 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4470 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4471 "mdt_ldlm_client", m->mdt_ldlm_client);
4473 ping_evictor_start();
4475 /* recovery will be started upon mdt_prepare()
4476 * when the whole stack is complete and ready
4477 * to serve the requests */
4479 mdt_init_capa_ctxt(env, m);
4481 /* Reduce the initial timeout on an MDS because it doesn't need such
4482 * a long timeout as an OST does. Adaptive timeouts will adjust this
4483 * value appropriately. */
4484 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4485 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4491 target_recovery_fini(obd);
4492 upcall_cache_cleanup(m->mdt_identity_cache);
4493 m->mdt_identity_cache = NULL;
4495 mdt_fs_cleanup(env, m);
4497 tgt_fini(env, &m->mdt_lut);
4499 cfs_timer_disarm(&m->mdt_ck_timer);
4500 mdt_ck_thread_stop(m);
4502 mdt_hsm_cdt_fini(m);
4504 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4505 obd->obd_namespace = m->mdt_namespace = NULL;
4507 mdt_seq_fini(env, m);
4509 mdt_fld_fini(env, m);
4511 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4514 server_put_mount(dev);
4518 /* For interoperability, the left element is old parameter, the right one
4519 * is the new version of the parameter, if some parameter is deprecated,
4520 * the new version should be set as NULL. */
4521 static struct cfg_interop_param mdt_interop_param[] = {
4522 { "mdt.group_upcall", NULL },
4523 { "mdt.quota_type", NULL },
4524 { "mdd.quota_type", NULL },
4525 { "mdt.rootsquash", "mdt.root_squash" },
4526 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4530 /* used by MGS to process specific configurations */
4531 static int mdt_process_config(const struct lu_env *env,
4532 struct lu_device *d, struct lustre_cfg *cfg)
4534 struct mdt_device *m = mdt_dev(d);
4535 struct md_device *md_next = m->mdt_child;
4536 struct lu_device *next = md2lu_dev(md_next);
4540 switch (cfg->lcfg_command) {
4542 struct lprocfs_static_vars lvars;
4543 struct obd_device *obd = d->ld_obd;
4545 /* For interoperability */
4546 struct cfg_interop_param *ptr = NULL;
4547 struct lustre_cfg *old_cfg = NULL;
4550 param = lustre_cfg_string(cfg, 1);
4551 if (param == NULL) {
4552 CERROR("param is empty\n");
4557 ptr = class_find_old_param(param, mdt_interop_param);
4559 if (ptr->new_param == NULL) {
4561 CWARN("For interoperability, skip this %s."
4562 " It is obsolete.\n", ptr->old_param);
4566 CWARN("Found old param %s, changed it to %s.\n",
4567 ptr->old_param, ptr->new_param);
4570 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4577 lprocfs_mdt_init_vars(&lvars);
4578 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4580 if (rc > 0 || rc == -ENOSYS) {
4581 /* is it an HSM var ? */
4582 rc = class_process_proc_param(PARAM_HSM,
4583 hsm_cdt_get_proc_vars(),
4585 if (rc > 0 || rc == -ENOSYS)
4586 /* we don't understand; pass it on */
4587 rc = next->ld_ops->ldo_process_config(env, next,
4591 if (old_cfg != NULL)
4592 lustre_cfg_free(cfg);
4597 /* others are passed further */
4598 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4604 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4605 const struct lu_object_header *hdr,
4606 struct lu_device *d)
4608 struct mdt_object *mo;
4612 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
4614 struct lu_object *o;
4615 struct lu_object_header *h;
4618 h = &mo->mot_header;
4619 lu_object_header_init(h);
4620 lu_object_init(o, h, d);
4621 lu_object_add_top(h, o);
4622 o->lo_ops = &mdt_obj_ops;
4623 mutex_init(&mo->mot_ioepoch_mutex);
4624 mutex_init(&mo->mot_lov_mutex);
4625 init_rwsem(&mo->mot_open_sem);
4631 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4632 const struct lu_object_conf *unused)
4634 struct mdt_device *d = mdt_dev(o->lo_dev);
4635 struct lu_device *under;
4636 struct lu_object *below;
4640 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4641 PFID(lu_object_fid(o)));
4643 under = &d->mdt_child->md_lu_dev;
4644 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4645 if (below != NULL) {
4646 lu_object_add(o, below);
4653 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4655 struct mdt_object *mo = mdt_obj(o);
4656 struct lu_object_header *h;
4660 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4661 PFID(lu_object_fid(o)));
4663 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4664 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4667 lu_object_header_fini(h);
4668 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4673 static int mdt_object_print(const struct lu_env *env, void *cookie,
4674 lu_printer_t p, const struct lu_object *o)
4676 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4677 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4678 "flags="LPX64", epochcount=%d, writecount=%d)",
4679 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4680 mdto->mot_ioepoch_count, mdto->mot_writecount);
4683 static int mdt_prepare(const struct lu_env *env,
4684 struct lu_device *pdev,
4685 struct lu_device *cdev)
4687 struct mdt_device *mdt = mdt_dev(cdev);
4688 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4689 struct obd_device *obd = cdev->ld_obd;
4690 struct lfsck_start_param lsp;
4697 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4701 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4705 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4709 lsp.lsp_namespace = mdt->mdt_namespace;
4710 lsp.lsp_start = NULL;
4711 lsp.lsp_index_valid = 0;
4712 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
4713 OBD_IOC_START_LFSCK,
4716 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
4717 mdt_obd_name(mdt), rc);
4721 if (mdt->mdt_seq_site.ss_node_id == 0) {
4722 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4723 &mdt->mdt_md_root_fid);
4728 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4729 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4730 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4731 LASSERT(obd->obd_no_conn);
4732 spin_lock(&obd->obd_dev_lock);
4733 obd->obd_no_conn = 0;
4734 spin_unlock(&obd->obd_dev_lock);
4736 if (obd->obd_recovering == 0)
4737 mdt_postrecov(env, mdt);
4742 const struct lu_device_operations mdt_lu_ops = {
4743 .ldo_object_alloc = mdt_object_alloc,
4744 .ldo_process_config = mdt_process_config,
4745 .ldo_prepare = mdt_prepare,
4748 static const struct lu_object_operations mdt_obj_ops = {
4749 .loo_object_init = mdt_object_init,
4750 .loo_object_free = mdt_object_free,
4751 .loo_object_print = mdt_object_print
4754 static int mdt_obd_set_info_async(const struct lu_env *env,
4755 struct obd_export *exp,
4756 __u32 keylen, void *key,
4757 __u32 vallen, void *val,
4758 struct ptlrpc_request_set *set)
4764 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4765 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4773 * Match client and server connection feature flags.
4775 * Compute the compatibility flags for a connection request based on
4776 * features mutually supported by client and server.
4778 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4779 * must not be updated here, otherwise a partially initialized value may
4780 * be exposed. After the connection request is successfully processed,
4781 * the top-level MDT connect request handler atomically updates the export
4782 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4783 * reply. \see mdt_connect().
4785 * \param exp the obd_export associated with this client/target pair
4786 * \param mdt the target device for the connection
4787 * \param data stores data for this connect request
4790 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4791 * \retval -EBADE client and server feature requirements are incompatible
4793 static int mdt_connect_internal(struct obd_export *exp,
4794 struct mdt_device *mdt,
4795 struct obd_connect_data *data)
4797 LASSERT(data != NULL);
4799 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4800 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4802 /* If no known bits (which should not happen, probably,
4803 as everybody should support LOOKUP and UPDATE bits at least)
4804 revert to compat mode with plain locks. */
4805 if (!data->ocd_ibits_known &&
4806 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4807 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4809 if (!mdt->mdt_opts.mo_acl)
4810 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4812 if (!mdt->mdt_opts.mo_user_xattr)
4813 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4815 if (!mdt->mdt_som_conf)
4816 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4818 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4819 data->ocd_brw_size = min(data->ocd_brw_size,
4820 (__u32)MD_MAX_BRW_SIZE);
4821 if (data->ocd_brw_size == 0) {
4822 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4823 " ocd_version: %x ocd_grant: %d "
4824 "ocd_index: %u ocd_brw_size is "
4825 "unexpectedly zero, network data "
4826 "corruption? Refusing connection of this"
4829 exp->exp_client_uuid.uuid,
4830 exp, data->ocd_connect_flags, data->ocd_version,
4831 data->ocd_grant, data->ocd_index);
4836 /* NB: Disregard the rule against updating
4837 * exp_connect_data.ocd_connect_flags in this case, since
4838 * tgt_client_new() needs to know if this is a lightweight
4839 * connection, and it is safe to expose this flag before
4840 * connection processing completes. */
4841 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4842 spin_lock(&exp->exp_lock);
4843 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4844 spin_unlock(&exp->exp_lock);
4847 data->ocd_version = LUSTRE_VERSION_CODE;
4849 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4850 CWARN("%s: MDS requires FID support, but client not\n",
4855 if (mdt->mdt_som_conf &&
4856 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4857 OBD_CONNECT_MDS_MDS |
4858 OBD_CONNECT_SOM))) {
4859 CWARN("%s: MDS has SOM enabled, but client does not support "
4860 "it\n", mdt_obd_name(mdt));
4864 if (OCD_HAS_FLAG(data, PINGLESS)) {
4865 if (ptlrpc_pinger_suppress_pings()) {
4866 spin_lock(&exp->exp_obd->obd_dev_lock);
4867 list_del_init(&exp->exp_obd_chain_timed);
4868 spin_unlock(&exp->exp_obd->obd_dev_lock);
4870 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4874 data->ocd_max_easize = mdt->mdt_max_ea_size;
4879 /* mds_connect copy */
4880 static int mdt_obd_connect(const struct lu_env *env,
4881 struct obd_export **exp, struct obd_device *obd,
4882 struct obd_uuid *cluuid,
4883 struct obd_connect_data *data,
4886 struct obd_export *lexp;
4887 struct lustre_handle conn = { 0 };
4888 struct mdt_device *mdt;
4892 LASSERT(env != NULL);
4893 if (!exp || !obd || !cluuid)
4896 mdt = mdt_dev(obd->obd_lu_dev);
4899 * first, check whether the stack is ready to handle requests
4900 * XXX: probably not very appropriate method is used now
4901 * at some point we should find a better one
4903 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
4904 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
4905 rc = obd_get_info(env, mdt->mdt_child_exp,
4906 sizeof(KEY_OSP_CONNECTED),
4907 KEY_OSP_CONNECTED, NULL, NULL, NULL);
4910 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
4913 rc = class_connect(&conn, obd, cluuid);
4917 lexp = class_conn2export(&conn);
4918 LASSERT(lexp != NULL);
4920 rc = mdt_connect_internal(lexp, mdt, data);
4922 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
4925 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
4926 rc = tgt_client_new(env, lexp);
4928 mdt_export_stats_init(obd, lexp, localdata);
4930 /* For phase I, sync for cross-ref operation. */
4931 lexp->exp_keep_sync = 1;
4935 class_disconnect(lexp);
4944 static int mdt_obd_reconnect(const struct lu_env *env,
4945 struct obd_export *exp, struct obd_device *obd,
4946 struct obd_uuid *cluuid,
4947 struct obd_connect_data *data,
4953 if (exp == NULL || obd == NULL || cluuid == NULL)
4956 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4958 mdt_export_stats_init(obd, exp, localdata);
4963 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
4964 struct mdt_thread_info *info,
4965 struct mdt_file_data *mfd)
4967 struct lu_context ses;
4971 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
4976 lu_context_enter(&ses);
4978 mdt_ucred(info)->uc_valid = UCRED_OLD;
4979 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
4981 lu_context_exit(&ses);
4982 lu_context_fini(&ses);
4988 static int mdt_export_cleanup(struct obd_export *exp)
4990 struct mdt_export_data *med = &exp->exp_mdt_data;
4991 struct obd_device *obd = exp->exp_obd;
4992 struct mdt_device *mdt;
4993 struct mdt_thread_info *info;
4995 CFS_LIST_HEAD(closing_list);
4996 struct mdt_file_data *mfd, *n;
5000 spin_lock(&med->med_open_lock);
5001 while (!cfs_list_empty(&med->med_open_head)) {
5002 cfs_list_t *tmp = med->med_open_head.next;
5003 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5005 /* Remove mfd handle so it can't be found again.
5006 * We are consuming the mfd_list reference here. */
5007 class_handle_unhash(&mfd->mfd_handle);
5008 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5010 spin_unlock(&med->med_open_lock);
5011 mdt = mdt_dev(obd->obd_lu_dev);
5012 LASSERT(mdt != NULL);
5014 rc = lu_env_init(&env, LCT_MD_THREAD);
5018 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5019 LASSERT(info != NULL);
5020 memset(info, 0, sizeof *info);
5021 info->mti_env = &env;
5022 info->mti_mdt = mdt;
5023 info->mti_exp = exp;
5025 if (!cfs_list_empty(&closing_list)) {
5026 struct md_attr *ma = &info->mti_attr;
5028 /* Close any open files (which may also cause orphan unlinking). */
5029 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5030 cfs_list_del_init(&mfd->mfd_list);
5031 ma->ma_need = ma->ma_valid = 0;
5033 /* This file is being closed due to an eviction, it
5034 * could have been modified and now dirty regarding to
5035 * HSM archive, check this!
5036 * The logic here is to mark a file dirty if there's a
5037 * chance it was dirtied before the client was evicted,
5038 * so that we don't have to wait for a release attempt
5039 * before finding out the file was actually dirty and
5040 * fail the release. Aggressively marking it dirty here
5041 * will cause the policy engine to attempt to
5042 * re-archive it; when rearchiving, we can compare the
5043 * current version to the HSM data_version and make the
5044 * archive request into a noop if it's not actually
5047 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5048 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5050 /* Don't unlink orphan on failover umount, LU-184 */
5051 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5052 ma->ma_valid = MA_FLAGS;
5053 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5055 mdt_mfd_close(info, mfd);
5058 info->mti_mdt = NULL;
5059 /* cleanup client slot early */
5060 /* Do not erase record for recoverable client. */
5061 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5062 tgt_client_del(&env, exp);
5068 static int mdt_obd_disconnect(struct obd_export *exp)
5074 class_export_get(exp);
5076 rc = server_disconnect_export(exp);
5078 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5080 rc = mdt_export_cleanup(exp);
5081 class_export_put(exp);
5085 /* FIXME: Can we avoid using these two interfaces? */
5086 static int mdt_init_export(struct obd_export *exp)
5088 struct mdt_export_data *med = &exp->exp_mdt_data;
5092 CFS_INIT_LIST_HEAD(&med->med_open_head);
5093 spin_lock_init(&med->med_open_lock);
5094 mutex_init(&med->med_idmap_mutex);
5095 med->med_idmap = NULL;
5096 spin_lock(&exp->exp_lock);
5097 exp->exp_connecting = 1;
5098 spin_unlock(&exp->exp_lock);
5100 /* self-export doesn't need client data and ldlm initialization */
5101 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5102 &exp->exp_client_uuid)))
5105 rc = tgt_client_alloc(exp);
5109 rc = ldlm_init_export(exp);
5116 tgt_client_free(exp);
5118 CERROR("%s: Failed to initialize export: rc = %d\n",
5119 exp->exp_obd->obd_name, rc);
5123 static int mdt_destroy_export(struct obd_export *exp)
5127 if (exp_connect_rmtclient(exp))
5128 mdt_cleanup_idmap(&exp->exp_mdt_data);
5130 target_destroy_export(exp);
5131 /* destroy can be called from failed obd_setup, so
5132 * checking uuid is safer than obd_self_export */
5133 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5134 &exp->exp_client_uuid)))
5137 ldlm_destroy_export(exp);
5138 tgt_client_free(exp);
5140 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5141 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5146 /** The maximum depth that fid2path() will search.
5147 * This is limited only because we want to store the fids for
5148 * historical path lookup purposes.
5150 #define MAX_PATH_DEPTH 100
5152 /** mdt_path() lookup structure. */
5153 struct path_lookup_info {
5154 __u64 pli_recno; /**< history point */
5155 __u64 pli_currec; /**< current record */
5156 struct lu_fid pli_fid;
5157 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5158 struct mdt_object *pli_mdt_obj;
5159 char *pli_path; /**< full path */
5161 int pli_linkno; /**< which hardlink to follow */
5162 int pli_fidcount; /**< number of \a pli_fids */
5165 static int mdt_links_read(struct mdt_thread_info *info,
5166 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5170 LASSERT(ldata->ld_buf->lb_buf != NULL);
5172 if (!mdt_object_exists(mdt_obj))
5175 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5176 ldata->ld_buf, XATTR_NAME_LINK);
5177 if (rc == -ERANGE) {
5178 /* Buf was too small, figure out what we need. */
5179 lu_buf_free(ldata->ld_buf);
5180 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5181 ldata->ld_buf, XATTR_NAME_LINK);
5184 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5185 if (ldata->ld_buf->lb_buf == NULL)
5187 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5188 ldata->ld_buf, XATTR_NAME_LINK);
5193 return linkea_init(ldata);
5196 static int mdt_path_current(struct mdt_thread_info *info,
5197 struct path_lookup_info *pli)
5199 struct mdt_device *mdt = info->mti_mdt;
5200 struct mdt_object *mdt_obj;
5201 struct link_ea_header *leh;
5202 struct link_ea_entry *lee;
5203 struct lu_name *tmpname = &info->mti_name;
5204 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5205 struct lu_buf *buf = &info->mti_big_buf;
5208 struct linkea_data ldata = { 0 };
5212 /* temp buffer for path element, the buffer will be finally freed
5213 * in mdt_thread_info_fini */
5214 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5215 if (buf->lb_buf == NULL)
5219 ptr = pli->pli_path + pli->pli_pathlen - 1;
5222 pli->pli_fidcount = 0;
5223 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5225 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5226 * so checking root_fid can only happen on MDT0. */
5227 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5228 &pli->pli_fids[pli->pli_fidcount])) {
5229 mdt_obj = mdt_object_find(info->mti_env, mdt,
5230 &pli->pli_fids[pli->pli_fidcount]);
5231 if (IS_ERR(mdt_obj))
5232 GOTO(out, rc = PTR_ERR(mdt_obj));
5233 if (mdt_object_remote(mdt_obj)) {
5234 mdt_object_put(info->mti_env, mdt_obj);
5235 GOTO(remote_out, rc = -EREMOTE);
5237 if (!mdt_object_exists(mdt_obj)) {
5238 mdt_object_put(info->mti_env, mdt_obj);
5239 GOTO(out, rc = -ENOENT);
5242 rc = mdt_links_read(info, mdt_obj, &ldata);
5243 mdt_object_put(info->mti_env, mdt_obj);
5248 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5249 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5250 /* If set, use link #linkno for path lookup, otherwise use
5251 link #0. Only do this for the final path element. */
5252 if (pli->pli_fidcount == 0 &&
5253 pli->pli_linkno < leh->leh_reccount) {
5255 for (count = 0; count < pli->pli_linkno; count++) {
5256 lee = (struct link_ea_entry *)
5257 ((char *)lee + reclen);
5258 linkea_entry_unpack(lee, &reclen, tmpname,
5261 if (pli->pli_linkno < leh->leh_reccount - 1)
5262 /* indicate to user there are more links */
5266 /* Pack the name in the end of the buffer */
5267 ptr -= tmpname->ln_namelen;
5268 if (ptr - 1 <= pli->pli_path)
5269 GOTO(out, rc = -EOVERFLOW);
5270 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5273 /* Store the parent fid for historic lookup */
5274 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5275 GOTO(out, rc = -EOVERFLOW);
5276 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5280 ptr++; /* skip leading / */
5281 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5288 /* Returns the full path to this fid, as of changelog record recno. */
5289 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5290 char *path, int pathlen, __u64 *recno, int *linkno,
5293 struct mdt_device *mdt = info->mti_mdt;
5294 struct path_lookup_info *pli;
5302 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5311 pli->pli_mdt_obj = obj;
5312 pli->pli_recno = *recno;
5313 pli->pli_path = path;
5314 pli->pli_pathlen = pathlen;
5315 pli->pli_linkno = *linkno;
5317 /* Retry multiple times in case file is being moved */
5318 while (tries-- && rc == -EAGAIN)
5319 rc = mdt_path_current(info, pli);
5321 /* return the last resolved fids to the client, so the client will
5322 * build the left path on another MDT for remote object */
5323 *fid = pli->pli_fids[pli->pli_fidcount];
5325 *recno = pli->pli_currec;
5326 /* Return next link index to caller */
5327 *linkno = pli->pli_linkno;
5334 static int mdt_fid2path(struct mdt_thread_info *info,
5335 struct getinfo_fid2path *fp)
5337 struct mdt_device *mdt = info->mti_mdt;
5338 struct mdt_object *obj;
5342 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5343 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5345 if (!fid_is_sane(&fp->gf_fid))
5348 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5349 CWARN("%s: "DFID" is invalid, sequence should be "
5350 ">= "LPX64"\n", mdt_obd_name(mdt),
5351 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5355 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5356 if (obj == NULL || IS_ERR(obj)) {
5357 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5362 if (mdt_object_remote(obj))
5364 else if (!mdt_object_exists(obj))
5370 mdt_object_put(info->mti_env, obj);
5371 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5372 PFID(&fp->gf_fid), rc);
5376 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5377 &fp->gf_linkno, &fp->gf_fid);
5379 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5380 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5382 mdt_object_put(info->mti_env, obj);
5387 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5388 void *val, int vallen)
5390 struct getinfo_fid2path *fpout, *fpin;
5393 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5396 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5397 lustre_swab_fid2path(fpin);
5399 memcpy(fpout, fpin, sizeof(*fpin));
5400 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5403 rc = mdt_fid2path(info, fpout);
5407 int mdt_get_info(struct tgt_session_info *tsi)
5417 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5419 CDEBUG(D_IOCTL, "No GETINFO key");
5420 RETURN(err_serious(-EFAULT));
5422 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5425 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5426 if (vallen == NULL) {
5427 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5428 RETURN(err_serious(-EFAULT));
5431 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5433 rc = req_capsule_server_pack(tsi->tsi_pill);
5435 RETURN(err_serious(rc));
5437 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5438 if (valout == NULL) {
5439 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5440 RETURN(err_serious(-EFAULT));
5443 if (KEY_IS(KEY_FID2PATH)) {
5444 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5446 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5447 mdt_thread_info_fini(info);
5454 /* Pass the ioc down */
5455 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5456 unsigned int cmd, int len, void *data)
5458 struct lu_context ioctl_session;
5459 struct md_device *next = mdt->mdt_child;
5463 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5466 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5467 lu_context_enter(&ioctl_session);
5468 env->le_ses = &ioctl_session;
5470 LASSERT(next->md_ops->mdo_iocontrol);
5471 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5473 lu_context_exit(&ioctl_session);
5474 lu_context_fini(&ioctl_session);
5478 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5480 struct obd_ioctl_data *data = karg;
5483 struct mdt_object *obj;
5484 struct mdt_lock_handle *lh;
5488 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5489 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5492 fid = (struct lu_fid *)data->ioc_inlbuf1;
5494 if (!fid_is_sane(fid))
5497 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5499 lh = &mti->mti_lh[MDT_LH_PARENT];
5500 mdt_lock_reg_init(lh, LCK_CR);
5502 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5504 RETURN(PTR_ERR(obj));
5506 if (mdt_object_remote(obj)) {
5509 * before calling version get the correct MDS should be
5510 * fid, this is error to find remote object here
5512 CERROR("nonlocal object "DFID"\n", PFID(fid));
5513 } else if (!mdt_object_exists(obj)) {
5514 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5517 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5518 *(__u64 *)data->ioc_inlbuf2 = version;
5521 mdt_object_unlock_put(mti, obj, lh, 1);
5525 /* ioctls on obd dev */
5526 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5527 void *karg, void *uarg)
5530 struct obd_device *obd = exp->exp_obd;
5531 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5532 struct dt_device *dt = mdt->mdt_bottom;
5536 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5537 rc = lu_env_init(&env, LCT_MD_THREAD);
5543 rc = mdt_device_sync(&env, mdt);
5545 case OBD_IOC_SET_READONLY:
5546 rc = dt->dd_ops->dt_ro(&env, dt);
5548 case OBD_IOC_ABORT_RECOVERY:
5549 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5550 target_stop_recovery_thread(obd);
5553 case OBD_IOC_CHANGELOG_REG:
5554 case OBD_IOC_CHANGELOG_DEREG:
5555 case OBD_IOC_CHANGELOG_CLEAR:
5556 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5558 case OBD_IOC_START_LFSCK: {
5559 struct md_device *next = mdt->mdt_child;
5560 struct obd_ioctl_data *data = karg;
5561 struct lfsck_start_param lsp;
5563 if (unlikely(data == NULL)) {
5568 lsp.lsp_namespace = mdt->mdt_namespace;
5569 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5570 lsp.lsp_index_valid = 0;
5571 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5574 case OBD_IOC_STOP_LFSCK: {
5575 struct md_device *next = mdt->mdt_child;
5576 struct obd_ioctl_data *data = karg;
5577 struct lfsck_stop *stop =
5578 (struct lfsck_stop *)(data->ioc_inlbuf1);
5580 stop->ls_status = LS_STOPPED;
5581 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, stop);
5584 case OBD_IOC_GET_OBJ_VERSION: {
5585 struct mdt_thread_info *mti;
5586 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5587 memset(mti, 0, sizeof *mti);
5588 mti->mti_env = &env;
5592 rc = mdt_ioc_version_get(mti, karg);
5595 case OBD_IOC_CATLOGLIST:
5596 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg);
5600 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5601 mdt_obd_name(mdt), cmd, rc);
5608 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5610 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5614 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5618 int mdt_obd_postrecov(struct obd_device *obd)
5623 rc = lu_env_init(&env, LCT_MD_THREAD);
5626 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5631 static struct obd_ops mdt_obd_device_ops = {
5632 .o_owner = THIS_MODULE,
5633 .o_set_info_async = mdt_obd_set_info_async,
5634 .o_connect = mdt_obd_connect,
5635 .o_reconnect = mdt_obd_reconnect,
5636 .o_disconnect = mdt_obd_disconnect,
5637 .o_init_export = mdt_init_export,
5638 .o_destroy_export = mdt_destroy_export,
5639 .o_iocontrol = mdt_iocontrol,
5640 .o_postrecov = mdt_obd_postrecov,
5643 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5644 struct lu_device *d)
5646 struct mdt_device *m = mdt_dev(d);
5653 static struct lu_device *mdt_device_free(const struct lu_env *env,
5654 struct lu_device *d)
5656 struct mdt_device *m = mdt_dev(d);
5659 lu_device_fini(&m->mdt_lu_dev);
5665 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5666 struct lu_device_type *t,
5667 struct lustre_cfg *cfg)
5669 struct lu_device *l;
5670 struct mdt_device *m;
5677 rc = mdt_init0(env, m, t, cfg);
5679 mdt_device_free(env, l);
5684 l = ERR_PTR(-ENOMEM);
5688 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5689 LU_KEY_INIT(mdt, struct mdt_thread_info);
5691 static void mdt_key_fini(const struct lu_context *ctx,
5692 struct lu_context_key *key, void* data)
5694 struct mdt_thread_info *info = data;
5696 if (info->mti_big_lmm) {
5697 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5698 info->mti_big_lmm = NULL;
5699 info->mti_big_lmmsize = 0;
5704 /* context key: mdt_thread_key */
5705 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5707 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5709 return lu_ucred(info->mti_env);
5712 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5714 return lu_ucred_check(info->mti_env);
5718 * Enable/disable COS (Commit On Sharing).
5720 * Set/Clear the COS flag in mdt options.
5722 * \param mdt mdt device
5723 * \param val 0 disables COS, other values enable COS
5725 void mdt_enable_cos(struct mdt_device *mdt, int val)
5730 mdt->mdt_opts.mo_cos = !!val;
5731 rc = lu_env_init(&env, LCT_LOCAL);
5732 if (unlikely(rc != 0)) {
5733 CWARN("lu_env initialization failed with rc = %d,"
5734 "cannot sync\n", rc);
5737 mdt_device_sync(&env, mdt);
5742 * Check COS (Commit On Sharing) status.
5744 * Return COS flag status.
5746 * \param mdt mdt device
5748 int mdt_cos_is_enabled(struct mdt_device *mdt)
5750 return mdt->mdt_opts.mo_cos != 0;
5753 static struct lu_device_type_operations mdt_device_type_ops = {
5754 .ldto_device_alloc = mdt_device_alloc,
5755 .ldto_device_free = mdt_device_free,
5756 .ldto_device_fini = mdt_device_fini
5759 static struct lu_device_type mdt_device_type = {
5760 .ldt_tags = LU_DEVICE_MD,
5761 .ldt_name = LUSTRE_MDT_NAME,
5762 .ldt_ops = &mdt_device_type_ops,
5763 .ldt_ctx_tags = LCT_MD_THREAD
5766 static int __init mdt_mod_init(void)
5768 struct lprocfs_static_vars lvars;
5771 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5772 FID_NOBRACE_LEN + 1);
5773 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5775 rc = lu_kmem_init(mdt_caches);
5779 rc = mds_mod_init();
5783 lprocfs_mdt_init_vars(&lvars);
5784 rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
5785 #ifndef HAVE_ONLY_PROCFS_SEQ
5788 LUSTRE_MDT_NAME, &mdt_device_type);
5793 lu_kmem_fini(mdt_caches);
5800 static void __exit mdt_mod_exit(void)
5802 class_unregister_type(LUSTRE_MDT_NAME);
5804 lu_kmem_fini(mdt_caches);
5807 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5808 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5809 MODULE_LICENSE("GPL");
5811 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);