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_mdt.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
70 #include <lustre_quota.h>
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73 [LCK_MINMODE] = MDL_MINMODE,
80 [LCK_GROUP] = MDL_GROUP
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84 [MDL_MINMODE] = LCK_MINMODE,
91 [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);
97 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
98 struct getinfo_fid2path *fp);
100 static const struct lu_object_operations mdt_obj_ops;
102 /* Slab for MDT object allocation */
103 static cfs_mem_cache_t *mdt_object_kmem;
105 static struct lu_kmem_descr mdt_caches[] = {
107 .ckd_cache = &mdt_object_kmem,
108 .ckd_name = "mdt_obj",
109 .ckd_size = sizeof(struct mdt_object)
116 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
120 return (rep->lock_policy_res1 & flag);
123 void mdt_clear_disposition(struct mdt_thread_info *info,
124 struct ldlm_reply *rep, int flag)
127 info->mti_opdata &= ~flag;
129 rep->lock_policy_res1 &= ~flag;
132 void mdt_set_disposition(struct mdt_thread_info *info,
133 struct ldlm_reply *rep, int flag)
136 info->mti_opdata |= flag;
138 rep->lock_policy_res1 |= flag;
141 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
143 lh->mlh_pdo_hash = 0;
144 lh->mlh_reg_mode = lm;
145 lh->mlh_type = MDT_REG_LOCK;
148 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
149 const char *name, int namelen)
151 lh->mlh_reg_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);
158 LASSERT(namelen == 0);
159 lh->mlh_pdo_hash = 0ull;
163 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
164 struct mdt_lock_handle *lh)
170 * Any dir access needs couple of locks:
172 * 1) on part of dir we gonna take lookup/modify;
174 * 2) on whole dir to protect it from concurrent splitting and/or to
175 * flush client's cache for readdir().
177 * so, for a given mode and object this routine decides what lock mode
178 * to use for lock #2:
180 * 1) if caller's gonna lookup in dir then we need to protect dir from
181 * being splitted only - LCK_CR
183 * 2) if caller's gonna modify dir then we need to protect dir from
184 * being splitted and to flush cache - LCK_CW
186 * 3) if caller's gonna modify dir and that dir seems ready for
187 * splitting then we need to protect it from any type of access
188 * (lookup/modify/split) - LCK_EX --bzzz
191 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
192 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
195 * Ask underlaying level its opinion about preferable PDO lock mode
196 * having access type passed as regular lock mode:
198 * - MDL_MINMODE means that lower layer does not want to specify lock
201 * - MDL_NL means that no PDO lock should be taken. This is used in some
202 * cases. Say, for non-splittable directories no need to use PDO locks
205 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
206 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
208 if (mode != MDL_MINMODE) {
209 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
212 * Lower layer does not want to specify locking mode. We do it
213 * our selves. No special protection is needed, just flush
214 * client's cache on modification and allow concurrent
217 switch (lh->mlh_reg_mode) {
219 lh->mlh_pdo_mode = LCK_EX;
222 lh->mlh_pdo_mode = LCK_CR;
225 lh->mlh_pdo_mode = LCK_CW;
228 CERROR("Not expected lock type (0x%x)\n",
229 (int)lh->mlh_reg_mode);
234 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
238 int mdt_getstatus(struct mdt_thread_info *info)
240 struct mdt_device *mdt = info->mti_mdt;
241 struct md_device *next = mdt->mdt_child;
242 struct mdt_body *repbody;
247 rc = mdt_check_ucred(info);
249 RETURN(err_serious(rc));
251 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
252 RETURN(err_serious(-ENOMEM));
254 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
255 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
259 repbody->valid |= OBD_MD_FLID;
261 if (mdt->mdt_opts.mo_mds_capa &&
262 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
263 struct mdt_object *root;
264 struct lustre_capa *capa;
266 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
268 RETURN(PTR_ERR(root));
270 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
272 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
273 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
275 mdt_object_put(info->mti_env, root);
277 repbody->valid |= OBD_MD_FLMDSCAPA;
283 int mdt_statfs(struct mdt_thread_info *info)
285 struct ptlrpc_request *req = mdt_info_req(info);
286 struct md_device *next = info->mti_mdt->mdt_child;
287 struct ptlrpc_service_part *svcpt;
288 struct obd_statfs *osfs;
293 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
295 /* This will trigger a watchdog timeout */
296 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
297 (MDT_SERVICE_WATCHDOG_FACTOR *
298 at_get(&svcpt->scp_at_estimate)) + 1);
300 rc = mdt_check_ucred(info);
302 RETURN(err_serious(rc));
304 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
305 RETURN(err_serious(-ENOMEM));
307 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
311 /** statfs information are cached in the mdt_device */
312 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
313 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
314 /** statfs data is too old, get up-to-date one */
315 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
318 spin_lock(&info->mti_mdt->mdt_osfs_lock);
319 info->mti_mdt->mdt_osfs = *osfs;
320 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
321 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
323 /** use cached statfs data */
324 spin_lock(&info->mti_mdt->mdt_osfs_lock);
325 *osfs = info->mti_mdt->mdt_osfs;
326 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
330 mdt_counter_incr(req, LPROC_MDT_STATFS);
336 * Pack SOM attributes into the reply.
337 * Call under a DLM UPDATE lock.
339 static void mdt_pack_size2body(struct mdt_thread_info *info,
340 struct mdt_object *mo)
343 struct md_attr *ma = &info->mti_attr;
345 LASSERT(ma->ma_attr.la_valid & LA_MODE);
346 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
348 /* Check if Size-on-MDS is supported, if this is a regular file,
349 * if SOM is enabled on the object and if SOM cache exists and valid.
350 * Otherwise do not pack Size-on-MDS attributes to the reply. */
351 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
352 !S_ISREG(ma->ma_attr.la_mode) ||
353 !mdt_object_is_som_enabled(mo) ||
354 !(ma->ma_valid & MA_SOM))
357 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
358 b->size = ma->ma_som->msd_size;
359 b->blocks = ma->ma_som->msd_blocks;
362 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
363 const struct lu_attr *attr, const struct lu_fid *fid)
365 struct md_attr *ma = &info->mti_attr;
367 LASSERT(ma->ma_valid & MA_INODE);
369 b->atime = attr->la_atime;
370 b->mtime = attr->la_mtime;
371 b->ctime = attr->la_ctime;
372 b->mode = attr->la_mode;
373 b->size = attr->la_size;
374 b->blocks = attr->la_blocks;
375 b->uid = attr->la_uid;
376 b->gid = attr->la_gid;
377 b->flags = attr->la_flags;
378 b->nlink = attr->la_nlink;
379 b->rdev = attr->la_rdev;
381 /*XXX should pack the reply body according to lu_valid*/
382 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
383 OBD_MD_FLGID | OBD_MD_FLTYPE |
384 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
385 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
387 if (!S_ISREG(attr->la_mode)) {
388 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
389 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
390 /* means no objects are allocated on osts. */
391 LASSERT(!(ma->ma_valid & MA_LOV));
392 /* just ignore blocks occupied by extend attributes on MDS */
394 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
395 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
400 b->valid |= OBD_MD_FLID;
402 /* FIXME: these should be fixed when new igif ready.*/
403 b->ino = fid_oid(fid); /* 1.6 compatibility */
404 b->generation = fid_ver(fid); /* 1.6 compatibility */
405 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
407 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
408 PFID(fid), b->nlink, b->mode, b->size);
412 mdt_body_reverse_idmap(info, b);
414 if (b->valid & OBD_MD_FLSIZE)
415 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
416 PFID(fid), (unsigned long long)b->size);
419 static inline int mdt_body_has_lov(const struct lu_attr *la,
420 const struct mdt_body *body)
422 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
423 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
426 void mdt_client_compatibility(struct mdt_thread_info *info)
428 struct mdt_body *body;
429 struct ptlrpc_request *req = mdt_info_req(info);
430 struct obd_export *exp = req->rq_export;
431 struct md_attr *ma = &info->mti_attr;
432 struct lu_attr *la = &ma->ma_attr;
435 if (exp->exp_connect_flags & OBD_CONNECT_LAYOUTLOCK)
436 /* the client can deal with 16-bit lmm_stripe_count */
439 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
441 if (!mdt_body_has_lov(la, body))
444 /* now we have a reply with a lov for a client not compatible with the
445 * layout lock so we have to clean the layout generation number */
446 if (S_ISREG(la->la_mode))
447 ma->ma_lmm->lmm_layout_gen = 0;
451 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
454 const struct lu_env *env = info->mti_env;
458 LASSERT(info->mti_big_lmm_used == 0);
459 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
463 /* big_lmm may need to be grown */
464 if (info->mti_big_lmmsize < rc) {
465 int size = size_roundup_power2(rc);
467 if (info->mti_big_lmmsize > 0) {
468 /* free old buffer */
469 LASSERT(info->mti_big_lmm);
470 OBD_FREE_LARGE(info->mti_big_lmm,
471 info->mti_big_lmmsize);
472 info->mti_big_lmm = NULL;
473 info->mti_big_lmmsize = 0;
476 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
477 if (info->mti_big_lmm == NULL)
479 info->mti_big_lmmsize = size;
481 LASSERT(info->mti_big_lmmsize >= rc);
483 info->mti_buf.lb_buf = info->mti_big_lmm;
484 info->mti_buf.lb_len = info->mti_big_lmmsize;
485 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
490 int mdt_attr_get_lov(struct mdt_thread_info *info,
491 struct mdt_object *o, struct md_attr *ma)
493 struct md_object *next = mdt_object_child(o);
494 struct lu_buf *buf = &info->mti_buf;
497 buf->lb_buf = ma->ma_lmm;
498 buf->lb_len = ma->ma_lmm_size;
499 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
501 ma->ma_lmm_size = rc;
502 ma->ma_valid |= MA_LOV;
504 } else if (rc == -ENODATA) {
507 } else if (rc == -ERANGE) {
508 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
510 info->mti_big_lmm_used = 1;
511 ma->ma_valid |= MA_LOV;
512 ma->ma_lmm = info->mti_big_lmm;
513 ma->ma_lmm_size = rc;
514 /* update mdt_max_mdsize so all clients
515 * will be aware about that */
516 if (info->mti_mdt->mdt_max_mdsize < rc)
517 info->mti_mdt->mdt_max_mdsize = rc;
525 int mdt_attr_get_pfid(struct mdt_thread_info *info,
526 struct mdt_object *o, struct lu_fid *pfid)
528 struct lu_buf *buf = &info->mti_buf;
529 struct link_ea_header *leh;
530 struct link_ea_entry *lee;
534 buf->lb_buf = info->mti_big_lmm;
535 buf->lb_len = info->mti_big_lmmsize;
536 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
537 buf, XATTR_NAME_LINK);
538 /* ignore errors, MA_PFID won't be set and it is
539 * up to the caller to treat this as an error */
540 if (rc == -ERANGE || buf->lb_len == 0) {
541 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
542 buf->lb_buf = info->mti_big_lmm;
543 buf->lb_len = info->mti_big_lmmsize;
548 if (rc < sizeof(*leh)) {
549 CERROR("short LinkEA on "DFID": rc = %d\n",
550 PFID(mdt_object_fid(o)), rc);
554 leh = (struct link_ea_header *) buf->lb_buf;
555 lee = (struct link_ea_entry *)(leh + 1);
556 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
557 leh->leh_magic = LINK_EA_MAGIC;
558 leh->leh_reccount = __swab32(leh->leh_reccount);
559 leh->leh_len = __swab64(leh->leh_len);
561 if (leh->leh_magic != LINK_EA_MAGIC)
563 if (leh->leh_reccount == 0)
566 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
567 fid_be_to_cpu(pfid, pfid);
572 int mdt_attr_get_complex(struct mdt_thread_info *info,
573 struct mdt_object *o, struct md_attr *ma)
575 const struct lu_env *env = info->mti_env;
576 struct md_object *next = mdt_object_child(o);
577 struct lu_buf *buf = &info->mti_buf;
578 u32 mode = lu_object_attr(&next->mo_lu);
579 int need = ma->ma_need;
585 if (need & MA_INODE) {
586 ma->ma_need = MA_INODE;
587 rc = mo_attr_get(env, next, ma);
590 ma->ma_valid |= MA_INODE;
593 if (need & MA_PFID) {
594 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
596 ma->ma_valid |= MA_PFID;
597 /* ignore this error, parent fid is not mandatory */
601 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
602 rc = mdt_attr_get_lov(info, o, ma);
607 if (need & MA_LMV && S_ISDIR(mode)) {
608 buf->lb_buf = ma->ma_lmv;
609 buf->lb_len = ma->ma_lmv_size;
610 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
612 ma->ma_lmv_size = rc2;
613 ma->ma_valid |= MA_LMV;
614 } else if (rc2 == -ENODATA) {
621 if (need & MA_SOM && S_ISREG(mode)) {
622 buf->lb_buf = info->mti_xattr_buf;
623 buf->lb_len = sizeof(info->mti_xattr_buf);
624 CLASSERT(sizeof(struct som_attrs) <=
625 sizeof(info->mti_xattr_buf));
626 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
627 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
629 ma->ma_valid |= MA_SOM;
630 else if (rc2 < 0 && rc2 != -ENODATA)
634 if (need & MA_HSM && S_ISREG(mode)) {
635 buf->lb_buf = info->mti_xattr_buf;
636 buf->lb_len = sizeof(info->mti_xattr_buf);
637 CLASSERT(sizeof(struct hsm_attrs) <=
638 sizeof(info->mti_xattr_buf));
639 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
640 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
642 ma->ma_valid |= MA_HSM;
643 else if (rc2 < 0 && rc2 != -ENODATA)
647 #ifdef CONFIG_FS_POSIX_ACL
648 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
649 buf->lb_buf = ma->ma_acl;
650 buf->lb_len = ma->ma_acl_size;
651 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
653 ma->ma_acl_size = rc2;
654 ma->ma_valid |= MA_ACL_DEF;
655 } else if (rc2 == -ENODATA) {
664 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
665 rc, ma->ma_valid, ma->ma_lmm);
669 static int mdt_getattr_internal(struct mdt_thread_info *info,
670 struct mdt_object *o, int ma_need)
672 struct md_object *next = mdt_object_child(o);
673 const struct mdt_body *reqbody = info->mti_body;
674 struct ptlrpc_request *req = mdt_info_req(info);
675 struct md_attr *ma = &info->mti_attr;
676 struct lu_attr *la = &ma->ma_attr;
677 struct req_capsule *pill = info->mti_pill;
678 const struct lu_env *env = info->mti_env;
679 struct mdt_body *repbody;
680 struct lu_buf *buffer = &info->mti_buf;
685 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
686 RETURN(err_serious(-ENOMEM));
688 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
692 rc = mdt_object_exists(o);
694 /* This object is located on remote node.*/
695 repbody->fid1 = *mdt_object_fid(o);
696 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
700 buffer->lb_len = reqbody->eadatasize;
701 if (buffer->lb_len > 0)
702 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
704 buffer->lb_buf = NULL;
706 /* If it is dir object and client require MEA, then we got MEA */
707 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
708 reqbody->valid & OBD_MD_MEA) {
709 /* Assumption: MDT_MD size is enough for lmv size. */
710 ma->ma_lmv = buffer->lb_buf;
711 ma->ma_lmv_size = buffer->lb_len;
712 ma->ma_need = MA_LMV | MA_INODE;
714 ma->ma_lmm = buffer->lb_buf;
715 ma->ma_lmm_size = buffer->lb_len;
716 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
719 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
720 reqbody->valid & OBD_MD_FLDIREA &&
721 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
722 /* get default stripe info for this dir. */
723 ma->ma_need |= MA_LOV_DEF;
725 ma->ma_need |= ma_need;
726 if (ma->ma_need & MA_SOM)
727 ma->ma_som = &info->mti_u.som.data;
729 rc = mdt_attr_get_complex(info, o, ma);
731 CERROR("getattr error for "DFID": %d\n",
732 PFID(mdt_object_fid(o)), rc);
736 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
738 /* the Lustre protocol supposes to return default striping
739 * on the user-visible root if explicitly requested */
740 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
741 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
742 struct lu_fid rootfid;
743 struct mdt_object *root;
744 struct mdt_device *mdt = info->mti_mdt;
746 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
749 root = mdt_object_find(env, mdt, &rootfid);
751 RETURN(PTR_ERR(root));
752 rc = mdt_attr_get_lov(info, root, ma);
753 mdt_object_put(info->mti_env, root);
755 CERROR("getattr error for "DFID": %d\n",
756 PFID(mdt_object_fid(o)), rc);
761 if (likely(ma->ma_valid & MA_INODE))
762 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
766 if (mdt_body_has_lov(la, reqbody)) {
767 if (ma->ma_valid & MA_LOV) {
768 LASSERT(ma->ma_lmm_size);
769 mdt_dump_lmm(D_INFO, ma->ma_lmm);
770 repbody->eadatasize = ma->ma_lmm_size;
771 if (S_ISDIR(la->la_mode))
772 repbody->valid |= OBD_MD_FLDIREA;
774 repbody->valid |= OBD_MD_FLEASIZE;
776 if (ma->ma_valid & MA_LMV) {
777 LASSERT(S_ISDIR(la->la_mode));
778 repbody->eadatasize = ma->ma_lmv_size;
779 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
781 } else if (S_ISLNK(la->la_mode) &&
782 reqbody->valid & OBD_MD_LINKNAME) {
783 buffer->lb_buf = ma->ma_lmm;
784 /* eadatasize from client includes NULL-terminator, so
785 * there is no need to read it */
786 buffer->lb_len = reqbody->eadatasize - 1;
787 rc = mo_readlink(env, next, buffer);
788 if (unlikely(rc <= 0)) {
789 CERROR("readlink failed: %d\n", rc);
792 int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
794 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
796 repbody->valid |= OBD_MD_LINKNAME;
797 /* we need to report back size with NULL-terminator
798 * because client expects that */
799 repbody->eadatasize = rc + 1;
800 if (repbody->eadatasize != reqbody->eadatasize)
801 CERROR("Read shorter symlink %d, expected %d\n",
802 rc, reqbody->eadatasize - 1);
804 ((char *)ma->ma_lmm)[rc] = 0;
806 /* If the total CDEBUG() size is larger than a page, it
807 * will print a warning to the console, avoid this by
808 * printing just the last part of the symlink. */
809 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
810 print_limit < rc ? "..." : "", print_limit,
811 (char *)ma->ma_lmm + rc - print_limit, rc);
816 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
817 repbody->max_cookiesize = 0;
818 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
819 repbody->valid |= OBD_MD_FLMODEASIZE;
820 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
821 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
822 repbody->max_cookiesize);
825 if (exp_connect_rmtclient(info->mti_exp) &&
826 reqbody->valid & OBD_MD_FLRMTPERM) {
827 void *buf = req_capsule_server_get(pill, &RMF_ACL);
829 /* mdt_getattr_lock only */
830 rc = mdt_pack_remote_perm(info, o, buf);
832 repbody->valid &= ~OBD_MD_FLRMTPERM;
833 repbody->aclsize = 0;
836 repbody->valid |= OBD_MD_FLRMTPERM;
837 repbody->aclsize = sizeof(struct mdt_remote_perm);
840 #ifdef CONFIG_FS_POSIX_ACL
841 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
842 (reqbody->valid & OBD_MD_FLACL)) {
843 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
844 buffer->lb_len = req_capsule_get_size(pill,
845 &RMF_ACL, RCL_SERVER);
846 if (buffer->lb_len > 0) {
847 rc = mo_xattr_get(env, next, buffer,
848 XATTR_NAME_ACL_ACCESS);
850 if (rc == -ENODATA) {
851 repbody->aclsize = 0;
852 repbody->valid |= OBD_MD_FLACL;
854 } else if (rc == -EOPNOTSUPP) {
857 CERROR("got acl size: %d\n", rc);
860 repbody->aclsize = rc;
861 repbody->valid |= OBD_MD_FLACL;
868 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
869 info->mti_mdt->mdt_opts.mo_mds_capa &&
870 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
871 struct lustre_capa *capa;
873 capa = req_capsule_server_get(pill, &RMF_CAPA1);
875 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
876 rc = mo_capa_get(env, next, capa, 0);
879 repbody->valid |= OBD_MD_FLMDSCAPA;
884 mdt_counter_incr(req, LPROC_MDT_GETATTR);
889 static int mdt_renew_capa(struct mdt_thread_info *info)
891 struct mdt_object *obj = info->mti_object;
892 struct mdt_body *body;
893 struct lustre_capa *capa, *c;
897 /* if object doesn't exist, or server has disabled capability,
898 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
901 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
902 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
905 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
906 LASSERT(body != NULL);
908 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
911 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
915 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
917 body->valid |= OBD_MD_FLOSSCAPA;
921 int mdt_getattr(struct mdt_thread_info *info)
923 struct mdt_object *obj = info->mti_object;
924 struct req_capsule *pill = info->mti_pill;
925 struct mdt_body *reqbody;
926 struct mdt_body *repbody;
931 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
934 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
935 rc = req_capsule_server_pack(pill);
937 RETURN(err_serious(rc));
938 rc = mdt_renew_capa(info);
939 GOTO(out_shrink, rc);
942 LASSERT(obj != NULL);
943 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
945 mode = lu_object_attr(&obj->mot_obj.mo_lu);
947 /* old clients may not report needed easize, use max value then */
948 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
949 reqbody->eadatasize == 0 ?
950 info->mti_mdt->mdt_max_mdsize :
951 reqbody->eadatasize);
953 rc = req_capsule_server_pack(pill);
954 if (unlikely(rc != 0))
955 RETURN(err_serious(rc));
957 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
958 LASSERT(repbody != NULL);
959 repbody->eadatasize = 0;
960 repbody->aclsize = 0;
962 if (reqbody->valid & OBD_MD_FLRMTPERM)
963 rc = mdt_init_ucred(info, reqbody);
965 rc = mdt_check_ucred(info);
967 GOTO(out_shrink, rc);
969 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
972 * Don't check capability at all, because rename might getattr for
973 * remote obj, and at that time no capability is available.
975 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
976 rc = mdt_getattr_internal(info, obj, 0);
977 if (reqbody->valid & OBD_MD_FLRMTPERM)
978 mdt_exit_ucred(info);
981 mdt_client_compatibility(info);
982 rc2 = mdt_fix_reply(info);
988 int mdt_is_subdir(struct mdt_thread_info *info)
990 struct mdt_object *o = info->mti_object;
991 struct req_capsule *pill = info->mti_pill;
992 const struct mdt_body *body = info->mti_body;
993 struct mdt_body *repbody;
999 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1002 * We save last checked parent fid to @repbody->fid1 for remote
1005 LASSERT(fid_is_sane(&body->fid2));
1006 LASSERT(mdt_object_exists(o) > 0);
1007 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1008 &body->fid2, &repbody->fid1);
1009 if (rc == 0 || rc == -EREMOTE)
1010 repbody->valid |= OBD_MD_FLID;
1015 static int mdt_raw_lookup(struct mdt_thread_info *info,
1016 struct mdt_object *parent,
1017 const struct lu_name *lname,
1018 struct ldlm_reply *ldlm_rep)
1020 struct md_object *next = mdt_object_child(info->mti_object);
1021 const struct mdt_body *reqbody = info->mti_body;
1022 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1023 struct mdt_body *repbody;
1027 if (reqbody->valid != OBD_MD_FLID)
1030 LASSERT(!info->mti_cross_ref);
1032 /* Only got the fid of this obj by name */
1033 fid_zero(child_fid);
1034 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1037 /* XXX is raw_lookup possible as intent operation? */
1040 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1043 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1045 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1048 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1049 repbody->fid1 = *child_fid;
1050 repbody->valid = OBD_MD_FLID;
1056 * UPDATE lock should be taken against parent, and be release before exit;
1057 * child_bits lock should be taken against child, and be returned back:
1058 * (1)normal request should release the child lock;
1059 * (2)intent request will grant the lock to client.
1061 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1062 struct mdt_lock_handle *lhc,
1064 struct ldlm_reply *ldlm_rep)
1066 struct ptlrpc_request *req = mdt_info_req(info);
1067 struct mdt_body *reqbody = NULL;
1068 struct mdt_object *parent = info->mti_object;
1069 struct mdt_object *child;
1070 struct md_object *next = mdt_object_child(parent);
1071 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1072 struct lu_name *lname = NULL;
1073 const char *name = NULL;
1075 struct mdt_lock_handle *lhp = NULL;
1076 struct ldlm_lock *lock;
1077 struct ldlm_res_id *res_id;
1084 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1085 LASSERT(ergo(is_resent,
1086 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1088 LASSERT(parent != NULL);
1089 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1091 RETURN(err_serious(-EFAULT));
1093 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1095 if (!info->mti_cross_ref) {
1097 * XXX: Check for "namelen == 0" is for getattr by fid
1098 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1099 * that is the name must contain at least one character and
1100 * the terminating '\0'
1103 reqbody = req_capsule_client_get(info->mti_pill,
1105 if (unlikely(reqbody == NULL))
1106 RETURN(err_serious(-EFAULT));
1108 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1109 RETURN(err_serious(-EINVAL));
1112 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1114 PFID(mdt_object_fid(parent)),
1115 PFID(&reqbody->fid2), ldlm_rep);
1117 lname = mdt_name(info->mti_env, (char *)name, namelen);
1118 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1119 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1123 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1125 rc = mdt_object_exists(parent);
1126 if (unlikely(rc == 0)) {
1127 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1128 &parent->mot_obj.mo_lu,
1129 "Parent doesn't exist!\n");
1131 } else if (!info->mti_cross_ref) {
1132 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
1133 PFID(mdt_object_fid(parent)));
1136 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1144 if (info->mti_cross_ref) {
1145 /* Only getattr on the child. Parent is on another node. */
1146 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1148 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1149 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1152 /* Do not take lock for resent case. */
1153 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1154 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1155 lhc->mlh_reg_lh.cookie);
1156 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1157 &lock->l_resource->lr_name));
1158 LDLM_LOCK_PUT(lock);
1161 mdt_lock_handle_init(lhc);
1162 mdt_lock_reg_init(lhc, LCK_PR);
1165 * Object's name is on another MDS, no lookup lock is
1166 * needed here but update is.
1168 child_bits &= ~MDS_INODELOCK_LOOKUP;
1169 child_bits |= MDS_INODELOCK_UPDATE;
1171 rc = mdt_object_lock(info, child, lhc, child_bits,
1175 /* Finally, we can get attr for child. */
1176 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1178 rc = mdt_getattr_internal(info, child, 0);
1179 if (unlikely(rc != 0))
1180 mdt_object_unlock(info, child, lhc, 1);
1186 /* step 1: lock parent only if parent is a directory */
1187 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1188 lhp = &info->mti_lh[MDT_LH_PARENT];
1189 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1190 rc = mdt_object_lock(info, parent, lhp,
1191 MDS_INODELOCK_UPDATE,
1193 if (unlikely(rc != 0))
1197 /* step 2: lookup child's fid by name */
1198 fid_zero(child_fid);
1199 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1204 mdt_set_disposition(info, ldlm_rep,
1206 GOTO(out_parent, rc);
1208 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1210 *child_fid = reqbody->fid2;
1211 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1215 *step 3: find the child object by fid & lock it.
1216 * regardless if it is local or remote.
1218 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1220 if (unlikely(IS_ERR(child)))
1221 GOTO(out_parent, rc = PTR_ERR(child));
1223 /* Do not take lock for resent case. */
1224 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1225 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1226 lhc->mlh_reg_lh.cookie);
1228 res_id = &lock->l_resource->lr_name;
1229 if (!fid_res_name_eq(mdt_object_fid(child),
1230 &lock->l_resource->lr_name)) {
1231 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1232 &lock->l_resource->lr_name),
1233 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1234 (unsigned long)res_id->name[0],
1235 (unsigned long)res_id->name[1],
1236 (unsigned long)res_id->name[2],
1237 PFID(mdt_object_fid(parent)));
1238 CWARN("Although resent, but still not get child lock"
1239 "parent:"DFID" child:"DFID"\n",
1240 PFID(mdt_object_fid(parent)),
1241 PFID(mdt_object_fid(child)));
1242 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1243 LDLM_LOCK_PUT(lock);
1246 LDLM_LOCK_PUT(lock);
1249 bool try_layout = false;
1252 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1253 mdt_lock_handle_init(lhc);
1254 mdt_lock_reg_init(lhc, LCK_PR);
1256 if (mdt_object_exists(child) == 0) {
1257 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1258 &child->mot_obj.mo_lu,
1259 "Object doesn't exist!\n");
1260 GOTO(out_child, rc = -ENOENT);
1263 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1264 struct md_attr *ma = &info->mti_attr;
1267 ma->ma_need = MA_INODE;
1268 rc = mdt_attr_get_complex(info, child, ma);
1269 if (unlikely(rc != 0))
1270 GOTO(out_child, rc);
1272 /* If the file has not been changed for some time, we
1273 * return not only a LOOKUP lock, but also an UPDATE
1274 * lock and this might save us RPC on later STAT. For
1275 * directories, it also let negative dentry starts
1276 * working for this dir. */
1277 if (ma->ma_valid & MA_INODE &&
1278 ma->ma_attr.la_valid & LA_CTIME &&
1279 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1280 ma->ma_attr.la_ctime < cfs_time_current_sec())
1281 child_bits |= MDS_INODELOCK_UPDATE;
1284 /* layout lock must be granted in a best-effort way
1285 * for IT operations */
1286 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1287 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1288 exp_connect_layout(info->mti_exp) &&
1289 S_ISREG(lu_object_attr(&child->mot_obj.mo_lu)) &&
1291 /* try to grant layout lock for regular file. */
1297 child_bits |= MDS_INODELOCK_LAYOUT;
1298 /* try layout lock, it may fail to be granted due to
1299 * contention at LOOKUP or UPDATE */
1300 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1302 child_bits &= ~MDS_INODELOCK_LAYOUT;
1303 LASSERT(child_bits != 0);
1304 rc = mdt_object_lock(info, child, lhc,
1305 child_bits, MDT_CROSS_LOCK);
1310 rc = mdt_object_lock(info, child, lhc, child_bits,
1313 if (unlikely(rc != 0))
1314 GOTO(out_child, rc);
1317 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1318 /* Get MA_SOM attributes if update lock is given. */
1320 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1321 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1324 /* finally, we can get attr for child. */
1325 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1326 rc = mdt_getattr_internal(info, child, ma_need);
1327 if (unlikely(rc != 0)) {
1328 mdt_object_unlock(info, child, lhc, 1);
1330 /* Debugging code. */
1331 res_id = &lock->l_resource->lr_name;
1332 LDLM_DEBUG(lock, "Returning lock to client");
1333 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1334 &lock->l_resource->lr_name),
1335 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1336 (unsigned long)res_id->name[0],
1337 (unsigned long)res_id->name[1],
1338 (unsigned long)res_id->name[2],
1339 PFID(mdt_object_fid(child)));
1340 mdt_pack_size2body(info, child);
1343 LDLM_LOCK_PUT(lock);
1347 mdt_object_put(info->mti_env, child);
1350 mdt_object_unlock(info, parent, lhp, 1);
1354 /* normal handler: should release the child lock */
1355 int mdt_getattr_name(struct mdt_thread_info *info)
1357 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1358 struct mdt_body *reqbody;
1359 struct mdt_body *repbody;
1363 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1364 LASSERT(reqbody != NULL);
1365 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1366 LASSERT(repbody != NULL);
1368 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1369 repbody->eadatasize = 0;
1370 repbody->aclsize = 0;
1372 rc = mdt_init_ucred(info, reqbody);
1374 GOTO(out_shrink, rc);
1376 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1377 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1378 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1379 lhc->mlh_reg_lh.cookie = 0;
1381 mdt_exit_ucred(info);
1384 mdt_client_compatibility(info);
1385 rc2 = mdt_fix_reply(info);
1391 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1392 void *karg, void *uarg);
1394 int mdt_set_info(struct mdt_thread_info *info)
1396 struct ptlrpc_request *req = mdt_info_req(info);
1399 int keylen, vallen, rc = 0;
1402 rc = req_capsule_server_pack(info->mti_pill);
1406 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1408 DEBUG_REQ(D_HA, req, "no set_info key");
1412 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1415 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1417 DEBUG_REQ(D_HA, req, "no set_info val");
1421 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1424 /* Swab any part of val you need to here */
1425 if (KEY_IS(KEY_READ_ONLY)) {
1427 lustre_msg_set_status(req->rq_repmsg, 0);
1429 spin_lock(&req->rq_export->exp_lock);
1431 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1433 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1434 spin_unlock(&req->rq_export->exp_lock);
1436 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1437 struct changelog_setinfo *cs =
1438 (struct changelog_setinfo *)val;
1439 if (vallen != sizeof(*cs)) {
1440 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1443 if (ptlrpc_req_need_swab(req)) {
1444 __swab64s(&cs->cs_recno);
1445 __swab32s(&cs->cs_id);
1448 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1450 lustre_msg_set_status(req->rq_repmsg, rc);
1459 * Top-level handler for MDT connection requests.
1461 int mdt_connect(struct mdt_thread_info *info)
1464 struct obd_connect_data *reply;
1465 struct obd_export *exp;
1466 struct ptlrpc_request *req = mdt_info_req(info);
1468 rc = target_handle_connect(req);
1470 return err_serious(rc);
1472 LASSERT(req->rq_export != NULL);
1473 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1474 rc = mdt_init_sec_level(info);
1476 obd_disconnect(class_export_get(req->rq_export));
1480 /* To avoid exposing partially initialized connection flags, changes up
1481 * to this point have been staged in reply->ocd_connect_flags. Now that
1482 * connection handling has completed successfully, atomically update
1483 * the connect flags in the shared export data structure. LU-1623 */
1484 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1485 exp = req->rq_export;
1486 spin_lock(&exp->exp_lock);
1487 exp->exp_connect_flags = reply->ocd_connect_flags;
1488 spin_unlock(&exp->exp_lock);
1490 rc = mdt_init_idmap(info);
1492 obd_disconnect(class_export_get(req->rq_export));
1497 int mdt_disconnect(struct mdt_thread_info *info)
1502 rc = target_handle_disconnect(mdt_info_req(info));
1504 rc = err_serious(rc);
1508 static int mdt_sendpage(struct mdt_thread_info *info,
1509 struct lu_rdpg *rdpg, int nob)
1511 struct ptlrpc_request *req = mdt_info_req(info);
1512 struct obd_export *exp = req->rq_export;
1513 struct ptlrpc_bulk_desc *desc;
1514 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1521 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1526 if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1527 /* old client requires reply size in it's PAGE_SIZE,
1528 * which is rdpg->rp_count */
1529 nob = rdpg->rp_count;
1531 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1532 i++, tmpcount -= tmpsize) {
1533 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1534 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1537 LASSERT(desc->bd_nob == nob);
1538 rc = target_bulk_io(exp, desc, lwi);
1539 ptlrpc_free_bulk_pin(desc);
1543 int mdt_readpage(struct mdt_thread_info *info)
1545 struct mdt_object *object = info->mti_object;
1546 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1547 struct mdt_body *reqbody;
1548 struct mdt_body *repbody;
1553 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1554 RETURN(err_serious(-ENOMEM));
1556 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1557 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1558 if (reqbody == NULL || repbody == NULL)
1559 RETURN(err_serious(-EFAULT));
1562 * prepare @rdpg before calling lower layers and transfer itself. Here
1563 * reqbody->size contains offset of where to start to read and
1564 * reqbody->nlink contains number bytes to read.
1566 rdpg->rp_hash = reqbody->size;
1567 if (rdpg->rp_hash != reqbody->size) {
1568 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1569 rdpg->rp_hash, reqbody->size);
1573 rdpg->rp_attrs = reqbody->mode;
1574 if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1575 rdpg->rp_attrs |= LUDA_64BITHASH;
1576 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1577 PTLRPC_MAX_BRW_SIZE);
1578 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1580 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1581 if (rdpg->rp_pages == NULL)
1584 for (i = 0; i < rdpg->rp_npages; ++i) {
1585 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1586 if (rdpg->rp_pages[i] == NULL)
1587 GOTO(free_rdpg, rc = -ENOMEM);
1590 /* call lower layers to fill allocated pages with directory data */
1591 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1593 GOTO(free_rdpg, rc);
1595 /* send pages to client */
1596 rc = mdt_sendpage(info, rdpg, rc);
1601 for (i = 0; i < rdpg->rp_npages; i++)
1602 if (rdpg->rp_pages[i] != NULL)
1603 cfs_free_page(rdpg->rp_pages[i]);
1604 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1606 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1612 static int mdt_reint_internal(struct mdt_thread_info *info,
1613 struct mdt_lock_handle *lhc,
1616 struct req_capsule *pill = info->mti_pill;
1617 struct mdt_body *repbody;
1622 rc = mdt_reint_unpack(info, op);
1624 CERROR("Can't unpack reint, rc %d\n", rc);
1625 RETURN(err_serious(rc));
1628 /* for replay (no_create) lmm is not needed, client has it already */
1629 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1630 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1631 info->mti_rr.rr_eadatalen);
1633 /* llog cookies are always 0, the field is kept for compatibility */
1634 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1635 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1637 rc = req_capsule_server_pack(pill);
1639 CERROR("Can't pack response, rc %d\n", rc);
1640 RETURN(err_serious(rc));
1643 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1644 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1646 repbody->eadatasize = 0;
1647 repbody->aclsize = 0;
1650 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1652 /* for replay no cookkie / lmm need, because client have this already */
1653 if (info->mti_spec.no_create)
1654 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1655 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1657 rc = mdt_init_ucred_reint(info);
1659 GOTO(out_shrink, rc);
1661 rc = mdt_fix_attr_ucred(info, op);
1663 GOTO(out_ucred, rc = err_serious(rc));
1665 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1666 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1667 GOTO(out_ucred, rc);
1669 rc = mdt_reint_rec(info, lhc);
1672 mdt_exit_ucred(info);
1674 mdt_client_compatibility(info);
1675 rc2 = mdt_fix_reply(info);
1681 static long mdt_reint_opcode(struct mdt_thread_info *info,
1682 const struct req_format **fmt)
1684 struct mdt_rec_reint *rec;
1687 opc = err_serious(-EFAULT);
1688 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1690 opc = rec->rr_opcode;
1691 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1692 if (opc < REINT_MAX && fmt[opc] != NULL)
1693 req_capsule_extend(info->mti_pill, fmt[opc]);
1695 CERROR("Unsupported opc: %ld\n", opc);
1696 opc = err_serious(opc);
1702 int mdt_reint(struct mdt_thread_info *info)
1707 static const struct req_format *reint_fmts[REINT_MAX] = {
1708 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1709 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1710 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1711 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1712 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1713 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1714 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1719 opc = mdt_reint_opcode(info, reint_fmts);
1722 * No lock possible here from client to pass it to reint code
1725 rc = mdt_reint_internal(info, NULL, opc);
1730 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1734 /* this should sync the whole device */
1735 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1737 struct dt_device *dt = mdt->mdt_bottom;
1741 rc = dt->dd_ops->dt_sync(env, dt);
1745 /* this should sync this object */
1746 static int mdt_object_sync(struct mdt_thread_info *info)
1748 struct md_object *next;
1752 if (!mdt_object_exists(info->mti_object)) {
1753 CWARN("Non existing object "DFID"!\n",
1754 PFID(mdt_object_fid(info->mti_object)));
1757 next = mdt_object_child(info->mti_object);
1758 rc = mo_object_sync(info->mti_env, next);
1763 int mdt_sync(struct mdt_thread_info *info)
1765 struct ptlrpc_request *req = mdt_info_req(info);
1766 struct req_capsule *pill = info->mti_pill;
1767 struct mdt_body *body;
1771 /* The fid may be zero, so we req_capsule_set manually */
1772 req_capsule_set(pill, &RQF_MDS_SYNC);
1774 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1776 RETURN(err_serious(-EINVAL));
1778 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1779 RETURN(err_serious(-ENOMEM));
1781 if (fid_seq(&body->fid1) == 0) {
1782 /* sync the whole device */
1783 rc = req_capsule_server_pack(pill);
1785 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1787 rc = err_serious(rc);
1789 /* sync an object */
1790 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1792 rc = mdt_object_sync(info);
1794 const struct lu_fid *fid;
1795 struct lu_attr *la = &info->mti_attr.ma_attr;
1797 info->mti_attr.ma_need = MA_INODE;
1798 info->mti_attr.ma_valid = 0;
1799 rc = mdt_attr_get_complex(info, info->mti_object,
1802 body = req_capsule_server_get(pill,
1804 fid = mdt_object_fid(info->mti_object);
1805 mdt_pack_attr2body(info, body, la, fid);
1809 rc = err_serious(rc);
1812 mdt_counter_incr(req, LPROC_MDT_SYNC);
1818 * Quotacheck handler.
1819 * in-kernel quotacheck isn't supported any more.
1821 int mdt_quotacheck(struct mdt_thread_info *info)
1823 struct obd_quotactl *oqctl;
1827 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1829 RETURN(err_serious(-EPROTO));
1831 rc = req_capsule_server_pack(info->mti_pill);
1833 RETURN(err_serious(rc));
1835 /* deprecated, not used any more */
1836 RETURN(-EOPNOTSUPP);
1840 * Handle quota control requests to consult current usage/limit, but also
1841 * to configure quota enforcement
1843 int mdt_quotactl(struct mdt_thread_info *info)
1845 struct obd_export *exp = info->mti_exp;
1846 struct req_capsule *pill = info->mti_pill;
1847 struct obd_quotactl *oqctl, *repoqc;
1849 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1852 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1854 RETURN(err_serious(-EPROTO));
1856 rc = req_capsule_server_pack(pill);
1858 RETURN(err_serious(rc));
1860 switch (oqctl->qc_cmd) {
1862 case LUSTRE_Q_INVALIDATE:
1863 case LUSTRE_Q_FINVALIDATE:
1867 /* deprecated, not used any more */
1868 RETURN(-EOPNOTSUPP);
1869 /* master quotactl */
1875 RETURN(-EOPNOTSUPP);
1876 /* slave quotactl */
1881 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1885 /* map uid/gid for remote client */
1887 if (exp_connect_rmtclient(exp)) {
1888 struct lustre_idmap_table *idmap;
1890 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1892 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1893 oqctl->qc_cmd != Q_GETINFO))
1896 if (oqctl->qc_type == USRQUOTA)
1897 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1899 else if (oqctl->qc_type == GRPQUOTA)
1900 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1905 if (id == CFS_IDMAP_NOTFOUND) {
1906 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1911 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1913 RETURN(err_serious(-EFAULT));
1915 if (oqctl->qc_id != id)
1916 swap(oqctl->qc_id, id);
1918 switch (oqctl->qc_cmd) {
1924 /* forward quotactl request to QMT */
1925 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
1930 /* slave quotactl */
1931 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
1936 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1940 if (oqctl->qc_id != id)
1941 swap(oqctl->qc_id, id);
1948 * OBD PING and other handlers.
1950 int mdt_obd_ping(struct mdt_thread_info *info)
1955 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1957 rc = target_handle_ping(mdt_info_req(info));
1959 rc = err_serious(rc);
1964 * OBD_IDX_READ handler
1966 int mdt_obd_idx_read(struct mdt_thread_info *info)
1968 struct mdt_device *mdt = info->mti_mdt;
1969 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1970 struct idx_info *req_ii, *rep_ii;
1974 memset(rdpg, 0, sizeof(*rdpg));
1975 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
1977 /* extract idx_info buffer from request & reply */
1978 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
1979 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
1980 RETURN(err_serious(-EPROTO));
1982 rc = req_capsule_server_pack(info->mti_pill);
1984 RETURN(err_serious(rc));
1986 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
1988 RETURN(err_serious(-EFAULT));
1989 rep_ii->ii_magic = IDX_INFO_MAGIC;
1991 /* extract hash to start with */
1992 rdpg->rp_hash = req_ii->ii_hash_start;
1994 /* extract requested attributes */
1995 rdpg->rp_attrs = req_ii->ii_attrs;
1997 /* check that fid packed in request is valid and supported */
1998 if (!fid_is_sane(&req_ii->ii_fid))
2000 rep_ii->ii_fid = req_ii->ii_fid;
2003 rep_ii->ii_flags = req_ii->ii_flags;
2005 /* compute number of pages to allocate, ii_count is the number of 4KB
2007 if (req_ii->ii_count <= 0)
2008 GOTO(out, rc = -EFAULT);
2009 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2010 PTLRPC_MAX_BRW_SIZE);
2011 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2013 /* allocate pages to store the containers */
2014 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2015 if (rdpg->rp_pages == NULL)
2016 GOTO(out, rc = -ENOMEM);
2017 for (i = 0; i < rdpg->rp_npages; i++) {
2018 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2019 if (rdpg->rp_pages[i] == NULL)
2020 GOTO(out, rc = -ENOMEM);
2023 /* populate pages with key/record pairs */
2024 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2028 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2029 "asked %d > %d\n", rc, rdpg->rp_count);
2031 /* send pages to client */
2032 rc = mdt_sendpage(info, rdpg, rc);
2036 if (rdpg->rp_pages) {
2037 for (i = 0; i < rdpg->rp_npages; i++)
2038 if (rdpg->rp_pages[i])
2039 cfs_free_page(rdpg->rp_pages[i]);
2040 OBD_FREE(rdpg->rp_pages,
2041 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2046 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2048 return err_serious(-EOPNOTSUPP);
2051 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2053 return err_serious(-EOPNOTSUPP);
2060 /** clone llog ctxt from child (mdd)
2061 * This allows remote llog (replicator) access.
2062 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2063 * context was originally set up, or we can handle them directly.
2064 * I choose the latter, but that means I need any llog
2065 * contexts set up by child to be accessable by the mdt. So we clone the
2066 * context into our context list here.
2068 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2071 struct md_device *next = mdt->mdt_child;
2072 struct llog_ctxt *ctxt;
2075 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2078 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2079 if (rc || ctxt == NULL) {
2083 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2085 CERROR("Can't set mdt ctxt %d\n", rc);
2090 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2091 struct mdt_device *mdt, int idx)
2093 struct llog_ctxt *ctxt;
2095 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2098 /* Put once for the get we just did, and once for the clone */
2099 llog_ctxt_put(ctxt);
2100 llog_ctxt_put(ctxt);
2104 int mdt_llog_create(struct mdt_thread_info *info)
2108 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2109 rc = llog_origin_handle_open(mdt_info_req(info));
2110 return (rc < 0 ? err_serious(rc) : rc);
2113 int mdt_llog_destroy(struct mdt_thread_info *info)
2117 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2118 rc = llog_origin_handle_destroy(mdt_info_req(info));
2119 return (rc < 0 ? err_serious(rc) : rc);
2122 int mdt_llog_read_header(struct mdt_thread_info *info)
2126 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2127 rc = llog_origin_handle_read_header(mdt_info_req(info));
2128 return (rc < 0 ? err_serious(rc) : rc);
2131 int mdt_llog_next_block(struct mdt_thread_info *info)
2135 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2136 rc = llog_origin_handle_next_block(mdt_info_req(info));
2137 return (rc < 0 ? err_serious(rc) : rc);
2140 int mdt_llog_prev_block(struct mdt_thread_info *info)
2144 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2145 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2146 return (rc < 0 ? err_serious(rc) : rc);
2154 static struct ldlm_callback_suite cbs = {
2155 .lcs_completion = ldlm_server_completion_ast,
2156 .lcs_blocking = ldlm_server_blocking_ast,
2157 .lcs_glimpse = ldlm_server_glimpse_ast
2160 int mdt_enqueue(struct mdt_thread_info *info)
2162 struct ptlrpc_request *req;
2166 * info->mti_dlm_req already contains swapped and (if necessary)
2167 * converted dlm request.
2169 LASSERT(info->mti_dlm_req != NULL);
2171 req = mdt_info_req(info);
2172 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2173 req, info->mti_dlm_req, &cbs);
2174 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2175 return rc ? err_serious(rc) : req->rq_status;
2178 int mdt_convert(struct mdt_thread_info *info)
2181 struct ptlrpc_request *req;
2183 LASSERT(info->mti_dlm_req);
2184 req = mdt_info_req(info);
2185 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2186 return rc ? err_serious(rc) : req->rq_status;
2189 int mdt_bl_callback(struct mdt_thread_info *info)
2191 CERROR("bl callbacks should not happen on MDS\n");
2193 return err_serious(-EOPNOTSUPP);
2196 int mdt_cp_callback(struct mdt_thread_info *info)
2198 CERROR("cp callbacks should not happen on MDS\n");
2200 return err_serious(-EOPNOTSUPP);
2204 * sec context handlers
2206 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2210 rc = mdt_handle_idmap(info);
2213 struct ptlrpc_request *req = mdt_info_req(info);
2216 opc = lustre_msg_get_opc(req->rq_reqmsg);
2217 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2218 sptlrpc_svc_ctx_invalidate(req);
2221 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2227 * quota request handlers
2229 int mdt_quota_dqacq(struct mdt_thread_info *info)
2231 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2236 RETURN(err_serious(-EOPNOTSUPP));
2238 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2242 static struct mdt_object *mdt_obj(struct lu_object *o)
2244 LASSERT(lu_device_is_mdt(o->lo_dev));
2245 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2248 struct mdt_object *mdt_object_new(const struct lu_env *env,
2249 struct mdt_device *d,
2250 const struct lu_fid *f)
2252 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2253 struct lu_object *o;
2254 struct mdt_object *m;
2257 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2258 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2259 if (unlikely(IS_ERR(o)))
2260 m = (struct mdt_object *)o;
2266 struct mdt_object *mdt_object_find(const struct lu_env *env,
2267 struct mdt_device *d,
2268 const struct lu_fid *f)
2270 struct lu_object *o;
2271 struct mdt_object *m;
2274 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2275 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2276 if (unlikely(IS_ERR(o)))
2277 m = (struct mdt_object *)o;
2284 * Asyncronous commit for mdt device.
2286 * Pass asynchonous commit call down the MDS stack.
2288 * \param env environment
2289 * \param mdt the mdt device
2291 static void mdt_device_commit_async(const struct lu_env *env,
2292 struct mdt_device *mdt)
2294 struct dt_device *dt = mdt->mdt_bottom;
2297 rc = dt->dd_ops->dt_commit_async(env, dt);
2298 if (unlikely(rc != 0))
2299 CWARN("async commit start failed with rc = %d", rc);
2303 * Mark the lock as "synchonous".
2305 * Mark the lock to deffer transaction commit to the unlock time.
2307 * \param lock the lock to mark as "synchonous"
2309 * \see mdt_is_lock_sync
2310 * \see mdt_save_lock
2312 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2314 lock->l_ast_data = (void*)1;
2318 * Check whehter the lock "synchonous" or not.
2320 * \param lock the lock to check
2321 * \retval 1 the lock is "synchonous"
2322 * \retval 0 the lock isn't "synchronous"
2324 * \see mdt_set_lock_sync
2325 * \see mdt_save_lock
2327 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2329 return lock->l_ast_data != NULL;
2333 * Blocking AST for mdt locks.
2335 * Starts transaction commit if in case of COS lock conflict or
2336 * deffers such a commit to the mdt_save_lock.
2338 * \param lock the lock which blocks a request or cancelling lock
2339 * \param desc unused
2340 * \param data unused
2341 * \param flag indicates whether this cancelling or blocking callback
2343 * \see ldlm_blocking_ast_nocheck
2345 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2346 void *data, int flag)
2348 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2349 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2353 if (flag == LDLM_CB_CANCELING)
2355 lock_res_and_lock(lock);
2356 if (lock->l_blocking_ast != mdt_blocking_ast) {
2357 unlock_res_and_lock(lock);
2360 if (mdt_cos_is_enabled(mdt) &&
2361 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2362 lock->l_blocking_lock != NULL &&
2363 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2364 mdt_set_lock_sync(lock);
2366 rc = ldlm_blocking_ast_nocheck(lock);
2368 /* There is no lock conflict if l_blocking_lock == NULL,
2369 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2370 * when the last reference to a local lock was released */
2371 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2374 rc = lu_env_init(&env, LCT_LOCAL);
2375 if (unlikely(rc != 0))
2376 CWARN("lu_env initialization failed with rc = %d,"
2377 "cannot start asynchronous commit\n", rc);
2379 mdt_device_commit_async(&env, mdt);
2385 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2386 struct mdt_lock_handle *lh, __u64 ibits,
2387 bool nonblock, int locality)
2389 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2390 ldlm_policy_data_t *policy = &info->mti_policy;
2391 struct ldlm_res_id *res_id = &info->mti_res_id;
2396 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2397 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2398 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2399 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2401 if (mdt_object_exists(o) < 0) {
2402 if (locality == MDT_CROSS_LOCK) {
2403 /* cross-ref object fix */
2404 ibits &= ~MDS_INODELOCK_UPDATE;
2405 ibits |= MDS_INODELOCK_LOOKUP;
2407 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2408 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2410 /* No PDO lock on remote object */
2411 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2414 if (lh->mlh_type == MDT_PDO_LOCK) {
2415 /* check for exists after object is locked */
2416 if (mdt_object_exists(o) == 0) {
2417 /* Non-existent object shouldn't have PDO lock */
2420 /* Non-dir object shouldn't have PDO lock */
2421 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2426 memset(policy, 0, sizeof(*policy));
2427 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2429 dlmflags = LDLM_FL_ATOMIC_CB;
2431 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2434 * Take PDO lock on whole directory and build correct @res_id for lock
2435 * on part of directory.
2437 if (lh->mlh_pdo_hash != 0) {
2438 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2439 mdt_lock_pdo_mode(info, o, lh);
2440 if (lh->mlh_pdo_mode != LCK_NL) {
2442 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2443 * is never going to be sent to client and we do not
2444 * want it slowed down due to possible cancels.
2446 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2447 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2448 policy, res_id, dlmflags,
2449 &info->mti_exp->exp_handle.h_cookie);
2455 * Finish res_id initializing by name hash marking part of
2456 * directory which is taking modification.
2458 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2461 policy->l_inodebits.bits = ibits;
2464 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2465 * going to be sent to client. If it is - mdt_intent_policy() path will
2466 * fix it up and turn FL_LOCAL flag off.
2468 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2469 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2470 &info->mti_exp->exp_handle.h_cookie);
2472 mdt_object_unlock(info, o, lh, 1);
2473 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2474 lh->mlh_pdo_hash != 0 &&
2475 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2476 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2482 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2483 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2485 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2488 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2489 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2491 struct mdt_lock_handle tmp = *lh;
2494 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2502 * Save a lock within request object.
2504 * Keep the lock referenced until whether client ACK or transaction
2505 * commit happens or release the lock immediately depending on input
2506 * parameters. If COS is ON, a write lock is converted to COS lock
2509 * \param info thead info object
2510 * \param h lock handle
2511 * \param mode lock mode
2512 * \param decref force immediate lock releasing
2515 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2516 ldlm_mode_t mode, int decref)
2520 if (lustre_handle_is_used(h)) {
2521 if (decref || !info->mti_has_trans ||
2522 !(mode & (LCK_PW | LCK_EX))){
2523 mdt_fid_unlock(h, mode);
2525 struct mdt_device *mdt = info->mti_mdt;
2526 struct ldlm_lock *lock = ldlm_handle2lock(h);
2527 struct ptlrpc_request *req = mdt_info_req(info);
2530 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2532 CDEBUG(D_HA, "request = %p reply state = %p"
2533 " transno = "LPD64"\n",
2534 req, req->rq_reply_state, req->rq_transno);
2535 if (mdt_cos_is_enabled(mdt)) {
2537 ldlm_lock_downgrade(lock, LCK_COS);
2540 ptlrpc_save_lock(req, h, mode, no_ack);
2541 if (mdt_is_lock_sync(lock)) {
2542 CDEBUG(D_HA, "found sync-lock,"
2543 " async commit started\n");
2544 mdt_device_commit_async(info->mti_env,
2547 LDLM_LOCK_PUT(lock);
2556 * Unlock mdt object.
2558 * Immeditely release the regular lock and the PDO lock or save the
2559 * lock in reqeuest and keep them referenced until client ACK or
2560 * transaction commit.
2562 * \param info thread info object
2563 * \param o mdt object
2564 * \param lh mdt lock handle referencing regular and PDO locks
2565 * \param decref force immediate lock releasing
2567 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2568 struct mdt_lock_handle *lh, int decref)
2572 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2573 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2578 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2579 const struct lu_fid *f,
2580 struct mdt_lock_handle *lh,
2583 struct mdt_object *o;
2585 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2589 rc = mdt_object_lock(info, o, lh, ibits,
2592 mdt_object_put(info->mti_env, o);
2599 void mdt_object_unlock_put(struct mdt_thread_info * info,
2600 struct mdt_object * o,
2601 struct mdt_lock_handle *lh,
2604 mdt_object_unlock(info, o, lh, decref);
2605 mdt_object_put(info->mti_env, o);
2608 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2610 struct mdt_opc_slice *s;
2611 struct mdt_handler *h;
2614 for (s = supported; s->mos_hs != NULL; s++) {
2615 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2616 h = s->mos_hs + (opc - s->mos_opc_start);
2617 if (likely(h->mh_opc != 0))
2618 LASSERTF(h->mh_opc == opc,
2619 "opcode mismatch %d != %d\n",
2622 h = NULL; /* unsupported opc */
2629 static int mdt_lock_resname_compat(struct mdt_device *m,
2630 struct ldlm_request *req)
2632 /* XXX something... later. */
2636 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2638 /* XXX something... later. */
2643 * Generic code handling requests that have struct mdt_body passed in:
2645 * - extract mdt_body from request and save it in @info, if present;
2647 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2650 * - if HABEO_CORPUS flag is set for this request type check whether object
2651 * actually exists on storage (lu_object_exists()).
2654 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2656 const struct mdt_body *body;
2657 struct mdt_object *obj;
2658 const struct lu_env *env;
2659 struct req_capsule *pill;
2663 env = info->mti_env;
2664 pill = info->mti_pill;
2666 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2670 if (!(body->valid & OBD_MD_FLID))
2673 if (!fid_is_sane(&body->fid1)) {
2674 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2679 * Do not get size or any capa fields before we check that request
2680 * contains capa actually. There are some requests which do not, for
2681 * instance MDS_IS_SUBDIR.
2683 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2684 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2685 mdt_set_capainfo(info, 0, &body->fid1,
2686 req_capsule_client_get(pill, &RMF_CAPA1));
2688 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2690 if ((flags & HABEO_CORPUS) &&
2691 !mdt_object_exists(obj)) {
2692 mdt_object_put(env, obj);
2693 /* for capability renew ENOENT will be handled in
2695 if (body->valid & OBD_MD_FLOSSCAPA)
2700 info->mti_object = obj;
2709 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2711 struct req_capsule *pill = info->mti_pill;
2715 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2716 rc = mdt_body_unpack(info, flags);
2720 if (rc == 0 && (flags & HABEO_REFERO)) {
2722 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2723 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2724 info->mti_body->eadatasize);
2725 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2726 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2729 rc = req_capsule_server_pack(pill);
2734 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2736 struct md_device *next = m->mdt_child;
2738 return next->md_ops->mdo_init_capa_ctxt(env, next,
2739 m->mdt_opts.mo_mds_capa,
2740 m->mdt_capa_timeout,
2746 * Invoke handler for this request opc. Also do necessary preprocessing
2747 * (according to handler ->mh_flags), and post-processing (setting of
2748 * ->last_{xid,committed}).
2750 static int mdt_req_handle(struct mdt_thread_info *info,
2751 struct mdt_handler *h, struct ptlrpc_request *req)
2753 int rc, serious = 0;
2758 LASSERT(h->mh_act != NULL);
2759 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2760 LASSERT(current->journal_info == NULL);
2763 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2764 * to put same checks into handlers like mdt_close(), mdt_reint(),
2765 * etc., without talking to mdt authors first. Checking same thing
2766 * there again is useless and returning 0 error without packing reply
2767 * is buggy! Handlers either pack reply or return error.
2769 * We return 0 here and do not send any reply in order to emulate
2770 * network failure. Do not send any reply in case any of NET related
2771 * fail_id has occured.
2773 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2777 flags = h->mh_flags;
2778 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2780 if (h->mh_fmt != NULL) {
2781 req_capsule_set(info->mti_pill, h->mh_fmt);
2782 rc = mdt_unpack_req_pack_rep(info, flags);
2785 if (rc == 0 && flags & MUTABOR &&
2786 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2787 /* should it be rq_status? */
2790 if (rc == 0 && flags & HABEO_CLAVIS) {
2791 struct ldlm_request *dlm_req;
2793 LASSERT(h->mh_fmt != NULL);
2795 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2796 if (dlm_req != NULL) {
2797 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2799 dlm_req->lock_desc.l_policy_data.\
2800 l_inodebits.bits == 0)) {
2802 * Lock without inodebits makes no sense and
2803 * will oops later in ldlm. If client miss to
2804 * set such bits, do not trigger ASSERTION.
2806 * For liblustre flock case, it maybe zero.
2810 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2811 rc = mdt_lock_resname_compat(
2814 info->mti_dlm_req = dlm_req;
2821 /* capability setting changed via /proc, needs reinitialize ctxt */
2822 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2823 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2824 info->mti_mdt->mdt_capa_conf = 0;
2827 if (likely(rc == 0)) {
2829 * Process request, there can be two types of rc:
2830 * 1) errors with msg unpack/pack, other failures outside the
2831 * operation itself. This is counted as serious errors;
2832 * 2) errors during fs operation, should be placed in rq_status
2835 rc = h->mh_act(info);
2837 !req->rq_no_reply && req->rq_reply_state == NULL) {
2838 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2839 "pack reply and returned 0 error\n",
2843 serious = is_serious(rc);
2844 rc = clear_serious(rc);
2848 req->rq_status = rc;
2851 * ELDLM_* codes which > 0 should be in rq_status only as well as
2852 * all non-serious errors.
2854 if (rc > 0 || !serious)
2857 LASSERT(current->journal_info == NULL);
2859 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2860 info->mti_mdt->mdt_opts.mo_compat_resname) {
2861 struct ldlm_reply *dlmrep;
2863 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2865 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2868 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2869 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2870 target_committed_to_req(req);
2872 if (unlikely(req_is_replay(req) &&
2873 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2874 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2878 target_send_reply(req, rc, info->mti_fail_id);
2882 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2884 lh->mlh_type = MDT_NUL_LOCK;
2885 lh->mlh_reg_lh.cookie = 0ull;
2886 lh->mlh_reg_mode = LCK_MINMODE;
2887 lh->mlh_pdo_lh.cookie = 0ull;
2888 lh->mlh_pdo_mode = LCK_MINMODE;
2891 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2893 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2894 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2898 * Initialize fields of struct mdt_thread_info. Other fields are left in
2899 * uninitialized state, because it's too expensive to zero out whole
2900 * mdt_thread_info (> 1K) on each request arrival.
2902 static void mdt_thread_info_init(struct ptlrpc_request *req,
2903 struct mdt_thread_info *info)
2907 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2908 info->mti_pill = &req->rq_pill;
2911 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2912 mdt_lock_handle_init(&info->mti_lh[i]);
2914 /* mdt device: it can be NULL while CONNECT */
2915 if (req->rq_export) {
2916 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2917 info->mti_exp = req->rq_export;
2919 info->mti_mdt = NULL;
2920 info->mti_env = req->rq_svc_thread->t_env;
2921 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2922 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2923 info->mti_mos = NULL;
2925 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2926 info->mti_body = NULL;
2927 info->mti_object = NULL;
2928 info->mti_dlm_req = NULL;
2929 info->mti_has_trans = 0;
2930 info->mti_cross_ref = 0;
2931 info->mti_opdata = 0;
2932 info->mti_big_lmm_used = 0;
2934 /* To not check for split by default. */
2935 info->mti_spec.no_create = 0;
2938 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2942 req_capsule_fini(info->mti_pill);
2943 if (info->mti_object != NULL) {
2944 mdt_object_put(info->mti_env, info->mti_object);
2945 info->mti_object = NULL;
2947 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2948 mdt_lock_handle_fini(&info->mti_lh[i]);
2949 info->mti_env = NULL;
2952 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2953 struct obd_device *obd, int *process)
2955 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2956 case MDS_CONNECT: /* This will never get here, but for completeness. */
2957 case OST_CONNECT: /* This will never get here, but for completeness. */
2958 case MDS_DISCONNECT:
2959 case OST_DISCONNECT:
2965 case MDS_DONE_WRITING:
2966 case MDS_SYNC: /* used in unmounting */
2972 *process = target_queue_recovery_request(req, obd);
2976 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2983 * Handle recovery. Return:
2984 * +1: continue request processing;
2985 * -ve: abort immediately with the given error code;
2986 * 0: send reply with error code in req->rq_status;
2988 static int mdt_recovery(struct mdt_thread_info *info)
2990 struct ptlrpc_request *req = mdt_info_req(info);
2991 struct obd_device *obd;
2995 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2998 case SEC_CTX_INIT_CONT:
3004 rc = mdt_handle_idmap(info);
3013 if (unlikely(!class_connected_export(req->rq_export))) {
3014 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3015 lustre_msg_get_opc(req->rq_reqmsg),
3016 libcfs_id2str(req->rq_peer));
3017 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3018 * mds_A will get -ENOTCONN(especially for ping req),
3019 * which will cause that mds_A deactive timeout, then when
3020 * mds_A cleanup, the cleanup process will be suspended since
3021 * deactive timeout is not zero.
3023 req->rq_status = -ENOTCONN;
3024 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3028 /* sanity check: if the xid matches, the request must be marked as a
3029 * resent or replayed */
3030 if (req_xid_is_last(req)) {
3031 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3032 (MSG_RESENT | MSG_REPLAY))) {
3033 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3034 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3035 lustre_msg_get_flags(req->rq_reqmsg));
3037 req->rq_status = -ENOTCONN;
3042 /* else: note the opposite is not always true; a RESENT req after a
3043 * failover will usually not match the last_xid, since it was likely
3044 * never committed. A REPLAYed request will almost never match the
3045 * last xid, however it could for a committed, but still retained,
3048 obd = req->rq_export->exp_obd;
3050 /* Check for aborted recovery... */
3051 if (unlikely(obd->obd_recovering)) {
3054 DEBUG_REQ(D_INFO, req, "Got new replay");
3055 rc = mdt_filter_recovery_request(req, obd, &should_process);
3056 if (rc != 0 || !should_process)
3058 else if (should_process < 0) {
3059 req->rq_status = should_process;
3060 rc = ptlrpc_error(req);
3067 static int mdt_msg_check_version(struct lustre_msg *msg)
3071 switch (lustre_msg_get_opc(msg)) {
3073 case MDS_DISCONNECT:
3076 case SEC_CTX_INIT_CONT:
3079 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3081 CERROR("bad opc %u version %08x, expecting %08x\n",
3082 lustre_msg_get_opc(msg),
3083 lustre_msg_get_version(msg),
3084 LUSTRE_OBD_VERSION);
3088 case MDS_GETATTR_NAME:
3095 case MDS_DONE_WRITING:
3102 case MDS_HSM_PROGRESS:
3103 case MDS_HSM_REQUEST:
3104 case MDS_HSM_CT_REGISTER:
3105 case MDS_HSM_CT_UNREGISTER:
3106 case MDS_HSM_STATE_GET:
3107 case MDS_HSM_STATE_SET:
3108 case MDS_HSM_ACTION:
3109 case MDS_QUOTACHECK:
3116 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3118 CERROR("bad opc %u version %08x, expecting %08x\n",
3119 lustre_msg_get_opc(msg),
3120 lustre_msg_get_version(msg),
3121 LUSTRE_MDS_VERSION);
3125 case LDLM_BL_CALLBACK:
3126 case LDLM_CP_CALLBACK:
3127 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3129 CERROR("bad opc %u version %08x, expecting %08x\n",
3130 lustre_msg_get_opc(msg),
3131 lustre_msg_get_version(msg),
3132 LUSTRE_DLM_VERSION);
3134 case OBD_LOG_CANCEL:
3135 case LLOG_ORIGIN_HANDLE_CREATE:
3136 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3137 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3138 case LLOG_ORIGIN_HANDLE_CLOSE:
3139 case LLOG_ORIGIN_HANDLE_DESTROY:
3140 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3142 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3144 CERROR("bad opc %u version %08x, expecting %08x\n",
3145 lustre_msg_get_opc(msg),
3146 lustre_msg_get_version(msg),
3147 LUSTRE_LOG_VERSION);
3150 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3156 static int mdt_handle0(struct ptlrpc_request *req,
3157 struct mdt_thread_info *info,
3158 struct mdt_opc_slice *supported)
3160 struct mdt_handler *h;
3161 struct lustre_msg *msg;
3166 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3169 LASSERT(current->journal_info == NULL);
3171 msg = req->rq_reqmsg;
3172 rc = mdt_msg_check_version(msg);
3173 if (likely(rc == 0)) {
3174 rc = mdt_recovery(info);
3175 if (likely(rc == +1)) {
3176 h = mdt_handler_find(lustre_msg_get_opc(msg),
3178 if (likely(h != NULL)) {
3179 rc = mdt_req_handle(info, h, req);
3181 CERROR("The unsupported opc: 0x%x\n",
3182 lustre_msg_get_opc(msg) );
3183 req->rq_status = -ENOTSUPP;
3184 rc = ptlrpc_error(req);
3189 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3194 * MDT handler function called by ptlrpc service thread when request comes.
3196 * XXX common "target" functionality should be factored into separate module
3197 * shared by mdt, ost and stand-alone services like fld.
3199 int mdt_handle_common(struct ptlrpc_request *req,
3200 struct mdt_opc_slice *supported)
3203 struct mdt_thread_info *info;
3207 env = req->rq_svc_thread->t_env;
3208 /* Refill(initilize) the context(mdt_thread_info), in case it is
3209 * not initialized yet. Usually it happens during start up, after
3210 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3211 * before MDT_thread_info is initialized */
3213 LASSERT(env != NULL);
3214 LASSERT(env->le_ses != NULL);
3215 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3216 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3217 LASSERT(info != NULL);
3219 mdt_thread_info_init(req, info);
3221 rc = mdt_handle0(req, info, supported);
3223 mdt_thread_info_fini(info);
3228 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3231 int mdt_recovery_handle(struct ptlrpc_request *req)
3236 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3238 rc = mdt_handle_common(req, mdt_fld_handlers);
3241 rc = mdt_handle_common(req, mdt_seq_handlers);
3244 rc = mdt_handle_common(req, mdt_regular_handlers);
3266 static int mdt_intent_getattr(enum mdt_it_code opcode,
3267 struct mdt_thread_info *info,
3268 struct ldlm_lock **,
3270 static int mdt_intent_layout(enum mdt_it_code opcode,
3271 struct mdt_thread_info *info,
3272 struct ldlm_lock **,
3274 static int mdt_intent_reint(enum mdt_it_code opcode,
3275 struct mdt_thread_info *info,
3276 struct ldlm_lock **,
3279 static struct mdt_it_flavor {
3280 const struct req_format *it_fmt;
3282 int (*it_act)(enum mdt_it_code ,
3283 struct mdt_thread_info *,
3284 struct ldlm_lock **,
3287 } mdt_it_flavor[] = {
3289 .it_fmt = &RQF_LDLM_INTENT,
3290 /*.it_flags = HABEO_REFERO,*/
3292 .it_act = mdt_intent_reint,
3293 .it_reint = REINT_OPEN
3296 .it_fmt = &RQF_LDLM_INTENT,
3297 .it_flags = MUTABOR,
3298 .it_act = mdt_intent_reint,
3299 .it_reint = REINT_OPEN
3302 .it_fmt = &RQF_LDLM_INTENT,
3303 .it_flags = MUTABOR,
3304 .it_act = mdt_intent_reint,
3305 .it_reint = REINT_CREATE
3307 [MDT_IT_GETATTR] = {
3308 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3309 .it_flags = HABEO_REFERO,
3310 .it_act = mdt_intent_getattr
3312 [MDT_IT_READDIR] = {
3318 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3319 .it_flags = HABEO_REFERO,
3320 .it_act = mdt_intent_getattr
3323 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3324 .it_flags = MUTABOR,
3326 .it_reint = REINT_UNLINK
3330 .it_flags = MUTABOR,
3333 [MDT_IT_GETXATTR] = {
3339 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3341 .it_act = mdt_intent_layout
3345 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3346 struct ldlm_lock **lockp,
3347 struct ldlm_lock *new_lock,
3348 struct mdt_lock_handle *lh,
3351 struct ptlrpc_request *req = mdt_info_req(info);
3352 struct ldlm_lock *lock = *lockp;
3355 * Get new lock only for cases when possible resent did not find any
3358 if (new_lock == NULL)
3359 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3361 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3362 lh->mlh_reg_lh.cookie = 0;
3366 LASSERTF(new_lock != NULL,
3367 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3370 * If we've already given this lock to a client once, then we should
3371 * have no readers or writers. Otherwise, we should have one reader
3372 * _or_ writer ref (which will be zeroed below) before returning the
3375 if (new_lock->l_export == req->rq_export) {
3376 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3378 LASSERT(new_lock->l_export == NULL);
3379 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3384 if (new_lock->l_export == req->rq_export) {
3386 * Already gave this to the client, which means that we
3387 * reconstructed a reply.
3389 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3391 lh->mlh_reg_lh.cookie = 0;
3392 RETURN(ELDLM_LOCK_REPLACED);
3396 * Fixup the lock to be given to the client.
3398 lock_res_and_lock(new_lock);
3399 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3400 * possible blocking AST. */
3401 while (new_lock->l_readers > 0) {
3402 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3403 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3404 new_lock->l_readers--;
3406 while (new_lock->l_writers > 0) {
3407 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3408 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3409 new_lock->l_writers--;
3412 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3413 new_lock->l_blocking_ast = lock->l_blocking_ast;
3414 new_lock->l_completion_ast = lock->l_completion_ast;
3415 new_lock->l_remote_handle = lock->l_remote_handle;
3416 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3418 unlock_res_and_lock(new_lock);
3420 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3421 &new_lock->l_remote_handle,
3422 &new_lock->l_exp_hash);
3424 LDLM_LOCK_RELEASE(new_lock);
3425 lh->mlh_reg_lh.cookie = 0;
3427 RETURN(ELDLM_LOCK_REPLACED);
3430 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3431 struct ldlm_lock *new_lock,
3432 struct ldlm_lock **old_lock,
3433 struct mdt_lock_handle *lh)
3435 struct ptlrpc_request *req = mdt_info_req(info);
3436 struct obd_export *exp = req->rq_export;
3437 struct lustre_handle remote_hdl;
3438 struct ldlm_request *dlmreq;
3439 struct ldlm_lock *lock;
3441 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3444 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3445 remote_hdl = dlmreq->lock_handle[0];
3447 /* In the function below, .hs_keycmp resolves to
3448 * ldlm_export_lock_keycmp() */
3449 /* coverity[overrun-buffer-val] */
3450 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3452 if (lock != new_lock) {
3453 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3454 lh->mlh_reg_mode = lock->l_granted_mode;
3456 LDLM_DEBUG(lock, "Restoring lock cookie");
3457 DEBUG_REQ(D_DLMTRACE, req,
3458 "restoring lock cookie "LPX64,
3459 lh->mlh_reg_lh.cookie);
3461 *old_lock = LDLM_LOCK_GET(lock);
3462 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3466 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3470 * If the xid matches, then we know this is a resent request, and allow
3471 * it. (It's probably an OPEN, for which we don't send a lock.
3473 if (req_xid_is_last(req))
3477 * This remote handle isn't enqueued, so we never received or processed
3478 * this request. Clear MSG_RESENT, because it can be handled like any
3479 * normal request now.
3481 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3483 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3487 static int mdt_intent_getattr(enum mdt_it_code opcode,
3488 struct mdt_thread_info *info,
3489 struct ldlm_lock **lockp,
3492 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3493 struct ldlm_lock *new_lock = NULL;
3495 struct ldlm_reply *ldlm_rep;
3496 struct ptlrpc_request *req;
3497 struct mdt_body *reqbody;
3498 struct mdt_body *repbody;
3502 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3505 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3508 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3509 repbody->eadatasize = 0;
3510 repbody->aclsize = 0;
3514 child_bits = MDS_INODELOCK_LOOKUP;
3516 case MDT_IT_GETATTR:
3517 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3520 CERROR("Unsupported intent (%d)\n", opcode);
3521 GOTO(out_shrink, rc = -EINVAL);
3524 rc = mdt_init_ucred(info, reqbody);
3526 GOTO(out_shrink, rc);
3528 req = info->mti_pill->rc_req;
3529 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3530 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3532 /* Get lock from request for possible resent case. */
3533 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3535 ldlm_rep->lock_policy_res2 =
3536 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3538 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3539 ldlm_rep->lock_policy_res2 = 0;
3540 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3541 ldlm_rep->lock_policy_res2) {
3542 lhc->mlh_reg_lh.cookie = 0ull;
3543 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3546 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3549 mdt_exit_ucred(info);
3551 mdt_client_compatibility(info);
3552 rc2 = mdt_fix_reply(info);
3558 static int mdt_intent_layout(enum mdt_it_code opcode,
3559 struct mdt_thread_info *info,
3560 struct ldlm_lock **lockp,
3563 struct layout_intent *layout;
3567 if (opcode != MDT_IT_LAYOUT) {
3568 CERROR("%s: Unknown intent (%d)\n",
3569 info->mti_exp->exp_obd->obd_name, opcode);
3573 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3574 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3575 ldlm_lvbo_size(*lockp));
3576 rc = req_capsule_server_pack(info->mti_pill);
3580 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3581 LASSERT(layout != NULL);
3582 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3583 /* return to normal ldlm handling */
3586 CERROR("%s: Unsupported layout intent (%d)\n",
3587 info->mti_exp->exp_obd->obd_name, layout->li_opc);
3591 static int mdt_intent_reint(enum mdt_it_code opcode,
3592 struct mdt_thread_info *info,
3593 struct ldlm_lock **lockp,
3596 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3597 struct ldlm_reply *rep = NULL;
3601 static const struct req_format *intent_fmts[REINT_MAX] = {
3602 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3603 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3608 opc = mdt_reint_opcode(info, intent_fmts);
3612 if (mdt_it_flavor[opcode].it_reint != opc) {
3613 CERROR("Reint code %ld doesn't match intent: %d\n",
3615 RETURN(err_serious(-EPROTO));
3618 /* Get lock from request for possible resent case. */
3619 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3621 rc = mdt_reint_internal(info, lhc, opc);
3623 /* Check whether the reply has been packed successfully. */
3624 if (mdt_info_req(info)->rq_repmsg != NULL)
3625 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3627 RETURN(err_serious(-EFAULT));
3629 /* MDC expects this in any case */
3631 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3633 /* Cross-ref case, the lock should be returned to the client */
3634 if (rc == -EREMOTE) {
3635 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3636 rep->lock_policy_res2 = 0;
3637 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3640 rep->lock_policy_res2 = clear_serious(rc);
3642 if (rep->lock_policy_res2 == -ENOENT &&
3643 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3644 rep->lock_policy_res2 = 0;
3646 if (rc == -ENOTCONN || rc == -ENODEV ||
3647 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3649 * If it is the disconnect error (ENODEV & ENOCONN), the error
3650 * will be returned by rq_status, and client at ptlrpc layer
3651 * will detect this, then disconnect, reconnect the import
3652 * immediately, instead of impacting the following the rpc.
3654 lhc->mlh_reg_lh.cookie = 0ull;
3658 * For other cases, the error will be returned by intent.
3659 * and client will retrieve the result from intent.
3662 * FIXME: when open lock is finished, that should be
3665 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3666 LASSERTF(rc == 0, "Error occurred but lock handle "
3667 "is still in use, rc = %d\n", rc);
3668 rep->lock_policy_res2 = 0;
3669 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3672 lhc->mlh_reg_lh.cookie = 0ull;
3673 RETURN(ELDLM_LOCK_ABORTED);
3678 static int mdt_intent_code(long itcode)
3686 case IT_OPEN|IT_CREAT:
3693 rc = MDT_IT_READDIR;
3696 rc = MDT_IT_GETATTR;
3708 rc = MDT_IT_GETXATTR;
3713 case IT_QUOTA_DQACQ:
3718 CERROR("Unknown intent opcode: %ld\n", itcode);
3725 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3726 struct ldlm_lock **lockp, __u64 flags)
3728 struct req_capsule *pill;
3729 struct mdt_it_flavor *flv;
3734 opc = mdt_intent_code(itopc);
3738 pill = info->mti_pill;
3740 if (opc == MDT_IT_QUOTA) {
3741 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3744 RETURN(-EOPNOTSUPP);
3746 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3747 /* pass the request to quota master */
3748 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3749 mdt_info_req(info), lockp,
3754 flv = &mdt_it_flavor[opc];
3755 if (flv->it_fmt != NULL)
3756 req_capsule_extend(pill, flv->it_fmt);
3758 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3760 struct ptlrpc_request *req = mdt_info_req(info);
3761 if (flv->it_flags & MUTABOR &&
3762 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3765 if (rc == 0 && flv->it_act != NULL) {
3766 /* execute policy */
3767 rc = flv->it_act(opc, info, lockp, flags);
3774 static int mdt_intent_policy(struct ldlm_namespace *ns,
3775 struct ldlm_lock **lockp, void *req_cookie,
3776 ldlm_mode_t mode, __u64 flags, void *data)
3778 struct mdt_thread_info *info;
3779 struct ptlrpc_request *req = req_cookie;
3780 struct ldlm_intent *it;
3781 struct req_capsule *pill;
3786 LASSERT(req != NULL);
3788 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3790 LASSERT(info != NULL);
3791 pill = info->mti_pill;
3792 LASSERT(pill->rc_req == req);
3794 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3795 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3796 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3798 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3802 /* Lock without inodebits makes no sense and will oops
3803 * later in ldlm. Let's check it now to see if we have
3804 * ibits corrupted somewhere in mdt_intent_opc().
3805 * The case for client miss to set ibits has been
3806 * processed by others. */
3807 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3808 lr_type == LDLM_IBITS,
3809 info->mti_dlm_req->lock_desc.\
3810 l_policy_data.l_inodebits.bits != 0));
3812 rc = err_serious(-EFAULT);
3814 /* No intent was provided */
3815 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3816 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3817 rc = req_capsule_server_pack(pill);
3819 rc = err_serious(rc);
3824 static int mdt_seq_fini(const struct lu_env *env,
3825 struct mdt_device *m)
3827 return seq_site_fini(env, mdt_seq_site(m));
3830 static int mdt_seq_init(const struct lu_env *env,
3832 struct mdt_device *m)
3834 struct seq_server_site *ss;
3839 ss = mdt_seq_site(m);
3842 * This is sequence-controller node. Init seq-controller server on local
3845 if (ss->ss_node_id == 0) {
3846 LASSERT(ss->ss_control_seq == NULL);
3848 OBD_ALLOC_PTR(ss->ss_control_seq);
3849 if (ss->ss_control_seq == NULL)
3852 rc = seq_server_init(ss->ss_control_seq,
3853 m->mdt_bottom, uuid,
3854 LUSTRE_SEQ_CONTROLLER,
3859 GOTO(out_seq_fini, rc);
3861 OBD_ALLOC_PTR(ss->ss_client_seq);
3862 if (ss->ss_client_seq == NULL)
3863 GOTO(out_seq_fini, rc = -ENOMEM);
3865 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3866 if (prefix == NULL) {
3867 OBD_FREE_PTR(ss->ss_client_seq);
3868 GOTO(out_seq_fini, rc = -ENOMEM);
3871 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3875 * Init seq-controller client after seq-controller server is
3876 * ready. Pass ss->ss_control_seq to it for direct talking.
3878 rc = seq_client_init(ss->ss_client_seq, NULL,
3879 LUSTRE_SEQ_METADATA, prefix,
3880 ss->ss_control_seq);
3881 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3884 GOTO(out_seq_fini, rc);
3887 /* Init seq-server on local MDT */
3888 LASSERT(ss->ss_server_seq == NULL);
3890 OBD_ALLOC_PTR(ss->ss_server_seq);
3891 if (ss->ss_server_seq == NULL)
3892 GOTO(out_seq_fini, rc = -ENOMEM);
3894 rc = seq_server_init(ss->ss_server_seq,
3895 m->mdt_bottom, uuid,
3900 GOTO(out_seq_fini, rc = -ENOMEM);
3902 /* Assign seq-controller client to local seq-server. */
3903 if (ss->ss_node_id == 0) {
3904 LASSERT(ss->ss_client_seq != NULL);
3906 rc = seq_server_set_cli(ss->ss_server_seq,
3914 mdt_seq_fini(env, m);
3922 static int mdt_fld_fini(const struct lu_env *env,
3923 struct mdt_device *m)
3925 struct seq_server_site *ss = mdt_seq_site(m);
3928 if (ss && ss->ss_server_fld) {
3929 fld_server_fini(env, ss->ss_server_fld);
3930 OBD_FREE_PTR(ss->ss_server_fld);
3931 ss->ss_server_fld = NULL;
3937 static int mdt_fld_init(const struct lu_env *env,
3939 struct mdt_device *m)
3941 struct seq_server_site *ss;
3945 ss = mdt_seq_site(m);
3947 OBD_ALLOC_PTR(ss->ss_server_fld);
3948 if (ss->ss_server_fld == NULL)
3949 RETURN(rc = -ENOMEM);
3951 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3952 ss->ss_node_id, LU_SEQ_RANGE_MDT);
3954 OBD_FREE_PTR(ss->ss_server_fld);
3955 ss->ss_server_fld = NULL;
3962 static void mdt_stack_pre_fini(const struct lu_env *env,
3963 struct mdt_device *m, struct lu_device *top)
3965 struct obd_device *obd = mdt2obd_dev(m);
3966 struct lustre_cfg_bufs *bufs;
3967 struct lustre_cfg *lcfg;
3968 struct mdt_thread_info *info;
3973 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3974 LASSERT(info != NULL);
3976 bufs = &info->mti_u.bufs;
3978 LASSERT(m->mdt_child_exp);
3979 LASSERT(m->mdt_child_exp->exp_obd);
3980 obd = m->mdt_child_exp->exp_obd;
3982 /* process cleanup, pass mdt obd name to get obd umount flags */
3983 /* XXX: this is needed because all layers are referenced by
3984 * objects (some of them are pinned by osd, for example *
3985 * the proper solution should be a model where object used
3986 * by osd only doesn't have mdt/mdd slices -bzzz */
3987 lustre_cfg_bufs_reset(bufs, obd->obd_name);
3988 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3989 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3991 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3994 top->ld_ops->ldo_process_config(env, top, lcfg);
3995 lustre_cfg_free(lcfg);
3999 static void mdt_stack_fini(const struct lu_env *env,
4000 struct mdt_device *m, struct lu_device *top)
4002 struct obd_device *obd = mdt2obd_dev(m);
4003 struct lustre_cfg_bufs *bufs;
4004 struct lustre_cfg *lcfg;
4005 struct mdt_thread_info *info;
4009 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4010 LASSERT(info != NULL);
4012 lu_dev_del_linkage(top->ld_site, top);
4014 lu_site_purge(env, top->ld_site, -1);
4016 bufs = &info->mti_u.bufs;
4017 /* process cleanup, pass mdt obd name to get obd umount flags */
4018 /* another purpose is to let all layers to release their objects */
4019 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4024 lustre_cfg_bufs_set_string(bufs, 1, flags);
4025 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4027 CERROR("Cannot alloc lcfg!\n");
4031 top->ld_ops->ldo_process_config(env, top, lcfg);
4032 lustre_cfg_free(lcfg);
4034 lu_site_purge(env, top->ld_site, -1);
4036 m->mdt_child = NULL;
4037 m->mdt_bottom = NULL;
4039 obd_disconnect(m->mdt_child_exp);
4040 m->mdt_child_exp = NULL;
4042 obd_disconnect(m->mdt_bottom_exp);
4043 m->mdt_child_exp = NULL;
4046 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4047 const char *next, struct obd_export **exp)
4049 struct obd_connect_data *data = NULL;
4050 struct obd_device *obd;
4054 OBD_ALLOC_PTR(data);
4056 GOTO(out, rc = -ENOMEM);
4058 obd = class_name2obd(next);
4060 CERROR("%s: can't locate next device: %s\n",
4061 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4062 GOTO(out, rc = -ENOTCONN);
4065 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4066 data->ocd_version = LUSTRE_VERSION_CODE;
4068 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4070 CERROR("%s: cannot connect to next dev %s (%d)\n",
4071 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4081 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4082 struct lustre_cfg *cfg)
4084 char *dev = lustre_cfg_string(cfg, 0);
4085 int rc, name_size, uuid_size;
4086 char *name, *uuid, *p;
4087 struct lustre_cfg_bufs *bufs;
4088 struct lustre_cfg *lcfg;
4089 struct obd_device *obd;
4090 struct lustre_profile *lprof;
4091 struct lu_site *site;
4094 /* in 1.8 we had the only device in the stack - MDS.
4095 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4096 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4097 * to generate names and setup MDT, MDD. MDT will be using
4098 * generated name to connect to MDD. for MDD the next device
4099 * will be LOD with name taken from so called "profile" which
4100 * is generated by mount_option line
4102 * 1.8 MGS generates config. commands like this:
4103 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4104 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4105 * 2.0 MGS generates config. commands like this:
4106 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4107 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4108 * 3:lustre-MDT0000-mdtlov 4:f
4110 * we generate MDD name from MDT one, just replacing T with D
4112 * after all the preparations, the logical equivalent will be
4113 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4114 * 3:lustre-MDT0000-mdtlov 4:f
4115 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4116 * 3:lustre-MDD0000 4:f
4118 * notice we build the stack from down to top: MDD first, then MDT */
4120 name_size = MAX_OBD_NAME;
4121 uuid_size = MAX_OBD_NAME;
4123 OBD_ALLOC(name, name_size);
4124 OBD_ALLOC(uuid, uuid_size);
4125 if (name == NULL || uuid == NULL)
4126 GOTO(cleanup_mem, rc = -ENOMEM);
4128 OBD_ALLOC_PTR(bufs);
4130 GOTO(cleanup_mem, rc = -ENOMEM);
4133 p = strstr(name, "-MDT");
4135 GOTO(cleanup_mem, rc = -ENOMEM);
4138 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4140 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4141 if (lprof == NULL || lprof->lp_dt == NULL) {
4142 CERROR("can't find the profile: %s\n",
4143 lustre_cfg_string(cfg, 0));
4144 GOTO(cleanup_mem, rc = -EINVAL);
4147 lustre_cfg_bufs_reset(bufs, name);
4148 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4149 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4150 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4152 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4154 GOTO(free_bufs, rc = -ENOMEM);
4156 rc = class_attach(lcfg);
4158 GOTO(lcfg_cleanup, rc);
4160 obd = class_name2obd(name);
4162 CERROR("Can not find obd %s (%s in config)\n",
4163 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4164 GOTO(class_detach, rc = -EINVAL);
4167 lustre_cfg_free(lcfg);
4169 lustre_cfg_bufs_reset(bufs, name);
4170 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4171 lustre_cfg_bufs_set_string(bufs, 2, dev);
4172 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4174 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4176 rc = class_setup(obd, lcfg);
4178 GOTO(class_detach, rc);
4180 /* connect to MDD we just setup */
4181 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4185 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4187 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4188 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4189 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4190 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4193 /* now connect to bottom OSD */
4194 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4195 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4199 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4202 rc = lu_env_refill((struct lu_env *)env);
4204 CERROR("Failure to refill session: '%d'\n", rc);
4206 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4211 class_detach(obd, lcfg);
4213 lustre_cfg_free(lcfg);
4218 OBD_FREE(name, name_size);
4220 OBD_FREE(uuid, uuid_size);
4224 /* setup quota master target on MDT0 */
4225 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4226 struct lustre_cfg *cfg)
4228 struct obd_device *obd;
4229 char *dev = lustre_cfg_string(cfg, 0);
4230 char *qmtname, *uuid, *p;
4231 struct lustre_cfg_bufs *bufs;
4232 struct lustre_cfg *lcfg;
4233 struct lustre_profile *lprof;
4234 struct obd_connect_data *data;
4238 LASSERT(mdt->mdt_qmt_exp == NULL);
4239 LASSERT(mdt->mdt_qmt_dev == NULL);
4241 /* quota master is on MDT0 only for now */
4242 if (mdt->mdt_seq_site.ss_node_id != 0)
4245 /* MGS generates config commands which look as follows:
4246 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4247 * 3:lustre-MDT0000-mdtlov 4:f
4249 * We generate the QMT name from the MDT one, just replacing MD with QM
4250 * after all the preparations, the logical equivalent will be:
4251 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4252 * 3:lustre-MDT0000-osd 4:f */
4253 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4254 OBD_ALLOC(uuid, UUID_MAX);
4255 OBD_ALLOC_PTR(bufs);
4256 OBD_ALLOC_PTR(data);
4257 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4258 GOTO(cleanup_mem, rc = -ENOMEM);
4260 strcpy(qmtname, dev);
4261 p = strstr(qmtname, "-MDT");
4263 GOTO(cleanup_mem, rc = -ENOMEM);
4264 /* replace MD with QM */
4268 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4270 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4271 if (lprof == NULL || lprof->lp_dt == NULL) {
4272 CERROR("can't find profile for %s\n",
4273 lustre_cfg_string(cfg, 0));
4274 GOTO(cleanup_mem, rc = -EINVAL);
4277 lustre_cfg_bufs_reset(bufs, qmtname);
4278 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4279 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4280 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4282 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4284 GOTO(cleanup_mem, rc = -ENOMEM);
4286 rc = class_attach(lcfg);
4288 GOTO(lcfg_cleanup, rc);
4290 obd = class_name2obd(qmtname);
4292 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4293 lustre_cfg_string(cfg, 0));
4294 GOTO(class_detach, rc = -EINVAL);
4297 lustre_cfg_free(lcfg);
4299 lustre_cfg_bufs_reset(bufs, qmtname);
4300 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4301 lustre_cfg_bufs_set_string(bufs, 2, dev);
4303 /* for quota, the next device should be the OSD device */
4304 lustre_cfg_bufs_set_string(bufs, 3,
4305 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4307 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4309 rc = class_setup(obd, lcfg);
4311 GOTO(class_detach, rc);
4313 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4315 /* configure local quota objects */
4316 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4317 &mdt->mdt_md_dev.md_lu_dev,
4320 GOTO(class_cleanup, rc);
4322 /* connect to quota master target */
4323 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4324 data->ocd_version = LUSTRE_VERSION_CODE;
4325 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4328 CERROR("cannot connect to quota master device %s (%d)\n",
4330 GOTO(class_cleanup, rc);
4336 class_manual_cleanup(obd);
4337 mdt->mdt_qmt_dev = NULL;
4341 class_detach(obd, lcfg);
4343 lustre_cfg_free(lcfg);
4348 OBD_FREE(qmtname, MAX_OBD_NAME);
4350 OBD_FREE(uuid, UUID_MAX);
4356 /* Shutdown quota master target associated with mdt */
4357 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4361 if (mdt->mdt_qmt_exp == NULL)
4363 LASSERT(mdt->mdt_qmt_dev != NULL);
4365 /* the qmt automatically shuts down when the mdt disconnects */
4366 obd_disconnect(mdt->mdt_qmt_exp);
4367 mdt->mdt_qmt_exp = NULL;
4368 mdt->mdt_qmt_dev = NULL;
4372 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4374 struct md_device *next = m->mdt_child;
4375 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4376 struct obd_device *obd = mdt2obd_dev(m);
4379 target_recovery_fini(obd);
4381 ping_evictor_stop();
4384 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4385 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4386 obd_exports_barrier(obd);
4387 obd_zombie_barrier();
4391 tgt_fini(env, &m->mdt_lut);
4392 mdt_fs_cleanup(env, m);
4393 upcall_cache_cleanup(m->mdt_identity_cache);
4394 m->mdt_identity_cache = NULL;
4396 if (m->mdt_namespace != NULL) {
4397 ldlm_namespace_free(m->mdt_namespace, NULL,
4398 d->ld_obd->obd_force);
4399 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4402 mdt_quota_fini(env, m);
4404 cfs_free_nidlist(&m->mdt_nosquash_nids);
4405 if (m->mdt_nosquash_str) {
4406 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4407 m->mdt_nosquash_str = NULL;
4408 m->mdt_nosquash_strlen = 0;
4411 mdt_seq_fini(env, m);
4412 mdt_fld_fini(env, m);
4413 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4415 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4416 cfs_timer_disarm(&m->mdt_ck_timer);
4417 mdt_ck_thread_stop(m);
4422 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4424 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4426 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4431 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4433 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4434 struct sptlrpc_rule_set tmp_rset;
4437 sptlrpc_rule_set_init(&tmp_rset);
4438 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4440 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4445 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4447 write_lock(&m->mdt_sptlrpc_lock);
4448 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4449 m->mdt_sptlrpc_rset = tmp_rset;
4450 write_unlock(&m->mdt_sptlrpc_lock);
4455 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4457 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4458 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4460 struct mdt_thread_info *info;
4461 struct obd_device *obd;
4462 const char *dev = lustre_cfg_string(cfg, 0);
4463 const char *num = lustre_cfg_string(cfg, 2);
4464 struct lustre_mount_info *lmi = NULL;
4465 struct lustre_sb_info *lsi;
4467 struct seq_server_site *ss_site;
4468 const char *identity_upcall = "NONE";
4469 struct md_device *next;
4475 md_device_init(&m->mdt_md_dev, ldt);
4477 * Environment (env) might be missing mdt_thread_key values at that
4478 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4481 * Usually device allocation path doesn't use module key values, but
4482 * mdt has to do a lot of work here, so allocate key value.
4484 rc = lu_env_refill((struct lu_env *)env);
4488 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4489 LASSERT(info != NULL);
4491 obd = class_name2obd(dev);
4492 LASSERT(obd != NULL);
4494 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4496 m->mdt_som_conf = 0;
4498 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4499 lmi = server_get_mount(dev);
4501 CERROR("Cannot get mount info for %s!\n", dev);
4504 lsi = s2lsi(lmi->lmi_sb);
4505 /* CMD is supported only in IAM mode */
4507 node_id = simple_strtol(num, NULL, 10);
4508 obd->u.obt.obt_magic = OBT_MAGIC;
4511 rwlock_init(&m->mdt_sptlrpc_lock);
4512 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4514 spin_lock_init(&m->mdt_ioepoch_lock);
4515 m->mdt_opts.mo_compat_resname = 0;
4516 m->mdt_opts.mo_mds_capa = 1;
4517 m->mdt_opts.mo_oss_capa = 1;
4518 m->mdt_capa_timeout = CAPA_TIMEOUT;
4519 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4520 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4521 m->mdt_squash_uid = 0;
4522 m->mdt_squash_gid = 0;
4523 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4524 m->mdt_nosquash_str = NULL;
4525 m->mdt_nosquash_strlen = 0;
4526 init_rwsem(&m->mdt_squash_sem);
4527 spin_lock_init(&m->mdt_osfs_lock);
4528 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4529 m->mdt_enable_remote_dir = 0;
4531 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4532 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4533 /* set this lu_device to obd, because error handling need it */
4534 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4536 /* init the stack */
4537 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4539 CERROR("Can't init device stack, rc %d\n", rc);
4543 s = m->mdt_md_dev.md_lu_dev.ld_site;
4544 ss_site = &m->mdt_seq_site;
4545 s->ld_seq_site = ss_site;
4548 /* set server index */
4549 ss_site->ss_node_id = node_id;
4551 /* failover is the default
4552 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4553 * assumed whose ss_node_id == 0 XXX
4555 obd->obd_replayable = 1;
4556 /* No connection accepted until configurations will finish */
4557 obd->obd_no_conn = 1;
4559 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4560 char *str = lustre_cfg_string(cfg, 4);
4561 if (strchr(str, 'n')) {
4562 CWARN("%s: recovery disabled\n", obd->obd_name);
4563 obd->obd_replayable = 0;
4567 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4569 GOTO(err_fini_stack, rc);
4571 rc = mdt_fld_init(env, obd->obd_name, m);
4575 rc = mdt_seq_init(env, obd->obd_name, m);
4577 GOTO(err_fini_fld, rc);
4579 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4580 LUSTRE_MDT_NAME"-%p", m);
4581 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4582 LDLM_NAMESPACE_SERVER,
4583 LDLM_NAMESPACE_GREEDY,
4585 if (m->mdt_namespace == NULL)
4586 GOTO(err_fini_seq, rc = -ENOMEM);
4588 m->mdt_namespace->ns_lvbp = m;
4589 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4591 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4592 /* set obd_namespace for compatibility with old code */
4593 obd->obd_namespace = m->mdt_namespace;
4595 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4597 rc = mdt_ck_thread_start(m);
4599 GOTO(err_free_ns, rc);
4601 rc = mdt_fs_setup(env, m, obd, lsi);
4605 mdt_adapt_sptlrpc_conf(obd, 1);
4607 next = m->mdt_child;
4608 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4611 GOTO(err_llog_cleanup, rc);
4613 if (mntopts & MNTOPT_USERXATTR)
4614 m->mdt_opts.mo_user_xattr = 1;
4616 m->mdt_opts.mo_user_xattr = 0;
4618 if (mntopts & MNTOPT_ACL)
4619 m->mdt_opts.mo_acl = 1;
4621 m->mdt_opts.mo_acl = 0;
4623 /* XXX: to support suppgid for ACL, we enable identity_upcall
4624 * by default, otherwise, maybe got unexpected -EACCESS. */
4625 if (m->mdt_opts.mo_acl)
4626 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4628 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
4629 &mdt_identity_upcall_cache_ops);
4630 if (IS_ERR(m->mdt_identity_cache)) {
4631 rc = PTR_ERR(m->mdt_identity_cache);
4632 m->mdt_identity_cache = NULL;
4633 GOTO(err_llog_cleanup, rc);
4636 rc = mdt_procfs_init(m, dev);
4638 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4639 GOTO(err_recovery, rc);
4642 rc = mdt_quota_init(env, m, cfg);
4644 GOTO(err_procfs, rc);
4646 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4647 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4648 "mdt_ldlm_client", m->mdt_ldlm_client);
4650 ping_evictor_start();
4652 /* recovery will be started upon mdt_prepare()
4653 * when the whole stack is complete and ready
4654 * to serve the requests */
4656 mdt_init_capa_ctxt(env, m);
4658 /* Reduce the initial timeout on an MDS because it doesn't need such
4659 * a long timeout as an OST does. Adaptive timeouts will adjust this
4660 * value appropriately. */
4661 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4662 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4669 target_recovery_fini(obd);
4670 upcall_cache_cleanup(m->mdt_identity_cache);
4671 m->mdt_identity_cache = NULL;
4673 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4674 mdt_fs_cleanup(env, m);
4676 cfs_timer_disarm(&m->mdt_ck_timer);
4677 mdt_ck_thread_stop(m);
4679 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4680 obd->obd_namespace = m->mdt_namespace = NULL;
4682 mdt_seq_fini(env, m);
4684 mdt_fld_fini(env, m);
4686 tgt_fini(env, &m->mdt_lut);
4688 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4691 server_put_mount(dev, lmi->lmi_mnt);
4695 /* For interoperability, the left element is old parameter, the right one
4696 * is the new version of the parameter, if some parameter is deprecated,
4697 * the new version should be set as NULL. */
4698 static struct cfg_interop_param mdt_interop_param[] = {
4699 { "mdt.group_upcall", NULL },
4700 { "mdt.quota_type", NULL },
4701 { "mdd.quota_type", NULL },
4702 { "mdt.rootsquash", "mdt.root_squash" },
4703 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4707 /* used by MGS to process specific configurations */
4708 static int mdt_process_config(const struct lu_env *env,
4709 struct lu_device *d, struct lustre_cfg *cfg)
4711 struct mdt_device *m = mdt_dev(d);
4712 struct md_device *md_next = m->mdt_child;
4713 struct lu_device *next = md2lu_dev(md_next);
4717 switch (cfg->lcfg_command) {
4719 struct lprocfs_static_vars lvars;
4720 struct obd_device *obd = d->ld_obd;
4722 /* For interoperability */
4723 struct cfg_interop_param *ptr = NULL;
4724 struct lustre_cfg *old_cfg = NULL;
4727 param = lustre_cfg_string(cfg, 1);
4728 if (param == NULL) {
4729 CERROR("param is empty\n");
4734 ptr = class_find_old_param(param, mdt_interop_param);
4736 if (ptr->new_param == NULL) {
4738 CWARN("For interoperability, skip this %s."
4739 " It is obsolete.\n", ptr->old_param);
4743 CWARN("Found old param %s, changed it to %s.\n",
4744 ptr->old_param, ptr->new_param);
4747 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4754 lprocfs_mdt_init_vars(&lvars);
4755 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4757 if (rc > 0 || rc == -ENOSYS)
4758 /* we don't understand; pass it on */
4759 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4761 if (old_cfg != NULL)
4762 lustre_cfg_free(cfg);
4767 /* others are passed further */
4768 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4774 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4775 const struct lu_object_header *hdr,
4776 struct lu_device *d)
4778 struct mdt_object *mo;
4782 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
4784 struct lu_object *o;
4785 struct lu_object_header *h;
4787 o = &mo->mot_obj.mo_lu;
4788 h = &mo->mot_header;
4789 lu_object_header_init(h);
4790 lu_object_init(o, h, d);
4791 lu_object_add_top(h, o);
4792 o->lo_ops = &mdt_obj_ops;
4793 mutex_init(&mo->mot_ioepoch_mutex);
4794 mutex_init(&mo->mot_lov_mutex);
4800 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4801 const struct lu_object_conf *unused)
4803 struct mdt_device *d = mdt_dev(o->lo_dev);
4804 struct lu_device *under;
4805 struct lu_object *below;
4809 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4810 PFID(lu_object_fid(o)));
4812 under = &d->mdt_child->md_lu_dev;
4813 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4814 if (below != NULL) {
4815 lu_object_add(o, below);
4822 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4824 struct mdt_object *mo = mdt_obj(o);
4825 struct lu_object_header *h;
4829 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4830 PFID(lu_object_fid(o)));
4833 lu_object_header_fini(h);
4834 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4839 static int mdt_object_print(const struct lu_env *env, void *cookie,
4840 lu_printer_t p, const struct lu_object *o)
4842 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4843 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4844 "flags="LPX64", epochcount=%d, writecount=%d)",
4845 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4846 mdto->mot_ioepoch_count, mdto->mot_writecount);
4849 static int mdt_prepare(const struct lu_env *env,
4850 struct lu_device *pdev,
4851 struct lu_device *cdev)
4853 struct mdt_device *mdt = mdt_dev(cdev);
4854 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4855 struct obd_device *obd = cdev->ld_obd;
4862 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4866 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4870 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4871 &mdt->mdt_md_root_fid);
4875 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4876 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
4877 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4878 LASSERT(obd->obd_no_conn);
4879 spin_lock(&obd->obd_dev_lock);
4880 obd->obd_no_conn = 0;
4881 spin_unlock(&obd->obd_dev_lock);
4883 if (obd->obd_recovering == 0)
4884 mdt_postrecov(env, mdt);
4889 const struct lu_device_operations mdt_lu_ops = {
4890 .ldo_object_alloc = mdt_object_alloc,
4891 .ldo_process_config = mdt_process_config,
4892 .ldo_prepare = mdt_prepare,
4895 static const struct lu_object_operations mdt_obj_ops = {
4896 .loo_object_init = mdt_object_init,
4897 .loo_object_free = mdt_object_free,
4898 .loo_object_print = mdt_object_print
4901 static int mdt_obd_set_info_async(const struct lu_env *env,
4902 struct obd_export *exp,
4903 __u32 keylen, void *key,
4904 __u32 vallen, void *val,
4905 struct ptlrpc_request_set *set)
4907 struct obd_device *obd = exp->exp_obd;
4913 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4914 rc = mdt_adapt_sptlrpc_conf(obd, 0);
4922 * Match client and server connection feature flags.
4924 * Compute the compatibility flags for a connection request based on
4925 * features mutually supported by client and server.
4927 * The obd_export::exp_connect_flags field in \a exp must not be updated
4928 * here, otherwise a partially initialized value may be exposed. After
4929 * the connection request is successfully processed, the top-level MDT
4930 * connect request handler atomically updates the export connect flags
4931 * from the obd_connect_data::ocd_connect_flags field of the reply.
4932 * \see mdt_connect().
4934 * \param exp the obd_export associated with this client/target pair
4935 * \param mdt the target device for the connection
4936 * \param data stores data for this connect request
4939 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4940 * \retval -EBADE client and server feature requirements are incompatible
4942 static int mdt_connect_internal(struct obd_export *exp,
4943 struct mdt_device *mdt,
4944 struct obd_connect_data *data)
4946 LASSERT(data != NULL);
4948 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4949 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4951 /* If no known bits (which should not happen, probably,
4952 as everybody should support LOOKUP and UPDATE bits at least)
4953 revert to compat mode with plain locks. */
4954 if (!data->ocd_ibits_known &&
4955 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4956 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4958 if (!mdt->mdt_opts.mo_acl)
4959 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4961 if (!mdt->mdt_opts.mo_user_xattr)
4962 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4964 if (!mdt->mdt_som_conf)
4965 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4967 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4968 data->ocd_brw_size = min(data->ocd_brw_size,
4969 (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
4970 if (data->ocd_brw_size == 0) {
4971 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4972 " ocd_version: %x ocd_grant: %d "
4973 "ocd_index: %u ocd_brw_size is "
4974 "unexpectedly zero, network data "
4975 "corruption? Refusing connection of this"
4977 exp->exp_obd->obd_name,
4978 exp->exp_client_uuid.uuid,
4979 exp, data->ocd_connect_flags, data->ocd_version,
4980 data->ocd_grant, data->ocd_index);
4985 /* NB: Disregard the rule against updating exp_connect_flags in this
4986 * case, since tgt_client_new() needs to know if this is a lightweight
4987 * connection, and it is safe to expose this flag before connection
4988 * processing completes. */
4989 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4990 spin_lock(&exp->exp_lock);
4991 exp->exp_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
4992 spin_unlock(&exp->exp_lock);
4995 data->ocd_version = LUSTRE_VERSION_CODE;
4996 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4998 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4999 CWARN("%s: MDS requires FID support, but client not\n",
5000 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5004 if (mdt->mdt_som_conf &&
5005 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5006 OBD_CONNECT_MDS_MDS |
5007 OBD_CONNECT_SOM))) {
5008 CWARN("%s: MDS has SOM enabled, but client does not support "
5009 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5016 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5017 struct obd_export *exp,
5018 struct ptlrpc_request *req)
5020 struct sptlrpc_flavor flvr;
5023 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5024 read_lock(&mdt->mdt_sptlrpc_lock);
5025 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5029 read_unlock(&mdt->mdt_sptlrpc_lock);
5031 spin_lock(&exp->exp_lock);
5033 exp->exp_sp_peer = req->rq_sp_from;
5034 exp->exp_flvr = flvr;
5036 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5037 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5038 CERROR("unauthorized rpc flavor %x from %s, "
5039 "expect %x\n", req->rq_flvr.sf_rpc,
5040 libcfs_nid2str(req->rq_peer.nid),
5041 exp->exp_flvr.sf_rpc);
5045 spin_unlock(&exp->exp_lock);
5047 if (exp->exp_sp_peer != req->rq_sp_from) {
5048 CERROR("RPC source %s doesn't match %s\n",
5049 sptlrpc_part2name(req->rq_sp_from),
5050 sptlrpc_part2name(exp->exp_sp_peer));
5053 rc = sptlrpc_target_export_check(exp, req);
5060 /* mds_connect copy */
5061 static int mdt_obd_connect(const struct lu_env *env,
5062 struct obd_export **exp, struct obd_device *obd,
5063 struct obd_uuid *cluuid,
5064 struct obd_connect_data *data,
5067 struct mdt_thread_info *info;
5068 struct obd_export *lexp;
5069 struct lustre_handle conn = { 0 };
5070 struct mdt_device *mdt;
5071 struct ptlrpc_request *req;
5075 LASSERT(env != NULL);
5076 if (!exp || !obd || !cluuid)
5079 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5080 req = info->mti_pill->rc_req;
5081 mdt = mdt_dev(obd->obd_lu_dev);
5084 * first, check whether the stack is ready to handle requests
5085 * XXX: probably not very appropriate method is used now
5086 * at some point we should find a better one
5088 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5089 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5090 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5093 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5096 rc = class_connect(&conn, obd, cluuid);
5100 lexp = class_conn2export(&conn);
5101 LASSERT(lexp != NULL);
5103 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5107 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5108 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5110 rc = mdt_connect_internal(lexp, mdt, data);
5112 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5115 info->mti_exp = lexp;
5116 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5117 rc = tgt_client_new(env, lexp);
5119 mdt_export_stats_init(obd, lexp, localdata);
5124 class_disconnect(lexp);
5133 static int mdt_obd_reconnect(const struct lu_env *env,
5134 struct obd_export *exp, struct obd_device *obd,
5135 struct obd_uuid *cluuid,
5136 struct obd_connect_data *data,
5139 struct mdt_thread_info *info;
5140 struct mdt_device *mdt;
5141 struct ptlrpc_request *req;
5145 if (exp == NULL || obd == NULL || cluuid == NULL)
5148 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5149 req = info->mti_pill->rc_req;
5150 mdt = mdt_dev(obd->obd_lu_dev);
5152 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5156 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5158 mdt_export_stats_init(obd, exp, localdata);
5163 static int mdt_export_cleanup(struct obd_export *exp)
5165 struct mdt_export_data *med = &exp->exp_mdt_data;
5166 struct obd_device *obd = exp->exp_obd;
5167 struct mdt_device *mdt;
5168 struct mdt_thread_info *info;
5170 CFS_LIST_HEAD(closing_list);
5171 struct mdt_file_data *mfd, *n;
5175 spin_lock(&med->med_open_lock);
5176 while (!cfs_list_empty(&med->med_open_head)) {
5177 cfs_list_t *tmp = med->med_open_head.next;
5178 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5180 /* Remove mfd handle so it can't be found again.
5181 * We are consuming the mfd_list reference here. */
5182 class_handle_unhash(&mfd->mfd_handle);
5183 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5185 spin_unlock(&med->med_open_lock);
5186 mdt = mdt_dev(obd->obd_lu_dev);
5187 LASSERT(mdt != NULL);
5189 rc = lu_env_init(&env, LCT_MD_THREAD);
5193 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5194 LASSERT(info != NULL);
5195 memset(info, 0, sizeof *info);
5196 info->mti_env = &env;
5197 info->mti_mdt = mdt;
5198 info->mti_exp = exp;
5200 if (!cfs_list_empty(&closing_list)) {
5201 struct md_attr *ma = &info->mti_attr;
5203 /* Close any open files (which may also cause orphan unlinking). */
5204 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5205 cfs_list_del_init(&mfd->mfd_list);
5206 ma->ma_need = ma->ma_valid = 0;
5207 /* Don't unlink orphan on failover umount, LU-184 */
5208 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5209 ma->ma_valid = MA_FLAGS;
5210 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5212 mdt_mfd_close(info, mfd);
5215 info->mti_mdt = NULL;
5216 /* cleanup client slot early */
5217 /* Do not erase record for recoverable client. */
5218 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5219 tgt_client_del(&env, exp);
5225 static int mdt_obd_disconnect(struct obd_export *exp)
5231 class_export_get(exp);
5233 rc = server_disconnect_export(exp);
5235 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5237 rc = mdt_export_cleanup(exp);
5238 class_export_put(exp);
5242 /* FIXME: Can we avoid using these two interfaces? */
5243 static int mdt_init_export(struct obd_export *exp)
5245 struct mdt_export_data *med = &exp->exp_mdt_data;
5249 CFS_INIT_LIST_HEAD(&med->med_open_head);
5250 spin_lock_init(&med->med_open_lock);
5251 mutex_init(&med->med_idmap_mutex);
5252 med->med_idmap = NULL;
5253 spin_lock(&exp->exp_lock);
5254 exp->exp_connecting = 1;
5255 spin_unlock(&exp->exp_lock);
5257 /* self-export doesn't need client data and ldlm initialization */
5258 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5259 &exp->exp_client_uuid)))
5262 rc = tgt_client_alloc(exp);
5266 rc = ldlm_init_export(exp);
5273 tgt_client_free(exp);
5275 CERROR("%s: Failed to initialize export: rc = %d\n",
5276 exp->exp_obd->obd_name, rc);
5280 static int mdt_destroy_export(struct obd_export *exp)
5284 if (exp_connect_rmtclient(exp))
5285 mdt_cleanup_idmap(&exp->exp_mdt_data);
5287 target_destroy_export(exp);
5288 /* destroy can be called from failed obd_setup, so
5289 * checking uuid is safer than obd_self_export */
5290 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5291 &exp->exp_client_uuid)))
5294 ldlm_destroy_export(exp);
5295 tgt_client_free(exp);
5297 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5298 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5303 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5304 void *val, int vallen)
5306 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5307 struct getinfo_fid2path *fpout, *fpin;
5310 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5313 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5314 lustre_swab_fid2path(fpin);
5316 memcpy(fpout, fpin, sizeof(*fpin));
5317 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5320 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5324 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5325 struct getinfo_fid2path *fp)
5327 struct mdt_object *obj;
5328 struct obd_device *obd = mdt2obd_dev(mdt);
5332 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5333 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5335 if (!fid_is_sane(&fp->gf_fid))
5338 if (!fid_is_norm(&fp->gf_fid) && !fid_is_igif(&fp->gf_fid)) {
5339 CWARN("%s: "DFID" is invalid, sequence should be "
5340 ">= "LPX64"\n", obd->obd_name,
5341 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5345 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5346 if (obj == NULL || IS_ERR(obj)) {
5347 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5352 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5358 mdt_object_put(env, obj);
5359 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5360 PFID(&fp->gf_fid), rc);
5364 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5365 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5366 mdt_object_put(env, obj);
5371 int mdt_get_info(struct mdt_thread_info *info)
5373 struct ptlrpc_request *req = mdt_info_req(info);
5381 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5383 CDEBUG(D_IOCTL, "No GETINFO key");
5386 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5389 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5390 if (vallen == NULL) {
5391 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5395 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5397 rc = req_capsule_server_pack(info->mti_pill);
5398 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5399 if (valout == NULL) {
5400 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5404 if (KEY_IS(KEY_FID2PATH))
5405 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5409 lustre_msg_set_status(req->rq_repmsg, rc);
5414 /* Pass the ioc down */
5415 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5416 unsigned int cmd, int len, void *data)
5418 struct lu_context ioctl_session;
5419 struct md_device *next = mdt->mdt_child;
5423 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5426 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5427 lu_context_enter(&ioctl_session);
5428 env->le_ses = &ioctl_session;
5430 LASSERT(next->md_ops->mdo_iocontrol);
5431 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5433 lu_context_exit(&ioctl_session);
5434 lu_context_fini(&ioctl_session);
5438 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5440 struct obd_ioctl_data *data = karg;
5441 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5443 struct mdt_object *obj;
5444 struct mdt_lock_handle *lh;
5448 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5449 if (!fid_is_sane(fid))
5452 lh = &mti->mti_lh[MDT_LH_PARENT];
5453 mdt_lock_reg_init(lh, LCK_CR);
5455 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5457 RETURN(PTR_ERR(obj));
5459 rc = mdt_object_exists(obj);
5463 * before calling version get the correct MDS should be
5464 * fid, this is error to find remote object here
5466 CERROR("nonlocal object "DFID"\n", PFID(fid));
5467 } else if (rc == 0) {
5468 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5471 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5472 *(__u64 *)data->ioc_inlbuf2 = version;
5475 mdt_object_unlock_put(mti, obj, lh, 1);
5479 /* ioctls on obd dev */
5480 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5481 void *karg, void *uarg)
5484 struct obd_device *obd = exp->exp_obd;
5485 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5486 struct dt_device *dt = mdt->mdt_bottom;
5490 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5491 rc = lu_env_init(&env, LCT_MD_THREAD);
5497 rc = mdt_device_sync(&env, mdt);
5499 case OBD_IOC_SET_READONLY:
5500 rc = dt->dd_ops->dt_ro(&env, dt);
5502 case OBD_IOC_ABORT_RECOVERY:
5503 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5504 target_stop_recovery_thread(obd);
5507 case OBD_IOC_CHANGELOG_REG:
5508 case OBD_IOC_CHANGELOG_DEREG:
5509 case OBD_IOC_CHANGELOG_CLEAR:
5510 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5512 case OBD_IOC_START_LFSCK:
5513 case OBD_IOC_STOP_LFSCK: {
5514 struct md_device *next = mdt->mdt_child;
5515 struct obd_ioctl_data *data = karg;
5517 if (unlikely(data == NULL)) {
5522 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
5527 case OBD_IOC_GET_OBJ_VERSION: {
5528 struct mdt_thread_info *mti;
5529 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5530 memset(mti, 0, sizeof *mti);
5531 mti->mti_env = &env;
5535 rc = mdt_ioc_version_get(mti, karg);
5539 CERROR("Not supported cmd = %d for device %s\n",
5540 cmd, obd->obd_name);
5548 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5550 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5554 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5558 int mdt_obd_postrecov(struct obd_device *obd)
5563 rc = lu_env_init(&env, LCT_MD_THREAD);
5566 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5571 static struct obd_ops mdt_obd_device_ops = {
5572 .o_owner = THIS_MODULE,
5573 .o_set_info_async = mdt_obd_set_info_async,
5574 .o_connect = mdt_obd_connect,
5575 .o_reconnect = mdt_obd_reconnect,
5576 .o_disconnect = mdt_obd_disconnect,
5577 .o_init_export = mdt_init_export,
5578 .o_destroy_export = mdt_destroy_export,
5579 .o_iocontrol = mdt_iocontrol,
5580 .o_postrecov = mdt_obd_postrecov,
5583 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5584 struct lu_device *d)
5586 struct mdt_device *m = mdt_dev(d);
5593 static struct lu_device *mdt_device_free(const struct lu_env *env,
5594 struct lu_device *d)
5596 struct mdt_device *m = mdt_dev(d);
5599 md_device_fini(&m->mdt_md_dev);
5604 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5605 struct lu_device_type *t,
5606 struct lustre_cfg *cfg)
5608 struct lu_device *l;
5609 struct mdt_device *m;
5615 l = &m->mdt_md_dev.md_lu_dev;
5616 rc = mdt_init0(env, m, t, cfg);
5618 mdt_device_free(env, l);
5623 l = ERR_PTR(-ENOMEM);
5627 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5628 LU_KEY_INIT(mdt, struct mdt_thread_info);
5630 static void mdt_key_fini(const struct lu_context *ctx,
5631 struct lu_context_key *key, void* data)
5633 struct mdt_thread_info *info = data;
5635 if (info->mti_big_lmm) {
5636 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5637 info->mti_big_lmm = NULL;
5638 info->mti_big_lmmsize = 0;
5643 /* context key: mdt_thread_key */
5644 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5646 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5648 return lu_ucred(info->mti_env);
5651 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5653 return lu_ucred_check(info->mti_env);
5657 * Enable/disable COS (Commit On Sharing).
5659 * Set/Clear the COS flag in mdt options.
5661 * \param mdt mdt device
5662 * \param val 0 disables COS, other values enable COS
5664 void mdt_enable_cos(struct mdt_device *mdt, int val)
5669 mdt->mdt_opts.mo_cos = !!val;
5670 rc = lu_env_init(&env, LCT_LOCAL);
5671 if (unlikely(rc != 0)) {
5672 CWARN("lu_env initialization failed with rc = %d,"
5673 "cannot sync\n", rc);
5676 mdt_device_sync(&env, mdt);
5681 * Check COS (Commit On Sharing) status.
5683 * Return COS flag status.
5685 * \param mdt mdt device
5687 int mdt_cos_is_enabled(struct mdt_device *mdt)
5689 return mdt->mdt_opts.mo_cos != 0;
5692 static struct lu_device_type_operations mdt_device_type_ops = {
5693 .ldto_device_alloc = mdt_device_alloc,
5694 .ldto_device_free = mdt_device_free,
5695 .ldto_device_fini = mdt_device_fini
5698 static struct lu_device_type mdt_device_type = {
5699 .ldt_tags = LU_DEVICE_MD,
5700 .ldt_name = LUSTRE_MDT_NAME,
5701 .ldt_ops = &mdt_device_type_ops,
5702 .ldt_ctx_tags = LCT_MD_THREAD
5705 static int __init mdt_mod_init(void)
5707 struct lprocfs_static_vars lvars;
5710 rc = lu_kmem_init(mdt_caches);
5714 rc = mds_mod_init();
5718 lprocfs_mdt_init_vars(&lvars);
5719 rc = class_register_type(&mdt_obd_device_ops, NULL,
5720 lvars.module_vars, LUSTRE_MDT_NAME,
5726 lu_kmem_fini(mdt_caches);
5733 static void __exit mdt_mod_exit(void)
5735 class_unregister_type(LUSTRE_MDT_NAME);
5737 lu_kmem_fini(mdt_caches);
5740 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5741 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5742 MODULE_LICENSE("GPL");
5744 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);