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 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
294 RETURN(err_serious(-ENOMEM));
296 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
297 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
301 repbody->valid |= OBD_MD_FLID;
303 if (mdt->mdt_opts.mo_mds_capa) {
304 struct mdt_object *root;
305 struct lustre_capa *capa;
307 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
309 RETURN(PTR_ERR(root));
311 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
313 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
315 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
317 mdt_object_put(info->mti_env, root);
319 repbody->valid |= OBD_MD_FLMDSCAPA;
325 static int mdt_statfs(struct mdt_thread_info *info)
327 struct md_device *next = info->mti_mdt->mdt_child;
328 struct obd_statfs *osfs;
333 /* This will trigger a watchdog timeout */
334 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
335 (MDT_SERVICE_WATCHDOG_TIMEOUT / 1000) + 1);
338 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
339 rc = err_serious(-ENOMEM);
341 osfs = req_capsule_server_get(&info->mti_pill,&RMF_OBD_STATFS);
342 /* XXX max_age optimisation is needed here. See mds_statfs */
343 rc = next->md_ops->mdo_statfs(info->mti_env, next,
345 statfs_pack(osfs, &info->mti_u.ksfs);
350 void mdt_pack_size2body(struct mdt_body *b, const struct lu_attr *attr,
351 struct mdt_object *o)
353 /* Check if Size-on-MDS is enabled. */
354 if (S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
355 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
356 b->size = attr->la_size;
357 b->blocks = attr->la_blocks;
361 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
362 const struct lu_attr *attr, const struct lu_fid *fid)
364 /*XXX should pack the reply body according to lu_valid*/
365 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
366 OBD_MD_FLGID | OBD_MD_FLTYPE |
367 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
368 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
370 if (!S_ISREG(attr->la_mode))
371 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
373 b->atime = attr->la_atime;
374 b->mtime = attr->la_mtime;
375 b->ctime = attr->la_ctime;
376 b->mode = attr->la_mode;
377 b->size = attr->la_size;
378 b->blocks = attr->la_blocks;
379 b->uid = attr->la_uid;
380 b->gid = attr->la_gid;
381 b->flags = attr->la_flags;
382 b->nlink = attr->la_nlink;
383 b->rdev = attr->la_rdev;
387 b->valid |= OBD_MD_FLID;
388 CDEBUG(D_INODE, ""DFID": nlink=%d, mode=%o, size="LPU64"\n",
389 PFID(fid), b->nlink, b->mode, b->size);
393 mdt_body_reverse_idmap(info, b);
396 static inline int mdt_body_has_lov(const struct lu_attr *la,
397 const struct mdt_body *body)
399 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
400 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
403 static int mdt_getattr_internal(struct mdt_thread_info *info,
404 struct mdt_object *o)
406 struct md_object *next = mdt_object_child(o);
407 const struct mdt_body *reqbody = info->mti_body;
408 struct ptlrpc_request *req = mdt_info_req(info);
409 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
410 struct md_attr *ma = &info->mti_attr;
411 struct lu_attr *la = &ma->ma_attr;
412 struct req_capsule *pill = &info->mti_pill;
413 const struct lu_env *env = info->mti_env;
414 struct mdt_body *repbody;
415 struct lu_buf *buffer = &info->mti_buf;
419 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
420 RETURN(err_serious(-ENOMEM));
422 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
424 if (reqbody->valid & OBD_MD_MEA) {
425 /* Assumption: MDT_MD size is enough for lmv size FIXME */
426 ma->ma_lmv = req_capsule_server_get(pill, &RMF_MDT_MD);
427 ma->ma_lmv_size = req_capsule_get_size(pill, &RMF_MDT_MD,
429 ma->ma_need = MA_INODE | MA_LMV;
431 ma->ma_need = MA_INODE | MA_LOV ;
432 ma->ma_lmm = req_capsule_server_get(pill, &RMF_MDT_MD);
433 ma->ma_lmm_size = req_capsule_get_size(pill, &RMF_MDT_MD,
437 rc = mo_attr_get(env, next, ma);
438 if (rc == -EREMOTE) {
439 /* This object is located on remote node.*/
440 repbody->fid1 = *mdt_object_fid(o);
441 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
444 CERROR("getattr error for "DFID": %d\n",
445 PFID(mdt_object_fid(o)), rc);
449 if (ma->ma_valid & MA_INODE)
450 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
454 if (mdt_body_has_lov(la, reqbody)) {
455 if (ma->ma_valid & MA_LOV) {
456 LASSERT(ma->ma_lmm_size);
457 mdt_dump_lmm(D_INFO, ma->ma_lmm);
458 repbody->eadatasize = ma->ma_lmm_size;
459 if (S_ISDIR(la->la_mode))
460 repbody->valid |= OBD_MD_FLDIREA;
462 repbody->valid |= OBD_MD_FLEASIZE;
464 if (ma->ma_valid & MA_LMV) {
465 LASSERT(S_ISDIR(la->la_mode));
466 repbody->eadatasize = ma->ma_lmv_size;
467 repbody->valid |= OBD_MD_FLDIREA;
468 repbody->valid |= OBD_MD_MEA;
470 } else if (S_ISLNK(la->la_mode) &&
471 reqbody->valid & OBD_MD_LINKNAME) {
472 buffer->lb_buf = ma->ma_lmm;
473 buffer->lb_len = reqbody->eadatasize;
474 rc = mo_readlink(env, next, buffer);
476 CERROR("readlink failed: %d\n", rc);
479 repbody->valid |= OBD_MD_LINKNAME;
480 repbody->eadatasize = rc;
481 ((char*)ma->ma_lmm)[rc - 1] = 0; /* NULL terminate */
482 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
483 (char*)ma->ma_lmm, rc);
488 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
489 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
490 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
491 repbody->valid |= OBD_MD_FLMODEASIZE;
492 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
493 "MAX_COOKIE to : %d:%d\n",
495 repbody->max_cookiesize);
498 if (med->med_rmtclient && (reqbody->valid & OBD_MD_FLRMTPERM)) {
499 void *buf = req_capsule_server_get(pill, &RMF_ACL);
501 /* mdt_getattr_lock only */
502 rc = mdt_pack_remote_perm(info, o, buf);
504 repbody->valid &= ~OBD_MD_FLRMTPERM;
505 repbody->aclsize = 0;
508 repbody->valid |= OBD_MD_FLRMTPERM;
509 repbody->aclsize = sizeof(struct mdt_remote_perm);
512 #ifdef CONFIG_FS_POSIX_ACL
513 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
514 (reqbody->valid & OBD_MD_FLACL)) {
515 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
516 buffer->lb_len = req_capsule_get_size(pill,
517 &RMF_ACL, RCL_SERVER);
518 if (buffer->lb_len > 0) {
519 rc = mo_xattr_get(env, next, buffer,
520 XATTR_NAME_ACL_ACCESS);
522 if (rc == -ENODATA) {
523 repbody->aclsize = 0;
524 repbody->valid |= OBD_MD_FLACL;
526 } else if (rc == -EOPNOTSUPP) {
529 CERROR("got acl size: %d\n", rc);
532 repbody->aclsize = rc;
533 repbody->valid |= OBD_MD_FLACL;
540 if ((reqbody->valid & OBD_MD_FLMDSCAPA) &&
541 info->mti_mdt->mdt_opts.mo_mds_capa) {
542 struct lustre_capa *capa;
544 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
546 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
547 rc = mo_capa_get(env, next, capa, 0);
550 repbody->valid |= OBD_MD_FLMDSCAPA;
555 static int mdt_renew_capa(struct mdt_thread_info *info)
557 struct mdt_device *mdt = info->mti_mdt;
558 struct mdt_object *obj = info->mti_object;
559 struct mdt_body *body;
560 struct lustre_capa *capa, *c;
564 /* if object doesn't exist, or server has disabled capability,
565 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
568 if (!obj || !mdt->mdt_opts.mo_mds_capa)
571 body = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
572 LASSERT(body != NULL);
574 c = req_capsule_client_get(&info->mti_pill, &RMF_CAPA1);
577 capa = req_capsule_server_get(&info->mti_pill, &RMF_CAPA1);
581 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
583 body->valid |= OBD_MD_FLOSSCAPA;
588 static int mdt_getattr(struct mdt_thread_info *info)
590 struct mdt_object *obj = info->mti_object;
591 struct req_capsule *pill = &info->mti_pill;
592 struct mdt_body *reqbody;
593 struct mdt_body *repbody;
598 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
601 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
602 rc = req_capsule_pack(pill);
604 RETURN(err_serious(rc));
605 rc = mdt_renew_capa(info);
606 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 0, 0);
610 LASSERT(obj != NULL);
611 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
613 mode = lu_object_attr(&obj->mot_obj.mo_lu);
614 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
615 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize)) {
616 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
617 reqbody->eadatasize);
619 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
620 info->mti_mdt->mdt_max_mdsize);
623 rc = req_capsule_pack(pill);
625 RETURN(err_serious(rc));
627 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
629 repbody->eadatasize = 0;
630 repbody->aclsize = 0;
632 if (reqbody->valid & OBD_MD_FLRMTPERM) {
633 rc = mdt_init_ucred(info, reqbody);
638 /* don't check capability at all, because rename might
639 * getattr for remote obj, and at that time no capability
641 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
642 rc = mdt_getattr_internal(info, obj);
643 if (reqbody->valid & OBD_MD_FLRMTPERM)
644 mdt_exit_ucred(info);
647 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 1, 0);
651 static int mdt_is_subdir(struct mdt_thread_info *info)
653 struct mdt_object *o = info->mti_object;
654 struct req_capsule *pill = &info->mti_pill;
655 const struct mdt_body *body = info->mti_body;
656 struct mdt_body *repbody;
661 LASSERT(lu_object_assert_exists(&o->mot_obj.mo_lu));
663 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
666 * We save last checked parent fid to @repbody->fid1 for remote
669 LASSERT(fid_is_sane(&body->fid2));
670 mdt_set_capainfo(info, 0, &body->fid1, BYPASS_CAPA);
671 mdt_set_capainfo(info, 1, &body->fid2, BYPASS_CAPA);
673 LASSERT(mdt_object_exists(o) > 0);
674 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
675 &body->fid2, &repbody->fid1);
680 * Save error code to ->mode. Later it it is used for detecting the case
683 repbody->mode = rc < 0 ? -rc : rc;
684 repbody->valid = OBD_MD_FLMODE;
687 repbody->valid |= OBD_MD_FLID;
692 static int mdt_raw_lookup(struct mdt_thread_info *info,
693 struct mdt_object *parent,
695 struct ldlm_reply *ldlm_rep)
697 struct md_object *next = mdt_object_child(info->mti_object);
698 const struct mdt_body *reqbody = info->mti_body;
699 struct lu_fid *child_fid = &info->mti_tmp_fid1;
700 struct mdt_body *repbody;
704 if (reqbody->valid != OBD_MD_FLID)
707 /* Only got the fid of this obj by name */
708 rc = mdo_lookup(info->mti_env, next, name, child_fid);
711 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
714 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
716 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
717 repbody->fid1 = *child_fid;
718 repbody->valid = OBD_MD_FLID;
724 * UPDATE lock should be taken against parent, and be release before exit;
725 * child_bits lock should be taken against child, and be returned back:
726 * (1)normal request should release the child lock;
727 * (2)intent request will grant the lock to client.
729 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
730 struct mdt_lock_handle *lhc,
732 struct ldlm_reply *ldlm_rep)
734 struct ptlrpc_request *req = mdt_info_req(info);
735 struct mdt_object *parent = info->mti_object;
736 struct mdt_object *child;
737 struct md_object *next = mdt_object_child(info->mti_object);
738 struct lu_fid *child_fid = &info->mti_tmp_fid1;
739 int is_resent, rc, namelen = 0;
741 struct mdt_lock_handle *lhp;
742 struct ldlm_lock *lock;
745 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
747 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
749 LASSERT(info->mti_object != NULL);
750 name = req_capsule_client_get(&info->mti_pill, &RMF_NAME);
752 RETURN(err_serious(-EFAULT));
754 namelen = req_capsule_get_size(&info->mti_pill, &RMF_NAME,
757 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, ldlm_rep = %p\n",
758 PFID(mdt_object_fid(parent)), name, ldlm_rep);
760 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
762 rc = mdt_object_exists(parent);
766 CERROR("Object "DFID" locates on remote server\n",
767 PFID(mdt_object_fid(parent)));
771 rc = mdt_raw_lookup(info, parent, name, ldlm_rep);
779 /* Only getattr on the child. Parent is on another node. */
780 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
782 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
783 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
786 /* Do not take lock for resent case. */
787 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
789 CERROR("Invalid lock handle "LPX64"\n",
790 lhc->mlh_reg_lh.cookie);
793 LASSERT(fid_res_name_eq(mdt_object_fid(child),
794 &lock->l_resource->lr_name));
798 mdt_lock_handle_init(lhc);
799 mdt_lock_reg_init(lhc, LCK_PR);
802 * Object's name is on another MDS, no lookup lock is
803 * needed here but update is.
805 child_bits &= ~MDS_INODELOCK_LOOKUP;
806 child_bits |= MDS_INODELOCK_UPDATE;
808 rc = mdt_object_lock(info, child, lhc, child_bits,
812 /* Finally, we can get attr for child. */
813 mdt_set_capainfo(info, 0, mdt_object_fid(child),
815 rc = mdt_getattr_internal(info, child);
817 mdt_object_unlock(info, child, lhc, 1);
822 /*step 1: lock parent */
823 lhp = &info->mti_lh[MDT_LH_PARENT];
824 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
825 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
830 /*step 2: lookup child's fid by name */
831 rc = mdo_lookup(info->mti_env, next, name, child_fid);
834 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
835 GOTO(out_parent, rc);
837 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
839 *step 3: find the child object by fid & lock it.
840 * regardless if it is local or remote.
842 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
844 GOTO(out_parent, rc = PTR_ERR(child));
846 /* Do not take lock for resent case. */
847 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
849 CERROR("Invalid lock handle "LPX64"\n",
850 lhc->mlh_reg_lh.cookie);
853 LASSERT(fid_res_name_eq(child_fid,
854 &lock->l_resource->lr_name));
858 mdt_lock_handle_init(lhc);
859 mdt_lock_reg_init(lhc, LCK_PR);
861 rc = mdt_object_lock(info, child, lhc, child_bits,
867 /* finally, we can get attr for child. */
868 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
869 rc = mdt_getattr_internal(info, child);
871 mdt_object_unlock(info, child, lhc, 1);
873 struct ldlm_lock *lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
875 struct ldlm_res_id *res_id;
876 struct mdt_body *repbody;
879 /* Debugging code. */
880 res_id = &lock->l_resource->lr_name;
881 LDLM_DEBUG(lock, "we will return this lock client\n");
882 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
883 &lock->l_resource->lr_name),
884 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
885 (unsigned long)res_id->name[0],
886 (unsigned long)res_id->name[1],
887 (unsigned long)res_id->name[2],
888 PFID(mdt_object_fid(child)));
890 /* Pack Size-on-MDS inode attributes to the body if
891 * update lock is given. */
892 repbody = req_capsule_server_get(&info->mti_pill,
894 ma = &info->mti_attr.ma_attr;
895 if (lock->l_policy_data.l_inodebits.bits &
896 MDS_INODELOCK_UPDATE)
897 mdt_pack_size2body(repbody, ma, child);
903 mdt_object_put(info->mti_env, child);
905 mdt_object_unlock(info, parent, lhp, 1);
910 /* normal handler: should release the child lock */
911 static int mdt_getattr_name(struct mdt_thread_info *info)
913 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
914 struct mdt_body *reqbody;
915 struct mdt_body *repbody;
919 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
921 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
923 repbody->eadatasize = 0;
924 repbody->aclsize = 0;
926 rc = mdt_init_ucred(info, reqbody);
930 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
931 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
932 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
933 lhc->mlh_reg_lh.cookie = 0;
935 mdt_exit_ucred(info);
938 mdt_shrink_reply(info, REPLY_REC_OFF + 1, 1, 0);
942 static struct lu_device_operations mdt_lu_ops;
944 static int lu_device_is_mdt(struct lu_device *d)
946 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
949 static int mdt_connect(struct mdt_thread_info *info)
952 struct ptlrpc_request *req;
954 req = mdt_info_req(info);
955 rc = target_handle_connect(req);
957 LASSERT(req->rq_export != NULL);
958 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
959 rc = mdt_init_idmap(info);
961 rc = err_serious(rc);
965 static int mdt_disconnect(struct mdt_thread_info *info)
969 rc = target_handle_disconnect(mdt_info_req(info));
971 rc = err_serious(rc);
975 static int mdt_sendpage(struct mdt_thread_info *info,
976 struct lu_rdpg *rdpg)
978 struct ptlrpc_request *req = mdt_info_req(info);
979 struct ptlrpc_bulk_desc *desc;
980 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
987 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
990 GOTO(out, rc = -ENOMEM);
992 for (i = 0, tmpcount = rdpg->rp_count;
993 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
994 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
995 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
998 LASSERT(desc->bd_nob == rdpg->rp_count);
999 rc = ptlrpc_start_bulk_transfer(desc);
1001 GOTO(free_desc, rc);
1003 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1004 GOTO(abort_bulk, rc);
1006 *lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
1007 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), lwi);
1008 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1011 if (desc->bd_success &&
1012 desc->bd_nob_transferred == rdpg->rp_count)
1013 GOTO(free_desc, rc);
1015 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1018 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
1019 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1020 desc->bd_nob_transferred, rdpg->rp_count,
1021 req->rq_export->exp_client_uuid.uuid,
1022 req->rq_export->exp_connection->c_remote_uuid.uuid);
1024 class_fail_export(req->rq_export);
1028 ptlrpc_abort_bulk(desc);
1030 ptlrpc_free_bulk(desc);
1035 #ifdef HAVE_SPLIT_SUPPORT
1037 * Retrieve dir entry from the page and insert it to the slave object, actually,
1038 * this should be in osd layer, but since it will not in the final product, so
1039 * just do it here and do not define more moo api anymore for this.
1041 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1044 struct mdt_object *object = info->mti_object;
1045 int rc = 0, offset = 0, is_dir;
1046 struct lu_dirpage *dp;
1047 struct lu_dirent *ent;
1050 /* Make sure we have at least one entry. */
1055 * Disable trans for this name insert, since it will include many trans
1058 info->mti_no_need_trans = 1;
1061 dp = page_address(page);
1062 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1064 for (ent = lu_dirent_start(dp); ent != NULL;
1065 ent = lu_dirent_next(ent)) {
1066 struct lu_fid *lf = &ent->lde_fid;
1069 if (le16_to_cpu(ent->lde_namelen) == 0)
1072 is_dir = le32_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT;
1073 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1075 GOTO(out, rc = -ENOMEM);
1077 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1078 rc = mdo_name_insert(info->mti_env,
1079 md_object_next(&object->mot_obj),
1081 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1085 offset += le16_to_cpu(ent->lde_reclen);
1095 static int mdt_bulk_timeout(void *data)
1099 CERROR("mdt bulk transfer timeout \n");
1104 static int mdt_writepage(struct mdt_thread_info *info)
1106 struct ptlrpc_request *req = mdt_info_req(info);
1107 struct mdt_body *reqbody;
1108 struct l_wait_info *lwi;
1109 struct ptlrpc_bulk_desc *desc;
1115 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1116 if (reqbody == NULL)
1117 RETURN(err_serious(-EFAULT));
1119 desc = ptlrpc_prep_bulk_exp (req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1121 RETURN(err_serious(-ENOMEM));
1123 /* allocate the page for the desc */
1124 page = alloc_pages(GFP_KERNEL, 0);
1126 GOTO(desc_cleanup, rc = -ENOMEM);
1128 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1129 (int)reqbody->size, (int)reqbody->nlink);
1131 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1132 (int)reqbody->nlink);
1135 * Check if client was evicted while we were doing i/o before touching
1140 GOTO(cleanup_page, rc = -ENOMEM);
1142 if (desc->bd_export->exp_failed)
1145 rc = ptlrpc_start_bulk_transfer (desc);
1147 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1148 mdt_bulk_timeout, desc);
1149 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1150 desc->bd_export->exp_failed, lwi);
1151 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1152 if (rc == -ETIMEDOUT) {
1153 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1154 ptlrpc_abort_bulk(desc);
1155 } else if (desc->bd_export->exp_failed) {
1156 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1158 ptlrpc_abort_bulk(desc);
1159 } else if (!desc->bd_success ||
1160 desc->bd_nob_transferred != desc->bd_nob) {
1161 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1163 "truncated" : "network error on",
1164 desc->bd_nob_transferred, desc->bd_nob);
1165 /* XXX should this be a different errno? */
1169 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d\n", rc);
1172 GOTO(cleanup_lwi, rc);
1173 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1178 __free_pages(page, 0);
1180 ptlrpc_free_bulk(desc);
1185 static int mdt_readpage(struct mdt_thread_info *info)
1187 struct mdt_object *object = info->mti_object;
1188 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1189 struct mdt_body *reqbody;
1190 struct mdt_body *repbody;
1195 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1196 RETURN(err_serious(-ENOMEM));
1198 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
1199 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
1200 if (reqbody == NULL || repbody == NULL)
1201 RETURN(err_serious(-EFAULT));
1204 * prepare @rdpg before calling lower layers and transfer itself. Here
1205 * reqbody->size contains offset of where to start to read and
1206 * reqbody->nlink contains number bytes to read.
1208 rdpg->rp_hash = reqbody->size;
1209 if ((__u64)rdpg->rp_hash != reqbody->size) {
1210 CERROR("Invalid hash: %#llx != %#llx\n",
1211 (__u64)rdpg->rp_hash, reqbody->size);
1214 rdpg->rp_count = reqbody->nlink;
1215 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1216 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1217 if (rdpg->rp_pages == NULL)
1220 for (i = 0; i < rdpg->rp_npages; ++i) {
1221 rdpg->rp_pages[i] = alloc_pages(GFP_KERNEL, 0);
1222 if (rdpg->rp_pages[i] == NULL)
1223 GOTO(free_rdpg, rc = -ENOMEM);
1226 /* call lower layers to fill allocated pages with directory data */
1227 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1229 GOTO(free_rdpg, rc);
1231 /* send pages to client */
1232 rc = mdt_sendpage(info, rdpg);
1237 for (i = 0; i < rdpg->rp_npages; i++)
1238 if (rdpg->rp_pages[i] != NULL)
1239 __free_pages(rdpg->rp_pages[i], 0);
1240 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1242 MDT_FAIL_RETURN(OBD_FAIL_MDS_SENDPAGE, 0);
1247 static int mdt_reint_internal(struct mdt_thread_info *info,
1248 struct mdt_lock_handle *lhc,
1251 struct req_capsule *pill = &info->mti_pill;
1252 struct mdt_device *mdt = info->mti_mdt;
1253 struct ptlrpc_request *req = mdt_info_req(info);
1254 struct mdt_body *repbody;
1259 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1260 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1261 mdt->mdt_max_mdsize);
1262 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1263 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1264 mdt->mdt_max_cookiesize);
1265 rc = req_capsule_pack(pill);
1267 CERROR("Can't pack response, rc %d\n", rc);
1268 RETURN(err_serious(rc));
1271 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1272 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1274 repbody->eadatasize = 0;
1275 repbody->aclsize = 0;
1279 * Check this after packing response, because after we fail here without
1280 * allocating response, caller anyway may want to get ldlm_reply from it
1281 * and will get oops.
1283 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1284 RETURN(err_serious(-EFAULT));
1286 rc = mdt_reint_unpack(info, op);
1288 CERROR("Can't unpack reint, rc %d\n", rc);
1289 RETURN(err_serious(rc));
1292 rc = mdt_init_ucred_reint(info);
1296 rc = mdt_fix_attr_ucred(info, op);
1300 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1301 struct mdt_client_data *mcd;
1303 mcd = req->rq_export->exp_mdt_data.med_mcd;
1304 if (req_xid_is_last(req)) {
1305 mdt_reconstruct(info, lhc);
1306 rc = lustre_msg_get_status(req->rq_repmsg);
1309 DEBUG_REQ(D_HA, req, "no reply for RESENT (xid "LPD64")",
1312 rc = mdt_reint_rec(info, lhc);
1314 mdt_exit_ucred(info);
1318 static long mdt_reint_opcode(struct mdt_thread_info *info,
1319 const struct req_format **fmt)
1324 opc = err_serious(-EFAULT);
1325 ptr = req_capsule_client_get(&info->mti_pill, &RMF_REINT_OPC);
1328 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1329 if (opc < REINT_MAX && fmt[opc] != NULL)
1330 req_capsule_extend(&info->mti_pill, fmt[opc]);
1332 CERROR("Unsupported opc: %ld\n", opc);
1333 opc = err_serious(opc);
1339 static int mdt_reint(struct mdt_thread_info *info)
1344 static const struct req_format *reint_fmts[REINT_MAX] = {
1345 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1346 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1347 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1348 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1349 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1350 [REINT_OPEN] = &RQF_MDS_REINT_OPEN
1355 opc = mdt_reint_opcode(info, reint_fmts);
1358 * No lock possible here from client to pass it to reint code
1361 rc = mdt_reint_internal(info, NULL, opc);
1366 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1370 /* TODO these two methods not available now. */
1372 /* this should sync the whole device */
1373 static int mdt_device_sync(struct mdt_thread_info *info)
1378 /* this should sync this object */
1379 static int mdt_object_sync(struct mdt_thread_info *info)
1384 static int mdt_sync(struct mdt_thread_info *info)
1386 struct req_capsule *pill = &info->mti_pill;
1387 struct mdt_body *body;
1391 /* The fid may be zero, so we req_capsule_set manually */
1392 req_capsule_set(pill, &RQF_MDS_SYNC);
1394 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1396 RETURN(err_serious(-EINVAL));
1398 if (MDT_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1399 RETURN(err_serious(-ENOMEM));
1401 if (fid_seq(&body->fid1) == 0) {
1402 /* sync the whole device */
1403 rc = req_capsule_pack(pill);
1405 rc = mdt_device_sync(info);
1407 rc = err_serious(rc);
1409 /* sync an object */
1410 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1412 rc = mdt_object_sync(info);
1414 struct md_object *next;
1415 const struct lu_fid *fid;
1416 struct lu_attr *la = &info->mti_attr.ma_attr;
1418 next = mdt_object_child(info->mti_object);
1419 info->mti_attr.ma_need = MA_INODE;
1420 info->mti_attr.ma_valid = 0;
1421 rc = mo_attr_get(info->mti_env, next,
1424 body = req_capsule_server_get(pill,
1426 fid = mdt_object_fid(info->mti_object);
1427 mdt_pack_attr2body(info, body, la, fid);
1431 rc = err_serious(rc);
1436 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1438 return err_serious(-EOPNOTSUPP);
1441 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1443 return err_serious(-EOPNOTSUPP);
1447 * OBD PING and other handlers.
1449 static int mdt_obd_ping(struct mdt_thread_info *info)
1453 rc = target_handle_ping(mdt_info_req(info));
1455 rc = err_serious(rc);
1459 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1461 return err_serious(-EOPNOTSUPP);
1464 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1466 return err_serious(-EOPNOTSUPP);
1473 static struct ldlm_callback_suite cbs = {
1474 .lcs_completion = ldlm_server_completion_ast,
1475 .lcs_blocking = ldlm_server_blocking_ast,
1479 static int mdt_enqueue(struct mdt_thread_info *info)
1481 struct ptlrpc_request *req;
1486 * info->mti_dlm_req already contains swapped and (if necessary)
1487 * converted dlm request.
1489 LASSERT(info->mti_dlm_req != NULL);
1491 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE)) {
1492 info->mti_fail_id = OBD_FAIL_LDLM_ENQUEUE;
1496 req = mdt_info_req(info);
1499 * Lock without inodebits makes no sense and will oops later in
1500 * ldlm. Let's check it now to see if we have wrong lock from client or
1501 * bits get corrupted somewhere in mdt_intent_policy().
1503 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1504 LASSERT(req_bits != 0);
1506 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1507 req, info->mti_dlm_req, &cbs);
1508 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1509 return rc ? err_serious(rc) : req->rq_status;
1512 static int mdt_convert(struct mdt_thread_info *info)
1515 struct ptlrpc_request *req;
1517 LASSERT(info->mti_dlm_req);
1518 req = mdt_info_req(info);
1519 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1520 return rc ? err_serious(rc) : req->rq_status;
1523 static int mdt_bl_callback(struct mdt_thread_info *info)
1525 CERROR("bl callbacks should not happen on MDS\n");
1527 return err_serious(-EOPNOTSUPP);
1530 static int mdt_cp_callback(struct mdt_thread_info *info)
1532 CERROR("cp callbacks should not happen on MDS\n");
1534 return err_serious(-EOPNOTSUPP);
1538 * sec context handlers
1540 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
1542 return mdt_handle_idmap(info);
1545 static struct mdt_object *mdt_obj(struct lu_object *o)
1547 LASSERT(lu_device_is_mdt(o->lo_dev));
1548 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
1551 struct mdt_object *mdt_object_find(const struct lu_env *env,
1552 struct mdt_device *d,
1553 const struct lu_fid *f)
1555 struct lu_object *o;
1556 struct mdt_object *m;
1559 o = lu_object_find(env, d->mdt_md_dev.md_lu_dev.ld_site, f);
1561 m = (struct mdt_object *)o;
1567 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
1568 struct mdt_lock_handle *lh, __u64 ibits, int locality)
1570 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1571 ldlm_policy_data_t *policy = &info->mti_policy;
1572 struct ldlm_res_id *res_id = &info->mti_res_id;
1573 int exist = mdt_object_exists(o);
1577 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1578 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1579 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
1580 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
1583 if (locality == MDT_CROSS_LOCK) {
1584 /* cross-ref object fix */
1585 ibits &= ~MDS_INODELOCK_UPDATE;
1586 ibits |= MDS_INODELOCK_LOOKUP;
1588 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
1589 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
1591 /* No PDO lock on remote object */
1592 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
1593 } else if (exist == 0 && lh->mlh_type == MDT_PDO_LOCK) {
1595 * No PDO lock on non-existing object.
1596 * This may happen on removed $PWD on client.
1601 memset(policy, 0, sizeof(*policy));
1602 fid_build_reg_res_name(mdt_object_fid(o), res_id);
1605 * Take PDO lock on whole directory and build correct @res_id for lock
1606 * on part of directory.
1608 if (lh->mlh_type == MDT_PDO_LOCK && lh->mlh_pdo_hash != 0) {
1609 mdt_lock_pdo_mode(info, o, lh);
1610 if (lh->mlh_pdo_mode != LCK_NL) {
1612 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
1613 * is never going to be sent to client and we do not
1614 * want it slowed down due to possible cancels.
1616 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1617 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
1618 policy, res_id, LDLM_FL_ATOMIC_CB);
1624 * Finish res_id initializing by name hash marking patr of
1625 * directory which is taking modification.
1627 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
1630 policy->l_inodebits.bits = ibits;
1633 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1634 * going to be sent to client. If it is - mdt_intent_policy() path will
1635 * fix it up and turns FL_LOCAL flag off.
1637 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1638 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB);
1640 if (rc && lh->mlh_type == MDT_PDO_LOCK) {
1641 mdt_fid_unlock(&lh->mlh_pdo_lh, lh->mlh_pdo_mode);
1642 lh->mlh_pdo_lh.cookie = 0ull;
1649 * Just call ldlm_lock_decref() if decref, else we only call ptlrpc_save_lock()
1650 * to save this lock in req. when transaction committed, req will be released,
1651 * and lock will, too.
1653 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
1654 struct mdt_lock_handle *lh, int decref)
1656 struct ptlrpc_request *req = mdt_info_req(info);
1659 if (lustre_handle_is_used(&lh->mlh_pdo_lh)) {
1660 /* Do not save PDO locks to request, just decref. */
1661 mdt_fid_unlock(&lh->mlh_pdo_lh,
1663 lh->mlh_pdo_lh.cookie = 0;
1666 if (lustre_handle_is_used(&lh->mlh_reg_lh)) {
1668 mdt_fid_unlock(&lh->mlh_reg_lh,
1671 ptlrpc_save_lock(req, &lh->mlh_reg_lh,
1674 lh->mlh_reg_lh.cookie = 0;
1680 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
1681 const struct lu_fid *f,
1682 struct mdt_lock_handle *lh,
1685 struct mdt_object *o;
1687 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
1691 rc = mdt_object_lock(info, o, lh, ibits,
1694 mdt_object_put(info->mti_env, o);
1701 void mdt_object_unlock_put(struct mdt_thread_info * info,
1702 struct mdt_object * o,
1703 struct mdt_lock_handle *lh,
1706 mdt_object_unlock(info, o, lh, decref);
1707 mdt_object_put(info->mti_env, o);
1710 static struct mdt_handler *mdt_handler_find(__u32 opc,
1711 struct mdt_opc_slice *supported)
1713 struct mdt_opc_slice *s;
1714 struct mdt_handler *h;
1717 for (s = supported; s->mos_hs != NULL; s++) {
1718 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
1719 h = s->mos_hs + (opc - s->mos_opc_start);
1721 LASSERT(h->mh_opc == opc);
1723 h = NULL; /* unsupported opc */
1730 static int mdt_lock_resname_compat(struct mdt_device *m,
1731 struct ldlm_request *req)
1733 /* XXX something... later. */
1737 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
1739 /* XXX something... later. */
1744 * Generic code handling requests that have struct mdt_body passed in:
1746 * - extract mdt_body from request and save it in @info, if present;
1748 * - create lu_object, corresponding to the fid in mdt_body, and save it in
1751 * - if HABEO_CORPUS flag is set for this request type check whether object
1752 * actually exists on storage (lu_object_exists()).
1755 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
1757 const struct mdt_body *body;
1758 struct mdt_object *obj;
1759 const struct lu_env *env;
1760 struct req_capsule *pill;
1763 env = info->mti_env;
1764 pill = &info->mti_pill;
1766 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1770 if (!fid_is_sane(&body->fid1)) {
1771 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
1776 * Do not get size or any capa fields before we check that request
1777 * contains capa actually. There are some requests which do not, for
1778 * instance MDS_IS_SUBDIR.
1780 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
1781 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
1782 mdt_set_capainfo(info, 0, &body->fid1,
1783 req_capsule_client_get(pill, &RMF_CAPA1));
1785 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
1787 if ((flags & HABEO_CORPUS) &&
1788 !mdt_object_exists(obj)) {
1789 mdt_object_put(env, obj);
1790 /* for capability renew ENOENT will be handled in
1792 if (body->valid & OBD_MD_FLOSSCAPA)
1797 info->mti_object = obj;
1806 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
1808 struct req_capsule *pill;
1812 pill = &info->mti_pill;
1814 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
1815 rc = mdt_body_unpack(info, flags);
1819 if (rc == 0 && (flags & HABEO_REFERO)) {
1820 struct mdt_device *mdt = info->mti_mdt;
1822 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1823 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1824 mdt->mdt_max_mdsize);
1825 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1826 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1827 mdt->mdt_max_cookiesize);
1829 rc = req_capsule_pack(pill);
1834 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
1836 struct md_device *next = m->mdt_child;
1838 return next->md_ops->mdo_init_capa_ctxt(env, next,
1839 m->mdt_opts.mo_mds_capa,
1840 m->mdt_capa_timeout,
1846 * Invoke handler for this request opc. Also do necessary preprocessing
1847 * (according to handler ->mh_flags), and post-processing (setting of
1848 * ->last_{xid,committed}).
1850 static int mdt_req_handle(struct mdt_thread_info *info,
1851 struct mdt_handler *h, struct ptlrpc_request *req)
1853 int rc, serious = 0;
1858 LASSERT(h->mh_act != NULL);
1859 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
1860 LASSERT(current->journal_info == NULL);
1862 DEBUG_REQ(D_INODE, req, "%s", h->mh_name);
1865 * Do not use *_FAIL_CHECK_ONCE() macros, because they will stop
1866 * correct handling of failed req later in ldlm due to doing
1867 * obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED without actually
1868 * correct actions like it is done in target_send_reply_msg().
1870 if (h->mh_fail_id != 0) {
1872 * Set to info->mti_fail_id to handler fail_id, it will be used
1873 * later, and better than use default fail_id.
1875 if (OBD_FAIL_CHECK(h->mh_fail_id)) {
1876 info->mti_fail_id = h->mh_fail_id;
1882 flags = h->mh_flags;
1883 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
1885 if (h->mh_fmt != NULL) {
1886 req_capsule_set(&info->mti_pill, h->mh_fmt);
1887 rc = mdt_unpack_req_pack_rep(info, flags);
1890 if (rc == 0 && flags & MUTABOR &&
1891 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
1892 /* should it be rq_status? */
1895 if (rc == 0 && flags & HABEO_CLAVIS) {
1896 struct ldlm_request *dlm_req;
1898 LASSERT(h->mh_fmt != NULL);
1900 dlm_req = req_capsule_client_get(&info->mti_pill, &RMF_DLM_REQ);
1901 if (dlm_req != NULL) {
1902 if (info->mti_mdt->mdt_opts.mo_compat_resname)
1903 rc = mdt_lock_resname_compat(info->mti_mdt,
1905 info->mti_dlm_req = dlm_req;
1907 CERROR("Can't unpack dlm request\n");
1912 /* capability setting changed via /proc, needs reinitialize ctxt */
1913 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
1914 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
1915 info->mti_mdt->mdt_capa_conf = 0;
1920 * Process request, there can be two types of rc:
1921 * 1) errors with msg unpack/pack, other failures outside the
1922 * operation itself. This is counted as serious errors;
1923 * 2) errors during fs operation, should be placed in rq_status
1926 rc = h->mh_act(info);
1927 serious = is_serious(rc);
1928 rc = clear_serious(rc);
1932 req->rq_status = rc;
1935 * ELDLM_* codes which > 0 should be in rq_status only as well as
1936 * all non-serious errors.
1938 if (rc > 0 || !serious)
1941 LASSERT(current->journal_info == NULL);
1943 if (rc == 0 && (flags & HABEO_CLAVIS)
1944 && info->mti_mdt->mdt_opts.mo_compat_resname) {
1945 struct ldlm_reply *dlmrep;
1947 dlmrep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
1949 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
1952 /* If we're DISCONNECTing, the mdt_export_data is already freed */
1953 if (rc == 0 && h->mh_opc != MDS_DISCONNECT)
1954 target_committed_to_req(req);
1959 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
1961 lh->mlh_type = MDT_NUL_LOCK;
1962 lh->mlh_reg_lh.cookie = 0ull;
1963 lh->mlh_reg_mode = LCK_MINMODE;
1964 lh->mlh_pdo_lh.cookie = 0ull;
1965 lh->mlh_pdo_mode = LCK_MINMODE;
1968 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
1970 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
1971 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
1975 * Initialize fields of struct mdt_thread_info. Other fields are left in
1976 * uninitialized state, because it's too expensive to zero out whole
1977 * mdt_thread_info (> 1K) on each request arrival.
1979 static void mdt_thread_info_init(struct ptlrpc_request *req,
1980 struct mdt_thread_info *info)
1984 info->mti_rep_buf_nr = ARRAY_SIZE(info->mti_rep_buf_size);
1985 for (i = 0; i < ARRAY_SIZE(info->mti_rep_buf_size); i++)
1986 info->mti_rep_buf_size[i] = -1;
1987 req_capsule_init(&info->mti_pill, req, RCL_SERVER,
1988 info->mti_rep_buf_size);
1991 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
1992 mdt_lock_handle_init(&info->mti_lh[i]);
1994 /* mdt device: it can be NULL while CONNECT */
1996 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1998 info->mti_mdt = NULL;
1999 info->mti_env = req->rq_svc_thread->t_env;
2001 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2002 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2004 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2005 info->mti_body = NULL;
2006 info->mti_object = NULL;
2007 info->mti_dlm_req = NULL;
2008 info->mti_has_trans = 0;
2009 info->mti_no_need_trans = 0;
2010 info->mti_opdata = 0;
2013 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2017 req_capsule_fini(&info->mti_pill);
2018 if (info->mti_object != NULL) {
2019 mdt_object_put(info->mti_env, info->mti_object);
2020 info->mti_object = NULL;
2022 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2023 mdt_lock_handle_fini(&info->mti_lh[i]);
2024 info->mti_env = NULL;
2028 extern int mds_filter_recovery_request(struct ptlrpc_request *req,
2029 struct obd_device *obd, int *process);
2031 * Handle recovery. Return:
2032 * +1: continue request processing;
2033 * -ve: abort immediately with the given error code;
2034 * 0: send reply with error code in req->rq_status;
2036 static int mdt_recovery(struct mdt_thread_info *info)
2038 struct ptlrpc_request *req = mdt_info_req(info);
2040 struct obd_device *obd;
2044 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2047 case SEC_CTX_INIT_CONT:
2049 mdt_handle_idmap(info);
2053 if (req->rq_export == NULL) {
2054 CERROR("operation %d on unconnected MDS from %s\n",
2055 lustre_msg_get_opc(req->rq_reqmsg),
2056 libcfs_id2str(req->rq_peer));
2057 req->rq_status = -ENOTCONN;
2058 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2062 /* sanity check: if the xid matches, the request must be marked as a
2063 * resent or replayed */
2064 if (req_xid_is_last(req)) {
2065 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2066 (MSG_RESENT | MSG_REPLAY))) {
2067 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2068 "expected REPLAY or RESENT flag\n", req->rq_xid);
2070 req->rq_status = -ENOTCONN;
2075 /* else: note the opposite is not always true; a RESENT req after a
2076 * failover will usually not match the last_xid, since it was likely
2077 * never committed. A REPLAYed request will almost never match the
2078 * last xid, however it could for a committed, but still retained,
2081 obd = req->rq_export->exp_obd;
2083 /* Check for aborted recovery... */
2084 spin_lock_bh(&obd->obd_processing_task_lock);
2085 recovering = obd->obd_recovering;
2086 spin_unlock_bh(&obd->obd_processing_task_lock);
2090 DEBUG_REQ(D_INFO, req, "Got new replay");
2091 rc = mds_filter_recovery_request(req, obd, &should_process);
2092 if (rc != 0 || !should_process)
2094 else if (should_process < 0) {
2095 req->rq_status = should_process;
2096 rc = ptlrpc_error(req);
2103 static int mdt_reply(struct ptlrpc_request *req, int rc,
2104 struct mdt_thread_info *info)
2109 if (req->rq_reply_state == NULL && rc == 0) {
2110 req->rq_status = rc;
2111 lustre_pack_reply(req, 1, NULL, NULL);
2114 target_send_reply(req, rc, info->mti_fail_id);
2119 extern int mds_msg_check_version(struct lustre_msg *msg);
2121 static int mdt_handle0(struct ptlrpc_request *req,
2122 struct mdt_thread_info *info,
2123 struct mdt_opc_slice *supported)
2125 struct mdt_handler *h;
2126 struct lustre_msg *msg;
2131 MDT_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
2133 LASSERT(current->journal_info == NULL);
2135 msg = req->rq_reqmsg;
2136 rc = mds_msg_check_version(msg);
2138 rc = mdt_recovery(info);
2140 h = mdt_handler_find(lustre_msg_get_opc(msg),
2143 rc = mdt_req_handle(info, h, req);
2144 rc = mdt_reply(req, rc, info);
2146 req->rq_status = -ENOTSUPP;
2147 rc = ptlrpc_error(req);
2152 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2157 * MDT handler function called by ptlrpc service thread when request comes.
2159 * XXX common "target" functionality should be factored into separate module
2160 * shared by mdt, ost and stand-alone services like fld.
2162 static int mdt_handle_common(struct ptlrpc_request *req,
2163 struct mdt_opc_slice *supported)
2166 struct mdt_thread_info *info;
2170 env = req->rq_svc_thread->t_env;
2171 LASSERT(env != NULL);
2172 LASSERT(env->le_ses != NULL);
2173 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2174 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2175 LASSERT(info != NULL);
2177 mdt_thread_info_init(req, info);
2179 rc = mdt_handle0(req, info, supported);
2181 mdt_thread_info_fini(info);
2186 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2189 int mdt_recovery_handle(struct ptlrpc_request *req)
2194 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2196 rc = mdt_handle_common(req, mdt_fld_handlers);
2199 rc = mdt_handle_common(req, mdt_seq_handlers);
2202 rc = mdt_handle_common(req, mdt_regular_handlers);
2209 static int mdt_regular_handle(struct ptlrpc_request *req)
2211 return mdt_handle_common(req, mdt_regular_handlers);
2214 static int mdt_readpage_handle(struct ptlrpc_request *req)
2216 return mdt_handle_common(req, mdt_readpage_handlers);
2219 static int mdt_mdsc_handle(struct ptlrpc_request *req)
2221 return mdt_handle_common(req, mdt_seq_handlers);
2224 static int mdt_mdss_handle(struct ptlrpc_request *req)
2226 return mdt_handle_common(req, mdt_seq_handlers);
2229 static int mdt_dtss_handle(struct ptlrpc_request *req)
2231 return mdt_handle_common(req, mdt_seq_handlers);
2234 static int mdt_fld_handle(struct ptlrpc_request *req)
2236 return mdt_handle_common(req, mdt_fld_handlers);
2252 static int mdt_intent_getattr(enum mdt_it_code opcode,
2253 struct mdt_thread_info *info,
2254 struct ldlm_lock **,
2256 static int mdt_intent_reint(enum mdt_it_code opcode,
2257 struct mdt_thread_info *info,
2258 struct ldlm_lock **,
2261 static struct mdt_it_flavor {
2262 const struct req_format *it_fmt;
2264 int (*it_act)(enum mdt_it_code ,
2265 struct mdt_thread_info *,
2266 struct ldlm_lock **,
2269 } mdt_it_flavor[] = {
2271 .it_fmt = &RQF_LDLM_INTENT,
2272 /*.it_flags = HABEO_REFERO,*/
2274 .it_act = mdt_intent_reint,
2275 .it_reint = REINT_OPEN
2278 .it_fmt = &RQF_LDLM_INTENT,
2279 .it_flags = MUTABOR,
2280 .it_act = mdt_intent_reint,
2281 .it_reint = REINT_OPEN
2284 .it_fmt = &RQF_LDLM_INTENT,
2285 .it_flags = MUTABOR,
2286 .it_act = mdt_intent_reint,
2287 .it_reint = REINT_CREATE
2289 [MDT_IT_GETATTR] = {
2290 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2291 .it_flags = HABEO_REFERO,
2292 .it_act = mdt_intent_getattr
2294 [MDT_IT_READDIR] = {
2300 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2301 .it_flags = HABEO_REFERO,
2302 .it_act = mdt_intent_getattr
2305 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2306 .it_flags = MUTABOR,
2307 .it_act = NULL, /* XXX can be mdt_intent_reint, ? */
2308 .it_reint = REINT_UNLINK
2312 .it_flags = MUTABOR,
2315 [MDT_IT_GETXATTR] = {
2322 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2323 struct ldlm_lock **lockp,
2324 struct ldlm_lock *new_lock,
2325 struct mdt_lock_handle *lh,
2328 struct ptlrpc_request *req = mdt_info_req(info);
2329 struct ldlm_lock *lock = *lockp;
2332 * Get new lock only for cases when possible resent did not find any
2335 if (new_lock == NULL)
2336 new_lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2338 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2339 lh->mlh_reg_lh.cookie = 0;
2343 LASSERTF(new_lock != NULL,
2344 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2347 * If we've already given this lock to a client once, then we should
2348 * have no readers or writers. Otherwise, we should have one reader
2349 * _or_ writer ref (which will be zeroed below) before returning the
2352 if (new_lock->l_export == req->rq_export) {
2353 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2355 LASSERT(new_lock->l_export == NULL);
2356 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2361 if (new_lock->l_export == req->rq_export) {
2363 * Already gave this to the client, which means that we
2364 * reconstructed a reply.
2366 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2368 lh->mlh_reg_lh.cookie = 0;
2369 RETURN(ELDLM_LOCK_REPLACED);
2372 /* Fixup the lock to be given to the client */
2373 lock_res_and_lock(new_lock);
2374 new_lock->l_readers = 0;
2375 new_lock->l_writers = 0;
2377 new_lock->l_export = class_export_get(req->rq_export);
2378 spin_lock(&req->rq_export->exp_ldlm_data.led_lock);
2379 list_add(&new_lock->l_export_chain,
2380 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2381 spin_unlock(&req->rq_export->exp_ldlm_data.led_lock);
2383 new_lock->l_blocking_ast = lock->l_blocking_ast;
2384 new_lock->l_completion_ast = lock->l_completion_ast;
2385 new_lock->l_remote_handle = lock->l_remote_handle;
2386 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2388 unlock_res_and_lock(new_lock);
2389 LDLM_LOCK_PUT(new_lock);
2390 lh->mlh_reg_lh.cookie = 0;
2392 RETURN(ELDLM_LOCK_REPLACED);
2395 static void mdt_intent_fixup_resent(struct req_capsule *pill,
2396 struct ldlm_lock *new_lock,
2397 struct ldlm_lock **old_lock,
2398 struct mdt_lock_handle *lh)
2400 struct ptlrpc_request *req = pill->rc_req;
2401 struct obd_export *exp = req->rq_export;
2402 struct lustre_handle remote_hdl;
2403 struct ldlm_request *dlmreq;
2404 struct list_head *iter;
2406 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2409 dlmreq = req_capsule_client_get(pill, &RMF_DLM_REQ);
2410 remote_hdl = dlmreq->lock_handle1;
2412 spin_lock(&exp->exp_ldlm_data.led_lock);
2413 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2414 struct ldlm_lock *lock;
2415 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2416 if (lock == new_lock)
2418 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2419 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2420 lh->mlh_reg_mode = lock->l_granted_mode;
2422 LDLM_DEBUG(lock, "restoring lock cookie");
2423 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2424 lh->mlh_reg_lh.cookie);
2426 *old_lock = LDLM_LOCK_GET(lock);
2427 spin_unlock(&exp->exp_ldlm_data.led_lock);
2431 spin_unlock(&exp->exp_ldlm_data.led_lock);
2434 * If the xid matches, then we know this is a resent request, and allow
2435 * it. (It's probably an OPEN, for which we don't send a lock.
2437 if (req_xid_is_last(req))
2441 * This remote handle isn't enqueued, so we never received or processed
2442 * this request. Clear MSG_RESENT, because it can be handled like any
2443 * normal request now.
2445 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2447 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2451 static int mdt_intent_getattr(enum mdt_it_code opcode,
2452 struct mdt_thread_info *info,
2453 struct ldlm_lock **lockp,
2456 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2457 struct ldlm_lock *new_lock = NULL;
2459 struct ldlm_reply *ldlm_rep;
2460 struct ptlrpc_request *req;
2461 struct mdt_body *reqbody;
2462 struct mdt_body *repbody;
2466 reqbody = req_capsule_client_get(&info->mti_pill, &RMF_MDT_BODY);
2468 repbody = req_capsule_server_get(&info->mti_pill, &RMF_MDT_BODY);
2470 repbody->eadatasize = 0;
2471 repbody->aclsize = 0;
2475 child_bits = MDS_INODELOCK_LOOKUP;
2477 case MDT_IT_GETATTR:
2478 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2481 CERROR("Unhandled till now");
2482 GOTO(out, rc = -EINVAL);
2485 rc = mdt_init_ucred(info, reqbody);
2489 req = info->mti_pill.rc_req;
2490 ldlm_rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2491 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
2493 /* Get lock from request for possible resent case. */
2494 mdt_intent_fixup_resent(&info->mti_pill, *lockp, &new_lock, lhc);
2496 ldlm_rep->lock_policy_res2 =
2497 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
2499 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
2500 ldlm_rep->lock_policy_res2 = 0;
2501 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
2502 ldlm_rep->lock_policy_res2) {
2503 lhc->mlh_reg_lh.cookie = 0ull;
2504 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
2507 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
2509 mdt_exit_ucred(info);
2512 mdt_shrink_reply(info, DLM_REPLY_REC_OFF + 1, 1, 0);
2516 static int mdt_intent_reint(enum mdt_it_code opcode,
2517 struct mdt_thread_info *info,
2518 struct ldlm_lock **lockp,
2521 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2522 struct ldlm_reply *rep;
2526 static const struct req_format *intent_fmts[REINT_MAX] = {
2527 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
2528 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
2533 opc = mdt_reint_opcode(info, intent_fmts);
2537 if (mdt_it_flavor[opcode].it_reint != opc) {
2538 CERROR("Reint code %ld doesn't match intent: %d\n",
2540 RETURN(err_serious(-EPROTO));
2543 /* Get lock from request for possible resent case. */
2544 mdt_intent_fixup_resent(&info->mti_pill, *lockp, NULL, lhc);
2546 rc = mdt_reint_internal(info, lhc, opc);
2548 rep = req_capsule_server_get(&info->mti_pill, &RMF_DLM_REP);
2550 RETURN(err_serious(-EFAULT));
2552 /* MDC expects this in any case */
2554 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
2556 /* cross-ref case, the lock should be returned to the client */
2557 if (rc == -EREMOTE) {
2558 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
2559 rep->lock_policy_res2 = 0;
2560 RETURN(mdt_intent_lock_replace(info, lockp, NULL, lhc, flags));
2562 rep->lock_policy_res2 = clear_serious(rc);
2564 lhc->mlh_reg_lh.cookie = 0ull;
2565 RETURN(ELDLM_LOCK_ABORTED);
2568 static int mdt_intent_code(long itcode)
2576 case IT_OPEN|IT_CREAT:
2583 rc = MDT_IT_READDIR;
2586 rc = MDT_IT_GETATTR;
2598 rc = MDT_IT_GETXATTR;
2601 CERROR("Unknown intent opcode: %ld\n", itcode);
2608 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
2609 struct ldlm_lock **lockp, int flags)
2611 struct req_capsule *pill;
2612 struct mdt_it_flavor *flv;
2617 opc = mdt_intent_code(itopc);
2621 pill = &info->mti_pill;
2622 flv = &mdt_it_flavor[opc];
2624 if (flv->it_fmt != NULL)
2625 req_capsule_extend(pill, flv->it_fmt);
2627 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
2629 struct ptlrpc_request *req = mdt_info_req(info);
2630 if (flv->it_flags & MUTABOR &&
2631 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2634 if (rc == 0 && flv->it_act != NULL) {
2635 /* execute policy */
2636 rc = flv->it_act(opc, info, lockp, flags);
2642 static int mdt_intent_policy(struct ldlm_namespace *ns,
2643 struct ldlm_lock **lockp, void *req_cookie,
2644 ldlm_mode_t mode, int flags, void *data)
2646 struct mdt_thread_info *info;
2647 struct ptlrpc_request *req = req_cookie;
2648 struct ldlm_intent *it;
2649 struct req_capsule *pill;
2650 struct ldlm_lock *lock = *lockp;
2655 LASSERT(req != NULL);
2657 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
2659 LASSERT(info != NULL);
2660 pill = &info->mti_pill;
2661 LASSERT(pill->rc_req == req);
2663 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
2664 req_capsule_extend(pill, &RQF_LDLM_INTENT);
2665 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
2667 const struct ldlm_request *dlmreq;
2670 LDLM_DEBUG(lock, "intent policy opc: %s\n",
2671 ldlm_it2str(it->opc));
2673 rc = mdt_intent_opc(it->opc, info, lockp, flags);
2678 * Lock without inodebits makes no sense and will oops
2679 * later in ldlm. Let's check it now to see if we have
2680 * wrong lock from client or bits get corrupted
2681 * somewhere in mdt_intent_opc().
2683 dlmreq = info->mti_dlm_req;
2684 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
2685 LASSERT(req_bits != 0);
2688 rc = err_serious(-EFAULT);
2690 /* No intent was provided */
2691 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
2692 rc = req_capsule_pack(pill);
2694 rc = err_serious(rc);
2702 static int mdt_seq_fini(const struct lu_env *env,
2703 struct mdt_device *m)
2705 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2708 if (ls && ls->ls_server_seq) {
2709 seq_server_fini(ls->ls_server_seq, env);
2710 OBD_FREE_PTR(ls->ls_server_seq);
2711 ls->ls_server_seq = NULL;
2714 if (ls && ls->ls_control_seq) {
2715 seq_server_fini(ls->ls_control_seq, env);
2716 OBD_FREE_PTR(ls->ls_control_seq);
2717 ls->ls_control_seq = NULL;
2720 if (ls && ls->ls_client_seq) {
2721 seq_client_fini(ls->ls_client_seq);
2722 OBD_FREE_PTR(ls->ls_client_seq);
2723 ls->ls_client_seq = NULL;
2729 static int mdt_seq_init(const struct lu_env *env,
2731 struct mdt_device *m)
2738 ls = m->mdt_md_dev.md_lu_dev.ld_site;
2741 * This is sequence-controller node. Init seq-controller server on local
2744 if (ls->ls_node_id == 0) {
2745 LASSERT(ls->ls_control_seq == NULL);
2747 OBD_ALLOC_PTR(ls->ls_control_seq);
2748 if (ls->ls_control_seq == NULL)
2751 rc = seq_server_init(ls->ls_control_seq,
2752 m->mdt_bottom, uuid,
2753 LUSTRE_SEQ_CONTROLLER,
2757 GOTO(out_seq_fini, rc);
2759 OBD_ALLOC_PTR(ls->ls_client_seq);
2760 if (ls->ls_client_seq == NULL)
2761 GOTO(out_seq_fini, rc = -ENOMEM);
2763 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
2764 if (prefix == NULL) {
2765 OBD_FREE_PTR(ls->ls_client_seq);
2766 GOTO(out_seq_fini, rc = -ENOMEM);
2769 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
2773 * Init seq-controller client after seq-controller server is
2774 * ready. Pass ls->ls_control_seq to it for direct talking.
2776 rc = seq_client_init(ls->ls_client_seq, NULL,
2777 LUSTRE_SEQ_METADATA, prefix,
2778 ls->ls_control_seq);
2779 OBD_FREE(prefix, MAX_OBD_NAME + 5);
2782 GOTO(out_seq_fini, rc);
2785 /* Init seq-server on local MDT */
2786 LASSERT(ls->ls_server_seq == NULL);
2788 OBD_ALLOC_PTR(ls->ls_server_seq);
2789 if (ls->ls_server_seq == NULL)
2790 GOTO(out_seq_fini, rc = -ENOMEM);
2792 rc = seq_server_init(ls->ls_server_seq,
2793 m->mdt_bottom, uuid,
2797 GOTO(out_seq_fini, rc = -ENOMEM);
2799 /* Assign seq-controller client to local seq-server. */
2800 if (ls->ls_node_id == 0) {
2801 LASSERT(ls->ls_client_seq != NULL);
2803 rc = seq_server_set_cli(ls->ls_server_seq,
2811 mdt_seq_fini(env, m);
2816 * Init client sequence manager which is used by local MDS to talk to sequence
2817 * controller on remote node.
2819 static int mdt_seq_init_cli(const struct lu_env *env,
2820 struct mdt_device *m,
2821 struct lustre_cfg *cfg)
2823 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2824 struct obd_device *mdc;
2825 struct obd_uuid *uuidp, *mdcuuidp;
2826 char *uuid_str, *mdc_uuid_str;
2829 struct mdt_thread_info *info;
2830 char *p, *index_string = lustre_cfg_string(cfg, 2);
2833 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2834 uuidp = &info->mti_u.uuid[0];
2835 mdcuuidp = &info->mti_u.uuid[1];
2837 LASSERT(index_string);
2839 index = simple_strtol(index_string, &p, 10);
2841 CERROR("Invalid index in lustre_cgf, offset 2\n");
2845 /* check if this is adding the first MDC and controller is not yet
2847 if (index != 0 || ls->ls_client_seq)
2850 uuid_str = lustre_cfg_string(cfg, 1);
2851 mdc_uuid_str = lustre_cfg_string(cfg, 4);
2852 obd_str2uuid(uuidp, uuid_str);
2853 obd_str2uuid(mdcuuidp, mdc_uuid_str);
2855 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
2857 CERROR("can't find controller MDC by uuid %s\n",
2860 } else if (!mdc->obd_set_up) {
2861 CERROR("target %s not set up\n", mdc->obd_name);
2864 LASSERT(ls->ls_control_exp);
2865 OBD_ALLOC_PTR(ls->ls_client_seq);
2866 if (ls->ls_client_seq != NULL) {
2869 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
2873 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
2876 rc = seq_client_init(ls->ls_client_seq,
2878 LUSTRE_SEQ_METADATA,
2880 OBD_FREE(prefix, MAX_OBD_NAME + 5);
2887 LASSERT(ls->ls_server_seq != NULL);
2889 rc = seq_server_set_cli(ls->ls_server_seq,
2897 static void mdt_seq_fini_cli(struct mdt_device *m)
2903 ls = m->mdt_md_dev.md_lu_dev.ld_site;
2905 if (ls && ls->ls_server_seq)
2906 seq_server_set_cli(ls->ls_server_seq,
2909 if (ls && ls->ls_control_exp) {
2910 class_export_put(ls->ls_control_exp);
2911 ls->ls_control_exp = NULL;
2919 static int mdt_fld_fini(const struct lu_env *env,
2920 struct mdt_device *m)
2922 struct lu_site *ls = m->mdt_md_dev.md_lu_dev.ld_site;
2925 if (ls && ls->ls_server_fld) {
2926 fld_server_fini(ls->ls_server_fld, env);
2927 OBD_FREE_PTR(ls->ls_server_fld);
2928 ls->ls_server_fld = NULL;
2934 static int mdt_fld_init(const struct lu_env *env,
2936 struct mdt_device *m)
2942 ls = m->mdt_md_dev.md_lu_dev.ld_site;
2944 OBD_ALLOC_PTR(ls->ls_server_fld);
2945 if (ls->ls_server_fld == NULL)
2946 RETURN(rc = -ENOMEM);
2948 rc = fld_server_init(ls->ls_server_fld,
2949 m->mdt_bottom, uuid, env);
2951 OBD_FREE_PTR(ls->ls_server_fld);
2952 ls->ls_server_fld = NULL;
2959 /* device init/fini methods */
2960 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
2962 if (m->mdt_regular_service != NULL) {
2963 ptlrpc_unregister_service(m->mdt_regular_service);
2964 m->mdt_regular_service = NULL;
2966 if (m->mdt_readpage_service != NULL) {
2967 ptlrpc_unregister_service(m->mdt_readpage_service);
2968 m->mdt_readpage_service = NULL;
2970 if (m->mdt_setattr_service != NULL) {
2971 ptlrpc_unregister_service(m->mdt_setattr_service);
2972 m->mdt_setattr_service = NULL;
2974 if (m->mdt_mdsc_service != NULL) {
2975 ptlrpc_unregister_service(m->mdt_mdsc_service);
2976 m->mdt_mdsc_service = NULL;
2978 if (m->mdt_mdss_service != NULL) {
2979 ptlrpc_unregister_service(m->mdt_mdss_service);
2980 m->mdt_mdss_service = NULL;
2982 if (m->mdt_dtss_service != NULL) {
2983 ptlrpc_unregister_service(m->mdt_dtss_service);
2984 m->mdt_dtss_service = NULL;
2986 if (m->mdt_fld_service != NULL) {
2987 ptlrpc_unregister_service(m->mdt_fld_service);
2988 m->mdt_fld_service = NULL;
2992 static int mdt_start_ptlrpc_service(struct mdt_device *m)
2995 static struct ptlrpc_service_conf conf;
2998 conf = (typeof(conf)) {
2999 .psc_nbufs = MDS_NBUFS,
3000 .psc_bufsize = MDS_BUFSIZE,
3001 .psc_max_req_size = MDS_MAXREQSIZE,
3002 .psc_max_reply_size = MDS_MAXREPSIZE,
3003 .psc_req_portal = MDS_REQUEST_PORTAL,
3004 .psc_rep_portal = MDC_REPLY_PORTAL,
3005 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3007 * We'd like to have a mechanism to set this on a per-device
3008 * basis, but alas...
3010 .psc_num_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3012 .psc_ctx_tags = LCT_MD_THREAD
3015 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
3016 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3017 "mdt_ldlm_client", m->mdt_ldlm_client);
3019 m->mdt_regular_service =
3020 ptlrpc_init_svc_conf(&conf, mdt_regular_handle, LUSTRE_MDT_NAME,
3021 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3023 if (m->mdt_regular_service == NULL)
3026 rc = ptlrpc_start_threads(NULL, m->mdt_regular_service, LUSTRE_MDT_NAME);
3028 GOTO(err_mdt_svc, rc);
3031 * readpage service configuration. Parameters have to be adjusted,
3034 conf = (typeof(conf)) {
3035 .psc_nbufs = MDS_NBUFS,
3036 .psc_bufsize = MDS_BUFSIZE,
3037 .psc_max_req_size = MDS_MAXREQSIZE,
3038 .psc_max_reply_size = MDS_MAXREPSIZE,
3039 .psc_req_portal = MDS_READPAGE_PORTAL,
3040 .psc_rep_portal = MDC_REPLY_PORTAL,
3041 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3042 .psc_num_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3044 .psc_ctx_tags = LCT_MD_THREAD
3046 m->mdt_readpage_service =
3047 ptlrpc_init_svc_conf(&conf, mdt_readpage_handle,
3048 LUSTRE_MDT_NAME "_readpage",
3049 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3052 if (m->mdt_readpage_service == NULL) {
3053 CERROR("failed to start readpage service\n");
3054 GOTO(err_mdt_svc, rc = -ENOMEM);
3057 rc = ptlrpc_start_threads(NULL, m->mdt_readpage_service, "mdt_rdpg");
3060 * setattr service configuration.
3062 conf = (typeof(conf)) {
3063 .psc_nbufs = MDS_NBUFS,
3064 .psc_bufsize = MDS_BUFSIZE,
3065 .psc_max_req_size = MDS_MAXREQSIZE,
3066 .psc_max_reply_size = MDS_MAXREPSIZE,
3067 .psc_req_portal = MDS_SETATTR_PORTAL,
3068 .psc_rep_portal = MDC_REPLY_PORTAL,
3069 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3070 .psc_num_threads = min(max(mdt_num_threads, MDT_MIN_THREADS),
3072 .psc_ctx_tags = LCT_MD_THREAD
3075 m->mdt_setattr_service =
3076 ptlrpc_init_svc_conf(&conf, mdt_regular_handle,
3077 LUSTRE_MDT_NAME "_setattr",
3078 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3081 if (!m->mdt_setattr_service) {
3082 CERROR("failed to start setattr service\n");
3083 GOTO(err_mdt_svc, rc = -ENOMEM);
3086 rc = ptlrpc_start_threads(NULL, m->mdt_setattr_service, "mdt_attr");
3088 GOTO(err_mdt_svc, rc);
3091 * sequence controller service configuration
3093 conf = (typeof(conf)) {
3094 .psc_nbufs = MDS_NBUFS,
3095 .psc_bufsize = MDS_BUFSIZE,
3096 .psc_max_req_size = SEQ_MAXREQSIZE,
3097 .psc_max_reply_size = SEQ_MAXREPSIZE,
3098 .psc_req_portal = SEQ_CONTROLLER_PORTAL,
3099 .psc_rep_portal = MDC_REPLY_PORTAL,
3100 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3101 .psc_num_threads = SEQ_NUM_THREADS,
3102 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3105 m->mdt_mdsc_service =
3106 ptlrpc_init_svc_conf(&conf, mdt_mdsc_handle,
3107 LUSTRE_MDT_NAME"_mdsc",
3108 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3110 if (!m->mdt_mdsc_service) {
3111 CERROR("failed to start seq controller service\n");
3112 GOTO(err_mdt_svc, rc = -ENOMEM);
3115 rc = ptlrpc_start_threads(NULL, m->mdt_mdsc_service, "mdt_mdsc");
3117 GOTO(err_mdt_svc, rc);
3120 * metadata sequence server service configuration
3122 conf = (typeof(conf)) {
3123 .psc_nbufs = MDS_NBUFS,
3124 .psc_bufsize = MDS_BUFSIZE,
3125 .psc_max_req_size = SEQ_MAXREQSIZE,
3126 .psc_max_reply_size = SEQ_MAXREPSIZE,
3127 .psc_req_portal = SEQ_METADATA_PORTAL,
3128 .psc_rep_portal = MDC_REPLY_PORTAL,
3129 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3130 .psc_num_threads = SEQ_NUM_THREADS,
3131 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3134 m->mdt_mdss_service =
3135 ptlrpc_init_svc_conf(&conf, mdt_mdss_handle,
3136 LUSTRE_MDT_NAME"_mdss",
3137 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3139 if (!m->mdt_mdss_service) {
3140 CERROR("failed to start metadata seq server service\n");
3141 GOTO(err_mdt_svc, rc = -ENOMEM);
3144 rc = ptlrpc_start_threads(NULL, m->mdt_mdss_service, "mdt_mdss");
3146 GOTO(err_mdt_svc, rc);
3150 * Data sequence server service configuration. We want to have really
3151 * cluster-wide sequences space. This is why we start only one sequence
3152 * controller which manages space.
3154 conf = (typeof(conf)) {
3155 .psc_nbufs = MDS_NBUFS,
3156 .psc_bufsize = MDS_BUFSIZE,
3157 .psc_max_req_size = SEQ_MAXREQSIZE,
3158 .psc_max_reply_size = SEQ_MAXREPSIZE,
3159 .psc_req_portal = SEQ_DATA_PORTAL,
3160 .psc_rep_portal = OSC_REPLY_PORTAL,
3161 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3162 .psc_num_threads = SEQ_NUM_THREADS,
3163 .psc_ctx_tags = LCT_MD_THREAD|LCT_DT_THREAD
3166 m->mdt_dtss_service =
3167 ptlrpc_init_svc_conf(&conf, mdt_dtss_handle,
3168 LUSTRE_MDT_NAME"_dtss",
3169 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3171 if (!m->mdt_dtss_service) {
3172 CERROR("failed to start data seq server service\n");
3173 GOTO(err_mdt_svc, rc = -ENOMEM);
3176 rc = ptlrpc_start_threads(NULL, m->mdt_dtss_service, "mdt_dtss");
3178 GOTO(err_mdt_svc, rc);
3180 /* FLD service start */
3181 conf = (typeof(conf)) {
3182 .psc_nbufs = MDS_NBUFS,
3183 .psc_bufsize = MDS_BUFSIZE,
3184 .psc_max_req_size = FLD_MAXREQSIZE,
3185 .psc_max_reply_size = FLD_MAXREPSIZE,
3186 .psc_req_portal = FLD_REQUEST_PORTAL,
3187 .psc_rep_portal = MDC_REPLY_PORTAL,
3188 .psc_watchdog_timeout = MDT_SERVICE_WATCHDOG_TIMEOUT,
3189 .psc_num_threads = FLD_NUM_THREADS,
3190 .psc_ctx_tags = LCT_DT_THREAD|LCT_MD_THREAD
3193 m->mdt_fld_service =
3194 ptlrpc_init_svc_conf(&conf, mdt_fld_handle,
3195 LUSTRE_MDT_NAME"_fld",
3196 m->mdt_md_dev.md_lu_dev.ld_proc_entry,
3198 if (!m->mdt_fld_service) {
3199 CERROR("failed to start fld service\n");
3200 GOTO(err_mdt_svc, rc = -ENOMEM);
3203 rc = ptlrpc_start_threads(NULL, m->mdt_fld_service, "mdt_fld");
3205 GOTO(err_mdt_svc, rc);
3210 mdt_stop_ptlrpc_service(m);
3215 static void mdt_stack_fini(const struct lu_env *env,
3216 struct mdt_device *m, struct lu_device *top)
3218 struct lu_device *d = top, *n;
3219 struct lustre_cfg_bufs *bufs;
3220 struct lustre_cfg *lcfg;
3221 struct mdt_thread_info *info;
3224 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3225 LASSERT(info != NULL);
3227 bufs = &info->mti_u.bufs;
3228 /* process cleanup, pass mdt obd name to get obd umount flags */
3229 lustre_cfg_bufs_reset(bufs, m->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3230 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3232 CERROR("Cannot alloc lcfg!\n");
3236 top->ld_ops->ldo_process_config(env, top, lcfg);
3237 lustre_cfg_free(lcfg);
3239 lu_site_purge(env, top->ld_site, ~0);
3241 struct obd_type *type;
3242 struct lu_device_type *ldt = d->ld_type;
3244 /* each fini() returns next device in stack of layers
3245 * * so we can avoid the recursion */
3246 n = ldt->ldt_ops->ldto_device_fini(env, d);
3248 ldt->ldt_ops->ldto_device_free(env, d);
3249 type = ldt->ldt_obd_type;
3251 class_put_type(type);
3253 /* switch to the next device in the layer */
3256 m->mdt_child = NULL;
3259 static struct lu_device *mdt_layer_setup(const struct lu_env *env,
3260 const char *typename,
3261 struct lu_device *child,
3262 struct lustre_cfg *cfg)
3264 const char *dev = lustre_cfg_string(cfg, 0);
3265 struct obd_type *type;
3266 struct lu_device_type *ldt;
3267 struct lu_device *d;
3272 type = class_get_type(typename);
3274 CERROR("Unknown type: '%s'\n", typename);
3275 GOTO(out, rc = -ENODEV);
3278 rc = lu_context_refill(&env->le_ctx);
3280 CERROR("Failure to refill context: '%d'\n", rc);
3284 if (env->le_ses != NULL) {
3285 rc = lu_context_refill(env->le_ses);
3287 CERROR("Failure to refill session: '%d'\n", rc);
3294 CERROR("type: '%s'\n", typename);
3295 GOTO(out_type, rc = -EINVAL);
3298 ldt->ldt_obd_type = type;
3299 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
3301 CERROR("Cannot allocate device: '%s'\n", typename);
3302 GOTO(out_type, rc = -ENODEV);
3305 LASSERT(child->ld_site);
3306 d->ld_site = child->ld_site;
3309 rc = ldt->ldt_ops->ldto_device_init(env, d, dev, child);
3311 CERROR("can't init device '%s', rc %d\n", typename, rc);
3312 GOTO(out_alloc, rc);
3319 ldt->ldt_ops->ldto_device_free(env, d);
3322 class_put_type(type);
3327 static int mdt_stack_init(const struct lu_env *env,
3328 struct mdt_device *m, struct lustre_cfg *cfg)
3330 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3331 struct lu_device *tmp;
3332 struct md_device *md;
3336 /* init the stack */
3337 tmp = mdt_layer_setup(env, LUSTRE_OSD_NAME, d, cfg);
3339 RETURN(PTR_ERR(tmp));
3341 m->mdt_bottom = lu2dt_dev(tmp);
3343 tmp = mdt_layer_setup(env, LUSTRE_MDD_NAME, d, cfg);
3345 GOTO(out, rc = PTR_ERR(tmp));
3350 tmp = mdt_layer_setup(env, LUSTRE_CMM_NAME, d, cfg);
3352 GOTO(out, rc = PTR_ERR(tmp));
3355 /*set mdd upcall device*/
3356 md->md_upcall.mu_upcall_dev = lu2md_dev(d);
3359 /*set cmm upcall device*/
3360 md->md_upcall.mu_upcall_dev = &m->mdt_md_dev;
3362 m->mdt_child = lu2md_dev(d);
3364 /* process setup config */
3365 tmp = &m->mdt_md_dev.md_lu_dev;
3366 rc = tmp->ld_ops->ldo_process_config(env, tmp, cfg);
3369 /* fini from last known good lu_device */
3371 mdt_stack_fini(env, m, d);
3376 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
3378 struct md_device *next = m->mdt_child;
3379 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
3380 struct lu_site *ls = d->ld_site;
3384 mdt_fs_cleanup(env, m);
3386 ping_evictor_stop();
3387 mdt_stop_ptlrpc_service(m);
3389 cleanup_capas(CAPA_SITE_SERVER);
3390 del_timer(&m->mdt_ck_timer);
3391 mdt_ck_thread_stop(m);
3393 upcall_cache_cleanup(m->mdt_rmtacl_cache);
3394 m->mdt_rmtacl_cache = NULL;
3396 upcall_cache_cleanup(m->mdt_identity_cache);
3397 m->mdt_identity_cache = NULL;
3399 if (m->mdt_namespace != NULL) {
3400 ldlm_namespace_free(m->mdt_namespace, 0);
3401 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
3404 mdt_seq_fini(env, m);
3405 mdt_seq_fini_cli(m);
3406 mdt_fld_fini(env, m);
3407 lprocfs_obd_cleanup(d->ld_obd);
3409 if (m->mdt_rootsquash_info) {
3410 OBD_FREE_PTR(m->mdt_rootsquash_info);
3411 m->mdt_rootsquash_info = NULL;
3414 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
3415 cleanup_capas(CAPA_SITE_SERVER);
3416 del_timer(&m->mdt_ck_timer);
3417 mdt_ck_thread_stop(m);
3419 /* finish the stack */
3420 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3423 if (!list_empty(&ls->ls_lru) ||
3424 ls->ls_total != 0 || ls->ls_busy != 0) {
3426 * Uh-oh, objects still exist.
3428 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
3430 lu_site_print(env, ls, &cookie, lu_cdebug_printer);
3437 LASSERT(atomic_read(&d->ld_ref) == 0);
3438 md_device_fini(&m->mdt_md_dev);
3443 static void fsoptions_to_mdt_flags(struct mdt_device *m, char *options)
3453 while (*p && *p != ',')
3457 if ((len == sizeof("user_xattr") - 1) &&
3458 (memcmp(options, "user_xattr", len) == 0)) {
3459 m->mdt_opts.mo_user_xattr = 1;
3460 LCONSOLE_INFO("Enabling user_xattr\n");
3461 } else if ((len == sizeof("nouser_xattr") - 1) &&
3462 (memcmp(options, "nouser_xattr", len) == 0)) {
3463 m->mdt_opts.mo_user_xattr = 0;
3464 LCONSOLE_INFO("Disabling user_xattr\n");
3465 } else if ((len == sizeof("acl") - 1) &&
3466 (memcmp(options, "acl", len) == 0)) {
3467 #ifdef CONFIG_FS_POSIX_ACL
3468 m->mdt_opts.mo_acl = 1;
3469 LCONSOLE_INFO("Enabling ACL\n");
3471 m->mdt_opts.mo_acl = 0;
3472 CWARN("ignoring unsupported acl mount option\n");
3473 LCONSOLE_INFO("Disabling ACL\n");
3475 } else if ((len == sizeof("noacl") - 1) &&
3476 (memcmp(options, "noacl", len) == 0)) {
3477 #ifdef CONFIG_FS_POSIX_ACL
3478 m->mdt_opts.mo_acl = 0;
3479 LCONSOLE_INFO("Disabling ACL\n");
3487 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
3489 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
3490 struct lu_device_type *ldt, struct lustre_cfg *cfg)
3492 struct lprocfs_static_vars lvars;
3493 struct mdt_thread_info *info;
3494 struct obd_device *obd;
3495 const char *dev = lustre_cfg_string(cfg, 0);
3496 const char *num = lustre_cfg_string(cfg, 2);
3497 struct lustre_mount_info *lmi;
3498 struct lustre_sb_info *lsi;
3499 struct vfsmount *mnt;
3504 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3505 LASSERT(info != NULL);
3507 obd = class_name2obd(dev);
3508 LASSERT(obd != NULL);
3510 spin_lock_init(&m->mdt_transno_lock);
3512 m->mdt_max_mdsize = MAX_MD_SIZE;
3513 m->mdt_max_cookiesize = sizeof(struct llog_cookie);
3515 m->mdt_opts.mo_user_xattr = 0;
3516 m->mdt_opts.mo_acl = 0;
3517 lmi = server_get_mount_2(dev);
3519 CERROR("Cannot get mount info for %s!\n", dev);
3522 lsi = s2lsi(lmi->lmi_sb);
3523 fsoptions_to_mdt_flags(m, lsi->lsi_lmd->lmd_opts);
3526 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
3527 obd->obd_lvfs_ctxt.pwdmnt = mnt;
3528 obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
3529 obd->obd_lvfs_ctxt.fs = get_ds();
3531 server_put_mount_2(dev, mnt);
3534 spin_lock_init(&m->mdt_ioepoch_lock);
3535 m->mdt_opts.mo_compat_resname = 0;
3536 m->mdt_capa_timeout = CAPA_TIMEOUT;
3537 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
3538 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
3539 obd->obd_replayable = 1;
3540 spin_lock_init(&m->mdt_client_bitmap_lock);
3546 md_device_init(&m->mdt_md_dev, ldt);
3547 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
3548 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
3549 /* set this lu_device to obd, because error handling need it */
3550 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
3552 rc = lu_site_init(s, &m->mdt_md_dev.md_lu_dev);
3554 CERROR("can't init lu_site, rc %d\n", rc);
3555 GOTO(err_free_site, rc);
3558 lprocfs_init_vars(mdt, &lvars);
3559 rc = lprocfs_obd_setup(obd, lvars.obd_vars);
3561 CERROR("can't init lprocfs, rc %d\n", rc);
3562 GOTO(err_fini_site, rc);
3565 /* set server index */
3567 s->ls_node_id = simple_strtol(num, NULL, 10);
3569 /* init the stack */
3570 rc = mdt_stack_init(env, m, cfg);
3572 CERROR("can't init device stack, rc %d\n", rc);
3573 GOTO(err_fini_proc, rc);
3576 rc = mdt_fld_init(env, obd->obd_name, m);
3578 GOTO(err_fini_stack, rc);
3580 rc = mdt_seq_init(env, obd->obd_name, m);
3582 GOTO(err_fini_fld, rc);
3584 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
3585 LUSTRE_MDT_NAME"-%p", m);
3586 m->mdt_namespace = ldlm_namespace_new(info->mti_u.ns_name,
3587 LDLM_NAMESPACE_SERVER);
3588 if (m->mdt_namespace == NULL)
3589 GOTO(err_fini_seq, rc = -ENOMEM);
3591 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
3592 /* set obd_namespace for compatibility with old code */
3593 obd->obd_namespace = m->mdt_namespace;
3595 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,
3597 &mdt_identity_upcall_cache_ops);
3598 if (IS_ERR(m->mdt_identity_cache)) {
3599 rc = PTR_ERR(m->mdt_identity_cache);
3600 m->mdt_identity_cache = NULL;
3601 GOTO(err_free_ns, rc);
3604 m->mdt_rmtacl_cache = upcall_cache_init(obd->obd_name,
3605 MDT_RMTACL_UPCALL_PATH,
3606 &mdt_rmtacl_upcall_cache_ops);
3607 if (IS_ERR(m->mdt_rmtacl_cache)) {
3608 rc = PTR_ERR(m->mdt_rmtacl_cache);
3609 m->mdt_rmtacl_cache = NULL;
3610 GOTO(err_free_ns, rc);
3613 m->mdt_ck_timer.function = mdt_ck_timer_callback;
3614 m->mdt_ck_timer.data = (unsigned long)m;
3615 init_timer(&m->mdt_ck_timer);
3616 rc = mdt_ck_thread_start(m);
3618 GOTO(err_free_ns, rc);
3620 rc = mdt_start_ptlrpc_service(m);
3624 ping_evictor_start();
3626 rc = mdt_fs_setup(env, m, obd);
3628 GOTO(err_stop_service, rc);
3630 rc = lu_site_init_finish(s);
3632 GOTO(err_fs_cleanup, rc);
3634 if (obd->obd_recovering == 0)
3635 mdt_postrecov(env, m);
3637 mdt_init_capa_ctxt(env, m);
3641 mdt_fs_cleanup(env, m);
3643 mdt_stop_ptlrpc_service(m);
3645 del_timer(&m->mdt_ck_timer);
3646 mdt_ck_thread_stop(m);
3648 upcall_cache_cleanup(m->mdt_rmtacl_cache);
3649 m->mdt_rmtacl_cache = NULL;
3650 upcall_cache_cleanup(m->mdt_identity_cache);
3651 m->mdt_identity_cache = NULL;
3652 ldlm_namespace_free(m->mdt_namespace, 0);
3653 obd->obd_namespace = m->mdt_namespace = NULL;
3655 mdt_seq_fini(env, m);
3657 mdt_fld_fini(env, m);
3659 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
3661 lprocfs_obd_cleanup(obd);
3667 md_device_fini(&m->mdt_md_dev);
3671 /* used by MGS to process specific configurations */
3672 static int mdt_process_config(const struct lu_env *env,
3673 struct lu_device *d, struct lustre_cfg *cfg)
3675 struct mdt_device *m = mdt_dev(d);
3676 struct md_device *md_next = m->mdt_child;
3677 struct lu_device *next = md2lu_dev(md_next);
3681 switch (cfg->lcfg_command) {
3683 struct lprocfs_static_vars lvars;
3684 struct obd_device *obd = d->ld_obd;
3686 lprocfs_init_vars(mdt, &lvars);
3687 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, cfg, obd);
3689 /* others are passed further */
3690 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3695 * Add mdc hook to get first MDT uuid and connect it to
3696 * ls->controller to use for seq manager.
3698 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3700 CERROR("Can't add mdc, rc %d\n", rc);
3702 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
3705 /* others are passed further */
3706 rc = next->ld_ops->ldo_process_config(env, next, cfg);
3712 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
3713 const struct lu_object_header *hdr,
3714 struct lu_device *d)
3716 struct mdt_object *mo;
3722 struct lu_object *o;
3723 struct lu_object_header *h;
3725 o = &mo->mot_obj.mo_lu;
3726 h = &mo->mot_header;
3727 lu_object_header_init(h);
3728 lu_object_init(o, h, d);
3729 lu_object_add_top(h, o);
3730 o->lo_ops = &mdt_obj_ops;
3736 static int mdt_object_init(const struct lu_env *env, struct lu_object *o)
3738 struct mdt_device *d = mdt_dev(o->lo_dev);
3739 struct lu_device *under;
3740 struct lu_object *below;
3744 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
3745 PFID(lu_object_fid(o)));
3747 under = &d->mdt_child->md_lu_dev;
3748 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
3749 if (below != NULL) {
3750 lu_object_add(o, below);
3757 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
3759 struct mdt_object *mo = mdt_obj(o);
3760 struct lu_object_header *h;
3764 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
3765 PFID(lu_object_fid(o)));
3768 lu_object_header_fini(h);
3773 static int mdt_object_print(const struct lu_env *env, void *cookie,
3774 lu_printer_t p, const struct lu_object *o)
3776 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p", o);
3779 static struct lu_device_operations mdt_lu_ops = {
3780 .ldo_object_alloc = mdt_object_alloc,
3781 .ldo_process_config = mdt_process_config
3784 static struct lu_object_operations mdt_obj_ops = {
3785 .loo_object_init = mdt_object_init,
3786 .loo_object_free = mdt_object_free,
3787 .loo_object_print = mdt_object_print
3790 /* mds_connect_internal */
3791 static int mdt_connect_internal(struct obd_export *exp,
3792 struct mdt_device *mdt,
3793 struct obd_connect_data *data)
3798 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
3799 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
3801 /* If no known bits (which should not happen, probably,
3802 as everybody should support LOOKUP and UPDATE bits at least)
3803 revert to compat mode with plain locks. */
3804 if (!data->ocd_ibits_known &&
3805 data->ocd_connect_flags & OBD_CONNECT_IBITS)
3806 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
3808 if (!mdt->mdt_opts.mo_acl)
3809 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
3811 if (!mdt->mdt_opts.mo_user_xattr)
3812 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
3814 if (!mdt->mdt_opts.mo_mds_capa)
3815 data->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
3817 if (!mdt->mdt_opts.mo_oss_capa)
3818 data->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
3820 exp->exp_connect_flags = data->ocd_connect_flags;
3821 data->ocd_version = LUSTRE_VERSION_CODE;
3822 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
3826 if (mdt->mdt_opts.mo_acl &&
3827 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
3828 CWARN("%s: MDS requires ACL support but client does not\n",
3829 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3834 flags = OBD_CONNECT_LCL_CLIENT | OBD_CONNECT_RMT_CLIENT;
3835 if ((exp->exp_connect_flags & flags) == flags) {
3836 CWARN("%s: both local and remote client flags are set\n",
3837 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3841 if (mdt->mdt_opts.mo_mds_capa &&
3842 ((exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) == 0)) {
3843 CWARN("%s: MDS requires capability support, but client not\n",
3844 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3848 if (mdt->mdt_opts.mo_oss_capa &&
3849 ((exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA) == 0)) {
3850 CWARN("%s: MDS requires OSS capability support, "
3852 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
3859 /* mds_connect copy */
3860 static int mdt_obd_connect(const struct lu_env *env,
3861 struct lustre_handle *conn, struct obd_device *obd,
3862 struct obd_uuid *cluuid,
3863 struct obd_connect_data *data)
3865 struct mdt_export_data *med;
3866 struct mdt_client_data *mcd;
3867 struct obd_export *exp;
3868 struct mdt_device *mdt;
3872 LASSERT(env != NULL);
3873 if (!conn || !obd || !cluuid)
3876 mdt = mdt_dev(obd->obd_lu_dev);
3878 rc = class_connect(conn, obd, cluuid);
3882 exp = class_conn2export(conn);
3883 LASSERT(exp != NULL);
3884 med = &exp->exp_mdt_data;
3886 rc = mdt_connect_internal(exp, mdt, data);
3890 memcpy(mcd->mcd_uuid, cluuid, sizeof mcd->mcd_uuid);
3892 rc = mdt_client_new(env, mdt, med);
3895 med->med_mcd = NULL;
3902 class_disconnect(exp);
3904 class_export_put(exp);
3909 static int mdt_obd_reconnect(struct obd_export *exp, struct obd_device *obd,
3910 struct obd_uuid *cluuid,
3911 struct obd_connect_data *data)
3916 if (exp == NULL || obd == NULL || cluuid == NULL)
3919 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
3924 static int mdt_obd_disconnect(struct obd_export *exp)
3926 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
3931 class_export_get(exp);
3933 /* Disconnect early so that clients can't keep using export */
3934 rc = class_disconnect(exp);
3935 if (mdt->mdt_namespace != NULL || exp->exp_obd->obd_namespace != NULL)
3936 ldlm_cancel_locks_for_export(exp);
3938 /* complete all outstanding replies */
3939 spin_lock(&exp->exp_lock);
3940 while (!list_empty(&exp->exp_outstanding_replies)) {
3941 struct ptlrpc_reply_state *rs =
3942 list_entry(exp->exp_outstanding_replies.next,
3943 struct ptlrpc_reply_state, rs_exp_list);
3944 struct ptlrpc_service *svc = rs->rs_service;
3946 spin_lock(&svc->srv_lock);
3947 list_del_init(&rs->rs_exp_list);
3948 ptlrpc_schedule_difficult_reply(rs);
3949 spin_unlock(&svc->srv_lock);
3951 spin_unlock(&exp->exp_lock);
3953 class_export_put(exp);
3957 /* FIXME: Can we avoid using these two interfaces? */
3958 static int mdt_init_export(struct obd_export *exp)
3960 struct mdt_export_data *med = &exp->exp_mdt_data;
3963 INIT_LIST_HEAD(&med->med_open_head);
3964 spin_lock_init(&med->med_open_lock);
3965 exp->exp_connecting = 1;
3969 static int mdt_destroy_export(struct obd_export *export)
3971 struct mdt_export_data *med;
3972 struct obd_device *obd = export->exp_obd;
3973 struct mdt_device *mdt;
3974 struct mdt_thread_info *info;
3982 med = &export->exp_mdt_data;
3983 if (med->med_rmtclient)
3984 mdt_cleanup_idmap(med);
3986 target_destroy_export(export);
3988 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
3991 mdt = mdt_dev(obd->obd_lu_dev);
3992 LASSERT(mdt != NULL);
3994 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
3998 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
3999 LASSERT(info != NULL);
4000 memset(info, 0, sizeof *info);
4001 info->mti_env = &env;
4002 info->mti_mdt = mdt;
4004 ma = &info->mti_attr;
4005 lmm_size = ma->ma_lmm_size = mdt->mdt_max_mdsize;
4006 cookie_size = ma->ma_cookie_size = mdt->mdt_max_cookiesize;
4007 OBD_ALLOC(ma->ma_lmm, lmm_size);
4008 OBD_ALLOC(ma->ma_cookie, cookie_size);
4010 if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
4011 GOTO(out, rc = -ENOMEM);
4012 ma->ma_need = MA_LOV | MA_COOKIE;
4014 /* Close any open files (which may also cause orphan unlinking). */
4015 spin_lock(&med->med_open_lock);
4016 while (!list_empty(&med->med_open_head)) {
4017 struct list_head *tmp = med->med_open_head.next;
4018 struct mdt_file_data *mfd =
4019 list_entry(tmp, struct mdt_file_data, mfd_list);
4021 /* Remove mfd handle so it can't be found again.
4022 * We are consuming the mfd_list reference here. */
4023 class_handle_unhash(&mfd->mfd_handle);
4024 list_del_init(&mfd->mfd_list);
4025 spin_unlock(&med->med_open_lock);
4026 mdt_mfd_close(info, mfd);
4027 /* TODO: if we close the unlinked file,
4028 * we need to remove it's objects from OST */
4029 memset(&ma->ma_attr, 0, sizeof(ma->ma_attr));
4030 spin_lock(&med->med_open_lock);
4031 ma->ma_lmm_size = lmm_size;
4032 ma->ma_cookie_size = cookie_size;
4033 ma->ma_need = MA_LOV | MA_COOKIE;
4036 spin_unlock(&med->med_open_lock);
4037 info->mti_mdt = NULL;
4038 mdt_client_del(&env, mdt, med);
4043 OBD_FREE(ma->ma_lmm, lmm_size);
4047 OBD_FREE(ma->ma_cookie, cookie_size);
4048 ma->ma_cookie = NULL;
4055 static int mdt_upcall(const struct lu_env *env, struct md_device *md,
4056 enum md_upcall_event ev)
4058 struct mdt_device *m = mdt_dev(&md->md_lu_dev);
4059 struct md_device *next = m->mdt_child;
4060 struct mdt_thread_info *mti;
4066 rc = next->md_ops->mdo_maxsize_get(env, next,
4068 &m->mdt_max_cookiesize);
4069 CDEBUG(D_INFO, "get max mdsize %d max cookiesize %d\n",
4070 m->mdt_max_mdsize, m->mdt_max_cookiesize);
4073 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4074 mti->mti_no_need_trans = 1;
4075 CDEBUG(D_INFO, "disable mdt trans for this thread\n");
4078 CERROR("invalid event\n");
4085 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
4086 void *karg, void *uarg)
4089 struct obd_device *obd= exp->exp_obd;
4090 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
4091 struct dt_device *dt = mdt->mdt_bottom;
4095 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
4096 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4102 rc = dt->dd_ops->dt_sync(&env, dt);
4105 case OBD_IOC_SET_READONLY:
4106 rc = dt->dd_ops->dt_sync(&env, dt);
4107 dt->dd_ops->dt_ro(&env, dt);
4110 case OBD_IOC_ABORT_RECOVERY:
4111 CERROR("aborting recovery for device %s\n", obd->obd_name);
4112 target_stop_recovery_thread(obd);
4116 CERROR("not supported cmd = %d for device %s\n",
4117 cmd, obd->obd_name);
4125 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
4127 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
4130 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
4134 int mdt_obd_postrecov(struct obd_device *obd)
4139 rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
4142 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
4147 static struct obd_ops mdt_obd_device_ops = {
4148 .o_owner = THIS_MODULE,
4149 .o_connect = mdt_obd_connect,
4150 .o_reconnect = mdt_obd_reconnect,
4151 .o_disconnect = mdt_obd_disconnect,
4152 .o_init_export = mdt_init_export,
4153 .o_destroy_export = mdt_destroy_export,
4154 .o_iocontrol = mdt_iocontrol,
4155 .o_postrecov = mdt_obd_postrecov
4159 static struct lu_device* mdt_device_fini(const struct lu_env *env,
4160 struct lu_device *d)
4162 struct mdt_device *m = mdt_dev(d);
4168 static void mdt_device_free(const struct lu_env *env, struct lu_device *d)
4170 struct mdt_device *m = mdt_dev(d);
4175 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
4176 struct lu_device_type *t,
4177 struct lustre_cfg *cfg)
4179 struct lu_device *l;
4180 struct mdt_device *m;
4186 l = &m->mdt_md_dev.md_lu_dev;
4187 rc = mdt_init0(env, m, t, cfg);
4193 m->mdt_md_dev.md_upcall.mu_upcall = mdt_upcall;
4195 l = ERR_PTR(-ENOMEM);
4200 * context key constructor/destructor
4202 static void *mdt_key_init(const struct lu_context *ctx,
4203 struct lu_context_key *key)
4205 struct mdt_thread_info *info;
4208 * check that no high order allocations are incurred.
4210 CLASSERT(CFS_PAGE_SIZE >= sizeof *info);
4211 OBD_ALLOC_PTR(info);
4213 info = ERR_PTR(-ENOMEM);
4217 static void mdt_key_fini(const struct lu_context *ctx,
4218 struct lu_context_key *key, void *data)
4220 struct mdt_thread_info *info = data;
4224 struct lu_context_key mdt_thread_key = {
4225 .lct_tags = LCT_MD_THREAD,
4226 .lct_init = mdt_key_init,
4227 .lct_fini = mdt_key_fini
4230 static void *mdt_txn_key_init(const struct lu_context *ctx,
4231 struct lu_context_key *key)
4233 struct mdt_txn_info *txi;
4236 * check that no high order allocations are incurred.
4238 CLASSERT(CFS_PAGE_SIZE >= sizeof *txi);
4241 txi = ERR_PTR(-ENOMEM);
4245 static void mdt_txn_key_fini(const struct lu_context *ctx,
4246 struct lu_context_key *key, void *data)
4248 struct mdt_txn_info *txi = data;
4252 struct lu_context_key mdt_txn_key = {
4253 .lct_tags = LCT_TX_HANDLE,
4254 .lct_init = mdt_txn_key_init,
4255 .lct_fini = mdt_txn_key_fini
4258 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
4260 return md_ucred(info->mti_env);
4263 static int mdt_type_init(struct lu_device_type *t)
4267 rc = lu_context_key_register(&mdt_thread_key);
4269 rc = lu_context_key_register(&mdt_txn_key);
4273 static void mdt_type_fini(struct lu_device_type *t)
4275 lu_context_key_degister(&mdt_thread_key);
4276 lu_context_key_degister(&mdt_txn_key);
4279 static struct lu_device_type_operations mdt_device_type_ops = {
4280 .ldto_init = mdt_type_init,
4281 .ldto_fini = mdt_type_fini,
4283 .ldto_device_alloc = mdt_device_alloc,
4284 .ldto_device_free = mdt_device_free,
4285 .ldto_device_fini = mdt_device_fini
4288 static struct lu_device_type mdt_device_type = {
4289 .ldt_tags = LU_DEVICE_MD,
4290 .ldt_name = LUSTRE_MDT_NAME,
4291 .ldt_ops = &mdt_device_type_ops,
4292 .ldt_ctx_tags = LCT_MD_THREAD
4295 static int __init mdt_mod_init(void)
4297 struct lprocfs_static_vars lvars;
4300 mdt_num_threads = MDT_NUM_THREADS;
4301 lprocfs_init_vars(mdt, &lvars);
4302 rc = class_register_type(&mdt_obd_device_ops, NULL,
4303 lvars.module_vars, LUSTRE_MDT_NAME,
4309 static void __exit mdt_mod_exit(void)
4311 class_unregister_type(LUSTRE_MDT_NAME);
4315 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
4316 [prefix ## _ ## opc - prefix ## _ ## base] = { \
4318 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
4319 .mh_opc = prefix ## _ ## opc, \
4320 .mh_flags = flags, \
4325 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
4326 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
4328 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
4329 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
4331 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
4332 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
4334 * Request with a format known in advance
4336 #define DEF_MDT_HNDL_F(flags, name, fn) \
4337 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
4339 #define DEF_SEQ_HNDL_F(flags, name, fn) \
4340 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
4342 #define DEF_FLD_HNDL_F(flags, name, fn) \
4343 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
4345 * Request with a format we do not yet know
4347 #define DEF_MDT_HNDL_0(flags, name, fn) \
4348 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
4350 static struct mdt_handler mdt_mds_ops[] = {
4351 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
4352 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
4353 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
4354 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
4355 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
4356 DEF_MDT_HNDL_F(HABEO_CORPUS|MUTABOR, SETXATTR, mdt_setxattr),
4357 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
4358 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
4359 DEF_MDT_HNDL_F(0 |MUTABOR,
4361 DEF_MDT_HNDL_F(HABEO_CORPUS , CLOSE, mdt_close),
4362 DEF_MDT_HNDL_F(HABEO_CORPUS , DONE_WRITING, mdt_done_writing),
4363 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
4364 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
4365 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
4366 DEF_MDT_HNDL_0(0, QUOTACHECK, mdt_quotacheck_handle),
4367 DEF_MDT_HNDL_0(0, QUOTACTL, mdt_quotactl_handle)
4370 #define DEF_OBD_HNDL(flags, name, fn) \
4371 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
4374 static struct mdt_handler mdt_obd_ops[] = {
4375 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
4376 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
4377 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback)
4380 #define DEF_DLM_HNDL_0(flags, name, fn) \
4381 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
4382 #define DEF_DLM_HNDL_F(flags, name, fn) \
4383 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
4385 static struct mdt_handler mdt_dlm_ops[] = {
4386 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
4387 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
4388 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
4389 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
4392 static struct mdt_handler mdt_llog_ops[] = {
4395 #define DEF_SEC_CTX_HNDL(name, fn) \
4396 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
4398 static struct mdt_handler mdt_sec_ctx_ops[] = {
4399 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
4400 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
4401 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
4404 static struct mdt_opc_slice mdt_regular_handlers[] = {
4406 .mos_opc_start = MDS_GETATTR,
4407 .mos_opc_end = MDS_LAST_OPC,
4408 .mos_hs = mdt_mds_ops
4411 .mos_opc_start = OBD_PING,
4412 .mos_opc_end = OBD_LAST_OPC,
4413 .mos_hs = mdt_obd_ops
4416 .mos_opc_start = LDLM_ENQUEUE,
4417 .mos_opc_end = LDLM_LAST_OPC,
4418 .mos_hs = mdt_dlm_ops
4421 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
4422 .mos_opc_end = LLOG_LAST_OPC,
4423 .mos_hs = mdt_llog_ops
4426 .mos_opc_start = SEC_CTX_INIT,
4427 .mos_opc_end = SEC_LAST_OPC,
4428 .mos_hs = mdt_sec_ctx_ops
4435 static struct mdt_handler mdt_readpage_ops[] = {
4436 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
4437 #ifdef HAVE_SPLIT_SUPPORT
4438 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
4442 * XXX: this is ugly and should be fixed one day, see mdc_close() for
4443 * detailed comments. --umka
4445 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
4446 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
4449 static struct mdt_opc_slice mdt_readpage_handlers[] = {
4451 .mos_opc_start = MDS_GETATTR,
4452 .mos_opc_end = MDS_LAST_OPC,
4453 .mos_hs = mdt_readpage_ops
4460 static struct mdt_handler mdt_seq_ops[] = {
4461 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
4464 static struct mdt_opc_slice mdt_seq_handlers[] = {
4466 .mos_opc_start = SEQ_QUERY,
4467 .mos_opc_end = SEQ_LAST_OPC,
4468 .mos_hs = mdt_seq_ops
4475 static struct mdt_handler mdt_fld_ops[] = {
4476 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
4479 static struct mdt_opc_slice mdt_fld_handlers[] = {
4481 .mos_opc_start = FLD_QUERY,
4482 .mos_opc_end = FLD_LAST_OPC,
4483 .mos_hs = mdt_fld_ops
4490 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
4491 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
4492 MODULE_LICENSE("GPL");
4494 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
4495 "number of mdt service threads to start");
4497 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);