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);
168 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
169 struct getinfo_fid2path *fp);
171 static const struct lu_object_operations mdt_obj_ops;
173 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
177 return (rep->lock_policy_res1 & flag);
180 void mdt_clear_disposition(struct mdt_thread_info *info,
181 struct ldlm_reply *rep, int flag)
184 info->mti_opdata &= ~flag;
186 rep->lock_policy_res1 &= ~flag;
189 void mdt_set_disposition(struct mdt_thread_info *info,
190 struct ldlm_reply *rep, int flag)
193 info->mti_opdata |= flag;
195 rep->lock_policy_res1 |= flag;
198 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
200 lh->mlh_pdo_hash = 0;
201 lh->mlh_reg_mode = lm;
202 lh->mlh_type = MDT_REG_LOCK;
205 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
206 const char *name, int namelen)
208 lh->mlh_reg_mode = lm;
209 lh->mlh_type = MDT_PDO_LOCK;
212 LASSERT(namelen > 0);
213 lh->mlh_pdo_hash = full_name_hash(name, namelen);
215 LASSERT(namelen == 0);
216 lh->mlh_pdo_hash = 0ull;
220 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
221 struct mdt_lock_handle *lh)
227 * Any dir access needs couple of locks:
229 * 1) on part of dir we gonna take lookup/modify;
231 * 2) on whole dir to protect it from concurrent splitting and/or to
232 * flush client's cache for readdir().
234 * so, for a given mode and object this routine decides what lock mode
235 * to use for lock #2:
237 * 1) if caller's gonna lookup in dir then we need to protect dir from
238 * being splitted only - LCK_CR
240 * 2) if caller's gonna modify dir then we need to protect dir from
241 * being splitted and to flush cache - LCK_CW
243 * 3) if caller's gonna modify dir and that dir seems ready for
244 * splitting then we need to protect it from any type of access
245 * (lookup/modify/split) - LCK_EX --bzzz
248 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
249 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
252 * Ask underlaying level its opinion about preferable PDO lock mode
253 * having access type passed as regular lock mode:
255 * - MDL_MINMODE means that lower layer does not want to specify lock
258 * - MDL_NL means that no PDO lock should be taken. This is used in some
259 * cases. Say, for non-splittable directories no need to use PDO locks
262 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
263 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
265 if (mode != MDL_MINMODE) {
266 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
269 * Lower layer does not want to specify locking mode. We do it
270 * our selves. No special protection is needed, just flush
271 * client's cache on modification and allow concurrent
274 switch (lh->mlh_reg_mode) {
276 lh->mlh_pdo_mode = LCK_EX;
279 lh->mlh_pdo_mode = LCK_CR;
282 lh->mlh_pdo_mode = LCK_CW;
285 CERROR("Not expected lock type (0x%x)\n",
286 (int)lh->mlh_reg_mode);
291 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
295 static int mdt_getstatus(struct mdt_thread_info *info)
297 struct mdt_device *mdt = info->mti_mdt;
298 struct md_device *next = mdt->mdt_child;
299 struct mdt_body *repbody;
304 rc = mdt_check_ucred(info);
306 RETURN(err_serious(rc));
308 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
309 RETURN(err_serious(-ENOMEM));
311 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
312 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
316 repbody->valid |= OBD_MD_FLID;
318 if (mdt->mdt_opts.mo_mds_capa &&
319 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
320 struct mdt_object *root;
321 struct lustre_capa *capa;
323 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
325 RETURN(PTR_ERR(root));
327 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
329 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
330 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
332 mdt_object_put(info->mti_env, root);
334 repbody->valid |= OBD_MD_FLMDSCAPA;
340 static int mdt_statfs(struct mdt_thread_info *info)
342 struct md_device *next = info->mti_mdt->mdt_child;
343 struct ptlrpc_service *svc;
344 struct obd_statfs *osfs;
349 svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
351 /* This will trigger a watchdog timeout */
352 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
353 (MDT_SERVICE_WATCHDOG_FACTOR *
354 at_get(&svc->srv_at_estimate)) + 1);
356 rc = mdt_check_ucred(info);
358 RETURN(err_serious(rc));
360 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
361 rc = err_serious(-ENOMEM);
363 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
364 rc = next->md_ops->mdo_statfs(info->mti_env, next,
366 statfs_pack(osfs, &info->mti_u.ksfs);
371 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
374 struct lu_attr *attr = &info->mti_attr.ma_attr;
376 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
378 /* Check if Size-on-MDS is enabled. */
379 if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
380 S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
381 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
382 b->size = attr->la_size;
383 b->blocks = attr->la_blocks;
387 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
388 const struct lu_attr *attr, const struct lu_fid *fid)
390 /*XXX should pack the reply body according to lu_valid*/
391 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
392 OBD_MD_FLGID | OBD_MD_FLTYPE |
393 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
394 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
396 if (!S_ISREG(attr->la_mode))
397 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
399 b->atime = attr->la_atime;
400 b->mtime = attr->la_mtime;
401 b->ctime = attr->la_ctime;
402 b->mode = attr->la_mode;
403 b->size = attr->la_size;
404 b->blocks = attr->la_blocks;
405 b->uid = attr->la_uid;
406 b->gid = attr->la_gid;
407 b->flags = attr->la_flags;
408 b->nlink = attr->la_nlink;
409 b->rdev = attr->la_rdev;
413 b->valid |= OBD_MD_FLID;
415 /* FIXME: these should be fixed when new igif ready.*/
416 b->ino = fid_oid(fid); /* 1.6 compatibility */
417 b->generation = fid_ver(fid); /* 1.6 compatibility */
418 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
420 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
421 PFID(fid), b->nlink, b->mode, b->size);
425 mdt_body_reverse_idmap(info, b);
428 static inline int mdt_body_has_lov(const struct lu_attr *la,
429 const struct mdt_body *body)
431 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
432 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
435 static int mdt_getattr_internal(struct mdt_thread_info *info,
436 struct mdt_object *o)
438 struct md_object *next = mdt_object_child(o);
439 const struct mdt_body *reqbody = info->mti_body;
440 struct ptlrpc_request *req = mdt_info_req(info);
441 struct md_attr *ma = &info->mti_attr;
442 struct lu_attr *la = &ma->ma_attr;
443 struct req_capsule *pill = info->mti_pill;
444 const struct lu_env *env = info->mti_env;
445 struct mdt_body *repbody;
446 struct lu_buf *buffer = &info->mti_buf;
450 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
451 RETURN(err_serious(-ENOMEM));
453 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
457 rc = mdt_object_exists(o);
459 /* This object is located on remote node.*/
460 repbody->fid1 = *mdt_object_fid(o);
461 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
465 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
466 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
468 /* If it is dir object and client require MEA, then we got MEA */
469 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
470 reqbody->valid & OBD_MD_MEA) {
471 /* Assumption: MDT_MD size is enough for lmv size. */
472 ma->ma_lmv = buffer->lb_buf;
473 ma->ma_lmv_size = buffer->lb_len;
474 ma->ma_need = MA_LMV | MA_INODE;
476 ma->ma_lmm = buffer->lb_buf;
477 ma->ma_lmm_size = buffer->lb_len;
478 ma->ma_need = MA_LOV | MA_INODE;
481 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
482 reqbody->valid & OBD_MD_FLDIREA &&
483 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
484 /* get default stripe info for this dir. */
485 ma->ma_need |= MA_LOV_DEF;
487 rc = mo_attr_get(env, next, ma);
489 CERROR("getattr error for "DFID": %d\n",
490 PFID(mdt_object_fid(o)), rc);
494 if (likely(ma->ma_valid & MA_INODE))
495 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
499 if (mdt_body_has_lov(la, reqbody)) {
500 if (ma->ma_valid & MA_LOV) {
501 LASSERT(ma->ma_lmm_size);
502 mdt_dump_lmm(D_INFO, ma->ma_lmm);
503 repbody->eadatasize = ma->ma_lmm_size;
504 if (S_ISDIR(la->la_mode))
505 repbody->valid |= OBD_MD_FLDIREA;
507 repbody->valid |= OBD_MD_FLEASIZE;
509 if (ma->ma_valid & MA_LMV) {
510 LASSERT(S_ISDIR(la->la_mode));
511 repbody->eadatasize = ma->ma_lmv_size;
512 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
514 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
515 repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
517 } else if (S_ISLNK(la->la_mode) &&
518 reqbody->valid & OBD_MD_LINKNAME) {
519 buffer->lb_buf = ma->ma_lmm;
520 buffer->lb_len = reqbody->eadatasize;
521 rc = mo_readlink(env, next, buffer);
522 if (unlikely(rc <= 0)) {
523 CERROR("readlink failed: %d\n", rc);
526 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
528 repbody->valid |= OBD_MD_LINKNAME;
529 repbody->eadatasize = rc;
531 ((char*)ma->ma_lmm)[rc - 1] = 0;
532 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
533 (char*)ma->ma_lmm, rc);
538 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
539 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
540 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
541 repbody->valid |= OBD_MD_FLMODEASIZE;
542 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
543 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
544 repbody->max_cookiesize);
547 if (exp_connect_rmtclient(info->mti_exp) &&
548 reqbody->valid & OBD_MD_FLRMTPERM) {
549 void *buf = req_capsule_server_get(pill, &RMF_ACL);
551 /* mdt_getattr_lock only */
552 rc = mdt_pack_remote_perm(info, o, buf);
554 repbody->valid &= ~OBD_MD_FLRMTPERM;
555 repbody->aclsize = 0;
558 repbody->valid |= OBD_MD_FLRMTPERM;
559 repbody->aclsize = sizeof(struct mdt_remote_perm);
562 #ifdef CONFIG_FS_POSIX_ACL
563 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
564 (reqbody->valid & OBD_MD_FLACL)) {
565 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
566 buffer->lb_len = req_capsule_get_size(pill,
567 &RMF_ACL, RCL_SERVER);
568 if (buffer->lb_len > 0) {
569 rc = mo_xattr_get(env, next, buffer,
570 XATTR_NAME_ACL_ACCESS);
572 if (rc == -ENODATA) {
573 repbody->aclsize = 0;
574 repbody->valid |= OBD_MD_FLACL;
576 } else if (rc == -EOPNOTSUPP) {
579 CERROR("got acl size: %d\n", rc);
582 repbody->aclsize = rc;
583 repbody->valid |= OBD_MD_FLACL;
590 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
591 info->mti_mdt->mdt_opts.mo_mds_capa &&
592 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
593 struct lustre_capa *capa;
595 capa = req_capsule_server_get(pill, &RMF_CAPA1);
597 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
598 rc = mo_capa_get(env, next, capa, 0);
601 repbody->valid |= OBD_MD_FLMDSCAPA;
606 static int mdt_renew_capa(struct mdt_thread_info *info)
608 struct mdt_object *obj = info->mti_object;
609 struct mdt_body *body;
610 struct lustre_capa *capa, *c;
614 /* if object doesn't exist, or server has disabled capability,
615 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
618 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
619 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
622 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
623 LASSERT(body != NULL);
625 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
628 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
632 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
634 body->valid |= OBD_MD_FLOSSCAPA;
638 static int mdt_getattr(struct mdt_thread_info *info)
640 struct mdt_object *obj = info->mti_object;
641 struct req_capsule *pill = info->mti_pill;
642 struct mdt_body *reqbody;
643 struct mdt_body *repbody;
649 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
652 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
653 rc = req_capsule_server_pack(pill);
655 RETURN(err_serious(rc));
656 rc = mdt_renew_capa(info);
657 GOTO(out_shrink, rc);
660 LASSERT(obj != NULL);
661 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
663 mode = lu_object_attr(&obj->mot_obj.mo_lu);
664 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
665 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
666 md_size = reqbody->eadatasize;
668 md_size = info->mti_mdt->mdt_max_mdsize;
670 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
672 rc = req_capsule_server_pack(pill);
673 if (unlikely(rc != 0))
674 RETURN(err_serious(rc));
676 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
677 LASSERT(repbody != NULL);
678 repbody->eadatasize = 0;
679 repbody->aclsize = 0;
681 if (reqbody->valid & OBD_MD_FLRMTPERM)
682 rc = mdt_init_ucred(info, reqbody);
684 rc = mdt_check_ucred(info);
686 GOTO(out_shrink, rc);
688 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
689 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
692 * Don't check capability at all, because rename might getattr for
693 * remote obj, and at that time no capability is available.
695 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
696 rc = mdt_getattr_internal(info, obj);
697 if (reqbody->valid & OBD_MD_FLRMTPERM)
698 mdt_exit_ucred(info);
701 mdt_shrink_reply(info);
705 static int mdt_is_subdir(struct mdt_thread_info *info)
707 struct mdt_object *o = info->mti_object;
708 struct req_capsule *pill = info->mti_pill;
709 const struct mdt_body *body = info->mti_body;
710 struct mdt_body *repbody;
716 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
719 * We save last checked parent fid to @repbody->fid1 for remote
722 LASSERT(fid_is_sane(&body->fid2));
723 LASSERT(mdt_object_exists(o) > 0);
724 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
725 &body->fid2, &repbody->fid1);
726 if (rc == 0 || rc == -EREMOTE)
727 repbody->valid |= OBD_MD_FLID;
732 static int mdt_raw_lookup(struct mdt_thread_info *info,
733 struct mdt_object *parent,
734 const struct lu_name *lname,
735 struct ldlm_reply *ldlm_rep)
737 struct md_object *next = mdt_object_child(info->mti_object);
738 const struct mdt_body *reqbody = info->mti_body;
739 struct lu_fid *child_fid = &info->mti_tmp_fid1;
740 struct mdt_body *repbody;
744 if (reqbody->valid != OBD_MD_FLID)
747 LASSERT(!info->mti_cross_ref);
749 /* Only got the fid of this obj by name */
750 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
753 /* XXX is raw_lookup possible as intent operation? */
756 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
759 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
761 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
764 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
765 repbody->fid1 = *child_fid;
766 repbody->valid = OBD_MD_FLID;
772 * UPDATE lock should be taken against parent, and be release before exit;
773 * child_bits lock should be taken against child, and be returned back:
774 * (1)normal request should release the child lock;
775 * (2)intent request will grant the lock to client.
777 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
778 struct mdt_lock_handle *lhc,
780 struct ldlm_reply *ldlm_rep)
782 struct ptlrpc_request *req = mdt_info_req(info);
783 struct mdt_body *reqbody = NULL;
784 struct mdt_object *parent = info->mti_object;
785 struct mdt_object *child;
786 struct md_object *next = mdt_object_child(parent);
787 struct lu_fid *child_fid = &info->mti_tmp_fid1;
788 struct lu_name *lname = NULL;
789 const char *name = NULL;
791 struct mdt_lock_handle *lhp;
792 struct ldlm_lock *lock;
793 struct ldlm_res_id *res_id;
799 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
800 LASSERT(ergo(is_resent,
801 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
803 LASSERT(parent != NULL);
804 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
806 RETURN(err_serious(-EFAULT));
808 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
810 if (!info->mti_cross_ref) {
812 * XXX: Check for "namelen == 0" is for getattr by fid
813 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
814 * that is the name must contain at least one character and
815 * the terminating '\0'
818 reqbody = req_capsule_client_get(info->mti_pill,
820 LASSERT(fid_is_sane(&reqbody->fid2));
823 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
825 PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
828 lname = mdt_name(info->mti_env, (char *)name, namelen);
829 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
830 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
834 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
836 rc = mdt_object_exists(parent);
837 if (unlikely(rc == 0)) {
838 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
839 &parent->mot_obj.mo_lu,
840 "Parent doesn't exist!\n");
842 } else if (!info->mti_cross_ref) {
843 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
844 PFID(mdt_object_fid(parent)));
847 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
855 if (info->mti_cross_ref) {
856 /* Only getattr on the child. Parent is on another node. */
857 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
859 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
860 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
863 /* Do not take lock for resent case. */
864 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
865 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
866 lhc->mlh_reg_lh.cookie);
867 LASSERT(fid_res_name_eq(mdt_object_fid(child),
868 &lock->l_resource->lr_name));
872 mdt_lock_handle_init(lhc);
873 mdt_lock_reg_init(lhc, LCK_PR);
876 * Object's name is on another MDS, no lookup lock is
877 * needed here but update is.
879 child_bits &= ~MDS_INODELOCK_LOOKUP;
880 child_bits |= MDS_INODELOCK_UPDATE;
882 rc = mdt_object_lock(info, child, lhc, child_bits,
886 /* Finally, we can get attr for child. */
887 mdt_set_capainfo(info, 0, mdt_object_fid(child),
889 rc = mdt_getattr_internal(info, child);
890 if (unlikely(rc != 0))
891 mdt_object_unlock(info, child, lhc, 1);
896 /* step 1: lock parent */
897 lhp = &info->mti_lh[MDT_LH_PARENT];
898 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
899 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
902 if (unlikely(rc != 0))
906 /* step 2: lookup child's fid by name */
907 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
912 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
913 GOTO(out_parent, rc);
915 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
917 *child_fid = reqbody->fid2;
918 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
922 *step 3: find the child object by fid & lock it.
923 * regardless if it is local or remote.
925 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
927 if (unlikely(IS_ERR(child)))
928 GOTO(out_parent, rc = PTR_ERR(child));
930 /* Do not take lock for resent case. */
931 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
932 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
933 lhc->mlh_reg_lh.cookie);
935 res_id = &lock->l_resource->lr_name;
936 if (!fid_res_name_eq(mdt_object_fid(child),
937 &lock->l_resource->lr_name)) {
938 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
939 &lock->l_resource->lr_name),
940 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
941 (unsigned long)res_id->name[0],
942 (unsigned long)res_id->name[1],
943 (unsigned long)res_id->name[2],
944 PFID(mdt_object_fid(parent)));
945 CWARN("Although resent, but still not get child lock"
946 "parent:"DFID" child:"DFID"\n",
947 PFID(mdt_object_fid(parent)),
948 PFID(mdt_object_fid(child)));
949 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
958 ma = &info->mti_attr;
960 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
961 mdt_lock_handle_init(lhc);
962 mdt_lock_reg_init(lhc, LCK_PR);
964 if (mdt_object_exists(child) == 0) {
965 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
966 &child->mot_obj.mo_lu,
967 "Object doesn't exist!\n");
968 GOTO(out_child, rc = -ESTALE);
972 ma->ma_need = MA_INODE;
973 rc = mo_attr_get(info->mti_env, next, ma);
974 if (unlikely(rc != 0))
977 /* If the file has not been changed for some time, we return
978 * not only a LOOKUP lock, but also an UPDATE lock and this
979 * might save us RPC on later STAT. For directories, it also
980 * let negative dentry starts working for this dir. */
981 if (ma->ma_valid & MA_INODE &&
982 ma->ma_attr.la_valid & LA_CTIME &&
983 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
984 ma->ma_attr.la_ctime < cfs_time_current_sec())
985 child_bits |= MDS_INODELOCK_UPDATE;
987 rc = mdt_object_lock(info, child, lhc, child_bits,
990 if (unlikely(rc != 0))
994 /* finally, we can get attr for child. */
995 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
996 rc = mdt_getattr_internal(info, child);
997 if (unlikely(rc != 0)) {
998 mdt_object_unlock(info, child, lhc, 1);
1000 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1002 struct mdt_body *repbody;
1004 /* Debugging code. */
1005 res_id = &lock->l_resource->lr_name;
1006 LDLM_DEBUG(lock, "Returning lock to client");
1007 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1008 &lock->l_resource->lr_name),
1009 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1010 (unsigned long)res_id->name[0],
1011 (unsigned long)res_id->name[1],
1012 (unsigned long)res_id->name[2],
1013 PFID(mdt_object_fid(child)));
1015 * Pack Size-on-MDS inode attributes to the body if
1016 * update lock is given.
1018 repbody = req_capsule_server_get(info->mti_pill,
1020 if (lock->l_policy_data.l_inodebits.bits &
1021 MDS_INODELOCK_UPDATE)
1022 mdt_pack_size2body(info, child);
1023 LDLM_LOCK_PUT(lock);
1028 mdt_object_put(info->mti_env, child);
1030 mdt_object_unlock(info, parent, lhp, 1);
1034 /* normal handler: should release the child lock */
1035 static int mdt_getattr_name(struct mdt_thread_info *info)
1037 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1038 struct mdt_body *reqbody;
1039 struct mdt_body *repbody;
1043 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1044 LASSERT(reqbody != NULL);
1045 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1046 LASSERT(repbody != NULL);
1048 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1049 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1050 repbody->eadatasize = 0;
1051 repbody->aclsize = 0;
1053 rc = mdt_init_ucred(info, reqbody);
1055 GOTO(out_shrink, rc);
1057 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1058 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1059 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1060 lhc->mlh_reg_lh.cookie = 0;
1062 mdt_exit_ucred(info);
1065 mdt_shrink_reply(info);
1069 static const struct lu_device_operations mdt_lu_ops;
1071 static int lu_device_is_mdt(struct lu_device *d)
1073 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1076 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1077 void *karg, void *uarg);
1079 static int mdt_set_info(struct mdt_thread_info *info)
1081 struct ptlrpc_request *req = mdt_info_req(info);
1084 int keylen, vallen, rc = 0;
1087 rc = req_capsule_server_pack(info->mti_pill);
1091 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1093 DEBUG_REQ(D_HA, req, "no set_info key");
1097 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1100 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1102 DEBUG_REQ(D_HA, req, "no set_info val");
1106 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1109 /* Swab any part of val you need to here */
1110 if (KEY_IS(KEY_READ_ONLY)) {
1112 lustre_msg_set_status(req->rq_repmsg, 0);
1114 spin_lock(&req->rq_export->exp_lock);
1116 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1118 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1119 spin_unlock(&req->rq_export->exp_lock);
1121 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1122 struct changelog_setinfo *cs =
1123 (struct changelog_setinfo *)val;
1124 if (vallen != sizeof(*cs)) {
1125 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1128 if (ptlrpc_req_need_swab(req)) {
1129 __swab64s(&cs->cs_recno);
1130 __swab32s(&cs->cs_id);
1133 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1135 lustre_msg_set_status(req->rq_repmsg, rc);
1143 static int mdt_connect(struct mdt_thread_info *info)
1146 struct ptlrpc_request *req;
1148 req = mdt_info_req(info);
1149 rc = target_handle_connect(req);
1151 LASSERT(req->rq_export != NULL);
1152 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1153 rc = mdt_init_sec_level(info);
1155 rc = mdt_init_idmap(info);
1157 obd_disconnect(class_export_get(req->rq_export));
1159 rc = err_serious(rc);
1164 static int mdt_disconnect(struct mdt_thread_info *info)
1169 rc = target_handle_disconnect(mdt_info_req(info));
1171 rc = err_serious(rc);
1175 static int mdt_sendpage(struct mdt_thread_info *info,
1176 struct lu_rdpg *rdpg)
1178 struct ptlrpc_request *req = mdt_info_req(info);
1179 struct obd_export *exp = req->rq_export;
1180 struct ptlrpc_bulk_desc *desc;
1181 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1189 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1194 for (i = 0, tmpcount = rdpg->rp_count;
1195 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1196 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1197 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1200 LASSERT(desc->bd_nob == rdpg->rp_count);
1201 rc = sptlrpc_svc_wrap_bulk(req, desc);
1203 GOTO(free_desc, rc);
1205 rc = ptlrpc_start_bulk_transfer(desc);
1207 GOTO(free_desc, rc);
1209 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1210 GOTO(abort_bulk, rc = 0);
1212 timeout = (int) req->rq_deadline - cfs_time_current_sec();
1214 CERROR("Req deadline already passed %lu (now: %lu)\n",
1215 req->rq_deadline, cfs_time_current_sec());
1216 *lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(max(timeout, 1)),
1217 cfs_time_seconds(1), NULL, NULL);
1218 rc = l_wait_event(desc->bd_waitq, !ptlrpc_server_bulk_active(desc) ||
1219 exp->exp_failed || exp->exp_abort_active_req, lwi);
1220 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1223 if (desc->bd_success &&
1224 desc->bd_nob_transferred == rdpg->rp_count)
1225 GOTO(free_desc, rc);
1228 if (exp->exp_abort_active_req || exp->exp_failed)
1229 GOTO(abort_bulk, rc);
1232 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1233 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1234 desc->bd_nob_transferred, rdpg->rp_count,
1235 exp->exp_client_uuid.uuid,
1236 exp->exp_connection->c_remote_uuid.uuid);
1238 class_fail_export(exp);
1242 ptlrpc_abort_bulk(desc);
1244 ptlrpc_free_bulk(desc);
1248 #ifdef HAVE_SPLIT_SUPPORT
1250 * Retrieve dir entry from the page and insert it to the slave object, actually,
1251 * this should be in osd layer, but since it will not in the final product, so
1252 * just do it here and do not define more moo api anymore for this.
1254 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1257 struct mdt_object *object = info->mti_object;
1258 struct lu_fid *lf = &info->mti_tmp_fid2;
1259 struct md_attr *ma = &info->mti_attr;
1260 struct lu_dirpage *dp;
1261 struct lu_dirent *ent;
1262 int rc = 0, offset = 0;
1265 /* Make sure we have at least one entry. */
1270 * Disable trans for this name insert, since it will include many trans
1273 info->mti_no_need_trans = 1;
1275 * When write_dir_page, no need update parent's ctime,
1276 * and no permission check for name_insert.
1278 ma->ma_attr.la_ctime = 0;
1279 ma->ma_attr.la_valid = LA_MODE;
1280 ma->ma_valid = MA_INODE;
1283 dp = page_address(page);
1284 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1286 for (ent = lu_dirent_start(dp); ent != NULL;
1287 ent = lu_dirent_next(ent)) {
1288 struct lu_name *lname;
1291 if (le16_to_cpu(ent->lde_namelen) == 0)
1294 fid_le_to_cpu(lf, &ent->lde_fid);
1295 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1296 ma->ma_attr.la_mode = S_IFDIR;
1298 ma->ma_attr.la_mode = 0;
1299 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1301 GOTO(out, rc = -ENOMEM);
1303 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1304 lname = mdt_name(info->mti_env, name,
1305 le16_to_cpu(ent->lde_namelen));
1306 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1307 rc = mdo_name_insert(info->mti_env,
1308 md_object_next(&object->mot_obj),
1310 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1312 CERROR("Can't insert %*.*s, rc %d\n",
1313 le16_to_cpu(ent->lde_namelen),
1314 le16_to_cpu(ent->lde_namelen),
1319 offset += lu_dirent_size(ent);
1329 static int mdt_bulk_timeout(void *data)
1333 CERROR("mdt bulk transfer timeout \n");
1338 static int mdt_writepage(struct mdt_thread_info *info)
1340 struct ptlrpc_request *req = mdt_info_req(info);
1341 struct mdt_body *reqbody;
1342 struct l_wait_info *lwi;
1343 struct ptlrpc_bulk_desc *desc;
1349 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1350 if (reqbody == NULL)
1351 RETURN(err_serious(-EFAULT));
1353 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1355 RETURN(err_serious(-ENOMEM));
1357 /* allocate the page for the desc */
1358 page = cfs_alloc_page(CFS_ALLOC_STD);
1360 GOTO(desc_cleanup, rc = -ENOMEM);
1362 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1363 (int)reqbody->size, (int)reqbody->nlink);
1365 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1366 (int)reqbody->nlink);
1368 rc = sptlrpc_svc_prep_bulk(req, desc);
1370 GOTO(cleanup_page, rc);
1372 * Check if client was evicted while we were doing i/o before touching
1377 GOTO(cleanup_page, rc = -ENOMEM);
1379 if (desc->bd_export->exp_failed)
1382 rc = ptlrpc_start_bulk_transfer (desc);
1384 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1385 mdt_bulk_timeout, desc);
1386 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1387 desc->bd_export->exp_failed, lwi);
1388 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1389 if (rc == -ETIMEDOUT) {
1390 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1391 ptlrpc_abort_bulk(desc);
1392 } else if (desc->bd_export->exp_failed) {
1393 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1395 ptlrpc_abort_bulk(desc);
1396 } else if (!desc->bd_success ||
1397 desc->bd_nob_transferred != desc->bd_nob) {
1398 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1400 "truncated" : "network error on",
1401 desc->bd_nob_transferred, desc->bd_nob);
1402 /* XXX should this be a different errno? */
1406 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1409 GOTO(cleanup_lwi, rc);
1410 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1415 cfs_free_page(page);
1417 ptlrpc_free_bulk(desc);
1422 static int mdt_readpage(struct mdt_thread_info *info)
1424 struct mdt_object *object = info->mti_object;
1425 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1426 struct mdt_body *reqbody;
1427 struct mdt_body *repbody;
1432 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1433 RETURN(err_serious(-ENOMEM));
1435 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1436 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1437 if (reqbody == NULL || repbody == NULL)
1438 RETURN(err_serious(-EFAULT));
1441 * prepare @rdpg before calling lower layers and transfer itself. Here
1442 * reqbody->size contains offset of where to start to read and
1443 * reqbody->nlink contains number bytes to read.
1445 rdpg->rp_hash = reqbody->size;
1446 if (rdpg->rp_hash != reqbody->size) {
1447 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1448 rdpg->rp_hash, reqbody->size);
1452 rdpg->rp_attrs = reqbody->mode;
1453 rdpg->rp_count = reqbody->nlink;
1454 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1455 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1456 if (rdpg->rp_pages == NULL)
1459 for (i = 0; i < rdpg->rp_npages; ++i) {
1460 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1461 if (rdpg->rp_pages[i] == NULL)
1462 GOTO(free_rdpg, rc = -ENOMEM);
1465 /* call lower layers to fill allocated pages with directory data */
1466 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1468 GOTO(free_rdpg, rc);
1470 /* send pages to client */
1471 rc = mdt_sendpage(info, rdpg);
1476 for (i = 0; i < rdpg->rp_npages; i++)
1477 if (rdpg->rp_pages[i] != NULL)
1478 cfs_free_page(rdpg->rp_pages[i]);
1479 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1481 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1487 static int mdt_reint_internal(struct mdt_thread_info *info,
1488 struct mdt_lock_handle *lhc,
1491 struct req_capsule *pill = info->mti_pill;
1492 struct mdt_device *mdt = info->mti_mdt;
1493 struct mdt_body *repbody;
1498 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1499 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1500 mdt->mdt_max_mdsize);
1501 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1502 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1503 mdt->mdt_max_cookiesize);
1505 rc = req_capsule_server_pack(pill);
1507 CERROR("Can't pack response, rc %d\n", rc);
1508 RETURN(err_serious(rc));
1511 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1512 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1514 repbody->eadatasize = 0;
1515 repbody->aclsize = 0;
1518 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1519 GOTO(out_shrink, rc = err_serious(-EFAULT));
1521 rc = mdt_reint_unpack(info, op);
1523 CERROR("Can't unpack reint, rc %d\n", rc);
1524 GOTO(out_shrink, rc = err_serious(rc));
1527 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1529 /* for replay no cookkie / lmm need, because client have this already */
1530 if (info->mti_spec.no_create == 1) {
1531 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1532 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1534 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1535 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1539 rc = mdt_init_ucred_reint(info);
1541 GOTO(out_shrink, rc);
1543 rc = mdt_fix_attr_ucred(info, op);
1545 GOTO(out_ucred, rc = err_serious(rc));
1547 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1548 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1549 GOTO(out_ucred, rc);
1551 rc = mdt_reint_rec(info, lhc);
1554 mdt_exit_ucred(info);
1556 mdt_shrink_reply(info);
1560 static long mdt_reint_opcode(struct mdt_thread_info *info,
1561 const struct req_format **fmt)
1563 struct mdt_rec_reint *rec;
1566 opc = err_serious(-EFAULT);
1567 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1569 opc = rec->rr_opcode;
1570 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1571 if (opc < REINT_MAX && fmt[opc] != NULL)
1572 req_capsule_extend(info->mti_pill, fmt[opc]);
1574 CERROR("Unsupported opc: %ld\n", opc);
1575 opc = err_serious(opc);
1581 static int mdt_reint(struct mdt_thread_info *info)
1586 static const struct req_format *reint_fmts[REINT_MAX] = {
1587 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1588 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1589 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1590 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1591 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1592 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1593 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1598 opc = mdt_reint_opcode(info, reint_fmts);
1601 * No lock possible here from client to pass it to reint code
1604 rc = mdt_reint_internal(info, NULL, opc);
1609 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1613 /* this should sync the whole device */
1614 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1616 struct dt_device *dt = mdt->mdt_bottom;
1620 rc = dt->dd_ops->dt_sync(env, dt);
1624 /* this should sync this object */
1625 static int mdt_object_sync(struct mdt_thread_info *info)
1627 struct md_object *next;
1631 if (!mdt_object_exists(info->mti_object)) {
1632 CWARN("Non existing object "DFID"!\n",
1633 PFID(mdt_object_fid(info->mti_object)));
1636 next = mdt_object_child(info->mti_object);
1637 rc = mo_object_sync(info->mti_env, next);
1642 static int mdt_sync(struct mdt_thread_info *info)
1644 struct req_capsule *pill = info->mti_pill;
1645 struct mdt_body *body;
1649 /* The fid may be zero, so we req_capsule_set manually */
1650 req_capsule_set(pill, &RQF_MDS_SYNC);
1652 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1654 RETURN(err_serious(-EINVAL));
1656 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1657 RETURN(err_serious(-ENOMEM));
1659 if (fid_seq(&body->fid1) == 0) {
1660 /* sync the whole device */
1661 rc = req_capsule_server_pack(pill);
1663 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1665 rc = err_serious(rc);
1667 /* sync an object */
1668 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1670 rc = mdt_object_sync(info);
1672 struct md_object *next;
1673 const struct lu_fid *fid;
1674 struct lu_attr *la = &info->mti_attr.ma_attr;
1676 next = mdt_object_child(info->mti_object);
1677 info->mti_attr.ma_need = MA_INODE;
1678 info->mti_attr.ma_valid = 0;
1679 rc = mo_attr_get(info->mti_env, next,
1682 body = req_capsule_server_get(pill,
1684 fid = mdt_object_fid(info->mti_object);
1685 mdt_pack_attr2body(info, body, la, fid);
1689 rc = err_serious(rc);
1694 #ifdef HAVE_QUOTA_SUPPORT
1695 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1697 struct obd_quotactl *oqctl;
1698 struct req_capsule *pill = info->mti_pill;
1699 struct obd_export *exp = info->mti_exp;
1700 struct md_device *next = info->mti_mdt->mdt_child;
1704 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1708 /* remote client has no permission for quotacheck */
1709 if (unlikely(exp_connect_rmtclient(exp)))
1712 rc = req_capsule_server_pack(pill);
1716 rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next, exp,
1721 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1723 struct obd_quotactl *oqctl, *repoqc;
1724 struct req_capsule *pill = info->mti_pill;
1725 struct obd_export *exp = info->mti_exp;
1726 struct md_device *next = info->mti_mdt->mdt_child;
1727 const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1731 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1736 if (exp_connect_rmtclient(exp)) {
1737 struct ptlrpc_request *req = mdt_info_req(info);
1738 struct mdt_export_data *med = mdt_req2med(req);
1739 struct lustre_idmap_table *idmap = med->med_idmap;
1741 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1742 oqctl->qc_cmd != Q_GETINFO))
1746 if (oqctl->qc_type == USRQUOTA)
1747 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1749 else if (oqctl->qc_type == GRPQUOTA)
1750 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1755 if (id == CFS_IDMAP_NOTFOUND) {
1756 CDEBUG(D_QUOTA, "no mapping for id %u\n",
1762 rc = req_capsule_server_pack(pill);
1766 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1767 LASSERT(repoqc != NULL);
1769 switch (oqctl->qc_cmd) {
1771 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1774 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1777 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1781 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1785 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1789 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1793 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1797 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1800 case LUSTRE_Q_INVALIDATE:
1801 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1803 case LUSTRE_Q_FINVALIDATE:
1804 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1807 CERROR("unsupported mdt_quotactl command: %d\n",
1819 * OBD PING and other handlers.
1821 static int mdt_obd_ping(struct mdt_thread_info *info)
1826 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1828 rc = target_handle_ping(mdt_info_req(info));
1830 rc = err_serious(rc);
1834 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1836 return err_serious(-EOPNOTSUPP);
1839 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1841 return err_serious(-EOPNOTSUPP);
1849 /** clone llog ctxt from child (mdd)
1850 * This allows remote llog (replicator) access.
1851 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1852 * context was originally set up, or we can handle them directly.
1853 * I choose the latter, but that means I need any llog
1854 * contexts set up by child to be accessable by the mdt. So we clone the
1855 * context into our context list here.
1857 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1860 struct md_device *next = mdt->mdt_child;
1861 struct llog_ctxt *ctxt;
1864 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1867 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1868 if (rc || ctxt == NULL) {
1869 CERROR("Can't get mdd ctxt %d\n", rc);
1873 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1875 CERROR("Can't set mdt ctxt %d\n", rc);
1880 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1881 struct mdt_device *mdt, int idx)
1883 struct llog_ctxt *ctxt;
1885 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1888 /* Put once for the get we just did, and once for the clone */
1889 llog_ctxt_put(ctxt);
1890 llog_ctxt_put(ctxt);
1894 static int mdt_llog_create(struct mdt_thread_info *info)
1898 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1899 rc = llog_origin_handle_create(mdt_info_req(info));
1900 return (rc < 0 ? err_serious(rc) : rc);
1903 static int mdt_llog_destroy(struct mdt_thread_info *info)
1907 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1908 rc = llog_origin_handle_destroy(mdt_info_req(info));
1909 return (rc < 0 ? err_serious(rc) : rc);
1912 static int mdt_llog_read_header(struct mdt_thread_info *info)
1916 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1917 rc = llog_origin_handle_read_header(mdt_info_req(info));
1918 return (rc < 0 ? err_serious(rc) : rc);
1921 static int mdt_llog_next_block(struct mdt_thread_info *info)
1925 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1926 rc = llog_origin_handle_next_block(mdt_info_req(info));
1927 return (rc < 0 ? err_serious(rc) : rc);
1930 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1934 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1935 rc = llog_origin_handle_prev_block(mdt_info_req(info));
1936 return (rc < 0 ? err_serious(rc) : rc);
1943 static struct ldlm_callback_suite cbs = {
1944 .lcs_completion = ldlm_server_completion_ast,
1945 .lcs_blocking = ldlm_server_blocking_ast,
1949 static int mdt_enqueue(struct mdt_thread_info *info)
1951 struct ptlrpc_request *req;
1956 * info->mti_dlm_req already contains swapped and (if necessary)
1957 * converted dlm request.
1959 LASSERT(info->mti_dlm_req != NULL);
1961 req = mdt_info_req(info);
1964 * Lock without inodebits makes no sense and will oops later in
1965 * ldlm. Let's check it now to see if we have wrong lock from client or
1966 * bits get corrupted somewhere in mdt_intent_policy().
1968 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1969 /* This is disabled because we need to support liblustre flock.
1970 * LASSERT(req_bits != 0);
1973 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1974 req, info->mti_dlm_req, &cbs);
1975 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1976 return rc ? err_serious(rc) : req->rq_status;
1979 static int mdt_convert(struct mdt_thread_info *info)
1982 struct ptlrpc_request *req;
1984 LASSERT(info->mti_dlm_req);
1985 req = mdt_info_req(info);
1986 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1987 return rc ? err_serious(rc) : req->rq_status;
1990 static int mdt_bl_callback(struct mdt_thread_info *info)
1992 CERROR("bl callbacks should not happen on MDS\n");
1994 return err_serious(-EOPNOTSUPP);
1997 static int mdt_cp_callback(struct mdt_thread_info *info)
1999 CERROR("cp callbacks should not happen on MDS\n");
2001 return err_serious(-EOPNOTSUPP);
2005 * sec context handlers
2007 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2011 rc = mdt_handle_idmap(info);
2014 struct ptlrpc_request *req = mdt_info_req(info);
2017 opc = lustre_msg_get_opc(req->rq_reqmsg);
2018 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2019 sptlrpc_svc_ctx_invalidate(req);
2022 OBD_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, obd_fail_val);
2027 static struct mdt_object *mdt_obj(struct lu_object *o)
2029 LASSERT(lu_device_is_mdt(o->lo_dev));
2030 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2033 struct mdt_object *mdt_object_find(const struct lu_env *env,
2034 struct mdt_device *d,
2035 const struct lu_fid *f)
2037 struct lu_object *o;
2038 struct mdt_object *m;
2041 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2042 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2043 if (unlikely(IS_ERR(o)))
2044 m = (struct mdt_object *)o;
2051 * Asyncronous commit for mdt device.
2053 * Pass asynchonous commit call down the MDS stack.
2055 * \param env environment
2056 * \param mdt the mdt device
2058 static void mdt_device_commit_async(const struct lu_env *env,
2059 struct mdt_device *mdt)
2061 struct dt_device *dt = mdt->mdt_bottom;
2064 rc = dt->dd_ops->dt_commit_async(env, dt);
2065 if (unlikely(rc != 0))
2066 CWARN("async commit start failed with rc = %d", rc);
2070 * Mark the lock as "synchonous".
2072 * Mark the lock to deffer transaction commit to the unlock time.
2074 * \param lock the lock to mark as "synchonous"
2076 * \see mdt_is_lock_sync
2077 * \see mdt_save_lock
2079 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2081 lock->l_ast_data = (void*)1;
2085 * Check whehter the lock "synchonous" or not.
2087 * \param lock the lock to check
2088 * \retval 1 the lock is "synchonous"
2089 * \retval 0 the lock isn't "synchronous"
2091 * \see mdt_set_lock_sync
2092 * \see mdt_save_lock
2094 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2096 return lock->l_ast_data != NULL;
2100 * Blocking AST for mdt locks.
2102 * Starts transaction commit if in case of COS lock conflict or
2103 * deffers such a commit to the mdt_save_lock.
2105 * \param lock the lock which blocks a request or cancelling lock
2106 * \param desc unused
2107 * \param data unused
2108 * \param flag indicates whether this cancelling or blocking callback
2110 * \see ldlm_blocking_ast_nocheck
2112 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2113 void *data, int flag)
2115 struct obd_device *obd = lock->l_resource->lr_namespace->ns_obd;
2116 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2120 if (flag == LDLM_CB_CANCELING)
2122 lock_res_and_lock(lock);
2123 if (lock->l_blocking_ast != mdt_blocking_ast) {
2124 unlock_res_and_lock(lock);
2127 if (mdt_cos_is_enabled(mdt) &&
2128 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2129 lock->l_blocking_lock != NULL &&
2130 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2131 mdt_set_lock_sync(lock);
2133 rc = ldlm_blocking_ast_nocheck(lock);
2135 /* There is no lock conflict if l_blocking_lock == NULL,
2136 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2137 * when the last reference to a local lock was released */
2138 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2141 rc = lu_env_init(&env, LCT_MD_THREAD);
2142 if (unlikely(rc != 0))
2143 CWARN("lu_env initialization failed with rc = %d,"
2144 "cannot start asynchronous commit\n", rc);
2146 mdt_device_commit_async(&env, mdt);
2152 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2153 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2155 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2156 ldlm_policy_data_t *policy = &info->mti_policy;
2157 struct ldlm_res_id *res_id = &info->mti_res_id;
2161 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2162 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2163 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2164 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2166 if (mdt_object_exists(o) < 0) {
2167 if (locality == MDT_CROSS_LOCK) {
2168 /* cross-ref object fix */
2169 ibits &= ~MDS_INODELOCK_UPDATE;
2170 ibits |= MDS_INODELOCK_LOOKUP;
2172 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2173 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2175 /* No PDO lock on remote object */
2176 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2179 if (lh->mlh_type == MDT_PDO_LOCK) {
2180 /* check for exists after object is locked */
2181 if (mdt_object_exists(o) == 0) {
2182 /* Non-existent object shouldn't have PDO lock */
2185 /* Non-dir object shouldn't have PDO lock */
2186 LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
2190 memset(policy, 0, sizeof(*policy));
2191 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2194 * Take PDO lock on whole directory and build correct @res_id for lock
2195 * on part of directory.
2197 if (lh->mlh_pdo_hash != 0) {
2198 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2199 mdt_lock_pdo_mode(info, o, lh);
2200 if (lh->mlh_pdo_mode != LCK_NL) {
2202 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2203 * is never going to be sent to client and we do not
2204 * want it slowed down due to possible cancels.
2206 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2207 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2208 policy, res_id, LDLM_FL_ATOMIC_CB,
2209 &info->mti_exp->exp_handle.h_cookie);
2215 * Finish res_id initializing by name hash marking part of
2216 * directory which is taking modification.
2218 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2221 policy->l_inodebits.bits = ibits;
2224 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2225 * going to be sent to client. If it is - mdt_intent_policy() path will
2226 * fix it up and turn FL_LOCAL flag off.
2228 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2229 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2230 &info->mti_exp->exp_handle.h_cookie);
2236 mdt_object_unlock(info, o, lh, 1);
2243 * Save a lock within request object.
2245 * Keep the lock referenced until whether client ACK or transaction
2246 * commit happens or release the lock immediately depending on input
2247 * parameters. If COS is ON, a write lock is converted to COS lock
2250 * \param info thead info object
2251 * \param h lock handle
2252 * \param mode lock mode
2253 * \param decref force immediate lock releasing
2256 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2257 ldlm_mode_t mode, int decref)
2261 if (lustre_handle_is_used(h)) {
2262 if (decref || !info->mti_has_trans ||
2263 !(mode & (LCK_PW | LCK_EX))){
2264 mdt_fid_unlock(h, mode);
2266 struct mdt_device *mdt = info->mti_mdt;
2267 struct ldlm_lock *lock = ldlm_handle2lock(h);
2268 struct ptlrpc_request *req = mdt_info_req(info);
2271 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2273 CDEBUG(D_HA, "request = %p reply state = %p"
2274 " transno = "LPD64"\n",
2275 req, req->rq_reply_state, req->rq_transno);
2276 if (mdt_cos_is_enabled(mdt)) {
2278 ldlm_lock_downgrade(lock, LCK_COS);
2281 ptlrpc_save_lock(req, h, mode, no_ack);
2282 if (mdt_is_lock_sync(lock)) {
2283 CDEBUG(D_HA, "found sync-lock,"
2284 " async commit started\n");
2285 mdt_device_commit_async(info->mti_env,
2288 LDLM_LOCK_PUT(lock);
2297 * Unlock mdt object.
2299 * Immeditely release the regular lock and the PDO lock or save the
2300 * lock in reqeuest and keep them referenced until client ACK or
2301 * transaction commit.
2303 * \param info thread info object
2304 * \param o mdt object
2305 * \param lh mdt lock handle referencing regular and PDO locks
2306 * \param decref force immediate lock releasing
2308 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2309 struct mdt_lock_handle *lh, int decref)
2313 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2314 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2319 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2320 const struct lu_fid *f,
2321 struct mdt_lock_handle *lh,
2324 struct mdt_object *o;
2326 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2330 rc = mdt_object_lock(info, o, lh, ibits,
2333 mdt_object_put(info->mti_env, o);
2340 void mdt_object_unlock_put(struct mdt_thread_info * info,
2341 struct mdt_object * o,
2342 struct mdt_lock_handle *lh,
2345 mdt_object_unlock(info, o, lh, decref);
2346 mdt_object_put(info->mti_env, o);
2349 static struct mdt_handler *mdt_handler_find(__u32 opc,
2350 struct mdt_opc_slice *supported)
2352 struct mdt_opc_slice *s;
2353 struct mdt_handler *h;
2356 for (s = supported; s->mos_hs != NULL; s++) {
2357 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2358 h = s->mos_hs + (opc - s->mos_opc_start);
2359 if (likely(h->mh_opc != 0))
2360 LASSERTF(h->mh_opc == opc,
2361 "opcode mismatch %d != %d\n",
2364 h = NULL; /* unsupported opc */
2371 static int mdt_lock_resname_compat(struct mdt_device *m,
2372 struct ldlm_request *req)
2374 /* XXX something... later. */
2378 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2380 /* XXX something... later. */
2385 * Generic code handling requests that have struct mdt_body passed in:
2387 * - extract mdt_body from request and save it in @info, if present;
2389 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2392 * - if HABEO_CORPUS flag is set for this request type check whether object
2393 * actually exists on storage (lu_object_exists()).
2396 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2398 const struct mdt_body *body;
2399 struct mdt_object *obj;
2400 const struct lu_env *env;
2401 struct req_capsule *pill;
2405 env = info->mti_env;
2406 pill = info->mti_pill;
2408 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2412 if (!(body->valid & OBD_MD_FLID))
2415 if (!fid_is_sane(&body->fid1)) {
2416 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2421 * Do not get size or any capa fields before we check that request
2422 * contains capa actually. There are some requests which do not, for
2423 * instance MDS_IS_SUBDIR.
2425 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2426 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2427 mdt_set_capainfo(info, 0, &body->fid1,
2428 req_capsule_client_get(pill, &RMF_CAPA1));
2430 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2432 if ((flags & HABEO_CORPUS) &&
2433 !mdt_object_exists(obj)) {
2434 mdt_object_put(env, obj);
2435 /* for capability renew ENOENT will be handled in
2437 if (body->valid & OBD_MD_FLOSSCAPA)
2442 info->mti_object = obj;
2451 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2453 struct req_capsule *pill = info->mti_pill;
2457 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2458 rc = mdt_body_unpack(info, flags);
2462 if (rc == 0 && (flags & HABEO_REFERO)) {
2463 struct mdt_device *mdt = info->mti_mdt;
2467 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2468 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2469 mdt->mdt_max_mdsize);
2470 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2471 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2472 mdt->mdt_max_cookiesize);
2474 rc = req_capsule_server_pack(pill);
2479 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2481 struct md_device *next = m->mdt_child;
2483 return next->md_ops->mdo_init_capa_ctxt(env, next,
2484 m->mdt_opts.mo_mds_capa,
2485 m->mdt_capa_timeout,
2491 * Invoke handler for this request opc. Also do necessary preprocessing
2492 * (according to handler ->mh_flags), and post-processing (setting of
2493 * ->last_{xid,committed}).
2495 static int mdt_req_handle(struct mdt_thread_info *info,
2496 struct mdt_handler *h, struct ptlrpc_request *req)
2498 int rc, serious = 0;
2503 LASSERT(h->mh_act != NULL);
2504 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2505 LASSERT(current->journal_info == NULL);
2508 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2509 * to put same checks into handlers like mdt_close(), mdt_reint(),
2510 * etc., without talking to mdt authors first. Checking same thing
2511 * there again is useless and returning 0 error without packing reply
2512 * is buggy! Handlers either pack reply or return error.
2514 * We return 0 here and do not send any reply in order to emulate
2515 * network failure. Do not send any reply in case any of NET related
2516 * fail_id has occured.
2518 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2522 flags = h->mh_flags;
2523 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2525 if (h->mh_fmt != NULL) {
2526 req_capsule_set(info->mti_pill, h->mh_fmt);
2527 rc = mdt_unpack_req_pack_rep(info, flags);
2530 if (rc == 0 && flags & MUTABOR &&
2531 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2532 /* should it be rq_status? */
2535 if (rc == 0 && flags & HABEO_CLAVIS) {
2536 struct ldlm_request *dlm_req;
2538 LASSERT(h->mh_fmt != NULL);
2540 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2541 if (dlm_req != NULL) {
2542 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2543 rc = mdt_lock_resname_compat(info->mti_mdt,
2545 info->mti_dlm_req = dlm_req;
2551 /* capability setting changed via /proc, needs reinitialize ctxt */
2552 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2553 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2554 info->mti_mdt->mdt_capa_conf = 0;
2557 if (likely(rc == 0)) {
2559 * Process request, there can be two types of rc:
2560 * 1) errors with msg unpack/pack, other failures outside the
2561 * operation itself. This is counted as serious errors;
2562 * 2) errors during fs operation, should be placed in rq_status
2565 rc = h->mh_act(info);
2567 !req->rq_no_reply && req->rq_reply_state == NULL) {
2568 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2569 "pack reply and returned 0 error\n",
2573 serious = is_serious(rc);
2574 rc = clear_serious(rc);
2578 req->rq_status = rc;
2581 * ELDLM_* codes which > 0 should be in rq_status only as well as
2582 * all non-serious errors.
2584 if (rc > 0 || !serious)
2587 LASSERT(current->journal_info == NULL);
2589 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2590 info->mti_mdt->mdt_opts.mo_compat_resname) {
2591 struct ldlm_reply *dlmrep;
2593 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2595 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2598 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2599 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2600 target_committed_to_req(req);
2602 if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2603 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2604 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2608 target_send_reply(req, rc, info->mti_fail_id);
2612 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2614 lh->mlh_type = MDT_NUL_LOCK;
2615 lh->mlh_reg_lh.cookie = 0ull;
2616 lh->mlh_reg_mode = LCK_MINMODE;
2617 lh->mlh_pdo_lh.cookie = 0ull;
2618 lh->mlh_pdo_mode = LCK_MINMODE;
2621 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2623 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2624 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2628 * Initialize fields of struct mdt_thread_info. Other fields are left in
2629 * uninitialized state, because it's too expensive to zero out whole
2630 * mdt_thread_info (> 1K) on each request arrival.
2632 static void mdt_thread_info_init(struct ptlrpc_request *req,
2633 struct mdt_thread_info *info)
2636 struct md_capainfo *ci;
2638 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2639 info->mti_pill = &req->rq_pill;
2642 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2643 mdt_lock_handle_init(&info->mti_lh[i]);
2645 /* mdt device: it can be NULL while CONNECT */
2646 if (req->rq_export) {
2647 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2648 info->mti_exp = req->rq_export;
2650 info->mti_mdt = NULL;
2651 info->mti_env = req->rq_svc_thread->t_env;
2652 ci = md_capainfo(info->mti_env);
2653 memset(ci, 0, sizeof *ci);
2654 if (req->rq_export) {
2655 if (exp_connect_rmtclient(req->rq_export))
2656 ci->mc_auth = LC_ID_CONVERT;
2657 else if (req->rq_export->exp_connect_flags &
2658 OBD_CONNECT_MDS_CAPA)
2659 ci->mc_auth = LC_ID_PLAIN;
2661 ci->mc_auth = LC_ID_NONE;
2664 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2665 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2666 info->mti_mos[0] = NULL;
2667 info->mti_mos[1] = NULL;
2668 info->mti_mos[2] = NULL;
2669 info->mti_mos[3] = NULL;
2671 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2672 info->mti_body = NULL;
2673 info->mti_object = NULL;
2674 info->mti_dlm_req = NULL;
2675 info->mti_has_trans = 0;
2676 info->mti_no_need_trans = 0;
2677 info->mti_cross_ref = 0;
2678 info->mti_opdata = 0;
2680 /* To not check for split by default. */
2681 info->mti_spec.sp_ck_split = 0;
2682 info->mti_spec.no_create = 0;
2685 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2689 req_capsule_fini(info->mti_pill);
2690 if (info->mti_object != NULL) {
2692 * freeing an object may lead to OSD level transaction, do not
2693 * let it mess with MDT. bz19385.
2695 info->mti_no_need_trans = 1;
2696 mdt_object_put(info->mti_env, info->mti_object);
2697 info->mti_object = NULL;
2699 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2700 mdt_lock_handle_fini(&info->mti_lh[i]);
2701 info->mti_env = NULL;
2704 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2705 struct obd_device *obd, int *process)
2707 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2708 case MDS_CONNECT: /* This will never get here, but for completeness. */
2709 case OST_CONNECT: /* This will never get here, but for completeness. */
2710 case MDS_DISCONNECT:
2711 case OST_DISCONNECT:
2716 case MDS_DONE_WRITING:
2717 case MDS_SYNC: /* used in unmounting */
2723 *process = target_queue_recovery_request(req, obd);
2727 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2734 * Handle recovery. Return:
2735 * +1: continue request processing;
2736 * -ve: abort immediately with the given error code;
2737 * 0: send reply with error code in req->rq_status;
2739 static int mdt_recovery(struct mdt_thread_info *info)
2741 struct ptlrpc_request *req = mdt_info_req(info);
2743 struct obd_device *obd;
2747 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2750 case SEC_CTX_INIT_CONT:
2756 rc = mdt_handle_idmap(info);
2765 if (unlikely(req->rq_export == NULL)) {
2766 CERROR("operation %d on unconnected MDS from %s\n",
2767 lustre_msg_get_opc(req->rq_reqmsg),
2768 libcfs_id2str(req->rq_peer));
2769 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2770 * mds_A will get -ENOTCONN(especially for ping req),
2771 * which will cause that mds_A deactive timeout, then when
2772 * mds_A cleanup, the cleanup process will be suspended since
2773 * deactive timeout is not zero.
2775 req->rq_status = -ENOTCONN;
2776 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2780 /* sanity check: if the xid matches, the request must be marked as a
2781 * resent or replayed */
2782 if (req_xid_is_last(req)) {
2783 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2784 (MSG_RESENT | MSG_REPLAY))) {
2785 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2786 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2787 lustre_msg_get_flags(req->rq_reqmsg));
2789 req->rq_status = -ENOTCONN;
2794 /* else: note the opposite is not always true; a RESENT req after a
2795 * failover will usually not match the last_xid, since it was likely
2796 * never committed. A REPLAYed request will almost never match the
2797 * last xid, however it could for a committed, but still retained,
2800 obd = req->rq_export->exp_obd;
2802 /* Check for aborted recovery... */
2803 spin_lock_bh(&obd->obd_processing_task_lock);
2804 recovering = obd->obd_recovering;
2805 spin_unlock_bh(&obd->obd_processing_task_lock);
2806 if (unlikely(recovering)) {
2809 DEBUG_REQ(D_INFO, req, "Got new replay");
2810 rc = mdt_filter_recovery_request(req, obd, &should_process);
2811 if (rc != 0 || !should_process)
2813 else if (should_process < 0) {
2814 req->rq_status = should_process;
2815 rc = ptlrpc_error(req);
2822 static int mdt_msg_check_version(struct lustre_msg *msg)
2826 switch (lustre_msg_get_opc(msg)) {
2828 case MDS_DISCONNECT:
2832 case SEC_CTX_INIT_CONT:
2834 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2836 CERROR("bad opc %u version %08x, expecting %08x\n",
2837 lustre_msg_get_opc(msg),
2838 lustre_msg_get_version(msg),
2839 LUSTRE_OBD_VERSION);
2843 case MDS_GETATTR_NAME:
2850 case MDS_DONE_WRITING:
2856 case MDS_QUOTACHECK:
2862 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2864 CERROR("bad opc %u version %08x, expecting %08x\n",
2865 lustre_msg_get_opc(msg),
2866 lustre_msg_get_version(msg),
2867 LUSTRE_MDS_VERSION);
2871 case LDLM_BL_CALLBACK:
2872 case LDLM_CP_CALLBACK:
2873 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2875 CERROR("bad opc %u version %08x, expecting %08x\n",
2876 lustre_msg_get_opc(msg),
2877 lustre_msg_get_version(msg),
2878 LUSTRE_DLM_VERSION);
2880 case OBD_LOG_CANCEL:
2881 case LLOG_ORIGIN_HANDLE_CREATE:
2882 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2883 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2884 case LLOG_ORIGIN_HANDLE_CLOSE:
2885 case LLOG_ORIGIN_HANDLE_DESTROY:
2886 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2888 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2890 CERROR("bad opc %u version %08x, expecting %08x\n",
2891 lustre_msg_get_opc(msg),
2892 lustre_msg_get_version(msg),
2893 LUSTRE_LOG_VERSION);
2896 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2902 static int mdt_handle0(struct ptlrpc_request *req,
2903 struct mdt_thread_info *info,
2904 struct mdt_opc_slice *supported)
2906 struct mdt_handler *h;
2907 struct lustre_msg *msg;
2912 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2915 LASSERT(current->journal_info == NULL);
2917 msg = req->rq_reqmsg;
2918 rc = mdt_msg_check_version(msg);
2919 if (likely(rc == 0)) {
2920 rc = mdt_recovery(info);
2921 if (likely(rc == +1)) {
2922 h = mdt_handler_find(lustre_msg_get_opc(msg),
2924 if (likely(h != NULL)) {
2925 rc = mdt_req_handle(info, h, req);
2927 CERROR("The unsupported opc: 0x%x\n",
2928 lustre_msg_get_opc(msg) );
2929 req->rq_status = -ENOTSUPP;
2930 rc = ptlrpc_error(req);
2935 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2940 * MDT handler function called by ptlrpc service thread when request comes.
2942 * XXX common "target" functionality should be factored into separate module
2943 * shared by mdt, ost and stand-alone services like fld.
2945 static int mdt_handle_common(struct ptlrpc_request *req,
2946 struct mdt_opc_slice *supported)
2949 struct mdt_thread_info *info;
2953 env = req->rq_svc_thread->t_env;
2954 LASSERT(env != NULL);
2955 LASSERT(env->le_ses != NULL);
2956 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2957 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2958 LASSERT(info != NULL);
2960 mdt_thread_info_init(req, info);
2962 rc = mdt_handle0(req, info, supported);
2964 mdt_thread_info_fini(info);
2969 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2972 int mdt_recovery_handle(struct ptlrpc_request *req)
2977 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2979 rc = mdt_handle_common(req, mdt_fld_handlers);
2982 rc = mdt_handle_common(req, mdt_seq_handlers);
2985 rc = mdt_handle_common(req, mdt_regular_handlers);
2992 static int mdt_regular_handle(struct ptlrpc_request *req)
2994 return mdt_handle_common(req, mdt_regular_handlers);
2997 static int mdt_readpage_handle(struct ptlrpc_request *req)
2999 return mdt_handle_common(req, mdt_readpage_handlers);
3002 static int mdt_xmds_handle(struct ptlrpc_request *req)
3004 return mdt_handle_common(req, mdt_xmds_handlers);
3007 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3009 return mdt_handle_common(req, mdt_seq_handlers);
3012 static int mdt_mdss_handle(struct ptlrpc_request *req)
3014 return mdt_handle_common(req, mdt_seq_handlers);
3017 static int mdt_dtss_handle(struct ptlrpc_request *req)
3019 return mdt_handle_common(req, mdt_seq_handlers);
3022 static int mdt_fld_handle(struct ptlrpc_request *req)
3024 return mdt_handle_common(req, mdt_fld_handlers);
3040 static int mdt_intent_getattr(enum mdt_it_code opcode,
3041 struct mdt_thread_info *info,
3042 struct ldlm_lock **,
3044 static int mdt_intent_reint(enum mdt_it_code opcode,
3045 struct mdt_thread_info *info,
3046 struct ldlm_lock **,
3049 static struct mdt_it_flavor {
3050 const struct req_format *it_fmt;
3052 int (*it_act)(enum mdt_it_code ,
3053 struct mdt_thread_info *,
3054 struct ldlm_lock **,
3057 } mdt_it_flavor[] = {
3059 .it_fmt = &RQF_LDLM_INTENT,
3060 /*.it_flags = HABEO_REFERO,*/
3062 .it_act = mdt_intent_reint,
3063 .it_reint = REINT_OPEN
3066 .it_fmt = &RQF_LDLM_INTENT,
3067 .it_flags = MUTABOR,
3068 .it_act = mdt_intent_reint,
3069 .it_reint = REINT_OPEN
3072 .it_fmt = &RQF_LDLM_INTENT,
3073 .it_flags = MUTABOR,
3074 .it_act = mdt_intent_reint,
3075 .it_reint = REINT_CREATE
3077 [MDT_IT_GETATTR] = {
3078 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3079 .it_flags = HABEO_REFERO,
3080 .it_act = mdt_intent_getattr
3082 [MDT_IT_READDIR] = {
3088 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3089 .it_flags = HABEO_REFERO,
3090 .it_act = mdt_intent_getattr
3093 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3094 .it_flags = MUTABOR,
3096 .it_reint = REINT_UNLINK
3100 .it_flags = MUTABOR,
3103 [MDT_IT_GETXATTR] = {
3110 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3111 struct ldlm_lock **lockp,
3112 struct ldlm_lock *new_lock,
3113 struct mdt_lock_handle *lh,
3116 struct ptlrpc_request *req = mdt_info_req(info);
3117 struct ldlm_lock *lock = *lockp;
3120 * Get new lock only for cases when possible resent did not find any
3123 if (new_lock == NULL)
3124 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3126 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3127 lh->mlh_reg_lh.cookie = 0;
3131 LASSERTF(new_lock != NULL,
3132 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3135 * If we've already given this lock to a client once, then we should
3136 * have no readers or writers. Otherwise, we should have one reader
3137 * _or_ writer ref (which will be zeroed below) before returning the
3140 if (new_lock->l_export == req->rq_export) {
3141 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3143 LASSERT(new_lock->l_export == NULL);
3144 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3149 if (new_lock->l_export == req->rq_export) {
3151 * Already gave this to the client, which means that we
3152 * reconstructed a reply.
3154 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3156 lh->mlh_reg_lh.cookie = 0;
3157 RETURN(ELDLM_LOCK_REPLACED);
3161 * Fixup the lock to be given to the client.
3163 lock_res_and_lock(new_lock);
3164 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3165 * possible blocking AST. */
3166 while (new_lock->l_readers > 0) {
3167 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3168 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3169 new_lock->l_readers--;
3171 while (new_lock->l_writers > 0) {
3172 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3173 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3174 new_lock->l_writers--;
3177 new_lock->l_export = class_export_lock_get(req->rq_export);
3178 new_lock->l_blocking_ast = lock->l_blocking_ast;
3179 new_lock->l_completion_ast = lock->l_completion_ast;
3180 new_lock->l_remote_handle = lock->l_remote_handle;
3181 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3183 unlock_res_and_lock(new_lock);
3185 lustre_hash_add(new_lock->l_export->exp_lock_hash,
3186 &new_lock->l_remote_handle,
3187 &new_lock->l_exp_hash);
3189 LDLM_LOCK_RELEASE(new_lock);
3190 lh->mlh_reg_lh.cookie = 0;
3192 RETURN(ELDLM_LOCK_REPLACED);
3195 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3196 struct ldlm_lock *new_lock,
3197 struct ldlm_lock **old_lock,
3198 struct mdt_lock_handle *lh)
3200 struct ptlrpc_request *req = mdt_info_req(info);
3201 struct obd_export *exp = req->rq_export;
3202 struct lustre_handle remote_hdl;
3203 struct ldlm_request *dlmreq;
3204 struct ldlm_lock *lock;
3206 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3209 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3210 remote_hdl = dlmreq->lock_handle[0];
3212 lock = lustre_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3214 if (lock != new_lock) {
3215 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3216 lh->mlh_reg_mode = lock->l_granted_mode;
3218 LDLM_DEBUG(lock, "Restoring lock cookie");
3219 DEBUG_REQ(D_DLMTRACE, req,
3220 "restoring lock cookie "LPX64,
3221 lh->mlh_reg_lh.cookie);
3223 *old_lock = LDLM_LOCK_GET(lock);
3224 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3228 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3232 * If the xid matches, then we know this is a resent request, and allow
3233 * it. (It's probably an OPEN, for which we don't send a lock.
3235 if (req_xid_is_last(req))
3239 * This remote handle isn't enqueued, so we never received or processed
3240 * this request. Clear MSG_RESENT, because it can be handled like any
3241 * normal request now.
3243 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3245 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3249 static int mdt_intent_getattr(enum mdt_it_code opcode,
3250 struct mdt_thread_info *info,
3251 struct ldlm_lock **lockp,
3254 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3255 struct ldlm_lock *new_lock = NULL;
3257 struct ldlm_reply *ldlm_rep;
3258 struct ptlrpc_request *req;
3259 struct mdt_body *reqbody;
3260 struct mdt_body *repbody;
3264 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3267 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3270 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3271 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3272 repbody->eadatasize = 0;
3273 repbody->aclsize = 0;
3277 child_bits = MDS_INODELOCK_LOOKUP;
3279 case MDT_IT_GETATTR:
3280 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3283 CERROR("Unhandled till now");
3284 GOTO(out_shrink, rc = -EINVAL);
3287 rc = mdt_init_ucred(info, reqbody);
3289 GOTO(out_shrink, rc);
3291 req = info->mti_pill->rc_req;
3292 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3293 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3295 /* Get lock from request for possible resent case. */
3296 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3298 ldlm_rep->lock_policy_res2 =
3299 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3301 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3302 ldlm_rep->lock_policy_res2 = 0;
3303 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3304 ldlm_rep->lock_policy_res2) {
3305 lhc->mlh_reg_lh.cookie = 0ull;
3306 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3309 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3312 mdt_exit_ucred(info);
3314 mdt_shrink_reply(info);
3318 static int mdt_intent_reint(enum mdt_it_code opcode,
3319 struct mdt_thread_info *info,
3320 struct ldlm_lock **lockp,
3323 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3324 struct ldlm_reply *rep = NULL;
3328 static const struct req_format *intent_fmts[REINT_MAX] = {
3329 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3330 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3335 opc = mdt_reint_opcode(info, intent_fmts);
3339 if (mdt_it_flavor[opcode].it_reint != opc) {
3340 CERROR("Reint code %ld doesn't match intent: %d\n",
3342 RETURN(err_serious(-EPROTO));
3345 /* Get lock from request for possible resent case. */
3346 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3348 rc = mdt_reint_internal(info, lhc, opc);
3350 /* Check whether the reply has been packed successfully. */
3351 if (mdt_info_req(info)->rq_repmsg != NULL)
3352 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3354 RETURN(err_serious(-EFAULT));
3356 /* MDC expects this in any case */
3358 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3360 /* Cross-ref case, the lock should be returned to the client */
3361 if (rc == -EREMOTE) {
3362 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3363 rep->lock_policy_res2 = 0;
3364 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3367 rep->lock_policy_res2 = clear_serious(rc);
3369 if (rc == -ENOTCONN || rc == -ENODEV ||
3370 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3372 * If it is the disconnect error (ENODEV & ENOCONN), the error
3373 * will be returned by rq_status, and client at ptlrpc layer
3374 * will detect this, then disconnect, reconnect the import
3375 * immediately, instead of impacting the following the rpc.
3377 lhc->mlh_reg_lh.cookie = 0ull;
3381 * For other cases, the error will be returned by intent.
3382 * and client will retrieve the result from intent.
3385 * FIXME: when open lock is finished, that should be
3388 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3389 rep->lock_policy_res2 = 0;
3390 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3393 lhc->mlh_reg_lh.cookie = 0ull;
3394 RETURN(ELDLM_LOCK_ABORTED);
3399 static int mdt_intent_code(long itcode)
3407 case IT_OPEN|IT_CREAT:
3414 rc = MDT_IT_READDIR;
3417 rc = MDT_IT_GETATTR;
3429 rc = MDT_IT_GETXATTR;
3432 CERROR("Unknown intent opcode: %ld\n", itcode);
3439 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3440 struct ldlm_lock **lockp, int flags)
3442 struct req_capsule *pill;
3443 struct mdt_it_flavor *flv;
3448 opc = mdt_intent_code(itopc);
3452 pill = info->mti_pill;
3453 flv = &mdt_it_flavor[opc];
3455 if (flv->it_fmt != NULL)
3456 req_capsule_extend(pill, flv->it_fmt);
3458 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3460 struct ptlrpc_request *req = mdt_info_req(info);
3461 if (flv->it_flags & MUTABOR &&
3462 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3465 if (rc == 0 && flv->it_act != NULL) {
3466 /* execute policy */
3467 rc = flv->it_act(opc, info, lockp, flags);
3474 static int mdt_intent_policy(struct ldlm_namespace *ns,
3475 struct ldlm_lock **lockp, void *req_cookie,
3476 ldlm_mode_t mode, int flags, void *data)
3478 struct mdt_thread_info *info;
3479 struct ptlrpc_request *req = req_cookie;
3480 struct ldlm_intent *it;
3481 struct req_capsule *pill;
3486 LASSERT(req != NULL);
3488 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3490 LASSERT(info != NULL);
3491 pill = info->mti_pill;
3492 LASSERT(pill->rc_req == req);
3494 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3495 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3496 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3498 const struct ldlm_request *dlmreq;
3501 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3506 * Lock without inodebits makes no sense and will oops
3507 * later in ldlm. Let's check it now to see if we have
3508 * wrong lock from client or bits get corrupted
3509 * somewhere in mdt_intent_opc().
3511 dlmreq = info->mti_dlm_req;
3512 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
3513 LASSERT(req_bits != 0);
3516 rc = err_serious(-EFAULT);
3518 /* No intent was provided */
3519 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3520 rc = req_capsule_server_pack(pill);
3522 rc = err_serious(rc);
3530 static void mdt_seq_adjust(const struct lu_env *env,
3531 struct mdt_device *m, int lost)
3533 struct md_site *ms = mdt_md_site(m);
3534 struct lu_seq_range out;
3537 LASSERT(ms && ms->ms_server_seq);
3539 /* get extra seq from seq_server, moving it's range up */
3540 while (lost-- > 0) {
3541 seq_server_alloc_meta(ms->ms_server_seq, NULL, &out, env);
3546 static int mdt_seq_fini(const struct lu_env *env,
3547 struct mdt_device *m)
3549 struct md_site *ms = mdt_md_site(m);
3553 if (ms->ms_server_seq) {
3554 seq_server_fini(ms->ms_server_seq, env);
3555 OBD_FREE_PTR(ms->ms_server_seq);
3556 ms->ms_server_seq = NULL;
3559 if (ms->ms_control_seq) {
3560 seq_server_fini(ms->ms_control_seq, env);
3561 OBD_FREE_PTR(ms->ms_control_seq);
3562 ms->ms_control_seq = NULL;
3565 if (ms->ms_client_seq) {
3566 seq_client_fini(ms->ms_client_seq);
3567 OBD_FREE_PTR(ms->ms_client_seq);
3568 ms->ms_client_seq = NULL;
3575 static int mdt_seq_init(const struct lu_env *env,
3577 struct mdt_device *m)
3584 ms = mdt_md_site(m);
3587 * This is sequence-controller node. Init seq-controller server on local
3590 if (ms->ms_node_id == 0) {
3591 LASSERT(ms->ms_control_seq == NULL);
3593 OBD_ALLOC_PTR(ms->ms_control_seq);
3594 if (ms->ms_control_seq == NULL)
3597 rc = seq_server_init(ms->ms_control_seq,
3598 m->mdt_bottom, uuid,
3599 LUSTRE_SEQ_CONTROLLER,
3604 GOTO(out_seq_fini, rc);
3606 OBD_ALLOC_PTR(ms->ms_client_seq);
3607 if (ms->ms_client_seq == NULL)
3608 GOTO(out_seq_fini, rc = -ENOMEM);
3610 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3611 if (prefix == NULL) {
3612 OBD_FREE_PTR(ms->ms_client_seq);
3613 GOTO(out_seq_fini, rc = -ENOMEM);
3616 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3620 * Init seq-controller client after seq-controller server is
3621 * ready. Pass ms->ms_control_seq to it for direct talking.
3623 rc = seq_client_init(ms->ms_client_seq, NULL,
3624 LUSTRE_SEQ_METADATA, prefix,
3625 ms->ms_control_seq);
3626 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3629 GOTO(out_seq_fini, rc);
3632 /* Init seq-server on local MDT */
3633 LASSERT(ms->ms_server_seq == NULL);
3635 OBD_ALLOC_PTR(ms->ms_server_seq);
3636 if (ms->ms_server_seq == NULL)
3637 GOTO(out_seq_fini, rc = -ENOMEM);
3639 rc = seq_server_init(ms->ms_server_seq,
3640 m->mdt_bottom, uuid,
3645 GOTO(out_seq_fini, rc = -ENOMEM);
3647 /* Assign seq-controller client to local seq-server. */
3648 if (ms->ms_node_id == 0) {
3649 LASSERT(ms->ms_client_seq != NULL);
3651 rc = seq_server_set_cli(ms->ms_server_seq,
3659 mdt_seq_fini(env, m);
3664 * Init client sequence manager which is used by local MDS to talk to sequence
3665 * controller on remote node.
3667 static int mdt_seq_init_cli(const struct lu_env *env,
3668 struct mdt_device *m,
3669 struct lustre_cfg *cfg)
3671 struct md_site *ms = mdt_md_site(m);
3672 struct obd_device *mdc;
3673 struct obd_uuid *uuidp, *mdcuuidp;
3674 char *uuid_str, *mdc_uuid_str;
3677 struct mdt_thread_info *info;
3678 char *p, *index_string = lustre_cfg_string(cfg, 2);
3681 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3682 uuidp = &info->mti_u.uuid[0];
3683 mdcuuidp = &info->mti_u.uuid[1];
3685 LASSERT(index_string);
3687 index = simple_strtol(index_string, &p, 10);
3689 CERROR("Invalid index in lustre_cgf, offset 2\n");
3693 /* check if this is adding the first MDC and controller is not yet
3695 if (index != 0 || ms->ms_client_seq)
3698 uuid_str = lustre_cfg_string(cfg, 1);
3699 mdc_uuid_str = lustre_cfg_string(cfg, 4);
3700 obd_str2uuid(uuidp, uuid_str);
3701 obd_str2uuid(mdcuuidp, mdc_uuid_str);
3703 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3705 CERROR("can't find controller MDC by uuid %s\n",
3708 } else if (!mdc->obd_set_up) {
3709 CERROR("target %s not set up\n", mdc->obd_name);
3712 LASSERT(ms->ms_control_exp);
3713 OBD_ALLOC_PTR(ms->ms_client_seq);
3714 if (ms->ms_client_seq != NULL) {
3717 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3721 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3724 rc = seq_client_init(ms->ms_client_seq,
3726 LUSTRE_SEQ_METADATA,
3728 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3735 LASSERT(ms->ms_server_seq != NULL);
3736 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
3743 static void mdt_seq_fini_cli(struct mdt_device *m)
3749 ms = mdt_md_site(m);
3752 if (ms->ms_server_seq)
3753 seq_server_set_cli(ms->ms_server_seq,
3756 if (ms->ms_control_exp) {
3757 class_export_put(ms->ms_control_exp);
3758 ms->ms_control_exp = NULL;
3767 static int mdt_fld_fini(const struct lu_env *env,
3768 struct mdt_device *m)
3770 struct md_site *ms = mdt_md_site(m);
3773 if (ms && ms->ms_server_fld) {
3774 fld_server_fini(ms->ms_server_fld, env);
3775 OBD_FREE_PTR(ms->ms_server_fld);
3776 ms->ms_server_fld = NULL;
3782 static int mdt_fld_init(const struct lu_env *env,
3784 struct mdt_device *m)
3790 ms = mdt_md_site(m);
3792 OBD_ALLOC_PTR(ms->ms_server_fld);
3793 if (ms->ms_server_fld == NULL)
3794 RETURN(rc = -ENOMEM);
3796 rc = fld_server_init(ms->ms_server_fld,
3797 m->mdt_bottom, uuid,
3798 env, ms->ms_node_id);
3800 OBD_FREE_PTR(ms->ms_server_fld);
3801 ms->ms_server_fld = NULL;
3808 /* device init/fini methods */
3809 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3812 if (m->mdt_regular_service != NULL) {
3813 ptlrpc_unregister_service(m->mdt_regular_service);
3814 m->mdt_regular_service = NULL;
3816 if (m->mdt_readpage_service != NULL) {
3817 ptlrpc_unregister_service(m->mdt_readpage_service);
3818 m->mdt_readpage_service = NULL;
3820 if (m->mdt_xmds_service != NULL) {
3821 ptlrpc_unregister_service(m->mdt_xmds_service);
3822 m->mdt_xmds_service = NULL;
3824 if (m->mdt_setattr_service != NULL) {
3825 ptlrpc_unregister_service(m->mdt_setattr_service);
3826 m->mdt_setattr_service = NULL;
3828 if (m->mdt_mdsc_service != NULL) {
3829 ptlrpc_unregister_service(m->mdt_mdsc_service);
3830 m->mdt_mdsc_service = NULL;
3832 if (m->mdt_mdss_service != NULL) {
3833 ptlrpc_unregister_service(m->mdt_mdss_service);
3834 m->mdt_mdss_service = NULL;
3836 if (m->mdt_dtss_service != NULL) {
3837 ptlrpc_unregister_service(m->mdt_dtss_service);
3838 m->mdt_dtss_service = NULL;
3840 if (m->mdt_fld_service != NULL) {
3841 ptlrpc_unregister_service(m->mdt_fld_service);
3842 m->mdt_fld_service = NULL;
3847 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3850 static struct ptlrpc_service_conf conf;
3851 cfs_proc_dir_entry_t *procfs_entry;
3854 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3856 conf = (typeof(conf)) {
3857 .psc_nbufs = MDS_NBUFS,
3858 .psc_bufsize = MDS_BUFSIZE,
3859 .psc_max_req_size = MDS_MAXREQSIZE,
3860 .psc_max_reply_size = MDS_MAXREPSIZE,
3861 .psc_req_portal = MDS_REQUEST_PORTAL,
3862 .psc_rep_portal = MDC_REPLY_PORTAL,
3863 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3865 * We'd like to have a mechanism to set this on a per-device
3866 * basis, but alas...
3868 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3870 .psc_max_threads = MDT_MAX_THREADS,
3871 .psc_ctx_tags = LCT_MD_THREAD
3874 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3875 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3876 "mdt_ldlm_client", m->mdt_ldlm_client);
3878 m->mdt_regular_service =
3879 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3880 procfs_entry, target_print_req,
3882 if (m->mdt_regular_service == NULL)
3885 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3887 GOTO(err_mdt_svc, rc);
3890 * readpage service configuration. Parameters have to be adjusted,
3893 conf = (typeof(conf)) {
3894 .psc_nbufs = MDS_NBUFS,
3895 .psc_bufsize = MDS_BUFSIZE,
3896 .psc_max_req_size = MDS_MAXREQSIZE,
3897 .psc_max_reply_size = MDS_MAXREPSIZE,
3898 .psc_req_portal = MDS_READPAGE_PORTAL,
3899 .psc_rep_portal = MDC_REPLY_PORTAL,
3900 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3901 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3903 .psc_max_threads = MDT_MAX_THREADS,
3904 .psc_ctx_tags = LCT_MD_THREAD
3906 m->mdt_readpage_service =
3907 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3908 LUSTRE_MDT_NAME "_readpage",
3909 procfs_entry, target_print_req,"mdt_rdpg");
3911 if (m->mdt_readpage_service == NULL) {
3912 CERROR("failed to start readpage service\n");
3913 GOTO(err_mdt_svc, rc = -ENOMEM);
3916 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3919 * setattr service configuration.
3921 conf = (typeof(conf)) {
3922 .psc_nbufs = MDS_NBUFS,
3923 .psc_bufsize = MDS_BUFSIZE,
3924 .psc_max_req_size = MDS_MAXREQSIZE,
3925 .psc_max_reply_size = MDS_MAXREPSIZE,
3926 .psc_req_portal = MDS_SETATTR_PORTAL,
3927 .psc_rep_portal = MDC_REPLY_PORTAL,
3928 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3929 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3931 .psc_max_threads = MDT_MAX_THREADS,
3932 .psc_ctx_tags = LCT_MD_THREAD
3935 m->mdt_setattr_service =
3936 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3937 LUSTRE_MDT_NAME "_setattr",
3938 procfs_entry, target_print_req,"mdt_attr");
3940 if (!m->mdt_setattr_service) {
3941 CERROR("failed to start setattr service\n");
3942 GOTO(err_mdt_svc, rc = -ENOMEM);
3945 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3947 GOTO(err_mdt_svc, rc);
3950 * sequence controller service configuration
3952 conf = (typeof(conf)) {
3953 .psc_nbufs = MDS_NBUFS,
3954 .psc_bufsize = MDS_BUFSIZE,
3955 .psc_max_req_size = SEQ_MAXREQSIZE,
3956 .psc_max_reply_size = SEQ_MAXREPSIZE,
3957 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3958 .psc_rep_portal = MDC_REPLY_PORTAL,
3959 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3960 .psc_min_threads = SEQ_NUM_THREADS,
3961 .psc_max_threads = SEQ_NUM_THREADS,
3962 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3965 m->mdt_mdsc_service =
3966 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3967 LUSTRE_MDT_NAME"_mdsc",
3968 procfs_entry, target_print_req,"mdt_mdsc");
3969 if (!m->mdt_mdsc_service) {
3970 CERROR("failed to start seq controller service\n");
3971 GOTO(err_mdt_svc, rc = -ENOMEM);
3974 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3976 GOTO(err_mdt_svc, rc);
3979 * metadata sequence server service configuration
3981 conf = (typeof(conf)) {
3982 .psc_nbufs = MDS_NBUFS,
3983 .psc_bufsize = MDS_BUFSIZE,
3984 .psc_max_req_size = SEQ_MAXREQSIZE,
3985 .psc_max_reply_size = SEQ_MAXREPSIZE,
3986 .psc_req_portal = SEQ_METADATA_PORTAL,
3987 .psc_rep_portal = MDC_REPLY_PORTAL,
3988 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3989 .psc_min_threads = SEQ_NUM_THREADS,
3990 .psc_max_threads = SEQ_NUM_THREADS,
3991 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3994 m->mdt_mdss_service =
3995 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3996 LUSTRE_MDT_NAME"_mdss",
3997 procfs_entry, target_print_req,"mdt_mdss");
3998 if (!m->mdt_mdss_service) {
3999 CERROR("failed to start metadata seq server service\n");
4000 GOTO(err_mdt_svc, rc = -ENOMEM);
4003 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
4005 GOTO(err_mdt_svc, rc);
4009 * Data sequence server service configuration. We want to have really
4010 * cluster-wide sequences space. This is why we start only one sequence
4011 * controller which manages space.
4013 conf = (typeof(conf)) {
4014 .psc_nbufs = MDS_NBUFS,
4015 .psc_bufsize = MDS_BUFSIZE,
4016 .psc_max_req_size = SEQ_MAXREQSIZE,
4017 .psc_max_reply_size = SEQ_MAXREPSIZE,
4018 .psc_req_portal = SEQ_DATA_PORTAL,
4019 .psc_rep_portal = OSC_REPLY_PORTAL,
4020 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4021 .psc_min_threads = SEQ_NUM_THREADS,
4022 .psc_max_threads = SEQ_NUM_THREADS,
4023 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4026 m->mdt_dtss_service =
4027 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
4028 LUSTRE_MDT_NAME"_dtss",
4029 procfs_entry, target_print_req,"mdt_dtss");
4030 if (!m->mdt_dtss_service) {
4031 CERROR("failed to start data seq server service\n");
4032 GOTO(err_mdt_svc, rc = -ENOMEM);
4035 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
4037 GOTO(err_mdt_svc, rc);
4039 /* FLD service start */
4040 conf = (typeof(conf)) {
4041 .psc_nbufs = MDS_NBUFS,
4042 .psc_bufsize = MDS_BUFSIZE,
4043 .psc_max_req_size = FLD_MAXREQSIZE,
4044 .psc_max_reply_size = FLD_MAXREPSIZE,
4045 .psc_req_portal = FLD_REQUEST_PORTAL,
4046 .psc_rep_portal = MDC_REPLY_PORTAL,
4047 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4048 .psc_min_threads = FLD_NUM_THREADS,
4049 .psc_max_threads = FLD_NUM_THREADS,
4050 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
4053 m->mdt_fld_service =
4054 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
4055 LUSTRE_MDT_NAME"_fld",
4056 procfs_entry, target_print_req, "mdt_fld");
4057 if (!m->mdt_fld_service) {
4058 CERROR("failed to start fld service\n");
4059 GOTO(err_mdt_svc, rc = -ENOMEM);
4062 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
4064 GOTO(err_mdt_svc, rc);
4067 * mds-mds service configuration. Separate portal is used to allow
4068 * mds-mds requests be not blocked during recovery.
4070 conf = (typeof(conf)) {
4071 .psc_nbufs = MDS_NBUFS,
4072 .psc_bufsize = MDS_BUFSIZE,
4073 .psc_max_req_size = MDS_MAXREQSIZE,
4074 .psc_max_reply_size = MDS_MAXREPSIZE,
4075 .psc_req_portal = MDS_MDS_PORTAL,
4076 .psc_rep_portal = MDC_REPLY_PORTAL,
4077 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4078 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
4080 .psc_max_threads = MDT_MAX_THREADS,
4081 .psc_ctx_tags = LCT_MD_THREAD
4083 m->mdt_xmds_service =
4084 ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
4085 LUSTRE_MDT_NAME "_mds",
4086 procfs_entry, target_print_req,"mdt_xmds");
4088 if (m->mdt_xmds_service == NULL) {
4089 CERROR("failed to start readpage service\n");
4090 GOTO(err_mdt_svc, rc = -ENOMEM);
4093 rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
4095 GOTO(err_mdt_svc, rc);
4100 mdt_stop_ptlrpc_service(m);
4105 static void mdt_stack_fini(const struct lu_env *env,
4106 struct mdt_device *m, struct lu_device *top)
4108 struct obd_device *obd = mdt2obd_dev(m);
4109 struct lustre_cfg_bufs *bufs;
4110 struct lustre_cfg *lcfg;
4111 struct mdt_thread_info *info;
4115 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4116 LASSERT(info != NULL);
4118 bufs = &info->mti_u.bufs;
4119 /* process cleanup, pass mdt obd name to get obd umount flags */
4120 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4125 lustre_cfg_bufs_set_string(bufs, 1, flags);
4126 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4128 CERROR("Cannot alloc lcfg!\n");
4133 top->ld_ops->ldo_process_config(env, top, lcfg);
4134 lustre_cfg_free(lcfg);
4136 lu_stack_fini(env, top);
4137 m->mdt_child = NULL;
4138 m->mdt_bottom = NULL;
4141 static struct lu_device *mdt_layer_setup(struct lu_env *env,
4142 const char *typename,
4143 struct lu_device *child,
4144 struct lustre_cfg *cfg)
4146 const char *dev = lustre_cfg_string(cfg, 0);
4147 struct obd_type *type;
4148 struct lu_device_type *ldt;
4149 struct lu_device *d;
4154 type = class_get_type(typename);
4156 CERROR("Unknown type: '%s'\n", typename);
4157 GOTO(out, rc = -ENODEV);
4160 rc = lu_env_refill((struct lu_env *)env);
4162 CERROR("Failure to refill session: '%d'\n", rc);
4168 CERROR("type: '%s'\n", typename);
4169 GOTO(out_type, rc = -EINVAL);
4172 ldt->ldt_obd_type = type;
4173 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
4175 CERROR("Cannot allocate device: '%s'\n", typename);
4176 GOTO(out_type, rc = -ENODEV);
4179 LASSERT(child->ld_site);
4180 d->ld_site = child->ld_site;
4183 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
4185 CERROR("can't init device '%s', rc %d\n", typename, rc);
4186 GOTO(out_alloc, rc);
4189 lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
4194 ldt->ldt_ops->ldto_device_free(env, d);
4197 class_put_type(type);
4202 static int mdt_stack_init(struct lu_env *env,
4203 struct mdt_device *m,
4204 struct lustre_cfg *cfg,
4205 struct lustre_mount_info *lmi)
4207 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4208 struct lu_device *tmp;
4209 struct md_device *md;
4210 struct lu_device *child_lu_dev;
4214 /* init the stack */
4215 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
4217 RETURN(PTR_ERR(tmp));
4219 m->mdt_bottom = lu2dt_dev(tmp);
4221 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
4223 GOTO(out, rc = PTR_ERR(tmp));
4228 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
4230 GOTO(out, rc = PTR_ERR(tmp));
4233 /*set mdd upcall device*/
4234 md_upcall_dev_set(md, lu2md_dev(d));
4237 /*set cmm upcall device*/
4238 md_upcall_dev_set(md, &m->mdt_md_dev);
4240 m->mdt_child = lu2md_dev(d);
4242 /* process setup config */
4243 tmp = &m->mdt_md_dev.md_lu_dev;
4244 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
4248 /* initialize local objects */
4249 child_lu_dev = &m->mdt_child->md_lu_dev;
4251 rc = child_lu_dev->ld_ops->ldo_prepare(env,
4252 &m->mdt_md_dev.md_lu_dev,
4255 /* fini from last known good lu_device */
4257 mdt_stack_fini(env, m, d);
4263 * setup CONFIG_ORIG context, used to access local config log.
4264 * this may need to be rewrite as part of llog rewrite for lu-api.
4266 static int mdt_obd_llog_setup(struct obd_device *obd,
4267 struct lustre_sb_info *lsi)
4271 LASSERT(obd->obd_fsops == NULL);
4273 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
4274 if (IS_ERR(obd->obd_fsops))
4275 return PTR_ERR(obd->obd_fsops);
4277 rc = fsfilt_setup(obd, lsi->lsi_srv_mnt->mnt_sb);
4279 fsfilt_put_ops(obd->obd_fsops);
4283 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
4284 obd->obd_lvfs_ctxt.pwdmnt = lsi->lsi_srv_mnt;
4285 obd->obd_lvfs_ctxt.pwd = lsi->lsi_srv_mnt->mnt_root;
4286 obd->obd_lvfs_ctxt.fs = get_ds();
4288 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd,
4289 0, NULL, &llog_lvfs_ops);
4291 CERROR("llog_setup() failed: %d\n", rc);
4292 fsfilt_put_ops(obd->obd_fsops);
4298 static void mdt_obd_llog_cleanup(struct obd_device *obd)
4300 struct llog_ctxt *ctxt;
4302 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
4306 if (obd->obd_fsops) {
4307 fsfilt_put_ops(obd->obd_fsops);
4308 obd->obd_fsops = NULL;
4312 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4314 struct md_device *next = m->mdt_child;
4315 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4316 struct lu_site *ls = d->ld_site;
4317 struct obd_device *obd = mdt2obd_dev(m);
4320 target_recovery_fini(obd);
4322 ping_evictor_stop();
4324 mdt_stop_ptlrpc_service(m);
4325 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4326 mdt_obd_llog_cleanup(obd);
4327 obd_exports_barrier(obd);
4328 obd_zombie_barrier();
4329 #ifdef HAVE_QUOTA_SUPPORT
4330 next->md_ops->mdo_quota.mqo_cleanup(env, next);
4332 lut_fini(env, &m->mdt_lut);
4333 mdt_fs_cleanup(env, m);
4334 upcall_cache_cleanup(m->mdt_identity_cache);
4335 m->mdt_identity_cache = NULL;
4337 if (m->mdt_namespace != NULL) {
4338 ldlm_namespace_free(m->mdt_namespace, NULL,
4339 d->ld_obd->obd_force);
4340 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4343 cfs_free_nidlist(&m->mdt_nosquash_nids);
4344 if (m->mdt_nosquash_str) {
4345 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4346 m->mdt_nosquash_str = NULL;
4347 m->mdt_nosquash_strlen = 0;
4350 mdt_seq_fini(env, m);
4351 mdt_seq_fini_cli(m);
4352 mdt_fld_fini(env, m);
4353 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4355 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4356 cfs_timer_disarm(&m->mdt_ck_timer);
4357 mdt_ck_thread_stop(m);
4362 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4365 if (obd->obd_proc_exports_entry) {
4366 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
4367 obd->obd_proc_exports_entry = NULL;
4369 lprocfs_free_per_client_stats(obd);
4370 lprocfs_free_obd_stats(obd);
4371 ptlrpc_lprocfs_unregister_obd(obd);
4372 lprocfs_obd_cleanup(obd);
4375 struct md_site *mite;
4378 mite = lu_site2md(ls);
4382 LASSERT(atomic_read(&d->ld_ref) == 0);
4387 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4389 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4390 struct sptlrpc_rule_set tmp_rset;
4393 sptlrpc_rule_set_init(&tmp_rset);
4394 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4396 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4401 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4403 write_lock(&m->mdt_sptlrpc_lock);
4404 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4405 m->mdt_sptlrpc_rset = tmp_rset;
4406 write_unlock(&m->mdt_sptlrpc_lock);
4411 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
4415 m->mdt_opts.mo_mds_capa = 1;
4416 m->mdt_opts.mo_oss_capa = 1;
4417 #ifdef CONFIG_FS_POSIX_ACL
4418 /* ACLs should be enabled by default (b=13829) */
4419 m->mdt_opts.mo_acl = 1;
4420 LCONSOLE_INFO("Enabling ACL\n");
4422 m->mdt_opts.mo_acl = 0;
4423 LCONSOLE_INFO("Disabling ACL\n");
4432 while (*p && *p != ',')
4436 if ((len == sizeof("user_xattr") - 1) &&
4437 (memcmp(options, "user_xattr", len) == 0)) {
4438 m->mdt_opts.mo_user_xattr = 1;
4439 LCONSOLE_INFO("Enabling user_xattr\n");
4440 } else if ((len == sizeof("nouser_xattr") - 1) &&
4441 (memcmp(options, "nouser_xattr", len) == 0)) {
4442 m->mdt_opts.mo_user_xattr = 0;
4443 LCONSOLE_INFO("Disabling user_xattr\n");
4444 } else if ((len == sizeof("noacl") - 1) &&
4445 (memcmp(options, "noacl", len) == 0)) {
4446 m->mdt_opts.mo_acl = 0;
4447 LCONSOLE_INFO("Disabling ACL\n");
4454 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4456 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4457 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4459 struct lprocfs_static_vars lvars;
4460 struct mdt_thread_info *info;
4461 struct obd_device *obd;
4462 const char *dev = lustre_cfg_string(cfg, 0);
4463 const char *num = lustre_cfg_string(cfg, 2);
4464 struct lustre_mount_info *lmi = NULL;
4465 struct lustre_sb_info *lsi;
4466 struct lustre_disk_data *ldd;
4468 struct md_site *mite;
4469 const char *identity_upcall = "NONE";
4470 #ifdef HAVE_QUOTA_SUPPORT
4471 struct md_device *next;
4477 md_device_init(&m->mdt_md_dev, ldt);
4479 * Environment (env) might be missing mdt_thread_key values at that
4480 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4483 * Usually device allocation path doesn't use module key values, but
4484 * mdt has to do a lot of work here, so allocate key value.
4486 rc = lu_env_refill((struct lu_env *)env);
4490 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4491 LASSERT(info != NULL);
4493 obd = class_name2obd(dev);
4494 LASSERT(obd != NULL);
4496 spin_lock_init(&m->mdt_transno_lock);
4498 m->mdt_max_mdsize = MAX_MD_SIZE;
4499 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
4500 m->mdt_som_conf = 0;
4502 m->mdt_opts.mo_user_xattr = 0;
4503 m->mdt_opts.mo_acl = 0;
4504 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4505 lmi = server_get_mount_2(dev);
4507 CERROR("Cannot get mount info for %s!\n", dev);
4510 lsi = s2lsi(lmi->lmi_sb);
4511 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
4512 /* CMD is supported only in IAM mode */
4515 node_id = simple_strtol(num, NULL, 10);
4516 if (!(ldd->ldd_flags & LDD_F_IAM_DIR) && node_id) {
4517 CERROR("CMD Operation not allowed in IOP mode\n");
4518 GOTO(err_lmi, rc = -EINVAL);
4522 rwlock_init(&m->mdt_sptlrpc_lock);
4523 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4525 spin_lock_init(&m->mdt_ioepoch_lock);
4526 m->mdt_opts.mo_compat_resname = 0;
4527 m->mdt_capa_timeout = CAPA_TIMEOUT;
4528 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4529 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4530 m->mdt_squash_uid = 0;
4531 m->mdt_squash_gid = 0;
4532 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4533 m->mdt_nosquash_str = NULL;
4534 m->mdt_nosquash_strlen = 0;
4535 init_rwsem(&m->mdt_squash_sem);
4537 spin_lock_init(&m->mdt_client_bitmap_lock);
4539 OBD_ALLOC_PTR(mite);
4541 GOTO(err_lmi, rc = -ENOMEM);
4545 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4546 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4547 /* set this lu_device to obd, because error handling need it */
4548 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4550 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
4552 CERROR("Can't init lu_site, rc %d\n", rc);
4553 GOTO(err_free_site, rc);
4556 lprocfs_mdt_init_vars(&lvars);
4557 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
4559 CERROR("Can't init lprocfs, rc %d\n", rc);
4560 GOTO(err_fini_site, rc);
4562 ptlrpc_lprocfs_register_obd(obd);
4564 rc = mdt_procfs_init(m, dev);
4566 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4567 GOTO(err_fini_proc, rc);
4570 obd->obd_proc_exports_entry = proc_mkdir("exports",
4571 obd->obd_proc_entry);
4572 if (obd->obd_proc_exports_entry)
4573 lprocfs_add_simple(obd->obd_proc_exports_entry,
4574 "clear", lprocfs_nid_stats_clear_read,
4575 lprocfs_nid_stats_clear_write, obd, NULL);
4577 /* set server index */
4578 lu_site2md(s)->ms_node_id = node_id;
4580 /* failover is the default
4581 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4582 * assumed whose ms_node_id == 0 XXX
4584 obd->obd_replayable = 1;
4585 /* No connection accepted until configurations will finish */
4586 obd->obd_no_conn = 1;
4588 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4589 char *str = lustre_cfg_string(cfg, 4);
4590 if (strchr(str, 'n')) {
4591 CWARN("%s: recovery disabled\n", obd->obd_name);
4592 obd->obd_replayable = 0;
4596 /* init the stack */
4597 rc = mdt_stack_init((struct lu_env *)env, m, cfg, lmi);
4599 CERROR("Can't init device stack, rc %d\n", rc);
4600 GOTO(err_fini_proc, rc);
4603 rc = lut_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4605 GOTO(err_fini_stack, rc);
4607 rc = mdt_fld_init(env, obd->obd_name, m);
4611 rc = mdt_seq_init(env, obd->obd_name, m);
4613 GOTO(err_fini_fld, rc);
4615 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4616 LUSTRE_MDT_NAME"-%p", m);
4617 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4618 LDLM_NAMESPACE_SERVER,
4619 LDLM_NAMESPACE_GREEDY);
4620 if (m->mdt_namespace == NULL)
4621 GOTO(err_fini_seq, rc = -ENOMEM);
4623 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4624 /* set obd_namespace for compatibility with old code */
4625 obd->obd_namespace = m->mdt_namespace;
4627 /* XXX: to support suppgid for ACL, we enable identity_upcall
4628 * by default, otherwise, maybe got unexpected -EACCESS. */
4629 if (m->mdt_opts.mo_acl)
4630 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4632 m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
4633 &mdt_identity_upcall_cache_ops);
4634 if (IS_ERR(m->mdt_identity_cache)) {
4635 rc = PTR_ERR(m->mdt_identity_cache);
4636 m->mdt_identity_cache = NULL;
4637 GOTO(err_free_ns, rc);
4640 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4642 rc = mdt_ck_thread_start(m);
4644 GOTO(err_free_ns, rc);
4646 rc = mdt_fs_setup(env, m, obd, lsi);
4650 rc = mdt_obd_llog_setup(obd, lsi);
4652 GOTO(err_fs_cleanup, rc);
4654 rc = mdt_llog_ctxt_clone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4656 GOTO(err_llog_cleanup, rc);
4658 mdt_adapt_sptlrpc_conf(obd, 1);
4660 #ifdef HAVE_QUOTA_SUPPORT
4661 next = m->mdt_child;
4662 rc = next->md_ops->mdo_quota.mqo_setup(env, next, lmi->lmi_mnt);
4664 GOTO(err_llog_cleanup, rc);
4667 server_put_mount_2(dev, lmi->lmi_mnt);
4670 target_recovery_init(&m->mdt_lut, mdt_recovery_handle);
4672 rc = mdt_start_ptlrpc_service(m);
4674 GOTO(err_recovery, rc);
4676 ping_evictor_start();
4678 rc = lu_site_init_finish(s);
4680 GOTO(err_stop_service, rc);
4682 if (obd->obd_recovering == 0)
4683 mdt_postrecov(env, m);
4685 mdt_init_capa_ctxt(env, m);
4687 /* Reduce the initial timeout on an MDS because it doesn't need such
4688 * a long timeout as an OST does. Adaptive timeouts will adjust this
4689 * value appropriately. */
4690 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4691 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4696 ping_evictor_stop();
4697 mdt_stop_ptlrpc_service(m);
4699 target_recovery_fini(obd);
4700 #ifdef HAVE_QUOTA_SUPPORT
4701 next->md_ops->mdo_quota.mqo_cleanup(env, next);
4704 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4705 mdt_obd_llog_cleanup(obd);
4707 mdt_fs_cleanup(env, m);
4709 cfs_timer_disarm(&m->mdt_ck_timer);
4710 mdt_ck_thread_stop(m);
4712 upcall_cache_cleanup(m->mdt_identity_cache);
4713 m->mdt_identity_cache = NULL;
4714 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4715 obd->obd_namespace = m->mdt_namespace = NULL;
4717 mdt_seq_fini(env, m);
4719 mdt_fld_fini(env, m);
4721 lut_fini(env, &m->mdt_lut);
4723 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4726 if (obd->obd_proc_exports_entry) {
4727 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
4728 obd->obd_proc_exports_entry = NULL;
4730 ptlrpc_lprocfs_unregister_obd(obd);
4731 lprocfs_obd_cleanup(obd);
4738 server_put_mount_2(dev, lmi->lmi_mnt);
4742 /* used by MGS to process specific configurations */
4743 static int mdt_process_config(const struct lu_env *env,
4744 struct lu_device *d, struct lustre_cfg *cfg)
4746 struct mdt_device *m = mdt_dev(d);
4747 struct md_device *md_next = m->mdt_child;
4748 struct lu_device *next = md2lu_dev(md_next);
4752 switch (cfg->lcfg_command) {
4754 struct lprocfs_static_vars lvars;
4755 struct obd_device *obd = d->ld_obd;
4758 * For interoperability between 1.8 and 2.0,
4759 * skip old "mdt.group_upcall" param.
4762 char *param = lustre_cfg_string(cfg, 1);
4763 if (param && !strncmp("mdt.group_upcall", param, 16)) {
4764 CWARN("For 1.8 interoperability, skip this"
4765 " mdt.group_upcall. It is obsolete\n");
4770 lprocfs_mdt_init_vars(&lvars);
4771 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4773 if (rc > 0 || rc == -ENOSYS)
4774 /* we don't understand; pass it on */
4775 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4780 * Add mdc hook to get first MDT uuid and connect it to
4781 * ls->controller to use for seq manager.
4783 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4785 CERROR("Can't add mdc, rc %d\n", rc);
4787 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4790 /* others are passed further */
4791 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4797 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4798 const struct lu_object_header *hdr,
4799 struct lu_device *d)
4801 struct mdt_object *mo;
4807 struct lu_object *o;
4808 struct lu_object_header *h;
4810 o = &mo->mot_obj.mo_lu;
4811 h = &mo->mot_header;
4812 lu_object_header_init(h);
4813 lu_object_init(o, h, d);
4814 lu_object_add_top(h, o);
4815 o->lo_ops = &mdt_obj_ops;
4821 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4822 const struct lu_object_conf *unused)
4824 struct mdt_device *d = mdt_dev(o->lo_dev);
4825 struct lu_device *under;
4826 struct lu_object *below;
4830 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4831 PFID(lu_object_fid(o)));
4833 under = &d->mdt_child->md_lu_dev;
4834 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4835 if (below != NULL) {
4836 lu_object_add(o, below);
4843 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4845 struct mdt_object *mo = mdt_obj(o);
4846 struct lu_object_header *h;
4850 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4851 PFID(lu_object_fid(o)));
4854 lu_object_header_fini(h);
4859 static int mdt_object_print(const struct lu_env *env, void *cookie,
4860 lu_printer_t p, const struct lu_object *o)
4862 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4863 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch=%llu "
4864 "flags=%llx, epochcount=%d, writecount=%d)",
4865 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4866 mdto->mot_epochcount, mdto->mot_writecount);
4869 static const struct lu_device_operations mdt_lu_ops = {
4870 .ldo_object_alloc = mdt_object_alloc,
4871 .ldo_process_config = mdt_process_config,
4874 static const struct lu_object_operations mdt_obj_ops = {
4875 .loo_object_init = mdt_object_init,
4876 .loo_object_free = mdt_object_free,
4877 .loo_object_print = mdt_object_print
4880 static int mdt_obd_set_info_async(struct obd_export *exp,
4881 __u32 keylen, void *key,
4882 __u32 vallen, void *val,
4883 struct ptlrpc_request_set *set)
4885 struct obd_device *obd = exp->exp_obd;
4891 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4892 rc = mdt_adapt_sptlrpc_conf(obd, 0);
4899 /* mds_connect_internal */
4900 static int mdt_connect_internal(struct obd_export *exp,
4901 struct mdt_device *mdt,
4902 struct obd_connect_data *data)
4905 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4906 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4908 /* If no known bits (which should not happen, probably,
4909 as everybody should support LOOKUP and UPDATE bits at least)
4910 revert to compat mode with plain locks. */
4911 if (!data->ocd_ibits_known &&
4912 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4913 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4915 if (!mdt->mdt_opts.mo_acl)
4916 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4918 if (!mdt->mdt_opts.mo_user_xattr)
4919 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4921 if (!mdt->mdt_som_conf)
4922 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
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);
4946 if (mdt->mdt_som_conf &&
4947 !(exp->exp_connect_flags & OBD_CONNECT_MDS_MDS) &&
4948 !(exp->exp_connect_flags & OBD_CONNECT_SOM)) {
4949 CWARN("%s: MDS has SOM enabled, but client does not support "
4950 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4957 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
4958 struct obd_export *exp,
4959 struct ptlrpc_request *req)
4961 struct sptlrpc_flavor flvr;
4964 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
4965 read_lock(&mdt->mdt_sptlrpc_lock);
4966 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
4970 read_unlock(&mdt->mdt_sptlrpc_lock);
4972 spin_lock(&exp->exp_lock);
4974 exp->exp_sp_peer = req->rq_sp_from;
4975 exp->exp_flvr = flvr;
4977 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
4978 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4979 CERROR("unauthorized rpc flavor %x from %s, "
4980 "expect %x\n", req->rq_flvr.sf_rpc,
4981 libcfs_nid2str(req->rq_peer.nid),
4982 exp->exp_flvr.sf_rpc);
4986 spin_unlock(&exp->exp_lock);
4988 if (exp->exp_sp_peer != req->rq_sp_from) {
4989 CERROR("RPC source %s doesn't match %s\n",
4990 sptlrpc_part2name(req->rq_sp_from),
4991 sptlrpc_part2name(exp->exp_sp_peer));
4994 rc = sptlrpc_target_export_check(exp, req);
5001 /* mds_connect copy */
5002 static int mdt_obd_connect(const struct lu_env *env,
5003 struct obd_export **exp, struct obd_device *obd,
5004 struct obd_uuid *cluuid,
5005 struct obd_connect_data *data,
5008 struct mdt_thread_info *info;
5009 struct lsd_client_data *lcd;
5010 struct obd_export *lexp;
5011 struct lustre_handle conn = { 0 };
5012 struct mdt_device *mdt;
5013 struct ptlrpc_request *req;
5017 LASSERT(env != NULL);
5018 if (!exp || !obd || !cluuid)
5021 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5022 req = info->mti_pill->rc_req;
5023 mdt = mdt_dev(obd->obd_lu_dev);
5025 rc = class_connect(&conn, obd, cluuid);
5029 lexp = class_conn2export(&conn);
5030 LASSERT(lexp != NULL);
5032 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5036 rc = mdt_connect_internal(lexp, mdt, data);
5040 struct mdt_thread_info *mti;
5041 mti = lu_context_key_get(&env->le_ctx,
5043 LASSERT(mti != NULL);
5044 mti->mti_exp = lexp;
5045 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5046 lexp->exp_mdt_data.med_lcd = lcd;
5047 rc = mdt_client_new(env, mdt);
5050 lexp->exp_mdt_data.med_lcd = NULL;
5052 mdt_export_stats_init(obd, lexp, localdata);
5060 class_disconnect(lexp);
5067 static int mdt_obd_reconnect(const struct lu_env *env,
5068 struct obd_export *exp, struct obd_device *obd,
5069 struct obd_uuid *cluuid,
5070 struct obd_connect_data *data,
5073 struct mdt_thread_info *info;
5074 struct mdt_device *mdt;
5075 struct ptlrpc_request *req;
5079 if (exp == NULL || obd == NULL || cluuid == NULL)
5082 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5083 req = info->mti_pill->rc_req;
5084 mdt = mdt_dev(obd->obd_lu_dev);
5086 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5090 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5092 mdt_export_stats_init(obd, exp, localdata);
5096 static int mdt_export_cleanup(struct obd_export *exp)
5098 struct mdt_export_data *med = &exp->exp_mdt_data;
5099 struct obd_device *obd = exp->exp_obd;
5100 struct mdt_device *mdt;
5101 struct mdt_thread_info *info;
5103 CFS_LIST_HEAD(closing_list);
5104 struct mdt_file_data *mfd, *n;
5108 spin_lock(&med->med_open_lock);
5109 while (!list_empty(&med->med_open_head)) {
5110 struct list_head *tmp = med->med_open_head.next;
5111 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5113 /* Remove mfd handle so it can't be found again.
5114 * We are consuming the mfd_list reference here. */
5115 class_handle_unhash(&mfd->mfd_handle);
5116 list_move_tail(&mfd->mfd_list, &closing_list);
5118 spin_unlock(&med->med_open_lock);
5119 mdt = mdt_dev(obd->obd_lu_dev);
5120 LASSERT(mdt != NULL);
5122 rc = lu_env_init(&env, LCT_MD_THREAD);
5126 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5127 LASSERT(info != NULL);
5128 memset(info, 0, sizeof *info);
5129 info->mti_env = &env;
5130 info->mti_mdt = mdt;
5131 info->mti_exp = exp;
5133 if (!list_empty(&closing_list)) {
5134 struct md_attr *ma = &info->mti_attr;
5138 lmm_size = mdt->mdt_max_mdsize;
5139 OBD_ALLOC(ma->ma_lmm, lmm_size);
5140 if (ma->ma_lmm == NULL)
5141 GOTO(out_lmm, rc = -ENOMEM);
5143 cookie_size = mdt->mdt_max_cookiesize;
5144 OBD_ALLOC(ma->ma_cookie, cookie_size);
5145 if (ma->ma_cookie == NULL)
5146 GOTO(out_cookie, rc = -ENOMEM);
5148 /* Close any open files (which may also cause orphan unlinking). */
5149 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5150 list_del_init(&mfd->mfd_list);
5151 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
5152 ma->ma_lmm_size = lmm_size;
5153 ma->ma_cookie_size = cookie_size;
5155 /* It is not for setattr, just tell MDD to send
5156 * DESTROY RPC to OSS if needed */
5157 ma->ma_attr_flags = MDS_CLOSE_CLEANUP;
5158 ma->ma_valid = MA_FLAGS;
5159 mdt_mfd_close(info, mfd);
5161 OBD_FREE(ma->ma_cookie, cookie_size);
5162 ma->ma_cookie = NULL;
5164 OBD_FREE(ma->ma_lmm, lmm_size);
5168 info->mti_mdt = NULL;
5169 /* cleanup client slot early */
5170 mdt_client_del(&env, mdt);
5176 static int mdt_obd_disconnect(struct obd_export *exp)
5182 class_export_get(exp);
5184 rc = server_disconnect_export(exp);
5186 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5188 rc = mdt_export_cleanup(exp);
5189 class_export_put(exp);
5193 /* FIXME: Can we avoid using these two interfaces? */
5194 static int mdt_init_export(struct obd_export *exp)
5196 struct mdt_export_data *med = &exp->exp_mdt_data;
5200 CFS_INIT_LIST_HEAD(&med->med_open_head);
5201 spin_lock_init(&med->med_open_lock);
5202 sema_init(&med->med_idmap_sem, 1);
5203 med->med_idmap = NULL;
5204 spin_lock(&exp->exp_lock);
5205 exp->exp_connecting = 1;
5206 spin_unlock(&exp->exp_lock);
5207 rc = ldlm_init_export(exp);
5209 CERROR("Error %d while initializing export\n", rc);
5213 static int mdt_destroy_export(struct obd_export *exp)
5215 struct mdt_export_data *med;
5219 med = &exp->exp_mdt_data;
5220 if (exp_connect_rmtclient(exp))
5221 mdt_cleanup_idmap(&exp->exp_mdt_data);
5223 target_destroy_export(exp);
5224 ldlm_destroy_export(exp);
5226 LASSERT(list_empty(&exp->exp_outstanding_replies));
5227 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5228 if (obd_uuid_equals(&exp->exp_client_uuid, &exp->exp_obd->obd_uuid))
5234 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
5236 if (flag & CONFIG_LOG)
5237 m->mdt_fl_cfglog = 1;
5239 /* also notify active event */
5240 if (flag & CONFIG_SYNC)
5241 m->mdt_fl_synced = 1;
5243 if (m->mdt_fl_cfglog && m->mdt_fl_synced)
5244 /* Open for clients */
5245 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
5248 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
5249 enum md_upcall_event ev, void *data)
5251 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
5252 struct md_device *next = m->mdt_child;
5253 struct mdt_thread_info *mti;
5259 rc = next->md_ops->mdo_maxsize_get(env, next,
5261 &m->mdt_max_cookiesize);
5262 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
5263 m->mdt_max_mdsize, m->mdt_max_cookiesize);
5264 mdt_allow_cli(m, CONFIG_SYNC);
5266 (*(__u64 *)data) = m->mdt_mount_count;
5269 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5270 mti->mti_no_need_trans = 1;
5271 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
5274 /* Check that MDT is not yet configured */
5275 LASSERT(!m->mdt_fl_cfglog);
5277 #ifdef HAVE_QUOTA_SUPPORT
5279 if (md->md_lu_dev.ld_obd->obd_recovering == 0 &&
5280 likely(md->md_lu_dev.ld_obd->obd_stopping == 0))
5281 next->md_ops->mdo_quota.mqo_recovery(env, next);
5285 CERROR("invalid event\n");
5292 static int mdt_obd_notify(struct obd_device *host,
5293 struct obd_device *watched,
5294 enum obd_notify_event ev, void *data)
5296 struct mdt_device *mdt = mdt_dev(host->obd_lu_dev);
5297 #ifdef HAVE_QUOTA_SUPPORT
5298 struct md_device *next = mdt->mdt_child;
5303 case OBD_NOTIFY_CONFIG:
5304 mdt_allow_cli(mdt, (unsigned long)data);
5306 #ifdef HAVE_QUOTA_SUPPORT
5307 /* quota_type has been processed, we can now handle
5308 * incoming quota requests */
5309 next->md_ops->mdo_quota.mqo_notify(NULL, next);
5313 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
5318 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5319 void *val, int vallen)
5321 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5322 struct getinfo_fid2path *fpout, *fpin;
5325 fpin = key + size_round(sizeof(KEY_FID2PATH));
5328 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5329 lustre_swab_fid2path(fpin);
5331 memcpy(fpout, fpin, sizeof(*fpin));
5332 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5335 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5339 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5340 struct getinfo_fid2path *fp)
5342 struct mdt_object *obj;
5346 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5347 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5349 if (!fid_is_sane(&fp->gf_fid))
5352 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5353 if (obj == NULL || IS_ERR(obj)) {
5354 CDEBUG(D_IOCTL, "no object "DFID": %ld\n",PFID(&fp->gf_fid),
5359 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5365 mdt_object_put(env, obj);
5366 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5367 PFID(&fp->gf_fid), rc);
5371 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5372 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5373 mdt_object_put(env, obj);
5378 static int mdt_get_info(struct mdt_thread_info *info)
5380 struct ptlrpc_request *req = mdt_info_req(info);
5388 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5390 CDEBUG(D_IOCTL, "No GETINFO key");
5393 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5396 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5397 if (vallen == NULL) {
5398 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5402 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5404 rc = req_capsule_server_pack(info->mti_pill);
5405 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5406 if (valout == NULL) {
5407 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5411 if (KEY_IS(KEY_FID2PATH))
5412 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5416 lustre_msg_set_status(req->rq_repmsg, rc);
5421 /* Pass the ioc down */
5422 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5423 unsigned int cmd, int len, void *data)
5425 struct lu_context ioctl_session;
5426 struct md_device *next = mdt->mdt_child;
5430 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5433 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5434 lu_context_enter(&ioctl_session);
5435 env->le_ses = &ioctl_session;
5437 LASSERT(next->md_ops->mdo_iocontrol);
5438 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5440 lu_context_exit(&ioctl_session);
5441 lu_context_fini(&ioctl_session);
5445 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5447 struct obd_ioctl_data *data = karg;
5448 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5450 struct mdt_object *obj;
5451 struct mdt_lock_handle *lh;
5454 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5455 if (!fid_is_sane(fid))
5458 lh = &mti->mti_lh[MDT_LH_PARENT];
5459 mdt_lock_reg_init(lh, LCK_CR);
5461 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5463 RETURN(PTR_ERR(obj));
5465 rc = mdt_object_exists(obj);
5469 * before calling version get the correct MDS should be
5470 * fid, this is error to find remote object here
5472 CERROR("nonlocal object "DFID"\n", PFID(fid));
5473 } else if (rc == 0) {
5475 CDEBUG(D_IOCTL, "no such object: "DFID"\n", PFID(fid));
5477 version = mo_version_get(mti->mti_env, mdt_object_child(obj));
5481 *(__u64 *)data->ioc_inlbuf2 = version;
5485 mdt_object_unlock_put(mti, obj, lh, 1);
5489 /* ioctls on obd dev */
5490 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5491 void *karg, void *uarg)
5494 struct obd_device *obd = exp->exp_obd;
5495 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5496 struct dt_device *dt = mdt->mdt_bottom;
5500 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5501 rc = lu_env_init(&env, LCT_MD_THREAD);
5507 rc = mdt_device_sync(&env, mdt);
5509 case OBD_IOC_SET_READONLY:
5510 dt->dd_ops->dt_ro(&env, dt);
5512 case OBD_IOC_ABORT_RECOVERY:
5513 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5514 target_stop_recovery_thread(obd);
5517 case OBD_IOC_CHANGELOG_REG:
5518 case OBD_IOC_CHANGELOG_DEREG:
5519 case OBD_IOC_CHANGELOG_CLEAR:
5520 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5522 case OBD_IOC_GET_OBJ_VERSION: {
5523 struct mdt_thread_info *mti;
5524 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5525 memset(mti, 0, sizeof *mti);
5526 mti->mti_env = &env;
5530 rc = mdt_ioc_version_get(mti, karg);
5534 CERROR("Not supported cmd = %d for device %s\n",
5535 cmd, obd->obd_name);
5543 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5545 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5546 struct obd_device *obd = mdt2obd_dev(mdt);
5547 #ifdef HAVE_QUOTA_SUPPORT
5548 struct md_device *next = mdt->mdt_child;
5552 /* if some clients didn't participate in recovery then we can possibly
5553 * lost sequence. Now we should increase sequence for safe value */
5554 lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
5555 mdt_seq_adjust(env, mdt, lost);
5557 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5558 #ifdef HAVE_QUOTA_SUPPORT
5559 if (likely(obd->obd_stopping == 0))
5560 next->md_ops->mdo_quota.mqo_recovery(env, next);
5565 int mdt_obd_postrecov(struct obd_device *obd)
5570 rc = lu_env_init(&env, LCT_MD_THREAD);
5573 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5579 * Send a copytool req to a client
5580 * Note this sends a request RPC from a server (MDT) to a client (MDC),
5581 * backwards of normal comms.
5583 int mdt_hsm_copytool_send(struct obd_export *exp)
5586 struct hsm_action_list *hal;
5587 struct hsm_action_item *hai;
5591 CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
5592 libcfs_nid2str(exp->exp_connection->c_peer.nid));
5594 len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
5595 /* for mockup below */ 2 * size_round(sizeof(*hai));
5600 lh->lnl_magic = LNL_MAGIC;
5601 lh->lnl_transport = LNL_TRANSPORT_HSM;
5602 lh->lnl_msgtype = HMT_ACTION_LIST;
5603 lh->lnl_msglen = len;
5605 hal = (struct hsm_action_list *)(lh + 1);
5606 hal->hal_version = HAL_VERSION;
5607 hal->hal_archive_num = 1;
5608 obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
5611 /* mock up an action list */
5613 hai = hai_zero(hal);
5614 hai->hai_action = HSMA_ARCHIVE;
5615 hai->hai_fid.f_oid = 0xA00A;
5616 hai->hai_len = sizeof(*hai);
5617 hai = hai_next(hai);
5618 hai->hai_action = HSMA_RESTORE;
5619 hai->hai_fid.f_oid = 0xB00B;
5620 hai->hai_len = sizeof(*hai);
5622 /* Uses the ldlm reverse import; this rpc will be seen by
5623 the ldlm_callback_handler */
5624 rc = target_set_info_rpc(exp->exp_imp_reverse, LDLM_SET_INFO,
5625 sizeof(KEY_HSM_COPYTOOL_SEND),
5626 KEY_HSM_COPYTOOL_SEND,
5634 static struct obd_ops mdt_obd_device_ops = {
5635 .o_owner = THIS_MODULE,
5636 .o_set_info_async = mdt_obd_set_info_async,
5637 .o_connect = mdt_obd_connect,
5638 .o_reconnect = mdt_obd_reconnect,
5639 .o_disconnect = mdt_obd_disconnect,
5640 .o_init_export = mdt_init_export,
5641 .o_destroy_export = mdt_destroy_export,
5642 .o_iocontrol = mdt_iocontrol,
5643 .o_postrecov = mdt_obd_postrecov,
5644 .o_notify = mdt_obd_notify
5647 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5648 struct lu_device *d)
5650 struct mdt_device *m = mdt_dev(d);
5657 static struct lu_device *mdt_device_free(const struct lu_env *env,
5658 struct lu_device *d)
5660 struct mdt_device *m = mdt_dev(d);
5663 md_device_fini(&m->mdt_md_dev);
5668 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5669 struct lu_device_type *t,
5670 struct lustre_cfg *cfg)
5672 struct lu_device *l;
5673 struct mdt_device *m;
5679 l = &m->mdt_md_dev.md_lu_dev;
5680 rc = mdt_init0(env, m, t, cfg);
5682 mdt_device_free(env, l);
5686 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
5688 l = ERR_PTR(-ENOMEM);
5692 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5693 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
5695 /* context key: mdt_thread_key */
5696 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5698 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
5699 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
5701 struct lu_context_key mdt_txn_key = {
5702 .lct_tags = LCT_TX_HANDLE,
5703 .lct_init = mdt_txn_key_init,
5704 .lct_fini = mdt_txn_key_fini
5707 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
5709 return md_ucred(info->mti_env);
5713 * Enable/disable COS (Commit On Sharing).
5715 * Set/Clear the COS flag in mdt options.
5717 * \param mdt mdt device
5718 * \param val 0 disables COS, other values enable COS
5720 void mdt_enable_cos(struct mdt_device *mdt, int val)
5725 mdt->mdt_opts.mo_cos = !!val;
5726 rc = lu_env_init(&env, LCT_MD_THREAD);
5727 if (unlikely(rc != 0)) {
5728 CWARN("lu_env initialization failed with rc = %d,"
5729 "cannot sync\n", rc);
5732 mdt_device_sync(&env, mdt);
5737 * Check COS (Commit On Sharing) status.
5739 * Return COS flag status.
5741 * \param mdt mdt device
5743 int mdt_cos_is_enabled(struct mdt_device *mdt)
5745 return mdt->mdt_opts.mo_cos != 0;
5748 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
5749 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
5751 static struct lu_device_type_operations mdt_device_type_ops = {
5752 .ldto_init = mdt_type_init,
5753 .ldto_fini = mdt_type_fini,
5755 .ldto_start = mdt_type_start,
5756 .ldto_stop = mdt_type_stop,
5758 .ldto_device_alloc = mdt_device_alloc,
5759 .ldto_device_free = mdt_device_free,
5760 .ldto_device_fini = mdt_device_fini
5763 static struct lu_device_type mdt_device_type = {
5764 .ldt_tags = LU_DEVICE_MD,
5765 .ldt_name = LUSTRE_MDT_NAME,
5766 .ldt_ops = &mdt_device_type_ops,
5767 .ldt_ctx_tags = LCT_MD_THREAD
5770 static struct lu_local_obj_desc mdt_last_recv = {
5771 .llod_name = LAST_RCVD,
5772 .llod_oid = MDT_LAST_RECV_OID,
5776 static int __init mdt_mod_init(void)
5778 struct lprocfs_static_vars lvars;
5781 llo_local_obj_register(&mdt_last_recv);
5783 mdt_num_threads = MDT_NUM_THREADS;
5784 lprocfs_mdt_init_vars(&lvars);
5785 rc = class_register_type(&mdt_obd_device_ops, NULL,
5786 lvars.module_vars, LUSTRE_MDT_NAME,
5792 static void __exit mdt_mod_exit(void)
5794 llo_local_obj_unregister(&mdt_last_recv);
5795 class_unregister_type(LUSTRE_MDT_NAME);
5799 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
5800 [prefix ## _ ## opc - prefix ## _ ## base] = { \
5802 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
5803 .mh_opc = prefix ## _ ## opc, \
5804 .mh_flags = flags, \
5809 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
5810 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
5812 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
5813 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
5815 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
5816 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
5818 * Request with a format known in advance
5820 #define DEF_MDT_HNDL_F(flags, name, fn) \
5821 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
5823 #define DEF_SEQ_HNDL_F(flags, name, fn) \
5824 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
5826 #define DEF_FLD_HNDL_F(flags, name, fn) \
5827 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
5829 * Request with a format we do not yet know
5831 #define DEF_MDT_HNDL_0(flags, name, fn) \
5832 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
5834 static struct mdt_handler mdt_mds_ops[] = {
5835 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5836 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
5837 DEF_MDT_HNDL (0, SET_INFO, mdt_set_info,
5839 DEF_MDT_HNDL_F(0, GET_INFO, mdt_get_info),
5840 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
5841 DEF_MDT_HNDL_F(HABEO_CORPUS, GETATTR, mdt_getattr),
5842 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
5843 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
5844 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
5845 DEF_MDT_HNDL_F(0 |MUTABOR, REINT, mdt_reint),
5846 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
5847 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
5848 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
5849 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
5850 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
5851 #ifdef HAVE_QUOTA_SUPPORT
5852 DEF_MDT_HNDL_F(0, QUOTACHECK, mdt_quotacheck_handle),
5853 DEF_MDT_HNDL_F(0, QUOTACTL, mdt_quotactl_handle)
5857 #define DEF_OBD_HNDL(flags, name, fn) \
5858 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
5861 static struct mdt_handler mdt_obd_ops[] = {
5862 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
5863 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
5864 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
5867 #define DEF_DLM_HNDL_0(flags, name, fn) \
5868 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
5869 #define DEF_DLM_HNDL_F(flags, name, fn) \
5870 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
5872 static struct mdt_handler mdt_dlm_ops[] = {
5873 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
5874 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
5875 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
5876 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
5879 #define DEF_LLOG_HNDL(flags, name, fn) \
5880 DEF_HNDL(LLOG, ORIGIN_HANDLE_CREATE, _NET, flags, name, fn, NULL)
5882 static struct mdt_handler mdt_llog_ops[] = {
5883 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CREATE, mdt_llog_create),
5884 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
5885 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
5886 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_WRITE_REC, NULL),
5887 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CLOSE, NULL),
5888 DEF_LLOG_HNDL(0, ORIGIN_CONNECT, NULL),
5889 DEF_LLOG_HNDL(0, CATINFO, NULL),
5890 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
5891 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
5894 #define DEF_SEC_CTX_HNDL(name, fn) \
5895 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
5897 static struct mdt_handler mdt_sec_ctx_ops[] = {
5898 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
5899 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
5900 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
5903 static struct mdt_opc_slice mdt_regular_handlers[] = {
5905 .mos_opc_start = MDS_GETATTR,
5906 .mos_opc_end = MDS_LAST_OPC,
5907 .mos_hs = mdt_mds_ops
5910 .mos_opc_start = OBD_PING,
5911 .mos_opc_end = OBD_LAST_OPC,
5912 .mos_hs = mdt_obd_ops
5915 .mos_opc_start = LDLM_ENQUEUE,
5916 .mos_opc_end = LDLM_LAST_OPC,
5917 .mos_hs = mdt_dlm_ops
5920 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
5921 .mos_opc_end = LLOG_LAST_OPC,
5922 .mos_hs = mdt_llog_ops
5925 .mos_opc_start = SEC_CTX_INIT,
5926 .mos_opc_end = SEC_LAST_OPC,
5927 .mos_hs = mdt_sec_ctx_ops
5934 static struct mdt_handler mdt_readpage_ops[] = {
5935 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5936 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
5937 #ifdef HAVE_SPLIT_SUPPORT
5938 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
5942 * XXX: this is ugly and should be fixed one day, see mdc_close() for
5943 * detailed comments. --umka
5945 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
5946 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
5949 static struct mdt_opc_slice mdt_readpage_handlers[] = {
5951 .mos_opc_start = MDS_GETATTR,
5952 .mos_opc_end = MDS_LAST_OPC,
5953 .mos_hs = mdt_readpage_ops
5960 static struct mdt_handler mdt_xmds_ops[] = {
5961 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5962 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
5963 DEF_MDT_HNDL_F(0 | MUTABOR , REINT, mdt_reint),
5964 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
5967 static struct mdt_opc_slice mdt_xmds_handlers[] = {
5969 .mos_opc_start = MDS_GETATTR,
5970 .mos_opc_end = MDS_LAST_OPC,
5971 .mos_hs = mdt_xmds_ops
5974 .mos_opc_start = OBD_PING,
5975 .mos_opc_end = OBD_LAST_OPC,
5976 .mos_hs = mdt_obd_ops
5979 .mos_opc_start = SEC_CTX_INIT,
5980 .mos_opc_end = SEC_LAST_OPC,
5981 .mos_hs = mdt_sec_ctx_ops
5988 static struct mdt_handler mdt_seq_ops[] = {
5989 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
5992 static struct mdt_opc_slice mdt_seq_handlers[] = {
5994 .mos_opc_start = SEQ_QUERY,
5995 .mos_opc_end = SEQ_LAST_OPC,
5996 .mos_hs = mdt_seq_ops
6003 static struct mdt_handler mdt_fld_ops[] = {
6004 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
6007 static struct mdt_opc_slice mdt_fld_handlers[] = {
6009 .mos_opc_start = FLD_QUERY,
6010 .mos_opc_end = FLD_LAST_OPC,
6011 .mos_hs = mdt_fld_ops
6018 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6019 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
6020 MODULE_LICENSE("GPL");
6022 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
6023 "number of mdt service threads to start");
6025 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);