1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/mdt/mdt_handler.c
5 * Lustre Metadata Target (mdt) request handler
7 * Copyright (c) 2006 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
11 * Author: Mike Shaver <shaver@clusterfs.com>
12 * Author: Nikita Danilov <nikita@clusterfs.com>
13 * Author: Huang Hua <huanghua@clusterfs.com>
14 * Author: Yury Umanets <umka@clusterfs.com>
16 * This file is part of the Lustre file system, http://www.lustre.org
17 * Lustre is a trademark of Cluster File Systems, Inc.
19 * You may have signed or agreed to another license before downloading
20 * this software. If so, you are bound by the terms and conditions
21 * of that agreement, and the following does not apply to you. See the
22 * LICENSE file included with this distribution for more information.
24 * If you did not agree to a different license, then this copy of Lustre
25 * is open source software; you can redistribute it and/or modify it
26 * under the terms of version 2 of the GNU General Public License as
27 * published by the Free Software Foundation.
29 * In either case, Lustre is distributed in the hope that it will be
30 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
31 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * license text for more details.
36 # define EXPORT_SYMTAB
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <linux/module.h>
42 /* LUSTRE_VERSION_CODE */
43 #include <lustre_ver.h>
45 * struct OBD_{ALLOC,FREE}*()
48 #include <obd_support.h>
49 /* struct ptlrpc_request */
50 #include <lustre_net.h>
51 /* struct obd_export */
52 #include <lustre_export.h>
53 /* struct obd_device */
56 #include <dt_object.h>
57 #include <lustre_mds.h>
58 #include <lustre_mdt.h>
59 #include "mdt_internal.h"
60 #include <linux/lustre_acl.h>
61 #include <lustre_param.h>
63 mdl_mode_t mdt_mdl_lock_modes[] = {
64 [LCK_MINMODE] = MDL_MINMODE,
71 [LCK_GROUP] = MDL_GROUP
74 ldlm_mode_t mdt_dlm_lock_modes[] = {
75 [MDL_MINMODE] = LCK_MINMODE,
82 [MDL_GROUP] = LCK_GROUP
86 * Initialized in mdt_mod_init().
88 unsigned long mdt_num_threads;
90 /* ptlrpc request handler for MDT. All handlers are
91 * grouped into several slices - struct mdt_opc_slice,
92 * and stored in an array - mdt_handlers[].
95 /* The name of this handler. */
97 /* Fail id for this handler, checked at the beginning of this handler*/
99 /* Operation code for this handler */
101 /* flags are listed in enum mdt_handler_flags below. */
103 /* The actual handler function to execute. */
104 int (*mh_act)(struct mdt_thread_info *info);
105 /* Request format for this request. */
106 const struct req_format *mh_fmt;
109 enum mdt_handler_flags {
111 * struct mdt_body is passed in the incoming message, and object
112 * identified by this fid exists on disk.
114 * "habeo corpus" == "I have a body"
116 HABEO_CORPUS = (1 << 0),
118 * struct ldlm_request is passed in the incoming message.
120 * "habeo clavis" == "I have a key"
122 HABEO_CLAVIS = (1 << 1),
124 * this request has fixed reply format, so that reply message can be
125 * packed by generic code.
127 * "habeo refero" == "I have a reply"
129 HABEO_REFERO = (1 << 2),
131 * this request will modify something, so check whether the filesystem
132 * is readonly or not, then return -EROFS to client asap if necessary.
134 * "mutabor" == "I shall modify"
139 struct mdt_opc_slice {
142 struct mdt_handler *mos_hs;
145 static struct mdt_opc_slice mdt_regular_handlers[];
146 static struct mdt_opc_slice mdt_readpage_handlers[];
147 static struct mdt_opc_slice mdt_seq_handlers[];
148 static struct mdt_opc_slice mdt_fld_handlers[];
150 static struct mdt_device *mdt_dev(struct lu_device *d);
151 static int mdt_regular_handle(struct ptlrpc_request *req);
152 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
154 static struct lu_object_operations mdt_obj_ops;
156 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
160 return (rep->lock_policy_res1 & flag);
163 void mdt_clear_disposition(struct mdt_thread_info *info,
164 struct ldlm_reply *rep, int flag)
167 info->mti_opdata &= ~flag;
169 rep->lock_policy_res1 &= ~flag;
172 void mdt_set_disposition(struct mdt_thread_info *info,
173 struct ldlm_reply *rep, int flag)
176 info->mti_opdata |= flag;
178 rep->lock_policy_res1 |= flag;
181 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
183 lh->mlh_pdo_hash = 0;
184 lh->mlh_reg_mode = lm;
185 lh->mlh_type = MDT_REG_LOCK;
188 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
189 const char *name, int namelen)
191 lh->mlh_reg_mode = lm;
192 lh->mlh_type = MDT_PDO_LOCK;
195 LASSERT(namelen > 0);
196 lh->mlh_pdo_hash = full_name_hash(name, namelen - 1);
198 LASSERT(namelen == 0);
199 lh->mlh_pdo_hash = 0ull;
203 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
204 struct mdt_lock_handle *lh)
210 * Any dir access needs couple of locks:
212 * 1) on part of dir we gonna take lookup/modify;
214 * 2) on whole dir to protect it from concurrent splitting and/or to
215 * flush client's cache for readdir().
217 * so, for a given mode and object this routine decides what lock mode
218 * to use for lock #2:
220 * 1) if caller's gonna lookup in dir then we need to protect dir from
221 * being splitted only - LCK_CR
223 * 2) if caller's gonna modify dir then we need to protect dir from
224 * being splitted and to flush cache - LCK_CW
226 * 3) if caller's gonna modify dir and that dir seems ready for
227 * splitting then we need to protect it from any type of access
228 * (lookup/modify/split) - LCK_EX --bzzz
231 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
232 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
235 * No pdo locks possible on not existing objects, because pdo lock is
236 * taken on parent dir and parent can't be absent.
238 LASSERT(mdt_object_exists(o) > 0);
241 * Ask underlaying level its opinion about preferable PDO lock mode
242 * having access type passed as regular lock mode:
244 * - MDL_MINMODE means that lower layer does not want to specify lock
247 * - MDL_NL means that no PDO lock should be taken. This is used in some
248 * cases. Say, for non-splittable directories no need to use PDO locks
251 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
252 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
254 if (mode != MDL_MINMODE) {
255 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
258 * Lower layer does not want to specify locking mode. We do it
259 * our selves. No special protection is needed, just flush
260 * client's cache on modification and allow concurrent
263 switch (lh->mlh_reg_mode) {
265 lh->mlh_pdo_mode = LCK_EX;
268 lh->mlh_pdo_mode = LCK_CR;
271 lh->mlh_pdo_mode = LCK_CW;
274 CERROR("Not expected lock type (0x%x)\n",
275 (int)lh->mlh_reg_mode);
280 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
284 static int mdt_getstatus(struct mdt_thread_info *info)
286 struct mdt_device *mdt = info->mti_mdt;
287 struct md_device *next = mdt->mdt_child;
288 struct mdt_body *repbody;
293 rc = mdt_check_ucred(info);
295 RETURN(err_serious(rc));
297 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
298 RETURN(err_serious(-ENOMEM));
300 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
301 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
305 repbody->valid |= OBD_MD_FLID;
307 if (mdt->mdt_opts.mo_mds_capa) {
308 struct mdt_object *root;
309 struct lustre_capa *capa;
311 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
313 RETURN(PTR_ERR(root));
315 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
317 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
319 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
321 mdt_object_put(info->mti_env, root);
323 repbody->valid |= OBD_MD_FLMDSCAPA;
329 static int mdt_statfs(struct mdt_thread_info *info)
331 struct md_device *next = info->mti_mdt->mdt_child;
332 struct obd_statfs *osfs;
337 /* This will trigger a watchdog timeout */
338 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
339 (MDT_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
341 rc = mdt_check_ucred(info);
343 RETURN(err_serious(rc));
345 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
346 rc = err_serious(-ENOMEM);
348 osfs = req_capsule_server_get(&info->mti_pill,&RMF_OBD_STATFS);
349 /* XXX max_age optimisation is needed here. See mds_statfs */
350 rc = next->md_ops->mdo_statfs(info->mti_env, next,
352 statfs_pack(osfs, &info->mti_u.ksfs);
357 void mdt_pack_size2body(struct mdt_body *b, const struct lu_attr *attr,
358 struct mdt_object *o)
360 /* Check if Size-on-MDS is enabled. */
361 if (S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
362 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
363 b->size = attr->la_size;
364 b->blocks = attr->la_blocks;
368 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
369 const struct lu_attr *attr, const struct lu_fid *fid)
371 /*XXX should pack the reply body according to lu_valid*/
372 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
373 OBD_MD_FLGID | OBD_MD_FLTYPE |
374 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
375 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
377 if (!S_ISREG(attr->la_mode))
378 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
380 b->atime = attr->la_atime;
381 b->mtime = attr->la_mtime;
382 b->ctime = attr->la_ctime;
383 b->mode = attr->la_mode;
384 b->size = attr->la_size;
385 b->blocks = attr->la_blocks;
386 b->uid = attr->la_uid;
387 b->gid = attr->la_gid;
388 b->flags = attr->la_flags;
389 b->nlink = attr->la_nlink;
390 b->rdev = attr->la_rdev;
394 b->valid |= OBD_MD_FLID;
395 CDEBUG(D_INODE, ""DFID": nlink=%d, mode=%o, size="LPU64"\n",
396 PFID(fid), b->nlink, b->mode, b->size);
400 mdt_body_reverse_idmap(info, b);
403 static inline int mdt_body_has_lov(const struct lu_attr *la,
404 const struct mdt_body *body)
406 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
407 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
410 static int mdt_getattr_internal(struct mdt_thread_info *info,
411 struct mdt_object *o)
413 struct md_object *next = mdt_object_child(o);
414 const struct mdt_body *reqbody = info->mti_body;
415 struct ptlrpc_request *req = mdt_info_req(info);
416 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
417 struct md_attr *ma = &info->mti_attr;
418 struct lu_attr *la = &ma->ma_attr;
419 struct req_capsule *pill = &info->mti_pill;
420 const struct lu_env *env = info->mti_env;
421 struct mdt_body *repbody;
422 struct lu_buf *buffer = &info->mti_buf;
426 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
427 RETURN(err_serious(-ENOMEM));
429 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
431 if (reqbody->valid & OBD_MD_MEA) {
432 /* Assumption: MDT_MD size is enough for lmv size FIXME */
433 ma->ma_lmv = req_capsule_server_get(pill, &RMF_MDT_MD);
434 ma->ma_lmv_size = req_capsule_get_size(pill, &RMF_MDT_MD,
436 ma->ma_need = MA_INODE | MA_LMV;
438 ma->ma_need = MA_INODE | MA_LOV ;
439 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
440 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
444 rc = mo_attr_get(env, next, ma);
445 if (rc == -EREMOTE) {
446 /* This object is located on remote node.*/
447 repbody->fid1 = *mdt_object_fid(o);
448 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
451 CERROR("getattr error for "DFID": %d\n",
452 PFID(mdt_object_fid(o)), rc);
456 if (ma->ma_valid & MA_INODE)
457 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
461 if (mdt_body_has_lov(la, reqbody)) {
462 if (ma->ma_valid & MA_LOV) {
463 LASSERT(ma->ma_lmm_size);
464 mdt_dump_lmm(D_INFO, ma->ma_lmm);
465 repbody->eadatasize = ma->ma_lmm_size;
466 if (S_ISDIR(la->la_mode))
467 repbody->valid |= OBD_MD_FLDIREA;
469 repbody->valid |= OBD_MD_FLEASIZE;
471 if (ma->ma_valid & MA_LMV) {
472 LASSERT(S_ISDIR(la->la_mode));
473 repbody->eadatasize = ma->ma_lmv_size;
474 repbody->valid |= OBD_MD_FLDIREA;
475 repbody->valid |= OBD_MD_MEA;
477 } else if (S_ISLNK(la->la_mode) &&
478 reqbody->valid & OBD_MD_LINKNAME) {
479 buffer->lb_buf = ma->ma_lmm;
480 buffer->lb_len = reqbody->eadatasize;
481 rc = mo_readlink(env, next, buffer);
483 CERROR("readlink failed: %d\n", rc);
486 repbody->valid |= OBD_MD_LINKNAME;
487 repbody->eadatasize = rc;
488 ((char*)ma->ma_lmm)[rc - 1] = 0; /* NULL terminate */
489 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
490 (char*)ma->ma_lmm, rc);
495 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
496 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
497 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
498 repbody->valid |= OBD_MD_FLMODEASIZE;
499 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
500 "MAX_COOKIE to : %d:%d\n",
502 repbody->max_cookiesize);
505 if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
506 void *buf = req_capsule_server_get(pill, &RMF_ACL);
508 /* mdt_getattr_lock only */
509 rc = mdt_pack_remote_perm(info, o, buf);
511 repbody->valid &= ~OBD_MD_FLRMTPERM;
512 repbody->aclsize = 0;
515 repbody->valid |= OBD_MD_FLRMTPERM;
516 repbody->aclsize = sizeof(struct mdt_remote_perm);
519 #ifdef CONFIG_FS_POSIX_ACL
520 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
521 (reqbody->valid & OBD_MD_FLACL)) {
522 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
523 buffer->lb_len = req_capsule_get_size(pill,
524 &RMF_ACL, RCL_SERVER);
525 if (buffer->lb_len > 0) {
526 rc = mo_xattr_get(env, next, buffer,
527 XATTR_NAME_ACL_ACCESS);
529 if (rc == -ENODATA) {
530 repbody->aclsize = 0;
531 repbody->valid |= OBD_MD_FLACL;
533 } else if (rc == -EOPNOTSUPP) {
536 CERROR("got acl size: %d\n", rc);
539 repbody->aclsize = rc;
540 repbody->valid |= OBD_MD_FLACL;
547 if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
548 info->mti_mdt->mdt_opts.mo_mds_capa) {
549 struct lustre_capa *capa;
551 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
553 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
554 rc = mo_capa_get(env, next, capa, 0);
557 repbody->valid |= OBD_MD_FLMDSCAPA;
562 static int mdt_renew_capa(struct mdt_thread_info *info)
564 struct mdt_device *mdt = info->mti_mdt;
565 struct mdt_object *obj = info->mti_object;
566 struct mdt_body *body;
567 struct lustre_capa *capa, *c;
571 /* if object doesn't exist, or server has disabled capability,
572 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
575 if (!obj || !mdt->mdt_opts.mo_mds_capa)
578 body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
579 LASSERT(body != NULL);
581 c = req_capsule_client_get(&info->mti_pill, &RMF_CAPA1);
584 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
588 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
590 body->valid |= OBD_MD_FLOSSCAPA;
595 static int mdt_getattr(struct mdt_thread_info *info)
597 struct mdt_object *obj = info->mti_object;
598 struct req_capsule *pill = &info->mti_pill;
599 struct mdt_body *reqbody;
600 struct mdt_body *repbody;
605 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
608 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
609 rc = req_capsule_pack(pill);
611 RETURN(err_serious(rc));
612 rc = mdt_renew_capa(info);
613 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 0, 0);
617 LASSERT(obj != NULL);
618 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
620 mode = lu_object_attr(&obj->mot_obj.mo_lu);
621 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
622 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize)) {
623 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
624 reqbody->eadatasize);
626 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
627 info->mti_mdt->mdt_max_mdsize);
630 rc = req_capsule_pack(pill);
632 RETURN(err_serious(rc));
634 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
636 repbody->eadatasize = 0;
637 repbody->aclsize = 0;
639 if (reqbody->valid & OBD_MD_FLRMTPERM)
640 rc = mdt_init_ucred(info, reqbody);
642 rc = mdt_check_ucred(info);
646 /* don't check capability at all, because rename might
647 * getattr for remote obj, and at that time no capability
649 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
650 rc = mdt_getattr_internal(info, obj);
651 if (reqbody->valid & OBD_MD_FLRMTPERM)
652 mdt_exit_ucred(info);
655 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 1, 0);
659 static int mdt_is_subdir(struct mdt_thread_info *info)
661 struct mdt_object *o = info->mti_object;
662 struct req_capsule *pill = &info->mti_pill;
663 const struct mdt_body *body = info->mti_body;
664 struct mdt_body *repbody;
670 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
673 * We save last checked parent fid to @repbody->fid1 for remote
676 LASSERT(fid_is_sane(&body->fid2));
677 mdt_set_capainfo(info, 0, &body->fid1, BYPASS_CAPA);
678 mdt_set_capainfo(info, 1, &body->fid2, BYPASS_CAPA);
680 LASSERT(mdt_object_exists(o) > 0);
681 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
682 &body->fid2, &repbody->fid1);
683 if (rc == 0 || rc == -EREMOTE)
684 repbody->valid |= OBD_MD_FLID;
689 static int mdt_raw_lookup(struct mdt_thread_info *info,
690 struct mdt_object *parent,
692 struct ldlm_reply *ldlm_rep)
694 struct md_object *next = mdt_object_child(info->mti_object);
695 const struct mdt_body *reqbody = info->mti_body;
696 struct lu_fid *child_fid = &info->mti_tmp_fid1;
697 struct mdt_body *repbody;
701 if (reqbody->valid != OBD_MD_FLID)
704 /* Only got the fid of this obj by name */
705 rc = mdo_lookup(info->mti_env, next, name, child_fid);
708 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
711 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
713 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
714 repbody->fid1 = *child_fid;
715 repbody->valid = OBD_MD_FLID;
721 * UPDATE lock should be taken against parent, and be release before exit;
722 * child_bits lock should be taken against child, and be returned back:
723 * (1)normal request should release the child lock;
724 * (2)intent request will grant the lock to client.
726 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
727 struct mdt_lock_handle *lhc,
729 struct ldlm_reply *ldlm_rep)
731 struct ptlrpc_request *req = mdt_info_req(info);
732 struct mdt_object *parent = info->mti_object;
733 struct mdt_object *child;
734 struct md_object *next = mdt_object_child(info->mti_object);
735 struct lu_fid *child_fid = &info->mti_tmp_fid1;
736 int is_resent, rc, namelen = 0;
738 struct mdt_lock_handle *lhp;
739 struct ldlm_lock *lock;
742 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
744 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
746 LASSERT(info->mti_object != NULL);
747 name = req_capsule_client_get(&info->mti_pill, &RMF_NAME);
749 RETURN(err_serious(-EFAULT));
751 namelen = req_capsule_get_size(&info->mti_pill, &RMF_NAME,
754 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, ldlm_rep = %p\n",
755 PFID(mdt_object_fid(parent)), name, ldlm_rep);
757 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
759 rc = mdt_object_exists(parent);
763 CERROR("Object "DFID" locates on remote server\n",
764 PFID(mdt_object_fid(parent)));
768 rc = mdt_raw_lookup(info, parent, name, ldlm_rep);
776 /* Only getattr on the child. Parent is on another node. */
777 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
779 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
780 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
783 /* Do not take lock for resent case. */
784 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
786 CERROR("Invalid lock handle "LPX64"\n",
787 lhc->mlh_reg_lh.cookie);
790 LASSERT(fid_res_name_eq(mdt_object_fid(child),
791 &lock->l_resource->lr_name));
795 mdt_lock_handle_init(lhc);
796 mdt_lock_reg_init(lhc, LCK_PR);
799 * Object's name is on another MDS, no lookup lock is
800 * needed here but update is.
802 child_bits &= ~MDS_INODELOCK_LOOKUP;
803 child_bits |= MDS_INODELOCK_UPDATE;
805 rc = mdt_object_lock(info, child, lhc, child_bits,
809 /* Finally, we can get attr for child. */
810 mdt_set_capainfo(info, 0, mdt_object_fid(child),
812 rc = mdt_getattr_internal(info, child);
814 mdt_object_unlock(info, child, lhc, 1);
819 /* step 1: lock parent */
820 lhp = &info->mti_lh[MDT_LH_PARENT];
821 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
822 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
827 /*step 2: lookup child's fid by name */
828 rc = mdo_lookup(info->mti_env, next, name, child_fid);
831 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
832 GOTO(out_parent, rc);
834 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
836 *step 3: find the child object by fid & lock it.
837 * regardless if it is local or remote.
839 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
841 GOTO(out_parent, rc = PTR_ERR(child));
843 /* Do not take lock for resent case. */
844 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
846 CERROR("Invalid lock handle "LPX64"\n",
847 lhc->mlh_reg_lh.cookie);
850 LASSERT(fid_res_name_eq(child_fid,
851 &lock->l_resource->lr_name));
855 mdt_lock_handle_init(lhc);
856 mdt_lock_reg_init(lhc, LCK_PR);
858 rc = mdt_object_lock(info, child, lhc, child_bits,
864 /* finally, we can get attr for child. */
865 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
866 rc = mdt_getattr_internal(info, child);
868 mdt_object_unlock(info, child, lhc, 1);
870 struct ldlm_lock *lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
872 struct ldlm_res_id *res_id;
873 struct mdt_body *repbody;
876 /* Debugging code. */
877 res_id = &lock->l_resource->lr_name;
878 LDLM_DEBUG(lock, "we will return this lock client\n");
879 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
880 &lock->l_resource->lr_name),
881 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
882 (unsigned long)res_id->name[0],
883 (unsigned long)res_id->name[1],
884 (unsigned long)res_id->name[2],
885 PFID(mdt_object_fid(child)));
887 /* Pack Size-on-MDS inode attributes to the body if
888 * update lock is given. */
889 repbody = req_capsule_server_get(&info->mti_pill,
891 ma = &info->mti_attr.ma_attr;
892 if (lock->l_policy_data.l_inodebits.bits &
893 MDS_INODELOCK_UPDATE)
894 mdt_pack_size2body(repbody, ma, child);
900 mdt_object_put(info->mti_env, child);
902 mdt_object_unlock(info, parent, lhp, 1);
907 /* normal handler: should release the child lock */
908 static int mdt_getattr_name(struct mdt_thread_info *info)
910 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
911 struct mdt_body *reqbody;
912 struct mdt_body *repbody;
916 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
918 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
920 repbody->eadatasize = 0;
921 repbody->aclsize = 0;
923 rc = mdt_init_ucred(info, reqbody);
927 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
928 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
929 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
930 lhc->mlh_reg_lh.cookie = 0;
932 mdt_exit_ucred(info);
935 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 1, 0);
939 static struct lu_device_operations mdt_lu_ops;
941 static int lu_device_is_mdt(struct lu_device *d)
943 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
946 static int mdt_connect(struct mdt_thread_info *info)
949 struct ptlrpc_request *req;
951 req = mdt_info_req(info);
952 rc = target_handle_connect(req);
954 LASSERT(req->rq_export != NULL);
955 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
956 rc = mdt_init_idmap(info);
958 rc = err_serious(rc);
962 static int mdt_disconnect(struct mdt_thread_info *info)
966 rc = target_handle_disconnect(mdt_info_req(info));
968 rc = err_serious(rc);
972 static int mdt_sendpage(struct mdt_thread_info *info,
973 struct lu_rdpg *rdpg)
975 struct ptlrpc_request *req = mdt_info_req(info);
976 struct ptlrpc_bulk_desc *desc;
977 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
984 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
987 GOTO(out, rc = -ENOMEM);
989 for (i = 0, tmpcount = rdpg->rp_count;
990 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
991 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
992 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
995 LASSERT(desc->bd_nob == rdpg->rp_count);
996 rc = ptlrpc_start_bulk_transfer(desc);
1000 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1001 GOTO(abort_bulk, rc);
1003 *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
1004 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1005 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1008 if (desc->bd_success &&
1009 desc->bd_nob_transferred == rdpg->rp_count)
1010 GOTO(free_desc, rc);
1012 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1015 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
1016 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1017 desc->bd_nob_transferred, rdpg->rp_count,
1018 req->rq_export->exp_client_uuid.uuid,
1019 req->rq_export->exp_connection->c_remote_uuid.uuid);
1021 class_fail_export(req->rq_export);
1025 ptlrpc_abort_bulk(desc);
1027 ptlrpc_free_bulk(desc);
1032 #ifdef HAVE_SPLIT_SUPPORT
1034 * Retrieve dir entry from the page and insert it to the slave object, actually,
1035 * this should be in osd layer, but since it will not in the final product, so
1036 * just do it here and do not define more moo api anymore for this.
1038 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1041 struct mdt_object *object = info->mti_object;
1042 int rc = 0, offset = 0, is_dir;
1043 struct lu_dirpage *dp;
1044 struct lu_dirent *ent;
1047 /* Make sure we have at least one entry. */
1052 * Disable trans for this name insert, since it will include many trans
1055 info->mti_no_need_trans = 1;
1058 dp = page_address(page);
1059 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1061 for (ent = lu_dirent_start(dp); ent != NULL;
1062 ent = lu_dirent_next(ent)) {
1063 struct lu_fid *lf = &info->mti_tmp_fid2;
1066 if (le16_to_cpu(ent->lde_namelen) == 0)
1069 fid_le_to_cpu(lf, &ent->lde_fid);
1070 is_dir = le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT;
1071 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1073 GOTO(out, rc = -ENOMEM);
1075 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1076 rc = mdo_name_insert(info->mti_env,
1077 md_object_next(&object->mot_obj),
1079 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1081 CERROR("Can't insert %*.*s, rc %d\n",
1082 le16_to_cpu(ent->lde_namelen),
1083 le16_to_cpu(ent->lde_namelen),
1088 offset += lu_dirent_size(ent);
1098 static int mdt_bulk_timeout(void *data)
1102 CERROR("mdt bulk transfer timeout \n");
1107 static int mdt_writepage(struct mdt_thread_info *info)
1109 struct ptlrpc_request *req = mdt_info_req(info);
1110 struct mdt_body *reqbody;
1111 struct l_wait_info *lwi;
1112 struct ptlrpc_bulk_desc *desc;
1118 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1119 if (reqbody == NULL)
1120 RETURN(err_serious(-EFAULT));
1122 desc = ptlrpc_prep_bulk_exp (req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1124 RETURN(err_serious(-ENOMEM));
1126 /* allocate the page for the desc */
1127 page = alloc_pages(GFP_KERNEL, 0);
1129 GOTO(desc_cleanup, rc = -ENOMEM);
1131 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1132 (int)reqbody->size, (int)reqbody->nlink);
1134 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1135 (int)reqbody->nlink);
1138 * Check if client was evicted while we were doing i/o before touching
1143 GOTO(cleanup_page, rc = -ENOMEM);
1145 if (desc->bd_export->exp_failed)
1148 rc = ptlrpc_start_bulk_transfer (desc);
1150 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1151 mdt_bulk_timeout, desc);
1152 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1153 desc->bd_export->exp_failed, lwi);
1154 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1155 if (rc == -ETIMEDOUT) {
1156 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1157 ptlrpc_abort_bulk(desc);
1158 } else if (desc->bd_export->exp_failed) {
1159 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1161 ptlrpc_abort_bulk(desc);
1162 } else if (!desc->bd_success ||
1163 desc->bd_nob_transferred != desc->bd_nob) {
1164 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1166 "truncated" : "network error on",
1167 desc->bd_nob_transferred, desc->bd_nob);
1168 /* XXX should this be a different errno? */
1172 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
1175 GOTO(cleanup_lwi, rc);
1176 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1181 __free_pages(page, 0);
1183 ptlrpc_free_bulk(desc);
1188 static int mdt_readpage(struct mdt_thread_info *info)
1190 struct mdt_object *object = info->mti_object;
1191 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1192 struct mdt_body *reqbody;
1193 struct mdt_body *repbody;
1198 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1199 RETURN(err_serious(-ENOMEM));
1201 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1202 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
1203 if (reqbody == NULL || repbody == NULL)
1204 RETURN(err_serious(-EFAULT));
1206 rc = mdt_check_ucred(info);
1208 RETURN(err_serious(rc));
1211 * prepare @rdpg before calling lower layers and transfer itself. Here
1212 * reqbody->size contains offset of where to start to read and
1213 * reqbody->nlink contains number bytes to read.
1215 rdpg->rp_hash = reqbody->size;
1216 if ((__u64)rdpg->rp_hash != reqbody->size) {
1217 CERROR("Invalid hash: %#llx != %#llx\n",
1218 (__u64)rdpg->rp_hash, reqbody->size);
1221 rdpg->rp_count = reqbody->nlink;
1222 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1223 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1224 if (rdpg->rp_pages == NULL)
1227 for (i = 0; i < rdpg->rp_npages; ++i) {
1228 rdpg->rp_pages[i] = alloc_pages(GFP_KERNEL, 0);
1229 if (rdpg->rp_pages[i] == NULL)
1230 GOTO(free_rdpg, rc = -ENOMEM);
1233 /* call lower layers to fill allocated pages with directory data */
1234 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1236 GOTO(free_rdpg, rc);
1238 /* send pages to client */
1239 rc = mdt_sendpage(info, rdpg);
1244 for (i = 0; i < rdpg->rp_npages; i++)
1245 if (rdpg->rp_pages[i] != NULL)
1246 __free_pages(rdpg->rp_pages[i], 0);
1247 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1249 MDT_FAIL_RETURN(OBD_FAIL_MDS_SENDPAGE, 0);
1254 static int mdt_reint_internal(struct mdt_thread_info *info,
1255 struct mdt_lock_handle *lhc,
1258 struct req_capsule *pill = &info->mti_pill;
1259 struct mdt_device *mdt = info->mti_mdt;
1260 struct ptlrpc_request *req = mdt_info_req(info);
1261 struct mdt_body *repbody;
1266 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1267 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1268 mdt->mdt_max_mdsize);
1269 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1270 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1271 mdt->mdt_max_cookiesize);
1272 rc = req_capsule_pack(pill);
1274 CERROR("Can't pack response, rc %d\n", rc);
1275 RETURN(err_serious(rc));
1278 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1279 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1281 repbody->eadatasize = 0;
1282 repbody->aclsize = 0;
1286 * Check this after packing response, because after we fail here without
1287 * allocating response, caller anyway may want to get ldlm_reply from it
1288 * and will get oops.
1290 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1291 RETURN(err_serious(-EFAULT));
1293 rc = mdt_reint_unpack(info, op);
1295 CERROR("Can't unpack reint, rc %d\n", rc);
1296 RETURN(err_serious(rc));
1299 rc = mdt_init_ucred_reint(info);
1303 rc = mdt_fix_attr_ucred(info, op);
1307 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1308 struct mdt_client_data *mcd;
1310 mcd = req->rq_export->exp_mdt_data.med_mcd;
1311 if (req_xid_is_last(req)) {
1312 mdt_reconstruct(info, lhc);
1313 rc = lustre_msg_get_status(req->rq_repmsg);
1316 DEBUG_REQ(D_HA, req, "no reply for RESENT (xid "LPD64")",
1319 rc = mdt_reint_rec(info, lhc);
1321 mdt_exit_ucred(info);
1325 static long mdt_reint_opcode(struct mdt_thread_info *info,
1326 const struct req_format **fmt)
1331 opc = err_serious(-EFAULT);
1332 ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
1335 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1336 if (opc < REINT_MAX && fmt[opc] != NULL)
1337 req_capsule_extend(&info->mti_pill, fmt[opc]);
1339 CERROR("Unsupported opc: %ld\n", opc);
1340 opc = err_serious(opc);
1346 static int mdt_reint(struct mdt_thread_info *info)
1351 static const struct req_format *reint_fmts[REINT_MAX] = {
1352 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1353 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1354 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1355 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1356 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1357 [REINT_OPEN] = &RQF_MDS_REINT_OPEN
1362 opc = mdt_reint_opcode(info, reint_fmts);
1365 * No lock possible here from client to pass it to reint code
1368 rc = mdt_reint_internal(info, NULL, opc);
1373 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1377 /* TODO these two methods not available now. */
1379 /* this should sync the whole device */
1380 static int mdt_device_sync(struct mdt_thread_info *info)
1385 /* this should sync this object */
1386 static int mdt_object_sync(struct mdt_thread_info *info)
1391 static int mdt_sync(struct mdt_thread_info *info)
1393 struct req_capsule *pill = &info->mti_pill;
1394 struct mdt_body *body;
1398 /* The fid may be zero, so we req_capsule_set manually */
1399 req_capsule_set(pill, &RQF_MDS_SYNC);
1401 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1403 RETURN(err_serious(-EINVAL));
1405 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1406 RETURN(err_serious(-ENOMEM));
1408 if (fid_seq(&body->fid1) == 0) {
1409 /* sync the whole device */
1410 rc = req_capsule_pack(pill);
1412 rc = mdt_device_sync(info);
1414 rc = err_serious(rc);
1416 /* sync an object */
1417 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1419 rc = mdt_object_sync(info);
1421 struct md_object *next;
1422 const struct lu_fid *fid;
1423 struct lu_attr *la = &info->mti_attr.ma_attr;
1425 next = mdt_object_child(info->mti_object);
1426 info->mti_attr.ma_need = MA_INODE;
1427 info->mti_attr.ma_valid = 0;
1428 rc = mo_attr_get(info->mti_env, next,
1431 body = req_capsule_server_get(pill,
1433 fid = mdt_object_fid(info->mti_object);
1434 mdt_pack_attr2body(info, body, la, fid);
1438 rc = err_serious(rc);
1443 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1445 return err_serious(-EOPNOTSUPP);
1448 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1450 return err_serious(-EOPNOTSUPP);
1454 * OBD PING and other handlers.
1456 static int mdt_obd_ping(struct mdt_thread_info *info)
1460 rc = target_handle_ping(mdt_info_req(info));
1462 rc = err_serious(rc);
1466 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1468 return err_serious(-EOPNOTSUPP);
1471 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1473 return err_serious(-EOPNOTSUPP);
1480 static struct ldlm_callback_suite cbs = {
1481 .lcs_completion = ldlm_server_completion_ast,
1482 .lcs_blocking = ldlm_server_blocking_ast,
1486 static int mdt_enqueue(struct mdt_thread_info *info)
1488 struct ptlrpc_request *req;
1493 * info->mti_dlm_req already contains swapped and (if necessary)
1494 * converted dlm request.
1496 LASSERT(info->mti_dlm_req != NULL);
1498 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1499 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1503 req = mdt_info_req(info);
1506 * Lock without inodebits makes no sense and will oops later in
1507 * ldlm. Let's check it now to see if we have wrong lock from client or
1508 * bits get corrupted somewhere in mdt_intent_policy().
1510 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1511 LASSERT(req_bits != 0);
1513 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1514 req, info->mti_dlm_req, &cbs);
1515 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1516 return rc ? err_serious(rc) : req->rq_status;
1519 static int mdt_convert(struct mdt_thread_info *info)
1522 struct ptlrpc_request *req;
1524 LASSERT(info->mti_dlm_req);
1525 req = mdt_info_req(info);
1526 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1527 return rc ? err_serious(rc) : req->rq_status;
1530 static int mdt_bl_callback(struct mdt_thread_info *info)
1532 CERROR("bl callbacks should not happen on MDS\n");
1534 return err_serious(-EOPNOTSUPP);
1537 static int mdt_cp_callback(struct mdt_thread_info *info)
1539 CERROR("cp callbacks should not happen on MDS\n");
1541 return err_serious(-EOPNOTSUPP);
1545 * sec context handlers
1547 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1549 return mdt_handle_idmap(info);
1552 static struct mdt_object *mdt_obj(struct lu_object *o)
1554 LASSERT(lu_device_is_mdt(o->lo_dev));
1555 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1558 struct mdt_object *mdt_object_find(const struct lu_env *env,
1559 struct mdt_device *d,
1560 const struct lu_fid *f)
1562 struct lu_object *o;
1563 struct mdt_object *m;
1566 o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1568 m = (struct mdt_object *)o;
1574 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1575 struct mdt_lock_handle *lh, __u64 ibits, int locality)
1577 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1578 ldlm_policy_data_t *policy = &info->mti_policy;
1579 struct ldlm_res_id *res_id = &info->mti_res_id;
1580 int exist = mdt_object_exists(o);
1584 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1585 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1586 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1587 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1590 if (locality == MDT_CROSS_LOCK) {
1591 /* cross-ref object fix */
1592 ibits &= ~MDS_INODELOCK_UPDATE;
1593 ibits |= MDS_INODELOCK_LOOKUP;
1595 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1596 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1598 /* No PDO lock on remote object */
1599 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1600 } else if (exist == 0 && lh->mlh_type == MDT_PDO_LOCK) {
1602 * No PDO lock on non-existing object.
1603 * This may happen on removed $PWD on client.
1608 memset(policy, 0, sizeof(*policy));
1609 fid_build_reg_res_name(mdt_object_fid(o), res_id);
1612 * Take PDO lock on whole directory and build correct @res_id for lock
1613 * on part of directory.
1615 if (lh->mlh_type == MDT_PDO_LOCK && lh->mlh_pdo_hash != 0) {
1616 mdt_lock_pdo_mode(info, o, lh);
1617 if (lh->mlh_pdo_mode != LCK_NL) {
1619 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1620 * is never going to be sent to client and we do not
1621 * want it slowed down due to possible cancels.
1623 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1624 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1625 policy, res_id, LDLM_FL_ATOMIC_CB);
1631 * Finish res_id initializing by name hash marking patr of
1632 * directory which is taking modification.
1634 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1637 policy->l_inodebits.bits = ibits;
1640 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1641 * going to be sent to client. If it is - mdt_intent_policy() path will
1642 * fix it up and turns FL_LOCAL flag off.
1644 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1645 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1647 if (rc && lh->mlh_type == MDT_PDO_LOCK) {
1648 mdt_fid_unlock(&lh->mlh_pdo_lh, lh->mlh_pdo_mode);
1649 lh->mlh_pdo_lh.cookie = 0ull;
1656 * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1657 * to save this lock in req. when transaction committed, req will be released,
1658 * and lock will, too.
1660 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1661 struct mdt_lock_handle *lh, int decref)
1663 struct ptlrpc_request *req = mdt_info_req(info);
1666 if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1667 /* Do not save PDO locks to request, just decref. */
1668 mdt_fid_unlock(&lh->mlh_pdo_lh,
1670 lh->mlh_pdo_lh.cookie = 0;
1673 if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1675 mdt_fid_unlock(&lh->mlh_reg_lh,
1678 ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1681 lh->mlh_reg_lh.cookie = 0;
1687 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1688 const struct lu_fid *f,
1689 struct mdt_lock_handle *lh,
1692 struct mdt_object *o;
1694 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1698 rc = mdt_object_lock(info, o, lh, ibits,
1701 mdt_object_put(info->mti_env, o);
1708 void mdt_object_unlock_put(struct mdt_thread_info * info,
1709 struct mdt_object * o,
1710 struct mdt_lock_handle *lh,
1713 mdt_object_unlock(info, o, lh, decref);
1714 mdt_object_put(info->mti_env, o);
1717 static struct mdt_handler *mdt_handler_find(__u32 opc,
1718 struct mdt_opc_slice *supported)
1720 struct mdt_opc_slice *s;
1721 struct mdt_handler *h;
1724 for (s = supported; s->mos_hs != NULL; s++) {
1725 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1726 h = s->mos_hs + (opc - s->mos_opc_start);
1728 LASSERT(h->mh_opc == opc);
1730 h = NULL; /* unsupported opc */
1737 static int mdt_lock_resname_compat(struct mdt_device *m,
1738 struct ldlm_request *req)
1740 /* XXX something... later. */
1744 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1746 /* XXX something... later. */
1751 * Generic code handling requests that have struct mdt_body passed in:
1753 * - extract mdt_body from request and save it in @info, if present;
1755 * - create lu_object, corresponding to the fid in mdt_body, and save it in
1758 * - if HABEO_CORPUS flag is set for this request type check whether object
1759 * actually exists on storage (lu_object_exists()).
1762 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1764 const struct mdt_body *body;
1765 struct mdt_object *obj;
1766 const struct lu_env *env;
1767 struct req_capsule *pill;
1770 env = info->mti_env;
1771 pill = &info->mti_pill;
1773 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1777 if (!fid_is_sane(&body->fid1)) {
1778 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1783 * Do not get size or any capa fields before we check that request
1784 * contains capa actually. There are some requests which do not, for
1785 * instance MDS_IS_SUBDIR.
1787 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1788 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1789 mdt_set_capainfo(info, 0, &body->fid1,
1790 req_capsule_client_get(pill, &RMF_CAPA1));
1792 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1794 if ((flags & HABEO_CORPUS) &&
1795 !mdt_object_exists(obj)) {
1796 mdt_object_put(env, obj);
1797 /* for capability renew ENOENT will be handled in
1799 if (body->valid & OBD_MD_FLOSSCAPA)
1804 info->mti_object = obj;
1813 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1815 struct req_capsule *pill;
1819 pill = &info->mti_pill;
1821 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
1822 rc = mdt_body_unpack(info, flags);
1826 if (rc == 0 && (flags & HABEO_REFERO)) {
1827 struct mdt_device *mdt = info->mti_mdt;
1829 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1830 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1831 mdt->mdt_max_mdsize);
1832 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1833 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1834 mdt->mdt_max_cookiesize);
1836 rc = req_capsule_pack(pill);
1841 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
1843 struct md_device *next = m->mdt_child;
1845 return next->md_ops->mdo_init_capa_ctxt(env, next,
1846 m->mdt_opts.mo_mds_capa,
1847 m->mdt_capa_timeout,
1853 * Invoke handler for this request opc. Also do necessary preprocessing
1854 * (according to handler ->mh_flags), and post-processing (setting of
1855 * ->last_{xid,committed}).
1857 static int mdt_req_handle(struct mdt_thread_info *info,
1858 struct mdt_handler *h, struct ptlrpc_request *req)
1860 int rc, serious = 0;
1865 LASSERT(h->mh_act != NULL);
1866 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
1867 LASSERT(current->journal_info == NULL);
1869 DEBUG_REQ(D_INODE, req, "%s", h->mh_name);
1872 * Do not use *_FAIL_CHECK_ONCE() macros, because they will stop
1873 * correct handling of failed req later in ldlm due to doing
1874 * obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED without actually
1875 * correct actions like it is done in target_send_reply_msg().
1877 if (h->mh_fail_id != 0) {
1879 * Set to info->mti_fail_id to handler fail_id, it will be used
1880 * later, and better than use default fail_id.
1882 if (OBD_FAIL_CHECK(h->mh_fail_id)) {
1883 info->mti_fail_id = h->mh_fail_id;
1889 flags = h->mh_flags;
1890 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
1892 if (h->mh_fmt != NULL) {
1893 req_capsule_set(&info->mti_pill, h->mh_fmt);
1894 rc = mdt_unpack_req_pack_rep(info, flags);
1897 if (rc == 0 && flags & MUTABOR &&
1898 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1899 /* should it be rq_status? */
1902 if (rc == 0 && flags & HABEO_CLAVIS) {
1903 struct ldlm_request *dlm_req;
1905 LASSERT(h->mh_fmt != NULL);
1907 dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
1908 if (dlm_req != NULL) {
1909 if (info->mti_mdt->mdt_opts.mo_compat_resname)
1910 rc = mdt_lock_resname_compat(info->mti_mdt,
1912 info->mti_dlm_req = dlm_req;
1914 CERROR("Can't unpack dlm request\n");
1919 /* capability setting changed via /proc, needs reinitialize ctxt */
1920 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
1921 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
1922 info->mti_mdt->mdt_capa_conf = 0;
1927 * Process request, there can be two types of rc:
1928 * 1) errors with msg unpack/pack, other failures outside the
1929 * operation itself. This is counted as serious errors;
1930 * 2) errors during fs operation, should be placed in rq_status
1933 rc = h->mh_act(info);
1934 serious = is_serious(rc);
1935 rc = clear_serious(rc);
1939 req->rq_status = rc;
1942 * ELDLM_* codes which > 0 should be in rq_status only as well as
1943 * all non-serious errors.
1945 if (rc > 0 || !serious)
1948 LASSERT(current->journal_info == NULL);
1950 if (rc == 0 && (flags & HABEO_CLAVIS)
1951 && info->mti_mdt->mdt_opts.mo_compat_resname) {
1952 struct ldlm_reply *dlmrep;
1954 dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
1956 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
1959 /* If we're DISCONNECTing, the mdt_export_data is already freed */
1960 if (rc == 0 && h->mh_opc != MDS_DISCONNECT)
1961 target_committed_to_req(req);
1963 if ((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
1964 lustre_msg_get_transno(req->rq_reqmsg) == 0) {
1965 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY\n");
1972 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
1974 lh->mlh_type = MDT_NUL_LOCK;
1975 lh->mlh_reg_lh.cookie = 0ull;
1976 lh->mlh_reg_mode = LCK_MINMODE;
1977 lh->mlh_pdo_lh.cookie = 0ull;
1978 lh->mlh_pdo_mode = LCK_MINMODE;
1981 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
1983 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1984 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1988 * Initialize fields of struct mdt_thread_info. Other fields are left in
1989 * uninitialized state, because it's too expensive to zero out whole
1990 * mdt_thread_info (> 1K) on each request arrival.
1992 static void mdt_thread_info_init(struct ptlrpc_request *req,
1993 struct mdt_thread_info *info)
1997 info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
1998 for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
1999 info->mti_rep_buf_size[i] = -1;
2000 req_capsule_init(&info->mti_pill, req, RCL_SERVER,
2001 info->mti_rep_buf_size);
2004 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2005 mdt_lock_handle_init(&info->mti_lh[i]);
2007 /* mdt device: it can be NULL while CONNECT */
2009 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2011 info->mti_mdt = NULL;
2012 info->mti_env = req->rq_svc_thread->t_env;
2014 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2015 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2017 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2018 info->mti_body = NULL;
2019 info->mti_object = NULL;
2020 info->mti_dlm_req = NULL;
2021 info->mti_has_trans = 0;
2022 info->mti_no_need_trans = 0;
2023 info->mti_opdata = 0;
2026 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2030 req_capsule_fini(&info->mti_pill);
2031 if (info->mti_object != NULL) {
2032 mdt_object_put(info->mti_env, info->mti_object);
2033 info->mti_object = NULL;
2035 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2036 mdt_lock_handle_fini(&info->mti_lh[i]);
2037 info->mti_env = NULL;
2041 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2042 struct obd_device *obd, int *process);
2044 * Handle recovery. Return:
2045 * +1: continue request processing;
2046 * -ve: abort immediately with the given error code;
2047 * 0: send reply with error code in req->rq_status;
2049 static int mdt_recovery(struct mdt_thread_info *info)
2051 struct ptlrpc_request *req = mdt_info_req(info);
2053 struct obd_device *obd;
2057 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2060 case SEC_CTX_INIT_CONT:
2066 rc = mdt_handle_idmap(info);
2075 if (req->rq_export == NULL) {
2076 CERROR("operation %d on unconnected MDS from %s\n",
2077 lustre_msg_get_opc(req->rq_reqmsg),
2078 libcfs_id2str(req->rq_peer));
2079 req->rq_status = -ENOTCONN;
2080 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2084 /* sanity check: if the xid matches, the request must be marked as a
2085 * resent or replayed */
2086 if (req_xid_is_last(req)) {
2087 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2088 (MSG_RESENT | MSG_REPLAY))) {
2089 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2090 "expected REPLAY or RESENT flag\n", req->rq_xid);
2092 req->rq_status = -ENOTCONN;
2097 /* else: note the opposite is not always true; a RESENT req after a
2098 * failover will usually not match the last_xid, since it was likely
2099 * never committed. A REPLAYed request will almost never match the
2100 * last xid, however it could for a committed, but still retained,
2103 obd = req->rq_export->exp_obd;
2105 /* Check for aborted recovery... */
2106 spin_lock_bh(&obd->obd_processing_task_lock);
2107 recovering = obd->obd_recovering;
2108 spin_unlock_bh(&obd->obd_processing_task_lock);
2112 DEBUG_REQ(D_INFO, req, "Got new replay");
2113 rc = mds_filter_recovery_request(req, obd, &should_process);
2114 if (rc != 0 || !should_process)
2116 else if (should_process < 0) {
2117 req->rq_status = should_process;
2118 rc = ptlrpc_error(req);
2125 static int mdt_reply(struct ptlrpc_request *req, int rc,
2126 struct mdt_thread_info *info)
2131 if (req->rq_reply_state == NULL && rc == 0) {
2132 req->rq_status = rc;
2133 lustre_pack_reply(req, 1, NULL, NULL);
2136 target_send_reply(req, rc, info->mti_fail_id);
2141 extern int mds_msg_check_version(struct lustre_msg *msg);
2143 static int mdt_handle0(struct ptlrpc_request *req,
2144 struct mdt_thread_info *info,
2145 struct mdt_opc_slice *supported)
2147 struct mdt_handler *h;
2148 struct lustre_msg *msg;
2153 MDT_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2155 LASSERT(current->journal_info == NULL);
2157 msg = req->rq_reqmsg;
2158 rc = mds_msg_check_version(msg);
2160 rc = mdt_recovery(info);
2162 h = mdt_handler_find(lustre_msg_get_opc(msg),
2165 rc = mdt_req_handle(info, h, req);
2166 rc = mdt_reply(req, rc, info);
2168 req->rq_status = -ENOTSUPP;
2169 rc = ptlrpc_error(req);
2174 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2179 * MDT handler function called by ptlrpc service thread when request comes.
2181 * XXX common "target" functionality should be factored into separate module
2182 * shared by mdt, ost and stand-alone services like fld.
2184 static int mdt_handle_common(struct ptlrpc_request *req,
2185 struct mdt_opc_slice *supported)
2188 struct mdt_thread_info *info;
2192 env = req->rq_svc_thread->t_env;
2193 LASSERT(env != NULL);
2194 LASSERT(env->le_ses != NULL);
2195 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2196 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2197 LASSERT(info != NULL);
2199 mdt_thread_info_init(req, info);
2201 rc = mdt_handle0(req, info, supported);
2203 mdt_thread_info_fini(info);
2208 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2211 int mdt_recovery_handle(struct ptlrpc_request *req)
2216 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2218 rc = mdt_handle_common(req, mdt_fld_handlers);
2221 rc = mdt_handle_common(req, mdt_seq_handlers);
2224 rc = mdt_handle_common(req, mdt_regular_handlers);
2231 static int mdt_regular_handle(struct ptlrpc_request *req)
2233 return mdt_handle_common(req, mdt_regular_handlers);
2236 static int mdt_readpage_handle(struct ptlrpc_request *req)
2238 return mdt_handle_common(req, mdt_readpage_handlers);
2241 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2243 return mdt_handle_common(req, mdt_seq_handlers);
2246 static int mdt_mdss_handle(struct ptlrpc_request *req)
2248 return mdt_handle_common(req, mdt_seq_handlers);
2251 static int mdt_dtss_handle(struct ptlrpc_request *req)
2253 return mdt_handle_common(req, mdt_seq_handlers);
2256 static int mdt_fld_handle(struct ptlrpc_request *req)
2258 return mdt_handle_common(req, mdt_fld_handlers);
2274 static int mdt_intent_getattr(enum mdt_it_code opcode,
2275 struct mdt_thread_info *info,
2276 struct ldlm_lock **,
2278 static int mdt_intent_reint(enum mdt_it_code opcode,
2279 struct mdt_thread_info *info,
2280 struct ldlm_lock **,
2283 static struct mdt_it_flavor {
2284 const struct req_format *it_fmt;
2286 int (*it_act)(enum mdt_it_code ,
2287 struct mdt_thread_info *,
2288 struct ldlm_lock **,
2291 } mdt_it_flavor[] = {
2293 .it_fmt = &RQF_LDLM_INTENT,
2294 /*.it_flags = HABEO_REFERO,*/
2296 .it_act = mdt_intent_reint,
2297 .it_reint = REINT_OPEN
2300 .it_fmt = &RQF_LDLM_INTENT,
2301 .it_flags = MUTABOR,
2302 .it_act = mdt_intent_reint,
2303 .it_reint = REINT_OPEN
2306 .it_fmt = &RQF_LDLM_INTENT,
2307 .it_flags = MUTABOR,
2308 .it_act = mdt_intent_reint,
2309 .it_reint = REINT_CREATE
2311 [MDT_IT_GETATTR] = {
2312 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2313 .it_flags = HABEO_REFERO,
2314 .it_act = mdt_intent_getattr
2316 [MDT_IT_READDIR] = {
2322 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2323 .it_flags = HABEO_REFERO,
2324 .it_act = mdt_intent_getattr
2327 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2328 .it_flags = MUTABOR,
2329 .it_act = NULL, /* XXX can be mdt_intent_reint, ? */
2330 .it_reint = REINT_UNLINK
2334 .it_flags = MUTABOR,
2337 [MDT_IT_GETXATTR] = {
2344 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2345 struct ldlm_lock **lockp,
2346 struct ldlm_lock *new_lock,
2347 struct mdt_lock_handle *lh,
2350 struct ptlrpc_request *req = mdt_info_req(info);
2351 struct ldlm_lock *lock = *lockp;
2354 * Get new lock only for cases when possible resent did not find any
2357 if (new_lock == NULL)
2358 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2360 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2361 lh->mlh_reg_lh.cookie = 0;
2365 LASSERTF(new_lock != NULL,
2366 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2369 * If we've already given this lock to a client once, then we should
2370 * have no readers or writers. Otherwise, we should have one reader
2371 * _or_ writer ref (which will be zeroed below) before returning the
2374 if (new_lock->l_export == req->rq_export) {
2375 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2377 LASSERT(new_lock->l_export == NULL);
2378 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2383 if (new_lock->l_export == req->rq_export) {
2385 * Already gave this to the client, which means that we
2386 * reconstructed a reply.
2388 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2390 lh->mlh_reg_lh.cookie = 0;
2391 RETURN(ELDLM_LOCK_REPLACED);
2394 /* Fixup the lock to be given to the client */
2395 lock_res_and_lock(new_lock);
2396 new_lock->l_readers = 0;
2397 new_lock->l_writers = 0;
2399 new_lock->l_export = class_export_get(req->rq_export);
2400 spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2401 list_add(&new_lock->l_export_chain,
2402 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2403 spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2405 new_lock->l_blocking_ast = lock->l_blocking_ast;
2406 new_lock->l_completion_ast = lock->l_completion_ast;
2407 new_lock->l_remote_handle = lock->l_remote_handle;
2408 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2410 unlock_res_and_lock(new_lock);
2411 LDLM_LOCK_PUT(new_lock);
2412 lh->mlh_reg_lh.cookie = 0;
2414 RETURN(ELDLM_LOCK_REPLACED);
2417 static void mdt_intent_fixup_resent(struct req_capsule *pill,
2418 struct ldlm_lock *new_lock,
2419 struct ldlm_lock **old_lock,
2420 struct mdt_lock_handle *lh)
2422 struct ptlrpc_request *req = pill->rc_req;
2423 struct obd_export *exp = req->rq_export;
2424 struct lustre_handle remote_hdl;
2425 struct ldlm_request *dlmreq;
2426 struct list_head *iter;
2428 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2431 dlmreq = req_capsule_client_get(pill, &RMF_DLM_REQ);
2432 remote_hdl = dlmreq->lock_handle1;
2434 spin_lock(&exp->exp_ldlm_data.led_lock);
2435 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2436 struct ldlm_lock *lock;
2437 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2438 if (lock == new_lock)
2440 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2441 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2442 lh->mlh_reg_mode = lock->l_granted_mode;
2444 LDLM_DEBUG(lock, "restoring lock cookie");
2445 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2446 lh->mlh_reg_lh.cookie);
2448 *old_lock = LDLM_LOCK_GET(lock);
2449 spin_unlock(&exp->exp_ldlm_data.led_lock);
2453 spin_unlock(&exp->exp_ldlm_data.led_lock);
2456 * If the xid matches, then we know this is a resent request, and allow
2457 * it. (It's probably an OPEN, for which we don't send a lock.
2459 if (req_xid_is_last(req))
2463 * This remote handle isn't enqueued, so we never received or processed
2464 * this request. Clear MSG_RESENT, because it can be handled like any
2465 * normal request now.
2467 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2469 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2473 static int mdt_intent_getattr(enum mdt_it_code opcode,
2474 struct mdt_thread_info *info,
2475 struct ldlm_lock **lockp,
2478 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2479 struct ldlm_lock *new_lock = NULL;
2481 struct ldlm_reply *ldlm_rep;
2482 struct ptlrpc_request *req;
2483 struct mdt_body *reqbody;
2484 struct mdt_body *repbody;
2488 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
2490 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
2492 repbody->eadatasize = 0;
2493 repbody->aclsize = 0;
2497 child_bits = MDS_INODELOCK_LOOKUP;
2499 case MDT_IT_GETATTR:
2500 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2503 CERROR("Unhandled till now");
2504 GOTO(out, rc = -EINVAL);
2507 rc = mdt_init_ucred(info, reqbody);
2511 req = info->mti_pill.rc_req;
2512 ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2513 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2515 /* Get lock from request for possible resent case. */
2516 mdt_intent_fixup_resent(&info->mti_pill, *lockp, &new_lock, lhc);
2518 ldlm_rep->lock_policy_res2 =
2519 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2521 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2522 ldlm_rep->lock_policy_res2 = 0;
2523 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2524 ldlm_rep->lock_policy_res2) {
2525 lhc->mlh_reg_lh.cookie = 0ull;
2526 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2529 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2531 mdt_exit_ucred(info);
2534 mdt_shrink_reply(info, DLM_REPLY_REC_OFF + 1, 1, 0);
2538 static int mdt_intent_reint(enum mdt_it_code opcode,
2539 struct mdt_thread_info *info,
2540 struct ldlm_lock **lockp,
2543 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2544 struct ldlm_reply *rep;
2548 static const struct req_format *intent_fmts[REINT_MAX] = {
2549 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
2550 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
2555 opc = mdt_reint_opcode(info, intent_fmts);
2559 if (mdt_it_flavor[opcode].it_reint != opc) {
2560 CERROR("Reint code %ld doesn't match intent: %d\n",
2562 RETURN(err_serious(-EPROTO));
2565 /* Get lock from request for possible resent case. */
2566 mdt_intent_fixup_resent(&info->mti_pill, *lockp, NULL, lhc);
2568 rc = mdt_reint_internal(info, lhc, opc);
2570 rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2572 RETURN(err_serious(-EFAULT));
2574 /* MDC expects this in any case */
2576 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2578 /* cross-ref case, the lock should be returned to the client */
2579 if (rc == -EREMOTE) {
2580 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2581 rep->lock_policy_res2 = 0;
2582 RETURN(mdt_intent_lock_replace(info, lockp, NULL, lhc, flags));
2584 rep->lock_policy_res2 = clear_serious(rc);
2586 lhc->mlh_reg_lh.cookie = 0ull;
2587 RETURN(ELDLM_LOCK_ABORTED);
2590 static int mdt_intent_code(long itcode)
2598 case IT_OPEN|IT_CREAT:
2605 rc = MDT_IT_READDIR;
2608 rc = MDT_IT_GETATTR;
2620 rc = MDT_IT_GETXATTR;
2623 CERROR("Unknown intent opcode: %ld\n", itcode);
2630 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2631 struct ldlm_lock **lockp, int flags)
2633 struct req_capsule *pill;
2634 struct mdt_it_flavor *flv;
2639 opc = mdt_intent_code(itopc);
2643 pill = &info->mti_pill;
2644 flv = &mdt_it_flavor[opc];
2646 if (flv->it_fmt != NULL)
2647 req_capsule_extend(pill, flv->it_fmt);
2649 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2651 struct ptlrpc_request *req = mdt_info_req(info);
2652 if (flv->it_flags & MUTABOR &&
2653 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2656 if (rc == 0 && flv->it_act != NULL) {
2657 /* execute policy */
2658 rc = flv->it_act(opc, info, lockp, flags);
2664 static int mdt_intent_policy(struct ldlm_namespace *ns,
2665 struct ldlm_lock **lockp, void *req_cookie,
2666 ldlm_mode_t mode, int flags, void *data)
2668 struct mdt_thread_info *info;
2669 struct ptlrpc_request *req = req_cookie;
2670 struct ldlm_intent *it;
2671 struct req_capsule *pill;
2672 struct ldlm_lock *lock = *lockp;
2677 LASSERT(req != NULL);
2679 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2681 LASSERT(info != NULL);
2682 pill = &info->mti_pill;
2683 LASSERT(pill->rc_req == req);
2685 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2686 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2687 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2689 const struct ldlm_request *dlmreq;
2692 LDLM_DEBUG(lock, "intent policy opc: %s\n",
2693 ldlm_it2str(it->opc));
2695 rc = mdt_intent_opc(it->opc, info, lockp, flags);
2700 * Lock without inodebits makes no sense and will oops
2701 * later in ldlm. Let's check it now to see if we have
2702 * wrong lock from client or bits get corrupted
2703 * somewhere in mdt_intent_opc().
2705 dlmreq = info->mti_dlm_req;
2706 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2707 LASSERT(req_bits != 0);
2710 rc = err_serious(-EFAULT);
2712 /* No intent was provided */
2713 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2714 rc = req_capsule_pack(pill);
2716 rc = err_serious(rc);
2724 static int mdt_seq_fini(const struct lu_env *env,
2725 struct mdt_device *m)
2727 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2730 if (ls && ls->ls_server_seq) {
2731 seq_server_fini(ls->ls_server_seq, env);
2732 OBD_FREE_PTR(ls->ls_server_seq);
2733 ls->ls_server_seq = NULL;
2736 if (ls && ls->ls_control_seq) {
2737 seq_server_fini(ls->ls_control_seq, env);
2738 OBD_FREE_PTR(ls->ls_control_seq);
2739 ls->ls_control_seq = NULL;
2742 if (ls && ls->ls_client_seq) {
2743 seq_client_fini(ls->ls_client_seq);
2744 OBD_FREE_PTR(ls->ls_client_seq);
2745 ls->ls_client_seq = NULL;
2751 static int mdt_seq_init(const struct lu_env *env,
2753 struct mdt_device *m)
2760 ls = m->mdt_md_dev.md_lu_dev.ld_site;
2763 * This is sequence-controller node. Init seq-controller server on local
2766 if (ls->ls_node_id == 0) {
2767 LASSERT(ls->ls_control_seq == NULL);
2769 OBD_ALLOC_PTR(ls->ls_control_seq);
2770 if (ls->ls_control_seq == NULL)
2773 rc = seq_server_init(ls->ls_control_seq,
2774 m->mdt_bottom, uuid,
2775 LUSTRE_SEQ_CONTROLLER,
2779 GOTO(out_seq_fini, rc);
2781 OBD_ALLOC_PTR(ls->ls_client_seq);
2782 if (ls->ls_client_seq == NULL)
2783 GOTO(out_seq_fini, rc = -ENOMEM);
2785 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
2786 if (prefix == NULL) {
2787 OBD_FREE_PTR(ls->ls_client_seq);
2788 GOTO(out_seq_fini, rc = -ENOMEM);
2791 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
2795 * Init seq-controller client after seq-controller server is
2796 * ready. Pass ls->ls_control_seq to it for direct talking.
2798 rc = seq_client_init(ls->ls_client_seq, NULL,
2799 LUSTRE_SEQ_METADATA, prefix,
2800 ls->ls_control_seq);
2801 OBD_FREE(prefix, MAX_OBD_NAME + 5);
2804 GOTO(out_seq_fini, rc);
2807 /* Init seq-server on local MDT */
2808 LASSERT(ls->ls_server_seq == NULL);
2810 OBD_ALLOC_PTR(ls->ls_server_seq);
2811 if (ls->ls_server_seq == NULL)
2812 GOTO(out_seq_fini, rc = -ENOMEM);
2814 rc = seq_server_init(ls->ls_server_seq,
2815 m->mdt_bottom, uuid,
2819 GOTO(out_seq_fini, rc = -ENOMEM);
2821 /* Assign seq-controller client to local seq-server. */
2822 if (ls->ls_node_id == 0) {
2823 LASSERT(ls->ls_client_seq != NULL);
2825 rc = seq_server_set_cli(ls->ls_server_seq,
2833 mdt_seq_fini(env, m);
2838 * Init client sequence manager which is used by local MDS to talk to sequence
2839 * controller on remote node.
2841 static int mdt_seq_init_cli(const struct lu_env *env,
2842 struct mdt_device *m,
2843 struct lustre_cfg *cfg)
2845 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2846 struct obd_device *mdc;
2847 struct obd_uuid *uuidp, *mdcuuidp;
2848 char *uuid_str, *mdc_uuid_str;
2851 struct mdt_thread_info *info;
2852 char *p, *index_string = lustre_cfg_string(cfg, 2);
2855 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2856 uuidp = &info->mti_u.uuid[0];
2857 mdcuuidp = &info->mti_u.uuid[1];
2859 LASSERT(index_string);
2861 index = simple_strtol(index_string, &p, 10);
2863 CERROR("Invalid index in lustre_cgf, offset 2\n");
2867 /* check if this is adding the first MDC and controller is not yet
2869 if (index != 0 || ls->ls_client_seq)
2872 uuid_str = lustre_cfg_string(cfg, 1);
2873 mdc_uuid_str = lustre_cfg_string(cfg, 4);
2874 obd_str2uuid(uuidp, uuid_str);
2875 obd_str2uuid(mdcuuidp, mdc_uuid_str);
2877 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
2879 CERROR("can't find controller MDC by uuid %s\n",
2882 } else if (!mdc->obd_set_up) {
2883 CERROR("target %s not set up\n", mdc->obd_name);
2886 LASSERT(ls->ls_control_exp);
2887 OBD_ALLOC_PTR(ls->ls_client_seq);
2888 if (ls->ls_client_seq != NULL) {
2891 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
2895 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
2898 rc = seq_client_init(ls->ls_client_seq,
2900 LUSTRE_SEQ_METADATA,
2902 OBD_FREE(prefix, MAX_OBD_NAME + 5);
2909 LASSERT(ls->ls_server_seq != NULL);
2911 rc = seq_server_set_cli(ls->ls_server_seq,
2919 static void mdt_seq_fini_cli(struct mdt_device *m)
2925 ls = m->mdt_md_dev.md_lu_dev.ld_site;
2927 if (ls && ls->ls_server_seq)
2928 seq_server_set_cli(ls->ls_server_seq,
2931 if (ls && ls->ls_control_exp) {
2932 class_export_put(ls->ls_control_exp);
2933 ls->ls_control_exp = NULL;
2941 static int mdt_fld_fini(const struct lu_env *env,
2942 struct mdt_device *m)
2944 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2947 if (ls && ls->ls_server_fld) {
2948 fld_server_fini(ls->ls_server_fld, env);
2949 OBD_FREE_PTR(ls->ls_server_fld);
2950 ls->ls_server_fld = NULL;
2956 static int mdt_fld_init(const struct lu_env *env,
2958 struct mdt_device *m)
2964 ls = m->mdt_md_dev.md_lu_dev.ld_site;
2966 OBD_ALLOC_PTR(ls->ls_server_fld);
2967 if (ls->ls_server_fld == NULL)
2968 RETURN(rc = -ENOMEM);
2970 rc = fld_server_init(ls->ls_server_fld,
2971 m->mdt_bottom, uuid, env);
2973 OBD_FREE_PTR(ls->ls_server_fld);
2974 ls->ls_server_fld = NULL;
2981 /* device init/fini methods */
2982 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
2984 if (m->mdt_regular_service != NULL) {
2985 ptlrpc_unregister_service(m->mdt_regular_service);
2986 m->mdt_regular_service = NULL;
2988 if (m->mdt_readpage_service != NULL) {
2989 ptlrpc_unregister_service(m->mdt_readpage_service);
2990 m->mdt_readpage_service = NULL;
2992 if (m->mdt_setattr_service != NULL) {
2993 ptlrpc_unregister_service(m->mdt_setattr_service);
2994 m->mdt_setattr_service = NULL;
2996 if (m->mdt_mdsc_service != NULL) {
2997 ptlrpc_unregister_service(m->mdt_mdsc_service);
2998 m->mdt_mdsc_service = NULL;
3000 if (m->mdt_mdss_service != NULL) {
3001 ptlrpc_unregister_service(m->mdt_mdss_service);
3002 m->mdt_mdss_service = NULL;
3004 if (m->mdt_dtss_service != NULL) {
3005 ptlrpc_unregister_service(m->mdt_dtss_service);
3006 m->mdt_dtss_service = NULL;
3008 if (m->mdt_fld_service != NULL) {
3009 ptlrpc_unregister_service(m->mdt_fld_service);
3010 m->mdt_fld_service = NULL;
3014 static int mdt_start_ptlrpc_service(struct mdt_device *m)
3017 static struct ptlrpc_service_conf conf;
3018 cfs_proc_dir_entry_t *procfs_entry;
3021 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
3023 conf = (typeof(conf)) {
3024 .psc_nbufs = MDS_NBUFS,
3025 .psc_bufsize = MDS_BUFSIZE,
3026 .psc_max_req_size = MDS_MAXREQSIZE,
3027 .psc_max_reply_size = MDS_MAXREPSIZE,
3028 .psc_req_portal = MDS_REQUEST_PORTAL,
3029 .psc_rep_portal = MDC_REPLY_PORTAL,
3030 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3032 * We'd like to have a mechanism to set this on a per-device
3033 * basis, but alas...
3035 .psc_num_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3037 .psc_ctx_tags = LCT_MD_THREAD
3040 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3041 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3042 "mdt_ldlm_client", m->mdt_ldlm_client);
3044 m->mdt_regular_service =
3045 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3046 procfs_entry, NULL);
3047 if (m->mdt_regular_service == NULL)
3050 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service, LUSTRE_MDT_NAME);
3052 GOTO(err_mdt_svc, rc);
3055 * readpage service configuration. Parameters have to be adjusted,
3058 conf = (typeof(conf)) {
3059 .psc_nbufs = MDS_NBUFS,
3060 .psc_bufsize = MDS_BUFSIZE,
3061 .psc_max_req_size = MDS_MAXREQSIZE,
3062 .psc_max_reply_size = MDS_MAXREPSIZE,
3063 .psc_req_portal = MDS_READPAGE_PORTAL,
3064 .psc_rep_portal = MDC_REPLY_PORTAL,
3065 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3066 .psc_num_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3068 .psc_ctx_tags = LCT_MD_THREAD
3070 m->mdt_readpage_service =
3071 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3072 LUSTRE_MDT_NAME "_readpage",
3073 procfs_entry, NULL);
3075 if (m->mdt_readpage_service == NULL) {
3076 CERROR("failed to start readpage service\n");
3077 GOTO(err_mdt_svc, rc = -ENOMEM);
3080 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service, "mdt_rdpg");
3083 * setattr service configuration.
3085 conf = (typeof(conf)) {
3086 .psc_nbufs = MDS_NBUFS,
3087 .psc_bufsize = MDS_BUFSIZE,
3088 .psc_max_req_size = MDS_MAXREQSIZE,
3089 .psc_max_reply_size = MDS_MAXREPSIZE,
3090 .psc_req_portal = MDS_SETATTR_PORTAL,
3091 .psc_rep_portal = MDC_REPLY_PORTAL,
3092 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3093 .psc_num_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3095 .psc_ctx_tags = LCT_MD_THREAD
3098 m->mdt_setattr_service =
3099 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3100 LUSTRE_MDT_NAME "_setattr",
3101 procfs_entry, NULL);
3103 if (!m->mdt_setattr_service) {
3104 CERROR("failed to start setattr service\n");
3105 GOTO(err_mdt_svc, rc = -ENOMEM);
3108 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service, "mdt_attr");
3110 GOTO(err_mdt_svc, rc);
3113 * sequence controller service configuration
3115 conf = (typeof(conf)) {
3116 .psc_nbufs = MDS_NBUFS,
3117 .psc_bufsize = MDS_BUFSIZE,
3118 .psc_max_req_size = SEQ_MAXREQSIZE,
3119 .psc_max_reply_size = SEQ_MAXREPSIZE,
3120 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3121 .psc_rep_portal = MDC_REPLY_PORTAL,
3122 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3123 .psc_num_threads = SEQ_NUM_THREADS,
3124 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3127 m->mdt_mdsc_service =
3128 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3129 LUSTRE_MDT_NAME"_mdsc",
3130 procfs_entry, NULL);
3131 if (!m->mdt_mdsc_service) {
3132 CERROR("failed to start seq controller service\n");
3133 GOTO(err_mdt_svc, rc = -ENOMEM);
3136 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service, "mdt_mdsc");
3138 GOTO(err_mdt_svc, rc);
3141 * metadata sequence server service configuration
3143 conf = (typeof(conf)) {
3144 .psc_nbufs = MDS_NBUFS,
3145 .psc_bufsize = MDS_BUFSIZE,
3146 .psc_max_req_size = SEQ_MAXREQSIZE,
3147 .psc_max_reply_size = SEQ_MAXREPSIZE,
3148 .psc_req_portal = SEQ_METADATA_PORTAL,
3149 .psc_rep_portal = MDC_REPLY_PORTAL,
3150 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3151 .psc_num_threads = SEQ_NUM_THREADS,
3152 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3155 m->mdt_mdss_service =
3156 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3157 LUSTRE_MDT_NAME"_mdss",
3158 procfs_entry, NULL);
3159 if (!m->mdt_mdss_service) {
3160 CERROR("failed to start metadata seq server service\n");
3161 GOTO(err_mdt_svc, rc = -ENOMEM);
3164 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service, "mdt_mdss");
3166 GOTO(err_mdt_svc, rc);
3170 * Data sequence server service configuration. We want to have really
3171 * cluster-wide sequences space. This is why we start only one sequence
3172 * controller which manages space.
3174 conf = (typeof(conf)) {
3175 .psc_nbufs = MDS_NBUFS,
3176 .psc_bufsize = MDS_BUFSIZE,
3177 .psc_max_req_size = SEQ_MAXREQSIZE,
3178 .psc_max_reply_size = SEQ_MAXREPSIZE,
3179 .psc_req_portal = SEQ_DATA_PORTAL,
3180 .psc_rep_portal = OSC_REPLY_PORTAL,
3181 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3182 .psc_num_threads = SEQ_NUM_THREADS,
3183 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3186 m->mdt_dtss_service =
3187 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3188 LUSTRE_MDT_NAME"_dtss",
3189 procfs_entry, NULL);
3190 if (!m->mdt_dtss_service) {
3191 CERROR("failed to start data seq server service\n");
3192 GOTO(err_mdt_svc, rc = -ENOMEM);
3195 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service, "mdt_dtss");
3197 GOTO(err_mdt_svc, rc);
3199 /* FLD service start */
3200 conf = (typeof(conf)) {
3201 .psc_nbufs = MDS_NBUFS,
3202 .psc_bufsize = MDS_BUFSIZE,
3203 .psc_max_req_size = FLD_MAXREQSIZE,
3204 .psc_max_reply_size = FLD_MAXREPSIZE,
3205 .psc_req_portal = FLD_REQUEST_PORTAL,
3206 .psc_rep_portal = MDC_REPLY_PORTAL,
3207 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3208 .psc_num_threads = FLD_NUM_THREADS,
3209 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
3212 m->mdt_fld_service =
3213 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3214 LUSTRE_MDT_NAME"_fld",
3215 procfs_entry, NULL);
3216 if (!m->mdt_fld_service) {
3217 CERROR("failed to start fld service\n");
3218 GOTO(err_mdt_svc, rc = -ENOMEM);
3221 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service, "mdt_fld");
3223 GOTO(err_mdt_svc, rc);
3228 mdt_stop_ptlrpc_service(m);
3233 static void mdt_stack_fini(const struct lu_env *env,
3234 struct mdt_device *m, struct lu_device *top)
3236 struct lu_device *d = top, *n;
3237 struct lustre_cfg_bufs *bufs;
3238 struct lustre_cfg *lcfg;
3239 struct mdt_thread_info *info;
3242 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3243 LASSERT(info != NULL);
3245 bufs = &info->mti_u.bufs;
3246 /* process cleanup, pass mdt obd name to get obd umount flags */
3247 lustre_cfg_bufs_reset(bufs, m->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3248 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3250 CERROR("Cannot alloc lcfg!\n");
3254 top->ld_ops->ldo_process_config(env, top, lcfg);
3255 lustre_cfg_free(lcfg);
3257 lu_site_purge(env, top->ld_site, ~0);
3259 struct obd_type *type;
3260 struct lu_device_type *ldt = d->ld_type;
3262 /* each fini() returns next device in stack of layers
3263 * * so we can avoid the recursion */
3264 n = ldt->ldt_ops->ldto_device_fini(env, d);
3266 ldt->ldt_ops->ldto_device_free(env, d);
3267 type = ldt->ldt_obd_type;
3269 class_put_type(type);
3271 /* switch to the next device in the layer */
3274 m->mdt_child = NULL;
3277 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3278 const char *typename,
3279 struct lu_device *child,
3280 struct lustre_cfg *cfg)
3282 const char *dev = lustre_cfg_string(cfg, 0);
3283 struct obd_type *type;
3284 struct lu_device_type *ldt;
3285 struct lu_device *d;
3290 type = class_get_type(typename);
3292 CERROR("Unknown type: '%s'\n", typename);
3293 GOTO(out, rc = -ENODEV);
3296 rc = lu_context_refill(&env->le_ctx);
3298 CERROR("Failure to refill context: '%d'\n", rc);
3302 if (env->le_ses != NULL) {
3303 rc = lu_context_refill(env->le_ses);
3305 CERROR("Failure to refill session: '%d'\n", rc);
3312 CERROR("type: '%s'\n", typename);
3313 GOTO(out_type, rc = -EINVAL);
3316 ldt->ldt_obd_type = type;
3317 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3319 CERROR("Cannot allocate device: '%s'\n", typename);
3320 GOTO(out_type, rc = -ENODEV);
3323 LASSERT(child->ld_site);
3324 d->ld_site = child->ld_site;
3327 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3329 CERROR("can't init device '%s', rc %d\n", typename, rc);
3330 GOTO(out_alloc, rc);
3337 ldt->ldt_ops->ldto_device_free(env, d);
3340 class_put_type(type);
3345 static int mdt_stack_init(const struct lu_env *env,
3346 struct mdt_device *m, struct lustre_cfg *cfg)
3348 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3349 struct lu_device *tmp;
3350 struct md_device *md;
3354 /* init the stack */
3355 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3357 RETURN(PTR_ERR(tmp));
3359 m->mdt_bottom = lu2dt_dev(tmp);
3361 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3363 GOTO(out, rc = PTR_ERR(tmp));
3368 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3370 GOTO(out, rc = PTR_ERR(tmp));
3373 /*set mdd upcall device*/
3374 md->md_upcall.mu_upcall_dev = lu2md_dev(d);
3377 /*set cmm upcall device*/
3378 md->md_upcall.mu_upcall_dev = &m->mdt_md_dev;
3380 m->mdt_child = lu2md_dev(d);
3382 /* process setup config */
3383 tmp = &m->mdt_md_dev.md_lu_dev;
3384 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3387 /* fini from last known good lu_device */
3389 mdt_stack_fini(env, m, d);
3394 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3396 struct md_device *next = m->mdt_child;
3397 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3398 struct lu_site *ls = d->ld_site;
3402 mdt_fs_cleanup(env, m);
3404 ping_evictor_stop();
3405 mdt_stop_ptlrpc_service(m);
3407 cleanup_capas(CAPA_SITE_SERVER);
3408 del_timer(&m->mdt_ck_timer);
3409 mdt_ck_thread_stop(m);
3411 upcall_cache_cleanup(m->mdt_rmtacl_cache);
3412 m->mdt_rmtacl_cache = NULL;
3414 upcall_cache_cleanup(m->mdt_identity_cache);
3415 m->mdt_identity_cache = NULL;
3417 if (m->mdt_namespace != NULL) {
3418 ldlm_namespace_free(m->mdt_namespace, 0);
3419 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3422 mdt_seq_fini(env, m);
3423 mdt_seq_fini_cli(m);
3424 mdt_fld_fini(env, m);
3425 lprocfs_obd_cleanup(d->ld_obd);
3427 if (m->mdt_rootsquash_info) {
3428 OBD_FREE_PTR(m->mdt_rootsquash_info);
3429 m->mdt_rootsquash_info = NULL;
3432 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3433 cleanup_capas(CAPA_SITE_SERVER);
3434 del_timer(&m->mdt_ck_timer);
3435 mdt_ck_thread_stop(m);
3437 /* finish the stack */
3438 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3441 if (!list_empty(&ls->ls_lru) ||
3442 ls->ls_total != 0 || ls->ls_busy != 0) {
3444 * Uh-oh, objects still exist.
3446 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
3448 lu_site_print(env, ls, &cookie, lu_cdebug_printer);
3455 LASSERT(atomic_read(&d->ld_ref) == 0);
3456 md_device_fini(&m->mdt_md_dev);
3461 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3471 while (*p && *p != ',')
3475 if ((len == sizeof("user_xattr") - 1) &&
3476 (memcmp(options, "user_xattr", len) == 0)) {
3477 m->mdt_opts.mo_user_xattr = 1;
3478 LCONSOLE_INFO("Enabling user_xattr\n");
3479 } else if ((len == sizeof("nouser_xattr") - 1) &&
3480 (memcmp(options, "nouser_xattr", len) == 0)) {
3481 m->mdt_opts.mo_user_xattr = 0;
3482 LCONSOLE_INFO("Disabling user_xattr\n");
3483 } else if ((len == sizeof("acl") - 1) &&
3484 (memcmp(options, "acl", len) == 0)) {
3485 #ifdef CONFIG_FS_POSIX_ACL
3486 m->mdt_opts.mo_acl = 1;
3487 LCONSOLE_INFO("Enabling ACL\n");
3489 m->mdt_opts.mo_acl = 0;
3490 CWARN("ignoring unsupported acl mount option\n");
3491 LCONSOLE_INFO("Disabling ACL\n");
3493 } else if ((len == sizeof("noacl") - 1) &&
3494 (memcmp(options, "noacl", len) == 0)) {
3495 #ifdef CONFIG_FS_POSIX_ACL
3496 m->mdt_opts.mo_acl = 0;
3497 LCONSOLE_INFO("Disabling ACL\n");
3505 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3507 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3508 struct lu_device_type *ldt, struct lustre_cfg *cfg)
3510 struct lprocfs_static_vars lvars;
3511 struct mdt_thread_info *info;
3512 struct obd_device *obd;
3513 const char *dev = lustre_cfg_string(cfg, 0);
3514 const char *num = lustre_cfg_string(cfg, 2);
3515 struct lustre_mount_info *lmi;
3516 struct lustre_sb_info *lsi;
3517 struct vfsmount *mnt;
3522 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3523 LASSERT(info != NULL);
3525 obd = class_name2obd(dev);
3526 LASSERT(obd != NULL);
3528 spin_lock_init(&m->mdt_transno_lock);
3530 m->mdt_max_mdsize = MAX_MD_SIZE;
3531 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3533 m->mdt_opts.mo_user_xattr = 0;
3534 m->mdt_opts.mo_acl = 0;
3535 lmi = server_get_mount_2(dev);
3537 CERROR("Cannot get mount info for %s!\n", dev);
3540 lsi = s2lsi(lmi->lmi_sb);
3541 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3544 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
3545 obd->obd_lvfs_ctxt.pwdmnt = mnt;
3546 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
3547 obd->obd_lvfs_ctxt.fs = get_ds();
3549 server_put_mount_2(dev, mnt);
3552 spin_lock_init(&m->mdt_ioepoch_lock);
3553 m->mdt_opts.mo_compat_resname = 0;
3554 m->mdt_capa_timeout = CAPA_TIMEOUT;
3555 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3556 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3557 obd->obd_replayable = 1;
3558 spin_lock_init(&m->mdt_client_bitmap_lock);
3564 md_device_init(&m->mdt_md_dev, ldt);
3565 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3566 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3567 /* set this lu_device to obd, because error handling need it */
3568 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3570 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3572 CERROR("can't init lu_site, rc %d\n", rc);
3573 GOTO(err_free_site, rc);
3576 lprocfs_init_vars(mdt, &lvars);
3577 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3579 CERROR("can't init lprocfs, rc %d\n", rc);
3580 GOTO(err_fini_site, rc);
3582 ptlrpc_lprocfs_register_obd(obd);
3584 /* set server index */
3586 s->ls_node_id = simple_strtol(num, NULL, 10);
3588 /* init the stack */
3589 rc = mdt_stack_init(env, m, cfg);
3591 CERROR("can't init device stack, rc %d\n", rc);
3592 GOTO(err_fini_proc, rc);
3595 rc = mdt_fld_init(env, obd->obd_name, m);
3597 GOTO(err_fini_stack, rc);
3599 rc = mdt_seq_init(env, obd->obd_name, m);
3601 GOTO(err_fini_fld, rc);
3603 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
3604 LUSTRE_MDT_NAME"-%p", m);
3605 m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
3606 LDLM_NAMESPACE_SERVER);
3607 if (m->mdt_namespace == NULL)
3608 GOTO(err_fini_seq, rc = -ENOMEM);
3610 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
3611 /* set obd_namespace for compatibility with old code */
3612 obd->obd_namespace = m->mdt_namespace;
3614 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,
3616 &mdt_identity_upcall_cache_ops);
3617 if (IS_ERR(m->mdt_identity_cache)) {
3618 rc = PTR_ERR(m->mdt_identity_cache);
3619 m->mdt_identity_cache = NULL;
3620 GOTO(err_free_ns, rc);
3623 m->mdt_rmtacl_cache = upcall_cache_init(obd->obd_name,
3624 MDT_RMTACL_UPCALL_PATH,
3625 &mdt_rmtacl_upcall_cache_ops);
3626 if (IS_ERR(m->mdt_rmtacl_cache)) {
3627 rc = PTR_ERR(m->mdt_rmtacl_cache);
3628 m->mdt_rmtacl_cache = NULL;
3629 GOTO(err_free_ns, rc);
3632 m->mdt_ck_timer.function = mdt_ck_timer_callback;
3633 m->mdt_ck_timer.data = (unsigned long)m;
3634 init_timer(&m->mdt_ck_timer);
3635 rc = mdt_ck_thread_start(m);
3637 GOTO(err_free_ns, rc);
3639 rc = mdt_start_ptlrpc_service(m);
3643 ping_evictor_start();
3645 rc = mdt_fs_setup(env, m, obd);
3647 GOTO(err_stop_service, rc);
3649 rc = lu_site_init_finish(s);
3651 GOTO(err_fs_cleanup, rc);
3653 if (obd->obd_recovering == 0)
3654 mdt_postrecov(env, m);
3656 mdt_init_capa_ctxt(env, m);
3660 mdt_fs_cleanup(env, m);
3662 mdt_stop_ptlrpc_service(m);
3664 del_timer(&m->mdt_ck_timer);
3665 mdt_ck_thread_stop(m);
3667 upcall_cache_cleanup(m->mdt_rmtacl_cache);
3668 m->mdt_rmtacl_cache = NULL;
3669 upcall_cache_cleanup(m->mdt_identity_cache);
3670 m->mdt_identity_cache = NULL;
3671 ldlm_namespace_free(m->mdt_namespace, 0);
3672 obd->obd_namespace = m->mdt_namespace = NULL;
3674 mdt_seq_fini(env, m);
3676 mdt_fld_fini(env, m);
3678 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3680 lprocfs_obd_cleanup(obd);
3686 md_device_fini(&m->mdt_md_dev);
3690 /* used by MGS to process specific configurations */
3691 static int mdt_process_config(const struct lu_env *env,
3692 struct lu_device *d, struct lustre_cfg *cfg)
3694 struct mdt_device *m = mdt_dev(d);
3695 struct md_device *md_next = m->mdt_child;
3696 struct lu_device *next = md2lu_dev(md_next);
3700 switch (cfg->lcfg_command) {
3702 struct lprocfs_static_vars lvars;
3703 struct obd_device *obd = d->ld_obd;
3705 lprocfs_init_vars(mdt, &lvars);
3706 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
3708 /* others are passed further */
3709 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3714 * Add mdc hook to get first MDT uuid and connect it to
3715 * ls->controller to use for seq manager.
3717 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3719 CERROR("Can't add mdc, rc %d\n", rc);
3721 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
3724 /* others are passed further */
3725 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3731 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
3732 const struct lu_object_header *hdr,
3733 struct lu_device *d)
3735 struct mdt_object *mo;
3741 struct lu_object *o;
3742 struct lu_object_header *h;
3744 o = &mo->mot_obj.mo_lu;
3745 h = &mo->mot_header;
3746 lu_object_header_init(h);
3747 lu_object_init(o, h, d);
3748 lu_object_add_top(h, o);
3749 o->lo_ops = &mdt_obj_ops;
3755 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
3757 struct mdt_device *d = mdt_dev(o->lo_dev);
3758 struct lu_device *under;
3759 struct lu_object *below;
3763 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
3764 PFID(lu_object_fid(o)));
3766 under = &d->mdt_child->md_lu_dev;
3767 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
3768 if (below != NULL) {
3769 lu_object_add(o, below);
3776 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
3778 struct mdt_object *mo = mdt_obj(o);
3779 struct lu_object_header *h;
3783 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
3784 PFID(lu_object_fid(o)));
3787 lu_object_header_fini(h);
3792 static int mdt_object_print(const struct lu_env *env, void *cookie,
3793 lu_printer_t p, const struct lu_object *o)
3795 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
3798 static struct lu_device_operations mdt_lu_ops = {
3799 .ldo_object_alloc = mdt_object_alloc,
3800 .ldo_process_config = mdt_process_config
3803 static struct lu_object_operations mdt_obj_ops = {
3804 .loo_object_init = mdt_object_init,
3805 .loo_object_free = mdt_object_free,
3806 .loo_object_print = mdt_object_print
3809 /* mds_connect_internal */
3810 static int mdt_connect_internal(struct obd_export *exp,
3811 struct mdt_device *mdt,
3812 struct obd_connect_data *data)
3817 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
3818 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
3820 /* If no known bits (which should not happen, probably,
3821 as everybody should support LOOKUP and UPDATE bits at least)
3822 revert to compat mode with plain locks. */
3823 if (!data->ocd_ibits_known &&
3824 data->ocd_connect_flags & OBD_CONNECT_IBITS)
3825 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
3827 if (!mdt->mdt_opts.mo_acl)
3828 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
3830 if (!mdt->mdt_opts.mo_user_xattr)
3831 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
3833 if (!mdt->mdt_opts.mo_mds_capa)
3834 data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
3836 if (!mdt->mdt_opts.mo_oss_capa)
3837 data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
3839 exp->exp_connect_flags = data->ocd_connect_flags;
3840 data->ocd_version = LUSTRE_VERSION_CODE;
3841 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
3845 if (mdt->mdt_opts.mo_acl &&
3846 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
3847 CWARN("%s: MDS requires ACL support but client does not\n",
3848 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3853 flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
3854 if ((exp->exp_connect_flags & flags) == flags) {
3855 CWARN("%s: both local and remote client flags are set\n",
3856 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3860 if (mdt->mdt_opts.mo_mds_capa &&
3861 ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
3862 CWARN("%s: MDS requires capability support, but client not\n",
3863 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3867 if (mdt->mdt_opts.mo_oss_capa &&
3868 ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
3869 CWARN("%s: MDS requires OSS capability support, "
3871 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3878 /* mds_connect copy */
3879 static int mdt_obd_connect(const struct lu_env *env,
3880 struct lustre_handle *conn, struct obd_device *obd,
3881 struct obd_uuid *cluuid,
3882 struct obd_connect_data *data)
3884 struct mdt_export_data *med;
3885 struct mdt_client_data *mcd;
3886 struct obd_export *exp;
3887 struct mdt_device *mdt;
3891 LASSERT(env != NULL);
3892 if (!conn || !obd || !cluuid)
3895 mdt = mdt_dev(obd->obd_lu_dev);
3897 rc = class_connect(conn, obd, cluuid);
3901 exp = class_conn2export(conn);
3902 LASSERT(exp != NULL);
3903 med = &exp->exp_mdt_data;
3905 rc = mdt_connect_internal(exp, mdt, data);
3909 memcpy(mcd->mcd_uuid, cluuid, sizeof mcd->mcd_uuid);
3911 rc = mdt_client_new(env, mdt, med);
3914 med->med_mcd = NULL;
3921 class_disconnect(exp);
3923 class_export_put(exp);
3928 static int mdt_obd_reconnect(struct obd_export *exp, struct obd_device *obd,
3929 struct obd_uuid *cluuid,
3930 struct obd_connect_data *data)
3935 if (exp == NULL || obd == NULL || cluuid == NULL)
3938 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
3943 static int mdt_obd_disconnect(struct obd_export *exp)
3945 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
3950 class_export_get(exp);
3952 /* Disconnect early so that clients can't keep using export */
3953 rc = class_disconnect(exp);
3954 if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
3955 ldlm_cancel_locks_for_export(exp);
3957 /* complete all outstanding replies */
3958 spin_lock(&exp->exp_lock);
3959 while (!list_empty(&exp->exp_outstanding_replies)) {
3960 struct ptlrpc_reply_state *rs =
3961 list_entry(exp->exp_outstanding_replies.next,
3962 struct ptlrpc_reply_state, rs_exp_list);
3963 struct ptlrpc_service *svc = rs->rs_service;
3965 spin_lock(&svc->srv_lock);
3966 list_del_init(&rs->rs_exp_list);
3967 ptlrpc_schedule_difficult_reply(rs);
3968 spin_unlock(&svc->srv_lock);
3970 spin_unlock(&exp->exp_lock);
3972 class_export_put(exp);
3976 /* FIXME: Can we avoid using these two interfaces? */
3977 static int mdt_init_export(struct obd_export *exp)
3979 struct mdt_export_data *med = &exp->exp_mdt_data;
3982 INIT_LIST_HEAD(&med->med_open_head);
3983 spin_lock_init(&med->med_open_lock);
3984 exp->exp_connecting = 1;
3988 static int mdt_destroy_export(struct obd_export *export)
3990 struct mdt_export_data *med;
3991 struct obd_device *obd = export->exp_obd;
3992 struct mdt_device *mdt;
3993 struct mdt_thread_info *info;
4001 med = &export->exp_mdt_data;
4002 if (med->med_rmtclient)
4003 mdt_cleanup_idmap(med);
4005 target_destroy_export(export);
4007 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
4010 mdt = mdt_dev(obd->obd_lu_dev);
4011 LASSERT(mdt != NULL);
4013 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4017 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4018 LASSERT(info != NULL);
4019 memset(info, 0, sizeof *info);
4020 info->mti_env = &env;
4021 info->mti_mdt = mdt;
4023 ma = &info->mti_attr;
4024 lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4025 cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4026 OBD_ALLOC(ma->ma_lmm, lmm_size);
4027 OBD_ALLOC(ma->ma_cookie, cookie_size);
4029 if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4030 GOTO(out, rc = -ENOMEM);
4031 ma->ma_need = MA_LOV | MA_COOKIE;
4033 /* Close any open files (which may also cause orphan unlinking). */
4034 spin_lock(&med->med_open_lock);
4035 while (!list_empty(&med->med_open_head)) {
4036 struct list_head *tmp = med->med_open_head.next;
4037 struct mdt_file_data *mfd =
4038 list_entry(tmp, struct mdt_file_data, mfd_list);
4040 /* Remove mfd handle so it can't be found again.
4041 * We are consuming the mfd_list reference here. */
4042 class_handle_unhash(&mfd->mfd_handle);
4043 list_del_init(&mfd->mfd_list);
4044 spin_unlock(&med->med_open_lock);
4045 mdt_mfd_close(info, mfd);
4046 /* TODO: if we close the unlinked file,
4047 * we need to remove it's objects from OST */
4048 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4049 spin_lock(&med->med_open_lock);
4050 ma->ma_lmm_size = lmm_size;
4051 ma->ma_cookie_size = cookie_size;
4052 ma->ma_need = MA_LOV | MA_COOKIE;
4055 spin_unlock(&med->med_open_lock);
4056 info->mti_mdt = NULL;
4057 mdt_client_del(&env, mdt, med);
4062 OBD_FREE(ma->ma_lmm, lmm_size);
4066 OBD_FREE(ma->ma_cookie, cookie_size);
4067 ma->ma_cookie = NULL;
4074 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4075 enum md_upcall_event ev)
4077 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4078 struct md_device *next = m->mdt_child;
4079 struct mdt_thread_info *mti;
4085 rc = next->md_ops->mdo_maxsize_get(env, next,
4087 &m->mdt_max_cookiesize);
4088 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4089 m->mdt_max_mdsize, m->mdt_max_cookiesize);
4092 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4093 mti->mti_no_need_trans = 1;
4094 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4097 CERROR("invalid event\n");
4104 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4105 void *karg, void *uarg)
4108 struct obd_device *obd= exp->exp_obd;
4109 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4110 struct dt_device *dt = mdt->mdt_bottom;
4114 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4115 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4121 rc = dt->dd_ops->dt_sync(&env, dt);
4124 case OBD_IOC_SET_READONLY:
4125 rc = dt->dd_ops->dt_sync(&env, dt);
4126 dt->dd_ops->dt_ro(&env, dt);
4129 case OBD_IOC_ABORT_RECOVERY:
4130 CERROR("aborting recovery for device %s\n", obd->obd_name);
4131 target_stop_recovery_thread(obd);
4135 CERROR("not supported cmd = %d for device %s\n",
4136 cmd, obd->obd_name);
4144 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4146 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4149 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4153 int mdt_obd_postrecov(struct obd_device *obd)
4158 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4161 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4166 static struct obd_ops mdt_obd_device_ops = {
4167 .o_owner = THIS_MODULE,
4168 .o_connect = mdt_obd_connect,
4169 .o_reconnect = mdt_obd_reconnect,
4170 .o_disconnect = mdt_obd_disconnect,
4171 .o_init_export = mdt_init_export,
4172 .o_destroy_export = mdt_destroy_export,
4173 .o_iocontrol = mdt_iocontrol,
4174 .o_postrecov = mdt_obd_postrecov
4178 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4179 struct lu_device *d)
4181 struct mdt_device *m = mdt_dev(d);
4187 static void mdt_device_free(const struct lu_env *env, struct lu_device *d)
4189 struct mdt_device *m = mdt_dev(d);
4194 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4195 struct lu_device_type *t,
4196 struct lustre_cfg *cfg)
4198 struct lu_device *l;
4199 struct mdt_device *m;
4205 l = &m->mdt_md_dev.md_lu_dev;
4206 rc = mdt_init0(env, m, t, cfg);
4212 m->mdt_md_dev.md_upcall.mu_upcall = mdt_upcall;
4214 l = ERR_PTR(-ENOMEM);
4219 * context key constructor/destructor
4221 static void *mdt_key_init(const struct lu_context *ctx,
4222 struct lu_context_key *key)
4224 struct mdt_thread_info *info;
4227 * check that no high order allocations are incurred.
4229 CLASSERT(CFS_PAGE_SIZE >= sizeof *info);
4230 OBD_ALLOC_PTR(info);
4232 info = ERR_PTR(-ENOMEM);
4236 static void mdt_key_fini(const struct lu_context *ctx,
4237 struct lu_context_key *key, void *data)
4239 struct mdt_thread_info *info = data;
4243 struct lu_context_key mdt_thread_key = {
4244 .lct_tags = LCT_MD_THREAD,
4245 .lct_init = mdt_key_init,
4246 .lct_fini = mdt_key_fini
4249 static void *mdt_txn_key_init(const struct lu_context *ctx,
4250 struct lu_context_key *key)
4252 struct mdt_txn_info *txi;
4255 * check that no high order allocations are incurred.
4257 CLASSERT(CFS_PAGE_SIZE >= sizeof *txi);
4260 txi = ERR_PTR(-ENOMEM);
4264 static void mdt_txn_key_fini(const struct lu_context *ctx,
4265 struct lu_context_key *key, void *data)
4267 struct mdt_txn_info *txi = data;
4271 struct lu_context_key mdt_txn_key = {
4272 .lct_tags = LCT_TX_HANDLE,
4273 .lct_init = mdt_txn_key_init,
4274 .lct_fini = mdt_txn_key_fini
4277 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4279 return md_ucred(info->mti_env);
4282 static int mdt_type_init(struct lu_device_type *t)
4286 rc = lu_context_key_register(&mdt_thread_key);
4288 rc = lu_context_key_register(&mdt_txn_key);
4292 static void mdt_type_fini(struct lu_device_type *t)
4294 lu_context_key_degister(&mdt_thread_key);
4295 lu_context_key_degister(&mdt_txn_key);
4298 static struct lu_device_type_operations mdt_device_type_ops = {
4299 .ldto_init = mdt_type_init,
4300 .ldto_fini = mdt_type_fini,
4302 .ldto_device_alloc = mdt_device_alloc,
4303 .ldto_device_free = mdt_device_free,
4304 .ldto_device_fini = mdt_device_fini
4307 static struct lu_device_type mdt_device_type = {
4308 .ldt_tags = LU_DEVICE_MD,
4309 .ldt_name = LUSTRE_MDT_NAME,
4310 .ldt_ops = &mdt_device_type_ops,
4311 .ldt_ctx_tags = LCT_MD_THREAD
4314 static int __init mdt_mod_init(void)
4316 struct lprocfs_static_vars lvars;
4319 mdt_num_threads = MDT_NUM_THREADS;
4320 lprocfs_init_vars(mdt, &lvars);
4321 rc = class_register_type(&mdt_obd_device_ops, NULL,
4322 lvars.module_vars, LUSTRE_MDT_NAME,
4328 static void __exit mdt_mod_exit(void)
4330 class_unregister_type(LUSTRE_MDT_NAME);
4334 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
4335 [prefix ## _ ## opc - prefix ## _ ## base] = { \
4337 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
4338 .mh_opc = prefix ## _ ## opc, \
4339 .mh_flags = flags, \
4344 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
4345 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4347 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
4348 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4350 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
4351 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4353 * Request with a format known in advance
4355 #define DEF_MDT_HNDL_F(flags, name, fn) \
4356 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4358 #define DEF_SEQ_HNDL_F(flags, name, fn) \
4359 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4361 #define DEF_FLD_HNDL_F(flags, name, fn) \
4362 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4364 * Request with a format we do not yet know
4366 #define DEF_MDT_HNDL_0(flags, name, fn) \
4367 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4369 static struct mdt_handler mdt_mds_ops[] = {
4370 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4371 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
4372 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
4373 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
4374 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4375 DEF_MDT_HNDL_F(HABEO_CORPUS|MUTABOR, SETXATTR, mdt_setxattr),
4376 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
4377 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
4378 DEF_MDT_HNDL_F(0 |MUTABOR,
4380 DEF_MDT_HNDL_F(HABEO_CORPUS , CLOSE, mdt_close),
4381 DEF_MDT_HNDL_F(HABEO_CORPUS , DONE_WRITING, mdt_done_writing),
4382 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
4383 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
4384 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
4385 DEF_MDT_HNDL_0(0, QUOTACHECK, mdt_quotacheck_handle),
4386 DEF_MDT_HNDL_0(0, QUOTACTL, mdt_quotactl_handle)
4389 #define DEF_OBD_HNDL(flags, name, fn) \
4390 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4393 static struct mdt_handler mdt_obd_ops[] = {
4394 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
4395 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
4396 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
4399 #define DEF_DLM_HNDL_0(flags, name, fn) \
4400 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4401 #define DEF_DLM_HNDL_F(flags, name, fn) \
4402 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4404 static struct mdt_handler mdt_dlm_ops[] = {
4405 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
4406 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
4407 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
4408 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
4411 static struct mdt_handler mdt_llog_ops[] = {
4414 #define DEF_SEC_CTX_HNDL(name, fn) \
4415 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4417 static struct mdt_handler mdt_sec_ctx_ops[] = {
4418 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
4419 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
4420 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
4423 static struct mdt_opc_slice mdt_regular_handlers[] = {
4425 .mos_opc_start = MDS_GETATTR,
4426 .mos_opc_end = MDS_LAST_OPC,
4427 .mos_hs = mdt_mds_ops
4430 .mos_opc_start = OBD_PING,
4431 .mos_opc_end = OBD_LAST_OPC,
4432 .mos_hs = mdt_obd_ops
4435 .mos_opc_start = LDLM_ENQUEUE,
4436 .mos_opc_end = LDLM_LAST_OPC,
4437 .mos_hs = mdt_dlm_ops
4440 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4441 .mos_opc_end = LLOG_LAST_OPC,
4442 .mos_hs = mdt_llog_ops
4445 .mos_opc_start = SEC_CTX_INIT,
4446 .mos_opc_end = SEC_LAST_OPC,
4447 .mos_hs = mdt_sec_ctx_ops
4454 static struct mdt_handler mdt_readpage_ops[] = {
4455 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4456 #ifdef HAVE_SPLIT_SUPPORT
4457 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4461 * XXX: this is ugly and should be fixed one day, see mdc_close() for
4462 * detailed comments. --umka
4464 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
4465 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
4468 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4470 .mos_opc_start = MDS_GETATTR,
4471 .mos_opc_end = MDS_LAST_OPC,
4472 .mos_hs = mdt_readpage_ops
4479 static struct mdt_handler mdt_seq_ops[] = {
4480 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
4483 static struct mdt_opc_slice mdt_seq_handlers[] = {
4485 .mos_opc_start = SEQ_QUERY,
4486 .mos_opc_end = SEQ_LAST_OPC,
4487 .mos_hs = mdt_seq_ops
4494 static struct mdt_handler mdt_fld_ops[] = {
4495 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
4498 static struct mdt_opc_slice mdt_fld_handlers[] = {
4500 .mos_opc_start = FLD_QUERY,
4501 .mos_opc_end = FLD_LAST_OPC,
4502 .mos_hs = mdt_fld_ops
4509 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4510 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
4511 MODULE_LICENSE("GPL");
4513 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
4514 "number of mdt service threads to start");
4516 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);