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;
143 rep->lock_policy_res1 &= ~flag;
146 void mdt_set_disposition(struct mdt_thread_info *info,
147 struct ldlm_reply *rep, int flag)
150 info->mti_opdata |= flag;
152 rep->lock_policy_res1 |= flag;
155 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
157 lh->mlh_pdo_hash = 0;
158 lh->mlh_reg_mode = lm;
159 lh->mlh_rreg_mode = lm;
160 lh->mlh_type = MDT_REG_LOCK;
163 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
164 const char *name, int namelen)
166 lh->mlh_reg_mode = lm;
167 lh->mlh_rreg_mode = lm;
168 lh->mlh_type = MDT_PDO_LOCK;
170 if (name != NULL && (name[0] != '\0')) {
171 LASSERT(namelen > 0);
172 lh->mlh_pdo_hash = full_name_hash(name, namelen);
173 /* XXX Workaround for LU-2856
174 * Zero is a valid return value of full_name_hash, but several
175 * users of mlh_pdo_hash assume a non-zero hash value. We
176 * therefore map zero onto an arbitrary, but consistent
177 * value (1) to avoid problems further down the road. */
178 if (unlikely(!lh->mlh_pdo_hash))
179 lh->mlh_pdo_hash = 1;
181 LASSERT(namelen == 0);
182 lh->mlh_pdo_hash = 0ull;
186 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
187 struct mdt_lock_handle *lh)
193 * Any dir access needs couple of locks:
195 * 1) on part of dir we gonna take lookup/modify;
197 * 2) on whole dir to protect it from concurrent splitting and/or to
198 * flush client's cache for readdir().
200 * so, for a given mode and object this routine decides what lock mode
201 * to use for lock #2:
203 * 1) if caller's gonna lookup in dir then we need to protect dir from
204 * being splitted only - LCK_CR
206 * 2) if caller's gonna modify dir then we need to protect dir from
207 * being splitted and to flush cache - LCK_CW
209 * 3) if caller's gonna modify dir and that dir seems ready for
210 * splitting then we need to protect it from any type of access
211 * (lookup/modify/split) - LCK_EX --bzzz
214 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
215 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
218 * Ask underlaying level its opinion about preferable PDO lock mode
219 * having access type passed as regular lock mode:
221 * - MDL_MINMODE means that lower layer does not want to specify lock
224 * - MDL_NL means that no PDO lock should be taken. This is used in some
225 * cases. Say, for non-splittable directories no need to use PDO locks
228 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
229 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
231 if (mode != MDL_MINMODE) {
232 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
235 * Lower layer does not want to specify locking mode. We do it
236 * our selves. No special protection is needed, just flush
237 * client's cache on modification and allow concurrent
240 switch (lh->mlh_reg_mode) {
242 lh->mlh_pdo_mode = LCK_EX;
245 lh->mlh_pdo_mode = LCK_CR;
248 lh->mlh_pdo_mode = LCK_CW;
251 CERROR("Not expected lock type (0x%x)\n",
252 (int)lh->mlh_reg_mode);
257 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
261 int mdt_getstatus(struct mdt_thread_info *info)
263 struct mdt_device *mdt = info->mti_mdt;
264 struct mdt_body *repbody;
268 rc = mdt_check_ucred(info);
270 RETURN(err_serious(rc));
272 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
273 RETURN(err_serious(-ENOMEM));
275 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
276 repbody->fid1 = mdt->mdt_md_root_fid;
277 repbody->valid |= OBD_MD_FLID;
279 if (mdt->mdt_opts.mo_mds_capa &&
280 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
281 struct mdt_object *root;
282 struct lustre_capa *capa;
284 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
286 RETURN(PTR_ERR(root));
288 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
290 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
291 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
293 mdt_object_put(info->mti_env, root);
295 repbody->valid |= OBD_MD_FLMDSCAPA;
301 int mdt_statfs(struct mdt_thread_info *info)
303 struct ptlrpc_request *req = mdt_info_req(info);
304 struct md_device *next = info->mti_mdt->mdt_child;
305 struct ptlrpc_service_part *svcpt;
306 struct obd_statfs *osfs;
311 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
313 /* This will trigger a watchdog timeout */
314 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
315 (MDT_SERVICE_WATCHDOG_FACTOR *
316 at_get(&svcpt->scp_at_estimate)) + 1);
318 rc = mdt_check_ucred(info);
320 RETURN(err_serious(rc));
322 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
323 RETURN(err_serious(-ENOMEM));
325 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
329 /** statfs information are cached in the mdt_device */
330 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
331 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
332 /** statfs data is too old, get up-to-date one */
333 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
336 spin_lock(&info->mti_mdt->mdt_osfs_lock);
337 info->mti_mdt->mdt_osfs = *osfs;
338 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
339 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
341 /** use cached statfs data */
342 spin_lock(&info->mti_mdt->mdt_osfs_lock);
343 *osfs = info->mti_mdt->mdt_osfs;
344 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
348 mdt_counter_incr(req, LPROC_MDT_STATFS);
354 * Pack SOM attributes into the reply.
355 * Call under a DLM UPDATE lock.
357 static void mdt_pack_size2body(struct mdt_thread_info *info,
358 struct mdt_object *mo)
361 struct md_attr *ma = &info->mti_attr;
363 LASSERT(ma->ma_attr.la_valid & LA_MODE);
364 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
366 /* Check if Size-on-MDS is supported, if this is a regular file,
367 * if SOM is enabled on the object and if SOM cache exists and valid.
368 * Otherwise do not pack Size-on-MDS attributes to the reply. */
369 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
370 !S_ISREG(ma->ma_attr.la_mode) ||
371 !mdt_object_is_som_enabled(mo) ||
372 !(ma->ma_valid & MA_SOM))
375 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
376 b->size = ma->ma_som->msd_size;
377 b->blocks = ma->ma_som->msd_blocks;
380 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
381 const struct lu_attr *attr, const struct lu_fid *fid)
383 struct md_attr *ma = &info->mti_attr;
385 LASSERT(ma->ma_valid & MA_INODE);
387 b->atime = attr->la_atime;
388 b->mtime = attr->la_mtime;
389 b->ctime = attr->la_ctime;
390 b->mode = attr->la_mode;
391 b->size = attr->la_size;
392 b->blocks = attr->la_blocks;
393 b->uid = attr->la_uid;
394 b->gid = attr->la_gid;
395 b->flags = attr->la_flags;
396 b->nlink = attr->la_nlink;
397 b->rdev = attr->la_rdev;
399 /*XXX should pack the reply body according to lu_valid*/
400 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
401 OBD_MD_FLGID | OBD_MD_FLTYPE |
402 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
403 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
405 if (!S_ISREG(attr->la_mode)) {
406 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
407 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
408 /* means no objects are allocated on osts. */
409 LASSERT(!(ma->ma_valid & MA_LOV));
410 /* just ignore blocks occupied by extend attributes on MDS */
412 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
413 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
414 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
415 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
416 /* A released file stores its size on MDS. */
417 /* But return 1 block for released file, unless tools like tar
418 * will consider it fully sparse. (LU-3864)
420 if (unlikely(b->size == 0))
424 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
429 b->valid |= OBD_MD_FLID;
430 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
431 PFID(fid), b->nlink, b->mode, b->size);
435 mdt_body_reverse_idmap(info, b);
437 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
438 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
439 PFID(fid), (unsigned long long)b->size);
442 static inline int mdt_body_has_lov(const struct lu_attr *la,
443 const struct mdt_body *body)
445 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
446 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
449 void mdt_client_compatibility(struct mdt_thread_info *info)
451 struct mdt_body *body;
452 struct ptlrpc_request *req = mdt_info_req(info);
453 struct obd_export *exp = req->rq_export;
454 struct md_attr *ma = &info->mti_attr;
455 struct lu_attr *la = &ma->ma_attr;
458 if (exp_connect_layout(exp))
459 /* the client can deal with 16-bit lmm_stripe_count */
462 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
464 if (!mdt_body_has_lov(la, body))
467 /* now we have a reply with a lov for a client not compatible with the
468 * layout lock so we have to clean the layout generation number */
469 if (S_ISREG(la->la_mode))
470 ma->ma_lmm->lmm_layout_gen = 0;
474 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
476 const struct lu_env *env = info->mti_env;
479 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
488 /* Is it a directory? Let's check for the LMV as well */
489 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
490 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
492 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
500 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
503 const struct lu_env *env = info->mti_env;
507 LASSERT(info->mti_big_lmm_used == 0);
508 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
512 /* big_lmm may need to be grown */
513 if (info->mti_big_lmmsize < rc) {
514 int size = size_roundup_power2(rc);
516 if (info->mti_big_lmmsize > 0) {
517 /* free old buffer */
518 LASSERT(info->mti_big_lmm);
519 OBD_FREE_LARGE(info->mti_big_lmm,
520 info->mti_big_lmmsize);
521 info->mti_big_lmm = NULL;
522 info->mti_big_lmmsize = 0;
525 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
526 if (info->mti_big_lmm == NULL)
528 info->mti_big_lmmsize = size;
530 LASSERT(info->mti_big_lmmsize >= rc);
532 info->mti_buf.lb_buf = info->mti_big_lmm;
533 info->mti_buf.lb_len = info->mti_big_lmmsize;
534 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
539 int mdt_attr_get_lov(struct mdt_thread_info *info,
540 struct mdt_object *o, struct md_attr *ma)
542 struct md_object *next = mdt_object_child(o);
543 struct lu_buf *buf = &info->mti_buf;
546 buf->lb_buf = ma->ma_lmm;
547 buf->lb_len = ma->ma_lmm_size;
548 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
550 ma->ma_lmm_size = rc;
551 ma->ma_valid |= MA_LOV;
553 } else if (rc == -ENODATA) {
556 } else if (rc == -ERANGE) {
557 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
559 info->mti_big_lmm_used = 1;
560 ma->ma_valid |= MA_LOV;
561 ma->ma_lmm = info->mti_big_lmm;
562 ma->ma_lmm_size = rc;
563 /* update mdt_max_mdsize so all clients
564 * will be aware about that */
565 if (info->mti_mdt->mdt_max_mdsize < rc)
566 info->mti_mdt->mdt_max_mdsize = rc;
574 int mdt_attr_get_pfid(struct mdt_thread_info *info,
575 struct mdt_object *o, struct lu_fid *pfid)
577 struct lu_buf *buf = &info->mti_buf;
578 struct link_ea_header *leh;
579 struct link_ea_entry *lee;
583 buf->lb_buf = info->mti_big_lmm;
584 buf->lb_len = info->mti_big_lmmsize;
585 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
586 buf, XATTR_NAME_LINK);
587 /* ignore errors, MA_PFID won't be set and it is
588 * up to the caller to treat this as an error */
589 if (rc == -ERANGE || buf->lb_len == 0) {
590 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
591 buf->lb_buf = info->mti_big_lmm;
592 buf->lb_len = info->mti_big_lmmsize;
597 if (rc < sizeof(*leh)) {
598 CERROR("short LinkEA on "DFID": rc = %d\n",
599 PFID(mdt_object_fid(o)), rc);
603 leh = (struct link_ea_header *) buf->lb_buf;
604 lee = (struct link_ea_entry *)(leh + 1);
605 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
606 leh->leh_magic = LINK_EA_MAGIC;
607 leh->leh_reccount = __swab32(leh->leh_reccount);
608 leh->leh_len = __swab64(leh->leh_len);
610 if (leh->leh_magic != LINK_EA_MAGIC)
612 if (leh->leh_reccount == 0)
615 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
616 fid_be_to_cpu(pfid, pfid);
621 int mdt_attr_get_complex(struct mdt_thread_info *info,
622 struct mdt_object *o, struct md_attr *ma)
624 const struct lu_env *env = info->mti_env;
625 struct md_object *next = mdt_object_child(o);
626 struct lu_buf *buf = &info->mti_buf;
627 u32 mode = lu_object_attr(&next->mo_lu);
628 int need = ma->ma_need;
634 if (need & MA_INODE) {
635 ma->ma_need = MA_INODE;
636 rc = mo_attr_get(env, next, ma);
639 ma->ma_valid |= MA_INODE;
642 if (need & MA_PFID) {
643 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
645 ma->ma_valid |= MA_PFID;
646 /* ignore this error, parent fid is not mandatory */
650 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
651 rc = mdt_attr_get_lov(info, o, ma);
656 if (need & MA_LMV && S_ISDIR(mode)) {
657 buf->lb_buf = ma->ma_lmv;
658 buf->lb_len = ma->ma_lmv_size;
659 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
661 ma->ma_lmv_size = rc2;
662 ma->ma_valid |= MA_LMV;
663 } else if (rc2 == -ENODATA) {
670 if (need & MA_SOM && S_ISREG(mode)) {
671 buf->lb_buf = info->mti_xattr_buf;
672 buf->lb_len = sizeof(info->mti_xattr_buf);
673 CLASSERT(sizeof(struct som_attrs) <=
674 sizeof(info->mti_xattr_buf));
675 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
676 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
678 ma->ma_valid |= MA_SOM;
679 else if (rc2 < 0 && rc2 != -ENODATA)
683 if (need & MA_HSM && S_ISREG(mode)) {
684 buf->lb_buf = info->mti_xattr_buf;
685 buf->lb_len = sizeof(info->mti_xattr_buf);
686 CLASSERT(sizeof(struct hsm_attrs) <=
687 sizeof(info->mti_xattr_buf));
688 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
689 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
691 ma->ma_valid |= MA_HSM;
692 else if (rc2 < 0 && rc2 != -ENODATA)
696 #ifdef CONFIG_FS_POSIX_ACL
697 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
698 buf->lb_buf = ma->ma_acl;
699 buf->lb_len = ma->ma_acl_size;
700 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
702 ma->ma_acl_size = rc2;
703 ma->ma_valid |= MA_ACL_DEF;
704 } else if (rc2 == -ENODATA) {
713 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
714 rc, ma->ma_valid, ma->ma_lmm);
718 static int mdt_getattr_internal(struct mdt_thread_info *info,
719 struct mdt_object *o, int ma_need)
721 struct md_object *next = mdt_object_child(o);
722 const struct mdt_body *reqbody = info->mti_body;
723 struct ptlrpc_request *req = mdt_info_req(info);
724 struct md_attr *ma = &info->mti_attr;
725 struct lu_attr *la = &ma->ma_attr;
726 struct req_capsule *pill = info->mti_pill;
727 const struct lu_env *env = info->mti_env;
728 struct mdt_body *repbody;
729 struct lu_buf *buffer = &info->mti_buf;
734 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
735 RETURN(err_serious(-ENOMEM));
737 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
741 if (mdt_object_remote(o)) {
742 /* This object is located on remote node.*/
743 /* Return -EIO for old client */
744 if (!mdt_is_dne_client(req->rq_export))
745 GOTO(out, rc = -EIO);
747 repbody->fid1 = *mdt_object_fid(o);
748 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
752 if (reqbody->eadatasize > 0) {
753 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
754 if (buffer->lb_buf == NULL)
755 GOTO(out, rc = -EPROTO);
756 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
759 buffer->lb_buf = NULL;
761 ma_need &= ~(MA_LOV | MA_LMV);
762 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
763 mdt_obd_name(info->mti_mdt),
764 req->rq_export->exp_client_uuid.uuid);
767 /* If it is dir object and client require MEA, then we got MEA */
768 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
769 reqbody->valid & OBD_MD_MEA) {
770 /* Assumption: MDT_MD size is enough for lmv size. */
771 ma->ma_lmv = buffer->lb_buf;
772 ma->ma_lmv_size = buffer->lb_len;
773 ma->ma_need = MA_INODE;
774 if (ma->ma_lmm_size > 0)
775 ma->ma_need |= MA_LMV;
777 ma->ma_lmm = buffer->lb_buf;
778 ma->ma_lmm_size = buffer->lb_len;
779 ma->ma_need = MA_INODE | MA_HSM;
780 if (ma->ma_lmm_size > 0)
781 ma->ma_need |= MA_LOV;
784 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
785 reqbody->valid & OBD_MD_FLDIREA &&
786 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
787 /* get default stripe info for this dir. */
788 ma->ma_need |= MA_LOV_DEF;
790 ma->ma_need |= ma_need;
791 if (ma->ma_need & MA_SOM)
792 ma->ma_som = &info->mti_u.som.data;
794 rc = mdt_attr_get_complex(info, o, ma);
796 CERROR("%s: getattr error for "DFID": rc = %d\n",
797 mdt_obd_name(info->mti_mdt),
798 PFID(mdt_object_fid(o)), rc);
802 /* if file is released, check if a restore is running */
803 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
804 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
805 repbody->t_state = MS_RESTORE;
806 repbody->valid |= OBD_MD_TSTATE;
809 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
811 /* the Lustre protocol supposes to return default striping
812 * on the user-visible root if explicitly requested */
813 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
814 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
815 struct lu_fid rootfid;
816 struct mdt_object *root;
817 struct mdt_device *mdt = info->mti_mdt;
819 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
822 root = mdt_object_find(env, mdt, &rootfid);
824 RETURN(PTR_ERR(root));
825 rc = mdt_attr_get_lov(info, root, ma);
826 mdt_object_put(info->mti_env, root);
828 CERROR("%s: getattr error for "DFID": rc = %d\n",
829 mdt_obd_name(info->mti_mdt),
830 PFID(mdt_object_fid(o)), rc);
835 if (likely(ma->ma_valid & MA_INODE))
836 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
840 if (mdt_body_has_lov(la, reqbody)) {
841 if (ma->ma_valid & MA_LOV) {
842 LASSERT(ma->ma_lmm_size);
843 repbody->eadatasize = ma->ma_lmm_size;
844 if (S_ISDIR(la->la_mode))
845 repbody->valid |= OBD_MD_FLDIREA;
847 repbody->valid |= OBD_MD_FLEASIZE;
848 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
850 if (ma->ma_valid & MA_LMV) {
851 LASSERT(S_ISDIR(la->la_mode));
852 repbody->eadatasize = ma->ma_lmv_size;
853 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
855 } else if (S_ISLNK(la->la_mode) &&
856 reqbody->valid & OBD_MD_LINKNAME) {
857 buffer->lb_buf = ma->ma_lmm;
858 /* eadatasize from client includes NULL-terminator, so
859 * there is no need to read it */
860 buffer->lb_len = reqbody->eadatasize - 1;
861 rc = mo_readlink(env, next, buffer);
862 if (unlikely(rc <= 0)) {
863 CERROR("%s: readlink failed for "DFID": rc = %d\n",
864 mdt_obd_name(info->mti_mdt),
865 PFID(mdt_object_fid(o)), rc);
868 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
870 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
872 repbody->valid |= OBD_MD_LINKNAME;
873 /* we need to report back size with NULL-terminator
874 * because client expects that */
875 repbody->eadatasize = rc + 1;
876 if (repbody->eadatasize != reqbody->eadatasize)
877 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
878 "on "DFID ", expected %d\n",
879 mdt_obd_name(info->mti_mdt),
880 rc, PFID(mdt_object_fid(o)),
881 reqbody->eadatasize - 1);
883 ((char *)ma->ma_lmm)[rc] = 0;
885 /* If the total CDEBUG() size is larger than a page, it
886 * will print a warning to the console, avoid this by
887 * printing just the last part of the symlink. */
888 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
889 print_limit < rc ? "..." : "", print_limit,
890 (char *)ma->ma_lmm + rc - print_limit, rc);
895 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
896 repbody->max_cookiesize = 0;
897 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
898 repbody->valid |= OBD_MD_FLMODEASIZE;
899 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
900 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
901 repbody->max_cookiesize);
904 if (exp_connect_rmtclient(info->mti_exp) &&
905 reqbody->valid & OBD_MD_FLRMTPERM) {
906 void *buf = req_capsule_server_get(pill, &RMF_ACL);
908 /* mdt_getattr_lock only */
909 rc = mdt_pack_remote_perm(info, o, buf);
911 repbody->valid &= ~OBD_MD_FLRMTPERM;
912 repbody->aclsize = 0;
915 repbody->valid |= OBD_MD_FLRMTPERM;
916 repbody->aclsize = sizeof(struct mdt_remote_perm);
919 #ifdef CONFIG_FS_POSIX_ACL
920 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
921 (reqbody->valid & OBD_MD_FLACL)) {
922 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
923 buffer->lb_len = req_capsule_get_size(pill,
924 &RMF_ACL, RCL_SERVER);
925 if (buffer->lb_len > 0) {
926 rc = mo_xattr_get(env, next, buffer,
927 XATTR_NAME_ACL_ACCESS);
929 if (rc == -ENODATA) {
930 repbody->aclsize = 0;
931 repbody->valid |= OBD_MD_FLACL;
933 } else if (rc == -EOPNOTSUPP) {
936 CERROR("%s: unable to read "DFID
938 mdt_obd_name(info->mti_mdt),
939 PFID(mdt_object_fid(o)), rc);
942 repbody->aclsize = rc;
943 repbody->valid |= OBD_MD_FLACL;
950 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
951 info->mti_mdt->mdt_opts.mo_mds_capa &&
952 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
953 struct lustre_capa *capa;
955 capa = req_capsule_server_get(pill, &RMF_CAPA1);
957 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
958 rc = mo_capa_get(env, next, capa, 0);
961 repbody->valid |= OBD_MD_FLMDSCAPA;
966 mdt_counter_incr(req, LPROC_MDT_GETATTR);
971 static int mdt_renew_capa(struct mdt_thread_info *info)
973 struct mdt_object *obj = info->mti_object;
974 struct mdt_body *body;
975 struct lustre_capa *capa, *c;
979 /* if object doesn't exist, or server has disabled capability,
980 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
983 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
984 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
987 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
988 LASSERT(body != NULL);
990 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
993 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
997 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
999 body->valid |= OBD_MD_FLOSSCAPA;
1003 int mdt_getattr(struct mdt_thread_info *info)
1005 struct mdt_object *obj = info->mti_object;
1006 struct req_capsule *pill = info->mti_pill;
1007 struct mdt_body *reqbody;
1008 struct mdt_body *repbody;
1013 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1016 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1017 rc = req_capsule_server_pack(pill);
1019 RETURN(err_serious(rc));
1020 rc = mdt_renew_capa(info);
1021 GOTO(out_shrink, rc);
1024 LASSERT(obj != NULL);
1025 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1027 mode = lu_object_attr(&obj->mot_obj);
1030 if (reqbody->valid & OBD_MD_LINKNAME) {
1031 /* No easy way to know how long is the symlink, but it cannot
1032 * be more than PATH_MAX, so we allocate +1 */
1035 /* A special case for fs ROOT: getattr there might fetch
1036 * default EA for entire fs, not just for this dir!
1038 } else if (lu_fid_eq(mdt_object_fid(obj),
1039 &info->mti_mdt->mdt_md_root_fid) &&
1040 (reqbody->valid & OBD_MD_FLDIREA) &&
1041 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1043 /* Should the default strping be bigger, mdt_fix_reply
1044 * will reallocate */
1045 rc = DEF_REP_MD_SIZE;
1047 /* Hopefully no race in EA change for either file or directory?
1049 rc = mdt_attr_get_eabuf_size(info, obj);
1053 GOTO(out_shrink, rc);
1055 /* old clients may not report needed easize, use max value then */
1056 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1058 rc = req_capsule_server_pack(pill);
1059 if (unlikely(rc != 0))
1060 RETURN(err_serious(rc));
1062 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1063 LASSERT(repbody != NULL);
1064 repbody->eadatasize = 0;
1065 repbody->aclsize = 0;
1067 if (reqbody->valid & OBD_MD_FLRMTPERM)
1068 rc = mdt_init_ucred(info, reqbody);
1070 rc = mdt_check_ucred(info);
1072 GOTO(out_shrink, rc);
1074 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1077 * Don't check capability at all, because rename might getattr for
1078 * remote obj, and at that time no capability is available.
1080 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1081 rc = mdt_getattr_internal(info, obj, 0);
1082 if (reqbody->valid & OBD_MD_FLRMTPERM)
1083 mdt_exit_ucred(info);
1086 mdt_client_compatibility(info);
1087 rc2 = mdt_fix_reply(info);
1093 int mdt_is_subdir(struct mdt_thread_info *info)
1095 struct mdt_object *o = info->mti_object;
1096 struct req_capsule *pill = info->mti_pill;
1097 const struct mdt_body *body = info->mti_body;
1098 struct mdt_body *repbody;
1104 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1107 * We save last checked parent fid to @repbody->fid1 for remote
1110 LASSERT(fid_is_sane(&body->fid2));
1111 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1112 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1113 &body->fid2, &repbody->fid1);
1114 if (rc == 0 || rc == -EREMOTE)
1115 repbody->valid |= OBD_MD_FLID;
1120 int mdt_swap_layouts(struct mdt_thread_info *info)
1122 struct ptlrpc_request *req = mdt_info_req(info);
1123 struct obd_export *exp = req->rq_export;
1124 struct mdt_object *o1, *o2, *o;
1125 struct mdt_lock_handle *lh1, *lh2;
1126 struct mdc_swap_layouts *msl;
1130 /* client does not support layout lock, so layout swaping
1132 * FIXME: there is a problem for old clients which don't support
1133 * layout lock yet. If those clients have already opened the file
1134 * they won't be notified at all so that old layout may still be
1135 * used to do IO. This can be fixed after file release is landed by
1136 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1137 if (!exp_connect_layout(exp))
1138 RETURN(-EOPNOTSUPP);
1140 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1141 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1142 req_capsule_client_get(info->mti_pill,
1145 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1146 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1147 req_capsule_client_get(info->mti_pill,
1150 o1 = info->mti_object;
1151 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1152 &info->mti_body->fid2);
1154 GOTO(out, rc = PTR_ERR(o));
1156 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1157 GOTO(put, rc = -ENOENT);
1159 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1160 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1166 /* permission check. Make sure the calling process having permission
1167 * to write both files. */
1168 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1173 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1178 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1180 GOTO(put, rc = -EPROTO);
1182 lh1 = &info->mti_lh[MDT_LH_NEW];
1183 mdt_lock_reg_init(lh1, LCK_EX);
1184 lh2 = &info->mti_lh[MDT_LH_OLD];
1185 mdt_lock_reg_init(lh2, LCK_EX);
1187 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1188 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1192 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1193 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1197 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1198 mdt_object_child(o2), msl->msl_flags);
1201 mdt_object_unlock(info, o2, lh2, rc);
1203 mdt_object_unlock(info, o1, lh1, rc);
1205 mdt_object_put(info->mti_env, o);
1210 static int mdt_raw_lookup(struct mdt_thread_info *info,
1211 struct mdt_object *parent,
1212 const struct lu_name *lname,
1213 struct ldlm_reply *ldlm_rep)
1215 struct md_object *next = mdt_object_child(info->mti_object);
1216 const struct mdt_body *reqbody = info->mti_body;
1217 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1218 struct mdt_body *repbody;
1222 if (reqbody->valid != OBD_MD_FLID)
1225 LASSERT(!info->mti_cross_ref);
1227 /* Only got the fid of this obj by name */
1228 fid_zero(child_fid);
1229 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1232 /* XXX is raw_lookup possible as intent operation? */
1235 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1238 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1240 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1243 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1244 repbody->fid1 = *child_fid;
1245 repbody->valid = OBD_MD_FLID;
1251 * UPDATE lock should be taken against parent, and be release before exit;
1252 * child_bits lock should be taken against child, and be returned back:
1253 * (1)normal request should release the child lock;
1254 * (2)intent request will grant the lock to client.
1256 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1257 struct mdt_lock_handle *lhc,
1259 struct ldlm_reply *ldlm_rep)
1261 struct ptlrpc_request *req = mdt_info_req(info);
1262 struct mdt_body *reqbody = NULL;
1263 struct mdt_object *parent = info->mti_object;
1264 struct mdt_object *child;
1265 struct md_object *next = mdt_object_child(parent);
1266 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1267 struct lu_name *lname = NULL;
1268 const char *name = NULL;
1270 struct mdt_lock_handle *lhp = NULL;
1271 struct ldlm_lock *lock;
1272 struct ldlm_res_id *res_id;
1279 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1280 LASSERT(ergo(is_resent,
1281 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1283 LASSERT(parent != NULL);
1284 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1286 RETURN(err_serious(-EFAULT));
1288 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1290 if (!info->mti_cross_ref) {
1292 * XXX: Check for "namelen == 0" is for getattr by fid
1293 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1294 * that is the name must contain at least one character and
1295 * the terminating '\0'
1298 reqbody = req_capsule_client_get(info->mti_pill,
1300 if (unlikely(reqbody == NULL))
1301 RETURN(err_serious(-EFAULT));
1303 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1304 RETURN(err_serious(-EINVAL));
1307 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1309 PFID(mdt_object_fid(parent)),
1310 PFID(&reqbody->fid2), ldlm_rep);
1312 lname = mdt_name(info->mti_env, (char *)name, namelen);
1313 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1314 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1318 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1320 if (unlikely(!mdt_object_exists(parent)) && lname) {
1321 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1323 "Parent doesn't exist!\n");
1325 } else if (!info->mti_cross_ref) {
1326 LASSERTF(!mdt_object_remote(parent),
1327 "Parent "DFID" is on remote server\n",
1328 PFID(mdt_object_fid(parent)));
1331 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1339 if (info->mti_cross_ref) {
1340 /* Only getattr on the child. Parent is on another node. */
1341 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1343 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1344 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1347 /* Do not take lock for resent case. */
1348 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1349 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1350 lhc->mlh_reg_lh.cookie);
1351 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1352 &lock->l_resource->lr_name));
1353 LDLM_LOCK_PUT(lock);
1356 mdt_lock_handle_init(lhc);
1357 mdt_lock_reg_init(lhc, LCK_PR);
1360 * Object's name is on another MDS, no lookup lock is
1361 * needed here but update is.
1363 child_bits &= ~MDS_INODELOCK_LOOKUP;
1364 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1366 rc = mdt_object_lock(info, child, lhc, child_bits,
1370 /* Finally, we can get attr for child. */
1371 if (!mdt_object_exists(child)) {
1372 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1374 "remote object doesn't exist.\n");
1375 mdt_object_unlock(info, child, lhc, 1);
1379 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1381 rc = mdt_getattr_internal(info, child, 0);
1382 if (unlikely(rc != 0))
1383 mdt_object_unlock(info, child, lhc, 1);
1389 /* step 1: lock parent only if parent is a directory */
1390 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1391 lhp = &info->mti_lh[MDT_LH_PARENT];
1392 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1393 rc = mdt_object_lock(info, parent, lhp,
1394 MDS_INODELOCK_UPDATE,
1396 if (unlikely(rc != 0))
1400 /* step 2: lookup child's fid by name */
1401 fid_zero(child_fid);
1402 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1407 mdt_set_disposition(info, ldlm_rep,
1409 GOTO(out_parent, rc);
1411 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1413 *child_fid = reqbody->fid2;
1414 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1418 *step 3: find the child object by fid & lock it.
1419 * regardless if it is local or remote.
1421 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1422 * set parent dir fid the same as child fid in getattr by fid case
1423 * we should not lu_object_find() the object again, could lead
1424 * to hung if there is a concurrent unlink destroyed the object.
1426 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1427 mdt_object_get(info->mti_env, parent);
1430 child = mdt_object_find(info->mti_env, info->mti_mdt,
1434 if (unlikely(IS_ERR(child)))
1435 GOTO(out_parent, rc = PTR_ERR(child));
1437 /* Do not take lock for resent case. */
1438 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1439 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1440 lhc->mlh_reg_lh.cookie);
1442 res_id = &lock->l_resource->lr_name;
1443 if (!fid_res_name_eq(mdt_object_fid(child),
1444 &lock->l_resource->lr_name)) {
1445 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1446 &lock->l_resource->lr_name),
1447 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1448 PLDLMRES(lock->l_resource),
1449 PFID(mdt_object_fid(parent)));
1450 CWARN("Although resent, but still not get child lock"
1451 "parent:"DFID" child:"DFID"\n",
1452 PFID(mdt_object_fid(parent)),
1453 PFID(mdt_object_fid(child)));
1454 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1455 LDLM_LOCK_PUT(lock);
1458 LDLM_LOCK_PUT(lock);
1461 bool try_layout = false;
1464 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1465 mdt_lock_handle_init(lhc);
1466 mdt_lock_reg_init(lhc, LCK_PR);
1468 if (!mdt_object_exists(child)) {
1469 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1471 "Object doesn't exist!\n");
1472 GOTO(out_child, rc = -ENOENT);
1475 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1476 mdt_object_exists(child) && !mdt_object_remote(child)) {
1477 struct md_attr *ma = &info->mti_attr;
1480 ma->ma_need = MA_INODE;
1481 rc = mdt_attr_get_complex(info, child, ma);
1482 if (unlikely(rc != 0))
1483 GOTO(out_child, rc);
1485 /* If the file has not been changed for some time, we
1486 * return not only a LOOKUP lock, but also an UPDATE
1487 * lock and this might save us RPC on later STAT. For
1488 * directories, it also let negative dentry cache start
1489 * working for this dir. */
1490 if (ma->ma_valid & MA_INODE &&
1491 ma->ma_attr.la_valid & LA_CTIME &&
1492 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1493 ma->ma_attr.la_ctime < cfs_time_current_sec())
1494 child_bits |= MDS_INODELOCK_UPDATE;
1497 /* layout lock must be granted in a best-effort way
1498 * for IT operations */
1499 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1500 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1501 exp_connect_layout(info->mti_exp) &&
1502 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1504 /* try to grant layout lock for regular file. */
1510 child_bits |= MDS_INODELOCK_LAYOUT;
1511 /* try layout lock, it may fail to be granted due to
1512 * contention at LOOKUP or UPDATE */
1513 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1515 child_bits &= ~MDS_INODELOCK_LAYOUT;
1516 LASSERT(child_bits != 0);
1517 rc = mdt_object_lock(info, child, lhc,
1518 child_bits, MDT_CROSS_LOCK);
1523 rc = mdt_object_lock(info, child, lhc, child_bits,
1526 if (unlikely(rc != 0))
1527 GOTO(out_child, rc);
1530 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1531 /* Get MA_SOM attributes if update lock is given. */
1533 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1534 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1537 /* finally, we can get attr for child. */
1538 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1539 rc = mdt_getattr_internal(info, child, ma_need);
1540 if (unlikely(rc != 0)) {
1541 mdt_object_unlock(info, child, lhc, 1);
1543 /* Debugging code. */
1544 res_id = &lock->l_resource->lr_name;
1545 LDLM_DEBUG(lock, "Returning lock to client");
1546 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1547 &lock->l_resource->lr_name),
1548 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1549 PLDLMRES(lock->l_resource),
1550 PFID(mdt_object_fid(child)));
1551 if (mdt_object_exists(child) && !mdt_object_remote(child))
1552 mdt_pack_size2body(info, child);
1555 LDLM_LOCK_PUT(lock);
1559 mdt_object_put(info->mti_env, child);
1562 mdt_object_unlock(info, parent, lhp, 1);
1566 /* normal handler: should release the child lock */
1567 int mdt_getattr_name(struct mdt_thread_info *info)
1569 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1570 struct mdt_body *reqbody;
1571 struct mdt_body *repbody;
1575 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1576 LASSERT(reqbody != NULL);
1577 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1578 LASSERT(repbody != NULL);
1580 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1581 repbody->eadatasize = 0;
1582 repbody->aclsize = 0;
1584 rc = mdt_init_ucred(info, reqbody);
1586 GOTO(out_shrink, rc);
1588 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1589 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1590 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1591 lhc->mlh_reg_lh.cookie = 0;
1593 mdt_exit_ucred(info);
1596 mdt_client_compatibility(info);
1597 rc2 = mdt_fix_reply(info);
1603 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1604 void *karg, void *uarg);
1606 int mdt_set_info(struct mdt_thread_info *info)
1608 struct ptlrpc_request *req = mdt_info_req(info);
1611 int keylen, vallen, rc = 0;
1614 rc = req_capsule_server_pack(info->mti_pill);
1618 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1620 DEBUG_REQ(D_HA, req, "no set_info key");
1624 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1627 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1629 DEBUG_REQ(D_HA, req, "no set_info val");
1633 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1636 /* Swab any part of val you need to here */
1637 if (KEY_IS(KEY_READ_ONLY)) {
1639 lustre_msg_set_status(req->rq_repmsg, 0);
1641 spin_lock(&req->rq_export->exp_lock);
1643 *exp_connect_flags_ptr(req->rq_export) |=
1646 *exp_connect_flags_ptr(req->rq_export) &=
1647 ~OBD_CONNECT_RDONLY;
1648 spin_unlock(&req->rq_export->exp_lock);
1650 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1651 struct changelog_setinfo *cs =
1652 (struct changelog_setinfo *)val;
1653 if (vallen != sizeof(*cs)) {
1654 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1657 if (ptlrpc_req_need_swab(req)) {
1658 __swab64s(&cs->cs_recno);
1659 __swab32s(&cs->cs_id);
1662 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1664 lustre_msg_set_status(req->rq_repmsg, rc);
1672 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
1673 struct ptlrpc_request *req);
1676 * Top-level handler for MDT connection requests.
1678 int mdt_connect(struct mdt_thread_info *info)
1681 struct obd_connect_data *reply;
1682 struct obd_export *exp;
1683 struct ptlrpc_request *req = mdt_info_req(info);
1687 rc = target_handle_connect(req);
1689 RETURN(err_serious(rc));
1691 LASSERT(req->rq_export != NULL);
1692 exp = req->rq_export;
1693 info->mti_exp = exp;
1694 info->mti_mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
1695 rc = mdt_init_sec_level(info);
1699 rc = mdt_connect_check_sptlrpc(info->mti_mdt, exp, req);
1703 /* To avoid exposing partially initialized connection flags, changes up
1704 * to this point have been staged in reply->ocd_connect_flags. Now that
1705 * connection handling has completed successfully, atomically update
1706 * the connect flags in the shared export data structure. LU-1623 */
1707 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1708 spin_lock(&exp->exp_lock);
1709 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1710 exp->exp_mdt_data.med_ibits_known = reply->ocd_ibits_known;
1711 exp->exp_connect_data.ocd_brw_size = reply->ocd_brw_size;
1712 spin_unlock(&exp->exp_lock);
1714 rc = mdt_init_idmap(info);
1719 obd_disconnect(class_export_get(req->rq_export));
1723 int mdt_disconnect(struct mdt_thread_info *info)
1728 rc = target_handle_disconnect(mdt_info_req(info));
1730 rc = err_serious(rc);
1734 static int mdt_sendpage(struct mdt_thread_info *info,
1735 struct lu_rdpg *rdpg, int nob)
1737 struct ptlrpc_request *req = mdt_info_req(info);
1738 struct obd_export *exp = req->rq_export;
1739 struct ptlrpc_bulk_desc *desc;
1740 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1747 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1752 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1753 /* old client requires reply size in it's PAGE_SIZE,
1754 * which is rdpg->rp_count */
1755 nob = rdpg->rp_count;
1757 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1758 i++, tmpcount -= tmpsize) {
1759 tmpsize = min_t(int, tmpcount, PAGE_CACHE_SIZE);
1760 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1763 LASSERT(desc->bd_nob == nob);
1764 rc = target_bulk_io(exp, desc, lwi);
1765 ptlrpc_free_bulk_pin(desc);
1769 int mdt_readpage(struct mdt_thread_info *info)
1771 struct mdt_object *object = info->mti_object;
1772 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1773 struct mdt_body *reqbody;
1774 struct mdt_body *repbody;
1779 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1780 RETURN(err_serious(-ENOMEM));
1782 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1783 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1784 if (reqbody == NULL || repbody == NULL)
1785 RETURN(err_serious(-EFAULT));
1788 * prepare @rdpg before calling lower layers and transfer itself. Here
1789 * reqbody->size contains offset of where to start to read and
1790 * reqbody->nlink contains number bytes to read.
1792 rdpg->rp_hash = reqbody->size;
1793 if (rdpg->rp_hash != reqbody->size) {
1794 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1795 rdpg->rp_hash, reqbody->size);
1799 rdpg->rp_attrs = reqbody->mode;
1800 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1801 rdpg->rp_attrs |= LUDA_64BITHASH;
1802 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1803 exp_max_brw_size(info->mti_exp));
1804 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1806 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1807 if (rdpg->rp_pages == NULL)
1810 for (i = 0; i < rdpg->rp_npages; ++i) {
1811 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1812 if (rdpg->rp_pages[i] == NULL)
1813 GOTO(free_rdpg, rc = -ENOMEM);
1816 /* call lower layers to fill allocated pages with directory data */
1817 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1819 GOTO(free_rdpg, rc);
1821 /* send pages to client */
1822 rc = mdt_sendpage(info, rdpg, rc);
1827 for (i = 0; i < rdpg->rp_npages; i++)
1828 if (rdpg->rp_pages[i] != NULL)
1829 __free_page(rdpg->rp_pages[i]);
1830 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1832 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1838 static int mdt_reint_internal(struct mdt_thread_info *info,
1839 struct mdt_lock_handle *lhc,
1842 struct req_capsule *pill = info->mti_pill;
1843 struct mdt_body *repbody;
1848 rc = mdt_reint_unpack(info, op);
1850 CERROR("Can't unpack reint, rc %d\n", rc);
1851 RETURN(err_serious(rc));
1854 /* for replay (no_create) lmm is not needed, client has it already */
1855 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1856 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1859 /* llog cookies are always 0, the field is kept for compatibility */
1860 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1861 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1863 rc = req_capsule_server_pack(pill);
1865 CERROR("Can't pack response, rc %d\n", rc);
1866 RETURN(err_serious(rc));
1869 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1870 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1872 repbody->eadatasize = 0;
1873 repbody->aclsize = 0;
1876 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1878 /* for replay no cookkie / lmm need, because client have this already */
1879 if (info->mti_spec.no_create)
1880 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1881 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1883 rc = mdt_init_ucred_reint(info);
1885 GOTO(out_shrink, rc);
1887 rc = mdt_fix_attr_ucred(info, op);
1889 GOTO(out_ucred, rc = err_serious(rc));
1891 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1892 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1893 GOTO(out_ucred, rc);
1895 rc = mdt_reint_rec(info, lhc);
1898 mdt_exit_ucred(info);
1900 mdt_client_compatibility(info);
1901 rc2 = mdt_fix_reply(info);
1907 static long mdt_reint_opcode(struct mdt_thread_info *info,
1908 const struct req_format **fmt)
1910 struct mdt_rec_reint *rec;
1913 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1915 opc = rec->rr_opcode;
1916 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1917 if (opc < REINT_MAX && fmt[opc] != NULL)
1918 req_capsule_extend(info->mti_pill, fmt[opc]);
1920 CERROR("%s: Unsupported opcode '%ld' from client '%s': "
1921 "rc = %d\n", mdt_obd_name(info->mti_mdt), opc,
1922 info->mti_mdt->mdt_ldlm_client->cli_name,
1924 opc = err_serious(-EFAULT);
1927 opc = err_serious(-EFAULT);
1932 int mdt_reint(struct mdt_thread_info *info)
1937 static const struct req_format *reint_fmts[REINT_MAX] = {
1938 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1939 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1940 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1941 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1942 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1943 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1944 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1945 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1950 opc = mdt_reint_opcode(info, reint_fmts);
1953 * No lock possible here from client to pass it to reint code
1956 rc = mdt_reint_internal(info, NULL, opc);
1961 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1965 /* this should sync the whole device */
1966 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1968 struct dt_device *dt = mdt->mdt_bottom;
1972 rc = dt->dd_ops->dt_sync(env, dt);
1976 /* this should sync this object */
1977 static int mdt_object_sync(struct mdt_thread_info *info)
1979 struct md_object *next;
1983 if (!mdt_object_exists(info->mti_object)) {
1984 CWARN("Non existing object "DFID"!\n",
1985 PFID(mdt_object_fid(info->mti_object)));
1988 next = mdt_object_child(info->mti_object);
1989 rc = mo_object_sync(info->mti_env, next);
1994 int mdt_sync(struct mdt_thread_info *info)
1996 struct ptlrpc_request *req = mdt_info_req(info);
1997 struct req_capsule *pill = info->mti_pill;
1998 struct mdt_body *body;
2002 /* The fid may be zero, so we req_capsule_set manually */
2003 req_capsule_set(pill, &RQF_MDS_SYNC);
2005 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2007 RETURN(err_serious(-EINVAL));
2009 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2010 RETURN(err_serious(-ENOMEM));
2012 if (fid_seq(&body->fid1) == 0) {
2013 /* sync the whole device */
2014 rc = req_capsule_server_pack(pill);
2016 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
2018 rc = err_serious(rc);
2020 /* sync an object */
2021 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
2023 rc = mdt_object_sync(info);
2025 const struct lu_fid *fid;
2026 struct lu_attr *la = &info->mti_attr.ma_attr;
2028 info->mti_attr.ma_need = MA_INODE;
2029 info->mti_attr.ma_valid = 0;
2030 rc = mdt_attr_get_complex(info, info->mti_object,
2033 body = req_capsule_server_get(pill,
2035 fid = mdt_object_fid(info->mti_object);
2036 mdt_pack_attr2body(info, body, la, fid);
2040 rc = err_serious(rc);
2043 mdt_counter_incr(req, LPROC_MDT_SYNC);
2049 * Quotacheck handler.
2050 * in-kernel quotacheck isn't supported any more.
2052 int mdt_quotacheck(struct mdt_thread_info *info)
2054 struct obd_quotactl *oqctl;
2058 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
2060 RETURN(err_serious(-EPROTO));
2062 rc = req_capsule_server_pack(info->mti_pill);
2064 RETURN(err_serious(rc));
2066 /* deprecated, not used any more */
2067 RETURN(-EOPNOTSUPP);
2071 * Handle quota control requests to consult current usage/limit, but also
2072 * to configure quota enforcement
2074 int mdt_quotactl(struct mdt_thread_info *info)
2076 struct obd_export *exp = info->mti_exp;
2077 struct req_capsule *pill = info->mti_pill;
2078 struct obd_quotactl *oqctl, *repoqc;
2080 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2083 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2085 RETURN(err_serious(-EPROTO));
2087 rc = req_capsule_server_pack(pill);
2089 RETURN(err_serious(rc));
2091 switch (oqctl->qc_cmd) {
2093 case LUSTRE_Q_INVALIDATE:
2094 case LUSTRE_Q_FINVALIDATE:
2098 /* deprecated, not used any more */
2099 RETURN(-EOPNOTSUPP);
2100 /* master quotactl */
2106 RETURN(-EOPNOTSUPP);
2107 /* slave quotactl */
2112 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2116 /* map uid/gid for remote client */
2118 if (exp_connect_rmtclient(exp)) {
2119 struct lustre_idmap_table *idmap;
2121 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2123 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2124 oqctl->qc_cmd != Q_GETINFO))
2127 if (oqctl->qc_type == USRQUOTA)
2128 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2130 else if (oqctl->qc_type == GRPQUOTA)
2131 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2136 if (id == CFS_IDMAP_NOTFOUND) {
2137 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2142 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2144 RETURN(err_serious(-EFAULT));
2146 if (oqctl->qc_id != id)
2147 swap(oqctl->qc_id, id);
2149 switch (oqctl->qc_cmd) {
2155 /* forward quotactl request to QMT */
2156 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2161 /* slave quotactl */
2162 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2167 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2171 if (oqctl->qc_id != id)
2172 swap(oqctl->qc_id, id);
2179 * OBD PING and other handlers.
2181 int mdt_obd_ping(struct mdt_thread_info *info)
2186 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2188 rc = target_handle_ping(mdt_info_req(info));
2190 rc = err_serious(rc);
2195 * OBD_IDX_READ handler
2197 int mdt_obd_idx_read(struct mdt_thread_info *info)
2199 struct mdt_device *mdt = info->mti_mdt;
2200 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2201 struct idx_info *req_ii, *rep_ii;
2205 memset(rdpg, 0, sizeof(*rdpg));
2206 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2208 /* extract idx_info buffer from request & reply */
2209 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2210 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2211 RETURN(err_serious(-EPROTO));
2213 rc = req_capsule_server_pack(info->mti_pill);
2215 RETURN(err_serious(rc));
2217 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2219 RETURN(err_serious(-EFAULT));
2220 rep_ii->ii_magic = IDX_INFO_MAGIC;
2222 /* extract hash to start with */
2223 rdpg->rp_hash = req_ii->ii_hash_start;
2225 /* extract requested attributes */
2226 rdpg->rp_attrs = req_ii->ii_attrs;
2228 /* check that fid packed in request is valid and supported */
2229 if (!fid_is_sane(&req_ii->ii_fid))
2231 rep_ii->ii_fid = req_ii->ii_fid;
2234 rep_ii->ii_flags = req_ii->ii_flags;
2236 /* compute number of pages to allocate, ii_count is the number of 4KB
2238 if (req_ii->ii_count <= 0)
2239 GOTO(out, rc = -EFAULT);
2240 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2241 exp_max_brw_size(info->mti_exp));
2242 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
2245 /* allocate pages to store the containers */
2246 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2247 if (rdpg->rp_pages == NULL)
2248 GOTO(out, rc = -ENOMEM);
2249 for (i = 0; i < rdpg->rp_npages; i++) {
2250 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
2251 if (rdpg->rp_pages[i] == NULL)
2252 GOTO(out, rc = -ENOMEM);
2255 /* populate pages with key/record pairs */
2256 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2260 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2261 "asked %d > %d\n", rc, rdpg->rp_count);
2263 /* send pages to client */
2264 rc = mdt_sendpage(info, rdpg, rc);
2268 if (rdpg->rp_pages) {
2269 for (i = 0; i < rdpg->rp_npages; i++)
2270 if (rdpg->rp_pages[i])
2271 __free_page(rdpg->rp_pages[i]);
2272 OBD_FREE(rdpg->rp_pages,
2273 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2278 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2280 return err_serious(-EOPNOTSUPP);
2283 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2285 return err_serious(-EOPNOTSUPP);
2292 /** clone llog ctxt from child (mdd)
2293 * This allows remote llog (replicator) access.
2294 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2295 * context was originally set up, or we can handle them directly.
2296 * I choose the latter, but that means I need any llog
2297 * contexts set up by child to be accessable by the mdt. So we clone the
2298 * context into our context list here.
2300 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2303 struct md_device *next = mdt->mdt_child;
2304 struct llog_ctxt *ctxt;
2307 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2310 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2311 if (rc || ctxt == NULL) {
2315 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2317 CERROR("Can't set mdt ctxt %d\n", rc);
2322 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2323 struct mdt_device *mdt, int idx)
2325 struct llog_ctxt *ctxt;
2327 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2330 /* Put once for the get we just did, and once for the clone */
2331 llog_ctxt_put(ctxt);
2332 llog_ctxt_put(ctxt);
2336 int mdt_llog_create(struct mdt_thread_info *info)
2340 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2341 rc = llog_origin_handle_open(mdt_info_req(info));
2342 return (rc < 0 ? err_serious(rc) : rc);
2345 int mdt_llog_destroy(struct mdt_thread_info *info)
2349 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2350 rc = llog_origin_handle_destroy(mdt_info_req(info));
2351 return (rc < 0 ? err_serious(rc) : rc);
2354 int mdt_llog_read_header(struct mdt_thread_info *info)
2358 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2359 rc = llog_origin_handle_read_header(mdt_info_req(info));
2360 return (rc < 0 ? err_serious(rc) : rc);
2363 int mdt_llog_next_block(struct mdt_thread_info *info)
2367 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2368 rc = llog_origin_handle_next_block(mdt_info_req(info));
2369 return (rc < 0 ? err_serious(rc) : rc);
2372 int mdt_llog_prev_block(struct mdt_thread_info *info)
2376 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2377 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2378 return (rc < 0 ? err_serious(rc) : rc);
2386 static struct ldlm_callback_suite cbs = {
2387 .lcs_completion = ldlm_server_completion_ast,
2388 .lcs_blocking = ldlm_server_blocking_ast,
2389 .lcs_glimpse = ldlm_server_glimpse_ast
2392 int mdt_enqueue(struct mdt_thread_info *info)
2394 struct ptlrpc_request *req;
2398 * info->mti_dlm_req already contains swapped and (if necessary)
2399 * converted dlm request.
2401 LASSERT(info->mti_dlm_req != NULL);
2403 req = mdt_info_req(info);
2404 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2405 req, info->mti_dlm_req, &cbs);
2406 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2407 return rc ? err_serious(rc) : req->rq_status;
2410 int mdt_convert(struct mdt_thread_info *info)
2413 struct ptlrpc_request *req;
2415 LASSERT(info->mti_dlm_req);
2416 req = mdt_info_req(info);
2417 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2418 return rc ? err_serious(rc) : req->rq_status;
2421 int mdt_bl_callback(struct mdt_thread_info *info)
2423 CERROR("bl callbacks should not happen on MDS\n");
2425 return err_serious(-EOPNOTSUPP);
2428 int mdt_cp_callback(struct mdt_thread_info *info)
2430 CERROR("cp callbacks should not happen on MDS\n");
2432 return err_serious(-EOPNOTSUPP);
2436 * sec context handlers
2438 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2442 rc = mdt_handle_idmap(info);
2445 struct ptlrpc_request *req = mdt_info_req(info);
2448 opc = lustre_msg_get_opc(req->rq_reqmsg);
2449 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2450 sptlrpc_svc_ctx_invalidate(req);
2453 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2459 * quota request handlers
2461 int mdt_quota_dqacq(struct mdt_thread_info *info)
2463 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2468 RETURN(err_serious(-EOPNOTSUPP));
2470 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2474 static struct mdt_object *mdt_obj(struct lu_object *o)
2476 LASSERT(lu_device_is_mdt(o->lo_dev));
2477 return container_of0(o, struct mdt_object, mot_obj);
2480 struct mdt_object *mdt_object_new(const struct lu_env *env,
2481 struct mdt_device *d,
2482 const struct lu_fid *f)
2484 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2485 struct lu_object *o;
2486 struct mdt_object *m;
2489 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2490 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2491 if (unlikely(IS_ERR(o)))
2492 m = (struct mdt_object *)o;
2498 struct mdt_object *mdt_object_find(const struct lu_env *env,
2499 struct mdt_device *d,
2500 const struct lu_fid *f)
2502 struct lu_object *o;
2503 struct mdt_object *m;
2506 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2507 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2508 if (unlikely(IS_ERR(o)))
2509 m = (struct mdt_object *)o;
2517 * Asyncronous commit for mdt device.
2519 * Pass asynchonous commit call down the MDS stack.
2521 * \param env environment
2522 * \param mdt the mdt device
2524 static void mdt_device_commit_async(const struct lu_env *env,
2525 struct mdt_device *mdt)
2527 struct dt_device *dt = mdt->mdt_bottom;
2530 rc = dt->dd_ops->dt_commit_async(env, dt);
2531 if (unlikely(rc != 0))
2532 CWARN("async commit start failed with rc = %d", rc);
2536 * Mark the lock as "synchonous".
2538 * Mark the lock to deffer transaction commit to the unlock time.
2540 * \param lock the lock to mark as "synchonous"
2542 * \see mdt_is_lock_sync
2543 * \see mdt_save_lock
2545 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2547 lock->l_ast_data = (void*)1;
2551 * Check whehter the lock "synchonous" or not.
2553 * \param lock the lock to check
2554 * \retval 1 the lock is "synchonous"
2555 * \retval 0 the lock isn't "synchronous"
2557 * \see mdt_set_lock_sync
2558 * \see mdt_save_lock
2560 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2562 return lock->l_ast_data != NULL;
2566 * Blocking AST for mdt locks.
2568 * Starts transaction commit if in case of COS lock conflict or
2569 * deffers such a commit to the mdt_save_lock.
2571 * \param lock the lock which blocks a request or cancelling lock
2572 * \param desc unused
2573 * \param data unused
2574 * \param flag indicates whether this cancelling or blocking callback
2576 * \see ldlm_blocking_ast_nocheck
2578 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2579 void *data, int flag)
2581 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2582 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2586 if (flag == LDLM_CB_CANCELING)
2588 lock_res_and_lock(lock);
2589 if (lock->l_blocking_ast != mdt_blocking_ast) {
2590 unlock_res_and_lock(lock);
2593 if (mdt_cos_is_enabled(mdt) &&
2594 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2595 lock->l_blocking_lock != NULL &&
2596 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2597 mdt_set_lock_sync(lock);
2599 rc = ldlm_blocking_ast_nocheck(lock);
2601 /* There is no lock conflict if l_blocking_lock == NULL,
2602 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2603 * when the last reference to a local lock was released */
2604 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2607 rc = lu_env_init(&env, LCT_LOCAL);
2608 if (unlikely(rc != 0))
2609 CWARN("lu_env initialization failed with rc = %d,"
2610 "cannot start asynchronous commit\n", rc);
2612 mdt_device_commit_async(&env, mdt);
2618 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2619 void *data, int flag)
2621 struct lustre_handle lockh;
2625 case LDLM_CB_BLOCKING:
2626 ldlm_lock2handle(lock, &lockh);
2627 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2629 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2633 case LDLM_CB_CANCELING:
2634 LDLM_DEBUG(lock, "Revoke remote lock\n");
2642 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2643 struct mdt_object *o, struct lustre_handle *lh,
2644 ldlm_mode_t mode, __u64 ibits)
2646 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2647 ldlm_policy_data_t *policy = &mti->mti_policy;
2651 LASSERT(mdt_object_remote(o));
2653 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2655 memset(einfo, 0, sizeof(*einfo));
2656 einfo->ei_type = LDLM_IBITS;
2657 einfo->ei_mode = mode;
2658 einfo->ei_cb_bl = mdt_md_blocking_ast;
2659 einfo->ei_cb_cp = ldlm_completion_ast;
2661 memset(policy, 0, sizeof(*policy));
2662 policy->l_inodebits.bits = ibits;
2664 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2669 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2670 struct mdt_lock_handle *lh, __u64 ibits,
2671 bool nonblock, int locality)
2673 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2674 ldlm_policy_data_t *policy = &info->mti_policy;
2675 struct ldlm_res_id *res_id = &info->mti_res_id;
2680 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2681 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2682 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2683 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2685 if (mdt_object_remote(o)) {
2686 if (locality == MDT_CROSS_LOCK) {
2687 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2688 ibits |= MDS_INODELOCK_LOOKUP;
2691 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2692 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2693 mdt_obd_name(info->mti_mdt), ibits,
2694 PFID(mdt_object_fid(o)));
2695 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2697 /* No PDO lock on remote object */
2698 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2701 if (lh->mlh_type == MDT_PDO_LOCK) {
2702 /* check for exists after object is locked */
2703 if (mdt_object_exists(o) == 0) {
2704 /* Non-existent object shouldn't have PDO lock */
2707 /* Non-dir object shouldn't have PDO lock */
2708 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2713 memset(policy, 0, sizeof(*policy));
2714 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2716 dlmflags = LDLM_FL_ATOMIC_CB;
2718 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2721 * Take PDO lock on whole directory and build correct @res_id for lock
2722 * on part of directory.
2724 if (lh->mlh_pdo_hash != 0) {
2725 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2726 mdt_lock_pdo_mode(info, o, lh);
2727 if (lh->mlh_pdo_mode != LCK_NL) {
2729 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2730 * is never going to be sent to client and we do not
2731 * want it slowed down due to possible cancels.
2733 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2734 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2735 policy, res_id, dlmflags,
2736 info->mti_exp == NULL ? NULL :
2737 &info->mti_exp->exp_handle.h_cookie);
2743 * Finish res_id initializing by name hash marking part of
2744 * directory which is taking modification.
2746 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2749 policy->l_inodebits.bits = ibits;
2752 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2753 * going to be sent to client. If it is - mdt_intent_policy() path will
2754 * fix it up and turn FL_LOCAL flag off.
2756 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2757 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2758 info->mti_exp == NULL ? NULL :
2759 &info->mti_exp->exp_handle.h_cookie);
2761 mdt_object_unlock(info, o, lh, 1);
2762 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2763 lh->mlh_pdo_hash != 0 &&
2764 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2765 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2771 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2772 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2774 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2777 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2778 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2780 struct mdt_lock_handle tmp = *lh;
2783 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2791 * Save a lock within request object.
2793 * Keep the lock referenced until whether client ACK or transaction
2794 * commit happens or release the lock immediately depending on input
2795 * parameters. If COS is ON, a write lock is converted to COS lock
2798 * \param info thead info object
2799 * \param h lock handle
2800 * \param mode lock mode
2801 * \param decref force immediate lock releasing
2804 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2805 ldlm_mode_t mode, int decref)
2809 if (lustre_handle_is_used(h)) {
2810 if (decref || !info->mti_has_trans ||
2811 !(mode & (LCK_PW | LCK_EX))){
2812 mdt_fid_unlock(h, mode);
2814 struct mdt_device *mdt = info->mti_mdt;
2815 struct ldlm_lock *lock = ldlm_handle2lock(h);
2816 struct ptlrpc_request *req = mdt_info_req(info);
2819 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2821 /* there is no request if mdt_object_unlock() is called
2822 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2823 if (likely(req != NULL)) {
2824 CDEBUG(D_HA, "request = %p reply state = %p"
2825 " transno = "LPD64"\n", req,
2826 req->rq_reply_state, req->rq_transno);
2827 if (mdt_cos_is_enabled(mdt)) {
2829 ldlm_lock_downgrade(lock, LCK_COS);
2832 ptlrpc_save_lock(req, h, mode, no_ack);
2834 ldlm_lock_decref(h, mode);
2836 if (mdt_is_lock_sync(lock)) {
2837 CDEBUG(D_HA, "found sync-lock,"
2838 " async commit started\n");
2839 mdt_device_commit_async(info->mti_env,
2842 LDLM_LOCK_PUT(lock);
2851 * Unlock mdt object.
2853 * Immeditely release the regular lock and the PDO lock or save the
2854 * lock in reqeuest and keep them referenced until client ACK or
2855 * transaction commit.
2857 * \param info thread info object
2858 * \param o mdt object
2859 * \param lh mdt lock handle referencing regular and PDO locks
2860 * \param decref force immediate lock releasing
2862 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2863 struct mdt_lock_handle *lh, int decref)
2867 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2868 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2870 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2871 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2876 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2877 const struct lu_fid *f,
2878 struct mdt_lock_handle *lh,
2881 struct mdt_object *o;
2883 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2887 rc = mdt_object_lock(info, o, lh, ibits,
2890 mdt_object_put(info->mti_env, o);
2897 void mdt_object_unlock_put(struct mdt_thread_info * info,
2898 struct mdt_object * o,
2899 struct mdt_lock_handle *lh,
2902 mdt_object_unlock(info, o, lh, decref);
2903 mdt_object_put(info->mti_env, o);
2906 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2908 struct mdt_opc_slice *s;
2909 struct mdt_handler *h;
2912 for (s = supported; s->mos_hs != NULL; s++) {
2913 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2914 h = s->mos_hs + (opc - s->mos_opc_start);
2915 if (likely(h->mh_opc != 0))
2916 LASSERTF(h->mh_opc == opc,
2917 "opcode mismatch %d != %d\n",
2920 h = NULL; /* unsupported opc */
2927 static int mdt_lock_resname_compat(struct mdt_device *m,
2928 struct ldlm_request *req)
2930 /* XXX something... later. */
2934 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2936 /* XXX something... later. */
2941 * Generic code handling requests that have struct mdt_body passed in:
2943 * - extract mdt_body from request and save it in @info, if present;
2945 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2948 * - if HABEO_CORPUS flag is set for this request type check whether object
2949 * actually exists on storage (lu_object_exists()).
2952 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2954 const struct mdt_body *body;
2955 struct mdt_object *obj;
2956 const struct lu_env *env;
2957 struct req_capsule *pill;
2961 env = info->mti_env;
2962 pill = info->mti_pill;
2964 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2968 if (!(body->valid & OBD_MD_FLID))
2971 if (!fid_is_sane(&body->fid1)) {
2972 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2977 * Do not get size or any capa fields before we check that request
2978 * contains capa actually. There are some requests which do not, for
2979 * instance MDS_IS_SUBDIR.
2981 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2982 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2983 mdt_set_capainfo(info, 0, &body->fid1,
2984 req_capsule_client_get(pill, &RMF_CAPA1));
2986 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2988 if ((flags & HABEO_CORPUS) &&
2989 !mdt_object_exists(obj)) {
2990 mdt_object_put(env, obj);
2991 /* for capability renew ENOENT will be handled in
2993 if (body->valid & OBD_MD_FLOSSCAPA)
2998 info->mti_object = obj;
3007 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3009 struct req_capsule *pill = info->mti_pill;
3013 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3014 rc = mdt_body_unpack(info, flags);
3018 if (rc == 0 && (flags & HABEO_REFERO)) {
3020 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3021 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3023 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3024 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3027 rc = req_capsule_server_pack(pill);
3032 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
3034 struct md_device *next = m->mdt_child;
3036 return next->md_ops->mdo_init_capa_ctxt(env, next,
3037 m->mdt_opts.mo_mds_capa,
3038 m->mdt_capa_timeout,
3044 * Invoke handler for this request opc. Also do necessary preprocessing
3045 * (according to handler ->mh_flags), and post-processing (setting of
3046 * ->last_{xid,committed}).
3048 static int mdt_req_handle(struct mdt_thread_info *info,
3049 struct mdt_handler *h, struct ptlrpc_request *req)
3051 int rc, serious = 0;
3056 LASSERT(h->mh_act != NULL);
3057 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
3058 LASSERT(current->journal_info == NULL);
3061 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
3062 * to put same checks into handlers like mdt_close(), mdt_reint(),
3063 * etc., without talking to mdt authors first. Checking same thing
3064 * there again is useless and returning 0 error without packing reply
3065 * is buggy! Handlers either pack reply or return error.
3067 * We return 0 here and do not send any reply in order to emulate
3068 * network failure. Do not send any reply in case any of NET related
3069 * fail_id has occured.
3071 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
3075 flags = h->mh_flags;
3076 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
3078 if (h->mh_fmt != NULL) {
3079 req_capsule_set(info->mti_pill, h->mh_fmt);
3080 rc = mdt_unpack_req_pack_rep(info, flags);
3083 if (rc == 0 && flags & MUTABOR &&
3084 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3085 /* should it be rq_status? */
3088 if (rc == 0 && flags & HABEO_CLAVIS) {
3089 struct ldlm_request *dlm_req;
3091 LASSERT(h->mh_fmt != NULL);
3093 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3094 if (dlm_req != NULL) {
3095 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
3097 dlm_req->lock_desc.l_policy_data.\
3098 l_inodebits.bits == 0)) {
3100 * Lock without inodebits makes no sense and
3101 * will oops later in ldlm. If client miss to
3102 * set such bits, do not trigger ASSERTION.
3104 * For liblustre flock case, it maybe zero.
3108 if (info->mti_mdt &&
3109 info->mti_mdt->mdt_opts.mo_compat_resname)
3110 rc = mdt_lock_resname_compat(
3113 info->mti_dlm_req = dlm_req;
3120 /* capability setting changed via /proc, needs reinitialize ctxt */
3121 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
3122 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
3123 info->mti_mdt->mdt_capa_conf = 0;
3126 if (likely(rc == 0)) {
3128 * Process request, there can be two types of rc:
3129 * 1) errors with msg unpack/pack, other failures outside the
3130 * operation itself. This is counted as serious errors;
3131 * 2) errors during fs operation, should be placed in rq_status
3134 rc = h->mh_act(info);
3136 !req->rq_no_reply && req->rq_reply_state == NULL) {
3137 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3138 "pack reply and returned 0 error\n",
3142 serious = is_serious(rc);
3143 rc = clear_serious(rc);
3147 req->rq_status = rc;
3150 * ELDLM_* codes which > 0 should be in rq_status only as well as
3151 * all non-serious errors.
3153 if (rc > 0 || !serious)
3156 LASSERT(current->journal_info == NULL);
3158 if (rc == 0 && (flags & HABEO_CLAVIS) && info->mti_mdt &&
3159 info->mti_mdt->mdt_opts.mo_compat_resname) {
3160 struct ldlm_reply *dlmrep;
3162 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3164 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3167 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3168 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3169 target_committed_to_req(req);
3171 if (unlikely(req_is_replay(req) &&
3172 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3173 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3177 target_send_reply(req, rc, info->mti_fail_id);
3181 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3183 lh->mlh_type = MDT_NUL_LOCK;
3184 lh->mlh_reg_lh.cookie = 0ull;
3185 lh->mlh_reg_mode = LCK_MINMODE;
3186 lh->mlh_pdo_lh.cookie = 0ull;
3187 lh->mlh_pdo_mode = LCK_MINMODE;
3188 lh->mlh_rreg_lh.cookie = 0ull;
3189 lh->mlh_rreg_mode = LCK_MINMODE;
3192 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3194 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3195 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3199 * Initialize fields of struct mdt_thread_info. Other fields are left in
3200 * uninitialized state, because it's too expensive to zero out whole
3201 * mdt_thread_info (> 1K) on each request arrival.
3203 void mdt_thread_info_init(struct ptlrpc_request *req,
3204 struct mdt_thread_info *info)
3208 info->mti_pill = &req->rq_pill;
3211 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3212 mdt_lock_handle_init(&info->mti_lh[i]);
3214 /* mdt device: it can be NULL while CONNECT */
3215 if (req->rq_export) {
3216 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3217 info->mti_exp = req->rq_export;
3219 info->mti_mdt = NULL;
3220 info->mti_env = req->rq_svc_thread->t_env;
3221 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3222 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3223 info->mti_mos = NULL;
3225 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3226 info->mti_big_buf = LU_BUF_NULL;
3227 info->mti_body = NULL;
3228 info->mti_object = NULL;
3229 info->mti_dlm_req = NULL;
3230 info->mti_has_trans = 0;
3231 info->mti_cross_ref = 0;
3232 info->mti_opdata = 0;
3233 info->mti_big_lmm_used = 0;
3235 /* To not check for split by default. */
3236 info->mti_spec.no_create = 0;
3237 info->mti_spec.sp_rm_entry = 0;
3240 void mdt_thread_info_fini(struct mdt_thread_info *info)
3244 if (info->mti_object != NULL) {
3245 mdt_object_put(info->mti_env, info->mti_object);
3246 info->mti_object = NULL;
3249 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3250 mdt_lock_handle_fini(&info->mti_lh[i]);
3251 info->mti_env = NULL;
3252 info->mti_pill = NULL;
3253 info->mti_exp = NULL;
3255 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3256 lu_buf_free(&info->mti_big_buf);
3259 int mdt_tgt_connect(struct tgt_session_info *tsi)
3261 struct ptlrpc_request *req = tgt_ses_req(tsi);
3262 struct mdt_thread_info *mti;
3267 rc = tgt_connect(tsi);
3271 /* XXX: switch mdt_init_idmap() to use tgt_session_info */
3272 lu_env_refill((void *)tsi->tsi_env);
3273 mti = lu_context_key_get(&tsi->tsi_env->le_ctx, &mdt_thread_key);
3274 LASSERT(mti != NULL);
3276 mdt_thread_info_init(req, mti);
3277 rc = mdt_init_idmap(mti);
3278 mdt_thread_info_fini(mti);
3283 obd_disconnect(class_export_get(req->rq_export));
3287 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3288 struct obd_device *obd, int *process)
3290 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3291 case MDS_CONNECT: /* This will never get here, but for completeness. */
3292 case OST_CONNECT: /* This will never get here, but for completeness. */
3293 case MDS_DISCONNECT:
3294 case OST_DISCONNECT:
3300 case MDS_DONE_WRITING:
3301 case MDS_SYNC: /* used in unmounting */
3308 *process = target_queue_recovery_request(req, obd);
3312 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3319 * Handle recovery. Return:
3320 * +1: continue request processing;
3321 * -ve: abort immediately with the given error code;
3322 * 0: send reply with error code in req->rq_status;
3324 static int mdt_recovery(struct mdt_thread_info *info)
3326 struct ptlrpc_request *req = mdt_info_req(info);
3327 struct obd_device *obd;
3331 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3334 case SEC_CTX_INIT_CONT:
3340 rc = mdt_handle_idmap(info);
3349 if (unlikely(!class_connected_export(req->rq_export))) {
3350 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3351 lustre_msg_get_opc(req->rq_reqmsg),
3352 libcfs_id2str(req->rq_peer));
3353 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3354 * mds_A will get -ENOTCONN(especially for ping req),
3355 * which will cause that mds_A deactive timeout, then when
3356 * mds_A cleanup, the cleanup process will be suspended since
3357 * deactive timeout is not zero.
3359 req->rq_status = -ENOTCONN;
3360 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3364 /* sanity check: if the xid matches, the request must be marked as a
3365 * resent or replayed */
3366 if (req_xid_is_last(req)) {
3367 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3368 (MSG_RESENT | MSG_REPLAY))) {
3369 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3370 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3371 lustre_msg_get_flags(req->rq_reqmsg));
3373 req->rq_status = -ENOTCONN;
3378 /* else: note the opposite is not always true; a RESENT req after a
3379 * failover will usually not match the last_xid, since it was likely
3380 * never committed. A REPLAYed request will almost never match the
3381 * last xid, however it could for a committed, but still retained,
3384 obd = req->rq_export->exp_obd;
3386 /* Check for aborted recovery... */
3387 if (unlikely(obd->obd_recovering)) {
3390 DEBUG_REQ(D_INFO, req, "Got new replay");
3391 rc = mdt_filter_recovery_request(req, obd, &should_process);
3392 if (rc != 0 || !should_process)
3394 else if (should_process < 0) {
3395 req->rq_status = should_process;
3396 rc = ptlrpc_error(req);
3403 static int mdt_msg_check_version(struct lustre_msg *msg)
3407 switch (lustre_msg_get_opc(msg)) {
3409 case MDS_DISCONNECT:
3412 case SEC_CTX_INIT_CONT:
3415 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3417 CERROR("bad opc %u version %08x, expecting %08x\n",
3418 lustre_msg_get_opc(msg),
3419 lustre_msg_get_version(msg),
3420 LUSTRE_OBD_VERSION);
3424 case MDS_GETATTR_NAME:
3431 case MDS_DONE_WRITING:
3438 case MDS_HSM_PROGRESS:
3439 case MDS_HSM_REQUEST:
3440 case MDS_HSM_CT_REGISTER:
3441 case MDS_HSM_CT_UNREGISTER:
3442 case MDS_HSM_STATE_GET:
3443 case MDS_HSM_STATE_SET:
3444 case MDS_HSM_ACTION:
3445 case MDS_QUOTACHECK:
3448 case MDS_SWAP_LAYOUTS:
3453 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3455 CERROR("bad opc %u version %08x, expecting %08x\n",
3456 lustre_msg_get_opc(msg),
3457 lustre_msg_get_version(msg),
3458 LUSTRE_MDS_VERSION);
3462 case LDLM_BL_CALLBACK:
3463 case LDLM_CP_CALLBACK:
3464 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3466 CERROR("bad opc %u version %08x, expecting %08x\n",
3467 lustre_msg_get_opc(msg),
3468 lustre_msg_get_version(msg),
3469 LUSTRE_DLM_VERSION);
3471 case OBD_LOG_CANCEL:
3472 case LLOG_ORIGIN_HANDLE_CREATE:
3473 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3474 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3475 case LLOG_ORIGIN_HANDLE_CLOSE:
3476 case LLOG_ORIGIN_HANDLE_DESTROY:
3477 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3479 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3481 CERROR("bad opc %u version %08x, expecting %08x\n",
3482 lustre_msg_get_opc(msg),
3483 lustre_msg_get_version(msg),
3484 LUSTRE_LOG_VERSION);
3487 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3493 static int mdt_handle0(struct ptlrpc_request *req,
3494 struct mdt_thread_info *info,
3495 struct mdt_opc_slice *supported)
3497 struct mdt_handler *h;
3498 struct lustre_msg *msg;
3503 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3506 LASSERT(current->journal_info == NULL);
3508 msg = req->rq_reqmsg;
3509 rc = mdt_msg_check_version(msg);
3510 if (likely(rc == 0)) {
3511 rc = mdt_recovery(info);
3512 if (likely(rc == +1)) {
3513 h = mdt_handler_find(lustre_msg_get_opc(msg),
3515 if (likely(h != NULL)) {
3516 rc = mdt_req_handle(info, h, req);
3518 CERROR("%s: opc unsupported: 0x%x\n",
3519 mdt_obd_name(info->mti_mdt),
3520 lustre_msg_get_opc(msg));
3521 req->rq_status = -ENOTSUPP;
3522 rc = ptlrpc_error(req);
3527 CDEBUG(D_INFO, "%s: drops mal-formed request: rc = %d\n",
3528 mdt_obd_name(info->mti_mdt), rc);
3529 req->rq_status = rc;
3530 rc = ptlrpc_error(req);
3536 * MDT handler function called by ptlrpc service thread when request comes.
3538 * XXX common "target" functionality should be factored into separate module
3539 * shared by mdt, ost and stand-alone services like fld.
3541 int mdt_handle_common(struct ptlrpc_request *req,
3542 struct mdt_opc_slice *supported)
3545 struct mdt_thread_info *info;
3549 env = req->rq_svc_thread->t_env;
3550 LASSERT(env != NULL);
3551 /* Refill(initilize) the context(mdt_thread_info), in case it is
3552 * not initialized yet. Usually it happens during start up, after
3553 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3554 * before MDT_thread_info is initialized */
3556 LASSERT(env->le_ses != NULL);
3557 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3558 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3559 LASSERT(info != NULL);
3561 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3562 mdt_thread_info_init(req, info);
3564 rc = mdt_handle0(req, info, supported);
3566 mdt_thread_info_fini(info);
3567 req_capsule_fini(&req->rq_pill);
3572 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3575 int mdt_recovery_handle(struct ptlrpc_request *req)
3581 rc = mdt_handle_common(req, mdt_regular_handlers);
3601 static int mdt_intent_getattr(enum mdt_it_code opcode,
3602 struct mdt_thread_info *info,
3603 struct ldlm_lock **,
3606 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3607 struct mdt_thread_info *info,
3608 struct ldlm_lock **lockp,
3611 static int mdt_intent_layout(enum mdt_it_code opcode,
3612 struct mdt_thread_info *info,
3613 struct ldlm_lock **,
3615 static int mdt_intent_reint(enum mdt_it_code opcode,
3616 struct mdt_thread_info *info,
3617 struct ldlm_lock **,
3620 static struct mdt_it_flavor {
3621 const struct req_format *it_fmt;
3623 int (*it_act)(enum mdt_it_code ,
3624 struct mdt_thread_info *,
3625 struct ldlm_lock **,
3628 } mdt_it_flavor[] = {
3630 .it_fmt = &RQF_LDLM_INTENT,
3631 /*.it_flags = HABEO_REFERO,*/
3633 .it_act = mdt_intent_reint,
3634 .it_reint = REINT_OPEN
3637 .it_fmt = &RQF_LDLM_INTENT,
3639 * OCREAT is not a MUTABOR request as if the file
3641 * We do the extra check of OBD_CONNECT_RDONLY in
3642 * mdt_reint_open() when we really need to create
3646 .it_act = mdt_intent_reint,
3647 .it_reint = REINT_OPEN
3650 .it_fmt = &RQF_LDLM_INTENT,
3651 .it_flags = MUTABOR,
3652 .it_act = mdt_intent_reint,
3653 .it_reint = REINT_CREATE
3655 [MDT_IT_GETATTR] = {
3656 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3657 .it_flags = HABEO_REFERO,
3658 .it_act = mdt_intent_getattr
3660 [MDT_IT_READDIR] = {
3666 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3667 .it_flags = HABEO_REFERO,
3668 .it_act = mdt_intent_getattr
3671 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3672 .it_flags = MUTABOR,
3674 .it_reint = REINT_UNLINK
3678 .it_flags = MUTABOR,
3681 [MDT_IT_GETXATTR] = {
3682 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3683 .it_flags = HABEO_CORPUS,
3684 .it_act = mdt_intent_getxattr
3687 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3689 .it_act = mdt_intent_layout
3693 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3694 struct ldlm_lock **lockp,
3695 struct ldlm_lock *new_lock,
3696 struct mdt_lock_handle *lh,
3699 struct ptlrpc_request *req = mdt_info_req(info);
3700 struct ldlm_lock *lock = *lockp;
3703 * Get new lock only for cases when possible resent did not find any
3706 if (new_lock == NULL)
3707 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3709 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3710 lh->mlh_reg_lh.cookie = 0;
3714 LASSERTF(new_lock != NULL,
3715 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3718 * If we've already given this lock to a client once, then we should
3719 * have no readers or writers. Otherwise, we should have one reader
3720 * _or_ writer ref (which will be zeroed below) before returning the
3723 if (new_lock->l_export == req->rq_export) {
3724 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3726 LASSERT(new_lock->l_export == NULL);
3727 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3732 if (new_lock->l_export == req->rq_export) {
3734 * Already gave this to the client, which means that we
3735 * reconstructed a reply.
3737 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3739 lh->mlh_reg_lh.cookie = 0;
3740 RETURN(ELDLM_LOCK_REPLACED);
3744 * Fixup the lock to be given to the client.
3746 lock_res_and_lock(new_lock);
3747 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3748 * possible blocking AST. */
3749 while (new_lock->l_readers > 0) {
3750 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3751 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3752 new_lock->l_readers--;
3754 while (new_lock->l_writers > 0) {
3755 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3756 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3757 new_lock->l_writers--;
3760 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3761 new_lock->l_blocking_ast = lock->l_blocking_ast;
3762 new_lock->l_completion_ast = lock->l_completion_ast;
3763 new_lock->l_remote_handle = lock->l_remote_handle;
3764 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3766 unlock_res_and_lock(new_lock);
3768 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3769 &new_lock->l_remote_handle,
3770 &new_lock->l_exp_hash);
3772 LDLM_LOCK_RELEASE(new_lock);
3773 lh->mlh_reg_lh.cookie = 0;
3775 RETURN(ELDLM_LOCK_REPLACED);
3778 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3779 struct ldlm_lock *new_lock,
3780 struct ldlm_lock **old_lock,
3781 struct mdt_lock_handle *lh,
3782 enum mdt_it_code opcode)
3784 struct ptlrpc_request *req = mdt_info_req(info);
3785 struct obd_export *exp = req->rq_export;
3786 struct lustre_handle remote_hdl;
3787 struct ldlm_request *dlmreq;
3788 struct ldlm_lock *lock;
3790 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3793 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3794 remote_hdl = dlmreq->lock_handle[0];
3795 /* If the client does not require open lock, it does not need to
3796 * search lock in exp_lock_hash, since the server thread will
3797 * make sure the lock will be released, and the resend request
3798 * can always re-enqueue the lock */
3799 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3800 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3801 /* In the function below, .hs_keycmp resolves to
3802 * ldlm_export_lock_keycmp() */
3803 /* coverity[overrun-buffer-val] */
3804 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3806 lock_res_and_lock(lock);
3807 if (lock != new_lock) {
3808 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3809 lh->mlh_reg_mode = lock->l_granted_mode;
3811 LDLM_DEBUG(lock, "Restoring lock cookie");
3812 DEBUG_REQ(D_DLMTRACE, req,
3813 "restoring lock cookie "LPX64,
3814 lh->mlh_reg_lh.cookie);
3816 *old_lock = LDLM_LOCK_GET(lock);
3817 cfs_hash_put(exp->exp_lock_hash,
3819 unlock_res_and_lock(lock);
3822 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3823 unlock_res_and_lock(lock);
3827 * If the xid matches, then we know this is a resent request, and allow
3828 * it. (It's probably an OPEN, for which we don't send a lock.
3830 if (req_xid_is_last(req))
3834 * This remote handle isn't enqueued, so we never received or processed
3835 * this request. Clear MSG_RESENT, because it can be handled like any
3836 * normal request now.
3838 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3840 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3844 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3845 struct mdt_thread_info *info,
3846 struct ldlm_lock **lockp,
3849 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3850 struct ldlm_reply *ldlm_rep = NULL;
3854 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3855 * (for the resend case) or a new lock. Below we will use it to
3856 * replace the original lock.
3858 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3859 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3860 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3861 rc = mdt_object_lock(info, info->mti_object, lhc,
3862 MDS_INODELOCK_XATTR,
3868 grc = mdt_getxattr(info);
3870 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3872 if (mdt_info_req(info)->rq_repmsg != NULL)
3873 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3874 if (ldlm_rep == NULL)
3875 RETURN(err_serious(-EFAULT));
3877 ldlm_rep->lock_policy_res2 = grc;
3882 static int mdt_intent_getattr(enum mdt_it_code opcode,
3883 struct mdt_thread_info *info,
3884 struct ldlm_lock **lockp,
3887 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3888 struct ldlm_lock *new_lock = NULL;
3890 struct ldlm_reply *ldlm_rep;
3891 struct ptlrpc_request *req;
3892 struct mdt_body *reqbody;
3893 struct mdt_body *repbody;
3897 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3900 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3903 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3904 repbody->eadatasize = 0;
3905 repbody->aclsize = 0;
3909 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3911 case MDT_IT_GETATTR:
3912 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3916 CERROR("Unsupported intent (%d)\n", opcode);
3917 GOTO(out_shrink, rc = -EINVAL);
3920 rc = mdt_init_ucred(info, reqbody);
3922 GOTO(out_shrink, rc);
3924 req = info->mti_pill->rc_req;
3925 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3926 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3928 /* Get lock from request for possible resent case. */
3929 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3931 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3932 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3934 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3935 ldlm_rep->lock_policy_res2 = 0;
3936 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3937 ldlm_rep->lock_policy_res2) {
3938 lhc->mlh_reg_lh.cookie = 0ull;
3939 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3942 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3945 mdt_exit_ucred(info);
3947 mdt_client_compatibility(info);
3948 rc2 = mdt_fix_reply(info);
3954 static int mdt_intent_layout(enum mdt_it_code opcode,
3955 struct mdt_thread_info *info,
3956 struct ldlm_lock **lockp,
3959 struct layout_intent *layout;
3961 struct mdt_object *obj = NULL;
3965 if (opcode != MDT_IT_LAYOUT) {
3966 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3971 fid = &info->mti_tmp_fid2;
3972 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3974 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3976 RETURN(PTR_ERR(obj));
3978 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3979 /* get the length of lsm */
3980 rc = mdt_attr_get_eabuf_size(info, obj);
3984 if (rc > info->mti_mdt->mdt_max_mdsize)
3985 info->mti_mdt->mdt_max_mdsize = rc;
3988 mdt_object_put(info->mti_env, obj);
3990 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3991 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
3992 rc = req_capsule_server_pack(info->mti_pill);
3996 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3997 LASSERT(layout != NULL);
3998 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3999 /* return to normal ldlm handling */
4002 CERROR("%s: Unsupported layout intent (%d)\n",
4003 mdt_obd_name(info->mti_mdt), layout->li_opc);
4007 static int mdt_intent_reint(enum mdt_it_code opcode,
4008 struct mdt_thread_info *info,
4009 struct ldlm_lock **lockp,
4012 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4013 struct ldlm_reply *rep = NULL;
4017 static const struct req_format *intent_fmts[REINT_MAX] = {
4018 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4019 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4024 opc = mdt_reint_opcode(info, intent_fmts);
4028 if (mdt_it_flavor[opcode].it_reint != opc) {
4029 CERROR("Reint code %ld doesn't match intent: %d\n",
4031 RETURN(err_serious(-EPROTO));
4034 /* Get lock from request for possible resent case. */
4035 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
4037 rc = mdt_reint_internal(info, lhc, opc);
4039 /* Check whether the reply has been packed successfully. */
4040 if (mdt_info_req(info)->rq_repmsg != NULL)
4041 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4043 RETURN(err_serious(-EFAULT));
4045 /* MDC expects this in any case */
4047 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4049 /* the open lock or the lock for cross-ref object should be
4050 * returned to the client */
4051 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
4052 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
4053 rep->lock_policy_res2 = 0;
4054 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
4058 rep->lock_policy_res2 = clear_serious(rc);
4060 if (rep->lock_policy_res2 == -ENOENT &&
4061 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
4062 rep->lock_policy_res2 = 0;
4064 if (rc == -ENOTCONN || rc == -ENODEV ||
4065 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4067 * If it is the disconnect error (ENODEV & ENOCONN), the error
4068 * will be returned by rq_status, and client at ptlrpc layer
4069 * will detect this, then disconnect, reconnect the import
4070 * immediately, instead of impacting the following the rpc.
4072 lhc->mlh_reg_lh.cookie = 0ull;
4076 * For other cases, the error will be returned by intent.
4077 * and client will retrieve the result from intent.
4080 * FIXME: when open lock is finished, that should be
4083 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4084 LASSERTF(rc == 0, "Error occurred but lock handle "
4085 "is still in use, rc = %d\n", rc);
4086 rep->lock_policy_res2 = 0;
4087 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
4090 lhc->mlh_reg_lh.cookie = 0ull;
4091 RETURN(ELDLM_LOCK_ABORTED);
4096 static int mdt_intent_code(long itcode)
4104 case IT_OPEN|IT_CREAT:
4111 rc = MDT_IT_READDIR;
4114 rc = MDT_IT_GETATTR;
4126 rc = MDT_IT_GETXATTR;
4131 case IT_QUOTA_DQACQ:
4136 CERROR("Unknown intent opcode: %ld\n", itcode);
4143 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
4144 struct ldlm_lock **lockp, __u64 flags)
4146 struct req_capsule *pill;
4147 struct mdt_it_flavor *flv;
4152 opc = mdt_intent_code(itopc);
4156 pill = info->mti_pill;
4158 if (opc == MDT_IT_QUOTA) {
4159 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4162 RETURN(-EOPNOTSUPP);
4164 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4165 /* pass the request to quota master */
4166 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4167 mdt_info_req(info), lockp,
4172 flv = &mdt_it_flavor[opc];
4173 if (flv->it_fmt != NULL)
4174 req_capsule_extend(pill, flv->it_fmt);
4176 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
4178 struct ptlrpc_request *req = mdt_info_req(info);
4179 if (flv->it_flags & MUTABOR &&
4180 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
4183 if (rc == 0 && flv->it_act != NULL) {
4184 struct ldlm_reply *rep;
4186 /* execute policy */
4187 rc = flv->it_act(opc, info, lockp, flags);
4189 /* Check whether the reply has been packed successfully. */
4190 if (mdt_info_req(info)->rq_repmsg != NULL) {
4191 rep = req_capsule_server_get(info->mti_pill,
4193 rep->lock_policy_res2 =
4194 ptlrpc_status_hton(rep->lock_policy_res2);
4202 static int mdt_intent_policy(struct ldlm_namespace *ns,
4203 struct ldlm_lock **lockp, void *req_cookie,
4204 ldlm_mode_t mode, __u64 flags, void *data)
4206 struct mdt_thread_info *info;
4207 struct ptlrpc_request *req = req_cookie;
4208 struct ldlm_intent *it;
4209 struct req_capsule *pill;
4214 LASSERT(req != NULL);
4216 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
4218 LASSERT(info != NULL);
4219 pill = info->mti_pill;
4220 LASSERT(pill->rc_req == req);
4222 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4223 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4224 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4226 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4230 /* Lock without inodebits makes no sense and will oops
4231 * later in ldlm. Let's check it now to see if we have
4232 * ibits corrupted somewhere in mdt_intent_opc().
4233 * The case for client miss to set ibits has been
4234 * processed by others. */
4235 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
4236 lr_type == LDLM_IBITS,
4237 info->mti_dlm_req->lock_desc.\
4238 l_policy_data.l_inodebits.bits != 0));
4240 rc = err_serious(-EFAULT);
4242 /* No intent was provided */
4243 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4244 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4245 rc = req_capsule_server_pack(pill);
4247 rc = err_serious(rc);
4252 static int mdt_seq_fini(const struct lu_env *env,
4253 struct mdt_device *m)
4255 return seq_site_fini(env, mdt_seq_site(m));
4258 static int mdt_seq_init(const struct lu_env *env,
4260 struct mdt_device *m)
4262 struct seq_server_site *ss;
4267 ss = mdt_seq_site(m);
4270 * This is sequence-controller node. Init seq-controller server on local
4273 if (ss->ss_node_id == 0) {
4274 LASSERT(ss->ss_control_seq == NULL);
4276 OBD_ALLOC_PTR(ss->ss_control_seq);
4277 if (ss->ss_control_seq == NULL)
4280 rc = seq_server_init(ss->ss_control_seq,
4281 m->mdt_bottom, uuid,
4282 LUSTRE_SEQ_CONTROLLER,
4287 GOTO(out_seq_fini, rc);
4289 OBD_ALLOC_PTR(ss->ss_client_seq);
4290 if (ss->ss_client_seq == NULL)
4291 GOTO(out_seq_fini, rc = -ENOMEM);
4293 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4294 if (prefix == NULL) {
4295 OBD_FREE_PTR(ss->ss_client_seq);
4296 GOTO(out_seq_fini, rc = -ENOMEM);
4299 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4303 * Init seq-controller client after seq-controller server is
4304 * ready. Pass ss->ss_control_seq to it for direct talking.
4306 rc = seq_client_init(ss->ss_client_seq, NULL,
4307 LUSTRE_SEQ_METADATA, prefix,
4308 ss->ss_control_seq);
4309 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4312 GOTO(out_seq_fini, rc);
4315 /* Init seq-server on local MDT */
4316 LASSERT(ss->ss_server_seq == NULL);
4318 OBD_ALLOC_PTR(ss->ss_server_seq);
4319 if (ss->ss_server_seq == NULL)
4320 GOTO(out_seq_fini, rc = -ENOMEM);
4322 rc = seq_server_init(ss->ss_server_seq,
4323 m->mdt_bottom, uuid,
4328 GOTO(out_seq_fini, rc = -ENOMEM);
4330 /* Assign seq-controller client to local seq-server. */
4331 if (ss->ss_node_id == 0) {
4332 LASSERT(ss->ss_client_seq != NULL);
4334 rc = seq_server_set_cli(ss->ss_server_seq,
4342 mdt_seq_fini(env, m);
4350 static int mdt_fld_fini(const struct lu_env *env,
4351 struct mdt_device *m)
4353 struct seq_server_site *ss = mdt_seq_site(m);
4356 if (ss && ss->ss_server_fld) {
4357 fld_server_fini(env, ss->ss_server_fld);
4358 OBD_FREE_PTR(ss->ss_server_fld);
4359 ss->ss_server_fld = NULL;
4365 static int mdt_fld_init(const struct lu_env *env,
4367 struct mdt_device *m)
4369 struct seq_server_site *ss;
4373 ss = mdt_seq_site(m);
4375 OBD_ALLOC_PTR(ss->ss_server_fld);
4376 if (ss->ss_server_fld == NULL)
4377 RETURN(rc = -ENOMEM);
4379 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4380 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4382 OBD_FREE_PTR(ss->ss_server_fld);
4383 ss->ss_server_fld = NULL;
4390 static void mdt_stack_pre_fini(const struct lu_env *env,
4391 struct mdt_device *m, struct lu_device *top)
4393 struct obd_device *obd;
4394 struct lustre_cfg_bufs *bufs;
4395 struct lustre_cfg *lcfg;
4396 struct mdt_thread_info *info;
4401 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4402 LASSERT(info != NULL);
4404 bufs = &info->mti_u.bufs;
4406 LASSERT(m->mdt_child_exp);
4407 LASSERT(m->mdt_child_exp->exp_obd);
4408 obd = m->mdt_child_exp->exp_obd;
4410 /* process cleanup, pass mdt obd name to get obd umount flags */
4411 /* XXX: this is needed because all layers are referenced by
4412 * objects (some of them are pinned by osd, for example *
4413 * the proper solution should be a model where object used
4414 * by osd only doesn't have mdt/mdd slices -bzzz */
4415 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4416 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4417 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4419 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4422 top->ld_ops->ldo_process_config(env, top, lcfg);
4423 lustre_cfg_free(lcfg);
4427 static void mdt_stack_fini(const struct lu_env *env,
4428 struct mdt_device *m, struct lu_device *top)
4430 struct obd_device *obd = mdt2obd_dev(m);
4431 struct lustre_cfg_bufs *bufs;
4432 struct lustre_cfg *lcfg;
4433 struct mdt_thread_info *info;
4437 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4438 LASSERT(info != NULL);
4440 lu_dev_del_linkage(top->ld_site, top);
4442 lu_site_purge(env, top->ld_site, -1);
4444 bufs = &info->mti_u.bufs;
4445 /* process cleanup, pass mdt obd name to get obd umount flags */
4446 /* another purpose is to let all layers to release their objects */
4447 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4452 lustre_cfg_bufs_set_string(bufs, 1, flags);
4453 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4455 CERROR("Cannot alloc lcfg!\n");
4459 top->ld_ops->ldo_process_config(env, top, lcfg);
4460 lustre_cfg_free(lcfg);
4462 lu_site_purge(env, top->ld_site, -1);
4464 m->mdt_child = NULL;
4465 m->mdt_bottom = NULL;
4467 obd_disconnect(m->mdt_child_exp);
4468 m->mdt_child_exp = NULL;
4470 obd_disconnect(m->mdt_bottom_exp);
4471 m->mdt_child_exp = NULL;
4474 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4475 const char *next, struct obd_export **exp)
4477 struct obd_connect_data *data = NULL;
4478 struct obd_device *obd;
4482 OBD_ALLOC_PTR(data);
4484 GOTO(out, rc = -ENOMEM);
4486 obd = class_name2obd(next);
4488 CERROR("%s: can't locate next device: %s\n",
4489 mdt_obd_name(m), next);
4490 GOTO(out, rc = -ENOTCONN);
4493 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4494 data->ocd_version = LUSTRE_VERSION_CODE;
4496 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4498 CERROR("%s: cannot connect to next dev %s (%d)\n",
4499 mdt_obd_name(m), next, rc);
4509 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4510 struct lustre_cfg *cfg)
4512 char *dev = lustre_cfg_string(cfg, 0);
4513 int rc, name_size, uuid_size;
4514 char *name, *uuid, *p;
4515 struct lustre_cfg_bufs *bufs;
4516 struct lustre_cfg *lcfg;
4517 struct obd_device *obd;
4518 struct lustre_profile *lprof;
4519 struct lu_site *site;
4522 /* in 1.8 we had the only device in the stack - MDS.
4523 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4524 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4525 * to generate names and setup MDT, MDD. MDT will be using
4526 * generated name to connect to MDD. for MDD the next device
4527 * will be LOD with name taken from so called "profile" which
4528 * is generated by mount_option line
4530 * 1.8 MGS generates config. commands like this:
4531 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4532 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4533 * 2.0 MGS generates config. commands like this:
4534 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4535 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4536 * 3:lustre-MDT0000-mdtlov 4:f
4538 * we generate MDD name from MDT one, just replacing T with D
4540 * after all the preparations, the logical equivalent will be
4541 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4542 * 3:lustre-MDT0000-mdtlov 4:f
4543 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4544 * 3:lustre-MDD0000 4:f
4546 * notice we build the stack from down to top: MDD first, then MDT */
4548 name_size = MAX_OBD_NAME;
4549 uuid_size = MAX_OBD_NAME;
4551 OBD_ALLOC(name, name_size);
4552 OBD_ALLOC(uuid, uuid_size);
4553 if (name == NULL || uuid == NULL)
4554 GOTO(cleanup_mem, rc = -ENOMEM);
4556 OBD_ALLOC_PTR(bufs);
4558 GOTO(cleanup_mem, rc = -ENOMEM);
4561 p = strstr(name, "-MDT");
4563 GOTO(cleanup_mem, rc = -ENOMEM);
4566 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4568 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4569 if (lprof == NULL || lprof->lp_dt == NULL) {
4570 CERROR("can't find the profile: %s\n",
4571 lustre_cfg_string(cfg, 0));
4572 GOTO(cleanup_mem, rc = -EINVAL);
4575 lustre_cfg_bufs_reset(bufs, name);
4576 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4577 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4578 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4580 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4582 GOTO(free_bufs, rc = -ENOMEM);
4584 rc = class_attach(lcfg);
4586 GOTO(lcfg_cleanup, rc);
4588 obd = class_name2obd(name);
4590 CERROR("Can not find obd %s (%s in config)\n",
4591 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4592 GOTO(class_detach, rc = -EINVAL);
4595 lustre_cfg_free(lcfg);
4597 lustre_cfg_bufs_reset(bufs, name);
4598 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4599 lustre_cfg_bufs_set_string(bufs, 2, dev);
4600 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4602 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4604 rc = class_setup(obd, lcfg);
4606 GOTO(class_detach, rc);
4608 /* connect to MDD we just setup */
4609 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4613 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4615 LASSERT(mdt_lu_site(mdt) == NULL);
4616 mdt->mdt_lu_dev.ld_site = site;
4617 site->ls_top_dev = &mdt->mdt_lu_dev;
4618 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4621 /* now connect to bottom OSD */
4622 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4623 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4627 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4630 rc = lu_env_refill((struct lu_env *)env);
4632 CERROR("Failure to refill session: '%d'\n", rc);
4634 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4639 class_detach(obd, lcfg);
4641 lustre_cfg_free(lcfg);
4646 OBD_FREE(name, name_size);
4648 OBD_FREE(uuid, uuid_size);
4652 /* setup quota master target on MDT0 */
4653 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4654 struct lustre_cfg *cfg)
4656 struct obd_device *obd;
4657 char *dev = lustre_cfg_string(cfg, 0);
4658 char *qmtname, *uuid, *p;
4659 struct lustre_cfg_bufs *bufs;
4660 struct lustre_cfg *lcfg;
4661 struct lustre_profile *lprof;
4662 struct obd_connect_data *data;
4666 LASSERT(mdt->mdt_qmt_exp == NULL);
4667 LASSERT(mdt->mdt_qmt_dev == NULL);
4669 /* quota master is on MDT0 only for now */
4670 if (mdt->mdt_seq_site.ss_node_id != 0)
4673 /* MGS generates config commands which look as follows:
4674 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4675 * 3:lustre-MDT0000-mdtlov 4:f
4677 * We generate the QMT name from the MDT one, just replacing MD with QM
4678 * after all the preparations, the logical equivalent will be:
4679 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4680 * 3:lustre-MDT0000-osd 4:f */
4681 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4682 OBD_ALLOC(uuid, UUID_MAX);
4683 OBD_ALLOC_PTR(bufs);
4684 OBD_ALLOC_PTR(data);
4685 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4686 GOTO(cleanup_mem, rc = -ENOMEM);
4688 strcpy(qmtname, dev);
4689 p = strstr(qmtname, "-MDT");
4691 GOTO(cleanup_mem, rc = -ENOMEM);
4692 /* replace MD with QM */
4696 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4698 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4699 if (lprof == NULL || lprof->lp_dt == NULL) {
4700 CERROR("can't find profile for %s\n",
4701 lustre_cfg_string(cfg, 0));
4702 GOTO(cleanup_mem, rc = -EINVAL);
4705 lustre_cfg_bufs_reset(bufs, qmtname);
4706 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4707 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4708 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4710 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4712 GOTO(cleanup_mem, rc = -ENOMEM);
4714 rc = class_attach(lcfg);
4716 GOTO(lcfg_cleanup, rc);
4718 obd = class_name2obd(qmtname);
4720 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4721 lustre_cfg_string(cfg, 0));
4722 GOTO(class_detach, rc = -EINVAL);
4725 lustre_cfg_free(lcfg);
4727 lustre_cfg_bufs_reset(bufs, qmtname);
4728 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4729 lustre_cfg_bufs_set_string(bufs, 2, dev);
4731 /* for quota, the next device should be the OSD device */
4732 lustre_cfg_bufs_set_string(bufs, 3,
4733 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4735 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4737 rc = class_setup(obd, lcfg);
4739 GOTO(class_detach, rc);
4741 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4743 /* configure local quota objects */
4744 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4748 GOTO(class_cleanup, rc);
4750 /* connect to quota master target */
4751 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4752 data->ocd_version = LUSTRE_VERSION_CODE;
4753 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4756 CERROR("cannot connect to quota master device %s (%d)\n",
4758 GOTO(class_cleanup, rc);
4764 class_manual_cleanup(obd);
4765 mdt->mdt_qmt_dev = NULL;
4769 class_detach(obd, lcfg);
4771 lustre_cfg_free(lcfg);
4776 OBD_FREE(qmtname, MAX_OBD_NAME);
4778 OBD_FREE(uuid, UUID_MAX);
4784 /* Shutdown quota master target associated with mdt */
4785 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4789 if (mdt->mdt_qmt_exp == NULL)
4791 LASSERT(mdt->mdt_qmt_dev != NULL);
4793 /* the qmt automatically shuts down when the mdt disconnects */
4794 obd_disconnect(mdt->mdt_qmt_exp);
4795 mdt->mdt_qmt_exp = NULL;
4796 mdt->mdt_qmt_dev = NULL;
4800 static struct tgt_handler mdt_tgt_handlers[] = {
4801 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4802 0, MDS_CONNECT, mdt_tgt_connect,
4803 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4804 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4805 0, MDS_DISCONNECT, tgt_disconnect,
4806 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4809 static struct tgt_opc_slice mdt_common_slice[] = {
4811 .tos_opc_start = MDS_FIRST_OPC,
4812 .tos_opc_end = MDS_LAST_OPC,
4813 .tos_hs = mdt_tgt_handlers
4816 .tos_opc_start = OBD_FIRST_OPC,
4817 .tos_opc_end = OBD_LAST_OPC,
4818 .tos_hs = tgt_obd_handlers
4821 .tos_opc_start = LDLM_FIRST_OPC,
4822 .tos_opc_end = LDLM_LAST_OPC,
4823 .tos_hs = tgt_dlm_handlers
4826 .tos_opc_start = SEC_FIRST_OPC,
4827 .tos_opc_end = SEC_LAST_OPC,
4828 .tos_hs = tgt_sec_ctx_handlers
4831 .tos_opc_start = UPDATE_OBJ,
4832 .tos_opc_end = UPDATE_LAST_OPC,
4833 .tos_hs = tgt_out_handlers
4836 .tos_opc_start = FLD_FIRST_OPC,
4837 .tos_opc_end = FLD_LAST_OPC,
4838 .tos_hs = fld_handlers
4841 .tos_opc_start = SEQ_FIRST_OPC,
4842 .tos_opc_end = SEQ_LAST_OPC,
4843 .tos_hs = seq_handlers
4850 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4852 struct md_device *next = m->mdt_child;
4853 struct lu_device *d = &m->mdt_lu_dev;
4854 struct obd_device *obd = mdt2obd_dev(m);
4857 target_recovery_fini(obd);
4859 ping_evictor_stop();
4861 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4863 if (m->mdt_opts.mo_coordinator)
4864 mdt_hsm_cdt_stop(m);
4866 mdt_hsm_cdt_fini(m);
4868 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4869 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4870 obd_exports_barrier(obd);
4871 obd_zombie_barrier();
4875 tgt_fini(env, &m->mdt_lut);
4876 mdt_fs_cleanup(env, m);
4877 upcall_cache_cleanup(m->mdt_identity_cache);
4878 m->mdt_identity_cache = NULL;
4880 if (m->mdt_namespace != NULL) {
4881 ldlm_namespace_free(m->mdt_namespace, NULL,
4882 d->ld_obd->obd_force);
4883 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4886 mdt_quota_fini(env, m);
4888 cfs_free_nidlist(&m->mdt_nosquash_nids);
4889 if (m->mdt_nosquash_str) {
4890 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4891 m->mdt_nosquash_str = NULL;
4892 m->mdt_nosquash_strlen = 0;
4895 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4897 mdt_seq_fini(env, m);
4898 mdt_fld_fini(env, m);
4899 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4901 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4902 cfs_timer_disarm(&m->mdt_ck_timer);
4903 mdt_ck_thread_stop(m);
4908 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4910 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4912 server_put_mount(mdt_obd_name(m));
4917 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4919 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4920 struct sptlrpc_rule_set tmp_rset;
4923 sptlrpc_rule_set_init(&tmp_rset);
4924 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4926 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4927 mdt_obd_name(m), rc);
4931 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4933 write_lock(&m->mdt_sptlrpc_lock);
4934 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4935 m->mdt_sptlrpc_rset = tmp_rset;
4936 write_unlock(&m->mdt_sptlrpc_lock);
4941 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4943 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4944 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4946 struct mdt_thread_info *info;
4947 struct obd_device *obd;
4948 const char *dev = lustre_cfg_string(cfg, 0);
4949 const char *num = lustre_cfg_string(cfg, 2);
4950 struct lustre_mount_info *lmi = NULL;
4951 struct lustre_sb_info *lsi;
4953 struct seq_server_site *ss_site;
4954 const char *identity_upcall = "NONE";
4955 struct md_device *next;
4961 lu_device_init(&m->mdt_lu_dev, ldt);
4963 * Environment (env) might be missing mdt_thread_key values at that
4964 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4967 * Usually device allocation path doesn't use module key values, but
4968 * mdt has to do a lot of work here, so allocate key value.
4970 rc = lu_env_refill((struct lu_env *)env);
4974 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4975 LASSERT(info != NULL);
4977 obd = class_name2obd(dev);
4978 LASSERT(obd != NULL);
4980 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4982 m->mdt_som_conf = 0;
4984 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4986 /* default is coordinator off, it is started through conf_param
4988 m->mdt_opts.mo_coordinator = 0;
4990 lmi = server_get_mount(dev);
4992 CERROR("Cannot get mount info for %s!\n", dev);
4995 lsi = s2lsi(lmi->lmi_sb);
4996 /* CMD is supported only in IAM mode */
4998 node_id = simple_strtol(num, NULL, 10);
4999 obd->u.obt.obt_magic = OBT_MAGIC;
5002 rwlock_init(&m->mdt_sptlrpc_lock);
5003 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
5005 spin_lock_init(&m->mdt_ioepoch_lock);
5006 m->mdt_opts.mo_compat_resname = 0;
5007 m->mdt_opts.mo_mds_capa = 1;
5008 m->mdt_opts.mo_oss_capa = 1;
5009 m->mdt_capa_timeout = CAPA_TIMEOUT;
5010 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5011 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5012 m->mdt_squash_uid = 0;
5013 m->mdt_squash_gid = 0;
5014 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5015 m->mdt_nosquash_str = NULL;
5016 m->mdt_nosquash_strlen = 0;
5017 init_rwsem(&m->mdt_squash_sem);
5018 spin_lock_init(&m->mdt_osfs_lock);
5019 m->mdt_osfs_age = cfs_time_shift_64(-1000);
5020 m->mdt_enable_remote_dir = 0;
5021 m->mdt_enable_remote_dir_gid = 0;
5023 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5024 m->mdt_lu_dev.ld_obd = obd;
5025 /* Set this lu_device to obd for error handling purposes. */
5026 obd->obd_lu_dev = &m->mdt_lu_dev;
5028 /* init the stack */
5029 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5031 CERROR("%s: Can't init device stack, rc %d\n",
5032 mdt_obd_name(m), rc);
5037 ss_site = mdt_seq_site(m);
5038 s->ld_seq_site = ss_site;
5041 /* set server index */
5042 ss_site->ss_node_id = node_id;
5044 /* failover is the default
5045 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5046 * assumed whose ss_node_id == 0 XXX
5048 obd->obd_replayable = 1;
5049 /* No connection accepted until configurations will finish */
5050 obd->obd_no_conn = 1;
5052 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5053 char *str = lustre_cfg_string(cfg, 4);
5054 if (strchr(str, 'n')) {
5055 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5056 obd->obd_replayable = 0;
5060 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5062 GOTO(err_fini_stack, rc);
5064 rc = mdt_seq_init(env, mdt_obd_name(m), m);
5066 GOTO(err_fini_fld, rc);
5068 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5069 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5070 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5071 LDLM_NAMESPACE_SERVER,
5072 LDLM_NAMESPACE_GREEDY,
5074 if (m->mdt_namespace == NULL)
5075 GOTO(err_fini_seq, rc = -ENOMEM);
5077 m->mdt_namespace->ns_lvbp = m;
5078 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5080 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5081 /* set obd_namespace for compatibility with old code */
5082 obd->obd_namespace = m->mdt_namespace;
5084 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5086 rc = mdt_hsm_cdt_init(m);
5088 CERROR("%s: error initializing coordinator, rc %d\n",
5089 mdt_obd_name(m), rc);
5090 GOTO(err_free_ns, rc);
5093 rc = mdt_ck_thread_start(m);
5095 GOTO(err_free_hsm, rc);
5097 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5098 OBD_FAIL_MDS_ALL_REQUEST_NET,
5099 OBD_FAIL_MDS_ALL_REPLY_NET);
5103 rc = mdt_fs_setup(env, m, obd, lsi);
5107 mdt_adapt_sptlrpc_conf(obd, 1);
5109 next = m->mdt_child;
5110 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5113 GOTO(err_fs_cleanup, rc);
5115 if (mntopts & MNTOPT_USERXATTR)
5116 m->mdt_opts.mo_user_xattr = 1;
5118 m->mdt_opts.mo_user_xattr = 0;
5120 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
5122 GOTO(err_fs_cleanup, rc);
5124 if (mntopts & MNTOPT_ACL)
5125 m->mdt_opts.mo_acl = 1;
5127 m->mdt_opts.mo_acl = 0;
5129 /* XXX: to support suppgid for ACL, we enable identity_upcall
5130 * by default, otherwise, maybe got unexpected -EACCESS. */
5131 if (m->mdt_opts.mo_acl)
5132 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5134 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5136 &mdt_identity_upcall_cache_ops);
5137 if (IS_ERR(m->mdt_identity_cache)) {
5138 rc = PTR_ERR(m->mdt_identity_cache);
5139 m->mdt_identity_cache = NULL;
5140 GOTO(err_fs_cleanup, rc);
5143 rc = mdt_procfs_init(m, dev);
5145 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5146 GOTO(err_recovery, rc);
5149 rc = mdt_quota_init(env, m, cfg);
5151 GOTO(err_procfs, rc);
5153 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5154 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5155 "mdt_ldlm_client", m->mdt_ldlm_client);
5157 ping_evictor_start();
5159 /* recovery will be started upon mdt_prepare()
5160 * when the whole stack is complete and ready
5161 * to serve the requests */
5163 mdt_init_capa_ctxt(env, m);
5165 /* Reduce the initial timeout on an MDS because it doesn't need such
5166 * a long timeout as an OST does. Adaptive timeouts will adjust this
5167 * value appropriately. */
5168 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5169 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5175 target_recovery_fini(obd);
5176 upcall_cache_cleanup(m->mdt_identity_cache);
5177 m->mdt_identity_cache = NULL;
5179 mdt_fs_cleanup(env, m);
5181 tgt_fini(env, &m->mdt_lut);
5183 cfs_timer_disarm(&m->mdt_ck_timer);
5184 mdt_ck_thread_stop(m);
5186 mdt_hsm_cdt_fini(m);
5188 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5189 obd->obd_namespace = m->mdt_namespace = NULL;
5191 mdt_seq_fini(env, m);
5193 mdt_fld_fini(env, m);
5195 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5198 server_put_mount(dev);
5202 /* For interoperability, the left element is old parameter, the right one
5203 * is the new version of the parameter, if some parameter is deprecated,
5204 * the new version should be set as NULL. */
5205 static struct cfg_interop_param mdt_interop_param[] = {
5206 { "mdt.group_upcall", NULL },
5207 { "mdt.quota_type", NULL },
5208 { "mdd.quota_type", NULL },
5209 { "mdt.rootsquash", "mdt.root_squash" },
5210 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5214 /* used by MGS to process specific configurations */
5215 static int mdt_process_config(const struct lu_env *env,
5216 struct lu_device *d, struct lustre_cfg *cfg)
5218 struct mdt_device *m = mdt_dev(d);
5219 struct md_device *md_next = m->mdt_child;
5220 struct lu_device *next = md2lu_dev(md_next);
5224 switch (cfg->lcfg_command) {
5226 struct lprocfs_static_vars lvars;
5227 struct obd_device *obd = d->ld_obd;
5229 /* For interoperability */
5230 struct cfg_interop_param *ptr = NULL;
5231 struct lustre_cfg *old_cfg = NULL;
5234 param = lustre_cfg_string(cfg, 1);
5235 if (param == NULL) {
5236 CERROR("param is empty\n");
5241 ptr = class_find_old_param(param, mdt_interop_param);
5243 if (ptr->new_param == NULL) {
5245 CWARN("For interoperability, skip this %s."
5246 " It is obsolete.\n", ptr->old_param);
5250 CWARN("Found old param %s, changed it to %s.\n",
5251 ptr->old_param, ptr->new_param);
5254 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5261 lprocfs_mdt_init_vars(&lvars);
5262 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5264 if (rc > 0 || rc == -ENOSYS) {
5265 /* is it an HSM var ? */
5266 rc = class_process_proc_param(PARAM_HSM,
5267 hsm_cdt_get_proc_vars(),
5269 if (rc > 0 || rc == -ENOSYS)
5270 /* we don't understand; pass it on */
5271 rc = next->ld_ops->ldo_process_config(env, next,
5275 if (old_cfg != NULL)
5276 lustre_cfg_free(cfg);
5281 /* others are passed further */
5282 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5288 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5289 const struct lu_object_header *hdr,
5290 struct lu_device *d)
5292 struct mdt_object *mo;
5296 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5298 struct lu_object *o;
5299 struct lu_object_header *h;
5302 h = &mo->mot_header;
5303 lu_object_header_init(h);
5304 lu_object_init(o, h, d);
5305 lu_object_add_top(h, o);
5306 o->lo_ops = &mdt_obj_ops;
5307 mutex_init(&mo->mot_ioepoch_mutex);
5308 mutex_init(&mo->mot_lov_mutex);
5309 init_rwsem(&mo->mot_open_sem);
5315 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5316 const struct lu_object_conf *unused)
5318 struct mdt_device *d = mdt_dev(o->lo_dev);
5319 struct lu_device *under;
5320 struct lu_object *below;
5324 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5325 PFID(lu_object_fid(o)));
5327 under = &d->mdt_child->md_lu_dev;
5328 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5329 if (below != NULL) {
5330 lu_object_add(o, below);
5337 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5339 struct mdt_object *mo = mdt_obj(o);
5340 struct lu_object_header *h;
5344 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5345 PFID(lu_object_fid(o)));
5347 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5348 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5351 lu_object_header_fini(h);
5352 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5357 static int mdt_object_print(const struct lu_env *env, void *cookie,
5358 lu_printer_t p, const struct lu_object *o)
5360 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5361 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5362 "flags="LPX64", epochcount=%d, writecount=%d)",
5363 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5364 mdto->mot_ioepoch_count, mdto->mot_writecount);
5367 static int mdt_prepare(const struct lu_env *env,
5368 struct lu_device *pdev,
5369 struct lu_device *cdev)
5371 struct mdt_device *mdt = mdt_dev(cdev);
5372 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5373 struct obd_device *obd = cdev->ld_obd;
5374 struct lfsck_start_param lsp;
5381 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5385 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5389 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5393 lsp.lsp_start = NULL;
5394 lsp.lsp_namespace = mdt->mdt_namespace;
5395 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5396 OBD_IOC_START_LFSCK,
5399 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5400 mdt_obd_name(mdt), rc);
5404 if (mdt->mdt_seq_site.ss_node_id == 0) {
5405 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5406 &mdt->mdt_md_root_fid);
5411 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5412 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5413 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5414 LASSERT(obd->obd_no_conn);
5415 spin_lock(&obd->obd_dev_lock);
5416 obd->obd_no_conn = 0;
5417 spin_unlock(&obd->obd_dev_lock);
5419 if (obd->obd_recovering == 0)
5420 mdt_postrecov(env, mdt);
5425 const struct lu_device_operations mdt_lu_ops = {
5426 .ldo_object_alloc = mdt_object_alloc,
5427 .ldo_process_config = mdt_process_config,
5428 .ldo_prepare = mdt_prepare,
5431 static const struct lu_object_operations mdt_obj_ops = {
5432 .loo_object_init = mdt_object_init,
5433 .loo_object_free = mdt_object_free,
5434 .loo_object_print = mdt_object_print
5437 static int mdt_obd_set_info_async(const struct lu_env *env,
5438 struct obd_export *exp,
5439 __u32 keylen, void *key,
5440 __u32 vallen, void *val,
5441 struct ptlrpc_request_set *set)
5443 struct obd_device *obd = exp->exp_obd;
5449 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5450 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5458 * Match client and server connection feature flags.
5460 * Compute the compatibility flags for a connection request based on
5461 * features mutually supported by client and server.
5463 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5464 * must not be updated here, otherwise a partially initialized value may
5465 * be exposed. After the connection request is successfully processed,
5466 * the top-level MDT connect request handler atomically updates the export
5467 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5468 * reply. \see mdt_connect().
5470 * \param exp the obd_export associated with this client/target pair
5471 * \param mdt the target device for the connection
5472 * \param data stores data for this connect request
5475 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5476 * \retval -EBADE client and server feature requirements are incompatible
5478 static int mdt_connect_internal(struct obd_export *exp,
5479 struct mdt_device *mdt,
5480 struct obd_connect_data *data)
5482 LASSERT(data != NULL);
5484 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5485 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5487 /* If no known bits (which should not happen, probably,
5488 as everybody should support LOOKUP and UPDATE bits at least)
5489 revert to compat mode with plain locks. */
5490 if (!data->ocd_ibits_known &&
5491 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5492 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5494 if (!mdt->mdt_opts.mo_acl)
5495 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5497 if (!mdt->mdt_opts.mo_user_xattr)
5498 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5500 if (!mdt->mdt_som_conf)
5501 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5503 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5504 data->ocd_brw_size = min(data->ocd_brw_size,
5505 (__u32)MD_MAX_BRW_SIZE);
5506 if (data->ocd_brw_size == 0) {
5507 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5508 " ocd_version: %x ocd_grant: %d "
5509 "ocd_index: %u ocd_brw_size is "
5510 "unexpectedly zero, network data "
5511 "corruption? Refusing connection of this"
5514 exp->exp_client_uuid.uuid,
5515 exp, data->ocd_connect_flags, data->ocd_version,
5516 data->ocd_grant, data->ocd_index);
5521 /* NB: Disregard the rule against updating
5522 * exp_connect_data.ocd_connect_flags in this case, since
5523 * tgt_client_new() needs to know if this is a lightweight
5524 * connection, and it is safe to expose this flag before
5525 * connection processing completes. */
5526 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5527 spin_lock(&exp->exp_lock);
5528 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5529 spin_unlock(&exp->exp_lock);
5532 data->ocd_version = LUSTRE_VERSION_CODE;
5534 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5535 CWARN("%s: MDS requires FID support, but client not\n",
5540 if (mdt->mdt_som_conf &&
5541 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5542 OBD_CONNECT_MDS_MDS |
5543 OBD_CONNECT_SOM))) {
5544 CWARN("%s: MDS has SOM enabled, but client does not support "
5545 "it\n", mdt_obd_name(mdt));
5549 if (OCD_HAS_FLAG(data, PINGLESS)) {
5550 if (ptlrpc_pinger_suppress_pings()) {
5551 spin_lock(&exp->exp_obd->obd_dev_lock);
5552 list_del_init(&exp->exp_obd_chain_timed);
5553 spin_unlock(&exp->exp_obd->obd_dev_lock);
5555 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5559 data->ocd_max_easize = mdt->mdt_max_ea_size;
5564 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
5565 struct ptlrpc_request *req)
5567 struct sptlrpc_flavor flvr;
5570 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5571 read_lock(&mdt->mdt_sptlrpc_lock);
5572 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5576 read_unlock(&mdt->mdt_sptlrpc_lock);
5578 spin_lock(&exp->exp_lock);
5580 exp->exp_sp_peer = req->rq_sp_from;
5581 exp->exp_flvr = flvr;
5583 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5584 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5585 CERROR("unauthorized rpc flavor %x from %s, "
5586 "expect %x\n", req->rq_flvr.sf_rpc,
5587 libcfs_nid2str(req->rq_peer.nid),
5588 exp->exp_flvr.sf_rpc);
5592 spin_unlock(&exp->exp_lock);
5594 if (exp->exp_sp_peer != req->rq_sp_from) {
5595 CERROR("RPC source %s doesn't match %s\n",
5596 sptlrpc_part2name(req->rq_sp_from),
5597 sptlrpc_part2name(exp->exp_sp_peer));
5600 rc = sptlrpc_target_export_check(exp, req);
5607 /* mds_connect copy */
5608 static int mdt_obd_connect(const struct lu_env *env,
5609 struct obd_export **exp, struct obd_device *obd,
5610 struct obd_uuid *cluuid,
5611 struct obd_connect_data *data,
5614 struct obd_export *lexp;
5615 struct lustre_handle conn = { 0 };
5616 struct mdt_device *mdt;
5620 LASSERT(env != NULL);
5621 if (!exp || !obd || !cluuid)
5624 mdt = mdt_dev(obd->obd_lu_dev);
5627 * first, check whether the stack is ready to handle requests
5628 * XXX: probably not very appropriate method is used now
5629 * at some point we should find a better one
5631 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5632 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5633 rc = obd_get_info(env, mdt->mdt_child_exp,
5634 sizeof(KEY_OSP_CONNECTED),
5635 KEY_OSP_CONNECTED, NULL, NULL, NULL);
5638 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5641 rc = class_connect(&conn, obd, cluuid);
5645 lexp = class_conn2export(&conn);
5646 LASSERT(lexp != NULL);
5648 rc = mdt_connect_internal(lexp, mdt, data);
5650 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5653 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5654 rc = tgt_client_new(env, lexp);
5656 mdt_export_stats_init(obd, lexp, localdata);
5660 class_disconnect(lexp);
5669 static int mdt_obd_reconnect(const struct lu_env *env,
5670 struct obd_export *exp, struct obd_device *obd,
5671 struct obd_uuid *cluuid,
5672 struct obd_connect_data *data,
5678 if (exp == NULL || obd == NULL || cluuid == NULL)
5681 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5683 mdt_export_stats_init(obd, exp, localdata);
5688 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5689 struct mdt_thread_info *info,
5690 struct mdt_file_data *mfd)
5692 struct lu_context ses;
5696 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5701 lu_context_enter(&ses);
5703 mdt_ucred(info)->uc_valid = UCRED_OLD;
5704 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5706 lu_context_exit(&ses);
5707 lu_context_fini(&ses);
5713 static int mdt_export_cleanup(struct obd_export *exp)
5715 struct mdt_export_data *med = &exp->exp_mdt_data;
5716 struct obd_device *obd = exp->exp_obd;
5717 struct mdt_device *mdt;
5718 struct mdt_thread_info *info;
5720 CFS_LIST_HEAD(closing_list);
5721 struct mdt_file_data *mfd, *n;
5725 spin_lock(&med->med_open_lock);
5726 while (!cfs_list_empty(&med->med_open_head)) {
5727 cfs_list_t *tmp = med->med_open_head.next;
5728 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5730 /* Remove mfd handle so it can't be found again.
5731 * We are consuming the mfd_list reference here. */
5732 class_handle_unhash(&mfd->mfd_handle);
5733 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5735 spin_unlock(&med->med_open_lock);
5736 mdt = mdt_dev(obd->obd_lu_dev);
5737 LASSERT(mdt != NULL);
5739 rc = lu_env_init(&env, LCT_MD_THREAD);
5743 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5744 LASSERT(info != NULL);
5745 memset(info, 0, sizeof *info);
5746 info->mti_env = &env;
5747 info->mti_mdt = mdt;
5748 info->mti_exp = exp;
5750 if (!cfs_list_empty(&closing_list)) {
5751 struct md_attr *ma = &info->mti_attr;
5753 /* Close any open files (which may also cause orphan unlinking). */
5754 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5755 cfs_list_del_init(&mfd->mfd_list);
5756 ma->ma_need = ma->ma_valid = 0;
5758 /* This file is being closed due to an eviction, it
5759 * could have been modified and now dirty regarding to
5760 * HSM archive, check this!
5761 * The logic here is to mark a file dirty if there's a
5762 * chance it was dirtied before the client was evicted,
5763 * so that we don't have to wait for a release attempt
5764 * before finding out the file was actually dirty and
5765 * fail the release. Aggressively marking it dirty here
5766 * will cause the policy engine to attempt to
5767 * re-archive it; when rearchiving, we can compare the
5768 * current version to the HSM data_version and make the
5769 * archive request into a noop if it's not actually
5772 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5773 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5775 /* Don't unlink orphan on failover umount, LU-184 */
5776 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5777 ma->ma_valid = MA_FLAGS;
5778 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5780 mdt_mfd_close(info, mfd);
5783 info->mti_mdt = NULL;
5784 /* cleanup client slot early */
5785 /* Do not erase record for recoverable client. */
5786 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5787 tgt_client_del(&env, exp);
5793 static int mdt_obd_disconnect(struct obd_export *exp)
5799 class_export_get(exp);
5801 rc = server_disconnect_export(exp);
5803 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5805 rc = mdt_export_cleanup(exp);
5806 class_export_put(exp);
5810 /* FIXME: Can we avoid using these two interfaces? */
5811 static int mdt_init_export(struct obd_export *exp)
5813 struct mdt_export_data *med = &exp->exp_mdt_data;
5817 CFS_INIT_LIST_HEAD(&med->med_open_head);
5818 spin_lock_init(&med->med_open_lock);
5819 mutex_init(&med->med_idmap_mutex);
5820 med->med_idmap = NULL;
5821 spin_lock(&exp->exp_lock);
5822 exp->exp_connecting = 1;
5823 spin_unlock(&exp->exp_lock);
5825 /* self-export doesn't need client data and ldlm initialization */
5826 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5827 &exp->exp_client_uuid)))
5830 rc = tgt_client_alloc(exp);
5834 rc = ldlm_init_export(exp);
5841 tgt_client_free(exp);
5843 CERROR("%s: Failed to initialize export: rc = %d\n",
5844 exp->exp_obd->obd_name, rc);
5848 static int mdt_destroy_export(struct obd_export *exp)
5852 if (exp_connect_rmtclient(exp))
5853 mdt_cleanup_idmap(&exp->exp_mdt_data);
5855 target_destroy_export(exp);
5856 /* destroy can be called from failed obd_setup, so
5857 * checking uuid is safer than obd_self_export */
5858 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5859 &exp->exp_client_uuid)))
5862 ldlm_destroy_export(exp);
5863 tgt_client_free(exp);
5865 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5866 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5871 /** The maximum depth that fid2path() will search.
5872 * This is limited only because we want to store the fids for
5873 * historical path lookup purposes.
5875 #define MAX_PATH_DEPTH 100
5877 /** mdt_path() lookup structure. */
5878 struct path_lookup_info {
5879 __u64 pli_recno; /**< history point */
5880 __u64 pli_currec; /**< current record */
5881 struct lu_fid pli_fid;
5882 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5883 struct mdt_object *pli_mdt_obj;
5884 char *pli_path; /**< full path */
5886 int pli_linkno; /**< which hardlink to follow */
5887 int pli_fidcount; /**< number of \a pli_fids */
5890 static int mdt_links_read(struct mdt_thread_info *info,
5891 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5895 LASSERT(ldata->ld_buf->lb_buf != NULL);
5897 if (!mdt_object_exists(mdt_obj))
5900 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5901 ldata->ld_buf, XATTR_NAME_LINK);
5902 if (rc == -ERANGE) {
5903 /* Buf was too small, figure out what we need. */
5904 lu_buf_free(ldata->ld_buf);
5905 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5906 ldata->ld_buf, XATTR_NAME_LINK);
5909 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5910 if (ldata->ld_buf->lb_buf == NULL)
5912 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5913 ldata->ld_buf, XATTR_NAME_LINK);
5918 return linkea_init(ldata);
5921 static int mdt_path_current(struct mdt_thread_info *info,
5922 struct path_lookup_info *pli)
5924 struct mdt_device *mdt = info->mti_mdt;
5925 struct mdt_object *mdt_obj;
5926 struct link_ea_header *leh;
5927 struct link_ea_entry *lee;
5928 struct lu_name *tmpname = &info->mti_name;
5929 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5930 struct lu_buf *buf = &info->mti_big_buf;
5933 struct linkea_data ldata = { 0 };
5937 /* temp buffer for path element, the buffer will be finally freed
5938 * in mdt_thread_info_fini */
5939 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5940 if (buf->lb_buf == NULL)
5944 ptr = pli->pli_path + pli->pli_pathlen - 1;
5947 pli->pli_fidcount = 0;
5948 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5950 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5951 * so checking root_fid can only happen on MDT0. */
5952 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5953 &pli->pli_fids[pli->pli_fidcount])) {
5954 mdt_obj = mdt_object_find(info->mti_env, mdt,
5955 &pli->pli_fids[pli->pli_fidcount]);
5956 if (IS_ERR(mdt_obj))
5957 GOTO(out, rc = PTR_ERR(mdt_obj));
5958 if (mdt_object_remote(mdt_obj)) {
5959 mdt_object_put(info->mti_env, mdt_obj);
5960 GOTO(remote_out, rc = -EREMOTE);
5962 if (!mdt_object_exists(mdt_obj)) {
5963 mdt_object_put(info->mti_env, mdt_obj);
5964 GOTO(out, rc = -ENOENT);
5967 rc = mdt_links_read(info, mdt_obj, &ldata);
5968 mdt_object_put(info->mti_env, mdt_obj);
5973 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5974 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5975 /* If set, use link #linkno for path lookup, otherwise use
5976 link #0. Only do this for the final path element. */
5977 if (pli->pli_fidcount == 0 &&
5978 pli->pli_linkno < leh->leh_reccount) {
5980 for (count = 0; count < pli->pli_linkno; count++) {
5981 lee = (struct link_ea_entry *)
5982 ((char *)lee + reclen);
5983 linkea_entry_unpack(lee, &reclen, tmpname,
5986 if (pli->pli_linkno < leh->leh_reccount - 1)
5987 /* indicate to user there are more links */
5991 /* Pack the name in the end of the buffer */
5992 ptr -= tmpname->ln_namelen;
5993 if (ptr - 1 <= pli->pli_path)
5994 GOTO(out, rc = -EOVERFLOW);
5995 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5998 /* Store the parent fid for historic lookup */
5999 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
6000 GOTO(out, rc = -EOVERFLOW);
6001 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
6005 ptr++; /* skip leading / */
6006 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
6013 /* Returns the full path to this fid, as of changelog record recno. */
6014 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6015 char *path, int pathlen, __u64 *recno, int *linkno,
6018 struct mdt_device *mdt = info->mti_mdt;
6019 struct path_lookup_info *pli;
6027 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
6036 pli->pli_mdt_obj = obj;
6037 pli->pli_recno = *recno;
6038 pli->pli_path = path;
6039 pli->pli_pathlen = pathlen;
6040 pli->pli_linkno = *linkno;
6042 /* Retry multiple times in case file is being moved */
6043 while (tries-- && rc == -EAGAIN)
6044 rc = mdt_path_current(info, pli);
6046 /* return the last resolved fids to the client, so the client will
6047 * build the left path on another MDT for remote object */
6048 *fid = pli->pli_fids[pli->pli_fidcount];
6050 *recno = pli->pli_currec;
6051 /* Return next link index to caller */
6052 *linkno = pli->pli_linkno;
6059 static int mdt_fid2path(struct mdt_thread_info *info,
6060 struct getinfo_fid2path *fp)
6062 struct mdt_device *mdt = info->mti_mdt;
6063 struct mdt_object *obj;
6067 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
6068 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6070 if (!fid_is_sane(&fp->gf_fid))
6073 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6074 CWARN("%s: "DFID" is invalid, sequence should be "
6075 ">= "LPX64"\n", mdt_obd_name(mdt),
6076 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6080 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6081 if (obj == NULL || IS_ERR(obj)) {
6082 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
6087 if (mdt_object_remote(obj))
6089 else if (!mdt_object_exists(obj))
6095 mdt_object_put(info->mti_env, obj);
6096 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
6097 PFID(&fp->gf_fid), rc);
6101 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
6102 &fp->gf_linkno, &fp->gf_fid);
6104 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
6105 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
6107 mdt_object_put(info->mti_env, obj);
6112 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
6113 void *val, int vallen)
6115 struct getinfo_fid2path *fpout, *fpin;
6118 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6121 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6122 lustre_swab_fid2path(fpin);
6124 memcpy(fpout, fpin, sizeof(*fpin));
6125 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6128 rc = mdt_fid2path(info, fpout);
6132 int mdt_get_info(struct mdt_thread_info *info)
6134 struct ptlrpc_request *req = mdt_info_req(info);
6142 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
6144 CDEBUG(D_IOCTL, "No GETINFO key");
6147 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
6150 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
6151 if (vallen == NULL) {
6152 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
6156 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6158 rc = req_capsule_server_pack(info->mti_pill);
6159 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
6160 if (valout == NULL) {
6161 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
6165 if (KEY_IS(KEY_FID2PATH))
6166 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
6170 lustre_msg_set_status(req->rq_repmsg, rc);
6175 /* Pass the ioc down */
6176 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
6177 unsigned int cmd, int len, void *data)
6179 struct lu_context ioctl_session;
6180 struct md_device *next = mdt->mdt_child;
6184 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
6187 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
6188 lu_context_enter(&ioctl_session);
6189 env->le_ses = &ioctl_session;
6191 LASSERT(next->md_ops->mdo_iocontrol);
6192 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
6194 lu_context_exit(&ioctl_session);
6195 lu_context_fini(&ioctl_session);
6199 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6201 struct obd_ioctl_data *data = karg;
6204 struct mdt_object *obj;
6205 struct mdt_lock_handle *lh;
6209 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6210 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6213 fid = (struct lu_fid *)data->ioc_inlbuf1;
6215 if (!fid_is_sane(fid))
6218 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6220 lh = &mti->mti_lh[MDT_LH_PARENT];
6221 mdt_lock_reg_init(lh, LCK_CR);
6223 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6225 RETURN(PTR_ERR(obj));
6227 if (mdt_object_remote(obj)) {
6230 * before calling version get the correct MDS should be
6231 * fid, this is error to find remote object here
6233 CERROR("nonlocal object "DFID"\n", PFID(fid));
6234 } else if (!mdt_object_exists(obj)) {
6235 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6238 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6239 *(__u64 *)data->ioc_inlbuf2 = version;
6242 mdt_object_unlock_put(mti, obj, lh, 1);
6246 /* ioctls on obd dev */
6247 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6248 void *karg, void *uarg)
6251 struct obd_device *obd = exp->exp_obd;
6252 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6253 struct dt_device *dt = mdt->mdt_bottom;
6257 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6258 rc = lu_env_init(&env, LCT_MD_THREAD);
6264 rc = mdt_device_sync(&env, mdt);
6266 case OBD_IOC_SET_READONLY:
6267 rc = dt->dd_ops->dt_ro(&env, dt);
6269 case OBD_IOC_ABORT_RECOVERY:
6270 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6271 target_stop_recovery_thread(obd);
6274 case OBD_IOC_CHANGELOG_REG:
6275 case OBD_IOC_CHANGELOG_DEREG:
6276 case OBD_IOC_CHANGELOG_CLEAR:
6277 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6279 case OBD_IOC_START_LFSCK: {
6280 struct md_device *next = mdt->mdt_child;
6281 struct obd_ioctl_data *data = karg;
6282 struct lfsck_start_param lsp;
6284 if (unlikely(data == NULL)) {
6289 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6290 lsp.lsp_namespace = mdt->mdt_namespace;
6291 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6294 case OBD_IOC_STOP_LFSCK: {
6295 struct md_device *next = mdt->mdt_child;
6297 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
6300 case OBD_IOC_GET_OBJ_VERSION: {
6301 struct mdt_thread_info *mti;
6302 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6303 memset(mti, 0, sizeof *mti);
6304 mti->mti_env = &env;
6308 rc = mdt_ioc_version_get(mti, karg);
6313 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6314 mdt_obd_name(mdt), cmd, rc);
6321 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6323 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6327 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6331 int mdt_obd_postrecov(struct obd_device *obd)
6336 rc = lu_env_init(&env, LCT_MD_THREAD);
6339 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6344 static struct obd_ops mdt_obd_device_ops = {
6345 .o_owner = THIS_MODULE,
6346 .o_set_info_async = mdt_obd_set_info_async,
6347 .o_connect = mdt_obd_connect,
6348 .o_reconnect = mdt_obd_reconnect,
6349 .o_disconnect = mdt_obd_disconnect,
6350 .o_init_export = mdt_init_export,
6351 .o_destroy_export = mdt_destroy_export,
6352 .o_iocontrol = mdt_iocontrol,
6353 .o_postrecov = mdt_obd_postrecov,
6356 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6357 struct lu_device *d)
6359 struct mdt_device *m = mdt_dev(d);
6366 static struct lu_device *mdt_device_free(const struct lu_env *env,
6367 struct lu_device *d)
6369 struct mdt_device *m = mdt_dev(d);
6372 lu_device_fini(&m->mdt_lu_dev);
6378 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6379 struct lu_device_type *t,
6380 struct lustre_cfg *cfg)
6382 struct lu_device *l;
6383 struct mdt_device *m;
6390 rc = mdt_init0(env, m, t, cfg);
6392 mdt_device_free(env, l);
6397 l = ERR_PTR(-ENOMEM);
6401 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6402 LU_KEY_INIT(mdt, struct mdt_thread_info);
6404 static void mdt_key_fini(const struct lu_context *ctx,
6405 struct lu_context_key *key, void* data)
6407 struct mdt_thread_info *info = data;
6409 if (info->mti_big_lmm) {
6410 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6411 info->mti_big_lmm = NULL;
6412 info->mti_big_lmmsize = 0;
6417 /* context key: mdt_thread_key */
6418 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6420 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6422 return lu_ucred(info->mti_env);
6425 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6427 return lu_ucred_check(info->mti_env);
6431 * Enable/disable COS (Commit On Sharing).
6433 * Set/Clear the COS flag in mdt options.
6435 * \param mdt mdt device
6436 * \param val 0 disables COS, other values enable COS
6438 void mdt_enable_cos(struct mdt_device *mdt, int val)
6443 mdt->mdt_opts.mo_cos = !!val;
6444 rc = lu_env_init(&env, LCT_LOCAL);
6445 if (unlikely(rc != 0)) {
6446 CWARN("lu_env initialization failed with rc = %d,"
6447 "cannot sync\n", rc);
6450 mdt_device_sync(&env, mdt);
6455 * Check COS (Commit On Sharing) status.
6457 * Return COS flag status.
6459 * \param mdt mdt device
6461 int mdt_cos_is_enabled(struct mdt_device *mdt)
6463 return mdt->mdt_opts.mo_cos != 0;
6466 static struct lu_device_type_operations mdt_device_type_ops = {
6467 .ldto_device_alloc = mdt_device_alloc,
6468 .ldto_device_free = mdt_device_free,
6469 .ldto_device_fini = mdt_device_fini
6472 static struct lu_device_type mdt_device_type = {
6473 .ldt_tags = LU_DEVICE_MD,
6474 .ldt_name = LUSTRE_MDT_NAME,
6475 .ldt_ops = &mdt_device_type_ops,
6476 .ldt_ctx_tags = LCT_MD_THREAD
6479 static int __init mdt_mod_init(void)
6481 struct lprocfs_static_vars lvars;
6484 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6485 FID_NOBRACE_LEN + 1);
6486 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6488 rc = lu_kmem_init(mdt_caches);
6492 rc = mds_mod_init();
6496 lprocfs_mdt_init_vars(&lvars);
6497 rc = class_register_type(&mdt_obd_device_ops, NULL,
6498 lvars.module_vars, LUSTRE_MDT_NAME,
6504 lu_kmem_fini(mdt_caches);
6511 static void __exit mdt_mod_exit(void)
6513 class_unregister_type(LUSTRE_MDT_NAME);
6515 lu_kmem_fini(mdt_caches);
6518 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6519 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6520 MODULE_LICENSE("GPL");
6522 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);