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 #include <lustre_acl.h>
71 #include <lustre_param.h>
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 ldlm_mode_t mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
96 * Initialized in mdt_mod_init().
98 unsigned long mdt_num_threads;
100 /* ptlrpc request handler for MDT. All handlers are
101 * grouped into several slices - struct mdt_opc_slice,
102 * and stored in an array - mdt_handlers[].
105 /* The name of this handler. */
107 /* Fail id for this handler, checked at the beginning of this handler*/
109 /* Operation code for this handler */
111 /* flags are listed in enum mdt_handler_flags below. */
113 /* The actual handler function to execute. */
114 int (*mh_act)(struct mdt_thread_info *info);
115 /* Request format for this request. */
116 const struct req_format *mh_fmt;
119 enum mdt_handler_flags {
121 * struct mdt_body is passed in the incoming message, and object
122 * identified by this fid exists on disk.
124 * "habeo corpus" == "I have a body"
126 HABEO_CORPUS = (1 << 0),
128 * struct ldlm_request is passed in the incoming message.
130 * "habeo clavis" == "I have a key"
132 HABEO_CLAVIS = (1 << 1),
134 * this request has fixed reply format, so that reply message can be
135 * packed by generic code.
137 * "habeo refero" == "I have a reply"
139 HABEO_REFERO = (1 << 2),
141 * this request will modify something, so check whether the filesystem
142 * is readonly or not, then return -EROFS to client asap if necessary.
144 * "mutabor" == "I shall modify"
149 struct mdt_opc_slice {
152 struct mdt_handler *mos_hs;
155 static struct mdt_opc_slice mdt_regular_handlers[];
156 static struct mdt_opc_slice mdt_readpage_handlers[];
157 static struct mdt_opc_slice mdt_xmds_handlers[];
158 static struct mdt_opc_slice mdt_seq_handlers[];
159 static struct mdt_opc_slice mdt_fld_handlers[];
161 static struct mdt_device *mdt_dev(struct lu_device *d);
162 static int mdt_regular_handle(struct ptlrpc_request *req);
163 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
165 static struct lu_object_operations mdt_obj_ops;
167 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
171 return (rep->lock_policy_res1 & flag);
174 void mdt_clear_disposition(struct mdt_thread_info *info,
175 struct ldlm_reply *rep, int flag)
178 info->mti_opdata &= ~flag;
180 rep->lock_policy_res1 &= ~flag;
183 void mdt_set_disposition(struct mdt_thread_info *info,
184 struct ldlm_reply *rep, int flag)
187 info->mti_opdata |= flag;
189 rep->lock_policy_res1 |= flag;
192 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
194 lh->mlh_pdo_hash = 0;
195 lh->mlh_reg_mode = lm;
196 lh->mlh_type = MDT_REG_LOCK;
199 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
200 const char *name, int namelen)
202 lh->mlh_reg_mode = lm;
203 lh->mlh_type = MDT_PDO_LOCK;
206 LASSERT(namelen > 0);
207 lh->mlh_pdo_hash = full_name_hash(name, namelen);
209 LASSERT(namelen == 0);
210 lh->mlh_pdo_hash = 0ull;
214 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
215 struct mdt_lock_handle *lh)
221 * Any dir access needs couple of locks:
223 * 1) on part of dir we gonna take lookup/modify;
225 * 2) on whole dir to protect it from concurrent splitting and/or to
226 * flush client's cache for readdir().
228 * so, for a given mode and object this routine decides what lock mode
229 * to use for lock #2:
231 * 1) if caller's gonna lookup in dir then we need to protect dir from
232 * being splitted only - LCK_CR
234 * 2) if caller's gonna modify dir then we need to protect dir from
235 * being splitted and to flush cache - LCK_CW
237 * 3) if caller's gonna modify dir and that dir seems ready for
238 * splitting then we need to protect it from any type of access
239 * (lookup/modify/split) - LCK_EX --bzzz
242 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
243 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
246 * Ask underlaying level its opinion about preferable PDO lock mode
247 * having access type passed as regular lock mode:
249 * - MDL_MINMODE means that lower layer does not want to specify lock
252 * - MDL_NL means that no PDO lock should be taken. This is used in some
253 * cases. Say, for non-splittable directories no need to use PDO locks
256 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
257 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
259 if (mode != MDL_MINMODE) {
260 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
263 * Lower layer does not want to specify locking mode. We do it
264 * our selves. No special protection is needed, just flush
265 * client's cache on modification and allow concurrent
268 switch (lh->mlh_reg_mode) {
270 lh->mlh_pdo_mode = LCK_EX;
273 lh->mlh_pdo_mode = LCK_CR;
276 lh->mlh_pdo_mode = LCK_CW;
279 CERROR("Not expected lock type (0x%x)\n",
280 (int)lh->mlh_reg_mode);
285 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
289 static int mdt_getstatus(struct mdt_thread_info *info)
291 struct mdt_device *mdt = info->mti_mdt;
292 struct md_device *next = mdt->mdt_child;
293 struct mdt_body *repbody;
298 rc = mdt_check_ucred(info);
300 RETURN(err_serious(rc));
302 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
303 RETURN(err_serious(-ENOMEM));
305 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
306 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
310 repbody->valid |= OBD_MD_FLID;
312 if (mdt->mdt_opts.mo_mds_capa) {
313 struct mdt_object *root;
314 struct lustre_capa *capa;
316 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
318 RETURN(PTR_ERR(root));
320 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
322 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
324 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
326 mdt_object_put(info->mti_env, root);
328 repbody->valid |= OBD_MD_FLMDSCAPA;
334 static int mdt_statfs(struct mdt_thread_info *info)
336 struct md_device *next = info->mti_mdt->mdt_child;
337 struct ptlrpc_service *svc;
338 struct obd_statfs *osfs;
343 svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
345 /* This will trigger a watchdog timeout */
346 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
347 (MDT_SERVICE_WATCHDOG_FACTOR *
348 at_get(&svc->srv_at_estimate) / 1000) + 1);
350 rc = mdt_check_ucred(info);
352 RETURN(err_serious(rc));
354 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
355 rc = err_serious(-ENOMEM);
357 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
358 rc = next->md_ops->mdo_statfs(info->mti_env, next,
360 statfs_pack(osfs, &info->mti_u.ksfs);
365 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
368 struct lu_attr *attr = &info->mti_attr.ma_attr;
370 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
372 /* Check if Size-on-MDS is enabled. */
373 if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
374 S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
375 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
376 b->size = attr->la_size;
377 b->blocks = attr->la_blocks;
381 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
382 const struct lu_attr *attr, const struct lu_fid *fid)
384 /*XXX should pack the reply body according to lu_valid*/
385 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
386 OBD_MD_FLGID | OBD_MD_FLTYPE |
387 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
388 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
390 if (!S_ISREG(attr->la_mode))
391 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
393 b->atime = attr->la_atime;
394 b->mtime = attr->la_mtime;
395 b->ctime = attr->la_ctime;
396 b->mode = attr->la_mode;
397 b->size = attr->la_size;
398 b->blocks = attr->la_blocks;
399 b->uid = attr->la_uid;
400 b->gid = attr->la_gid;
401 b->flags = attr->la_flags;
402 b->nlink = attr->la_nlink;
403 b->rdev = attr->la_rdev;
407 b->valid |= OBD_MD_FLID;
409 /* FIXME: these should be fixed when new igif ready.*/
410 b->ino = fid_oid(fid); /* 1.6 compatibility */
411 b->generation = fid_ver(fid); /* 1.6 compatibility */
412 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
414 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
415 PFID(fid), b->nlink, b->mode, b->size);
419 mdt_body_reverse_idmap(info, b);
422 static inline int mdt_body_has_lov(const struct lu_attr *la,
423 const struct mdt_body *body)
425 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
426 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
429 static int mdt_getattr_internal(struct mdt_thread_info *info,
430 struct mdt_object *o)
432 struct md_object *next = mdt_object_child(o);
433 const struct mdt_body *reqbody = info->mti_body;
434 struct ptlrpc_request *req = mdt_info_req(info);
435 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
436 struct md_attr *ma = &info->mti_attr;
437 struct lu_attr *la = &ma->ma_attr;
438 struct req_capsule *pill = info->mti_pill;
439 const struct lu_env *env = info->mti_env;
440 struct mdt_body *repbody;
441 struct lu_buf *buffer = &info->mti_buf;
445 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
446 RETURN(err_serious(-ENOMEM));
448 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
452 rc = mdt_object_exists(o);
454 /* This object is located on remote node.*/
455 repbody->fid1 = *mdt_object_fid(o);
456 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
460 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
461 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
463 /* If it is dir object and client require MEA, then we got MEA */
464 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
465 reqbody->valid & OBD_MD_MEA) {
466 /* Assumption: MDT_MD size is enough for lmv size. */
467 ma->ma_lmv = buffer->lb_buf;
468 ma->ma_lmv_size = buffer->lb_len;
469 ma->ma_need = MA_LMV | MA_INODE;
471 ma->ma_lmm = buffer->lb_buf;
472 ma->ma_lmm_size = buffer->lb_len;
473 ma->ma_need = MA_LOV | MA_INODE;
476 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
477 reqbody->valid & OBD_MD_FLDIREA &&
478 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
479 /* get default stripe info for this dir. */
480 ma->ma_need |= MA_LOV_DEF;
482 rc = mo_attr_get(env, next, ma);
484 CERROR("getattr error for "DFID": %d\n",
485 PFID(mdt_object_fid(o)), rc);
489 if (likely(ma->ma_valid & MA_INODE))
490 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
494 if (mdt_body_has_lov(la, reqbody)) {
495 if (ma->ma_valid & MA_LOV) {
496 LASSERT(ma->ma_lmm_size);
497 mdt_dump_lmm(D_INFO, ma->ma_lmm);
498 repbody->eadatasize = ma->ma_lmm_size;
499 if (S_ISDIR(la->la_mode))
500 repbody->valid |= OBD_MD_FLDIREA;
502 repbody->valid |= OBD_MD_FLEASIZE;
504 if (ma->ma_valid & MA_LMV) {
505 LASSERT(S_ISDIR(la->la_mode));
506 repbody->eadatasize = ma->ma_lmv_size;
507 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
509 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
510 repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
512 } else if (S_ISLNK(la->la_mode) &&
513 reqbody->valid & OBD_MD_LINKNAME) {
514 buffer->lb_buf = ma->ma_lmm;
515 buffer->lb_len = reqbody->eadatasize;
516 rc = mo_readlink(env, next, buffer);
517 if (unlikely(rc <= 0)) {
518 CERROR("readlink failed: %d\n", rc);
521 repbody->valid |= OBD_MD_LINKNAME;
522 repbody->eadatasize = rc;
524 ((char*)ma->ma_lmm)[rc - 1] = 0;
525 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
526 (char*)ma->ma_lmm, rc);
531 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
532 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
533 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
534 repbody->valid |= OBD_MD_FLMODEASIZE;
535 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
536 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
537 repbody->max_cookiesize);
540 if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
541 void *buf = req_capsule_server_get(pill, &RMF_ACL);
543 /* mdt_getattr_lock only */
544 rc = mdt_pack_remote_perm(info, o, buf);
546 repbody->valid &= ~OBD_MD_FLRMTPERM;
547 repbody->aclsize = 0;
550 repbody->valid |= OBD_MD_FLRMTPERM;
551 repbody->aclsize = sizeof(struct mdt_remote_perm);
554 #ifdef CONFIG_FS_POSIX_ACL
555 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
556 (reqbody->valid & OBD_MD_FLACL)) {
557 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
558 buffer->lb_len = req_capsule_get_size(pill,
559 &RMF_ACL, RCL_SERVER);
560 if (buffer->lb_len > 0) {
561 rc = mo_xattr_get(env, next, buffer,
562 XATTR_NAME_ACL_ACCESS);
564 if (rc == -ENODATA) {
565 repbody->aclsize = 0;
566 repbody->valid |= OBD_MD_FLACL;
568 } else if (rc == -EOPNOTSUPP) {
571 CERROR("got acl size: %d\n", rc);
574 repbody->aclsize = rc;
575 repbody->valid |= OBD_MD_FLACL;
582 if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
583 info->mti_mdt->mdt_opts.mo_mds_capa) {
584 struct lustre_capa *capa;
586 capa = req_capsule_server_get(pill, &RMF_CAPA1);
588 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
589 rc = mo_capa_get(env, next, capa, 0);
592 repbody->valid |= OBD_MD_FLMDSCAPA;
597 static int mdt_renew_capa(struct mdt_thread_info *info)
599 struct mdt_device *mdt = info->mti_mdt;
600 struct mdt_object *obj = info->mti_object;
601 struct mdt_body *body;
602 struct lustre_capa *capa, *c;
606 /* if object doesn't exist, or server has disabled capability,
607 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
610 if (!obj || !mdt->mdt_opts.mo_mds_capa)
613 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
614 LASSERT(body != NULL);
616 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
619 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
623 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
625 body->valid |= OBD_MD_FLOSSCAPA;
629 static int mdt_getattr(struct mdt_thread_info *info)
631 struct mdt_object *obj = info->mti_object;
632 struct req_capsule *pill = info->mti_pill;
633 struct mdt_body *reqbody;
634 struct mdt_body *repbody;
640 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
643 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
644 rc = req_capsule_server_pack(pill);
646 RETURN(err_serious(rc));
647 rc = mdt_renew_capa(info);
648 GOTO(out_shrink, rc);
651 LASSERT(obj != NULL);
652 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
654 mode = lu_object_attr(&obj->mot_obj.mo_lu);
655 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
656 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
657 md_size = reqbody->eadatasize;
659 md_size = info->mti_mdt->mdt_max_mdsize;
661 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
663 rc = req_capsule_server_pack(pill);
664 if (unlikely(rc != 0))
665 RETURN(err_serious(rc));
667 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
668 LASSERT(repbody != NULL);
669 repbody->eadatasize = 0;
670 repbody->aclsize = 0;
672 if (reqbody->valid & OBD_MD_FLRMTPERM)
673 rc = mdt_init_ucred(info, reqbody);
675 rc = mdt_check_ucred(info);
677 GOTO(out_shrink, rc);
679 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
680 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
683 * Don't check capability at all, because rename might getattr for
684 * remote obj, and at that time no capability is available.
686 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
687 rc = mdt_getattr_internal(info, obj);
688 if (reqbody->valid & OBD_MD_FLRMTPERM)
689 mdt_exit_ucred(info);
692 mdt_shrink_reply(info);
696 static int mdt_is_subdir(struct mdt_thread_info *info)
698 struct mdt_object *o = info->mti_object;
699 struct req_capsule *pill = info->mti_pill;
700 const struct mdt_body *body = info->mti_body;
701 struct mdt_body *repbody;
707 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
710 * We save last checked parent fid to @repbody->fid1 for remote
713 LASSERT(fid_is_sane(&body->fid2));
714 LASSERT(mdt_object_exists(o) > 0);
715 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
716 &body->fid2, &repbody->fid1);
717 if (rc == 0 || rc == -EREMOTE)
718 repbody->valid |= OBD_MD_FLID;
723 static int mdt_raw_lookup(struct mdt_thread_info *info,
724 struct mdt_object *parent,
725 const struct lu_name *lname,
726 struct ldlm_reply *ldlm_rep)
728 struct md_object *next = mdt_object_child(info->mti_object);
729 const struct mdt_body *reqbody = info->mti_body;
730 struct lu_fid *child_fid = &info->mti_tmp_fid1;
731 struct mdt_body *repbody;
735 if (reqbody->valid != OBD_MD_FLID)
738 LASSERT(!info->mti_cross_ref);
740 /* Only got the fid of this obj by name */
741 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
744 /* XXX is raw_lookup possible as intent operation? */
747 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
750 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
752 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
755 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
756 repbody->fid1 = *child_fid;
757 repbody->valid = OBD_MD_FLID;
763 * UPDATE lock should be taken against parent, and be release before exit;
764 * child_bits lock should be taken against child, and be returned back:
765 * (1)normal request should release the child lock;
766 * (2)intent request will grant the lock to client.
768 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
769 struct mdt_lock_handle *lhc,
771 struct ldlm_reply *ldlm_rep)
773 struct ptlrpc_request *req = mdt_info_req(info);
774 struct mdt_body *reqbody = NULL;
775 struct mdt_object *parent = info->mti_object;
776 struct mdt_object *child;
777 struct md_object *next = mdt_object_child(parent);
778 struct lu_fid *child_fid = &info->mti_tmp_fid1;
779 struct lu_name *lname = NULL;
782 struct mdt_lock_handle *lhp;
783 struct ldlm_lock *lock;
784 struct ldlm_res_id *res_id;
790 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
791 LASSERT(ergo(is_resent,
792 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
794 LASSERT(parent != NULL);
795 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
797 RETURN(err_serious(-EFAULT));
799 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
801 LASSERT(namelen >= 0);
803 /* XXX: "namelen == 0" is for getattr by fid (OBD_CONNECT_ATTRFID),
804 * otherwise do not allow empty name, that is the name must contain
805 * at least one character and the terminating '\0'*/
807 reqbody =req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
808 LASSERT(fid_is_sane(&reqbody->fid2));
811 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
813 PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
816 lname = mdt_name(info->mti_env, (char *)name, namelen);
817 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
819 PFID(mdt_object_fid(parent)), name, ldlm_rep);
822 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
824 rc = mdt_object_exists(parent);
825 if (unlikely(rc == 0)) {
826 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
827 &parent->mot_obj.mo_lu,
828 "Parent doesn't exist!\n");
831 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
832 PFID(mdt_object_fid(parent)));
835 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
843 if (info->mti_cross_ref) {
844 /* Only getattr on the child. Parent is on another node. */
845 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
847 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
848 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
851 /* Do not take lock for resent case. */
852 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
853 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
854 lhc->mlh_reg_lh.cookie);
855 LASSERT(fid_res_name_eq(mdt_object_fid(child),
856 &lock->l_resource->lr_name));
860 mdt_lock_handle_init(lhc);
861 mdt_lock_reg_init(lhc, LCK_PR);
864 * Object's name is on another MDS, no lookup lock is
865 * needed here but update is.
867 child_bits &= ~MDS_INODELOCK_LOOKUP;
868 child_bits |= MDS_INODELOCK_UPDATE;
870 rc = mdt_object_lock(info, child, lhc, child_bits,
874 /* Finally, we can get attr for child. */
875 mdt_set_capainfo(info, 0, mdt_object_fid(child),
877 rc = mdt_getattr_internal(info, child);
878 if (unlikely(rc != 0))
879 mdt_object_unlock(info, child, lhc, 1);
884 /* step 1: lock parent */
885 lhp = &info->mti_lh[MDT_LH_PARENT];
886 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
887 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
890 if (unlikely(rc != 0))
894 /* step 2: lookup child's fid by name */
895 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
900 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
901 GOTO(out_parent, rc);
903 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
905 *child_fid = reqbody->fid2;
906 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
910 *step 3: find the child object by fid & lock it.
911 * regardless if it is local or remote.
913 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
915 if (unlikely(IS_ERR(child)))
916 GOTO(out_parent, rc = PTR_ERR(child));
918 /* Do not take lock for resent case. */
919 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
920 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
921 lhc->mlh_reg_lh.cookie);
923 res_id = &lock->l_resource->lr_name;
924 if (!fid_res_name_eq(mdt_object_fid(child),
925 &lock->l_resource->lr_name)) {
926 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
927 &lock->l_resource->lr_name),
928 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
929 (unsigned long)res_id->name[0],
930 (unsigned long)res_id->name[1],
931 (unsigned long)res_id->name[2],
932 PFID(mdt_object_fid(parent)));
933 CWARN("Although resent, but still not get child lock"
934 "parent:"DFID" child:"DFID"\n",
935 PFID(mdt_object_fid(parent)),
936 PFID(mdt_object_fid(child)));
937 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
946 ma = &info->mti_attr;
948 mdt_lock_handle_init(lhc);
949 mdt_lock_reg_init(lhc, LCK_PR);
951 if (mdt_object_exists(child) == 0) {
952 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
953 &child->mot_obj.mo_lu,
954 "Object doesn't exist!\n");
955 GOTO(out_child, rc = -ESTALE);
959 ma->ma_need = MA_INODE;
960 rc = mo_attr_get(info->mti_env, next, ma);
961 if (unlikely(rc != 0))
964 /* If the file has not been changed for some time, we return
965 * not only a LOOKUP lock, but also an UPDATE lock and this
966 * might save us RPC on later STAT. For directories, it also
967 * let negative dentry starts working for this dir. */
968 if (ma->ma_valid & MA_INODE &&
969 ma->ma_attr.la_valid & LA_CTIME &&
970 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
971 ma->ma_attr.la_ctime < cfs_time_current_sec())
972 child_bits |= MDS_INODELOCK_UPDATE;
974 rc = mdt_object_lock(info, child, lhc, child_bits,
977 if (unlikely(rc != 0))
981 /* finally, we can get attr for child. */
982 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
983 rc = mdt_getattr_internal(info, child);
984 if (unlikely(rc != 0)) {
985 mdt_object_unlock(info, child, lhc, 1);
987 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
989 struct mdt_body *repbody;
991 /* Debugging code. */
992 res_id = &lock->l_resource->lr_name;
993 LDLM_DEBUG(lock, "Returning lock to client\n");
994 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
995 &lock->l_resource->lr_name),
996 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
997 (unsigned long)res_id->name[0],
998 (unsigned long)res_id->name[1],
999 (unsigned long)res_id->name[2],
1000 PFID(mdt_object_fid(child)));
1002 * Pack Size-on-MDS inode attributes to the body if
1003 * update lock is given.
1005 repbody = req_capsule_server_get(info->mti_pill,
1007 if (lock->l_policy_data.l_inodebits.bits &
1008 MDS_INODELOCK_UPDATE)
1009 mdt_pack_size2body(info, child);
1010 LDLM_LOCK_PUT(lock);
1015 mdt_object_put(info->mti_env, child);
1017 mdt_object_unlock(info, parent, lhp, 1);
1021 /* normal handler: should release the child lock */
1022 static int mdt_getattr_name(struct mdt_thread_info *info)
1024 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1025 struct mdt_body *reqbody;
1026 struct mdt_body *repbody;
1030 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1031 LASSERT(reqbody != NULL);
1032 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1033 LASSERT(repbody != NULL);
1035 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1036 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1037 repbody->eadatasize = 0;
1038 repbody->aclsize = 0;
1040 rc = mdt_init_ucred(info, reqbody);
1042 GOTO(out_shrink, rc);
1044 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1045 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1046 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1047 lhc->mlh_reg_lh.cookie = 0;
1049 mdt_exit_ucred(info);
1052 mdt_shrink_reply(info);
1056 static struct lu_device_operations mdt_lu_ops;
1058 static int lu_device_is_mdt(struct lu_device *d)
1060 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1063 static int mdt_set_info(struct mdt_thread_info *info)
1065 struct ptlrpc_request *req = mdt_info_req(info);
1071 rc = req_capsule_server_pack(info->mti_pill);
1075 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1077 DEBUG_REQ(D_HA, req, "no set_info key");
1081 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1084 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1086 DEBUG_REQ(D_HA, req, "no set_info val");
1090 if (!KEY_IS(KEY_READ_ONLY))
1094 lustre_msg_set_status(req->rq_repmsg, 0);
1096 spin_lock(&req->rq_export->exp_lock);
1098 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1100 req->rq_export->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1101 spin_unlock(&req->rq_export->exp_lock);
1106 static int mdt_connect(struct mdt_thread_info *info)
1109 struct ptlrpc_request *req;
1111 req = mdt_info_req(info);
1112 rc = target_handle_connect(req);
1114 LASSERT(req->rq_export != NULL);
1115 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1116 rc = mdt_init_idmap(info);
1118 /* if mdt_init_idmap failed, revocation for connect */
1119 obd_disconnect(class_export_get(req->rq_export));
1121 rc = err_serious(rc);
1125 static int mdt_disconnect(struct mdt_thread_info *info)
1130 rc = target_handle_disconnect(mdt_info_req(info));
1132 rc = err_serious(rc);
1136 static int mdt_sendpage(struct mdt_thread_info *info,
1137 struct lu_rdpg *rdpg)
1139 struct ptlrpc_request *req = mdt_info_req(info);
1140 struct ptlrpc_bulk_desc *desc;
1141 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1149 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1154 for (i = 0, tmpcount = rdpg->rp_count;
1155 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1156 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1157 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1160 LASSERT(desc->bd_nob == rdpg->rp_count);
1161 rc = ptlrpc_start_bulk_transfer(desc);
1163 GOTO(free_desc, rc);
1165 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1166 GOTO(abort_bulk, rc = 0);
1168 timeout = (int) req->rq_deadline - cfs_time_current_sec();
1170 CERROR("Req deadline already passed %lu (now: %lu)\n",
1171 req->rq_deadline, cfs_time_current_sec());
1172 *lwi = LWI_TIMEOUT(max(timeout, 1) * HZ, NULL, NULL);
1173 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1174 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1177 if (desc->bd_success &&
1178 desc->bd_nob_transferred == rdpg->rp_count)
1179 GOTO(free_desc, rc);
1181 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1184 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1185 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1186 desc->bd_nob_transferred, rdpg->rp_count,
1187 req->rq_export->exp_client_uuid.uuid,
1188 req->rq_export->exp_connection->c_remote_uuid.uuid);
1190 class_fail_export(req->rq_export);
1194 ptlrpc_abort_bulk(desc);
1196 ptlrpc_free_bulk(desc);
1200 #ifdef HAVE_SPLIT_SUPPORT
1202 * Retrieve dir entry from the page and insert it to the slave object, actually,
1203 * this should be in osd layer, but since it will not in the final product, so
1204 * just do it here and do not define more moo api anymore for this.
1206 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1209 struct mdt_object *object = info->mti_object;
1210 struct lu_fid *lf = &info->mti_tmp_fid2;
1211 struct md_attr *ma = &info->mti_attr;
1212 struct lu_dirpage *dp;
1213 struct lu_dirent *ent;
1214 int rc = 0, offset = 0;
1217 /* Make sure we have at least one entry. */
1222 * Disable trans for this name insert, since it will include many trans
1225 info->mti_no_need_trans = 1;
1227 * When write_dir_page, no need update parent's ctime,
1228 * and no permission check for name_insert.
1230 ma->ma_attr.la_ctime = 0;
1231 ma->ma_attr.la_valid = LA_MODE;
1232 ma->ma_valid = MA_INODE;
1235 dp = page_address(page);
1236 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1238 for (ent = lu_dirent_start(dp); ent != NULL;
1239 ent = lu_dirent_next(ent)) {
1240 struct lu_name *lname;
1243 if (le16_to_cpu(ent->lde_namelen) == 0)
1246 fid_le_to_cpu(lf, &ent->lde_fid);
1247 if (le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1248 ma->ma_attr.la_mode = S_IFDIR;
1250 ma->ma_attr.la_mode = 0;
1251 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1253 GOTO(out, rc = -ENOMEM);
1255 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1256 lname = mdt_name(info->mti_env, name,
1257 le16_to_cpu(ent->lde_namelen) + 1);
1258 ma->ma_attr_flags |= MDS_PERM_BYPASS;
1259 rc = mdo_name_insert(info->mti_env,
1260 md_object_next(&object->mot_obj),
1262 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1264 CERROR("Can't insert %*.*s, rc %d\n",
1265 le16_to_cpu(ent->lde_namelen),
1266 le16_to_cpu(ent->lde_namelen),
1271 offset += lu_dirent_size(ent);
1281 static int mdt_bulk_timeout(void *data)
1285 CERROR("mdt bulk transfer timeout \n");
1290 static int mdt_writepage(struct mdt_thread_info *info)
1292 struct ptlrpc_request *req = mdt_info_req(info);
1293 struct mdt_body *reqbody;
1294 struct l_wait_info *lwi;
1295 struct ptlrpc_bulk_desc *desc;
1301 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1302 if (reqbody == NULL)
1303 RETURN(err_serious(-EFAULT));
1305 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1307 RETURN(err_serious(-ENOMEM));
1309 /* allocate the page for the desc */
1310 page = cfs_alloc_page(CFS_ALLOC_STD);
1312 GOTO(desc_cleanup, rc = -ENOMEM);
1314 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1315 (int)reqbody->size, (int)reqbody->nlink);
1317 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1318 (int)reqbody->nlink);
1321 * Check if client was evicted while we were doing i/o before touching
1326 GOTO(cleanup_page, rc = -ENOMEM);
1328 if (desc->bd_export->exp_failed)
1331 rc = ptlrpc_start_bulk_transfer (desc);
1333 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1334 mdt_bulk_timeout, desc);
1335 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1336 desc->bd_export->exp_failed, lwi);
1337 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1338 if (rc == -ETIMEDOUT) {
1339 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1340 ptlrpc_abort_bulk(desc);
1341 } else if (desc->bd_export->exp_failed) {
1342 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1344 ptlrpc_abort_bulk(desc);
1345 } else if (!desc->bd_success ||
1346 desc->bd_nob_transferred != desc->bd_nob) {
1347 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1349 "truncated" : "network error on",
1350 desc->bd_nob_transferred, desc->bd_nob);
1351 /* XXX should this be a different errno? */
1355 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1358 GOTO(cleanup_lwi, rc);
1359 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1364 cfs_free_page(page);
1366 ptlrpc_free_bulk(desc);
1371 static int mdt_readpage(struct mdt_thread_info *info)
1373 struct mdt_object *object = info->mti_object;
1374 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1375 struct mdt_body *reqbody;
1376 struct mdt_body *repbody;
1381 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1382 RETURN(err_serious(-ENOMEM));
1384 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1385 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1386 if (reqbody == NULL || repbody == NULL)
1387 RETURN(err_serious(-EFAULT));
1390 * prepare @rdpg before calling lower layers and transfer itself. Here
1391 * reqbody->size contains offset of where to start to read and
1392 * reqbody->nlink contains number bytes to read.
1394 rdpg->rp_hash = reqbody->size;
1395 if ((__u64)rdpg->rp_hash != reqbody->size) {
1396 CERROR("Invalid hash: %#llx != %#llx\n",
1397 (__u64)rdpg->rp_hash, reqbody->size);
1400 rdpg->rp_count = reqbody->nlink;
1401 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1402 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1403 if (rdpg->rp_pages == NULL)
1406 for (i = 0; i < rdpg->rp_npages; ++i) {
1407 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1408 if (rdpg->rp_pages[i] == NULL)
1409 GOTO(free_rdpg, rc = -ENOMEM);
1412 /* call lower layers to fill allocated pages with directory data */
1413 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1415 GOTO(free_rdpg, rc);
1417 /* send pages to client */
1418 rc = mdt_sendpage(info, rdpg);
1423 for (i = 0; i < rdpg->rp_npages; i++)
1424 if (rdpg->rp_pages[i] != NULL)
1425 cfs_free_page(rdpg->rp_pages[i]);
1426 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1428 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1434 static int mdt_reint_internal(struct mdt_thread_info *info,
1435 struct mdt_lock_handle *lhc,
1438 struct req_capsule *pill = info->mti_pill;
1439 struct mdt_device *mdt = info->mti_mdt;
1440 struct mdt_body *repbody;
1445 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1446 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1447 mdt->mdt_max_mdsize);
1448 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1449 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1450 mdt->mdt_max_cookiesize);
1452 rc = req_capsule_server_pack(pill);
1454 CERROR("Can't pack response, rc %d\n", rc);
1455 RETURN(err_serious(rc));
1458 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1459 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1461 repbody->eadatasize = 0;
1462 repbody->aclsize = 0;
1465 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1466 GOTO(out_shrink, rc = err_serious(-EFAULT));
1468 rc = mdt_reint_unpack(info, op);
1470 CERROR("Can't unpack reint, rc %d\n", rc);
1471 GOTO(out_shrink, rc = err_serious(rc));
1474 rc = mdt_init_ucred_reint(info);
1476 GOTO(out_shrink, rc);
1478 rc = mdt_fix_attr_ucred(info, op);
1480 GOTO(out_ucred, rc = err_serious(rc));
1482 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1483 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1484 GOTO(out_ucred, rc);
1486 rc = mdt_reint_rec(info, lhc);
1489 mdt_exit_ucred(info);
1491 mdt_shrink_reply(info);
1495 static long mdt_reint_opcode(struct mdt_thread_info *info,
1496 const struct req_format **fmt)
1498 struct mdt_rec_reint *rec;
1501 opc = err_serious(-EFAULT);
1502 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1504 opc = rec->rr_opcode;
1505 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1506 if (opc < REINT_MAX && fmt[opc] != NULL)
1507 req_capsule_extend(info->mti_pill, fmt[opc]);
1509 CERROR("Unsupported opc: %ld\n", opc);
1510 opc = err_serious(opc);
1516 static int mdt_reint(struct mdt_thread_info *info)
1521 static const struct req_format *reint_fmts[REINT_MAX] = {
1522 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1523 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1524 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1525 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1526 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1527 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1528 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1533 opc = mdt_reint_opcode(info, reint_fmts);
1536 * No lock possible here from client to pass it to reint code
1539 rc = mdt_reint_internal(info, NULL, opc);
1544 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1548 /* this should sync the whole device */
1549 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1551 struct dt_device *dt = mdt->mdt_bottom;
1555 rc = dt->dd_ops->dt_sync(env, dt);
1559 /* this should sync this object */
1560 static int mdt_object_sync(struct mdt_thread_info *info)
1562 struct md_object *next;
1566 if (!mdt_object_exists(info->mti_object)) {
1567 CWARN("Non existing object "DFID"!\n",
1568 PFID(mdt_object_fid(info->mti_object)));
1571 next = mdt_object_child(info->mti_object);
1572 rc = mo_object_sync(info->mti_env, next);
1577 static int mdt_sync(struct mdt_thread_info *info)
1579 struct req_capsule *pill = info->mti_pill;
1580 struct mdt_body *body;
1584 /* The fid may be zero, so we req_capsule_set manually */
1585 req_capsule_set(pill, &RQF_MDS_SYNC);
1587 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1589 RETURN(err_serious(-EINVAL));
1591 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1592 RETURN(err_serious(-ENOMEM));
1594 if (fid_seq(&body->fid1) == 0) {
1595 /* sync the whole device */
1596 rc = req_capsule_server_pack(pill);
1598 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1600 rc = err_serious(rc);
1602 /* sync an object */
1603 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1605 rc = mdt_object_sync(info);
1607 struct md_object *next;
1608 const struct lu_fid *fid;
1609 struct lu_attr *la = &info->mti_attr.ma_attr;
1611 next = mdt_object_child(info->mti_object);
1612 info->mti_attr.ma_need = MA_INODE;
1613 info->mti_attr.ma_valid = 0;
1614 rc = mo_attr_get(info->mti_env, next,
1617 body = req_capsule_server_get(pill,
1619 fid = mdt_object_fid(info->mti_object);
1620 mdt_pack_attr2body(info, body, la, fid);
1624 rc = err_serious(rc);
1629 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1631 return err_serious(-EOPNOTSUPP);
1634 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1636 return err_serious(-EOPNOTSUPP);
1640 * OBD PING and other handlers.
1642 static int mdt_obd_ping(struct mdt_thread_info *info)
1647 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1649 rc = target_handle_ping(mdt_info_req(info));
1651 rc = err_serious(rc);
1655 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1657 return err_serious(-EOPNOTSUPP);
1660 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1662 return err_serious(-EOPNOTSUPP);
1669 static struct ldlm_callback_suite cbs = {
1670 .lcs_completion = ldlm_server_completion_ast,
1671 .lcs_blocking = ldlm_server_blocking_ast,
1675 static int mdt_enqueue(struct mdt_thread_info *info)
1677 struct ptlrpc_request *req;
1682 * info->mti_dlm_req already contains swapped and (if necessary)
1683 * converted dlm request.
1685 LASSERT(info->mti_dlm_req != NULL);
1687 req = mdt_info_req(info);
1690 * Lock without inodebits makes no sense and will oops later in
1691 * ldlm. Let's check it now to see if we have wrong lock from client or
1692 * bits get corrupted somewhere in mdt_intent_policy().
1694 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1695 /* This is disabled because we need to support liblustre flock.
1696 * LASSERT(req_bits != 0);
1699 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1700 req, info->mti_dlm_req, &cbs);
1701 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1702 return rc ? err_serious(rc) : req->rq_status;
1705 static int mdt_convert(struct mdt_thread_info *info)
1708 struct ptlrpc_request *req;
1710 LASSERT(info->mti_dlm_req);
1711 req = mdt_info_req(info);
1712 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1713 return rc ? err_serious(rc) : req->rq_status;
1716 static int mdt_bl_callback(struct mdt_thread_info *info)
1718 CERROR("bl callbacks should not happen on MDS\n");
1720 return err_serious(-EOPNOTSUPP);
1723 static int mdt_cp_callback(struct mdt_thread_info *info)
1725 CERROR("cp callbacks should not happen on MDS\n");
1727 return err_serious(-EOPNOTSUPP);
1731 * sec context handlers
1733 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1737 rc = mdt_handle_idmap(info);
1740 struct ptlrpc_request *req = mdt_info_req(info);
1743 opc = lustre_msg_get_opc(req->rq_reqmsg);
1744 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
1745 sptlrpc_svc_ctx_invalidate(req);
1748 OBD_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, obd_fail_val);
1753 static struct mdt_object *mdt_obj(struct lu_object *o)
1755 LASSERT(lu_device_is_mdt(o->lo_dev));
1756 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1759 struct mdt_object *mdt_object_find(const struct lu_env *env,
1760 struct mdt_device *d,
1761 const struct lu_fid *f)
1763 struct lu_object *o;
1764 struct mdt_object *m;
1767 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
1768 o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1769 if (unlikely(IS_ERR(o)))
1770 m = (struct mdt_object *)o;
1776 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1777 struct mdt_lock_handle *lh, __u64 ibits, int locality)
1779 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1780 ldlm_policy_data_t *policy = &info->mti_policy;
1781 struct ldlm_res_id *res_id = &info->mti_res_id;
1785 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1786 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1787 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1788 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1790 if (mdt_object_exists(o) < 0) {
1791 if (locality == MDT_CROSS_LOCK) {
1792 /* cross-ref object fix */
1793 ibits &= ~MDS_INODELOCK_UPDATE;
1794 ibits |= MDS_INODELOCK_LOOKUP;
1796 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1797 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1799 /* No PDO lock on remote object */
1800 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1803 memset(policy, 0, sizeof(*policy));
1804 fid_build_reg_res_name(mdt_object_fid(o), res_id);
1807 * Take PDO lock on whole directory and build correct @res_id for lock
1808 * on part of directory.
1810 if (lh->mlh_pdo_hash != 0) {
1811 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
1812 mdt_lock_pdo_mode(info, o, lh);
1813 if (lh->mlh_pdo_mode != LCK_NL) {
1815 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1816 * is never going to be sent to client and we do not
1817 * want it slowed down due to possible cancels.
1819 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1820 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1821 policy, res_id, LDLM_FL_ATOMIC_CB);
1827 * Finish res_id initializing by name hash marking patr of
1828 * directory which is taking modification.
1830 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1833 policy->l_inodebits.bits = ibits;
1836 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1837 * going to be sent to client. If it is - mdt_intent_policy() path will
1838 * fix it up and turns FL_LOCAL flag off.
1840 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1841 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1846 if (lh->mlh_type == MDT_PDO_LOCK) {
1847 /* check for exists after object is locked */
1848 if (mdt_object_exists(o) == 0) {
1849 /* Non-existent object shouldn't have PDO lock */
1852 /* Non-dir object shouldn't have PDO lock */
1853 LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
1858 mdt_object_unlock(info, o, lh, 1);
1865 void mdt_save_lock(struct ptlrpc_request *req, struct lustre_handle *h,
1866 ldlm_mode_t mode, int decref)
1870 if (lustre_handle_is_used(h)) {
1872 mdt_fid_unlock(h, mode);
1874 ptlrpc_save_lock(req, h, mode);
1882 * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1883 * to save this lock in req. when transaction committed, req will be released,
1884 * and lock will, too.
1886 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1887 struct mdt_lock_handle *lh, int decref)
1889 struct ptlrpc_request *req = mdt_info_req(info);
1892 mdt_save_lock(req, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
1893 mdt_save_lock(req, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
1898 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1899 const struct lu_fid *f,
1900 struct mdt_lock_handle *lh,
1903 struct mdt_object *o;
1905 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1909 rc = mdt_object_lock(info, o, lh, ibits,
1912 mdt_object_put(info->mti_env, o);
1919 void mdt_object_unlock_put(struct mdt_thread_info * info,
1920 struct mdt_object * o,
1921 struct mdt_lock_handle *lh,
1924 mdt_object_unlock(info, o, lh, decref);
1925 mdt_object_put(info->mti_env, o);
1928 static struct mdt_handler *mdt_handler_find(__u32 opc,
1929 struct mdt_opc_slice *supported)
1931 struct mdt_opc_slice *s;
1932 struct mdt_handler *h;
1935 for (s = supported; s->mos_hs != NULL; s++) {
1936 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1937 h = s->mos_hs + (opc - s->mos_opc_start);
1938 if (likely(h->mh_opc != 0))
1939 LASSERT(h->mh_opc == opc);
1941 h = NULL; /* unsupported opc */
1948 static int mdt_lock_resname_compat(struct mdt_device *m,
1949 struct ldlm_request *req)
1951 /* XXX something... later. */
1955 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1957 /* XXX something... later. */
1962 * Generic code handling requests that have struct mdt_body passed in:
1964 * - extract mdt_body from request and save it in @info, if present;
1966 * - create lu_object, corresponding to the fid in mdt_body, and save it in
1969 * - if HABEO_CORPUS flag is set for this request type check whether object
1970 * actually exists on storage (lu_object_exists()).
1973 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1975 const struct mdt_body *body;
1976 struct mdt_object *obj;
1977 const struct lu_env *env;
1978 struct req_capsule *pill;
1982 env = info->mti_env;
1983 pill = info->mti_pill;
1985 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1989 if (!(body->valid & OBD_MD_FLID))
1992 if (!fid_is_sane(&body->fid1)) {
1993 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1998 * Do not get size or any capa fields before we check that request
1999 * contains capa actually. There are some requests which do not, for
2000 * instance MDS_IS_SUBDIR.
2002 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2003 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2004 mdt_set_capainfo(info, 0, &body->fid1,
2005 req_capsule_client_get(pill, &RMF_CAPA1));
2007 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2009 if ((flags & HABEO_CORPUS) &&
2010 !mdt_object_exists(obj)) {
2011 mdt_object_put(env, obj);
2012 /* for capability renew ENOENT will be handled in
2014 if (body->valid & OBD_MD_FLOSSCAPA)
2019 info->mti_object = obj;
2028 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2030 struct req_capsule *pill = info->mti_pill;
2034 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2035 rc = mdt_body_unpack(info, flags);
2039 if (rc == 0 && (flags & HABEO_REFERO)) {
2040 struct mdt_device *mdt = info->mti_mdt;
2043 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2044 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2045 mdt->mdt_max_mdsize);
2046 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2047 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2048 mdt->mdt_max_cookiesize);
2050 rc = req_capsule_server_pack(pill);
2055 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2057 struct md_device *next = m->mdt_child;
2059 return next->md_ops->mdo_init_capa_ctxt(env, next,
2060 m->mdt_opts.mo_mds_capa,
2061 m->mdt_capa_timeout,
2067 * Invoke handler for this request opc. Also do necessary preprocessing
2068 * (according to handler ->mh_flags), and post-processing (setting of
2069 * ->last_{xid,committed}).
2071 static int mdt_req_handle(struct mdt_thread_info *info,
2072 struct mdt_handler *h, struct ptlrpc_request *req)
2074 int rc, serious = 0;
2079 LASSERT(h->mh_act != NULL);
2080 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2081 LASSERT(current->journal_info == NULL);
2084 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2085 * to put same checks into handlers like mdt_close(), mdt_reint(),
2086 * etc., without talking to mdt authors first. Checking same thing
2087 * there again is useless and returning 0 error wihtout packing reply
2088 * is buggy! Handlers either pack reply or return error.
2090 * We return 0 here and do not send any reply in order to emulate
2091 * network failure. Do not send any reply in case any of NET related
2092 * fail_id has occured.
2094 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2098 flags = h->mh_flags;
2099 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2101 if (h->mh_fmt != NULL) {
2102 req_capsule_set(info->mti_pill, h->mh_fmt);
2103 rc = mdt_unpack_req_pack_rep(info, flags);
2106 if (rc == 0 && flags & MUTABOR &&
2107 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2108 /* should it be rq_status? */
2111 if (rc == 0 && flags & HABEO_CLAVIS) {
2112 struct ldlm_request *dlm_req;
2114 LASSERT(h->mh_fmt != NULL);
2116 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2117 if (dlm_req != NULL) {
2118 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2119 rc = mdt_lock_resname_compat(info->mti_mdt,
2121 info->mti_dlm_req = dlm_req;
2127 /* capability setting changed via /proc, needs reinitialize ctxt */
2128 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2129 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2130 info->mti_mdt->mdt_capa_conf = 0;
2133 if (likely(rc == 0)) {
2135 * Process request, there can be two types of rc:
2136 * 1) errors with msg unpack/pack, other failures outside the
2137 * operation itself. This is counted as serious errors;
2138 * 2) errors during fs operation, should be placed in rq_status
2141 rc = h->mh_act(info);
2143 !req->rq_no_reply && req->rq_reply_state == NULL) {
2144 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2145 "pack reply and returned 0 error\n",
2149 serious = is_serious(rc);
2150 rc = clear_serious(rc);
2154 req->rq_status = rc;
2157 * ELDLM_* codes which > 0 should be in rq_status only as well as
2158 * all non-serious errors.
2160 if (rc > 0 || !serious)
2163 LASSERT(current->journal_info == NULL);
2165 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2166 info->mti_mdt->mdt_opts.mo_compat_resname) {
2167 struct ldlm_reply *dlmrep;
2169 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2171 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2174 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2175 if (likely(rc == 0 && h->mh_opc != MDS_DISCONNECT))
2176 target_committed_to_req(req);
2178 if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2179 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2180 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2184 target_send_reply(req, rc, info->mti_fail_id);
2188 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2190 lh->mlh_type = MDT_NUL_LOCK;
2191 lh->mlh_reg_lh.cookie = 0ull;
2192 lh->mlh_reg_mode = LCK_MINMODE;
2193 lh->mlh_pdo_lh.cookie = 0ull;
2194 lh->mlh_pdo_mode = LCK_MINMODE;
2197 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2199 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2200 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2204 * Initialize fields of struct mdt_thread_info. Other fields are left in
2205 * uninitialized state, because it's too expensive to zero out whole
2206 * mdt_thread_info (> 1K) on each request arrival.
2208 static void mdt_thread_info_init(struct ptlrpc_request *req,
2209 struct mdt_thread_info *info)
2212 struct md_capainfo *ci;
2214 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2215 info->mti_pill = &req->rq_pill;
2218 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2219 mdt_lock_handle_init(&info->mti_lh[i]);
2221 /* mdt device: it can be NULL while CONNECT */
2222 if (req->rq_export) {
2223 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2224 info->mti_exp = req->rq_export;
2226 info->mti_mdt = NULL;
2227 info->mti_env = req->rq_svc_thread->t_env;
2228 ci = md_capainfo(info->mti_env);
2229 memset(ci, 0, sizeof *ci);
2231 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2232 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2234 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2235 info->mti_body = NULL;
2236 info->mti_object = NULL;
2237 info->mti_dlm_req = NULL;
2238 info->mti_has_trans = 0;
2239 info->mti_no_need_trans = 0;
2240 info->mti_cross_ref = 0;
2241 info->mti_opdata = 0;
2243 /* To not check for split by default. */
2244 info->mti_spec.sp_ck_split = 0;
2247 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2251 req_capsule_fini(info->mti_pill);
2252 if (info->mti_object != NULL) {
2253 mdt_object_put(info->mti_env, info->mti_object);
2254 info->mti_object = NULL;
2256 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2257 mdt_lock_handle_fini(&info->mti_lh[i]);
2258 info->mti_env = NULL;
2261 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2262 struct obd_device *obd, int *process)
2264 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2265 case MDS_CONNECT: /* This will never get here, but for completeness. */
2266 case OST_CONNECT: /* This will never get here, but for completeness. */
2267 case MDS_DISCONNECT:
2268 case OST_DISCONNECT:
2273 case MDS_DONE_WRITING:
2274 case MDS_SYNC: /* used in unmounting */
2280 *process = target_queue_recovery_request(req, obd);
2284 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2291 * Handle recovery. Return:
2292 * +1: continue request processing;
2293 * -ve: abort immediately with the given error code;
2294 * 0: send reply with error code in req->rq_status;
2296 static int mdt_recovery(struct mdt_thread_info *info)
2298 struct ptlrpc_request *req = mdt_info_req(info);
2300 struct obd_device *obd;
2304 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2307 case SEC_CTX_INIT_CONT:
2313 rc = mdt_handle_idmap(info);
2322 if (unlikely(req->rq_export == NULL)) {
2323 CERROR("operation %d on unconnected MDS from %s\n",
2324 lustre_msg_get_opc(req->rq_reqmsg),
2325 libcfs_id2str(req->rq_peer));
2326 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2327 * mds_A will get -ENOTCONN(especially for ping req),
2328 * which will cause that mds_A deactive timeout, then when
2329 * mds_A cleanup, the cleanup process will be suspended since
2330 * deactive timeout is not zero.
2332 req->rq_status = -ENOTCONN;
2333 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2337 /* sanity check: if the xid matches, the request must be marked as a
2338 * resent or replayed */
2339 if (req_xid_is_last(req)) {
2340 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2341 (MSG_RESENT | MSG_REPLAY))) {
2342 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2343 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2344 lustre_msg_get_flags(req->rq_reqmsg));
2346 req->rq_status = -ENOTCONN;
2351 /* else: note the opposite is not always true; a RESENT req after a
2352 * failover will usually not match the last_xid, since it was likely
2353 * never committed. A REPLAYed request will almost never match the
2354 * last xid, however it could for a committed, but still retained,
2357 obd = req->rq_export->exp_obd;
2359 /* Check for aborted recovery... */
2360 spin_lock_bh(&obd->obd_processing_task_lock);
2361 recovering = obd->obd_recovering;
2362 spin_unlock_bh(&obd->obd_processing_task_lock);
2363 if (unlikely(recovering)) {
2366 DEBUG_REQ(D_INFO, req, "Got new replay");
2367 rc = mdt_filter_recovery_request(req, obd, &should_process);
2368 if (rc != 0 || !should_process)
2370 else if (should_process < 0) {
2371 req->rq_status = should_process;
2372 rc = ptlrpc_error(req);
2379 static int mdt_msg_check_version(struct lustre_msg *msg)
2383 switch (lustre_msg_get_opc(msg)) {
2385 case MDS_DISCONNECT:
2388 case SEC_CTX_INIT_CONT:
2390 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2392 CERROR("bad opc %u version %08x, expecting %08x\n",
2393 lustre_msg_get_opc(msg),
2394 lustre_msg_get_version(msg),
2395 LUSTRE_OBD_VERSION);
2399 case MDS_GETATTR_NAME:
2406 case MDS_DONE_WRITING:
2412 case MDS_QUOTACHECK:
2418 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2420 CERROR("bad opc %u version %08x, expecting %08x\n",
2421 lustre_msg_get_opc(msg),
2422 lustre_msg_get_version(msg),
2423 LUSTRE_MDS_VERSION);
2427 case LDLM_BL_CALLBACK:
2428 case LDLM_CP_CALLBACK:
2429 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2431 CERROR("bad opc %u version %08x, expecting %08x\n",
2432 lustre_msg_get_opc(msg),
2433 lustre_msg_get_version(msg),
2434 LUSTRE_DLM_VERSION);
2436 case OBD_LOG_CANCEL:
2437 case LLOG_ORIGIN_HANDLE_CREATE:
2438 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2439 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2440 case LLOG_ORIGIN_HANDLE_CLOSE:
2441 case LLOG_ORIGIN_HANDLE_DESTROY:
2442 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2444 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2446 CERROR("bad opc %u version %08x, expecting %08x\n",
2447 lustre_msg_get_opc(msg),
2448 lustre_msg_get_version(msg),
2449 LUSTRE_LOG_VERSION);
2452 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2458 static int mdt_handle0(struct ptlrpc_request *req,
2459 struct mdt_thread_info *info,
2460 struct mdt_opc_slice *supported)
2462 struct mdt_handler *h;
2463 struct lustre_msg *msg;
2468 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2471 LASSERT(current->journal_info == NULL);
2473 msg = req->rq_reqmsg;
2474 rc = mdt_msg_check_version(msg);
2475 if (likely(rc == 0)) {
2476 rc = mdt_recovery(info);
2477 if (likely(rc == +1)) {
2478 h = mdt_handler_find(lustre_msg_get_opc(msg),
2480 if (likely(h != NULL)) {
2481 rc = mdt_req_handle(info, h, req);
2483 CERROR("The unsupported opc: 0x%x\n", lustre_msg_get_opc(msg) );
2484 req->rq_status = -ENOTSUPP;
2485 rc = ptlrpc_error(req);
2490 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2495 * MDT handler function called by ptlrpc service thread when request comes.
2497 * XXX common "target" functionality should be factored into separate module
2498 * shared by mdt, ost and stand-alone services like fld.
2500 static int mdt_handle_common(struct ptlrpc_request *req,
2501 struct mdt_opc_slice *supported)
2504 struct mdt_thread_info *info;
2508 env = req->rq_svc_thread->t_env;
2509 LASSERT(env != NULL);
2510 LASSERT(env->le_ses != NULL);
2511 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2512 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2513 LASSERT(info != NULL);
2515 mdt_thread_info_init(req, info);
2517 rc = mdt_handle0(req, info, supported);
2519 mdt_thread_info_fini(info);
2524 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2527 int mdt_recovery_handle(struct ptlrpc_request *req)
2532 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2534 rc = mdt_handle_common(req, mdt_fld_handlers);
2537 rc = mdt_handle_common(req, mdt_seq_handlers);
2540 rc = mdt_handle_common(req, mdt_regular_handlers);
2547 static int mdt_regular_handle(struct ptlrpc_request *req)
2549 return mdt_handle_common(req, mdt_regular_handlers);
2552 static int mdt_readpage_handle(struct ptlrpc_request *req)
2554 return mdt_handle_common(req, mdt_readpage_handlers);
2557 static int mdt_xmds_handle(struct ptlrpc_request *req)
2559 return mdt_handle_common(req, mdt_xmds_handlers);
2562 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2564 return mdt_handle_common(req, mdt_seq_handlers);
2567 static int mdt_mdss_handle(struct ptlrpc_request *req)
2569 return mdt_handle_common(req, mdt_seq_handlers);
2572 static int mdt_dtss_handle(struct ptlrpc_request *req)
2574 return mdt_handle_common(req, mdt_seq_handlers);
2577 static int mdt_fld_handle(struct ptlrpc_request *req)
2579 return mdt_handle_common(req, mdt_fld_handlers);
2595 static int mdt_intent_getattr(enum mdt_it_code opcode,
2596 struct mdt_thread_info *info,
2597 struct ldlm_lock **,
2599 static int mdt_intent_reint(enum mdt_it_code opcode,
2600 struct mdt_thread_info *info,
2601 struct ldlm_lock **,
2604 static struct mdt_it_flavor {
2605 const struct req_format *it_fmt;
2607 int (*it_act)(enum mdt_it_code ,
2608 struct mdt_thread_info *,
2609 struct ldlm_lock **,
2612 } mdt_it_flavor[] = {
2614 .it_fmt = &RQF_LDLM_INTENT,
2615 /*.it_flags = HABEO_REFERO,*/
2617 .it_act = mdt_intent_reint,
2618 .it_reint = REINT_OPEN
2621 .it_fmt = &RQF_LDLM_INTENT,
2622 .it_flags = MUTABOR,
2623 .it_act = mdt_intent_reint,
2624 .it_reint = REINT_OPEN
2627 .it_fmt = &RQF_LDLM_INTENT,
2628 .it_flags = MUTABOR,
2629 .it_act = mdt_intent_reint,
2630 .it_reint = REINT_CREATE
2632 [MDT_IT_GETATTR] = {
2633 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2634 .it_flags = HABEO_REFERO,
2635 .it_act = mdt_intent_getattr
2637 [MDT_IT_READDIR] = {
2643 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2644 .it_flags = HABEO_REFERO,
2645 .it_act = mdt_intent_getattr
2648 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2649 .it_flags = MUTABOR,
2651 .it_reint = REINT_UNLINK
2655 .it_flags = MUTABOR,
2658 [MDT_IT_GETXATTR] = {
2665 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2666 struct ldlm_lock **lockp,
2667 struct ldlm_lock *new_lock,
2668 struct mdt_lock_handle *lh,
2671 struct ptlrpc_request *req = mdt_info_req(info);
2672 struct ldlm_lock *lock = *lockp;
2675 * Get new lock only for cases when possible resent did not find any
2678 if (new_lock == NULL)
2679 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2681 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2682 lh->mlh_reg_lh.cookie = 0;
2686 LASSERTF(new_lock != NULL,
2687 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2690 * If we've already given this lock to a client once, then we should
2691 * have no readers or writers. Otherwise, we should have one reader
2692 * _or_ writer ref (which will be zeroed below) before returning the
2695 if (new_lock->l_export == req->rq_export) {
2696 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2698 LASSERT(new_lock->l_export == NULL);
2699 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2704 if (new_lock->l_export == req->rq_export) {
2706 * Already gave this to the client, which means that we
2707 * reconstructed a reply.
2709 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2711 lh->mlh_reg_lh.cookie = 0;
2712 RETURN(ELDLM_LOCK_REPLACED);
2715 /* This lock might already be given to the client by an resent req,
2716 * in this case we should return ELDLM_LOCK_ABORTED,
2717 * so we should check led_held_locks here, but it will affect
2718 * performance, FIXME
2720 /* Fixup the lock to be given to the client */
2721 lock_res_and_lock(new_lock);
2722 new_lock->l_readers = 0;
2723 new_lock->l_writers = 0;
2725 new_lock->l_export = class_export_get(req->rq_export);
2726 spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2727 list_add(&new_lock->l_export_chain,
2728 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2729 spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2731 new_lock->l_blocking_ast = lock->l_blocking_ast;
2732 new_lock->l_completion_ast = lock->l_completion_ast;
2733 new_lock->l_remote_handle = lock->l_remote_handle;
2734 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2736 unlock_res_and_lock(new_lock);
2737 LDLM_LOCK_PUT(new_lock);
2738 lh->mlh_reg_lh.cookie = 0;
2740 RETURN(ELDLM_LOCK_REPLACED);
2743 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2744 struct ldlm_lock *new_lock,
2745 struct ldlm_lock **old_lock,
2746 struct mdt_lock_handle *lh)
2748 struct ptlrpc_request *req = mdt_info_req(info);
2749 struct obd_export *exp = req->rq_export;
2750 struct lustre_handle remote_hdl;
2751 struct ldlm_request *dlmreq;
2752 struct list_head *iter;
2754 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2757 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2758 remote_hdl = dlmreq->lock_handle[0];
2760 spin_lock(&exp->exp_ldlm_data.led_lock);
2761 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2762 struct ldlm_lock *lock;
2763 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2764 if (lock == new_lock)
2766 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2767 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2768 lh->mlh_reg_mode = lock->l_granted_mode;
2770 LDLM_DEBUG(lock, "restoring lock cookie");
2771 DEBUG_REQ(D_DLMTRACE, req,
2772 "restoring lock cookie "LPX64,
2773 lh->mlh_reg_lh.cookie);
2775 *old_lock = LDLM_LOCK_GET(lock);
2776 spin_unlock(&exp->exp_ldlm_data.led_lock);
2780 spin_unlock(&exp->exp_ldlm_data.led_lock);
2783 * If the xid matches, then we know this is a resent request, and allow
2784 * it. (It's probably an OPEN, for which we don't send a lock.
2786 if (req_xid_is_last(req))
2790 * This remote handle isn't enqueued, so we never received or processed
2791 * this request. Clear MSG_RESENT, because it can be handled like any
2792 * normal request now.
2794 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2796 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2800 static int mdt_intent_getattr(enum mdt_it_code opcode,
2801 struct mdt_thread_info *info,
2802 struct ldlm_lock **lockp,
2805 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2806 struct ldlm_lock *new_lock = NULL;
2808 struct ldlm_reply *ldlm_rep;
2809 struct ptlrpc_request *req;
2810 struct mdt_body *reqbody;
2811 struct mdt_body *repbody;
2815 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2818 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2821 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
2822 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
2823 repbody->eadatasize = 0;
2824 repbody->aclsize = 0;
2828 child_bits = MDS_INODELOCK_LOOKUP;
2830 case MDT_IT_GETATTR:
2831 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2834 CERROR("Unhandled till now");
2835 GOTO(out_shrink, rc = -EINVAL);
2838 rc = mdt_init_ucred(info, reqbody);
2840 GOTO(out_shrink, rc);
2842 req = info->mti_pill->rc_req;
2843 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2844 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2846 /* Get lock from request for possible resent case. */
2847 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
2849 ldlm_rep->lock_policy_res2 =
2850 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2852 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2853 ldlm_rep->lock_policy_res2 = 0;
2854 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2855 ldlm_rep->lock_policy_res2) {
2856 lhc->mlh_reg_lh.cookie = 0ull;
2857 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2860 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2863 mdt_exit_ucred(info);
2865 mdt_shrink_reply(info);
2869 static int mdt_intent_reint(enum mdt_it_code opcode,
2870 struct mdt_thread_info *info,
2871 struct ldlm_lock **lockp,
2874 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2875 struct ldlm_reply *rep = NULL;
2879 static const struct req_format *intent_fmts[REINT_MAX] = {
2880 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
2881 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
2886 opc = mdt_reint_opcode(info, intent_fmts);
2890 if (mdt_it_flavor[opcode].it_reint != opc) {
2891 CERROR("Reint code %ld doesn't match intent: %d\n",
2893 RETURN(err_serious(-EPROTO));
2896 /* Get lock from request for possible resent case. */
2897 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2899 rc = mdt_reint_internal(info, lhc, opc);
2901 /* Check whether the reply has been packed successfully. */
2902 if (mdt_info_req(info)->rq_repmsg != NULL)
2903 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2905 RETURN(err_serious(-EFAULT));
2907 /* MDC expects this in any case */
2909 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2911 /* Cross-ref case, the lock should be returned to the client */
2912 if (rc == -EREMOTE) {
2913 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2914 rep->lock_policy_res2 = 0;
2915 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
2918 rep->lock_policy_res2 = clear_serious(rc);
2920 lhc->mlh_reg_lh.cookie = 0ull;
2921 if (rc == -ENOTCONN || rc == -ENODEV) {
2923 * If it is the disconnect error (ENODEV & ENOCONN), the error
2924 * will be returned by rq_status, and client at ptlrpc layer
2925 * will detect this, then disconnect, reconnect the import
2926 * immediately, instead of impacting the following the rpc.
2931 * For other cases, the error will be returned by intent.
2932 * and client will retrieve the result from intent.
2935 * FIXME: when open lock is finished, that should be
2938 RETURN(ELDLM_LOCK_ABORTED);
2942 static int mdt_intent_code(long itcode)
2950 case IT_OPEN|IT_CREAT:
2957 rc = MDT_IT_READDIR;
2960 rc = MDT_IT_GETATTR;
2972 rc = MDT_IT_GETXATTR;
2975 CERROR("Unknown intent opcode: %ld\n", itcode);
2982 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2983 struct ldlm_lock **lockp, int flags)
2985 struct req_capsule *pill;
2986 struct mdt_it_flavor *flv;
2991 opc = mdt_intent_code(itopc);
2995 pill = info->mti_pill;
2996 flv = &mdt_it_flavor[opc];
2998 if (flv->it_fmt != NULL)
2999 req_capsule_extend(pill, flv->it_fmt);
3001 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3003 struct ptlrpc_request *req = mdt_info_req(info);
3004 if (flv->it_flags & MUTABOR &&
3005 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3008 if (rc == 0 && flv->it_act != NULL) {
3009 /* execute policy */
3010 rc = flv->it_act(opc, info, lockp, flags);
3017 static int mdt_intent_policy(struct ldlm_namespace *ns,
3018 struct ldlm_lock **lockp, void *req_cookie,
3019 ldlm_mode_t mode, int flags, void *data)
3021 struct mdt_thread_info *info;
3022 struct ptlrpc_request *req = req_cookie;
3023 struct ldlm_intent *it;
3024 struct req_capsule *pill;
3029 LASSERT(req != NULL);
3031 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3033 LASSERT(info != NULL);
3034 pill = info->mti_pill;
3035 LASSERT(pill->rc_req == req);
3037 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3038 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3039 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3041 const struct ldlm_request *dlmreq;
3044 struct ldlm_lock *lock = *lockp;
3046 LDLM_DEBUG(lock, "intent policy opc: %s\n",
3047 ldlm_it2str(it->opc));
3050 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3055 * Lock without inodebits makes no sense and will oops
3056 * later in ldlm. Let's check it now to see if we have
3057 * wrong lock from client or bits get corrupted
3058 * somewhere in mdt_intent_opc().
3060 dlmreq = info->mti_dlm_req;
3061 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
3062 LASSERT(req_bits != 0);
3065 rc = err_serious(-EFAULT);
3067 /* No intent was provided */
3068 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3069 rc = req_capsule_server_pack(pill);
3071 rc = err_serious(rc);
3079 static void mdt_seq_adjust(const struct lu_env *env,
3080 struct mdt_device *m, int lost)
3082 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3083 struct lu_range out;
3086 LASSERT(ls && ls->ls_server_seq);
3088 /* get extra seq from seq_server, moving it's range up */
3089 while (lost-- > 0) {
3090 seq_server_alloc_meta(ls->ls_server_seq, NULL, &out, env);
3095 static int mdt_seq_fini(const struct lu_env *env,
3096 struct mdt_device *m)
3098 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3101 if (ls && ls->ls_server_seq) {
3102 seq_server_fini(ls->ls_server_seq, env);
3103 OBD_FREE_PTR(ls->ls_server_seq);
3104 ls->ls_server_seq = NULL;
3107 if (ls && ls->ls_control_seq) {
3108 seq_server_fini(ls->ls_control_seq, env);
3109 OBD_FREE_PTR(ls->ls_control_seq);
3110 ls->ls_control_seq = NULL;
3113 if (ls && ls->ls_client_seq) {
3114 seq_client_fini(ls->ls_client_seq);
3115 OBD_FREE_PTR(ls->ls_client_seq);
3116 ls->ls_client_seq = NULL;
3122 static int mdt_seq_init(const struct lu_env *env,
3124 struct mdt_device *m)
3131 ls = m->mdt_md_dev.md_lu_dev.ld_site;
3134 * This is sequence-controller node. Init seq-controller server on local
3137 if (ls->ls_node_id == 0) {
3138 LASSERT(ls->ls_control_seq == NULL);
3140 OBD_ALLOC_PTR(ls->ls_control_seq);
3141 if (ls->ls_control_seq == NULL)
3144 rc = seq_server_init(ls->ls_control_seq,
3145 m->mdt_bottom, uuid,
3146 LUSTRE_SEQ_CONTROLLER,
3150 GOTO(out_seq_fini, rc);
3152 OBD_ALLOC_PTR(ls->ls_client_seq);
3153 if (ls->ls_client_seq == NULL)
3154 GOTO(out_seq_fini, rc = -ENOMEM);
3156 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3157 if (prefix == NULL) {
3158 OBD_FREE_PTR(ls->ls_client_seq);
3159 GOTO(out_seq_fini, rc = -ENOMEM);
3162 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3166 * Init seq-controller client after seq-controller server is
3167 * ready. Pass ls->ls_control_seq to it for direct talking.
3169 rc = seq_client_init(ls->ls_client_seq, NULL,
3170 LUSTRE_SEQ_METADATA, prefix,
3171 ls->ls_control_seq);
3172 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3175 GOTO(out_seq_fini, rc);
3178 /* Init seq-server on local MDT */
3179 LASSERT(ls->ls_server_seq == NULL);
3181 OBD_ALLOC_PTR(ls->ls_server_seq);
3182 if (ls->ls_server_seq == NULL)
3183 GOTO(out_seq_fini, rc = -ENOMEM);
3185 rc = seq_server_init(ls->ls_server_seq,
3186 m->mdt_bottom, uuid,
3190 GOTO(out_seq_fini, rc = -ENOMEM);
3192 /* Assign seq-controller client to local seq-server. */
3193 if (ls->ls_node_id == 0) {
3194 LASSERT(ls->ls_client_seq != NULL);
3196 rc = seq_server_set_cli(ls->ls_server_seq,
3204 mdt_seq_fini(env, m);
3209 * Init client sequence manager which is used by local MDS to talk to sequence
3210 * controller on remote node.
3212 static int mdt_seq_init_cli(const struct lu_env *env,
3213 struct mdt_device *m,
3214 struct lustre_cfg *cfg)
3216 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3217 struct obd_device *mdc;
3218 struct obd_uuid *uuidp, *mdcuuidp;
3219 char *uuid_str, *mdc_uuid_str;
3222 struct mdt_thread_info *info;
3223 char *p, *index_string = lustre_cfg_string(cfg, 2);
3226 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3227 uuidp = &info->mti_u.uuid[0];
3228 mdcuuidp = &info->mti_u.uuid[1];
3230 LASSERT(index_string);
3232 index = simple_strtol(index_string, &p, 10);
3234 CERROR("Invalid index in lustre_cgf, offset 2\n");
3238 /* check if this is adding the first MDC and controller is not yet
3240 if (index != 0 || ls->ls_client_seq)
3243 uuid_str = lustre_cfg_string(cfg, 1);
3244 mdc_uuid_str = lustre_cfg_string(cfg, 4);
3245 obd_str2uuid(uuidp, uuid_str);
3246 obd_str2uuid(mdcuuidp, mdc_uuid_str);
3248 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3250 CERROR("can't find controller MDC by uuid %s\n",
3253 } else if (!mdc->obd_set_up) {
3254 CERROR("target %s not set up\n", mdc->obd_name);
3257 LASSERT(ls->ls_control_exp);
3258 OBD_ALLOC_PTR(ls->ls_client_seq);
3259 if (ls->ls_client_seq != NULL) {
3262 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3266 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3269 rc = seq_client_init(ls->ls_client_seq,
3271 LUSTRE_SEQ_METADATA,
3273 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3280 LASSERT(ls->ls_server_seq != NULL);
3281 rc = seq_server_set_cli(ls->ls_server_seq, ls->ls_client_seq,
3288 static void mdt_seq_fini_cli(struct mdt_device *m)
3294 ls = m->mdt_md_dev.md_lu_dev.ld_site;
3296 if (ls && ls->ls_server_seq)
3297 seq_server_set_cli(ls->ls_server_seq,
3300 if (ls && ls->ls_control_exp) {
3301 class_export_put(ls->ls_control_exp);
3302 ls->ls_control_exp = NULL;
3310 static int mdt_fld_fini(const struct lu_env *env,
3311 struct mdt_device *m)
3313 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
3316 if (ls && ls->ls_server_fld) {
3317 fld_server_fini(ls->ls_server_fld, env);
3318 OBD_FREE_PTR(ls->ls_server_fld);
3319 ls->ls_server_fld = NULL;
3325 static int mdt_fld_init(const struct lu_env *env,
3327 struct mdt_device *m)
3333 ls = m->mdt_md_dev.md_lu_dev.ld_site;
3335 OBD_ALLOC_PTR(ls->ls_server_fld);
3336 if (ls->ls_server_fld == NULL)
3337 RETURN(rc = -ENOMEM);
3339 rc = fld_server_init(ls->ls_server_fld,
3340 m->mdt_bottom, uuid, env);
3342 OBD_FREE_PTR(ls->ls_server_fld);
3343 ls->ls_server_fld = NULL;
3350 /* device init/fini methods */
3351 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
3354 if (m->mdt_regular_service != NULL) {
3355 ptlrpc_unregister_service(m->mdt_regular_service);
3356 m->mdt_regular_service = NULL;
3358 if (m->mdt_readpage_service != NULL) {
3359 ptlrpc_unregister_service(m->mdt_readpage_service);
3360 m->mdt_readpage_service = NULL;
3362 if (m->mdt_xmds_service != NULL) {
3363 ptlrpc_unregister_service(m->mdt_xmds_service);
3364 m->mdt_xmds_service = NULL;
3366 if (m->mdt_setattr_service != NULL) {
3367 ptlrpc_unregister_service(m->mdt_setattr_service);
3368 m->mdt_setattr_service = NULL;
3370 if (m->mdt_mdsc_service != NULL) {
3371 ptlrpc_unregister_service(m->mdt_mdsc_service);
3372 m->mdt_mdsc_service = NULL;
3374 if (m->mdt_mdss_service != NULL) {
3375 ptlrpc_unregister_service(m->mdt_mdss_service);
3376 m->mdt_mdss_service = NULL;
3378 if (m->mdt_dtss_service != NULL) {
3379 ptlrpc_unregister_service(m->mdt_dtss_service);
3380 m->mdt_dtss_service = NULL;
3382 if (m->mdt_fld_service != NULL) {
3383 ptlrpc_unregister_service(m->mdt_fld_service);
3384 m->mdt_fld_service = NULL;
3389 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3392 static struct ptlrpc_service_conf conf;
3393 cfs_proc_dir_entry_t *procfs_entry;
3396 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3398 conf = (typeof(conf)) {
3399 .psc_nbufs = MDS_NBUFS,
3400 .psc_bufsize = MDS_BUFSIZE,
3401 .psc_max_req_size = MDS_MAXREQSIZE,
3402 .psc_max_reply_size = MDS_MAXREPSIZE,
3403 .psc_req_portal = MDS_REQUEST_PORTAL,
3404 .psc_rep_portal = MDC_REPLY_PORTAL,
3405 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3407 * We'd like to have a mechanism to set this on a per-device
3408 * basis, but alas...
3410 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3412 .psc_max_threads = MDT_MAX_THREADS,
3413 .psc_ctx_tags = LCT_MD_THREAD
3416 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3417 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3418 "mdt_ldlm_client", m->mdt_ldlm_client);
3420 m->mdt_regular_service =
3421 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3422 procfs_entry, target_print_req,
3424 if (m->mdt_regular_service == NULL)
3427 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service);
3429 GOTO(err_mdt_svc, rc);
3432 * readpage service configuration. Parameters have to be adjusted,
3435 conf = (typeof(conf)) {
3436 .psc_nbufs = MDS_NBUFS,
3437 .psc_bufsize = MDS_BUFSIZE,
3438 .psc_max_req_size = MDS_MAXREQSIZE,
3439 .psc_max_reply_size = MDS_MAXREPSIZE,
3440 .psc_req_portal = MDS_READPAGE_PORTAL,
3441 .psc_rep_portal = MDC_REPLY_PORTAL,
3442 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3443 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3445 .psc_max_threads = MDT_MAX_THREADS,
3446 .psc_ctx_tags = LCT_MD_THREAD
3448 m->mdt_readpage_service =
3449 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3450 LUSTRE_MDT_NAME "_readpage",
3451 procfs_entry, target_print_req,"mdt_rdpg");
3453 if (m->mdt_readpage_service == NULL) {
3454 CERROR("failed to start readpage service\n");
3455 GOTO(err_mdt_svc, rc = -ENOMEM);
3458 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service);
3461 * setattr service configuration.
3463 conf = (typeof(conf)) {
3464 .psc_nbufs = MDS_NBUFS,
3465 .psc_bufsize = MDS_BUFSIZE,
3466 .psc_max_req_size = MDS_MAXREQSIZE,
3467 .psc_max_reply_size = MDS_MAXREPSIZE,
3468 .psc_req_portal = MDS_SETATTR_PORTAL,
3469 .psc_rep_portal = MDC_REPLY_PORTAL,
3470 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3471 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3473 .psc_max_threads = MDT_MAX_THREADS,
3474 .psc_ctx_tags = LCT_MD_THREAD
3477 m->mdt_setattr_service =
3478 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3479 LUSTRE_MDT_NAME "_setattr",
3480 procfs_entry, target_print_req,"mdt_attr");
3482 if (!m->mdt_setattr_service) {
3483 CERROR("failed to start setattr service\n");
3484 GOTO(err_mdt_svc, rc = -ENOMEM);
3487 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service);
3489 GOTO(err_mdt_svc, rc);
3492 * sequence controller service configuration
3494 conf = (typeof(conf)) {
3495 .psc_nbufs = MDS_NBUFS,
3496 .psc_bufsize = MDS_BUFSIZE,
3497 .psc_max_req_size = SEQ_MAXREQSIZE,
3498 .psc_max_reply_size = SEQ_MAXREPSIZE,
3499 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3500 .psc_rep_portal = MDC_REPLY_PORTAL,
3501 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3502 .psc_min_threads = SEQ_NUM_THREADS,
3503 .psc_max_threads = SEQ_NUM_THREADS,
3504 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3507 m->mdt_mdsc_service =
3508 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3509 LUSTRE_MDT_NAME"_mdsc",
3510 procfs_entry, target_print_req,"mdt_mdsc");
3511 if (!m->mdt_mdsc_service) {
3512 CERROR("failed to start seq controller service\n");
3513 GOTO(err_mdt_svc, rc = -ENOMEM);
3516 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service);
3518 GOTO(err_mdt_svc, rc);
3521 * metadata sequence server service configuration
3523 conf = (typeof(conf)) {
3524 .psc_nbufs = MDS_NBUFS,
3525 .psc_bufsize = MDS_BUFSIZE,
3526 .psc_max_req_size = SEQ_MAXREQSIZE,
3527 .psc_max_reply_size = SEQ_MAXREPSIZE,
3528 .psc_req_portal = SEQ_METADATA_PORTAL,
3529 .psc_rep_portal = MDC_REPLY_PORTAL,
3530 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3531 .psc_min_threads = SEQ_NUM_THREADS,
3532 .psc_max_threads = SEQ_NUM_THREADS,
3533 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3536 m->mdt_mdss_service =
3537 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3538 LUSTRE_MDT_NAME"_mdss",
3539 procfs_entry, target_print_req,"mdt_mdss");
3540 if (!m->mdt_mdss_service) {
3541 CERROR("failed to start metadata seq server service\n");
3542 GOTO(err_mdt_svc, rc = -ENOMEM);
3545 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service);
3547 GOTO(err_mdt_svc, rc);
3551 * Data sequence server service configuration. We want to have really
3552 * cluster-wide sequences space. This is why we start only one sequence
3553 * controller which manages space.
3555 conf = (typeof(conf)) {
3556 .psc_nbufs = MDS_NBUFS,
3557 .psc_bufsize = MDS_BUFSIZE,
3558 .psc_max_req_size = SEQ_MAXREQSIZE,
3559 .psc_max_reply_size = SEQ_MAXREPSIZE,
3560 .psc_req_portal = SEQ_DATA_PORTAL,
3561 .psc_rep_portal = OSC_REPLY_PORTAL,
3562 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3563 .psc_min_threads = SEQ_NUM_THREADS,
3564 .psc_max_threads = SEQ_NUM_THREADS,
3565 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3568 m->mdt_dtss_service =
3569 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3570 LUSTRE_MDT_NAME"_dtss",
3571 procfs_entry, target_print_req,"mdt_dtss");
3572 if (!m->mdt_dtss_service) {
3573 CERROR("failed to start data seq server service\n");
3574 GOTO(err_mdt_svc, rc = -ENOMEM);
3577 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service);
3579 GOTO(err_mdt_svc, rc);
3581 /* FLD service start */
3582 conf = (typeof(conf)) {
3583 .psc_nbufs = MDS_NBUFS,
3584 .psc_bufsize = MDS_BUFSIZE,
3585 .psc_max_req_size = FLD_MAXREQSIZE,
3586 .psc_max_reply_size = FLD_MAXREPSIZE,
3587 .psc_req_portal = FLD_REQUEST_PORTAL,
3588 .psc_rep_portal = MDC_REPLY_PORTAL,
3589 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3590 .psc_min_threads = FLD_NUM_THREADS,
3591 .psc_max_threads = FLD_NUM_THREADS,
3592 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
3595 m->mdt_fld_service =
3596 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3597 LUSTRE_MDT_NAME"_fld",
3598 procfs_entry, target_print_req, "mdt_fld");
3599 if (!m->mdt_fld_service) {
3600 CERROR("failed to start fld service\n");
3601 GOTO(err_mdt_svc, rc = -ENOMEM);
3604 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service);
3606 GOTO(err_mdt_svc, rc);
3609 * mds-mds service configuration. Separate portal is used to allow
3610 * mds-mds requests be not blocked during recovery.
3612 conf = (typeof(conf)) {
3613 .psc_nbufs = MDS_NBUFS,
3614 .psc_bufsize = MDS_BUFSIZE,
3615 .psc_max_req_size = MDS_MAXREQSIZE,
3616 .psc_max_reply_size = MDS_MAXREPSIZE,
3617 .psc_req_portal = MDS_MDS_PORTAL,
3618 .psc_rep_portal = MDC_REPLY_PORTAL,
3619 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
3620 .psc_min_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3622 .psc_max_threads = MDT_MAX_THREADS,
3623 .psc_ctx_tags = LCT_MD_THREAD
3625 m->mdt_xmds_service =
3626 ptlrpc_init_svc_conf(&conf, mdt_xmds_handle,
3627 LUSTRE_MDT_NAME "_mds",
3628 procfs_entry, target_print_req,"mdt_xmds");
3630 if (m->mdt_xmds_service == NULL) {
3631 CERROR("failed to start readpage service\n");
3632 GOTO(err_mdt_svc, rc = -ENOMEM);
3635 rc = ptlrpc_start_threads(NULL, m->mdt_xmds_service);
3637 GOTO(err_mdt_svc, rc);
3642 mdt_stop_ptlrpc_service(m);
3647 static void mdt_stack_fini(const struct lu_env *env,
3648 struct mdt_device *m, struct lu_device *top)
3650 struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3651 struct lustre_cfg_bufs *bufs;
3652 struct lustre_cfg *lcfg;
3653 struct mdt_thread_info *info;
3657 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3658 LASSERT(info != NULL);
3660 bufs = &info->mti_u.bufs;
3661 /* process cleanup, pass mdt obd name to get obd umount flags */
3662 lustre_cfg_bufs_reset(bufs, obd->obd_name);
3667 lustre_cfg_bufs_set_string(bufs, 1, flags);
3668 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3670 CERROR("Cannot alloc lcfg!\n");
3675 top->ld_ops->ldo_process_config(env, top, lcfg);
3676 lustre_cfg_free(lcfg);
3678 lu_stack_fini(env, top);
3679 m->mdt_child = NULL;
3680 m->mdt_bottom = NULL;
3683 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3684 const char *typename,
3685 struct lu_device *child,
3686 struct lustre_cfg *cfg)
3688 const char *dev = lustre_cfg_string(cfg, 0);
3689 struct obd_type *type;
3690 struct lu_device_type *ldt;
3691 struct lu_device *d;
3696 type = class_get_type(typename);
3698 CERROR("Unknown type: '%s'\n", typename);
3699 GOTO(out, rc = -ENODEV);
3702 rc = lu_context_refill(&env->le_ctx);
3704 CERROR("Failure to refill context: '%d'\n", rc);
3708 if (env->le_ses != NULL) {
3709 rc = lu_context_refill(env->le_ses);
3711 CERROR("Failure to refill session: '%d'\n", rc);
3718 CERROR("type: '%s'\n", typename);
3719 GOTO(out_type, rc = -EINVAL);
3722 ldt->ldt_obd_type = type;
3723 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3725 CERROR("Cannot allocate device: '%s'\n", typename);
3726 GOTO(out_type, rc = -ENODEV);
3729 LASSERT(child->ld_site);
3730 d->ld_site = child->ld_site;
3733 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3735 CERROR("can't init device '%s', rc %d\n", typename, rc);
3736 GOTO(out_alloc, rc);
3743 ldt->ldt_ops->ldto_device_free(env, d);
3746 class_put_type(type);
3751 static int mdt_stack_init(const struct lu_env *env,
3752 struct mdt_device *m, struct lustre_cfg *cfg)
3754 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3755 struct lu_device *tmp;
3756 struct md_device *md;
3760 /* init the stack */
3761 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3763 RETURN(PTR_ERR(tmp));
3765 m->mdt_bottom = lu2dt_dev(tmp);
3767 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3769 GOTO(out, rc = PTR_ERR(tmp));
3774 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3776 GOTO(out, rc = PTR_ERR(tmp));
3779 /*set mdd upcall device*/
3780 md_upcall_dev_set(md, lu2md_dev(d));
3783 /*set cmm upcall device*/
3784 md_upcall_dev_set(md, &m->mdt_md_dev);
3786 m->mdt_child = lu2md_dev(d);
3788 /* process setup config */
3789 tmp = &m->mdt_md_dev.md_lu_dev;
3790 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3793 /* fini from last known good lu_device */
3795 mdt_stack_fini(env, m, d);
3800 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3802 struct md_device *next = m->mdt_child;
3803 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3804 struct lu_site *ls = d->ld_site;
3805 struct obd_device *obd = m->mdt_md_dev.md_lu_dev.ld_obd;
3808 ping_evictor_stop();
3810 target_recovery_fini(obd);
3811 mdt_stop_ptlrpc_service(m);
3813 mdt_fs_cleanup(env, m);
3815 upcall_cache_cleanup(m->mdt_identity_cache);
3816 m->mdt_identity_cache = NULL;
3818 if (m->mdt_namespace != NULL) {
3819 ldlm_namespace_free(m->mdt_namespace, NULL, d->ld_obd->obd_force);
3820 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3823 mdt_seq_fini(env, m);
3824 mdt_seq_fini_cli(m);
3825 mdt_fld_fini(env, m);
3827 ptlrpc_lprocfs_unregister_obd(d->ld_obd);
3828 lprocfs_obd_cleanup(d->ld_obd);
3830 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
3832 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3833 cfs_timer_disarm(&m->mdt_ck_timer);
3834 mdt_ck_thread_stop(m);
3836 /* finish the stack */
3837 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3844 LASSERT(atomic_read(&d->ld_ref) == 0);
3845 md_device_fini(&m->mdt_md_dev);
3850 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3854 #ifdef CONFIG_FS_POSIX_ACL
3855 /* ACLs should be enabled by default (b=13829) */
3856 m->mdt_opts.mo_acl = 1;
3857 LCONSOLE_INFO("Enabling ACL\n");
3859 m->mdt_opts.mo_acl = 0;
3860 LCONSOLE_INFO("Disabling ACL\n");
3869 while (*p && *p != ',')
3873 if ((len == sizeof("user_xattr") - 1) &&
3874 (memcmp(options, "user_xattr", len) == 0)) {
3875 m->mdt_opts.mo_user_xattr = 1;
3876 LCONSOLE_INFO("Enabling user_xattr\n");
3877 } else if ((len == sizeof("nouser_xattr") - 1) &&
3878 (memcmp(options, "nouser_xattr", len) == 0)) {
3879 m->mdt_opts.mo_user_xattr = 0;
3880 LCONSOLE_INFO("Disabling user_xattr\n");
3881 } else if ((len == sizeof("noacl") - 1) &&
3882 (memcmp(options, "noacl", len) == 0)) {
3883 m->mdt_opts.mo_acl = 0;
3884 LCONSOLE_INFO("Disabling ACL\n");
3891 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3893 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3894 struct lu_device_type *ldt, struct lustre_cfg *cfg)
3896 struct lprocfs_static_vars lvars;
3897 struct mdt_thread_info *info;
3898 struct obd_device *obd;
3899 const char *dev = lustre_cfg_string(cfg, 0);
3900 const char *num = lustre_cfg_string(cfg, 2);
3901 struct lustre_mount_info *lmi;
3902 struct lustre_sb_info *lsi;
3904 const char *identity_upcall = "NONE";
3908 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3909 LASSERT(info != NULL);
3911 obd = class_name2obd(dev);
3912 LASSERT(obd != NULL);
3914 spin_lock_init(&m->mdt_transno_lock);
3916 m->mdt_max_mdsize = MAX_MD_SIZE;
3917 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3919 m->mdt_opts.mo_user_xattr = 0;
3920 m->mdt_opts.mo_acl = 0;
3921 lmi = server_get_mount_2(dev);
3923 CERROR("Cannot get mount info for %s!\n", dev);
3926 lsi = s2lsi(lmi->lmi_sb);
3927 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3928 server_put_mount_2(dev, lmi->lmi_mnt);
3931 m->mdt_sptlrpc_lock = RW_LOCK_UNLOCKED;
3932 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
3934 spin_lock_init(&m->mdt_ioepoch_lock);
3935 m->mdt_opts.mo_compat_resname = 0;
3936 m->mdt_capa_timeout = CAPA_TIMEOUT;
3937 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3938 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3940 spin_lock_init(&m->mdt_client_bitmap_lock);
3946 md_device_init(&m->mdt_md_dev, ldt);
3947 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3948 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3949 /* set this lu_device to obd, because error handling need it */
3950 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3952 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3954 CERROR("Can't init lu_site, rc %d\n", rc);
3955 GOTO(err_free_site, rc);
3958 lprocfs_mdt_init_vars(&lvars);
3959 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3961 CERROR("Can't init lprocfs, rc %d\n", rc);
3962 GOTO(err_fini_site, rc);
3964 ptlrpc_lprocfs_register_obd(obd);
3966 rc = mdt_procfs_init(m, dev);
3968 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
3969 GOTO(err_fini_proc, rc);
3972 /* set server index */
3974 s->ls_node_id = simple_strtol(num, NULL, 10);
3976 /* failover is the default
3977 * FIXME: we do not failout mds0/mgs, which may cause some problems.
3978 * assumed whose ls_node_id == 0 XXX
3980 obd->obd_replayable = 1;
3981 /* No connection accepted until configurations will finish */
3982 obd->obd_no_conn = 1;
3984 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
3985 char *str = lustre_cfg_string(cfg, 4);
3986 if (strchr(str, 'n')) {
3987 CWARN("%s: recovery disabled\n", obd->obd_name);
3988 obd->obd_replayable = 0;
3992 /* init the stack */
3993 rc = mdt_stack_init(env, m, cfg);
3995 CERROR("Can't init device stack, rc %d\n", rc);
3996 GOTO(err_fini_proc, rc);
3999 rc = mdt_fld_init(env, obd->obd_name, m);
4001 GOTO(err_fini_stack, rc);
4003 rc = mdt_seq_init(env, obd->obd_name, m);
4005 GOTO(err_fini_fld, rc);
4007 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4008 LUSTRE_MDT_NAME"-%p", m);
4009 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4010 LDLM_NAMESPACE_SERVER,
4011 LDLM_NAMESPACE_GREEDY);
4012 if (m->mdt_namespace == NULL)
4013 GOTO(err_fini_seq, rc = -ENOMEM);
4015 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4016 /* set obd_namespace for compatibility with old code */
4017 obd->obd_namespace = m->mdt_namespace;
4019 /* XXX: to support suppgid for ACL, we enable identity_upcall
4020 * by default, otherwise, maybe got unexpected -EACCESS. */
4021 if (m->mdt_opts.mo_acl)
4022 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4024 m->mdt_identity_cache = upcall_cache_init(obd->obd_name, identity_upcall,
4025 &mdt_identity_upcall_cache_ops);
4026 if (IS_ERR(m->mdt_identity_cache)) {
4027 rc = PTR_ERR(m->mdt_identity_cache);
4028 m->mdt_identity_cache = NULL;
4029 GOTO(err_free_ns, rc);
4032 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4034 rc = mdt_ck_thread_start(m);
4036 GOTO(err_free_ns, rc);
4038 rc = mdt_fs_setup(env, m, obd);
4042 target_recovery_init(obd, mdt_recovery_handle);
4044 rc = mdt_start_ptlrpc_service(m);
4046 GOTO(err_fs_cleanup, rc);
4048 ping_evictor_start();
4050 rc = lu_site_init_finish(s);
4052 GOTO(err_stop_service, rc);
4054 if (obd->obd_recovering == 0)
4055 mdt_postrecov(env, m);
4057 mdt_init_capa_ctxt(env, m);
4059 /* Reduce the initial timeout on an MDS because it doesn't need such
4060 * a long timeout as an OST does. Adaptive timeouts will adjust this
4061 * value appropriately. */
4062 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4063 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4068 ping_evictor_stop();
4069 mdt_stop_ptlrpc_service(m);
4071 target_recovery_fini(obd);
4072 mdt_fs_cleanup(env, m);
4074 cfs_timer_disarm(&m->mdt_ck_timer);
4075 mdt_ck_thread_stop(m);
4077 upcall_cache_cleanup(m->mdt_identity_cache);
4078 m->mdt_identity_cache = NULL;
4079 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4080 obd->obd_namespace = m->mdt_namespace = NULL;
4082 mdt_seq_fini(env, m);
4084 mdt_fld_fini(env, m);
4086 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4089 ptlrpc_lprocfs_unregister_obd(obd);
4090 lprocfs_obd_cleanup(obd);
4096 md_device_fini(&m->mdt_md_dev);
4100 /* used by MGS to process specific configurations */
4101 static int mdt_process_config(const struct lu_env *env,
4102 struct lu_device *d, struct lustre_cfg *cfg)
4104 struct mdt_device *m = mdt_dev(d);
4105 struct md_device *md_next = m->mdt_child;
4106 struct lu_device *next = md2lu_dev(md_next);
4110 switch (cfg->lcfg_command) {
4111 case LCFG_SPTLRPC_CONF: {
4112 struct sptlrpc_conf_log *log;
4113 struct sptlrpc_rule_set tmp_rset;
4115 log = sptlrpc_conf_log_extract(cfg);
4121 sptlrpc_rule_set_init(&tmp_rset);
4123 rc = sptlrpc_rule_set_from_log(&tmp_rset, log);
4125 CERROR("mdt %p: failed get sptlrpc rules: %d\n", m, rc);
4129 write_lock(&m->mdt_sptlrpc_lock);
4130 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4131 m->mdt_sptlrpc_rset = tmp_rset;
4132 write_unlock(&m->mdt_sptlrpc_lock);
4134 sptlrpc_target_update_exp_flavor(
4135 md2lu_dev(&m->mdt_md_dev)->ld_obd, &tmp_rset);
4140 struct lprocfs_static_vars lvars;
4141 struct obd_device *obd = d->ld_obd;
4143 lprocfs_mdt_init_vars(&lvars);
4144 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
4146 /* others are passed further */
4147 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4152 * Add mdc hook to get first MDT uuid and connect it to
4153 * ls->controller to use for seq manager.
4155 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4157 CERROR("Can't add mdc, rc %d\n", rc);
4159 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
4162 /* others are passed further */
4163 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4169 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4170 const struct lu_object_header *hdr,
4171 struct lu_device *d)
4173 struct mdt_object *mo;
4179 struct lu_object *o;
4180 struct lu_object_header *h;
4182 o = &mo->mot_obj.mo_lu;
4183 h = &mo->mot_header;
4184 lu_object_header_init(h);
4185 lu_object_init(o, h, d);
4186 lu_object_add_top(h, o);
4187 o->lo_ops = &mdt_obj_ops;
4193 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
4195 struct mdt_device *d = mdt_dev(o->lo_dev);
4196 struct lu_device *under;
4197 struct lu_object *below;
4201 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4202 PFID(lu_object_fid(o)));
4204 under = &d->mdt_child->md_lu_dev;
4205 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4206 if (below != NULL) {
4207 lu_object_add(o, below);
4214 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4216 struct mdt_object *mo = mdt_obj(o);
4217 struct lu_object_header *h;
4221 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4222 PFID(lu_object_fid(o)));
4225 lu_object_header_fini(h);
4230 static int mdt_object_print(const struct lu_env *env, void *cookie,
4231 lu_printer_t p, const struct lu_object *o)
4233 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
4236 static struct lu_device_operations mdt_lu_ops = {
4237 .ldo_object_alloc = mdt_object_alloc,
4238 .ldo_process_config = mdt_process_config
4241 static struct lu_object_operations mdt_obj_ops = {
4242 .loo_object_init = mdt_object_init,
4243 .loo_object_free = mdt_object_free,
4244 .loo_object_print = mdt_object_print
4247 /* mds_connect_internal */
4248 static int mdt_connect_internal(struct obd_export *exp,
4249 struct mdt_device *mdt,
4250 struct obd_connect_data *data)
4255 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4256 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4258 /* If no known bits (which should not happen, probably,
4259 as everybody should support LOOKUP and UPDATE bits at least)
4260 revert to compat mode with plain locks. */
4261 if (!data->ocd_ibits_known &&
4262 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4263 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4265 if (!mdt->mdt_opts.mo_acl)
4266 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4268 if (!mdt->mdt_opts.mo_user_xattr)
4269 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4271 if (!mdt->mdt_opts.mo_mds_capa)
4272 data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
4274 if (!mdt->mdt_opts.mo_oss_capa)
4275 data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
4277 spin_lock(&exp->exp_lock);
4278 exp->exp_connect_flags = data->ocd_connect_flags;
4279 spin_unlock(&exp->exp_lock);
4280 data->ocd_version = LUSTRE_VERSION_CODE;
4281 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
4285 if (mdt->mdt_opts.mo_acl &&
4286 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
4287 CWARN("%s: MDS requires ACL support but client does not\n",
4288 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4293 flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
4294 if ((exp->exp_connect_flags & flags) == flags) {
4295 CWARN("%s: both local and remote client flags are set\n",
4296 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4300 if (mdt->mdt_opts.mo_mds_capa &&
4301 ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
4302 CWARN("%s: MDS requires capability support, but client not\n",
4303 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4307 if (mdt->mdt_opts.mo_oss_capa &&
4308 ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
4309 CWARN("%s: MDS requires OSS capability support, "
4311 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4315 if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
4316 CWARN("%s: MDS requires FID support, but client not\n",
4317 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
4324 /* mds_connect copy */
4325 static int mdt_obd_connect(const struct lu_env *env,
4326 struct lustre_handle *conn, struct obd_device *obd,
4327 struct obd_uuid *cluuid,
4328 struct obd_connect_data *data,
4331 struct mdt_thread_info *info;
4332 struct lsd_client_data *lcd;
4333 struct obd_export *exp;
4334 struct mdt_device *mdt;
4335 struct ptlrpc_request *req;
4339 LASSERT(env != NULL);
4340 if (!conn || !obd || !cluuid)
4343 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4344 req = info->mti_pill->rc_req;
4345 mdt = mdt_dev(obd->obd_lu_dev);
4347 rc = class_connect(conn, obd, cluuid);
4351 exp = class_conn2export(conn);
4352 LASSERT(exp != NULL);
4354 CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
4356 spin_lock(&exp->exp_lock);
4357 exp->exp_sp_peer = req->rq_sp_from;
4359 read_lock(&mdt->mdt_sptlrpc_lock);
4360 sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset, exp->exp_sp_peer,
4361 req->rq_peer.nid, &exp->exp_flvr);
4362 read_unlock(&mdt->mdt_sptlrpc_lock);
4364 if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4365 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
4366 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
4367 libcfs_nid2str(req->rq_peer.nid));
4368 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
4369 spin_unlock(&exp->exp_lock);
4372 spin_unlock(&exp->exp_lock);
4374 rc = mdt_connect_internal(exp, mdt, data);
4378 struct mdt_thread_info *mti;
4379 mti = lu_context_key_get(&env->le_ctx,
4381 LASSERT(mti != NULL);
4383 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
4384 exp->exp_mdt_data.med_lcd = lcd;
4385 rc = mdt_client_new(env, mdt);
4388 exp->exp_mdt_data.med_lcd = NULL;
4395 class_disconnect(exp);
4397 class_export_put(exp);
4402 static int mdt_obd_reconnect(const struct lu_env *env,
4403 struct obd_export *exp, struct obd_device *obd,
4404 struct obd_uuid *cluuid,
4405 struct obd_connect_data *data)
4407 struct mdt_thread_info *info;
4408 struct mdt_device *mdt;
4409 struct ptlrpc_request *req;
4413 if (exp == NULL || obd == NULL || cluuid == NULL)
4416 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4417 req = info->mti_pill->rc_req;
4418 mdt = mdt_dev(obd->obd_lu_dev);
4420 CDEBUG(D_SEC, "from %s\n", sptlrpc_part2name(req->rq_sp_from));
4422 spin_lock(&exp->exp_lock);
4423 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
4424 exp->exp_sp_peer = req->rq_sp_from;
4426 read_lock(&mdt->mdt_sptlrpc_lock);
4427 sptlrpc_rule_set_choose(&mdt->mdt_sptlrpc_rset,
4429 req->rq_peer.nid, &exp->exp_flvr);
4430 read_unlock(&mdt->mdt_sptlrpc_lock);
4432 if (exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
4433 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
4434 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
4435 libcfs_nid2str(req->rq_peer.nid));
4436 exp->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
4437 spin_unlock(&exp->exp_lock);
4441 spin_unlock(&exp->exp_lock);
4443 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4448 static int mdt_obd_disconnect(struct obd_export *exp)
4450 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
4455 class_export_get(exp);
4457 /* Disconnect early so that clients can't keep using export */
4458 rc = class_disconnect(exp);
4459 if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
4460 ldlm_cancel_locks_for_export(exp);
4462 /* complete all outstanding replies */
4463 spin_lock(&exp->exp_lock);
4464 while (!list_empty(&exp->exp_outstanding_replies)) {
4465 struct ptlrpc_reply_state *rs =
4466 list_entry(exp->exp_outstanding_replies.next,
4467 struct ptlrpc_reply_state, rs_exp_list);
4468 struct ptlrpc_service *svc = rs->rs_service;
4470 spin_lock(&svc->srv_lock);
4471 list_del_init(&rs->rs_exp_list);
4472 ptlrpc_schedule_difficult_reply(rs);
4473 spin_unlock(&svc->srv_lock);
4475 spin_unlock(&exp->exp_lock);
4477 class_export_put(exp);
4481 /* FIXME: Can we avoid using these two interfaces? */
4482 static int mdt_init_export(struct obd_export *exp)
4484 struct mdt_export_data *med = &exp->exp_mdt_data;
4487 CFS_INIT_LIST_HEAD(&med->med_open_head);
4488 spin_lock_init(&med->med_open_lock);
4489 sema_init(&med->med_idmap_sem, 1);
4490 med->med_idmap = NULL;
4491 spin_lock(&exp->exp_lock);
4492 exp->exp_connecting = 1;
4493 spin_unlock(&exp->exp_lock);
4497 static int mdt_destroy_export(struct obd_export *export)
4499 struct mdt_export_data *med;
4500 struct obd_device *obd = export->exp_obd;
4501 struct mdt_device *mdt;
4502 struct mdt_thread_info *info;
4510 med = &export->exp_mdt_data;
4511 if (med->med_rmtclient)
4512 mdt_cleanup_idmap(med);
4514 target_destroy_export(export);
4516 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
4519 mdt = mdt_dev(obd->obd_lu_dev);
4520 LASSERT(mdt != NULL);
4522 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4526 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4527 LASSERT(info != NULL);
4528 memset(info, 0, sizeof *info);
4529 info->mti_env = &env;
4530 info->mti_mdt = mdt;
4531 info->mti_exp = export;
4533 ma = &info->mti_attr;
4534 lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4535 cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4536 OBD_ALLOC(ma->ma_lmm, lmm_size);
4537 OBD_ALLOC(ma->ma_cookie, cookie_size);
4539 if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4540 GOTO(out, rc = -ENOMEM);
4541 ma->ma_need = MA_LOV | MA_COOKIE;
4543 /* Close any open files (which may also cause orphan unlinking). */
4544 spin_lock(&med->med_open_lock);
4545 while (!list_empty(&med->med_open_head)) {
4546 struct list_head *tmp = med->med_open_head.next;
4547 struct mdt_file_data *mfd =
4548 list_entry(tmp, struct mdt_file_data, mfd_list);
4550 /* Remove mfd handle so it can't be found again.
4551 * We are consuming the mfd_list reference here. */
4552 class_handle_unhash(&mfd->mfd_handle);
4553 list_del_init(&mfd->mfd_list);
4554 spin_unlock(&med->med_open_lock);
4555 mdt_mfd_close(info, mfd);
4556 /* TODO: if we close the unlinked file,
4557 * we need to remove it's objects from OST */
4558 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4559 spin_lock(&med->med_open_lock);
4560 ma->ma_lmm_size = lmm_size;
4561 ma->ma_cookie_size = cookie_size;
4562 ma->ma_need = MA_LOV | MA_COOKIE;
4565 spin_unlock(&med->med_open_lock);
4566 info->mti_mdt = NULL;
4567 mdt_client_del(&env, mdt);
4572 OBD_FREE(ma->ma_lmm, lmm_size);
4576 OBD_FREE(ma->ma_cookie, cookie_size);
4577 ma->ma_cookie = NULL;
4584 static void mdt_allow_cli(struct mdt_device *m, unsigned int flag)
4586 if (flag & CONFIG_LOG)
4587 m->mdt_fl_cfglog = 1;
4588 if (flag & CONFIG_SYNC)
4589 m->mdt_fl_synced = 1;
4591 if (m->mdt_fl_cfglog /* bz11778: && m->mdt_fl_synced */)
4592 /* Open for clients */
4593 m->mdt_md_dev.md_lu_dev.ld_obd->obd_no_conn = 0;
4596 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4597 enum md_upcall_event ev)
4599 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4600 struct md_device *next = m->mdt_child;
4601 struct mdt_thread_info *mti;
4607 rc = next->md_ops->mdo_maxsize_get(env, next,
4609 &m->mdt_max_cookiesize);
4610 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4611 m->mdt_max_mdsize, m->mdt_max_cookiesize);
4612 mdt_allow_cli(m, CONFIG_SYNC);
4615 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4616 mti->mti_no_need_trans = 1;
4617 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4620 /* Check that MDT is not yet configured */
4621 LASSERT(!m->mdt_fl_cfglog);
4624 CERROR("invalid event\n");
4631 static int mdt_obd_notify(struct obd_device *host,
4632 struct obd_device *watched,
4633 enum obd_notify_event ev, void *data)
4638 case OBD_NOTIFY_CONFIG:
4639 mdt_allow_cli(mdt_dev(host->obd_lu_dev), (unsigned long)data);
4642 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
4647 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4648 void *karg, void *uarg)
4651 struct obd_device *obd= exp->exp_obd;
4652 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4653 struct dt_device *dt = mdt->mdt_bottom;
4657 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4658 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4664 rc = mdt_device_sync(&env, mdt);
4666 case OBD_IOC_SET_READONLY:
4667 rc = dt->dd_ops->dt_sync(&env, dt);
4668 dt->dd_ops->dt_ro(&env, dt);
4670 case OBD_IOC_ABORT_RECOVERY:
4671 CERROR("Aborting recovery for device %s\n", obd->obd_name);
4672 target_stop_recovery_thread(obd);
4676 CERROR("Not supported cmd = %d for device %s\n",
4677 cmd, obd->obd_name);
4685 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4687 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4688 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
4691 /* if some clients didn't participate in recovery then we can possibly
4692 * lost sequence. Now we should increase sequence for safe value */
4693 lost = obd->obd_max_recoverable_clients - obd->obd_connected_clients;
4694 mdt_seq_adjust(env, mdt, lost);
4696 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4700 int mdt_obd_postrecov(struct obd_device *obd)
4705 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4708 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4713 static struct obd_ops mdt_obd_device_ops = {
4714 .o_owner = THIS_MODULE,
4715 .o_connect = mdt_obd_connect,
4716 .o_reconnect = mdt_obd_reconnect,
4717 .o_disconnect = mdt_obd_disconnect,
4718 .o_init_export = mdt_init_export,
4719 .o_destroy_export = mdt_destroy_export,
4720 .o_iocontrol = mdt_iocontrol,
4721 .o_postrecov = mdt_obd_postrecov,
4722 .o_notify = mdt_obd_notify
4725 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4726 struct lu_device *d)
4728 struct mdt_device *m = mdt_dev(d);
4735 static struct lu_device *mdt_device_free(const struct lu_env *env,
4736 struct lu_device *d)
4738 struct mdt_device *m = mdt_dev(d);
4745 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4746 struct lu_device_type *t,
4747 struct lustre_cfg *cfg)
4749 struct lu_device *l;
4750 struct mdt_device *m;
4756 l = &m->mdt_md_dev.md_lu_dev;
4757 rc = mdt_init0(env, m, t, cfg);
4763 md_upcall_init(&m->mdt_md_dev, mdt_upcall);
4765 l = ERR_PTR(-ENOMEM);
4769 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
4770 LU_KEY_INIT_FINI(mdt, struct mdt_thread_info);
4772 /* context key: mdt_thread_key */
4773 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
4775 /* context key constructor/destructor: mdt_txn_key_init, mdt_txn_key_fini */
4776 LU_KEY_INIT_FINI(mdt_txn, struct mdt_txn_info);
4778 struct lu_context_key mdt_txn_key = {
4779 .lct_tags = LCT_TX_HANDLE,
4780 .lct_init = mdt_txn_key_init,
4781 .lct_fini = mdt_txn_key_fini
4784 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4786 return md_ucred(info->mti_env);
4789 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
4790 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key, &mdt_txn_key);
4792 static struct lu_device_type_operations mdt_device_type_ops = {
4793 .ldto_init = mdt_type_init,
4794 .ldto_fini = mdt_type_fini,
4796 .ldto_device_alloc = mdt_device_alloc,
4797 .ldto_device_free = mdt_device_free,
4798 .ldto_device_fini = mdt_device_fini
4801 static struct lu_device_type mdt_device_type = {
4802 .ldt_tags = LU_DEVICE_MD,
4803 .ldt_name = LUSTRE_MDT_NAME,
4804 .ldt_ops = &mdt_device_type_ops,
4805 .ldt_ctx_tags = LCT_MD_THREAD
4808 static int __init mdt_mod_init(void)
4810 struct lprocfs_static_vars lvars;
4813 mdt_num_threads = MDT_NUM_THREADS;
4814 lprocfs_mdt_init_vars(&lvars);
4815 rc = class_register_type(&mdt_obd_device_ops, NULL,
4816 lvars.module_vars, LUSTRE_MDT_NAME,
4822 static void __exit mdt_mod_exit(void)
4824 class_unregister_type(LUSTRE_MDT_NAME);
4828 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
4829 [prefix ## _ ## opc - prefix ## _ ## base] = { \
4831 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
4832 .mh_opc = prefix ## _ ## opc, \
4833 .mh_flags = flags, \
4838 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
4839 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4841 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
4842 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4844 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
4845 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4847 * Request with a format known in advance
4849 #define DEF_MDT_HNDL_F(flags, name, fn) \
4850 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4852 #define DEF_SEQ_HNDL_F(flags, name, fn) \
4853 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4855 #define DEF_FLD_HNDL_F(flags, name, fn) \
4856 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4858 * Request with a format we do not yet know
4860 #define DEF_MDT_HNDL_0(flags, name, fn) \
4861 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4863 static struct mdt_handler mdt_mds_ops[] = {
4864 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4865 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
4866 DEF_MDT_HNDL_F(0, SET_INFO, mdt_set_info),
4867 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
4868 DEF_MDT_HNDL_F(HABEO_CORPUS, GETATTR, mdt_getattr),
4869 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4870 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
4871 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
4872 DEF_MDT_HNDL_F(0 |MUTABOR, REINT, mdt_reint),
4873 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
4874 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
4875 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
4876 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
4877 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
4878 DEF_MDT_HNDL_F(0, QUOTACHECK, mdt_quotacheck_handle),
4879 DEF_MDT_HNDL_F(0, QUOTACTL, mdt_quotactl_handle)
4882 #define DEF_OBD_HNDL(flags, name, fn) \
4883 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4886 static struct mdt_handler mdt_obd_ops[] = {
4887 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
4888 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
4889 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
4892 #define DEF_DLM_HNDL_0(flags, name, fn) \
4893 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4894 #define DEF_DLM_HNDL_F(flags, name, fn) \
4895 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4897 static struct mdt_handler mdt_dlm_ops[] = {
4898 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
4899 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
4900 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
4901 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
4904 static struct mdt_handler mdt_llog_ops[] = {
4907 #define DEF_SEC_CTX_HNDL(name, fn) \
4908 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4910 static struct mdt_handler mdt_sec_ctx_ops[] = {
4911 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
4912 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
4913 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
4916 static struct mdt_opc_slice mdt_regular_handlers[] = {
4918 .mos_opc_start = MDS_GETATTR,
4919 .mos_opc_end = MDS_LAST_OPC,
4920 .mos_hs = mdt_mds_ops
4923 .mos_opc_start = OBD_PING,
4924 .mos_opc_end = OBD_LAST_OPC,
4925 .mos_hs = mdt_obd_ops
4928 .mos_opc_start = LDLM_ENQUEUE,
4929 .mos_opc_end = LDLM_LAST_OPC,
4930 .mos_hs = mdt_dlm_ops
4933 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4934 .mos_opc_end = LLOG_LAST_OPC,
4935 .mos_hs = mdt_llog_ops
4938 .mos_opc_start = SEC_CTX_INIT,
4939 .mos_opc_end = SEC_LAST_OPC,
4940 .mos_hs = mdt_sec_ctx_ops
4947 static struct mdt_handler mdt_readpage_ops[] = {
4948 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4949 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4950 #ifdef HAVE_SPLIT_SUPPORT
4951 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4955 * XXX: this is ugly and should be fixed one day, see mdc_close() for
4956 * detailed comments. --umka
4958 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
4959 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
4962 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4964 .mos_opc_start = MDS_GETATTR,
4965 .mos_opc_end = MDS_LAST_OPC,
4966 .mos_hs = mdt_readpage_ops
4973 static struct mdt_handler mdt_xmds_ops[] = {
4974 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4975 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
4976 DEF_MDT_HNDL_F(0 | MUTABOR , REINT, mdt_reint),
4977 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
4980 static struct mdt_opc_slice mdt_xmds_handlers[] = {
4982 .mos_opc_start = MDS_GETATTR,
4983 .mos_opc_end = MDS_LAST_OPC,
4984 .mos_hs = mdt_xmds_ops
4987 .mos_opc_start = OBD_PING,
4988 .mos_opc_end = OBD_LAST_OPC,
4989 .mos_hs = mdt_obd_ops
4992 .mos_opc_start = SEC_CTX_INIT,
4993 .mos_opc_end = SEC_LAST_OPC,
4994 .mos_hs = mdt_sec_ctx_ops
5001 static struct mdt_handler mdt_seq_ops[] = {
5002 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
5005 static struct mdt_opc_slice mdt_seq_handlers[] = {
5007 .mos_opc_start = SEQ_QUERY,
5008 .mos_opc_end = SEQ_LAST_OPC,
5009 .mos_hs = mdt_seq_ops
5016 static struct mdt_handler mdt_fld_ops[] = {
5017 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
5020 static struct mdt_opc_slice mdt_fld_handlers[] = {
5022 .mos_opc_start = FLD_QUERY,
5023 .mos_opc_end = FLD_LAST_OPC,
5024 .mos_hs = mdt_fld_ops
5031 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5032 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
5033 MODULE_LICENSE("GPL");
5035 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
5036 "number of mdt service threads to start");
5038 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);