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 static struct lu_kmem_descr mdt_caches[] = {
105 .ckd_cache = &mdt_object_kmem,
106 .ckd_name = "mdt_obj",
107 .ckd_size = sizeof(struct mdt_object)
114 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
118 return (rep->lock_policy_res1 & flag);
121 void mdt_clear_disposition(struct mdt_thread_info *info,
122 struct ldlm_reply *rep, int flag)
125 info->mti_opdata &= ~flag;
127 rep->lock_policy_res1 &= ~flag;
130 void mdt_set_disposition(struct mdt_thread_info *info,
131 struct ldlm_reply *rep, int flag)
134 info->mti_opdata |= flag;
136 rep->lock_policy_res1 |= flag;
139 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
141 lh->mlh_pdo_hash = 0;
142 lh->mlh_reg_mode = lm;
143 lh->mlh_rreg_mode = lm;
144 lh->mlh_type = MDT_REG_LOCK;
147 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
148 const char *name, int namelen)
150 lh->mlh_reg_mode = lm;
151 lh->mlh_rreg_mode = lm;
152 lh->mlh_type = MDT_PDO_LOCK;
154 if (name != NULL && (name[0] != '\0')) {
155 LASSERT(namelen > 0);
156 lh->mlh_pdo_hash = full_name_hash(name, namelen);
157 /* XXX Workaround for LU-2856
158 * Zero is a valid return value of full_name_hash, but several
159 * users of mlh_pdo_hash assume a non-zero hash value. We
160 * therefore map zero onto an arbitrary, but consistent
161 * value (1) to avoid problems further down the road. */
162 if (unlikely(!lh->mlh_pdo_hash))
163 lh->mlh_pdo_hash = 1;
165 LASSERT(namelen == 0);
166 lh->mlh_pdo_hash = 0ull;
170 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
171 struct mdt_lock_handle *lh)
177 * Any dir access needs couple of locks:
179 * 1) on part of dir we gonna take lookup/modify;
181 * 2) on whole dir to protect it from concurrent splitting and/or to
182 * flush client's cache for readdir().
184 * so, for a given mode and object this routine decides what lock mode
185 * to use for lock #2:
187 * 1) if caller's gonna lookup in dir then we need to protect dir from
188 * being splitted only - LCK_CR
190 * 2) if caller's gonna modify dir then we need to protect dir from
191 * being splitted and to flush cache - LCK_CW
193 * 3) if caller's gonna modify dir and that dir seems ready for
194 * splitting then we need to protect it from any type of access
195 * (lookup/modify/split) - LCK_EX --bzzz
198 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
199 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
202 * Ask underlaying level its opinion about preferable PDO lock mode
203 * having access type passed as regular lock mode:
205 * - MDL_MINMODE means that lower layer does not want to specify lock
208 * - MDL_NL means that no PDO lock should be taken. This is used in some
209 * cases. Say, for non-splittable directories no need to use PDO locks
212 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
213 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
215 if (mode != MDL_MINMODE) {
216 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
219 * Lower layer does not want to specify locking mode. We do it
220 * our selves. No special protection is needed, just flush
221 * client's cache on modification and allow concurrent
224 switch (lh->mlh_reg_mode) {
226 lh->mlh_pdo_mode = LCK_EX;
229 lh->mlh_pdo_mode = LCK_CR;
232 lh->mlh_pdo_mode = LCK_CW;
235 CERROR("Not expected lock type (0x%x)\n",
236 (int)lh->mlh_reg_mode);
241 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
245 int mdt_getstatus(struct mdt_thread_info *info)
247 struct mdt_device *mdt = info->mti_mdt;
248 struct mdt_body *repbody;
252 rc = mdt_check_ucred(info);
254 RETURN(err_serious(rc));
256 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
257 RETURN(err_serious(-ENOMEM));
259 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
260 repbody->fid1 = mdt->mdt_md_root_fid;
261 repbody->valid |= OBD_MD_FLID;
263 if (mdt->mdt_opts.mo_mds_capa &&
264 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
265 struct mdt_object *root;
266 struct lustre_capa *capa;
268 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
270 RETURN(PTR_ERR(root));
272 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
274 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
275 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
277 mdt_object_put(info->mti_env, root);
279 repbody->valid |= OBD_MD_FLMDSCAPA;
285 int mdt_statfs(struct mdt_thread_info *info)
287 struct ptlrpc_request *req = mdt_info_req(info);
288 struct md_device *next = info->mti_mdt->mdt_child;
289 struct ptlrpc_service_part *svcpt;
290 struct obd_statfs *osfs;
295 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
297 /* This will trigger a watchdog timeout */
298 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
299 (MDT_SERVICE_WATCHDOG_FACTOR *
300 at_get(&svcpt->scp_at_estimate)) + 1);
302 rc = mdt_check_ucred(info);
304 RETURN(err_serious(rc));
306 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
307 RETURN(err_serious(-ENOMEM));
309 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
313 /** statfs information are cached in the mdt_device */
314 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
315 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
316 /** statfs data is too old, get up-to-date one */
317 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
320 spin_lock(&info->mti_mdt->mdt_osfs_lock);
321 info->mti_mdt->mdt_osfs = *osfs;
322 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
323 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
325 /** use cached statfs data */
326 spin_lock(&info->mti_mdt->mdt_osfs_lock);
327 *osfs = info->mti_mdt->mdt_osfs;
328 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
332 mdt_counter_incr(req, LPROC_MDT_STATFS);
338 * Pack SOM attributes into the reply.
339 * Call under a DLM UPDATE lock.
341 static void mdt_pack_size2body(struct mdt_thread_info *info,
342 struct mdt_object *mo)
345 struct md_attr *ma = &info->mti_attr;
347 LASSERT(ma->ma_attr.la_valid & LA_MODE);
348 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
350 /* Check if Size-on-MDS is supported, if this is a regular file,
351 * if SOM is enabled on the object and if SOM cache exists and valid.
352 * Otherwise do not pack Size-on-MDS attributes to the reply. */
353 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
354 !S_ISREG(ma->ma_attr.la_mode) ||
355 !mdt_object_is_som_enabled(mo) ||
356 !(ma->ma_valid & MA_SOM))
359 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
360 b->size = ma->ma_som->msd_size;
361 b->blocks = ma->ma_som->msd_blocks;
364 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
365 const struct lu_attr *attr, const struct lu_fid *fid)
367 struct md_attr *ma = &info->mti_attr;
369 LASSERT(ma->ma_valid & MA_INODE);
371 b->atime = attr->la_atime;
372 b->mtime = attr->la_mtime;
373 b->ctime = attr->la_ctime;
374 b->mode = attr->la_mode;
375 b->size = attr->la_size;
376 b->blocks = attr->la_blocks;
377 b->uid = attr->la_uid;
378 b->gid = attr->la_gid;
379 b->flags = attr->la_flags;
380 b->nlink = attr->la_nlink;
381 b->rdev = attr->la_rdev;
383 /*XXX should pack the reply body according to lu_valid*/
384 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
385 OBD_MD_FLGID | OBD_MD_FLTYPE |
386 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
387 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
389 if (!S_ISREG(attr->la_mode)) {
390 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
391 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
392 /* means no objects are allocated on osts. */
393 LASSERT(!(ma->ma_valid & MA_LOV));
394 /* just ignore blocks occupied by extend attributes on MDS */
396 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
397 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
398 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm &&
399 (ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
400 /* A released file stores its size on MDS. */
402 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
407 b->valid |= OBD_MD_FLID;
408 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
409 PFID(fid), b->nlink, b->mode, b->size);
413 mdt_body_reverse_idmap(info, b);
415 if (b->valid & OBD_MD_FLSIZE)
416 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
417 PFID(fid), (unsigned long long)b->size);
420 static inline int mdt_body_has_lov(const struct lu_attr *la,
421 const struct mdt_body *body)
423 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
424 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
427 void mdt_client_compatibility(struct mdt_thread_info *info)
429 struct mdt_body *body;
430 struct ptlrpc_request *req = mdt_info_req(info);
431 struct obd_export *exp = req->rq_export;
432 struct md_attr *ma = &info->mti_attr;
433 struct lu_attr *la = &ma->ma_attr;
436 if (exp_connect_layout(exp))
437 /* the client can deal with 16-bit lmm_stripe_count */
440 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
442 if (!mdt_body_has_lov(la, body))
445 /* now we have a reply with a lov for a client not compatible with the
446 * layout lock so we have to clean the layout generation number */
447 if (S_ISREG(la->la_mode))
448 ma->ma_lmm->lmm_layout_gen = 0;
452 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
455 const struct lu_env *env = info->mti_env;
459 LASSERT(info->mti_big_lmm_used == 0);
460 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
464 /* big_lmm may need to be grown */
465 if (info->mti_big_lmmsize < rc) {
466 int size = size_roundup_power2(rc);
468 if (info->mti_big_lmmsize > 0) {
469 /* free old buffer */
470 LASSERT(info->mti_big_lmm);
471 OBD_FREE_LARGE(info->mti_big_lmm,
472 info->mti_big_lmmsize);
473 info->mti_big_lmm = NULL;
474 info->mti_big_lmmsize = 0;
477 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
478 if (info->mti_big_lmm == NULL)
480 info->mti_big_lmmsize = size;
482 LASSERT(info->mti_big_lmmsize >= rc);
484 info->mti_buf.lb_buf = info->mti_big_lmm;
485 info->mti_buf.lb_len = info->mti_big_lmmsize;
486 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
491 int mdt_attr_get_lov(struct mdt_thread_info *info,
492 struct mdt_object *o, struct md_attr *ma)
494 struct md_object *next = mdt_object_child(o);
495 struct lu_buf *buf = &info->mti_buf;
498 buf->lb_buf = ma->ma_lmm;
499 buf->lb_len = ma->ma_lmm_size;
500 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
502 ma->ma_lmm_size = rc;
503 ma->ma_valid |= MA_LOV;
505 } else if (rc == -ENODATA) {
508 } else if (rc == -ERANGE) {
509 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
511 info->mti_big_lmm_used = 1;
512 ma->ma_valid |= MA_LOV;
513 ma->ma_lmm = info->mti_big_lmm;
514 ma->ma_lmm_size = rc;
515 /* update mdt_max_mdsize so all clients
516 * will be aware about that */
517 if (info->mti_mdt->mdt_max_mdsize < rc)
518 info->mti_mdt->mdt_max_mdsize = rc;
526 int mdt_attr_get_pfid(struct mdt_thread_info *info,
527 struct mdt_object *o, struct lu_fid *pfid)
529 struct lu_buf *buf = &info->mti_buf;
530 struct link_ea_header *leh;
531 struct link_ea_entry *lee;
535 buf->lb_buf = info->mti_big_lmm;
536 buf->lb_len = info->mti_big_lmmsize;
537 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
538 buf, XATTR_NAME_LINK);
539 /* ignore errors, MA_PFID won't be set and it is
540 * up to the caller to treat this as an error */
541 if (rc == -ERANGE || buf->lb_len == 0) {
542 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
543 buf->lb_buf = info->mti_big_lmm;
544 buf->lb_len = info->mti_big_lmmsize;
549 if (rc < sizeof(*leh)) {
550 CERROR("short LinkEA on "DFID": rc = %d\n",
551 PFID(mdt_object_fid(o)), rc);
555 leh = (struct link_ea_header *) buf->lb_buf;
556 lee = (struct link_ea_entry *)(leh + 1);
557 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
558 leh->leh_magic = LINK_EA_MAGIC;
559 leh->leh_reccount = __swab32(leh->leh_reccount);
560 leh->leh_len = __swab64(leh->leh_len);
562 if (leh->leh_magic != LINK_EA_MAGIC)
564 if (leh->leh_reccount == 0)
567 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
568 fid_be_to_cpu(pfid, pfid);
573 int mdt_attr_get_complex(struct mdt_thread_info *info,
574 struct mdt_object *o, struct md_attr *ma)
576 const struct lu_env *env = info->mti_env;
577 struct md_object *next = mdt_object_child(o);
578 struct lu_buf *buf = &info->mti_buf;
579 u32 mode = lu_object_attr(&next->mo_lu);
580 int need = ma->ma_need;
586 if (need & MA_INODE) {
587 ma->ma_need = MA_INODE;
588 rc = mo_attr_get(env, next, ma);
591 ma->ma_valid |= MA_INODE;
594 if (need & MA_PFID) {
595 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
597 ma->ma_valid |= MA_PFID;
598 /* ignore this error, parent fid is not mandatory */
602 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
603 rc = mdt_attr_get_lov(info, o, ma);
608 if (need & MA_LMV && S_ISDIR(mode)) {
609 buf->lb_buf = ma->ma_lmv;
610 buf->lb_len = ma->ma_lmv_size;
611 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
613 ma->ma_lmv_size = rc2;
614 ma->ma_valid |= MA_LMV;
615 } else if (rc2 == -ENODATA) {
622 if (need & MA_SOM && S_ISREG(mode)) {
623 buf->lb_buf = info->mti_xattr_buf;
624 buf->lb_len = sizeof(info->mti_xattr_buf);
625 CLASSERT(sizeof(struct som_attrs) <=
626 sizeof(info->mti_xattr_buf));
627 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
628 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
630 ma->ma_valid |= MA_SOM;
631 else if (rc2 < 0 && rc2 != -ENODATA)
635 if (need & MA_HSM && S_ISREG(mode)) {
636 buf->lb_buf = info->mti_xattr_buf;
637 buf->lb_len = sizeof(info->mti_xattr_buf);
638 CLASSERT(sizeof(struct hsm_attrs) <=
639 sizeof(info->mti_xattr_buf));
640 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
641 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
643 ma->ma_valid |= MA_HSM;
644 else if (rc2 < 0 && rc2 != -ENODATA)
648 #ifdef CONFIG_FS_POSIX_ACL
649 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
650 buf->lb_buf = ma->ma_acl;
651 buf->lb_len = ma->ma_acl_size;
652 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
654 ma->ma_acl_size = rc2;
655 ma->ma_valid |= MA_ACL_DEF;
656 } else if (rc2 == -ENODATA) {
665 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
666 rc, ma->ma_valid, ma->ma_lmm);
670 static int mdt_getattr_internal(struct mdt_thread_info *info,
671 struct mdt_object *o, int ma_need)
673 struct md_object *next = mdt_object_child(o);
674 const struct mdt_body *reqbody = info->mti_body;
675 struct ptlrpc_request *req = mdt_info_req(info);
676 struct md_attr *ma = &info->mti_attr;
677 struct lu_attr *la = &ma->ma_attr;
678 struct req_capsule *pill = info->mti_pill;
679 const struct lu_env *env = info->mti_env;
680 struct mdt_body *repbody;
681 struct lu_buf *buffer = &info->mti_buf;
686 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
687 RETURN(err_serious(-ENOMEM));
689 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
693 if (mdt_object_remote(o)) {
694 /* This object is located on remote node.*/
695 /* Return -EIO for old client */
696 if (!mdt_is_dne_client(req->rq_export))
697 GOTO(out, rc = -EIO);
699 repbody->fid1 = *mdt_object_fid(o);
700 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
704 buffer->lb_len = reqbody->eadatasize;
705 if (buffer->lb_len > 0)
706 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
708 buffer->lb_buf = NULL;
710 /* If it is dir object and client require MEA, then we got MEA */
711 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
712 reqbody->valid & OBD_MD_MEA) {
713 /* Assumption: MDT_MD size is enough for lmv size. */
714 ma->ma_lmv = buffer->lb_buf;
715 ma->ma_lmv_size = buffer->lb_len;
716 ma->ma_need = MA_LMV | MA_INODE;
718 ma->ma_lmm = buffer->lb_buf;
719 ma->ma_lmm_size = buffer->lb_len;
720 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
723 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
724 reqbody->valid & OBD_MD_FLDIREA &&
725 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
726 /* get default stripe info for this dir. */
727 ma->ma_need |= MA_LOV_DEF;
729 ma->ma_need |= ma_need;
730 if (ma->ma_need & MA_SOM)
731 ma->ma_som = &info->mti_u.som.data;
733 rc = mdt_attr_get_complex(info, o, ma);
735 CERROR("%s: getattr error for "DFID": rc = %d\n",
736 mdt_obd_name(info->mti_mdt),
737 PFID(mdt_object_fid(o)), rc);
741 /* if file is released, check if a restore is running */
742 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
743 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
744 repbody->t_state = MS_RESTORE;
745 repbody->valid |= OBD_MD_TSTATE;
748 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
750 /* the Lustre protocol supposes to return default striping
751 * on the user-visible root if explicitly requested */
752 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
753 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
754 struct lu_fid rootfid;
755 struct mdt_object *root;
756 struct mdt_device *mdt = info->mti_mdt;
758 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
761 root = mdt_object_find(env, mdt, &rootfid);
763 RETURN(PTR_ERR(root));
764 rc = mdt_attr_get_lov(info, root, ma);
765 mdt_object_put(info->mti_env, root);
767 CERROR("%s: getattr error for "DFID": rc = %d\n",
768 mdt_obd_name(info->mti_mdt),
769 PFID(mdt_object_fid(o)), rc);
774 if (likely(ma->ma_valid & MA_INODE))
775 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
779 if (mdt_body_has_lov(la, reqbody)) {
780 if (ma->ma_valid & MA_LOV) {
781 LASSERT(ma->ma_lmm_size);
782 mdt_dump_lmm(D_INFO, ma->ma_lmm);
783 repbody->eadatasize = ma->ma_lmm_size;
784 if (S_ISDIR(la->la_mode))
785 repbody->valid |= OBD_MD_FLDIREA;
787 repbody->valid |= OBD_MD_FLEASIZE;
789 if (ma->ma_valid & MA_LMV) {
790 LASSERT(S_ISDIR(la->la_mode));
791 repbody->eadatasize = ma->ma_lmv_size;
792 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
794 } else if (S_ISLNK(la->la_mode) &&
795 reqbody->valid & OBD_MD_LINKNAME) {
796 buffer->lb_buf = ma->ma_lmm;
797 /* eadatasize from client includes NULL-terminator, so
798 * there is no need to read it */
799 buffer->lb_len = reqbody->eadatasize - 1;
800 rc = mo_readlink(env, next, buffer);
801 if (unlikely(rc <= 0)) {
802 CERROR("%s: readlink failed for "DFID": rc = %d\n",
803 mdt_obd_name(info->mti_mdt),
804 PFID(mdt_object_fid(o)), rc);
807 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
809 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
811 repbody->valid |= OBD_MD_LINKNAME;
812 /* we need to report back size with NULL-terminator
813 * because client expects that */
814 repbody->eadatasize = rc + 1;
815 if (repbody->eadatasize != reqbody->eadatasize)
816 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
817 "on "DFID ", expected %d\n",
818 mdt_obd_name(info->mti_mdt),
819 rc, PFID(mdt_object_fid(o)),
820 reqbody->eadatasize - 1);
822 ((char *)ma->ma_lmm)[rc] = 0;
824 /* If the total CDEBUG() size is larger than a page, it
825 * will print a warning to the console, avoid this by
826 * printing just the last part of the symlink. */
827 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
828 print_limit < rc ? "..." : "", print_limit,
829 (char *)ma->ma_lmm + rc - print_limit, rc);
834 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
835 repbody->max_cookiesize = 0;
836 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
837 repbody->valid |= OBD_MD_FLMODEASIZE;
838 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
839 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
840 repbody->max_cookiesize);
843 if (exp_connect_rmtclient(info->mti_exp) &&
844 reqbody->valid & OBD_MD_FLRMTPERM) {
845 void *buf = req_capsule_server_get(pill, &RMF_ACL);
847 /* mdt_getattr_lock only */
848 rc = mdt_pack_remote_perm(info, o, buf);
850 repbody->valid &= ~OBD_MD_FLRMTPERM;
851 repbody->aclsize = 0;
854 repbody->valid |= OBD_MD_FLRMTPERM;
855 repbody->aclsize = sizeof(struct mdt_remote_perm);
858 #ifdef CONFIG_FS_POSIX_ACL
859 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
860 (reqbody->valid & OBD_MD_FLACL)) {
861 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
862 buffer->lb_len = req_capsule_get_size(pill,
863 &RMF_ACL, RCL_SERVER);
864 if (buffer->lb_len > 0) {
865 rc = mo_xattr_get(env, next, buffer,
866 XATTR_NAME_ACL_ACCESS);
868 if (rc == -ENODATA) {
869 repbody->aclsize = 0;
870 repbody->valid |= OBD_MD_FLACL;
872 } else if (rc == -EOPNOTSUPP) {
875 CERROR("%s: unable to read "DFID
877 mdt_obd_name(info->mti_mdt),
878 PFID(mdt_object_fid(o)), rc);
881 repbody->aclsize = rc;
882 repbody->valid |= OBD_MD_FLACL;
889 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
890 info->mti_mdt->mdt_opts.mo_mds_capa &&
891 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
892 struct lustre_capa *capa;
894 capa = req_capsule_server_get(pill, &RMF_CAPA1);
896 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
897 rc = mo_capa_get(env, next, capa, 0);
900 repbody->valid |= OBD_MD_FLMDSCAPA;
905 mdt_counter_incr(req, LPROC_MDT_GETATTR);
910 static int mdt_renew_capa(struct mdt_thread_info *info)
912 struct mdt_object *obj = info->mti_object;
913 struct mdt_body *body;
914 struct lustre_capa *capa, *c;
918 /* if object doesn't exist, or server has disabled capability,
919 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
922 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
923 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
926 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
927 LASSERT(body != NULL);
929 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
932 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
936 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
938 body->valid |= OBD_MD_FLOSSCAPA;
942 int mdt_getattr(struct mdt_thread_info *info)
944 struct mdt_object *obj = info->mti_object;
945 struct req_capsule *pill = info->mti_pill;
946 struct mdt_body *reqbody;
947 struct mdt_body *repbody;
952 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
955 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
956 rc = req_capsule_server_pack(pill);
958 RETURN(err_serious(rc));
959 rc = mdt_renew_capa(info);
960 GOTO(out_shrink, rc);
963 LASSERT(obj != NULL);
964 LASSERT(lu_object_assert_exists(&obj->mot_obj));
966 mode = lu_object_attr(&obj->mot_obj);
968 /* old clients may not report needed easize, use max value then */
969 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
970 reqbody->eadatasize == 0 ?
971 info->mti_mdt->mdt_max_mdsize :
972 reqbody->eadatasize);
974 rc = req_capsule_server_pack(pill);
975 if (unlikely(rc != 0))
976 RETURN(err_serious(rc));
978 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
979 LASSERT(repbody != NULL);
980 repbody->eadatasize = 0;
981 repbody->aclsize = 0;
983 if (reqbody->valid & OBD_MD_FLRMTPERM)
984 rc = mdt_init_ucred(info, reqbody);
986 rc = mdt_check_ucred(info);
988 GOTO(out_shrink, rc);
990 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
993 * Don't check capability at all, because rename might getattr for
994 * remote obj, and at that time no capability is available.
996 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
997 rc = mdt_getattr_internal(info, obj, 0);
998 if (reqbody->valid & OBD_MD_FLRMTPERM)
999 mdt_exit_ucred(info);
1002 mdt_client_compatibility(info);
1003 rc2 = mdt_fix_reply(info);
1009 int mdt_is_subdir(struct mdt_thread_info *info)
1011 struct mdt_object *o = info->mti_object;
1012 struct req_capsule *pill = info->mti_pill;
1013 const struct mdt_body *body = info->mti_body;
1014 struct mdt_body *repbody;
1020 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1023 * We save last checked parent fid to @repbody->fid1 for remote
1026 LASSERT(fid_is_sane(&body->fid2));
1027 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1028 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1029 &body->fid2, &repbody->fid1);
1030 if (rc == 0 || rc == -EREMOTE)
1031 repbody->valid |= OBD_MD_FLID;
1036 int mdt_swap_layouts(struct mdt_thread_info *info)
1038 struct ptlrpc_request *req = mdt_info_req(info);
1039 struct obd_export *exp = req->rq_export;
1040 struct mdt_object *o1, *o2, *o;
1041 struct mdt_lock_handle *lh1, *lh2;
1042 struct mdc_swap_layouts *msl;
1046 /* client does not support layout lock, so layout swaping
1048 * FIXME: there is a problem for old clients which don't support
1049 * layout lock yet. If those clients have already opened the file
1050 * they won't be notified at all so that old layout may still be
1051 * used to do IO. This can be fixed after file release is landed by
1052 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1053 if (!exp_connect_layout(exp))
1054 RETURN(-EOPNOTSUPP);
1056 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1057 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1058 req_capsule_client_get(info->mti_pill,
1061 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1062 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1063 req_capsule_client_get(info->mti_pill,
1066 o1 = info->mti_object;
1067 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1068 &info->mti_body->fid2);
1070 GOTO(out, rc = PTR_ERR(o));
1072 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1073 GOTO(put, rc = -ENOENT);
1075 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1076 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1082 /* permission check. Make sure the calling process having permission
1083 * to write both files. */
1084 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1089 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1094 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1096 GOTO(put, rc = -EPROTO);
1098 lh1 = &info->mti_lh[MDT_LH_NEW];
1099 mdt_lock_reg_init(lh1, LCK_EX);
1100 lh2 = &info->mti_lh[MDT_LH_OLD];
1101 mdt_lock_reg_init(lh2, LCK_EX);
1103 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT,
1108 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT,
1113 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1114 mdt_object_child(o2), msl->msl_flags);
1117 mdt_object_unlock(info, o2, lh2, rc);
1119 mdt_object_unlock(info, o1, lh1, rc);
1121 mdt_object_put(info->mti_env, o);
1126 static int mdt_raw_lookup(struct mdt_thread_info *info,
1127 struct mdt_object *parent,
1128 const struct lu_name *lname,
1129 struct ldlm_reply *ldlm_rep)
1131 struct md_object *next = mdt_object_child(info->mti_object);
1132 const struct mdt_body *reqbody = info->mti_body;
1133 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1134 struct mdt_body *repbody;
1138 if (reqbody->valid != OBD_MD_FLID)
1141 LASSERT(!info->mti_cross_ref);
1143 /* Only got the fid of this obj by name */
1144 fid_zero(child_fid);
1145 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1148 /* XXX is raw_lookup possible as intent operation? */
1151 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1154 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1156 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1159 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1160 repbody->fid1 = *child_fid;
1161 repbody->valid = OBD_MD_FLID;
1167 * UPDATE lock should be taken against parent, and be release before exit;
1168 * child_bits lock should be taken against child, and be returned back:
1169 * (1)normal request should release the child lock;
1170 * (2)intent request will grant the lock to client.
1172 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1173 struct mdt_lock_handle *lhc,
1175 struct ldlm_reply *ldlm_rep)
1177 struct ptlrpc_request *req = mdt_info_req(info);
1178 struct mdt_body *reqbody = NULL;
1179 struct mdt_object *parent = info->mti_object;
1180 struct mdt_object *child;
1181 struct md_object *next = mdt_object_child(parent);
1182 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1183 struct lu_name *lname = NULL;
1184 const char *name = NULL;
1186 struct mdt_lock_handle *lhp = NULL;
1187 struct ldlm_lock *lock;
1188 struct ldlm_res_id *res_id;
1195 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1196 LASSERT(ergo(is_resent,
1197 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1199 LASSERT(parent != NULL);
1200 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1202 RETURN(err_serious(-EFAULT));
1204 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1206 if (!info->mti_cross_ref) {
1208 * XXX: Check for "namelen == 0" is for getattr by fid
1209 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1210 * that is the name must contain at least one character and
1211 * the terminating '\0'
1214 reqbody = req_capsule_client_get(info->mti_pill,
1216 if (unlikely(reqbody == NULL))
1217 RETURN(err_serious(-EFAULT));
1219 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1220 RETURN(err_serious(-EINVAL));
1223 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1225 PFID(mdt_object_fid(parent)),
1226 PFID(&reqbody->fid2), ldlm_rep);
1228 lname = mdt_name(info->mti_env, (char *)name, namelen);
1229 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1230 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1234 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1236 if (unlikely(!mdt_object_exists(parent))) {
1237 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1239 "Parent doesn't exist!\n");
1241 } else if (!info->mti_cross_ref) {
1242 LASSERTF(!mdt_object_remote(parent),
1243 "Parent "DFID" is on remote server\n",
1244 PFID(mdt_object_fid(parent)));
1247 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1255 if (info->mti_cross_ref) {
1256 /* Only getattr on the child. Parent is on another node. */
1257 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1259 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1260 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1263 /* Do not take lock for resent case. */
1264 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1265 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1266 lhc->mlh_reg_lh.cookie);
1267 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1268 &lock->l_resource->lr_name));
1269 LDLM_LOCK_PUT(lock);
1272 mdt_lock_handle_init(lhc);
1273 mdt_lock_reg_init(lhc, LCK_PR);
1276 * Object's name is on another MDS, no lookup lock is
1277 * needed here but update is.
1279 child_bits &= ~MDS_INODELOCK_LOOKUP;
1280 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1282 rc = mdt_object_lock(info, child, lhc, child_bits,
1286 /* Finally, we can get attr for child. */
1287 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1289 rc = mdt_getattr_internal(info, child, 0);
1290 if (unlikely(rc != 0))
1291 mdt_object_unlock(info, child, lhc, 1);
1297 /* step 1: lock parent only if parent is a directory */
1298 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1299 lhp = &info->mti_lh[MDT_LH_PARENT];
1300 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1301 rc = mdt_object_lock(info, parent, lhp,
1302 MDS_INODELOCK_UPDATE,
1304 if (unlikely(rc != 0))
1308 /* step 2: lookup child's fid by name */
1309 fid_zero(child_fid);
1310 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1315 mdt_set_disposition(info, ldlm_rep,
1317 GOTO(out_parent, rc);
1319 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1321 *child_fid = reqbody->fid2;
1322 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1326 *step 3: find the child object by fid & lock it.
1327 * regardless if it is local or remote.
1329 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1331 if (unlikely(IS_ERR(child)))
1332 GOTO(out_parent, rc = PTR_ERR(child));
1334 /* Do not take lock for resent case. */
1335 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1336 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1337 lhc->mlh_reg_lh.cookie);
1339 res_id = &lock->l_resource->lr_name;
1340 if (!fid_res_name_eq(mdt_object_fid(child),
1341 &lock->l_resource->lr_name)) {
1342 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1343 &lock->l_resource->lr_name),
1344 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1345 PLDLMRES(lock->l_resource),
1346 PFID(mdt_object_fid(parent)));
1347 CWARN("Although resent, but still not get child lock"
1348 "parent:"DFID" child:"DFID"\n",
1349 PFID(mdt_object_fid(parent)),
1350 PFID(mdt_object_fid(child)));
1351 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1352 LDLM_LOCK_PUT(lock);
1355 LDLM_LOCK_PUT(lock);
1358 bool try_layout = false;
1361 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1362 mdt_lock_handle_init(lhc);
1363 mdt_lock_reg_init(lhc, LCK_PR);
1365 if (!mdt_object_exists(child)) {
1366 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1368 "Object doesn't exist!\n");
1369 GOTO(out_child, rc = -ENOENT);
1372 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1373 mdt_object_exists(child) && !mdt_object_remote(child)) {
1374 struct md_attr *ma = &info->mti_attr;
1377 ma->ma_need = MA_INODE;
1378 rc = mdt_attr_get_complex(info, child, ma);
1379 if (unlikely(rc != 0))
1380 GOTO(out_child, rc);
1382 /* If the file has not been changed for some time, we
1383 * return not only a LOOKUP lock, but also an UPDATE
1384 * lock and this might save us RPC on later STAT. For
1385 * directories, it also let negative dentry starts
1386 * working for this dir. */
1387 if (ma->ma_valid & MA_INODE &&
1388 ma->ma_attr.la_valid & LA_CTIME &&
1389 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1390 ma->ma_attr.la_ctime < cfs_time_current_sec())
1391 child_bits |= MDS_INODELOCK_UPDATE;
1394 /* layout lock must be granted in a best-effort way
1395 * for IT operations */
1396 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1397 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1398 exp_connect_layout(info->mti_exp) &&
1399 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1401 /* try to grant layout lock for regular file. */
1407 child_bits |= MDS_INODELOCK_LAYOUT;
1408 /* try layout lock, it may fail to be granted due to
1409 * contention at LOOKUP or UPDATE */
1410 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1412 child_bits &= ~MDS_INODELOCK_LAYOUT;
1413 LASSERT(child_bits != 0);
1414 rc = mdt_object_lock(info, child, lhc,
1415 child_bits, MDT_CROSS_LOCK);
1420 rc = mdt_object_lock(info, child, lhc, child_bits,
1423 if (unlikely(rc != 0))
1424 GOTO(out_child, rc);
1427 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1428 /* Get MA_SOM attributes if update lock is given. */
1430 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1431 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1434 /* finally, we can get attr for child. */
1435 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1436 rc = mdt_getattr_internal(info, child, ma_need);
1437 if (unlikely(rc != 0)) {
1438 mdt_object_unlock(info, child, lhc, 1);
1440 /* Debugging code. */
1441 res_id = &lock->l_resource->lr_name;
1442 LDLM_DEBUG(lock, "Returning lock to client");
1443 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1444 &lock->l_resource->lr_name),
1445 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1446 PLDLMRES(lock->l_resource),
1447 PFID(mdt_object_fid(child)));
1448 if (mdt_object_exists(child) && !mdt_object_remote(child))
1449 mdt_pack_size2body(info, child);
1452 LDLM_LOCK_PUT(lock);
1456 mdt_object_put(info->mti_env, child);
1459 mdt_object_unlock(info, parent, lhp, 1);
1463 /* normal handler: should release the child lock */
1464 int mdt_getattr_name(struct mdt_thread_info *info)
1466 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1467 struct mdt_body *reqbody;
1468 struct mdt_body *repbody;
1472 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1473 LASSERT(reqbody != NULL);
1474 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1475 LASSERT(repbody != NULL);
1477 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1478 repbody->eadatasize = 0;
1479 repbody->aclsize = 0;
1481 rc = mdt_init_ucred(info, reqbody);
1483 GOTO(out_shrink, rc);
1485 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1486 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1487 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1488 lhc->mlh_reg_lh.cookie = 0;
1490 mdt_exit_ucred(info);
1493 mdt_client_compatibility(info);
1494 rc2 = mdt_fix_reply(info);
1500 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1501 void *karg, void *uarg);
1503 int mdt_set_info(struct mdt_thread_info *info)
1505 struct ptlrpc_request *req = mdt_info_req(info);
1508 int keylen, vallen, rc = 0;
1511 rc = req_capsule_server_pack(info->mti_pill);
1515 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1517 DEBUG_REQ(D_HA, req, "no set_info key");
1521 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1524 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1526 DEBUG_REQ(D_HA, req, "no set_info val");
1530 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1533 /* Swab any part of val you need to here */
1534 if (KEY_IS(KEY_READ_ONLY)) {
1536 lustre_msg_set_status(req->rq_repmsg, 0);
1538 spin_lock(&req->rq_export->exp_lock);
1540 *exp_connect_flags_ptr(req->rq_export) |=
1543 *exp_connect_flags_ptr(req->rq_export) &=
1544 ~OBD_CONNECT_RDONLY;
1545 spin_unlock(&req->rq_export->exp_lock);
1547 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1548 struct changelog_setinfo *cs =
1549 (struct changelog_setinfo *)val;
1550 if (vallen != sizeof(*cs)) {
1551 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1554 if (ptlrpc_req_need_swab(req)) {
1555 __swab64s(&cs->cs_recno);
1556 __swab32s(&cs->cs_id);
1559 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1561 lustre_msg_set_status(req->rq_repmsg, rc);
1569 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
1570 struct ptlrpc_request *req);
1573 * Top-level handler for MDT connection requests.
1575 int mdt_connect(struct mdt_thread_info *info)
1578 struct obd_connect_data *reply;
1579 struct obd_export *exp;
1580 struct ptlrpc_request *req = mdt_info_req(info);
1584 rc = target_handle_connect(req);
1586 RETURN(err_serious(rc));
1588 LASSERT(req->rq_export != NULL);
1589 exp = req->rq_export;
1590 info->mti_exp = exp;
1591 info->mti_mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
1592 rc = mdt_init_sec_level(info);
1596 rc = mdt_connect_check_sptlrpc(info->mti_mdt, exp, req);
1600 /* To avoid exposing partially initialized connection flags, changes up
1601 * to this point have been staged in reply->ocd_connect_flags. Now that
1602 * connection handling has completed successfully, atomically update
1603 * the connect flags in the shared export data structure. LU-1623 */
1604 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1605 spin_lock(&exp->exp_lock);
1606 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1607 spin_unlock(&exp->exp_lock);
1609 rc = mdt_init_idmap(info);
1614 obd_disconnect(class_export_get(req->rq_export));
1618 int mdt_disconnect(struct mdt_thread_info *info)
1623 rc = target_handle_disconnect(mdt_info_req(info));
1625 rc = err_serious(rc);
1629 static int mdt_sendpage(struct mdt_thread_info *info,
1630 struct lu_rdpg *rdpg, int nob)
1632 struct ptlrpc_request *req = mdt_info_req(info);
1633 struct obd_export *exp = req->rq_export;
1634 struct ptlrpc_bulk_desc *desc;
1635 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1642 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1647 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1648 /* old client requires reply size in it's PAGE_SIZE,
1649 * which is rdpg->rp_count */
1650 nob = rdpg->rp_count;
1652 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1653 i++, tmpcount -= tmpsize) {
1654 tmpsize = min_t(int, tmpcount, PAGE_CACHE_SIZE);
1655 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1658 LASSERT(desc->bd_nob == nob);
1659 rc = target_bulk_io(exp, desc, lwi);
1660 ptlrpc_free_bulk_pin(desc);
1664 int mdt_readpage(struct mdt_thread_info *info)
1666 struct mdt_object *object = info->mti_object;
1667 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1668 struct mdt_body *reqbody;
1669 struct mdt_body *repbody;
1674 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1675 RETURN(err_serious(-ENOMEM));
1677 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1678 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1679 if (reqbody == NULL || repbody == NULL)
1680 RETURN(err_serious(-EFAULT));
1683 * prepare @rdpg before calling lower layers and transfer itself. Here
1684 * reqbody->size contains offset of where to start to read and
1685 * reqbody->nlink contains number bytes to read.
1687 rdpg->rp_hash = reqbody->size;
1688 if (rdpg->rp_hash != reqbody->size) {
1689 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1690 rdpg->rp_hash, reqbody->size);
1694 rdpg->rp_attrs = reqbody->mode;
1695 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1696 rdpg->rp_attrs |= LUDA_64BITHASH;
1697 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1698 exp_max_brw_size(info->mti_exp));
1699 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1701 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1702 if (rdpg->rp_pages == NULL)
1705 for (i = 0; i < rdpg->rp_npages; ++i) {
1706 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1707 if (rdpg->rp_pages[i] == NULL)
1708 GOTO(free_rdpg, rc = -ENOMEM);
1711 /* call lower layers to fill allocated pages with directory data */
1712 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1714 GOTO(free_rdpg, rc);
1716 /* send pages to client */
1717 rc = mdt_sendpage(info, rdpg, rc);
1722 for (i = 0; i < rdpg->rp_npages; i++)
1723 if (rdpg->rp_pages[i] != NULL)
1724 __free_page(rdpg->rp_pages[i]);
1725 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1727 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1733 static int mdt_reint_internal(struct mdt_thread_info *info,
1734 struct mdt_lock_handle *lhc,
1737 struct req_capsule *pill = info->mti_pill;
1738 struct mdt_body *repbody;
1743 rc = mdt_reint_unpack(info, op);
1745 CERROR("Can't unpack reint, rc %d\n", rc);
1746 RETURN(err_serious(rc));
1749 /* for replay (no_create) lmm is not needed, client has it already */
1750 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1751 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1752 info->mti_rr.rr_eadatalen);
1754 /* llog cookies are always 0, the field is kept for compatibility */
1755 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1756 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1758 rc = req_capsule_server_pack(pill);
1760 CERROR("Can't pack response, rc %d\n", rc);
1761 RETURN(err_serious(rc));
1764 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1765 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1767 repbody->eadatasize = 0;
1768 repbody->aclsize = 0;
1771 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1773 /* for replay no cookkie / lmm need, because client have this already */
1774 if (info->mti_spec.no_create)
1775 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1776 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1778 rc = mdt_init_ucred_reint(info);
1780 GOTO(out_shrink, rc);
1782 rc = mdt_fix_attr_ucred(info, op);
1784 GOTO(out_ucred, rc = err_serious(rc));
1786 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1787 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1788 GOTO(out_ucred, rc);
1790 rc = mdt_reint_rec(info, lhc);
1793 mdt_exit_ucred(info);
1795 mdt_client_compatibility(info);
1796 rc2 = mdt_fix_reply(info);
1802 static long mdt_reint_opcode(struct mdt_thread_info *info,
1803 const struct req_format **fmt)
1805 struct mdt_rec_reint *rec;
1808 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1810 opc = rec->rr_opcode;
1811 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1812 if (opc < REINT_MAX && fmt[opc] != NULL)
1813 req_capsule_extend(info->mti_pill, fmt[opc]);
1815 CERROR("%s: Unsupported opcode '%ld' from client '%s': "
1816 "rc = %d\n", mdt_obd_name(info->mti_mdt), opc,
1817 info->mti_mdt->mdt_ldlm_client->cli_name,
1819 opc = err_serious(-EFAULT);
1822 opc = err_serious(-EFAULT);
1827 int mdt_reint(struct mdt_thread_info *info)
1832 static const struct req_format *reint_fmts[REINT_MAX] = {
1833 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1834 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1835 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1836 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1837 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1838 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1839 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1840 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1845 opc = mdt_reint_opcode(info, reint_fmts);
1848 * No lock possible here from client to pass it to reint code
1851 rc = mdt_reint_internal(info, NULL, opc);
1856 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1860 /* this should sync the whole device */
1861 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1863 struct dt_device *dt = mdt->mdt_bottom;
1867 rc = dt->dd_ops->dt_sync(env, dt);
1871 /* this should sync this object */
1872 static int mdt_object_sync(struct mdt_thread_info *info)
1874 struct md_object *next;
1878 if (!mdt_object_exists(info->mti_object)) {
1879 CWARN("Non existing object "DFID"!\n",
1880 PFID(mdt_object_fid(info->mti_object)));
1883 next = mdt_object_child(info->mti_object);
1884 rc = mo_object_sync(info->mti_env, next);
1889 int mdt_sync(struct mdt_thread_info *info)
1891 struct ptlrpc_request *req = mdt_info_req(info);
1892 struct req_capsule *pill = info->mti_pill;
1893 struct mdt_body *body;
1897 /* The fid may be zero, so we req_capsule_set manually */
1898 req_capsule_set(pill, &RQF_MDS_SYNC);
1900 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1902 RETURN(err_serious(-EINVAL));
1904 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1905 RETURN(err_serious(-ENOMEM));
1907 if (fid_seq(&body->fid1) == 0) {
1908 /* sync the whole device */
1909 rc = req_capsule_server_pack(pill);
1911 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1913 rc = err_serious(rc);
1915 /* sync an object */
1916 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1918 rc = mdt_object_sync(info);
1920 const struct lu_fid *fid;
1921 struct lu_attr *la = &info->mti_attr.ma_attr;
1923 info->mti_attr.ma_need = MA_INODE;
1924 info->mti_attr.ma_valid = 0;
1925 rc = mdt_attr_get_complex(info, info->mti_object,
1928 body = req_capsule_server_get(pill,
1930 fid = mdt_object_fid(info->mti_object);
1931 mdt_pack_attr2body(info, body, la, fid);
1935 rc = err_serious(rc);
1938 mdt_counter_incr(req, LPROC_MDT_SYNC);
1944 * Quotacheck handler.
1945 * in-kernel quotacheck isn't supported any more.
1947 int mdt_quotacheck(struct mdt_thread_info *info)
1949 struct obd_quotactl *oqctl;
1953 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1955 RETURN(err_serious(-EPROTO));
1957 rc = req_capsule_server_pack(info->mti_pill);
1959 RETURN(err_serious(rc));
1961 /* deprecated, not used any more */
1962 RETURN(-EOPNOTSUPP);
1966 * Handle quota control requests to consult current usage/limit, but also
1967 * to configure quota enforcement
1969 int mdt_quotactl(struct mdt_thread_info *info)
1971 struct obd_export *exp = info->mti_exp;
1972 struct req_capsule *pill = info->mti_pill;
1973 struct obd_quotactl *oqctl, *repoqc;
1975 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1978 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1980 RETURN(err_serious(-EPROTO));
1982 rc = req_capsule_server_pack(pill);
1984 RETURN(err_serious(rc));
1986 switch (oqctl->qc_cmd) {
1988 case LUSTRE_Q_INVALIDATE:
1989 case LUSTRE_Q_FINVALIDATE:
1993 /* deprecated, not used any more */
1994 RETURN(-EOPNOTSUPP);
1995 /* master quotactl */
2001 RETURN(-EOPNOTSUPP);
2002 /* slave quotactl */
2007 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2011 /* map uid/gid for remote client */
2013 if (exp_connect_rmtclient(exp)) {
2014 struct lustre_idmap_table *idmap;
2016 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2018 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2019 oqctl->qc_cmd != Q_GETINFO))
2022 if (oqctl->qc_type == USRQUOTA)
2023 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2025 else if (oqctl->qc_type == GRPQUOTA)
2026 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2031 if (id == CFS_IDMAP_NOTFOUND) {
2032 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2037 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2039 RETURN(err_serious(-EFAULT));
2041 if (oqctl->qc_id != id)
2042 swap(oqctl->qc_id, id);
2044 switch (oqctl->qc_cmd) {
2050 /* forward quotactl request to QMT */
2051 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2056 /* slave quotactl */
2057 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2062 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2066 if (oqctl->qc_id != id)
2067 swap(oqctl->qc_id, id);
2074 * OBD PING and other handlers.
2076 int mdt_obd_ping(struct mdt_thread_info *info)
2081 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2083 rc = target_handle_ping(mdt_info_req(info));
2085 rc = err_serious(rc);
2090 * OBD_IDX_READ handler
2092 int mdt_obd_idx_read(struct mdt_thread_info *info)
2094 struct mdt_device *mdt = info->mti_mdt;
2095 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2096 struct idx_info *req_ii, *rep_ii;
2100 memset(rdpg, 0, sizeof(*rdpg));
2101 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2103 /* extract idx_info buffer from request & reply */
2104 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2105 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2106 RETURN(err_serious(-EPROTO));
2108 rc = req_capsule_server_pack(info->mti_pill);
2110 RETURN(err_serious(rc));
2112 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2114 RETURN(err_serious(-EFAULT));
2115 rep_ii->ii_magic = IDX_INFO_MAGIC;
2117 /* extract hash to start with */
2118 rdpg->rp_hash = req_ii->ii_hash_start;
2120 /* extract requested attributes */
2121 rdpg->rp_attrs = req_ii->ii_attrs;
2123 /* check that fid packed in request is valid and supported */
2124 if (!fid_is_sane(&req_ii->ii_fid))
2126 rep_ii->ii_fid = req_ii->ii_fid;
2129 rep_ii->ii_flags = req_ii->ii_flags;
2131 /* compute number of pages to allocate, ii_count is the number of 4KB
2133 if (req_ii->ii_count <= 0)
2134 GOTO(out, rc = -EFAULT);
2135 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2136 exp_max_brw_size(info->mti_exp));
2137 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
2140 /* allocate pages to store the containers */
2141 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2142 if (rdpg->rp_pages == NULL)
2143 GOTO(out, rc = -ENOMEM);
2144 for (i = 0; i < rdpg->rp_npages; i++) {
2145 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
2146 if (rdpg->rp_pages[i] == NULL)
2147 GOTO(out, rc = -ENOMEM);
2150 /* populate pages with key/record pairs */
2151 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2155 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2156 "asked %d > %d\n", rc, rdpg->rp_count);
2158 /* send pages to client */
2159 rc = mdt_sendpage(info, rdpg, rc);
2163 if (rdpg->rp_pages) {
2164 for (i = 0; i < rdpg->rp_npages; i++)
2165 if (rdpg->rp_pages[i])
2166 __free_page(rdpg->rp_pages[i]);
2167 OBD_FREE(rdpg->rp_pages,
2168 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2173 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2175 return err_serious(-EOPNOTSUPP);
2178 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2180 return err_serious(-EOPNOTSUPP);
2187 /** clone llog ctxt from child (mdd)
2188 * This allows remote llog (replicator) access.
2189 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2190 * context was originally set up, or we can handle them directly.
2191 * I choose the latter, but that means I need any llog
2192 * contexts set up by child to be accessable by the mdt. So we clone the
2193 * context into our context list here.
2195 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2198 struct md_device *next = mdt->mdt_child;
2199 struct llog_ctxt *ctxt;
2202 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2205 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2206 if (rc || ctxt == NULL) {
2210 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2212 CERROR("Can't set mdt ctxt %d\n", rc);
2217 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2218 struct mdt_device *mdt, int idx)
2220 struct llog_ctxt *ctxt;
2222 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2225 /* Put once for the get we just did, and once for the clone */
2226 llog_ctxt_put(ctxt);
2227 llog_ctxt_put(ctxt);
2231 int mdt_llog_create(struct mdt_thread_info *info)
2235 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2236 rc = llog_origin_handle_open(mdt_info_req(info));
2237 return (rc < 0 ? err_serious(rc) : rc);
2240 int mdt_llog_destroy(struct mdt_thread_info *info)
2244 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2245 rc = llog_origin_handle_destroy(mdt_info_req(info));
2246 return (rc < 0 ? err_serious(rc) : rc);
2249 int mdt_llog_read_header(struct mdt_thread_info *info)
2253 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2254 rc = llog_origin_handle_read_header(mdt_info_req(info));
2255 return (rc < 0 ? err_serious(rc) : rc);
2258 int mdt_llog_next_block(struct mdt_thread_info *info)
2262 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2263 rc = llog_origin_handle_next_block(mdt_info_req(info));
2264 return (rc < 0 ? err_serious(rc) : rc);
2267 int mdt_llog_prev_block(struct mdt_thread_info *info)
2271 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2272 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2273 return (rc < 0 ? err_serious(rc) : rc);
2281 static struct ldlm_callback_suite cbs = {
2282 .lcs_completion = ldlm_server_completion_ast,
2283 .lcs_blocking = ldlm_server_blocking_ast,
2284 .lcs_glimpse = ldlm_server_glimpse_ast
2287 int mdt_enqueue(struct mdt_thread_info *info)
2289 struct ptlrpc_request *req;
2293 * info->mti_dlm_req already contains swapped and (if necessary)
2294 * converted dlm request.
2296 LASSERT(info->mti_dlm_req != NULL);
2298 req = mdt_info_req(info);
2299 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2300 req, info->mti_dlm_req, &cbs);
2301 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2302 return rc ? err_serious(rc) : req->rq_status;
2305 int mdt_convert(struct mdt_thread_info *info)
2308 struct ptlrpc_request *req;
2310 LASSERT(info->mti_dlm_req);
2311 req = mdt_info_req(info);
2312 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2313 return rc ? err_serious(rc) : req->rq_status;
2316 int mdt_bl_callback(struct mdt_thread_info *info)
2318 CERROR("bl callbacks should not happen on MDS\n");
2320 return err_serious(-EOPNOTSUPP);
2323 int mdt_cp_callback(struct mdt_thread_info *info)
2325 CERROR("cp callbacks should not happen on MDS\n");
2327 return err_serious(-EOPNOTSUPP);
2331 * sec context handlers
2333 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2337 rc = mdt_handle_idmap(info);
2340 struct ptlrpc_request *req = mdt_info_req(info);
2343 opc = lustre_msg_get_opc(req->rq_reqmsg);
2344 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2345 sptlrpc_svc_ctx_invalidate(req);
2348 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2354 * quota request handlers
2356 int mdt_quota_dqacq(struct mdt_thread_info *info)
2358 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2363 RETURN(err_serious(-EOPNOTSUPP));
2365 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2369 static struct mdt_object *mdt_obj(struct lu_object *o)
2371 LASSERT(lu_device_is_mdt(o->lo_dev));
2372 return container_of0(o, struct mdt_object, mot_obj);
2375 struct mdt_object *mdt_object_new(const struct lu_env *env,
2376 struct mdt_device *d,
2377 const struct lu_fid *f)
2379 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2380 struct lu_object *o;
2381 struct mdt_object *m;
2384 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2385 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2386 if (unlikely(IS_ERR(o)))
2387 m = (struct mdt_object *)o;
2393 struct mdt_object *mdt_object_find(const struct lu_env *env,
2394 struct mdt_device *d,
2395 const struct lu_fid *f)
2397 struct lu_object *o;
2398 struct mdt_object *m;
2401 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2402 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2403 if (unlikely(IS_ERR(o)))
2404 m = (struct mdt_object *)o;
2412 * Asyncronous commit for mdt device.
2414 * Pass asynchonous commit call down the MDS stack.
2416 * \param env environment
2417 * \param mdt the mdt device
2419 static void mdt_device_commit_async(const struct lu_env *env,
2420 struct mdt_device *mdt)
2422 struct dt_device *dt = mdt->mdt_bottom;
2425 rc = dt->dd_ops->dt_commit_async(env, dt);
2426 if (unlikely(rc != 0))
2427 CWARN("async commit start failed with rc = %d", rc);
2431 * Mark the lock as "synchonous".
2433 * Mark the lock to deffer transaction commit to the unlock time.
2435 * \param lock the lock to mark as "synchonous"
2437 * \see mdt_is_lock_sync
2438 * \see mdt_save_lock
2440 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2442 lock->l_ast_data = (void*)1;
2446 * Check whehter the lock "synchonous" or not.
2448 * \param lock the lock to check
2449 * \retval 1 the lock is "synchonous"
2450 * \retval 0 the lock isn't "synchronous"
2452 * \see mdt_set_lock_sync
2453 * \see mdt_save_lock
2455 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2457 return lock->l_ast_data != NULL;
2461 * Blocking AST for mdt locks.
2463 * Starts transaction commit if in case of COS lock conflict or
2464 * deffers such a commit to the mdt_save_lock.
2466 * \param lock the lock which blocks a request or cancelling lock
2467 * \param desc unused
2468 * \param data unused
2469 * \param flag indicates whether this cancelling or blocking callback
2471 * \see ldlm_blocking_ast_nocheck
2473 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2474 void *data, int flag)
2476 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2477 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2481 if (flag == LDLM_CB_CANCELING)
2483 lock_res_and_lock(lock);
2484 if (lock->l_blocking_ast != mdt_blocking_ast) {
2485 unlock_res_and_lock(lock);
2488 if (mdt_cos_is_enabled(mdt) &&
2489 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2490 lock->l_blocking_lock != NULL &&
2491 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2492 mdt_set_lock_sync(lock);
2494 rc = ldlm_blocking_ast_nocheck(lock);
2496 /* There is no lock conflict if l_blocking_lock == NULL,
2497 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2498 * when the last reference to a local lock was released */
2499 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2502 rc = lu_env_init(&env, LCT_LOCAL);
2503 if (unlikely(rc != 0))
2504 CWARN("lu_env initialization failed with rc = %d,"
2505 "cannot start asynchronous commit\n", rc);
2507 mdt_device_commit_async(&env, mdt);
2513 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2514 void *data, int flag)
2516 struct lustre_handle lockh;
2520 case LDLM_CB_BLOCKING:
2521 ldlm_lock2handle(lock, &lockh);
2522 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2524 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2528 case LDLM_CB_CANCELING:
2529 LDLM_DEBUG(lock, "Revoke remote lock\n");
2537 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2538 struct mdt_object *o, struct lustre_handle *lh,
2539 ldlm_mode_t mode, __u64 ibits)
2541 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2542 ldlm_policy_data_t *policy = &mti->mti_policy;
2546 LASSERT(mdt_object_remote(o));
2548 LASSERT((ibits & MDS_INODELOCK_UPDATE));
2550 memset(einfo, 0, sizeof(*einfo));
2551 einfo->ei_type = LDLM_IBITS;
2552 einfo->ei_mode = mode;
2553 einfo->ei_cb_bl = mdt_md_blocking_ast;
2554 einfo->ei_cb_cp = ldlm_completion_ast;
2556 memset(policy, 0, sizeof(*policy));
2557 policy->l_inodebits.bits = ibits;
2559 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2564 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2565 struct mdt_lock_handle *lh, __u64 ibits,
2566 bool nonblock, int locality)
2568 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2569 ldlm_policy_data_t *policy = &info->mti_policy;
2570 struct ldlm_res_id *res_id = &info->mti_res_id;
2575 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2576 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2577 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2578 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2580 if (mdt_object_remote(o)) {
2581 if (locality == MDT_CROSS_LOCK) {
2582 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2583 ibits |= MDS_INODELOCK_LOOKUP;
2586 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2587 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2588 mdt_obd_name(info->mti_mdt), ibits,
2589 PFID(mdt_object_fid(o)));
2590 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2592 /* No PDO lock on remote object */
2593 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2596 if (lh->mlh_type == MDT_PDO_LOCK) {
2597 /* check for exists after object is locked */
2598 if (mdt_object_exists(o) == 0) {
2599 /* Non-existent object shouldn't have PDO lock */
2602 /* Non-dir object shouldn't have PDO lock */
2603 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2608 memset(policy, 0, sizeof(*policy));
2609 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2611 dlmflags = LDLM_FL_ATOMIC_CB;
2613 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2616 * Take PDO lock on whole directory and build correct @res_id for lock
2617 * on part of directory.
2619 if (lh->mlh_pdo_hash != 0) {
2620 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2621 mdt_lock_pdo_mode(info, o, lh);
2622 if (lh->mlh_pdo_mode != LCK_NL) {
2624 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2625 * is never going to be sent to client and we do not
2626 * want it slowed down due to possible cancels.
2628 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2629 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2630 policy, res_id, dlmflags,
2631 &info->mti_exp->exp_handle.h_cookie);
2637 * Finish res_id initializing by name hash marking part of
2638 * directory which is taking modification.
2640 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2643 policy->l_inodebits.bits = ibits;
2646 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2647 * going to be sent to client. If it is - mdt_intent_policy() path will
2648 * fix it up and turn FL_LOCAL flag off.
2650 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2651 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2652 &info->mti_exp->exp_handle.h_cookie);
2654 mdt_object_unlock(info, o, lh, 1);
2655 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2656 lh->mlh_pdo_hash != 0 &&
2657 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2658 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2664 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2665 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2667 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2670 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2671 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2673 struct mdt_lock_handle tmp = *lh;
2676 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2684 * Save a lock within request object.
2686 * Keep the lock referenced until whether client ACK or transaction
2687 * commit happens or release the lock immediately depending on input
2688 * parameters. If COS is ON, a write lock is converted to COS lock
2691 * \param info thead info object
2692 * \param h lock handle
2693 * \param mode lock mode
2694 * \param decref force immediate lock releasing
2697 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2698 ldlm_mode_t mode, int decref)
2702 if (lustre_handle_is_used(h)) {
2703 if (decref || !info->mti_has_trans ||
2704 !(mode & (LCK_PW | LCK_EX))){
2705 mdt_fid_unlock(h, mode);
2707 struct mdt_device *mdt = info->mti_mdt;
2708 struct ldlm_lock *lock = ldlm_handle2lock(h);
2709 struct ptlrpc_request *req = mdt_info_req(info);
2712 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2714 CDEBUG(D_HA, "request = %p reply state = %p"
2715 " transno = "LPD64"\n",
2716 req, req->rq_reply_state, req->rq_transno);
2717 if (mdt_cos_is_enabled(mdt)) {
2719 ldlm_lock_downgrade(lock, LCK_COS);
2722 ptlrpc_save_lock(req, h, mode, no_ack);
2723 if (mdt_is_lock_sync(lock)) {
2724 CDEBUG(D_HA, "found sync-lock,"
2725 " async commit started\n");
2726 mdt_device_commit_async(info->mti_env,
2729 LDLM_LOCK_PUT(lock);
2738 * Unlock mdt object.
2740 * Immeditely release the regular lock and the PDO lock or save the
2741 * lock in reqeuest and keep them referenced until client ACK or
2742 * transaction commit.
2744 * \param info thread info object
2745 * \param o mdt object
2746 * \param lh mdt lock handle referencing regular and PDO locks
2747 * \param decref force immediate lock releasing
2749 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2750 struct mdt_lock_handle *lh, int decref)
2754 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2755 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2757 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2758 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2763 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2764 const struct lu_fid *f,
2765 struct mdt_lock_handle *lh,
2768 struct mdt_object *o;
2770 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2774 rc = mdt_object_lock(info, o, lh, ibits,
2777 mdt_object_put(info->mti_env, o);
2784 void mdt_object_unlock_put(struct mdt_thread_info * info,
2785 struct mdt_object * o,
2786 struct mdt_lock_handle *lh,
2789 mdt_object_unlock(info, o, lh, decref);
2790 mdt_object_put(info->mti_env, o);
2793 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2795 struct mdt_opc_slice *s;
2796 struct mdt_handler *h;
2799 for (s = supported; s->mos_hs != NULL; s++) {
2800 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2801 h = s->mos_hs + (opc - s->mos_opc_start);
2802 if (likely(h->mh_opc != 0))
2803 LASSERTF(h->mh_opc == opc,
2804 "opcode mismatch %d != %d\n",
2807 h = NULL; /* unsupported opc */
2814 static int mdt_lock_resname_compat(struct mdt_device *m,
2815 struct ldlm_request *req)
2817 /* XXX something... later. */
2821 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2823 /* XXX something... later. */
2828 * Generic code handling requests that have struct mdt_body passed in:
2830 * - extract mdt_body from request and save it in @info, if present;
2832 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2835 * - if HABEO_CORPUS flag is set for this request type check whether object
2836 * actually exists on storage (lu_object_exists()).
2839 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2841 const struct mdt_body *body;
2842 struct mdt_object *obj;
2843 const struct lu_env *env;
2844 struct req_capsule *pill;
2848 env = info->mti_env;
2849 pill = info->mti_pill;
2851 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2855 if (!(body->valid & OBD_MD_FLID))
2858 if (!fid_is_sane(&body->fid1)) {
2859 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2864 * Do not get size or any capa fields before we check that request
2865 * contains capa actually. There are some requests which do not, for
2866 * instance MDS_IS_SUBDIR.
2868 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2869 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2870 mdt_set_capainfo(info, 0, &body->fid1,
2871 req_capsule_client_get(pill, &RMF_CAPA1));
2873 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2875 if ((flags & HABEO_CORPUS) &&
2876 !mdt_object_exists(obj)) {
2877 mdt_object_put(env, obj);
2878 /* for capability renew ENOENT will be handled in
2880 if (body->valid & OBD_MD_FLOSSCAPA)
2885 info->mti_object = obj;
2894 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2896 struct req_capsule *pill = info->mti_pill;
2900 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2901 rc = mdt_body_unpack(info, flags);
2905 if (rc == 0 && (flags & HABEO_REFERO)) {
2907 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2908 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2909 info->mti_body->eadatasize);
2910 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2911 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2914 rc = req_capsule_server_pack(pill);
2919 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2921 struct md_device *next = m->mdt_child;
2923 return next->md_ops->mdo_init_capa_ctxt(env, next,
2924 m->mdt_opts.mo_mds_capa,
2925 m->mdt_capa_timeout,
2931 * Invoke handler for this request opc. Also do necessary preprocessing
2932 * (according to handler ->mh_flags), and post-processing (setting of
2933 * ->last_{xid,committed}).
2935 static int mdt_req_handle(struct mdt_thread_info *info,
2936 struct mdt_handler *h, struct ptlrpc_request *req)
2938 int rc, serious = 0;
2943 LASSERT(h->mh_act != NULL);
2944 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2945 LASSERT(current->journal_info == NULL);
2948 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2949 * to put same checks into handlers like mdt_close(), mdt_reint(),
2950 * etc., without talking to mdt authors first. Checking same thing
2951 * there again is useless and returning 0 error without packing reply
2952 * is buggy! Handlers either pack reply or return error.
2954 * We return 0 here and do not send any reply in order to emulate
2955 * network failure. Do not send any reply in case any of NET related
2956 * fail_id has occured.
2958 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2962 flags = h->mh_flags;
2963 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2965 if (h->mh_fmt != NULL) {
2966 req_capsule_set(info->mti_pill, h->mh_fmt);
2967 rc = mdt_unpack_req_pack_rep(info, flags);
2970 if (rc == 0 && flags & MUTABOR &&
2971 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2972 /* should it be rq_status? */
2975 if (rc == 0 && flags & HABEO_CLAVIS) {
2976 struct ldlm_request *dlm_req;
2978 LASSERT(h->mh_fmt != NULL);
2980 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2981 if (dlm_req != NULL) {
2982 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2984 dlm_req->lock_desc.l_policy_data.\
2985 l_inodebits.bits == 0)) {
2987 * Lock without inodebits makes no sense and
2988 * will oops later in ldlm. If client miss to
2989 * set such bits, do not trigger ASSERTION.
2991 * For liblustre flock case, it maybe zero.
2995 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2996 rc = mdt_lock_resname_compat(
2999 info->mti_dlm_req = dlm_req;
3006 /* capability setting changed via /proc, needs reinitialize ctxt */
3007 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
3008 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
3009 info->mti_mdt->mdt_capa_conf = 0;
3012 if (likely(rc == 0)) {
3014 * Process request, there can be two types of rc:
3015 * 1) errors with msg unpack/pack, other failures outside the
3016 * operation itself. This is counted as serious errors;
3017 * 2) errors during fs operation, should be placed in rq_status
3020 rc = h->mh_act(info);
3022 !req->rq_no_reply && req->rq_reply_state == NULL) {
3023 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3024 "pack reply and returned 0 error\n",
3028 serious = is_serious(rc);
3029 rc = clear_serious(rc);
3033 req->rq_status = rc;
3036 * ELDLM_* codes which > 0 should be in rq_status only as well as
3037 * all non-serious errors.
3039 if (rc > 0 || !serious)
3042 LASSERT(current->journal_info == NULL);
3044 if (rc == 0 && (flags & HABEO_CLAVIS) &&
3045 info->mti_mdt->mdt_opts.mo_compat_resname) {
3046 struct ldlm_reply *dlmrep;
3048 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3050 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3053 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3054 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3055 target_committed_to_req(req);
3057 if (unlikely(req_is_replay(req) &&
3058 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3059 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3063 target_send_reply(req, rc, info->mti_fail_id);
3067 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3069 lh->mlh_type = MDT_NUL_LOCK;
3070 lh->mlh_reg_lh.cookie = 0ull;
3071 lh->mlh_reg_mode = LCK_MINMODE;
3072 lh->mlh_pdo_lh.cookie = 0ull;
3073 lh->mlh_pdo_mode = LCK_MINMODE;
3074 lh->mlh_rreg_lh.cookie = 0ull;
3075 lh->mlh_rreg_mode = LCK_MINMODE;
3078 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3080 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3081 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3085 * Initialize fields of struct mdt_thread_info. Other fields are left in
3086 * uninitialized state, because it's too expensive to zero out whole
3087 * mdt_thread_info (> 1K) on each request arrival.
3089 void mdt_thread_info_init(struct ptlrpc_request *req,
3090 struct mdt_thread_info *info)
3094 info->mti_pill = &req->rq_pill;
3097 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3098 mdt_lock_handle_init(&info->mti_lh[i]);
3100 /* mdt device: it can be NULL while CONNECT */
3101 if (req->rq_export) {
3102 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3103 info->mti_exp = req->rq_export;
3105 info->mti_mdt = NULL;
3106 info->mti_env = req->rq_svc_thread->t_env;
3107 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3108 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3109 info->mti_mos = NULL;
3111 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3112 info->mti_big_buf = LU_BUF_NULL;
3113 info->mti_body = NULL;
3114 info->mti_object = NULL;
3115 info->mti_dlm_req = NULL;
3116 info->mti_has_trans = 0;
3117 info->mti_cross_ref = 0;
3118 info->mti_opdata = 0;
3119 info->mti_big_lmm_used = 0;
3121 /* To not check for split by default. */
3122 info->mti_spec.no_create = 0;
3123 info->mti_spec.sp_rm_entry = 0;
3126 void mdt_thread_info_fini(struct mdt_thread_info *info)
3130 if (info->mti_object != NULL) {
3131 mdt_object_put(info->mti_env, info->mti_object);
3132 info->mti_object = NULL;
3135 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3136 mdt_lock_handle_fini(&info->mti_lh[i]);
3137 info->mti_env = NULL;
3138 info->mti_pill = NULL;
3139 info->mti_exp = NULL;
3141 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3142 lu_buf_free(&info->mti_big_buf);
3145 int mdt_tgt_connect(struct tgt_session_info *tsi)
3147 struct ptlrpc_request *req = tgt_ses_req(tsi);
3148 struct mdt_thread_info *mti;
3153 rc = tgt_connect(tsi);
3157 /* XXX: switch mdt_init_idmap() to use tgt_session_info */
3158 lu_env_refill((void *)tsi->tsi_env);
3159 mti = lu_context_key_get(&tsi->tsi_env->le_ctx, &mdt_thread_key);
3160 LASSERT(mti != NULL);
3162 mdt_thread_info_init(req, mti);
3163 rc = mdt_init_idmap(mti);
3164 mdt_thread_info_fini(mti);
3169 obd_disconnect(class_export_get(req->rq_export));
3173 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3174 struct obd_device *obd, int *process)
3176 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3177 case MDS_CONNECT: /* This will never get here, but for completeness. */
3178 case OST_CONNECT: /* This will never get here, but for completeness. */
3179 case MDS_DISCONNECT:
3180 case OST_DISCONNECT:
3186 case MDS_DONE_WRITING:
3187 case MDS_SYNC: /* used in unmounting */
3194 *process = target_queue_recovery_request(req, obd);
3198 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3205 * Handle recovery. Return:
3206 * +1: continue request processing;
3207 * -ve: abort immediately with the given error code;
3208 * 0: send reply with error code in req->rq_status;
3210 static int mdt_recovery(struct mdt_thread_info *info)
3212 struct ptlrpc_request *req = mdt_info_req(info);
3213 struct obd_device *obd;
3217 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3220 case SEC_CTX_INIT_CONT:
3226 rc = mdt_handle_idmap(info);
3235 if (unlikely(!class_connected_export(req->rq_export))) {
3236 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3237 lustre_msg_get_opc(req->rq_reqmsg),
3238 libcfs_id2str(req->rq_peer));
3239 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3240 * mds_A will get -ENOTCONN(especially for ping req),
3241 * which will cause that mds_A deactive timeout, then when
3242 * mds_A cleanup, the cleanup process will be suspended since
3243 * deactive timeout is not zero.
3245 req->rq_status = -ENOTCONN;
3246 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3250 /* sanity check: if the xid matches, the request must be marked as a
3251 * resent or replayed */
3252 if (req_xid_is_last(req)) {
3253 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3254 (MSG_RESENT | MSG_REPLAY))) {
3255 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3256 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3257 lustre_msg_get_flags(req->rq_reqmsg));
3259 req->rq_status = -ENOTCONN;
3264 /* else: note the opposite is not always true; a RESENT req after a
3265 * failover will usually not match the last_xid, since it was likely
3266 * never committed. A REPLAYed request will almost never match the
3267 * last xid, however it could for a committed, but still retained,
3270 obd = req->rq_export->exp_obd;
3272 /* Check for aborted recovery... */
3273 if (unlikely(obd->obd_recovering)) {
3276 DEBUG_REQ(D_INFO, req, "Got new replay");
3277 rc = mdt_filter_recovery_request(req, obd, &should_process);
3278 if (rc != 0 || !should_process)
3280 else if (should_process < 0) {
3281 req->rq_status = should_process;
3282 rc = ptlrpc_error(req);
3289 static int mdt_msg_check_version(struct lustre_msg *msg)
3293 switch (lustre_msg_get_opc(msg)) {
3295 case MDS_DISCONNECT:
3298 case SEC_CTX_INIT_CONT:
3301 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3303 CERROR("bad opc %u version %08x, expecting %08x\n",
3304 lustre_msg_get_opc(msg),
3305 lustre_msg_get_version(msg),
3306 LUSTRE_OBD_VERSION);
3310 case MDS_GETATTR_NAME:
3317 case MDS_DONE_WRITING:
3324 case MDS_HSM_PROGRESS:
3325 case MDS_HSM_REQUEST:
3326 case MDS_HSM_CT_REGISTER:
3327 case MDS_HSM_CT_UNREGISTER:
3328 case MDS_HSM_STATE_GET:
3329 case MDS_HSM_STATE_SET:
3330 case MDS_HSM_ACTION:
3331 case MDS_QUOTACHECK:
3334 case MDS_SWAP_LAYOUTS:
3339 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3341 CERROR("bad opc %u version %08x, expecting %08x\n",
3342 lustre_msg_get_opc(msg),
3343 lustre_msg_get_version(msg),
3344 LUSTRE_MDS_VERSION);
3348 case LDLM_BL_CALLBACK:
3349 case LDLM_CP_CALLBACK:
3350 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3352 CERROR("bad opc %u version %08x, expecting %08x\n",
3353 lustre_msg_get_opc(msg),
3354 lustre_msg_get_version(msg),
3355 LUSTRE_DLM_VERSION);
3357 case OBD_LOG_CANCEL:
3358 case LLOG_ORIGIN_HANDLE_CREATE:
3359 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3360 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3361 case LLOG_ORIGIN_HANDLE_CLOSE:
3362 case LLOG_ORIGIN_HANDLE_DESTROY:
3363 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3365 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3367 CERROR("bad opc %u version %08x, expecting %08x\n",
3368 lustre_msg_get_opc(msg),
3369 lustre_msg_get_version(msg),
3370 LUSTRE_LOG_VERSION);
3373 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3379 static int mdt_handle0(struct ptlrpc_request *req,
3380 struct mdt_thread_info *info,
3381 struct mdt_opc_slice *supported)
3383 struct mdt_handler *h;
3384 struct lustre_msg *msg;
3389 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3392 LASSERT(current->journal_info == NULL);
3394 msg = req->rq_reqmsg;
3395 rc = mdt_msg_check_version(msg);
3396 if (likely(rc == 0)) {
3397 rc = mdt_recovery(info);
3398 if (likely(rc == +1)) {
3399 h = mdt_handler_find(lustre_msg_get_opc(msg),
3401 if (likely(h != NULL)) {
3402 rc = mdt_req_handle(info, h, req);
3404 CERROR("%s: opc unsupported: 0x%x\n",
3405 mdt_obd_name(info->mti_mdt),
3406 lustre_msg_get_opc(msg));
3407 req->rq_status = -ENOTSUPP;
3408 rc = ptlrpc_error(req);
3413 CDEBUG(D_INFO, "%s: drops mal-formed request: rc = %d\n",
3414 mdt_obd_name(info->mti_mdt), rc);
3415 req->rq_status = rc;
3416 rc = ptlrpc_error(req);
3422 * MDT handler function called by ptlrpc service thread when request comes.
3424 * XXX common "target" functionality should be factored into separate module
3425 * shared by mdt, ost and stand-alone services like fld.
3427 int mdt_handle_common(struct ptlrpc_request *req,
3428 struct mdt_opc_slice *supported)
3431 struct mdt_thread_info *info;
3435 env = req->rq_svc_thread->t_env;
3436 /* Refill(initilize) the context(mdt_thread_info), in case it is
3437 * not initialized yet. Usually it happens during start up, after
3438 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3439 * before MDT_thread_info is initialized */
3441 LASSERT(env != NULL);
3442 LASSERT(env->le_ses != NULL);
3443 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3444 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3445 LASSERT(info != NULL);
3447 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3448 mdt_thread_info_init(req, info);
3450 rc = mdt_handle0(req, info, supported);
3452 mdt_thread_info_fini(info);
3453 req_capsule_fini(&req->rq_pill);
3458 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3461 int mdt_recovery_handle(struct ptlrpc_request *req)
3467 rc = mdt_handle_common(req, mdt_regular_handlers);
3487 static int mdt_intent_getattr(enum mdt_it_code opcode,
3488 struct mdt_thread_info *info,
3489 struct ldlm_lock **,
3492 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3493 struct mdt_thread_info *info,
3494 struct ldlm_lock **lockp,
3497 static int mdt_intent_layout(enum mdt_it_code opcode,
3498 struct mdt_thread_info *info,
3499 struct ldlm_lock **,
3501 static int mdt_intent_reint(enum mdt_it_code opcode,
3502 struct mdt_thread_info *info,
3503 struct ldlm_lock **,
3506 static struct mdt_it_flavor {
3507 const struct req_format *it_fmt;
3509 int (*it_act)(enum mdt_it_code ,
3510 struct mdt_thread_info *,
3511 struct ldlm_lock **,
3514 } mdt_it_flavor[] = {
3516 .it_fmt = &RQF_LDLM_INTENT,
3517 /*.it_flags = HABEO_REFERO,*/
3519 .it_act = mdt_intent_reint,
3520 .it_reint = REINT_OPEN
3523 .it_fmt = &RQF_LDLM_INTENT,
3524 .it_flags = MUTABOR,
3525 .it_act = mdt_intent_reint,
3526 .it_reint = REINT_OPEN
3529 .it_fmt = &RQF_LDLM_INTENT,
3530 .it_flags = MUTABOR,
3531 .it_act = mdt_intent_reint,
3532 .it_reint = REINT_CREATE
3534 [MDT_IT_GETATTR] = {
3535 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3536 .it_flags = HABEO_REFERO,
3537 .it_act = mdt_intent_getattr
3539 [MDT_IT_READDIR] = {
3545 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3546 .it_flags = HABEO_REFERO,
3547 .it_act = mdt_intent_getattr
3550 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3551 .it_flags = MUTABOR,
3553 .it_reint = REINT_UNLINK
3557 .it_flags = MUTABOR,
3560 [MDT_IT_GETXATTR] = {
3561 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3563 .it_act = mdt_intent_getxattr
3566 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3568 .it_act = mdt_intent_layout
3572 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3573 struct ldlm_lock **lockp,
3574 struct ldlm_lock *new_lock,
3575 struct mdt_lock_handle *lh,
3578 struct ptlrpc_request *req = mdt_info_req(info);
3579 struct ldlm_lock *lock = *lockp;
3582 * Get new lock only for cases when possible resent did not find any
3585 if (new_lock == NULL)
3586 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3588 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3589 lh->mlh_reg_lh.cookie = 0;
3593 LASSERTF(new_lock != NULL,
3594 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3597 * If we've already given this lock to a client once, then we should
3598 * have no readers or writers. Otherwise, we should have one reader
3599 * _or_ writer ref (which will be zeroed below) before returning the
3602 if (new_lock->l_export == req->rq_export) {
3603 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3605 LASSERT(new_lock->l_export == NULL);
3606 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3611 if (new_lock->l_export == req->rq_export) {
3613 * Already gave this to the client, which means that we
3614 * reconstructed a reply.
3616 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3618 lh->mlh_reg_lh.cookie = 0;
3619 RETURN(ELDLM_LOCK_REPLACED);
3623 * Fixup the lock to be given to the client.
3625 lock_res_and_lock(new_lock);
3626 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3627 * possible blocking AST. */
3628 while (new_lock->l_readers > 0) {
3629 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3630 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3631 new_lock->l_readers--;
3633 while (new_lock->l_writers > 0) {
3634 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3635 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3636 new_lock->l_writers--;
3639 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3640 new_lock->l_blocking_ast = lock->l_blocking_ast;
3641 new_lock->l_completion_ast = lock->l_completion_ast;
3642 new_lock->l_remote_handle = lock->l_remote_handle;
3643 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3645 unlock_res_and_lock(new_lock);
3647 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3648 &new_lock->l_remote_handle,
3649 &new_lock->l_exp_hash);
3651 LDLM_LOCK_RELEASE(new_lock);
3652 lh->mlh_reg_lh.cookie = 0;
3654 RETURN(ELDLM_LOCK_REPLACED);
3657 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3658 struct ldlm_lock *new_lock,
3659 struct ldlm_lock **old_lock,
3660 struct mdt_lock_handle *lh)
3662 struct ptlrpc_request *req = mdt_info_req(info);
3663 struct obd_export *exp = req->rq_export;
3664 struct lustre_handle remote_hdl;
3665 struct ldlm_request *dlmreq;
3666 struct ldlm_lock *lock;
3668 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3671 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3672 remote_hdl = dlmreq->lock_handle[0];
3674 /* In the function below, .hs_keycmp resolves to
3675 * ldlm_export_lock_keycmp() */
3676 /* coverity[overrun-buffer-val] */
3677 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3679 if (lock != new_lock) {
3680 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3681 lh->mlh_reg_mode = lock->l_granted_mode;
3683 LDLM_DEBUG(lock, "Restoring lock cookie");
3684 DEBUG_REQ(D_DLMTRACE, req,
3685 "restoring lock cookie "LPX64,
3686 lh->mlh_reg_lh.cookie);
3688 *old_lock = LDLM_LOCK_GET(lock);
3689 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3693 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3697 * If the xid matches, then we know this is a resent request, and allow
3698 * it. (It's probably an OPEN, for which we don't send a lock.
3700 if (req_xid_is_last(req))
3704 * This remote handle isn't enqueued, so we never received or processed
3705 * this request. Clear MSG_RESENT, because it can be handled like any
3706 * normal request now.
3708 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3710 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3714 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3715 struct mdt_thread_info *info,
3716 struct ldlm_lock **lockp,
3719 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3720 struct ldlm_reply *ldlm_rep = NULL;
3724 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3725 * (for the resend case) or a new lock. Below we will use it to
3726 * replace the original lock.
3728 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3729 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3730 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3731 rc = mdt_object_lock(info, info->mti_object, lhc,
3732 MDS_INODELOCK_XATTR,
3738 grc = mdt_getxattr(info);
3740 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3742 if (mdt_info_req(info)->rq_repmsg != NULL)
3743 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3744 if (ldlm_rep == NULL)
3745 RETURN(err_serious(-EFAULT));
3747 ldlm_rep->lock_policy_res2 = grc;
3752 static int mdt_intent_getattr(enum mdt_it_code opcode,
3753 struct mdt_thread_info *info,
3754 struct ldlm_lock **lockp,
3757 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3758 struct ldlm_lock *new_lock = NULL;
3760 struct ldlm_reply *ldlm_rep;
3761 struct ptlrpc_request *req;
3762 struct mdt_body *reqbody;
3763 struct mdt_body *repbody;
3767 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3770 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3773 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3774 repbody->eadatasize = 0;
3775 repbody->aclsize = 0;
3779 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3781 case MDT_IT_GETATTR:
3782 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3786 CERROR("Unsupported intent (%d)\n", opcode);
3787 GOTO(out_shrink, rc = -EINVAL);
3790 rc = mdt_init_ucred(info, reqbody);
3792 GOTO(out_shrink, rc);
3794 req = info->mti_pill->rc_req;
3795 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3796 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3798 /* Get lock from request for possible resent case. */
3799 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3801 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3802 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3804 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3805 ldlm_rep->lock_policy_res2 = 0;
3806 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3807 ldlm_rep->lock_policy_res2) {
3808 lhc->mlh_reg_lh.cookie = 0ull;
3809 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3812 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3815 mdt_exit_ucred(info);
3817 mdt_client_compatibility(info);
3818 rc2 = mdt_fix_reply(info);
3824 static int mdt_intent_layout(enum mdt_it_code opcode,
3825 struct mdt_thread_info *info,
3826 struct ldlm_lock **lockp,
3829 struct layout_intent *layout;
3831 struct mdt_object *obj = NULL;
3832 struct md_object *child = NULL;
3836 if (opcode != MDT_IT_LAYOUT) {
3837 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3842 fid = &info->mti_tmp_fid2;
3843 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3845 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3847 RETURN(PTR_ERR(obj));
3849 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3850 child = mdt_object_child(obj);
3852 /* get the length of lsm */
3853 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3856 if (rc > info->mti_mdt->mdt_max_mdsize)
3857 info->mti_mdt->mdt_max_mdsize = rc;
3860 mdt_object_put(info->mti_env, obj);
3862 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3863 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3864 ldlm_lvbo_size(*lockp));
3865 rc = req_capsule_server_pack(info->mti_pill);
3869 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3870 LASSERT(layout != NULL);
3871 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3872 /* return to normal ldlm handling */
3875 CERROR("%s: Unsupported layout intent (%d)\n",
3876 mdt_obd_name(info->mti_mdt), layout->li_opc);
3880 static int mdt_intent_reint(enum mdt_it_code opcode,
3881 struct mdt_thread_info *info,
3882 struct ldlm_lock **lockp,
3885 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3886 struct ldlm_reply *rep = NULL;
3890 static const struct req_format *intent_fmts[REINT_MAX] = {
3891 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3892 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3897 opc = mdt_reint_opcode(info, intent_fmts);
3901 if (mdt_it_flavor[opcode].it_reint != opc) {
3902 CERROR("Reint code %ld doesn't match intent: %d\n",
3904 RETURN(err_serious(-EPROTO));
3907 /* Get lock from request for possible resent case. */
3908 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3910 rc = mdt_reint_internal(info, lhc, opc);
3912 /* Check whether the reply has been packed successfully. */
3913 if (mdt_info_req(info)->rq_repmsg != NULL)
3914 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3916 RETURN(err_serious(-EFAULT));
3918 /* MDC expects this in any case */
3920 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3922 /* the open lock or the lock for cross-ref object should be
3923 * returned to the client */
3924 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3925 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3926 rep->lock_policy_res2 = 0;
3927 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3931 rep->lock_policy_res2 = clear_serious(rc);
3933 if (rep->lock_policy_res2 == -ENOENT &&
3934 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3935 rep->lock_policy_res2 = 0;
3937 if (rc == -ENOTCONN || rc == -ENODEV ||
3938 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3940 * If it is the disconnect error (ENODEV & ENOCONN), the error
3941 * will be returned by rq_status, and client at ptlrpc layer
3942 * will detect this, then disconnect, reconnect the import
3943 * immediately, instead of impacting the following the rpc.
3945 lhc->mlh_reg_lh.cookie = 0ull;
3949 * For other cases, the error will be returned by intent.
3950 * and client will retrieve the result from intent.
3953 * FIXME: when open lock is finished, that should be
3956 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3957 LASSERTF(rc == 0, "Error occurred but lock handle "
3958 "is still in use, rc = %d\n", rc);
3959 rep->lock_policy_res2 = 0;
3960 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3963 lhc->mlh_reg_lh.cookie = 0ull;
3964 RETURN(ELDLM_LOCK_ABORTED);
3969 static int mdt_intent_code(long itcode)
3977 case IT_OPEN|IT_CREAT:
3984 rc = MDT_IT_READDIR;
3987 rc = MDT_IT_GETATTR;
3999 rc = MDT_IT_GETXATTR;
4004 case IT_QUOTA_DQACQ:
4009 CERROR("Unknown intent opcode: %ld\n", itcode);
4016 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
4017 struct ldlm_lock **lockp, __u64 flags)
4019 struct req_capsule *pill;
4020 struct mdt_it_flavor *flv;
4025 opc = mdt_intent_code(itopc);
4029 pill = info->mti_pill;
4031 if (opc == MDT_IT_QUOTA) {
4032 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4035 RETURN(-EOPNOTSUPP);
4037 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4038 /* pass the request to quota master */
4039 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4040 mdt_info_req(info), lockp,
4045 flv = &mdt_it_flavor[opc];
4046 if (flv->it_fmt != NULL)
4047 req_capsule_extend(pill, flv->it_fmt);
4049 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
4051 struct ptlrpc_request *req = mdt_info_req(info);
4052 if (flv->it_flags & MUTABOR &&
4053 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
4056 if (rc == 0 && flv->it_act != NULL) {
4057 struct ldlm_reply *rep;
4059 /* execute policy */
4060 rc = flv->it_act(opc, info, lockp, flags);
4062 /* Check whether the reply has been packed successfully. */
4063 if (mdt_info_req(info)->rq_repmsg != NULL) {
4064 rep = req_capsule_server_get(info->mti_pill,
4066 rep->lock_policy_res2 =
4067 ptlrpc_status_hton(rep->lock_policy_res2);
4075 static int mdt_intent_policy(struct ldlm_namespace *ns,
4076 struct ldlm_lock **lockp, void *req_cookie,
4077 ldlm_mode_t mode, __u64 flags, void *data)
4079 struct mdt_thread_info *info;
4080 struct ptlrpc_request *req = req_cookie;
4081 struct ldlm_intent *it;
4082 struct req_capsule *pill;
4087 LASSERT(req != NULL);
4089 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
4091 LASSERT(info != NULL);
4092 pill = info->mti_pill;
4093 LASSERT(pill->rc_req == req);
4095 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4096 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4097 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4099 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4103 /* Lock without inodebits makes no sense and will oops
4104 * later in ldlm. Let's check it now to see if we have
4105 * ibits corrupted somewhere in mdt_intent_opc().
4106 * The case for client miss to set ibits has been
4107 * processed by others. */
4108 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
4109 lr_type == LDLM_IBITS,
4110 info->mti_dlm_req->lock_desc.\
4111 l_policy_data.l_inodebits.bits != 0));
4113 rc = err_serious(-EFAULT);
4115 /* No intent was provided */
4116 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4117 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4118 rc = req_capsule_server_pack(pill);
4120 rc = err_serious(rc);
4125 static int mdt_seq_fini(const struct lu_env *env,
4126 struct mdt_device *m)
4128 return seq_site_fini(env, mdt_seq_site(m));
4131 static int mdt_seq_init(const struct lu_env *env,
4133 struct mdt_device *m)
4135 struct seq_server_site *ss;
4140 ss = mdt_seq_site(m);
4143 * This is sequence-controller node. Init seq-controller server on local
4146 if (ss->ss_node_id == 0) {
4147 LASSERT(ss->ss_control_seq == NULL);
4149 OBD_ALLOC_PTR(ss->ss_control_seq);
4150 if (ss->ss_control_seq == NULL)
4153 rc = seq_server_init(ss->ss_control_seq,
4154 m->mdt_bottom, uuid,
4155 LUSTRE_SEQ_CONTROLLER,
4160 GOTO(out_seq_fini, rc);
4162 OBD_ALLOC_PTR(ss->ss_client_seq);
4163 if (ss->ss_client_seq == NULL)
4164 GOTO(out_seq_fini, rc = -ENOMEM);
4166 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4167 if (prefix == NULL) {
4168 OBD_FREE_PTR(ss->ss_client_seq);
4169 GOTO(out_seq_fini, rc = -ENOMEM);
4172 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4176 * Init seq-controller client after seq-controller server is
4177 * ready. Pass ss->ss_control_seq to it for direct talking.
4179 rc = seq_client_init(ss->ss_client_seq, NULL,
4180 LUSTRE_SEQ_METADATA, prefix,
4181 ss->ss_control_seq);
4182 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4185 GOTO(out_seq_fini, rc);
4188 /* Init seq-server on local MDT */
4189 LASSERT(ss->ss_server_seq == NULL);
4191 OBD_ALLOC_PTR(ss->ss_server_seq);
4192 if (ss->ss_server_seq == NULL)
4193 GOTO(out_seq_fini, rc = -ENOMEM);
4195 rc = seq_server_init(ss->ss_server_seq,
4196 m->mdt_bottom, uuid,
4201 GOTO(out_seq_fini, rc = -ENOMEM);
4203 /* Assign seq-controller client to local seq-server. */
4204 if (ss->ss_node_id == 0) {
4205 LASSERT(ss->ss_client_seq != NULL);
4207 rc = seq_server_set_cli(ss->ss_server_seq,
4215 mdt_seq_fini(env, m);
4223 static int mdt_fld_fini(const struct lu_env *env,
4224 struct mdt_device *m)
4226 struct seq_server_site *ss = mdt_seq_site(m);
4229 if (ss && ss->ss_server_fld) {
4230 fld_server_fini(env, ss->ss_server_fld);
4231 OBD_FREE_PTR(ss->ss_server_fld);
4232 ss->ss_server_fld = NULL;
4238 static int mdt_fld_init(const struct lu_env *env,
4240 struct mdt_device *m)
4242 struct seq_server_site *ss;
4246 ss = mdt_seq_site(m);
4248 OBD_ALLOC_PTR(ss->ss_server_fld);
4249 if (ss->ss_server_fld == NULL)
4250 RETURN(rc = -ENOMEM);
4252 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4253 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4255 OBD_FREE_PTR(ss->ss_server_fld);
4256 ss->ss_server_fld = NULL;
4263 static void mdt_stack_pre_fini(const struct lu_env *env,
4264 struct mdt_device *m, struct lu_device *top)
4266 struct obd_device *obd;
4267 struct lustre_cfg_bufs *bufs;
4268 struct lustre_cfg *lcfg;
4269 struct mdt_thread_info *info;
4274 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4275 LASSERT(info != NULL);
4277 bufs = &info->mti_u.bufs;
4279 LASSERT(m->mdt_child_exp);
4280 LASSERT(m->mdt_child_exp->exp_obd);
4281 obd = m->mdt_child_exp->exp_obd;
4283 /* process cleanup, pass mdt obd name to get obd umount flags */
4284 /* XXX: this is needed because all layers are referenced by
4285 * objects (some of them are pinned by osd, for example *
4286 * the proper solution should be a model where object used
4287 * by osd only doesn't have mdt/mdd slices -bzzz */
4288 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4289 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4290 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4292 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4295 top->ld_ops->ldo_process_config(env, top, lcfg);
4296 lustre_cfg_free(lcfg);
4300 static void mdt_stack_fini(const struct lu_env *env,
4301 struct mdt_device *m, struct lu_device *top)
4303 struct obd_device *obd = mdt2obd_dev(m);
4304 struct lustre_cfg_bufs *bufs;
4305 struct lustre_cfg *lcfg;
4306 struct mdt_thread_info *info;
4310 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4311 LASSERT(info != NULL);
4313 lu_dev_del_linkage(top->ld_site, top);
4315 lu_site_purge(env, top->ld_site, -1);
4317 bufs = &info->mti_u.bufs;
4318 /* process cleanup, pass mdt obd name to get obd umount flags */
4319 /* another purpose is to let all layers to release their objects */
4320 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4325 lustre_cfg_bufs_set_string(bufs, 1, flags);
4326 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4328 CERROR("Cannot alloc lcfg!\n");
4332 top->ld_ops->ldo_process_config(env, top, lcfg);
4333 lustre_cfg_free(lcfg);
4335 lu_site_purge(env, top->ld_site, -1);
4337 m->mdt_child = NULL;
4338 m->mdt_bottom = NULL;
4340 obd_disconnect(m->mdt_child_exp);
4341 m->mdt_child_exp = NULL;
4343 obd_disconnect(m->mdt_bottom_exp);
4344 m->mdt_child_exp = NULL;
4347 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4348 const char *next, struct obd_export **exp)
4350 struct obd_connect_data *data = NULL;
4351 struct obd_device *obd;
4355 OBD_ALLOC_PTR(data);
4357 GOTO(out, rc = -ENOMEM);
4359 obd = class_name2obd(next);
4361 CERROR("%s: can't locate next device: %s\n",
4362 mdt_obd_name(m), next);
4363 GOTO(out, rc = -ENOTCONN);
4366 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4367 data->ocd_version = LUSTRE_VERSION_CODE;
4369 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4371 CERROR("%s: cannot connect to next dev %s (%d)\n",
4372 mdt_obd_name(m), next, rc);
4382 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4383 struct lustre_cfg *cfg)
4385 char *dev = lustre_cfg_string(cfg, 0);
4386 int rc, name_size, uuid_size;
4387 char *name, *uuid, *p;
4388 struct lustre_cfg_bufs *bufs;
4389 struct lustre_cfg *lcfg;
4390 struct obd_device *obd;
4391 struct lustre_profile *lprof;
4392 struct lu_site *site;
4395 /* in 1.8 we had the only device in the stack - MDS.
4396 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4397 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4398 * to generate names and setup MDT, MDD. MDT will be using
4399 * generated name to connect to MDD. for MDD the next device
4400 * will be LOD with name taken from so called "profile" which
4401 * is generated by mount_option line
4403 * 1.8 MGS generates config. commands like this:
4404 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4405 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4406 * 2.0 MGS generates config. commands like this:
4407 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4408 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4409 * 3:lustre-MDT0000-mdtlov 4:f
4411 * we generate MDD name from MDT one, just replacing T with D
4413 * after all the preparations, the logical equivalent will be
4414 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4415 * 3:lustre-MDT0000-mdtlov 4:f
4416 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4417 * 3:lustre-MDD0000 4:f
4419 * notice we build the stack from down to top: MDD first, then MDT */
4421 name_size = MAX_OBD_NAME;
4422 uuid_size = MAX_OBD_NAME;
4424 OBD_ALLOC(name, name_size);
4425 OBD_ALLOC(uuid, uuid_size);
4426 if (name == NULL || uuid == NULL)
4427 GOTO(cleanup_mem, rc = -ENOMEM);
4429 OBD_ALLOC_PTR(bufs);
4431 GOTO(cleanup_mem, rc = -ENOMEM);
4434 p = strstr(name, "-MDT");
4436 GOTO(cleanup_mem, rc = -ENOMEM);
4439 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4441 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4442 if (lprof == NULL || lprof->lp_dt == NULL) {
4443 CERROR("can't find the profile: %s\n",
4444 lustre_cfg_string(cfg, 0));
4445 GOTO(cleanup_mem, rc = -EINVAL);
4448 lustre_cfg_bufs_reset(bufs, name);
4449 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4450 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4451 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4453 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4455 GOTO(free_bufs, rc = -ENOMEM);
4457 rc = class_attach(lcfg);
4459 GOTO(lcfg_cleanup, rc);
4461 obd = class_name2obd(name);
4463 CERROR("Can not find obd %s (%s in config)\n",
4464 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4465 GOTO(class_detach, rc = -EINVAL);
4468 lustre_cfg_free(lcfg);
4470 lustre_cfg_bufs_reset(bufs, name);
4471 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4472 lustre_cfg_bufs_set_string(bufs, 2, dev);
4473 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4475 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4477 rc = class_setup(obd, lcfg);
4479 GOTO(class_detach, rc);
4481 /* connect to MDD we just setup */
4482 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4486 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4488 LASSERT(mdt_lu_site(mdt) == NULL);
4489 mdt->mdt_lu_dev.ld_site = site;
4490 site->ls_top_dev = &mdt->mdt_lu_dev;
4491 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4494 /* now connect to bottom OSD */
4495 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4496 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4500 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4503 rc = lu_env_refill((struct lu_env *)env);
4505 CERROR("Failure to refill session: '%d'\n", rc);
4507 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4512 class_detach(obd, lcfg);
4514 lustre_cfg_free(lcfg);
4519 OBD_FREE(name, name_size);
4521 OBD_FREE(uuid, uuid_size);
4525 /* setup quota master target on MDT0 */
4526 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4527 struct lustre_cfg *cfg)
4529 struct obd_device *obd;
4530 char *dev = lustre_cfg_string(cfg, 0);
4531 char *qmtname, *uuid, *p;
4532 struct lustre_cfg_bufs *bufs;
4533 struct lustre_cfg *lcfg;
4534 struct lustre_profile *lprof;
4535 struct obd_connect_data *data;
4539 LASSERT(mdt->mdt_qmt_exp == NULL);
4540 LASSERT(mdt->mdt_qmt_dev == NULL);
4542 /* quota master is on MDT0 only for now */
4543 if (mdt->mdt_seq_site.ss_node_id != 0)
4546 /* MGS generates config commands which look as follows:
4547 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4548 * 3:lustre-MDT0000-mdtlov 4:f
4550 * We generate the QMT name from the MDT one, just replacing MD with QM
4551 * after all the preparations, the logical equivalent will be:
4552 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4553 * 3:lustre-MDT0000-osd 4:f */
4554 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4555 OBD_ALLOC(uuid, UUID_MAX);
4556 OBD_ALLOC_PTR(bufs);
4557 OBD_ALLOC_PTR(data);
4558 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4559 GOTO(cleanup_mem, rc = -ENOMEM);
4561 strcpy(qmtname, dev);
4562 p = strstr(qmtname, "-MDT");
4564 GOTO(cleanup_mem, rc = -ENOMEM);
4565 /* replace MD with QM */
4569 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4571 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4572 if (lprof == NULL || lprof->lp_dt == NULL) {
4573 CERROR("can't find profile for %s\n",
4574 lustre_cfg_string(cfg, 0));
4575 GOTO(cleanup_mem, rc = -EINVAL);
4578 lustre_cfg_bufs_reset(bufs, qmtname);
4579 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4580 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4581 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4583 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4585 GOTO(cleanup_mem, rc = -ENOMEM);
4587 rc = class_attach(lcfg);
4589 GOTO(lcfg_cleanup, rc);
4591 obd = class_name2obd(qmtname);
4593 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4594 lustre_cfg_string(cfg, 0));
4595 GOTO(class_detach, rc = -EINVAL);
4598 lustre_cfg_free(lcfg);
4600 lustre_cfg_bufs_reset(bufs, qmtname);
4601 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4602 lustre_cfg_bufs_set_string(bufs, 2, dev);
4604 /* for quota, the next device should be the OSD device */
4605 lustre_cfg_bufs_set_string(bufs, 3,
4606 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4608 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4610 rc = class_setup(obd, lcfg);
4612 GOTO(class_detach, rc);
4614 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4616 /* configure local quota objects */
4617 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4621 GOTO(class_cleanup, rc);
4623 /* connect to quota master target */
4624 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4625 data->ocd_version = LUSTRE_VERSION_CODE;
4626 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4629 CERROR("cannot connect to quota master device %s (%d)\n",
4631 GOTO(class_cleanup, rc);
4637 class_manual_cleanup(obd);
4638 mdt->mdt_qmt_dev = NULL;
4642 class_detach(obd, lcfg);
4644 lustre_cfg_free(lcfg);
4649 OBD_FREE(qmtname, MAX_OBD_NAME);
4651 OBD_FREE(uuid, UUID_MAX);
4657 /* Shutdown quota master target associated with mdt */
4658 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4662 if (mdt->mdt_qmt_exp == NULL)
4664 LASSERT(mdt->mdt_qmt_dev != NULL);
4666 /* the qmt automatically shuts down when the mdt disconnects */
4667 obd_disconnect(mdt->mdt_qmt_exp);
4668 mdt->mdt_qmt_exp = NULL;
4669 mdt->mdt_qmt_dev = NULL;
4673 static struct tgt_handler mdt_tgt_handlers[] = {
4674 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4675 0, MDS_CONNECT, mdt_tgt_connect,
4676 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4677 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4678 0, MDS_DISCONNECT, tgt_disconnect,
4679 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4682 static struct tgt_opc_slice mdt_common_slice[] = {
4684 .tos_opc_start = MDS_FIRST_OPC,
4685 .tos_opc_end = MDS_LAST_OPC,
4686 .tos_hs = mdt_tgt_handlers
4689 .tos_opc_start = OBD_FIRST_OPC,
4690 .tos_opc_end = OBD_LAST_OPC,
4691 .tos_hs = tgt_obd_handlers
4694 .tos_opc_start = LDLM_FIRST_OPC,
4695 .tos_opc_end = LDLM_LAST_OPC,
4696 .tos_hs = tgt_dlm_handlers
4699 .tos_opc_start = SEC_FIRST_OPC,
4700 .tos_opc_end = SEC_LAST_OPC,
4701 .tos_hs = tgt_sec_ctx_handlers
4704 .tos_opc_start = UPDATE_OBJ,
4705 .tos_opc_end = UPDATE_LAST_OPC,
4706 .tos_hs = tgt_out_handlers
4709 .tos_opc_start = FLD_FIRST_OPC,
4710 .tos_opc_end = FLD_LAST_OPC,
4711 .tos_hs = fld_handlers
4714 .tos_opc_start = SEQ_FIRST_OPC,
4715 .tos_opc_end = SEQ_LAST_OPC,
4716 .tos_hs = seq_handlers
4723 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4725 struct md_device *next = m->mdt_child;
4726 struct lu_device *d = &m->mdt_lu_dev;
4727 struct obd_device *obd = mdt2obd_dev(m);
4730 target_recovery_fini(obd);
4732 ping_evictor_stop();
4734 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4736 if (m->mdt_opts.mo_coordinator)
4737 mdt_hsm_cdt_stop(m);
4739 mdt_hsm_cdt_fini(m);
4741 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4742 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4743 obd_exports_barrier(obd);
4744 obd_zombie_barrier();
4748 tgt_fini(env, &m->mdt_lut);
4749 mdt_fs_cleanup(env, m);
4750 upcall_cache_cleanup(m->mdt_identity_cache);
4751 m->mdt_identity_cache = NULL;
4753 if (m->mdt_namespace != NULL) {
4754 ldlm_namespace_free(m->mdt_namespace, NULL,
4755 d->ld_obd->obd_force);
4756 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4759 mdt_quota_fini(env, m);
4761 cfs_free_nidlist(&m->mdt_nosquash_nids);
4762 if (m->mdt_nosquash_str) {
4763 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4764 m->mdt_nosquash_str = NULL;
4765 m->mdt_nosquash_strlen = 0;
4768 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4770 mdt_seq_fini(env, m);
4771 mdt_fld_fini(env, m);
4772 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4774 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4775 cfs_timer_disarm(&m->mdt_ck_timer);
4776 mdt_ck_thread_stop(m);
4781 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4783 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4785 server_put_mount(mdt_obd_name(m), NULL);
4790 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4792 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4793 struct sptlrpc_rule_set tmp_rset;
4796 sptlrpc_rule_set_init(&tmp_rset);
4797 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4799 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4800 mdt_obd_name(m), rc);
4804 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4806 write_lock(&m->mdt_sptlrpc_lock);
4807 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4808 m->mdt_sptlrpc_rset = tmp_rset;
4809 write_unlock(&m->mdt_sptlrpc_lock);
4814 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4816 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4817 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4819 struct mdt_thread_info *info;
4820 struct obd_device *obd;
4821 const char *dev = lustre_cfg_string(cfg, 0);
4822 const char *num = lustre_cfg_string(cfg, 2);
4823 struct lustre_mount_info *lmi = NULL;
4824 struct lustre_sb_info *lsi;
4826 struct seq_server_site *ss_site;
4827 const char *identity_upcall = "NONE";
4828 struct md_device *next;
4834 lu_device_init(&m->mdt_lu_dev, ldt);
4836 * Environment (env) might be missing mdt_thread_key values at that
4837 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4840 * Usually device allocation path doesn't use module key values, but
4841 * mdt has to do a lot of work here, so allocate key value.
4843 rc = lu_env_refill((struct lu_env *)env);
4847 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4848 LASSERT(info != NULL);
4850 obd = class_name2obd(dev);
4851 LASSERT(obd != NULL);
4853 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4855 m->mdt_som_conf = 0;
4857 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4859 /* default is coordinator off, it is started through conf_param
4861 m->mdt_opts.mo_coordinator = 0;
4863 lmi = server_get_mount(dev);
4865 CERROR("Cannot get mount info for %s!\n", dev);
4868 lsi = s2lsi(lmi->lmi_sb);
4869 /* CMD is supported only in IAM mode */
4871 node_id = simple_strtol(num, NULL, 10);
4872 obd->u.obt.obt_magic = OBT_MAGIC;
4875 rwlock_init(&m->mdt_sptlrpc_lock);
4876 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4878 spin_lock_init(&m->mdt_ioepoch_lock);
4879 m->mdt_opts.mo_compat_resname = 0;
4880 m->mdt_opts.mo_mds_capa = 1;
4881 m->mdt_opts.mo_oss_capa = 1;
4882 m->mdt_capa_timeout = CAPA_TIMEOUT;
4883 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4884 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4885 m->mdt_squash_uid = 0;
4886 m->mdt_squash_gid = 0;
4887 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4888 m->mdt_nosquash_str = NULL;
4889 m->mdt_nosquash_strlen = 0;
4890 init_rwsem(&m->mdt_squash_sem);
4891 spin_lock_init(&m->mdt_osfs_lock);
4892 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4893 m->mdt_enable_remote_dir = 0;
4894 m->mdt_enable_remote_dir_gid = 0;
4896 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4897 m->mdt_lu_dev.ld_obd = obd;
4898 /* Set this lu_device to obd for error handling purposes. */
4899 obd->obd_lu_dev = &m->mdt_lu_dev;
4901 /* init the stack */
4902 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4904 CERROR("%s: Can't init device stack, rc %d\n",
4905 mdt_obd_name(m), rc);
4910 ss_site = mdt_seq_site(m);
4911 s->ld_seq_site = ss_site;
4914 /* set server index */
4915 ss_site->ss_node_id = node_id;
4917 /* failover is the default
4918 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4919 * assumed whose ss_node_id == 0 XXX
4921 obd->obd_replayable = 1;
4922 /* No connection accepted until configurations will finish */
4923 obd->obd_no_conn = 1;
4925 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4926 char *str = lustre_cfg_string(cfg, 4);
4927 if (strchr(str, 'n')) {
4928 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4929 obd->obd_replayable = 0;
4933 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4935 GOTO(err_fini_stack, rc);
4937 rc = mdt_seq_init(env, mdt_obd_name(m), m);
4939 GOTO(err_fini_fld, rc);
4941 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4942 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4943 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4944 LDLM_NAMESPACE_SERVER,
4945 LDLM_NAMESPACE_GREEDY,
4947 if (m->mdt_namespace == NULL)
4948 GOTO(err_fini_seq, rc = -ENOMEM);
4950 m->mdt_namespace->ns_lvbp = m;
4951 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4953 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4954 /* set obd_namespace for compatibility with old code */
4955 obd->obd_namespace = m->mdt_namespace;
4957 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4959 rc = mdt_hsm_cdt_init(m);
4961 CERROR("%s: Cannot init coordinator, rc %d\n",
4962 mdt_obd_name(m), rc);
4964 rc = mdt_ck_thread_start(m);
4966 GOTO(err_free_ns, rc);
4968 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4969 OBD_FAIL_MDS_ALL_REQUEST_NET,
4970 OBD_FAIL_MDS_ALL_REPLY_NET);
4974 rc = mdt_fs_setup(env, m, obd, lsi);
4978 mdt_adapt_sptlrpc_conf(obd, 1);
4980 next = m->mdt_child;
4981 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4984 GOTO(err_fs_cleanup, rc);
4986 if (mntopts & MNTOPT_USERXATTR)
4987 m->mdt_opts.mo_user_xattr = 1;
4989 m->mdt_opts.mo_user_xattr = 0;
4991 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4993 GOTO(err_fs_cleanup, rc);
4995 if (mntopts & MNTOPT_ACL)
4996 m->mdt_opts.mo_acl = 1;
4998 m->mdt_opts.mo_acl = 0;
5000 /* XXX: to support suppgid for ACL, we enable identity_upcall
5001 * by default, otherwise, maybe got unexpected -EACCESS. */
5002 if (m->mdt_opts.mo_acl)
5003 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5005 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5007 &mdt_identity_upcall_cache_ops);
5008 if (IS_ERR(m->mdt_identity_cache)) {
5009 rc = PTR_ERR(m->mdt_identity_cache);
5010 m->mdt_identity_cache = NULL;
5011 GOTO(err_fs_cleanup, rc);
5014 rc = mdt_procfs_init(m, dev);
5016 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5017 GOTO(err_recovery, rc);
5020 rc = mdt_quota_init(env, m, cfg);
5022 GOTO(err_procfs, rc);
5024 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5025 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5026 "mdt_ldlm_client", m->mdt_ldlm_client);
5028 ping_evictor_start();
5030 /* recovery will be started upon mdt_prepare()
5031 * when the whole stack is complete and ready
5032 * to serve the requests */
5034 mdt_init_capa_ctxt(env, m);
5036 /* Reduce the initial timeout on an MDS because it doesn't need such
5037 * a long timeout as an OST does. Adaptive timeouts will adjust this
5038 * value appropriately. */
5039 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5040 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5046 target_recovery_fini(obd);
5047 upcall_cache_cleanup(m->mdt_identity_cache);
5048 m->mdt_identity_cache = NULL;
5050 mdt_fs_cleanup(env, m);
5052 tgt_fini(env, &m->mdt_lut);
5054 cfs_timer_disarm(&m->mdt_ck_timer);
5055 mdt_ck_thread_stop(m);
5057 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5058 obd->obd_namespace = m->mdt_namespace = NULL;
5060 mdt_seq_fini(env, m);
5062 mdt_fld_fini(env, m);
5064 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5067 server_put_mount(dev, lmi->lmi_mnt);
5071 /* For interoperability, the left element is old parameter, the right one
5072 * is the new version of the parameter, if some parameter is deprecated,
5073 * the new version should be set as NULL. */
5074 static struct cfg_interop_param mdt_interop_param[] = {
5075 { "mdt.group_upcall", NULL },
5076 { "mdt.quota_type", NULL },
5077 { "mdd.quota_type", NULL },
5078 { "mdt.rootsquash", "mdt.root_squash" },
5079 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5083 /* used by MGS to process specific configurations */
5084 static int mdt_process_config(const struct lu_env *env,
5085 struct lu_device *d, struct lustre_cfg *cfg)
5087 struct mdt_device *m = mdt_dev(d);
5088 struct md_device *md_next = m->mdt_child;
5089 struct lu_device *next = md2lu_dev(md_next);
5093 switch (cfg->lcfg_command) {
5095 struct lprocfs_static_vars lvars;
5096 struct obd_device *obd = d->ld_obd;
5098 /* For interoperability */
5099 struct cfg_interop_param *ptr = NULL;
5100 struct lustre_cfg *old_cfg = NULL;
5103 param = lustre_cfg_string(cfg, 1);
5104 if (param == NULL) {
5105 CERROR("param is empty\n");
5110 ptr = class_find_old_param(param, mdt_interop_param);
5112 if (ptr->new_param == NULL) {
5114 CWARN("For interoperability, skip this %s."
5115 " It is obsolete.\n", ptr->old_param);
5119 CWARN("Found old param %s, changed it to %s.\n",
5120 ptr->old_param, ptr->new_param);
5123 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5130 lprocfs_mdt_init_vars(&lvars);
5131 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5133 if (rc > 0 || rc == -ENOSYS)
5134 /* we don't understand; pass it on */
5135 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5137 if (old_cfg != NULL)
5138 lustre_cfg_free(cfg);
5143 /* others are passed further */
5144 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5150 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5151 const struct lu_object_header *hdr,
5152 struct lu_device *d)
5154 struct mdt_object *mo;
5158 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
5160 struct lu_object *o;
5161 struct lu_object_header *h;
5164 h = &mo->mot_header;
5165 lu_object_header_init(h);
5166 lu_object_init(o, h, d);
5167 lu_object_add_top(h, o);
5168 o->lo_ops = &mdt_obj_ops;
5169 mutex_init(&mo->mot_ioepoch_mutex);
5170 mutex_init(&mo->mot_lov_mutex);
5171 init_rwsem(&mo->mot_open_sem);
5172 init_rwsem(&mo->mot_xattr_sem);
5178 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5179 const struct lu_object_conf *unused)
5181 struct mdt_device *d = mdt_dev(o->lo_dev);
5182 struct lu_device *under;
5183 struct lu_object *below;
5187 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5188 PFID(lu_object_fid(o)));
5190 under = &d->mdt_child->md_lu_dev;
5191 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5192 if (below != NULL) {
5193 lu_object_add(o, below);
5200 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5202 struct mdt_object *mo = mdt_obj(o);
5203 struct lu_object_header *h;
5207 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5208 PFID(lu_object_fid(o)));
5210 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5211 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5214 lu_object_header_fini(h);
5215 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5220 static int mdt_object_print(const struct lu_env *env, void *cookie,
5221 lu_printer_t p, const struct lu_object *o)
5223 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5224 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5225 "flags="LPX64", epochcount=%d, writecount=%d)",
5226 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5227 mdto->mot_ioepoch_count, mdto->mot_writecount);
5230 static int mdt_prepare(const struct lu_env *env,
5231 struct lu_device *pdev,
5232 struct lu_device *cdev)
5234 struct mdt_device *mdt = mdt_dev(cdev);
5235 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5236 struct obd_device *obd = cdev->ld_obd;
5237 struct lfsck_start_param lsp;
5244 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5248 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5252 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5256 lsp.lsp_start = NULL;
5257 lsp.lsp_namespace = mdt->mdt_namespace;
5258 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5259 OBD_IOC_START_LFSCK,
5262 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5263 mdt_obd_name(mdt), rc);
5267 if (mdt->mdt_seq_site.ss_node_id == 0) {
5268 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5269 &mdt->mdt_md_root_fid);
5274 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5275 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5276 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5277 LASSERT(obd->obd_no_conn);
5278 spin_lock(&obd->obd_dev_lock);
5279 obd->obd_no_conn = 0;
5280 spin_unlock(&obd->obd_dev_lock);
5282 if (obd->obd_recovering == 0)
5283 mdt_postrecov(env, mdt);
5288 const struct lu_device_operations mdt_lu_ops = {
5289 .ldo_object_alloc = mdt_object_alloc,
5290 .ldo_process_config = mdt_process_config,
5291 .ldo_prepare = mdt_prepare,
5294 static const struct lu_object_operations mdt_obj_ops = {
5295 .loo_object_init = mdt_object_init,
5296 .loo_object_free = mdt_object_free,
5297 .loo_object_print = mdt_object_print
5300 static int mdt_obd_set_info_async(const struct lu_env *env,
5301 struct obd_export *exp,
5302 __u32 keylen, void *key,
5303 __u32 vallen, void *val,
5304 struct ptlrpc_request_set *set)
5306 struct obd_device *obd = exp->exp_obd;
5312 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5313 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5321 * Match client and server connection feature flags.
5323 * Compute the compatibility flags for a connection request based on
5324 * features mutually supported by client and server.
5326 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5327 * must not be updated here, otherwise a partially initialized value may
5328 * be exposed. After the connection request is successfully processed,
5329 * the top-level MDT connect request handler atomically updates the export
5330 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5331 * reply. \see mdt_connect().
5333 * \param exp the obd_export associated with this client/target pair
5334 * \param mdt the target device for the connection
5335 * \param data stores data for this connect request
5338 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5339 * \retval -EBADE client and server feature requirements are incompatible
5341 static int mdt_connect_internal(struct obd_export *exp,
5342 struct mdt_device *mdt,
5343 struct obd_connect_data *data)
5345 LASSERT(data != NULL);
5347 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5348 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5350 /* If no known bits (which should not happen, probably,
5351 as everybody should support LOOKUP and UPDATE bits at least)
5352 revert to compat mode with plain locks. */
5353 if (!data->ocd_ibits_known &&
5354 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5355 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5357 if (!mdt->mdt_opts.mo_acl)
5358 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5360 if (!mdt->mdt_opts.mo_user_xattr)
5361 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5363 if (!mdt->mdt_som_conf)
5364 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5366 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5367 data->ocd_brw_size = min(data->ocd_brw_size,
5368 (__u32)MD_MAX_BRW_SIZE);
5369 if (data->ocd_brw_size == 0) {
5370 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5371 " ocd_version: %x ocd_grant: %d "
5372 "ocd_index: %u ocd_brw_size is "
5373 "unexpectedly zero, network data "
5374 "corruption? Refusing connection of this"
5377 exp->exp_client_uuid.uuid,
5378 exp, data->ocd_connect_flags, data->ocd_version,
5379 data->ocd_grant, data->ocd_index);
5384 /* NB: Disregard the rule against updating
5385 * exp_connect_data.ocd_connect_flags in this case, since
5386 * tgt_client_new() needs to know if this is a lightweight
5387 * connection, and it is safe to expose this flag before
5388 * connection processing completes. */
5389 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5390 spin_lock(&exp->exp_lock);
5391 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5392 spin_unlock(&exp->exp_lock);
5395 data->ocd_version = LUSTRE_VERSION_CODE;
5396 exp->exp_connect_data = *data;
5397 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5399 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5400 CWARN("%s: MDS requires FID support, but client not\n",
5405 if (mdt->mdt_som_conf &&
5406 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5407 OBD_CONNECT_MDS_MDS |
5408 OBD_CONNECT_SOM))) {
5409 CWARN("%s: MDS has SOM enabled, but client does not support "
5410 "it\n", mdt_obd_name(mdt));
5414 if (OCD_HAS_FLAG(data, PINGLESS)) {
5415 if (ptlrpc_pinger_suppress_pings()) {
5416 spin_lock(&exp->exp_obd->obd_dev_lock);
5417 list_del_init(&exp->exp_obd_chain_timed);
5418 spin_unlock(&exp->exp_obd->obd_dev_lock);
5420 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5424 data->ocd_max_easize = mdt->mdt_max_ea_size;
5429 int mdt_connect_check_sptlrpc(struct mdt_device *mdt, struct obd_export *exp,
5430 struct ptlrpc_request *req)
5432 struct sptlrpc_flavor flvr;
5435 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5436 read_lock(&mdt->mdt_sptlrpc_lock);
5437 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5441 read_unlock(&mdt->mdt_sptlrpc_lock);
5443 spin_lock(&exp->exp_lock);
5445 exp->exp_sp_peer = req->rq_sp_from;
5446 exp->exp_flvr = flvr;
5448 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5449 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5450 CERROR("unauthorized rpc flavor %x from %s, "
5451 "expect %x\n", req->rq_flvr.sf_rpc,
5452 libcfs_nid2str(req->rq_peer.nid),
5453 exp->exp_flvr.sf_rpc);
5457 spin_unlock(&exp->exp_lock);
5459 if (exp->exp_sp_peer != req->rq_sp_from) {
5460 CERROR("RPC source %s doesn't match %s\n",
5461 sptlrpc_part2name(req->rq_sp_from),
5462 sptlrpc_part2name(exp->exp_sp_peer));
5465 rc = sptlrpc_target_export_check(exp, req);
5472 /* mds_connect copy */
5473 static int mdt_obd_connect(const struct lu_env *env,
5474 struct obd_export **exp, struct obd_device *obd,
5475 struct obd_uuid *cluuid,
5476 struct obd_connect_data *data,
5479 struct obd_export *lexp;
5480 struct lustre_handle conn = { 0 };
5481 struct mdt_device *mdt;
5485 LASSERT(env != NULL);
5486 if (!exp || !obd || !cluuid)
5489 mdt = mdt_dev(obd->obd_lu_dev);
5492 * first, check whether the stack is ready to handle requests
5493 * XXX: probably not very appropriate method is used now
5494 * at some point we should find a better one
5496 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5497 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5498 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5501 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5504 rc = class_connect(&conn, obd, cluuid);
5508 lexp = class_conn2export(&conn);
5509 LASSERT(lexp != NULL);
5511 rc = mdt_connect_internal(lexp, mdt, data);
5513 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5516 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5517 rc = tgt_client_new(env, lexp);
5519 mdt_export_stats_init(obd, lexp, localdata);
5523 class_disconnect(lexp);
5532 static int mdt_obd_reconnect(const struct lu_env *env,
5533 struct obd_export *exp, struct obd_device *obd,
5534 struct obd_uuid *cluuid,
5535 struct obd_connect_data *data,
5541 if (exp == NULL || obd == NULL || cluuid == NULL)
5544 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5546 mdt_export_stats_init(obd, exp, localdata);
5551 static int mdt_export_cleanup(struct obd_export *exp)
5553 struct mdt_export_data *med = &exp->exp_mdt_data;
5554 struct obd_device *obd = exp->exp_obd;
5555 struct mdt_device *mdt;
5556 struct mdt_thread_info *info;
5558 CFS_LIST_HEAD(closing_list);
5559 struct mdt_file_data *mfd, *n;
5563 spin_lock(&med->med_open_lock);
5564 while (!cfs_list_empty(&med->med_open_head)) {
5565 cfs_list_t *tmp = med->med_open_head.next;
5566 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5568 /* Remove mfd handle so it can't be found again.
5569 * We are consuming the mfd_list reference here. */
5570 class_handle_unhash(&mfd->mfd_handle);
5571 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5573 spin_unlock(&med->med_open_lock);
5574 mdt = mdt_dev(obd->obd_lu_dev);
5575 LASSERT(mdt != NULL);
5577 rc = lu_env_init(&env, LCT_MD_THREAD);
5581 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5582 LASSERT(info != NULL);
5583 memset(info, 0, sizeof *info);
5584 info->mti_env = &env;
5585 info->mti_mdt = mdt;
5586 info->mti_exp = exp;
5588 if (!cfs_list_empty(&closing_list)) {
5589 struct md_attr *ma = &info->mti_attr;
5591 /* Close any open files (which may also cause orphan unlinking). */
5592 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5593 cfs_list_del_init(&mfd->mfd_list);
5594 ma->ma_need = ma->ma_valid = 0;
5595 /* Don't unlink orphan on failover umount, LU-184 */
5596 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5597 ma->ma_valid = MA_FLAGS;
5598 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5600 mdt_mfd_close(info, mfd);
5603 info->mti_mdt = NULL;
5604 /* cleanup client slot early */
5605 /* Do not erase record for recoverable client. */
5606 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5607 tgt_client_del(&env, exp);
5613 static int mdt_obd_disconnect(struct obd_export *exp)
5619 class_export_get(exp);
5621 rc = server_disconnect_export(exp);
5623 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5625 rc = mdt_export_cleanup(exp);
5626 class_export_put(exp);
5630 /* FIXME: Can we avoid using these two interfaces? */
5631 static int mdt_init_export(struct obd_export *exp)
5633 struct mdt_export_data *med = &exp->exp_mdt_data;
5637 CFS_INIT_LIST_HEAD(&med->med_open_head);
5638 spin_lock_init(&med->med_open_lock);
5639 mutex_init(&med->med_idmap_mutex);
5640 med->med_idmap = NULL;
5641 spin_lock(&exp->exp_lock);
5642 exp->exp_connecting = 1;
5643 spin_unlock(&exp->exp_lock);
5645 /* self-export doesn't need client data and ldlm initialization */
5646 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5647 &exp->exp_client_uuid)))
5650 rc = tgt_client_alloc(exp);
5654 rc = ldlm_init_export(exp);
5661 tgt_client_free(exp);
5663 CERROR("%s: Failed to initialize export: rc = %d\n",
5664 exp->exp_obd->obd_name, rc);
5668 static int mdt_destroy_export(struct obd_export *exp)
5672 if (exp_connect_rmtclient(exp))
5673 mdt_cleanup_idmap(&exp->exp_mdt_data);
5675 target_destroy_export(exp);
5676 /* destroy can be called from failed obd_setup, so
5677 * checking uuid is safer than obd_self_export */
5678 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5679 &exp->exp_client_uuid)))
5682 ldlm_destroy_export(exp);
5683 tgt_client_free(exp);
5685 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5686 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5691 /** The maximum depth that fid2path() will search.
5692 * This is limited only because we want to store the fids for
5693 * historical path lookup purposes.
5695 #define MAX_PATH_DEPTH 100
5697 /** mdt_path() lookup structure. */
5698 struct path_lookup_info {
5699 __u64 pli_recno; /**< history point */
5700 __u64 pli_currec; /**< current record */
5701 struct lu_fid pli_fid;
5702 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5703 struct mdt_object *pli_mdt_obj;
5704 char *pli_path; /**< full path */
5706 int pli_linkno; /**< which hardlink to follow */
5707 int pli_fidcount; /**< number of \a pli_fids */
5710 static int mdt_links_read(struct mdt_thread_info *info,
5711 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5715 LASSERT(ldata->ld_buf->lb_buf != NULL);
5717 if (!mdt_object_exists(mdt_obj))
5720 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5721 ldata->ld_buf, XATTR_NAME_LINK);
5722 if (rc == -ERANGE) {
5723 /* Buf was too small, figure out what we need. */
5724 lu_buf_free(ldata->ld_buf);
5725 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5726 ldata->ld_buf, XATTR_NAME_LINK);
5729 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5730 if (ldata->ld_buf->lb_buf == NULL)
5732 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5733 ldata->ld_buf, XATTR_NAME_LINK);
5738 return linkea_init(ldata);
5741 static int mdt_path_current(struct mdt_thread_info *info,
5742 struct path_lookup_info *pli)
5744 struct mdt_device *mdt = info->mti_mdt;
5745 struct mdt_object *mdt_obj;
5746 struct link_ea_header *leh;
5747 struct link_ea_entry *lee;
5748 struct lu_name *tmpname = &info->mti_name;
5749 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5750 struct lu_buf *buf = &info->mti_big_buf;
5753 struct linkea_data ldata = { 0 };
5757 /* temp buffer for path element, the buffer will be finally freed
5758 * in mdt_thread_info_fini */
5759 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5760 if (buf->lb_buf == NULL)
5764 ptr = pli->pli_path + pli->pli_pathlen - 1;
5767 pli->pli_fidcount = 0;
5768 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5770 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5771 * so checking root_fid can only happen on MDT0. */
5772 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5773 &pli->pli_fids[pli->pli_fidcount])) {
5774 mdt_obj = mdt_object_find(info->mti_env, mdt,
5775 &pli->pli_fids[pli->pli_fidcount]);
5776 if (IS_ERR(mdt_obj))
5777 GOTO(out, rc = PTR_ERR(mdt_obj));
5778 if (mdt_object_remote(mdt_obj)) {
5779 mdt_object_put(info->mti_env, mdt_obj);
5780 GOTO(remote_out, rc = -EREMOTE);
5782 if (!mdt_object_exists(mdt_obj)) {
5783 mdt_object_put(info->mti_env, mdt_obj);
5784 GOTO(out, rc = -ENOENT);
5787 rc = mdt_links_read(info, mdt_obj, &ldata);
5788 mdt_object_put(info->mti_env, mdt_obj);
5793 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5794 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5795 /* If set, use link #linkno for path lookup, otherwise use
5796 link #0. Only do this for the final path element. */
5797 if ((pli->pli_fidcount == 0) &&
5798 (pli->pli_linkno < leh->leh_reccount)) {
5800 for (count = 0; count < pli->pli_linkno; count++) {
5801 lee = (struct link_ea_entry *)
5802 ((char *)lee + reclen);
5803 linkea_entry_unpack(lee, &reclen, tmpname,
5806 if (pli->pli_linkno < leh->leh_reccount - 1)
5807 /* indicate to user there are more links */
5811 /* Pack the name in the end of the buffer */
5812 ptr -= tmpname->ln_namelen;
5813 if (ptr - 1 <= pli->pli_path)
5814 GOTO(out, rc = -EOVERFLOW);
5815 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5818 /* Store the parent fid for historic lookup */
5819 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5820 GOTO(out, rc = -EOVERFLOW);
5821 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5825 ptr++; /* skip leading / */
5826 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5833 /* Returns the full path to this fid, as of changelog record recno. */
5834 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5835 char *path, int pathlen, __u64 *recno, int *linkno,
5838 struct mdt_device *mdt = info->mti_mdt;
5839 struct path_lookup_info *pli;
5847 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5856 pli->pli_mdt_obj = obj;
5857 pli->pli_recno = *recno;
5858 pli->pli_path = path;
5859 pli->pli_pathlen = pathlen;
5860 pli->pli_linkno = *linkno;
5862 /* Retry multiple times in case file is being moved */
5863 while (tries-- && rc == -EAGAIN)
5864 rc = mdt_path_current(info, pli);
5866 /* return the last resolved fids to the client, so the client will
5867 * build the left path on another MDT for remote object */
5868 *fid = pli->pli_fids[pli->pli_fidcount];
5870 *recno = pli->pli_currec;
5871 /* Return next link index to caller */
5872 *linkno = pli->pli_linkno;
5879 static int mdt_fid2path(struct mdt_thread_info *info,
5880 struct getinfo_fid2path *fp)
5882 struct mdt_device *mdt = info->mti_mdt;
5883 struct mdt_object *obj;
5887 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5888 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5890 if (!fid_is_sane(&fp->gf_fid))
5893 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5894 CWARN("%s: "DFID" is invalid, sequence should be "
5895 ">= "LPX64"\n", mdt_obd_name(mdt),
5896 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5900 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5901 if (obj == NULL || IS_ERR(obj)) {
5902 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5907 if (mdt_object_remote(obj))
5909 else if (!mdt_object_exists(obj))
5915 mdt_object_put(info->mti_env, obj);
5916 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5917 PFID(&fp->gf_fid), rc);
5921 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5922 &fp->gf_linkno, &fp->gf_fid);
5924 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5925 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5927 mdt_object_put(info->mti_env, obj);
5932 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5933 void *val, int vallen)
5935 struct getinfo_fid2path *fpout, *fpin;
5938 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5941 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5942 lustre_swab_fid2path(fpin);
5944 memcpy(fpout, fpin, sizeof(*fpin));
5945 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5948 rc = mdt_fid2path(info, fpout);
5952 int mdt_get_info(struct mdt_thread_info *info)
5954 struct ptlrpc_request *req = mdt_info_req(info);
5962 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5964 CDEBUG(D_IOCTL, "No GETINFO key");
5967 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5970 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5971 if (vallen == NULL) {
5972 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5976 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5978 rc = req_capsule_server_pack(info->mti_pill);
5979 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5980 if (valout == NULL) {
5981 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5985 if (KEY_IS(KEY_FID2PATH))
5986 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5990 lustre_msg_set_status(req->rq_repmsg, rc);
5995 /* Pass the ioc down */
5996 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5997 unsigned int cmd, int len, void *data)
5999 struct lu_context ioctl_session;
6000 struct md_device *next = mdt->mdt_child;
6004 rc = lu_context_init(&ioctl_session, LCT_SESSION);
6007 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
6008 lu_context_enter(&ioctl_session);
6009 env->le_ses = &ioctl_session;
6011 LASSERT(next->md_ops->mdo_iocontrol);
6012 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
6014 lu_context_exit(&ioctl_session);
6015 lu_context_fini(&ioctl_session);
6019 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6021 struct obd_ioctl_data *data = karg;
6024 struct mdt_object *obj;
6025 struct mdt_lock_handle *lh;
6029 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6030 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6033 fid = (struct lu_fid *)data->ioc_inlbuf1;
6035 if (!fid_is_sane(fid))
6038 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6040 lh = &mti->mti_lh[MDT_LH_PARENT];
6041 mdt_lock_reg_init(lh, LCK_CR);
6043 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6045 RETURN(PTR_ERR(obj));
6047 if (mdt_object_remote(obj)) {
6050 * before calling version get the correct MDS should be
6051 * fid, this is error to find remote object here
6053 CERROR("nonlocal object "DFID"\n", PFID(fid));
6054 } else if (!mdt_object_exists(obj)) {
6055 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6058 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6059 *(__u64 *)data->ioc_inlbuf2 = version;
6062 mdt_object_unlock_put(mti, obj, lh, 1);
6066 /* ioctls on obd dev */
6067 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6068 void *karg, void *uarg)
6071 struct obd_device *obd = exp->exp_obd;
6072 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6073 struct dt_device *dt = mdt->mdt_bottom;
6077 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6078 rc = lu_env_init(&env, LCT_MD_THREAD);
6084 rc = mdt_device_sync(&env, mdt);
6086 case OBD_IOC_SET_READONLY:
6087 rc = dt->dd_ops->dt_ro(&env, dt);
6089 case OBD_IOC_ABORT_RECOVERY:
6090 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6091 target_stop_recovery_thread(obd);
6094 case OBD_IOC_CHANGELOG_REG:
6095 case OBD_IOC_CHANGELOG_DEREG:
6096 case OBD_IOC_CHANGELOG_CLEAR:
6097 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6099 case OBD_IOC_START_LFSCK: {
6100 struct md_device *next = mdt->mdt_child;
6101 struct obd_ioctl_data *data = karg;
6102 struct lfsck_start_param lsp;
6104 if (unlikely(data == NULL)) {
6109 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6110 lsp.lsp_namespace = mdt->mdt_namespace;
6111 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6114 case OBD_IOC_STOP_LFSCK: {
6115 struct md_device *next = mdt->mdt_child;
6117 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
6120 case OBD_IOC_GET_OBJ_VERSION: {
6121 struct mdt_thread_info *mti;
6122 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6123 memset(mti, 0, sizeof *mti);
6124 mti->mti_env = &env;
6128 rc = mdt_ioc_version_get(mti, karg);
6133 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6134 mdt_obd_name(mdt), cmd, rc);
6141 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6143 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6147 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6151 int mdt_obd_postrecov(struct obd_device *obd)
6156 rc = lu_env_init(&env, LCT_MD_THREAD);
6159 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6164 static struct obd_ops mdt_obd_device_ops = {
6165 .o_owner = THIS_MODULE,
6166 .o_set_info_async = mdt_obd_set_info_async,
6167 .o_connect = mdt_obd_connect,
6168 .o_reconnect = mdt_obd_reconnect,
6169 .o_disconnect = mdt_obd_disconnect,
6170 .o_init_export = mdt_init_export,
6171 .o_destroy_export = mdt_destroy_export,
6172 .o_iocontrol = mdt_iocontrol,
6173 .o_postrecov = mdt_obd_postrecov,
6176 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6177 struct lu_device *d)
6179 struct mdt_device *m = mdt_dev(d);
6186 static struct lu_device *mdt_device_free(const struct lu_env *env,
6187 struct lu_device *d)
6189 struct mdt_device *m = mdt_dev(d);
6192 lu_device_fini(&m->mdt_lu_dev);
6198 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6199 struct lu_device_type *t,
6200 struct lustre_cfg *cfg)
6202 struct lu_device *l;
6203 struct mdt_device *m;
6210 rc = mdt_init0(env, m, t, cfg);
6212 mdt_device_free(env, l);
6217 l = ERR_PTR(-ENOMEM);
6221 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6222 LU_KEY_INIT(mdt, struct mdt_thread_info);
6224 static void mdt_key_fini(const struct lu_context *ctx,
6225 struct lu_context_key *key, void* data)
6227 struct mdt_thread_info *info = data;
6229 if (info->mti_big_lmm) {
6230 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6231 info->mti_big_lmm = NULL;
6232 info->mti_big_lmmsize = 0;
6237 /* context key: mdt_thread_key */
6238 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6240 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6242 return lu_ucred(info->mti_env);
6245 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6247 return lu_ucred_check(info->mti_env);
6251 * Enable/disable COS (Commit On Sharing).
6253 * Set/Clear the COS flag in mdt options.
6255 * \param mdt mdt device
6256 * \param val 0 disables COS, other values enable COS
6258 void mdt_enable_cos(struct mdt_device *mdt, int val)
6263 mdt->mdt_opts.mo_cos = !!val;
6264 rc = lu_env_init(&env, LCT_LOCAL);
6265 if (unlikely(rc != 0)) {
6266 CWARN("lu_env initialization failed with rc = %d,"
6267 "cannot sync\n", rc);
6270 mdt_device_sync(&env, mdt);
6275 * Check COS (Commit On Sharing) status.
6277 * Return COS flag status.
6279 * \param mdt mdt device
6281 int mdt_cos_is_enabled(struct mdt_device *mdt)
6283 return mdt->mdt_opts.mo_cos != 0;
6286 static struct lu_device_type_operations mdt_device_type_ops = {
6287 .ldto_device_alloc = mdt_device_alloc,
6288 .ldto_device_free = mdt_device_free,
6289 .ldto_device_fini = mdt_device_fini
6292 static struct lu_device_type mdt_device_type = {
6293 .ldt_tags = LU_DEVICE_MD,
6294 .ldt_name = LUSTRE_MDT_NAME,
6295 .ldt_ops = &mdt_device_type_ops,
6296 .ldt_ctx_tags = LCT_MD_THREAD
6299 static int __init mdt_mod_init(void)
6301 struct lprocfs_static_vars lvars;
6304 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6305 FID_NOBRACE_LEN + 1);
6306 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6308 rc = lu_kmem_init(mdt_caches);
6312 rc = mds_mod_init();
6316 lprocfs_mdt_init_vars(&lvars);
6317 rc = class_register_type(&mdt_obd_device_ops, NULL,
6318 lvars.module_vars, LUSTRE_MDT_NAME,
6324 lu_kmem_fini(mdt_caches);
6331 static void __exit mdt_mod_exit(void)
6333 class_unregister_type(LUSTRE_MDT_NAME);
6335 lu_kmem_fini(mdt_caches);
6338 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6339 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6340 MODULE_LICENSE("GPL");
6342 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);