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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/mdt/mdt_handler.c
40 * Lustre Metadata Target (mdt) request handler
42 * Author: Peter Braam <braam@clusterfs.com>
43 * Author: Andreas Dilger <adilger@clusterfs.com>
44 * Author: Phil Schwan <phil@clusterfs.com>
45 * Author: Mike Shaver <shaver@clusterfs.com>
46 * Author: Nikita Danilov <nikita@clusterfs.com>
47 * Author: Huang Hua <huanghua@clusterfs.com>
48 * Author: Yury Umanets <umka@clusterfs.com>
52 # define EXPORT_SYMTAB
54 #define DEBUG_SUBSYSTEM S_MDS
56 #include <linux/module.h>
58 * struct OBD_{ALLOC,FREE}*()
60 #include <obd_support.h>
61 /* struct ptlrpc_request */
62 #include <lustre_net.h>
63 /* struct obd_export */
64 #include <lustre_export.h>
65 /* struct obd_device */
68 #include <dt_object.h>
69 #include <lustre_mds.h>
70 #include <lustre_mdt.h>
71 #include "mdt_internal.h"
72 #ifdef HAVE_QUOTA_SUPPORT
73 # include <lustre_quota.h>
75 #include <lustre_acl.h>
76 #include <lustre_param.h>
77 #include <lustre_fsfilt.h>
79 mdl_mode_t mdt_mdl_lock_modes[] = {
80 [LCK_MINMODE] = MDL_MINMODE,
87 [LCK_GROUP] = MDL_GROUP
90 ldlm_mode_t mdt_dlm_lock_modes[] = {
91 [MDL_MINMODE] = LCK_MINMODE,
98 [MDL_GROUP] = LCK_GROUP
102 * Initialized in mdt_mod_init().
104 static unsigned long mdt_num_threads;
105 static unsigned long mdt_min_threads;
106 static unsigned long mdt_max_threads;
108 /* ptlrpc request handler for MDT. All handlers are
109 * grouped into several slices - struct mdt_opc_slice,
110 * and stored in an array - mdt_handlers[].
113 /* The name of this handler. */
115 /* Fail id for this handler, checked at the beginning of this handler*/
117 /* Operation code for this handler */
119 /* flags are listed in enum mdt_handler_flags below. */
121 /* The actual handler function to execute. */
122 int (*mh_act)(struct mdt_thread_info *info);
123 /* Request format for this request. */
124 const struct req_format *mh_fmt;
127 enum mdt_handler_flags {
129 * struct mdt_body is passed in the incoming message, and object
130 * identified by this fid exists on disk.
132 * "habeo corpus" == "I have a body"
134 HABEO_CORPUS = (1 << 0),
136 * struct ldlm_request is passed in the incoming message.
138 * "habeo clavis" == "I have a key"
140 HABEO_CLAVIS = (1 << 1),
142 * this request has fixed reply format, so that reply message can be
143 * packed by generic code.
145 * "habeo refero" == "I have a reply"
147 HABEO_REFERO = (1 << 2),
149 * this request will modify something, so check whether the filesystem
150 * is readonly or not, then return -EROFS to client asap if necessary.
152 * "mutabor" == "I shall modify"
157 struct mdt_opc_slice {
160 struct mdt_handler *mos_hs;
163 static struct mdt_opc_slice mdt_regular_handlers[];
164 static struct mdt_opc_slice mdt_readpage_handlers[];
165 static struct mdt_opc_slice mdt_xmds_handlers[];
166 static struct mdt_opc_slice mdt_seq_handlers[];
167 static struct mdt_opc_slice mdt_fld_handlers[];
169 static struct mdt_device *mdt_dev(struct lu_device *d);
170 static int mdt_regular_handle(struct ptlrpc_request *req);
171 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
172 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
173 struct getinfo_fid2path *fp);
175 static const struct lu_object_operations mdt_obj_ops;
177 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
181 return (rep->lock_policy_res1 & flag);
184 void mdt_clear_disposition(struct mdt_thread_info *info,
185 struct ldlm_reply *rep, int flag)
188 info->mti_opdata &= ~flag;
190 rep->lock_policy_res1 &= ~flag;
193 void mdt_set_disposition(struct mdt_thread_info *info,
194 struct ldlm_reply *rep, int flag)
197 info->mti_opdata |= flag;
199 rep->lock_policy_res1 |= flag;
202 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
204 lh->mlh_pdo_hash = 0;
205 lh->mlh_reg_mode = lm;
206 lh->mlh_type = MDT_REG_LOCK;
209 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
210 const char *name, int namelen)
212 lh->mlh_reg_mode = lm;
213 lh->mlh_type = MDT_PDO_LOCK;
215 if (name != NULL && (name[0] != '\0')) {
216 LASSERT(namelen > 0);
217 lh->mlh_pdo_hash = full_name_hash(name, namelen);
219 LASSERT(namelen == 0);
220 lh->mlh_pdo_hash = 0ull;
224 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
225 struct mdt_lock_handle *lh)
231 * Any dir access needs couple of locks:
233 * 1) on part of dir we gonna take lookup/modify;
235 * 2) on whole dir to protect it from concurrent splitting and/or to
236 * flush client's cache for readdir().
238 * so, for a given mode and object this routine decides what lock mode
239 * to use for lock #2:
241 * 1) if caller's gonna lookup in dir then we need to protect dir from
242 * being splitted only - LCK_CR
244 * 2) if caller's gonna modify dir then we need to protect dir from
245 * being splitted and to flush cache - LCK_CW
247 * 3) if caller's gonna modify dir and that dir seems ready for
248 * splitting then we need to protect it from any type of access
249 * (lookup/modify/split) - LCK_EX --bzzz
252 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
253 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
256 * Ask underlaying level its opinion about preferable PDO lock mode
257 * having access type passed as regular lock mode:
259 * - MDL_MINMODE means that lower layer does not want to specify lock
262 * - MDL_NL means that no PDO lock should be taken. This is used in some
263 * cases. Say, for non-splittable directories no need to use PDO locks
266 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
267 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
269 if (mode != MDL_MINMODE) {
270 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
273 * Lower layer does not want to specify locking mode. We do it
274 * our selves. No special protection is needed, just flush
275 * client's cache on modification and allow concurrent
278 switch (lh->mlh_reg_mode) {
280 lh->mlh_pdo_mode = LCK_EX;
283 lh->mlh_pdo_mode = LCK_CR;
286 lh->mlh_pdo_mode = LCK_CW;
289 CERROR("Not expected lock type (0x%x)\n",
290 (int)lh->mlh_reg_mode);
295 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
299 static int mdt_getstatus(struct mdt_thread_info *info)
301 struct mdt_device *mdt = info->mti_mdt;
302 struct md_device *next = mdt->mdt_child;
303 struct mdt_body *repbody;
308 rc = mdt_check_ucred(info);
310 RETURN(err_serious(rc));
312 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
313 RETURN(err_serious(-ENOMEM));
315 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
316 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
320 repbody->valid |= OBD_MD_FLID;
322 if (mdt->mdt_opts.mo_mds_capa &&
323 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
324 struct mdt_object *root;
325 struct lustre_capa *capa;
327 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
329 RETURN(PTR_ERR(root));
331 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
333 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
334 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
336 mdt_object_put(info->mti_env, root);
338 repbody->valid |= OBD_MD_FLMDSCAPA;
344 static int mdt_statfs(struct mdt_thread_info *info)
346 struct ptlrpc_request *req = mdt_info_req(info);
347 struct md_device *next = info->mti_mdt->mdt_child;
348 struct ptlrpc_service *svc;
349 struct obd_statfs *osfs;
354 svc = info->mti_pill->rc_req->rq_rqbd->rqbd_service;
356 /* This will trigger a watchdog timeout */
357 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
358 (MDT_SERVICE_WATCHDOG_FACTOR *
359 at_get(&svc->srv_at_estimate)) + 1);
361 rc = mdt_check_ucred(info);
363 RETURN(err_serious(rc));
365 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
366 rc = err_serious(-ENOMEM);
368 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
369 rc = next->md_ops->mdo_statfs(info->mti_env, next,
371 statfs_pack(osfs, &info->mti_u.ksfs);
375 mdt_counter_incr(req->rq_export, LPROC_MDT_STATFS);
381 * Pack SOM attributes into the reply.
382 * Call under a DLM UPDATE lock.
384 static void mdt_pack_size2body(struct mdt_thread_info *info,
385 struct mdt_object *mo)
388 struct md_attr *ma = &info->mti_attr;
390 LASSERT(ma->ma_attr.la_valid & LA_MODE);
391 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
393 /* Check if Size-on-MDS is supported, if this is a regular file,
394 * if SOM is enabled on the object and if SOM cache exists and valid.
395 * Otherwise do not pack Size-on-MDS attributes to the reply. */
396 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
397 !S_ISREG(ma->ma_attr.la_mode) ||
398 !mdt_object_is_som_enabled(mo) ||
399 !(ma->ma_valid & MA_SOM))
402 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
403 b->size = ma->ma_som->msd_size;
404 b->blocks = ma->ma_som->msd_blocks;
407 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
408 const struct lu_attr *attr, const struct lu_fid *fid)
410 struct md_attr *ma = &info->mti_attr;
412 LASSERT(ma->ma_valid & MA_INODE);
414 b->atime = attr->la_atime;
415 b->mtime = attr->la_mtime;
416 b->ctime = attr->la_ctime;
417 b->mode = attr->la_mode;
418 b->size = attr->la_size;
419 b->blocks = attr->la_blocks;
420 b->uid = attr->la_uid;
421 b->gid = attr->la_gid;
422 b->flags = attr->la_flags;
423 b->nlink = attr->la_nlink;
424 b->rdev = attr->la_rdev;
426 /*XXX should pack the reply body according to lu_valid*/
427 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
428 OBD_MD_FLGID | OBD_MD_FLTYPE |
429 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
430 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
432 if (!S_ISREG(attr->la_mode)) {
433 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
434 } else if (ma->ma_need & MA_LOV && ma->ma_lmm_size == 0) {
435 /* means no objects are allocated on osts. */
436 LASSERT(!(ma->ma_valid & MA_LOV));
437 /* just ignore blocks occupied by extend attributes on MDS */
439 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
440 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
445 b->valid |= OBD_MD_FLID;
447 /* FIXME: these should be fixed when new igif ready.*/
448 b->ino = fid_oid(fid); /* 1.6 compatibility */
449 b->generation = fid_ver(fid); /* 1.6 compatibility */
450 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
452 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
453 PFID(fid), b->nlink, b->mode, b->size);
457 mdt_body_reverse_idmap(info, b);
459 if (b->valid & OBD_MD_FLSIZE)
460 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
461 PFID(fid), (unsigned long long)b->size);
464 static inline int mdt_body_has_lov(const struct lu_attr *la,
465 const struct mdt_body *body)
467 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
468 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
471 void mdt_client_compatibility(struct mdt_thread_info *info)
473 struct mdt_body *body;
474 struct ptlrpc_request *req = mdt_info_req(info);
475 struct obd_export *exp = req->rq_export;
476 struct md_attr *ma = &info->mti_attr;
477 struct lu_attr *la = &ma->ma_attr;
480 if (exp->exp_connect_flags & OBD_CONNECT_LAYOUTLOCK)
481 /* the client can deal with 16-bit lmm_stripe_count */
484 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
486 if (!mdt_body_has_lov(la, body))
489 /* now we have a reply with a lov for a client not compatible with the
490 * layout lock so we have to clean the layout generation number */
491 if (S_ISREG(la->la_mode))
492 ma->ma_lmm->lmm_layout_gen = 0;
497 static int mdt_getattr_internal(struct mdt_thread_info *info,
498 struct mdt_object *o, int ma_need)
500 struct md_object *next = mdt_object_child(o);
501 const struct mdt_body *reqbody = info->mti_body;
502 struct ptlrpc_request *req = mdt_info_req(info);
503 struct md_attr *ma = &info->mti_attr;
504 struct lu_attr *la = &ma->ma_attr;
505 struct req_capsule *pill = info->mti_pill;
506 const struct lu_env *env = info->mti_env;
507 struct mdt_body *repbody;
508 struct lu_buf *buffer = &info->mti_buf;
512 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
513 RETURN(err_serious(-ENOMEM));
515 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
519 rc = mdt_object_exists(o);
521 /* This object is located on remote node.*/
522 repbody->fid1 = *mdt_object_fid(o);
523 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
527 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
528 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD, RCL_SERVER);
530 /* If it is dir object and client require MEA, then we got MEA */
531 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
532 reqbody->valid & OBD_MD_MEA) {
533 /* Assumption: MDT_MD size is enough for lmv size. */
534 ma->ma_lmv = buffer->lb_buf;
535 ma->ma_lmv_size = buffer->lb_len;
536 ma->ma_need = MA_LMV | MA_INODE;
538 ma->ma_lmm = buffer->lb_buf;
539 ma->ma_lmm_size = buffer->lb_len;
540 ma->ma_need = MA_LOV | MA_INODE;
543 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
544 reqbody->valid & OBD_MD_FLDIREA &&
545 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
546 /* get default stripe info for this dir. */
547 ma->ma_need |= MA_LOV_DEF;
549 ma->ma_need |= ma_need;
550 if (ma->ma_need & MA_SOM)
551 ma->ma_som = &info->mti_u.som.data;
553 rc = mo_attr_get(env, next, ma);
555 CERROR("getattr error for "DFID": %d\n",
556 PFID(mdt_object_fid(o)), rc);
560 if (likely(ma->ma_valid & MA_INODE))
561 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
565 if (mdt_body_has_lov(la, reqbody)) {
566 if (ma->ma_valid & MA_LOV) {
567 LASSERT(ma->ma_lmm_size);
568 mdt_dump_lmm(D_INFO, ma->ma_lmm);
569 repbody->eadatasize = ma->ma_lmm_size;
570 if (S_ISDIR(la->la_mode))
571 repbody->valid |= OBD_MD_FLDIREA;
573 repbody->valid |= OBD_MD_FLEASIZE;
575 if (ma->ma_valid & MA_LMV) {
576 LASSERT(S_ISDIR(la->la_mode));
577 repbody->eadatasize = ma->ma_lmv_size;
578 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
580 } else if (S_ISLNK(la->la_mode) &&
581 reqbody->valid & OBD_MD_LINKNAME) {
582 buffer->lb_buf = ma->ma_lmm;
583 buffer->lb_len = reqbody->eadatasize;
584 rc = mo_readlink(env, next, buffer);
585 if (unlikely(rc <= 0)) {
586 CERROR("readlink failed: %d\n", rc);
589 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
591 repbody->valid |= OBD_MD_LINKNAME;
592 repbody->eadatasize = rc;
594 ((char*)ma->ma_lmm)[rc - 1] = 0;
595 CDEBUG(D_INODE, "symlink dest %s, len = %d\n",
596 (char*)ma->ma_lmm, rc);
601 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
602 repbody->max_cookiesize = info->mti_mdt->mdt_max_cookiesize;
603 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
604 repbody->valid |= OBD_MD_FLMODEASIZE;
605 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
606 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
607 repbody->max_cookiesize);
610 if (exp_connect_rmtclient(info->mti_exp) &&
611 reqbody->valid & OBD_MD_FLRMTPERM) {
612 void *buf = req_capsule_server_get(pill, &RMF_ACL);
614 /* mdt_getattr_lock only */
615 rc = mdt_pack_remote_perm(info, o, buf);
617 repbody->valid &= ~OBD_MD_FLRMTPERM;
618 repbody->aclsize = 0;
621 repbody->valid |= OBD_MD_FLRMTPERM;
622 repbody->aclsize = sizeof(struct mdt_remote_perm);
625 #ifdef CONFIG_FS_POSIX_ACL
626 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
627 (reqbody->valid & OBD_MD_FLACL)) {
628 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
629 buffer->lb_len = req_capsule_get_size(pill,
630 &RMF_ACL, RCL_SERVER);
631 if (buffer->lb_len > 0) {
632 rc = mo_xattr_get(env, next, buffer,
633 XATTR_NAME_ACL_ACCESS);
635 if (rc == -ENODATA) {
636 repbody->aclsize = 0;
637 repbody->valid |= OBD_MD_FLACL;
639 } else if (rc == -EOPNOTSUPP) {
642 CERROR("got acl size: %d\n", rc);
645 repbody->aclsize = rc;
646 repbody->valid |= OBD_MD_FLACL;
653 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
654 info->mti_mdt->mdt_opts.mo_mds_capa &&
655 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
656 struct lustre_capa *capa;
658 capa = req_capsule_server_get(pill, &RMF_CAPA1);
660 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
661 rc = mo_capa_get(env, next, capa, 0);
664 repbody->valid |= OBD_MD_FLMDSCAPA;
669 static int mdt_renew_capa(struct mdt_thread_info *info)
671 struct mdt_object *obj = info->mti_object;
672 struct mdt_body *body;
673 struct lustre_capa *capa, *c;
677 /* if object doesn't exist, or server has disabled capability,
678 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
681 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
682 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
685 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
686 LASSERT(body != NULL);
688 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
691 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
695 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
697 body->valid |= OBD_MD_FLOSSCAPA;
701 static int mdt_getattr(struct mdt_thread_info *info)
703 struct ptlrpc_request *req = mdt_info_req(info);
704 struct mdt_object *obj = info->mti_object;
705 struct req_capsule *pill = info->mti_pill;
706 struct mdt_body *reqbody;
707 struct mdt_body *repbody;
712 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
715 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
716 rc = req_capsule_server_pack(pill);
718 RETURN(err_serious(rc));
719 rc = mdt_renew_capa(info);
720 GOTO(out_shrink, rc);
723 LASSERT(obj != NULL);
724 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
726 mode = lu_object_attr(&obj->mot_obj.mo_lu);
728 /* old clients may not report needed easize, use max value then */
729 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
730 reqbody->eadatasize == 0 ?
731 info->mti_mdt->mdt_max_mdsize :
732 reqbody->eadatasize);
734 rc = req_capsule_server_pack(pill);
735 if (unlikely(rc != 0))
736 RETURN(err_serious(rc));
738 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
739 LASSERT(repbody != NULL);
740 repbody->eadatasize = 0;
741 repbody->aclsize = 0;
743 if (reqbody->valid & OBD_MD_FLRMTPERM)
744 rc = mdt_init_ucred(info, reqbody);
746 rc = mdt_check_ucred(info);
748 GOTO(out_shrink, rc);
750 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
751 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
754 * Don't check capability at all, because rename might getattr for
755 * remote obj, and at that time no capability is available.
757 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
758 rc = mdt_getattr_internal(info, obj, 0);
759 if (reqbody->valid & OBD_MD_FLRMTPERM)
760 mdt_exit_ucred(info);
764 mdt_counter_incr(req->rq_export, LPROC_MDT_GETATTR);
766 mdt_client_compatibility(info);
767 rc2 = mdt_fix_reply(info);
773 static int mdt_is_subdir(struct mdt_thread_info *info)
775 struct mdt_object *o = info->mti_object;
776 struct req_capsule *pill = info->mti_pill;
777 const struct mdt_body *body = info->mti_body;
778 struct mdt_body *repbody;
784 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
787 * We save last checked parent fid to @repbody->fid1 for remote
790 LASSERT(fid_is_sane(&body->fid2));
791 LASSERT(mdt_object_exists(o) > 0);
792 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
793 &body->fid2, &repbody->fid1);
794 if (rc == 0 || rc == -EREMOTE)
795 repbody->valid |= OBD_MD_FLID;
800 static int mdt_raw_lookup(struct mdt_thread_info *info,
801 struct mdt_object *parent,
802 const struct lu_name *lname,
803 struct ldlm_reply *ldlm_rep)
805 struct md_object *next = mdt_object_child(info->mti_object);
806 const struct mdt_body *reqbody = info->mti_body;
807 struct lu_fid *child_fid = &info->mti_tmp_fid1;
808 struct mdt_body *repbody;
812 if (reqbody->valid != OBD_MD_FLID)
815 LASSERT(!info->mti_cross_ref);
817 /* Only got the fid of this obj by name */
819 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
822 /* XXX is raw_lookup possible as intent operation? */
825 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
828 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
830 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
833 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
834 repbody->fid1 = *child_fid;
835 repbody->valid = OBD_MD_FLID;
841 * UPDATE lock should be taken against parent, and be release before exit;
842 * child_bits lock should be taken against child, and be returned back:
843 * (1)normal request should release the child lock;
844 * (2)intent request will grant the lock to client.
846 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
847 struct mdt_lock_handle *lhc,
849 struct ldlm_reply *ldlm_rep)
851 struct ptlrpc_request *req = mdt_info_req(info);
852 struct mdt_body *reqbody = NULL;
853 struct mdt_object *parent = info->mti_object;
854 struct mdt_object *child;
855 struct md_object *next = mdt_object_child(parent);
856 struct lu_fid *child_fid = &info->mti_tmp_fid1;
857 struct lu_name *lname = NULL;
858 const char *name = NULL;
860 struct mdt_lock_handle *lhp = NULL;
861 struct ldlm_lock *lock;
862 struct ldlm_res_id *res_id;
869 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
870 LASSERT(ergo(is_resent,
871 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
873 LASSERT(parent != NULL);
874 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
876 RETURN(err_serious(-EFAULT));
878 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
880 if (!info->mti_cross_ref) {
882 * XXX: Check for "namelen == 0" is for getattr by fid
883 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
884 * that is the name must contain at least one character and
885 * the terminating '\0'
888 reqbody = req_capsule_client_get(info->mti_pill,
890 if (unlikely(reqbody == NULL))
891 RETURN(err_serious(-EFAULT));
893 if (unlikely(!fid_is_sane(&reqbody->fid2)))
894 RETURN(err_serious(-EINVAL));
897 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
899 PFID(mdt_object_fid(parent)),
900 PFID(&reqbody->fid2), ldlm_rep);
902 lname = mdt_name(info->mti_env, (char *)name, namelen);
903 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
904 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
908 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
910 rc = mdt_object_exists(parent);
911 if (unlikely(rc == 0)) {
912 LU_OBJECT_DEBUG(D_WARNING, info->mti_env,
913 &parent->mot_obj.mo_lu,
914 "Parent doesn't exist!\n");
916 } else if (!info->mti_cross_ref) {
917 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
918 PFID(mdt_object_fid(parent)));
921 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
929 if (info->mti_cross_ref) {
930 /* Only getattr on the child. Parent is on another node. */
931 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
933 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
934 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
937 /* Do not take lock for resent case. */
938 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
939 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
940 lhc->mlh_reg_lh.cookie);
941 LASSERT(fid_res_name_eq(mdt_object_fid(child),
942 &lock->l_resource->lr_name));
946 mdt_lock_handle_init(lhc);
947 mdt_lock_reg_init(lhc, LCK_PR);
950 * Object's name is on another MDS, no lookup lock is
951 * needed here but update is.
953 child_bits &= ~MDS_INODELOCK_LOOKUP;
954 child_bits |= MDS_INODELOCK_UPDATE;
956 rc = mdt_object_lock(info, child, lhc, child_bits,
960 /* Finally, we can get attr for child. */
961 mdt_set_capainfo(info, 0, mdt_object_fid(child),
963 rc = mdt_getattr_internal(info, child, 0);
964 if (unlikely(rc != 0))
965 mdt_object_unlock(info, child, lhc, 1);
971 /* step 1: lock parent only if parent is a directory */
972 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
973 lhp = &info->mti_lh[MDT_LH_PARENT];
974 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
975 rc = mdt_object_lock(info, parent, lhp,
976 MDS_INODELOCK_UPDATE,
978 if (unlikely(rc != 0))
982 /* step 2: lookup child's fid by name */
984 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
989 mdt_set_disposition(info, ldlm_rep,
991 GOTO(out_parent, rc);
993 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
995 *child_fid = reqbody->fid2;
996 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1000 *step 3: find the child object by fid & lock it.
1001 * regardless if it is local or remote.
1003 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1005 if (unlikely(IS_ERR(child)))
1006 GOTO(out_parent, rc = PTR_ERR(child));
1008 /* Do not take lock for resent case. */
1009 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1010 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1011 lhc->mlh_reg_lh.cookie);
1013 res_id = &lock->l_resource->lr_name;
1014 if (!fid_res_name_eq(mdt_object_fid(child),
1015 &lock->l_resource->lr_name)) {
1016 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1017 &lock->l_resource->lr_name),
1018 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1019 (unsigned long)res_id->name[0],
1020 (unsigned long)res_id->name[1],
1021 (unsigned long)res_id->name[2],
1022 PFID(mdt_object_fid(parent)));
1023 CWARN("Although resent, but still not get child lock"
1024 "parent:"DFID" child:"DFID"\n",
1025 PFID(mdt_object_fid(parent)),
1026 PFID(mdt_object_fid(child)));
1027 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1028 LDLM_LOCK_PUT(lock);
1031 LDLM_LOCK_PUT(lock);
1035 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1036 mdt_lock_handle_init(lhc);
1037 if (child_bits == MDS_INODELOCK_LAYOUT)
1038 mdt_lock_reg_init(lhc, LCK_CR);
1040 mdt_lock_reg_init(lhc, LCK_PR);
1042 if (mdt_object_exists(child) == 0) {
1043 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1044 &child->mot_obj.mo_lu,
1045 "Object doesn't exist!\n");
1046 GOTO(out_child, rc = -ENOENT);
1049 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1050 struct md_attr *ma = &info->mti_attr;
1053 ma->ma_need = MA_INODE;
1054 rc = mo_attr_get(info->mti_env,
1055 mdt_object_child(child), ma);
1056 if (unlikely(rc != 0))
1057 GOTO(out_child, rc);
1059 /* layout lock is used only on regular files */
1060 if ((ma->ma_valid & MA_INODE) &&
1061 (ma->ma_attr.la_valid & LA_MODE) &&
1062 !S_ISREG(ma->ma_attr.la_mode))
1063 child_bits &= ~MDS_INODELOCK_LAYOUT;
1065 /* If the file has not been changed for some time, we
1066 * return not only a LOOKUP lock, but also an UPDATE
1067 * lock and this might save us RPC on later STAT. For
1068 * directories, it also let negative dentry starts
1069 * working for this dir. */
1070 if (ma->ma_valid & MA_INODE &&
1071 ma->ma_attr.la_valid & LA_CTIME &&
1072 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1073 ma->ma_attr.la_ctime < cfs_time_current_sec())
1074 child_bits |= MDS_INODELOCK_UPDATE;
1077 rc = mdt_object_lock(info, child, lhc, child_bits,
1080 if (unlikely(rc != 0))
1081 GOTO(out_child, rc);
1084 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1085 /* Get MA_SOM attributes if update lock is given. */
1087 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1088 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1091 /* finally, we can get attr for child. */
1092 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1093 rc = mdt_getattr_internal(info, child, ma_need);
1094 if (unlikely(rc != 0)) {
1095 mdt_object_unlock(info, child, lhc, 1);
1097 /* Debugging code. */
1098 res_id = &lock->l_resource->lr_name;
1099 LDLM_DEBUG(lock, "Returning lock to client");
1100 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1101 &lock->l_resource->lr_name),
1102 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1103 (unsigned long)res_id->name[0],
1104 (unsigned long)res_id->name[1],
1105 (unsigned long)res_id->name[2],
1106 PFID(mdt_object_fid(child)));
1107 mdt_pack_size2body(info, child);
1110 LDLM_LOCK_PUT(lock);
1114 mdt_object_put(info->mti_env, child);
1117 mdt_object_unlock(info, parent, lhp, 1);
1121 /* normal handler: should release the child lock */
1122 static int mdt_getattr_name(struct mdt_thread_info *info)
1124 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1125 struct mdt_body *reqbody;
1126 struct mdt_body *repbody;
1130 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1131 LASSERT(reqbody != NULL);
1132 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1133 LASSERT(repbody != NULL);
1135 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1136 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1137 repbody->eadatasize = 0;
1138 repbody->aclsize = 0;
1140 rc = mdt_init_ucred(info, reqbody);
1142 GOTO(out_shrink, rc);
1144 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1145 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1146 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1147 lhc->mlh_reg_lh.cookie = 0;
1149 mdt_exit_ucred(info);
1152 mdt_client_compatibility(info);
1153 rc2 = mdt_fix_reply(info);
1159 static const struct lu_device_operations mdt_lu_ops;
1161 static int lu_device_is_mdt(struct lu_device *d)
1163 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1166 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1167 void *karg, void *uarg);
1169 static int mdt_set_info(struct mdt_thread_info *info)
1171 struct ptlrpc_request *req = mdt_info_req(info);
1174 int keylen, vallen, rc = 0;
1177 rc = req_capsule_server_pack(info->mti_pill);
1181 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1183 DEBUG_REQ(D_HA, req, "no set_info key");
1187 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1190 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1192 DEBUG_REQ(D_HA, req, "no set_info val");
1196 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1199 /* Swab any part of val you need to here */
1200 if (KEY_IS(KEY_READ_ONLY)) {
1202 lustre_msg_set_status(req->rq_repmsg, 0);
1204 cfs_spin_lock(&req->rq_export->exp_lock);
1206 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1208 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1209 cfs_spin_unlock(&req->rq_export->exp_lock);
1211 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1212 struct changelog_setinfo *cs =
1213 (struct changelog_setinfo *)val;
1214 if (vallen != sizeof(*cs)) {
1215 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1218 if (ptlrpc_req_need_swab(req)) {
1219 __swab64s(&cs->cs_recno);
1220 __swab32s(&cs->cs_id);
1223 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1225 lustre_msg_set_status(req->rq_repmsg, rc);
1233 static int mdt_connect(struct mdt_thread_info *info)
1236 struct ptlrpc_request *req;
1238 req = mdt_info_req(info);
1239 rc = target_handle_connect(req);
1241 LASSERT(req->rq_export != NULL);
1242 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1243 rc = mdt_init_sec_level(info);
1245 rc = mdt_init_idmap(info);
1247 obd_disconnect(class_export_get(req->rq_export));
1249 rc = err_serious(rc);
1254 static int mdt_disconnect(struct mdt_thread_info *info)
1259 rc = target_handle_disconnect(mdt_info_req(info));
1261 rc = err_serious(rc);
1265 static int mdt_sendpage(struct mdt_thread_info *info,
1266 struct lu_rdpg *rdpg, int nob)
1268 struct ptlrpc_request *req = mdt_info_req(info);
1269 struct obd_export *exp = req->rq_export;
1270 struct ptlrpc_bulk_desc *desc;
1271 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1278 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1283 for (i = 0, tmpcount = nob;
1284 i < rdpg->rp_npages && tmpcount > 0; i++, tmpcount -= tmpsize) {
1285 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1286 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1289 LASSERT(desc->bd_nob == nob);
1290 rc = target_bulk_io(exp, desc, lwi);
1291 ptlrpc_free_bulk(desc);
1295 #ifdef HAVE_SPLIT_SUPPORT
1297 * Retrieve dir entry from the page and insert it to the slave object, actually,
1298 * this should be in osd layer, but since it will not in the final product, so
1299 * just do it here and do not define more moo api anymore for this.
1301 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1304 struct mdt_object *object = info->mti_object;
1305 struct lu_fid *lf = &info->mti_tmp_fid2;
1306 struct md_attr *ma = &info->mti_attr;
1307 struct lu_dirpage *dp;
1308 struct lu_dirent *ent;
1309 int rc = 0, offset = 0;
1312 /* Make sure we have at least one entry. */
1317 * Disable trans for this name insert, since it will include many trans
1320 info->mti_no_need_trans = 1;
1322 * When write_dir_page, no need update parent's ctime,
1323 * and no permission check for name_insert.
1325 ma->ma_attr.la_ctime = 0;
1326 ma->ma_attr.la_valid = LA_MODE;
1327 ma->ma_valid = MA_INODE;
1330 dp = page_address(page);
1331 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1333 for (ent = lu_dirent_start(dp); ent != NULL;
1334 ent = lu_dirent_next(ent)) {
1335 struct lu_name *lname;
1338 if (le16_to_cpu(ent->lde_namelen) == 0)
1341 fid_le_to_cpu(lf, &ent->lde_fid);
1342 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1343 ma->ma_attr.la_mode = S_IFDIR;
1345 ma->ma_attr.la_mode = 0;
1346 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1348 GOTO(out, rc = -ENOMEM);
1350 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1351 lname = mdt_name(info->mti_env, name,
1352 le16_to_cpu(ent->lde_namelen));
1353 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1354 rc = mdo_name_insert(info->mti_env,
1355 md_object_next(&object->mot_obj),
1357 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1359 CERROR("Can't insert %*.*s, rc %d\n",
1360 le16_to_cpu(ent->lde_namelen),
1361 le16_to_cpu(ent->lde_namelen),
1366 offset += lu_dirent_size(ent);
1376 static int mdt_bulk_timeout(void *data)
1380 CERROR("mdt bulk transfer timeout \n");
1385 static int mdt_writepage(struct mdt_thread_info *info)
1387 struct ptlrpc_request *req = mdt_info_req(info);
1388 struct mdt_body *reqbody;
1389 struct l_wait_info *lwi;
1390 struct ptlrpc_bulk_desc *desc;
1396 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1397 if (reqbody == NULL)
1398 RETURN(err_serious(-EFAULT));
1400 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1402 RETURN(err_serious(-ENOMEM));
1404 /* allocate the page for the desc */
1405 page = cfs_alloc_page(CFS_ALLOC_STD);
1407 GOTO(desc_cleanup, rc = -ENOMEM);
1409 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1410 (int)reqbody->size, (int)reqbody->nlink);
1412 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1413 (int)reqbody->nlink);
1415 rc = sptlrpc_svc_prep_bulk(req, desc);
1417 GOTO(cleanup_page, rc);
1419 * Check if client was evicted while we were doing i/o before touching
1424 GOTO(cleanup_page, rc = -ENOMEM);
1426 if (desc->bd_export->exp_failed)
1429 rc = ptlrpc_start_bulk_transfer (desc);
1431 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * CFS_HZ / 4, CFS_HZ,
1432 mdt_bulk_timeout, desc);
1433 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1434 desc->bd_export->exp_failed, lwi);
1435 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1436 if (rc == -ETIMEDOUT) {
1437 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1438 ptlrpc_abort_bulk(desc);
1439 } else if (desc->bd_export->exp_failed) {
1440 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1442 ptlrpc_abort_bulk(desc);
1443 } else if (!desc->bd_success ||
1444 desc->bd_nob_transferred != desc->bd_nob) {
1445 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1447 "truncated" : "network error on",
1448 desc->bd_nob_transferred, desc->bd_nob);
1449 /* XXX should this be a different errno? */
1453 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1456 GOTO(cleanup_lwi, rc);
1457 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1462 cfs_free_page(page);
1464 ptlrpc_free_bulk(desc);
1469 static int mdt_readpage(struct mdt_thread_info *info)
1471 struct mdt_object *object = info->mti_object;
1472 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1473 struct mdt_body *reqbody;
1474 struct mdt_body *repbody;
1479 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1480 RETURN(err_serious(-ENOMEM));
1482 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1483 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1484 if (reqbody == NULL || repbody == NULL)
1485 RETURN(err_serious(-EFAULT));
1488 * prepare @rdpg before calling lower layers and transfer itself. Here
1489 * reqbody->size contains offset of where to start to read and
1490 * reqbody->nlink contains number bytes to read.
1492 rdpg->rp_hash = reqbody->size;
1493 if (rdpg->rp_hash != reqbody->size) {
1494 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1495 rdpg->rp_hash, reqbody->size);
1499 rdpg->rp_attrs = reqbody->mode;
1500 if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1501 rdpg->rp_attrs |= LUDA_64BITHASH;
1502 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1503 PTLRPC_MAX_BRW_SIZE);
1504 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1506 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1507 if (rdpg->rp_pages == NULL)
1510 for (i = 0; i < rdpg->rp_npages; ++i) {
1511 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1512 if (rdpg->rp_pages[i] == NULL)
1513 GOTO(free_rdpg, rc = -ENOMEM);
1516 /* call lower layers to fill allocated pages with directory data */
1517 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1519 GOTO(free_rdpg, rc);
1521 /* send pages to client */
1522 rc = mdt_sendpage(info, rdpg, rc);
1527 for (i = 0; i < rdpg->rp_npages; i++)
1528 if (rdpg->rp_pages[i] != NULL)
1529 cfs_free_page(rdpg->rp_pages[i]);
1530 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1532 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1538 static int mdt_reint_internal(struct mdt_thread_info *info,
1539 struct mdt_lock_handle *lhc,
1542 struct req_capsule *pill = info->mti_pill;
1543 struct md_quota *mq = md_quota(info->mti_env);
1544 struct mdt_body *repbody;
1549 rc = mdt_reint_unpack(info, op);
1551 CERROR("Can't unpack reint, rc %d\n", rc);
1552 RETURN(err_serious(rc));
1555 /* for replay (no_create) lmm is not needed, client has it already */
1556 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1557 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1558 info->mti_rr.rr_eadatalen);
1560 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1561 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
1562 info->mti_mdt->mdt_max_cookiesize);
1564 rc = req_capsule_server_pack(pill);
1566 CERROR("Can't pack response, rc %d\n", rc);
1567 RETURN(err_serious(rc));
1570 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1571 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1573 repbody->eadatasize = 0;
1574 repbody->aclsize = 0;
1577 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1579 /* for replay no cookkie / lmm need, because client have this already */
1580 if (info->mti_spec.no_create)
1581 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1582 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1584 rc = mdt_init_ucred_reint(info);
1586 GOTO(out_shrink, rc);
1588 rc = mdt_fix_attr_ucred(info, op);
1590 GOTO(out_ucred, rc = err_serious(rc));
1592 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1593 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1594 GOTO(out_ucred, rc);
1596 mq->mq_exp = info->mti_exp;
1597 rc = mdt_reint_rec(info, lhc);
1600 mdt_exit_ucred(info);
1602 mdt_client_compatibility(info);
1603 rc2 = mdt_fix_reply(info);
1609 static long mdt_reint_opcode(struct mdt_thread_info *info,
1610 const struct req_format **fmt)
1612 struct mdt_rec_reint *rec;
1615 opc = err_serious(-EFAULT);
1616 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1618 opc = rec->rr_opcode;
1619 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1620 if (opc < REINT_MAX && fmt[opc] != NULL)
1621 req_capsule_extend(info->mti_pill, fmt[opc]);
1623 CERROR("Unsupported opc: %ld\n", opc);
1624 opc = err_serious(opc);
1630 static int mdt_reint(struct mdt_thread_info *info)
1635 static const struct req_format *reint_fmts[REINT_MAX] = {
1636 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1637 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1638 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1639 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1640 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1641 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1642 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1647 opc = mdt_reint_opcode(info, reint_fmts);
1650 * No lock possible here from client to pass it to reint code
1653 rc = mdt_reint_internal(info, NULL, opc);
1658 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1662 /* this should sync the whole device */
1663 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1665 struct dt_device *dt = mdt->mdt_bottom;
1669 rc = dt->dd_ops->dt_sync(env, dt);
1673 /* this should sync this object */
1674 static int mdt_object_sync(struct mdt_thread_info *info)
1676 struct md_object *next;
1680 if (!mdt_object_exists(info->mti_object)) {
1681 CWARN("Non existing object "DFID"!\n",
1682 PFID(mdt_object_fid(info->mti_object)));
1685 next = mdt_object_child(info->mti_object);
1686 rc = mo_object_sync(info->mti_env, next);
1691 static int mdt_sync(struct mdt_thread_info *info)
1693 struct ptlrpc_request *req = mdt_info_req(info);
1694 struct req_capsule *pill = info->mti_pill;
1695 struct mdt_body *body;
1699 /* The fid may be zero, so we req_capsule_set manually */
1700 req_capsule_set(pill, &RQF_MDS_SYNC);
1702 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1704 RETURN(err_serious(-EINVAL));
1706 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1707 RETURN(err_serious(-ENOMEM));
1709 if (fid_seq(&body->fid1) == 0) {
1710 /* sync the whole device */
1711 rc = req_capsule_server_pack(pill);
1713 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1715 rc = err_serious(rc);
1717 /* sync an object */
1718 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1720 rc = mdt_object_sync(info);
1722 struct md_object *next;
1723 const struct lu_fid *fid;
1724 struct lu_attr *la = &info->mti_attr.ma_attr;
1726 next = mdt_object_child(info->mti_object);
1727 info->mti_attr.ma_need = MA_INODE;
1728 info->mti_attr.ma_valid = 0;
1729 rc = mo_attr_get(info->mti_env, next,
1732 body = req_capsule_server_get(pill,
1734 fid = mdt_object_fid(info->mti_object);
1735 mdt_pack_attr2body(info, body, la, fid);
1739 rc = err_serious(rc);
1742 mdt_counter_incr(req->rq_export, LPROC_MDT_SYNC);
1747 #ifdef HAVE_QUOTA_SUPPORT
1748 static int mdt_quotacheck_handle(struct mdt_thread_info *info)
1750 struct obd_quotactl *oqctl;
1751 struct req_capsule *pill = info->mti_pill;
1752 struct obd_export *exp = info->mti_exp;
1753 struct md_quota *mq = md_quota(info->mti_env);
1754 struct md_device *next = info->mti_mdt->mdt_child;
1758 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1762 /* remote client has no permission for quotacheck */
1763 if (unlikely(exp_connect_rmtclient(exp)))
1766 rc = req_capsule_server_pack(pill);
1771 rc = next->md_ops->mdo_quota.mqo_check(info->mti_env, next,
1776 static int mdt_quotactl_handle(struct mdt_thread_info *info)
1778 struct obd_quotactl *oqctl, *repoqc;
1779 struct req_capsule *pill = info->mti_pill;
1780 struct obd_export *exp = info->mti_exp;
1781 struct md_quota *mq = md_quota(info->mti_env);
1782 struct md_device *next = info->mti_mdt->mdt_child;
1783 const struct md_quota_operations *mqo = &next->md_ops->mdo_quota;
1787 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1792 if (exp_connect_rmtclient(exp)) {
1793 struct ptlrpc_request *req = mdt_info_req(info);
1794 struct mdt_export_data *med = mdt_req2med(req);
1795 struct lustre_idmap_table *idmap = med->med_idmap;
1797 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1798 oqctl->qc_cmd != Q_GETINFO))
1802 if (oqctl->qc_type == USRQUOTA)
1803 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1805 else if (oqctl->qc_type == GRPQUOTA)
1806 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1811 if (id == CFS_IDMAP_NOTFOUND) {
1812 CDEBUG(D_QUOTA, "no mapping for id %u\n",
1818 rc = req_capsule_server_pack(pill);
1822 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1823 LASSERT(repoqc != NULL);
1826 switch (oqctl->qc_cmd) {
1828 rc = mqo->mqo_on(info->mti_env, next, oqctl->qc_type);
1831 rc = mqo->mqo_off(info->mti_env, next, oqctl->qc_type);
1834 rc = mqo->mqo_setinfo(info->mti_env, next, oqctl->qc_type, id,
1838 rc = mqo->mqo_getinfo(info->mti_env, next, oqctl->qc_type, id,
1842 rc = mqo->mqo_setquota(info->mti_env, next, oqctl->qc_type, id,
1846 rc = mqo->mqo_getquota(info->mti_env, next, oqctl->qc_type, id,
1850 rc = mqo->mqo_getoinfo(info->mti_env, next, oqctl->qc_type, id,
1854 rc = mqo->mqo_getoquota(info->mti_env, next, oqctl->qc_type, id,
1857 case LUSTRE_Q_INVALIDATE:
1858 rc = mqo->mqo_invalidate(info->mti_env, next, oqctl->qc_type);
1860 case LUSTRE_Q_FINVALIDATE:
1861 rc = mqo->mqo_finvalidate(info->mti_env, next, oqctl->qc_type);
1864 CERROR("unsupported mdt_quotactl command: %d\n",
1876 * OBD PING and other handlers.
1878 static int mdt_obd_ping(struct mdt_thread_info *info)
1883 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
1885 rc = target_handle_ping(mdt_info_req(info));
1887 rc = err_serious(rc);
1891 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
1893 return err_serious(-EOPNOTSUPP);
1896 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
1898 return err_serious(-EOPNOTSUPP);
1906 /** clone llog ctxt from child (mdd)
1907 * This allows remote llog (replicator) access.
1908 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1909 * context was originally set up, or we can handle them directly.
1910 * I choose the latter, but that means I need any llog
1911 * contexts set up by child to be accessable by the mdt. So we clone the
1912 * context into our context list here.
1914 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
1917 struct md_device *next = mdt->mdt_child;
1918 struct llog_ctxt *ctxt;
1921 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
1924 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
1925 if (rc || ctxt == NULL) {
1926 CERROR("Can't get mdd ctxt %d\n", rc);
1930 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
1932 CERROR("Can't set mdt ctxt %d\n", rc);
1937 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
1938 struct mdt_device *mdt, int idx)
1940 struct llog_ctxt *ctxt;
1942 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
1945 /* Put once for the get we just did, and once for the clone */
1946 llog_ctxt_put(ctxt);
1947 llog_ctxt_put(ctxt);
1951 static int mdt_llog_create(struct mdt_thread_info *info)
1955 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1956 rc = llog_origin_handle_create(mdt_info_req(info));
1957 return (rc < 0 ? err_serious(rc) : rc);
1960 static int mdt_llog_destroy(struct mdt_thread_info *info)
1964 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
1965 rc = llog_origin_handle_destroy(mdt_info_req(info));
1966 return (rc < 0 ? err_serious(rc) : rc);
1969 static int mdt_llog_read_header(struct mdt_thread_info *info)
1973 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
1974 rc = llog_origin_handle_read_header(mdt_info_req(info));
1975 return (rc < 0 ? err_serious(rc) : rc);
1978 static int mdt_llog_next_block(struct mdt_thread_info *info)
1982 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
1983 rc = llog_origin_handle_next_block(mdt_info_req(info));
1984 return (rc < 0 ? err_serious(rc) : rc);
1987 static int mdt_llog_prev_block(struct mdt_thread_info *info)
1991 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
1992 rc = llog_origin_handle_prev_block(mdt_info_req(info));
1993 return (rc < 0 ? err_serious(rc) : rc);
2000 static struct ldlm_callback_suite cbs = {
2001 .lcs_completion = ldlm_server_completion_ast,
2002 .lcs_blocking = ldlm_server_blocking_ast,
2006 static int mdt_enqueue(struct mdt_thread_info *info)
2008 struct ptlrpc_request *req;
2012 * info->mti_dlm_req already contains swapped and (if necessary)
2013 * converted dlm request.
2015 LASSERT(info->mti_dlm_req != NULL);
2017 req = mdt_info_req(info);
2018 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2019 req, info->mti_dlm_req, &cbs);
2020 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2021 return rc ? err_serious(rc) : req->rq_status;
2024 static int mdt_convert(struct mdt_thread_info *info)
2027 struct ptlrpc_request *req;
2029 LASSERT(info->mti_dlm_req);
2030 req = mdt_info_req(info);
2031 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2032 return rc ? err_serious(rc) : req->rq_status;
2035 static int mdt_bl_callback(struct mdt_thread_info *info)
2037 CERROR("bl callbacks should not happen on MDS\n");
2039 return err_serious(-EOPNOTSUPP);
2042 static int mdt_cp_callback(struct mdt_thread_info *info)
2044 CERROR("cp callbacks should not happen on MDS\n");
2046 return err_serious(-EOPNOTSUPP);
2050 * sec context handlers
2052 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2056 rc = mdt_handle_idmap(info);
2059 struct ptlrpc_request *req = mdt_info_req(info);
2062 opc = lustre_msg_get_opc(req->rq_reqmsg);
2063 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2064 sptlrpc_svc_ctx_invalidate(req);
2067 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2072 static struct mdt_object *mdt_obj(struct lu_object *o)
2074 LASSERT(lu_device_is_mdt(o->lo_dev));
2075 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2078 struct mdt_object *mdt_object_find(const struct lu_env *env,
2079 struct mdt_device *d,
2080 const struct lu_fid *f)
2082 struct lu_object *o;
2083 struct mdt_object *m;
2086 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2087 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2088 if (unlikely(IS_ERR(o)))
2089 m = (struct mdt_object *)o;
2096 * Asyncronous commit for mdt device.
2098 * Pass asynchonous commit call down the MDS stack.
2100 * \param env environment
2101 * \param mdt the mdt device
2103 static void mdt_device_commit_async(const struct lu_env *env,
2104 struct mdt_device *mdt)
2106 struct dt_device *dt = mdt->mdt_bottom;
2109 rc = dt->dd_ops->dt_commit_async(env, dt);
2110 if (unlikely(rc != 0))
2111 CWARN("async commit start failed with rc = %d", rc);
2115 * Mark the lock as "synchonous".
2117 * Mark the lock to deffer transaction commit to the unlock time.
2119 * \param lock the lock to mark as "synchonous"
2121 * \see mdt_is_lock_sync
2122 * \see mdt_save_lock
2124 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2126 lock->l_ast_data = (void*)1;
2130 * Check whehter the lock "synchonous" or not.
2132 * \param lock the lock to check
2133 * \retval 1 the lock is "synchonous"
2134 * \retval 0 the lock isn't "synchronous"
2136 * \see mdt_set_lock_sync
2137 * \see mdt_save_lock
2139 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2141 return lock->l_ast_data != NULL;
2145 * Blocking AST for mdt locks.
2147 * Starts transaction commit if in case of COS lock conflict or
2148 * deffers such a commit to the mdt_save_lock.
2150 * \param lock the lock which blocks a request or cancelling lock
2151 * \param desc unused
2152 * \param data unused
2153 * \param flag indicates whether this cancelling or blocking callback
2155 * \see ldlm_blocking_ast_nocheck
2157 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2158 void *data, int flag)
2160 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2161 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2165 if (flag == LDLM_CB_CANCELING)
2167 lock_res_and_lock(lock);
2168 if (lock->l_blocking_ast != mdt_blocking_ast) {
2169 unlock_res_and_lock(lock);
2172 if (mdt_cos_is_enabled(mdt) &&
2173 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2174 lock->l_blocking_lock != NULL &&
2175 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2176 mdt_set_lock_sync(lock);
2178 rc = ldlm_blocking_ast_nocheck(lock);
2180 /* There is no lock conflict if l_blocking_lock == NULL,
2181 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2182 * when the last reference to a local lock was released */
2183 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2186 rc = lu_env_init(&env, LCT_MD_THREAD);
2187 if (unlikely(rc != 0))
2188 CWARN("lu_env initialization failed with rc = %d,"
2189 "cannot start asynchronous commit\n", rc);
2191 mdt_device_commit_async(&env, mdt);
2197 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2198 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2200 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2201 ldlm_policy_data_t *policy = &info->mti_policy;
2202 struct ldlm_res_id *res_id = &info->mti_res_id;
2206 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2207 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2208 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2209 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2211 if (mdt_object_exists(o) < 0) {
2212 if (locality == MDT_CROSS_LOCK) {
2213 /* cross-ref object fix */
2214 ibits &= ~MDS_INODELOCK_UPDATE;
2215 ibits |= MDS_INODELOCK_LOOKUP;
2217 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2218 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2220 /* No PDO lock on remote object */
2221 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2224 if (lh->mlh_type == MDT_PDO_LOCK) {
2225 /* check for exists after object is locked */
2226 if (mdt_object_exists(o) == 0) {
2227 /* Non-existent object shouldn't have PDO lock */
2230 /* Non-dir object shouldn't have PDO lock */
2231 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2236 memset(policy, 0, sizeof(*policy));
2237 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2240 * Take PDO lock on whole directory and build correct @res_id for lock
2241 * on part of directory.
2243 if (lh->mlh_pdo_hash != 0) {
2244 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2245 mdt_lock_pdo_mode(info, o, lh);
2246 if (lh->mlh_pdo_mode != LCK_NL) {
2248 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2249 * is never going to be sent to client and we do not
2250 * want it slowed down due to possible cancels.
2252 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2253 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2254 policy, res_id, LDLM_FL_ATOMIC_CB,
2255 &info->mti_exp->exp_handle.h_cookie);
2261 * Finish res_id initializing by name hash marking part of
2262 * directory which is taking modification.
2264 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2267 policy->l_inodebits.bits = ibits;
2270 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2271 * going to be sent to client. If it is - mdt_intent_policy() path will
2272 * fix it up and turn FL_LOCAL flag off.
2274 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2275 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2276 &info->mti_exp->exp_handle.h_cookie);
2278 mdt_object_unlock(info, o, lh, 1);
2279 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2280 lh->mlh_pdo_hash != 0 &&
2281 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2282 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2289 * Save a lock within request object.
2291 * Keep the lock referenced until whether client ACK or transaction
2292 * commit happens or release the lock immediately depending on input
2293 * parameters. If COS is ON, a write lock is converted to COS lock
2296 * \param info thead info object
2297 * \param h lock handle
2298 * \param mode lock mode
2299 * \param decref force immediate lock releasing
2302 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2303 ldlm_mode_t mode, int decref)
2307 if (lustre_handle_is_used(h)) {
2308 if (decref || !info->mti_has_trans ||
2309 !(mode & (LCK_PW | LCK_EX))){
2310 mdt_fid_unlock(h, mode);
2312 struct mdt_device *mdt = info->mti_mdt;
2313 struct ldlm_lock *lock = ldlm_handle2lock(h);
2314 struct ptlrpc_request *req = mdt_info_req(info);
2317 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2319 CDEBUG(D_HA, "request = %p reply state = %p"
2320 " transno = "LPD64"\n",
2321 req, req->rq_reply_state, req->rq_transno);
2322 if (mdt_cos_is_enabled(mdt)) {
2324 ldlm_lock_downgrade(lock, LCK_COS);
2327 ptlrpc_save_lock(req, h, mode, no_ack);
2328 if (mdt_is_lock_sync(lock)) {
2329 CDEBUG(D_HA, "found sync-lock,"
2330 " async commit started\n");
2331 mdt_device_commit_async(info->mti_env,
2334 LDLM_LOCK_PUT(lock);
2343 * Unlock mdt object.
2345 * Immeditely release the regular lock and the PDO lock or save the
2346 * lock in reqeuest and keep them referenced until client ACK or
2347 * transaction commit.
2349 * \param info thread info object
2350 * \param o mdt object
2351 * \param lh mdt lock handle referencing regular and PDO locks
2352 * \param decref force immediate lock releasing
2354 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2355 struct mdt_lock_handle *lh, int decref)
2359 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2360 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2365 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2366 const struct lu_fid *f,
2367 struct mdt_lock_handle *lh,
2370 struct mdt_object *o;
2372 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2376 rc = mdt_object_lock(info, o, lh, ibits,
2379 mdt_object_put(info->mti_env, o);
2386 void mdt_object_unlock_put(struct mdt_thread_info * info,
2387 struct mdt_object * o,
2388 struct mdt_lock_handle *lh,
2391 mdt_object_unlock(info, o, lh, decref);
2392 mdt_object_put(info->mti_env, o);
2395 static struct mdt_handler *mdt_handler_find(__u32 opc,
2396 struct mdt_opc_slice *supported)
2398 struct mdt_opc_slice *s;
2399 struct mdt_handler *h;
2402 for (s = supported; s->mos_hs != NULL; s++) {
2403 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2404 h = s->mos_hs + (opc - s->mos_opc_start);
2405 if (likely(h->mh_opc != 0))
2406 LASSERTF(h->mh_opc == opc,
2407 "opcode mismatch %d != %d\n",
2410 h = NULL; /* unsupported opc */
2417 static int mdt_lock_resname_compat(struct mdt_device *m,
2418 struct ldlm_request *req)
2420 /* XXX something... later. */
2424 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2426 /* XXX something... later. */
2431 * Generic code handling requests that have struct mdt_body passed in:
2433 * - extract mdt_body from request and save it in @info, if present;
2435 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2438 * - if HABEO_CORPUS flag is set for this request type check whether object
2439 * actually exists on storage (lu_object_exists()).
2442 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2444 const struct mdt_body *body;
2445 struct mdt_object *obj;
2446 const struct lu_env *env;
2447 struct req_capsule *pill;
2451 env = info->mti_env;
2452 pill = info->mti_pill;
2454 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2458 if (!(body->valid & OBD_MD_FLID))
2461 if (!fid_is_sane(&body->fid1)) {
2462 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2467 * Do not get size or any capa fields before we check that request
2468 * contains capa actually. There are some requests which do not, for
2469 * instance MDS_IS_SUBDIR.
2471 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2472 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2473 mdt_set_capainfo(info, 0, &body->fid1,
2474 req_capsule_client_get(pill, &RMF_CAPA1));
2476 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2478 if ((flags & HABEO_CORPUS) &&
2479 !mdt_object_exists(obj)) {
2480 mdt_object_put(env, obj);
2481 /* for capability renew ENOENT will be handled in
2483 if (body->valid & OBD_MD_FLOSSCAPA)
2488 info->mti_object = obj;
2497 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2499 struct req_capsule *pill = info->mti_pill;
2503 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2504 rc = mdt_body_unpack(info, flags);
2508 if (rc == 0 && (flags & HABEO_REFERO)) {
2510 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2511 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2512 info->mti_body->eadatasize);
2513 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2514 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER,
2515 info->mti_mdt->mdt_max_cookiesize);
2517 rc = req_capsule_server_pack(pill);
2522 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2524 struct md_device *next = m->mdt_child;
2526 return next->md_ops->mdo_init_capa_ctxt(env, next,
2527 m->mdt_opts.mo_mds_capa,
2528 m->mdt_capa_timeout,
2534 * Invoke handler for this request opc. Also do necessary preprocessing
2535 * (according to handler ->mh_flags), and post-processing (setting of
2536 * ->last_{xid,committed}).
2538 static int mdt_req_handle(struct mdt_thread_info *info,
2539 struct mdt_handler *h, struct ptlrpc_request *req)
2541 int rc, serious = 0;
2546 LASSERT(h->mh_act != NULL);
2547 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2548 LASSERT(current->journal_info == NULL);
2551 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2552 * to put same checks into handlers like mdt_close(), mdt_reint(),
2553 * etc., without talking to mdt authors first. Checking same thing
2554 * there again is useless and returning 0 error without packing reply
2555 * is buggy! Handlers either pack reply or return error.
2557 * We return 0 here and do not send any reply in order to emulate
2558 * network failure. Do not send any reply in case any of NET related
2559 * fail_id has occured.
2561 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2565 flags = h->mh_flags;
2566 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2568 if (h->mh_fmt != NULL) {
2569 req_capsule_set(info->mti_pill, h->mh_fmt);
2570 rc = mdt_unpack_req_pack_rep(info, flags);
2573 if (rc == 0 && flags & MUTABOR &&
2574 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2575 /* should it be rq_status? */
2578 if (rc == 0 && flags & HABEO_CLAVIS) {
2579 struct ldlm_request *dlm_req;
2581 LASSERT(h->mh_fmt != NULL);
2583 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2584 if (dlm_req != NULL) {
2585 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2587 dlm_req->lock_desc.l_policy_data.\
2588 l_inodebits.bits == 0)) {
2590 * Lock without inodebits makes no sense and
2591 * will oops later in ldlm. If client miss to
2592 * set such bits, do not trigger ASSERTION.
2594 * For liblustre flock case, it maybe zero.
2598 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2599 rc = mdt_lock_resname_compat(
2602 info->mti_dlm_req = dlm_req;
2609 /* capability setting changed via /proc, needs reinitialize ctxt */
2610 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2611 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2612 info->mti_mdt->mdt_capa_conf = 0;
2615 if (likely(rc == 0)) {
2617 * Process request, there can be two types of rc:
2618 * 1) errors with msg unpack/pack, other failures outside the
2619 * operation itself. This is counted as serious errors;
2620 * 2) errors during fs operation, should be placed in rq_status
2623 rc = h->mh_act(info);
2625 !req->rq_no_reply && req->rq_reply_state == NULL) {
2626 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2627 "pack reply and returned 0 error\n",
2631 serious = is_serious(rc);
2632 rc = clear_serious(rc);
2636 req->rq_status = rc;
2639 * ELDLM_* codes which > 0 should be in rq_status only as well as
2640 * all non-serious errors.
2642 if (rc > 0 || !serious)
2645 LASSERT(current->journal_info == NULL);
2647 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2648 info->mti_mdt->mdt_opts.mo_compat_resname) {
2649 struct ldlm_reply *dlmrep;
2651 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2653 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2656 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2657 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2658 target_committed_to_req(req);
2660 if (unlikely(req_is_replay(req) &&
2661 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2662 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2666 target_send_reply(req, rc, info->mti_fail_id);
2670 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2672 lh->mlh_type = MDT_NUL_LOCK;
2673 lh->mlh_reg_lh.cookie = 0ull;
2674 lh->mlh_reg_mode = LCK_MINMODE;
2675 lh->mlh_pdo_lh.cookie = 0ull;
2676 lh->mlh_pdo_mode = LCK_MINMODE;
2679 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2681 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2682 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2686 * Initialize fields of struct mdt_thread_info. Other fields are left in
2687 * uninitialized state, because it's too expensive to zero out whole
2688 * mdt_thread_info (> 1K) on each request arrival.
2690 static void mdt_thread_info_init(struct ptlrpc_request *req,
2691 struct mdt_thread_info *info)
2694 struct md_capainfo *ci;
2696 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2697 info->mti_pill = &req->rq_pill;
2700 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2701 mdt_lock_handle_init(&info->mti_lh[i]);
2703 /* mdt device: it can be NULL while CONNECT */
2704 if (req->rq_export) {
2705 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2706 info->mti_exp = req->rq_export;
2708 info->mti_mdt = NULL;
2709 info->mti_env = req->rq_svc_thread->t_env;
2710 ci = md_capainfo(info->mti_env);
2711 memset(ci, 0, sizeof *ci);
2712 if (req->rq_export) {
2713 if (exp_connect_rmtclient(req->rq_export))
2714 ci->mc_auth = LC_ID_CONVERT;
2715 else if (req->rq_export->exp_connect_flags &
2716 OBD_CONNECT_MDS_CAPA)
2717 ci->mc_auth = LC_ID_PLAIN;
2719 ci->mc_auth = LC_ID_NONE;
2722 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2723 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2724 info->mti_mos = NULL;
2726 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2727 info->mti_body = NULL;
2728 info->mti_object = NULL;
2729 info->mti_dlm_req = NULL;
2730 info->mti_has_trans = 0;
2731 info->mti_no_need_trans = 0;
2732 info->mti_cross_ref = 0;
2733 info->mti_opdata = 0;
2735 /* To not check for split by default. */
2736 info->mti_spec.sp_ck_split = 0;
2737 info->mti_spec.no_create = 0;
2740 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2744 req_capsule_fini(info->mti_pill);
2745 if (info->mti_object != NULL) {
2747 * freeing an object may lead to OSD level transaction, do not
2748 * let it mess with MDT. bz19385.
2750 info->mti_no_need_trans = 1;
2751 mdt_object_put(info->mti_env, info->mti_object);
2752 info->mti_object = NULL;
2754 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2755 mdt_lock_handle_fini(&info->mti_lh[i]);
2756 info->mti_env = NULL;
2759 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2760 struct obd_device *obd, int *process)
2762 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2763 case MDS_CONNECT: /* This will never get here, but for completeness. */
2764 case OST_CONNECT: /* This will never get here, but for completeness. */
2765 case MDS_DISCONNECT:
2766 case OST_DISCONNECT:
2771 case MDS_DONE_WRITING:
2772 case MDS_SYNC: /* used in unmounting */
2778 *process = target_queue_recovery_request(req, obd);
2782 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
2789 * Handle recovery. Return:
2790 * +1: continue request processing;
2791 * -ve: abort immediately with the given error code;
2792 * 0: send reply with error code in req->rq_status;
2794 static int mdt_recovery(struct mdt_thread_info *info)
2796 struct ptlrpc_request *req = mdt_info_req(info);
2797 struct obd_device *obd;
2801 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2804 case SEC_CTX_INIT_CONT:
2810 rc = mdt_handle_idmap(info);
2819 if (unlikely(!class_connected_export(req->rq_export))) {
2820 CERROR("operation %d on unconnected MDS from %s\n",
2821 lustre_msg_get_opc(req->rq_reqmsg),
2822 libcfs_id2str(req->rq_peer));
2823 /* FIXME: For CMD cleanup, when mds_B stop, the req from
2824 * mds_A will get -ENOTCONN(especially for ping req),
2825 * which will cause that mds_A deactive timeout, then when
2826 * mds_A cleanup, the cleanup process will be suspended since
2827 * deactive timeout is not zero.
2829 req->rq_status = -ENOTCONN;
2830 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
2834 /* sanity check: if the xid matches, the request must be marked as a
2835 * resent or replayed */
2836 if (req_xid_is_last(req)) {
2837 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
2838 (MSG_RESENT | MSG_REPLAY))) {
2839 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
2840 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
2841 lustre_msg_get_flags(req->rq_reqmsg));
2843 req->rq_status = -ENOTCONN;
2848 /* else: note the opposite is not always true; a RESENT req after a
2849 * failover will usually not match the last_xid, since it was likely
2850 * never committed. A REPLAYed request will almost never match the
2851 * last xid, however it could for a committed, but still retained,
2854 obd = req->rq_export->exp_obd;
2856 /* Check for aborted recovery... */
2857 if (unlikely(obd->obd_recovering)) {
2860 DEBUG_REQ(D_INFO, req, "Got new replay");
2861 rc = mdt_filter_recovery_request(req, obd, &should_process);
2862 if (rc != 0 || !should_process)
2864 else if (should_process < 0) {
2865 req->rq_status = should_process;
2866 rc = ptlrpc_error(req);
2873 static int mdt_msg_check_version(struct lustre_msg *msg)
2877 switch (lustre_msg_get_opc(msg)) {
2879 case MDS_DISCONNECT:
2882 case SEC_CTX_INIT_CONT:
2884 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
2886 CERROR("bad opc %u version %08x, expecting %08x\n",
2887 lustre_msg_get_opc(msg),
2888 lustre_msg_get_version(msg),
2889 LUSTRE_OBD_VERSION);
2893 case MDS_GETATTR_NAME:
2900 case MDS_DONE_WRITING:
2907 case MDS_QUOTACHECK:
2913 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
2915 CERROR("bad opc %u version %08x, expecting %08x\n",
2916 lustre_msg_get_opc(msg),
2917 lustre_msg_get_version(msg),
2918 LUSTRE_MDS_VERSION);
2922 case LDLM_BL_CALLBACK:
2923 case LDLM_CP_CALLBACK:
2924 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
2926 CERROR("bad opc %u version %08x, expecting %08x\n",
2927 lustre_msg_get_opc(msg),
2928 lustre_msg_get_version(msg),
2929 LUSTRE_DLM_VERSION);
2931 case OBD_LOG_CANCEL:
2932 case LLOG_ORIGIN_HANDLE_CREATE:
2933 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2934 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2935 case LLOG_ORIGIN_HANDLE_CLOSE:
2936 case LLOG_ORIGIN_HANDLE_DESTROY:
2937 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
2939 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
2941 CERROR("bad opc %u version %08x, expecting %08x\n",
2942 lustre_msg_get_opc(msg),
2943 lustre_msg_get_version(msg),
2944 LUSTRE_LOG_VERSION);
2947 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
2953 static int mdt_handle0(struct ptlrpc_request *req,
2954 struct mdt_thread_info *info,
2955 struct mdt_opc_slice *supported)
2957 struct mdt_handler *h;
2958 struct lustre_msg *msg;
2963 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
2966 LASSERT(current->journal_info == NULL);
2968 msg = req->rq_reqmsg;
2969 rc = mdt_msg_check_version(msg);
2970 if (likely(rc == 0)) {
2971 rc = mdt_recovery(info);
2972 if (likely(rc == +1)) {
2973 h = mdt_handler_find(lustre_msg_get_opc(msg),
2975 if (likely(h != NULL)) {
2976 rc = mdt_req_handle(info, h, req);
2978 CERROR("The unsupported opc: 0x%x\n",
2979 lustre_msg_get_opc(msg) );
2980 req->rq_status = -ENOTSUPP;
2981 rc = ptlrpc_error(req);
2986 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
2991 * MDT handler function called by ptlrpc service thread when request comes.
2993 * XXX common "target" functionality should be factored into separate module
2994 * shared by mdt, ost and stand-alone services like fld.
2996 static int mdt_handle_common(struct ptlrpc_request *req,
2997 struct mdt_opc_slice *supported)
3000 struct mdt_thread_info *info;
3004 env = req->rq_svc_thread->t_env;
3005 LASSERT(env != NULL);
3006 LASSERT(env->le_ses != NULL);
3007 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3008 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3009 LASSERT(info != NULL);
3011 mdt_thread_info_init(req, info);
3013 rc = mdt_handle0(req, info, supported);
3015 mdt_thread_info_fini(info);
3020 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3023 int mdt_recovery_handle(struct ptlrpc_request *req)
3028 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3030 rc = mdt_handle_common(req, mdt_fld_handlers);
3033 rc = mdt_handle_common(req, mdt_seq_handlers);
3036 rc = mdt_handle_common(req, mdt_regular_handlers);
3043 static int mdt_regular_handle(struct ptlrpc_request *req)
3045 return mdt_handle_common(req, mdt_regular_handlers);
3048 static int mdt_readpage_handle(struct ptlrpc_request *req)
3050 return mdt_handle_common(req, mdt_readpage_handlers);
3053 static int mdt_xmds_handle(struct ptlrpc_request *req)
3055 return mdt_handle_common(req, mdt_xmds_handlers);
3058 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3060 return mdt_handle_common(req, mdt_seq_handlers);
3063 static int mdt_mdss_handle(struct ptlrpc_request *req)
3065 return mdt_handle_common(req, mdt_seq_handlers);
3068 static int mdt_dtss_handle(struct ptlrpc_request *req)
3070 return mdt_handle_common(req, mdt_seq_handlers);
3073 static int mdt_fld_handle(struct ptlrpc_request *req)
3075 return mdt_handle_common(req, mdt_fld_handlers);
3092 static int mdt_intent_getattr(enum mdt_it_code opcode,
3093 struct mdt_thread_info *info,
3094 struct ldlm_lock **,
3096 static int mdt_intent_reint(enum mdt_it_code opcode,
3097 struct mdt_thread_info *info,
3098 struct ldlm_lock **,
3101 static struct mdt_it_flavor {
3102 const struct req_format *it_fmt;
3104 int (*it_act)(enum mdt_it_code ,
3105 struct mdt_thread_info *,
3106 struct ldlm_lock **,
3109 } mdt_it_flavor[] = {
3111 .it_fmt = &RQF_LDLM_INTENT,
3112 /*.it_flags = HABEO_REFERO,*/
3114 .it_act = mdt_intent_reint,
3115 .it_reint = REINT_OPEN
3118 .it_fmt = &RQF_LDLM_INTENT,
3119 .it_flags = MUTABOR,
3120 .it_act = mdt_intent_reint,
3121 .it_reint = REINT_OPEN
3124 .it_fmt = &RQF_LDLM_INTENT,
3125 .it_flags = MUTABOR,
3126 .it_act = mdt_intent_reint,
3127 .it_reint = REINT_CREATE
3129 [MDT_IT_GETATTR] = {
3130 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3131 .it_flags = HABEO_REFERO,
3132 .it_act = mdt_intent_getattr
3134 [MDT_IT_READDIR] = {
3140 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3141 .it_flags = HABEO_REFERO,
3142 .it_act = mdt_intent_getattr
3145 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3146 .it_flags = MUTABOR,
3148 .it_reint = REINT_UNLINK
3152 .it_flags = MUTABOR,
3155 [MDT_IT_GETXATTR] = {
3161 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3162 .it_flags = HABEO_REFERO,
3163 .it_act = mdt_intent_getattr
3167 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3168 struct ldlm_lock **lockp,
3169 struct ldlm_lock *new_lock,
3170 struct mdt_lock_handle *lh,
3173 struct ptlrpc_request *req = mdt_info_req(info);
3174 struct ldlm_lock *lock = *lockp;
3177 * Get new lock only for cases when possible resent did not find any
3180 if (new_lock == NULL)
3181 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3183 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3184 lh->mlh_reg_lh.cookie = 0;
3188 LASSERTF(new_lock != NULL,
3189 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3192 * If we've already given this lock to a client once, then we should
3193 * have no readers or writers. Otherwise, we should have one reader
3194 * _or_ writer ref (which will be zeroed below) before returning the
3197 if (new_lock->l_export == req->rq_export) {
3198 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3200 LASSERT(new_lock->l_export == NULL);
3201 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3206 if (new_lock->l_export == req->rq_export) {
3208 * Already gave this to the client, which means that we
3209 * reconstructed a reply.
3211 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3213 lh->mlh_reg_lh.cookie = 0;
3214 RETURN(ELDLM_LOCK_REPLACED);
3218 * Fixup the lock to be given to the client.
3220 lock_res_and_lock(new_lock);
3221 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3222 * possible blocking AST. */
3223 while (new_lock->l_readers > 0) {
3224 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3225 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3226 new_lock->l_readers--;
3228 while (new_lock->l_writers > 0) {
3229 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3230 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3231 new_lock->l_writers--;
3234 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3235 new_lock->l_blocking_ast = lock->l_blocking_ast;
3236 new_lock->l_completion_ast = lock->l_completion_ast;
3237 new_lock->l_remote_handle = lock->l_remote_handle;
3238 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3240 unlock_res_and_lock(new_lock);
3242 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3243 &new_lock->l_remote_handle,
3244 &new_lock->l_exp_hash);
3246 LDLM_LOCK_RELEASE(new_lock);
3247 lh->mlh_reg_lh.cookie = 0;
3249 RETURN(ELDLM_LOCK_REPLACED);
3252 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3253 struct ldlm_lock *new_lock,
3254 struct ldlm_lock **old_lock,
3255 struct mdt_lock_handle *lh)
3257 struct ptlrpc_request *req = mdt_info_req(info);
3258 struct obd_export *exp = req->rq_export;
3259 struct lustre_handle remote_hdl;
3260 struct ldlm_request *dlmreq;
3261 struct ldlm_lock *lock;
3263 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3266 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3267 remote_hdl = dlmreq->lock_handle[0];
3269 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3271 if (lock != new_lock) {
3272 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3273 lh->mlh_reg_mode = lock->l_granted_mode;
3275 LDLM_DEBUG(lock, "Restoring lock cookie");
3276 DEBUG_REQ(D_DLMTRACE, req,
3277 "restoring lock cookie "LPX64,
3278 lh->mlh_reg_lh.cookie);
3280 *old_lock = LDLM_LOCK_GET(lock);
3281 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3285 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3289 * If the xid matches, then we know this is a resent request, and allow
3290 * it. (It's probably an OPEN, for which we don't send a lock.
3292 if (req_xid_is_last(req))
3296 * This remote handle isn't enqueued, so we never received or processed
3297 * this request. Clear MSG_RESENT, because it can be handled like any
3298 * normal request now.
3300 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3302 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3306 static int mdt_intent_getattr(enum mdt_it_code opcode,
3307 struct mdt_thread_info *info,
3308 struct ldlm_lock **lockp,
3311 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3312 struct ldlm_lock *new_lock = NULL;
3314 struct ldlm_reply *ldlm_rep;
3315 struct ptlrpc_request *req;
3316 struct mdt_body *reqbody;
3317 struct mdt_body *repbody;
3321 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3324 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3327 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3328 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3329 repbody->eadatasize = 0;
3330 repbody->aclsize = 0;
3334 child_bits = MDS_INODELOCK_LOOKUP;
3336 case MDT_IT_GETATTR:
3337 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3339 case MDT_IT_LAYOUT: {
3340 static int printed = 0;
3343 CERROR("layout lock not supported by this version\n");
3346 GOTO(out_shrink, rc = -EINVAL);
3350 CERROR("Unsupported intent (%d)\n", opcode);
3351 GOTO(out_shrink, rc = -EINVAL);
3354 rc = mdt_init_ucred(info, reqbody);
3356 GOTO(out_shrink, rc);
3358 req = info->mti_pill->rc_req;
3359 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3360 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3362 /* Get lock from request for possible resent case. */
3363 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3365 ldlm_rep->lock_policy_res2 =
3366 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3368 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3369 ldlm_rep->lock_policy_res2 = 0;
3370 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3371 ldlm_rep->lock_policy_res2) {
3372 lhc->mlh_reg_lh.cookie = 0ull;
3373 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3376 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3379 mdt_exit_ucred(info);
3381 mdt_client_compatibility(info);
3382 rc2 = mdt_fix_reply(info);
3388 static int mdt_intent_reint(enum mdt_it_code opcode,
3389 struct mdt_thread_info *info,
3390 struct ldlm_lock **lockp,
3393 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3394 struct ldlm_reply *rep = NULL;
3398 static const struct req_format *intent_fmts[REINT_MAX] = {
3399 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3400 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3405 opc = mdt_reint_opcode(info, intent_fmts);
3409 if (mdt_it_flavor[opcode].it_reint != opc) {
3410 CERROR("Reint code %ld doesn't match intent: %d\n",
3412 RETURN(err_serious(-EPROTO));
3415 /* Get lock from request for possible resent case. */
3416 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3418 rc = mdt_reint_internal(info, lhc, opc);
3420 /* Check whether the reply has been packed successfully. */
3421 if (mdt_info_req(info)->rq_repmsg != NULL)
3422 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3424 RETURN(err_serious(-EFAULT));
3426 /* MDC expects this in any case */
3428 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3430 /* Cross-ref case, the lock should be returned to the client */
3431 if (rc == -EREMOTE) {
3432 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3433 rep->lock_policy_res2 = 0;
3434 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3437 rep->lock_policy_res2 = clear_serious(rc);
3439 if (rep->lock_policy_res2 == -ENOENT &&
3440 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3441 rep->lock_policy_res2 = 0;
3443 if (rc == -ENOTCONN || rc == -ENODEV ||
3444 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3446 * If it is the disconnect error (ENODEV & ENOCONN), the error
3447 * will be returned by rq_status, and client at ptlrpc layer
3448 * will detect this, then disconnect, reconnect the import
3449 * immediately, instead of impacting the following the rpc.
3451 lhc->mlh_reg_lh.cookie = 0ull;
3455 * For other cases, the error will be returned by intent.
3456 * and client will retrieve the result from intent.
3459 * FIXME: when open lock is finished, that should be
3462 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3463 LASSERTF(rc == 0, "Error occurred but lock handle "
3464 "is still in use\n");
3465 rep->lock_policy_res2 = 0;
3466 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3469 lhc->mlh_reg_lh.cookie = 0ull;
3470 RETURN(ELDLM_LOCK_ABORTED);
3475 static int mdt_intent_code(long itcode)
3483 case IT_OPEN|IT_CREAT:
3490 rc = MDT_IT_READDIR;
3493 rc = MDT_IT_GETATTR;
3505 rc = MDT_IT_GETXATTR;
3511 CERROR("Unknown intent opcode: %ld\n", itcode);
3518 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3519 struct ldlm_lock **lockp, int flags)
3521 struct req_capsule *pill;
3522 struct mdt_it_flavor *flv;
3527 opc = mdt_intent_code(itopc);
3531 pill = info->mti_pill;
3532 flv = &mdt_it_flavor[opc];
3534 if (flv->it_fmt != NULL)
3535 req_capsule_extend(pill, flv->it_fmt);
3537 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3539 struct ptlrpc_request *req = mdt_info_req(info);
3540 if (flv->it_flags & MUTABOR &&
3541 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3544 if (rc == 0 && flv->it_act != NULL) {
3545 /* execute policy */
3546 rc = flv->it_act(opc, info, lockp, flags);
3553 static int mdt_intent_policy(struct ldlm_namespace *ns,
3554 struct ldlm_lock **lockp, void *req_cookie,
3555 ldlm_mode_t mode, int flags, void *data)
3557 struct mdt_thread_info *info;
3558 struct ptlrpc_request *req = req_cookie;
3559 struct ldlm_intent *it;
3560 struct req_capsule *pill;
3565 LASSERT(req != NULL);
3567 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3569 LASSERT(info != NULL);
3570 pill = info->mti_pill;
3571 LASSERT(pill->rc_req == req);
3573 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3574 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3575 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3577 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3581 /* Lock without inodebits makes no sense and will oops
3582 * later in ldlm. Let's check it now to see if we have
3583 * ibits corrupted somewhere in mdt_intent_opc().
3584 * The case for client miss to set ibits has been
3585 * processed by others. */
3586 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3587 lr_type == LDLM_IBITS,
3588 info->mti_dlm_req->lock_desc.\
3589 l_policy_data.l_inodebits.bits != 0));
3591 rc = err_serious(-EFAULT);
3593 /* No intent was provided */
3594 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3595 rc = req_capsule_server_pack(pill);
3597 rc = err_serious(rc);
3602 static int mdt_seq_fini(const struct lu_env *env,
3603 struct mdt_device *m)
3605 struct md_site *ms = mdt_md_site(m);
3609 if (ms->ms_server_seq) {
3610 seq_server_fini(ms->ms_server_seq, env);
3611 OBD_FREE_PTR(ms->ms_server_seq);
3612 ms->ms_server_seq = NULL;
3615 if (ms->ms_control_seq) {
3616 seq_server_fini(ms->ms_control_seq, env);
3617 OBD_FREE_PTR(ms->ms_control_seq);
3618 ms->ms_control_seq = NULL;
3621 if (ms->ms_client_seq) {
3622 seq_client_fini(ms->ms_client_seq);
3623 OBD_FREE_PTR(ms->ms_client_seq);
3624 ms->ms_client_seq = NULL;
3631 static int mdt_seq_init(const struct lu_env *env,
3633 struct mdt_device *m)
3640 ms = mdt_md_site(m);
3643 * This is sequence-controller node. Init seq-controller server on local
3646 if (ms->ms_node_id == 0) {
3647 LASSERT(ms->ms_control_seq == NULL);
3649 OBD_ALLOC_PTR(ms->ms_control_seq);
3650 if (ms->ms_control_seq == NULL)
3653 rc = seq_server_init(ms->ms_control_seq,
3654 m->mdt_bottom, uuid,
3655 LUSTRE_SEQ_CONTROLLER,
3660 GOTO(out_seq_fini, rc);
3662 OBD_ALLOC_PTR(ms->ms_client_seq);
3663 if (ms->ms_client_seq == NULL)
3664 GOTO(out_seq_fini, rc = -ENOMEM);
3666 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3667 if (prefix == NULL) {
3668 OBD_FREE_PTR(ms->ms_client_seq);
3669 GOTO(out_seq_fini, rc = -ENOMEM);
3672 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3676 * Init seq-controller client after seq-controller server is
3677 * ready. Pass ms->ms_control_seq to it for direct talking.
3679 rc = seq_client_init(ms->ms_client_seq, NULL,
3680 LUSTRE_SEQ_METADATA, prefix,
3681 ms->ms_control_seq);
3682 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3685 GOTO(out_seq_fini, rc);
3688 /* Init seq-server on local MDT */
3689 LASSERT(ms->ms_server_seq == NULL);
3691 OBD_ALLOC_PTR(ms->ms_server_seq);
3692 if (ms->ms_server_seq == NULL)
3693 GOTO(out_seq_fini, rc = -ENOMEM);
3695 rc = seq_server_init(ms->ms_server_seq,
3696 m->mdt_bottom, uuid,
3701 GOTO(out_seq_fini, rc = -ENOMEM);
3703 /* Assign seq-controller client to local seq-server. */
3704 if (ms->ms_node_id == 0) {
3705 LASSERT(ms->ms_client_seq != NULL);
3707 rc = seq_server_set_cli(ms->ms_server_seq,
3715 mdt_seq_fini(env, m);
3720 * Init client sequence manager which is used by local MDS to talk to sequence
3721 * controller on remote node.
3723 static int mdt_seq_init_cli(const struct lu_env *env,
3724 struct mdt_device *m,
3725 struct lustre_cfg *cfg)
3727 struct md_site *ms = mdt_md_site(m);
3728 struct obd_device *mdc;
3729 struct obd_uuid *uuidp, *mdcuuidp;
3730 char *uuid_str, *mdc_uuid_str;
3733 struct mdt_thread_info *info;
3734 char *p, *index_string = lustre_cfg_string(cfg, 2);
3737 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3738 uuidp = &info->mti_u.uuid[0];
3739 mdcuuidp = &info->mti_u.uuid[1];
3741 LASSERT(index_string);
3743 index = simple_strtol(index_string, &p, 10);
3745 CERROR("Invalid index in lustre_cgf, offset 2\n");