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);
535 ma->ma_lmm_size = rc;
536 ma->ma_valid |= MA_LOV;
538 } else if (rc == -ENODATA) {
541 } else if (rc == -ERANGE) {
542 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
544 info->mti_big_lmm_used = 1;
545 ma->ma_valid |= MA_LOV;
546 ma->ma_lmm = info->mti_big_lmm;
547 ma->ma_lmm_size = rc;
548 /* update mdt_max_mdsize so all clients
549 * will be aware about that */
550 if (info->mti_mdt->mdt_max_mdsize < rc)
551 info->mti_mdt->mdt_max_mdsize = rc;
559 int mdt_attr_get_pfid(struct mdt_thread_info *info,
560 struct mdt_object *o, struct lu_fid *pfid)
562 struct lu_buf *buf = &info->mti_buf;
563 struct link_ea_header *leh;
564 struct link_ea_entry *lee;
568 buf->lb_buf = info->mti_big_lmm;
569 buf->lb_len = info->mti_big_lmmsize;
570 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
571 buf, XATTR_NAME_LINK);
572 /* ignore errors, MA_PFID won't be set and it is
573 * up to the caller to treat this as an error */
574 if (rc == -ERANGE || buf->lb_len == 0) {
575 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
576 buf->lb_buf = info->mti_big_lmm;
577 buf->lb_len = info->mti_big_lmmsize;
582 if (rc < sizeof(*leh)) {
583 CERROR("short LinkEA on "DFID": rc = %d\n",
584 PFID(mdt_object_fid(o)), rc);
588 leh = (struct link_ea_header *) buf->lb_buf;
589 lee = (struct link_ea_entry *)(leh + 1);
590 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
591 leh->leh_magic = LINK_EA_MAGIC;
592 leh->leh_reccount = __swab32(leh->leh_reccount);
593 leh->leh_len = __swab64(leh->leh_len);
595 if (leh->leh_magic != LINK_EA_MAGIC)
597 if (leh->leh_reccount == 0)
600 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
601 fid_be_to_cpu(pfid, pfid);
606 int mdt_attr_get_complex(struct mdt_thread_info *info,
607 struct mdt_object *o, struct md_attr *ma)
609 const struct lu_env *env = info->mti_env;
610 struct md_object *next = mdt_object_child(o);
611 struct lu_buf *buf = &info->mti_buf;
612 u32 mode = lu_object_attr(&next->mo_lu);
613 int need = ma->ma_need;
619 if (need & MA_INODE) {
620 ma->ma_need = MA_INODE;
621 rc = mo_attr_get(env, next, ma);
624 ma->ma_valid |= MA_INODE;
627 if (need & MA_PFID) {
628 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
630 ma->ma_valid |= MA_PFID;
631 /* ignore this error, parent fid is not mandatory */
635 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
636 rc = mdt_attr_get_lov(info, o, ma);
641 if (need & MA_LMV && S_ISDIR(mode)) {
642 buf->lb_buf = ma->ma_lmv;
643 buf->lb_len = ma->ma_lmv_size;
644 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
646 ma->ma_lmv_size = rc2;
647 ma->ma_valid |= MA_LMV;
648 } else if (rc2 == -ENODATA) {
655 if (need & MA_SOM && S_ISREG(mode)) {
656 buf->lb_buf = info->mti_xattr_buf;
657 buf->lb_len = sizeof(info->mti_xattr_buf);
658 CLASSERT(sizeof(struct som_attrs) <=
659 sizeof(info->mti_xattr_buf));
660 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
661 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
663 ma->ma_valid |= MA_SOM;
664 else if (rc2 < 0 && rc2 != -ENODATA)
668 if (need & MA_HSM && S_ISREG(mode)) {
669 buf->lb_buf = info->mti_xattr_buf;
670 buf->lb_len = sizeof(info->mti_xattr_buf);
671 CLASSERT(sizeof(struct hsm_attrs) <=
672 sizeof(info->mti_xattr_buf));
673 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
674 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
676 ma->ma_valid |= MA_HSM;
677 else if (rc2 < 0 && rc2 != -ENODATA)
681 #ifdef CONFIG_FS_POSIX_ACL
682 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
683 buf->lb_buf = ma->ma_acl;
684 buf->lb_len = ma->ma_acl_size;
685 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
687 ma->ma_acl_size = rc2;
688 ma->ma_valid |= MA_ACL_DEF;
689 } else if (rc2 == -ENODATA) {
698 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
699 rc, ma->ma_valid, ma->ma_lmm);
703 static int mdt_getattr_internal(struct mdt_thread_info *info,
704 struct mdt_object *o, int ma_need)
706 struct md_object *next = mdt_object_child(o);
707 const struct mdt_body *reqbody = info->mti_body;
708 struct ptlrpc_request *req = mdt_info_req(info);
709 struct md_attr *ma = &info->mti_attr;
710 struct lu_attr *la = &ma->ma_attr;
711 struct req_capsule *pill = info->mti_pill;
712 const struct lu_env *env = info->mti_env;
713 struct mdt_body *repbody;
714 struct lu_buf *buffer = &info->mti_buf;
719 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
720 RETURN(err_serious(-ENOMEM));
722 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
726 if (mdt_object_remote(o)) {
727 /* This object is located on remote node.*/
728 /* Return -EIO for old client */
729 if (!mdt_is_dne_client(req->rq_export))
730 GOTO(out, rc = -EIO);
732 repbody->fid1 = *mdt_object_fid(o);
733 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
737 buffer->lb_len = reqbody->eadatasize;
738 if (buffer->lb_len > 0)
739 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
741 buffer->lb_buf = NULL;
743 /* If it is dir object and client require MEA, then we got MEA */
744 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
745 reqbody->valid & OBD_MD_MEA) {
746 /* Assumption: MDT_MD size is enough for lmv size. */
747 ma->ma_lmv = buffer->lb_buf;
748 ma->ma_lmv_size = buffer->lb_len;
749 ma->ma_need = MA_LMV | MA_INODE;
751 ma->ma_lmm = buffer->lb_buf;
752 ma->ma_lmm_size = buffer->lb_len;
753 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
756 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
757 reqbody->valid & OBD_MD_FLDIREA &&
758 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
759 /* get default stripe info for this dir. */
760 ma->ma_need |= MA_LOV_DEF;
762 ma->ma_need |= ma_need;
763 if (ma->ma_need & MA_SOM)
764 ma->ma_som = &info->mti_u.som.data;
766 rc = mdt_attr_get_complex(info, o, ma);
768 CERROR("%s: getattr error for "DFID": rc = %d\n",
769 mdt_obd_name(info->mti_mdt),
770 PFID(mdt_object_fid(o)), rc);
774 /* if file is released, check if a restore is running */
775 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
776 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
777 repbody->t_state = MS_RESTORE;
778 repbody->valid |= OBD_MD_TSTATE;
781 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
783 /* the Lustre protocol supposes to return default striping
784 * on the user-visible root if explicitly requested */
785 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
786 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
787 struct lu_fid rootfid;
788 struct mdt_object *root;
789 struct mdt_device *mdt = info->mti_mdt;
791 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
794 root = mdt_object_find(env, mdt, &rootfid);
796 RETURN(PTR_ERR(root));
797 rc = mdt_attr_get_lov(info, root, ma);
798 mdt_object_put(info->mti_env, root);
800 CERROR("%s: getattr error for "DFID": rc = %d\n",
801 mdt_obd_name(info->mti_mdt),
802 PFID(mdt_object_fid(o)), rc);
807 if (likely(ma->ma_valid & MA_INODE))
808 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
812 if (mdt_body_has_lov(la, reqbody)) {
813 if (ma->ma_valid & MA_LOV) {
814 LASSERT(ma->ma_lmm_size);
815 mdt_dump_lmm(D_INFO, ma->ma_lmm);
816 repbody->eadatasize = ma->ma_lmm_size;
817 if (S_ISDIR(la->la_mode))
818 repbody->valid |= OBD_MD_FLDIREA;
820 repbody->valid |= OBD_MD_FLEASIZE;
822 if (ma->ma_valid & MA_LMV) {
823 LASSERT(S_ISDIR(la->la_mode));
824 repbody->eadatasize = ma->ma_lmv_size;
825 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
827 } else if (S_ISLNK(la->la_mode) &&
828 reqbody->valid & OBD_MD_LINKNAME) {
829 buffer->lb_buf = ma->ma_lmm;
830 /* eadatasize from client includes NULL-terminator, so
831 * there is no need to read it */
832 buffer->lb_len = reqbody->eadatasize - 1;
833 rc = mo_readlink(env, next, buffer);
834 if (unlikely(rc <= 0)) {
835 CERROR("%s: readlink failed for "DFID": rc = %d\n",
836 mdt_obd_name(info->mti_mdt),
837 PFID(mdt_object_fid(o)), rc);
840 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
842 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
844 repbody->valid |= OBD_MD_LINKNAME;
845 /* we need to report back size with NULL-terminator
846 * because client expects that */
847 repbody->eadatasize = rc + 1;
848 if (repbody->eadatasize != reqbody->eadatasize)
849 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
850 "on "DFID ", expected %d\n",
851 mdt_obd_name(info->mti_mdt),
852 rc, PFID(mdt_object_fid(o)),
853 reqbody->eadatasize - 1);
855 ((char *)ma->ma_lmm)[rc] = 0;
857 /* If the total CDEBUG() size is larger than a page, it
858 * will print a warning to the console, avoid this by
859 * printing just the last part of the symlink. */
860 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
861 print_limit < rc ? "..." : "", print_limit,
862 (char *)ma->ma_lmm + rc - print_limit, rc);
867 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
868 repbody->max_cookiesize = 0;
869 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
870 repbody->valid |= OBD_MD_FLMODEASIZE;
871 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
872 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
873 repbody->max_cookiesize);
876 if (exp_connect_rmtclient(info->mti_exp) &&
877 reqbody->valid & OBD_MD_FLRMTPERM) {
878 void *buf = req_capsule_server_get(pill, &RMF_ACL);
880 /* mdt_getattr_lock only */
881 rc = mdt_pack_remote_perm(info, o, buf);
883 repbody->valid &= ~OBD_MD_FLRMTPERM;
884 repbody->aclsize = 0;
887 repbody->valid |= OBD_MD_FLRMTPERM;
888 repbody->aclsize = sizeof(struct mdt_remote_perm);
891 #ifdef CONFIG_FS_POSIX_ACL
892 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
893 (reqbody->valid & OBD_MD_FLACL)) {
894 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
895 buffer->lb_len = req_capsule_get_size(pill,
896 &RMF_ACL, RCL_SERVER);
897 if (buffer->lb_len > 0) {
898 rc = mo_xattr_get(env, next, buffer,
899 XATTR_NAME_ACL_ACCESS);
901 if (rc == -ENODATA) {
902 repbody->aclsize = 0;
903 repbody->valid |= OBD_MD_FLACL;
905 } else if (rc == -EOPNOTSUPP) {
908 CERROR("%s: unable to read "DFID
910 mdt_obd_name(info->mti_mdt),
911 PFID(mdt_object_fid(o)), rc);
914 repbody->aclsize = rc;
915 repbody->valid |= OBD_MD_FLACL;
922 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
923 info->mti_mdt->mdt_lut.lut_mds_capa &&
924 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
925 struct lustre_capa *capa;
927 capa = req_capsule_server_get(pill, &RMF_CAPA1);
929 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
930 rc = mo_capa_get(env, next, capa, 0);
933 repbody->valid |= OBD_MD_FLMDSCAPA;
938 mdt_counter_incr(req, LPROC_MDT_GETATTR);
943 static int mdt_renew_capa(struct mdt_thread_info *info)
945 struct mdt_object *obj = info->mti_object;
946 struct mdt_body *body;
947 struct lustre_capa *capa, *c;
951 /* if object doesn't exist, or server has disabled capability,
952 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
955 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
956 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
959 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
960 LASSERT(body != NULL);
962 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
965 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
969 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
971 body->valid |= OBD_MD_FLOSSCAPA;
975 int mdt_getattr(struct tgt_session_info *tsi)
977 struct mdt_thread_info *info = tsi2mdt_info(tsi);
978 struct mdt_object *obj = info->mti_object;
979 struct req_capsule *pill = info->mti_pill;
980 struct mdt_body *reqbody;
981 struct mdt_body *repbody;
986 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
989 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
990 rc = req_capsule_server_pack(pill);
992 RETURN(err_serious(rc));
993 rc = mdt_renew_capa(info);
994 GOTO(out_shrink, rc);
997 LASSERT(obj != NULL);
998 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1000 mode = lu_object_attr(&obj->mot_obj);
1002 /* old clients may not report needed easize, use max value then */
1003 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1004 reqbody->eadatasize == 0 ?
1005 info->mti_mdt->mdt_max_mdsize :
1006 reqbody->eadatasize);
1008 rc = req_capsule_server_pack(pill);
1009 if (unlikely(rc != 0))
1010 GOTO(out, rc = err_serious(rc));
1012 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1013 LASSERT(repbody != NULL);
1014 repbody->eadatasize = 0;
1015 repbody->aclsize = 0;
1017 if (reqbody->valid & OBD_MD_FLRMTPERM)
1018 rc = mdt_init_ucred(info, reqbody);
1020 rc = mdt_check_ucred(info);
1022 GOTO(out_shrink, rc);
1024 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1027 * Don't check capability at all, because rename might getattr for
1028 * remote obj, and at that time no capability is available.
1030 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1031 rc = mdt_getattr_internal(info, obj, 0);
1032 if (reqbody->valid & OBD_MD_FLRMTPERM)
1033 mdt_exit_ucred(info);
1036 mdt_client_compatibility(info);
1037 rc2 = mdt_fix_reply(info);
1041 mdt_thread_info_fini(info);
1045 int mdt_is_subdir(struct tgt_session_info *tsi)
1047 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1048 struct mdt_object *o = info->mti_object;
1049 struct req_capsule *pill = info->mti_pill;
1050 const struct mdt_body *body = info->mti_body;
1051 struct mdt_body *repbody;
1057 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1060 * We save last checked parent fid to @repbody->fid1 for remote
1063 LASSERT(fid_is_sane(&body->fid2));
1064 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1065 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1066 &body->fid2, &repbody->fid1);
1067 if (rc == 0 || rc == -EREMOTE)
1068 repbody->valid |= OBD_MD_FLID;
1070 mdt_thread_info_fini(info);
1074 int mdt_swap_layouts(struct tgt_session_info *tsi)
1076 struct mdt_thread_info *info;
1077 struct ptlrpc_request *req = tgt_ses_req(tsi);
1078 struct obd_export *exp = req->rq_export;
1079 struct mdt_object *o1, *o2, *o;
1080 struct mdt_lock_handle *lh1, *lh2;
1081 struct mdc_swap_layouts *msl;
1085 /* client does not support layout lock, so layout swaping
1087 * FIXME: there is a problem for old clients which don't support
1088 * layout lock yet. If those clients have already opened the file
1089 * they won't be notified at all so that old layout may still be
1090 * used to do IO. This can be fixed after file release is landed by
1091 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1092 if (!exp_connect_layout(exp))
1093 RETURN(-EOPNOTSUPP);
1095 info = tsi2mdt_info(tsi);
1096 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1097 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1098 req_capsule_client_get(info->mti_pill,
1101 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1102 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1103 req_capsule_client_get(info->mti_pill,
1106 o1 = info->mti_object;
1107 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1108 &info->mti_body->fid2);
1110 GOTO(out, rc = PTR_ERR(o));
1112 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1113 GOTO(put, rc = -ENOENT);
1115 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1116 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1122 /* permission check. Make sure the calling process having permission
1123 * to write both files. */
1124 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1129 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1134 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1136 GOTO(put, rc = -EPROTO);
1138 lh1 = &info->mti_lh[MDT_LH_NEW];
1139 mdt_lock_reg_init(lh1, LCK_EX);
1140 lh2 = &info->mti_lh[MDT_LH_OLD];
1141 mdt_lock_reg_init(lh2, LCK_EX);
1143 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1144 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1148 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1149 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1153 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1154 mdt_object_child(o2), msl->msl_flags);
1157 mdt_object_unlock(info, o2, lh2, rc);
1159 mdt_object_unlock(info, o1, lh1, rc);
1161 mdt_object_put(info->mti_env, o);
1163 mdt_thread_info_fini(info);
1167 static int mdt_raw_lookup(struct mdt_thread_info *info,
1168 struct mdt_object *parent,
1169 const struct lu_name *lname,
1170 struct ldlm_reply *ldlm_rep)
1172 struct md_object *next = mdt_object_child(info->mti_object);
1173 const struct mdt_body *reqbody = info->mti_body;
1174 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1175 struct mdt_body *repbody;
1179 if (reqbody->valid != OBD_MD_FLID)
1182 LASSERT(!info->mti_cross_ref);
1184 /* Only got the fid of this obj by name */
1185 fid_zero(child_fid);
1186 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1189 /* XXX is raw_lookup possible as intent operation? */
1192 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1195 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1197 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1200 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1201 repbody->fid1 = *child_fid;
1202 repbody->valid = OBD_MD_FLID;
1208 * UPDATE lock should be taken against parent, and be release before exit;
1209 * child_bits lock should be taken against child, and be returned back:
1210 * (1)normal request should release the child lock;
1211 * (2)intent request will grant the lock to client.
1213 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1214 struct mdt_lock_handle *lhc,
1216 struct ldlm_reply *ldlm_rep)
1218 struct ptlrpc_request *req = mdt_info_req(info);
1219 struct mdt_body *reqbody = NULL;
1220 struct mdt_object *parent = info->mti_object;
1221 struct mdt_object *child;
1222 struct md_object *next = mdt_object_child(parent);
1223 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1224 struct lu_name *lname = NULL;
1225 struct mdt_lock_handle *lhp = NULL;
1226 struct ldlm_lock *lock;
1227 struct ldlm_res_id *res_id;
1234 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1235 LASSERT(ergo(is_resent,
1236 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1238 LASSERT(parent != NULL);
1240 lname = &info->mti_name;
1241 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1243 if (!info->mti_cross_ref) {
1245 * XXX: Check for anonymous name is for getattr by fid
1246 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1247 * that is the name must contain at least one character and
1248 * the terminating '\0'.
1250 if (!lu_name_is_valid(lname)) {
1251 reqbody = req_capsule_client_get(info->mti_pill,
1253 if (unlikely(reqbody == NULL))
1254 RETURN(err_serious(-EFAULT));
1256 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1257 RETURN(err_serious(-EINVAL));
1259 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1261 PFID(mdt_object_fid(parent)),
1262 PFID(&reqbody->fid2), ldlm_rep);
1264 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1265 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1266 PNAME(lname), ldlm_rep);
1270 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1272 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1273 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1275 "Parent doesn't exist!\n");
1277 } else if (!info->mti_cross_ref) {
1278 LASSERTF(!mdt_object_remote(parent),
1279 "Parent "DFID" is on remote server\n",
1280 PFID(mdt_object_fid(parent)));
1283 if (lu_name_is_valid(lname)) {
1284 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1292 if (info->mti_cross_ref) {
1293 /* Only getattr on the child. Parent is on another node. */
1294 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1296 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1297 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1300 /* Do not take lock for resent case. */
1301 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1302 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1303 lhc->mlh_reg_lh.cookie);
1304 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1305 &lock->l_resource->lr_name));
1306 LDLM_LOCK_PUT(lock);
1309 mdt_lock_handle_init(lhc);
1310 mdt_lock_reg_init(lhc, LCK_PR);
1313 * Object's name is on another MDS, no lookup or layout
1314 * lock is needed here but update lock is.
1316 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1317 MDS_INODELOCK_LAYOUT);
1318 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1320 rc = mdt_object_lock(info, child, lhc, child_bits,
1324 /* Finally, we can get attr for child. */
1325 if (!mdt_object_exists(child)) {
1326 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1328 "remote object doesn't exist.\n");
1329 mdt_object_unlock(info, child, lhc, 1);
1333 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1335 rc = mdt_getattr_internal(info, child, 0);
1336 if (unlikely(rc != 0))
1337 mdt_object_unlock(info, child, lhc, 1);
1342 if (lu_name_is_valid(lname)) {
1343 /* step 1: lock parent only if parent is a directory */
1344 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1345 lhp = &info->mti_lh[MDT_LH_PARENT];
1346 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1347 rc = mdt_object_lock(info, parent, lhp,
1348 MDS_INODELOCK_UPDATE,
1350 if (unlikely(rc != 0))
1354 /* step 2: lookup child's fid by name */
1355 fid_zero(child_fid);
1356 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1361 mdt_set_disposition(info, ldlm_rep,
1363 GOTO(out_parent, rc);
1365 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1367 *child_fid = reqbody->fid2;
1368 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1372 *step 3: find the child object by fid & lock it.
1373 * regardless if it is local or remote.
1375 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1376 * set parent dir fid the same as child fid in getattr by fid case
1377 * we should not lu_object_find() the object again, could lead
1378 * to hung if there is a concurrent unlink destroyed the object.
1380 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1381 mdt_object_get(info->mti_env, parent);
1384 child = mdt_object_find(info->mti_env, info->mti_mdt,
1388 if (unlikely(IS_ERR(child)))
1389 GOTO(out_parent, rc = PTR_ERR(child));
1391 /* Do not take lock for resent case. */
1392 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1393 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1394 lhc->mlh_reg_lh.cookie);
1396 res_id = &lock->l_resource->lr_name;
1397 if (!fid_res_name_eq(mdt_object_fid(child),
1398 &lock->l_resource->lr_name)) {
1399 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1400 &lock->l_resource->lr_name),
1401 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1402 PLDLMRES(lock->l_resource),
1403 PFID(mdt_object_fid(parent)));
1404 CWARN("Although resent, but still not get child lock"
1405 "parent:"DFID" child:"DFID"\n",
1406 PFID(mdt_object_fid(parent)),
1407 PFID(mdt_object_fid(child)));
1408 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1409 LDLM_LOCK_PUT(lock);
1412 LDLM_LOCK_PUT(lock);
1415 bool try_layout = false;
1418 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1419 mdt_lock_handle_init(lhc);
1420 mdt_lock_reg_init(lhc, LCK_PR);
1422 if (!mdt_object_exists(child)) {
1423 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1425 "Object doesn't exist!\n");
1426 GOTO(out_child, rc = -ENOENT);
1429 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1430 mdt_object_exists(child) && !mdt_object_remote(child)) {
1431 struct md_attr *ma = &info->mti_attr;
1434 ma->ma_need = MA_INODE;
1435 rc = mdt_attr_get_complex(info, child, ma);
1436 if (unlikely(rc != 0))
1437 GOTO(out_child, rc);
1439 /* If the file has not been changed for some time, we
1440 * return not only a LOOKUP lock, but also an UPDATE
1441 * lock and this might save us RPC on later STAT. For
1442 * directories, it also let negative dentry cache start
1443 * working for this dir. */
1444 if (ma->ma_valid & MA_INODE &&
1445 ma->ma_attr.la_valid & LA_CTIME &&
1446 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1447 ma->ma_attr.la_ctime < cfs_time_current_sec())
1448 child_bits |= MDS_INODELOCK_UPDATE;
1451 /* layout lock must be granted in a best-effort way
1452 * for IT operations */
1453 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1454 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1455 exp_connect_layout(info->mti_exp) &&
1456 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1457 !mdt_object_remote(child) && ldlm_rep != NULL) {
1458 /* try to grant layout lock for regular file. */
1464 child_bits |= MDS_INODELOCK_LAYOUT;
1465 /* try layout lock, it may fail to be granted due to
1466 * contention at LOOKUP or UPDATE */
1467 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1469 child_bits &= ~MDS_INODELOCK_LAYOUT;
1470 LASSERT(child_bits != 0);
1471 rc = mdt_object_lock(info, child, lhc,
1472 child_bits, MDT_CROSS_LOCK);
1477 rc = mdt_object_lock(info, child, lhc, child_bits,
1480 if (unlikely(rc != 0))
1481 GOTO(out_child, rc);
1484 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1485 /* Get MA_SOM attributes if update lock is given. */
1487 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1488 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1491 /* finally, we can get attr for child. */
1492 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1493 rc = mdt_getattr_internal(info, child, ma_need);
1494 if (unlikely(rc != 0)) {
1495 mdt_object_unlock(info, child, lhc, 1);
1497 /* Debugging code. */
1498 res_id = &lock->l_resource->lr_name;
1499 LDLM_DEBUG(lock, "Returning lock to client");
1500 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1501 &lock->l_resource->lr_name),
1502 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1503 PLDLMRES(lock->l_resource),
1504 PFID(mdt_object_fid(child)));
1505 if (mdt_object_exists(child) && !mdt_object_remote(child))
1506 mdt_pack_size2body(info, child);
1509 LDLM_LOCK_PUT(lock);
1513 mdt_object_put(info->mti_env, child);
1516 mdt_object_unlock(info, parent, lhp, 1);
1520 /* normal handler: should release the child lock */
1521 int mdt_getattr_name(struct tgt_session_info *tsi)
1523 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1524 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1525 struct mdt_body *reqbody;
1526 struct mdt_body *repbody;
1530 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1531 LASSERT(reqbody != NULL);
1532 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1533 LASSERT(repbody != NULL);
1535 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1536 repbody->eadatasize = 0;
1537 repbody->aclsize = 0;
1539 rc = mdt_init_ucred(info, reqbody);
1541 GOTO(out_shrink, rc);
1543 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1544 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1545 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1546 lhc->mlh_reg_lh.cookie = 0;
1548 mdt_exit_ucred(info);
1551 mdt_client_compatibility(info);
1552 rc2 = mdt_fix_reply(info);
1555 mdt_thread_info_fini(info);
1559 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1560 void *karg, void *uarg);
1562 int mdt_set_info(struct tgt_session_info *tsi)
1564 struct ptlrpc_request *req = tgt_ses_req(tsi);
1567 int keylen, vallen, rc = 0;
1571 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1573 DEBUG_REQ(D_HA, req, "no set_info key");
1574 RETURN(err_serious(-EFAULT));
1577 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1580 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1582 DEBUG_REQ(D_HA, req, "no set_info val");
1583 RETURN(err_serious(-EFAULT));
1586 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1589 /* Swab any part of val you need to here */
1590 if (KEY_IS(KEY_READ_ONLY)) {
1591 spin_lock(&req->rq_export->exp_lock);
1593 *exp_connect_flags_ptr(req->rq_export) |=
1596 *exp_connect_flags_ptr(req->rq_export) &=
1597 ~OBD_CONNECT_RDONLY;
1598 spin_unlock(&req->rq_export->exp_lock);
1599 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1600 struct changelog_setinfo *cs = val;
1602 if (vallen != sizeof(*cs)) {
1603 CERROR("%s: bad changelog_clear setinfo size %d\n",
1604 tgt_name(tsi->tsi_tgt), vallen);
1607 if (ptlrpc_req_need_swab(req)) {
1608 __swab64s(&cs->cs_recno);
1609 __swab32s(&cs->cs_id);
1612 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1620 int mdt_readpage(struct tgt_session_info *tsi)
1622 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1623 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1624 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1625 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1626 struct mdt_body *repbody;
1632 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1633 RETURN(err_serious(-ENOMEM));
1635 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1636 if (repbody == NULL || reqbody == NULL)
1637 RETURN(err_serious(-EFAULT));
1640 * prepare @rdpg before calling lower layers and transfer itself. Here
1641 * reqbody->size contains offset of where to start to read and
1642 * reqbody->nlink contains number bytes to read.
1644 rdpg->rp_hash = reqbody->size;
1645 if (rdpg->rp_hash != reqbody->size) {
1646 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1647 rdpg->rp_hash, reqbody->size);
1651 rdpg->rp_attrs = reqbody->mode;
1652 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1653 rdpg->rp_attrs |= LUDA_64BITHASH;
1654 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1655 exp_max_brw_size(tsi->tsi_exp));
1656 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1658 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1659 if (rdpg->rp_pages == NULL)
1662 for (i = 0; i < rdpg->rp_npages; ++i) {
1663 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1664 if (rdpg->rp_pages[i] == NULL)
1665 GOTO(free_rdpg, rc = -ENOMEM);
1668 /* call lower layers to fill allocated pages with directory data */
1669 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1671 GOTO(free_rdpg, rc);
1673 /* send pages to client */
1674 rc = tgt_sendpage(tsi, rdpg, rc);
1679 for (i = 0; i < rdpg->rp_npages; i++)
1680 if (rdpg->rp_pages[i] != NULL)
1681 __free_page(rdpg->rp_pages[i]);
1682 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1684 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1690 static int mdt_reint_internal(struct mdt_thread_info *info,
1691 struct mdt_lock_handle *lhc,
1694 struct req_capsule *pill = info->mti_pill;
1695 struct mdt_body *repbody;
1700 rc = mdt_reint_unpack(info, op);
1702 CERROR("Can't unpack reint, rc %d\n", rc);
1703 RETURN(err_serious(rc));
1706 /* for replay (no_create) lmm is not needed, client has it already */
1707 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1708 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1709 info->mti_rr.rr_eadatalen);
1711 /* llog cookies are always 0, the field is kept for compatibility */
1712 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1713 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1715 rc = req_capsule_server_pack(pill);
1717 CERROR("Can't pack response, rc %d\n", rc);
1718 RETURN(err_serious(rc));
1721 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1722 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1724 repbody->eadatasize = 0;
1725 repbody->aclsize = 0;
1728 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1730 /* for replay no cookkie / lmm need, because client have this already */
1731 if (info->mti_spec.no_create)
1732 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1733 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1735 rc = mdt_init_ucred_reint(info);
1737 GOTO(out_shrink, rc);
1739 rc = mdt_fix_attr_ucred(info, op);
1741 GOTO(out_ucred, rc = err_serious(rc));
1743 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1744 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1745 GOTO(out_ucred, rc);
1747 rc = mdt_reint_rec(info, lhc);
1750 mdt_exit_ucred(info);
1752 mdt_client_compatibility(info);
1753 rc2 = mdt_fix_reply(info);
1759 static long mdt_reint_opcode(struct ptlrpc_request *req,
1760 const struct req_format **fmt)
1762 struct mdt_device *mdt;
1763 struct mdt_rec_reint *rec;
1766 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1768 opc = rec->rr_opcode;
1769 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1770 if (opc < REINT_MAX && fmt[opc] != NULL)
1771 req_capsule_extend(&req->rq_pill, fmt[opc]);
1773 mdt = mdt_exp2dev(req->rq_export);
1774 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1775 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1776 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1777 opc = err_serious(-EFAULT);
1780 opc = err_serious(-EFAULT);
1785 int mdt_reint(struct tgt_session_info *tsi)
1789 static const struct req_format *reint_fmts[REINT_MAX] = {
1790 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1791 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1792 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1793 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1794 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1795 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1796 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1797 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1802 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1804 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1806 * No lock possible here from client to pass it to reint code
1809 rc = mdt_reint_internal(info, NULL, opc);
1810 mdt_thread_info_fini(info);
1815 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1819 /* this should sync the whole device */
1820 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1822 struct dt_device *dt = mdt->mdt_bottom;
1826 rc = dt->dd_ops->dt_sync(env, dt);
1830 /* this should sync this object */
1831 static int mdt_object_sync(struct mdt_thread_info *info)
1833 struct md_object *next;
1837 if (!mdt_object_exists(info->mti_object)) {
1838 CWARN("Non existing object "DFID"!\n",
1839 PFID(mdt_object_fid(info->mti_object)));
1842 next = mdt_object_child(info->mti_object);
1843 rc = mo_object_sync(info->mti_env, next);
1848 int mdt_sync(struct tgt_session_info *tsi)
1850 struct ptlrpc_request *req = tgt_ses_req(tsi);
1851 struct req_capsule *pill = tsi->tsi_pill;
1852 struct mdt_body *body;
1857 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1858 RETURN(err_serious(-ENOMEM));
1860 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1861 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1863 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1865 /* sync an object */
1866 rc = mdt_object_sync(info);
1868 const struct lu_fid *fid;
1869 struct lu_attr *la = &info->mti_attr.ma_attr;
1871 info->mti_attr.ma_need = MA_INODE;
1872 info->mti_attr.ma_valid = 0;
1873 rc = mdt_attr_get_complex(info, info->mti_object,
1876 body = req_capsule_server_get(pill,
1878 fid = mdt_object_fid(info->mti_object);
1879 mdt_pack_attr2body(info, body, la, fid);
1882 mdt_thread_info_fini(info);
1885 mdt_counter_incr(req, LPROC_MDT_SYNC);
1891 * Handle quota control requests to consult current usage/limit, but also
1892 * to configure quota enforcement
1894 int mdt_quotactl(struct tgt_session_info *tsi)
1896 struct obd_export *exp = tsi->tsi_exp;
1897 struct req_capsule *pill = tsi->tsi_pill;
1898 struct obd_quotactl *oqctl, *repoqc;
1900 struct mdt_device *mdt = mdt_exp2dev(exp);
1901 struct lu_device *qmt = mdt->mdt_qmt_dev;
1904 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1906 RETURN(err_serious(-EPROTO));
1908 rc = req_capsule_server_pack(pill);
1910 RETURN(err_serious(rc));
1912 switch (oqctl->qc_cmd) {
1914 case LUSTRE_Q_INVALIDATE:
1915 case LUSTRE_Q_FINVALIDATE:
1919 /* deprecated, not used any more */
1920 RETURN(-EOPNOTSUPP);
1921 /* master quotactl */
1927 RETURN(-EOPNOTSUPP);
1928 /* slave quotactl */
1933 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1937 /* map uid/gid for remote client */
1939 if (exp_connect_rmtclient(exp)) {
1940 struct lustre_idmap_table *idmap;
1942 idmap = exp->exp_mdt_data.med_idmap;
1944 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1945 oqctl->qc_cmd != Q_GETINFO))
1948 if (oqctl->qc_type == USRQUOTA)
1949 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1951 else if (oqctl->qc_type == GRPQUOTA)
1952 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1957 if (id == CFS_IDMAP_NOTFOUND) {
1958 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1963 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1965 RETURN(err_serious(-EFAULT));
1967 if (oqctl->qc_id != id)
1968 swap(oqctl->qc_id, id);
1970 switch (oqctl->qc_cmd) {
1976 /* forward quotactl request to QMT */
1977 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
1982 /* slave quotactl */
1983 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
1988 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1992 if (oqctl->qc_id != id)
1993 swap(oqctl->qc_id, id);
1999 /** clone llog ctxt from child (mdd)
2000 * This allows remote llog (replicator) access.
2001 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2002 * context was originally set up, or we can handle them directly.
2003 * I choose the latter, but that means I need any llog
2004 * contexts set up by child to be accessable by the mdt. So we clone the
2005 * context into our context list here.
2007 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2010 struct md_device *next = mdt->mdt_child;
2011 struct llog_ctxt *ctxt;
2014 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2017 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2018 if (rc || ctxt == NULL) {
2022 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2024 CERROR("Can't set mdt ctxt %d\n", rc);
2029 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2030 struct mdt_device *mdt, int idx)
2032 struct llog_ctxt *ctxt;
2034 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2037 /* Put once for the get we just did, and once for the clone */
2038 llog_ctxt_put(ctxt);
2039 llog_ctxt_put(ctxt);
2044 * sec context handlers
2046 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2050 rc = mdt_handle_idmap(tsi);
2052 struct ptlrpc_request *req = tgt_ses_req(tsi);
2055 opc = lustre_msg_get_opc(req->rq_reqmsg);
2056 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2057 sptlrpc_svc_ctx_invalidate(req);
2060 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2066 * quota request handlers
2068 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2070 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2071 struct lu_device *qmt = mdt->mdt_qmt_dev;
2076 RETURN(err_serious(-EOPNOTSUPP));
2078 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2082 struct mdt_object *mdt_object_new(const struct lu_env *env,
2083 struct mdt_device *d,
2084 const struct lu_fid *f)
2086 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2087 struct lu_object *o;
2088 struct mdt_object *m;
2091 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2092 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2093 if (unlikely(IS_ERR(o)))
2094 m = (struct mdt_object *)o;
2100 struct mdt_object *mdt_object_find(const struct lu_env *env,
2101 struct mdt_device *d,
2102 const struct lu_fid *f)
2104 struct lu_object *o;
2105 struct mdt_object *m;
2108 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2109 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2110 if (unlikely(IS_ERR(o)))
2111 m = (struct mdt_object *)o;
2119 * Asyncronous commit for mdt device.
2121 * Pass asynchonous commit call down the MDS stack.
2123 * \param env environment
2124 * \param mdt the mdt device
2126 static void mdt_device_commit_async(const struct lu_env *env,
2127 struct mdt_device *mdt)
2129 struct dt_device *dt = mdt->mdt_bottom;
2132 rc = dt->dd_ops->dt_commit_async(env, dt);
2133 if (unlikely(rc != 0))
2134 CWARN("async commit start failed with rc = %d", rc);
2138 * Mark the lock as "synchonous".
2140 * Mark the lock to deffer transaction commit to the unlock time.
2142 * \param lock the lock to mark as "synchonous"
2144 * \see mdt_is_lock_sync
2145 * \see mdt_save_lock
2147 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2149 lock->l_ast_data = (void*)1;
2153 * Check whehter the lock "synchonous" or not.
2155 * \param lock the lock to check
2156 * \retval 1 the lock is "synchonous"
2157 * \retval 0 the lock isn't "synchronous"
2159 * \see mdt_set_lock_sync
2160 * \see mdt_save_lock
2162 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2164 return lock->l_ast_data != NULL;
2168 * Blocking AST for mdt locks.
2170 * Starts transaction commit if in case of COS lock conflict or
2171 * deffers such a commit to the mdt_save_lock.
2173 * \param lock the lock which blocks a request or cancelling lock
2174 * \param desc unused
2175 * \param data unused
2176 * \param flag indicates whether this cancelling or blocking callback
2178 * \see ldlm_blocking_ast_nocheck
2180 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2181 void *data, int flag)
2183 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2184 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2188 if (flag == LDLM_CB_CANCELING)
2190 lock_res_and_lock(lock);
2191 if (lock->l_blocking_ast != mdt_blocking_ast) {
2192 unlock_res_and_lock(lock);
2195 if (mdt_cos_is_enabled(mdt) &&
2196 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2197 lock->l_blocking_lock != NULL &&
2198 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2199 mdt_set_lock_sync(lock);
2201 rc = ldlm_blocking_ast_nocheck(lock);
2203 /* There is no lock conflict if l_blocking_lock == NULL,
2204 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2205 * when the last reference to a local lock was released */
2206 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2209 rc = lu_env_init(&env, LCT_LOCAL);
2210 if (unlikely(rc != 0))
2211 CWARN("lu_env initialization failed with rc = %d,"
2212 "cannot start asynchronous commit\n", rc);
2214 mdt_device_commit_async(&env, mdt);
2220 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2221 void *data, int flag)
2223 struct lustre_handle lockh;
2227 case LDLM_CB_BLOCKING:
2228 ldlm_lock2handle(lock, &lockh);
2229 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2231 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2235 case LDLM_CB_CANCELING:
2236 LDLM_DEBUG(lock, "Revoke remote lock\n");
2244 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2245 struct mdt_object *o, struct lustre_handle *lh,
2246 ldlm_mode_t mode, __u64 ibits)
2248 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2249 ldlm_policy_data_t *policy = &mti->mti_policy;
2253 LASSERT(mdt_object_remote(o));
2255 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2257 memset(einfo, 0, sizeof(*einfo));
2258 einfo->ei_type = LDLM_IBITS;
2259 einfo->ei_mode = mode;
2260 einfo->ei_cb_bl = mdt_md_blocking_ast;
2261 einfo->ei_cb_cp = ldlm_completion_ast;
2263 memset(policy, 0, sizeof(*policy));
2264 policy->l_inodebits.bits = ibits;
2266 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2271 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2272 struct mdt_lock_handle *lh, __u64 ibits,
2273 bool nonblock, int locality)
2275 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2276 ldlm_policy_data_t *policy = &info->mti_policy;
2277 struct ldlm_res_id *res_id = &info->mti_res_id;
2282 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2283 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2284 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2285 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2287 if (mdt_object_remote(o)) {
2288 if (locality == MDT_CROSS_LOCK) {
2289 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2290 MDS_INODELOCK_LAYOUT);
2291 ibits |= MDS_INODELOCK_LOOKUP;
2294 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2295 MDS_INODELOCK_LAYOUT)),
2296 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2297 mdt_obd_name(info->mti_mdt), ibits,
2298 PFID(mdt_object_fid(o)));
2299 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2301 /* No PDO lock on remote object */
2302 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2305 if (lh->mlh_type == MDT_PDO_LOCK) {
2306 /* check for exists after object is locked */
2307 if (mdt_object_exists(o) == 0) {
2308 /* Non-existent object shouldn't have PDO lock */
2311 /* Non-dir object shouldn't have PDO lock */
2312 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2317 memset(policy, 0, sizeof(*policy));
2318 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2320 dlmflags = LDLM_FL_ATOMIC_CB;
2322 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2325 * Take PDO lock on whole directory and build correct @res_id for lock
2326 * on part of directory.
2328 if (lh->mlh_pdo_hash != 0) {
2329 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2330 mdt_lock_pdo_mode(info, o, lh);
2331 if (lh->mlh_pdo_mode != LCK_NL) {
2333 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2334 * is never going to be sent to client and we do not
2335 * want it slowed down due to possible cancels.
2337 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2338 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2339 policy, res_id, dlmflags,
2340 &info->mti_exp->exp_handle.h_cookie);
2346 * Finish res_id initializing by name hash marking part of
2347 * directory which is taking modification.
2349 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2352 policy->l_inodebits.bits = ibits;
2355 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2356 * going to be sent to client. If it is - mdt_intent_policy() path will
2357 * fix it up and turn FL_LOCAL flag off.
2359 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2360 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2361 &info->mti_exp->exp_handle.h_cookie);
2363 mdt_object_unlock(info, o, lh, 1);
2364 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2365 lh->mlh_pdo_hash != 0 &&
2366 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2367 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2373 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2374 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2376 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2379 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2380 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2382 struct mdt_lock_handle tmp = *lh;
2385 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2393 * Save a lock within request object.
2395 * Keep the lock referenced until whether client ACK or transaction
2396 * commit happens or release the lock immediately depending on input
2397 * parameters. If COS is ON, a write lock is converted to COS lock
2400 * \param info thead info object
2401 * \param h lock handle
2402 * \param mode lock mode
2403 * \param decref force immediate lock releasing
2406 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2407 ldlm_mode_t mode, int decref)
2411 if (lustre_handle_is_used(h)) {
2412 if (decref || !info->mti_has_trans ||
2413 !(mode & (LCK_PW | LCK_EX))){
2414 mdt_fid_unlock(h, mode);
2416 struct mdt_device *mdt = info->mti_mdt;
2417 struct ldlm_lock *lock = ldlm_handle2lock(h);
2418 struct ptlrpc_request *req = mdt_info_req(info);
2421 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2423 /* there is no request if mdt_object_unlock() is called
2424 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2425 if (likely(req != NULL)) {
2426 CDEBUG(D_HA, "request = %p reply state = %p"
2427 " transno = "LPD64"\n", req,
2428 req->rq_reply_state, req->rq_transno);
2429 if (mdt_cos_is_enabled(mdt)) {
2431 ldlm_lock_downgrade(lock, LCK_COS);
2434 ptlrpc_save_lock(req, h, mode, no_ack);
2436 ldlm_lock_decref(h, mode);
2438 if (mdt_is_lock_sync(lock)) {
2439 CDEBUG(D_HA, "found sync-lock,"
2440 " async commit started\n");
2441 mdt_device_commit_async(info->mti_env,
2444 LDLM_LOCK_PUT(lock);
2453 * Unlock mdt object.
2455 * Immeditely release the regular lock and the PDO lock or save the
2456 * lock in reqeuest and keep them referenced until client ACK or
2457 * transaction commit.
2459 * \param info thread info object
2460 * \param o mdt object
2461 * \param lh mdt lock handle referencing regular and PDO locks
2462 * \param decref force immediate lock releasing
2464 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2465 struct mdt_lock_handle *lh, int decref)
2469 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2470 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2472 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2473 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2478 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2479 const struct lu_fid *f,
2480 struct mdt_lock_handle *lh,
2483 struct mdt_object *o;
2485 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2489 rc = mdt_object_lock(info, o, lh, ibits,
2492 mdt_object_put(info->mti_env, o);
2499 void mdt_object_unlock_put(struct mdt_thread_info * info,
2500 struct mdt_object * o,
2501 struct mdt_lock_handle *lh,
2504 mdt_object_unlock(info, o, lh, decref);
2505 mdt_object_put(info->mti_env, o);
2509 * Generic code handling requests that have struct mdt_body passed in:
2511 * - extract mdt_body from request and save it in @info, if present;
2513 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2516 * - if HABEO_CORPUS flag is set for this request type check whether object
2517 * actually exists on storage (lu_object_exists()).
2520 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2522 const struct mdt_body *body;
2523 struct mdt_object *obj;
2524 const struct lu_env *env;
2525 struct req_capsule *pill;
2529 env = info->mti_env;
2530 pill = info->mti_pill;
2532 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2536 if (!(body->valid & OBD_MD_FLID))
2539 if (!fid_is_sane(&body->fid1)) {
2540 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2545 * Do not get size or any capa fields before we check that request
2546 * contains capa actually. There are some requests which do not, for
2547 * instance MDS_IS_SUBDIR.
2549 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2550 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2551 mdt_set_capainfo(info, 0, &body->fid1,
2552 req_capsule_client_get(pill, &RMF_CAPA1));
2554 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2556 if ((flags & HABEO_CORPUS) &&
2557 !mdt_object_exists(obj)) {
2558 mdt_object_put(env, obj);
2559 /* for capability renew ENOENT will be handled in
2561 if (body->valid & OBD_MD_FLOSSCAPA)
2566 info->mti_object = obj;
2575 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2577 struct req_capsule *pill = info->mti_pill;
2581 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2582 rc = mdt_body_unpack(info, flags);
2586 if (rc == 0 && (flags & HABEO_REFERO)) {
2588 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2589 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2590 info->mti_body->eadatasize);
2591 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2592 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2595 rc = req_capsule_server_pack(pill);
2600 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2602 struct md_device *next = m->mdt_child;
2604 return next->md_ops->mdo_init_capa_ctxt(env, next,
2605 m->mdt_lut.lut_mds_capa,
2606 m->mdt_capa_timeout,
2611 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2613 lh->mlh_type = MDT_NUL_LOCK;
2614 lh->mlh_reg_lh.cookie = 0ull;
2615 lh->mlh_reg_mode = LCK_MINMODE;
2616 lh->mlh_pdo_lh.cookie = 0ull;
2617 lh->mlh_pdo_mode = LCK_MINMODE;
2618 lh->mlh_rreg_lh.cookie = 0ull;
2619 lh->mlh_rreg_mode = LCK_MINMODE;
2622 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2624 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2625 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2629 * Initialize fields of struct mdt_thread_info. Other fields are left in
2630 * uninitialized state, because it's too expensive to zero out whole
2631 * mdt_thread_info (> 1K) on each request arrival.
2633 void mdt_thread_info_init(struct ptlrpc_request *req,
2634 struct mdt_thread_info *info)
2638 info->mti_pill = &req->rq_pill;
2641 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2642 mdt_lock_handle_init(&info->mti_lh[i]);
2644 /* mdt device: it can be NULL while CONNECT */
2645 if (req->rq_export) {
2646 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2647 info->mti_exp = req->rq_export;
2649 info->mti_mdt = NULL;
2650 info->mti_env = req->rq_svc_thread->t_env;
2651 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2653 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2654 info->mti_big_buf = LU_BUF_NULL;
2655 info->mti_body = NULL;
2656 info->mti_object = NULL;
2657 info->mti_dlm_req = NULL;
2658 info->mti_has_trans = 0;
2659 info->mti_cross_ref = 0;
2660 info->mti_opdata = 0;
2661 info->mti_big_lmm_used = 0;
2663 /* To not check for split by default. */
2664 info->mti_spec.no_create = 0;
2665 info->mti_spec.sp_rm_entry = 0;
2668 void mdt_thread_info_fini(struct mdt_thread_info *info)
2672 if (info->mti_object != NULL) {
2673 mdt_object_put(info->mti_env, info->mti_object);
2674 info->mti_object = NULL;
2677 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2678 mdt_lock_handle_fini(&info->mti_lh[i]);
2679 info->mti_env = NULL;
2680 info->mti_pill = NULL;
2681 info->mti_exp = NULL;
2683 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2684 lu_buf_free(&info->mti_big_buf);
2687 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2689 struct mdt_thread_info *mti;
2690 struct lustre_capa *lc;
2692 mti = mdt_th_info(tsi->tsi_env);
2693 LASSERT(mti != NULL);
2695 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2696 if (tsi->tsi_corpus != NULL) {
2697 struct req_capsule *pill = tsi->tsi_pill;
2699 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2700 lu_object_get(tsi->tsi_corpus);
2703 * XXX: must be part of tgt_mdt_body_unpack but moved here
2704 * due to mdt_set_capainfo().
2706 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2707 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2708 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2709 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2712 mti->mti_body = tsi->tsi_mdt_body;
2713 mti->mti_dlm_req = tsi->tsi_dlm_req;
2718 int mdt_tgt_connect(struct tgt_session_info *tsi)
2720 struct ptlrpc_request *req = tgt_ses_req(tsi);
2725 rc = tgt_connect(tsi);
2729 rc = mdt_init_idmap(tsi);
2734 obd_disconnect(class_export_get(req->rq_export));
2753 static int mdt_intent_getattr(enum mdt_it_code opcode,
2754 struct mdt_thread_info *info,
2755 struct ldlm_lock **,
2758 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2759 struct mdt_thread_info *info,
2760 struct ldlm_lock **lockp,
2763 static int mdt_intent_layout(enum mdt_it_code opcode,
2764 struct mdt_thread_info *info,
2765 struct ldlm_lock **,
2767 static int mdt_intent_reint(enum mdt_it_code opcode,
2768 struct mdt_thread_info *info,
2769 struct ldlm_lock **,
2772 static struct mdt_it_flavor {
2773 const struct req_format *it_fmt;
2775 int (*it_act)(enum mdt_it_code ,
2776 struct mdt_thread_info *,
2777 struct ldlm_lock **,
2780 } mdt_it_flavor[] = {
2782 .it_fmt = &RQF_LDLM_INTENT,
2783 /*.it_flags = HABEO_REFERO,*/
2785 .it_act = mdt_intent_reint,
2786 .it_reint = REINT_OPEN
2789 .it_fmt = &RQF_LDLM_INTENT,
2791 * OCREAT is not a MUTABOR request as if the file
2793 * We do the extra check of OBD_CONNECT_RDONLY in
2794 * mdt_reint_open() when we really need to create
2798 .it_act = mdt_intent_reint,
2799 .it_reint = REINT_OPEN
2802 .it_fmt = &RQF_LDLM_INTENT,
2803 .it_flags = MUTABOR,
2804 .it_act = mdt_intent_reint,
2805 .it_reint = REINT_CREATE
2807 [MDT_IT_GETATTR] = {
2808 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2809 .it_flags = HABEO_REFERO,
2810 .it_act = mdt_intent_getattr
2812 [MDT_IT_READDIR] = {
2818 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2819 .it_flags = HABEO_REFERO,
2820 .it_act = mdt_intent_getattr
2823 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2824 .it_flags = MUTABOR,
2826 .it_reint = REINT_UNLINK
2830 .it_flags = MUTABOR,
2833 [MDT_IT_GETXATTR] = {
2834 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2835 .it_flags = HABEO_CORPUS,
2836 .it_act = mdt_intent_getxattr
2839 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2841 .it_act = mdt_intent_layout
2845 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2846 struct ldlm_lock **lockp,
2847 struct ldlm_lock *new_lock,
2848 struct mdt_lock_handle *lh,
2851 struct ptlrpc_request *req = mdt_info_req(info);
2852 struct ldlm_lock *lock = *lockp;
2855 * Get new lock only for cases when possible resent did not find any
2858 if (new_lock == NULL)
2859 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2861 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2862 lh->mlh_reg_lh.cookie = 0;
2866 LASSERTF(new_lock != NULL,
2867 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2870 * If we've already given this lock to a client once, then we should
2871 * have no readers or writers. Otherwise, we should have one reader
2872 * _or_ writer ref (which will be zeroed below) before returning the
2875 if (new_lock->l_export == req->rq_export) {
2876 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2878 LASSERT(new_lock->l_export == NULL);
2879 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2884 if (new_lock->l_export == req->rq_export) {
2886 * Already gave this to the client, which means that we
2887 * reconstructed a reply.
2889 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2891 lh->mlh_reg_lh.cookie = 0;
2892 RETURN(ELDLM_LOCK_REPLACED);
2896 * Fixup the lock to be given to the client.
2898 lock_res_and_lock(new_lock);
2899 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2900 * possible blocking AST. */
2901 while (new_lock->l_readers > 0) {
2902 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2903 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2904 new_lock->l_readers--;
2906 while (new_lock->l_writers > 0) {
2907 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2908 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2909 new_lock->l_writers--;
2912 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2913 new_lock->l_blocking_ast = lock->l_blocking_ast;
2914 new_lock->l_completion_ast = lock->l_completion_ast;
2915 new_lock->l_remote_handle = lock->l_remote_handle;
2916 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2918 unlock_res_and_lock(new_lock);
2920 cfs_hash_add(new_lock->l_export->exp_lock_hash,
2921 &new_lock->l_remote_handle,
2922 &new_lock->l_exp_hash);
2924 LDLM_LOCK_RELEASE(new_lock);
2925 lh->mlh_reg_lh.cookie = 0;
2927 RETURN(ELDLM_LOCK_REPLACED);
2930 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2931 struct ldlm_lock *new_lock,
2932 struct ldlm_lock **old_lock,
2933 struct mdt_lock_handle *lh)
2935 struct ptlrpc_request *req = mdt_info_req(info);
2936 struct obd_export *exp = req->rq_export;
2937 struct lustre_handle remote_hdl;
2938 struct ldlm_request *dlmreq;
2939 struct ldlm_lock *lock;
2941 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2944 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2945 remote_hdl = dlmreq->lock_handle[0];
2947 /* In the function below, .hs_keycmp resolves to
2948 * ldlm_export_lock_keycmp() */
2949 /* coverity[overrun-buffer-val] */
2950 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
2952 if (lock != new_lock) {
2953 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2954 lh->mlh_reg_mode = lock->l_granted_mode;
2956 LDLM_DEBUG(lock, "Restoring lock cookie");
2957 DEBUG_REQ(D_DLMTRACE, req,
2958 "restoring lock cookie "LPX64,
2959 lh->mlh_reg_lh.cookie);
2961 *old_lock = LDLM_LOCK_GET(lock);
2962 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2966 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2970 * If the xid matches, then we know this is a resent request, and allow
2971 * it. (It's probably an OPEN, for which we don't send a lock.
2973 if (req_xid_is_last(req))
2977 * This remote handle isn't enqueued, so we never received or processed
2978 * this request. Clear MSG_RESENT, because it can be handled like any
2979 * normal request now.
2981 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2983 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2987 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2988 struct mdt_thread_info *info,
2989 struct ldlm_lock **lockp,
2992 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2993 struct ldlm_reply *ldlm_rep = NULL;
2997 * Initialize lhc->mlh_reg_lh either from a previously granted lock
2998 * (for the resend case) or a new lock. Below we will use it to
2999 * replace the original lock.
3001 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3002 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3003 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3004 rc = mdt_object_lock(info, info->mti_object, lhc,
3005 MDS_INODELOCK_XATTR,
3011 grc = mdt_getxattr(info);
3013 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3015 if (mdt_info_req(info)->rq_repmsg != NULL)
3016 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3017 if (ldlm_rep == NULL)
3018 RETURN(err_serious(-EFAULT));
3020 ldlm_rep->lock_policy_res2 = grc;
3025 static int mdt_intent_getattr(enum mdt_it_code opcode,
3026 struct mdt_thread_info *info,
3027 struct ldlm_lock **lockp,
3030 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3031 struct ldlm_lock *new_lock = NULL;
3033 struct ldlm_reply *ldlm_rep;
3034 struct ptlrpc_request *req;
3035 struct mdt_body *reqbody;
3036 struct mdt_body *repbody;
3040 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3043 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3046 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3047 repbody->eadatasize = 0;
3048 repbody->aclsize = 0;
3052 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3054 case MDT_IT_GETATTR:
3055 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3059 CERROR("Unsupported intent (%d)\n", opcode);
3060 GOTO(out_shrink, rc = -EINVAL);
3063 rc = mdt_init_ucred(info, reqbody);
3065 GOTO(out_shrink, rc);
3067 req = info->mti_pill->rc_req;
3068 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3069 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3071 /* Get lock from request for possible resent case. */
3072 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3074 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3075 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3077 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3078 ldlm_rep->lock_policy_res2 = 0;
3079 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3080 ldlm_rep->lock_policy_res2) {
3081 lhc->mlh_reg_lh.cookie = 0ull;
3082 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3085 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3088 mdt_exit_ucred(info);
3090 mdt_client_compatibility(info);
3091 rc2 = mdt_fix_reply(info);
3097 static int mdt_intent_layout(enum mdt_it_code opcode,
3098 struct mdt_thread_info *info,
3099 struct ldlm_lock **lockp,
3102 struct layout_intent *layout;
3104 struct mdt_object *obj = NULL;
3105 struct md_object *child = NULL;
3109 if (opcode != MDT_IT_LAYOUT) {
3110 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3115 fid = &info->mti_tmp_fid2;
3116 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3118 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3120 RETURN(PTR_ERR(obj));
3122 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3123 child = mdt_object_child(obj);
3125 /* get the length of lsm */
3126 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3129 if (rc > info->mti_mdt->mdt_max_mdsize)
3130 info->mti_mdt->mdt_max_mdsize = rc;
3133 mdt_object_put(info->mti_env, obj);
3135 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3136 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3137 ldlm_lvbo_size(*lockp));
3138 rc = req_capsule_server_pack(info->mti_pill);
3142 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3143 LASSERT(layout != NULL);
3144 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3145 /* return to normal ldlm handling */
3148 CERROR("%s: Unsupported layout intent (%d)\n",
3149 mdt_obd_name(info->mti_mdt), layout->li_opc);
3153 static int mdt_intent_reint(enum mdt_it_code opcode,
3154 struct mdt_thread_info *info,
3155 struct ldlm_lock **lockp,
3158 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3159 struct ldlm_reply *rep = NULL;
3163 static const struct req_format *intent_fmts[REINT_MAX] = {
3164 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3165 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3170 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3174 if (mdt_it_flavor[opcode].it_reint != opc) {
3175 CERROR("Reint code %ld doesn't match intent: %d\n",
3177 RETURN(err_serious(-EPROTO));
3180 /* Get lock from request for possible resent case. */
3181 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3183 rc = mdt_reint_internal(info, lhc, opc);
3185 /* Check whether the reply has been packed successfully. */
3186 if (mdt_info_req(info)->rq_repmsg != NULL)
3187 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3189 RETURN(err_serious(-EFAULT));
3191 /* MDC expects this in any case */
3193 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3195 /* the open lock or the lock for cross-ref object should be
3196 * returned to the client */
3197 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3198 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3199 rep->lock_policy_res2 = 0;
3200 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3204 rep->lock_policy_res2 = clear_serious(rc);
3206 if (rep->lock_policy_res2 == -ENOENT &&
3207 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3208 rep->lock_policy_res2 = 0;
3210 if (rc == -ENOTCONN || rc == -ENODEV ||
3211 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3213 * If it is the disconnect error (ENODEV & ENOCONN), the error
3214 * will be returned by rq_status, and client at ptlrpc layer
3215 * will detect this, then disconnect, reconnect the import
3216 * immediately, instead of impacting the following the rpc.
3218 lhc->mlh_reg_lh.cookie = 0ull;
3222 * For other cases, the error will be returned by intent.
3223 * and client will retrieve the result from intent.
3226 * FIXME: when open lock is finished, that should be
3229 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3230 LASSERTF(rc == 0, "Error occurred but lock handle "
3231 "is still in use, rc = %d\n", rc);
3232 rep->lock_policy_res2 = 0;
3233 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3236 lhc->mlh_reg_lh.cookie = 0ull;
3237 RETURN(ELDLM_LOCK_ABORTED);
3242 static int mdt_intent_code(long itcode)
3250 case IT_OPEN|IT_CREAT:
3257 rc = MDT_IT_READDIR;
3260 rc = MDT_IT_GETATTR;
3272 rc = MDT_IT_GETXATTR;
3277 case IT_QUOTA_DQACQ:
3282 CERROR("Unknown intent opcode: %ld\n", itcode);
3289 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3290 struct ldlm_lock **lockp, __u64 flags)
3292 struct req_capsule *pill;
3293 struct mdt_it_flavor *flv;
3298 opc = mdt_intent_code(itopc);
3302 pill = info->mti_pill;
3304 if (opc == MDT_IT_QUOTA) {
3305 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3308 RETURN(-EOPNOTSUPP);
3310 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3311 /* pass the request to quota master */
3312 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3313 mdt_info_req(info), lockp,
3318 flv = &mdt_it_flavor[opc];
3319 if (flv->it_fmt != NULL)
3320 req_capsule_extend(pill, flv->it_fmt);
3322 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3324 struct ptlrpc_request *req = mdt_info_req(info);
3325 if (flv->it_flags & MUTABOR &&
3326 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3329 if (rc == 0 && flv->it_act != NULL) {
3330 struct ldlm_reply *rep;
3332 /* execute policy */
3333 rc = flv->it_act(opc, info, lockp, flags);
3335 /* Check whether the reply has been packed successfully. */
3336 if (mdt_info_req(info)->rq_repmsg != NULL) {
3337 rep = req_capsule_server_get(info->mti_pill,
3339 rep->lock_policy_res2 =
3340 ptlrpc_status_hton(rep->lock_policy_res2);
3348 static int mdt_intent_policy(struct ldlm_namespace *ns,
3349 struct ldlm_lock **lockp, void *req_cookie,
3350 ldlm_mode_t mode, __u64 flags, void *data)
3352 struct tgt_session_info *tsi;
3353 struct mdt_thread_info *info;
3354 struct ptlrpc_request *req = req_cookie;
3355 struct ldlm_intent *it;
3356 struct req_capsule *pill;
3361 LASSERT(req != NULL);
3363 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3365 info = tsi2mdt_info(tsi);
3366 LASSERT(info != NULL);
3367 pill = info->mti_pill;
3368 LASSERT(pill->rc_req == req);
3370 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3371 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3372 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3374 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3378 /* Lock without inodebits makes no sense and will oops
3379 * later in ldlm. Let's check it now to see if we have
3380 * ibits corrupted somewhere in mdt_intent_opc().
3381 * The case for client miss to set ibits has been
3382 * processed by others. */
3383 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3384 lr_type == LDLM_IBITS,
3385 info->mti_dlm_req->lock_desc.\
3386 l_policy_data.l_inodebits.bits != 0));
3388 rc = err_serious(-EFAULT);
3390 /* No intent was provided */
3391 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3392 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3393 rc = req_capsule_server_pack(pill);
3395 rc = err_serious(rc);
3397 mdt_thread_info_fini(info);
3401 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3403 struct seq_server_site *ss = mdt_seq_site(mdt);
3405 if (ss->ss_node_id == 0)
3408 if (ss->ss_client_seq != NULL) {
3409 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3410 ss->ss_client_seq->lcs_exp = NULL;
3413 if (ss->ss_server_fld != NULL) {
3414 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3415 ss->ss_server_fld->lsf_control_exp = NULL;
3419 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3421 struct seq_server_site *ss = mdt_seq_site(mdt);
3426 if (ss->ss_server_seq == NULL)
3427 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3432 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3434 mdt_seq_fini_cli(mdt);
3435 mdt_deregister_seq_exp(mdt);
3437 return seq_site_fini(env, mdt_seq_site(mdt));
3441 * It will retrieve its FLDB entries from MDT0, and it only happens
3442 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3443 * and it needs to refresh FLDB from the MDT0.
3445 static int mdt_register_lwp_callback(void *data)
3448 struct mdt_device *mdt = data;
3449 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3453 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3455 if (!likely(fld->lsf_new))
3458 rc = lu_env_init(&env, LCT_MD_THREAD);
3460 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3464 rc = fld_update_from_controller(&env, fld);
3466 CERROR("%s: cannot update controller: rc = %d\n",
3467 mdt_obd_name(mdt), rc);
3475 static int mdt_register_seq_exp(struct mdt_device *mdt)
3477 struct seq_server_site *ss = mdt_seq_site(mdt);
3478 char *lwp_name = NULL;
3481 if (ss->ss_node_id == 0)
3484 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3485 if (lwp_name == NULL)
3486 GOTO(out_free, rc = -ENOMEM);
3488 rc = tgt_name2lwpname(mdt_obd_name(mdt), lwp_name);
3492 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3497 rc = lustre_register_lwp_item(lwp_name,
3498 &ss->ss_server_fld->lsf_control_exp,
3499 mdt_register_lwp_callback, mdt);
3501 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3502 ss->ss_client_seq->lcs_exp = NULL;
3506 if (lwp_name != NULL)
3507 OBD_FREE(lwp_name, MAX_OBD_NAME);
3513 * Init client sequence manager which is used by local MDS to talk to sequence
3514 * controller on remote node.
3516 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3518 struct seq_server_site *ss = mdt_seq_site(mdt);
3523 /* check if this is adding the first MDC and controller is not yet
3525 OBD_ALLOC_PTR(ss->ss_client_seq);
3526 if (ss->ss_client_seq == NULL)
3529 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3530 if (prefix == NULL) {
3531 OBD_FREE_PTR(ss->ss_client_seq);
3532 ss->ss_client_seq = NULL;
3536 /* Note: seq_client_fini will be called in seq_site_fini */
3537 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3538 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3539 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3541 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3543 OBD_FREE_PTR(ss->ss_client_seq);
3544 ss->ss_client_seq = NULL;
3548 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3553 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3555 struct seq_server_site *ss;
3559 ss = mdt_seq_site(mdt);
3560 /* init sequence controller server(MDT0) */
3561 if (ss->ss_node_id == 0) {
3562 OBD_ALLOC_PTR(ss->ss_control_seq);
3563 if (ss->ss_control_seq == NULL)
3566 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3567 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3570 GOTO(out_seq_fini, rc);
3573 /* Init normal sequence server */
3574 OBD_ALLOC_PTR(ss->ss_server_seq);
3575 if (ss->ss_server_seq == NULL)
3576 GOTO(out_seq_fini, rc = -ENOMEM);
3578 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3579 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3581 GOTO(out_seq_fini, rc);
3583 /* init seq client for seq server to talk to seq controller(MDT0) */
3584 rc = mdt_seq_init_cli(env, mdt);
3586 GOTO(out_seq_fini, rc);
3588 if (ss->ss_node_id != 0)
3589 /* register controler export through lwp */
3590 rc = mdt_register_seq_exp(mdt);
3595 mdt_seq_fini(env, mdt);
3603 static int mdt_fld_fini(const struct lu_env *env,
3604 struct mdt_device *m)
3606 struct seq_server_site *ss = mdt_seq_site(m);
3609 if (ss && ss->ss_server_fld) {
3610 fld_server_fini(env, ss->ss_server_fld);
3611 OBD_FREE_PTR(ss->ss_server_fld);
3612 ss->ss_server_fld = NULL;
3618 static int mdt_fld_init(const struct lu_env *env,
3620 struct mdt_device *m)
3622 struct seq_server_site *ss;
3626 ss = mdt_seq_site(m);
3628 OBD_ALLOC_PTR(ss->ss_server_fld);
3629 if (ss->ss_server_fld == NULL)
3630 RETURN(rc = -ENOMEM);
3632 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3635 OBD_FREE_PTR(ss->ss_server_fld);
3636 ss->ss_server_fld = NULL;
3643 static void mdt_stack_pre_fini(const struct lu_env *env,
3644 struct mdt_device *m, struct lu_device *top)
3646 struct obd_device *obd;
3647 struct lustre_cfg_bufs *bufs;
3648 struct lustre_cfg *lcfg;
3649 struct mdt_thread_info *info;
3654 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3655 LASSERT(info != NULL);
3657 bufs = &info->mti_u.bufs;
3659 LASSERT(m->mdt_child_exp);
3660 LASSERT(m->mdt_child_exp->exp_obd);
3661 obd = m->mdt_child_exp->exp_obd;
3663 /* process cleanup, pass mdt obd name to get obd umount flags */
3664 /* XXX: this is needed because all layers are referenced by
3665 * objects (some of them are pinned by osd, for example *
3666 * the proper solution should be a model where object used
3667 * by osd only doesn't have mdt/mdd slices -bzzz */
3668 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3669 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3670 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3672 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3675 top->ld_ops->ldo_process_config(env, top, lcfg);
3676 lustre_cfg_free(lcfg);
3680 static void mdt_stack_fini(const struct lu_env *env,
3681 struct mdt_device *m, struct lu_device *top)
3683 struct obd_device *obd = mdt2obd_dev(m);
3684 struct lustre_cfg_bufs *bufs;
3685 struct lustre_cfg *lcfg;
3686 struct mdt_thread_info *info;
3690 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3691 LASSERT(info != NULL);
3693 lu_dev_del_linkage(top->ld_site, top);
3695 lu_site_purge(env, top->ld_site, -1);
3697 bufs = &info->mti_u.bufs;
3698 /* process cleanup, pass mdt obd name to get obd umount flags */
3699 /* another purpose is to let all layers to release their objects */
3700 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3705 lustre_cfg_bufs_set_string(bufs, 1, flags);
3706 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3708 CERROR("Cannot alloc lcfg!\n");
3712 top->ld_ops->ldo_process_config(env, top, lcfg);
3713 lustre_cfg_free(lcfg);
3715 lu_site_purge(env, top->ld_site, -1);
3717 m->mdt_child = NULL;
3718 m->mdt_bottom = NULL;
3720 obd_disconnect(m->mdt_child_exp);
3721 m->mdt_child_exp = NULL;
3723 obd_disconnect(m->mdt_bottom_exp);
3724 m->mdt_child_exp = NULL;
3727 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3728 const char *next, struct obd_export **exp)
3730 struct obd_connect_data *data = NULL;
3731 struct obd_device *obd;
3735 OBD_ALLOC_PTR(data);
3737 GOTO(out, rc = -ENOMEM);
3739 obd = class_name2obd(next);
3741 CERROR("%s: can't locate next device: %s\n",
3742 mdt_obd_name(m), next);
3743 GOTO(out, rc = -ENOTCONN);
3746 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3747 data->ocd_version = LUSTRE_VERSION_CODE;
3749 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3751 CERROR("%s: cannot connect to next dev %s (%d)\n",
3752 mdt_obd_name(m), next, rc);
3762 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3763 struct lustre_cfg *cfg)
3765 char *dev = lustre_cfg_string(cfg, 0);
3766 int rc, name_size, uuid_size;
3767 char *name, *uuid, *p;
3768 struct lustre_cfg_bufs *bufs;
3769 struct lustre_cfg *lcfg;
3770 struct obd_device *obd;
3771 struct lustre_profile *lprof;
3772 struct lu_site *site;
3775 /* in 1.8 we had the only device in the stack - MDS.
3776 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3777 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3778 * to generate names and setup MDT, MDD. MDT will be using
3779 * generated name to connect to MDD. for MDD the next device
3780 * will be LOD with name taken from so called "profile" which
3781 * is generated by mount_option line
3783 * 1.8 MGS generates config. commands like this:
3784 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3785 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3786 * 2.0 MGS generates config. commands like this:
3787 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3788 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3789 * 3:lustre-MDT0000-mdtlov 4:f
3791 * we generate MDD name from MDT one, just replacing T with D
3793 * after all the preparations, the logical equivalent will be
3794 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3795 * 3:lustre-MDT0000-mdtlov 4:f
3796 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3797 * 3:lustre-MDD0000 4:f
3799 * notice we build the stack from down to top: MDD first, then MDT */
3801 name_size = MAX_OBD_NAME;
3802 uuid_size = MAX_OBD_NAME;
3804 OBD_ALLOC(name, name_size);
3805 OBD_ALLOC(uuid, uuid_size);
3806 if (name == NULL || uuid == NULL)
3807 GOTO(cleanup_mem, rc = -ENOMEM);
3809 OBD_ALLOC_PTR(bufs);
3811 GOTO(cleanup_mem, rc = -ENOMEM);
3814 p = strstr(name, "-MDT");
3816 GOTO(free_bufs, rc = -ENOMEM);
3819 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3821 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3822 if (lprof == NULL || lprof->lp_dt == NULL) {
3823 CERROR("can't find the profile: %s\n",
3824 lustre_cfg_string(cfg, 0));
3825 GOTO(free_bufs, rc = -EINVAL);
3828 lustre_cfg_bufs_reset(bufs, name);
3829 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3830 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3831 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3833 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3835 GOTO(free_bufs, rc = -ENOMEM);
3837 rc = class_attach(lcfg);
3839 GOTO(lcfg_cleanup, rc);
3841 obd = class_name2obd(name);
3843 CERROR("Can not find obd %s (%s in config)\n",
3844 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
3845 GOTO(class_detach, rc = -EINVAL);
3848 lustre_cfg_free(lcfg);
3850 lustre_cfg_bufs_reset(bufs, name);
3851 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3852 lustre_cfg_bufs_set_string(bufs, 2, dev);
3853 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3855 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3857 rc = class_setup(obd, lcfg);
3859 GOTO(class_detach, rc);
3861 /* connect to MDD we just setup */
3862 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
3864 GOTO(class_detach, rc);
3866 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
3868 LASSERT(mdt_lu_site(mdt) == NULL);
3869 mdt->mdt_lu_dev.ld_site = site;
3870 site->ls_top_dev = &mdt->mdt_lu_dev;
3871 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
3874 /* now connect to bottom OSD */
3875 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
3876 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
3878 GOTO(class_detach, rc);
3880 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
3883 rc = lu_env_refill((struct lu_env *)env);
3885 CERROR("Failure to refill session: '%d'\n", rc);
3887 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
3892 class_detach(obd, lcfg);
3894 lustre_cfg_free(lcfg);
3899 OBD_FREE(name, name_size);
3901 OBD_FREE(uuid, uuid_size);
3905 /* setup quota master target on MDT0 */
3906 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
3907 struct lustre_cfg *cfg)
3909 struct obd_device *obd;
3910 char *dev = lustre_cfg_string(cfg, 0);
3911 char *qmtname, *uuid, *p;
3912 struct lustre_cfg_bufs *bufs;
3913 struct lustre_cfg *lcfg;
3914 struct lustre_profile *lprof;
3915 struct obd_connect_data *data;
3919 LASSERT(mdt->mdt_qmt_exp == NULL);
3920 LASSERT(mdt->mdt_qmt_dev == NULL);
3922 /* quota master is on MDT0 only for now */
3923 if (mdt->mdt_seq_site.ss_node_id != 0)
3926 /* MGS generates config commands which look as follows:
3927 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3928 * 3:lustre-MDT0000-mdtlov 4:f
3930 * We generate the QMT name from the MDT one, just replacing MD with QM
3931 * after all the preparations, the logical equivalent will be:
3932 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
3933 * 3:lustre-MDT0000-osd 4:f */
3934 OBD_ALLOC(qmtname, MAX_OBD_NAME);
3935 OBD_ALLOC(uuid, UUID_MAX);
3936 OBD_ALLOC_PTR(bufs);
3937 OBD_ALLOC_PTR(data);
3938 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
3939 GOTO(cleanup_mem, rc = -ENOMEM);
3941 strcpy(qmtname, dev);
3942 p = strstr(qmtname, "-MDT");
3944 GOTO(cleanup_mem, rc = -ENOMEM);
3945 /* replace MD with QM */
3949 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
3951 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3952 if (lprof == NULL || lprof->lp_dt == NULL) {
3953 CERROR("can't find profile for %s\n",
3954 lustre_cfg_string(cfg, 0));
3955 GOTO(cleanup_mem, rc = -EINVAL);
3958 lustre_cfg_bufs_reset(bufs, qmtname);
3959 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
3960 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3961 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3963 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3965 GOTO(cleanup_mem, rc = -ENOMEM);
3967 rc = class_attach(lcfg);
3969 GOTO(lcfg_cleanup, rc);
3971 obd = class_name2obd(qmtname);
3973 CERROR("Can not find obd %s (%s in config)\n", qmtname,
3974 lustre_cfg_string(cfg, 0));
3975 GOTO(class_detach, rc = -EINVAL);
3978 lustre_cfg_free(lcfg);
3980 lustre_cfg_bufs_reset(bufs, qmtname);
3981 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3982 lustre_cfg_bufs_set_string(bufs, 2, dev);
3984 /* for quota, the next device should be the OSD device */
3985 lustre_cfg_bufs_set_string(bufs, 3,
3986 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
3988 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3990 rc = class_setup(obd, lcfg);
3992 GOTO(class_detach, rc);
3994 mdt->mdt_qmt_dev = obd->obd_lu_dev;
3996 /* configure local quota objects */
3997 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4001 GOTO(class_cleanup, rc);
4003 /* connect to quota master target */
4004 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4005 data->ocd_version = LUSTRE_VERSION_CODE;
4006 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4009 CERROR("cannot connect to quota master device %s (%d)\n",
4011 GOTO(class_cleanup, rc);
4017 class_manual_cleanup(obd);
4018 mdt->mdt_qmt_dev = NULL;
4022 class_detach(obd, lcfg);
4024 lustre_cfg_free(lcfg);
4029 OBD_FREE(qmtname, MAX_OBD_NAME);
4031 OBD_FREE(uuid, UUID_MAX);
4037 /* Shutdown quota master target associated with mdt */
4038 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4042 if (mdt->mdt_qmt_exp == NULL)
4044 LASSERT(mdt->mdt_qmt_dev != NULL);
4046 /* the qmt automatically shuts down when the mdt disconnects */
4047 obd_disconnect(mdt->mdt_qmt_exp);
4048 mdt->mdt_qmt_exp = NULL;
4049 mdt->mdt_qmt_dev = NULL;
4053 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4054 * for now. This will be removed along with converting rest of MDT code
4055 * to use tgt_session_info */
4056 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4058 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4061 rc = mdt_getxattr(info);
4063 mdt_thread_info_fini(info);
4067 static struct tgt_handler mdt_tgt_handlers[] = {
4068 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4069 0, MDS_CONNECT, mdt_tgt_connect,
4070 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4071 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4072 0, MDS_DISCONNECT, tgt_disconnect,
4073 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4074 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4075 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4076 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4077 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4078 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4079 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4080 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4082 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4083 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4084 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4085 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4086 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4088 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4089 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4090 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4091 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4092 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4094 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4095 mdt_hsm_ct_register),
4096 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4097 mdt_hsm_ct_unregister),
4098 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4100 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4102 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4103 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4105 TGT_MDT_HDL(HABEO_CORPUS|HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
4109 static struct tgt_handler mdt_sec_ctx_ops[] = {
4110 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4111 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4112 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4115 static struct tgt_handler mdt_quota_ops[] = {
4116 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4119 static struct tgt_opc_slice mdt_common_slice[] = {
4121 .tos_opc_start = MDS_FIRST_OPC,
4122 .tos_opc_end = MDS_LAST_OPC,
4123 .tos_hs = mdt_tgt_handlers
4126 .tos_opc_start = OBD_FIRST_OPC,
4127 .tos_opc_end = OBD_LAST_OPC,
4128 .tos_hs = tgt_obd_handlers
4131 .tos_opc_start = LDLM_FIRST_OPC,
4132 .tos_opc_end = LDLM_LAST_OPC,
4133 .tos_hs = tgt_dlm_handlers
4136 .tos_opc_start = SEC_FIRST_OPC,
4137 .tos_opc_end = SEC_LAST_OPC,
4138 .tos_hs = mdt_sec_ctx_ops
4141 .tos_opc_start = UPDATE_OBJ,
4142 .tos_opc_end = UPDATE_LAST_OPC,
4143 .tos_hs = tgt_out_handlers
4146 .tos_opc_start = FLD_FIRST_OPC,
4147 .tos_opc_end = FLD_LAST_OPC,
4148 .tos_hs = fld_handlers
4151 .tos_opc_start = SEQ_FIRST_OPC,
4152 .tos_opc_end = SEQ_LAST_OPC,
4153 .tos_hs = seq_handlers
4156 .tos_opc_start = QUOTA_DQACQ,
4157 .tos_opc_end = QUOTA_LAST_OPC,
4158 .tos_hs = mdt_quota_ops
4161 .tos_opc_start = LLOG_FIRST_OPC,
4162 .tos_opc_end = LLOG_LAST_OPC,
4163 .tos_hs = tgt_llog_handlers
4171 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4173 struct md_device *next = m->mdt_child;
4174 struct lu_device *d = &m->mdt_lu_dev;
4175 struct obd_device *obd = mdt2obd_dev(m);
4178 target_recovery_fini(obd);
4180 ping_evictor_stop();
4182 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4184 if (m->mdt_opts.mo_coordinator)
4185 mdt_hsm_cdt_stop(m);
4187 mdt_hsm_cdt_fini(m);
4189 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4190 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4191 obd_exports_barrier(obd);
4192 obd_zombie_barrier();
4196 tgt_fini(env, &m->mdt_lut);
4197 mdt_fs_cleanup(env, m);
4198 upcall_cache_cleanup(m->mdt_identity_cache);
4199 m->mdt_identity_cache = NULL;
4201 if (m->mdt_namespace != NULL) {
4202 ldlm_namespace_free(m->mdt_namespace, NULL,
4203 d->ld_obd->obd_force);
4204 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4207 mdt_quota_fini(env, m);
4209 cfs_free_nidlist(&m->mdt_nosquash_nids);
4210 if (m->mdt_nosquash_str) {
4211 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4212 m->mdt_nosquash_str = NULL;
4213 m->mdt_nosquash_strlen = 0;
4216 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4218 mdt_seq_fini(env, m);
4219 mdt_fld_fini(env, m);
4221 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4222 cfs_timer_disarm(&m->mdt_ck_timer);
4223 mdt_ck_thread_stop(m);
4228 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4230 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4232 server_put_mount(mdt_obd_name(m));
4237 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4239 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4240 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4242 struct mdt_thread_info *info;
4243 struct obd_device *obd;
4244 const char *dev = lustre_cfg_string(cfg, 0);
4245 const char *num = lustre_cfg_string(cfg, 2);
4246 struct lustre_mount_info *lmi = NULL;
4247 struct lustre_sb_info *lsi;
4249 struct seq_server_site *ss_site;
4250 const char *identity_upcall = "NONE";
4251 struct md_device *next;
4257 lu_device_init(&m->mdt_lu_dev, ldt);
4259 * Environment (env) might be missing mdt_thread_key values at that
4260 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4263 * Usually device allocation path doesn't use module key values, but
4264 * mdt has to do a lot of work here, so allocate key value.
4266 rc = lu_env_refill((struct lu_env *)env);
4270 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4271 LASSERT(info != NULL);
4273 obd = class_name2obd(dev);
4274 LASSERT(obd != NULL);
4276 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4278 m->mdt_som_conf = 0;
4280 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4282 /* default is coordinator off, it is started through conf_param
4284 m->mdt_opts.mo_coordinator = 0;
4286 lmi = server_get_mount(dev);
4288 CERROR("Cannot get mount info for %s!\n", dev);
4291 lsi = s2lsi(lmi->lmi_sb);
4292 /* CMD is supported only in IAM mode */
4294 node_id = simple_strtol(num, NULL, 10);
4295 obd->u.obt.obt_magic = OBT_MAGIC;
4298 spin_lock_init(&m->mdt_ioepoch_lock);
4299 m->mdt_capa_timeout = CAPA_TIMEOUT;
4300 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4301 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4302 m->mdt_squash_uid = 0;
4303 m->mdt_squash_gid = 0;
4304 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4305 m->mdt_nosquash_str = NULL;
4306 m->mdt_nosquash_strlen = 0;
4307 init_rwsem(&m->mdt_squash_sem);
4308 spin_lock_init(&m->mdt_osfs_lock);
4309 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4310 m->mdt_enable_remote_dir = 0;
4311 m->mdt_enable_remote_dir_gid = 0;
4313 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4314 m->mdt_lu_dev.ld_obd = obd;
4315 /* Set this lu_device to obd for error handling purposes. */
4316 obd->obd_lu_dev = &m->mdt_lu_dev;
4318 /* init the stack */
4319 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4321 CERROR("%s: Can't init device stack, rc %d\n",
4322 mdt_obd_name(m), rc);
4327 ss_site = mdt_seq_site(m);
4328 s->ld_seq_site = ss_site;
4331 /* set server index */
4332 ss_site->ss_node_id = node_id;
4334 /* failover is the default
4335 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4336 * assumed whose ss_node_id == 0 XXX
4338 obd->obd_replayable = 1;
4339 /* No connection accepted until configurations will finish */
4340 obd->obd_no_conn = 1;
4342 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4343 char *str = lustre_cfg_string(cfg, 4);
4344 if (strchr(str, 'n')) {
4345 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4346 obd->obd_replayable = 0;
4350 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4352 GOTO(err_fini_stack, rc);
4354 rc = mdt_seq_init(env, m);
4356 GOTO(err_fini_fld, rc);
4358 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4359 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4360 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4361 LDLM_NAMESPACE_SERVER,
4362 LDLM_NAMESPACE_GREEDY,
4364 if (m->mdt_namespace == NULL)
4365 GOTO(err_fini_seq, rc = -ENOMEM);
4367 m->mdt_namespace->ns_lvbp = m;
4368 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4370 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4371 /* set obd_namespace for compatibility with old code */
4372 obd->obd_namespace = m->mdt_namespace;
4374 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4376 rc = mdt_hsm_cdt_init(m);
4378 CERROR("%s: error initializing coordinator, rc %d\n",
4379 mdt_obd_name(m), rc);
4380 GOTO(err_free_ns, rc);
4383 rc = mdt_ck_thread_start(m);
4385 GOTO(err_free_hsm, rc);
4387 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4388 OBD_FAIL_MDS_ALL_REQUEST_NET,
4389 OBD_FAIL_MDS_ALL_REPLY_NET);
4393 rc = mdt_fs_setup(env, m, obd, lsi);
4397 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4399 next = m->mdt_child;
4400 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4403 GOTO(err_fs_cleanup, rc);
4405 if (mntopts & MNTOPT_USERXATTR)
4406 m->mdt_opts.mo_user_xattr = 1;
4408 m->mdt_opts.mo_user_xattr = 0;
4410 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4412 GOTO(err_fs_cleanup, rc);
4414 if (mntopts & MNTOPT_ACL)
4415 m->mdt_opts.mo_acl = 1;
4417 m->mdt_opts.mo_acl = 0;
4419 /* XXX: to support suppgid for ACL, we enable identity_upcall
4420 * by default, otherwise, maybe got unexpected -EACCESS. */
4421 if (m->mdt_opts.mo_acl)
4422 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4424 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4426 &mdt_identity_upcall_cache_ops);
4427 if (IS_ERR(m->mdt_identity_cache)) {
4428 rc = PTR_ERR(m->mdt_identity_cache);
4429 m->mdt_identity_cache = NULL;
4430 GOTO(err_fs_cleanup, rc);
4433 rc = mdt_procfs_init(m, dev);
4435 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4436 GOTO(err_recovery, rc);
4439 rc = mdt_quota_init(env, m, cfg);
4441 GOTO(err_procfs, rc);
4443 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4444 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4445 "mdt_ldlm_client", m->mdt_ldlm_client);
4447 ping_evictor_start();
4449 /* recovery will be started upon mdt_prepare()
4450 * when the whole stack is complete and ready
4451 * to serve the requests */
4453 mdt_init_capa_ctxt(env, m);
4455 /* Reduce the initial timeout on an MDS because it doesn't need such
4456 * a long timeout as an OST does. Adaptive timeouts will adjust this
4457 * value appropriately. */
4458 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4459 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4465 target_recovery_fini(obd);
4466 upcall_cache_cleanup(m->mdt_identity_cache);
4467 m->mdt_identity_cache = NULL;
4469 mdt_fs_cleanup(env, m);
4471 tgt_fini(env, &m->mdt_lut);
4473 cfs_timer_disarm(&m->mdt_ck_timer);
4474 mdt_ck_thread_stop(m);
4476 mdt_hsm_cdt_fini(m);
4478 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4479 obd->obd_namespace = m->mdt_namespace = NULL;
4481 mdt_seq_fini(env, m);
4483 mdt_fld_fini(env, m);
4485 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4488 server_put_mount(dev);
4492 /* For interoperability, the left element is old parameter, the right one
4493 * is the new version of the parameter, if some parameter is deprecated,
4494 * the new version should be set as NULL. */
4495 static struct cfg_interop_param mdt_interop_param[] = {
4496 { "mdt.group_upcall", NULL },
4497 { "mdt.quota_type", NULL },
4498 { "mdd.quota_type", NULL },
4499 { "mdt.rootsquash", "mdt.root_squash" },
4500 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4504 /* used by MGS to process specific configurations */
4505 static int mdt_process_config(const struct lu_env *env,
4506 struct lu_device *d, struct lustre_cfg *cfg)
4508 struct mdt_device *m = mdt_dev(d);
4509 struct md_device *md_next = m->mdt_child;
4510 struct lu_device *next = md2lu_dev(md_next);
4514 switch (cfg->lcfg_command) {
4516 struct lprocfs_static_vars lvars;
4517 struct obd_device *obd = d->ld_obd;
4519 /* For interoperability */
4520 struct cfg_interop_param *ptr = NULL;
4521 struct lustre_cfg *old_cfg = NULL;
4524 param = lustre_cfg_string(cfg, 1);
4525 if (param == NULL) {
4526 CERROR("param is empty\n");
4531 ptr = class_find_old_param(param, mdt_interop_param);
4533 if (ptr->new_param == NULL) {
4535 CWARN("For interoperability, skip this %s."
4536 " It is obsolete.\n", ptr->old_param);
4540 CWARN("Found old param %s, changed it to %s.\n",
4541 ptr->old_param, ptr->new_param);
4544 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4551 lprocfs_mdt_init_vars(&lvars);
4552 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4554 if (rc > 0 || rc == -ENOSYS) {
4555 /* is it an HSM var ? */
4556 rc = class_process_proc_param(PARAM_HSM,
4557 hsm_cdt_get_proc_vars(),
4559 if (rc > 0 || rc == -ENOSYS)
4560 /* we don't understand; pass it on */
4561 rc = next->ld_ops->ldo_process_config(env, next,
4565 if (old_cfg != NULL)
4566 lustre_cfg_free(cfg);
4571 /* others are passed further */
4572 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4578 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4579 const struct lu_object_header *hdr,
4580 struct lu_device *d)
4582 struct mdt_object *mo;
4586 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
4588 struct lu_object *o;
4589 struct lu_object_header *h;
4592 h = &mo->mot_header;
4593 lu_object_header_init(h);
4594 lu_object_init(o, h, d);
4595 lu_object_add_top(h, o);
4596 o->lo_ops = &mdt_obj_ops;
4597 mutex_init(&mo->mot_ioepoch_mutex);
4598 mutex_init(&mo->mot_lov_mutex);
4599 init_rwsem(&mo->mot_open_sem);
4605 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4606 const struct lu_object_conf *unused)
4608 struct mdt_device *d = mdt_dev(o->lo_dev);
4609 struct lu_device *under;
4610 struct lu_object *below;
4614 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4615 PFID(lu_object_fid(o)));
4617 under = &d->mdt_child->md_lu_dev;
4618 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4619 if (below != NULL) {
4620 lu_object_add(o, below);
4627 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4629 struct mdt_object *mo = mdt_obj(o);
4630 struct lu_object_header *h;
4634 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4635 PFID(lu_object_fid(o)));
4637 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4638 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4641 lu_object_header_fini(h);
4642 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4647 static int mdt_object_print(const struct lu_env *env, void *cookie,
4648 lu_printer_t p, const struct lu_object *o)
4650 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4651 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4652 "flags="LPX64", epochcount=%d, writecount=%d)",
4653 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4654 mdto->mot_ioepoch_count, mdto->mot_writecount);
4657 static int mdt_prepare(const struct lu_env *env,
4658 struct lu_device *pdev,
4659 struct lu_device *cdev)
4661 struct mdt_device *mdt = mdt_dev(cdev);
4662 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4663 struct obd_device *obd = cdev->ld_obd;
4664 struct lfsck_start_param lsp;
4671 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4675 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4679 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4683 lsp.lsp_start = NULL;
4684 lsp.lsp_namespace = mdt->mdt_namespace;
4685 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
4686 OBD_IOC_START_LFSCK,
4689 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
4690 mdt_obd_name(mdt), rc);
4694 if (mdt->mdt_seq_site.ss_node_id == 0) {
4695 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4696 &mdt->mdt_md_root_fid);
4701 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4702 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4703 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4704 LASSERT(obd->obd_no_conn);
4705 spin_lock(&obd->obd_dev_lock);
4706 obd->obd_no_conn = 0;
4707 spin_unlock(&obd->obd_dev_lock);
4709 if (obd->obd_recovering == 0)
4710 mdt_postrecov(env, mdt);
4715 const struct lu_device_operations mdt_lu_ops = {
4716 .ldo_object_alloc = mdt_object_alloc,
4717 .ldo_process_config = mdt_process_config,
4718 .ldo_prepare = mdt_prepare,
4721 static const struct lu_object_operations mdt_obj_ops = {
4722 .loo_object_init = mdt_object_init,
4723 .loo_object_free = mdt_object_free,
4724 .loo_object_print = mdt_object_print
4727 static int mdt_obd_set_info_async(const struct lu_env *env,
4728 struct obd_export *exp,
4729 __u32 keylen, void *key,
4730 __u32 vallen, void *val,
4731 struct ptlrpc_request_set *set)
4737 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4738 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4746 * Match client and server connection feature flags.
4748 * Compute the compatibility flags for a connection request based on
4749 * features mutually supported by client and server.
4751 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4752 * must not be updated here, otherwise a partially initialized value may
4753 * be exposed. After the connection request is successfully processed,
4754 * the top-level MDT connect request handler atomically updates the export
4755 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4756 * reply. \see mdt_connect().
4758 * \param exp the obd_export associated with this client/target pair
4759 * \param mdt the target device for the connection
4760 * \param data stores data for this connect request
4763 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4764 * \retval -EBADE client and server feature requirements are incompatible
4766 static int mdt_connect_internal(struct obd_export *exp,
4767 struct mdt_device *mdt,
4768 struct obd_connect_data *data)
4770 LASSERT(data != NULL);
4772 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4773 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4775 /* If no known bits (which should not happen, probably,
4776 as everybody should support LOOKUP and UPDATE bits at least)
4777 revert to compat mode with plain locks. */
4778 if (!data->ocd_ibits_known &&
4779 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4780 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4782 if (!mdt->mdt_opts.mo_acl)
4783 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4785 if (!mdt->mdt_opts.mo_user_xattr)
4786 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4788 if (!mdt->mdt_som_conf)
4789 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4791 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4792 data->ocd_brw_size = min(data->ocd_brw_size,
4793 (__u32)MD_MAX_BRW_SIZE);
4794 if (data->ocd_brw_size == 0) {
4795 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4796 " ocd_version: %x ocd_grant: %d "
4797 "ocd_index: %u ocd_brw_size is "
4798 "unexpectedly zero, network data "
4799 "corruption? Refusing connection of this"
4802 exp->exp_client_uuid.uuid,
4803 exp, data->ocd_connect_flags, data->ocd_version,
4804 data->ocd_grant, data->ocd_index);
4809 /* NB: Disregard the rule against updating
4810 * exp_connect_data.ocd_connect_flags in this case, since
4811 * tgt_client_new() needs to know if this is a lightweight
4812 * connection, and it is safe to expose this flag before
4813 * connection processing completes. */
4814 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4815 spin_lock(&exp->exp_lock);
4816 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4817 spin_unlock(&exp->exp_lock);
4820 data->ocd_version = LUSTRE_VERSION_CODE;
4822 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4823 CWARN("%s: MDS requires FID support, but client not\n",
4828 if (mdt->mdt_som_conf &&
4829 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4830 OBD_CONNECT_MDS_MDS |
4831 OBD_CONNECT_SOM))) {
4832 CWARN("%s: MDS has SOM enabled, but client does not support "
4833 "it\n", mdt_obd_name(mdt));
4837 if (OCD_HAS_FLAG(data, PINGLESS)) {
4838 if (ptlrpc_pinger_suppress_pings()) {
4839 spin_lock(&exp->exp_obd->obd_dev_lock);
4840 list_del_init(&exp->exp_obd_chain_timed);
4841 spin_unlock(&exp->exp_obd->obd_dev_lock);
4843 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4847 data->ocd_max_easize = mdt->mdt_max_ea_size;
4852 /* mds_connect copy */
4853 static int mdt_obd_connect(const struct lu_env *env,
4854 struct obd_export **exp, struct obd_device *obd,
4855 struct obd_uuid *cluuid,
4856 struct obd_connect_data *data,
4859 struct obd_export *lexp;
4860 struct lustre_handle conn = { 0 };
4861 struct mdt_device *mdt;
4865 LASSERT(env != NULL);
4866 if (!exp || !obd || !cluuid)
4869 mdt = mdt_dev(obd->obd_lu_dev);
4872 * first, check whether the stack is ready to handle requests
4873 * XXX: probably not very appropriate method is used now
4874 * at some point we should find a better one
4876 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
4877 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
4878 rc = obd_get_info(env, mdt->mdt_child_exp,
4879 sizeof(KEY_OSP_CONNECTED),
4880 KEY_OSP_CONNECTED, NULL, NULL, NULL);
4883 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
4886 rc = class_connect(&conn, obd, cluuid);
4890 lexp = class_conn2export(&conn);
4891 LASSERT(lexp != NULL);
4893 rc = mdt_connect_internal(lexp, mdt, data);
4895 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
4898 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
4899 rc = tgt_client_new(env, lexp);
4901 mdt_export_stats_init(obd, lexp, localdata);
4905 class_disconnect(lexp);
4914 static int mdt_obd_reconnect(const struct lu_env *env,
4915 struct obd_export *exp, struct obd_device *obd,
4916 struct obd_uuid *cluuid,
4917 struct obd_connect_data *data,
4923 if (exp == NULL || obd == NULL || cluuid == NULL)
4926 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4928 mdt_export_stats_init(obd, exp, localdata);
4933 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
4934 struct mdt_thread_info *info,
4935 struct mdt_file_data *mfd)
4937 struct lu_context ses;
4941 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
4946 lu_context_enter(&ses);
4948 mdt_ucred(info)->uc_valid = UCRED_OLD;
4949 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
4951 lu_context_exit(&ses);
4952 lu_context_fini(&ses);
4958 static int mdt_export_cleanup(struct obd_export *exp)
4960 struct mdt_export_data *med = &exp->exp_mdt_data;
4961 struct obd_device *obd = exp->exp_obd;
4962 struct mdt_device *mdt;
4963 struct mdt_thread_info *info;
4965 CFS_LIST_HEAD(closing_list);
4966 struct mdt_file_data *mfd, *n;
4970 spin_lock(&med->med_open_lock);
4971 while (!cfs_list_empty(&med->med_open_head)) {
4972 cfs_list_t *tmp = med->med_open_head.next;
4973 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
4975 /* Remove mfd handle so it can't be found again.
4976 * We are consuming the mfd_list reference here. */
4977 class_handle_unhash(&mfd->mfd_handle);
4978 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
4980 spin_unlock(&med->med_open_lock);
4981 mdt = mdt_dev(obd->obd_lu_dev);
4982 LASSERT(mdt != NULL);
4984 rc = lu_env_init(&env, LCT_MD_THREAD);
4988 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4989 LASSERT(info != NULL);
4990 memset(info, 0, sizeof *info);
4991 info->mti_env = &env;
4992 info->mti_mdt = mdt;
4993 info->mti_exp = exp;
4995 if (!cfs_list_empty(&closing_list)) {
4996 struct md_attr *ma = &info->mti_attr;
4998 /* Close any open files (which may also cause orphan unlinking). */
4999 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5000 cfs_list_del_init(&mfd->mfd_list);
5001 ma->ma_need = ma->ma_valid = 0;
5003 /* This file is being closed due to an eviction, it
5004 * could have been modified and now dirty regarding to
5005 * HSM archive, check this!
5006 * The logic here is to mark a file dirty if there's a
5007 * chance it was dirtied before the client was evicted,
5008 * so that we don't have to wait for a release attempt
5009 * before finding out the file was actually dirty and
5010 * fail the release. Aggressively marking it dirty here
5011 * will cause the policy engine to attempt to
5012 * re-archive it; when rearchiving, we can compare the
5013 * current version to the HSM data_version and make the
5014 * archive request into a noop if it's not actually
5017 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5018 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5020 /* Don't unlink orphan on failover umount, LU-184 */
5021 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5022 ma->ma_valid = MA_FLAGS;
5023 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5025 mdt_mfd_close(info, mfd);
5028 info->mti_mdt = NULL;
5029 /* cleanup client slot early */
5030 /* Do not erase record for recoverable client. */
5031 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5032 tgt_client_del(&env, exp);
5038 static int mdt_obd_disconnect(struct obd_export *exp)
5044 class_export_get(exp);
5046 rc = server_disconnect_export(exp);
5048 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5050 rc = mdt_export_cleanup(exp);
5051 class_export_put(exp);
5055 /* FIXME: Can we avoid using these two interfaces? */
5056 static int mdt_init_export(struct obd_export *exp)
5058 struct mdt_export_data *med = &exp->exp_mdt_data;
5062 CFS_INIT_LIST_HEAD(&med->med_open_head);
5063 spin_lock_init(&med->med_open_lock);
5064 mutex_init(&med->med_idmap_mutex);
5065 med->med_idmap = NULL;
5066 spin_lock(&exp->exp_lock);
5067 exp->exp_connecting = 1;
5068 spin_unlock(&exp->exp_lock);
5070 /* self-export doesn't need client data and ldlm initialization */
5071 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5072 &exp->exp_client_uuid)))
5075 rc = tgt_client_alloc(exp);
5079 rc = ldlm_init_export(exp);
5086 tgt_client_free(exp);
5088 CERROR("%s: Failed to initialize export: rc = %d\n",
5089 exp->exp_obd->obd_name, rc);
5093 static int mdt_destroy_export(struct obd_export *exp)
5097 if (exp_connect_rmtclient(exp))
5098 mdt_cleanup_idmap(&exp->exp_mdt_data);
5100 target_destroy_export(exp);
5101 /* destroy can be called from failed obd_setup, so
5102 * checking uuid is safer than obd_self_export */
5103 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5104 &exp->exp_client_uuid)))
5107 ldlm_destroy_export(exp);
5108 tgt_client_free(exp);
5110 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5111 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5116 /** The maximum depth that fid2path() will search.
5117 * This is limited only because we want to store the fids for
5118 * historical path lookup purposes.
5120 #define MAX_PATH_DEPTH 100
5122 /** mdt_path() lookup structure. */
5123 struct path_lookup_info {
5124 __u64 pli_recno; /**< history point */
5125 __u64 pli_currec; /**< current record */
5126 struct lu_fid pli_fid;
5127 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5128 struct mdt_object *pli_mdt_obj;
5129 char *pli_path; /**< full path */
5131 int pli_linkno; /**< which hardlink to follow */
5132 int pli_fidcount; /**< number of \a pli_fids */
5135 static int mdt_links_read(struct mdt_thread_info *info,
5136 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5140 LASSERT(ldata->ld_buf->lb_buf != NULL);
5142 if (!mdt_object_exists(mdt_obj))
5145 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5146 ldata->ld_buf, XATTR_NAME_LINK);
5147 if (rc == -ERANGE) {
5148 /* Buf was too small, figure out what we need. */
5149 lu_buf_free(ldata->ld_buf);
5150 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5151 ldata->ld_buf, XATTR_NAME_LINK);
5154 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5155 if (ldata->ld_buf->lb_buf == NULL)
5157 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5158 ldata->ld_buf, XATTR_NAME_LINK);
5163 return linkea_init(ldata);
5166 static int mdt_path_current(struct mdt_thread_info *info,
5167 struct path_lookup_info *pli)
5169 struct mdt_device *mdt = info->mti_mdt;
5170 struct mdt_object *mdt_obj;
5171 struct link_ea_header *leh;
5172 struct link_ea_entry *lee;
5173 struct lu_name *tmpname = &info->mti_name;
5174 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5175 struct lu_buf *buf = &info->mti_big_buf;
5178 struct linkea_data ldata = { 0 };
5182 /* temp buffer for path element, the buffer will be finally freed
5183 * in mdt_thread_info_fini */
5184 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5185 if (buf->lb_buf == NULL)
5189 ptr = pli->pli_path + pli->pli_pathlen - 1;
5192 pli->pli_fidcount = 0;
5193 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5195 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5196 * so checking root_fid can only happen on MDT0. */
5197 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5198 &pli->pli_fids[pli->pli_fidcount])) {
5199 mdt_obj = mdt_object_find(info->mti_env, mdt,
5200 &pli->pli_fids[pli->pli_fidcount]);
5201 if (IS_ERR(mdt_obj))
5202 GOTO(out, rc = PTR_ERR(mdt_obj));
5203 if (mdt_object_remote(mdt_obj)) {
5204 mdt_object_put(info->mti_env, mdt_obj);
5205 GOTO(remote_out, rc = -EREMOTE);
5207 if (!mdt_object_exists(mdt_obj)) {
5208 mdt_object_put(info->mti_env, mdt_obj);
5209 GOTO(out, rc = -ENOENT);
5212 rc = mdt_links_read(info, mdt_obj, &ldata);
5213 mdt_object_put(info->mti_env, mdt_obj);
5218 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5219 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5220 /* If set, use link #linkno for path lookup, otherwise use
5221 link #0. Only do this for the final path element. */
5222 if (pli->pli_fidcount == 0 &&
5223 pli->pli_linkno < leh->leh_reccount) {
5225 for (count = 0; count < pli->pli_linkno; count++) {
5226 lee = (struct link_ea_entry *)
5227 ((char *)lee + reclen);
5228 linkea_entry_unpack(lee, &reclen, tmpname,
5231 if (pli->pli_linkno < leh->leh_reccount - 1)
5232 /* indicate to user there are more links */
5236 /* Pack the name in the end of the buffer */
5237 ptr -= tmpname->ln_namelen;
5238 if (ptr - 1 <= pli->pli_path)
5239 GOTO(out, rc = -EOVERFLOW);
5240 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5243 /* Store the parent fid for historic lookup */
5244 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5245 GOTO(out, rc = -EOVERFLOW);
5246 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5250 ptr++; /* skip leading / */
5251 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5258 /* Returns the full path to this fid, as of changelog record recno. */
5259 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5260 char *path, int pathlen, __u64 *recno, int *linkno,
5263 struct mdt_device *mdt = info->mti_mdt;
5264 struct path_lookup_info *pli;
5272 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5281 pli->pli_mdt_obj = obj;
5282 pli->pli_recno = *recno;
5283 pli->pli_path = path;
5284 pli->pli_pathlen = pathlen;
5285 pli->pli_linkno = *linkno;
5287 /* Retry multiple times in case file is being moved */
5288 while (tries-- && rc == -EAGAIN)
5289 rc = mdt_path_current(info, pli);
5291 /* return the last resolved fids to the client, so the client will
5292 * build the left path on another MDT for remote object */
5293 *fid = pli->pli_fids[pli->pli_fidcount];
5295 *recno = pli->pli_currec;
5296 /* Return next link index to caller */
5297 *linkno = pli->pli_linkno;
5304 static int mdt_fid2path(struct mdt_thread_info *info,
5305 struct getinfo_fid2path *fp)
5307 struct mdt_device *mdt = info->mti_mdt;
5308 struct mdt_object *obj;
5312 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5313 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5315 if (!fid_is_sane(&fp->gf_fid))
5318 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5319 CWARN("%s: "DFID" is invalid, sequence should be "
5320 ">= "LPX64"\n", mdt_obd_name(mdt),
5321 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5325 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5326 if (obj == NULL || IS_ERR(obj)) {
5327 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5332 if (mdt_object_remote(obj))
5334 else if (!mdt_object_exists(obj))
5340 mdt_object_put(info->mti_env, obj);
5341 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5342 PFID(&fp->gf_fid), rc);
5346 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5347 &fp->gf_linkno, &fp->gf_fid);
5349 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5350 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5352 mdt_object_put(info->mti_env, obj);
5357 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5358 void *val, int vallen)
5360 struct getinfo_fid2path *fpout, *fpin;
5363 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5366 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5367 lustre_swab_fid2path(fpin);
5369 memcpy(fpout, fpin, sizeof(*fpin));
5370 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5373 rc = mdt_fid2path(info, fpout);
5377 int mdt_get_info(struct tgt_session_info *tsi)
5387 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5389 CDEBUG(D_IOCTL, "No GETINFO key");
5390 RETURN(err_serious(-EFAULT));
5392 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5395 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5396 if (vallen == NULL) {
5397 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5398 RETURN(err_serious(-EFAULT));
5401 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5403 rc = req_capsule_server_pack(tsi->tsi_pill);
5405 RETURN(err_serious(rc));
5407 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5408 if (valout == NULL) {
5409 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5410 RETURN(err_serious(-EFAULT));
5413 if (KEY_IS(KEY_FID2PATH)) {
5414 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5416 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5417 mdt_thread_info_fini(info);
5424 /* Pass the ioc down */
5425 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5426 unsigned int cmd, int len, void *data)
5428 struct lu_context ioctl_session;
5429 struct md_device *next = mdt->mdt_child;
5433 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5436 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5437 lu_context_enter(&ioctl_session);
5438 env->le_ses = &ioctl_session;
5440 LASSERT(next->md_ops->mdo_iocontrol);
5441 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5443 lu_context_exit(&ioctl_session);
5444 lu_context_fini(&ioctl_session);
5448 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5450 struct obd_ioctl_data *data = karg;
5453 struct mdt_object *obj;
5454 struct mdt_lock_handle *lh;
5458 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5459 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5462 fid = (struct lu_fid *)data->ioc_inlbuf1;
5464 if (!fid_is_sane(fid))
5467 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5469 lh = &mti->mti_lh[MDT_LH_PARENT];
5470 mdt_lock_reg_init(lh, LCK_CR);
5472 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5474 RETURN(PTR_ERR(obj));
5476 if (mdt_object_remote(obj)) {
5479 * before calling version get the correct MDS should be
5480 * fid, this is error to find remote object here
5482 CERROR("nonlocal object "DFID"\n", PFID(fid));
5483 } else if (!mdt_object_exists(obj)) {
5484 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5487 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5488 *(__u64 *)data->ioc_inlbuf2 = version;
5491 mdt_object_unlock_put(mti, obj, lh, 1);
5495 /* ioctls on obd dev */
5496 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5497 void *karg, void *uarg)
5500 struct obd_device *obd = exp->exp_obd;
5501 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5502 struct dt_device *dt = mdt->mdt_bottom;
5506 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5507 rc = lu_env_init(&env, LCT_MD_THREAD);
5513 rc = mdt_device_sync(&env, mdt);
5515 case OBD_IOC_SET_READONLY:
5516 rc = dt->dd_ops->dt_ro(&env, dt);
5518 case OBD_IOC_ABORT_RECOVERY:
5519 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5520 target_stop_recovery_thread(obd);
5523 case OBD_IOC_CHANGELOG_REG:
5524 case OBD_IOC_CHANGELOG_DEREG:
5525 case OBD_IOC_CHANGELOG_CLEAR:
5526 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5528 case OBD_IOC_START_LFSCK: {
5529 struct md_device *next = mdt->mdt_child;
5530 struct obd_ioctl_data *data = karg;
5531 struct lfsck_start_param lsp;
5533 if (unlikely(data == NULL)) {
5538 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5539 lsp.lsp_namespace = mdt->mdt_namespace;
5540 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5543 case OBD_IOC_STOP_LFSCK: {
5544 struct md_device *next = mdt->mdt_child;
5546 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
5549 case OBD_IOC_GET_OBJ_VERSION: {
5550 struct mdt_thread_info *mti;
5551 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5552 memset(mti, 0, sizeof *mti);
5553 mti->mti_env = &env;
5557 rc = mdt_ioc_version_get(mti, karg);
5560 case OBD_IOC_CATLOGLIST:
5561 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg);
5565 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5566 mdt_obd_name(mdt), cmd, rc);
5573 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5575 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5579 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5583 int mdt_obd_postrecov(struct obd_device *obd)
5588 rc = lu_env_init(&env, LCT_MD_THREAD);
5591 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5596 static struct obd_ops mdt_obd_device_ops = {
5597 .o_owner = THIS_MODULE,
5598 .o_set_info_async = mdt_obd_set_info_async,
5599 .o_connect = mdt_obd_connect,
5600 .o_reconnect = mdt_obd_reconnect,
5601 .o_disconnect = mdt_obd_disconnect,
5602 .o_init_export = mdt_init_export,
5603 .o_destroy_export = mdt_destroy_export,
5604 .o_iocontrol = mdt_iocontrol,
5605 .o_postrecov = mdt_obd_postrecov,
5608 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5609 struct lu_device *d)
5611 struct mdt_device *m = mdt_dev(d);
5618 static struct lu_device *mdt_device_free(const struct lu_env *env,
5619 struct lu_device *d)
5621 struct mdt_device *m = mdt_dev(d);
5624 lu_device_fini(&m->mdt_lu_dev);
5630 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5631 struct lu_device_type *t,
5632 struct lustre_cfg *cfg)
5634 struct lu_device *l;
5635 struct mdt_device *m;
5642 rc = mdt_init0(env, m, t, cfg);
5644 mdt_device_free(env, l);
5649 l = ERR_PTR(-ENOMEM);
5653 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5654 LU_KEY_INIT(mdt, struct mdt_thread_info);
5656 static void mdt_key_fini(const struct lu_context *ctx,
5657 struct lu_context_key *key, void* data)
5659 struct mdt_thread_info *info = data;
5661 if (info->mti_big_lmm) {
5662 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5663 info->mti_big_lmm = NULL;
5664 info->mti_big_lmmsize = 0;
5669 /* context key: mdt_thread_key */
5670 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5672 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5674 return lu_ucred(info->mti_env);
5677 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5679 return lu_ucred_check(info->mti_env);
5683 * Enable/disable COS (Commit On Sharing).
5685 * Set/Clear the COS flag in mdt options.
5687 * \param mdt mdt device
5688 * \param val 0 disables COS, other values enable COS
5690 void mdt_enable_cos(struct mdt_device *mdt, int val)
5695 mdt->mdt_opts.mo_cos = !!val;
5696 rc = lu_env_init(&env, LCT_LOCAL);
5697 if (unlikely(rc != 0)) {
5698 CWARN("lu_env initialization failed with rc = %d,"
5699 "cannot sync\n", rc);
5702 mdt_device_sync(&env, mdt);
5707 * Check COS (Commit On Sharing) status.
5709 * Return COS flag status.
5711 * \param mdt mdt device
5713 int mdt_cos_is_enabled(struct mdt_device *mdt)
5715 return mdt->mdt_opts.mo_cos != 0;
5718 static struct lu_device_type_operations mdt_device_type_ops = {
5719 .ldto_device_alloc = mdt_device_alloc,
5720 .ldto_device_free = mdt_device_free,
5721 .ldto_device_fini = mdt_device_fini
5724 static struct lu_device_type mdt_device_type = {
5725 .ldt_tags = LU_DEVICE_MD,
5726 .ldt_name = LUSTRE_MDT_NAME,
5727 .ldt_ops = &mdt_device_type_ops,
5728 .ldt_ctx_tags = LCT_MD_THREAD
5731 static int __init mdt_mod_init(void)
5733 struct lprocfs_static_vars lvars;
5736 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5737 FID_NOBRACE_LEN + 1);
5738 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5740 rc = lu_kmem_init(mdt_caches);
5744 rc = mds_mod_init();
5748 lprocfs_mdt_init_vars(&lvars);
5749 rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
5750 #ifndef HAVE_ONLY_PROCFS_SEQ
5753 LUSTRE_MDT_NAME, &mdt_device_type);
5758 lu_kmem_fini(mdt_caches);
5765 static void __exit mdt_mod_exit(void)
5767 class_unregister_type(LUSTRE_MDT_NAME);
5769 lu_kmem_fini(mdt_caches);
5772 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5773 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5774 MODULE_LICENSE("GPL");
5776 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);