1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/mdt/mdt_handler.c
5 * Lustre Metadata Target (mdt) request handler
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
11 * Author: Mike Shaver <shaver@clusterfs.com>
12 * Author: Nikita Danilov <nikita@clusterfs.com>
13 * Author: Huang Hua <huanghua@clusterfs.com>
14 * Author: Yury Umanets <umka@clusterfs.com>
16 * This file is part of the Lustre file system, http://www.lustre.org
17 * Lustre is a trademark of Cluster File Systems, Inc.
19 * You may have signed or agreed to another license before downloading
20 * this software. If so, you are bound by the terms and conditions
21 * of that agreement, and the following does not apply to you. See the
22 * LICENSE file included with this distribution for more information.
24 * If you did not agree to a different license, then this copy of Lustre
25 * is open source software; you can redistribute it and/or modify it
26 * under the terms of version 2 of the GNU General Public License as
27 * published by the Free Software Foundation.
29 * In either case, Lustre is distributed in the hope that it will be
30 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * license text for more details.
36 # define EXPORT_SYMTAB
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <linux/module.h>
42 * struct OBD_{ALLOC,FREE}*()
44 #include <obd_support.h>
45 /* struct ptlrpc_request */
46 #include <lustre_net.h>
47 /* struct obd_export */
48 #include <lustre_export.h>
49 /* struct obd_device */
52 #include <dt_object.h>
53 #include <lustre_mds.h>
54 #include <lustre_mdt.h>
55 #include "mdt_internal.h"
56 #include <linux/lustre_acl.h>
57 #include <lustre_param.h>
59 mdl_mode_t mdt_mdl_lock_modes[] = {
60 [LCK_MINMODE] = MDL_MINMODE,
67 [LCK_GROUP] = MDL_GROUP
70 ldlm_mode_t mdt_dlm_lock_modes[] = {
71 [MDL_MINMODE] = LCK_MINMODE,
78 [MDL_GROUP] = LCK_GROUP
82 * Initialized in mdt_mod_init().
84 unsigned long mdt_num_threads;
86 /* ptlrpc request handler for MDT. All handlers are
87 * grouped into several slices - struct mdt_opc_slice,
88 * and stored in an array - mdt_handlers[].
91 /* The name of this handler. */
93 /* Fail id for this handler, checked at the beginning of this handler*/
95 /* Operation code for this handler */
97 /* flags are listed in enum mdt_handler_flags below. */
99 /* The actual handler function to execute. */
100 int (*mh_act)(struct mdt_thread_info *info);
101 /* Request format for this request. */
102 const struct req_format *mh_fmt;
105 enum mdt_handler_flags {
107 * struct mdt_body is passed in the incoming message, and object
108 * identified by this fid exists on disk.
110 * "habeo corpus" == "I have a body"
112 HABEO_CORPUS = (1 << 0),
114 * struct ldlm_request is passed in the incoming message.
116 * "habeo clavis" == "I have a key"
118 HABEO_CLAVIS = (1 << 1),
120 * this request has fixed reply format, so that reply message can be
121 * packed by generic code.
123 * "habeo refero" == "I have a reply"
125 HABEO_REFERO = (1 << 2),
127 * this request will modify something, so check whether the filesystem
128 * is readonly or not, then return -EROFS to client asap if necessary.
130 * "mutabor" == "I shall modify"
135 struct mdt_opc_slice {
138 struct mdt_handler *mos_hs;
141 static struct mdt_opc_slice mdt_regular_handlers[];
142 static struct mdt_opc_slice mdt_readpage_handlers[];
143 static struct mdt_opc_slice mdt_xmds_handlers[];
144 static struct mdt_opc_slice mdt_seq_handlers[];
145 static struct mdt_opc_slice mdt_fld_handlers[];
147 static struct mdt_device *mdt_dev(struct lu_device *d);
148 static int mdt_regular_handle(struct ptlrpc_request *req);
149 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
151 static struct lu_object_operations mdt_obj_ops;
153 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
157 return (rep->lock_policy_res1 & flag);
160 void mdt_clear_disposition(struct mdt_thread_info *info,
161 struct ldlm_reply *rep, int flag)
164 info->mti_opdata &= ~flag;
166 rep->lock_policy_res1 &= ~flag;
169 void mdt_set_disposition(struct mdt_thread_info *info,
170 struct ldlm_reply *rep, int flag)
173 info->mti_opdata |= flag;
175 rep->lock_policy_res1 |= flag;
178 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
180 lh->mlh_pdo_hash = 0;
181 lh->mlh_reg_mode = lm;
182 lh->mlh_type = MDT_REG_LOCK;
185 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
186 const char *name, int namelen)
188 lh->mlh_reg_mode = lm;
189 lh->mlh_type = MDT_PDO_LOCK;
192 LASSERT(namelen > 0);
193 lh->mlh_pdo_hash = full_name_hash(name, namelen);
195 LASSERT(namelen == 0);
196 lh->mlh_pdo_hash = 0ull;
200 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
201 struct mdt_lock_handle *lh)
207 * Any dir access needs couple of locks:
209 * 1) on part of dir we gonna take lookup/modify;
211 * 2) on whole dir to protect it from concurrent splitting and/or to
212 * flush client's cache for readdir().
214 * so, for a given mode and object this routine decides what lock mode
215 * to use for lock #2:
217 * 1) if caller's gonna lookup in dir then we need to protect dir from
218 * being splitted only - LCK_CR
220 * 2) if caller's gonna modify dir then we need to protect dir from
221 * being splitted and to flush cache - LCK_CW
223 * 3) if caller's gonna modify dir and that dir seems ready for
224 * splitting then we need to protect it from any type of access
225 * (lookup/modify/split) - LCK_EX --bzzz
228 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
229 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
232 * Ask underlaying level its opinion about preferable PDO lock mode
233 * having access type passed as regular lock mode:
235 * - MDL_MINMODE means that lower layer does not want to specify lock
238 * - MDL_NL means that no PDO lock should be taken. This is used in some
239 * cases. Say, for non-splittable directories no need to use PDO locks
242 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
243 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
245 if (mode != MDL_MINMODE) {
246 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
249 * Lower layer does not want to specify locking mode. We do it
250 * our selves. No special protection is needed, just flush
251 * client's cache on modification and allow concurrent
254 switch (lh->mlh_reg_mode) {
256 lh->mlh_pdo_mode = LCK_EX;
259 lh->mlh_pdo_mode = LCK_CR;
262 lh->mlh_pdo_mode = LCK_CW;
265 CERROR("Not expected lock type (0x%x)\n",
266 (int)lh->mlh_reg_mode);
271 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
275 static int mdt_getstatus(struct mdt_thread_info *info)
277 struct mdt_device *mdt = info->mti_mdt;
278 struct md_device *next = mdt->mdt_child;
279 struct mdt_body *repbody;
284 rc = mdt_check_ucred(info);
286 RETURN(err_serious(rc));
288 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
289 RETURN(err_serious(-ENOMEM));
291 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
292 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
296 repbody->valid |= OBD_MD_FLID;
298 if (mdt->mdt_opts.mo_mds_capa) {
299 struct mdt_object *root;
300 struct lustre_capa *capa;
302 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
304 RETURN(PTR_ERR(root));
306 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
308 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
310 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
312 mdt_object_put(info->mti_env, root);
314 repbody->valid |= OBD_MD_FLMDSCAPA;
320 static int mdt_statfs(struct mdt_thread_info *info)
322 struct md_device *next = info->mti_mdt->mdt_child;
323 struct obd_statfs *osfs;
328 /* This will trigger a watchdog timeout */
329 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
330 (MDT_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
332 rc = mdt_check_ucred(info);
334 RETURN(err_serious(rc));
336 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
337 rc = err_serious(-ENOMEM);
339 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
340 rc = next->md_ops->mdo_statfs(info->mti_env, next,
342 statfs_pack(osfs, &info->mti_u.ksfs);
347 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
350 struct lu_attr *attr = &info->mti_attr.ma_attr;
352 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
354 /* Check if Size-on-MDS is enabled. */
355 if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
356 S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
357 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
358 b->size = attr->la_size;
359 b->blocks = attr->la_blocks;
363 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
364 const struct lu_attr *attr, const struct lu_fid *fid)
366 /*XXX should pack the reply body according to lu_valid*/
367 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
368 OBD_MD_FLGID | OBD_MD_FLTYPE |
369 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
370 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
372 if (!S_ISREG(attr->la_mode))
373 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
375 b->atime = attr->la_atime;
376 b->mtime = attr->la_mtime;
377 b->ctime = attr->la_ctime;
378 b->mode = attr->la_mode;
379 b->size = attr->la_size;
380 b->blocks = attr->la_blocks;
381 b->uid = attr->la_uid;
382 b->gid = attr->la_gid;
383 b->flags = attr->la_flags;
384 b->nlink = attr->la_nlink;
385 b->rdev = attr->la_rdev;
389 b->valid |= OBD_MD_FLID;
391 /* FIXME: these should be fixed when new igif ready.*/
392 b->ino = fid_oid(fid); /* 1.6 compatibility */
393 b->generation = fid_ver(fid); /* 1.6 compatibility */
394 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
396 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
397 PFID(fid), b->nlink, b->mode, b->size);
401 mdt_body_reverse_idmap(info, b);
404 static inline int mdt_body_has_lov(const struct lu_attr *la,
405 const struct mdt_body *body)
407 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
408 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
411 static int mdt_getattr_internal(struct mdt_thread_info *info,
412 struct mdt_object *o)
414 struct md_object *next = mdt_object_child(o);
415 const struct mdt_body *reqbody = info->mti_body;
416 struct ptlrpc_request *req = mdt_info_req(info);
417 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
418 struct md_attr *ma = &info->mti_attr;
419 struct lu_attr *la = &ma->ma_attr;
420 struct req_capsule *pill = info->mti_pill;
421 const struct lu_env *env = info->mti_env;
422 struct mdt_body *repbody;
423 struct lu_buf *buffer = &info->mti_buf;
427 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
428 RETURN(err_serious(-ENOMEM));
430 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
434 rc = mdt_object_exists(o);
436 /* This object is located on remote node.*/
437 repbody->fid1 = *mdt_object_fid(o);
438 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
442 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
443 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
445 /* If it is dir object and client require MEA, then we got MEA */
446 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
447 reqbody->valid & OBD_MD_MEA) {
448 /* Assumption: MDT_MD size is enough for lmv size. */
449 ma->ma_lmv = buffer->lb_buf;
450 ma->ma_lmv_size = buffer->lb_len;
451 ma->ma_need = MA_LMV | MA_INODE;
453 ma->ma_lmm = buffer->lb_buf;
454 ma->ma_lmm_size = buffer->lb_len;
455 ma->ma_need = MA_LOV | MA_INODE;
458 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
459 reqbody->valid & OBD_MD_FLDIREA &&
460 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
461 /* get default stripe info for this dir. */
462 ma->ma_need |= MA_LOV_DEF;
464 rc = mo_attr_get(env, next, ma);
466 CERROR("getattr error for "DFID": %d\n",
467 PFID(mdt_object_fid(o)), rc);
471 if (likely(ma->ma_valid & MA_INODE))
472 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
476 if (mdt_body_has_lov(la, reqbody)) {
477 if (ma->ma_valid & MA_LOV) {
478 LASSERT(ma->ma_lmm_size);
479 mdt_dump_lmm(D_INFO, ma->ma_lmm);
480 repbody->eadatasize = ma->ma_lmm_size;
481 if (S_ISDIR(la->la_mode))
482 repbody->valid |= OBD_MD_FLDIREA;
484 repbody->valid |= OBD_MD_FLEASIZE;
486 if (ma->ma_valid & MA_LMV) {
487 LASSERT(S_ISDIR(la->la_mode));
488 repbody->eadatasize = ma->ma_lmv_size;
489 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
491 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
492 repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
494 } else if (S_ISLNK(la->la_mode) &&
495 reqbody->valid & OBD_MD_LINKNAME) {
496 buffer->lb_buf = ma->ma_lmm;
497 buffer->lb_len = reqbody->eadatasize;
498 rc = mo_readlink(env, next, buffer);
499 if (unlikely(rc <= 0)) {
500 CERROR("readlink failed: %d\n", rc);
503 repbody->valid |= OBD_MD_LINKNAME;
504 repbody->eadatasize = rc;
506 ((char*)ma->ma_lmm)[rc - 1] = 0;
507 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
508 (char*)ma->ma_lmm, rc);
513 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
514 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
515 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
516 repbody->valid |= OBD_MD_FLMODEASIZE;
517 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
518 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
519 repbody->max_cookiesize);
522 if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
523 void *buf = req_capsule_server_get(pill, &RMF_ACL);
525 /* mdt_getattr_lock only */
526 rc = mdt_pack_remote_perm(info, o, buf);
528 repbody->valid &= ~OBD_MD_FLRMTPERM;
529 repbody->aclsize = 0;
532 repbody->valid |= OBD_MD_FLRMTPERM;
533 repbody->aclsize = sizeof(struct mdt_remote_perm);
536 #ifdef CONFIG_FS_POSIX_ACL
537 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
538 (reqbody->valid & OBD_MD_FLACL)) {
539 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
540 buffer->lb_len = req_capsule_get_size(pill,
541 &RMF_ACL, RCL_SERVER);
542 if (buffer->lb_len > 0) {
543 rc = mo_xattr_get(env, next, buffer,
544 XATTR_NAME_ACL_ACCESS);
546 if (rc == -ENODATA) {
547 repbody->aclsize = 0;
548 repbody->valid |= OBD_MD_FLACL;
550 } else if (rc == -EOPNOTSUPP) {
553 CERROR("got acl size: %d\n", rc);
556 repbody->aclsize = rc;
557 repbody->valid |= OBD_MD_FLACL;
564 if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
565 info->mti_mdt->mdt_opts.mo_mds_capa) {
566 struct lustre_capa *capa;
568 capa = req_capsule_server_get(pill, &RMF_CAPA1);
570 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
571 rc = mo_capa_get(env, next, capa, 0);
574 repbody->valid |= OBD_MD_FLMDSCAPA;
579 static int mdt_renew_capa(struct mdt_thread_info *info)
581 struct mdt_device *mdt = info->mti_mdt;
582 struct mdt_object *obj = info->mti_object;
583 struct mdt_body *body;
584 struct lustre_capa *capa, *c;
588 /* if object doesn't exist, or server has disabled capability,
589 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
592 if (!obj || !mdt->mdt_opts.mo_mds_capa)
595 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
596 LASSERT(body != NULL);
598 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
601 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
605 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
607 body->valid |= OBD_MD_FLOSSCAPA;
611 static int mdt_getattr(struct mdt_thread_info *info)
613 struct mdt_object *obj = info->mti_object;
614 struct req_capsule *pill = info->mti_pill;
615 struct mdt_body *reqbody;
616 struct mdt_body *repbody;
622 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
625 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
626 rc = req_capsule_server_pack(pill);
628 RETURN(err_serious(rc));
629 rc = mdt_renew_capa(info);
630 GOTO(out_shrink, rc);
633 LASSERT(obj != NULL);
634 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
636 mode = lu_object_attr(&obj->mot_obj.mo_lu);
637 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
638 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
639 md_size = reqbody->eadatasize;
641 md_size = info->mti_mdt->mdt_max_mdsize;
643 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
645 rc = req_capsule_server_pack(pill);
646 if (unlikely(rc != 0))
647 RETURN(err_serious(rc));
649 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
650 LASSERT(repbody != NULL);
651 repbody->eadatasize = 0;
652 repbody->aclsize = 0;
654 if (reqbody->valid & OBD_MD_FLRMTPERM)
655 rc = mdt_init_ucred(info, reqbody);
657 rc = mdt_check_ucred(info);
659 GOTO(out_shrink, rc);
661 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
662 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
665 * Don't check capability at all, because rename might getattr for
666 * remote obj, and at that time no capability is available.
668 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
669 rc = mdt_getattr_internal(info, obj);
670 if (reqbody->valid & OBD_MD_FLRMTPERM)
671 mdt_exit_ucred(info);
674 mdt_shrink_reply(info);
678 static int mdt_is_subdir(struct mdt_thread_info *info)
680 struct mdt_object *o = info->mti_object;
681 struct req_capsule *pill = info->mti_pill;
682 const struct mdt_body *body = info->mti_body;
683 struct mdt_body *repbody;
689 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
692 * We save last checked parent fid to @repbody->fid1 for remote
695 LASSERT(fid_is_sane(&body->fid2));
696 LASSERT(mdt_object_exists(o) > 0);
697 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
698 &body->fid2, &repbody->fid1);
699 if (rc == 0 || rc == -EREMOTE)
700 repbody->valid |= OBD_MD_FLID;
705 static int mdt_raw_lookup(struct mdt_thread_info *info,
706 struct mdt_object *parent,
707 const struct lu_name *lname,
708 struct ldlm_reply *ldlm_rep)
710 struct md_object *next = mdt_object_child(info->mti_object);
711 const struct mdt_body *reqbody = info->mti_body;
712 struct lu_fid *child_fid = &info->mti_tmp_fid1;
713 struct mdt_body *repbody;
717 if (reqbody->valid != OBD_MD_FLID)
720 LASSERT(!info->mti_cross_ref);
722 /* Only got the fid of this obj by name */
723 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
726 /* XXX is raw_lookup possible as intent operation? */
729 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
732 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
734 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
737 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
738 repbody->fid1 = *child_fid;
739 repbody->valid = OBD_MD_FLID;
745 * UPDATE lock should be taken against parent, and be release before exit;
746 * child_bits lock should be taken against child, and be returned back:
747 * (1)normal request should release the child lock;
748 * (2)intent request will grant the lock to client.
750 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
751 struct mdt_lock_handle *lhc,
753 struct ldlm_reply *ldlm_rep)
755 struct ptlrpc_request *req = mdt_info_req(info);
756 struct mdt_body *reqbody = NULL;
757 struct mdt_object *parent = info->mti_object;
758 struct mdt_object *child;
759 struct md_object *next = mdt_object_child(parent);
760 struct lu_fid *child_fid = &info->mti_tmp_fid1;
761 struct lu_name *lname = NULL;
764 struct mdt_lock_handle *lhp;
765 struct ldlm_lock *lock;
766 struct ldlm_res_id *res_id;
772 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
773 LASSERT(ergo(is_resent,
774 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
776 LASSERT(parent != NULL);
777 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
779 RETURN(err_serious(-EFAULT));
781 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
783 LASSERT(namelen >= 0);
785 /* XXX: "namelen == 0" is for getattr by fid (OBD_CONNECT_ATTRFID),
786 * otherwise do not allow empty name, that is the name must contain
787 * at least one character and the terminating '\0'*/
789 reqbody =req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
790 LASSERT(fid_is_sane(&reqbody->fid2));
793 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
795 PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
798 lname = mdt_name(info->mti_env, (char *)name, namelen);
799 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
801 PFID(mdt_object_fid(parent)), name, ldlm_rep);
804 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
806 rc = mdt_object_exists(parent);
807 if (unlikely(rc == 0)) {
808 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
809 &parent->mot_obj.mo_lu,
810 "Parent doesn't exist!\n");
813 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
814 PFID(mdt_object_fid(parent)));
817 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
825 if (info->mti_cross_ref) {
826 /* Only getattr on the child. Parent is on another node. */
827 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
829 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
830 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
833 /* Do not take lock for resent case. */
834 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
835 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
836 lhc->mlh_reg_lh.cookie);
837 LASSERT(fid_res_name_eq(mdt_object_fid(child),
838 &lock->l_resource->lr_name));
842 mdt_lock_handle_init(lhc);
843 mdt_lock_reg_init(lhc, LCK_PR);
846 * Object's name is on another MDS, no lookup lock is
847 * needed here but update is.
849 child_bits &= ~MDS_INODELOCK_LOOKUP;
850 child_bits |= MDS_INODELOCK_UPDATE;
852 rc = mdt_object_lock(info, child, lhc, child_bits,
856 /* Finally, we can get attr for child. */
857 mdt_set_capainfo(info, 0, mdt_object_fid(child),
859 rc = mdt_getattr_internal(info, child);
860 if (unlikely(rc != 0))
861 mdt_object_unlock(info, child, lhc, 1);
866 /* step 1: lock parent */
867 lhp = &info->mti_lh[MDT_LH_PARENT];
868 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
869 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
872 if (unlikely(rc != 0))
876 /* step 2: lookup child's fid by name */
877 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
882 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
883 GOTO(out_parent, rc);
885 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
887 *child_fid = reqbody->fid2;
888 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
892 *step 3: find the child object by fid & lock it.
893 * regardless if it is local or remote.
895 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
897 if (unlikely(IS_ERR(child)))
898 GOTO(out_parent, rc = PTR_ERR(child));
900 /* Do not take lock for resent case. */
901 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
902 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
903 lhc->mlh_reg_lh.cookie);
905 res_id = &lock->l_resource->lr_name;
906 if (!fid_res_name_eq(mdt_object_fid(child),
907 &lock->l_resource->lr_name)) {
908 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
909 &lock->l_resource->lr_name),
910 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
911 (unsigned long)res_id->name[0],
912 (unsigned long)res_id->name[1],
913 (unsigned long)res_id->name[2],
914 PFID(mdt_object_fid(parent)));
915 CWARN("Although resent, but still not get child lock"
916 "parent:"DFID" child:"DFID"\n",
917 PFID(mdt_object_fid(parent)),
918 PFID(mdt_object_fid(child)));
919 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
926 struct md_attr *ma = &info->mti_attr;
928 mdt_lock_handle_init(lhc);
929 mdt_lock_reg_init(lhc, LCK_PR);
931 if (mdt_object_exists(child) == 0) {
932 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
933 &child->mot_obj.mo_lu,
934 "Object doesn't exist!\n");
938 ma->ma_need = MA_INODE;
939 rc = mo_attr_get(info->mti_env, next, ma);
940 if (unlikely(rc != 0))
943 /* If the file has not been changed for some time, we return
944 * not only a LOOKUP lock, but also an UPDATE lock and this
945 * might save us RPC on later STAT. For directories, it also
946 * let negative dentry starts working for this dir. */
947 if (ma->ma_valid & MA_INODE &&
948 ma->ma_attr.la_valid & LA_CTIME &&
949 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
950 ma->ma_attr.la_ctime < cfs_time_current_sec())
951 child_bits |= MDS_INODELOCK_UPDATE;
953 rc = mdt_object_lock(info, child, lhc, child_bits,
956 if (unlikely(rc != 0))
960 /* finally, we can get attr for child. */
961 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
962 rc = mdt_getattr_internal(info, child);
963 if (unlikely(rc != 0)) {
964 mdt_object_unlock(info, child, lhc, 1);
966 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
968 struct mdt_body *repbody;
971 /* Debugging code. */
972 res_id = &lock->l_resource->lr_name;
973 LDLM_DEBUG(lock, "Returning lock to client\n");
974 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
975 &lock->l_resource->lr_name),
976 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
977 (unsigned long)res_id->name[0],
978 (unsigned long)res_id->name[1],
979 (unsigned long)res_id->name[2],
980 PFID(mdt_object_fid(child)));
982 * Pack Size-on-MDS inode attributes to the body if
983 * update lock is given.
985 repbody = req_capsule_server_get(info->mti_pill,
987 ma = &info->mti_attr.ma_attr;
988 if (lock->l_policy_data.l_inodebits.bits &
989 MDS_INODELOCK_UPDATE)
990 mdt_pack_size2body(info, child);
996 mdt_object_put(info->mti_env, child);
998 mdt_object_unlock(info, parent, lhp, 1);
1002 /* normal handler: should release the child lock */
1003 static int mdt_getattr_name(struct mdt_thread_info *info)
1005 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1006 struct mdt_body *reqbody;
1007 struct mdt_body *repbody;
1011 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1012 LASSERT(reqbody != NULL);
1013 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1014 LASSERT(repbody != NULL);
1016 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1017 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1018 repbody->eadatasize = 0;
1019 repbody->aclsize = 0;
1021 rc = mdt_init_ucred(info, reqbody);
1023 GOTO(out_shrink, rc);
1025 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1026 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1027 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1028 lhc->mlh_reg_lh.cookie = 0;
1030 mdt_exit_ucred(info);
1033 mdt_shrink_reply(info);
1037 static struct lu_device_operations mdt_lu_ops;
1039 static int lu_device_is_mdt(struct lu_device *d)
1041 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1044 static int mdt_set_info(struct mdt_thread_info *info)
1046 struct ptlrpc_request *req = mdt_info_req(info);
1052 rc = req_capsule_server_pack(info->mti_pill);
1056 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1058 DEBUG_REQ(D_HA, req, "no set_info key");
1062 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1065 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1067 DEBUG_REQ(D_HA, req, "no set_info val");
1071 if (keylen != (sizeof(KEY_READ_ONLY) - 1) ||
1072 memcmp(key, KEY_READ_ONLY, keylen) != 0)
1076 lustre_msg_set_status(req->rq_repmsg, 0);
1078 spin_lock(&req->rq_export->exp_lock);
1080 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1082 req->rq_export->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1083 spin_unlock(&req->rq_export->exp_lock);
1088 static int mdt_connect(struct mdt_thread_info *info)
1091 struct ptlrpc_request *req;
1093 req = mdt_info_req(info);
1094 rc = target_handle_connect(req);
1096 LASSERT(req->rq_export != NULL);
1097 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1098 rc = mdt_init_idmap(info);
1100 /* if mdt_init_idmap failed, revocation for connect */
1101 obd_disconnect(class_export_get(req->rq_export));
1103 rc = err_serious(rc);
1107 static int mdt_disconnect(struct mdt_thread_info *info)
1112 rc = target_handle_disconnect(mdt_info_req(info));
1114 rc = err_serious(rc);
1118 static int mdt_sendpage(struct mdt_thread_info *info,
1119 struct lu_rdpg *rdpg)
1121 struct ptlrpc_request *req = mdt_info_req(info);
1122 struct ptlrpc_bulk_desc *desc;
1123 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1130 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1135 for (i = 0, tmpcount = rdpg->rp_count;
1136 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1137 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1138 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1141 LASSERT(desc->bd_nob == rdpg->rp_count);
1142 rc = ptlrpc_start_bulk_transfer(desc);
1144 GOTO(free_desc, rc);
1146 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1147 GOTO(abort_bulk, rc);
1149 *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
1150 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1151 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1154 if (desc->bd_success &&
1155 desc->bd_nob_transferred == rdpg->rp_count)
1156 GOTO(free_desc, rc);
1158 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1161 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1162 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1163 desc->bd_nob_transferred, rdpg->rp_count,
1164 req->rq_export->exp_client_uuid.uuid,
1165 req->rq_export->exp_connection->c_remote_uuid.uuid);
1167 class_fail_export(req->rq_export);
1171 ptlrpc_abort_bulk(desc);
1173 ptlrpc_free_bulk(desc);
1177 #ifdef HAVE_SPLIT_SUPPORT
1179 * Retrieve dir entry from the page and insert it to the slave object, actually,
1180 * this should be in osd layer, but since it will not in the final product, so
1181 * just do it here and do not define more moo api anymore for this.
1183 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1186 struct mdt_object *object = info->mti_object;
1187 struct lu_fid *lf = &info->mti_tmp_fid2;
1188 struct md_attr *ma = &info->mti_attr;
1189 struct lu_dirpage *dp;
1190 struct lu_dirent *ent;
1191 int rc = 0, offset = 0;
1194 /* Make sure we have at least one entry. */
1199 * Disable trans for this name insert, since it will include many trans
1202 info->mti_no_need_trans = 1;
1204 * When write_dir_page, no need update parent's ctime,
1205 * and no permission check for name_insert.
1207 ma->ma_attr.la_ctime = 0;
1208 ma->ma_attr.la_valid = LA_MODE;
1209 ma->ma_valid = MA_INODE;
1212 dp = page_address(page);
1213 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1215 for (ent = lu_dirent_start(dp); ent != NULL;
1216 ent = lu_dirent_next(ent)) {
1217 struct lu_name *lname;
1220 if (le16_to_cpu(ent->lde_namelen) == 0)
1223 fid_le_to_cpu(lf, &ent->lde_fid);
1224 if (le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1225 ma->ma_attr.la_mode = S_IFDIR;
1227 ma->ma_attr.la_mode = 0;
1228 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1230 GOTO(out, rc = -ENOMEM);
1232 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1233 lname = mdt_name(info->mti_env, name,
1234 le16_to_cpu(ent->lde_namelen) + 1);
1235 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1236 rc = mdo_name_insert(info->mti_env,
1237 md_object_next(&object->mot_obj),
1239 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1241 CERROR("Can't insert %*.*s, rc %d\n",
1242 le16_to_cpu(ent->lde_namelen),
1243 le16_to_cpu(ent->lde_namelen),
1248 offset += lu_dirent_size(ent);
1258 static int mdt_bulk_timeout(void *data)
1262 CERROR("mdt bulk transfer timeout \n");
1267 static int mdt_writepage(struct mdt_thread_info *info)
1269 struct ptlrpc_request *req = mdt_info_req(info);
1270 struct mdt_body *reqbody;
1271 struct l_wait_info *lwi;
1272 struct ptlrpc_bulk_desc *desc;
1278 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1279 if (reqbody == NULL)
1280 RETURN(err_serious(-EFAULT));
1282 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1284 RETURN(err_serious(-ENOMEM));
1286 /* allocate the page for the desc */
1287 page = cfs_alloc_page(CFS_ALLOC_STD);
1289 GOTO(desc_cleanup, rc = -ENOMEM);
1291 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1292 (int)reqbody->size, (int)reqbody->nlink);
1294 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1295 (int)reqbody->nlink);
1298 * Check if client was evicted while we were doing i/o before touching
1303 GOTO(cleanup_page, rc = -ENOMEM);
1305 if (desc->bd_export->exp_failed)
1308 rc = ptlrpc_start_bulk_transfer (desc);
1310 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1311 mdt_bulk_timeout, desc);
1312 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1313 desc->bd_export->exp_failed, lwi);
1314 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1315 if (rc == -ETIMEDOUT) {
1316 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1317 ptlrpc_abort_bulk(desc);
1318 } else if (desc->bd_export->exp_failed) {
1319 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1321 ptlrpc_abort_bulk(desc);
1322 } else if (!desc->bd_success ||
1323 desc->bd_nob_transferred != desc->bd_nob) {
1324 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1326 "truncated" : "network error on",
1327 desc->bd_nob_transferred, desc->bd_nob);
1328 /* XXX should this be a different errno? */
1332 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1335 GOTO(cleanup_lwi, rc);
1336 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1341 cfs_free_page(page);
1343 ptlrpc_free_bulk(desc);
1348 static int mdt_readpage(struct mdt_thread_info *info)
1350 struct mdt_object *object = info->mti_object;
1351 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1352 struct mdt_body *reqbody;
1353 struct mdt_body *repbody;
1358 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1359 RETURN(err_serious(-ENOMEM));
1361 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1362 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1363 if (reqbody == NULL || repbody == NULL)
1364 RETURN(err_serious(-EFAULT));
1367 * prepare @rdpg before calling lower layers and transfer itself. Here
1368 * reqbody->size contains offset of where to start to read and
1369 * reqbody->nlink contains number bytes to read.
1371 rdpg->rp_hash = reqbody->size;
1372 if ((__u64)rdpg->rp_hash != reqbody->size) {
1373 CERROR("Invalid hash: %#llx != %#llx\n",
1374 (__u64)rdpg->rp_hash, reqbody->size);
1377 rdpg->rp_count = reqbody->nlink;
1378 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1379 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1380 if (rdpg->rp_pages == NULL)
1383 for (i = 0; i < rdpg->rp_npages; ++i) {
1384 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1385 if (rdpg->rp_pages[i] == NULL)
1386 GOTO(free_rdpg, rc = -ENOMEM);
1389 /* call lower layers to fill allocated pages with directory data */
1390 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1392 GOTO(free_rdpg, rc);
1394 /* send pages to client */
1395 rc = mdt_sendpage(info, rdpg);
1400 for (i = 0; i < rdpg->rp_npages; i++)
1401 if (rdpg->rp_pages[i] != NULL)
1402 cfs_free_page(rdpg->rp_pages[i]);
1403 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1405 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1411 static int mdt_reint_internal(struct mdt_thread_info *info,
1412 struct mdt_lock_handle *lhc,
1415 struct req_capsule *pill = info->mti_pill;
1416 struct mdt_device *mdt = info->mti_mdt;
1417 struct mdt_body *repbody;
1422 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1423 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1424 mdt->mdt_max_mdsize);
1425 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1426 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1427 mdt->mdt_max_cookiesize);
1429 rc = req_capsule_server_pack(pill);
1431 CERROR("Can't pack response, rc %d\n", rc);
1432 RETURN(err_serious(rc));
1435 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1436 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1438 repbody->eadatasize = 0;
1439 repbody->aclsize = 0;
1442 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1443 GOTO(out_shrink, rc = err_serious(-EFAULT));
1445 rc = mdt_reint_unpack(info, op);
1447 CERROR("Can't unpack reint, rc %d\n", rc);
1448 GOTO(out_shrink, rc = err_serious(rc));
1451 rc = mdt_init_ucred_reint(info);
1453 GOTO(out_shrink, rc);
1455 rc = mdt_fix_attr_ucred(info, op);
1457 GOTO(out_ucred, rc = err_serious(rc));
1459 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1460 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1461 GOTO(out_ucred, rc);
1463 rc = mdt_reint_rec(info, lhc);
1466 mdt_exit_ucred(info);
1468 mdt_shrink_reply(info);
1472 static long mdt_reint_opcode(struct mdt_thread_info *info,
1473 const struct req_format **fmt)
1475 struct mdt_rec_reint *rec;
1478 opc = err_serious(-EFAULT);
1479 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1481 opc = rec->rr_opcode;
1482 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1483 if (opc < REINT_MAX && fmt[opc] != NULL)
1484 req_capsule_extend(info->mti_pill, fmt[opc]);
1486 CERROR("Unsupported opc: %ld\n", opc);
1487 opc = err_serious(opc);
1493 static int mdt_reint(struct mdt_thread_info *info)
1498 static const struct req_format *reint_fmts[REINT_MAX] = {
1499 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1500 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1501 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1502 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1503 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1504 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1505 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1510 if (OBD_FAIL_CHECK_RESET(OBD_FAIL_MDS_REINT_NET,
1511 OBD_FAIL_MDS_REINT_NET)) {
1512 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET;
1516 opc = mdt_reint_opcode(info, reint_fmts);
1519 * No lock possible here from client to pass it to reint code
1522 rc = mdt_reint_internal(info, NULL, opc);
1527 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1531 /* TODO these two methods not available now. */
1533 /* this should sync the whole device */
1534 static int mdt_device_sync(struct mdt_thread_info *info)
1539 /* this should sync this object */
1540 static int mdt_object_sync(struct mdt_thread_info *info)
1545 static int mdt_sync(struct mdt_thread_info *info)
1547 struct req_capsule *pill = info->mti_pill;
1548 struct mdt_body *body;
1552 /* The fid may be zero, so we req_capsule_set manually */
1553 req_capsule_set(pill, &RQF_MDS_SYNC);
1555 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1557 RETURN(err_serious(-EINVAL));
1559 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1560 RETURN(err_serious(-ENOMEM));
1562 if (fid_seq(&body->fid1) == 0) {
1563 /* sync the whole device */
1564 rc = req_capsule_server_pack(pill);
1566 rc = mdt_device_sync(info);
1568 rc = err_serious(rc);
1570 /* sync an object */
1571 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1573 rc = mdt_object_sync(info);
1575 struct md_object *next;
1576 const struct lu_fid *fid;
1577 struct lu_attr *la = &info->mti_attr.ma_attr;
1579 next = mdt_object_child(info->mti_object);
1580 info->mti_attr.ma_need = MA_INODE;
1581 info->mti_attr.ma_valid = 0;
1582 rc = mo_attr_get(info->mti_env, next,
1585 body = req_capsule_server_get(pill,
1587 fid = mdt_object_fid(info->mti_object);
1588 mdt_pack_attr2body(info, body, la, fid);
1592 rc = err_serious(rc);
1597 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1599 return err_serious(-EOPNOTSUPP);
1602 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1604 return err_serious(-EOPNOTSUPP);
1608 * OBD PING and other handlers.
1610 static int mdt_obd_ping(struct mdt_thread_info *info)
1615 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1617 rc = target_handle_ping(mdt_info_req(info));
1619 rc = err_serious(rc);
1623 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1625 return err_serious(-EOPNOTSUPP);
1628 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1630 return err_serious(-EOPNOTSUPP);
1637 static struct ldlm_callback_suite cbs = {
1638 .lcs_completion = ldlm_server_completion_ast,
1639 .lcs_blocking = ldlm_server_blocking_ast,
1643 static int mdt_enqueue(struct mdt_thread_info *info)
1645 struct ptlrpc_request *req;
1650 * info->mti_dlm_req already contains swapped and (if necessary)
1651 * converted dlm request.
1653 LASSERT(info->mti_dlm_req != NULL);
1655 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1656 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1660 req = mdt_info_req(info);
1663 * Lock without inodebits makes no sense and will oops later in
1664 * ldlm. Let's check it now to see if we have wrong lock from client or
1665 * bits get corrupted somewhere in mdt_intent_policy().
1667 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1668 /* This is disabled because we need to support liblustre flock.
1669 * LASSERT(req_bits != 0);
1672 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1673 req, info->mti_dlm_req, &cbs);
1674 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1675 return rc ? err_serious(rc) : req->rq_status;
1678 static int mdt_convert(struct mdt_thread_info *info)
1681 struct ptlrpc_request *req;
1683 LASSERT(info->mti_dlm_req);
1684 req = mdt_info_req(info);
1685 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1686 return rc ? err_serious(rc) : req->rq_status;
1689 static int mdt_bl_callback(struct mdt_thread_info *info)
1691 CERROR("bl callbacks should not happen on MDS\n");
1693 return err_serious(-EOPNOTSUPP);
1696 static int mdt_cp_callback(struct mdt_thread_info *info)
1698 CERROR("cp callbacks should not happen on MDS\n");
1700 return err_serious(-EOPNOTSUPP);
1704 * sec context handlers
1706 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1710 rc = mdt_handle_idmap(info);
1713 struct ptlrpc_request *req = mdt_info_req(info);
1716 opc = lustre_msg_get_opc(req->rq_reqmsg);
1717 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1718 sptlrpc_svc_ctx_invalidate(req);
1724 static struct mdt_object *mdt_obj(struct lu_object *o)
1726 LASSERT(lu_device_is_mdt(o->lo_dev));
1727 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1730 struct mdt_object *mdt_object_find(const struct lu_env *env,
1731 struct mdt_device *d,
1732 const struct lu_fid *f)
1734 struct lu_object *o;
1735 struct mdt_object *m;
1738 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
1739 o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1740 if (unlikely(IS_ERR(o)))
1741 m = (struct mdt_object *)o;
1747 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1748 struct mdt_lock_handle *lh, __u64 ibits, int locality)
1750 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1751 ldlm_policy_data_t *policy = &info->mti_policy;
1752 struct ldlm_res_id *res_id = &info->mti_res_id;
1756 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1757 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1758 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1759 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1761 if (mdt_object_exists(o) < 0) {
1762 if (locality == MDT_CROSS_LOCK) {
1763 /* cross-ref object fix */
1764 ibits &= ~MDS_INODELOCK_UPDATE;
1765 ibits |= MDS_INODELOCK_LOOKUP;
1767 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1768 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1770 /* No PDO lock on remote object */
1771 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1774 memset(policy, 0, sizeof(*policy));
1775 fid_build_reg_res_name(mdt_object_fid(o), res_id);
1778 * Take PDO lock on whole directory and build correct @res_id for lock
1779 * on part of directory.
1781 if (lh->mlh_pdo_hash != 0) {
1782 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1783 mdt_lock_pdo_mode(info, o, lh);
1784 if (lh->mlh_pdo_mode != LCK_NL) {
1786 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1787 * is never going to be sent to client and we do not
1788 * want it slowed down due to possible cancels.
1790 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1791 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1792 policy, res_id, LDLM_FL_ATOMIC_CB);
1798 * Finish res_id initializing by name hash marking patr of
1799 * directory which is taking modification.
1801 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1804 policy->l_inodebits.bits = ibits;
1807 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1808 * going to be sent to client. If it is - mdt_intent_policy() path will
1809 * fix it up and turns FL_LOCAL flag off.
1811 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1812 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1817 if (lh->mlh_type == MDT_PDO_LOCK) {
1818 /* check for exists after object is locked */
1819 if (mdt_object_exists(o) == 0) {
1820 /* Non-existent object shouldn't have PDO lock */
1823 /* Non-dir object shouldn't have PDO lock */
1824 LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1829 mdt_object_unlock(info, o, lh, 1);
1836 * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1837 * to save this lock in req. when transaction committed, req will be released,
1838 * and lock will, too.
1840 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1841 struct mdt_lock_handle *lh, int decref)
1843 struct ptlrpc_request *req = mdt_info_req(info);
1846 if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1847 /* Do not save PDO locks to request, just decref. */
1848 mdt_fid_unlock(&lh->mlh_pdo_lh,
1850 lh->mlh_pdo_lh.cookie = 0ull;
1853 if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1855 mdt_fid_unlock(&lh->mlh_reg_lh,
1858 ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1861 lh->mlh_reg_lh.cookie = 0ull;
1867 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1868 const struct lu_fid *f,
1869 struct mdt_lock_handle *lh,
1872 struct mdt_object *o;
1874 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1878 rc = mdt_object_lock(info, o, lh, ibits,
1881 mdt_object_put(info->mti_env, o);
1888 void mdt_object_unlock_put(struct mdt_thread_info * info,
1889 struct mdt_object * o,
1890 struct mdt_lock_handle *lh,
1893 mdt_object_unlock(info, o, lh, decref);
1894 mdt_object_put(info->mti_env, o);
1897 static struct mdt_handler *mdt_handler_find(__u32 opc,
1898 struct mdt_opc_slice *supported)
1900 struct mdt_opc_slice *s;
1901 struct mdt_handler *h;
1904 for (s = supported; s->mos_hs != NULL; s++) {
1905 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1906 h = s->mos_hs + (opc - s->mos_opc_start);
1907 if (likely(h->mh_opc != 0))
1908 LASSERT(h->mh_opc == opc);
1910 h = NULL; /* unsupported opc */
1917 static int mdt_lock_resname_compat(struct mdt_device *m,
1918 struct ldlm_request *req)
1920 /* XXX something... later. */
1924 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1926 /* XXX something... later. */
1931 * Generic code handling requests that have struct mdt_body passed in:
1933 * - extract mdt_body from request and save it in @info, if present;
1935 * - create lu_object, corresponding to the fid in mdt_body, and save it in
1938 * - if HABEO_CORPUS flag is set for this request type check whether object
1939 * actually exists on storage (lu_object_exists()).
1942 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1944 const struct mdt_body *body;
1945 struct mdt_object *obj;
1946 const struct lu_env *env;
1947 struct req_capsule *pill;
1951 env = info->mti_env;
1952 pill = info->mti_pill;
1954 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1958 if (!(body->valid & OBD_MD_FLID))
1961 if (!fid_is_sane(&body->fid1)) {
1962 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1967 * Do not get size or any capa fields before we check that request
1968 * contains capa actually. There are some requests which do not, for
1969 * instance MDS_IS_SUBDIR.
1971 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1972 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1973 mdt_set_capainfo(info, 0, &body->fid1,
1974 req_capsule_client_get(pill, &RMF_CAPA1));
1976 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1978 if ((flags & HABEO_CORPUS) &&
1979 !mdt_object_exists(obj)) {
1980 mdt_object_put(env, obj);
1981 /* for capability renew ENOENT will be handled in
1983 if (body->valid & OBD_MD_FLOSSCAPA)
1988 info->mti_object = obj;
1997 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1999 struct req_capsule *pill = info->mti_pill;
2003 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2004 rc = mdt_body_unpack(info, flags);
2008 if (rc == 0 && (flags & HABEO_REFERO)) {
2009 struct mdt_device *mdt = info->mti_mdt;
2012 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2013 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2014 mdt->mdt_max_mdsize);
2015 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2016 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2017 mdt->mdt_max_cookiesize);
2019 rc = req_capsule_server_pack(pill);
2024 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2026 struct md_device *next = m->mdt_child;
2028 return next->md_ops->mdo_init_capa_ctxt(env, next,
2029 m->mdt_opts.mo_mds_capa,
2030 m->mdt_capa_timeout,
2036 * Invoke handler for this request opc. Also do necessary preprocessing
2037 * (according to handler ->mh_flags), and post-processing (setting of
2038 * ->last_{xid,committed}).
2040 static int mdt_req_handle(struct mdt_thread_info *info,
2041 struct mdt_handler *h, struct ptlrpc_request *req)
2043 int rc, serious = 0;
2048 LASSERT(h->mh_act != NULL);
2049 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2050 LASSERT(current->journal_info == NULL);
2053 * Mask out OBD_FAIL_ONCE, because that will stop
2054 * correct handling of failed req later in ldlm due to doing
2055 * obd_fail_loc |= OBD_FAIL_ONCE without actually
2056 * correct actions like it is done in target_send_reply_msg().
2058 if (h->mh_fail_id != 0) {
2060 * Set to info->mti_fail_id to handler fail_id, it will be used
2061 * later, and better than use default fail_id.
2063 if (OBD_FAIL_CHECK_RESET(h->mh_fail_id && OBD_FAIL_MASK_LOC,
2064 h->mh_fail_id & ~OBD_FAILED)) {
2065 info->mti_fail_id = h->mh_fail_id;
2071 flags = h->mh_flags;
2072 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2074 if (h->mh_fmt != NULL) {
2075 req_capsule_set(info->mti_pill, h->mh_fmt);
2076 rc = mdt_unpack_req_pack_rep(info, flags);
2079 if (rc == 0 && flags & MUTABOR &&
2080 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2081 /* should it be rq_status? */
2084 if (rc == 0 && flags & HABEO_CLAVIS) {
2085 struct ldlm_request *dlm_req;
2087 LASSERT(h->mh_fmt != NULL);
2089 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2090 if (dlm_req != NULL) {
2091 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2092 rc = mdt_lock_resname_compat(info->mti_mdt,
2094 info->mti_dlm_req = dlm_req;
2100 /* capability setting changed via /proc, needs reinitialize ctxt */
2101 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2102 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2103 info->mti_mdt->mdt_capa_conf = 0;
2106 if (likely(rc == 0)) {
2108 * Process request, there can be two types of rc:
2109 * 1) errors with msg unpack/pack, other failures outside the
2110 * operation itself. This is counted as serious errors;
2111 * 2) errors during fs operation, should be placed in rq_status
2114 rc = h->mh_act(info);
2115 serious = is_serious(rc);
2116 rc = clear_serious(rc);
2120 req->rq_status = rc;
2123 * ELDLM_* codes which > 0 should be in rq_status only as well as
2124 * all non-serious errors.
2126 if (rc > 0 || !serious)
2129 LASSERT(current->journal_info == NULL);
2131 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2132 info->mti_mdt->mdt_opts.mo_compat_resname) {
2133 struct ldlm_reply *dlmrep;
2135 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2137 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2140 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2141 if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2142 target_committed_to_req(req);
2144 if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2145 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2146 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2153 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2155 lh->mlh_type = MDT_NUL_LOCK;
2156 lh->mlh_reg_lh.cookie = 0ull;
2157 lh->mlh_reg_mode = LCK_MINMODE;
2158 lh->mlh_pdo_lh.cookie = 0ull;
2159 lh->mlh_pdo_mode = LCK_MINMODE;
2162 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2164 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2165 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2169 * Initialize fields of struct mdt_thread_info. Other fields are left in
2170 * uninitialized state, because it's too expensive to zero out whole
2171 * mdt_thread_info (> 1K) on each request arrival.
2173 static void mdt_thread_info_init(struct ptlrpc_request *req,
2174 struct mdt_thread_info *info)
2177 struct md_capainfo *ci;
2179 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2180 info->mti_pill = &req->rq_pill;
2183 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2184 mdt_lock_handle_init(&info->mti_lh[i]);
2186 /* mdt device: it can be NULL while CONNECT */
2187 if (req->rq_export) {
2188 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2189 info->mti_exp = req->rq_export;
2191 info->mti_mdt = NULL;
2192 info->mti_env = req->rq_svc_thread->t_env;
2193 ci = md_capainfo(info->mti_env);
2194 memset(ci, 0, sizeof *ci);
2196 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2197 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2199 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2200 info->mti_body = NULL;
2201 info->mti_object = NULL;
2202 info->mti_dlm_req = NULL;
2203 info->mti_has_trans = 0;
2204 info->mti_no_need_trans = 0;
2205 info->mti_cross_ref = 0;
2206 info->mti_opdata = 0;
2208 /* To not check for split by default. */
2209 info->mti_spec.sp_ck_split = 0;
2212 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2216 req_capsule_fini(info->mti_pill);
2217 if (info->mti_object != NULL) {
2218 mdt_object_put(info->mti_env, info->mti_object);
2219 info->mti_object = NULL;
2221 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2222 mdt_lock_handle_fini(&info->mti_lh[i]);
2223 info->mti_env = NULL;
2226 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2227 struct obd_device *obd, int *process)
2229 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2230 case MDS_CONNECT: /* This will never get here, but for completeness. */
2231 case OST_CONNECT: /* This will never get here, but for completeness. */
2232 case MDS_DISCONNECT:
2233 case OST_DISCONNECT:
2238 case MDS_DONE_WRITING:
2239 case MDS_SYNC: /* used in unmounting */
2245 *process = target_queue_recovery_request(req, obd);
2249 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2256 * Handle recovery. Return:
2257 * +1: continue request processing;
2258 * -ve: abort immediately with the given error code;
2259 * 0: send reply with error code in req->rq_status;
2261 static int mdt_recovery(struct mdt_thread_info *info)
2263 struct ptlrpc_request *req = mdt_info_req(info);
2265 struct obd_device *obd;
2269 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2272 case SEC_CTX_INIT_CONT:
2278 rc = mdt_handle_idmap(info);
2287 if (unlikely(req->rq_export == NULL)) {
2288 CERROR("operation %d on unconnected MDS from %s\n",
2289 lustre_msg_get_opc(req->rq_reqmsg),
2290 libcfs_id2str(req->rq_peer));
2291 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2292 * mds_A will get -ENOTCONN(especially for ping req),
2293 * which will cause that mds_A deactive timeout, then when
2294 * mds_A cleanup, the cleanup process will be suspended since
2295 * deactive timeout is not zero.
2297 req->rq_status = -ENOTCONN;
2298 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2302 /* sanity check: if the xid matches, the request must be marked as a
2303 * resent or replayed */
2304 if (req_xid_is_last(req)) {
2305 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2306 (MSG_RESENT | MSG_REPLAY))) {
2307 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2308 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2309 lustre_msg_get_flags(req->rq_reqmsg));
2311 req->rq_status = -ENOTCONN;
2316 /* else: note the opposite is not always true; a RESENT req after a
2317 * failover will usually not match the last_xid, since it was likely
2318 * never committed. A REPLAYed request will almost never match the
2319 * last xid, however it could for a committed, but still retained,
2322 obd = req->rq_export->exp_obd;
2324 /* Check for aborted recovery... */
2325 spin_lock_bh(&obd->obd_processing_task_lock);
2326 recovering = obd->obd_recovering;
2327 spin_unlock_bh(&obd->obd_processing_task_lock);
2328 if (unlikely(recovering)) {
2331 DEBUG_REQ(D_INFO, req, "Got new replay");
2332 rc = mdt_filter_recovery_request(req, obd, &should_process);
2333 if (rc != 0 || !should_process)
2335 else if (should_process < 0) {
2336 req->rq_status = should_process;
2337 rc = ptlrpc_error(req);
2344 static int mdt_reply(struct ptlrpc_request *req, int rc,
2345 struct mdt_thread_info *info)
2350 if (req->rq_reply_state == NULL && rc == 0) {
2351 req->rq_status = rc;
2352 lustre_pack_reply(req, 1, NULL, NULL);
2355 target_send_reply(req, rc, info->mti_fail_id);
2359 static int mdt_msg_check_version(struct lustre_msg *msg)
2363 switch (lustre_msg_get_opc(msg)) {
2365 case MDS_DISCONNECT:
2368 case SEC_CTX_INIT_CONT:
2370 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2372 CERROR("bad opc %u version %08x, expecting %08x\n",
2373 lustre_msg_get_opc(msg),
2374 lustre_msg_get_version(msg),
2375 LUSTRE_OBD_VERSION);
2379 case MDS_GETATTR_NAME:
2386 case MDS_DONE_WRITING:
2392 case MDS_QUOTACHECK:
2398 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2400 CERROR("bad opc %u version %08x, expecting %08x\n",
2401 lustre_msg_get_opc(msg),
2402 lustre_msg_get_version(msg),
2403 LUSTRE_MDS_VERSION);
2407 case LDLM_BL_CALLBACK:
2408 case LDLM_CP_CALLBACK:
2409 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2411 CERROR("bad opc %u version %08x, expecting %08x\n",
2412 lustre_msg_get_opc(msg),
2413 lustre_msg_get_version(msg),
2414 LUSTRE_DLM_VERSION);
2416 case OBD_LOG_CANCEL:
2417 case LLOG_ORIGIN_HANDLE_CREATE:
2418 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2419 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2420 case LLOG_ORIGIN_HANDLE_CLOSE:
2421 case LLOG_ORIGIN_HANDLE_DESTROY:
2422 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2424 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2426 CERROR("bad opc %u version %08x, expecting %08x\n",
2427 lustre_msg_get_opc(msg),
2428 lustre_msg_get_version(msg),
2429 LUSTRE_LOG_VERSION);
2432 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2438 static int mdt_handle0(struct ptlrpc_request *req,
2439 struct mdt_thread_info *info,
2440 struct mdt_opc_slice *supported)
2442 struct mdt_handler *h;
2443 struct lustre_msg *msg;
2448 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2451 LASSERT(current->journal_info == NULL);
2453 msg = req->rq_reqmsg;
2454 rc = mdt_msg_check_version(msg);
2455 if (likely(rc == 0)) {
2456 rc = mdt_recovery(info);
2457 if (likely(rc == +1)) {
2458 h = mdt_handler_find(lustre_msg_get_opc(msg),
2460 if (likely(h != NULL)) {
2461 rc = mdt_req_handle(info, h, req);
2462 rc = mdt_reply(req, rc, info);
2464 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2465 req->rq_status = -ENOTSUPP;
2466 rc = ptlrpc_error(req);
2471 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2476 * MDT handler function called by ptlrpc service thread when request comes.
2478 * XXX common "target" functionality should be factored into separate module
2479 * shared by mdt, ost and stand-alone services like fld.
2481 static int mdt_handle_common(struct ptlrpc_request *req,
2482 struct mdt_opc_slice *supported)
2485 struct mdt_thread_info *info;
2489 env = req->rq_svc_thread->t_env;
2490 LASSERT(env != NULL);
2491 LASSERT(env->le_ses != NULL);
2492 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2493 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2494 LASSERT(info != NULL);
2496 mdt_thread_info_init(req, info);
2498 rc = mdt_handle0(req, info, supported);
2500 mdt_thread_info_fini(info);
2505 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2508 int mdt_recovery_handle(struct ptlrpc_request *req)
2513 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2515 rc = mdt_handle_common(req, mdt_fld_handlers);
2518 rc = mdt_handle_common(req, mdt_seq_handlers);
2521 rc = mdt_handle_common(req, mdt_regular_handlers);
2528 static int mdt_regular_handle(struct ptlrpc_request *req)
2530 return mdt_handle_common(req, mdt_regular_handlers);
2533 static int mdt_readpage_handle(struct ptlrpc_request *req)
2535 return mdt_handle_common(req, mdt_readpage_handlers);
2538 static int mdt_xmds_handle(struct ptlrpc_request *req)
2540 return mdt_handle_common(req, mdt_xmds_handlers);
2543 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2545 return mdt_handle_common(req, mdt_seq_handlers);
2548 static int mdt_mdss_handle(struct ptlrpc_request *req)
2550 return mdt_handle_common(req, mdt_seq_handlers);
2553 static int mdt_dtss_handle(struct ptlrpc_request *req)
2555 return mdt_handle_common(req, mdt_seq_handlers);
2558 static int mdt_fld_handle(struct ptlrpc_request *req)
2560 return mdt_handle_common(req, mdt_fld_handlers);
2576 static int mdt_intent_getattr(enum mdt_it_code opcode,
2577 struct mdt_thread_info *info,
2578 struct ldlm_lock **,
2580 static int mdt_intent_reint(enum mdt_it_code opcode,
2581 struct mdt_thread_info *info,
2582 struct ldlm_lock **,
2585 static struct mdt_it_flavor {
2586 const struct req_format *it_fmt;
2588 int (*it_act)(enum mdt_it_code ,
2589 struct mdt_thread_info *,
2590 struct ldlm_lock **,
2593 } mdt_it_flavor[] = {
2595 .it_fmt = &RQF_LDLM_INTENT,
2596 /*.it_flags = HABEO_REFERO,*/
2598 .it_act = mdt_intent_reint,
2599 .it_reint = REINT_OPEN
2602 .it_fmt = &RQF_LDLM_INTENT,
2603 .it_flags = MUTABOR,
2604 .it_act = mdt_intent_reint,
2605 .it_reint = REINT_OPEN
2608 .it_fmt = &RQF_LDLM_INTENT,
2609 .it_flags = MUTABOR,
2610 .it_act = mdt_intent_reint,
2611 .it_reint = REINT_CREATE
2613 [MDT_IT_GETATTR] = {
2614 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2615 .it_flags = HABEO_REFERO,
2616 .it_act = mdt_intent_getattr
2618 [MDT_IT_READDIR] = {
2624 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2625 .it_flags = HABEO_REFERO,
2626 .it_act = mdt_intent_getattr
2629 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2630 .it_flags = MUTABOR,
2632 .it_reint = REINT_UNLINK
2636 .it_flags = MUTABOR,
2639 [MDT_IT_GETXATTR] = {
2646 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2647 struct ldlm_lock **lockp,
2648 struct ldlm_lock *new_lock,
2649 struct mdt_lock_handle *lh,
2652 struct ptlrpc_request *req = mdt_info_req(info);
2653 struct ldlm_lock *lock = *lockp;
2656 * Get new lock only for cases when possible resent did not find any
2659 if (new_lock == NULL)
2660 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2662 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2663 lh->mlh_reg_lh.cookie = 0;
2667 LASSERTF(new_lock != NULL,
2668 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2671 * If we've already given this lock to a client once, then we should
2672 * have no readers or writers. Otherwise, we should have one reader
2673 * _or_ writer ref (which will be zeroed below) before returning the
2676 if (new_lock->l_export == req->rq_export) {
2677 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2679 LASSERT(new_lock->l_export == NULL);
2680 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2685 if (new_lock->l_export == req->rq_export) {
2687 * Already gave this to the client, which means that we
2688 * reconstructed a reply.
2690 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2692 lh->mlh_reg_lh.cookie = 0;
2693 RETURN(ELDLM_LOCK_REPLACED);
2696 /* This lock might already be given to the client by an resent req,
2697 * in this case we should return ELDLM_LOCK_ABORTED,
2698 * so we should check led_held_locks here, but it will affect
2699 * performance, FIXME
2701 /* Fixup the lock to be given to the client */
2702 lock_res_and_lock(new_lock);
2703 new_lock->l_readers = 0;
2704 new_lock->l_writers = 0;
2706 new_lock->l_export = class_export_get(req->rq_export);
2707 spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2708 list_add(&new_lock->l_export_chain,
2709 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2710 spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2712 new_lock->l_blocking_ast = lock->l_blocking_ast;
2713 new_lock->l_completion_ast = lock->l_completion_ast;
2714 new_lock->l_remote_handle = lock->l_remote_handle;
2715 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2717 unlock_res_and_lock(new_lock);
2718 LDLM_LOCK_PUT(new_lock);
2719 lh->mlh_reg_lh.cookie = 0;
2721 RETURN(ELDLM_LOCK_REPLACED);
2724 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2725 struct ldlm_lock *new_lock,
2726 struct ldlm_lock **old_lock,
2727 struct mdt_lock_handle *lh)
2729 struct ptlrpc_request *req = mdt_info_req(info);
2730 struct obd_export *exp = req->rq_export;
2731 struct lustre_handle remote_hdl;
2732 struct ldlm_request *dlmreq;
2733 struct list_head *iter;
2735 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2738 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2739 remote_hdl = dlmreq->lock_handle[0];
2741 spin_lock(&exp->exp_ldlm_data.led_lock);
2742 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2743 struct ldlm_lock *lock;
2744 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2745 if (lock == new_lock)
2747 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2748 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2749 lh->mlh_reg_mode = lock->l_granted_mode;
2751 LDLM_DEBUG(lock, "restoring lock cookie");
2752 DEBUG_REQ(D_DLMTRACE, req,
2753 "restoring lock cookie "LPX64,
2754 lh->mlh_reg_lh.cookie);
2756 *old_lock = LDLM_LOCK_GET(lock);
2757 spin_unlock(&exp->exp_ldlm_data.led_lock);
2761 spin_unlock(&exp->exp_ldlm_data.led_lock);
2764 * If the xid matches, then we know this is a resent request, and allow
2765 * it. (It's probably an OPEN, for which we don't send a lock.
2767 if (req_xid_is_last(req))
2771 * This remote handle isn't enqueued, so we never received or processed
2772 * this request. Clear MSG_RESENT, because it can be handled like any
2773 * normal request now.
2775 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2777 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2781 static int mdt_intent_getattr(enum mdt_it_code opcode,
2782 struct mdt_thread_info *info,
2783 struct ldlm_lock **lockp,
2786 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2787 struct ldlm_lock *new_lock = NULL;
2789 struct ldlm_reply *ldlm_rep;
2790 struct ptlrpc_request *req;
2791 struct mdt_body *reqbody;
2792 struct mdt_body *repbody;
2796 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2799 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2802 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2803 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2804 repbody->eadatasize = 0;
2805 repbody->aclsize = 0;
2809 child_bits = MDS_INODELOCK_LOOKUP;
2811 case MDT_IT_GETATTR:
2812 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2815 CERROR("Unhandled till now");
2816 GOTO(out_shrink, rc = -EINVAL);
2819 rc = mdt_init_ucred(info, reqbody);
2821 GOTO(out_shrink, rc);
2823 req = info->mti_pill->rc_req;
2824 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2825 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2827 /* Get lock from request for possible resent case. */
2828 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2830 ldlm_rep->lock_policy_res2 =
2831 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2833 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2834 ldlm_rep->lock_policy_res2 = 0;
2835 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2836 ldlm_rep->lock_policy_res2) {
2837 lhc->mlh_reg_lh.cookie = 0ull;
2838 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2841 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2844 mdt_exit_ucred(info);
2846 mdt_shrink_reply(info);
2850 static int mdt_intent_reint(enum mdt_it_code opcode,
2851 struct mdt_thread_info *info,
2852 struct ldlm_lock **lockp,
2855 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2856 struct ldlm_reply *rep = NULL;
2860 static const struct req_format *intent_fmts[REINT_MAX] = {
2861 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
2862 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
2867 opc = mdt_reint_opcode(info, intent_fmts);
2871 if (mdt_it_flavor[opcode].it_reint != opc) {
2872 CERROR("Reint code %ld doesn't match intent: %d\n",
2874 RETURN(err_serious(-EPROTO));
2877 /* Get lock from request for possible resent case. */
2878 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2880 rc = mdt_reint_internal(info, lhc, opc);
2882 /* Check whether the reply has been packed successfully. */
2883 if (mdt_info_req(info)->rq_repmsg != NULL)
2884 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2886 RETURN(err_serious(-EFAULT));
2888 /* MDC expects this in any case */
2890 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2892 /* Cross-ref case, the lock should be returned to the client */
2893 if (rc == -EREMOTE) {
2894 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2895 rep->lock_policy_res2 = 0;
2896 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2899 rep->lock_policy_res2 = clear_serious(rc);
2901 lhc->mlh_reg_lh.cookie = 0ull;
2902 rc = ELDLM_LOCK_ABORTED;
2906 static int mdt_intent_code(long itcode)
2914 case IT_OPEN|IT_CREAT:
2921 rc = MDT_IT_READDIR;
2924 rc = MDT_IT_GETATTR;
2936 rc = MDT_IT_GETXATTR;
2939 CERROR("Unknown intent opcode: %ld\n", itcode);
2946 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2947 struct ldlm_lock **lockp, int flags)
2949 struct req_capsule *pill;
2950 struct mdt_it_flavor *flv;
2955 opc = mdt_intent_code(itopc);
2959 pill = info->mti_pill;
2960 flv = &mdt_it_flavor[opc];
2962 if (flv->it_fmt != NULL)
2963 req_capsule_extend(pill, flv->it_fmt);
2965 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2967 struct ptlrpc_request *req = mdt_info_req(info);
2968 if (flv->it_flags & MUTABOR &&
2969 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2972 if (rc == 0 && flv->it_act != NULL) {
2973 /* execute policy */
2974 rc = flv->it_act(opc, info, lockp, flags);
2981 static int mdt_intent_policy(struct ldlm_namespace *ns,
2982 struct ldlm_lock **lockp, void *req_cookie,
2983 ldlm_mode_t mode, int flags, void *data)
2985 struct mdt_thread_info *info;
2986 struct ptlrpc_request *req = req_cookie;
2987 struct ldlm_intent *it;
2988 struct req_capsule *pill;
2993 LASSERT(req != NULL);
2995 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2997 LASSERT(info != NULL);
2998 pill = info->mti_pill;
2999 LASSERT(pill->rc_req == req);
3001 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3002 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3003 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3005 const struct ldlm_request *dlmreq;
3008 struct ldlm_lock *lock = *lockp;
3010 LDLM_DEBUG(lock, "intent policy opc: %s\n",
3011 ldlm_it2str(it->opc));
3014 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3019 * Lock without inodebits makes no sense and will oops
3020 * later in ldlm. Let's check it now to see if we have
3021 * wrong lock from client or bits get corrupted
3022 * somewhere in mdt_intent_opc().
3024 dlmreq = info->mti_dlm_req;
3025 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
3026 LASSERT(req_bits != 0);
3029 rc = err_serious(-EFAULT);
3031 /* No intent was provided */
3032 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3033 rc = req_capsule_server_pack(pill);
3035 rc = err_serious(rc);
3043 static void mdt_seq_adjust(const struct lu_env *env,
3044 struct mdt_device *m, int lost)
3046 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3047 struct lu_range out;
3050 LASSERT(ls && ls->ls_server_seq);
3052 /* get extra seq from seq_server, moving it's range up */
3053 while (lost-- > 0) {
3054 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
3059 static int mdt_seq_fini(const struct lu_env *env,
3060 struct mdt_device *m)
3062 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3065 if (ls && ls->ls_server_seq) {
3066 seq_server_fini(ls->ls_server_seq, env);
3067 OBD_FREE_PTR(ls->ls_server_seq);
3068 ls->ls_server_seq = NULL;
3071 if (ls && ls->ls_control_seq) {
3072 seq_server_fini(ls->ls_control_seq, env);
3073 OBD_FREE_PTR(ls->ls_control_seq);
3074 ls->ls_control_seq = NULL;
3077 if (ls && ls->ls_client_seq) {
3078 seq_client_fini(ls->ls_client_seq);
3079 OBD_FREE_PTR(ls->ls_client_seq);
3080 ls->ls_client_seq = NULL;
3086 static int mdt_seq_init(const struct lu_env *env,
3088 struct mdt_device *m)
3095 ls = m->mdt_md_dev.md_lu_dev.ld_site;
3098 * This is sequence-controller node. Init seq-controller server on local
3101 if (ls->ls_node_id == 0) {
3102 LASSERT(ls->ls_control_seq == NULL);
3104 OBD_ALLOC_PTR(ls->ls_control_seq);
3105 if (ls->ls_control_seq == NULL)
3108 rc = seq_server_init(ls->ls_control_seq,
3109 m->mdt_bottom, uuid,
3110 LUSTRE_SEQ_CONTROLLER,
3114 GOTO(out_seq_fini, rc);
3116 OBD_ALLOC_PTR(ls->ls_client_seq);
3117 if (ls->ls_client_seq == NULL)
3118 GOTO(out_seq_fini, rc = -ENOMEM);
3120 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3121 if (prefix == NULL) {
3122 OBD_FREE_PTR(ls->ls_client_seq);
3123 GOTO(out_seq_fini, rc = -ENOMEM);
3126 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3130 * Init seq-controller client after seq-controller server is
3131 * ready. Pass ls->ls_control_seq to it for direct talking.
3133 rc = seq_client_init(ls->ls_client_seq, NULL,
3134 LUSTRE_SEQ_METADATA, prefix,
3135 ls->ls_control_seq);
3136 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3139 GOTO(out_seq_fini, rc);
3142 /* Init seq-server on local MDT */
3143 LASSERT(ls->ls_server_seq == NULL);
3145 OBD_ALLOC_PTR(ls->ls_server_seq);
3146 if (ls->ls_server_seq == NULL)
3147 GOTO(out_seq_fini, rc = -ENOMEM);
3149 rc = seq_server_init(ls->ls_server_seq,
3150 m->mdt_bottom, uuid,
3154 GOTO(out_seq_fini, rc = -ENOMEM);
3156 /* Assign seq-controller client to local seq-server. */
3157 if (ls->ls_node_id == 0) {
3158 LASSERT(ls->ls_client_seq != NULL);
3160 rc = seq_server_set_cli(ls->ls_server_seq,
3168 mdt_seq_fini(env, m);
3173 * Init client sequence manager which is used by local MDS to talk to sequence
3174 * controller on remote node.
3176 static int mdt_seq_init_cli(const struct lu_env *env,
3177 struct mdt_device *m,
3178 struct lustre_cfg *cfg)
3180 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3181 struct obd_device *mdc;
3182 struct obd_uuid *uuidp, *mdcuuidp;
3183 char *uuid_str, *mdc_uuid_str;
3186 struct mdt_thread_info *info;
3187 char *p, *index_string = lustre_cfg_string(cfg, 2);
3190 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3191 uuidp = &info->mti_u.uuid[0];
3192 mdcuuidp = &info->mti_u.uuid[1];
3194 LASSERT(index_string);
3196 index = simple_strtol(index_string, &p, 10);
3198 CERROR("Invalid index in lustre_cgf, offset 2\n");
3202 /* check if this is adding the first MDC and controller is not yet
3204 if (index != 0 || ls->ls_client_seq)
3207 uuid_str = lustre_cfg_string(cfg, 1);
3208 mdc_uuid_str = lustre_cfg_string(cfg, 4);
3209 obd_str2uuid(uuidp, uuid_str);
3210 obd_str2uuid(mdcuuidp, mdc_uuid_str);
3212 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3214 CERROR("can't find controller MDC by uuid %s\n",
3217 } else if (!mdc->obd_set_up) {
3218 CERROR("target %s not set up\n", mdc->obd_name);
3221 LASSERT(ls->ls_control_exp);
3222 OBD_ALLOC_PTR(ls->ls_client_seq);
3223 if (ls->ls_client_seq != NULL) {
3226 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3230 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3233 rc = seq_client_init(ls->ls_client_seq,
3235 LUSTRE_SEQ_METADATA,
3237 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3244 LASSERT(ls->ls_server_seq != NULL);
3245 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3252 static void mdt_seq_fini_cli(struct mdt_device *m)
3258 ls = m->mdt_md_dev.md_lu_dev.ld_site;
3260 if (ls && ls->ls_server_seq)
3261 seq_server_set_cli(ls->ls_server_seq,
3264 if (ls && ls->ls_control_exp) {
3265 class_export_put(ls->ls_control_exp);
3266 ls->ls_control_exp = NULL;
3274 static int mdt_fld_fini(const struct lu_env *env,
3275 struct mdt_device *m)
3277 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3280 if (ls && ls->ls_server_fld) {
3281 fld_server_fini(ls->ls_server_fld, env);
3282 OBD_FREE_PTR(ls->ls_server_fld);
3283 ls->ls_server_fld = NULL;
3289 static int mdt_fld_init(const struct lu_env *env,
3291 struct mdt_device *m)
3297 ls = m->mdt_md_dev.md_lu_dev.ld_site;
3299 OBD_ALLOC_PTR(ls->ls_server_fld);
3300 if (ls->ls_server_fld == NULL)
3301 RETURN(rc = -ENOMEM);
3303 rc = fld_server_init(ls->ls_server_fld,
3304 m->mdt_bottom, uuid, env);
3306 OBD_FREE_PTR(ls->ls_server_fld);
3307 ls->ls_server_fld = NULL;
3314 /* device init/fini methods */
3315 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3318 if (m->mdt_regular_service != NULL) {
3319 ptlrpc_unregister_service(m->mdt_regular_service);
3320 m->mdt_regular_service = NULL;
3322 if (m->mdt_readpage_service != NULL) {
3323 ptlrpc_unregister_service(m->mdt_readpage_service);
3324 m->mdt_readpage_service = NULL;
3326 if (m->mdt_xmds_service != NULL) {
3327 ptlrpc_unregister_service(m->mdt_xmds_service);
3328 m->mdt_xmds_service = NULL;
3330 if (m->mdt_setattr_service != NULL) {
3331 ptlrpc_unregister_service(m->mdt_setattr_service);
3332 m->mdt_setattr_service = NULL;
3334 if (m->mdt_mdsc_service != NULL) {
3335 ptlrpc_unregister_service(m->mdt_mdsc_service);
3336 m->mdt_mdsc_service = NULL;
3338 if (m->mdt_mdss_service != NULL) {
3339 ptlrpc_unregister_service(m->mdt_mdss_service);
3340 m->mdt_mdss_service = NULL;
3342 if (m->mdt_dtss_service != NULL) {
3343 ptlrpc_unregister_service(m->mdt_dtss_service);
3344 m->mdt_dtss_service = NULL;
3346 if (m->mdt_fld_service != NULL) {
3347 ptlrpc_unregister_service(m->mdt_fld_service);
3348 m->mdt_fld_service = NULL;
3353 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3356 static struct ptlrpc_service_conf conf;
3357 cfs_proc_dir_entry_t *procfs_entry;
3360 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3362 conf = (typeof(conf)) {
3363 .psc_nbufs = MDS_NBUFS,
3364 .psc_bufsize = MDS_BUFSIZE,
3365 .psc_max_req_size = MDS_MAXREQSIZE,
3366 .psc_max_reply_size = MDS_MAXREPSIZE,
3367 .psc_req_portal = MDS_REQUEST_PORTAL,
3368 .psc_rep_portal = MDC_REPLY_PORTAL,
3369 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3371 * We'd like to have a mechanism to set this on a per-device
3372 * basis, but alas...
3374 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3376 .psc_max_threads = MDT_MAX_THREADS,
3377 .psc_ctx_tags = LCT_MD_THREAD
3380 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3381 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3382 "mdt_ldlm_client", m->mdt_ldlm_client);
3384 m->mdt_regular_service =
3385 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3386 procfs_entry, NULL, LUSTRE_MDT_NAME);
3387 if (m->mdt_regular_service == NULL)
3390 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3392 GOTO(err_mdt_svc, rc);
3395 * readpage service configuration. Parameters have to be adjusted,
3398 conf = (typeof(conf)) {
3399 .psc_nbufs = MDS_NBUFS,
3400 .psc_bufsize = MDS_BUFSIZE,
3401 .psc_max_req_size = MDS_MAXREQSIZE,
3402 .psc_max_reply_size = MDS_MAXREPSIZE,
3403 .psc_req_portal = MDS_READPAGE_PORTAL,
3404 .psc_rep_portal = MDC_REPLY_PORTAL,
3405 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3406 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3408 .psc_max_threads = MDT_MAX_THREADS,
3409 .psc_ctx_tags = LCT_MD_THREAD
3411 m->mdt_readpage_service =
3412 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3413 LUSTRE_MDT_NAME "_readpage",
3414 procfs_entry, NULL, "mdt_rdpg");
3416 if (m->mdt_readpage_service == NULL) {
3417 CERROR("failed to start readpage service\n");
3418 GOTO(err_mdt_svc, rc = -ENOMEM);
3421 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3424 * setattr service configuration.
3426 conf = (typeof(conf)) {
3427 .psc_nbufs = MDS_NBUFS,
3428 .psc_bufsize = MDS_BUFSIZE,
3429 .psc_max_req_size = MDS_MAXREQSIZE,
3430 .psc_max_reply_size = MDS_MAXREPSIZE,
3431 .psc_req_portal = MDS_SETATTR_PORTAL,
3432 .psc_rep_portal = MDC_REPLY_PORTAL,
3433 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3434 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3436 .psc_max_threads = MDT_MAX_THREADS,
3437 .psc_ctx_tags = LCT_MD_THREAD
3440 m->mdt_setattr_service =
3441 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3442 LUSTRE_MDT_NAME "_setattr",
3443 procfs_entry, NULL, "mdt_attr");
3445 if (!m->mdt_setattr_service) {
3446 CERROR("failed to start setattr service\n");
3447 GOTO(err_mdt_svc, rc = -ENOMEM);
3450 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3452 GOTO(err_mdt_svc, rc);
3455 * sequence controller service configuration
3457 conf = (typeof(conf)) {
3458 .psc_nbufs = MDS_NBUFS,
3459 .psc_bufsize = MDS_BUFSIZE,
3460 .psc_max_req_size = SEQ_MAXREQSIZE,
3461 .psc_max_reply_size = SEQ_MAXREPSIZE,
3462 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3463 .psc_rep_portal = MDC_REPLY_PORTAL,
3464 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3465 .psc_min_threads = SEQ_NUM_THREADS,
3466 .psc_max_threads = SEQ_NUM_THREADS,
3467 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3470 m->mdt_mdsc_service =
3471 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3472 LUSTRE_MDT_NAME"_mdsc",
3473 procfs_entry, NULL, "mdt_mdsc");
3474 if (!m->mdt_mdsc_service) {
3475 CERROR("failed to start seq controller service\n");
3476 GOTO(err_mdt_svc, rc = -ENOMEM);
3479 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3481 GOTO(err_mdt_svc, rc);
3484 * metadata sequence server service configuration
3486 conf = (typeof(conf)) {
3487 .psc_nbufs = MDS_NBUFS,
3488 .psc_bufsize = MDS_BUFSIZE,
3489 .psc_max_req_size = SEQ_MAXREQSIZE,
3490 .psc_max_reply_size = SEQ_MAXREPSIZE,
3491 .psc_req_portal = SEQ_METADATA_PORTAL,
3492 .psc_rep_portal = MDC_REPLY_PORTAL,
3493 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3494 .psc_min_threads = SEQ_NUM_THREADS,
3495 .psc_max_threads = SEQ_NUM_THREADS,
3496 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3499 m->mdt_mdss_service =
3500 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3501 LUSTRE_MDT_NAME"_mdss",
3502 procfs_entry, NULL, "mdt_mdss");
3503 if (!m->mdt_mdss_service) {
3504 CERROR("failed to start metadata seq server service\n");
3505 GOTO(err_mdt_svc, rc = -ENOMEM);
3508 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3510 GOTO(err_mdt_svc, rc);
3514 * Data sequence server service configuration. We want to have really
3515 * cluster-wide sequences space. This is why we start only one sequence
3516 * controller which manages space.
3518 conf = (typeof(conf)) {
3519 .psc_nbufs = MDS_NBUFS,
3520 .psc_bufsize = MDS_BUFSIZE,
3521 .psc_max_req_size = SEQ_MAXREQSIZE,
3522 .psc_max_reply_size = SEQ_MAXREPSIZE,
3523 .psc_req_portal = SEQ_DATA_PORTAL,
3524 .psc_rep_portal = OSC_REPLY_PORTAL,
3525 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3526 .psc_min_threads = SEQ_NUM_THREADS,
3527 .psc_max_threads = SEQ_NUM_THREADS,
3528 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3531 m->mdt_dtss_service =
3532 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3533 LUSTRE_MDT_NAME"_dtss",
3534 procfs_entry, NULL, "mdt_dtss");
3535 if (!m->mdt_dtss_service) {
3536 CERROR("failed to start data seq server service\n");
3537 GOTO(err_mdt_svc, rc = -ENOMEM);
3540 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
3542 GOTO(err_mdt_svc, rc);
3544 /* FLD service start */
3545 conf = (typeof(conf)) {
3546 .psc_nbufs = MDS_NBUFS,
3547 .psc_bufsize = MDS_BUFSIZE,
3548 .psc_max_req_size = FLD_MAXREQSIZE,
3549 .psc_max_reply_size = FLD_MAXREPSIZE,
3550 .psc_req_portal = FLD_REQUEST_PORTAL,
3551 .psc_rep_portal = MDC_REPLY_PORTAL,
3552 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3553 .psc_min_threads = FLD_NUM_THREADS,
3554 .psc_max_threads = FLD_NUM_THREADS,
3555 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
3558 m->mdt_fld_service =
3559 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3560 LUSTRE_MDT_NAME"_fld",
3561 procfs_entry, NULL, "mdt_fld");
3562 if (!m->mdt_fld_service) {
3563 CERROR("failed to start fld service\n");
3564 GOTO(err_mdt_svc, rc = -ENOMEM);
3567 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
3569 GOTO(err_mdt_svc, rc);
3572 * mds-mds service configuration. Separate portal is used to allow
3573 * mds-mds requests be not blocked during recovery.
3575 conf = (typeof(conf)) {
3576 .psc_nbufs = MDS_NBUFS,
3577 .psc_bufsize = MDS_BUFSIZE,
3578 .psc_max_req_size = MDS_MAXREQSIZE,
3579 .psc_max_reply_size = MDS_MAXREPSIZE,
3580 .psc_req_portal = MDS_MDS_PORTAL,
3581 .psc_rep_portal = MDC_REPLY_PORTAL,
3582 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3583 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3585 .psc_max_threads = MDT_MAX_THREADS,
3586 .psc_ctx_tags = LCT_MD_THREAD
3588 m->mdt_xmds_service = ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
3589 LUSTRE_MDT_NAME "_mds",
3590 procfs_entry, NULL, "mdt_xmds");
3592 if (m->mdt_xmds_service == NULL) {
3593 CERROR("failed to start readpage service\n");
3594 GOTO(err_mdt_svc, rc = -ENOMEM);
3597 rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
3599 GOTO(err_mdt_svc, rc);
3604 mdt_stop_ptlrpc_service(m);
3609 static void mdt_stack_fini(const struct lu_env *env,
3610 struct mdt_device *m, struct lu_device *top)
3612 struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3613 struct lustre_cfg_bufs *bufs;
3614 struct lustre_cfg *lcfg;
3615 struct mdt_thread_info *info;
3619 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3620 LASSERT(info != NULL);
3622 bufs = &info->mti_u.bufs;
3623 /* process cleanup, pass mdt obd name to get obd umount flags */
3624 lustre_cfg_bufs_reset(bufs, obd->obd_name);
3629 lustre_cfg_bufs_set_string(bufs, 1, flags);
3630 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3632 CERROR("Cannot alloc lcfg!\n");
3637 top->ld_ops->ldo_process_config(env, top, lcfg);
3638 lustre_cfg_free(lcfg);
3640 lu_stack_fini(env, top);
3641 m->mdt_child = NULL;
3642 m->mdt_bottom = NULL;
3645 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3646 const char *typename,
3647 struct lu_device *child,
3648 struct lustre_cfg *cfg)
3650 const char *dev = lustre_cfg_string(cfg, 0);
3651 struct obd_type *type;
3652 struct lu_device_type *ldt;
3653 struct lu_device *d;
3658 type = class_get_type(typename);
3660 CERROR("Unknown type: '%s'\n", typename);
3661 GOTO(out, rc = -ENODEV);
3664 rc = lu_context_refill(&env->le_ctx);
3666 CERROR("Failure to refill context: '%d'\n", rc);
3670 if (env->le_ses != NULL) {
3671 rc = lu_context_refill(env->le_ses);
3673 CERROR("Failure to refill session: '%d'\n", rc);
3680 CERROR("type: '%s'\n", typename);
3681 GOTO(out_type, rc = -EINVAL);
3684 ldt->ldt_obd_type = type;
3685 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3687 CERROR("Cannot allocate device: '%s'\n", typename);
3688 GOTO(out_type, rc = -ENODEV);
3691 LASSERT(child->ld_site);
3692 d->ld_site = child->ld_site;
3695 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3697 CERROR("can't init device '%s', rc %d\n", typename, rc);
3698 GOTO(out_alloc, rc);
3705 ldt->ldt_ops->ldto_device_free(env, d);
3708 class_put_type(type);
3713 static int mdt_stack_init(const struct lu_env *env,
3714 struct mdt_device *m, struct lustre_cfg *cfg)
3716 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3717 struct lu_device *tmp;
3718 struct md_device *md;
3722 /* init the stack */
3723 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3725 RETURN(PTR_ERR(tmp));
3727 m->mdt_bottom = lu2dt_dev(tmp);
3729 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3731 GOTO(out, rc = PTR_ERR(tmp));
3736 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3738 GOTO(out, rc = PTR_ERR(tmp));
3741 /*set mdd upcall device*/
3742 md_upcall_dev_set(md, lu2md_dev(d));
3745 /*set cmm upcall device*/
3746 md_upcall_dev_set(md, &m->mdt_md_dev);
3748 m->mdt_child = lu2md_dev(d);
3750 /* process setup config */
3751 tmp = &m->mdt_md_dev.md_lu_dev;
3752 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3755 /* fini from last known good lu_device */
3757 mdt_stack_fini(env, m, d);
3762 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3764 struct md_device *next = m->mdt_child;
3765 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3766 struct lu_site *ls = d->ld_site;
3767 struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3770 ping_evictor_stop();
3772 target_recovery_fini(obd);
3773 mdt_stop_ptlrpc_service(m);
3775 mdt_fs_cleanup(env, m);
3777 upcall_cache_cleanup(m->mdt_identity_cache);
3778 m->mdt_identity_cache = NULL;
3780 if (m->mdt_namespace != NULL) {
3781 ldlm_namespace_free(m->mdt_namespace, NULL, d->ld_obd->obd_force);
3782 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3785 mdt_seq_fini(env, m);
3786 mdt_seq_fini_cli(m);
3787 mdt_fld_fini(env, m);
3789 ptlrpc_lprocfs_unregister_obd(d->ld_obd);
3790 lprocfs_obd_cleanup(d->ld_obd);
3792 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
3794 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3795 cfs_timer_disarm(&m->mdt_ck_timer);
3796 mdt_ck_thread_stop(m);
3798 /* finish the stack */
3799 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3806 LASSERT(atomic_read(&d->ld_ref) == 0);
3807 md_device_fini(&m->mdt_md_dev);
3812 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3816 #ifdef CONFIG_FS_POSIX_ACL
3817 /* ACLs should be enabled by default (b=13829) */
3818 m->mdt_opts.mo_acl = 1;
3819 LCONSOLE_INFO("Enabling ACL\n");
3821 m->mdt_opts.mo_acl = 0;
3822 LCONSOLE_INFO("Disabling ACL\n");
3831 while (*p && *p != ',')
3835 if ((len == sizeof("user_xattr") - 1) &&
3836 (memcmp(options, "user_xattr", len) == 0)) {
3837 m->mdt_opts.mo_user_xattr = 1;
3838 LCONSOLE_INFO("Enabling user_xattr\n");
3839 } else if ((len == sizeof("nouser_xattr") - 1) &&
3840 (memcmp(options, "nouser_xattr", len) == 0)) {
3841 m->mdt_opts.mo_user_xattr = 0;
3842 LCONSOLE_INFO("Disabling user_xattr\n");
3843 } else if ((len == sizeof("noacl") - 1) &&
3844 (memcmp(options, "noacl", len) == 0)) {
3845 m->mdt_opts.mo_acl = 0;
3846 LCONSOLE_INFO("Disabling ACL\n");
3853 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3855 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3856 struct lu_device_type *ldt, struct lustre_cfg *cfg)
3858 struct lprocfs_static_vars lvars;
3859 struct mdt_thread_info *info;
3860 struct obd_device *obd;
3861 const char *dev = lustre_cfg_string(cfg, 0);
3862 const char *num = lustre_cfg_string(cfg, 2);
3863 struct lustre_mount_info *lmi;
3864 struct lustre_sb_info *lsi;
3866 const char *identity_upcall = "NONE";
3870 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3871 LASSERT(info != NULL);
3873 obd = class_name2obd(dev);
3874 LASSERT(obd != NULL);
3876 spin_lock_init(&m->mdt_transno_lock);
3878 m->mdt_max_mdsize = MAX_MD_SIZE;
3879 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3881 m->mdt_opts.mo_user_xattr = 0;
3882 m->mdt_opts.mo_acl = 0;
3883 lmi = server_get_mount_2(dev);
3885 CERROR("Cannot get mount info for %s!\n", dev);
3888 lsi = s2lsi(lmi->lmi_sb);
3889 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3890 server_put_mount_2(dev, lmi->lmi_mnt);
3893 m->mdt_sptlrpc_lock = RW_LOCK_UNLOCKED;
3894 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
3896 spin_lock_init(&m->mdt_ioepoch_lock);
3897 m->mdt_opts.mo_compat_resname = 0;
3898 m->mdt_capa_timeout = CAPA_TIMEOUT;
3899 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3900 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3902 spin_lock_init(&m->mdt_client_bitmap_lock);
3908 md_device_init(&m->mdt_md_dev, ldt);
3909 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3910 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3911 /* set this lu_device to obd, because error handling need it */
3912 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3914 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3916 CERROR("Can't init lu_site, rc %d\n", rc);
3917 GOTO(err_free_site, rc);
3920 lprocfs_mdt_init_vars(&lvars);
3921 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3923 CERROR("Can't init lprocfs, rc %d\n", rc);
3924 GOTO(err_fini_site, rc);
3926 ptlrpc_lprocfs_register_obd(obd);
3928 rc = mdt_procfs_init(m, dev);
3930 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
3931 GOTO(err_fini_proc, rc);
3934 /* set server index */
3936 s->ls_node_id = simple_strtol(num, NULL, 10);
3938 /* failover is the default
3939 * FIXME: we do not failout mds0/mgs, which may cause some problems.
3940 * assumed whose ls_node_id == 0 XXX
3942 obd->obd_replayable = 1;
3943 /* No connection accepted until configurations will finish */
3944 obd->obd_no_conn = 1;
3946 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
3947 char *str = lustre_cfg_string(cfg, 4);
3948 if (strchr(str, 'n')) {
3949 CWARN("%s: recovery disabled\n", obd->obd_name);
3950 obd->obd_replayable = 0;
3954 /* init the stack */
3955 rc = mdt_stack_init(env, m, cfg);
3957 CERROR("Can't init device stack, rc %d\n", rc);
3958 GOTO(err_fini_proc, rc);
3961 rc = mdt_fld_init(env, obd->obd_name, m);
3963 GOTO(err_fini_stack, rc);
3965 rc = mdt_seq_init(env, obd->obd_name, m);
3967 GOTO(err_fini_fld, rc);
3969 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
3970 LUSTRE_MDT_NAME"-%p", m);
3971 m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
3972 LDLM_NAMESPACE_SERVER,
3973 LDLM_NAMESPACE_GREEDY);
3974 if (m->mdt_namespace == NULL)
3975 GOTO(err_fini_seq, rc = -ENOMEM);
3977 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
3978 /* set obd_namespace for compatibility with old code */
3979 obd->obd_namespace = m->mdt_namespace;
3981 /* XXX: to support suppgid for ACL, we enable identity_upcall
3982 * by default, otherwise, maybe got unexpected -EACCESS. */
3983 if (m->mdt_opts.mo_acl)
3984 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
3986 m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
3987 &mdt_identity_upcall_cache_ops);
3988 if (IS_ERR(m->mdt_identity_cache)) {
3989 rc = PTR_ERR(m->mdt_identity_cache);
3990 m->mdt_identity_cache = NULL;
3991 GOTO(err_free_ns, rc);
3994 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
3996 rc = mdt_ck_thread_start(m);
3998 GOTO(err_free_ns, rc);
4000 rc = mdt_fs_setup(env, m, obd);
4004 target_recovery_init(obd, mdt_recovery_handle);
4006 rc = mdt_start_ptlrpc_service(m);
4008 GOTO(err_fs_cleanup, rc);
4010 ping_evictor_start();
4012 rc = lu_site_init_finish(s);
4014 GOTO(err_stop_service, rc);
4016 if (obd->obd_recovering == 0)
4017 mdt_postrecov(env, m);
4019 mdt_init_capa_ctxt(env, m);
4021 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4027 ping_evictor_stop();
4028 mdt_stop_ptlrpc_service(m);
4030 target_recovery_fini(obd);
4031 mdt_fs_cleanup(env, m);
4033 cfs_timer_disarm(&m->mdt_ck_timer);
4034 mdt_ck_thread_stop(m);
4036 upcall_cache_cleanup(m->mdt_identity_cache);
4037 m->mdt_identity_cache = NULL;
4038 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4039 obd->obd_namespace = m->mdt_namespace = NULL;
4041 mdt_seq_fini(env, m);
4043 mdt_fld_fini(env, m);
4045 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4048 lprocfs_obd_cleanup(obd);
4054 md_device_fini(&m->mdt_md_dev);
4058 /* used by MGS to process specific configurations */
4059 static int mdt_process_config(const struct lu_env *env,
4060 struct lu_device *d, struct lustre_cfg *cfg)
4062 struct mdt_device *m = mdt_dev(d);
4063 struct md_device *md_next = m->mdt_child;
4064 struct lu_device *next = md2lu_dev(md_next);
4068 switch (cfg->lcfg_command) {
4069 case LCFG_SPTLRPC_CONF: {
4070 struct sptlrpc_conf_log *log;
4071 struct sptlrpc_rule_set tmp_rset;
4073 log = sptlrpc_conf_log_extract(cfg);
4079 sptlrpc_rule_set_init(&tmp_rset);
4081 rc = sptlrpc_rule_set_from_log(&tmp_rset, log);
4083 CERROR("mdt %p: failed get sptlrpc rules: %d\n", m, rc);
4087 write_lock(&m->mdt_sptlrpc_lock);
4088 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4089 m->mdt_sptlrpc_rset = tmp_rset;
4090 write_unlock(&m->mdt_sptlrpc_lock);
4092 sptlrpc_target_update_exp_flavor(
4093 md2lu_dev(&m->mdt_md_dev)->ld_obd, &tmp_rset);
4098 struct lprocfs_static_vars lvars;
4099 struct obd_device *obd = d->ld_obd;
4101 lprocfs_mdt_init_vars(&lvars);
4102 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
4104 /* others are passed further */
4105 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4110 * Add mdc hook to get first MDT uuid and connect it to
4111 * ls->controller to use for seq manager.
4113 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4115 CERROR("Can't add mdc, rc %d\n", rc);
4117 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4120 /* others are passed further */
4121 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4127 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4128 const struct lu_object_header *hdr,
4129 struct lu_device *d)
4131 struct mdt_object *mo;
4137 struct lu_object *o;
4138 struct lu_object_header *h;
4140 o = &mo->mot_obj.mo_lu;
4141 h = &mo->mot_header;
4142 lu_object_header_init(h);
4143 lu_object_init(o, h, d);
4144 lu_object_add_top(h, o);
4145 o->lo_ops = &mdt_obj_ops;
4151 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
4153 struct mdt_device *d = mdt_dev(o->lo_dev);
4154 struct lu_device *under;
4155 struct lu_object *below;
4159 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4160 PFID(lu_object_fid(o)));
4162 under = &d->mdt_child->md_lu_dev;
4163 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4164 if (below != NULL) {
4165 lu_object_add(o, below);
4172 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4174 struct mdt_object *mo = mdt_obj(o);
4175 struct lu_object_header *h;
4179 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4180 PFID(lu_object_fid(o)));
4183 lu_object_header_fini(h);
4188 static int mdt_object_print(const struct lu_env *env, void *cookie,
4189 lu_printer_t p, const struct lu_object *o)
4191 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
4194 static struct lu_device_operations mdt_lu_ops = {
4195 .ldo_object_alloc = mdt_object_alloc,
4196 .ldo_process_config = mdt_process_config
4199 static struct lu_object_operations mdt_obj_ops = {
4200 .loo_object_init = mdt_object_init,
4201 .loo_object_free = mdt_object_free,
4202 .loo_object_print = mdt_object_print
4205 /* mds_connect_internal */
4206 static int mdt_connect_internal(struct obd_export *exp,
4207 struct mdt_device *mdt,
4208 struct obd_connect_data *data)
4213 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4214 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4216 /* If no known bits (which should not happen, probably,
4217 as everybody should support LOOKUP and UPDATE bits at least)
4218 revert to compat mode with plain locks. */
4219 if (!data->ocd_ibits_known &&
4220 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4221 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4223 if (!mdt->mdt_opts.mo_acl)
4224 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4226 if (!mdt->mdt_opts.mo_user_xattr)
4227 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4229 if (!mdt->mdt_opts.mo_mds_capa)
4230 data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
4232 if (!mdt->mdt_opts.mo_oss_capa)
4233 data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
4235 spin_lock(&exp->exp_lock);
4236 exp->exp_connect_flags = data->ocd_connect_flags;
4237 spin_unlock(&exp->exp_lock);
4238 data->ocd_version = LUSTRE_VERSION_CODE;
4239 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4243 if (mdt->mdt_opts.mo_acl &&
4244 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4245 CWARN("%s: MDS requires ACL support but client does not\n",
4246 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4251 flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
4252 if ((exp->exp_connect_flags & flags) == flags) {
4253 CWARN("%s: both local and remote client flags are set\n",
4254 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4258 if (mdt->mdt_opts.mo_mds_capa &&
4259 ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
4260 CWARN("%s: MDS requires capability support, but client not\n",
4261 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4265 if (mdt->mdt_opts.mo_oss_capa &&
4266 ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
4267 CWARN("%s: MDS requires OSS capability support, "
4269 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4273 if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
4274 CWARN("%s: MDS requires FID support, but client not\n",
4275 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4282 /* mds_connect copy */
4283 static int mdt_obd_connect(const struct lu_env *env,
4284 struct lustre_handle *conn, struct obd_device *obd,
4285 struct obd_uuid *cluuid,
4286 struct obd_connect_data *data,
4289 struct mdt_thread_info *info;
4290 struct mdt_client_data *mcd;
4291 struct obd_export *exp;
4292 struct mdt_device *mdt;
4293 struct ptlrpc_request *req;
4297 LASSERT(env != NULL);
4298 if (!conn || !obd || !cluuid)
4301 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4302 req = info->mti_pill->rc_req;
4303 mdt = mdt_dev(obd->obd_lu_dev);
4305 rc = class_connect(conn, obd, cluuid);
4309 exp = class_conn2export(conn);
4310 LASSERT(exp != NULL);
4312 CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
4314 spin_lock(&exp->exp_lock);
4315 exp->exp_sp_peer = req->rq_sp_from;
4317 read_lock(&mdt->mdt_sptlrpc_lock);
4318 sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset, exp->exp_sp_peer,
4319 req->rq_peer.nid, &exp->exp_flvr);
4320 read_unlock(&mdt->mdt_sptlrpc_lock);
4322 if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4323 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
4324 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
4325 libcfs_nid2str(req->rq_peer.nid));
4326 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
4327 spin_unlock(&exp->exp_lock);
4330 spin_unlock(&exp->exp_lock);
4332 rc = mdt_connect_internal(exp, mdt, data);
4336 struct mdt_thread_info *mti;
4337 mti = lu_context_key_get(&env->le_ctx,
4339 LASSERT(mti != NULL);
4341 memcpy(mcd->mcd_uuid, cluuid, sizeof mcd->mcd_uuid);
4342 exp->exp_mdt_data.med_mcd = mcd;
4343 rc = mdt_client_new(env, mdt);
4346 exp->exp_mdt_data.med_mcd = NULL;
4353 class_disconnect(exp);
4355 class_export_put(exp);
4360 static int mdt_obd_reconnect(const struct lu_env *env,
4361 struct obd_export *exp, struct obd_device *obd,
4362 struct obd_uuid *cluuid,
4363 struct obd_connect_data *data)
4365 struct mdt_thread_info *info;
4366 struct mdt_device *mdt;
4367 struct ptlrpc_request *req;
4371 if (exp == NULL || obd == NULL || cluuid == NULL)
4374 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4375 req = info->mti_pill->rc_req;
4376 mdt = mdt_dev(obd->obd_lu_dev);
4378 CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
4380 spin_lock(&exp->exp_lock);
4381 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
4382 exp->exp_sp_peer = req->rq_sp_from;
4384 read_lock(&mdt->mdt_sptlrpc_lock);
4385 sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset,
4387 req->rq_peer.nid, &exp->exp_flvr);
4388 read_unlock(&mdt->mdt_sptlrpc_lock);
4390 if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4391 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
4392 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
4393 libcfs_nid2str(req->rq_peer.nid));
4394 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
4395 spin_unlock(&exp->exp_lock);
4399 spin_unlock(&exp->exp_lock);
4401 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4406 static int mdt_obd_disconnect(struct obd_export *exp)
4408 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
4413 class_export_get(exp);
4415 /* Disconnect early so that clients can't keep using export */
4416 rc = class_disconnect(exp);
4417 if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
4418 ldlm_cancel_locks_for_export(exp);
4420 /* complete all outstanding replies */
4421 spin_lock(&exp->exp_lock);
4422 while (!list_empty(&exp->exp_outstanding_replies)) {
4423 struct ptlrpc_reply_state *rs =
4424 list_entry(exp->exp_outstanding_replies.next,
4425 struct ptlrpc_reply_state, rs_exp_list);
4426 struct ptlrpc_service *svc = rs->rs_service;
4428 spin_lock(&svc->srv_lock);
4429 list_del_init(&rs->rs_exp_list);
4430 ptlrpc_schedule_difficult_reply(rs);
4431 spin_unlock(&svc->srv_lock);
4433 spin_unlock(&exp->exp_lock);
4435 class_export_put(exp);
4439 /* FIXME: Can we avoid using these two interfaces? */
4440 static int mdt_init_export(struct obd_export *exp)
4442 struct mdt_export_data *med = &exp->exp_mdt_data;
4445 CFS_INIT_LIST_HEAD(&med->med_open_head);
4446 spin_lock_init(&med->med_open_lock);
4447 sema_init(&med->med_idmap_sem, 1);
4448 med->med_idmap = NULL;
4449 spin_lock(&exp->exp_lock);
4450 exp->exp_connecting = 1;
4451 spin_unlock(&exp->exp_lock);
4455 static int mdt_destroy_export(struct obd_export *export)
4457 struct mdt_export_data *med;
4458 struct obd_device *obd = export->exp_obd;
4459 struct mdt_device *mdt;
4460 struct mdt_thread_info *info;
4468 med = &export->exp_mdt_data;
4469 if (med->med_rmtclient)
4470 mdt_cleanup_idmap(med);
4472 target_destroy_export(export);
4474 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
4477 mdt = mdt_dev(obd->obd_lu_dev);
4478 LASSERT(mdt != NULL);
4480 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4484 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4485 LASSERT(info != NULL);
4486 memset(info, 0, sizeof *info);
4487 info->mti_env = &env;
4488 info->mti_mdt = mdt;
4489 info->mti_exp = export;
4491 ma = &info->mti_attr;
4492 lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4493 cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4494 OBD_ALLOC(ma->ma_lmm, lmm_size);
4495 OBD_ALLOC(ma->ma_cookie, cookie_size);
4497 if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4498 GOTO(out, rc = -ENOMEM);
4499 ma->ma_need = MA_LOV | MA_COOKIE;
4501 /* Close any open files (which may also cause orphan unlinking). */
4502 spin_lock(&med->med_open_lock);
4503 while (!list_empty(&med->med_open_head)) {
4504 struct list_head *tmp = med->med_open_head.next;
4505 struct mdt_file_data *mfd =
4506 list_entry(tmp, struct mdt_file_data, mfd_list);
4508 /* Remove mfd handle so it can't be found again.
4509 * We are consuming the mfd_list reference here. */
4510 class_handle_unhash(&mfd->mfd_handle);
4511 list_del_init(&mfd->mfd_list);
4512 spin_unlock(&med->med_open_lock);
4513 mdt_mfd_close(info, mfd);
4514 /* TODO: if we close the unlinked file,
4515 * we need to remove it's objects from OST */
4516 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4517 spin_lock(&med->med_open_lock);
4518 ma->ma_lmm_size = lmm_size;
4519 ma->ma_cookie_size = cookie_size;
4520 ma->ma_need = MA_LOV | MA_COOKIE;
4523 spin_unlock(&med->med_open_lock);
4524 info->mti_mdt = NULL;
4525 mdt_client_del(&env, mdt);
4530 OBD_FREE(ma->ma_lmm, lmm_size);
4534 OBD_FREE(ma->ma_cookie, cookie_size);
4535 ma->ma_cookie = NULL;
4542 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
4544 if (flag & CONFIG_LOG)
4545 m->mdt_fl_cfglog = 1;
4546 if (flag & CONFIG_SYNC)
4547 m->mdt_fl_synced = 1;
4549 if (m->mdt_fl_cfglog /* bz11778: && m->mdt_fl_synced */)
4550 /* Open for clients */
4551 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
4554 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4555 enum md_upcall_event ev)
4557 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4558 struct md_device *next = m->mdt_child;
4559 struct mdt_thread_info *mti;
4565 rc = next->md_ops->mdo_maxsize_get(env, next,
4567 &m->mdt_max_cookiesize);
4568 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4569 m->mdt_max_mdsize, m->mdt_max_cookiesize);
4570 mdt_allow_cli(m, CONFIG_SYNC);
4573 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4574 mti->mti_no_need_trans = 1;
4575 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4578 /* Check that MDT is not yet configured */
4579 LASSERT(!m->mdt_fl_cfglog);
4582 CERROR("invalid event\n");
4589 static int mdt_obd_notify(struct obd_device *host,
4590 struct obd_device *watched,
4591 enum obd_notify_event ev, void *data)
4596 case OBD_NOTIFY_CONFIG:
4597 mdt_allow_cli(mdt_dev(host->obd_lu_dev), (unsigned int)data);
4600 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
4605 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4606 void *karg, void *uarg)
4609 struct obd_device *obd= exp->exp_obd;
4610 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4611 struct dt_device *dt = mdt->mdt_bottom;
4615 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4616 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4622 rc = dt->dd_ops->dt_sync(&env, dt);
4624 case OBD_IOC_SET_READONLY:
4625 rc = dt->dd_ops->dt_sync(&env, dt);
4626 dt->dd_ops->dt_ro(&env, dt);
4628 case OBD_IOC_ABORT_RECOVERY:
4629 CERROR("Aborting recovery for device %s\n", obd->obd_name);
4630 target_stop_recovery_thread(obd);
4634 CERROR("Not supported cmd = %d for device %s\n",
4635 cmd, obd->obd_name);
4643 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4645 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4646 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
4649 /* if some clients didn't participate in recovery then we can possibly
4650 * lost sequence. Now we should increase sequence for safe value */
4651 lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
4652 mdt_seq_adjust(env, mdt, lost);
4654 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4658 int mdt_obd_postrecov(struct obd_device *obd)
4663 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4666 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4671 static struct obd_ops mdt_obd_device_ops = {
4672 .o_owner = THIS_MODULE,
4673 .o_connect = mdt_obd_connect,
4674 .o_reconnect = mdt_obd_reconnect,
4675 .o_disconnect = mdt_obd_disconnect,
4676 .o_init_export = mdt_init_export,
4677 .o_destroy_export = mdt_destroy_export,
4678 .o_iocontrol = mdt_iocontrol,
4679 .o_postrecov = mdt_obd_postrecov,
4680 .o_notify = mdt_obd_notify
4683 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4684 struct lu_device *d)
4686 struct mdt_device *m = mdt_dev(d);
4693 static struct lu_device *mdt_device_free(const struct lu_env *env,
4694 struct lu_device *d)
4696 struct mdt_device *m = mdt_dev(d);
4703 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4704 struct lu_device_type *t,
4705 struct lustre_cfg *cfg)
4707 struct lu_device *l;
4708 struct mdt_device *m;
4714 l = &m->mdt_md_dev.md_lu_dev;
4715 rc = mdt_init0(env, m, t, cfg);
4721 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
4723 l = ERR_PTR(-ENOMEM);
4727 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
4728 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
4730 /* context key: mdt_thread_key */
4731 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
4733 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
4734 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
4736 struct lu_context_key mdt_txn_key = {
4737 .lct_tags = LCT_TX_HANDLE,
4738 .lct_init = mdt_txn_key_init,
4739 .lct_fini = mdt_txn_key_fini
4742 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4744 return md_ucred(info->mti_env);
4747 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
4748 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
4750 static struct lu_device_type_operations mdt_device_type_ops = {
4751 .ldto_init = mdt_type_init,
4752 .ldto_fini = mdt_type_fini,
4754 .ldto_device_alloc = mdt_device_alloc,
4755 .ldto_device_free = mdt_device_free,
4756 .ldto_device_fini = mdt_device_fini
4759 static struct lu_device_type mdt_device_type = {
4760 .ldt_tags = LU_DEVICE_MD,
4761 .ldt_name = LUSTRE_MDT_NAME,
4762 .ldt_ops = &mdt_device_type_ops,
4763 .ldt_ctx_tags = LCT_MD_THREAD
4766 static int __init mdt_mod_init(void)
4768 struct lprocfs_static_vars lvars;
4771 mdt_num_threads = MDT_NUM_THREADS;
4772 lprocfs_mdt_init_vars(&lvars);
4773 rc = class_register_type(&mdt_obd_device_ops, NULL,
4774 lvars.module_vars, LUSTRE_MDT_NAME,
4780 static void __exit mdt_mod_exit(void)
4782 class_unregister_type(LUSTRE_MDT_NAME);
4786 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
4787 [prefix ## _ ## opc - prefix ## _ ## base] = { \
4789 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
4790 .mh_opc = prefix ## _ ## opc, \
4791 .mh_flags = flags, \
4796 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
4797 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4799 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
4800 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4802 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
4803 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4805 * Request with a format known in advance
4807 #define DEF_MDT_HNDL_F(flags, name, fn) \
4808 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4810 #define DEF_SEQ_HNDL_F(flags, name, fn) \
4811 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4813 #define DEF_FLD_HNDL_F(flags, name, fn) \
4814 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4816 * Request with a format we do not yet know
4818 #define DEF_MDT_HNDL_0(flags, name, fn) \
4819 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4821 static struct mdt_handler mdt_mds_ops[] = {
4822 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4823 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
4824 DEF_MDT_HNDL_F(0, SET_INFO, mdt_set_info),
4825 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
4826 DEF_MDT_HNDL_F(HABEO_CORPUS, GETATTR, mdt_getattr),
4827 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4828 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
4829 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
4830 DEF_MDT_HNDL_F(0 |MUTABOR, REINT, mdt_reint),
4831 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
4832 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
4833 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
4834 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
4835 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
4836 DEF_MDT_HNDL_F(0, QUOTACHECK, mdt_quotacheck_handle),
4837 DEF_MDT_HNDL_F(0, QUOTACTL, mdt_quotactl_handle)
4840 #define DEF_OBD_HNDL(flags, name, fn) \
4841 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4844 static struct mdt_handler mdt_obd_ops[] = {
4845 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
4846 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
4847 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
4850 #define DEF_DLM_HNDL_0(flags, name, fn) \
4851 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4852 #define DEF_DLM_HNDL_F(flags, name, fn) \
4853 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4855 static struct mdt_handler mdt_dlm_ops[] = {
4856 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
4857 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
4858 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
4859 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
4862 static struct mdt_handler mdt_llog_ops[] = {
4865 #define DEF_SEC_CTX_HNDL(name, fn) \
4866 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4868 static struct mdt_handler mdt_sec_ctx_ops[] = {
4869 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
4870 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
4871 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
4874 static struct mdt_opc_slice mdt_regular_handlers[] = {
4876 .mos_opc_start = MDS_GETATTR,
4877 .mos_opc_end = MDS_LAST_OPC,
4878 .mos_hs = mdt_mds_ops
4881 .mos_opc_start = OBD_PING,
4882 .mos_opc_end = OBD_LAST_OPC,
4883 .mos_hs = mdt_obd_ops
4886 .mos_opc_start = LDLM_ENQUEUE,
4887 .mos_opc_end = LDLM_LAST_OPC,
4888 .mos_hs = mdt_dlm_ops
4891 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4892 .mos_opc_end = LLOG_LAST_OPC,
4893 .mos_hs = mdt_llog_ops
4896 .mos_opc_start = SEC_CTX_INIT,
4897 .mos_opc_end = SEC_LAST_OPC,
4898 .mos_hs = mdt_sec_ctx_ops
4905 static struct mdt_handler mdt_readpage_ops[] = {
4906 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4907 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4908 #ifdef HAVE_SPLIT_SUPPORT
4909 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4913 * XXX: this is ugly and should be fixed one day, see mdc_close() for
4914 * detailed comments. --umka
4916 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
4917 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
4920 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4922 .mos_opc_start = MDS_GETATTR,
4923 .mos_opc_end = MDS_LAST_OPC,
4924 .mos_hs = mdt_readpage_ops
4931 static struct mdt_handler mdt_xmds_ops[] = {
4932 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4933 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
4934 DEF_MDT_HNDL_F(0 | MUTABOR , REINT, mdt_reint),
4935 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
4938 static struct mdt_opc_slice mdt_xmds_handlers[] = {
4940 .mos_opc_start = MDS_GETATTR,
4941 .mos_opc_end = MDS_LAST_OPC,
4942 .mos_hs = mdt_xmds_ops
4945 .mos_opc_start = OBD_PING,
4946 .mos_opc_end = OBD_LAST_OPC,
4947 .mos_hs = mdt_obd_ops
4950 .mos_opc_start = SEC_CTX_INIT,
4951 .mos_opc_end = SEC_LAST_OPC,
4952 .mos_hs = mdt_sec_ctx_ops
4959 static struct mdt_handler mdt_seq_ops[] = {
4960 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
4963 static struct mdt_opc_slice mdt_seq_handlers[] = {
4965 .mos_opc_start = SEQ_QUERY,
4966 .mos_opc_end = SEQ_LAST_OPC,
4967 .mos_hs = mdt_seq_ops
4974 static struct mdt_handler mdt_fld_ops[] = {
4975 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
4978 static struct mdt_opc_slice mdt_fld_handlers[] = {
4980 .mos_opc_start = FLD_QUERY,
4981 .mos_opc_end = FLD_LAST_OPC,
4982 .mos_hs = mdt_fld_ops
4989 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4990 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
4991 MODULE_LICENSE("GPL");
4993 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
4994 "number of mdt service threads to start");
4996 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);