1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
50 # define EXPORT_SYMTAB
52 #define DEBUG_SUBSYSTEM S_MDS
54 #include <linux/module.h>
56 * struct OBD_{ALLOC,FREE}*()
58 #include <obd_support.h>
59 /* struct ptlrpc_request */
60 #include <lustre_net.h>
61 /* struct obd_export */
62 #include <lustre_export.h>
63 /* struct obd_device */
66 #include <dt_object.h>
67 #include <lustre_mds.h>
68 #include <lustre_mdt.h>
69 #include "mdt_internal.h"
70 #ifdef HAVE_QUOTA_SUPPORT
71 # include <lustre_quota.h>
73 #include <lustre_acl.h>
74 #include <lustre_param.h>
75 #include <lustre_fsfilt.h>
77 mdl_mode_t mdt_mdl_lock_modes[] = {
78 [LCK_MINMODE] = MDL_MINMODE,
85 [LCK_GROUP] = MDL_GROUP
88 ldlm_mode_t mdt_dlm_lock_modes[] = {
89 [MDL_MINMODE] = LCK_MINMODE,
96 [MDL_GROUP] = LCK_GROUP
100 * Initialized in mdt_mod_init().
102 unsigned long mdt_num_threads;
104 /* ptlrpc request handler for MDT. All handlers are
105 * grouped into several slices - struct mdt_opc_slice,
106 * and stored in an array - mdt_handlers[].
109 /* The name of this handler. */
111 /* Fail id for this handler, checked at the beginning of this handler*/
113 /* Operation code for this handler */
115 /* flags are listed in enum mdt_handler_flags below. */
117 /* The actual handler function to execute. */
118 int (*mh_act)(struct mdt_thread_info *info);
119 /* Request format for this request. */
120 const struct req_format *mh_fmt;
123 enum mdt_handler_flags {
125 * struct mdt_body is passed in the incoming message, and object
126 * identified by this fid exists on disk.
128 * "habeo corpus" == "I have a body"
130 HABEO_CORPUS = (1 << 0),
132 * struct ldlm_request is passed in the incoming message.
134 * "habeo clavis" == "I have a key"
136 HABEO_CLAVIS = (1 << 1),
138 * this request has fixed reply format, so that reply message can be
139 * packed by generic code.
141 * "habeo refero" == "I have a reply"
143 HABEO_REFERO = (1 << 2),
145 * this request will modify something, so check whether the filesystem
146 * is readonly or not, then return -EROFS to client asap if necessary.
148 * "mutabor" == "I shall modify"
153 struct mdt_opc_slice {
156 struct mdt_handler *mos_hs;
159 static struct mdt_opc_slice mdt_regular_handlers[];
160 static struct mdt_opc_slice mdt_readpage_handlers[];
161 static struct mdt_opc_slice mdt_xmds_handlers[];
162 static struct mdt_opc_slice mdt_seq_handlers[];
163 static struct mdt_opc_slice mdt_fld_handlers[];
165 static struct mdt_device *mdt_dev(struct lu_device *d);
166 static int mdt_regular_handle(struct ptlrpc_request *req);
167 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
169 static const struct lu_object_operations mdt_obj_ops;
171 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
175 return (rep->lock_policy_res1 & flag);
178 void mdt_clear_disposition(struct mdt_thread_info *info,
179 struct ldlm_reply *rep, int flag)
182 info->mti_opdata &= ~flag;
184 rep->lock_policy_res1 &= ~flag;
187 void mdt_set_disposition(struct mdt_thread_info *info,
188 struct ldlm_reply *rep, int flag)
191 info->mti_opdata |= flag;
193 rep->lock_policy_res1 |= flag;
196 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
198 lh->mlh_pdo_hash = 0;
199 lh->mlh_reg_mode = lm;
200 lh->mlh_type = MDT_REG_LOCK;
203 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
204 const char *name, int namelen)
206 lh->mlh_reg_mode = lm;
207 lh->mlh_type = MDT_PDO_LOCK;
210 LASSERT(namelen > 0);
211 lh->mlh_pdo_hash = full_name_hash(name, namelen);
213 LASSERT(namelen == 0);
214 lh->mlh_pdo_hash = 0ull;
218 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
219 struct mdt_lock_handle *lh)
225 * Any dir access needs couple of locks:
227 * 1) on part of dir we gonna take lookup/modify;
229 * 2) on whole dir to protect it from concurrent splitting and/or to
230 * flush client's cache for readdir().
232 * so, for a given mode and object this routine decides what lock mode
233 * to use for lock #2:
235 * 1) if caller's gonna lookup in dir then we need to protect dir from
236 * being splitted only - LCK_CR
238 * 2) if caller's gonna modify dir then we need to protect dir from
239 * being splitted and to flush cache - LCK_CW
241 * 3) if caller's gonna modify dir and that dir seems ready for
242 * splitting then we need to protect it from any type of access
243 * (lookup/modify/split) - LCK_EX --bzzz
246 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
247 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
250 * Ask underlaying level its opinion about preferable PDO lock mode
251 * having access type passed as regular lock mode:
253 * - MDL_MINMODE means that lower layer does not want to specify lock
256 * - MDL_NL means that no PDO lock should be taken. This is used in some
257 * cases. Say, for non-splittable directories no need to use PDO locks
260 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
261 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
263 if (mode != MDL_MINMODE) {
264 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
267 * Lower layer does not want to specify locking mode. We do it
268 * our selves. No special protection is needed, just flush
269 * client's cache on modification and allow concurrent
272 switch (lh->mlh_reg_mode) {
274 lh->mlh_pdo_mode = LCK_EX;
277 lh->mlh_pdo_mode = LCK_CR;
280 lh->mlh_pdo_mode = LCK_CW;
283 CERROR("Not expected lock type (0x%x)\n",
284 (int)lh->mlh_reg_mode);
289 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
293 static int mdt_getstatus(struct mdt_thread_info *info)
295 struct mdt_device *mdt = info->mti_mdt;
296 struct md_device *next = mdt->mdt_child;
297 struct mdt_body *repbody;
302 rc = mdt_check_ucred(info);
304 RETURN(err_serious(rc));
306 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
307 RETURN(err_serious(-ENOMEM));
309 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
310 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
314 repbody->valid |= OBD_MD_FLID;
316 if (mdt->mdt_opts.mo_mds_capa &&
317 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
318 struct mdt_object *root;
319 struct lustre_capa *capa;
321 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
323 RETURN(PTR_ERR(root));
325 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
327 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
328 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
330 mdt_object_put(info->mti_env, root);
332 repbody->valid |= OBD_MD_FLMDSCAPA;
338 static int mdt_statfs(struct mdt_thread_info *info)
340 struct md_device *next = info->mti_mdt->mdt_child;
341 struct ptlrpc_service *svc;
342 struct obd_statfs *osfs;
347 svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
349 /* This will trigger a watchdog timeout */
350 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
351 (MDT_SERVICE_WATCHDOG_FACTOR *
352 at_get(&svc->srv_at_estimate) / 1000) + 1);
354 rc = mdt_check_ucred(info);
356 RETURN(err_serious(rc));
358 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
359 rc = err_serious(-ENOMEM);
361 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
362 rc = next->md_ops->mdo_statfs(info->mti_env, next,
364 statfs_pack(osfs, &info->mti_u.ksfs);
369 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
372 struct lu_attr *attr = &info->mti_attr.ma_attr;
374 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
376 /* Check if Size-on-MDS is enabled. */
377 if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
378 S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
379 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
380 b->size = attr->la_size;
381 b->blocks = attr->la_blocks;
385 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
386 const struct lu_attr *attr, const struct lu_fid *fid)
388 /*XXX should pack the reply body according to lu_valid*/
389 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
390 OBD_MD_FLGID | OBD_MD_FLTYPE |
391 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
392 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
394 if (!S_ISREG(attr->la_mode))
395 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
397 b->atime = attr->la_atime;
398 b->mtime = attr->la_mtime;
399 b->ctime = attr->la_ctime;
400 b->mode = attr->la_mode;
401 b->size = attr->la_size;
402 b->blocks = attr->la_blocks;
403 b->uid = attr->la_uid;
404 b->gid = attr->la_gid;
405 b->flags = attr->la_flags;
406 b->nlink = attr->la_nlink;
407 b->rdev = attr->la_rdev;
411 b->valid |= OBD_MD_FLID;
413 /* FIXME: these should be fixed when new igif ready.*/
414 b->ino = fid_oid(fid); /* 1.6 compatibility */
415 b->generation = fid_ver(fid); /* 1.6 compatibility */
416 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
418 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
419 PFID(fid), b->nlink, b->mode, b->size);
423 mdt_body_reverse_idmap(info, b);
426 static inline int mdt_body_has_lov(const struct lu_attr *la,
427 const struct mdt_body *body)
429 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
430 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
433 static int mdt_getattr_internal(struct mdt_thread_info *info,
434 struct mdt_object *o)
436 struct md_object *next = mdt_object_child(o);
437 const struct mdt_body *reqbody = info->mti_body;
438 struct ptlrpc_request *req = mdt_info_req(info);
439 struct md_attr *ma = &info->mti_attr;
440 struct lu_attr *la = &ma->ma_attr;
441 struct req_capsule *pill = info->mti_pill;
442 const struct lu_env *env = info->mti_env;
443 struct mdt_body *repbody;
444 struct lu_buf *buffer = &info->mti_buf;
448 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
449 RETURN(err_serious(-ENOMEM));
451 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
455 rc = mdt_object_exists(o);
457 /* This object is located on remote node.*/
458 repbody->fid1 = *mdt_object_fid(o);
459 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
463 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
464 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
466 /* If it is dir object and client require MEA, then we got MEA */
467 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
468 reqbody->valid & OBD_MD_MEA) {
469 /* Assumption: MDT_MD size is enough for lmv size. */
470 ma->ma_lmv = buffer->lb_buf;
471 ma->ma_lmv_size = buffer->lb_len;
472 ma->ma_need = MA_LMV | MA_INODE;
474 ma->ma_lmm = buffer->lb_buf;
475 ma->ma_lmm_size = buffer->lb_len;
476 ma->ma_need = MA_LOV | MA_INODE;
479 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
480 reqbody->valid & OBD_MD_FLDIREA &&
481 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
482 /* get default stripe info for this dir. */
483 ma->ma_need |= MA_LOV_DEF;
485 rc = mo_attr_get(env, next, ma);
487 CERROR("getattr error for "DFID": %d\n",
488 PFID(mdt_object_fid(o)), rc);
492 if (likely(ma->ma_valid & MA_INODE))
493 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
497 if (mdt_body_has_lov(la, reqbody)) {
498 if (ma->ma_valid & MA_LOV) {
499 LASSERT(ma->ma_lmm_size);
500 mdt_dump_lmm(D_INFO, ma->ma_lmm);
501 repbody->eadatasize = ma->ma_lmm_size;
502 if (S_ISDIR(la->la_mode))
503 repbody->valid |= OBD_MD_FLDIREA;
505 repbody->valid |= OBD_MD_FLEASIZE;
507 if (ma->ma_valid & MA_LMV) {
508 LASSERT(S_ISDIR(la->la_mode));
509 repbody->eadatasize = ma->ma_lmv_size;
510 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
512 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
513 repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
515 } else if (S_ISLNK(la->la_mode) &&
516 reqbody->valid & OBD_MD_LINKNAME) {
517 buffer->lb_buf = ma->ma_lmm;
518 buffer->lb_len = reqbody->eadatasize;
519 rc = mo_readlink(env, next, buffer);
520 if (unlikely(rc <= 0)) {
521 CERROR("readlink failed: %d\n", rc);
524 repbody->valid |= OBD_MD_LINKNAME;
525 repbody->eadatasize = rc;
527 ((char*)ma->ma_lmm)[rc - 1] = 0;
528 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
529 (char*)ma->ma_lmm, rc);
534 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
535 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
536 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
537 repbody->valid |= OBD_MD_FLMODEASIZE;
538 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
539 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
540 repbody->max_cookiesize);
543 if (exp_connect_rmtclient(info->mti_exp) &&
544 reqbody->valid & OBD_MD_FLRMTPERM) {
545 void *buf = req_capsule_server_get(pill, &RMF_ACL);
547 /* mdt_getattr_lock only */
548 rc = mdt_pack_remote_perm(info, o, buf);
550 repbody->valid &= ~OBD_MD_FLRMTPERM;
551 repbody->aclsize = 0;
554 repbody->valid |= OBD_MD_FLRMTPERM;
555 repbody->aclsize = sizeof(struct mdt_remote_perm);
558 #ifdef CONFIG_FS_POSIX_ACL
559 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
560 (reqbody->valid & OBD_MD_FLACL)) {
561 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
562 buffer->lb_len = req_capsule_get_size(pill,
563 &RMF_ACL, RCL_SERVER);
564 if (buffer->lb_len > 0) {
565 rc = mo_xattr_get(env, next, buffer,
566 XATTR_NAME_ACL_ACCESS);
568 if (rc == -ENODATA) {
569 repbody->aclsize = 0;
570 repbody->valid |= OBD_MD_FLACL;
572 } else if (rc == -EOPNOTSUPP) {
575 CERROR("got acl size: %d\n", rc);
578 repbody->aclsize = rc;
579 repbody->valid |= OBD_MD_FLACL;
586 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
587 info->mti_mdt->mdt_opts.mo_mds_capa &&
588 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
589 struct lustre_capa *capa;
591 capa = req_capsule_server_get(pill, &RMF_CAPA1);
593 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
594 rc = mo_capa_get(env, next, capa, 0);
597 repbody->valid |= OBD_MD_FLMDSCAPA;
602 static int mdt_renew_capa(struct mdt_thread_info *info)
604 struct mdt_object *obj = info->mti_object;
605 struct mdt_body *body;
606 struct lustre_capa *capa, *c;
610 /* if object doesn't exist, or server has disabled capability,
611 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
614 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
615 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
618 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
619 LASSERT(body != NULL);
621 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
624 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
628 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
630 body->valid |= OBD_MD_FLOSSCAPA;
634 static int mdt_getattr(struct mdt_thread_info *info)
636 struct mdt_object *obj = info->mti_object;
637 struct req_capsule *pill = info->mti_pill;
638 struct mdt_body *reqbody;
639 struct mdt_body *repbody;
645 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
648 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
649 rc = req_capsule_server_pack(pill);
651 RETURN(err_serious(rc));
652 rc = mdt_renew_capa(info);
653 GOTO(out_shrink, rc);
656 LASSERT(obj != NULL);
657 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
659 mode = lu_object_attr(&obj->mot_obj.mo_lu);
660 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
661 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
662 md_size = reqbody->eadatasize;
664 md_size = info->mti_mdt->mdt_max_mdsize;
666 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
668 rc = req_capsule_server_pack(pill);
669 if (unlikely(rc != 0))
670 RETURN(err_serious(rc));
672 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
673 LASSERT(repbody != NULL);
674 repbody->eadatasize = 0;
675 repbody->aclsize = 0;
677 if (reqbody->valid & OBD_MD_FLRMTPERM)
678 rc = mdt_init_ucred(info, reqbody);
680 rc = mdt_check_ucred(info);
682 GOTO(out_shrink, rc);
684 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
685 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
688 * Don't check capability at all, because rename might getattr for
689 * remote obj, and at that time no capability is available.
691 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
692 rc = mdt_getattr_internal(info, obj);
693 if (reqbody->valid & OBD_MD_FLRMTPERM)
694 mdt_exit_ucred(info);
697 mdt_shrink_reply(info);
701 static int mdt_is_subdir(struct mdt_thread_info *info)
703 struct mdt_object *o = info->mti_object;
704 struct req_capsule *pill = info->mti_pill;
705 const struct mdt_body *body = info->mti_body;
706 struct mdt_body *repbody;
712 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
715 * We save last checked parent fid to @repbody->fid1 for remote
718 LASSERT(fid_is_sane(&body->fid2));
719 LASSERT(mdt_object_exists(o) > 0);
720 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
721 &body->fid2, &repbody->fid1);
722 if (rc == 0 || rc == -EREMOTE)
723 repbody->valid |= OBD_MD_FLID;
728 static int mdt_raw_lookup(struct mdt_thread_info *info,
729 struct mdt_object *parent,
730 const struct lu_name *lname,
731 struct ldlm_reply *ldlm_rep)
733 struct md_object *next = mdt_object_child(info->mti_object);
734 const struct mdt_body *reqbody = info->mti_body;
735 struct lu_fid *child_fid = &info->mti_tmp_fid1;
736 struct mdt_body *repbody;
740 if (reqbody->valid != OBD_MD_FLID)
743 LASSERT(!info->mti_cross_ref);
745 /* Only got the fid of this obj by name */
746 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
749 /* XXX is raw_lookup possible as intent operation? */
752 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
755 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
757 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
760 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
761 repbody->fid1 = *child_fid;
762 repbody->valid = OBD_MD_FLID;
768 * UPDATE lock should be taken against parent, and be release before exit;
769 * child_bits lock should be taken against child, and be returned back:
770 * (1)normal request should release the child lock;
771 * (2)intent request will grant the lock to client.
773 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
774 struct mdt_lock_handle *lhc,
776 struct ldlm_reply *ldlm_rep)
778 struct ptlrpc_request *req = mdt_info_req(info);
779 struct mdt_body *reqbody = NULL;
780 struct mdt_object *parent = info->mti_object;
781 struct mdt_object *child;
782 struct md_object *next = mdt_object_child(parent);
783 struct lu_fid *child_fid = &info->mti_tmp_fid1;
784 struct lu_name *lname = NULL;
785 const char *name = NULL;
787 struct mdt_lock_handle *lhp;
788 struct ldlm_lock *lock;
789 struct ldlm_res_id *res_id;
795 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
796 LASSERT(ergo(is_resent,
797 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
799 LASSERT(parent != NULL);
800 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
802 RETURN(err_serious(-EFAULT));
804 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
806 if (!info->mti_cross_ref) {
808 * XXX: Check for "namelen == 0" is for getattr by fid
809 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
810 * that is the name must contain at least one character and
811 * the terminating '\0'
814 reqbody = req_capsule_client_get(info->mti_pill,
816 LASSERT(fid_is_sane(&reqbody->fid2));
819 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
821 PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
824 lname = mdt_name(info->mti_env, (char *)name, namelen);
825 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
826 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
830 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
832 rc = mdt_object_exists(parent);
833 if (unlikely(rc == 0)) {
834 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
835 &parent->mot_obj.mo_lu,
836 "Parent doesn't exist!\n");
838 } else if (!info->mti_cross_ref) {
839 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
840 PFID(mdt_object_fid(parent)));
843 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
851 if (info->mti_cross_ref) {
852 /* Only getattr on the child. Parent is on another node. */
853 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
855 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
856 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
859 /* Do not take lock for resent case. */
860 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
861 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
862 lhc->mlh_reg_lh.cookie);
863 LASSERT(fid_res_name_eq(mdt_object_fid(child),
864 &lock->l_resource->lr_name));
868 mdt_lock_handle_init(lhc);
869 mdt_lock_reg_init(lhc, LCK_PR);
872 * Object's name is on another MDS, no lookup lock is
873 * needed here but update is.
875 child_bits &= ~MDS_INODELOCK_LOOKUP;
876 child_bits |= MDS_INODELOCK_UPDATE;
878 rc = mdt_object_lock(info, child, lhc, child_bits,
882 /* Finally, we can get attr for child. */
883 mdt_set_capainfo(info, 0, mdt_object_fid(child),
885 rc = mdt_getattr_internal(info, child);
886 if (unlikely(rc != 0))
887 mdt_object_unlock(info, child, lhc, 1);
892 /* step 1: lock parent */
893 lhp = &info->mti_lh[MDT_LH_PARENT];
894 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
895 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
898 if (unlikely(rc != 0))
902 /* step 2: lookup child's fid by name */
903 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
908 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
909 GOTO(out_parent, rc);
911 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
913 *child_fid = reqbody->fid2;
914 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
918 *step 3: find the child object by fid & lock it.
919 * regardless if it is local or remote.
921 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
923 if (unlikely(IS_ERR(child)))
924 GOTO(out_parent, rc = PTR_ERR(child));
926 /* Do not take lock for resent case. */
927 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
928 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
929 lhc->mlh_reg_lh.cookie);
931 res_id = &lock->l_resource->lr_name;
932 if (!fid_res_name_eq(mdt_object_fid(child),
933 &lock->l_resource->lr_name)) {
934 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
935 &lock->l_resource->lr_name),
936 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
937 (unsigned long)res_id->name[0],
938 (unsigned long)res_id->name[1],
939 (unsigned long)res_id->name[2],
940 PFID(mdt_object_fid(parent)));
941 CWARN("Although resent, but still not get child lock"
942 "parent:"DFID" child:"DFID"\n",
943 PFID(mdt_object_fid(parent)),
944 PFID(mdt_object_fid(child)));
945 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
954 ma = &info->mti_attr;
956 mdt_lock_handle_init(lhc);
957 mdt_lock_reg_init(lhc, LCK_PR);
959 if (mdt_object_exists(child) == 0) {
960 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
961 &child->mot_obj.mo_lu,
962 "Object doesn't exist!\n");
963 GOTO(out_child, rc = -ESTALE);
967 ma->ma_need = MA_INODE;
968 rc = mo_attr_get(info->mti_env, next, ma);
969 if (unlikely(rc != 0))
972 /* If the file has not been changed for some time, we return
973 * not only a LOOKUP lock, but also an UPDATE lock and this
974 * might save us RPC on later STAT. For directories, it also
975 * let negative dentry starts working for this dir. */
976 if (ma->ma_valid & MA_INODE &&
977 ma->ma_attr.la_valid & LA_CTIME &&
978 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
979 ma->ma_attr.la_ctime < cfs_time_current_sec())
980 child_bits |= MDS_INODELOCK_UPDATE;
982 rc = mdt_object_lock(info, child, lhc, child_bits,
985 if (unlikely(rc != 0))
989 /* finally, we can get attr for child. */
990 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
991 rc = mdt_getattr_internal(info, child);
992 if (unlikely(rc != 0)) {
993 mdt_object_unlock(info, child, lhc, 1);
995 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
997 struct mdt_body *repbody;
999 /* Debugging code. */
1000 res_id = &lock->l_resource->lr_name;
1001 LDLM_DEBUG(lock, "Returning lock to client\n");
1002 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1003 &lock->l_resource->lr_name),
1004 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1005 (unsigned long)res_id->name[0],
1006 (unsigned long)res_id->name[1],
1007 (unsigned long)res_id->name[2],
1008 PFID(mdt_object_fid(child)));
1010 * Pack Size-on-MDS inode attributes to the body if
1011 * update lock is given.
1013 repbody = req_capsule_server_get(info->mti_pill,
1015 if (lock->l_policy_data.l_inodebits.bits &
1016 MDS_INODELOCK_UPDATE)
1017 mdt_pack_size2body(info, child);
1018 LDLM_LOCK_PUT(lock);
1023 mdt_object_put(info->mti_env, child);
1025 mdt_object_unlock(info, parent, lhp, 1);
1029 /* normal handler: should release the child lock */
1030 static int mdt_getattr_name(struct mdt_thread_info *info)
1032 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1033 struct mdt_body *reqbody;
1034 struct mdt_body *repbody;
1038 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1039 LASSERT(reqbody != NULL);
1040 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1041 LASSERT(repbody != NULL);
1043 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1044 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1045 repbody->eadatasize = 0;
1046 repbody->aclsize = 0;
1048 rc = mdt_init_ucred(info, reqbody);
1050 GOTO(out_shrink, rc);
1052 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1053 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1054 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1055 lhc->mlh_reg_lh.cookie = 0;
1057 mdt_exit_ucred(info);
1060 mdt_shrink_reply(info);
1064 static const struct lu_device_operations mdt_lu_ops;
1066 static int lu_device_is_mdt(struct lu_device *d)
1068 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1071 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1072 void *karg, void *uarg);
1074 static int mdt_set_info(struct mdt_thread_info *info)
1076 struct ptlrpc_request *req = mdt_info_req(info);
1079 int keylen, vallen, rc = 0;
1082 rc = req_capsule_server_pack(info->mti_pill);
1086 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1088 DEBUG_REQ(D_HA, req, "no set_info key");
1092 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1095 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1097 DEBUG_REQ(D_HA, req, "no set_info val");
1101 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1104 if (KEY_IS(KEY_READ_ONLY)) {
1106 lustre_msg_set_status(req->rq_repmsg, 0);
1108 spin_lock(&req->rq_export->exp_lock);
1110 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1112 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1113 spin_unlock(&req->rq_export->exp_lock);
1115 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1116 if (lustre_msg_swabbed(req->rq_reqmsg)) {
1117 struct changelog_setinfo *cs =
1118 (struct changelog_setinfo *)val;
1119 __swab64s(&cs->cs_recno);
1120 __swab32s(&cs->cs_id);
1123 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1125 lustre_msg_set_status(req->rq_repmsg, rc);
1133 static int mdt_connect(struct mdt_thread_info *info)
1136 struct ptlrpc_request *req;
1138 req = mdt_info_req(info);
1139 rc = target_handle_connect(req);
1141 LASSERT(req->rq_export != NULL);
1142 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1143 rc = mdt_init_sec_level(info);
1145 rc = mdt_init_idmap(info);
1147 obd_disconnect(class_export_get(req->rq_export));
1149 rc = err_serious(rc);
1154 static int mdt_disconnect(struct mdt_thread_info *info)
1159 rc = target_handle_disconnect(mdt_info_req(info));
1161 rc = err_serious(rc);
1165 static int mdt_sendpage(struct mdt_thread_info *info,
1166 struct lu_rdpg *rdpg)
1168 struct ptlrpc_request *req = mdt_info_req(info);
1169 struct ptlrpc_bulk_desc *desc;
1170 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1178 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1183 for (i = 0, tmpcount = rdpg->rp_count;
1184 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1185 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1186 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1189 LASSERT(desc->bd_nob == rdpg->rp_count);
1190 rc = sptlrpc_svc_wrap_bulk(req, desc);
1192 GOTO(free_desc, rc);
1194 rc = ptlrpc_start_bulk_transfer(desc);
1196 GOTO(free_desc, rc);
1198 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1199 GOTO(abort_bulk, rc = 0);
1201 timeout = (int) req->rq_deadline - cfs_time_current_sec();
1203 CERROR("Req deadline already passed %lu (now: %lu)\n",
1204 req->rq_deadline, cfs_time_current_sec());
1205 *lwi = LWI_TIMEOUT(cfs_time_seconds(max(timeout, 1)), NULL, NULL);
1206 rc = l_wait_event(desc->bd_waitq, !ptlrpc_server_bulk_active(desc), lwi);
1207 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1210 if (desc->bd_success &&
1211 desc->bd_nob_transferred == rdpg->rp_count)
1212 GOTO(free_desc, rc);
1214 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1217 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1218 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1219 desc->bd_nob_transferred, rdpg->rp_count,
1220 req->rq_export->exp_client_uuid.uuid,
1221 req->rq_export->exp_connection->c_remote_uuid.uuid);
1223 class_fail_export(req->rq_export);
1227 ptlrpc_abort_bulk(desc);
1229 ptlrpc_free_bulk(desc);
1233 #ifdef HAVE_SPLIT_SUPPORT
1235 * Retrieve dir entry from the page and insert it to the slave object, actually,
1236 * this should be in osd layer, but since it will not in the final product, so
1237 * just do it here and do not define more moo api anymore for this.
1239 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1242 struct mdt_object *object = info->mti_object;
1243 struct lu_fid *lf = &info->mti_tmp_fid2;
1244 struct md_attr *ma = &info->mti_attr;
1245 struct lu_dirpage *dp;
1246 struct lu_dirent *ent;
1247 int rc = 0, offset = 0;
1250 /* Make sure we have at least one entry. */
1255 * Disable trans for this name insert, since it will include many trans
1258 info->mti_no_need_trans = 1;
1260 * When write_dir_page, no need update parent's ctime,
1261 * and no permission check for name_insert.
1263 ma->ma_attr.la_ctime = 0;
1264 ma->ma_attr.la_valid = LA_MODE;
1265 ma->ma_valid = MA_INODE;
1268 dp = page_address(page);
1269 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1271 for (ent = lu_dirent_start(dp); ent != NULL;
1272 ent = lu_dirent_next(ent)) {
1273 struct lu_name *lname;
1276 if (le16_to_cpu(ent->lde_namelen) == 0)
1279 fid_le_to_cpu(lf, &ent->lde_fid);
1280 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1281 ma->ma_attr.la_mode = S_IFDIR;
1283 ma->ma_attr.la_mode = 0;
1284 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1286 GOTO(out, rc = -ENOMEM);
1288 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1289 lname = mdt_name(info->mti_env, name,
1290 le16_to_cpu(ent->lde_namelen));
1291 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1292 rc = mdo_name_insert(info->mti_env,
1293 md_object_next(&object->mot_obj),
1295 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1297 CERROR("Can't insert %*.*s, rc %d\n",
1298 le16_to_cpu(ent->lde_namelen),
1299 le16_to_cpu(ent->lde_namelen),
1304 offset += lu_dirent_size(ent);
1314 static int mdt_bulk_timeout(void *data)
1318 CERROR("mdt bulk transfer timeout \n");
1323 static int mdt_writepage(struct mdt_thread_info *info)
1325 struct ptlrpc_request *req = mdt_info_req(info);
1326 struct mdt_body *reqbody;
1327 struct l_wait_info *lwi;
1328 struct ptlrpc_bulk_desc *desc;
1334 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1335 if (reqbody == NULL)
1336 RETURN(err_serious(-EFAULT));
1338 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1340 RETURN(err_serious(-ENOMEM));
1342 /* allocate the page for the desc */
1343 page = cfs_alloc_page(CFS_ALLOC_STD);
1345 GOTO(desc_cleanup, rc = -ENOMEM);
1347 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1348 (int)reqbody->size, (int)reqbody->nlink);
1350 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1351 (int)reqbody->nlink);
1353 rc = sptlrpc_svc_prep_bulk(req, desc);
1355 GOTO(cleanup_page, rc);
1357 * Check if client was evicted while we were doing i/o before touching
1362 GOTO(cleanup_page, rc = -ENOMEM);
1364 if (desc->bd_export->exp_failed)
1367 rc = ptlrpc_start_bulk_transfer (desc);
1369 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1370 mdt_bulk_timeout, desc);
1371 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1372 desc->bd_export->exp_failed, lwi);
1373 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1374 if (rc == -ETIMEDOUT) {
1375 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1376 ptlrpc_abort_bulk(desc);
1377 } else if (desc->bd_export->exp_failed) {
1378 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1380 ptlrpc_abort_bulk(desc);
1381 } else if (!desc->bd_success ||
1382 desc->bd_nob_transferred != desc->bd_nob) {
1383 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1385 "truncated" : "network error on",
1386 desc->bd_nob_transferred, desc->bd_nob);
1387 /* XXX should this be a different errno? */
1391 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1394 GOTO(cleanup_lwi, rc);
1395 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1400 cfs_free_page(page);
1402 ptlrpc_free_bulk(desc);
1407 static int mdt_readpage(struct mdt_thread_info *info)
1409 struct mdt_object *object = info->mti_object;
1410 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1411 struct mdt_body *reqbody;
1412 struct mdt_body *repbody;
1417 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1418 RETURN(err_serious(-ENOMEM));
1420 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1421 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1422 if (reqbody == NULL || repbody == NULL)
1423 RETURN(err_serious(-EFAULT));
1426 * prepare @rdpg before calling lower layers and transfer itself. Here
1427 * reqbody->size contains offset of where to start to read and
1428 * reqbody->nlink contains number bytes to read.
1430 rdpg->rp_hash = reqbody->size;
1431 if (rdpg->rp_hash != reqbody->size) {
1432 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1433 rdpg->rp_hash, reqbody->size);
1437 rdpg->rp_attrs = reqbody->mode;
1438 rdpg->rp_count = reqbody->nlink;
1439 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1440 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1441 if (rdpg->rp_pages == NULL)
1444 for (i = 0; i < rdpg->rp_npages; ++i) {
1445 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1446 if (rdpg->rp_pages[i] == NULL)
1447 GOTO(free_rdpg, rc = -ENOMEM);
1450 /* call lower layers to fill allocated pages with directory data */
1451 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1453 GOTO(free_rdpg, rc);
1455 /* send pages to client */
1456 rc = mdt_sendpage(info, rdpg);
1461 for (i = 0; i < rdpg->rp_npages; i++)
1462 if (rdpg->rp_pages[i] != NULL)
1463 cfs_free_page(rdpg->rp_pages[i]);
1464 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1466 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1472 static int mdt_reint_internal(struct mdt_thread_info *info,
1473 struct mdt_lock_handle *lhc,
1476 struct req_capsule *pill = info->mti_pill;
1477 struct mdt_device *mdt = info->mti_mdt;
1478 struct mdt_body *repbody;
1483 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1484 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1485 mdt->mdt_max_mdsize);
1486 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1487 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1488 mdt->mdt_max_cookiesize);
1490 rc = req_capsule_server_pack(pill);
1492 CERROR("Can't pack response, rc %d\n", rc);
1493 RETURN(err_serious(rc));
1496 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1497 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1499 repbody->eadatasize = 0;
1500 repbody->aclsize = 0;
1503 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1504 GOTO(out_shrink, rc = err_serious(-EFAULT));
1506 rc = mdt_reint_unpack(info, op);
1508 CERROR("Can't unpack reint, rc %d\n", rc);
1509 GOTO(out_shrink, rc = err_serious(rc));
1512 /* for replay no cookkie / lmm need, because client have this already */
1513 if (info->mti_spec.no_create == 1) {
1514 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1515 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1517 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1518 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1522 rc = mdt_init_ucred_reint(info);
1524 GOTO(out_shrink, rc);
1526 rc = mdt_fix_attr_ucred(info, op);
1528 GOTO(out_ucred, rc = err_serious(rc));
1530 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1531 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1532 GOTO(out_ucred, rc);
1534 rc = mdt_reint_rec(info, lhc);
1537 mdt_exit_ucred(info);
1539 mdt_shrink_reply(info);
1543 static long mdt_reint_opcode(struct mdt_thread_info *info,
1544 const struct req_format **fmt)
1546 struct mdt_rec_reint *rec;
1549 opc = err_serious(-EFAULT);
1550 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1552 opc = rec->rr_opcode;
1553 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1554 if (opc < REINT_MAX && fmt[opc] != NULL)
1555 req_capsule_extend(info->mti_pill, fmt[opc]);
1557 CERROR("Unsupported opc: %ld\n", opc);
1558 opc = err_serious(opc);
1564 static int mdt_reint(struct mdt_thread_info *info)
1569 static const struct req_format *reint_fmts[REINT_MAX] = {
1570 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1571 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1572 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1573 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1574 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1575 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1576 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1581 opc = mdt_reint_opcode(info, reint_fmts);
1584 * No lock possible here from client to pass it to reint code
1587 rc = mdt_reint_internal(info, NULL, opc);
1592 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1596 /* this should sync the whole device */
1597 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1599 struct dt_device *dt = mdt->mdt_bottom;
1603 rc = dt->dd_ops->dt_sync(env, dt);
1607 /* this should sync this object */
1608 static int mdt_object_sync(struct mdt_thread_info *info)
1610 struct md_object *next;
1614 if (!mdt_object_exists(info->mti_object)) {
1615 CWARN("Non existing object "DFID"!\n",
1616 PFID(mdt_object_fid(info->mti_object)));
1619 next = mdt_object_child(info->mti_object);
1620 rc = mo_object_sync(info->mti_env, next);
1625 static int mdt_sync(struct mdt_thread_info *info)
1627 struct req_capsule *pill = info->mti_pill;
1628 struct mdt_body *body;
1632 /* The fid may be zero, so we req_capsule_set manually */
1633 req_capsule_set(pill, &RQF_MDS_SYNC);
1635 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1637 RETURN(err_serious(-EINVAL));
1639 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1640 RETURN(err_serious(-ENOMEM));
1642 if (fid_seq(&body->fid1) == 0) {
1643 /* sync the whole device */
1644 rc = req_capsule_server_pack(pill);
1646 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1648 rc = err_serious(rc);
1650 /* sync an object */
1651 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1653 rc = mdt_object_sync(info);
1655 struct md_object *next;
1656 const struct lu_fid *fid;
1657 struct lu_attr *la = &info->mti_attr.ma_attr;
1659 next = mdt_object_child(info->mti_object);
1660 info->mti_attr.ma_need = MA_INODE;
1661 info->mti_attr.ma_valid = 0;
1662 rc = mo_attr_get(info->mti_env, next,
1665 body = req_capsule_server_get(pill,
1667 fid = mdt_object_fid(info->mti_object);
1668 mdt_pack_attr2body(info, body, la, fid);
1672 rc = err_serious(rc);
1677 #ifdef HAVE_QUOTA_SUPPORT
1678 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1680 struct obd_quotactl *oqctl;
1681 struct req_capsule *pill = info->mti_pill;
1682 struct obd_export *exp = info->mti_exp;
1683 struct md_device *next = info->mti_mdt->mdt_child;
1687 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACHECK_NET))
1690 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1694 /* remote client has no permission for quotacheck */
1695 if (unlikely(exp_connect_rmtclient(exp)))
1698 rc = req_capsule_server_pack(pill);
1702 rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next, exp,
1707 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1709 struct obd_quotactl *oqctl, *repoqc;
1710 struct req_capsule *pill = info->mti_pill;
1711 struct obd_export *exp = info->mti_exp;
1712 struct md_device *next = info->mti_mdt->mdt_child;
1713 const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1717 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACTL_NET))
1720 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1725 if (exp_connect_rmtclient(exp)) {
1726 struct ptlrpc_request *req = mdt_info_req(info);
1727 struct mdt_export_data *med = mdt_req2med(req);
1728 struct lustre_idmap_table *idmap = med->med_idmap;
1730 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1731 oqctl->qc_cmd != Q_GETINFO))
1735 if (oqctl->qc_type == USRQUOTA)
1736 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1738 else if (oqctl->qc_type == GRPQUOTA)
1739 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1744 if (id == CFS_IDMAP_NOTFOUND) {
1745 CDEBUG(D_QUOTA, "no mapping for id %u\n",
1751 rc = req_capsule_server_pack(pill);
1755 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1756 LASSERT(repoqc != NULL);
1758 switch (oqctl->qc_cmd) {
1760 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1763 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1766 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1770 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1774 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1778 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1782 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1786 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1789 case LUSTRE_Q_INVALIDATE:
1790 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1792 case LUSTRE_Q_FINVALIDATE:
1793 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1796 CERROR("unsupported mdt_quotactl command: %d\n",
1808 * OBD PING and other handlers.
1810 static int mdt_obd_ping(struct mdt_thread_info *info)
1815 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1817 rc = target_handle_ping(mdt_info_req(info));
1819 rc = err_serious(rc);
1823 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1825 return err_serious(-EOPNOTSUPP);
1828 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1830 return err_serious(-EOPNOTSUPP);
1838 /** clone llog ctxt from child (mdd)
1839 * This allows remote llog (replicator) access.
1840 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1841 * context was originally set up, or we can handle them directly.
1842 * I choose the latter, but that means I need any llog
1843 * contexts set up by child to be accessable by the mdt. So we clone the
1844 * context into our context list here.
1846 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1849 struct md_device *next = mdt->mdt_child;
1850 struct llog_ctxt *ctxt;
1853 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1856 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1857 if (rc || ctxt == NULL) {
1858 CERROR("Can't get mdd ctxt %d\n", rc);
1862 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1864 CERROR("Can't set mdt ctxt %d\n", rc);
1869 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1870 struct mdt_device *mdt, int idx)
1872 struct llog_ctxt *ctxt;
1874 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1877 /* Put once for the get we just did, and once for the clone */
1878 llog_ctxt_put(ctxt);
1879 llog_ctxt_put(ctxt);
1883 static int mdt_llog_create(struct mdt_thread_info *info)
1887 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1888 rc = llog_origin_handle_create(mdt_info_req(info));
1889 return (rc < 0 ? err_serious(rc) : rc);
1892 static int mdt_llog_destroy(struct mdt_thread_info *info)
1896 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1897 rc = llog_origin_handle_destroy(mdt_info_req(info));
1898 return (rc < 0 ? err_serious(rc) : rc);
1901 static int mdt_llog_read_header(struct mdt_thread_info *info)
1905 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1906 rc = llog_origin_handle_read_header(mdt_info_req(info));
1907 return (rc < 0 ? err_serious(rc) : rc);
1910 static int mdt_llog_next_block(struct mdt_thread_info *info)
1914 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1915 rc = llog_origin_handle_next_block(mdt_info_req(info));
1916 return (rc < 0 ? err_serious(rc) : rc);
1919 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1923 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1924 rc = llog_origin_handle_prev_block(mdt_info_req(info));
1925 return (rc < 0 ? err_serious(rc) : rc);
1932 static struct ldlm_callback_suite cbs = {
1933 .lcs_completion = ldlm_server_completion_ast,
1934 .lcs_blocking = ldlm_server_blocking_ast,
1938 static int mdt_enqueue(struct mdt_thread_info *info)
1940 struct ptlrpc_request *req;
1945 * info->mti_dlm_req already contains swapped and (if necessary)
1946 * converted dlm request.
1948 LASSERT(info->mti_dlm_req != NULL);
1950 req = mdt_info_req(info);
1953 * Lock without inodebits makes no sense and will oops later in
1954 * ldlm. Let's check it now to see if we have wrong lock from client or
1955 * bits get corrupted somewhere in mdt_intent_policy().
1957 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1958 /* This is disabled because we need to support liblustre flock.
1959 * LASSERT(req_bits != 0);
1962 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1963 req, info->mti_dlm_req, &cbs);
1964 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1965 return rc ? err_serious(rc) : req->rq_status;
1968 static int mdt_convert(struct mdt_thread_info *info)
1971 struct ptlrpc_request *req;
1973 LASSERT(info->mti_dlm_req);
1974 req = mdt_info_req(info);
1975 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1976 return rc ? err_serious(rc) : req->rq_status;
1979 static int mdt_bl_callback(struct mdt_thread_info *info)
1981 CERROR("bl callbacks should not happen on MDS\n");
1983 return err_serious(-EOPNOTSUPP);
1986 static int mdt_cp_callback(struct mdt_thread_info *info)
1988 CERROR("cp callbacks should not happen on MDS\n");
1990 return err_serious(-EOPNOTSUPP);
1994 * sec context handlers
1996 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2000 rc = mdt_handle_idmap(info);
2003 struct ptlrpc_request *req = mdt_info_req(info);
2006 opc = lustre_msg_get_opc(req->rq_reqmsg);
2007 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2008 sptlrpc_svc_ctx_invalidate(req);
2011 OBD_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, obd_fail_val);
2016 static struct mdt_object *mdt_obj(struct lu_object *o)
2018 LASSERT(lu_device_is_mdt(o->lo_dev));
2019 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2022 struct mdt_object *mdt_object_find(const struct lu_env *env,
2023 struct mdt_device *d,
2024 const struct lu_fid *f)
2026 struct lu_object *o;
2027 struct mdt_object *m;
2030 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2031 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2032 if (unlikely(IS_ERR(o)))
2033 m = (struct mdt_object *)o;
2040 * Asyncronous commit for mdt device.
2042 * Pass asynchonous commit call down the MDS stack.
2044 * \param env environment
2045 * \param mdt the mdt device
2047 static void mdt_device_commit_async(const struct lu_env *env,
2048 struct mdt_device *mdt)
2050 struct dt_device *dt = mdt->mdt_bottom;
2053 rc = dt->dd_ops->dt_commit_async(env, dt);
2054 if (unlikely(rc != 0))
2055 CWARN("async commit start failed with rc = %d", rc);
2059 * Mark the lock as "synchonous".
2061 * Mark the lock to deffer transaction commit to the unlock time.
2063 * \param lock the lock to mark as "synchonous"
2065 * \see mdt_is_lock_sync
2066 * \see mdt_save_lock
2068 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2070 lock->l_ast_data = (void*)1;
2074 * Check whehter the lock "synchonous" or not.
2076 * \param lock the lock to check
2077 * \retval 1 the lock is "synchonous"
2078 * \retval 0 the lock isn't "synchronous"
2080 * \see mdt_set_lock_sync
2081 * \see mdt_save_lock
2083 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2085 return lock->l_ast_data != NULL;
2089 * Blocking AST for mdt locks.
2091 * Starts transaction commit if in case of COS lock conflict or
2092 * deffers such a commit to the mdt_save_lock.
2094 * \param lock the lock which blocks a request or cancelling lock
2095 * \param desc unused
2096 * \param data unused
2097 * \param flag indicates whether this cancelling or blocking callback
2099 * \see ldlm_blocking_ast_nocheck
2101 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2102 void *data, int flag)
2104 struct obd_device *obd = lock->l_resource->lr_namespace->ns_obd;
2105 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2109 if (flag == LDLM_CB_CANCELING)
2111 lock_res_and_lock(lock);
2112 if (lock->l_blocking_ast != mdt_blocking_ast) {
2113 unlock_res_and_lock(lock);
2116 if (mdt_cos_is_enabled(mdt) &&
2117 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2118 lock->l_blocking_lock != NULL &&
2119 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2120 mdt_set_lock_sync(lock);
2122 rc = ldlm_blocking_ast_nocheck(lock);
2124 /* There is no lock conflict if l_blocking_lock == NULL,
2125 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2126 * when the last reference to a local lock was released */
2127 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2130 rc = lu_env_init(&env, LCT_MD_THREAD);
2131 if (unlikely(rc != 0))
2132 CWARN("lu_env initialization failed with rc = %d,"
2133 "cannot start asynchronous commit\n", rc);
2135 mdt_device_commit_async(&env, mdt);
2141 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2142 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2144 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2145 ldlm_policy_data_t *policy = &info->mti_policy;
2146 struct ldlm_res_id *res_id = &info->mti_res_id;
2150 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2151 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2152 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2153 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2155 if (mdt_object_exists(o) < 0) {
2156 if (locality == MDT_CROSS_LOCK) {
2157 /* cross-ref object fix */
2158 ibits &= ~MDS_INODELOCK_UPDATE;
2159 ibits |= MDS_INODELOCK_LOOKUP;
2161 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2162 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2164 /* No PDO lock on remote object */
2165 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2168 if (lh->mlh_type == MDT_PDO_LOCK) {
2169 /* check for exists after object is locked */
2170 if (mdt_object_exists(o) == 0) {
2171 /* Non-existent object shouldn't have PDO lock */
2174 /* Non-dir object shouldn't have PDO lock */
2175 LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
2179 memset(policy, 0, sizeof(*policy));
2180 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2183 * Take PDO lock on whole directory and build correct @res_id for lock
2184 * on part of directory.
2186 if (lh->mlh_pdo_hash != 0) {
2187 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2188 mdt_lock_pdo_mode(info, o, lh);
2189 if (lh->mlh_pdo_mode != LCK_NL) {
2191 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2192 * is never going to be sent to client and we do not
2193 * want it slowed down due to possible cancels.
2195 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2196 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2197 policy, res_id, LDLM_FL_ATOMIC_CB,
2198 &info->mti_exp->exp_handle.h_cookie);
2204 * Finish res_id initializing by name hash marking part of
2205 * directory which is taking modification.
2207 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2210 policy->l_inodebits.bits = ibits;
2213 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2214 * going to be sent to client. If it is - mdt_intent_policy() path will
2215 * fix it up and turn FL_LOCAL flag off.
2217 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2218 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2219 &info->mti_exp->exp_handle.h_cookie);
2225 mdt_object_unlock(info, o, lh, 1);
2232 * Save a lock within request object.
2234 * Keep the lock referenced until whether client ACK or transaction
2235 * commit happens or release the lock immediately depending on input
2236 * parameters. If COS is ON, a write lock is converted to COS lock
2239 * \param info thead info object
2240 * \param h lock handle
2241 * \param mode lock mode
2242 * \param decref force immediate lock releasing
2245 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2246 ldlm_mode_t mode, int decref)
2250 if (lustre_handle_is_used(h)) {
2251 if (decref || !info->mti_has_trans ||
2252 !(mode & (LCK_PW | LCK_EX))){
2253 mdt_fid_unlock(h, mode);
2255 struct mdt_device *mdt = info->mti_mdt;
2256 struct ldlm_lock *lock = ldlm_handle2lock(h);
2257 struct ptlrpc_request *req = mdt_info_req(info);
2260 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2262 CDEBUG(D_HA, "request = %p reply state = %p"
2263 " transno = "LPD64"\n",
2264 req, req->rq_reply_state, req->rq_transno);
2265 if (mdt_cos_is_enabled(mdt)) {
2267 ldlm_lock_downgrade(lock, LCK_COS);
2270 ptlrpc_save_lock(req, h, mode, no_ack);
2271 if (mdt_is_lock_sync(lock)) {
2272 CDEBUG(D_HA, "found sync-lock,"
2273 " async commit started\n");
2274 mdt_device_commit_async(info->mti_env,
2277 LDLM_LOCK_PUT(lock);
2286 * Unlock mdt object.
2288 * Immeditely release the regular lock and the PDO lock or save the
2289 * lock in reqeuest and keep them referenced until client ACK or
2290 * transaction commit.
2292 * \param info thread info object
2293 * \param o mdt object
2294 * \param h mdt lock handle referencing regular and PDO locks
2295 * \param decref force immediate lock releasing
2297 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2298 struct mdt_lock_handle *lh, int decref)
2302 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2303 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2308 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2309 const struct lu_fid *f,
2310 struct mdt_lock_handle *lh,
2313 struct mdt_object *o;
2315 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2319 rc = mdt_object_lock(info, o, lh, ibits,
2322 mdt_object_put(info->mti_env, o);
2329 void mdt_object_unlock_put(struct mdt_thread_info * info,
2330 struct mdt_object * o,
2331 struct mdt_lock_handle *lh,
2334 mdt_object_unlock(info, o, lh, decref);
2335 mdt_object_put(info->mti_env, o);
2338 static struct mdt_handler *mdt_handler_find(__u32 opc,
2339 struct mdt_opc_slice *supported)
2341 struct mdt_opc_slice *s;
2342 struct mdt_handler *h;
2345 for (s = supported; s->mos_hs != NULL; s++) {
2346 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2347 h = s->mos_hs + (opc - s->mos_opc_start);
2348 if (likely(h->mh_opc != 0))
2349 LASSERTF(h->mh_opc == opc,
2350 "opcode mismatch %d != %d\n",
2353 h = NULL; /* unsupported opc */
2360 static int mdt_lock_resname_compat(struct mdt_device *m,
2361 struct ldlm_request *req)
2363 /* XXX something... later. */
2367 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2369 /* XXX something... later. */
2374 * Generic code handling requests that have struct mdt_body passed in:
2376 * - extract mdt_body from request and save it in @info, if present;
2378 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2381 * - if HABEO_CORPUS flag is set for this request type check whether object
2382 * actually exists on storage (lu_object_exists()).
2385 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2387 const struct mdt_body *body;
2388 struct mdt_object *obj;
2389 const struct lu_env *env;
2390 struct req_capsule *pill;
2394 env = info->mti_env;
2395 pill = info->mti_pill;
2397 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2401 if (!(body->valid & OBD_MD_FLID))
2404 if (!fid_is_sane(&body->fid1)) {
2405 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2410 * Do not get size or any capa fields before we check that request
2411 * contains capa actually. There are some requests which do not, for
2412 * instance MDS_IS_SUBDIR.
2414 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2415 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2416 mdt_set_capainfo(info, 0, &body->fid1,
2417 req_capsule_client_get(pill, &RMF_CAPA1));
2419 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2421 if ((flags & HABEO_CORPUS) &&
2422 !mdt_object_exists(obj)) {
2423 mdt_object_put(env, obj);
2424 /* for capability renew ENOENT will be handled in
2426 if (body->valid & OBD_MD_FLOSSCAPA)
2431 info->mti_object = obj;
2440 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2442 struct req_capsule *pill = info->mti_pill;
2446 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2447 rc = mdt_body_unpack(info, flags);
2451 if (rc == 0 && (flags & HABEO_REFERO)) {
2452 struct mdt_device *mdt = info->mti_mdt;
2456 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2457 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2458 mdt->mdt_max_mdsize);
2459 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2460 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2461 mdt->mdt_max_cookiesize);
2463 rc = req_capsule_server_pack(pill);
2468 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2470 struct md_device *next = m->mdt_child;
2472 return next->md_ops->mdo_init_capa_ctxt(env, next,
2473 m->mdt_opts.mo_mds_capa,
2474 m->mdt_capa_timeout,
2480 * Invoke handler for this request opc. Also do necessary preprocessing
2481 * (according to handler ->mh_flags), and post-processing (setting of
2482 * ->last_{xid,committed}).
2484 static int mdt_req_handle(struct mdt_thread_info *info,
2485 struct mdt_handler *h, struct ptlrpc_request *req)
2487 int rc, serious = 0;
2492 LASSERT(h->mh_act != NULL);
2493 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2494 LASSERT(current->journal_info == NULL);
2497 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2498 * to put same checks into handlers like mdt_close(), mdt_reint(),
2499 * etc., without talking to mdt authors first. Checking same thing
2500 * there again is useless and returning 0 error wihtout packing reply
2501 * is buggy! Handlers either pack reply or return error.
2503 * We return 0 here and do not send any reply in order to emulate
2504 * network failure. Do not send any reply in case any of NET related
2505 * fail_id has occured.
2507 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2511 flags = h->mh_flags;
2512 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2514 if (h->mh_fmt != NULL) {
2515 req_capsule_set(info->mti_pill, h->mh_fmt);
2516 rc = mdt_unpack_req_pack_rep(info, flags);
2519 if (rc == 0 && flags & MUTABOR &&
2520 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2521 /* should it be rq_status? */
2524 if (rc == 0 && flags & HABEO_CLAVIS) {
2525 struct ldlm_request *dlm_req;
2527 LASSERT(h->mh_fmt != NULL);
2529 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2530 if (dlm_req != NULL) {
2531 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2532 rc = mdt_lock_resname_compat(info->mti_mdt,
2534 info->mti_dlm_req = dlm_req;
2540 /* capability setting changed via /proc, needs reinitialize ctxt */
2541 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2542 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2543 info->mti_mdt->mdt_capa_conf = 0;
2546 if (likely(rc == 0)) {
2548 * Process request, there can be two types of rc:
2549 * 1) errors with msg unpack/pack, other failures outside the
2550 * operation itself. This is counted as serious errors;
2551 * 2) errors during fs operation, should be placed in rq_status
2554 rc = h->mh_act(info);
2556 !req->rq_no_reply && req->rq_reply_state == NULL) {
2557 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2558 "pack reply and returned 0 error\n",
2562 serious = is_serious(rc);
2563 rc = clear_serious(rc);
2567 req->rq_status = rc;
2570 * ELDLM_* codes which > 0 should be in rq_status only as well as
2571 * all non-serious errors.
2573 if (rc > 0 || !serious)
2576 LASSERT(current->journal_info == NULL);
2578 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2579 info->mti_mdt->mdt_opts.mo_compat_resname) {
2580 struct ldlm_reply *dlmrep;
2582 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2584 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2587 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2588 if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2589 target_committed_to_req(req);
2591 if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2592 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2593 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2597 target_send_reply(req, rc, info->mti_fail_id);
2601 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2603 lh->mlh_type = MDT_NUL_LOCK;
2604 lh->mlh_reg_lh.cookie = 0ull;
2605 lh->mlh_reg_mode = LCK_MINMODE;
2606 lh->mlh_pdo_lh.cookie = 0ull;
2607 lh->mlh_pdo_mode = LCK_MINMODE;
2610 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2612 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2613 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2617 * Initialize fields of struct mdt_thread_info. Other fields are left in
2618 * uninitialized state, because it's too expensive to zero out whole
2619 * mdt_thread_info (> 1K) on each request arrival.
2621 static void mdt_thread_info_init(struct ptlrpc_request *req,
2622 struct mdt_thread_info *info)
2625 struct md_capainfo *ci;
2627 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2628 info->mti_pill = &req->rq_pill;
2631 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2632 mdt_lock_handle_init(&info->mti_lh[i]);
2634 /* mdt device: it can be NULL while CONNECT */
2635 if (req->rq_export) {
2636 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2637 info->mti_exp = req->rq_export;
2639 info->mti_mdt = NULL;
2640 info->mti_env = req->rq_svc_thread->t_env;
2641 ci = md_capainfo(info->mti_env);
2642 memset(ci, 0, sizeof *ci);
2643 if (req->rq_export) {
2644 if (exp_connect_rmtclient(req->rq_export))
2645 ci->mc_auth = LC_ID_CONVERT;
2646 else if (req->rq_export->exp_connect_flags &
2647 OBD_CONNECT_MDS_CAPA)
2648 ci->mc_auth = LC_ID_PLAIN;
2650 ci->mc_auth = LC_ID_NONE;
2653 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2654 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2655 info->mti_mos[0] = NULL;
2656 info->mti_mos[1] = NULL;
2657 info->mti_mos[2] = NULL;
2658 info->mti_mos[3] = NULL;
2660 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2661 info->mti_body = NULL;
2662 info->mti_object = NULL;
2663 info->mti_dlm_req = NULL;
2664 info->mti_has_trans = 0;
2665 info->mti_no_need_trans = 0;
2666 info->mti_cross_ref = 0;
2667 info->mti_opdata = 0;
2669 /* To not check for split by default. */
2670 info->mti_spec.sp_ck_split = 0;
2671 info->mti_spec.no_create = 0;
2674 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2678 req_capsule_fini(info->mti_pill);
2679 if (info->mti_object != NULL) {
2680 mdt_object_put(info->mti_env, info->mti_object);
2681 info->mti_object = NULL;
2683 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2684 mdt_lock_handle_fini(&info->mti_lh[i]);
2685 info->mti_env = NULL;
2688 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2689 struct obd_device *obd, int *process)
2691 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2692 case MDS_CONNECT: /* This will never get here, but for completeness. */
2693 case OST_CONNECT: /* This will never get here, but for completeness. */
2694 case MDS_DISCONNECT:
2695 case OST_DISCONNECT:
2700 case MDS_DONE_WRITING:
2701 case MDS_SYNC: /* used in unmounting */
2707 *process = target_queue_recovery_request(req, obd);
2711 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2718 * Handle recovery. Return:
2719 * +1: continue request processing;
2720 * -ve: abort immediately with the given error code;
2721 * 0: send reply with error code in req->rq_status;
2723 static int mdt_recovery(struct mdt_thread_info *info)
2725 struct ptlrpc_request *req = mdt_info_req(info);
2727 struct obd_device *obd;
2731 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2734 case SEC_CTX_INIT_CONT:
2740 rc = mdt_handle_idmap(info);
2749 if (unlikely(req->rq_export == NULL)) {
2750 CERROR("operation %d on unconnected MDS from %s\n",
2751 lustre_msg_get_opc(req->rq_reqmsg),
2752 libcfs_id2str(req->rq_peer));
2753 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2754 * mds_A will get -ENOTCONN(especially for ping req),
2755 * which will cause that mds_A deactive timeout, then when
2756 * mds_A cleanup, the cleanup process will be suspended since
2757 * deactive timeout is not zero.
2759 req->rq_status = -ENOTCONN;
2760 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2764 /* sanity check: if the xid matches, the request must be marked as a
2765 * resent or replayed */
2766 if (req_xid_is_last(req)) {
2767 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2768 (MSG_RESENT | MSG_REPLAY))) {
2769 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2770 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2771 lustre_msg_get_flags(req->rq_reqmsg));
2773 req->rq_status = -ENOTCONN;
2778 /* else: note the opposite is not always true; a RESENT req after a
2779 * failover will usually not match the last_xid, since it was likely
2780 * never committed. A REPLAYed request will almost never match the
2781 * last xid, however it could for a committed, but still retained,
2784 obd = req->rq_export->exp_obd;
2786 /* Check for aborted recovery... */
2787 spin_lock_bh(&obd->obd_processing_task_lock);
2788 recovering = obd->obd_recovering;
2789 spin_unlock_bh(&obd->obd_processing_task_lock);
2790 if (unlikely(recovering)) {
2793 DEBUG_REQ(D_INFO, req, "Got new replay");
2794 rc = mdt_filter_recovery_request(req, obd, &should_process);
2795 if (rc != 0 || !should_process)
2797 else if (should_process < 0) {
2798 req->rq_status = should_process;
2799 rc = ptlrpc_error(req);
2806 static int mdt_msg_check_version(struct lustre_msg *msg)
2810 switch (lustre_msg_get_opc(msg)) {
2812 case MDS_DISCONNECT:
2815 case SEC_CTX_INIT_CONT:
2817 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2819 CERROR("bad opc %u version %08x, expecting %08x\n",
2820 lustre_msg_get_opc(msg),
2821 lustre_msg_get_version(msg),
2822 LUSTRE_OBD_VERSION);
2826 case MDS_GETATTR_NAME:
2833 case MDS_DONE_WRITING:
2839 case MDS_QUOTACHECK:
2845 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2847 CERROR("bad opc %u version %08x, expecting %08x\n",
2848 lustre_msg_get_opc(msg),
2849 lustre_msg_get_version(msg),
2850 LUSTRE_MDS_VERSION);
2854 case LDLM_BL_CALLBACK:
2855 case LDLM_CP_CALLBACK:
2856 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2858 CERROR("bad opc %u version %08x, expecting %08x\n",
2859 lustre_msg_get_opc(msg),
2860 lustre_msg_get_version(msg),
2861 LUSTRE_DLM_VERSION);
2863 case OBD_LOG_CANCEL:
2864 case LLOG_ORIGIN_HANDLE_CREATE:
2865 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2866 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2867 case LLOG_ORIGIN_HANDLE_CLOSE:
2868 case LLOG_ORIGIN_HANDLE_DESTROY:
2869 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2871 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2873 CERROR("bad opc %u version %08x, expecting %08x\n",
2874 lustre_msg_get_opc(msg),
2875 lustre_msg_get_version(msg),
2876 LUSTRE_LOG_VERSION);
2879 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2885 static int mdt_handle0(struct ptlrpc_request *req,
2886 struct mdt_thread_info *info,
2887 struct mdt_opc_slice *supported)
2889 struct mdt_handler *h;
2890 struct lustre_msg *msg;
2895 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2898 LASSERT(current->journal_info == NULL);
2900 msg = req->rq_reqmsg;
2901 rc = mdt_msg_check_version(msg);
2902 if (likely(rc == 0)) {
2903 rc = mdt_recovery(info);
2904 if (likely(rc == +1)) {
2905 switch (lustre_msg_get_opc(msg)) {
2907 req->rq_bulk_read = 1;
2910 req->rq_bulk_write = 1;
2914 h = mdt_handler_find(lustre_msg_get_opc(msg),
2916 if (likely(h != NULL)) {
2917 rc = mdt_req_handle(info, h, req);
2919 CERROR("The unsupported opc: 0x%x\n",
2920 lustre_msg_get_opc(msg) );
2921 req->rq_status = -ENOTSUPP;
2922 rc = ptlrpc_error(req);
2927 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2932 * MDT handler function called by ptlrpc service thread when request comes.
2934 * XXX common "target" functionality should be factored into separate module
2935 * shared by mdt, ost and stand-alone services like fld.
2937 static int mdt_handle_common(struct ptlrpc_request *req,
2938 struct mdt_opc_slice *supported)
2941 struct mdt_thread_info *info;
2945 env = req->rq_svc_thread->t_env;
2946 LASSERT(env != NULL);
2947 LASSERT(env->le_ses != NULL);
2948 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2949 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2950 LASSERT(info != NULL);
2952 mdt_thread_info_init(req, info);
2954 rc = mdt_handle0(req, info, supported);
2956 mdt_thread_info_fini(info);
2961 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2964 int mdt_recovery_handle(struct ptlrpc_request *req)
2969 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2971 rc = mdt_handle_common(req, mdt_fld_handlers);
2974 rc = mdt_handle_common(req, mdt_seq_handlers);
2977 rc = mdt_handle_common(req, mdt_regular_handlers);
2984 static int mdt_regular_handle(struct ptlrpc_request *req)
2986 return mdt_handle_common(req, mdt_regular_handlers);
2989 static int mdt_readpage_handle(struct ptlrpc_request *req)
2991 return mdt_handle_common(req, mdt_readpage_handlers);
2994 static int mdt_xmds_handle(struct ptlrpc_request *req)
2996 return mdt_handle_common(req, mdt_xmds_handlers);
2999 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3001 return mdt_handle_common(req, mdt_seq_handlers);
3004 static int mdt_mdss_handle(struct ptlrpc_request *req)
3006 return mdt_handle_common(req, mdt_seq_handlers);
3009 static int mdt_dtss_handle(struct ptlrpc_request *req)
3011 return mdt_handle_common(req, mdt_seq_handlers);
3014 static int mdt_fld_handle(struct ptlrpc_request *req)
3016 return mdt_handle_common(req, mdt_fld_handlers);
3032 static int mdt_intent_getattr(enum mdt_it_code opcode,
3033 struct mdt_thread_info *info,
3034 struct ldlm_lock **,
3036 static int mdt_intent_reint(enum mdt_it_code opcode,
3037 struct mdt_thread_info *info,
3038 struct ldlm_lock **,
3041 static struct mdt_it_flavor {
3042 const struct req_format *it_fmt;
3044 int (*it_act)(enum mdt_it_code ,
3045 struct mdt_thread_info *,
3046 struct ldlm_lock **,
3049 } mdt_it_flavor[] = {
3051 .it_fmt = &RQF_LDLM_INTENT,
3052 /*.it_flags = HABEO_REFERO,*/
3054 .it_act = mdt_intent_reint,
3055 .it_reint = REINT_OPEN
3058 .it_fmt = &RQF_LDLM_INTENT,
3059 .it_flags = MUTABOR,
3060 .it_act = mdt_intent_reint,
3061 .it_reint = REINT_OPEN
3064 .it_fmt = &RQF_LDLM_INTENT,
3065 .it_flags = MUTABOR,
3066 .it_act = mdt_intent_reint,
3067 .it_reint = REINT_CREATE
3069 [MDT_IT_GETATTR] = {
3070 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3071 .it_flags = HABEO_REFERO,
3072 .it_act = mdt_intent_getattr
3074 [MDT_IT_READDIR] = {
3080 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3081 .it_flags = HABEO_REFERO,
3082 .it_act = mdt_intent_getattr
3085 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3086 .it_flags = MUTABOR,
3088 .it_reint = REINT_UNLINK
3092 .it_flags = MUTABOR,
3095 [MDT_IT_GETXATTR] = {
3102 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3103 struct ldlm_lock **lockp,
3104 struct ldlm_lock *new_lock,
3105 struct mdt_lock_handle *lh,
3108 struct ptlrpc_request *req = mdt_info_req(info);
3109 struct ldlm_lock *lock = *lockp;
3112 * Get new lock only for cases when possible resent did not find any
3115 if (new_lock == NULL)
3116 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3118 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3119 lh->mlh_reg_lh.cookie = 0;
3123 LASSERTF(new_lock != NULL,
3124 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3127 * If we've already given this lock to a client once, then we should
3128 * have no readers or writers. Otherwise, we should have one reader
3129 * _or_ writer ref (which will be zeroed below) before returning the
3132 if (new_lock->l_export == req->rq_export) {
3133 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3135 LASSERT(new_lock->l_export == NULL);
3136 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3141 if (new_lock->l_export == req->rq_export) {
3143 * Already gave this to the client, which means that we
3144 * reconstructed a reply.
3146 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3148 lh->mlh_reg_lh.cookie = 0;
3149 RETURN(ELDLM_LOCK_REPLACED);
3153 * Fixup the lock to be given to the client.
3155 lock_res_and_lock(new_lock);
3156 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3157 * possible blocking AST. */
3158 while (new_lock->l_readers > 0) {
3159 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3160 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3161 new_lock->l_readers--;
3163 while (new_lock->l_writers > 0) {
3164 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3165 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3166 new_lock->l_writers--;
3169 new_lock->l_export = class_export_get(req->rq_export);
3170 new_lock->l_blocking_ast = lock->l_blocking_ast;
3171 new_lock->l_completion_ast = lock->l_completion_ast;
3172 new_lock->l_remote_handle = lock->l_remote_handle;
3173 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3175 unlock_res_and_lock(new_lock);
3177 lustre_hash_add(new_lock->l_export->exp_lock_hash,
3178 &new_lock->l_remote_handle,
3179 &new_lock->l_exp_hash);
3181 LDLM_LOCK_RELEASE(new_lock);
3182 lh->mlh_reg_lh.cookie = 0;
3184 RETURN(ELDLM_LOCK_REPLACED);
3187 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3188 struct ldlm_lock *new_lock,
3189 struct ldlm_lock **old_lock,
3190 struct mdt_lock_handle *lh)
3192 struct ptlrpc_request *req = mdt_info_req(info);
3193 struct obd_export *exp = req->rq_export;
3194 struct lustre_handle remote_hdl;
3195 struct ldlm_request *dlmreq;
3196 struct ldlm_lock *lock;
3198 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3201 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3202 remote_hdl = dlmreq->lock_handle[0];
3204 lock = lustre_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3206 if (lock != new_lock) {
3207 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3208 lh->mlh_reg_mode = lock->l_granted_mode;
3210 LDLM_DEBUG(lock, "Restoring lock cookie");
3211 DEBUG_REQ(D_DLMTRACE, req,
3212 "restoring lock cookie "LPX64,
3213 lh->mlh_reg_lh.cookie);
3215 *old_lock = LDLM_LOCK_GET(lock);
3216 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3220 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3224 * If the xid matches, then we know this is a resent request, and allow
3225 * it. (It's probably an OPEN, for which we don't send a lock.
3227 if (req_xid_is_last(req))
3231 * This remote handle isn't enqueued, so we never received or processed
3232 * this request. Clear MSG_RESENT, because it can be handled like any
3233 * normal request now.
3235 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3237 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3241 static int mdt_intent_getattr(enum mdt_it_code opcode,
3242 struct mdt_thread_info *info,
3243 struct ldlm_lock **lockp,
3246 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3247 struct ldlm_lock *new_lock = NULL;
3249 struct ldlm_reply *ldlm_rep;
3250 struct ptlrpc_request *req;
3251 struct mdt_body *reqbody;
3252 struct mdt_body *repbody;
3256 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3259 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3262 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3263 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3264 repbody->eadatasize = 0;
3265 repbody->aclsize = 0;
3269 child_bits = MDS_INODELOCK_LOOKUP;
3271 case MDT_IT_GETATTR:
3272 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3275 CERROR("Unhandled till now");
3276 GOTO(out_shrink, rc = -EINVAL);
3279 rc = mdt_init_ucred(info, reqbody);
3281 GOTO(out_shrink, rc);
3283 req = info->mti_pill->rc_req;
3284 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3285 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3287 /* Get lock from request for possible resent case. */
3288 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3290 ldlm_rep->lock_policy_res2 =
3291 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3293 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3294 ldlm_rep->lock_policy_res2 = 0;
3295 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3296 ldlm_rep->lock_policy_res2) {
3297 lhc->mlh_reg_lh.cookie = 0ull;
3298 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3301 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3304 mdt_exit_ucred(info);
3306 mdt_shrink_reply(info);
3310 static int mdt_intent_reint(enum mdt_it_code opcode,
3311 struct mdt_thread_info *info,
3312 struct ldlm_lock **lockp,
3315 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3316 struct ldlm_reply *rep = NULL;
3320 static const struct req_format *intent_fmts[REINT_MAX] = {
3321 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3322 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3327 opc = mdt_reint_opcode(info, intent_fmts);
3331 if (mdt_it_flavor[opcode].it_reint != opc) {
3332 CERROR("Reint code %ld doesn't match intent: %d\n",
3334 RETURN(err_serious(-EPROTO));
3337 /* Get lock from request for possible resent case. */
3338 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3340 rc = mdt_reint_internal(info, lhc, opc);
3342 /* Check whether the reply has been packed successfully. */
3343 if (mdt_info_req(info)->rq_repmsg != NULL)
3344 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3346 RETURN(err_serious(-EFAULT));
3348 /* MDC expects this in any case */
3350 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3352 /* Cross-ref case, the lock should be returned to the client */
3353 if (rc == -EREMOTE) {
3354 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3355 rep->lock_policy_res2 = 0;
3356 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3359 rep->lock_policy_res2 = clear_serious(rc);
3361 lhc->mlh_reg_lh.cookie = 0ull;
3362 if (rc == -ENOTCONN || rc == -ENODEV ||
3363 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3365 * If it is the disconnect error (ENODEV & ENOCONN), the error
3366 * will be returned by rq_status, and client at ptlrpc layer
3367 * will detect this, then disconnect, reconnect the import
3368 * immediately, instead of impacting the following the rpc.
3373 * For other cases, the error will be returned by intent.
3374 * and client will retrieve the result from intent.
3377 * FIXME: when open lock is finished, that should be
3380 RETURN(ELDLM_LOCK_ABORTED);
3384 static int mdt_intent_code(long itcode)
3392 case IT_OPEN|IT_CREAT:
3399 rc = MDT_IT_READDIR;
3402 rc = MDT_IT_GETATTR;
3414 rc = MDT_IT_GETXATTR;
3417 CERROR("Unknown intent opcode: %ld\n", itcode);
3424 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3425 struct ldlm_lock **lockp, int flags)
3427 struct req_capsule *pill;
3428 struct mdt_it_flavor *flv;
3433 opc = mdt_intent_code(itopc);
3437 pill = info->mti_pill;
3438 flv = &mdt_it_flavor[opc];
3440 if (flv->it_fmt != NULL)
3441 req_capsule_extend(pill, flv->it_fmt);
3443 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3445 struct ptlrpc_request *req = mdt_info_req(info);
3446 if (flv->it_flags & MUTABOR &&
3447 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3450 if (rc == 0 && flv->it_act != NULL) {
3451 /* execute policy */
3452 rc = flv->it_act(opc, info, lockp, flags);
3459 static int mdt_intent_policy(struct ldlm_namespace *ns,
3460 struct ldlm_lock **lockp, void *req_cookie,
3461 ldlm_mode_t mode, int flags, void *data)
3463 struct mdt_thread_info *info;
3464 struct ptlrpc_request *req = req_cookie;
3465 struct ldlm_intent *it;
3466 struct req_capsule *pill;
3471 LASSERT(req != NULL);
3473 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3475 LASSERT(info != NULL);
3476 pill = info->mti_pill;
3477 LASSERT(pill->rc_req == req);
3479 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3480 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3481 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3483 const struct ldlm_request *dlmreq;
3486 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3491 * Lock without inodebits makes no sense and will oops
3492 * later in ldlm. Let's check it now to see if we have
3493 * wrong lock from client or bits get corrupted
3494 * somewhere in mdt_intent_opc().
3496 dlmreq = info->mti_dlm_req;
3497 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
3498 LASSERT(req_bits != 0);
3501 rc = err_serious(-EFAULT);
3503 /* No intent was provided */
3504 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3505 rc = req_capsule_server_pack(pill);
3507 rc = err_serious(rc);
3515 static void mdt_seq_adjust(const struct lu_env *env,
3516 struct mdt_device *m, int lost)
3518 struct md_site *ms = mdt_md_site(m);
3519 struct lu_seq_range out;
3522 LASSERT(ms && ms->ms_server_seq);
3524 /* get extra seq from seq_server, moving it's range up */
3525 while (lost-- > 0) {
3526 seq_server_alloc_meta(ms->ms_server_seq, NULL, &out, env);
3531 static int mdt_seq_fini(const struct lu_env *env,
3532 struct mdt_device *m)
3534 struct md_site *ms = mdt_md_site(m);
3538 if (ms->ms_server_seq) {
3539 seq_server_fini(ms->ms_server_seq, env);
3540 OBD_FREE_PTR(ms->ms_server_seq);
3541 ms->ms_server_seq = NULL;
3544 if (ms->ms_control_seq) {
3545 seq_server_fini(ms->ms_control_seq, env);
3546 OBD_FREE_PTR(ms->ms_control_seq);
3547 ms->ms_control_seq = NULL;
3550 if (ms->ms_client_seq) {
3551 seq_client_fini(ms->ms_client_seq);
3552 OBD_FREE_PTR(ms->ms_client_seq);
3553 ms->ms_client_seq = NULL;
3560 static int mdt_seq_init(const struct lu_env *env,
3562 struct mdt_device *m)
3569 ms = mdt_md_site(m);
3572 * This is sequence-controller node. Init seq-controller server on local
3575 if (ms->ms_node_id == 0) {
3576 LASSERT(ms->ms_control_seq == NULL);
3578 OBD_ALLOC_PTR(ms->ms_control_seq);
3579 if (ms->ms_control_seq == NULL)
3582 rc = seq_server_init(ms->ms_control_seq,
3583 m->mdt_bottom, uuid,
3584 LUSTRE_SEQ_CONTROLLER,
3589 GOTO(out_seq_fini, rc);
3591 OBD_ALLOC_PTR(ms->ms_client_seq);
3592 if (ms->ms_client_seq == NULL)
3593 GOTO(out_seq_fini, rc = -ENOMEM);
3595 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3596 if (prefix == NULL) {
3597 OBD_FREE_PTR(ms->ms_client_seq);
3598 GOTO(out_seq_fini, rc = -ENOMEM);
3601 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3605 * Init seq-controller client after seq-controller server is
3606 * ready. Pass ms->ms_control_seq to it for direct talking.
3608 rc = seq_client_init(ms->ms_client_seq, NULL,
3609 LUSTRE_SEQ_METADATA, prefix,
3610 ms->ms_control_seq);
3611 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3614 GOTO(out_seq_fini, rc);
3617 /* Init seq-server on local MDT */
3618 LASSERT(ms->ms_server_seq == NULL);
3620 OBD_ALLOC_PTR(ms->ms_server_seq);
3621 if (ms->ms_server_seq == NULL)
3622 GOTO(out_seq_fini, rc = -ENOMEM);
3624 rc = seq_server_init(ms->ms_server_seq,
3625 m->mdt_bottom, uuid,
3630 GOTO(out_seq_fini, rc = -ENOMEM);
3632 /* Assign seq-controller client to local seq-server. */
3633 if (ms->ms_node_id == 0) {
3634 LASSERT(ms->ms_client_seq != NULL);
3636 rc = seq_server_set_cli(ms->ms_server_seq,
3644 mdt_seq_fini(env, m);
3649 * Init client sequence manager which is used by local MDS to talk to sequence
3650 * controller on remote node.
3652 static int mdt_seq_init_cli(const struct lu_env *env,
3653 struct mdt_device *m,
3654 struct lustre_cfg *cfg)
3656 struct md_site *ms = mdt_md_site(m);
3657 struct obd_device *mdc;
3658 struct obd_uuid *uuidp, *mdcuuidp;
3659 char *uuid_str, *mdc_uuid_str;
3662 struct mdt_thread_info *info;
3663 char *p, *index_string = lustre_cfg_string(cfg, 2);
3666 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3667 uuidp = &info->mti_u.uuid[0];
3668 mdcuuidp = &info->mti_u.uuid[1];
3670 LASSERT(index_string);
3672 index = simple_strtol(index_string, &p, 10);
3674 CERROR("Invalid index in lustre_cgf, offset 2\n");
3678 /* check if this is adding the first MDC and controller is not yet
3680 if (index != 0 || ms->ms_client_seq)
3683 uuid_str = lustre_cfg_string(cfg, 1);
3684 mdc_uuid_str = lustre_cfg_string(cfg, 4);
3685 obd_str2uuid(uuidp, uuid_str);
3686 obd_str2uuid(mdcuuidp, mdc_uuid_str);
3688 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3690 CERROR("can't find controller MDC by uuid %s\n",
3693 } else if (!mdc->obd_set_up) {
3694 CERROR("target %s not set up\n", mdc->obd_name);
3697 LASSERT(ms->ms_control_exp);
3698 OBD_ALLOC_PTR(ms->ms_client_seq);
3699 if (ms->ms_client_seq != NULL) {
3702 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3706 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3709 rc = seq_client_init(ms->ms_client_seq,
3711 LUSTRE_SEQ_METADATA,
3713 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3720 LASSERT(ms->ms_server_seq != NULL);
3721 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
3728 static void mdt_seq_fini_cli(struct mdt_device *m)
3734 ms = mdt_md_site(m);
3737 if (ms->ms_server_seq)
3738 seq_server_set_cli(ms->ms_server_seq,
3741 if (ms->ms_control_exp) {
3742 class_export_put(ms->ms_control_exp);
3743 ms->ms_control_exp = NULL;
3752 static int mdt_fld_fini(const struct lu_env *env,
3753 struct mdt_device *m)
3755 struct md_site *ms = mdt_md_site(m);
3758 if (ms && ms->ms_server_fld) {
3759 fld_server_fini(ms->ms_server_fld, env);
3760 OBD_FREE_PTR(ms->ms_server_fld);
3761 ms->ms_server_fld = NULL;
3767 static int mdt_fld_init(const struct lu_env *env,
3769 struct mdt_device *m)
3775 ms = mdt_md_site(m);
3777 OBD_ALLOC_PTR(ms->ms_server_fld);
3778 if (ms->ms_server_fld == NULL)
3779 RETURN(rc = -ENOMEM);
3781 rc = fld_server_init(ms->ms_server_fld,
3782 m->mdt_bottom, uuid,
3783 env, ms->ms_node_id);
3785 OBD_FREE_PTR(ms->ms_server_fld);
3786 ms->ms_server_fld = NULL;
3793 /* device init/fini methods */
3794 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3797 if (m->mdt_regular_service != NULL) {
3798 ptlrpc_unregister_service(m->mdt_regular_service);
3799 m->mdt_regular_service = NULL;
3801 if (m->mdt_readpage_service != NULL) {
3802 ptlrpc_unregister_service(m->mdt_readpage_service);
3803 m->mdt_readpage_service = NULL;
3805 if (m->mdt_xmds_service != NULL) {
3806 ptlrpc_unregister_service(m->mdt_xmds_service);
3807 m->mdt_xmds_service = NULL;
3809 if (m->mdt_setattr_service != NULL) {
3810 ptlrpc_unregister_service(m->mdt_setattr_service);
3811 m->mdt_setattr_service = NULL;
3813 if (m->mdt_mdsc_service != NULL) {
3814 ptlrpc_unregister_service(m->mdt_mdsc_service);
3815 m->mdt_mdsc_service = NULL;
3817 if (m->mdt_mdss_service != NULL) {
3818 ptlrpc_unregister_service(m->mdt_mdss_service);
3819 m->mdt_mdss_service = NULL;
3821 if (m->mdt_dtss_service != NULL) {
3822 ptlrpc_unregister_service(m->mdt_dtss_service);
3823 m->mdt_dtss_service = NULL;
3825 if (m->mdt_fld_service != NULL) {
3826 ptlrpc_unregister_service(m->mdt_fld_service);
3827 m->mdt_fld_service = NULL;
3832 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3835 static struct ptlrpc_service_conf conf;
3836 cfs_proc_dir_entry_t *procfs_entry;
3839 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3841 conf = (typeof(conf)) {
3842 .psc_nbufs = MDS_NBUFS,
3843 .psc_bufsize = MDS_BUFSIZE,
3844 .psc_max_req_size = MDS_MAXREQSIZE,
3845 .psc_max_reply_size = MDS_MAXREPSIZE,
3846 .psc_req_portal = MDS_REQUEST_PORTAL,
3847 .psc_rep_portal = MDC_REPLY_PORTAL,
3848 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3850 * We'd like to have a mechanism to set this on a per-device
3851 * basis, but alas...
3853 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3855 .psc_max_threads = MDT_MAX_THREADS,
3856 .psc_ctx_tags = LCT_MD_THREAD
3859 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3860 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3861 "mdt_ldlm_client", m->mdt_ldlm_client);
3863 m->mdt_regular_service =
3864 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3865 procfs_entry, target_print_req,
3867 if (m->mdt_regular_service == NULL)
3870 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3872 GOTO(err_mdt_svc, rc);
3875 * readpage service configuration. Parameters have to be adjusted,
3878 conf = (typeof(conf)) {
3879 .psc_nbufs = MDS_NBUFS,
3880 .psc_bufsize = MDS_BUFSIZE,
3881 .psc_max_req_size = MDS_MAXREQSIZE,
3882 .psc_max_reply_size = MDS_MAXREPSIZE,
3883 .psc_req_portal = MDS_READPAGE_PORTAL,
3884 .psc_rep_portal = MDC_REPLY_PORTAL,
3885 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3886 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3888 .psc_max_threads = MDT_MAX_THREADS,
3889 .psc_ctx_tags = LCT_MD_THREAD
3891 m->mdt_readpage_service =
3892 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3893 LUSTRE_MDT_NAME "_readpage",
3894 procfs_entry, target_print_req,"mdt_rdpg");
3896 if (m->mdt_readpage_service == NULL) {
3897 CERROR("failed to start readpage service\n");
3898 GOTO(err_mdt_svc, rc = -ENOMEM);
3901 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3904 * setattr service configuration.
3906 conf = (typeof(conf)) {
3907 .psc_nbufs = MDS_NBUFS,
3908 .psc_bufsize = MDS_BUFSIZE,
3909 .psc_max_req_size = MDS_MAXREQSIZE,
3910 .psc_max_reply_size = MDS_MAXREPSIZE,
3911 .psc_req_portal = MDS_SETATTR_PORTAL,
3912 .psc_rep_portal = MDC_REPLY_PORTAL,
3913 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3914 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3916 .psc_max_threads = MDT_MAX_THREADS,
3917 .psc_ctx_tags = LCT_MD_THREAD
3920 m->mdt_setattr_service =
3921 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3922 LUSTRE_MDT_NAME "_setattr",
3923 procfs_entry, target_print_req,"mdt_attr");
3925 if (!m->mdt_setattr_service) {
3926 CERROR("failed to start setattr service\n");
3927 GOTO(err_mdt_svc, rc = -ENOMEM);
3930 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3932 GOTO(err_mdt_svc, rc);
3935 * sequence controller service configuration
3937 conf = (typeof(conf)) {
3938 .psc_nbufs = MDS_NBUFS,
3939 .psc_bufsize = MDS_BUFSIZE,
3940 .psc_max_req_size = SEQ_MAXREQSIZE,
3941 .psc_max_reply_size = SEQ_MAXREPSIZE,
3942 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3943 .psc_rep_portal = MDC_REPLY_PORTAL,
3944 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3945 .psc_min_threads = SEQ_NUM_THREADS,
3946 .psc_max_threads = SEQ_NUM_THREADS,
3947 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3950 m->mdt_mdsc_service =
3951 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3952 LUSTRE_MDT_NAME"_mdsc",
3953 procfs_entry, target_print_req,"mdt_mdsc");
3954 if (!m->mdt_mdsc_service) {
3955 CERROR("failed to start seq controller service\n");
3956 GOTO(err_mdt_svc, rc = -ENOMEM);
3959 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3961 GOTO(err_mdt_svc, rc);
3964 * metadata sequence server service configuration
3966 conf = (typeof(conf)) {
3967 .psc_nbufs = MDS_NBUFS,
3968 .psc_bufsize = MDS_BUFSIZE,
3969 .psc_max_req_size = SEQ_MAXREQSIZE,
3970 .psc_max_reply_size = SEQ_MAXREPSIZE,
3971 .psc_req_portal = SEQ_METADATA_PORTAL,
3972 .psc_rep_portal = MDC_REPLY_PORTAL,
3973 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3974 .psc_min_threads = SEQ_NUM_THREADS,
3975 .psc_max_threads = SEQ_NUM_THREADS,
3976 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3979 m->mdt_mdss_service =
3980 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3981 LUSTRE_MDT_NAME"_mdss",
3982 procfs_entry, target_print_req,"mdt_mdss");
3983 if (!m->mdt_mdss_service) {
3984 CERROR("failed to start metadata seq server service\n");
3985 GOTO(err_mdt_svc, rc = -ENOMEM);
3988 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3990 GOTO(err_mdt_svc, rc);
3994 * Data sequence server service configuration. We want to have really
3995 * cluster-wide sequences space. This is why we start only one sequence
3996 * controller which manages space.
3998 conf = (typeof(conf)) {
3999 .psc_nbufs = MDS_NBUFS,
4000 .psc_bufsize = MDS_BUFSIZE,
4001 .psc_max_req_size = SEQ_MAXREQSIZE,
4002 .psc_max_reply_size = SEQ_MAXREPSIZE,
4003 .psc_req_portal = SEQ_DATA_PORTAL,
4004 .psc_rep_portal = OSC_REPLY_PORTAL,
4005 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4006 .psc_min_threads = SEQ_NUM_THREADS,
4007 .psc_max_threads = SEQ_NUM_THREADS,
4008 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4011 m->mdt_dtss_service =
4012 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
4013 LUSTRE_MDT_NAME"_dtss",
4014 procfs_entry, target_print_req,"mdt_dtss");
4015 if (!m->mdt_dtss_service) {
4016 CERROR("failed to start data seq server service\n");
4017 GOTO(err_mdt_svc, rc = -ENOMEM);
4020 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
4022 GOTO(err_mdt_svc, rc);
4024 /* FLD service start */
4025 conf = (typeof(conf)) {
4026 .psc_nbufs = MDS_NBUFS,
4027 .psc_bufsize = MDS_BUFSIZE,
4028 .psc_max_req_size = FLD_MAXREQSIZE,
4029 .psc_max_reply_size = FLD_MAXREPSIZE,
4030 .psc_req_portal = FLD_REQUEST_PORTAL,
4031 .psc_rep_portal = MDC_REPLY_PORTAL,
4032 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4033 .psc_min_threads = FLD_NUM_THREADS,
4034 .psc_max_threads = FLD_NUM_THREADS,
4035 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
4038 m->mdt_fld_service =
4039 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
4040 LUSTRE_MDT_NAME"_fld",
4041 procfs_entry, target_print_req, "mdt_fld");
4042 if (!m->mdt_fld_service) {
4043 CERROR("failed to start fld service\n");
4044 GOTO(err_mdt_svc, rc = -ENOMEM);
4047 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
4049 GOTO(err_mdt_svc, rc);
4052 * mds-mds service configuration. Separate portal is used to allow
4053 * mds-mds requests be not blocked during recovery.
4055 conf = (typeof(conf)) {
4056 .psc_nbufs = MDS_NBUFS,
4057 .psc_bufsize = MDS_BUFSIZE,
4058 .psc_max_req_size = MDS_MAXREQSIZE,
4059 .psc_max_reply_size = MDS_MAXREPSIZE,
4060 .psc_req_portal = MDS_MDS_PORTAL,
4061 .psc_rep_portal = MDC_REPLY_PORTAL,
4062 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4063 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
4065 .psc_max_threads = MDT_MAX_THREADS,
4066 .psc_ctx_tags = LCT_MD_THREAD
4068 m->mdt_xmds_service =
4069 ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
4070 LUSTRE_MDT_NAME "_mds",
4071 procfs_entry, target_print_req,"mdt_xmds");
4073 if (m->mdt_xmds_service == NULL) {
4074 CERROR("failed to start readpage service\n");
4075 GOTO(err_mdt_svc, rc = -ENOMEM);
4078 rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
4080 GOTO(err_mdt_svc, rc);
4085 mdt_stop_ptlrpc_service(m);
4090 static void mdt_stack_fini(const struct lu_env *env,
4091 struct mdt_device *m, struct lu_device *top)
4093 struct obd_device *obd = mdt2obd_dev(m);
4094 struct lustre_cfg_bufs *bufs;
4095 struct lustre_cfg *lcfg;
4096 struct mdt_thread_info *info;
4100 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4101 LASSERT(info != NULL);
4103 bufs = &info->mti_u.bufs;
4104 /* process cleanup, pass mdt obd name to get obd umount flags */
4105 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4110 lustre_cfg_bufs_set_string(bufs, 1, flags);
4111 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4113 CERROR("Cannot alloc lcfg!\n");
4118 top->ld_ops->ldo_process_config(env, top, lcfg);
4119 lustre_cfg_free(lcfg);
4121 lu_stack_fini(env, top);
4122 m->mdt_child = NULL;
4123 m->mdt_bottom = NULL;
4126 static struct lu_device *mdt_layer_setup(struct lu_env *env,
4127 const char *typename,
4128 struct lu_device *child,
4129 struct lustre_cfg *cfg)
4131 const char *dev = lustre_cfg_string(cfg, 0);
4132 struct obd_type *type;
4133 struct lu_device_type *ldt;
4134 struct lu_device *d;
4139 type = class_get_type(typename);
4141 CERROR("Unknown type: '%s'\n", typename);
4142 GOTO(out, rc = -ENODEV);
4145 rc = lu_env_refill((struct lu_env *)env);
4147 CERROR("Failure to refill session: '%d'\n", rc);
4153 CERROR("type: '%s'\n", typename);
4154 GOTO(out_type, rc = -EINVAL);
4157 ldt->ldt_obd_type = type;
4158 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
4160 CERROR("Cannot allocate device: '%s'\n", typename);
4161 GOTO(out_type, rc = -ENODEV);
4164 LASSERT(child->ld_site);
4165 d->ld_site = child->ld_site;
4168 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
4170 CERROR("can't init device '%s', rc %d\n", typename, rc);
4171 GOTO(out_alloc, rc);
4174 lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
4179 ldt->ldt_ops->ldto_device_free(env, d);
4182 class_put_type(type);
4187 static int mdt_stack_init(struct lu_env *env,
4188 struct mdt_device *m,
4189 struct lustre_cfg *cfg,
4190 struct lustre_mount_info *lmi)
4192 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4193 struct lu_device *tmp;
4194 struct md_device *md;
4195 struct lu_device *child_lu_dev;
4199 /* init the stack */
4200 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
4202 RETURN(PTR_ERR(tmp));
4204 m->mdt_bottom = lu2dt_dev(tmp);
4206 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
4208 GOTO(out, rc = PTR_ERR(tmp));
4213 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
4215 GOTO(out, rc = PTR_ERR(tmp));
4218 /*set mdd upcall device*/
4219 md_upcall_dev_set(md, lu2md_dev(d));
4222 /*set cmm upcall device*/
4223 md_upcall_dev_set(md, &m->mdt_md_dev);
4225 m->mdt_child = lu2md_dev(d);
4227 /* process setup config */
4228 tmp = &m->mdt_md_dev.md_lu_dev;
4229 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
4233 /* initialize local objects */
4234 child_lu_dev = &m->mdt_child->md_lu_dev;
4236 rc = child_lu_dev->ld_ops->ldo_prepare(env,
4237 &m->mdt_md_dev.md_lu_dev,
4240 /* fini from last known good lu_device */
4242 mdt_stack_fini(env, m, d);
4248 * setup CONFIG_ORIG context, used to access local config log.
4249 * this may need to be rewrite as part of llog rewrite for lu-api.
4251 static int mdt_obd_llog_setup(struct obd_device *obd,
4252 struct lustre_sb_info *lsi)
4256 LASSERT(obd->obd_fsops == NULL);
4258 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
4259 if (IS_ERR(obd->obd_fsops))
4260 return PTR_ERR(obd->obd_fsops);
4262 rc = fsfilt_setup(obd, lsi->lsi_srv_mnt->mnt_sb);
4264 fsfilt_put_ops(obd->obd_fsops);
4268 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
4269 obd->obd_lvfs_ctxt.pwdmnt = lsi->lsi_srv_mnt;
4270 obd->obd_lvfs_ctxt.pwd = lsi->lsi_srv_mnt->mnt_root;
4271 obd->obd_lvfs_ctxt.fs = get_ds();
4273 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd,
4274 0, NULL, &llog_lvfs_ops);
4276 CERROR("llog_setup() failed: %d\n", rc);
4277 fsfilt_put_ops(obd->obd_fsops);
4283 static void mdt_obd_llog_cleanup(struct obd_device *obd)
4285 struct llog_ctxt *ctxt;
4287 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
4291 if (obd->obd_fsops) {
4292 fsfilt_put_ops(obd->obd_fsops);
4293 obd->obd_fsops = NULL;
4297 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4299 struct md_device *next = m->mdt_child;
4300 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4301 struct lu_site *ls = d->ld_site;
4302 struct obd_device *obd = mdt2obd_dev(m);
4306 target_recovery_fini(obd);
4307 /* At this point, obd exports might still be on the "obd_zombie_exports"
4308 * list, and obd_zombie_impexp_thread() is trying to destroy them.
4309 * We wait a little bit until all exports (except the self-export)
4310 * have been destroyed, because the whole mdt stack might be accessed
4311 * in mdt_destroy_export(). This will not be a long time, maybe one or
4312 * two seconds are enough. This is not a problem while umounting.
4314 * The three references that should be remaining are the
4315 * obd_self_export and the attach and setup references.
4317 while (atomic_read(&obd->obd_refcount) > 3) {
4318 cfs_schedule_timeout(CFS_TASK_UNINT, cfs_time_seconds(1));
4320 if (waited > 5 && IS_PO2(waited))
4321 LCONSOLE_WARN("Waiting for obd_zombie_impexp_thread "
4322 "more than %d seconds to destroy all "
4323 "the exports. The current obd refcount ="
4324 " %d. Is it stuck there?\n",
4325 waited, atomic_read(&obd->obd_refcount));
4328 ping_evictor_stop();
4330 mdt_stop_ptlrpc_service(m);
4331 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4332 mdt_obd_llog_cleanup(obd);
4333 obd_zombie_barrier();
4334 #ifdef HAVE_QUOTA_SUPPORT
4335 next->md_ops->mdo_quota.mqo_cleanup(env, next);
4337 lut_fini(env, &m->mdt_lut);
4338 mdt_fs_cleanup(env, m);
4339 upcall_cache_cleanup(m->mdt_identity_cache);
4340 m->mdt_identity_cache = NULL;
4342 if (m->mdt_namespace != NULL) {
4343 ldlm_namespace_free(m->mdt_namespace, NULL, d->ld_obd->obd_force);
4344 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4347 cfs_free_nidlist(&m->mdt_nosquash_nids);
4348 if (m->mdt_nosquash_str) {
4349 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4350 m->mdt_nosquash_str = NULL;
4351 m->mdt_nosquash_strlen = 0;
4354 mdt_seq_fini(env, m);
4355 mdt_seq_fini_cli(m);
4356 mdt_fld_fini(env, m);
4357 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4359 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4360 cfs_timer_disarm(&m->mdt_ck_timer);
4361 mdt_ck_thread_stop(m);
4366 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4369 if (obd->obd_proc_exports_entry) {
4370 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
4371 obd->obd_proc_exports_entry = NULL;
4373 lprocfs_free_per_client_stats(obd);
4374 lprocfs_free_obd_stats(obd);
4375 ptlrpc_lprocfs_unregister_obd(obd);
4376 lprocfs_obd_cleanup(obd);
4379 struct md_site *mite;
4382 mite = lu_site2md(ls);
4386 LASSERT(atomic_read(&d->ld_ref) == 0);
4391 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4393 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4394 struct sptlrpc_rule_set tmp_rset;
4397 sptlrpc_rule_set_init(&tmp_rset);
4398 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4400 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4405 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4407 write_lock(&m->mdt_sptlrpc_lock);
4408 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4409 m->mdt_sptlrpc_rset = tmp_rset;
4410 write_unlock(&m->mdt_sptlrpc_lock);
4415 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
4419 m->mdt_opts.mo_mds_capa = 1;
4420 m->mdt_opts.mo_oss_capa = 1;
4421 #ifdef CONFIG_FS_POSIX_ACL
4422 /* ACLs should be enabled by default (b=13829) */
4423 m->mdt_opts.mo_acl = 1;
4424 LCONSOLE_INFO("Enabling ACL\n");
4426 m->mdt_opts.mo_acl = 0;
4427 LCONSOLE_INFO("Disabling ACL\n");
4436 while (*p && *p != ',')
4440 if ((len == sizeof("user_xattr") - 1) &&
4441 (memcmp(options, "user_xattr", len) == 0)) {
4442 m->mdt_opts.mo_user_xattr = 1;
4443 LCONSOLE_INFO("Enabling user_xattr\n");
4444 } else if ((len == sizeof("nouser_xattr") - 1) &&
4445 (memcmp(options, "nouser_xattr", len) == 0)) {
4446 m->mdt_opts.mo_user_xattr = 0;
4447 LCONSOLE_INFO("Disabling user_xattr\n");
4448 } else if ((len == sizeof("noacl") - 1) &&
4449 (memcmp(options, "noacl", len) == 0)) {
4450 m->mdt_opts.mo_acl = 0;
4451 LCONSOLE_INFO("Disabling ACL\n");
4458 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4460 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4461 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4463 struct lprocfs_static_vars lvars;
4464 struct mdt_thread_info *info;
4465 struct obd_device *obd;
4466 const char *dev = lustre_cfg_string(cfg, 0);
4467 const char *num = lustre_cfg_string(cfg, 2);
4468 struct lustre_mount_info *lmi = NULL;
4469 struct lustre_sb_info *lsi;
4470 struct lustre_disk_data *ldd;
4472 struct md_site *mite;
4473 const char *identity_upcall = "NONE";
4474 #ifdef HAVE_QUOTA_SUPPORT
4475 struct md_device *next;
4481 md_device_init(&m->mdt_md_dev, ldt);
4483 * Environment (env) might be missing mdt_thread_key values at that
4484 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4487 * Usually device allocation path doesn't use module key values, but
4488 * mdt has to do a lot of work here, so allocate key value.
4490 rc = lu_env_refill((struct lu_env *)env);
4494 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4495 LASSERT(info != NULL);
4497 obd = class_name2obd(dev);
4498 LASSERT(obd != NULL);
4500 spin_lock_init(&m->mdt_transno_lock);
4502 m->mdt_max_mdsize = MAX_MD_SIZE;
4503 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
4505 m->mdt_opts.mo_user_xattr = 0;
4506 m->mdt_opts.mo_acl = 0;
4507 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4508 lmi = server_get_mount_2(dev);
4510 CERROR("Cannot get mount info for %s!\n", dev);
4513 lsi = s2lsi(lmi->lmi_sb);
4514 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
4515 /* CMD is supported only in IAM mode */
4518 node_id = simple_strtol(num, NULL, 10);
4519 if (!(ldd->ldd_flags & LDD_F_IAM_DIR) && node_id) {
4520 CERROR("CMD Operation not allowed in IOP mode\n");
4521 GOTO(err_lmi, rc = -EINVAL);
4525 rwlock_init(&m->mdt_sptlrpc_lock);
4526 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4528 spin_lock_init(&m->mdt_ioepoch_lock);
4529 m->mdt_opts.mo_compat_resname = 0;
4530 m->mdt_capa_timeout = CAPA_TIMEOUT;
4531 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4532 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4533 m->mdt_squash_uid = 0;
4534 m->mdt_squash_gid = 0;
4535 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4536 m->mdt_nosquash_str = NULL;
4537 m->mdt_nosquash_strlen = 0;
4538 init_rwsem(&m->mdt_squash_sem);
4540 spin_lock_init(&m->mdt_client_bitmap_lock);
4542 OBD_ALLOC_PTR(mite);
4544 GOTO(err_lmi, rc = -ENOMEM);
4548 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4549 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4550 /* set this lu_device to obd, because error handling need it */
4551 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4553 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
4555 CERROR("Can't init lu_site, rc %d\n", rc);
4556 GOTO(err_free_site, rc);
4559 lprocfs_mdt_init_vars(&lvars);
4560 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
4562 CERROR("Can't init lprocfs, rc %d\n", rc);
4563 GOTO(err_fini_site, rc);
4565 ptlrpc_lprocfs_register_obd(obd);
4567 rc = mdt_procfs_init(m, dev);
4569 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4570 GOTO(err_fini_proc, rc);
4573 obd->obd_proc_exports_entry = proc_mkdir("exports",
4574 obd->obd_proc_entry);
4575 if (obd->obd_proc_exports_entry)
4576 lprocfs_add_simple(obd->obd_proc_exports_entry,
4577 "clear", lprocfs_nid_stats_clear_read,
4578 lprocfs_nid_stats_clear_write, obd, NULL);
4580 /* set server index */
4581 lu_site2md(s)->ms_node_id = node_id;
4583 /* failover is the default
4584 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4585 * assumed whose ms_node_id == 0 XXX
4587 obd->obd_replayable = 1;
4588 /* No connection accepted until configurations will finish */
4589 obd->obd_no_conn = 1;
4591 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4592 char *str = lustre_cfg_string(cfg, 4);
4593 if (strchr(str, 'n')) {
4594 CWARN("%s: recovery disabled\n", obd->obd_name);
4595 obd->obd_replayable = 0;
4599 /* init the stack */
4600 rc = mdt_stack_init((struct lu_env *)env, m, cfg, lmi);
4602 CERROR("Can't init device stack, rc %d\n", rc);
4603 GOTO(err_fini_proc, rc);
4606 rc = lut_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4608 GOTO(err_fini_stack, rc);
4610 rc = mdt_fld_init(env, obd->obd_name, m);
4614 rc = mdt_seq_init(env, obd->obd_name, m);
4616 GOTO(err_fini_fld, rc);
4618 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4619 LUSTRE_MDT_NAME"-%p", m);
4620 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4621 LDLM_NAMESPACE_SERVER,
4622 LDLM_NAMESPACE_GREEDY);
4623 if (m->mdt_namespace == NULL)
4624 GOTO(err_fini_seq, rc = -ENOMEM);
4626 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4627 /* set obd_namespace for compatibility with old code */
4628 obd->obd_namespace = m->mdt_namespace;
4630 /* XXX: to support suppgid for ACL, we enable identity_upcall
4631 * by default, otherwise, maybe got unexpected -EACCESS. */
4632 if (m->mdt_opts.mo_acl)
4633 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4635 m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
4636 &mdt_identity_upcall_cache_ops);
4637 if (IS_ERR(m->mdt_identity_cache)) {
4638 rc = PTR_ERR(m->mdt_identity_cache);
4639 m->mdt_identity_cache = NULL;
4640 GOTO(err_free_ns, rc);
4643 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4645 rc = mdt_ck_thread_start(m);
4647 GOTO(err_free_ns, rc);
4649 rc = mdt_fs_setup(env, m, obd, lsi);
4653 rc = mdt_obd_llog_setup(obd, lsi);
4655 GOTO(err_fs_cleanup, rc);
4657 rc = mdt_llog_ctxt_clone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4659 GOTO(err_llog_cleanup, rc);
4661 mdt_adapt_sptlrpc_conf(obd, 1);
4663 #ifdef HAVE_QUOTA_SUPPORT
4664 next = m->mdt_child;
4665 rc = next->md_ops->mdo_quota.mqo_setup(env, next, lmi->lmi_mnt);
4667 GOTO(err_llog_cleanup, rc);
4670 server_put_mount_2(dev, lmi->lmi_mnt);
4673 target_recovery_init(&m->mdt_lut, mdt_recovery_handle);
4675 rc = mdt_start_ptlrpc_service(m);
4677 GOTO(err_recovery, rc);
4679 ping_evictor_start();
4681 rc = lu_site_init_finish(s);
4683 GOTO(err_stop_service, rc);
4685 if (obd->obd_recovering == 0)
4686 mdt_postrecov(env, m);
4688 mdt_init_capa_ctxt(env, m);
4690 /* Reduce the initial timeout on an MDS because it doesn't need such
4691 * a long timeout as an OST does. Adaptive timeouts will adjust this
4692 * value appropriately. */
4693 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4694 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4699 ping_evictor_stop();
4700 mdt_stop_ptlrpc_service(m);
4702 target_recovery_fini(obd);
4703 #ifdef HAVE_QUOTA_SUPPORT
4704 next->md_ops->mdo_quota.mqo_cleanup(env, next);
4707 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4708 mdt_obd_llog_cleanup(obd);
4710 mdt_fs_cleanup(env, m);
4712 cfs_timer_disarm(&m->mdt_ck_timer);
4713 mdt_ck_thread_stop(m);
4715 upcall_cache_cleanup(m->mdt_identity_cache);
4716 m->mdt_identity_cache = NULL;
4717 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4718 obd->obd_namespace = m->mdt_namespace = NULL;
4720 mdt_seq_fini(env, m);
4722 mdt_fld_fini(env, m);
4724 lut_fini(env, &m->mdt_lut);
4726 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4729 if (obd->obd_proc_exports_entry) {
4730 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
4731 obd->obd_proc_exports_entry = NULL;
4733 ptlrpc_lprocfs_unregister_obd(obd);
4734 lprocfs_obd_cleanup(obd);
4741 server_put_mount_2(dev, lmi->lmi_mnt);
4745 /* used by MGS to process specific configurations */
4746 static int mdt_process_config(const struct lu_env *env,
4747 struct lu_device *d, struct lustre_cfg *cfg)
4749 struct mdt_device *m = mdt_dev(d);
4750 struct md_device *md_next = m->mdt_child;
4751 struct lu_device *next = md2lu_dev(md_next);
4755 switch (cfg->lcfg_command) {
4757 struct lprocfs_static_vars lvars;
4758 struct obd_device *obd = d->ld_obd;
4761 * For interoperability between 1.8 and 2.0,
4762 * skip old "mdt.group_upcall" param.
4765 char *param = lustre_cfg_string(cfg, 1);
4766 if (param && !strncmp("mdt.group_upcall", param, 16)) {
4767 CWARN("For 1.8 interoperability, skip this"
4768 " mdt.group_upcall. It is obsolete\n");
4773 lprocfs_mdt_init_vars(&lvars);
4774 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4776 if (rc > 0 || rc == -ENOSYS)
4777 /* we don't understand; pass it on */
4778 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4783 * Add mdc hook to get first MDT uuid and connect it to
4784 * ls->controller to use for seq manager.
4786 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4788 CERROR("Can't add mdc, rc %d\n", rc);
4790 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4793 /* others are passed further */
4794 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4800 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4801 const struct lu_object_header *hdr,
4802 struct lu_device *d)
4804 struct mdt_object *mo;
4810 struct lu_object *o;
4811 struct lu_object_header *h;
4813 o = &mo->mot_obj.mo_lu;
4814 h = &mo->mot_header;
4815 lu_object_header_init(h);
4816 lu_object_init(o, h, d);
4817 lu_object_add_top(h, o);
4818 o->lo_ops = &mdt_obj_ops;
4824 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4825 const struct lu_object_conf *_)
4827 struct mdt_device *d = mdt_dev(o->lo_dev);
4828 struct lu_device *under;
4829 struct lu_object *below;
4833 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4834 PFID(lu_object_fid(o)));
4836 under = &d->mdt_child->md_lu_dev;
4837 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4838 if (below != NULL) {
4839 lu_object_add(o, below);
4846 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4848 struct mdt_object *mo = mdt_obj(o);
4849 struct lu_object_header *h;
4853 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4854 PFID(lu_object_fid(o)));
4857 lu_object_header_fini(h);
4862 static int mdt_object_print(const struct lu_env *env, void *cookie,
4863 lu_printer_t p, const struct lu_object *o)
4865 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4866 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch=%llu "
4867 "flags=%llx, epochcount=%d, writecount=%d)",
4868 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4869 mdto->mot_epochcount, mdto->mot_writecount);
4872 static const struct lu_device_operations mdt_lu_ops = {
4873 .ldo_object_alloc = mdt_object_alloc,
4874 .ldo_process_config = mdt_process_config,
4877 static const struct lu_object_operations mdt_obj_ops = {
4878 .loo_object_init = mdt_object_init,
4879 .loo_object_free = mdt_object_free,
4880 .loo_object_print = mdt_object_print
4883 static int mdt_obd_set_info_async(struct obd_export *exp,
4884 __u32 keylen, void *key,
4885 __u32 vallen, void *val,
4886 struct ptlrpc_request_set *set)
4888 struct obd_device *obd = exp->exp_obd;
4894 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4895 rc = mdt_adapt_sptlrpc_conf(obd, 0);
4902 /* mds_connect_internal */
4903 static int mdt_connect_internal(struct obd_export *exp,
4904 struct mdt_device *mdt,
4905 struct obd_connect_data *data)
4908 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4909 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4911 /* If no known bits (which should not happen, probably,
4912 as everybody should support LOOKUP and UPDATE bits at least)
4913 revert to compat mode with plain locks. */
4914 if (!data->ocd_ibits_known &&
4915 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4916 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4918 if (!mdt->mdt_opts.mo_acl)
4919 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4921 if (!mdt->mdt_opts.mo_user_xattr)
4922 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4924 spin_lock(&exp->exp_lock);
4925 exp->exp_connect_flags = data->ocd_connect_flags;
4926 spin_unlock(&exp->exp_lock);
4927 data->ocd_version = LUSTRE_VERSION_CODE;
4928 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4932 if (mdt->mdt_opts.mo_acl &&
4933 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4934 CWARN("%s: MDS requires ACL support but client does not\n",
4935 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4940 if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
4941 CWARN("%s: MDS requires FID support, but client not\n",
4942 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4949 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
4950 struct obd_export *exp,
4951 struct ptlrpc_request *req)
4953 struct sptlrpc_flavor flvr;
4956 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
4957 read_lock(&mdt->mdt_sptlrpc_lock);
4958 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
4962 read_unlock(&mdt->mdt_sptlrpc_lock);
4964 spin_lock(&exp->exp_lock);
4966 exp->exp_sp_peer = req->rq_sp_from;
4967 exp->exp_flvr = flvr;
4969 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
4970 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4971 CERROR("unauthorized rpc flavor %x from %s, "
4972 "expect %x\n", req->rq_flvr.sf_rpc,
4973 libcfs_nid2str(req->rq_peer.nid),
4974 exp->exp_flvr.sf_rpc);
4978 spin_unlock(&exp->exp_lock);
4980 if (exp->exp_sp_peer != req->rq_sp_from) {
4981 CERROR("RPC source %s doesn't match %s\n",
4982 sptlrpc_part2name(req->rq_sp_from),
4983 sptlrpc_part2name(exp->exp_sp_peer));
4986 rc = sptlrpc_target_export_check(exp, req);
4993 /* mds_connect copy */
4994 static int mdt_obd_connect(const struct lu_env *env,
4995 struct obd_export **exp, struct obd_device *obd,
4996 struct obd_uuid *cluuid,
4997 struct obd_connect_data *data,
5000 struct mdt_thread_info *info;
5001 struct lsd_client_data *lcd;
5002 struct obd_export *lexp;
5003 struct lustre_handle conn = { 0 };
5004 struct mdt_device *mdt;
5005 struct ptlrpc_request *req;
5009 LASSERT(env != NULL);
5010 if (!exp || !obd || !cluuid)
5013 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5014 req = info->mti_pill->rc_req;
5015 mdt = mdt_dev(obd->obd_lu_dev);
5017 rc = class_connect(&conn, obd, cluuid);
5021 lexp = class_conn2export(&conn);
5022 LASSERT(lexp != NULL);
5024 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5028 rc = mdt_connect_internal(lexp, mdt, data);
5032 struct mdt_thread_info *mti;
5033 mti = lu_context_key_get(&env->le_ctx,
5035 LASSERT(mti != NULL);
5036 mti->mti_exp = lexp;
5037 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5038 lexp->exp_mdt_data.med_lcd = lcd;
5039 rc = mdt_client_new(env, mdt);
5042 lexp->exp_mdt_data.med_lcd = NULL;
5044 mdt_export_stats_init(obd, lexp, localdata);
5052 class_disconnect(lexp);
5059 static int mdt_obd_reconnect(const struct lu_env *env,
5060 struct obd_export *exp, struct obd_device *obd,
5061 struct obd_uuid *cluuid,
5062 struct obd_connect_data *data,
5065 struct mdt_thread_info *info;
5066 struct mdt_device *mdt;
5067 struct ptlrpc_request *req;
5071 if (exp == NULL || obd == NULL || cluuid == NULL)
5074 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5075 req = info->mti_pill->rc_req;
5076 mdt = mdt_dev(obd->obd_lu_dev);
5078 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5082 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5084 mdt_export_stats_init(obd, exp, localdata);
5089 static int mdt_obd_disconnect(struct obd_export *exp)
5091 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5096 class_export_get(exp);
5098 /* Disconnect early so that clients can't keep using export */
5099 rc = class_disconnect(exp);
5100 if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
5101 ldlm_cancel_locks_for_export(exp);
5103 /* release nid stat refererence */
5104 lprocfs_exp_cleanup(exp);
5106 /* complete all outstanding replies */
5107 spin_lock(&exp->exp_lock);
5108 while (!list_empty(&exp->exp_outstanding_replies)) {
5109 struct ptlrpc_reply_state *rs =
5110 list_entry(exp->exp_outstanding_replies.next,
5111 struct ptlrpc_reply_state, rs_exp_list);
5112 struct ptlrpc_service *svc = rs->rs_service;
5114 spin_lock(&svc->srv_lock);
5115 list_del_init(&rs->rs_exp_list);
5116 spin_lock(&rs->rs_lock);
5117 ptlrpc_schedule_difficult_reply(rs);
5118 spin_unlock(&rs->rs_lock);
5119 spin_unlock(&svc->srv_lock);
5121 spin_unlock(&exp->exp_lock);
5123 class_export_put(exp);
5127 /* FIXME: Can we avoid using these two interfaces? */
5128 static int mdt_init_export(struct obd_export *exp)
5130 struct mdt_export_data *med = &exp->exp_mdt_data;
5134 CFS_INIT_LIST_HEAD(&med->med_open_head);
5135 spin_lock_init(&med->med_open_lock);
5136 sema_init(&med->med_idmap_sem, 1);
5137 med->med_idmap = NULL;
5138 spin_lock(&exp->exp_lock);
5139 exp->exp_connecting = 1;
5140 spin_unlock(&exp->exp_lock);
5141 rc = ldlm_init_export(exp);
5143 CERROR("Error %d while initializing export\n", rc);
5147 static int mdt_destroy_export(struct obd_export *export)
5149 struct mdt_export_data *med;
5150 struct obd_device *obd = export->exp_obd;
5151 struct mdt_device *mdt;
5152 struct mdt_thread_info *info;
5157 CFS_LIST_HEAD(closing_list);
5158 struct mdt_file_data *mfd, *n;
5162 med = &export->exp_mdt_data;
5163 if (exp_connect_rmtclient(export))
5164 mdt_cleanup_idmap(med);
5166 target_destroy_export(export);
5167 ldlm_destroy_export(export);
5169 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
5172 mdt = mdt_dev(obd->obd_lu_dev);
5173 LASSERT(mdt != NULL);
5175 rc = lu_env_init(&env, LCT_MD_THREAD);
5179 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5180 LASSERT(info != NULL);
5181 memset(info, 0, sizeof *info);
5182 info->mti_env = &env;
5183 info->mti_mdt = mdt;
5184 info->mti_exp = export;
5186 ma = &info->mti_attr;
5187 lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
5188 cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
5189 OBD_ALLOC(ma->ma_lmm, lmm_size);
5190 OBD_ALLOC(ma->ma_cookie, cookie_size);
5192 if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
5193 GOTO(out, rc = -ENOMEM);
5194 ma->ma_need = MA_LOV | MA_COOKIE;
5196 /* Close any open files (which may also cause orphan unlinking). */
5197 spin_lock(&med->med_open_lock);
5198 while (!list_empty(&med->med_open_head)) {
5199 struct list_head *tmp = med->med_open_head.next;
5200 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5202 /* Remove mfd handle so it can't be found again.
5203 * We are consuming the mfd_list reference here. */
5204 class_handle_unhash(&mfd->mfd_handle);
5205 list_move_tail(&mfd->mfd_list, &closing_list);
5207 spin_unlock(&med->med_open_lock);
5209 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5210 list_del_init(&mfd->mfd_list);
5211 mdt_mfd_close(info, mfd);
5212 /* TODO: if we close the unlinked file,
5213 * we need to remove its objects from OST */
5214 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
5215 spin_lock(&med->med_open_lock);
5216 ma->ma_lmm_size = lmm_size;
5217 ma->ma_cookie_size = cookie_size;
5218 ma->ma_need = MA_LOV | MA_COOKIE;
5220 spin_unlock(&med->med_open_lock);
5223 info->mti_mdt = NULL;
5224 mdt_client_del(&env, mdt);
5229 OBD_FREE(ma->ma_lmm, lmm_size);
5233 OBD_FREE(ma->ma_cookie, cookie_size);
5234 ma->ma_cookie = NULL;
5241 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
5243 if (flag & CONFIG_LOG)
5244 m->mdt_fl_cfglog = 1;
5246 /* also notify active event */
5247 if (flag & CONFIG_SYNC)
5248 m->mdt_fl_synced = 1;
5250 if (m->mdt_fl_cfglog && m->mdt_fl_synced)
5251 /* Open for clients */
5252 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
5255 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
5256 enum md_upcall_event ev)
5258 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
5259 struct md_device *next = m->mdt_child;
5260 struct mdt_thread_info *mti;
5266 rc = next->md_ops->mdo_maxsize_get(env, next,
5268 &m->mdt_max_cookiesize);
5269 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
5270 m->mdt_max_mdsize, m->mdt_max_cookiesize);
5271 mdt_allow_cli(m, CONFIG_SYNC);
5272 #ifdef HAVE_QUOTA_SUPPORT
5273 if (md->md_lu_dev.ld_obd->obd_recovering == 0)
5274 next->md_ops->mdo_quota.mqo_recovery(env, next);
5278 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5279 mti->mti_no_need_trans = 1;
5280 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
5283 /* Check that MDT is not yet configured */
5284 LASSERT(!m->mdt_fl_cfglog);
5287 CERROR("invalid event\n");
5294 static int mdt_obd_notify(struct obd_device *host,
5295 struct obd_device *watched,
5296 enum obd_notify_event ev, void *data)
5298 struct mdt_device *mdt = mdt_dev(host->obd_lu_dev);
5299 #ifdef HAVE_QUOTA_SUPPORT
5300 struct md_device *next = mdt->mdt_child;
5305 case OBD_NOTIFY_CONFIG:
5306 mdt_allow_cli(mdt, (unsigned long)data);
5308 #ifdef HAVE_QUOTA_SUPPORT
5309 /* quota_type has been processed, we can now handle
5310 * incoming quota requests */
5311 next->md_ops->mdo_quota.mqo_notify(NULL, next);
5315 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
5320 static int mdt_ioc_fid2path(struct lu_env *env, struct mdt_device *mdt,
5321 struct obd_ioctl_data *data)
5323 struct lu_context ioctl_session;
5324 struct mdt_object *obj;
5328 int pathlen = data->ioc_plen1;
5334 fid = (struct lu_fid *)data->ioc_inlbuf1;
5335 memcpy(&recno, data->ioc_inlbuf2, sizeof(recno));
5336 memcpy(&linkno, data->ioc_inlbuf3, sizeof(linkno));
5337 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5338 PFID(fid), recno, linkno);
5340 if (!fid_is_sane(fid))
5346 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5349 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5350 lu_context_enter(&ioctl_session);
5351 env->le_ses = &ioctl_session;
5353 OBD_ALLOC(path, pathlen);
5355 GOTO(out_context, rc = -ENOMEM);
5357 obj = mdt_object_find(env, mdt, fid);
5358 if (obj == NULL || IS_ERR(obj)) {
5359 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(fid),
5361 GOTO(out_free, rc = -EINVAL);
5364 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5370 mdt_object_put(env, obj);
5371 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n", PFID(fid),
5376 rc = mo_path(env, md_object_next(&obj->mot_obj), path, pathlen, &recno,
5378 mdt_object_put(env, obj);
5382 if (copy_to_user(data->ioc_pbuf1, path, pathlen))
5385 memcpy(data->ioc_inlbuf2, &recno, sizeof(recno));
5386 memcpy(data->ioc_inlbuf3, &linkno, sizeof(linkno));
5390 OBD_FREE(path, pathlen);
5392 lu_context_exit(&ioctl_session);
5393 lu_context_fini(&ioctl_session);
5397 /* Pass the ioc down */
5398 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5399 unsigned int cmd, int len, void *data)
5401 struct lu_context ioctl_session;
5402 struct md_device *next = mdt->mdt_child;
5406 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5409 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5410 lu_context_enter(&ioctl_session);
5411 env->le_ses = &ioctl_session;
5413 LASSERT(next->md_ops->mdo_iocontrol);
5414 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5416 lu_context_exit(&ioctl_session);
5417 lu_context_fini(&ioctl_session);
5421 /* ioctls on obd dev */
5422 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5423 void *karg, void *uarg)
5426 struct obd_device *obd = exp->exp_obd;
5427 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5428 struct dt_device *dt = mdt->mdt_bottom;
5432 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5433 rc = lu_env_init(&env, LCT_MD_THREAD);
5439 rc = mdt_device_sync(&env, mdt);
5441 case OBD_IOC_SET_READONLY:
5442 dt->dd_ops->dt_ro(&env, dt);
5444 case OBD_IOC_ABORT_RECOVERY:
5445 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5446 target_stop_recovery_thread(obd);
5449 case OBD_IOC_FID2PATH:
5450 rc = mdt_ioc_fid2path(&env, mdt, karg);
5452 case OBD_IOC_CHANGELOG_REG:
5453 case OBD_IOC_CHANGELOG_DEREG:
5454 case OBD_IOC_CHANGELOG_CLEAR:
5455 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5458 CERROR("Not supported cmd = %d for device %s\n",
5459 cmd, obd->obd_name);
5467 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5469 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5470 struct obd_device *obd = mdt2obd_dev(mdt);
5471 #ifdef HAVE_QUOTA_SUPPORT
5472 struct md_device *next = mdt->mdt_child;
5476 /* if some clients didn't participate in recovery then we can possibly
5477 * lost sequence. Now we should increase sequence for safe value */
5478 lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
5479 mdt_seq_adjust(env, mdt, lost);
5481 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5482 #ifdef HAVE_QUOTA_SUPPORT
5483 next->md_ops->mdo_quota.mqo_recovery(env, next);
5488 int mdt_obd_postrecov(struct obd_device *obd)
5493 rc = lu_env_init(&env, LCT_MD_THREAD);
5496 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5501 static struct obd_ops mdt_obd_device_ops = {
5502 .o_owner = THIS_MODULE,
5503 .o_set_info_async = mdt_obd_set_info_async,
5504 .o_connect = mdt_obd_connect,
5505 .o_reconnect = mdt_obd_reconnect,
5506 .o_disconnect = mdt_obd_disconnect,
5507 .o_init_export = mdt_init_export,
5508 .o_destroy_export = mdt_destroy_export,
5509 .o_iocontrol = mdt_iocontrol,
5510 .o_postrecov = mdt_obd_postrecov,
5511 .o_notify = mdt_obd_notify
5514 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5515 struct lu_device *d)
5517 struct mdt_device *m = mdt_dev(d);
5524 static struct lu_device *mdt_device_free(const struct lu_env *env,
5525 struct lu_device *d)
5527 struct mdt_device *m = mdt_dev(d);
5530 md_device_fini(&m->mdt_md_dev);
5535 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5536 struct lu_device_type *t,
5537 struct lustre_cfg *cfg)
5539 struct lu_device *l;
5540 struct mdt_device *m;
5546 l = &m->mdt_md_dev.md_lu_dev;
5547 rc = mdt_init0(env, m, t, cfg);
5549 mdt_device_free(env, l);
5553 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
5555 l = ERR_PTR(-ENOMEM);
5559 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5560 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
5562 /* context key: mdt_thread_key */
5563 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5565 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
5566 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
5568 struct lu_context_key mdt_txn_key = {
5569 .lct_tags = LCT_TX_HANDLE,
5570 .lct_init = mdt_txn_key_init,
5571 .lct_fini = mdt_txn_key_fini
5574 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
5576 return md_ucred(info->mti_env);
5580 * Enable/disable COS.
5582 * Set/Clear the COS flag in mdt options.
5584 * \param mdt mdt device
5585 * \param val 0 disables COS, other values enable COS
5587 void mdt_enable_cos(struct mdt_device *mdt, int val)
5592 mdt->mdt_opts.mo_cos = !!val;
5593 rc = lu_env_init(&env, LCT_MD_THREAD);
5594 if (unlikely(rc != 0)) {
5595 CWARN("lu_env initialization failed with rc = %d,"
5596 "cannot sync\n", rc);
5599 mdt_device_sync(&env, mdt);
5606 * Return COS flag status/
5608 * \param mdt mdt device
5610 int mdt_cos_is_enabled(struct mdt_device *mdt)
5612 return mdt->mdt_opts.mo_cos != 0;
5615 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
5616 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
5618 static struct lu_device_type_operations mdt_device_type_ops = {
5619 .ldto_init = mdt_type_init,
5620 .ldto_fini = mdt_type_fini,
5622 .ldto_start = mdt_type_start,
5623 .ldto_stop = mdt_type_stop,
5625 .ldto_device_alloc = mdt_device_alloc,
5626 .ldto_device_free = mdt_device_free,
5627 .ldto_device_fini = mdt_device_fini
5630 static struct lu_device_type mdt_device_type = {
5631 .ldt_tags = LU_DEVICE_MD,
5632 .ldt_name = LUSTRE_MDT_NAME,
5633 .ldt_ops = &mdt_device_type_ops,
5634 .ldt_ctx_tags = LCT_MD_THREAD
5637 static struct lu_local_obj_desc mdt_last_recv = {
5638 .llod_name = LAST_RCVD,
5639 .llod_oid = MDT_LAST_RECV_OID,
5643 static int __init mdt_mod_init(void)
5645 struct lprocfs_static_vars lvars;
5648 llo_local_obj_register(&mdt_last_recv);
5650 mdt_num_threads = MDT_NUM_THREADS;
5651 lprocfs_mdt_init_vars(&lvars);
5652 rc = class_register_type(&mdt_obd_device_ops, NULL,
5653 lvars.module_vars, LUSTRE_MDT_NAME,
5659 static void __exit mdt_mod_exit(void)
5661 llo_local_obj_unregister(&mdt_last_recv);
5662 class_unregister_type(LUSTRE_MDT_NAME);
5666 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
5667 [prefix ## _ ## opc - prefix ## _ ## base] = { \
5669 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
5670 .mh_opc = prefix ## _ ## opc, \
5671 .mh_flags = flags, \
5676 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
5677 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
5679 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
5680 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
5682 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
5683 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
5685 * Request with a format known in advance
5687 #define DEF_MDT_HNDL_F(flags, name, fn) \
5688 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
5690 #define DEF_SEQ_HNDL_F(flags, name, fn) \
5691 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
5693 #define DEF_FLD_HNDL_F(flags, name, fn) \
5694 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
5696 * Request with a format we do not yet know
5698 #define DEF_MDT_HNDL_0(flags, name, fn) \
5699 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
5701 static struct mdt_handler mdt_mds_ops[] = {
5702 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5703 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
5704 DEF_MDT_HNDL_F(0, SET_INFO, mdt_set_info),
5705 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
5706 DEF_MDT_HNDL_F(HABEO_CORPUS, GETATTR, mdt_getattr),
5707 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
5708 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
5709 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
5710 DEF_MDT_HNDL_F(0 |MUTABOR, REINT, mdt_reint),
5711 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
5712 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
5713 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
5714 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
5715 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
5716 #ifdef HAVE_QUOTA_SUPPORT
5717 DEF_MDT_HNDL_F(0, QUOTACHECK, mdt_quotacheck_handle),
5718 DEF_MDT_HNDL_F(0, QUOTACTL, mdt_quotactl_handle)
5722 #define DEF_OBD_HNDL(flags, name, fn) \
5723 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
5726 static struct mdt_handler mdt_obd_ops[] = {
5727 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
5728 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
5729 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
5732 #define DEF_DLM_HNDL_0(flags, name, fn) \
5733 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
5734 #define DEF_DLM_HNDL_F(flags, name, fn) \
5735 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
5737 static struct mdt_handler mdt_dlm_ops[] = {
5738 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
5739 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
5740 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
5741 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
5744 #define DEF_LLOG_HNDL(flags, name, fn) \
5745 DEF_HNDL(LLOG, ORIGIN_HANDLE_CREATE, _NET, flags, name, fn, NULL)
5747 static struct mdt_handler mdt_llog_ops[] = {
5748 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CREATE, mdt_llog_create),
5749 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
5750 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
5751 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_WRITE_REC, NULL),
5752 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CLOSE, NULL),
5753 DEF_LLOG_HNDL(0, ORIGIN_CONNECT, NULL),
5754 DEF_LLOG_HNDL(0, CATINFO, NULL),
5755 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
5756 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
5759 #define DEF_SEC_CTX_HNDL(name, fn) \
5760 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
5762 static struct mdt_handler mdt_sec_ctx_ops[] = {
5763 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
5764 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
5765 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
5768 static struct mdt_opc_slice mdt_regular_handlers[] = {
5770 .mos_opc_start = MDS_GETATTR,
5771 .mos_opc_end = MDS_LAST_OPC,
5772 .mos_hs = mdt_mds_ops
5775 .mos_opc_start = OBD_PING,
5776 .mos_opc_end = OBD_LAST_OPC,
5777 .mos_hs = mdt_obd_ops
5780 .mos_opc_start = LDLM_ENQUEUE,
5781 .mos_opc_end = LDLM_LAST_OPC,
5782 .mos_hs = mdt_dlm_ops
5785 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
5786 .mos_opc_end = LLOG_LAST_OPC,
5787 .mos_hs = mdt_llog_ops
5790 .mos_opc_start = SEC_CTX_INIT,
5791 .mos_opc_end = SEC_LAST_OPC,
5792 .mos_hs = mdt_sec_ctx_ops
5799 static struct mdt_handler mdt_readpage_ops[] = {
5800 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5801 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
5802 #ifdef HAVE_SPLIT_SUPPORT
5803 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
5807 * XXX: this is ugly and should be fixed one day, see mdc_close() for
5808 * detailed comments. --umka
5810 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
5811 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
5814 static struct mdt_opc_slice mdt_readpage_handlers[] = {
5816 .mos_opc_start = MDS_GETATTR,
5817 .mos_opc_end = MDS_LAST_OPC,
5818 .mos_hs = mdt_readpage_ops
5825 static struct mdt_handler mdt_xmds_ops[] = {
5826 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5827 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
5828 DEF_MDT_HNDL_F(0 | MUTABOR , REINT, mdt_reint),
5829 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
5832 static struct mdt_opc_slice mdt_xmds_handlers[] = {
5834 .mos_opc_start = MDS_GETATTR,
5835 .mos_opc_end = MDS_LAST_OPC,
5836 .mos_hs = mdt_xmds_ops
5839 .mos_opc_start = OBD_PING,
5840 .mos_opc_end = OBD_LAST_OPC,
5841 .mos_hs = mdt_obd_ops
5844 .mos_opc_start = SEC_CTX_INIT,
5845 .mos_opc_end = SEC_LAST_OPC,
5846 .mos_hs = mdt_sec_ctx_ops
5853 static struct mdt_handler mdt_seq_ops[] = {
5854 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
5857 static struct mdt_opc_slice mdt_seq_handlers[] = {
5859 .mos_opc_start = SEQ_QUERY,
5860 .mos_opc_end = SEQ_LAST_OPC,
5861 .mos_hs = mdt_seq_ops
5868 static struct mdt_handler mdt_fld_ops[] = {
5869 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
5872 static struct mdt_opc_slice mdt_fld_handlers[] = {
5874 .mos_opc_start = FLD_QUERY,
5875 .mos_opc_end = FLD_LAST_OPC,
5876 .mos_hs = mdt_fld_ops
5883 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5884 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
5885 MODULE_LICENSE("GPL");
5887 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
5888 "number of mdt service threads to start");
5890 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);