1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
50 # define EXPORT_SYMTAB
52 #define DEBUG_SUBSYSTEM S_MDS
54 #include <linux/module.h>
56 * struct OBD_{ALLOC,FREE}*()
58 #include <obd_support.h>
59 /* struct ptlrpc_request */
60 #include <lustre_net.h>
61 /* struct obd_export */
62 #include <lustre_export.h>
63 /* struct obd_device */
66 #include <dt_object.h>
67 #include <lustre_mds.h>
68 #include <lustre_mdt.h>
69 #include "mdt_internal.h"
70 #ifdef HAVE_QUOTA_SUPPORT
71 # include <lustre_quota.h>
73 #include <lustre_acl.h>
74 #include <lustre_param.h>
75 #include <lustre_fsfilt.h>
77 mdl_mode_t mdt_mdl_lock_modes[] = {
78 [LCK_MINMODE] = MDL_MINMODE,
85 [LCK_GROUP] = MDL_GROUP
88 ldlm_mode_t mdt_dlm_lock_modes[] = {
89 [MDL_MINMODE] = LCK_MINMODE,
96 [MDL_GROUP] = LCK_GROUP
100 * Initialized in mdt_mod_init().
102 unsigned long mdt_num_threads;
104 /* ptlrpc request handler for MDT. All handlers are
105 * grouped into several slices - struct mdt_opc_slice,
106 * and stored in an array - mdt_handlers[].
109 /* The name of this handler. */
111 /* Fail id for this handler, checked at the beginning of this handler*/
113 /* Operation code for this handler */
115 /* flags are listed in enum mdt_handler_flags below. */
117 /* The actual handler function to execute. */
118 int (*mh_act)(struct mdt_thread_info *info);
119 /* Request format for this request. */
120 const struct req_format *mh_fmt;
123 enum mdt_handler_flags {
125 * struct mdt_body is passed in the incoming message, and object
126 * identified by this fid exists on disk.
128 * "habeo corpus" == "I have a body"
130 HABEO_CORPUS = (1 << 0),
132 * struct ldlm_request is passed in the incoming message.
134 * "habeo clavis" == "I have a key"
136 HABEO_CLAVIS = (1 << 1),
138 * this request has fixed reply format, so that reply message can be
139 * packed by generic code.
141 * "habeo refero" == "I have a reply"
143 HABEO_REFERO = (1 << 2),
145 * this request will modify something, so check whether the filesystem
146 * is readonly or not, then return -EROFS to client asap if necessary.
148 * "mutabor" == "I shall modify"
153 struct mdt_opc_slice {
156 struct mdt_handler *mos_hs;
159 static struct mdt_opc_slice mdt_regular_handlers[];
160 static struct mdt_opc_slice mdt_readpage_handlers[];
161 static struct mdt_opc_slice mdt_xmds_handlers[];
162 static struct mdt_opc_slice mdt_seq_handlers[];
163 static struct mdt_opc_slice mdt_fld_handlers[];
165 static struct mdt_device *mdt_dev(struct lu_device *d);
166 static int mdt_regular_handle(struct ptlrpc_request *req);
167 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
168 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
169 struct getinfo_fid2path *fp);
171 static const struct lu_object_operations mdt_obj_ops;
173 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
177 return (rep->lock_policy_res1 & flag);
180 void mdt_clear_disposition(struct mdt_thread_info *info,
181 struct ldlm_reply *rep, int flag)
184 info->mti_opdata &= ~flag;
186 rep->lock_policy_res1 &= ~flag;
189 void mdt_set_disposition(struct mdt_thread_info *info,
190 struct ldlm_reply *rep, int flag)
193 info->mti_opdata |= flag;
195 rep->lock_policy_res1 |= flag;
198 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
200 lh->mlh_pdo_hash = 0;
201 lh->mlh_reg_mode = lm;
202 lh->mlh_type = MDT_REG_LOCK;
205 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
206 const char *name, int namelen)
208 lh->mlh_reg_mode = lm;
209 lh->mlh_type = MDT_PDO_LOCK;
212 LASSERT(namelen > 0);
213 lh->mlh_pdo_hash = full_name_hash(name, namelen);
215 LASSERT(namelen == 0);
216 lh->mlh_pdo_hash = 0ull;
220 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
221 struct mdt_lock_handle *lh)
227 * Any dir access needs couple of locks:
229 * 1) on part of dir we gonna take lookup/modify;
231 * 2) on whole dir to protect it from concurrent splitting and/or to
232 * flush client's cache for readdir().
234 * so, for a given mode and object this routine decides what lock mode
235 * to use for lock #2:
237 * 1) if caller's gonna lookup in dir then we need to protect dir from
238 * being splitted only - LCK_CR
240 * 2) if caller's gonna modify dir then we need to protect dir from
241 * being splitted and to flush cache - LCK_CW
243 * 3) if caller's gonna modify dir and that dir seems ready for
244 * splitting then we need to protect it from any type of access
245 * (lookup/modify/split) - LCK_EX --bzzz
248 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
249 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
252 * Ask underlaying level its opinion about preferable PDO lock mode
253 * having access type passed as regular lock mode:
255 * - MDL_MINMODE means that lower layer does not want to specify lock
258 * - MDL_NL means that no PDO lock should be taken. This is used in some
259 * cases. Say, for non-splittable directories no need to use PDO locks
262 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
263 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
265 if (mode != MDL_MINMODE) {
266 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
269 * Lower layer does not want to specify locking mode. We do it
270 * our selves. No special protection is needed, just flush
271 * client's cache on modification and allow concurrent
274 switch (lh->mlh_reg_mode) {
276 lh->mlh_pdo_mode = LCK_EX;
279 lh->mlh_pdo_mode = LCK_CR;
282 lh->mlh_pdo_mode = LCK_CW;
285 CERROR("Not expected lock type (0x%x)\n",
286 (int)lh->mlh_reg_mode);
291 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
295 static int mdt_getstatus(struct mdt_thread_info *info)
297 struct mdt_device *mdt = info->mti_mdt;
298 struct md_device *next = mdt->mdt_child;
299 struct mdt_body *repbody;
304 rc = mdt_check_ucred(info);
306 RETURN(err_serious(rc));
308 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
309 RETURN(err_serious(-ENOMEM));
311 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
312 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
316 repbody->valid |= OBD_MD_FLID;
318 if (mdt->mdt_opts.mo_mds_capa &&
319 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
320 struct mdt_object *root;
321 struct lustre_capa *capa;
323 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
325 RETURN(PTR_ERR(root));
327 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
329 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
330 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
332 mdt_object_put(info->mti_env, root);
334 repbody->valid |= OBD_MD_FLMDSCAPA;
340 static int mdt_statfs(struct mdt_thread_info *info)
342 struct md_device *next = info->mti_mdt->mdt_child;
343 struct ptlrpc_service *svc;
344 struct obd_statfs *osfs;
349 svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
351 /* This will trigger a watchdog timeout */
352 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
353 (MDT_SERVICE_WATCHDOG_FACTOR *
354 at_get(&svc->srv_at_estimate)) + 1);
356 rc = mdt_check_ucred(info);
358 RETURN(err_serious(rc));
360 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
361 rc = err_serious(-ENOMEM);
363 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
364 rc = next->md_ops->mdo_statfs(info->mti_env, next,
366 statfs_pack(osfs, &info->mti_u.ksfs);
371 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
374 struct lu_attr *attr = &info->mti_attr.ma_attr;
376 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
378 /* Check if Size-on-MDS is enabled. */
379 if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
380 S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
381 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
382 b->size = attr->la_size;
383 b->blocks = attr->la_blocks;
387 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
388 const struct lu_attr *attr, const struct lu_fid *fid)
390 /*XXX should pack the reply body according to lu_valid*/
391 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
392 OBD_MD_FLGID | OBD_MD_FLTYPE |
393 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
394 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
396 if (!S_ISREG(attr->la_mode))
397 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
399 b->atime = attr->la_atime;
400 b->mtime = attr->la_mtime;
401 b->ctime = attr->la_ctime;
402 b->mode = attr->la_mode;
403 b->size = attr->la_size;
404 b->blocks = attr->la_blocks;
405 b->uid = attr->la_uid;
406 b->gid = attr->la_gid;
407 b->flags = attr->la_flags;
408 b->nlink = attr->la_nlink;
409 b->rdev = attr->la_rdev;
413 b->valid |= OBD_MD_FLID;
415 /* FIXME: these should be fixed when new igif ready.*/
416 b->ino = fid_oid(fid); /* 1.6 compatibility */
417 b->generation = fid_ver(fid); /* 1.6 compatibility */
418 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
420 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
421 PFID(fid), b->nlink, b->mode, b->size);
425 mdt_body_reverse_idmap(info, b);
428 static inline int mdt_body_has_lov(const struct lu_attr *la,
429 const struct mdt_body *body)
431 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
432 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
435 static int mdt_getattr_internal(struct mdt_thread_info *info,
436 struct mdt_object *o)
438 struct md_object *next = mdt_object_child(o);
439 const struct mdt_body *reqbody = info->mti_body;
440 struct ptlrpc_request *req = mdt_info_req(info);
441 struct md_attr *ma = &info->mti_attr;
442 struct lu_attr *la = &ma->ma_attr;
443 struct req_capsule *pill = info->mti_pill;
444 const struct lu_env *env = info->mti_env;
445 struct mdt_body *repbody;
446 struct lu_buf *buffer = &info->mti_buf;
450 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
451 RETURN(err_serious(-ENOMEM));
453 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
457 rc = mdt_object_exists(o);
459 /* This object is located on remote node.*/
460 repbody->fid1 = *mdt_object_fid(o);
461 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
465 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
466 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
468 /* If it is dir object and client require MEA, then we got MEA */
469 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
470 reqbody->valid & OBD_MD_MEA) {
471 /* Assumption: MDT_MD size is enough for lmv size. */
472 ma->ma_lmv = buffer->lb_buf;
473 ma->ma_lmv_size = buffer->lb_len;
474 ma->ma_need = MA_LMV | MA_INODE;
476 ma->ma_lmm = buffer->lb_buf;
477 ma->ma_lmm_size = buffer->lb_len;
478 ma->ma_need = MA_LOV | MA_INODE;
481 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
482 reqbody->valid & OBD_MD_FLDIREA &&
483 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
484 /* get default stripe info for this dir. */
485 ma->ma_need |= MA_LOV_DEF;
487 rc = mo_attr_get(env, next, ma);
489 CERROR("getattr error for "DFID": %d\n",
490 PFID(mdt_object_fid(o)), rc);
494 if (likely(ma->ma_valid & MA_INODE))
495 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
499 if (mdt_body_has_lov(la, reqbody)) {
500 if (ma->ma_valid & MA_LOV) {
501 LASSERT(ma->ma_lmm_size);
502 mdt_dump_lmm(D_INFO, ma->ma_lmm);
503 repbody->eadatasize = ma->ma_lmm_size;
504 if (S_ISDIR(la->la_mode))
505 repbody->valid |= OBD_MD_FLDIREA;
507 repbody->valid |= OBD_MD_FLEASIZE;
509 if (ma->ma_valid & MA_LMV) {
510 LASSERT(S_ISDIR(la->la_mode));
511 repbody->eadatasize = ma->ma_lmv_size;
512 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
514 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
515 repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
517 } else if (S_ISLNK(la->la_mode) &&
518 reqbody->valid & OBD_MD_LINKNAME) {
519 buffer->lb_buf = ma->ma_lmm;
520 buffer->lb_len = reqbody->eadatasize;
521 rc = mo_readlink(env, next, buffer);
522 if (unlikely(rc <= 0)) {
523 CERROR("readlink failed: %d\n", rc);
526 repbody->valid |= OBD_MD_LINKNAME;
527 repbody->eadatasize = rc;
529 ((char*)ma->ma_lmm)[rc - 1] = 0;
530 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
531 (char*)ma->ma_lmm, rc);
536 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
537 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
538 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
539 repbody->valid |= OBD_MD_FLMODEASIZE;
540 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
541 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
542 repbody->max_cookiesize);
545 if (exp_connect_rmtclient(info->mti_exp) &&
546 reqbody->valid & OBD_MD_FLRMTPERM) {
547 void *buf = req_capsule_server_get(pill, &RMF_ACL);
549 /* mdt_getattr_lock only */
550 rc = mdt_pack_remote_perm(info, o, buf);
552 repbody->valid &= ~OBD_MD_FLRMTPERM;
553 repbody->aclsize = 0;
556 repbody->valid |= OBD_MD_FLRMTPERM;
557 repbody->aclsize = sizeof(struct mdt_remote_perm);
560 #ifdef CONFIG_FS_POSIX_ACL
561 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
562 (reqbody->valid & OBD_MD_FLACL)) {
563 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
564 buffer->lb_len = req_capsule_get_size(pill,
565 &RMF_ACL, RCL_SERVER);
566 if (buffer->lb_len > 0) {
567 rc = mo_xattr_get(env, next, buffer,
568 XATTR_NAME_ACL_ACCESS);
570 if (rc == -ENODATA) {
571 repbody->aclsize = 0;
572 repbody->valid |= OBD_MD_FLACL;
574 } else if (rc == -EOPNOTSUPP) {
577 CERROR("got acl size: %d\n", rc);
580 repbody->aclsize = rc;
581 repbody->valid |= OBD_MD_FLACL;
588 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
589 info->mti_mdt->mdt_opts.mo_mds_capa &&
590 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
591 struct lustre_capa *capa;
593 capa = req_capsule_server_get(pill, &RMF_CAPA1);
595 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
596 rc = mo_capa_get(env, next, capa, 0);
599 repbody->valid |= OBD_MD_FLMDSCAPA;
604 static int mdt_renew_capa(struct mdt_thread_info *info)
606 struct mdt_object *obj = info->mti_object;
607 struct mdt_body *body;
608 struct lustre_capa *capa, *c;
612 /* if object doesn't exist, or server has disabled capability,
613 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
616 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
617 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
620 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
621 LASSERT(body != NULL);
623 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
626 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
630 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
632 body->valid |= OBD_MD_FLOSSCAPA;
636 static int mdt_getattr(struct mdt_thread_info *info)
638 struct mdt_object *obj = info->mti_object;
639 struct req_capsule *pill = info->mti_pill;
640 struct mdt_body *reqbody;
641 struct mdt_body *repbody;
647 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
650 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
651 rc = req_capsule_server_pack(pill);
653 RETURN(err_serious(rc));
654 rc = mdt_renew_capa(info);
655 GOTO(out_shrink, rc);
658 LASSERT(obj != NULL);
659 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
661 mode = lu_object_attr(&obj->mot_obj.mo_lu);
662 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
663 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
664 md_size = reqbody->eadatasize;
666 md_size = info->mti_mdt->mdt_max_mdsize;
668 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
670 rc = req_capsule_server_pack(pill);
671 if (unlikely(rc != 0))
672 RETURN(err_serious(rc));
674 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
675 LASSERT(repbody != NULL);
676 repbody->eadatasize = 0;
677 repbody->aclsize = 0;
679 if (reqbody->valid & OBD_MD_FLRMTPERM)
680 rc = mdt_init_ucred(info, reqbody);
682 rc = mdt_check_ucred(info);
684 GOTO(out_shrink, rc);
686 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
687 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
690 * Don't check capability at all, because rename might getattr for
691 * remote obj, and at that time no capability is available.
693 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
694 rc = mdt_getattr_internal(info, obj);
695 if (reqbody->valid & OBD_MD_FLRMTPERM)
696 mdt_exit_ucred(info);
699 mdt_shrink_reply(info);
703 static int mdt_is_subdir(struct mdt_thread_info *info)
705 struct mdt_object *o = info->mti_object;
706 struct req_capsule *pill = info->mti_pill;
707 const struct mdt_body *body = info->mti_body;
708 struct mdt_body *repbody;
714 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
717 * We save last checked parent fid to @repbody->fid1 for remote
720 LASSERT(fid_is_sane(&body->fid2));
721 LASSERT(mdt_object_exists(o) > 0);
722 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
723 &body->fid2, &repbody->fid1);
724 if (rc == 0 || rc == -EREMOTE)
725 repbody->valid |= OBD_MD_FLID;
730 static int mdt_raw_lookup(struct mdt_thread_info *info,
731 struct mdt_object *parent,
732 const struct lu_name *lname,
733 struct ldlm_reply *ldlm_rep)
735 struct md_object *next = mdt_object_child(info->mti_object);
736 const struct mdt_body *reqbody = info->mti_body;
737 struct lu_fid *child_fid = &info->mti_tmp_fid1;
738 struct mdt_body *repbody;
742 if (reqbody->valid != OBD_MD_FLID)
745 LASSERT(!info->mti_cross_ref);
747 /* Only got the fid of this obj by name */
748 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
751 /* XXX is raw_lookup possible as intent operation? */
754 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
757 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
759 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
762 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
763 repbody->fid1 = *child_fid;
764 repbody->valid = OBD_MD_FLID;
770 * UPDATE lock should be taken against parent, and be release before exit;
771 * child_bits lock should be taken against child, and be returned back:
772 * (1)normal request should release the child lock;
773 * (2)intent request will grant the lock to client.
775 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
776 struct mdt_lock_handle *lhc,
778 struct ldlm_reply *ldlm_rep)
780 struct ptlrpc_request *req = mdt_info_req(info);
781 struct mdt_body *reqbody = NULL;
782 struct mdt_object *parent = info->mti_object;
783 struct mdt_object *child;
784 struct md_object *next = mdt_object_child(parent);
785 struct lu_fid *child_fid = &info->mti_tmp_fid1;
786 struct lu_name *lname = NULL;
787 const char *name = NULL;
789 struct mdt_lock_handle *lhp;
790 struct ldlm_lock *lock;
791 struct ldlm_res_id *res_id;
797 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
798 LASSERT(ergo(is_resent,
799 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
801 LASSERT(parent != NULL);
802 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
804 RETURN(err_serious(-EFAULT));
806 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
808 if (!info->mti_cross_ref) {
810 * XXX: Check for "namelen == 0" is for getattr by fid
811 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
812 * that is the name must contain at least one character and
813 * the terminating '\0'
816 reqbody = req_capsule_client_get(info->mti_pill,
818 LASSERT(fid_is_sane(&reqbody->fid2));
821 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
823 PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
826 lname = mdt_name(info->mti_env, (char *)name, namelen);
827 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
828 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
832 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
834 rc = mdt_object_exists(parent);
835 if (unlikely(rc == 0)) {
836 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
837 &parent->mot_obj.mo_lu,
838 "Parent doesn't exist!\n");
840 } else if (!info->mti_cross_ref) {
841 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
842 PFID(mdt_object_fid(parent)));
845 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
853 if (info->mti_cross_ref) {
854 /* Only getattr on the child. Parent is on another node. */
855 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
857 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
858 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
861 /* Do not take lock for resent case. */
862 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
863 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
864 lhc->mlh_reg_lh.cookie);
865 LASSERT(fid_res_name_eq(mdt_object_fid(child),
866 &lock->l_resource->lr_name));
870 mdt_lock_handle_init(lhc);
871 mdt_lock_reg_init(lhc, LCK_PR);
874 * Object's name is on another MDS, no lookup lock is
875 * needed here but update is.
877 child_bits &= ~MDS_INODELOCK_LOOKUP;
878 child_bits |= MDS_INODELOCK_UPDATE;
880 rc = mdt_object_lock(info, child, lhc, child_bits,
884 /* Finally, we can get attr for child. */
885 mdt_set_capainfo(info, 0, mdt_object_fid(child),
887 rc = mdt_getattr_internal(info, child);
888 if (unlikely(rc != 0))
889 mdt_object_unlock(info, child, lhc, 1);
894 /* step 1: lock parent */
895 lhp = &info->mti_lh[MDT_LH_PARENT];
896 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
897 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
900 if (unlikely(rc != 0))
904 /* step 2: lookup child's fid by name */
905 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
910 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
911 GOTO(out_parent, rc);
913 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
915 *child_fid = reqbody->fid2;
916 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
920 *step 3: find the child object by fid & lock it.
921 * regardless if it is local or remote.
923 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
925 if (unlikely(IS_ERR(child)))
926 GOTO(out_parent, rc = PTR_ERR(child));
928 /* Do not take lock for resent case. */
929 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
930 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
931 lhc->mlh_reg_lh.cookie);
933 res_id = &lock->l_resource->lr_name;
934 if (!fid_res_name_eq(mdt_object_fid(child),
935 &lock->l_resource->lr_name)) {
936 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
937 &lock->l_resource->lr_name),
938 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
939 (unsigned long)res_id->name[0],
940 (unsigned long)res_id->name[1],
941 (unsigned long)res_id->name[2],
942 PFID(mdt_object_fid(parent)));
943 CWARN("Although resent, but still not get child lock"
944 "parent:"DFID" child:"DFID"\n",
945 PFID(mdt_object_fid(parent)),
946 PFID(mdt_object_fid(child)));
947 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
956 ma = &info->mti_attr;
958 mdt_lock_handle_init(lhc);
959 mdt_lock_reg_init(lhc, LCK_PR);
961 if (mdt_object_exists(child) == 0) {
962 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
963 &child->mot_obj.mo_lu,
964 "Object doesn't exist!\n");
965 GOTO(out_child, rc = -ESTALE);
969 ma->ma_need = MA_INODE;
970 rc = mo_attr_get(info->mti_env, next, ma);
971 if (unlikely(rc != 0))
974 /* If the file has not been changed for some time, we return
975 * not only a LOOKUP lock, but also an UPDATE lock and this
976 * might save us RPC on later STAT. For directories, it also
977 * let negative dentry starts working for this dir. */
978 if (ma->ma_valid & MA_INODE &&
979 ma->ma_attr.la_valid & LA_CTIME &&
980 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
981 ma->ma_attr.la_ctime < cfs_time_current_sec())
982 child_bits |= MDS_INODELOCK_UPDATE;
984 rc = mdt_object_lock(info, child, lhc, child_bits,
987 if (unlikely(rc != 0))
991 /* finally, we can get attr for child. */
992 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
993 rc = mdt_getattr_internal(info, child);
994 if (unlikely(rc != 0)) {
995 mdt_object_unlock(info, child, lhc, 1);
997 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
999 struct mdt_body *repbody;
1001 /* Debugging code. */
1002 res_id = &lock->l_resource->lr_name;
1003 LDLM_DEBUG(lock, "Returning lock to client\n");
1004 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1005 &lock->l_resource->lr_name),
1006 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1007 (unsigned long)res_id->name[0],
1008 (unsigned long)res_id->name[1],
1009 (unsigned long)res_id->name[2],
1010 PFID(mdt_object_fid(child)));
1012 * Pack Size-on-MDS inode attributes to the body if
1013 * update lock is given.
1015 repbody = req_capsule_server_get(info->mti_pill,
1017 if (lock->l_policy_data.l_inodebits.bits &
1018 MDS_INODELOCK_UPDATE)
1019 mdt_pack_size2body(info, child);
1020 LDLM_LOCK_PUT(lock);
1025 mdt_object_put(info->mti_env, child);
1027 mdt_object_unlock(info, parent, lhp, 1);
1031 /* normal handler: should release the child lock */
1032 static int mdt_getattr_name(struct mdt_thread_info *info)
1034 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1035 struct mdt_body *reqbody;
1036 struct mdt_body *repbody;
1040 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1041 LASSERT(reqbody != NULL);
1042 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1043 LASSERT(repbody != NULL);
1045 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1046 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1047 repbody->eadatasize = 0;
1048 repbody->aclsize = 0;
1050 rc = mdt_init_ucred(info, reqbody);
1052 GOTO(out_shrink, rc);
1054 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1055 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1056 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1057 lhc->mlh_reg_lh.cookie = 0;
1059 mdt_exit_ucred(info);
1062 mdt_shrink_reply(info);
1066 static const struct lu_device_operations mdt_lu_ops;
1068 static int lu_device_is_mdt(struct lu_device *d)
1070 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1073 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1074 void *karg, void *uarg);
1076 static int mdt_set_info(struct mdt_thread_info *info)
1078 struct ptlrpc_request *req = mdt_info_req(info);
1081 int keylen, vallen, rc = 0;
1084 rc = req_capsule_server_pack(info->mti_pill);
1088 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1090 DEBUG_REQ(D_HA, req, "no set_info key");
1094 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1097 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1099 DEBUG_REQ(D_HA, req, "no set_info val");
1103 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1106 if (KEY_IS(KEY_READ_ONLY)) {
1108 lustre_msg_set_status(req->rq_repmsg, 0);
1110 spin_lock(&req->rq_export->exp_lock);
1112 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1114 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1115 spin_unlock(&req->rq_export->exp_lock);
1117 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1118 struct changelog_setinfo *cs =
1119 (struct changelog_setinfo *)val;
1120 if (vallen != sizeof(*cs)) {
1121 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1124 if (lustre_msg_swabbed(req->rq_reqmsg)) {
1125 __swab64s(&cs->cs_recno);
1126 __swab32s(&cs->cs_id);
1129 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1131 lustre_msg_set_status(req->rq_repmsg, rc);
1139 static int mdt_connect(struct mdt_thread_info *info)
1142 struct ptlrpc_request *req;
1144 req = mdt_info_req(info);
1145 rc = target_handle_connect(req);
1147 LASSERT(req->rq_export != NULL);
1148 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1149 rc = mdt_init_sec_level(info);
1151 rc = mdt_init_idmap(info);
1153 obd_disconnect(class_export_get(req->rq_export));
1155 rc = err_serious(rc);
1160 static int mdt_disconnect(struct mdt_thread_info *info)
1165 rc = target_handle_disconnect(mdt_info_req(info));
1167 rc = err_serious(rc);
1171 static int mdt_sendpage(struct mdt_thread_info *info,
1172 struct lu_rdpg *rdpg)
1174 struct ptlrpc_request *req = mdt_info_req(info);
1175 struct ptlrpc_bulk_desc *desc;
1176 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1184 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1189 for (i = 0, tmpcount = rdpg->rp_count;
1190 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1191 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1192 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1195 LASSERT(desc->bd_nob == rdpg->rp_count);
1196 rc = sptlrpc_svc_wrap_bulk(req, desc);
1198 GOTO(free_desc, rc);
1200 rc = ptlrpc_start_bulk_transfer(desc);
1202 GOTO(free_desc, rc);
1204 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1205 GOTO(abort_bulk, rc = 0);
1207 timeout = (int) req->rq_deadline - cfs_time_current_sec();
1209 CERROR("Req deadline already passed %lu (now: %lu)\n",
1210 req->rq_deadline, cfs_time_current_sec());
1211 *lwi = LWI_TIMEOUT(cfs_time_seconds(max(timeout, 1)), NULL, NULL);
1212 rc = l_wait_event(desc->bd_waitq, !ptlrpc_server_bulk_active(desc), lwi);
1213 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1216 if (desc->bd_success &&
1217 desc->bd_nob_transferred == rdpg->rp_count)
1218 GOTO(free_desc, rc);
1220 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1223 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1224 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1225 desc->bd_nob_transferred, rdpg->rp_count,
1226 req->rq_export->exp_client_uuid.uuid,
1227 req->rq_export->exp_connection->c_remote_uuid.uuid);
1229 class_fail_export(req->rq_export);
1233 ptlrpc_abort_bulk(desc);
1235 ptlrpc_free_bulk(desc);
1239 #ifdef HAVE_SPLIT_SUPPORT
1241 * Retrieve dir entry from the page and insert it to the slave object, actually,
1242 * this should be in osd layer, but since it will not in the final product, so
1243 * just do it here and do not define more moo api anymore for this.
1245 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1248 struct mdt_object *object = info->mti_object;
1249 struct lu_fid *lf = &info->mti_tmp_fid2;
1250 struct md_attr *ma = &info->mti_attr;
1251 struct lu_dirpage *dp;
1252 struct lu_dirent *ent;
1253 int rc = 0, offset = 0;
1256 /* Make sure we have at least one entry. */
1261 * Disable trans for this name insert, since it will include many trans
1264 info->mti_no_need_trans = 1;
1266 * When write_dir_page, no need update parent's ctime,
1267 * and no permission check for name_insert.
1269 ma->ma_attr.la_ctime = 0;
1270 ma->ma_attr.la_valid = LA_MODE;
1271 ma->ma_valid = MA_INODE;
1274 dp = page_address(page);
1275 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1277 for (ent = lu_dirent_start(dp); ent != NULL;
1278 ent = lu_dirent_next(ent)) {
1279 struct lu_name *lname;
1282 if (le16_to_cpu(ent->lde_namelen) == 0)
1285 fid_le_to_cpu(lf, &ent->lde_fid);
1286 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1287 ma->ma_attr.la_mode = S_IFDIR;
1289 ma->ma_attr.la_mode = 0;
1290 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1292 GOTO(out, rc = -ENOMEM);
1294 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1295 lname = mdt_name(info->mti_env, name,
1296 le16_to_cpu(ent->lde_namelen));
1297 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1298 rc = mdo_name_insert(info->mti_env,
1299 md_object_next(&object->mot_obj),
1301 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1303 CERROR("Can't insert %*.*s, rc %d\n",
1304 le16_to_cpu(ent->lde_namelen),
1305 le16_to_cpu(ent->lde_namelen),
1310 offset += lu_dirent_size(ent);
1320 static int mdt_bulk_timeout(void *data)
1324 CERROR("mdt bulk transfer timeout \n");
1329 static int mdt_writepage(struct mdt_thread_info *info)
1331 struct ptlrpc_request *req = mdt_info_req(info);
1332 struct mdt_body *reqbody;
1333 struct l_wait_info *lwi;
1334 struct ptlrpc_bulk_desc *desc;
1340 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1341 if (reqbody == NULL)
1342 RETURN(err_serious(-EFAULT));
1344 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1346 RETURN(err_serious(-ENOMEM));
1348 /* allocate the page for the desc */
1349 page = cfs_alloc_page(CFS_ALLOC_STD);
1351 GOTO(desc_cleanup, rc = -ENOMEM);
1353 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1354 (int)reqbody->size, (int)reqbody->nlink);
1356 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1357 (int)reqbody->nlink);
1359 rc = sptlrpc_svc_prep_bulk(req, desc);
1361 GOTO(cleanup_page, rc);
1363 * Check if client was evicted while we were doing i/o before touching
1368 GOTO(cleanup_page, rc = -ENOMEM);
1370 if (desc->bd_export->exp_failed)
1373 rc = ptlrpc_start_bulk_transfer (desc);
1375 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1376 mdt_bulk_timeout, desc);
1377 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1378 desc->bd_export->exp_failed, lwi);
1379 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1380 if (rc == -ETIMEDOUT) {
1381 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1382 ptlrpc_abort_bulk(desc);
1383 } else if (desc->bd_export->exp_failed) {
1384 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1386 ptlrpc_abort_bulk(desc);
1387 } else if (!desc->bd_success ||
1388 desc->bd_nob_transferred != desc->bd_nob) {
1389 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1391 "truncated" : "network error on",
1392 desc->bd_nob_transferred, desc->bd_nob);
1393 /* XXX should this be a different errno? */
1397 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1400 GOTO(cleanup_lwi, rc);
1401 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1406 cfs_free_page(page);
1408 ptlrpc_free_bulk(desc);
1413 static int mdt_readpage(struct mdt_thread_info *info)
1415 struct mdt_object *object = info->mti_object;
1416 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1417 struct mdt_body *reqbody;
1418 struct mdt_body *repbody;
1423 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1424 RETURN(err_serious(-ENOMEM));
1426 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1427 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1428 if (reqbody == NULL || repbody == NULL)
1429 RETURN(err_serious(-EFAULT));
1432 * prepare @rdpg before calling lower layers and transfer itself. Here
1433 * reqbody->size contains offset of where to start to read and
1434 * reqbody->nlink contains number bytes to read.
1436 rdpg->rp_hash = reqbody->size;
1437 if (rdpg->rp_hash != reqbody->size) {
1438 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1439 rdpg->rp_hash, reqbody->size);
1443 rdpg->rp_attrs = reqbody->mode;
1444 rdpg->rp_count = reqbody->nlink;
1445 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1446 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1447 if (rdpg->rp_pages == NULL)
1450 for (i = 0; i < rdpg->rp_npages; ++i) {
1451 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1452 if (rdpg->rp_pages[i] == NULL)
1453 GOTO(free_rdpg, rc = -ENOMEM);
1456 /* call lower layers to fill allocated pages with directory data */
1457 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1459 GOTO(free_rdpg, rc);
1461 /* send pages to client */
1462 rc = mdt_sendpage(info, rdpg);
1467 for (i = 0; i < rdpg->rp_npages; i++)
1468 if (rdpg->rp_pages[i] != NULL)
1469 cfs_free_page(rdpg->rp_pages[i]);
1470 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1472 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1478 static int mdt_reint_internal(struct mdt_thread_info *info,
1479 struct mdt_lock_handle *lhc,
1482 struct req_capsule *pill = info->mti_pill;
1483 struct mdt_device *mdt = info->mti_mdt;
1484 struct mdt_body *repbody;
1489 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1490 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1491 mdt->mdt_max_mdsize);
1492 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1493 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1494 mdt->mdt_max_cookiesize);
1496 rc = req_capsule_server_pack(pill);
1498 CERROR("Can't pack response, rc %d\n", rc);
1499 RETURN(err_serious(rc));
1502 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1503 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1505 repbody->eadatasize = 0;
1506 repbody->aclsize = 0;
1509 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1510 GOTO(out_shrink, rc = err_serious(-EFAULT));
1512 rc = mdt_reint_unpack(info, op);
1514 CERROR("Can't unpack reint, rc %d\n", rc);
1515 GOTO(out_shrink, rc = err_serious(rc));
1518 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1520 /* for replay no cookkie / lmm need, because client have this already */
1521 if (info->mti_spec.no_create == 1) {
1522 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1523 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1525 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1526 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1530 rc = mdt_init_ucred_reint(info);
1532 GOTO(out_shrink, rc);
1534 rc = mdt_fix_attr_ucred(info, op);
1536 GOTO(out_ucred, rc = err_serious(rc));
1538 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1539 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1540 GOTO(out_ucred, rc);
1542 rc = mdt_reint_rec(info, lhc);
1545 mdt_exit_ucred(info);
1547 mdt_shrink_reply(info);
1551 static long mdt_reint_opcode(struct mdt_thread_info *info,
1552 const struct req_format **fmt)
1554 struct mdt_rec_reint *rec;
1557 opc = err_serious(-EFAULT);
1558 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1560 opc = rec->rr_opcode;
1561 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1562 if (opc < REINT_MAX && fmt[opc] != NULL)
1563 req_capsule_extend(info->mti_pill, fmt[opc]);
1565 CERROR("Unsupported opc: %ld\n", opc);
1566 opc = err_serious(opc);
1572 static int mdt_reint(struct mdt_thread_info *info)
1577 static const struct req_format *reint_fmts[REINT_MAX] = {
1578 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1579 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1580 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1581 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1582 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1583 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1584 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1589 opc = mdt_reint_opcode(info, reint_fmts);
1592 * No lock possible here from client to pass it to reint code
1595 rc = mdt_reint_internal(info, NULL, opc);
1600 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1604 /* this should sync the whole device */
1605 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1607 struct dt_device *dt = mdt->mdt_bottom;
1611 rc = dt->dd_ops->dt_sync(env, dt);
1615 /* this should sync this object */
1616 static int mdt_object_sync(struct mdt_thread_info *info)
1618 struct md_object *next;
1622 if (!mdt_object_exists(info->mti_object)) {
1623 CWARN("Non existing object "DFID"!\n",
1624 PFID(mdt_object_fid(info->mti_object)));
1627 next = mdt_object_child(info->mti_object);
1628 rc = mo_object_sync(info->mti_env, next);
1633 static int mdt_sync(struct mdt_thread_info *info)
1635 struct req_capsule *pill = info->mti_pill;
1636 struct mdt_body *body;
1640 /* The fid may be zero, so we req_capsule_set manually */
1641 req_capsule_set(pill, &RQF_MDS_SYNC);
1643 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1645 RETURN(err_serious(-EINVAL));
1647 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1648 RETURN(err_serious(-ENOMEM));
1650 if (fid_seq(&body->fid1) == 0) {
1651 /* sync the whole device */
1652 rc = req_capsule_server_pack(pill);
1654 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1656 rc = err_serious(rc);
1658 /* sync an object */
1659 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1661 rc = mdt_object_sync(info);
1663 struct md_object *next;
1664 const struct lu_fid *fid;
1665 struct lu_attr *la = &info->mti_attr.ma_attr;
1667 next = mdt_object_child(info->mti_object);
1668 info->mti_attr.ma_need = MA_INODE;
1669 info->mti_attr.ma_valid = 0;
1670 rc = mo_attr_get(info->mti_env, next,
1673 body = req_capsule_server_get(pill,
1675 fid = mdt_object_fid(info->mti_object);
1676 mdt_pack_attr2body(info, body, la, fid);
1680 rc = err_serious(rc);
1685 #ifdef HAVE_QUOTA_SUPPORT
1686 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1688 struct obd_quotactl *oqctl;
1689 struct req_capsule *pill = info->mti_pill;
1690 struct obd_export *exp = info->mti_exp;
1691 struct md_device *next = info->mti_mdt->mdt_child;
1695 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACHECK_NET))
1698 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1702 /* remote client has no permission for quotacheck */
1703 if (unlikely(exp_connect_rmtclient(exp)))
1706 rc = req_capsule_server_pack(pill);
1710 rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next, exp,
1715 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1717 struct obd_quotactl *oqctl, *repoqc;
1718 struct req_capsule *pill = info->mti_pill;
1719 struct obd_export *exp = info->mti_exp;
1720 struct md_device *next = info->mti_mdt->mdt_child;
1721 const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1725 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACTL_NET))
1728 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1733 if (exp_connect_rmtclient(exp)) {
1734 struct ptlrpc_request *req = mdt_info_req(info);
1735 struct mdt_export_data *med = mdt_req2med(req);
1736 struct lustre_idmap_table *idmap = med->med_idmap;
1738 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1739 oqctl->qc_cmd != Q_GETINFO))
1743 if (oqctl->qc_type == USRQUOTA)
1744 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1746 else if (oqctl->qc_type == GRPQUOTA)
1747 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1752 if (id == CFS_IDMAP_NOTFOUND) {
1753 CDEBUG(D_QUOTA, "no mapping for id %u\n",
1759 rc = req_capsule_server_pack(pill);
1763 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1764 LASSERT(repoqc != NULL);
1766 switch (oqctl->qc_cmd) {
1768 if (info->mti_mdt->mdt_som_conf) {
1769 /* Quota cannot be used together with SOM while
1770 * SOM stored blocks in i_blocks but not in SOM EA. */
1771 LCONSOLE_ERROR("Fail to turn Quota on: SOM is enabled "
1772 "and temporary conflicts with quota.\n");
1775 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1778 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1781 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1785 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1789 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1793 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1797 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1801 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1804 case LUSTRE_Q_INVALIDATE:
1805 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1807 case LUSTRE_Q_FINVALIDATE:
1808 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1811 CERROR("unsupported mdt_quotactl command: %d\n",
1823 * OBD PING and other handlers.
1825 static int mdt_obd_ping(struct mdt_thread_info *info)
1830 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1832 rc = target_handle_ping(mdt_info_req(info));
1834 rc = err_serious(rc);
1838 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1840 return err_serious(-EOPNOTSUPP);
1843 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1845 return err_serious(-EOPNOTSUPP);
1853 /** clone llog ctxt from child (mdd)
1854 * This allows remote llog (replicator) access.
1855 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1856 * context was originally set up, or we can handle them directly.
1857 * I choose the latter, but that means I need any llog
1858 * contexts set up by child to be accessable by the mdt. So we clone the
1859 * context into our context list here.
1861 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1864 struct md_device *next = mdt->mdt_child;
1865 struct llog_ctxt *ctxt;
1868 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1871 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1872 if (rc || ctxt == NULL) {
1873 CERROR("Can't get mdd ctxt %d\n", rc);
1877 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1879 CERROR("Can't set mdt ctxt %d\n", rc);
1884 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1885 struct mdt_device *mdt, int idx)
1887 struct llog_ctxt *ctxt;
1889 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1892 /* Put once for the get we just did, and once for the clone */
1893 llog_ctxt_put(ctxt);
1894 llog_ctxt_put(ctxt);
1898 static int mdt_llog_create(struct mdt_thread_info *info)
1902 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1903 rc = llog_origin_handle_create(mdt_info_req(info));
1904 return (rc < 0 ? err_serious(rc) : rc);
1907 static int mdt_llog_destroy(struct mdt_thread_info *info)
1911 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1912 rc = llog_origin_handle_destroy(mdt_info_req(info));
1913 return (rc < 0 ? err_serious(rc) : rc);
1916 static int mdt_llog_read_header(struct mdt_thread_info *info)
1920 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1921 rc = llog_origin_handle_read_header(mdt_info_req(info));
1922 return (rc < 0 ? err_serious(rc) : rc);
1925 static int mdt_llog_next_block(struct mdt_thread_info *info)
1929 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1930 rc = llog_origin_handle_next_block(mdt_info_req(info));
1931 return (rc < 0 ? err_serious(rc) : rc);
1934 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1938 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1939 rc = llog_origin_handle_prev_block(mdt_info_req(info));
1940 return (rc < 0 ? err_serious(rc) : rc);
1947 static struct ldlm_callback_suite cbs = {
1948 .lcs_completion = ldlm_server_completion_ast,
1949 .lcs_blocking = ldlm_server_blocking_ast,
1953 static int mdt_enqueue(struct mdt_thread_info *info)
1955 struct ptlrpc_request *req;
1960 * info->mti_dlm_req already contains swapped and (if necessary)
1961 * converted dlm request.
1963 LASSERT(info->mti_dlm_req != NULL);
1965 req = mdt_info_req(info);
1968 * Lock without inodebits makes no sense and will oops later in
1969 * ldlm. Let's check it now to see if we have wrong lock from client or
1970 * bits get corrupted somewhere in mdt_intent_policy().
1972 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1973 /* This is disabled because we need to support liblustre flock.
1974 * LASSERT(req_bits != 0);
1977 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1978 req, info->mti_dlm_req, &cbs);
1979 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1980 return rc ? err_serious(rc) : req->rq_status;
1983 static int mdt_convert(struct mdt_thread_info *info)
1986 struct ptlrpc_request *req;
1988 LASSERT(info->mti_dlm_req);
1989 req = mdt_info_req(info);
1990 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1991 return rc ? err_serious(rc) : req->rq_status;
1994 static int mdt_bl_callback(struct mdt_thread_info *info)
1996 CERROR("bl callbacks should not happen on MDS\n");
1998 return err_serious(-EOPNOTSUPP);
2001 static int mdt_cp_callback(struct mdt_thread_info *info)
2003 CERROR("cp callbacks should not happen on MDS\n");
2005 return err_serious(-EOPNOTSUPP);
2009 * sec context handlers
2011 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2015 rc = mdt_handle_idmap(info);
2018 struct ptlrpc_request *req = mdt_info_req(info);
2021 opc = lustre_msg_get_opc(req->rq_reqmsg);
2022 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2023 sptlrpc_svc_ctx_invalidate(req);
2026 OBD_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, obd_fail_val);
2031 static struct mdt_object *mdt_obj(struct lu_object *o)
2033 LASSERT(lu_device_is_mdt(o->lo_dev));
2034 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2037 struct mdt_object *mdt_object_find(const struct lu_env *env,
2038 struct mdt_device *d,
2039 const struct lu_fid *f)
2041 struct lu_object *o;
2042 struct mdt_object *m;
2045 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2046 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2047 if (unlikely(IS_ERR(o)))
2048 m = (struct mdt_object *)o;
2055 * Asyncronous commit for mdt device.
2057 * Pass asynchonous commit call down the MDS stack.
2059 * \param env environment
2060 * \param mdt the mdt device
2062 static void mdt_device_commit_async(const struct lu_env *env,
2063 struct mdt_device *mdt)
2065 struct dt_device *dt = mdt->mdt_bottom;
2068 rc = dt->dd_ops->dt_commit_async(env, dt);
2069 if (unlikely(rc != 0))
2070 CWARN("async commit start failed with rc = %d", rc);
2074 * Mark the lock as "synchonous".
2076 * Mark the lock to deffer transaction commit to the unlock time.
2078 * \param lock the lock to mark as "synchonous"
2080 * \see mdt_is_lock_sync
2081 * \see mdt_save_lock
2083 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2085 lock->l_ast_data = (void*)1;
2089 * Check whehter the lock "synchonous" or not.
2091 * \param lock the lock to check
2092 * \retval 1 the lock is "synchonous"
2093 * \retval 0 the lock isn't "synchronous"
2095 * \see mdt_set_lock_sync
2096 * \see mdt_save_lock
2098 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2100 return lock->l_ast_data != NULL;
2104 * Blocking AST for mdt locks.
2106 * Starts transaction commit if in case of COS lock conflict or
2107 * deffers such a commit to the mdt_save_lock.
2109 * \param lock the lock which blocks a request or cancelling lock
2110 * \param desc unused
2111 * \param data unused
2112 * \param flag indicates whether this cancelling or blocking callback
2114 * \see ldlm_blocking_ast_nocheck
2116 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2117 void *data, int flag)
2119 struct obd_device *obd = lock->l_resource->lr_namespace->ns_obd;
2120 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2124 if (flag == LDLM_CB_CANCELING)
2126 lock_res_and_lock(lock);
2127 if (lock->l_blocking_ast != mdt_blocking_ast) {
2128 unlock_res_and_lock(lock);
2131 if (mdt_cos_is_enabled(mdt) &&
2132 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2133 lock->l_blocking_lock != NULL &&
2134 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2135 mdt_set_lock_sync(lock);
2137 rc = ldlm_blocking_ast_nocheck(lock);
2139 /* There is no lock conflict if l_blocking_lock == NULL,
2140 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2141 * when the last reference to a local lock was released */
2142 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2145 rc = lu_env_init(&env, LCT_MD_THREAD);
2146 if (unlikely(rc != 0))
2147 CWARN("lu_env initialization failed with rc = %d,"
2148 "cannot start asynchronous commit\n", rc);
2150 mdt_device_commit_async(&env, mdt);
2156 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2157 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2159 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2160 ldlm_policy_data_t *policy = &info->mti_policy;
2161 struct ldlm_res_id *res_id = &info->mti_res_id;
2165 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2166 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2167 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2168 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2170 if (mdt_object_exists(o) < 0) {
2171 if (locality == MDT_CROSS_LOCK) {
2172 /* cross-ref object fix */
2173 ibits &= ~MDS_INODELOCK_UPDATE;
2174 ibits |= MDS_INODELOCK_LOOKUP;
2176 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2177 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2179 /* No PDO lock on remote object */
2180 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2183 if (lh->mlh_type == MDT_PDO_LOCK) {
2184 /* check for exists after object is locked */
2185 if (mdt_object_exists(o) == 0) {
2186 /* Non-existent object shouldn't have PDO lock */
2189 /* Non-dir object shouldn't have PDO lock */
2190 LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
2194 memset(policy, 0, sizeof(*policy));
2195 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2198 * Take PDO lock on whole directory and build correct @res_id for lock
2199 * on part of directory.
2201 if (lh->mlh_pdo_hash != 0) {
2202 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2203 mdt_lock_pdo_mode(info, o, lh);
2204 if (lh->mlh_pdo_mode != LCK_NL) {
2206 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2207 * is never going to be sent to client and we do not
2208 * want it slowed down due to possible cancels.
2210 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2211 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2212 policy, res_id, LDLM_FL_ATOMIC_CB,
2213 &info->mti_exp->exp_handle.h_cookie);
2219 * Finish res_id initializing by name hash marking part of
2220 * directory which is taking modification.
2222 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2225 policy->l_inodebits.bits = ibits;
2228 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2229 * going to be sent to client. If it is - mdt_intent_policy() path will
2230 * fix it up and turn FL_LOCAL flag off.
2232 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2233 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2234 &info->mti_exp->exp_handle.h_cookie);
2240 mdt_object_unlock(info, o, lh, 1);
2247 * Save a lock within request object.
2249 * Keep the lock referenced until whether client ACK or transaction
2250 * commit happens or release the lock immediately depending on input
2251 * parameters. If COS is ON, a write lock is converted to COS lock
2254 * \param info thead info object
2255 * \param h lock handle
2256 * \param mode lock mode
2257 * \param decref force immediate lock releasing
2260 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2261 ldlm_mode_t mode, int decref)
2265 if (lustre_handle_is_used(h)) {
2266 if (decref || !info->mti_has_trans ||
2267 !(mode & (LCK_PW | LCK_EX))){
2268 mdt_fid_unlock(h, mode);
2270 struct mdt_device *mdt = info->mti_mdt;
2271 struct ldlm_lock *lock = ldlm_handle2lock(h);
2272 struct ptlrpc_request *req = mdt_info_req(info);
2275 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2277 CDEBUG(D_HA, "request = %p reply state = %p"
2278 " transno = "LPD64"\n",
2279 req, req->rq_reply_state, req->rq_transno);
2280 if (mdt_cos_is_enabled(mdt)) {
2282 ldlm_lock_downgrade(lock, LCK_COS);
2285 ptlrpc_save_lock(req, h, mode, no_ack);
2286 if (mdt_is_lock_sync(lock)) {
2287 CDEBUG(D_HA, "found sync-lock,"
2288 " async commit started\n");
2289 mdt_device_commit_async(info->mti_env,
2292 LDLM_LOCK_PUT(lock);
2301 * Unlock mdt object.
2303 * Immeditely release the regular lock and the PDO lock or save the
2304 * lock in reqeuest and keep them referenced until client ACK or
2305 * transaction commit.
2307 * \param info thread info object
2308 * \param o mdt object
2309 * \param h mdt lock handle referencing regular and PDO locks
2310 * \param decref force immediate lock releasing
2312 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2313 struct mdt_lock_handle *lh, int decref)
2317 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2318 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2323 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2324 const struct lu_fid *f,
2325 struct mdt_lock_handle *lh,
2328 struct mdt_object *o;
2330 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2334 rc = mdt_object_lock(info, o, lh, ibits,
2337 mdt_object_put(info->mti_env, o);
2344 void mdt_object_unlock_put(struct mdt_thread_info * info,
2345 struct mdt_object * o,
2346 struct mdt_lock_handle *lh,
2349 mdt_object_unlock(info, o, lh, decref);
2350 mdt_object_put(info->mti_env, o);
2353 static struct mdt_handler *mdt_handler_find(__u32 opc,
2354 struct mdt_opc_slice *supported)
2356 struct mdt_opc_slice *s;
2357 struct mdt_handler *h;
2360 for (s = supported; s->mos_hs != NULL; s++) {
2361 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2362 h = s->mos_hs + (opc - s->mos_opc_start);
2363 if (likely(h->mh_opc != 0))
2364 LASSERTF(h->mh_opc == opc,
2365 "opcode mismatch %d != %d\n",
2368 h = NULL; /* unsupported opc */
2375 static int mdt_lock_resname_compat(struct mdt_device *m,
2376 struct ldlm_request *req)
2378 /* XXX something... later. */
2382 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2384 /* XXX something... later. */
2389 * Generic code handling requests that have struct mdt_body passed in:
2391 * - extract mdt_body from request and save it in @info, if present;
2393 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2396 * - if HABEO_CORPUS flag is set for this request type check whether object
2397 * actually exists on storage (lu_object_exists()).
2400 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2402 const struct mdt_body *body;
2403 struct mdt_object *obj;
2404 const struct lu_env *env;
2405 struct req_capsule *pill;
2409 env = info->mti_env;
2410 pill = info->mti_pill;
2412 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2416 if (!(body->valid & OBD_MD_FLID))
2419 if (!fid_is_sane(&body->fid1)) {
2420 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2425 * Do not get size or any capa fields before we check that request
2426 * contains capa actually. There are some requests which do not, for
2427 * instance MDS_IS_SUBDIR.
2429 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2430 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2431 mdt_set_capainfo(info, 0, &body->fid1,
2432 req_capsule_client_get(pill, &RMF_CAPA1));
2434 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2436 if ((flags & HABEO_CORPUS) &&
2437 !mdt_object_exists(obj)) {
2438 mdt_object_put(env, obj);
2439 /* for capability renew ENOENT will be handled in
2441 if (body->valid & OBD_MD_FLOSSCAPA)
2446 info->mti_object = obj;
2455 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2457 struct req_capsule *pill = info->mti_pill;
2461 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2462 rc = mdt_body_unpack(info, flags);
2466 if (rc == 0 && (flags & HABEO_REFERO)) {
2467 struct mdt_device *mdt = info->mti_mdt;
2471 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2472 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2473 mdt->mdt_max_mdsize);
2474 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2475 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2476 mdt->mdt_max_cookiesize);
2478 rc = req_capsule_server_pack(pill);
2483 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2485 struct md_device *next = m->mdt_child;
2487 return next->md_ops->mdo_init_capa_ctxt(env, next,
2488 m->mdt_opts.mo_mds_capa,
2489 m->mdt_capa_timeout,
2495 * Invoke handler for this request opc. Also do necessary preprocessing
2496 * (according to handler ->mh_flags), and post-processing (setting of
2497 * ->last_{xid,committed}).
2499 static int mdt_req_handle(struct mdt_thread_info *info,
2500 struct mdt_handler *h, struct ptlrpc_request *req)
2502 int rc, serious = 0;
2507 LASSERT(h->mh_act != NULL);
2508 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2509 LASSERT(current->journal_info == NULL);
2512 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2513 * to put same checks into handlers like mdt_close(), mdt_reint(),
2514 * etc., without talking to mdt authors first. Checking same thing
2515 * there again is useless and returning 0 error wihtout packing reply
2516 * is buggy! Handlers either pack reply or return error.
2518 * We return 0 here and do not send any reply in order to emulate
2519 * network failure. Do not send any reply in case any of NET related
2520 * fail_id has occured.
2522 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2526 flags = h->mh_flags;
2527 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2529 if (h->mh_fmt != NULL) {
2530 req_capsule_set(info->mti_pill, h->mh_fmt);
2531 rc = mdt_unpack_req_pack_rep(info, flags);
2534 if (rc == 0 && flags & MUTABOR &&
2535 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2536 /* should it be rq_status? */
2539 if (rc == 0 && flags & HABEO_CLAVIS) {
2540 struct ldlm_request *dlm_req;
2542 LASSERT(h->mh_fmt != NULL);
2544 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2545 if (dlm_req != NULL) {
2546 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2547 rc = mdt_lock_resname_compat(info->mti_mdt,
2549 info->mti_dlm_req = dlm_req;
2555 /* capability setting changed via /proc, needs reinitialize ctxt */
2556 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2557 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2558 info->mti_mdt->mdt_capa_conf = 0;
2561 if (likely(rc == 0)) {
2563 * Process request, there can be two types of rc:
2564 * 1) errors with msg unpack/pack, other failures outside the
2565 * operation itself. This is counted as serious errors;
2566 * 2) errors during fs operation, should be placed in rq_status
2569 rc = h->mh_act(info);
2571 !req->rq_no_reply && req->rq_reply_state == NULL) {
2572 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2573 "pack reply and returned 0 error\n",
2577 serious = is_serious(rc);
2578 rc = clear_serious(rc);
2582 req->rq_status = rc;
2585 * ELDLM_* codes which > 0 should be in rq_status only as well as
2586 * all non-serious errors.
2588 if (rc > 0 || !serious)
2591 LASSERT(current->journal_info == NULL);
2593 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2594 info->mti_mdt->mdt_opts.mo_compat_resname) {
2595 struct ldlm_reply *dlmrep;
2597 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2599 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2602 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2603 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2604 target_committed_to_req(req);
2606 if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2607 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2608 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2612 target_send_reply(req, rc, info->mti_fail_id);
2616 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2618 lh->mlh_type = MDT_NUL_LOCK;
2619 lh->mlh_reg_lh.cookie = 0ull;
2620 lh->mlh_reg_mode = LCK_MINMODE;
2621 lh->mlh_pdo_lh.cookie = 0ull;
2622 lh->mlh_pdo_mode = LCK_MINMODE;
2625 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2627 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2628 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2632 * Initialize fields of struct mdt_thread_info. Other fields are left in
2633 * uninitialized state, because it's too expensive to zero out whole
2634 * mdt_thread_info (> 1K) on each request arrival.
2636 static void mdt_thread_info_init(struct ptlrpc_request *req,
2637 struct mdt_thread_info *info)
2640 struct md_capainfo *ci;
2642 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2643 info->mti_pill = &req->rq_pill;
2646 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2647 mdt_lock_handle_init(&info->mti_lh[i]);
2649 /* mdt device: it can be NULL while CONNECT */
2650 if (req->rq_export) {
2651 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2652 info->mti_exp = req->rq_export;
2654 info->mti_mdt = NULL;
2655 info->mti_env = req->rq_svc_thread->t_env;
2656 ci = md_capainfo(info->mti_env);
2657 memset(ci, 0, sizeof *ci);
2658 if (req->rq_export) {
2659 if (exp_connect_rmtclient(req->rq_export))
2660 ci->mc_auth = LC_ID_CONVERT;
2661 else if (req->rq_export->exp_connect_flags &
2662 OBD_CONNECT_MDS_CAPA)
2663 ci->mc_auth = LC_ID_PLAIN;
2665 ci->mc_auth = LC_ID_NONE;
2668 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2669 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2670 info->mti_mos[0] = NULL;
2671 info->mti_mos[1] = NULL;
2672 info->mti_mos[2] = NULL;
2673 info->mti_mos[3] = NULL;
2675 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2676 info->mti_body = NULL;
2677 info->mti_object = NULL;
2678 info->mti_dlm_req = NULL;
2679 info->mti_has_trans = 0;
2680 info->mti_no_need_trans = 0;
2681 info->mti_cross_ref = 0;
2682 info->mti_opdata = 0;
2684 /* To not check for split by default. */
2685 info->mti_spec.sp_ck_split = 0;
2686 info->mti_spec.no_create = 0;
2689 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2693 req_capsule_fini(info->mti_pill);
2694 if (info->mti_object != NULL) {
2696 * freeing an object may lead to OSD level transaction, do not
2697 * let it mess with MDT. bz19385.
2699 info->mti_no_need_trans = 1;
2700 mdt_object_put(info->mti_env, info->mti_object);
2701 info->mti_object = NULL;
2703 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2704 mdt_lock_handle_fini(&info->mti_lh[i]);
2705 info->mti_env = NULL;
2708 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2709 struct obd_device *obd, int *process)
2711 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2712 case MDS_CONNECT: /* This will never get here, but for completeness. */
2713 case OST_CONNECT: /* This will never get here, but for completeness. */
2714 case MDS_DISCONNECT:
2715 case OST_DISCONNECT:
2720 case MDS_DONE_WRITING:
2721 case MDS_SYNC: /* used in unmounting */
2727 *process = target_queue_recovery_request(req, obd);
2731 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2738 * Handle recovery. Return:
2739 * +1: continue request processing;
2740 * -ve: abort immediately with the given error code;
2741 * 0: send reply with error code in req->rq_status;
2743 static int mdt_recovery(struct mdt_thread_info *info)
2745 struct ptlrpc_request *req = mdt_info_req(info);
2747 struct obd_device *obd;
2751 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2754 case SEC_CTX_INIT_CONT:
2760 rc = mdt_handle_idmap(info);
2769 if (unlikely(req->rq_export == NULL)) {
2770 CERROR("operation %d on unconnected MDS from %s\n",
2771 lustre_msg_get_opc(req->rq_reqmsg),
2772 libcfs_id2str(req->rq_peer));
2773 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2774 * mds_A will get -ENOTCONN(especially for ping req),
2775 * which will cause that mds_A deactive timeout, then when
2776 * mds_A cleanup, the cleanup process will be suspended since
2777 * deactive timeout is not zero.
2779 req->rq_status = -ENOTCONN;
2780 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2784 /* sanity check: if the xid matches, the request must be marked as a
2785 * resent or replayed */
2786 if (req_xid_is_last(req)) {
2787 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2788 (MSG_RESENT | MSG_REPLAY))) {
2789 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2790 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2791 lustre_msg_get_flags(req->rq_reqmsg));
2793 req->rq_status = -ENOTCONN;
2798 /* else: note the opposite is not always true; a RESENT req after a
2799 * failover will usually not match the last_xid, since it was likely
2800 * never committed. A REPLAYed request will almost never match the
2801 * last xid, however it could for a committed, but still retained,
2804 obd = req->rq_export->exp_obd;
2806 /* Check for aborted recovery... */
2807 spin_lock_bh(&obd->obd_processing_task_lock);
2808 recovering = obd->obd_recovering;
2809 spin_unlock_bh(&obd->obd_processing_task_lock);
2810 if (unlikely(recovering)) {
2813 DEBUG_REQ(D_INFO, req, "Got new replay");
2814 rc = mdt_filter_recovery_request(req, obd, &should_process);
2815 if (rc != 0 || !should_process)
2817 else if (should_process < 0) {
2818 req->rq_status = should_process;
2819 rc = ptlrpc_error(req);
2826 static int mdt_msg_check_version(struct lustre_msg *msg)
2830 switch (lustre_msg_get_opc(msg)) {
2832 case MDS_DISCONNECT:
2835 case SEC_CTX_INIT_CONT:
2837 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2839 CERROR("bad opc %u version %08x, expecting %08x\n",
2840 lustre_msg_get_opc(msg),
2841 lustre_msg_get_version(msg),
2842 LUSTRE_OBD_VERSION);
2846 case MDS_GETATTR_NAME:
2853 case MDS_DONE_WRITING:
2860 case MDS_QUOTACHECK:
2866 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2868 CERROR("bad opc %u version %08x, expecting %08x\n",
2869 lustre_msg_get_opc(msg),
2870 lustre_msg_get_version(msg),
2871 LUSTRE_MDS_VERSION);
2875 case LDLM_BL_CALLBACK:
2876 case LDLM_CP_CALLBACK:
2877 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2879 CERROR("bad opc %u version %08x, expecting %08x\n",
2880 lustre_msg_get_opc(msg),
2881 lustre_msg_get_version(msg),
2882 LUSTRE_DLM_VERSION);
2884 case OBD_LOG_CANCEL:
2885 case LLOG_ORIGIN_HANDLE_CREATE:
2886 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2887 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2888 case LLOG_ORIGIN_HANDLE_CLOSE:
2889 case LLOG_ORIGIN_HANDLE_DESTROY:
2890 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2892 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2894 CERROR("bad opc %u version %08x, expecting %08x\n",
2895 lustre_msg_get_opc(msg),
2896 lustre_msg_get_version(msg),
2897 LUSTRE_LOG_VERSION);
2900 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2906 static int mdt_handle0(struct ptlrpc_request *req,
2907 struct mdt_thread_info *info,
2908 struct mdt_opc_slice *supported)
2910 struct mdt_handler *h;
2911 struct lustre_msg *msg;
2916 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2919 LASSERT(current->journal_info == NULL);
2921 msg = req->rq_reqmsg;
2922 rc = mdt_msg_check_version(msg);
2923 if (likely(rc == 0)) {
2924 rc = mdt_recovery(info);
2925 if (likely(rc == +1)) {
2926 switch (lustre_msg_get_opc(msg)) {
2928 req->rq_bulk_read = 1;
2931 req->rq_bulk_write = 1;
2935 h = mdt_handler_find(lustre_msg_get_opc(msg),
2937 if (likely(h != NULL)) {
2938 rc = mdt_req_handle(info, h, req);
2940 CERROR("The unsupported opc: 0x%x\n",
2941 lustre_msg_get_opc(msg) );
2942 req->rq_status = -ENOTSUPP;
2943 rc = ptlrpc_error(req);
2948 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2953 * MDT handler function called by ptlrpc service thread when request comes.
2955 * XXX common "target" functionality should be factored into separate module
2956 * shared by mdt, ost and stand-alone services like fld.
2958 static int mdt_handle_common(struct ptlrpc_request *req,
2959 struct mdt_opc_slice *supported)
2962 struct mdt_thread_info *info;
2966 env = req->rq_svc_thread->t_env;
2967 LASSERT(env != NULL);
2968 LASSERT(env->le_ses != NULL);
2969 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2970 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2971 LASSERT(info != NULL);
2973 mdt_thread_info_init(req, info);
2975 rc = mdt_handle0(req, info, supported);
2977 mdt_thread_info_fini(info);
2982 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2985 int mdt_recovery_handle(struct ptlrpc_request *req)
2990 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2992 rc = mdt_handle_common(req, mdt_fld_handlers);
2995 rc = mdt_handle_common(req, mdt_seq_handlers);
2998 rc = mdt_handle_common(req, mdt_regular_handlers);
3005 static int mdt_regular_handle(struct ptlrpc_request *req)
3007 return mdt_handle_common(req, mdt_regular_handlers);
3010 static int mdt_readpage_handle(struct ptlrpc_request *req)
3012 return mdt_handle_common(req, mdt_readpage_handlers);
3015 static int mdt_xmds_handle(struct ptlrpc_request *req)
3017 return mdt_handle_common(req, mdt_xmds_handlers);
3020 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3022 return mdt_handle_common(req, mdt_seq_handlers);
3025 static int mdt_mdss_handle(struct ptlrpc_request *req)
3027 return mdt_handle_common(req, mdt_seq_handlers);
3030 static int mdt_dtss_handle(struct ptlrpc_request *req)
3032 return mdt_handle_common(req, mdt_seq_handlers);
3035 static int mdt_fld_handle(struct ptlrpc_request *req)
3037 return mdt_handle_common(req, mdt_fld_handlers);
3053 static int mdt_intent_getattr(enum mdt_it_code opcode,
3054 struct mdt_thread_info *info,
3055 struct ldlm_lock **,
3057 static int mdt_intent_reint(enum mdt_it_code opcode,
3058 struct mdt_thread_info *info,
3059 struct ldlm_lock **,
3062 static struct mdt_it_flavor {
3063 const struct req_format *it_fmt;
3065 int (*it_act)(enum mdt_it_code ,
3066 struct mdt_thread_info *,
3067 struct ldlm_lock **,
3070 } mdt_it_flavor[] = {
3072 .it_fmt = &RQF_LDLM_INTENT,
3073 /*.it_flags = HABEO_REFERO,*/
3075 .it_act = mdt_intent_reint,
3076 .it_reint = REINT_OPEN
3079 .it_fmt = &RQF_LDLM_INTENT,
3080 .it_flags = MUTABOR,
3081 .it_act = mdt_intent_reint,
3082 .it_reint = REINT_OPEN
3085 .it_fmt = &RQF_LDLM_INTENT,
3086 .it_flags = MUTABOR,
3087 .it_act = mdt_intent_reint,
3088 .it_reint = REINT_CREATE
3090 [MDT_IT_GETATTR] = {
3091 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3092 .it_flags = HABEO_REFERO,
3093 .it_act = mdt_intent_getattr
3095 [MDT_IT_READDIR] = {
3101 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3102 .it_flags = HABEO_REFERO,
3103 .it_act = mdt_intent_getattr
3106 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3107 .it_flags = MUTABOR,
3109 .it_reint = REINT_UNLINK
3113 .it_flags = MUTABOR,
3116 [MDT_IT_GETXATTR] = {
3123 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3124 struct ldlm_lock **lockp,
3125 struct ldlm_lock *new_lock,
3126 struct mdt_lock_handle *lh,
3129 struct ptlrpc_request *req = mdt_info_req(info);
3130 struct ldlm_lock *lock = *lockp;
3133 * Get new lock only for cases when possible resent did not find any
3136 if (new_lock == NULL)
3137 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3139 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3140 lh->mlh_reg_lh.cookie = 0;
3144 LASSERTF(new_lock != NULL,
3145 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3148 * If we've already given this lock to a client once, then we should
3149 * have no readers or writers. Otherwise, we should have one reader
3150 * _or_ writer ref (which will be zeroed below) before returning the
3153 if (new_lock->l_export == req->rq_export) {
3154 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3156 LASSERT(new_lock->l_export == NULL);
3157 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3162 if (new_lock->l_export == req->rq_export) {
3164 * Already gave this to the client, which means that we
3165 * reconstructed a reply.
3167 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3169 lh->mlh_reg_lh.cookie = 0;
3170 RETURN(ELDLM_LOCK_REPLACED);
3174 * Fixup the lock to be given to the client.
3176 lock_res_and_lock(new_lock);
3177 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3178 * possible blocking AST. */
3179 while (new_lock->l_readers > 0) {
3180 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3181 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3182 new_lock->l_readers--;
3184 while (new_lock->l_writers > 0) {
3185 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3186 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3187 new_lock->l_writers--;
3190 new_lock->l_export = class_export_get(req->rq_export);
3191 atomic_inc(&lock->l_export->exp_locks_count);
3193 new_lock->l_blocking_ast = lock->l_blocking_ast;
3194 new_lock->l_completion_ast = lock->l_completion_ast;
3195 new_lock->l_remote_handle = lock->l_remote_handle;
3196 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3198 unlock_res_and_lock(new_lock);
3200 lustre_hash_add(new_lock->l_export->exp_lock_hash,
3201 &new_lock->l_remote_handle,
3202 &new_lock->l_exp_hash);
3204 LDLM_LOCK_RELEASE(new_lock);
3205 lh->mlh_reg_lh.cookie = 0;
3207 RETURN(ELDLM_LOCK_REPLACED);
3210 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3211 struct ldlm_lock *new_lock,
3212 struct ldlm_lock **old_lock,
3213 struct mdt_lock_handle *lh)
3215 struct ptlrpc_request *req = mdt_info_req(info);
3216 struct obd_export *exp = req->rq_export;
3217 struct lustre_handle remote_hdl;
3218 struct ldlm_request *dlmreq;
3219 struct ldlm_lock *lock;
3221 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3224 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3225 remote_hdl = dlmreq->lock_handle[0];
3227 lock = lustre_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3229 if (lock != new_lock) {
3230 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3231 lh->mlh_reg_mode = lock->l_granted_mode;
3233 LDLM_DEBUG(lock, "Restoring lock cookie");
3234 DEBUG_REQ(D_DLMTRACE, req,
3235 "restoring lock cookie "LPX64,
3236 lh->mlh_reg_lh.cookie);
3238 *old_lock = LDLM_LOCK_GET(lock);
3239 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3243 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3247 * If the xid matches, then we know this is a resent request, and allow
3248 * it. (It's probably an OPEN, for which we don't send a lock.
3250 if (req_xid_is_last(req))
3254 * This remote handle isn't enqueued, so we never received or processed
3255 * this request. Clear MSG_RESENT, because it can be handled like any
3256 * normal request now.
3258 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3260 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3264 static int mdt_intent_getattr(enum mdt_it_code opcode,
3265 struct mdt_thread_info *info,
3266 struct ldlm_lock **lockp,
3269 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3270 struct ldlm_lock *new_lock = NULL;
3272 struct ldlm_reply *ldlm_rep;
3273 struct ptlrpc_request *req;
3274 struct mdt_body *reqbody;
3275 struct mdt_body *repbody;
3279 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3282 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3285 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3286 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3287 repbody->eadatasize = 0;
3288 repbody->aclsize = 0;
3292 child_bits = MDS_INODELOCK_LOOKUP;
3294 case MDT_IT_GETATTR:
3295 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3298 CERROR("Unhandled till now");
3299 GOTO(out_shrink, rc = -EINVAL);
3302 rc = mdt_init_ucred(info, reqbody);
3304 GOTO(out_shrink, rc);
3306 req = info->mti_pill->rc_req;
3307 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3308 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3310 /* Get lock from request for possible resent case. */
3311 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3313 ldlm_rep->lock_policy_res2 =
3314 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3316 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3317 ldlm_rep->lock_policy_res2 = 0;
3318 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3319 ldlm_rep->lock_policy_res2) {
3320 lhc->mlh_reg_lh.cookie = 0ull;
3321 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3324 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3327 mdt_exit_ucred(info);
3329 mdt_shrink_reply(info);
3333 static int mdt_intent_reint(enum mdt_it_code opcode,
3334 struct mdt_thread_info *info,
3335 struct ldlm_lock **lockp,
3338 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3339 struct ldlm_reply *rep = NULL;
3343 static const struct req_format *intent_fmts[REINT_MAX] = {
3344 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3345 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3350 opc = mdt_reint_opcode(info, intent_fmts);
3354 if (mdt_it_flavor[opcode].it_reint != opc) {
3355 CERROR("Reint code %ld doesn't match intent: %d\n",
3357 RETURN(err_serious(-EPROTO));
3360 /* Get lock from request for possible resent case. */
3361 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3363 rc = mdt_reint_internal(info, lhc, opc);
3365 /* Check whether the reply has been packed successfully. */
3366 if (mdt_info_req(info)->rq_repmsg != NULL)
3367 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3369 RETURN(err_serious(-EFAULT));
3371 /* MDC expects this in any case */
3373 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3375 /* Cross-ref case, the lock should be returned to the client */
3376 if (rc == -EREMOTE) {
3377 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3378 rep->lock_policy_res2 = 0;
3379 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3382 rep->lock_policy_res2 = clear_serious(rc);
3384 if (rc == -ENOTCONN || rc == -ENODEV ||
3385 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3387 * If it is the disconnect error (ENODEV & ENOCONN), the error
3388 * will be returned by rq_status, and client at ptlrpc layer
3389 * will detect this, then disconnect, reconnect the import
3390 * immediately, instead of impacting the following the rpc.
3392 lhc->mlh_reg_lh.cookie = 0ull;
3396 * For other cases, the error will be returned by intent.
3397 * and client will retrieve the result from intent.
3400 * FIXME: when open lock is finished, that should be
3403 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3404 rep->lock_policy_res2 = 0;
3405 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3408 lhc->mlh_reg_lh.cookie = 0ull;
3409 RETURN(ELDLM_LOCK_ABORTED);
3414 static int mdt_intent_code(long itcode)
3422 case IT_OPEN|IT_CREAT:
3429 rc = MDT_IT_READDIR;
3432 rc = MDT_IT_GETATTR;
3444 rc = MDT_IT_GETXATTR;
3447 CERROR("Unknown intent opcode: %ld\n", itcode);
3454 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3455 struct ldlm_lock **lockp, int flags)
3457 struct req_capsule *pill;
3458 struct mdt_it_flavor *flv;
3463 opc = mdt_intent_code(itopc);
3467 pill = info->mti_pill;
3468 flv = &mdt_it_flavor[opc];
3470 if (flv->it_fmt != NULL)
3471 req_capsule_extend(pill, flv->it_fmt);
3473 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3475 struct ptlrpc_request *req = mdt_info_req(info);
3476 if (flv->it_flags & MUTABOR &&
3477 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3480 if (rc == 0 && flv->it_act != NULL) {
3481 /* execute policy */
3482 rc = flv->it_act(opc, info, lockp, flags);
3489 static int mdt_intent_policy(struct ldlm_namespace *ns,
3490 struct ldlm_lock **lockp, void *req_cookie,
3491 ldlm_mode_t mode, int flags, void *data)
3493 struct mdt_thread_info *info;
3494 struct ptlrpc_request *req = req_cookie;
3495 struct ldlm_intent *it;
3496 struct req_capsule *pill;
3501 LASSERT(req != NULL);
3503 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3505 LASSERT(info != NULL);
3506 pill = info->mti_pill;
3507 LASSERT(pill->rc_req == req);
3509 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3510 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3511 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3513 const struct ldlm_request *dlmreq;
3516 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3521 * Lock without inodebits makes no sense and will oops
3522 * later in ldlm. Let's check it now to see if we have
3523 * wrong lock from client or bits get corrupted
3524 * somewhere in mdt_intent_opc().
3526 dlmreq = info->mti_dlm_req;
3527 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
3528 LASSERT(req_bits != 0);
3531 rc = err_serious(-EFAULT);
3533 /* No intent was provided */
3534 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3535 rc = req_capsule_server_pack(pill);
3537 rc = err_serious(rc);
3545 static void mdt_seq_adjust(const struct lu_env *env,
3546 struct mdt_device *m, int lost)
3548 struct md_site *ms = mdt_md_site(m);
3549 struct lu_seq_range out;
3552 LASSERT(ms && ms->ms_server_seq);
3554 /* get extra seq from seq_server, moving it's range up */
3555 while (lost-- > 0) {
3556 seq_server_alloc_meta(ms->ms_server_seq, NULL, &out, env);
3561 static int mdt_seq_fini(const struct lu_env *env,
3562 struct mdt_device *m)
3564 struct md_site *ms = mdt_md_site(m);
3568 if (ms->ms_server_seq) {
3569 seq_server_fini(ms->ms_server_seq, env);
3570 OBD_FREE_PTR(ms->ms_server_seq);
3571 ms->ms_server_seq = NULL;
3574 if (ms->ms_control_seq) {
3575 seq_server_fini(ms->ms_control_seq, env);
3576 OBD_FREE_PTR(ms->ms_control_seq);
3577 ms->ms_control_seq = NULL;
3580 if (ms->ms_client_seq) {
3581 seq_client_fini(ms->ms_client_seq);
3582 OBD_FREE_PTR(ms->ms_client_seq);
3583 ms->ms_client_seq = NULL;
3590 static int mdt_seq_init(const struct lu_env *env,
3592 struct mdt_device *m)
3599 ms = mdt_md_site(m);
3602 * This is sequence-controller node. Init seq-controller server on local
3605 if (ms->ms_node_id == 0) {
3606 LASSERT(ms->ms_control_seq == NULL);
3608 OBD_ALLOC_PTR(ms->ms_control_seq);
3609 if (ms->ms_control_seq == NULL)
3612 rc = seq_server_init(ms->ms_control_seq,
3613 m->mdt_bottom, uuid,
3614 LUSTRE_SEQ_CONTROLLER,
3619 GOTO(out_seq_fini, rc);
3621 OBD_ALLOC_PTR(ms->ms_client_seq);
3622 if (ms->ms_client_seq == NULL)
3623 GOTO(out_seq_fini, rc = -ENOMEM);
3625 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3626 if (prefix == NULL) {
3627 OBD_FREE_PTR(ms->ms_client_seq);
3628 GOTO(out_seq_fini, rc = -ENOMEM);
3631 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3635 * Init seq-controller client after seq-controller server is
3636 * ready. Pass ms->ms_control_seq to it for direct talking.
3638 rc = seq_client_init(ms->ms_client_seq, NULL,
3639 LUSTRE_SEQ_METADATA, prefix,
3640 ms->ms_control_seq);
3641 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3644 GOTO(out_seq_fini, rc);
3647 /* Init seq-server on local MDT */
3648 LASSERT(ms->ms_server_seq == NULL);
3650 OBD_ALLOC_PTR(ms->ms_server_seq);
3651 if (ms->ms_server_seq == NULL)
3652 GOTO(out_seq_fini, rc = -ENOMEM);
3654 rc = seq_server_init(ms->ms_server_seq,
3655 m->mdt_bottom, uuid,
3660 GOTO(out_seq_fini, rc = -ENOMEM);
3662 /* Assign seq-controller client to local seq-server. */
3663 if (ms->ms_node_id == 0) {
3664 LASSERT(ms->ms_client_seq != NULL);
3666 rc = seq_server_set_cli(ms->ms_server_seq,
3674 mdt_seq_fini(env, m);
3679 * Init client sequence manager which is used by local MDS to talk to sequence
3680 * controller on remote node.
3682 static int mdt_seq_init_cli(const struct lu_env *env,
3683 struct mdt_device *m,
3684 struct lustre_cfg *cfg)
3686 struct md_site *ms = mdt_md_site(m);
3687 struct obd_device *mdc;
3688 struct obd_uuid *uuidp, *mdcuuidp;
3689 char *uuid_str, *mdc_uuid_str;
3692 struct mdt_thread_info *info;
3693 char *p, *index_string = lustre_cfg_string(cfg, 2);
3696 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3697 uuidp = &info->mti_u.uuid[0];
3698 mdcuuidp = &info->mti_u.uuid[1];
3700 LASSERT(index_string);
3702 index = simple_strtol(index_string, &p, 10);
3704 CERROR("Invalid index in lustre_cgf, offset 2\n");
3708 /* check if this is adding the first MDC and controller is not yet
3710 if (index != 0 || ms->ms_client_seq)
3713 uuid_str = lustre_cfg_string(cfg, 1);
3714 mdc_uuid_str = lustre_cfg_string(cfg, 4);
3715 obd_str2uuid(uuidp, uuid_str);
3716 obd_str2uuid(mdcuuidp, mdc_uuid_str);
3718 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3720 CERROR("can't find controller MDC by uuid %s\n",
3723 } else if (!mdc->obd_set_up) {
3724 CERROR("target %s not set up\n", mdc->obd_name);
3727 LASSERT(ms->ms_control_exp);
3728 OBD_ALLOC_PTR(ms->ms_client_seq);
3729 if (ms->ms_client_seq != NULL) {
3732 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3736 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3739 rc = seq_client_init(ms->ms_client_seq,
3741 LUSTRE_SEQ_METADATA,
3743 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3750 LASSERT(ms->ms_server_seq != NULL);
3751 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
3758 static void mdt_seq_fini_cli(struct mdt_device *m)
3764 ms = mdt_md_site(m);
3767 if (ms->ms_server_seq)
3768 seq_server_set_cli(ms->ms_server_seq,
3771 if (ms->ms_control_exp) {
3772 class_export_put(ms->ms_control_exp);
3773 ms->ms_control_exp = NULL;
3782 static int mdt_fld_fini(const struct lu_env *env,
3783 struct mdt_device *m)
3785 struct md_site *ms = mdt_md_site(m);
3788 if (ms && ms->ms_server_fld) {
3789 fld_server_fini(ms->ms_server_fld, env);
3790 OBD_FREE_PTR(ms->ms_server_fld);
3791 ms->ms_server_fld = NULL;
3797 static int mdt_fld_init(const struct lu_env *env,
3799 struct mdt_device *m)
3805 ms = mdt_md_site(m);
3807 OBD_ALLOC_PTR(ms->ms_server_fld);
3808 if (ms->ms_server_fld == NULL)
3809 RETURN(rc = -ENOMEM);
3811 rc = fld_server_init(ms->ms_server_fld,
3812 m->mdt_bottom, uuid,
3813 env, ms->ms_node_id);
3815 OBD_FREE_PTR(ms->ms_server_fld);
3816 ms->ms_server_fld = NULL;
3823 /* device init/fini methods */
3824 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3827 if (m->mdt_regular_service != NULL) {
3828 ptlrpc_unregister_service(m->mdt_regular_service);
3829 m->mdt_regular_service = NULL;
3831 if (m->mdt_readpage_service != NULL) {
3832 ptlrpc_unregister_service(m->mdt_readpage_service);
3833 m->mdt_readpage_service = NULL;
3835 if (m->mdt_xmds_service != NULL) {
3836 ptlrpc_unregister_service(m->mdt_xmds_service);
3837 m->mdt_xmds_service = NULL;
3839 if (m->mdt_setattr_service != NULL) {
3840 ptlrpc_unregister_service(m->mdt_setattr_service);
3841 m->mdt_setattr_service = NULL;
3843 if (m->mdt_mdsc_service != NULL) {
3844 ptlrpc_unregister_service(m->mdt_mdsc_service);
3845 m->mdt_mdsc_service = NULL;
3847 if (m->mdt_mdss_service != NULL) {
3848 ptlrpc_unregister_service(m->mdt_mdss_service);
3849 m->mdt_mdss_service = NULL;
3851 if (m->mdt_dtss_service != NULL) {
3852 ptlrpc_unregister_service(m->mdt_dtss_service);
3853 m->mdt_dtss_service = NULL;
3855 if (m->mdt_fld_service != NULL) {
3856 ptlrpc_unregister_service(m->mdt_fld_service);
3857 m->mdt_fld_service = NULL;
3862 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3865 static struct ptlrpc_service_conf conf;
3866 cfs_proc_dir_entry_t *procfs_entry;
3869 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3871 conf = (typeof(conf)) {
3872 .psc_nbufs = MDS_NBUFS,
3873 .psc_bufsize = MDS_BUFSIZE,
3874 .psc_max_req_size = MDS_MAXREQSIZE,
3875 .psc_max_reply_size = MDS_MAXREPSIZE,
3876 .psc_req_portal = MDS_REQUEST_PORTAL,
3877 .psc_rep_portal = MDC_REPLY_PORTAL,
3878 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3880 * We'd like to have a mechanism to set this on a per-device
3881 * basis, but alas...
3883 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3885 .psc_max_threads = MDT_MAX_THREADS,
3886 .psc_ctx_tags = LCT_MD_THREAD
3889 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3890 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3891 "mdt_ldlm_client", m->mdt_ldlm_client);
3893 m->mdt_regular_service =
3894 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3895 procfs_entry, target_print_req,
3897 if (m->mdt_regular_service == NULL)
3900 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3902 GOTO(err_mdt_svc, rc);
3905 * readpage service configuration. Parameters have to be adjusted,
3908 conf = (typeof(conf)) {
3909 .psc_nbufs = MDS_NBUFS,
3910 .psc_bufsize = MDS_BUFSIZE,
3911 .psc_max_req_size = MDS_MAXREQSIZE,
3912 .psc_max_reply_size = MDS_MAXREPSIZE,
3913 .psc_req_portal = MDS_READPAGE_PORTAL,
3914 .psc_rep_portal = MDC_REPLY_PORTAL,
3915 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3916 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3918 .psc_max_threads = MDT_MAX_THREADS,
3919 .psc_ctx_tags = LCT_MD_THREAD
3921 m->mdt_readpage_service =
3922 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3923 LUSTRE_MDT_NAME "_readpage",
3924 procfs_entry, target_print_req,"mdt_rdpg");
3926 if (m->mdt_readpage_service == NULL) {
3927 CERROR("failed to start readpage service\n");
3928 GOTO(err_mdt_svc, rc = -ENOMEM);
3931 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3934 * setattr service configuration.
3936 conf = (typeof(conf)) {
3937 .psc_nbufs = MDS_NBUFS,
3938 .psc_bufsize = MDS_BUFSIZE,
3939 .psc_max_req_size = MDS_MAXREQSIZE,
3940 .psc_max_reply_size = MDS_MAXREPSIZE,
3941 .psc_req_portal = MDS_SETATTR_PORTAL,
3942 .psc_rep_portal = MDC_REPLY_PORTAL,
3943 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3944 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3946 .psc_max_threads = MDT_MAX_THREADS,
3947 .psc_ctx_tags = LCT_MD_THREAD
3950 m->mdt_setattr_service =
3951 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3952 LUSTRE_MDT_NAME "_setattr",
3953 procfs_entry, target_print_req,"mdt_attr");
3955 if (!m->mdt_setattr_service) {
3956 CERROR("failed to start setattr service\n");
3957 GOTO(err_mdt_svc, rc = -ENOMEM);
3960 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3962 GOTO(err_mdt_svc, rc);
3965 * sequence controller service configuration
3967 conf = (typeof(conf)) {
3968 .psc_nbufs = MDS_NBUFS,
3969 .psc_bufsize = MDS_BUFSIZE,
3970 .psc_max_req_size = SEQ_MAXREQSIZE,
3971 .psc_max_reply_size = SEQ_MAXREPSIZE,
3972 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3973 .psc_rep_portal = MDC_REPLY_PORTAL,
3974 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3975 .psc_min_threads = SEQ_NUM_THREADS,
3976 .psc_max_threads = SEQ_NUM_THREADS,
3977 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3980 m->mdt_mdsc_service =
3981 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3982 LUSTRE_MDT_NAME"_mdsc",
3983 procfs_entry, target_print_req,"mdt_mdsc");
3984 if (!m->mdt_mdsc_service) {
3985 CERROR("failed to start seq controller service\n");
3986 GOTO(err_mdt_svc, rc = -ENOMEM);
3989 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3991 GOTO(err_mdt_svc, rc);
3994 * metadata sequence server service configuration
3996 conf = (typeof(conf)) {
3997 .psc_nbufs = MDS_NBUFS,
3998 .psc_bufsize = MDS_BUFSIZE,
3999 .psc_max_req_size = SEQ_MAXREQSIZE,
4000 .psc_max_reply_size = SEQ_MAXREPSIZE,
4001 .psc_req_portal = SEQ_METADATA_PORTAL,
4002 .psc_rep_portal = MDC_REPLY_PORTAL,
4003 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4004 .psc_min_threads = SEQ_NUM_THREADS,
4005 .psc_max_threads = SEQ_NUM_THREADS,
4006 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4009 m->mdt_mdss_service =
4010 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
4011 LUSTRE_MDT_NAME"_mdss",
4012 procfs_entry, target_print_req,"mdt_mdss");
4013 if (!m->mdt_mdss_service) {
4014 CERROR("failed to start metadata seq server service\n");
4015 GOTO(err_mdt_svc, rc = -ENOMEM);
4018 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
4020 GOTO(err_mdt_svc, rc);
4024 * Data sequence server service configuration. We want to have really
4025 * cluster-wide sequences space. This is why we start only one sequence
4026 * controller which manages space.
4028 conf = (typeof(conf)) {
4029 .psc_nbufs = MDS_NBUFS,
4030 .psc_bufsize = MDS_BUFSIZE,
4031 .psc_max_req_size = SEQ_MAXREQSIZE,
4032 .psc_max_reply_size = SEQ_MAXREPSIZE,
4033 .psc_req_portal = SEQ_DATA_PORTAL,
4034 .psc_rep_portal = OSC_REPLY_PORTAL,
4035 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4036 .psc_min_threads = SEQ_NUM_THREADS,
4037 .psc_max_threads = SEQ_NUM_THREADS,
4038 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
4041 m->mdt_dtss_service =
4042 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
4043 LUSTRE_MDT_NAME"_dtss",
4044 procfs_entry, target_print_req,"mdt_dtss");
4045 if (!m->mdt_dtss_service) {
4046 CERROR("failed to start data seq server service\n");
4047 GOTO(err_mdt_svc, rc = -ENOMEM);
4050 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
4052 GOTO(err_mdt_svc, rc);
4054 /* FLD service start */
4055 conf = (typeof(conf)) {
4056 .psc_nbufs = MDS_NBUFS,
4057 .psc_bufsize = MDS_BUFSIZE,
4058 .psc_max_req_size = FLD_MAXREQSIZE,
4059 .psc_max_reply_size = FLD_MAXREPSIZE,
4060 .psc_req_portal = FLD_REQUEST_PORTAL,
4061 .psc_rep_portal = MDC_REPLY_PORTAL,
4062 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4063 .psc_min_threads = FLD_NUM_THREADS,
4064 .psc_max_threads = FLD_NUM_THREADS,
4065 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
4068 m->mdt_fld_service =
4069 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
4070 LUSTRE_MDT_NAME"_fld",
4071 procfs_entry, target_print_req, "mdt_fld");
4072 if (!m->mdt_fld_service) {
4073 CERROR("failed to start fld service\n");
4074 GOTO(err_mdt_svc, rc = -ENOMEM);
4077 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
4079 GOTO(err_mdt_svc, rc);
4082 * mds-mds service configuration. Separate portal is used to allow
4083 * mds-mds requests be not blocked during recovery.
4085 conf = (typeof(conf)) {
4086 .psc_nbufs = MDS_NBUFS,
4087 .psc_bufsize = MDS_BUFSIZE,
4088 .psc_max_req_size = MDS_MAXREQSIZE,
4089 .psc_max_reply_size = MDS_MAXREPSIZE,
4090 .psc_req_portal = MDS_MDS_PORTAL,
4091 .psc_rep_portal = MDC_REPLY_PORTAL,
4092 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4093 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
4095 .psc_max_threads = MDT_MAX_THREADS,
4096 .psc_ctx_tags = LCT_MD_THREAD
4098 m->mdt_xmds_service =
4099 ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
4100 LUSTRE_MDT_NAME "_mds",
4101 procfs_entry, target_print_req,"mdt_xmds");
4103 if (m->mdt_xmds_service == NULL) {
4104 CERROR("failed to start readpage service\n");
4105 GOTO(err_mdt_svc, rc = -ENOMEM);
4108 rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
4110 GOTO(err_mdt_svc, rc);
4115 mdt_stop_ptlrpc_service(m);
4120 static void mdt_stack_fini(const struct lu_env *env,
4121 struct mdt_device *m, struct lu_device *top)
4123 struct obd_device *obd = mdt2obd_dev(m);
4124 struct lustre_cfg_bufs *bufs;
4125 struct lustre_cfg *lcfg;
4126 struct mdt_thread_info *info;
4130 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4131 LASSERT(info != NULL);
4133 bufs = &info->mti_u.bufs;
4134 /* process cleanup, pass mdt obd name to get obd umount flags */
4135 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4140 lustre_cfg_bufs_set_string(bufs, 1, flags);
4141 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4143 CERROR("Cannot alloc lcfg!\n");
4148 top->ld_ops->ldo_process_config(env, top, lcfg);
4149 lustre_cfg_free(lcfg);
4151 lu_stack_fini(env, top);
4152 m->mdt_child = NULL;
4153 m->mdt_bottom = NULL;
4156 static struct lu_device *mdt_layer_setup(struct lu_env *env,
4157 const char *typename,
4158 struct lu_device *child,
4159 struct lustre_cfg *cfg)
4161 const char *dev = lustre_cfg_string(cfg, 0);
4162 struct obd_type *type;
4163 struct lu_device_type *ldt;
4164 struct lu_device *d;
4169 type = class_get_type(typename);
4171 CERROR("Unknown type: '%s'\n", typename);
4172 GOTO(out, rc = -ENODEV);
4175 rc = lu_env_refill((struct lu_env *)env);
4177 CERROR("Failure to refill session: '%d'\n", rc);
4183 CERROR("type: '%s'\n", typename);
4184 GOTO(out_type, rc = -EINVAL);
4187 ldt->ldt_obd_type = type;
4188 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
4190 CERROR("Cannot allocate device: '%s'\n", typename);
4191 GOTO(out_type, rc = -ENODEV);
4194 LASSERT(child->ld_site);
4195 d->ld_site = child->ld_site;
4198 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
4200 CERROR("can't init device '%s', rc %d\n", typename, rc);
4201 GOTO(out_alloc, rc);
4204 lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
4209 ldt->ldt_ops->ldto_device_free(env, d);
4212 class_put_type(type);
4217 static int mdt_stack_init(struct lu_env *env,
4218 struct mdt_device *m,
4219 struct lustre_cfg *cfg,
4220 struct lustre_mount_info *lmi)
4222 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4223 struct lu_device *tmp;
4224 struct md_device *md;
4225 struct lu_device *child_lu_dev;
4229 /* init the stack */
4230 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
4232 RETURN(PTR_ERR(tmp));
4234 m->mdt_bottom = lu2dt_dev(tmp);
4236 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
4238 GOTO(out, rc = PTR_ERR(tmp));
4243 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
4245 GOTO(out, rc = PTR_ERR(tmp));
4248 /*set mdd upcall device*/
4249 md_upcall_dev_set(md, lu2md_dev(d));
4252 /*set cmm upcall device*/
4253 md_upcall_dev_set(md, &m->mdt_md_dev);
4255 m->mdt_child = lu2md_dev(d);
4257 /* process setup config */
4258 tmp = &m->mdt_md_dev.md_lu_dev;
4259 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
4263 /* initialize local objects */
4264 child_lu_dev = &m->mdt_child->md_lu_dev;
4266 rc = child_lu_dev->ld_ops->ldo_prepare(env,
4267 &m->mdt_md_dev.md_lu_dev,
4270 /* fini from last known good lu_device */
4272 mdt_stack_fini(env, m, d);
4278 * setup CONFIG_ORIG context, used to access local config log.
4279 * this may need to be rewrite as part of llog rewrite for lu-api.
4281 static int mdt_obd_llog_setup(struct obd_device *obd,
4282 struct lustre_sb_info *lsi)
4286 LASSERT(obd->obd_fsops == NULL);
4288 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
4289 if (IS_ERR(obd->obd_fsops))
4290 return PTR_ERR(obd->obd_fsops);
4292 rc = fsfilt_setup(obd, lsi->lsi_srv_mnt->mnt_sb);
4294 fsfilt_put_ops(obd->obd_fsops);
4298 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
4299 obd->obd_lvfs_ctxt.pwdmnt = lsi->lsi_srv_mnt;
4300 obd->obd_lvfs_ctxt.pwd = lsi->lsi_srv_mnt->mnt_root;
4301 obd->obd_lvfs_ctxt.fs = get_ds();
4303 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd,
4304 0, NULL, &llog_lvfs_ops);
4306 CERROR("llog_setup() failed: %d\n", rc);
4307 fsfilt_put_ops(obd->obd_fsops);
4313 static void mdt_obd_llog_cleanup(struct obd_device *obd)
4315 struct llog_ctxt *ctxt;
4317 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
4321 if (obd->obd_fsops) {
4322 fsfilt_put_ops(obd->obd_fsops);
4323 obd->obd_fsops = NULL;
4327 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4329 struct md_device *next = m->mdt_child;
4330 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4331 struct lu_site *ls = d->ld_site;
4332 struct obd_device *obd = mdt2obd_dev(m);
4335 target_recovery_fini(obd);
4337 ping_evictor_stop();
4339 mdt_stop_ptlrpc_service(m);
4340 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4341 mdt_obd_llog_cleanup(obd);
4342 obd_exports_barrier(obd);
4343 obd_zombie_barrier();
4344 #ifdef HAVE_QUOTA_SUPPORT
4345 next->md_ops->mdo_quota.mqo_cleanup(env, next);
4347 lut_fini(env, &m->mdt_lut);
4348 mdt_fs_cleanup(env, m);
4349 upcall_cache_cleanup(m->mdt_identity_cache);
4350 m->mdt_identity_cache = NULL;
4352 if (m->mdt_namespace != NULL) {
4353 ldlm_namespace_free(m->mdt_namespace, NULL, d->ld_obd->obd_force);
4354 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4357 cfs_free_nidlist(&m->mdt_nosquash_nids);
4358 if (m->mdt_nosquash_str) {
4359 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4360 m->mdt_nosquash_str = NULL;
4361 m->mdt_nosquash_strlen = 0;
4364 mdt_seq_fini(env, m);
4365 mdt_seq_fini_cli(m);
4366 mdt_fld_fini(env, m);
4367 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4369 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4370 cfs_timer_disarm(&m->mdt_ck_timer);
4371 mdt_ck_thread_stop(m);
4376 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4379 if (obd->obd_proc_exports_entry) {
4380 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
4381 obd->obd_proc_exports_entry = NULL;
4383 lprocfs_free_per_client_stats(obd);
4384 lprocfs_free_obd_stats(obd);
4385 ptlrpc_lprocfs_unregister_obd(obd);
4386 lprocfs_obd_cleanup(obd);
4389 struct md_site *mite;
4392 mite = lu_site2md(ls);
4396 LASSERT(atomic_read(&d->ld_ref) == 0);
4401 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4403 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4404 struct sptlrpc_rule_set tmp_rset;
4407 sptlrpc_rule_set_init(&tmp_rset);
4408 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4410 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4415 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4417 write_lock(&m->mdt_sptlrpc_lock);
4418 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4419 m->mdt_sptlrpc_rset = tmp_rset;
4420 write_unlock(&m->mdt_sptlrpc_lock);
4425 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
4429 m->mdt_opts.mo_mds_capa = 1;
4430 m->mdt_opts.mo_oss_capa = 1;
4431 #ifdef CONFIG_FS_POSIX_ACL
4432 /* ACLs should be enabled by default (b=13829) */
4433 m->mdt_opts.mo_acl = 1;
4434 LCONSOLE_INFO("Enabling ACL\n");
4436 m->mdt_opts.mo_acl = 0;
4437 LCONSOLE_INFO("Disabling ACL\n");
4446 while (*p && *p != ',')
4450 if ((len == sizeof("user_xattr") - 1) &&
4451 (memcmp(options, "user_xattr", len) == 0)) {
4452 m->mdt_opts.mo_user_xattr = 1;
4453 LCONSOLE_INFO("Enabling user_xattr\n");
4454 } else if ((len == sizeof("nouser_xattr") - 1) &&
4455 (memcmp(options, "nouser_xattr", len) == 0)) {
4456 m->mdt_opts.mo_user_xattr = 0;
4457 LCONSOLE_INFO("Disabling user_xattr\n");
4458 } else if ((len == sizeof("noacl") - 1) &&
4459 (memcmp(options, "noacl", len) == 0)) {
4460 m->mdt_opts.mo_acl = 0;
4461 LCONSOLE_INFO("Disabling ACL\n");
4468 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4470 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4471 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4473 struct lprocfs_static_vars lvars;
4474 struct mdt_thread_info *info;
4475 struct obd_device *obd;
4476 const char *dev = lustre_cfg_string(cfg, 0);
4477 const char *num = lustre_cfg_string(cfg, 2);
4478 struct lustre_mount_info *lmi = NULL;
4479 struct lustre_sb_info *lsi;
4480 struct lustre_disk_data *ldd;
4482 struct md_site *mite;
4483 const char *identity_upcall = "NONE";
4484 #ifdef HAVE_QUOTA_SUPPORT
4485 struct md_device *next;
4491 md_device_init(&m->mdt_md_dev, ldt);
4493 * Environment (env) might be missing mdt_thread_key values at that
4494 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4497 * Usually device allocation path doesn't use module key values, but
4498 * mdt has to do a lot of work here, so allocate key value.
4500 rc = lu_env_refill((struct lu_env *)env);
4504 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4505 LASSERT(info != NULL);
4507 obd = class_name2obd(dev);
4508 LASSERT(obd != NULL);
4510 spin_lock_init(&m->mdt_transno_lock);
4512 m->mdt_max_mdsize = MAX_MD_SIZE;
4513 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
4514 m->mdt_som_conf = 0;
4516 m->mdt_opts.mo_user_xattr = 0;
4517 m->mdt_opts.mo_acl = 0;
4518 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4519 lmi = server_get_mount_2(dev);
4521 CERROR("Cannot get mount info for %s!\n", dev);
4524 lsi = s2lsi(lmi->lmi_sb);
4525 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
4526 /* CMD is supported only in IAM mode */
4529 node_id = simple_strtol(num, NULL, 10);
4530 if (!(ldd->ldd_flags & LDD_F_IAM_DIR) && node_id) {
4531 CERROR("CMD Operation not allowed in IOP mode\n");
4532 GOTO(err_lmi, rc = -EINVAL);
4536 rwlock_init(&m->mdt_sptlrpc_lock);
4537 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4539 spin_lock_init(&m->mdt_ioepoch_lock);
4540 m->mdt_opts.mo_compat_resname = 0;
4541 m->mdt_capa_timeout = CAPA_TIMEOUT;
4542 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4543 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4544 m->mdt_squash_uid = 0;
4545 m->mdt_squash_gid = 0;
4546 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4547 m->mdt_nosquash_str = NULL;
4548 m->mdt_nosquash_strlen = 0;
4549 init_rwsem(&m->mdt_squash_sem);
4551 spin_lock_init(&m->mdt_client_bitmap_lock);
4553 OBD_ALLOC_PTR(mite);
4555 GOTO(err_lmi, rc = -ENOMEM);
4559 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4560 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4561 /* set this lu_device to obd, because error handling need it */
4562 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4564 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
4566 CERROR("Can't init lu_site, rc %d\n", rc);
4567 GOTO(err_free_site, rc);
4570 lprocfs_mdt_init_vars(&lvars);
4571 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
4573 CERROR("Can't init lprocfs, rc %d\n", rc);
4574 GOTO(err_fini_site, rc);
4576 ptlrpc_lprocfs_register_obd(obd);
4578 rc = mdt_procfs_init(m, dev);
4580 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4581 GOTO(err_fini_proc, rc);
4584 obd->obd_proc_exports_entry = proc_mkdir("exports",
4585 obd->obd_proc_entry);
4586 if (obd->obd_proc_exports_entry)
4587 lprocfs_add_simple(obd->obd_proc_exports_entry,
4588 "clear", lprocfs_nid_stats_clear_read,
4589 lprocfs_nid_stats_clear_write, obd, NULL);
4591 /* set server index */
4592 lu_site2md(s)->ms_node_id = node_id;
4594 /* failover is the default
4595 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4596 * assumed whose ms_node_id == 0 XXX
4598 obd->obd_replayable = 1;
4599 /* No connection accepted until configurations will finish */
4600 obd->obd_no_conn = 1;
4602 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4603 char *str = lustre_cfg_string(cfg, 4);
4604 if (strchr(str, 'n')) {
4605 CWARN("%s: recovery disabled\n", obd->obd_name);
4606 obd->obd_replayable = 0;
4610 /* init the stack */
4611 rc = mdt_stack_init((struct lu_env *)env, m, cfg, lmi);
4613 CERROR("Can't init device stack, rc %d\n", rc);
4614 GOTO(err_fini_proc, rc);
4617 rc = lut_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4619 GOTO(err_fini_stack, rc);
4621 rc = mdt_fld_init(env, obd->obd_name, m);
4625 rc = mdt_seq_init(env, obd->obd_name, m);
4627 GOTO(err_fini_fld, rc);
4629 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4630 LUSTRE_MDT_NAME"-%p", m);
4631 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4632 LDLM_NAMESPACE_SERVER,
4633 LDLM_NAMESPACE_GREEDY);
4634 if (m->mdt_namespace == NULL)
4635 GOTO(err_fini_seq, rc = -ENOMEM);
4637 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4638 /* set obd_namespace for compatibility with old code */
4639 obd->obd_namespace = m->mdt_namespace;
4641 /* XXX: to support suppgid for ACL, we enable identity_upcall
4642 * by default, otherwise, maybe got unexpected -EACCESS. */
4643 if (m->mdt_opts.mo_acl)
4644 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4646 m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
4647 &mdt_identity_upcall_cache_ops);
4648 if (IS_ERR(m->mdt_identity_cache)) {
4649 rc = PTR_ERR(m->mdt_identity_cache);
4650 m->mdt_identity_cache = NULL;
4651 GOTO(err_free_ns, rc);
4654 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4656 rc = mdt_ck_thread_start(m);
4658 GOTO(err_free_ns, rc);
4660 rc = mdt_fs_setup(env, m, obd, lsi);
4664 rc = mdt_obd_llog_setup(obd, lsi);
4666 GOTO(err_fs_cleanup, rc);
4668 rc = mdt_llog_ctxt_clone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4670 GOTO(err_llog_cleanup, rc);
4672 mdt_adapt_sptlrpc_conf(obd, 1);
4674 #ifdef HAVE_QUOTA_SUPPORT
4675 next = m->mdt_child;
4676 rc = next->md_ops->mdo_quota.mqo_setup(env, next, lmi->lmi_mnt);
4678 GOTO(err_llog_cleanup, rc);
4681 server_put_mount_2(dev, lmi->lmi_mnt);
4684 target_recovery_init(&m->mdt_lut, mdt_recovery_handle);
4686 rc = mdt_start_ptlrpc_service(m);
4688 GOTO(err_recovery, rc);
4690 ping_evictor_start();
4692 rc = lu_site_init_finish(s);
4694 GOTO(err_stop_service, rc);
4696 if (obd->obd_recovering == 0)
4697 mdt_postrecov(env, m);
4699 mdt_init_capa_ctxt(env, m);
4701 /* Reduce the initial timeout on an MDS because it doesn't need such
4702 * a long timeout as an OST does. Adaptive timeouts will adjust this
4703 * value appropriately. */
4704 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4705 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4710 ping_evictor_stop();
4711 mdt_stop_ptlrpc_service(m);
4713 target_recovery_fini(obd);
4714 #ifdef HAVE_QUOTA_SUPPORT
4715 next->md_ops->mdo_quota.mqo_cleanup(env, next);
4718 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4719 mdt_obd_llog_cleanup(obd);
4721 mdt_fs_cleanup(env, m);
4723 cfs_timer_disarm(&m->mdt_ck_timer);
4724 mdt_ck_thread_stop(m);
4726 upcall_cache_cleanup(m->mdt_identity_cache);
4727 m->mdt_identity_cache = NULL;
4728 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4729 obd->obd_namespace = m->mdt_namespace = NULL;
4731 mdt_seq_fini(env, m);
4733 mdt_fld_fini(env, m);
4735 lut_fini(env, &m->mdt_lut);
4737 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4740 if (obd->obd_proc_exports_entry) {
4741 lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
4742 obd->obd_proc_exports_entry = NULL;
4744 ptlrpc_lprocfs_unregister_obd(obd);
4745 lprocfs_obd_cleanup(obd);
4752 server_put_mount_2(dev, lmi->lmi_mnt);
4756 /* used by MGS to process specific configurations */
4757 static int mdt_process_config(const struct lu_env *env,
4758 struct lu_device *d, struct lustre_cfg *cfg)
4760 struct mdt_device *m = mdt_dev(d);
4761 struct md_device *md_next = m->mdt_child;
4762 struct lu_device *next = md2lu_dev(md_next);
4766 switch (cfg->lcfg_command) {
4768 struct lprocfs_static_vars lvars;
4769 struct obd_device *obd = d->ld_obd;
4772 * For interoperability between 1.8 and 2.0,
4773 * skip old "mdt.group_upcall" param.
4776 char *param = lustre_cfg_string(cfg, 1);
4777 if (param && !strncmp("mdt.group_upcall", param, 16)) {
4778 CWARN("For 1.8 interoperability, skip this"
4779 " mdt.group_upcall. It is obsolete\n");
4784 lprocfs_mdt_init_vars(&lvars);
4785 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4787 if (rc > 0 || rc == -ENOSYS)
4788 /* we don't understand; pass it on */
4789 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4794 * Add mdc hook to get first MDT uuid and connect it to
4795 * ls->controller to use for seq manager.
4797 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4799 CERROR("Can't add mdc, rc %d\n", rc);
4801 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4804 /* others are passed further */
4805 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4811 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4812 const struct lu_object_header *hdr,
4813 struct lu_device *d)
4815 struct mdt_object *mo;
4821 struct lu_object *o;
4822 struct lu_object_header *h;
4824 o = &mo->mot_obj.mo_lu;
4825 h = &mo->mot_header;
4826 lu_object_header_init(h);
4827 lu_object_init(o, h, d);
4828 lu_object_add_top(h, o);
4829 o->lo_ops = &mdt_obj_ops;
4835 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4836 const struct lu_object_conf *unused)
4838 struct mdt_device *d = mdt_dev(o->lo_dev);
4839 struct lu_device *under;
4840 struct lu_object *below;
4844 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4845 PFID(lu_object_fid(o)));
4847 under = &d->mdt_child->md_lu_dev;
4848 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4849 if (below != NULL) {
4850 lu_object_add(o, below);
4857 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4859 struct mdt_object *mo = mdt_obj(o);
4860 struct lu_object_header *h;
4864 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4865 PFID(lu_object_fid(o)));
4868 lu_object_header_fini(h);
4873 static int mdt_object_print(const struct lu_env *env, void *cookie,
4874 lu_printer_t p, const struct lu_object *o)
4876 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4877 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch=%llu "
4878 "flags=%llx, epochcount=%d, writecount=%d)",
4879 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4880 mdto->mot_epochcount, mdto->mot_writecount);
4883 static const struct lu_device_operations mdt_lu_ops = {
4884 .ldo_object_alloc = mdt_object_alloc,
4885 .ldo_process_config = mdt_process_config,
4888 static const struct lu_object_operations mdt_obj_ops = {
4889 .loo_object_init = mdt_object_init,
4890 .loo_object_free = mdt_object_free,
4891 .loo_object_print = mdt_object_print
4894 static int mdt_obd_set_info_async(struct obd_export *exp,
4895 __u32 keylen, void *key,
4896 __u32 vallen, void *val,
4897 struct ptlrpc_request_set *set)
4899 struct obd_device *obd = exp->exp_obd;
4905 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4906 rc = mdt_adapt_sptlrpc_conf(obd, 0);
4913 /* mds_connect_internal */
4914 static int mdt_connect_internal(struct obd_export *exp,
4915 struct mdt_device *mdt,
4916 struct obd_connect_data *data)
4919 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4920 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4922 /* If no known bits (which should not happen, probably,
4923 as everybody should support LOOKUP and UPDATE bits at least)
4924 revert to compat mode with plain locks. */
4925 if (!data->ocd_ibits_known &&
4926 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4927 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4929 if (!mdt->mdt_opts.mo_acl)
4930 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4932 if (!mdt->mdt_opts.mo_user_xattr)
4933 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4935 if (!mdt->mdt_som_conf)
4936 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4938 spin_lock(&exp->exp_lock);
4939 exp->exp_connect_flags = data->ocd_connect_flags;
4940 spin_unlock(&exp->exp_lock);
4941 data->ocd_version = LUSTRE_VERSION_CODE;
4942 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4946 if (mdt->mdt_opts.mo_acl &&
4947 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4948 CWARN("%s: MDS requires ACL support but client does not\n",
4949 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4954 if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
4955 CWARN("%s: MDS requires FID support, but client not\n",
4956 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4960 if (mdt->mdt_som_conf &&
4961 !(exp->exp_connect_flags & OBD_CONNECT_MDS_MDS) &&
4962 !(exp->exp_connect_flags & OBD_CONNECT_SOM)) {
4963 CWARN("%s: MDS has SOM enabled, but client does not support "
4964 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4971 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
4972 struct obd_export *exp,
4973 struct ptlrpc_request *req)
4975 struct sptlrpc_flavor flvr;
4978 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
4979 read_lock(&mdt->mdt_sptlrpc_lock);
4980 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
4984 read_unlock(&mdt->mdt_sptlrpc_lock);
4986 spin_lock(&exp->exp_lock);
4988 exp->exp_sp_peer = req->rq_sp_from;
4989 exp->exp_flvr = flvr;
4991 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
4992 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4993 CERROR("unauthorized rpc flavor %x from %s, "
4994 "expect %x\n", req->rq_flvr.sf_rpc,
4995 libcfs_nid2str(req->rq_peer.nid),
4996 exp->exp_flvr.sf_rpc);
5000 spin_unlock(&exp->exp_lock);
5002 if (exp->exp_sp_peer != req->rq_sp_from) {
5003 CERROR("RPC source %s doesn't match %s\n",
5004 sptlrpc_part2name(req->rq_sp_from),
5005 sptlrpc_part2name(exp->exp_sp_peer));
5008 rc = sptlrpc_target_export_check(exp, req);
5015 /* mds_connect copy */
5016 static int mdt_obd_connect(const struct lu_env *env,
5017 struct obd_export **exp, struct obd_device *obd,
5018 struct obd_uuid *cluuid,
5019 struct obd_connect_data *data,
5022 struct mdt_thread_info *info;
5023 struct lsd_client_data *lcd;
5024 struct obd_export *lexp;
5025 struct lustre_handle conn = { 0 };
5026 struct mdt_device *mdt;
5027 struct ptlrpc_request *req;
5031 LASSERT(env != NULL);
5032 if (!exp || !obd || !cluuid)
5035 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5036 req = info->mti_pill->rc_req;
5037 mdt = mdt_dev(obd->obd_lu_dev);
5039 rc = class_connect(&conn, obd, cluuid);
5043 lexp = class_conn2export(&conn);
5044 LASSERT(lexp != NULL);
5046 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5050 rc = mdt_connect_internal(lexp, mdt, data);
5054 struct mdt_thread_info *mti;
5055 mti = lu_context_key_get(&env->le_ctx,
5057 LASSERT(mti != NULL);
5058 mti->mti_exp = lexp;
5059 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5060 lexp->exp_mdt_data.med_lcd = lcd;
5061 rc = mdt_client_new(env, mdt);
5064 lexp->exp_mdt_data.med_lcd = NULL;
5066 mdt_export_stats_init(obd, lexp, localdata);
5074 class_disconnect(lexp);
5081 static int mdt_obd_reconnect(const struct lu_env *env,
5082 struct obd_export *exp, struct obd_device *obd,
5083 struct obd_uuid *cluuid,
5084 struct obd_connect_data *data,
5087 struct mdt_thread_info *info;
5088 struct mdt_device *mdt;
5089 struct ptlrpc_request *req;
5093 if (exp == NULL || obd == NULL || cluuid == NULL)
5096 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5097 req = info->mti_pill->rc_req;
5098 mdt = mdt_dev(obd->obd_lu_dev);
5100 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5104 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5106 mdt_export_stats_init(obd, exp, localdata);
5110 static int mdt_mfd_cleanup(struct obd_export *exp)
5112 struct mdt_export_data *med = &exp->exp_mdt_data;
5113 struct obd_device *obd = exp->exp_obd;
5114 struct mdt_device *mdt;
5115 struct mdt_thread_info *info;
5117 CFS_LIST_HEAD(closing_list);
5118 struct mdt_file_data *mfd, *n;
5122 spin_lock(&med->med_open_lock);
5123 while (!list_empty(&med->med_open_head)) {
5124 struct list_head *tmp = med->med_open_head.next;
5125 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5127 /* Remove mfd handle so it can't be found again.
5128 * We are consuming the mfd_list reference here. */
5129 class_handle_unhash(&mfd->mfd_handle);
5130 list_move_tail(&mfd->mfd_list, &closing_list);
5132 spin_unlock(&med->med_open_lock);
5133 mdt = mdt_dev(obd->obd_lu_dev);
5134 LASSERT(mdt != NULL);
5136 rc = lu_env_init(&env, LCT_MD_THREAD);
5140 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5141 LASSERT(info != NULL);
5142 memset(info, 0, sizeof *info);
5143 info->mti_env = &env;
5144 info->mti_mdt = mdt;
5145 info->mti_exp = exp;
5147 if (!list_empty(&closing_list)) {
5148 struct md_attr *ma = &info->mti_attr;
5152 lmm_size = mdt->mdt_max_mdsize;
5153 OBD_ALLOC(ma->ma_lmm, lmm_size);
5154 if (ma->ma_lmm == NULL)
5155 GOTO(out_lmm, rc = -ENOMEM);
5157 cookie_size = mdt->mdt_max_cookiesize;
5158 OBD_ALLOC(ma->ma_cookie, cookie_size);
5159 if (ma->ma_cookie == NULL)
5160 GOTO(out_cookie, rc = -ENOMEM);
5162 /* Close any open files (which may also cause orphan unlinking). */
5163 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5164 list_del_init(&mfd->mfd_list);
5165 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
5166 ma->ma_lmm_size = lmm_size;
5167 ma->ma_cookie_size = cookie_size;
5169 /* It is not for setattr, just tell MDD to send
5170 * DESTROY RPC to OSS if needed */
5171 ma->ma_attr_flags = MDS_CLOSE_CLEANUP;
5172 ma->ma_valid = MA_FLAGS;
5173 mdt_mfd_close(info, mfd);
5175 info->mti_mdt = NULL;
5176 OBD_FREE(ma->ma_cookie, cookie_size);
5177 ma->ma_cookie = NULL;
5179 OBD_FREE(ma->ma_lmm, lmm_size);
5188 static int mdt_obd_disconnect(struct obd_export *exp)
5190 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5195 class_export_get(exp);
5197 /* Disconnect early so that clients can't keep using export */
5198 rc = class_disconnect(exp);
5199 if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
5200 ldlm_cancel_locks_for_export(exp);
5202 /* release nid stat refererence */
5203 lprocfs_exp_cleanup(exp);
5205 /* complete all outstanding replies */
5206 spin_lock(&exp->exp_lock);
5207 while (!list_empty(&exp->exp_outstanding_replies)) {
5208 struct ptlrpc_reply_state *rs =
5209 list_entry(exp->exp_outstanding_replies.next,
5210 struct ptlrpc_reply_state, rs_exp_list);
5211 struct ptlrpc_service *svc = rs->rs_service;
5213 spin_lock(&svc->srv_lock);
5214 list_del_init(&rs->rs_exp_list);
5215 spin_lock(&rs->rs_lock);
5216 ptlrpc_schedule_difficult_reply(rs);
5217 spin_unlock(&rs->rs_lock);
5218 spin_unlock(&svc->srv_lock);
5220 spin_unlock(&exp->exp_lock);
5221 rc = mdt_mfd_cleanup(exp);
5222 class_export_put(exp);
5226 /* FIXME: Can we avoid using these two interfaces? */
5227 static int mdt_init_export(struct obd_export *exp)
5229 struct mdt_export_data *med = &exp->exp_mdt_data;
5233 CFS_INIT_LIST_HEAD(&med->med_open_head);
5234 spin_lock_init(&med->med_open_lock);
5235 sema_init(&med->med_idmap_sem, 1);
5236 med->med_idmap = NULL;
5237 spin_lock(&exp->exp_lock);
5238 exp->exp_connecting = 1;
5239 spin_unlock(&exp->exp_lock);
5240 rc = ldlm_init_export(exp);
5242 CERROR("Error %d while initializing export\n", rc);
5246 static int mdt_destroy_export(struct obd_export *export)
5248 struct mdt_export_data *med;
5249 struct obd_device *obd = export->exp_obd;
5250 struct mdt_device *mdt;
5251 struct mdt_thread_info *info;
5256 med = &export->exp_mdt_data;
5257 if (exp_connect_rmtclient(export))
5258 mdt_cleanup_idmap(med);
5260 target_destroy_export(export);
5261 ldlm_destroy_export(export);
5263 LASSERT(list_empty(&export->exp_outstanding_replies));
5264 LASSERT(list_empty(&med->med_open_head));
5265 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
5268 mdt = mdt_dev(obd->obd_lu_dev);
5269 LASSERT(mdt != NULL);
5271 rc = lu_env_init(&env, LCT_MD_THREAD);
5275 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5276 LASSERT(info != NULL);
5277 memset(info, 0, sizeof *info);
5278 info->mti_env = &env;
5279 info->mti_exp = export;
5280 info->mti_mdt = NULL;
5281 mdt_client_del(&env, mdt);
5287 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
5289 if (flag & CONFIG_LOG)
5290 m->mdt_fl_cfglog = 1;
5292 /* also notify active event */
5293 if (flag & CONFIG_SYNC)
5294 m->mdt_fl_synced = 1;
5296 if (m->mdt_fl_cfglog && m->mdt_fl_synced)
5297 /* Open for clients */
5298 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
5301 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
5302 enum md_upcall_event ev, void *data)
5304 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
5305 struct md_device *next = m->mdt_child;
5306 struct mdt_thread_info *mti;
5312 rc = next->md_ops->mdo_maxsize_get(env, next,
5314 &m->mdt_max_cookiesize);
5315 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
5316 m->mdt_max_mdsize, m->mdt_max_cookiesize);
5317 mdt_allow_cli(m, CONFIG_SYNC);
5319 (*(__u64 *)data) = m->mdt_mount_count;
5322 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5323 mti->mti_no_need_trans = 1;
5324 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
5327 /* Check that MDT is not yet configured */
5328 LASSERT(!m->mdt_fl_cfglog);
5330 #ifdef HAVE_QUOTA_SUPPORT
5332 if (md->md_lu_dev.ld_obd->obd_recovering == 0 &&
5333 likely(md->md_lu_dev.ld_obd->obd_stopping == 0))
5334 next->md_ops->mdo_quota.mqo_recovery(env, next);
5338 CERROR("invalid event\n");
5345 static int mdt_obd_notify(struct obd_device *host,
5346 struct obd_device *watched,
5347 enum obd_notify_event ev, void *data)
5349 struct mdt_device *mdt = mdt_dev(host->obd_lu_dev);
5350 #ifdef HAVE_QUOTA_SUPPORT
5351 struct md_device *next = mdt->mdt_child;
5356 case OBD_NOTIFY_CONFIG:
5357 mdt_allow_cli(mdt, (unsigned long)data);
5359 #ifdef HAVE_QUOTA_SUPPORT
5360 /* quota_type has been processed, we can now handle
5361 * incoming quota requests */
5362 next->md_ops->mdo_quota.mqo_notify(NULL, next);
5366 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
5371 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5372 void *val, int vallen)
5374 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5375 struct getinfo_fid2path *fpout, *fpin;
5378 fpin = key + size_round(sizeof(KEY_FID2PATH));
5381 if (lustre_msg_swabbed(mdt_info_req(info)->rq_reqmsg))
5382 lustre_swab_fid2path(fpin);
5384 memcpy(fpout, fpin, sizeof(*fpin));
5385 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5388 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5392 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5393 struct getinfo_fid2path *fp)
5395 struct mdt_object *obj;
5399 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5400 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5402 if (!fid_is_sane(&fp->gf_fid))
5405 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5406 if (obj == NULL || IS_ERR(obj)) {
5407 CDEBUG(D_IOCTL, "no object "DFID": %ld\n",PFID(&fp->gf_fid),
5412 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5418 mdt_object_put(env, obj);
5419 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5420 PFID(&fp->gf_fid), rc);
5424 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5425 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5426 mdt_object_put(env, obj);
5431 static int mdt_get_info(struct mdt_thread_info *info)
5433 struct ptlrpc_request *req = mdt_info_req(info);
5441 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5443 CDEBUG(D_IOCTL, "No GETINFO key");
5446 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5449 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5450 if (vallen == NULL) {
5451 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5455 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5457 rc = req_capsule_server_pack(info->mti_pill);
5458 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5459 if (valout == NULL) {
5460 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5464 if (KEY_IS(KEY_FID2PATH))
5465 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5469 lustre_msg_set_status(req->rq_repmsg, rc);
5474 /* Pass the ioc down */
5475 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5476 unsigned int cmd, int len, void *data)
5478 struct lu_context ioctl_session;
5479 struct md_device *next = mdt->mdt_child;
5483 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5486 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5487 lu_context_enter(&ioctl_session);
5488 env->le_ses = &ioctl_session;
5490 LASSERT(next->md_ops->mdo_iocontrol);
5491 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5493 lu_context_exit(&ioctl_session);
5494 lu_context_fini(&ioctl_session);
5498 /* ioctls on obd dev */
5499 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5500 void *karg, void *uarg)
5503 struct obd_device *obd = exp->exp_obd;
5504 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5505 struct dt_device *dt = mdt->mdt_bottom;
5509 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5510 rc = lu_env_init(&env, LCT_MD_THREAD);
5516 rc = mdt_device_sync(&env, mdt);
5518 case OBD_IOC_SET_READONLY:
5519 dt->dd_ops->dt_ro(&env, dt);
5521 case OBD_IOC_ABORT_RECOVERY:
5522 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5523 target_stop_recovery_thread(obd);
5526 case OBD_IOC_CHANGELOG_REG:
5527 case OBD_IOC_CHANGELOG_DEREG:
5528 case OBD_IOC_CHANGELOG_CLEAR:
5529 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5532 CERROR("Not supported cmd = %d for device %s\n",
5533 cmd, obd->obd_name);
5541 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5543 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5544 struct obd_device *obd = mdt2obd_dev(mdt);
5545 #ifdef HAVE_QUOTA_SUPPORT
5546 struct md_device *next = mdt->mdt_child;
5550 /* if some clients didn't participate in recovery then we can possibly
5551 * lost sequence. Now we should increase sequence for safe value */
5552 lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
5553 mdt_seq_adjust(env, mdt, lost);
5555 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5556 #ifdef HAVE_QUOTA_SUPPORT
5557 if (likely(obd->obd_stopping == 0))
5558 next->md_ops->mdo_quota.mqo_recovery(env, next);
5563 int mdt_obd_postrecov(struct obd_device *obd)
5568 rc = lu_env_init(&env, LCT_MD_THREAD);
5571 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5576 static struct obd_ops mdt_obd_device_ops = {
5577 .o_owner = THIS_MODULE,
5578 .o_set_info_async = mdt_obd_set_info_async,
5579 .o_connect = mdt_obd_connect,
5580 .o_reconnect = mdt_obd_reconnect,
5581 .o_disconnect = mdt_obd_disconnect,
5582 .o_init_export = mdt_init_export,
5583 .o_destroy_export = mdt_destroy_export,
5584 .o_iocontrol = mdt_iocontrol,
5585 .o_postrecov = mdt_obd_postrecov,
5586 .o_notify = mdt_obd_notify
5589 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5590 struct lu_device *d)
5592 struct mdt_device *m = mdt_dev(d);
5599 static struct lu_device *mdt_device_free(const struct lu_env *env,
5600 struct lu_device *d)
5602 struct mdt_device *m = mdt_dev(d);
5605 md_device_fini(&m->mdt_md_dev);
5610 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5611 struct lu_device_type *t,
5612 struct lustre_cfg *cfg)
5614 struct lu_device *l;
5615 struct mdt_device *m;
5621 l = &m->mdt_md_dev.md_lu_dev;
5622 rc = mdt_init0(env, m, t, cfg);
5624 mdt_device_free(env, l);
5628 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
5630 l = ERR_PTR(-ENOMEM);
5634 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5635 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
5637 /* context key: mdt_thread_key */
5638 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5640 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
5641 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
5643 struct lu_context_key mdt_txn_key = {
5644 .lct_tags = LCT_TX_HANDLE,
5645 .lct_init = mdt_txn_key_init,
5646 .lct_fini = mdt_txn_key_fini
5649 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
5651 return md_ucred(info->mti_env);
5655 * Enable/disable COS.
5657 * Set/Clear the COS flag in mdt options.
5659 * \param mdt mdt device
5660 * \param val 0 disables COS, other values enable COS
5662 void mdt_enable_cos(struct mdt_device *mdt, int val)
5667 mdt->mdt_opts.mo_cos = !!val;
5668 rc = lu_env_init(&env, LCT_MD_THREAD);
5669 if (unlikely(rc != 0)) {
5670 CWARN("lu_env initialization failed with rc = %d,"
5671 "cannot sync\n", rc);
5674 mdt_device_sync(&env, mdt);
5681 * Return COS flag status/
5683 * \param mdt mdt device
5685 int mdt_cos_is_enabled(struct mdt_device *mdt)
5687 return mdt->mdt_opts.mo_cos != 0;
5690 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
5691 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
5693 static struct lu_device_type_operations mdt_device_type_ops = {
5694 .ldto_init = mdt_type_init,
5695 .ldto_fini = mdt_type_fini,
5697 .ldto_start = mdt_type_start,
5698 .ldto_stop = mdt_type_stop,
5700 .ldto_device_alloc = mdt_device_alloc,
5701 .ldto_device_free = mdt_device_free,
5702 .ldto_device_fini = mdt_device_fini
5705 static struct lu_device_type mdt_device_type = {
5706 .ldt_tags = LU_DEVICE_MD,
5707 .ldt_name = LUSTRE_MDT_NAME,
5708 .ldt_ops = &mdt_device_type_ops,
5709 .ldt_ctx_tags = LCT_MD_THREAD
5712 static struct lu_local_obj_desc mdt_last_recv = {
5713 .llod_name = LAST_RCVD,
5714 .llod_oid = MDT_LAST_RECV_OID,
5718 static int __init mdt_mod_init(void)
5720 struct lprocfs_static_vars lvars;
5723 llo_local_obj_register(&mdt_last_recv);
5725 mdt_num_threads = MDT_NUM_THREADS;
5726 lprocfs_mdt_init_vars(&lvars);
5727 rc = class_register_type(&mdt_obd_device_ops, NULL,
5728 lvars.module_vars, LUSTRE_MDT_NAME,
5734 static void __exit mdt_mod_exit(void)
5736 llo_local_obj_unregister(&mdt_last_recv);
5737 class_unregister_type(LUSTRE_MDT_NAME);
5741 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
5742 [prefix ## _ ## opc - prefix ## _ ## base] = { \
5744 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
5745 .mh_opc = prefix ## _ ## opc, \
5746 .mh_flags = flags, \
5751 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
5752 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
5754 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
5755 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
5757 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
5758 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
5760 * Request with a format known in advance
5762 #define DEF_MDT_HNDL_F(flags, name, fn) \
5763 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
5765 #define DEF_SEQ_HNDL_F(flags, name, fn) \
5766 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
5768 #define DEF_FLD_HNDL_F(flags, name, fn) \
5769 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
5771 * Request with a format we do not yet know
5773 #define DEF_MDT_HNDL_0(flags, name, fn) \
5774 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
5776 static struct mdt_handler mdt_mds_ops[] = {
5777 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5778 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
5779 DEF_MDT_HNDL_F(0, SET_INFO, mdt_set_info),
5780 DEF_MDT_HNDL_F(0, GET_INFO, mdt_get_info),
5781 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
5782 DEF_MDT_HNDL_F(HABEO_CORPUS, GETATTR, mdt_getattr),
5783 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
5784 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
5785 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
5786 DEF_MDT_HNDL_F(0 |MUTABOR, REINT, mdt_reint),
5787 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
5788 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
5789 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
5790 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
5791 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
5792 #ifdef HAVE_QUOTA_SUPPORT
5793 DEF_MDT_HNDL_F(0, QUOTACHECK, mdt_quotacheck_handle),
5794 DEF_MDT_HNDL_F(0, QUOTACTL, mdt_quotactl_handle)
5798 #define DEF_OBD_HNDL(flags, name, fn) \
5799 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
5802 static struct mdt_handler mdt_obd_ops[] = {
5803 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
5804 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
5805 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
5808 #define DEF_DLM_HNDL_0(flags, name, fn) \
5809 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
5810 #define DEF_DLM_HNDL_F(flags, name, fn) \
5811 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
5813 static struct mdt_handler mdt_dlm_ops[] = {
5814 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
5815 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
5816 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
5817 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
5820 #define DEF_LLOG_HNDL(flags, name, fn) \
5821 DEF_HNDL(LLOG, ORIGIN_HANDLE_CREATE, _NET, flags, name, fn, NULL)
5823 static struct mdt_handler mdt_llog_ops[] = {
5824 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CREATE, mdt_llog_create),
5825 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
5826 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
5827 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_WRITE_REC, NULL),
5828 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CLOSE, NULL),
5829 DEF_LLOG_HNDL(0, ORIGIN_CONNECT, NULL),
5830 DEF_LLOG_HNDL(0, CATINFO, NULL),
5831 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
5832 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
5835 #define DEF_SEC_CTX_HNDL(name, fn) \
5836 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
5838 static struct mdt_handler mdt_sec_ctx_ops[] = {
5839 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
5840 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
5841 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
5844 static struct mdt_opc_slice mdt_regular_handlers[] = {
5846 .mos_opc_start = MDS_GETATTR,
5847 .mos_opc_end = MDS_LAST_OPC,
5848 .mos_hs = mdt_mds_ops
5851 .mos_opc_start = OBD_PING,
5852 .mos_opc_end = OBD_LAST_OPC,
5853 .mos_hs = mdt_obd_ops
5856 .mos_opc_start = LDLM_ENQUEUE,
5857 .mos_opc_end = LDLM_LAST_OPC,
5858 .mos_hs = mdt_dlm_ops
5861 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
5862 .mos_opc_end = LLOG_LAST_OPC,
5863 .mos_hs = mdt_llog_ops
5866 .mos_opc_start = SEC_CTX_INIT,
5867 .mos_opc_end = SEC_LAST_OPC,
5868 .mos_hs = mdt_sec_ctx_ops
5875 static struct mdt_handler mdt_readpage_ops[] = {
5876 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5877 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
5878 #ifdef HAVE_SPLIT_SUPPORT
5879 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
5883 * XXX: this is ugly and should be fixed one day, see mdc_close() for
5884 * detailed comments. --umka
5886 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
5887 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
5890 static struct mdt_opc_slice mdt_readpage_handlers[] = {
5892 .mos_opc_start = MDS_GETATTR,
5893 .mos_opc_end = MDS_LAST_OPC,
5894 .mos_hs = mdt_readpage_ops
5901 static struct mdt_handler mdt_xmds_ops[] = {
5902 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
5903 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
5904 DEF_MDT_HNDL_F(0 | MUTABOR , REINT, mdt_reint),
5905 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
5908 static struct mdt_opc_slice mdt_xmds_handlers[] = {
5910 .mos_opc_start = MDS_GETATTR,
5911 .mos_opc_end = MDS_LAST_OPC,
5912 .mos_hs = mdt_xmds_ops
5915 .mos_opc_start = OBD_PING,
5916 .mos_opc_end = OBD_LAST_OPC,
5917 .mos_hs = mdt_obd_ops
5920 .mos_opc_start = SEC_CTX_INIT,
5921 .mos_opc_end = SEC_LAST_OPC,
5922 .mos_hs = mdt_sec_ctx_ops
5929 static struct mdt_handler mdt_seq_ops[] = {
5930 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
5933 static struct mdt_opc_slice mdt_seq_handlers[] = {
5935 .mos_opc_start = SEQ_QUERY,
5936 .mos_opc_end = SEQ_LAST_OPC,
5937 .mos_hs = mdt_seq_ops
5944 static struct mdt_handler mdt_fld_ops[] = {
5945 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
5948 static struct mdt_opc_slice mdt_fld_handlers[] = {
5950 .mos_opc_start = FLD_QUERY,
5951 .mos_opc_end = FLD_LAST_OPC,
5952 .mos_hs = mdt_fld_ops
5959 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5960 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
5961 MODULE_LICENSE("GPL");
5963 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
5964 "number of mdt service threads to start");
5966 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);