1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
50 # define EXPORT_SYMTAB
52 #define DEBUG_SUBSYSTEM S_MDS
54 #include <linux/module.h>
56 * struct OBD_{ALLOC,FREE}*()
58 #include <obd_support.h>
59 /* struct ptlrpc_request */
60 #include <lustre_net.h>
61 /* struct obd_export */
62 #include <lustre_export.h>
63 /* struct obd_device */
66 #include <dt_object.h>
67 #include <lustre_mds.h>
68 #include <lustre_mdt.h>
69 #include "mdt_internal.h"
70 #ifdef HAVE_QUOTA_SUPPORT
71 # include <lustre_quota.h>
73 #include <lustre_acl.h>
74 #include <lustre_param.h>
75 #include <lustre_fsfilt.h>
77 mdl_mode_t mdt_mdl_lock_modes[] = {
78 [LCK_MINMODE] = MDL_MINMODE,
85 [LCK_GROUP] = MDL_GROUP
88 ldlm_mode_t mdt_dlm_lock_modes[] = {
89 [MDL_MINMODE] = LCK_MINMODE,
96 [MDL_GROUP] = LCK_GROUP
100 * Initialized in mdt_mod_init().
102 unsigned long mdt_num_threads;
104 /* ptlrpc request handler for MDT. All handlers are
105 * grouped into several slices - struct mdt_opc_slice,
106 * and stored in an array - mdt_handlers[].
109 /* The name of this handler. */
111 /* Fail id for this handler, checked at the beginning of this handler*/
113 /* Operation code for this handler */
115 /* flags are listed in enum mdt_handler_flags below. */
117 /* The actual handler function to execute. */
118 int (*mh_act)(struct mdt_thread_info *info);
119 /* Request format for this request. */
120 const struct req_format *mh_fmt;
123 enum mdt_handler_flags {
125 * struct mdt_body is passed in the incoming message, and object
126 * identified by this fid exists on disk.
128 * "habeo corpus" == "I have a body"
130 HABEO_CORPUS = (1 << 0),
132 * struct ldlm_request is passed in the incoming message.
134 * "habeo clavis" == "I have a key"
136 HABEO_CLAVIS = (1 << 1),
138 * this request has fixed reply format, so that reply message can be
139 * packed by generic code.
141 * "habeo refero" == "I have a reply"
143 HABEO_REFERO = (1 << 2),
145 * this request will modify something, so check whether the filesystem
146 * is readonly or not, then return -EROFS to client asap if necessary.
148 * "mutabor" == "I shall modify"
153 struct mdt_opc_slice {
156 struct mdt_handler *mos_hs;
159 static struct mdt_opc_slice mdt_regular_handlers[];
160 static struct mdt_opc_slice mdt_readpage_handlers[];
161 static struct mdt_opc_slice mdt_xmds_handlers[];
162 static struct mdt_opc_slice mdt_seq_handlers[];
163 static struct mdt_opc_slice mdt_fld_handlers[];
165 static struct mdt_device *mdt_dev(struct lu_device *d);
166 static int mdt_regular_handle(struct ptlrpc_request *req);
167 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
168 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
169 struct getinfo_fid2path *fp);
171 static const struct lu_object_operations mdt_obj_ops;
173 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
177 return (rep->lock_policy_res1 & flag);
180 void mdt_clear_disposition(struct mdt_thread_info *info,
181 struct ldlm_reply *rep, int flag)
184 info->mti_opdata &= ~flag;
186 rep->lock_policy_res1 &= ~flag;
189 void mdt_set_disposition(struct mdt_thread_info *info,
190 struct ldlm_reply *rep, int flag)
193 info->mti_opdata |= flag;
195 rep->lock_policy_res1 |= flag;
198 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
200 lh->mlh_pdo_hash = 0;
201 lh->mlh_reg_mode = lm;
202 lh->mlh_type = MDT_REG_LOCK;
205 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
206 const char *name, int namelen)
208 lh->mlh_reg_mode = lm;
209 lh->mlh_type = MDT_PDO_LOCK;
212 LASSERT(namelen > 0);
213 lh->mlh_pdo_hash = full_name_hash(name, namelen);
215 LASSERT(namelen == 0);
216 lh->mlh_pdo_hash = 0ull;
220 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
221 struct mdt_lock_handle *lh)
227 * Any dir access needs couple of locks:
229 * 1) on part of dir we gonna take lookup/modify;
231 * 2) on whole dir to protect it from concurrent splitting and/or to
232 * flush client's cache for readdir().
234 * so, for a given mode and object this routine decides what lock mode
235 * to use for lock #2:
237 * 1) if caller's gonna lookup in dir then we need to protect dir from
238 * being splitted only - LCK_CR
240 * 2) if caller's gonna modify dir then we need to protect dir from
241 * being splitted and to flush cache - LCK_CW
243 * 3) if caller's gonna modify dir and that dir seems ready for
244 * splitting then we need to protect it from any type of access
245 * (lookup/modify/split) - LCK_EX --bzzz
248 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
249 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
252 * Ask underlaying level its opinion about preferable PDO lock mode
253 * having access type passed as regular lock mode:
255 * - MDL_MINMODE means that lower layer does not want to specify lock
258 * - MDL_NL means that no PDO lock should be taken. This is used in some
259 * cases. Say, for non-splittable directories no need to use PDO locks
262 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
263 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
265 if (mode != MDL_MINMODE) {
266 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
269 * Lower layer does not want to specify locking mode. We do it
270 * our selves. No special protection is needed, just flush
271 * client's cache on modification and allow concurrent
274 switch (lh->mlh_reg_mode) {
276 lh->mlh_pdo_mode = LCK_EX;
279 lh->mlh_pdo_mode = LCK_CR;
282 lh->mlh_pdo_mode = LCK_CW;
285 CERROR("Not expected lock type (0x%x)\n",
286 (int)lh->mlh_reg_mode);
291 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
295 static int mdt_getstatus(struct mdt_thread_info *info)
297 struct mdt_device *mdt = info->mti_mdt;
298 struct md_device *next = mdt->mdt_child;
299 struct mdt_body *repbody;
304 rc = mdt_check_ucred(info);
306 RETURN(err_serious(rc));
308 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
309 RETURN(err_serious(-ENOMEM));
311 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
312 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
316 repbody->valid |= OBD_MD_FLID;
318 if (mdt->mdt_opts.mo_mds_capa &&
319 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
320 struct mdt_object *root;
321 struct lustre_capa *capa;
323 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
325 RETURN(PTR_ERR(root));
327 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
329 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
330 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
332 mdt_object_put(info->mti_env, root);
334 repbody->valid |= OBD_MD_FLMDSCAPA;
340 static int mdt_statfs(struct mdt_thread_info *info)
342 struct md_device *next = info->mti_mdt->mdt_child;
343 struct ptlrpc_service *svc;
344 struct obd_statfs *osfs;
349 svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
351 /* This will trigger a watchdog timeout */
352 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
353 (MDT_SERVICE_WATCHDOG_FACTOR *
354 at_get(&svc->srv_at_estimate)) + 1);
356 rc = mdt_check_ucred(info);
358 RETURN(err_serious(rc));
360 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
361 rc = err_serious(-ENOMEM);
363 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
364 rc = next->md_ops->mdo_statfs(info->mti_env, next,
366 statfs_pack(osfs, &info->mti_u.ksfs);
371 void mdt_pack_size2body(struct mdt_thread_info *info, struct mdt_object *o)
374 struct lu_attr *attr = &info->mti_attr.ma_attr;
376 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
378 /* Check if Size-on-MDS is enabled. */
379 if ((mdt_conn_flags(info) & OBD_CONNECT_SOM) &&
380 S_ISREG(attr->la_mode) && mdt_sizeonmds_enabled(o)) {
381 b->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS);
382 b->size = attr->la_size;
383 b->blocks = attr->la_blocks;
387 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
388 const struct lu_attr *attr, const struct lu_fid *fid)
390 /*XXX should pack the reply body according to lu_valid*/
391 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
392 OBD_MD_FLGID | OBD_MD_FLTYPE |
393 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
394 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
396 if (!S_ISREG(attr->la_mode))
397 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
399 b->atime = attr->la_atime;
400 b->mtime = attr->la_mtime;
401 b->ctime = attr->la_ctime;
402 b->mode = attr->la_mode;
403 b->size = attr->la_size;
404 b->blocks = attr->la_blocks;
405 b->uid = attr->la_uid;
406 b->gid = attr->la_gid;
407 b->flags = attr->la_flags;
408 b->nlink = attr->la_nlink;
409 b->rdev = attr->la_rdev;
413 b->valid |= OBD_MD_FLID;
415 /* FIXME: these should be fixed when new igif ready.*/
416 b->ino = fid_oid(fid); /* 1.6 compatibility */
417 b->generation = fid_ver(fid); /* 1.6 compatibility */
418 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
420 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
421 PFID(fid), b->nlink, b->mode, b->size);
425 mdt_body_reverse_idmap(info, b);
428 static inline int mdt_body_has_lov(const struct lu_attr *la,
429 const struct mdt_body *body)
431 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
432 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
435 static int mdt_getattr_internal(struct mdt_thread_info *info,
436 struct mdt_object *o)
438 struct md_object *next = mdt_object_child(o);
439 const struct mdt_body *reqbody = info->mti_body;
440 struct ptlrpc_request *req = mdt_info_req(info);
441 struct md_attr *ma = &info->mti_attr;
442 struct lu_attr *la = &ma->ma_attr;
443 struct req_capsule *pill = info->mti_pill;
444 const struct lu_env *env = info->mti_env;
445 struct mdt_body *repbody;
446 struct lu_buf *buffer = &info->mti_buf;
450 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
451 RETURN(err_serious(-ENOMEM));
453 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
457 rc = mdt_object_exists(o);
459 /* This object is located on remote node.*/
460 repbody->fid1 = *mdt_object_fid(o);
461 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
465 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
466 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
468 /* If it is dir object and client require MEA, then we got MEA */
469 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
470 reqbody->valid & OBD_MD_MEA) {
471 /* Assumption: MDT_MD size is enough for lmv size. */
472 ma->ma_lmv = buffer->lb_buf;
473 ma->ma_lmv_size = buffer->lb_len;
474 ma->ma_need = MA_LMV | MA_INODE;
476 ma->ma_lmm = buffer->lb_buf;
477 ma->ma_lmm_size = buffer->lb_len;
478 ma->ma_need = MA_LOV | MA_INODE;
481 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
482 reqbody->valid & OBD_MD_FLDIREA &&
483 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
484 /* get default stripe info for this dir. */
485 ma->ma_need |= MA_LOV_DEF;
487 rc = mo_attr_get(env, next, ma);
489 CERROR("getattr error for "DFID": %d\n",
490 PFID(mdt_object_fid(o)), rc);
494 if (likely(ma->ma_valid & MA_INODE))
495 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
499 if (mdt_body_has_lov(la, reqbody)) {
500 if (ma->ma_valid & MA_LOV) {
501 LASSERT(ma->ma_lmm_size);
502 mdt_dump_lmm(D_INFO, ma->ma_lmm);
503 repbody->eadatasize = ma->ma_lmm_size;
504 if (S_ISDIR(la->la_mode))
505 repbody->valid |= OBD_MD_FLDIREA;
507 repbody->valid |= OBD_MD_FLEASIZE;
509 if (ma->ma_valid & MA_LMV) {
510 LASSERT(S_ISDIR(la->la_mode));
511 repbody->eadatasize = ma->ma_lmv_size;
512 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
514 if (!(ma->ma_valid & MA_LOV) && !(ma->ma_valid & MA_LMV)) {
515 repbody->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
517 } else if (S_ISLNK(la->la_mode) &&
518 reqbody->valid & OBD_MD_LINKNAME) {
519 buffer->lb_buf = ma->ma_lmm;
520 buffer->lb_len = reqbody->eadatasize;
521 rc = mo_readlink(env, next, buffer);
522 if (unlikely(rc <= 0)) {
523 CERROR("readlink failed: %d\n", rc);
526 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
528 repbody->valid |= OBD_MD_LINKNAME;
529 repbody->eadatasize = rc;
531 ((char*)ma->ma_lmm)[rc - 1] = 0;
532 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
533 (char*)ma->ma_lmm, rc);
538 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
539 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
540 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
541 repbody->valid |= OBD_MD_FLMODEASIZE;
542 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
543 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
544 repbody->max_cookiesize);
547 if (exp_connect_rmtclient(info->mti_exp) &&
548 reqbody->valid & OBD_MD_FLRMTPERM) {
549 void *buf = req_capsule_server_get(pill, &RMF_ACL);
551 /* mdt_getattr_lock only */
552 rc = mdt_pack_remote_perm(info, o, buf);
554 repbody->valid &= ~OBD_MD_FLRMTPERM;
555 repbody->aclsize = 0;
558 repbody->valid |= OBD_MD_FLRMTPERM;
559 repbody->aclsize = sizeof(struct mdt_remote_perm);
562 #ifdef CONFIG_FS_POSIX_ACL
563 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
564 (reqbody->valid & OBD_MD_FLACL)) {
565 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
566 buffer->lb_len = req_capsule_get_size(pill,
567 &RMF_ACL, RCL_SERVER);
568 if (buffer->lb_len > 0) {
569 rc = mo_xattr_get(env, next, buffer,
570 XATTR_NAME_ACL_ACCESS);
572 if (rc == -ENODATA) {
573 repbody->aclsize = 0;
574 repbody->valid |= OBD_MD_FLACL;
576 } else if (rc == -EOPNOTSUPP) {
579 CERROR("got acl size: %d\n", rc);
582 repbody->aclsize = rc;
583 repbody->valid |= OBD_MD_FLACL;
590 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
591 info->mti_mdt->mdt_opts.mo_mds_capa &&
592 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
593 struct lustre_capa *capa;
595 capa = req_capsule_server_get(pill, &RMF_CAPA1);
597 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
598 rc = mo_capa_get(env, next, capa, 0);
601 repbody->valid |= OBD_MD_FLMDSCAPA;
606 static int mdt_renew_capa(struct mdt_thread_info *info)
608 struct mdt_object *obj = info->mti_object;
609 struct mdt_body *body;
610 struct lustre_capa *capa, *c;
614 /* if object doesn't exist, or server has disabled capability,
615 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
618 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
619 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
622 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
623 LASSERT(body != NULL);
625 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
628 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
632 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
634 body->valid |= OBD_MD_FLOSSCAPA;
638 static int mdt_getattr(struct mdt_thread_info *info)
640 struct mdt_object *obj = info->mti_object;
641 struct req_capsule *pill = info->mti_pill;
642 struct mdt_body *reqbody;
643 struct mdt_body *repbody;
649 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
652 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
653 rc = req_capsule_server_pack(pill);
655 RETURN(err_serious(rc));
656 rc = mdt_renew_capa(info);
657 GOTO(out_shrink, rc);
660 LASSERT(obj != NULL);
661 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
663 mode = lu_object_attr(&obj->mot_obj.mo_lu);
664 if (S_ISLNK(mode) && (reqbody->valid & OBD_MD_LINKNAME) &&
665 (reqbody->eadatasize > info->mti_mdt->mdt_max_mdsize))
666 md_size = reqbody->eadatasize;
668 md_size = info->mti_mdt->mdt_max_mdsize;
670 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, md_size);
672 rc = req_capsule_server_pack(pill);
673 if (unlikely(rc != 0))
674 RETURN(err_serious(rc));
676 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
677 LASSERT(repbody != NULL);
678 repbody->eadatasize = 0;
679 repbody->aclsize = 0;
681 if (reqbody->valid & OBD_MD_FLRMTPERM)
682 rc = mdt_init_ucred(info, reqbody);
684 rc = mdt_check_ucred(info);
686 GOTO(out_shrink, rc);
688 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
689 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
692 * Don't check capability at all, because rename might getattr for
693 * remote obj, and at that time no capability is available.
695 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
696 rc = mdt_getattr_internal(info, obj);
697 if (reqbody->valid & OBD_MD_FLRMTPERM)
698 mdt_exit_ucred(info);
701 mdt_shrink_reply(info);
705 static int mdt_is_subdir(struct mdt_thread_info *info)
707 struct mdt_object *o = info->mti_object;
708 struct req_capsule *pill = info->mti_pill;
709 const struct mdt_body *body = info->mti_body;
710 struct mdt_body *repbody;
716 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
719 * We save last checked parent fid to @repbody->fid1 for remote
722 LASSERT(fid_is_sane(&body->fid2));
723 LASSERT(mdt_object_exists(o) > 0);
724 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
725 &body->fid2, &repbody->fid1);
726 if (rc == 0 || rc == -EREMOTE)
727 repbody->valid |= OBD_MD_FLID;
732 static int mdt_raw_lookup(struct mdt_thread_info *info,
733 struct mdt_object *parent,
734 const struct lu_name *lname,
735 struct ldlm_reply *ldlm_rep)
737 struct md_object *next = mdt_object_child(info->mti_object);
738 const struct mdt_body *reqbody = info->mti_body;
739 struct lu_fid *child_fid = &info->mti_tmp_fid1;
740 struct mdt_body *repbody;
744 if (reqbody->valid != OBD_MD_FLID)
747 LASSERT(!info->mti_cross_ref);
749 /* Only got the fid of this obj by name */
750 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
753 /* XXX is raw_lookup possible as intent operation? */
756 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
759 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
761 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
764 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
765 repbody->fid1 = *child_fid;
766 repbody->valid = OBD_MD_FLID;
772 * UPDATE lock should be taken against parent, and be release before exit;
773 * child_bits lock should be taken against child, and be returned back:
774 * (1)normal request should release the child lock;
775 * (2)intent request will grant the lock to client.
777 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
778 struct mdt_lock_handle *lhc,
780 struct ldlm_reply *ldlm_rep)
782 struct ptlrpc_request *req = mdt_info_req(info);
783 struct mdt_body *reqbody = NULL;
784 struct mdt_object *parent = info->mti_object;
785 struct mdt_object *child;
786 struct md_object *next = mdt_object_child(parent);
787 struct lu_fid *child_fid = &info->mti_tmp_fid1;
788 struct lu_name *lname = NULL;
789 const char *name = NULL;
791 struct mdt_lock_handle *lhp;
792 struct ldlm_lock *lock;
793 struct ldlm_res_id *res_id;
799 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
800 LASSERT(ergo(is_resent,
801 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
803 LASSERT(parent != NULL);
804 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
806 RETURN(err_serious(-EFAULT));
808 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
810 if (!info->mti_cross_ref) {
812 * XXX: Check for "namelen == 0" is for getattr by fid
813 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
814 * that is the name must contain at least one character and
815 * the terminating '\0'
818 reqbody = req_capsule_client_get(info->mti_pill,
820 LASSERT(fid_is_sane(&reqbody->fid2));
823 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
825 PFID(mdt_object_fid(parent)), PFID(&reqbody->fid2),
828 lname = mdt_name(info->mti_env, (char *)name, namelen);
829 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
830 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
834 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
836 rc = mdt_object_exists(parent);
837 if (unlikely(rc == 0)) {
838 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
839 &parent->mot_obj.mo_lu,
840 "Parent doesn't exist!\n");
842 } else if (!info->mti_cross_ref) {
843 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
844 PFID(mdt_object_fid(parent)));
847 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
855 if (info->mti_cross_ref) {
856 /* Only getattr on the child. Parent is on another node. */
857 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
859 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
860 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
863 /* Do not take lock for resent case. */
864 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
865 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
866 lhc->mlh_reg_lh.cookie);
867 LASSERT(fid_res_name_eq(mdt_object_fid(child),
868 &lock->l_resource->lr_name));
872 mdt_lock_handle_init(lhc);
873 mdt_lock_reg_init(lhc, LCK_PR);
876 * Object's name is on another MDS, no lookup lock is
877 * needed here but update is.
879 child_bits &= ~MDS_INODELOCK_LOOKUP;
880 child_bits |= MDS_INODELOCK_UPDATE;
882 rc = mdt_object_lock(info, child, lhc, child_bits,
886 /* Finally, we can get attr for child. */
887 mdt_set_capainfo(info, 0, mdt_object_fid(child),
889 rc = mdt_getattr_internal(info, child);
890 if (unlikely(rc != 0))
891 mdt_object_unlock(info, child, lhc, 1);
896 /* step 1: lock parent */
897 lhp = &info->mti_lh[MDT_LH_PARENT];
898 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
899 rc = mdt_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
902 if (unlikely(rc != 0))
906 /* step 2: lookup child's fid by name */
907 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
912 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
913 GOTO(out_parent, rc);
915 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
917 *child_fid = reqbody->fid2;
918 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
922 *step 3: find the child object by fid & lock it.
923 * regardless if it is local or remote.
925 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
927 if (unlikely(IS_ERR(child)))
928 GOTO(out_parent, rc = PTR_ERR(child));
930 /* Do not take lock for resent case. */
931 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
932 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
933 lhc->mlh_reg_lh.cookie);
935 res_id = &lock->l_resource->lr_name;
936 if (!fid_res_name_eq(mdt_object_fid(child),
937 &lock->l_resource->lr_name)) {
938 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
939 &lock->l_resource->lr_name),
940 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
941 (unsigned long)res_id->name[0],
942 (unsigned long)res_id->name[1],
943 (unsigned long)res_id->name[2],
944 PFID(mdt_object_fid(parent)));
945 CWARN("Although resent, but still not get child lock"
946 "parent:"DFID" child:"DFID"\n",
947 PFID(mdt_object_fid(parent)),
948 PFID(mdt_object_fid(child)));
949 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
958 ma = &info->mti_attr;
960 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
961 mdt_lock_handle_init(lhc);
962 mdt_lock_reg_init(lhc, LCK_PR);
964 if (mdt_object_exists(child) == 0) {
965 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
966 &child->mot_obj.mo_lu,
967 "Object doesn't exist!\n");
968 GOTO(out_child, rc = -ESTALE);
972 ma->ma_need = MA_INODE;
973 rc = mo_attr_get(info->mti_env, next, ma);
974 if (unlikely(rc != 0))
977 /* If the file has not been changed for some time, we return
978 * not only a LOOKUP lock, but also an UPDATE lock and this
979 * might save us RPC on later STAT. For directories, it also
980 * let negative dentry starts working for this dir. */
981 if (ma->ma_valid & MA_INODE &&
982 ma->ma_attr.la_valid & LA_CTIME &&
983 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
984 ma->ma_attr.la_ctime < cfs_time_current_sec())
985 child_bits |= MDS_INODELOCK_UPDATE;
987 rc = mdt_object_lock(info, child, lhc, child_bits,
990 if (unlikely(rc != 0))
994 /* finally, we can get attr for child. */
995 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
996 rc = mdt_getattr_internal(info, child);
997 if (unlikely(rc != 0)) {
998 mdt_object_unlock(info, child, lhc, 1);
1000 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1002 struct mdt_body *repbody;
1004 /* Debugging code. */
1005 res_id = &lock->l_resource->lr_name;
1006 LDLM_DEBUG(lock, "Returning lock to client");
1007 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1008 &lock->l_resource->lr_name),
1009 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1010 (unsigned long)res_id->name[0],
1011 (unsigned long)res_id->name[1],
1012 (unsigned long)res_id->name[2],
1013 PFID(mdt_object_fid(child)));
1015 * Pack Size-on-MDS inode attributes to the body if
1016 * update lock is given.
1018 repbody = req_capsule_server_get(info->mti_pill,
1020 if (lock->l_policy_data.l_inodebits.bits &
1021 MDS_INODELOCK_UPDATE)
1022 mdt_pack_size2body(info, child);
1023 LDLM_LOCK_PUT(lock);
1028 mdt_object_put(info->mti_env, child);
1030 mdt_object_unlock(info, parent, lhp, 1);
1034 /* normal handler: should release the child lock */
1035 static int mdt_getattr_name(struct mdt_thread_info *info)
1037 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1038 struct mdt_body *reqbody;
1039 struct mdt_body *repbody;
1043 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1044 LASSERT(reqbody != NULL);
1045 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1046 LASSERT(repbody != NULL);
1048 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1049 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1050 repbody->eadatasize = 0;
1051 repbody->aclsize = 0;
1053 rc = mdt_init_ucred(info, reqbody);
1055 GOTO(out_shrink, rc);
1057 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1058 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1059 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1060 lhc->mlh_reg_lh.cookie = 0;
1062 mdt_exit_ucred(info);
1065 mdt_shrink_reply(info);
1069 static const struct lu_device_operations mdt_lu_ops;
1071 static int lu_device_is_mdt(struct lu_device *d)
1073 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1076 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1077 void *karg, void *uarg);
1079 static int mdt_set_info(struct mdt_thread_info *info)
1081 struct ptlrpc_request *req = mdt_info_req(info);
1084 int keylen, vallen, rc = 0;
1087 rc = req_capsule_server_pack(info->mti_pill);
1091 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1093 DEBUG_REQ(D_HA, req, "no set_info key");
1097 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1100 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1102 DEBUG_REQ(D_HA, req, "no set_info val");
1106 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1109 /* Swab any part of val you need to here */
1110 if (KEY_IS(KEY_READ_ONLY)) {
1112 lustre_msg_set_status(req->rq_repmsg, 0);
1114 spin_lock(&req->rq_export->exp_lock);
1116 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1118 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1119 spin_unlock(&req->rq_export->exp_lock);
1121 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1122 struct changelog_setinfo *cs =
1123 (struct changelog_setinfo *)val;
1124 if (vallen != sizeof(*cs)) {
1125 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1128 if (ptlrpc_req_need_swab(req)) {
1129 __swab64s(&cs->cs_recno);
1130 __swab32s(&cs->cs_id);
1133 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1135 lustre_msg_set_status(req->rq_repmsg, rc);
1143 static int mdt_connect(struct mdt_thread_info *info)
1146 struct ptlrpc_request *req;
1148 req = mdt_info_req(info);
1149 rc = target_handle_connect(req);
1151 LASSERT(req->rq_export != NULL);
1152 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1153 rc = mdt_init_sec_level(info);
1155 rc = mdt_init_idmap(info);
1157 obd_disconnect(class_export_get(req->rq_export));
1159 rc = err_serious(rc);
1164 static int mdt_disconnect(struct mdt_thread_info *info)
1169 rc = target_handle_disconnect(mdt_info_req(info));
1171 rc = err_serious(rc);
1175 static int mdt_sendpage(struct mdt_thread_info *info,
1176 struct lu_rdpg *rdpg)
1178 struct ptlrpc_request *req = mdt_info_req(info);
1179 struct ptlrpc_bulk_desc *desc;
1180 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1188 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1193 for (i = 0, tmpcount = rdpg->rp_count;
1194 i < rdpg->rp_npages; i++, tmpcount -= tmpsize) {
1195 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1196 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1199 LASSERT(desc->bd_nob == rdpg->rp_count);
1200 rc = sptlrpc_svc_wrap_bulk(req, desc);
1202 GOTO(free_desc, rc);
1204 rc = ptlrpc_start_bulk_transfer(desc);
1206 GOTO(free_desc, rc);
1208 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1209 GOTO(abort_bulk, rc = 0);
1211 timeout = (int) req->rq_deadline - cfs_time_current_sec();
1213 CERROR("Req deadline already passed %lu (now: %lu)\n",
1214 req->rq_deadline, cfs_time_current_sec());
1215 *lwi = LWI_TIMEOUT(cfs_time_seconds(max(timeout, 1)), NULL, NULL);
1216 rc = l_wait_event(desc->bd_waitq, !ptlrpc_server_bulk_active(desc), lwi);
1217 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1220 if (desc->bd_success &&
1221 desc->bd_nob_transferred == rdpg->rp_count)
1222 GOTO(free_desc, rc);
1224 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
1227 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s",
1228 (rc == -ETIMEDOUT) ? "timeout" : "network error",
1229 desc->bd_nob_transferred, rdpg->rp_count,
1230 req->rq_export->exp_client_uuid.uuid,
1231 req->rq_export->exp_connection->c_remote_uuid.uuid);
1233 class_fail_export(req->rq_export);
1237 ptlrpc_abort_bulk(desc);
1239 ptlrpc_free_bulk(desc);
1243 #ifdef HAVE_SPLIT_SUPPORT
1245 * Retrieve dir entry from the page and insert it to the slave object, actually,
1246 * this should be in osd layer, but since it will not in the final product, so
1247 * just do it here and do not define more moo api anymore for this.
1249 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1252 struct mdt_object *object = info->mti_object;
1253 struct lu_fid *lf = &info->mti_tmp_fid2;
1254 struct md_attr *ma = &info->mti_attr;
1255 struct lu_dirpage *dp;
1256 struct lu_dirent *ent;
1257 int rc = 0, offset = 0;
1260 /* Make sure we have at least one entry. */
1265 * Disable trans for this name insert, since it will include many trans
1268 info->mti_no_need_trans = 1;
1270 * When write_dir_page, no need update parent's ctime,
1271 * and no permission check for name_insert.
1273 ma->ma_attr.la_ctime = 0;
1274 ma->ma_attr.la_valid = LA_MODE;
1275 ma->ma_valid = MA_INODE;
1278 dp = page_address(page);
1279 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1281 for (ent = lu_dirent_start(dp); ent != NULL;
1282 ent = lu_dirent_next(ent)) {
1283 struct lu_name *lname;
1286 if (le16_to_cpu(ent->lde_namelen) == 0)
1289 fid_le_to_cpu(lf, &ent->lde_fid);
1290 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1291 ma->ma_attr.la_mode = S_IFDIR;
1293 ma->ma_attr.la_mode = 0;
1294 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1296 GOTO(out, rc = -ENOMEM);
1298 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1299 lname = mdt_name(info->mti_env, name,
1300 le16_to_cpu(ent->lde_namelen));
1301 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1302 rc = mdo_name_insert(info->mti_env,
1303 md_object_next(&object->mot_obj),
1305 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1307 CERROR("Can't insert %*.*s, rc %d\n",
1308 le16_to_cpu(ent->lde_namelen),
1309 le16_to_cpu(ent->lde_namelen),
1314 offset += lu_dirent_size(ent);
1324 static int mdt_bulk_timeout(void *data)
1328 CERROR("mdt bulk transfer timeout \n");
1333 static int mdt_writepage(struct mdt_thread_info *info)
1335 struct ptlrpc_request *req = mdt_info_req(info);
1336 struct mdt_body *reqbody;
1337 struct l_wait_info *lwi;
1338 struct ptlrpc_bulk_desc *desc;
1344 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1345 if (reqbody == NULL)
1346 RETURN(err_serious(-EFAULT));
1348 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1350 RETURN(err_serious(-ENOMEM));
1352 /* allocate the page for the desc */
1353 page = cfs_alloc_page(CFS_ALLOC_STD);
1355 GOTO(desc_cleanup, rc = -ENOMEM);
1357 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1358 (int)reqbody->size, (int)reqbody->nlink);
1360 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1361 (int)reqbody->nlink);
1363 rc = sptlrpc_svc_prep_bulk(req, desc);
1365 GOTO(cleanup_page, rc);
1367 * Check if client was evicted while we were doing i/o before touching
1372 GOTO(cleanup_page, rc = -ENOMEM);
1374 if (desc->bd_export->exp_failed)
1377 rc = ptlrpc_start_bulk_transfer (desc);
1379 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * HZ / 4, HZ,
1380 mdt_bulk_timeout, desc);
1381 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1382 desc->bd_export->exp_failed, lwi);
1383 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1384 if (rc == -ETIMEDOUT) {
1385 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1386 ptlrpc_abort_bulk(desc);
1387 } else if (desc->bd_export->exp_failed) {
1388 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1390 ptlrpc_abort_bulk(desc);
1391 } else if (!desc->bd_success ||
1392 desc->bd_nob_transferred != desc->bd_nob) {
1393 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1395 "truncated" : "network error on",
1396 desc->bd_nob_transferred, desc->bd_nob);
1397 /* XXX should this be a different errno? */
1401 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1404 GOTO(cleanup_lwi, rc);
1405 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1410 cfs_free_page(page);
1412 ptlrpc_free_bulk(desc);
1417 static int mdt_readpage(struct mdt_thread_info *info)
1419 struct mdt_object *object = info->mti_object;
1420 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1421 struct mdt_body *reqbody;
1422 struct mdt_body *repbody;
1427 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1428 RETURN(err_serious(-ENOMEM));
1430 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1431 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1432 if (reqbody == NULL || repbody == NULL)
1433 RETURN(err_serious(-EFAULT));
1436 * prepare @rdpg before calling lower layers and transfer itself. Here
1437 * reqbody->size contains offset of where to start to read and
1438 * reqbody->nlink contains number bytes to read.
1440 rdpg->rp_hash = reqbody->size;
1441 if (rdpg->rp_hash != reqbody->size) {
1442 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1443 rdpg->rp_hash, reqbody->size);
1447 rdpg->rp_attrs = reqbody->mode;
1448 rdpg->rp_count = reqbody->nlink;
1449 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1)>>CFS_PAGE_SHIFT;
1450 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1451 if (rdpg->rp_pages == NULL)
1454 for (i = 0; i < rdpg->rp_npages; ++i) {
1455 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1456 if (rdpg->rp_pages[i] == NULL)
1457 GOTO(free_rdpg, rc = -ENOMEM);
1460 /* call lower layers to fill allocated pages with directory data */
1461 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1463 GOTO(free_rdpg, rc);
1465 /* send pages to client */
1466 rc = mdt_sendpage(info, rdpg);
1471 for (i = 0; i < rdpg->rp_npages; i++)
1472 if (rdpg->rp_pages[i] != NULL)
1473 cfs_free_page(rdpg->rp_pages[i]);
1474 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1476 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1482 static int mdt_reint_internal(struct mdt_thread_info *info,
1483 struct mdt_lock_handle *lhc,
1486 struct req_capsule *pill = info->mti_pill;
1487 struct mdt_device *mdt = info->mti_mdt;
1488 struct mdt_body *repbody;
1493 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1494 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1495 mdt->mdt_max_mdsize);
1496 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1497 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1498 mdt->mdt_max_cookiesize);
1500 rc = req_capsule_server_pack(pill);
1502 CERROR("Can't pack response, rc %d\n", rc);
1503 RETURN(err_serious(rc));
1506 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1507 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1509 repbody->eadatasize = 0;
1510 repbody->aclsize = 0;
1513 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK))
1514 GOTO(out_shrink, rc = err_serious(-EFAULT));
1516 rc = mdt_reint_unpack(info, op);
1518 CERROR("Can't unpack reint, rc %d\n", rc);
1519 GOTO(out_shrink, rc = err_serious(rc));
1522 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1524 /* for replay no cookkie / lmm need, because client have this already */
1525 if (info->mti_spec.no_create == 1) {
1526 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1527 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1529 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1530 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1534 rc = mdt_init_ucred_reint(info);
1536 GOTO(out_shrink, rc);
1538 rc = mdt_fix_attr_ucred(info, op);
1540 GOTO(out_ucred, rc = err_serious(rc));
1542 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1543 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1544 GOTO(out_ucred, rc);
1546 rc = mdt_reint_rec(info, lhc);
1549 mdt_exit_ucred(info);
1551 mdt_shrink_reply(info);
1555 static long mdt_reint_opcode(struct mdt_thread_info *info,
1556 const struct req_format **fmt)
1558 struct mdt_rec_reint *rec;
1561 opc = err_serious(-EFAULT);
1562 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1564 opc = rec->rr_opcode;
1565 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1566 if (opc < REINT_MAX && fmt[opc] != NULL)
1567 req_capsule_extend(info->mti_pill, fmt[opc]);
1569 CERROR("Unsupported opc: %ld\n", opc);
1570 opc = err_serious(opc);
1576 static int mdt_reint(struct mdt_thread_info *info)
1581 static const struct req_format *reint_fmts[REINT_MAX] = {
1582 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1583 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1584 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1585 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1586 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1587 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1588 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1593 opc = mdt_reint_opcode(info, reint_fmts);
1596 * No lock possible here from client to pass it to reint code
1599 rc = mdt_reint_internal(info, NULL, opc);
1604 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1608 /* this should sync the whole device */
1609 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1611 struct dt_device *dt = mdt->mdt_bottom;
1615 rc = dt->dd_ops->dt_sync(env, dt);
1619 /* this should sync this object */
1620 static int mdt_object_sync(struct mdt_thread_info *info)
1622 struct md_object *next;
1626 if (!mdt_object_exists(info->mti_object)) {
1627 CWARN("Non existing object "DFID"!\n",
1628 PFID(mdt_object_fid(info->mti_object)));
1631 next = mdt_object_child(info->mti_object);
1632 rc = mo_object_sync(info->mti_env, next);
1637 static int mdt_sync(struct mdt_thread_info *info)
1639 struct req_capsule *pill = info->mti_pill;
1640 struct mdt_body *body;
1644 /* The fid may be zero, so we req_capsule_set manually */
1645 req_capsule_set(pill, &RQF_MDS_SYNC);
1647 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1649 RETURN(err_serious(-EINVAL));
1651 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1652 RETURN(err_serious(-ENOMEM));
1654 if (fid_seq(&body->fid1) == 0) {
1655 /* sync the whole device */
1656 rc = req_capsule_server_pack(pill);
1658 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1660 rc = err_serious(rc);
1662 /* sync an object */
1663 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1665 rc = mdt_object_sync(info);
1667 struct md_object *next;
1668 const struct lu_fid *fid;
1669 struct lu_attr *la = &info->mti_attr.ma_attr;
1671 next = mdt_object_child(info->mti_object);
1672 info->mti_attr.ma_need = MA_INODE;
1673 info->mti_attr.ma_valid = 0;
1674 rc = mo_attr_get(info->mti_env, next,
1677 body = req_capsule_server_get(pill,
1679 fid = mdt_object_fid(info->mti_object);
1680 mdt_pack_attr2body(info, body, la, fid);
1684 rc = err_serious(rc);
1689 #ifdef HAVE_QUOTA_SUPPORT
1690 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1692 struct obd_quotactl *oqctl;
1693 struct req_capsule *pill = info->mti_pill;
1694 struct obd_export *exp = info->mti_exp;
1695 struct md_device *next = info->mti_mdt->mdt_child;
1699 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACHECK_NET))
1702 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1706 /* remote client has no permission for quotacheck */
1707 if (unlikely(exp_connect_rmtclient(exp)))
1710 rc = req_capsule_server_pack(pill);
1714 rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next, exp,
1719 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1721 struct obd_quotactl *oqctl, *repoqc;
1722 struct req_capsule *pill = info->mti_pill;
1723 struct obd_export *exp = info->mti_exp;
1724 struct md_device *next = info->mti_mdt->mdt_child;
1725 const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1729 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_QUOTACTL_NET))
1732 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1737 if (exp_connect_rmtclient(exp)) {
1738 struct ptlrpc_request *req = mdt_info_req(info);
1739 struct mdt_export_data *med = mdt_req2med(req);
1740 struct lustre_idmap_table *idmap = med->med_idmap;
1742 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1743 oqctl->qc_cmd != Q_GETINFO))
1747 if (oqctl->qc_type == USRQUOTA)
1748 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1750 else if (oqctl->qc_type == GRPQUOTA)
1751 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1756 if (id == CFS_IDMAP_NOTFOUND) {
1757 CDEBUG(D_QUOTA, "no mapping for id %u\n",
1763 rc = req_capsule_server_pack(pill);
1767 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1768 LASSERT(repoqc != NULL);
1770 switch (oqctl->qc_cmd) {
1772 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1775 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1778 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1782 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1786 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1790 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1794 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1798 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1801 case LUSTRE_Q_INVALIDATE:
1802 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1804 case LUSTRE_Q_FINVALIDATE:
1805 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1808 CERROR("unsupported mdt_quotactl command: %d\n",
1820 * OBD PING and other handlers.
1822 static int mdt_obd_ping(struct mdt_thread_info *info)
1827 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1829 rc = target_handle_ping(mdt_info_req(info));
1831 rc = err_serious(rc);
1835 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1837 return err_serious(-EOPNOTSUPP);
1840 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1842 return err_serious(-EOPNOTSUPP);
1850 /** clone llog ctxt from child (mdd)
1851 * This allows remote llog (replicator) access.
1852 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1853 * context was originally set up, or we can handle them directly.
1854 * I choose the latter, but that means I need any llog
1855 * contexts set up by child to be accessable by the mdt. So we clone the
1856 * context into our context list here.
1858 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1861 struct md_device *next = mdt->mdt_child;
1862 struct llog_ctxt *ctxt;
1865 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1868 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1869 if (rc || ctxt == NULL) {
1870 CERROR("Can't get mdd ctxt %d\n", rc);
1874 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1876 CERROR("Can't set mdt ctxt %d\n", rc);
1881 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1882 struct mdt_device *mdt, int idx)
1884 struct llog_ctxt *ctxt;
1886 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1889 /* Put once for the get we just did, and once for the clone */
1890 llog_ctxt_put(ctxt);
1891 llog_ctxt_put(ctxt);
1895 static int mdt_llog_create(struct mdt_thread_info *info)
1899 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1900 rc = llog_origin_handle_create(mdt_info_req(info));
1901 return (rc < 0 ? err_serious(rc) : rc);
1904 static int mdt_llog_destroy(struct mdt_thread_info *info)
1908 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1909 rc = llog_origin_handle_destroy(mdt_info_req(info));
1910 return (rc < 0 ? err_serious(rc) : rc);
1913 static int mdt_llog_read_header(struct mdt_thread_info *info)
1917 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1918 rc = llog_origin_handle_read_header(mdt_info_req(info));
1919 return (rc < 0 ? err_serious(rc) : rc);
1922 static int mdt_llog_next_block(struct mdt_thread_info *info)
1926 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1927 rc = llog_origin_handle_next_block(mdt_info_req(info));
1928 return (rc < 0 ? err_serious(rc) : rc);
1931 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1935 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1936 rc = llog_origin_handle_prev_block(mdt_info_req(info));
1937 return (rc < 0 ? err_serious(rc) : rc);
1944 static struct ldlm_callback_suite cbs = {
1945 .lcs_completion = ldlm_server_completion_ast,
1946 .lcs_blocking = ldlm_server_blocking_ast,
1950 static int mdt_enqueue(struct mdt_thread_info *info)
1952 struct ptlrpc_request *req;
1957 * info->mti_dlm_req already contains swapped and (if necessary)
1958 * converted dlm request.
1960 LASSERT(info->mti_dlm_req != NULL);
1962 req = mdt_info_req(info);
1965 * Lock without inodebits makes no sense and will oops later in
1966 * ldlm. Let's check it now to see if we have wrong lock from client or
1967 * bits get corrupted somewhere in mdt_intent_policy().
1969 req_bits = info->mti_dlm_req->lock_desc.l_policy_data.l_inodebits.bits;
1970 /* This is disabled because we need to support liblustre flock.
1971 * LASSERT(req_bits != 0);
1974 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
1975 req, info->mti_dlm_req, &cbs);
1976 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
1977 return rc ? err_serious(rc) : req->rq_status;
1980 static int mdt_convert(struct mdt_thread_info *info)
1983 struct ptlrpc_request *req;
1985 LASSERT(info->mti_dlm_req);
1986 req = mdt_info_req(info);
1987 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
1988 return rc ? err_serious(rc) : req->rq_status;
1991 static int mdt_bl_callback(struct mdt_thread_info *info)
1993 CERROR("bl callbacks should not happen on MDS\n");
1995 return err_serious(-EOPNOTSUPP);
1998 static int mdt_cp_callback(struct mdt_thread_info *info)
2000 CERROR("cp callbacks should not happen on MDS\n");
2002 return err_serious(-EOPNOTSUPP);
2006 * sec context handlers
2008 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2012 rc = mdt_handle_idmap(info);
2015 struct ptlrpc_request *req = mdt_info_req(info);
2018 opc = lustre_msg_get_opc(req->rq_reqmsg);
2019 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2020 sptlrpc_svc_ctx_invalidate(req);
2023 OBD_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, obd_fail_val);
2028 static struct mdt_object *mdt_obj(struct lu_object *o)
2030 LASSERT(lu_device_is_mdt(o->lo_dev));
2031 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2034 struct mdt_object *mdt_object_find(const struct lu_env *env,
2035 struct mdt_device *d,
2036 const struct lu_fid *f)
2038 struct lu_object *o;
2039 struct mdt_object *m;
2042 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2043 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2044 if (unlikely(IS_ERR(o)))
2045 m = (struct mdt_object *)o;
2052 * Asyncronous commit for mdt device.
2054 * Pass asynchonous commit call down the MDS stack.
2056 * \param env environment
2057 * \param mdt the mdt device
2059 static void mdt_device_commit_async(const struct lu_env *env,
2060 struct mdt_device *mdt)
2062 struct dt_device *dt = mdt->mdt_bottom;
2065 rc = dt->dd_ops->dt_commit_async(env, dt);
2066 if (unlikely(rc != 0))
2067 CWARN("async commit start failed with rc = %d", rc);
2071 * Mark the lock as "synchonous".
2073 * Mark the lock to deffer transaction commit to the unlock time.
2075 * \param lock the lock to mark as "synchonous"
2077 * \see mdt_is_lock_sync
2078 * \see mdt_save_lock
2080 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2082 lock->l_ast_data = (void*)1;
2086 * Check whehter the lock "synchonous" or not.
2088 * \param lock the lock to check
2089 * \retval 1 the lock is "synchonous"
2090 * \retval 0 the lock isn't "synchronous"
2092 * \see mdt_set_lock_sync
2093 * \see mdt_save_lock
2095 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2097 return lock->l_ast_data != NULL;
2101 * Blocking AST for mdt locks.
2103 * Starts transaction commit if in case of COS lock conflict or
2104 * deffers such a commit to the mdt_save_lock.
2106 * \param lock the lock which blocks a request or cancelling lock
2107 * \param desc unused
2108 * \param data unused
2109 * \param flag indicates whether this cancelling or blocking callback
2111 * \see ldlm_blocking_ast_nocheck
2113 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2114 void *data, int flag)
2116 struct obd_device *obd = lock->l_resource->lr_namespace->ns_obd;
2117 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2121 if (flag == LDLM_CB_CANCELING)
2123 lock_res_and_lock(lock);
2124 if (lock->l_blocking_ast != mdt_blocking_ast) {
2125 unlock_res_and_lock(lock);
2128 if (mdt_cos_is_enabled(mdt) &&
2129 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2130 lock->l_blocking_lock != NULL &&
2131 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2132 mdt_set_lock_sync(lock);
2134 rc = ldlm_blocking_ast_nocheck(lock);
2136 /* There is no lock conflict if l_blocking_lock == NULL,
2137 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2138 * when the last reference to a local lock was released */
2139 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2142 rc = lu_env_init(&env, LCT_MD_THREAD);
2143 if (unlikely(rc != 0))
2144 CWARN("lu_env initialization failed with rc = %d,"
2145 "cannot start asynchronous commit\n", rc);
2147 mdt_device_commit_async(&env, mdt);
2153 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2154 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2156 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2157 ldlm_policy_data_t *policy = &info->mti_policy;
2158 struct ldlm_res_id *res_id = &info->mti_res_id;
2162 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2163 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2164 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2165 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2167 if (mdt_object_exists(o) < 0) {
2168 if (locality == MDT_CROSS_LOCK) {
2169 /* cross-ref object fix */
2170 ibits &= ~MDS_INODELOCK_UPDATE;
2171 ibits |= MDS_INODELOCK_LOOKUP;
2173 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2174 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2176 /* No PDO lock on remote object */
2177 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2180 if (lh->mlh_type == MDT_PDO_LOCK) {
2181 /* check for exists after object is locked */
2182 if (mdt_object_exists(o) == 0) {
2183 /* Non-existent object shouldn't have PDO lock */
2186 /* Non-dir object shouldn't have PDO lock */
2187 LASSERT(S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)));
2191 memset(policy, 0, sizeof(*policy));
2192 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2195 * Take PDO lock on whole directory and build correct @res_id for lock
2196 * on part of directory.
2198 if (lh->mlh_pdo_hash != 0) {
2199 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2200 mdt_lock_pdo_mode(info, o, lh);
2201 if (lh->mlh_pdo_mode != LCK_NL) {
2203 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2204 * is never going to be sent to client and we do not
2205 * want it slowed down due to possible cancels.
2207 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2208 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2209 policy, res_id, LDLM_FL_ATOMIC_CB,
2210 &info->mti_exp->exp_handle.h_cookie);
2216 * Finish res_id initializing by name hash marking part of
2217 * directory which is taking modification.
2219 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2222 policy->l_inodebits.bits = ibits;
2225 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2226 * going to be sent to client. If it is - mdt_intent_policy() path will
2227 * fix it up and turn FL_LOCAL flag off.
2229 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2230 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2231 &info->mti_exp->exp_handle.h_cookie);
2237 mdt_object_unlock(info, o, lh, 1);
2244 * Save a lock within request object.
2246 * Keep the lock referenced until whether client ACK or transaction
2247 * commit happens or release the lock immediately depending on input
2248 * parameters. If COS is ON, a write lock is converted to COS lock
2251 * \param info thead info object
2252 * \param h lock handle
2253 * \param mode lock mode
2254 * \param decref force immediate lock releasing
2257 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2258 ldlm_mode_t mode, int decref)
2262 if (lustre_handle_is_used(h)) {
2263 if (decref || !info->mti_has_trans ||
2264 !(mode & (LCK_PW | LCK_EX))){
2265 mdt_fid_unlock(h, mode);
2267 struct mdt_device *mdt = info->mti_mdt;
2268 struct ldlm_lock *lock = ldlm_handle2lock(h);
2269 struct ptlrpc_request *req = mdt_info_req(info);
2272 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2274 CDEBUG(D_HA, "request = %p reply state = %p"
2275 " transno = "LPD64"\n",
2276 req, req->rq_reply_state, req->rq_transno);
2277 if (mdt_cos_is_enabled(mdt)) {
2279 ldlm_lock_downgrade(lock, LCK_COS);
2282 ptlrpc_save_lock(req, h, mode, no_ack);
2283 if (mdt_is_lock_sync(lock)) {
2284 CDEBUG(D_HA, "found sync-lock,"
2285 " async commit started\n");
2286 mdt_device_commit_async(info->mti_env,
2289 LDLM_LOCK_PUT(lock);
2298 * Unlock mdt object.
2300 * Immeditely release the regular lock and the PDO lock or save the
2301 * lock in reqeuest and keep them referenced until client ACK or
2302 * transaction commit.
2304 * \param info thread info object
2305 * \param o mdt object
2306 * \param lh mdt lock handle referencing regular and PDO locks
2307 * \param decref force immediate lock releasing
2309 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2310 struct mdt_lock_handle *lh, int decref)
2314 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2315 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2320 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2321 const struct lu_fid *f,
2322 struct mdt_lock_handle *lh,
2325 struct mdt_object *o;
2327 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2331 rc = mdt_object_lock(info, o, lh, ibits,
2334 mdt_object_put(info->mti_env, o);
2341 void mdt_object_unlock_put(struct mdt_thread_info * info,
2342 struct mdt_object * o,
2343 struct mdt_lock_handle *lh,
2346 mdt_object_unlock(info, o, lh, decref);
2347 mdt_object_put(info->mti_env, o);
2350 static struct mdt_handler *mdt_handler_find(__u32 opc,
2351 struct mdt_opc_slice *supported)
2353 struct mdt_opc_slice *s;
2354 struct mdt_handler *h;
2357 for (s = supported; s->mos_hs != NULL; s++) {
2358 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2359 h = s->mos_hs + (opc - s->mos_opc_start);
2360 if (likely(h->mh_opc != 0))
2361 LASSERTF(h->mh_opc == opc,
2362 "opcode mismatch %d != %d\n",
2365 h = NULL; /* unsupported opc */
2372 static int mdt_lock_resname_compat(struct mdt_device *m,
2373 struct ldlm_request *req)
2375 /* XXX something... later. */
2379 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2381 /* XXX something... later. */
2386 * Generic code handling requests that have struct mdt_body passed in:
2388 * - extract mdt_body from request and save it in @info, if present;
2390 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2393 * - if HABEO_CORPUS flag is set for this request type check whether object
2394 * actually exists on storage (lu_object_exists()).
2397 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2399 const struct mdt_body *body;
2400 struct mdt_object *obj;
2401 const struct lu_env *env;
2402 struct req_capsule *pill;
2406 env = info->mti_env;
2407 pill = info->mti_pill;
2409 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2413 if (!(body->valid & OBD_MD_FLID))
2416 if (!fid_is_sane(&body->fid1)) {
2417 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2422 * Do not get size or any capa fields before we check that request
2423 * contains capa actually. There are some requests which do not, for
2424 * instance MDS_IS_SUBDIR.
2426 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2427 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2428 mdt_set_capainfo(info, 0, &body->fid1,
2429 req_capsule_client_get(pill, &RMF_CAPA1));
2431 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2433 if ((flags & HABEO_CORPUS) &&
2434 !mdt_object_exists(obj)) {
2435 mdt_object_put(env, obj);
2436 /* for capability renew ENOENT will be handled in
2438 if (body->valid & OBD_MD_FLOSSCAPA)
2443 info->mti_object = obj;
2452 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2454 struct req_capsule *pill = info->mti_pill;
2458 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2459 rc = mdt_body_unpack(info, flags);
2463 if (rc == 0 && (flags & HABEO_REFERO)) {
2464 struct mdt_device *mdt = info->mti_mdt;
2468 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2469 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2470 mdt->mdt_max_mdsize);
2471 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2472 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2473 mdt->mdt_max_cookiesize);
2475 rc = req_capsule_server_pack(pill);
2480 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2482 struct md_device *next = m->mdt_child;
2484 return next->md_ops->mdo_init_capa_ctxt(env, next,
2485 m->mdt_opts.mo_mds_capa,
2486 m->mdt_capa_timeout,
2492 * Invoke handler for this request opc. Also do necessary preprocessing
2493 * (according to handler ->mh_flags), and post-processing (setting of
2494 * ->last_{xid,committed}).
2496 static int mdt_req_handle(struct mdt_thread_info *info,
2497 struct mdt_handler *h, struct ptlrpc_request *req)
2499 int rc, serious = 0;
2504 LASSERT(h->mh_act != NULL);
2505 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2506 LASSERT(current->journal_info == NULL);
2509 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2510 * to put same checks into handlers like mdt_close(), mdt_reint(),
2511 * etc., without talking to mdt authors first. Checking same thing
2512 * there again is useless and returning 0 error wihtout packing reply
2513 * is buggy! Handlers either pack reply or return error.
2515 * We return 0 here and do not send any reply in order to emulate
2516 * network failure. Do not send any reply in case any of NET related
2517 * fail_id has occured.
2519 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2523 flags = h->mh_flags;
2524 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2526 if (h->mh_fmt != NULL) {
2527 req_capsule_set(info->mti_pill, h->mh_fmt);
2528 rc = mdt_unpack_req_pack_rep(info, flags);
2531 if (rc == 0 && flags & MUTABOR &&
2532 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2533 /* should it be rq_status? */
2536 if (rc == 0 && flags & HABEO_CLAVIS) {
2537 struct ldlm_request *dlm_req;
2539 LASSERT(h->mh_fmt != NULL);
2541 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2542 if (dlm_req != NULL) {
2543 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2544 rc = mdt_lock_resname_compat(info->mti_mdt,
2546 info->mti_dlm_req = dlm_req;
2552 /* capability setting changed via /proc, needs reinitialize ctxt */
2553 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2554 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2555 info->mti_mdt->mdt_capa_conf = 0;
2558 if (likely(rc == 0)) {
2560 * Process request, there can be two types of rc:
2561 * 1) errors with msg unpack/pack, other failures outside the
2562 * operation itself. This is counted as serious errors;
2563 * 2) errors during fs operation, should be placed in rq_status
2566 rc = h->mh_act(info);
2568 !req->rq_no_reply && req->rq_reply_state == NULL) {
2569 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2570 "pack reply and returned 0 error\n",
2574 serious = is_serious(rc);
2575 rc = clear_serious(rc);
2579 req->rq_status = rc;
2582 * ELDLM_* codes which > 0 should be in rq_status only as well as
2583 * all non-serious errors.
2585 if (rc > 0 || !serious)
2588 LASSERT(current->journal_info == NULL);
2590 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2591 info->mti_mdt->mdt_opts.mo_compat_resname) {
2592 struct ldlm_reply *dlmrep;
2594 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2596 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2599 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2600 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2601 target_committed_to_req(req);
2603 if (unlikely((lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) &&
2604 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2605 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2609 target_send_reply(req, rc, info->mti_fail_id);
2613 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2615 lh->mlh_type = MDT_NUL_LOCK;
2616 lh->mlh_reg_lh.cookie = 0ull;
2617 lh->mlh_reg_mode = LCK_MINMODE;
2618 lh->mlh_pdo_lh.cookie = 0ull;
2619 lh->mlh_pdo_mode = LCK_MINMODE;
2622 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2624 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2625 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2629 * Initialize fields of struct mdt_thread_info. Other fields are left in
2630 * uninitialized state, because it's too expensive to zero out whole
2631 * mdt_thread_info (> 1K) on each request arrival.
2633 static void mdt_thread_info_init(struct ptlrpc_request *req,
2634 struct mdt_thread_info *info)
2637 struct md_capainfo *ci;
2639 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2640 info->mti_pill = &req->rq_pill;
2643 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2644 mdt_lock_handle_init(&info->mti_lh[i]);
2646 /* mdt device: it can be NULL while CONNECT */
2647 if (req->rq_export) {
2648 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2649 info->mti_exp = req->rq_export;
2651 info->mti_mdt = NULL;
2652 info->mti_env = req->rq_svc_thread->t_env;
2653 ci = md_capainfo(info->mti_env);
2654 memset(ci, 0, sizeof *ci);
2655 if (req->rq_export) {
2656 if (exp_connect_rmtclient(req->rq_export))
2657 ci->mc_auth = LC_ID_CONVERT;
2658 else if (req->rq_export->exp_connect_flags &
2659 OBD_CONNECT_MDS_CAPA)
2660 ci->mc_auth = LC_ID_PLAIN;
2662 ci->mc_auth = LC_ID_NONE;
2665 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2666 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2667 info->mti_mos[0] = NULL;
2668 info->mti_mos[1] = NULL;
2669 info->mti_mos[2] = NULL;
2670 info->mti_mos[3] = NULL;
2672 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2673 info->mti_body = NULL;
2674 info->mti_object = NULL;
2675 info->mti_dlm_req = NULL;
2676 info->mti_has_trans = 0;
2677 info->mti_no_need_trans = 0;
2678 info->mti_cross_ref = 0;
2679 info->mti_opdata = 0;
2681 /* To not check for split by default. */
2682 info->mti_spec.sp_ck_split = 0;
2683 info->mti_spec.no_create = 0;
2686 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2690 req_capsule_fini(info->mti_pill);
2691 if (info->mti_object != NULL) {
2693 * freeing an object may lead to OSD level transaction, do not
2694 * let it mess with MDT. bz19385.
2696 info->mti_no_need_trans = 1;
2697 mdt_object_put(info->mti_env, info->mti_object);
2698 info->mti_object = NULL;
2700 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2701 mdt_lock_handle_fini(&info->mti_lh[i]);
2702 info->mti_env = NULL;
2705 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2706 struct obd_device *obd, int *process)
2708 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2709 case MDS_CONNECT: /* This will never get here, but for completeness. */
2710 case OST_CONNECT: /* This will never get here, but for completeness. */
2711 case MDS_DISCONNECT:
2712 case OST_DISCONNECT:
2717 case MDS_DONE_WRITING:
2718 case MDS_SYNC: /* used in unmounting */
2724 *process = target_queue_recovery_request(req, obd);
2728 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2735 * Handle recovery. Return:
2736 * +1: continue request processing;
2737 * -ve: abort immediately with the given error code;
2738 * 0: send reply with error code in req->rq_status;
2740 static int mdt_recovery(struct mdt_thread_info *info)
2742 struct ptlrpc_request *req = mdt_info_req(info);
2744 struct obd_device *obd;
2748 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2751 case SEC_CTX_INIT_CONT:
2757 rc = mdt_handle_idmap(info);
2766 if (unlikely(req->rq_export == NULL)) {
2767 CERROR("operation %d on unconnected MDS from %s\n",
2768 lustre_msg_get_opc(req->rq_reqmsg),
2769 libcfs_id2str(req->rq_peer));
2770 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2771 * mds_A will get -ENOTCONN(especially for ping req),
2772 * which will cause that mds_A deactive timeout, then when
2773 * mds_A cleanup, the cleanup process will be suspended since
2774 * deactive timeout is not zero.
2776 req->rq_status = -ENOTCONN;
2777 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2781 /* sanity check: if the xid matches, the request must be marked as a
2782 * resent or replayed */
2783 if (req_xid_is_last(req)) {
2784 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2785 (MSG_RESENT | MSG_REPLAY))) {
2786 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2787 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2788 lustre_msg_get_flags(req->rq_reqmsg));
2790 req->rq_status = -ENOTCONN;
2795 /* else: note the opposite is not always true; a RESENT req after a
2796 * failover will usually not match the last_xid, since it was likely
2797 * never committed. A REPLAYed request will almost never match the
2798 * last xid, however it could for a committed, but still retained,
2801 obd = req->rq_export->exp_obd;
2803 /* Check for aborted recovery... */
2804 spin_lock_bh(&obd->obd_processing_task_lock);
2805 recovering = obd->obd_recovering;
2806 spin_unlock_bh(&obd->obd_processing_task_lock);
2807 if (unlikely(recovering)) {
2810 DEBUG_REQ(D_INFO, req, "Got new replay");
2811 rc = mdt_filter_recovery_request(req, obd, &should_process);
2812 if (rc != 0 || !should_process)
2814 else if (should_process < 0) {
2815 req->rq_status = should_process;
2816 rc = ptlrpc_error(req);
2823 static int mdt_msg_check_version(struct lustre_msg *msg)
2827 switch (lustre_msg_get_opc(msg)) {
2829 case MDS_DISCONNECT:
2833 case SEC_CTX_INIT_CONT:
2835 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2837 CERROR("bad opc %u version %08x, expecting %08x\n",
2838 lustre_msg_get_opc(msg),
2839 lustre_msg_get_version(msg),
2840 LUSTRE_OBD_VERSION);
2844 case MDS_GETATTR_NAME:
2851 case MDS_DONE_WRITING:
2857 case MDS_QUOTACHECK:
2863 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2865 CERROR("bad opc %u version %08x, expecting %08x\n",
2866 lustre_msg_get_opc(msg),
2867 lustre_msg_get_version(msg),
2868 LUSTRE_MDS_VERSION);
2872 case LDLM_BL_CALLBACK:
2873 case LDLM_CP_CALLBACK:
2874 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2876 CERROR("bad opc %u version %08x, expecting %08x\n",
2877 lustre_msg_get_opc(msg),
2878 lustre_msg_get_version(msg),
2879 LUSTRE_DLM_VERSION);
2881 case OBD_LOG_CANCEL:
2882 case LLOG_ORIGIN_HANDLE_CREATE:
2883 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2884 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2885 case LLOG_ORIGIN_HANDLE_CLOSE:
2886 case LLOG_ORIGIN_HANDLE_DESTROY:
2887 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2889 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2891 CERROR("bad opc %u version %08x, expecting %08x\n",
2892 lustre_msg_get_opc(msg),
2893 lustre_msg_get_version(msg),
2894 LUSTRE_LOG_VERSION);
2897 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2903 static int mdt_handle0(struct ptlrpc_request *req,
2904 struct mdt_thread_info *info,
2905 struct mdt_opc_slice *supported)
2907 struct mdt_handler *h;
2908 struct lustre_msg *msg;
2913 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2916 LASSERT(current->journal_info == NULL);
2918 msg = req->rq_reqmsg;
2919 rc = mdt_msg_check_version(msg);
2920 if (likely(rc == 0)) {
2921 rc = mdt_recovery(info);
2922 if (likely(rc == +1)) {
2923 h = mdt_handler_find(lustre_msg_get_opc(msg),
2925 if (likely(h != NULL)) {
2926 rc = mdt_req_handle(info, h, req);
2928 CERROR("The unsupported opc: 0x%x\n",
2929 lustre_msg_get_opc(msg) );
2930 req->rq_status = -ENOTSUPP;
2931 rc = ptlrpc_error(req);
2936 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2941 * MDT handler function called by ptlrpc service thread when request comes.
2943 * XXX common "target" functionality should be factored into separate module
2944 * shared by mdt, ost and stand-alone services like fld.
2946 static int mdt_handle_common(struct ptlrpc_request *req,
2947 struct mdt_opc_slice *supported)
2950 struct mdt_thread_info *info;
2954 env = req->rq_svc_thread->t_env;
2955 LASSERT(env != NULL);
2956 LASSERT(env->le_ses != NULL);
2957 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
2958 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
2959 LASSERT(info != NULL);
2961 mdt_thread_info_init(req, info);
2963 rc = mdt_handle0(req, info, supported);
2965 mdt_thread_info_fini(info);
2970 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
2973 int mdt_recovery_handle(struct ptlrpc_request *req)
2978 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2980 rc = mdt_handle_common(req, mdt_fld_handlers);
2983 rc = mdt_handle_common(req, mdt_seq_handlers);
2986 rc = mdt_handle_common(req, mdt_regular_handlers);
2993 static int mdt_regular_handle(struct ptlrpc_request *req)
2995 return mdt_handle_common(req, mdt_regular_handlers);
2998 static int mdt_readpage_handle(struct ptlrpc_request *req)
3000 return mdt_handle_common(req, mdt_readpage_handlers);
3003 static int mdt_xmds_handle(struct ptlrpc_request *req)
3005 return mdt_handle_common(req, mdt_xmds_handlers);
3008 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3010 return mdt_handle_common(req, mdt_seq_handlers);
3013 static int mdt_mdss_handle(struct ptlrpc_request *req)
3015 return mdt_handle_common(req, mdt_seq_handlers);
3018 static int mdt_dtss_handle(struct ptlrpc_request *req)
3020 return mdt_handle_common(req, mdt_seq_handlers);
3023 static int mdt_fld_handle(struct ptlrpc_request *req)
3025 return mdt_handle_common(req, mdt_fld_handlers);
3041 static int mdt_intent_getattr(enum mdt_it_code opcode,
3042 struct mdt_thread_info *info,
3043 struct ldlm_lock **,
3045 static int mdt_intent_reint(enum mdt_it_code opcode,
3046 struct mdt_thread_info *info,
3047 struct ldlm_lock **,
3050 static struct mdt_it_flavor {
3051 const struct req_format *it_fmt;
3053 int (*it_act)(enum mdt_it_code ,
3054 struct mdt_thread_info *,
3055 struct ldlm_lock **,
3058 } mdt_it_flavor[] = {
3060 .it_fmt = &RQF_LDLM_INTENT,
3061 /*.it_flags = HABEO_REFERO,*/
3063 .it_act = mdt_intent_reint,
3064 .it_reint = REINT_OPEN
3067 .it_fmt = &RQF_LDLM_INTENT,
3068 .it_flags = MUTABOR,
3069 .it_act = mdt_intent_reint,
3070 .it_reint = REINT_OPEN
3073 .it_fmt = &RQF_LDLM_INTENT,
3074 .it_flags = MUTABOR,
3075 .it_act = mdt_intent_reint,
3076 .it_reint = REINT_CREATE
3078 [MDT_IT_GETATTR] = {
3079 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3080 .it_flags = HABEO_REFERO,
3081 .it_act = mdt_intent_getattr
3083 [MDT_IT_READDIR] = {
3089 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3090 .it_flags = HABEO_REFERO,
3091 .it_act = mdt_intent_getattr
3094 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3095 .it_flags = MUTABOR,
3097 .it_reint = REINT_UNLINK
3101 .it_flags = MUTABOR,
3104 [MDT_IT_GETXATTR] = {
3111 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3112 struct ldlm_lock **lockp,
3113 struct ldlm_lock *new_lock,
3114 struct mdt_lock_handle *lh,
3117 struct ptlrpc_request *req = mdt_info_req(info);
3118 struct ldlm_lock *lock = *lockp;
3121 * Get new lock only for cases when possible resent did not find any
3124 if (new_lock == NULL)
3125 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3127 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3128 lh->mlh_reg_lh.cookie = 0;
3132 LASSERTF(new_lock != NULL,
3133 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3136 * If we've already given this lock to a client once, then we should
3137 * have no readers or writers. Otherwise, we should have one reader
3138 * _or_ writer ref (which will be zeroed below) before returning the
3141 if (new_lock->l_export == req->rq_export) {
3142 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3144 LASSERT(new_lock->l_export == NULL);
3145 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3150 if (new_lock->l_export == req->rq_export) {
3152 * Already gave this to the client, which means that we
3153 * reconstructed a reply.
3155 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3157 lh->mlh_reg_lh.cookie = 0;
3158 RETURN(ELDLM_LOCK_REPLACED);
3162 * Fixup the lock to be given to the client.
3164 lock_res_and_lock(new_lock);
3165 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3166 * possible blocking AST. */
3167 while (new_lock->l_readers > 0) {
3168 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3169 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3170 new_lock->l_readers--;
3172 while (new_lock->l_writers > 0) {
3173 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3174 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3175 new_lock->l_writers--;
3178 new_lock->l_export = class_export_lock_get(req->rq_export);
3179 new_lock->l_blocking_ast = lock->l_blocking_ast;
3180 new_lock->l_completion_ast = lock->l_completion_ast;
3181 new_lock->l_remote_handle = lock->l_remote_handle;
3182 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3184 unlock_res_and_lock(new_lock);
3186 lustre_hash_add(new_lock->l_export->exp_lock_hash,
3187 &new_lock->l_remote_handle,
3188 &new_lock->l_exp_hash);
3190 LDLM_LOCK_RELEASE(new_lock);
3191 lh->mlh_reg_lh.cookie = 0;
3193 RETURN(ELDLM_LOCK_REPLACED);
3196 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3197 struct ldlm_lock *new_lock,
3198 struct ldlm_lock **old_lock,
3199 struct mdt_lock_handle *lh)
3201 struct ptlrpc_request *req = mdt_info_req(info);
3202 struct obd_export *exp = req->rq_export;
3203 struct lustre_handle remote_hdl;
3204 struct ldlm_request *dlmreq;
3205 struct ldlm_lock *lock;
3207 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3210 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3211 remote_hdl = dlmreq->lock_handle[0];
3213 lock = lustre_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3215 if (lock != new_lock) {
3216 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3217 lh->mlh_reg_mode = lock->l_granted_mode;
3219 LDLM_DEBUG(lock, "Restoring lock cookie");
3220 DEBUG_REQ(D_DLMTRACE, req,
3221 "restoring lock cookie "LPX64,
3222 lh->mlh_reg_lh.cookie);
3224 *old_lock = LDLM_LOCK_GET(lock);
3225 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3229 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
3233 * If the xid matches, then we know this is a resent request, and allow
3234 * it. (It's probably an OPEN, for which we don't send a lock.
3236 if (req_xid_is_last(req))
3240 * This remote handle isn't enqueued, so we never received or processed
3241 * this request. Clear MSG_RESENT, because it can be handled like any
3242 * normal request now.
3244 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3246 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3250 static int mdt_intent_getattr(enum mdt_it_code opcode,
3251 struct mdt_thread_info *info,
3252 struct ldlm_lock **lockp,
3255 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3256 struct ldlm_lock *new_lock = NULL;
3258 struct ldlm_reply *ldlm_rep;
3259 struct ptlrpc_request *req;
3260 struct mdt_body *reqbody;
3261 struct mdt_body *repbody;
3265 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3268 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3271 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3272 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3273 repbody->eadatasize = 0;
3274 repbody->aclsize = 0;
3278 child_bits = MDS_INODELOCK_LOOKUP;
3280 case MDT_IT_GETATTR:
3281 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3284 CERROR("Unhandled till now");
3285 GOTO(out_shrink, rc = -EINVAL);
3288 rc = mdt_init_ucred(info, reqbody);
3290 GOTO(out_shrink, rc);
3292 req = info->mti_pill->rc_req;
3293 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3294 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3296 /* Get lock from request for possible resent case. */
3297 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3299 ldlm_rep->lock_policy_res2 =
3300 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3302 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3303 ldlm_rep->lock_policy_res2 = 0;
3304 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3305 ldlm_rep->lock_policy_res2) {
3306 lhc->mlh_reg_lh.cookie = 0ull;
3307 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3310 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3313 mdt_exit_ucred(info);
3315 mdt_shrink_reply(info);
3319 static int mdt_intent_reint(enum mdt_it_code opcode,
3320 struct mdt_thread_info *info,
3321 struct ldlm_lock **lockp,
3324 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3325 struct ldlm_reply *rep = NULL;
3329 static const struct req_format *intent_fmts[REINT_MAX] = {
3330 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3331 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3336 opc = mdt_reint_opcode(info, intent_fmts);
3340 if (mdt_it_flavor[opcode].it_reint != opc) {
3341 CERROR("Reint code %ld doesn't match intent: %d\n",
3343 RETURN(err_serious(-EPROTO));
3346 /* Get lock from request for possible resent case. */
3347 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3349 rc = mdt_reint_internal(info, lhc, opc);
3351 /* Check whether the reply has been packed successfully. */
3352 if (mdt_info_req(info)->rq_repmsg != NULL)
3353 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3355 RETURN(err_serious(-EFAULT));
3357 /* MDC expects this in any case */
3359 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3361 /* Cross-ref case, the lock should be returned to the client */
3362 if (rc == -EREMOTE) {
3363 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3364 rep->lock_policy_res2 = 0;
3365 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3368 rep->lock_policy_res2 = clear_serious(rc);
3370 if (rc == -ENOTCONN || rc == -ENODEV ||
3371 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3373 * If it is the disconnect error (ENODEV & ENOCONN), the error
3374 * will be returned by rq_status, and client at ptlrpc layer
3375 * will detect this, then disconnect, reconnect the import
3376 * immediately, instead of impacting the following the rpc.
3378 lhc->mlh_reg_lh.cookie = 0ull;
3382 * For other cases, the error will be returned by intent.
3383 * and client will retrieve the result from intent.
3386 * FIXME: when open lock is finished, that should be
3389 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3390 rep->lock_policy_res2 = 0;
3391 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3394 lhc->mlh_reg_lh.cookie = 0ull;
3395 RETURN(ELDLM_LOCK_ABORTED);
3400 static int mdt_intent_code(long itcode)
3408 case IT_OPEN|IT_CREAT:
3415 rc = MDT_IT_READDIR;
3418 rc = MDT_IT_GETATTR;
3430 rc = MDT_IT_GETXATTR;
3433 CERROR("Unknown intent opcode: %ld\n", itcode);
3440 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3441 struct ldlm_lock **lockp, int flags)
3443 struct req_capsule *pill;
3444 struct mdt_it_flavor *flv;
3449 opc = mdt_intent_code(itopc);
3453 pill = info->mti_pill;
3454 flv = &mdt_it_flavor[opc];
3456 if (flv->it_fmt != NULL)
3457 req_capsule_extend(pill, flv->it_fmt);
3459 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3461 struct ptlrpc_request *req = mdt_info_req(info);
3462 if (flv->it_flags & MUTABOR &&
3463 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3466 if (rc == 0 && flv->it_act != NULL) {
3467 /* execute policy */
3468 rc = flv->it_act(opc, info, lockp, flags);
3475 static int mdt_intent_policy(struct ldlm_namespace *ns,
3476 struct ldlm_lock **lockp, void *req_cookie,
3477 ldlm_mode_t mode, int flags, void *data)
3479 struct mdt_thread_info *info;
3480 struct ptlrpc_request *req = req_cookie;
3481 struct ldlm_intent *it;
3482 struct req_capsule *pill;
3487 LASSERT(req != NULL);
3489 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3491 LASSERT(info != NULL);
3492 pill = info->mti_pill;
3493 LASSERT(pill->rc_req == req);
3495 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3496 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3497 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3499 const struct ldlm_request *dlmreq;
3502 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3507 * Lock without inodebits makes no sense and will oops
3508 * later in ldlm. Let's check it now to see if we have
3509 * wrong lock from client or bits get corrupted
3510 * somewhere in mdt_intent_opc().
3512 dlmreq = info->mti_dlm_req;
3513 req_bits = dlmreq->lock_desc.l_policy_data.l_inodebits.bits;
3514 LASSERT(req_bits != 0);
3517 rc = err_serious(-EFAULT);
3519 /* No intent was provided */
3520 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3521 rc = req_capsule_server_pack(pill);
3523 rc = err_serious(rc);
3531 static void mdt_seq_adjust(const struct lu_env *env,
3532 struct mdt_device *m, int lost)
3534 struct md_site *ms = mdt_md_site(m);
3535 struct lu_seq_range out;
3538 LASSERT(ms && ms->ms_server_seq);
3540 /* get extra seq from seq_server, moving it's range up */
3541 while (lost-- > 0) {
3542 seq_server_alloc_meta(ms->ms_server_seq, NULL, &out, env);
3547 static int mdt_seq_fini(const struct lu_env *env,
3548 struct mdt_device *m)
3550 struct md_site *ms = mdt_md_site(m);
3554 if (ms->ms_server_seq) {
3555 seq_server_fini(ms->ms_server_seq, env);
3556 OBD_FREE_PTR(ms->ms_server_seq);
3557 ms->ms_server_seq = NULL;
3560 if (ms->ms_control_seq) {
3561 seq_server_fini(ms->ms_control_seq, env);
3562 OBD_FREE_PTR(ms->ms_control_seq);
3563 ms->ms_control_seq = NULL;
3566 if (ms->ms_client_seq) {
3567 seq_client_fini(ms->ms_client_seq);
3568 OBD_FREE_PTR(ms->ms_client_seq);
3569 ms->ms_client_seq = NULL;
3576 static int mdt_seq_init(const struct lu_env *env,
3578 struct mdt_device *m)
3585 ms = mdt_md_site(m);
3588 * This is sequence-controller node. Init seq-controller server on local
3591 if (ms->ms_node_id == 0) {
3592 LASSERT(ms->ms_control_seq == NULL);
3594 OBD_ALLOC_PTR(ms->ms_control_seq);
3595 if (ms->ms_control_seq == NULL)
3598 rc = seq_server_init(ms->ms_control_seq,
3599 m->mdt_bottom, uuid,
3600 LUSTRE_SEQ_CONTROLLER,
3605 GOTO(out_seq_fini, rc);
3607 OBD_ALLOC_PTR(ms->ms_client_seq);
3608 if (ms->ms_client_seq == NULL)
3609 GOTO(out_seq_fini, rc = -ENOMEM);
3611 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3612 if (prefix == NULL) {
3613 OBD_FREE_PTR(ms->ms_client_seq);
3614 GOTO(out_seq_fini, rc = -ENOMEM);
3617 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3621 * Init seq-controller client after seq-controller server is
3622 * ready. Pass ms->ms_control_seq to it for direct talking.
3624 rc = seq_client_init(ms->ms_client_seq, NULL,
3625 LUSTRE_SEQ_METADATA, prefix,
3626 ms->ms_control_seq);
3627 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3630 GOTO(out_seq_fini, rc);
3633 /* Init seq-server on local MDT */
3634 LASSERT(ms->ms_server_seq == NULL);
3636 OBD_ALLOC_PTR(ms->ms_server_seq);
3637 if (ms->ms_server_seq == NULL)
3638 GOTO(out_seq_fini, rc = -ENOMEM);
3640 rc = seq_server_init(ms->ms_server_seq,
3641 m->mdt_bottom, uuid,
3646 GOTO(out_seq_fini, rc = -ENOMEM);
3648 /* Assign seq-controller client to local seq-server. */
3649 if (ms->ms_node_id == 0) {
3650 LASSERT(ms->ms_client_seq != NULL);
3652 rc = seq_server_set_cli(ms->ms_server_seq,
3660 mdt_seq_fini(env, m);
3665 * Init client sequence manager which is used by local MDS to talk to sequence
3666 * controller on remote node.
3668 static int mdt_seq_init_cli(const struct lu_env *env,
3669 struct mdt_device *m,
3670 struct lustre_cfg *cfg)
3672 struct md_site *ms = mdt_md_site(m);
3673 struct obd_device *mdc;
3674 struct obd_uuid *uuidp, *mdcuuidp;
3675 char *uuid_str, *mdc_uuid_str;
3678 struct mdt_thread_info *info;
3679 char *p, *index_string = lustre_cfg_string(cfg, 2);
3682 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3683 uuidp = &info->mti_u.uuid[0];
3684 mdcuuidp = &info->mti_u.uuid[1];
3686 LASSERT(index_string);
3688 index = simple_strtol(index_string, &p, 10);
3690 CERROR("Invalid index in lustre_cgf, offset 2\n");
3694 /* check if this is adding the first MDC and controller is not yet
3696 if (index != 0 || ms->ms_client_seq)
3699 uuid_str = lustre_cfg_string(cfg, 1);
3700 mdc_uuid_str = lustre_cfg_string(cfg, 4);
3701 obd_str2uuid(uuidp, uuid_str);
3702 obd_str2uuid(mdcuuidp, mdc_uuid_str);
3704 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
3706 CERROR("can't find controller MDC by uuid %s\n",
3709 } else if (!mdc->obd_set_up) {
3710 CERROR("target %s not set up\n", mdc->obd_name);
3713 LASSERT(ms->ms_control_exp);
3714 OBD_ALLOC_PTR(ms->ms_client_seq);
3715 if (ms->ms_client_seq != NULL) {
3718 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3722 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3725 rc = seq_client_init(ms->ms_client_seq,
3727 LUSTRE_SEQ_METADATA,
3729 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3736 LASSERT(ms->ms_server_seq != NULL);
3737 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
3744 static void mdt_seq_fini_cli(struct mdt_device *m)
3750 ms = mdt_md_site(m);
3753 if (ms->ms_server_seq)
3754 seq_server_set_cli(ms->ms_server_seq,
3757 if (ms->ms_control_exp) {
3758 class_export_put(ms->ms_control_exp);
3759 ms->ms_control_exp = NULL;
3768 static int mdt_fld_fini(const struct lu_env *env,