4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
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>
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 * struct OBD_{ALLOC,FREE}*()
55 #include <obd_support.h>
56 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_mdt.h>
66 #include <lustre_log.h>
67 #include "mdt_internal.h"
68 #include <lustre_acl.h>
69 #include <lustre_param.h>
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73 [LCK_MINMODE] = MDL_MINMODE,
80 [LCK_GROUP] = MDL_GROUP
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84 [MDL_MINMODE] = LCK_MINMODE,
91 [MDL_GROUP] = LCK_GROUP
95 * Initialized in mdt_mod_init().
97 static unsigned long mdt_num_threads;
98 CFS_MODULE_PARM(mdt_num_threads, "ul", ulong, 0444,
99 "number of MDS service threads to start "
100 "(deprecated in favor of mds_num_threads)");
102 static unsigned long mds_num_threads;
103 CFS_MODULE_PARM(mds_num_threads, "ul", ulong, 0444,
104 "number of MDS service threads to start");
106 static char *mds_num_cpts;
107 CFS_MODULE_PARM(mds_num_cpts, "c", charp, 0444,
108 "CPU partitions MDS threads should run on");
110 static unsigned long mds_rdpg_num_threads;
111 CFS_MODULE_PARM(mds_rdpg_num_threads, "ul", ulong, 0444,
112 "number of MDS readpage service threads to start");
114 static char *mds_rdpg_num_cpts;
115 CFS_MODULE_PARM(mds_rdpg_num_cpts, "c", charp, 0444,
116 "CPU partitions MDS readpage threads should run on");
118 /* NB: these two should be removed along with setattr service in the future */
119 static unsigned long mds_attr_num_threads;
120 CFS_MODULE_PARM(mds_attr_num_threads, "ul", ulong, 0444,
121 "number of MDS setattr service threads to start");
123 static char *mds_attr_num_cpts;
124 CFS_MODULE_PARM(mds_attr_num_cpts, "c", charp, 0444,
125 "CPU partitions MDS setattr threads should run on");
127 /* ptlrpc request handler for MDT. All handlers are
128 * grouped into several slices - struct mdt_opc_slice,
129 * and stored in an array - mdt_handlers[].
132 /* The name of this handler. */
134 /* Fail id for this handler, checked at the beginning of this handler*/
136 /* Operation code for this handler */
138 /* flags are listed in enum mdt_handler_flags below. */
140 /* The actual handler function to execute. */
141 int (*mh_act)(struct mdt_thread_info *info);
142 /* Request format for this request. */
143 const struct req_format *mh_fmt;
146 enum mdt_handler_flags {
148 * struct mdt_body is passed in the incoming message, and object
149 * identified by this fid exists on disk.
151 * "habeo corpus" == "I have a body"
153 HABEO_CORPUS = (1 << 0),
155 * struct ldlm_request is passed in the incoming message.
157 * "habeo clavis" == "I have a key"
159 HABEO_CLAVIS = (1 << 1),
161 * this request has fixed reply format, so that reply message can be
162 * packed by generic code.
164 * "habeo refero" == "I have a reply"
166 HABEO_REFERO = (1 << 2),
168 * this request will modify something, so check whether the filesystem
169 * is readonly or not, then return -EROFS to client asap if necessary.
171 * "mutabor" == "I shall modify"
176 struct mdt_opc_slice {
179 struct mdt_handler *mos_hs;
182 static struct mdt_opc_slice mdt_regular_handlers[];
183 static struct mdt_opc_slice mdt_readpage_handlers[];
184 static struct mdt_opc_slice mdt_xmds_handlers[];
185 static struct mdt_opc_slice mdt_seq_handlers[];
186 static struct mdt_opc_slice mdt_fld_handlers[];
188 static struct mdt_device *mdt_dev(struct lu_device *d);
189 static int mdt_regular_handle(struct ptlrpc_request *req);
190 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
191 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
192 struct getinfo_fid2path *fp);
194 static const struct lu_object_operations mdt_obj_ops;
196 /* Slab for MDT object allocation */
197 static cfs_mem_cache_t *mdt_object_kmem;
199 static struct lu_kmem_descr mdt_caches[] = {
201 .ckd_cache = &mdt_object_kmem,
202 .ckd_name = "mdt_obj",
203 .ckd_size = sizeof(struct mdt_object)
210 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
214 return (rep->lock_policy_res1 & flag);
217 void mdt_clear_disposition(struct mdt_thread_info *info,
218 struct ldlm_reply *rep, int flag)
221 info->mti_opdata &= ~flag;
223 rep->lock_policy_res1 &= ~flag;
226 void mdt_set_disposition(struct mdt_thread_info *info,
227 struct ldlm_reply *rep, int flag)
230 info->mti_opdata |= flag;
232 rep->lock_policy_res1 |= flag;
235 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
237 lh->mlh_pdo_hash = 0;
238 lh->mlh_reg_mode = lm;
239 lh->mlh_type = MDT_REG_LOCK;
242 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
243 const char *name, int namelen)
245 lh->mlh_reg_mode = lm;
246 lh->mlh_type = MDT_PDO_LOCK;
248 if (name != NULL && (name[0] != '\0')) {
249 LASSERT(namelen > 0);
250 lh->mlh_pdo_hash = full_name_hash(name, namelen);
252 LASSERT(namelen == 0);
253 lh->mlh_pdo_hash = 0ull;
257 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
258 struct mdt_lock_handle *lh)
264 * Any dir access needs couple of locks:
266 * 1) on part of dir we gonna take lookup/modify;
268 * 2) on whole dir to protect it from concurrent splitting and/or to
269 * flush client's cache for readdir().
271 * so, for a given mode and object this routine decides what lock mode
272 * to use for lock #2:
274 * 1) if caller's gonna lookup in dir then we need to protect dir from
275 * being splitted only - LCK_CR
277 * 2) if caller's gonna modify dir then we need to protect dir from
278 * being splitted and to flush cache - LCK_CW
280 * 3) if caller's gonna modify dir and that dir seems ready for
281 * splitting then we need to protect it from any type of access
282 * (lookup/modify/split) - LCK_EX --bzzz
285 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
286 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
289 * Ask underlaying level its opinion about preferable PDO lock mode
290 * having access type passed as regular lock mode:
292 * - MDL_MINMODE means that lower layer does not want to specify lock
295 * - MDL_NL means that no PDO lock should be taken. This is used in some
296 * cases. Say, for non-splittable directories no need to use PDO locks
299 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
300 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
302 if (mode != MDL_MINMODE) {
303 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
306 * Lower layer does not want to specify locking mode. We do it
307 * our selves. No special protection is needed, just flush
308 * client's cache on modification and allow concurrent
311 switch (lh->mlh_reg_mode) {
313 lh->mlh_pdo_mode = LCK_EX;
316 lh->mlh_pdo_mode = LCK_CR;
319 lh->mlh_pdo_mode = LCK_CW;
322 CERROR("Not expected lock type (0x%x)\n",
323 (int)lh->mlh_reg_mode);
328 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
332 static int mdt_getstatus(struct mdt_thread_info *info)
334 struct mdt_device *mdt = info->mti_mdt;
335 struct md_device *next = mdt->mdt_child;
336 struct mdt_body *repbody;
341 rc = mdt_check_ucred(info);
343 RETURN(err_serious(rc));
345 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
346 RETURN(err_serious(-ENOMEM));
348 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
349 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
353 repbody->valid |= OBD_MD_FLID;
355 if (mdt->mdt_opts.mo_mds_capa &&
356 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
357 struct mdt_object *root;
358 struct lustre_capa *capa;
360 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
362 RETURN(PTR_ERR(root));
364 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
366 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
367 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
369 mdt_object_put(info->mti_env, root);
371 repbody->valid |= OBD_MD_FLMDSCAPA;
377 static int mdt_statfs(struct mdt_thread_info *info)
379 struct ptlrpc_request *req = mdt_info_req(info);
380 struct md_device *next = info->mti_mdt->mdt_child;
381 struct ptlrpc_service_part *svcpt;
382 struct obd_statfs *osfs;
387 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
389 /* This will trigger a watchdog timeout */
390 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
391 (MDT_SERVICE_WATCHDOG_FACTOR *
392 at_get(&svcpt->scp_at_estimate)) + 1);
394 rc = mdt_check_ucred(info);
396 RETURN(err_serious(rc));
398 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
399 RETURN(err_serious(-ENOMEM));
401 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
405 /** statfs information are cached in the mdt_device */
406 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
407 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
408 /** statfs data is too old, get up-to-date one */
409 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
412 cfs_spin_lock(&info->mti_mdt->mdt_osfs_lock);
413 info->mti_mdt->mdt_osfs = *osfs;
414 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
415 cfs_spin_unlock(&info->mti_mdt->mdt_osfs_lock);
417 /** use cached statfs data */
418 cfs_spin_lock(&info->mti_mdt->mdt_osfs_lock);
419 *osfs = info->mti_mdt->mdt_osfs;
420 cfs_spin_unlock(&info->mti_mdt->mdt_osfs_lock);
424 mdt_counter_incr(req, LPROC_MDT_STATFS);
430 * Pack SOM attributes into the reply.
431 * Call under a DLM UPDATE lock.
433 static void mdt_pack_size2body(struct mdt_thread_info *info,
434 struct mdt_object *mo)
437 struct md_attr *ma = &info->mti_attr;
439 LASSERT(ma->ma_attr.la_valid & LA_MODE);
440 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
442 /* Check if Size-on-MDS is supported, if this is a regular file,
443 * if SOM is enabled on the object and if SOM cache exists and valid.
444 * Otherwise do not pack Size-on-MDS attributes to the reply. */
445 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
446 !S_ISREG(ma->ma_attr.la_mode) ||
447 !mdt_object_is_som_enabled(mo) ||
448 !(ma->ma_valid & MA_SOM))
451 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
452 b->size = ma->ma_som->msd_size;
453 b->blocks = ma->ma_som->msd_blocks;
456 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
457 const struct lu_attr *attr, const struct lu_fid *fid)
459 struct md_attr *ma = &info->mti_attr;
461 LASSERT(ma->ma_valid & MA_INODE);
463 b->atime = attr->la_atime;
464 b->mtime = attr->la_mtime;
465 b->ctime = attr->la_ctime;
466 b->mode = attr->la_mode;
467 b->size = attr->la_size;
468 b->blocks = attr->la_blocks;
469 b->uid = attr->la_uid;
470 b->gid = attr->la_gid;
471 b->flags = attr->la_flags;
472 b->nlink = attr->la_nlink;
473 b->rdev = attr->la_rdev;
475 /*XXX should pack the reply body according to lu_valid*/
476 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
477 OBD_MD_FLGID | OBD_MD_FLTYPE |
478 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
479 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
481 if (!S_ISREG(attr->la_mode)) {
482 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
483 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
484 /* means no objects are allocated on osts. */
485 LASSERT(!(ma->ma_valid & MA_LOV));
486 /* just ignore blocks occupied by extend attributes on MDS */
488 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
489 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
494 b->valid |= OBD_MD_FLID;
496 /* FIXME: these should be fixed when new igif ready.*/
497 b->ino = fid_oid(fid); /* 1.6 compatibility */
498 b->generation = fid_ver(fid); /* 1.6 compatibility */
499 b->valid |= OBD_MD_FLGENER; /* 1.6 compatibility */
501 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
502 PFID(fid), b->nlink, b->mode, b->size);
506 mdt_body_reverse_idmap(info, b);
508 if (b->valid & OBD_MD_FLSIZE)
509 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
510 PFID(fid), (unsigned long long)b->size);
513 static inline int mdt_body_has_lov(const struct lu_attr *la,
514 const struct mdt_body *body)
516 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
517 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
520 void mdt_client_compatibility(struct mdt_thread_info *info)
522 struct mdt_body *body;
523 struct ptlrpc_request *req = mdt_info_req(info);
524 struct obd_export *exp = req->rq_export;
525 struct md_attr *ma = &info->mti_attr;
526 struct lu_attr *la = &ma->ma_attr;
529 if (exp->exp_connect_flags & OBD_CONNECT_LAYOUTLOCK)
530 /* the client can deal with 16-bit lmm_stripe_count */
533 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
535 if (!mdt_body_has_lov(la, body))
538 /* now we have a reply with a lov for a client not compatible with the
539 * layout lock so we have to clean the layout generation number */
540 if (S_ISREG(la->la_mode))
541 ma->ma_lmm->lmm_layout_gen = 0;
545 static int mdt_big_lmm_get(const struct lu_env *env, struct mdt_object *o,
548 struct mdt_thread_info *info;
552 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
553 LASSERT(info != NULL);
554 LASSERT(ma->ma_lmm_size > 0);
555 LASSERT(info->mti_big_lmm_used == 0);
556 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
561 /* big_lmm may need to be grown */
562 if (info->mti_big_lmmsize < rc) {
563 int size = size_roundup_power2(rc);
565 if (info->mti_big_lmmsize > 0) {
566 /* free old buffer */
567 LASSERT(info->mti_big_lmm);
568 OBD_FREE_LARGE(info->mti_big_lmm,
569 info->mti_big_lmmsize);
570 info->mti_big_lmm = NULL;
571 info->mti_big_lmmsize = 0;
574 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
575 if (info->mti_big_lmm == NULL)
577 info->mti_big_lmmsize = size;
579 LASSERT(info->mti_big_lmmsize >= rc);
581 info->mti_buf.lb_buf = info->mti_big_lmm;
582 info->mti_buf.lb_len = info->mti_big_lmmsize;
583 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf,
588 info->mti_big_lmm_used = 1;
589 ma->ma_valid |= MA_LOV;
590 ma->ma_lmm = info->mti_big_lmm;
591 ma->ma_lmm_size = rc;
593 /* update mdt_max_mdsize so all clients will be aware about that */
594 if (info->mti_mdt->mdt_max_mdsize < rc)
595 info->mti_mdt->mdt_max_mdsize = rc;
600 int mdt_attr_get_lov(struct mdt_thread_info *info,
601 struct mdt_object *o, struct md_attr *ma)
603 struct md_object *next = mdt_object_child(o);
604 struct lu_buf *buf = &info->mti_buf;
607 buf->lb_buf = ma->ma_lmm;
608 buf->lb_len = ma->ma_lmm_size;
609 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
611 ma->ma_lmm_size = rc;
612 ma->ma_valid |= MA_LOV;
614 } else if (rc == -ENODATA) {
617 } else if (rc == -ERANGE) {
618 rc = mdt_big_lmm_get(info->mti_env, o, ma);
624 int mdt_attr_get_complex(struct mdt_thread_info *info,
625 struct mdt_object *o, struct md_attr *ma)
627 const struct lu_env *env = info->mti_env;
628 struct md_object *next = mdt_object_child(o);
629 struct lu_buf *buf = &info->mti_buf;
630 u32 mode = lu_object_attr(&next->mo_lu);
631 int need = ma->ma_need;
635 /* do we really need PFID */
636 LASSERT((ma->ma_need & MA_PFID) == 0);
640 if (need & MA_INODE) {
641 ma->ma_need = MA_INODE;
642 rc = mo_attr_get(env, next, ma);
645 ma->ma_valid |= MA_INODE;
648 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
649 rc = mdt_attr_get_lov(info, o, ma);
654 if (need & MA_LMV && S_ISDIR(mode)) {
655 buf->lb_buf = ma->ma_lmv;
656 buf->lb_len = ma->ma_lmv_size;
657 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
659 ma->ma_lmv_size = rc2;
660 ma->ma_valid |= MA_LMV;
661 } else if (rc2 == -ENODATA) {
669 if (rc == 0 && S_ISREG(mode) && (need & (MA_HSM | MA_SOM))) {
670 struct lustre_mdt_attrs *lma;
672 lma = (struct lustre_mdt_attrs *)info->mti_xattr_buf;
673 CLASSERT(sizeof(*lma) <= sizeof(info->mti_xattr_buf));
676 buf->lb_len = sizeof(info->mti_xattr_buf);
677 rc = mo_xattr_get(env, next, buf, XATTR_NAME_LMA);
679 lustre_lma_swab(lma);
680 /* Swab and copy LMA */
682 if (lma->lma_compat & LMAC_HSM)
683 ma->ma_hsm.mh_flags =
684 lma->lma_flags & HSM_FLAGS_MASK;
686 ma->ma_hsm.mh_flags = 0;
687 ma->ma_valid |= MA_HSM;
690 if (need & MA_SOM && lma->lma_compat & LMAC_SOM) {
691 LASSERT(ma->ma_som != NULL);
692 ma->ma_som->msd_ioepoch = lma->lma_ioepoch;
693 ma->ma_som->msd_size = lma->lma_som_size;
694 ma->ma_som->msd_blocks = lma->lma_som_blocks;
695 ma->ma_som->msd_mountid = lma->lma_som_mountid;
696 ma->ma_valid |= MA_SOM;
699 } else if (rc == -ENODATA) {
704 #ifdef CONFIG_FS_POSIX_ACL
705 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
706 buf->lb_buf = ma->ma_acl;
707 buf->lb_len = ma->ma_acl_size;
708 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
710 ma->ma_acl_size = rc2;
711 ma->ma_valid |= MA_ACL_DEF;
712 } else if (rc2 == -ENODATA) {
721 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
722 rc, ma->ma_valid, ma->ma_lmm);
726 static int mdt_getattr_internal(struct mdt_thread_info *info,
727 struct mdt_object *o, int ma_need)
729 struct md_object *next = mdt_object_child(o);
730 const struct mdt_body *reqbody = info->mti_body;
731 struct ptlrpc_request *req = mdt_info_req(info);
732 struct md_attr *ma = &info->mti_attr;
733 struct lu_attr *la = &ma->ma_attr;
734 struct req_capsule *pill = info->mti_pill;
735 const struct lu_env *env = info->mti_env;
736 struct mdt_body *repbody;
737 struct lu_buf *buffer = &info->mti_buf;
742 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
743 RETURN(err_serious(-ENOMEM));
745 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
749 rc = mdt_object_exists(o);
751 /* This object is located on remote node.*/
752 repbody->fid1 = *mdt_object_fid(o);
753 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
757 buffer->lb_len = reqbody->eadatasize;
758 if (buffer->lb_len > 0)
759 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
761 buffer->lb_buf = NULL;
763 /* If it is dir object and client require MEA, then we got MEA */
764 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
765 reqbody->valid & OBD_MD_MEA) {
766 /* Assumption: MDT_MD size is enough for lmv size. */
767 ma->ma_lmv = buffer->lb_buf;
768 ma->ma_lmv_size = buffer->lb_len;
769 ma->ma_need = MA_LMV | MA_INODE;
771 ma->ma_lmm = buffer->lb_buf;
772 ma->ma_lmm_size = buffer->lb_len;
773 ma->ma_need = MA_LOV | MA_INODE;
776 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
777 reqbody->valid & OBD_MD_FLDIREA &&
778 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
779 /* get default stripe info for this dir. */
780 ma->ma_need |= MA_LOV_DEF;
782 ma->ma_need |= ma_need;
783 if (ma->ma_need & MA_SOM)
784 ma->ma_som = &info->mti_u.som.data;
786 rc = mdt_attr_get_complex(info, o, ma);
788 CERROR("getattr error for "DFID": %d\n",
789 PFID(mdt_object_fid(o)), rc);
793 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
795 /* the Lustre protocol supposes to return default striping
796 * on the user-visible root if explicitly requested */
797 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
798 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
799 struct lu_fid rootfid;
800 struct mdt_object *root;
801 struct mdt_device *mdt = info->mti_mdt;
803 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
806 root = mdt_object_find(env, mdt, &rootfid);
808 RETURN(PTR_ERR(root));
809 rc = mdt_attr_get_lov(info, root, ma);
810 mdt_object_put(info->mti_env, root);
812 CERROR("getattr error for "DFID": %d\n",
813 PFID(mdt_object_fid(o)), rc);
818 if (likely(ma->ma_valid & MA_INODE))
819 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
823 if (mdt_body_has_lov(la, reqbody)) {
824 if (ma->ma_valid & MA_LOV) {
825 LASSERT(ma->ma_lmm_size);
826 mdt_dump_lmm(D_INFO, ma->ma_lmm);
827 repbody->eadatasize = ma->ma_lmm_size;
828 if (S_ISDIR(la->la_mode))
829 repbody->valid |= OBD_MD_FLDIREA;
831 repbody->valid |= OBD_MD_FLEASIZE;
833 if (ma->ma_valid & MA_LMV) {
834 LASSERT(S_ISDIR(la->la_mode));
835 repbody->eadatasize = ma->ma_lmv_size;
836 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
838 } else if (S_ISLNK(la->la_mode) &&
839 reqbody->valid & OBD_MD_LINKNAME) {
840 buffer->lb_buf = ma->ma_lmm;
841 /* eadatasize from client includes NULL-terminator, so
842 * there is no need to read it */
843 buffer->lb_len = reqbody->eadatasize - 1;
844 rc = mo_readlink(env, next, buffer);
845 if (unlikely(rc <= 0)) {
846 CERROR("readlink failed: %d\n", rc);
849 int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
851 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
853 repbody->valid |= OBD_MD_LINKNAME;
854 /* we need to report back size with NULL-terminator
855 * because client expects that */
856 repbody->eadatasize = rc + 1;
857 if (repbody->eadatasize != reqbody->eadatasize)
858 CERROR("Read shorter symlink %d, expected %d\n",
859 rc, reqbody->eadatasize - 1);
861 ((char *)ma->ma_lmm)[rc] = 0;
863 /* If the total CDEBUG() size is larger than a page, it
864 * will print a warning to the console, avoid this by
865 * printing just the last part of the symlink. */
866 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
867 print_limit < rc ? "..." : "", print_limit,
868 (char *)ma->ma_lmm + rc - print_limit, rc);
873 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
874 repbody->max_cookiesize = 0;
875 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
876 repbody->valid |= OBD_MD_FLMODEASIZE;
877 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
878 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
879 repbody->max_cookiesize);
882 if (exp_connect_rmtclient(info->mti_exp) &&
883 reqbody->valid & OBD_MD_FLRMTPERM) {
884 void *buf = req_capsule_server_get(pill, &RMF_ACL);
886 /* mdt_getattr_lock only */
887 rc = mdt_pack_remote_perm(info, o, buf);
889 repbody->valid &= ~OBD_MD_FLRMTPERM;
890 repbody->aclsize = 0;
893 repbody->valid |= OBD_MD_FLRMTPERM;
894 repbody->aclsize = sizeof(struct mdt_remote_perm);
897 #ifdef CONFIG_FS_POSIX_ACL
898 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
899 (reqbody->valid & OBD_MD_FLACL)) {
900 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
901 buffer->lb_len = req_capsule_get_size(pill,
902 &RMF_ACL, RCL_SERVER);
903 if (buffer->lb_len > 0) {
904 rc = mo_xattr_get(env, next, buffer,
905 XATTR_NAME_ACL_ACCESS);
907 if (rc == -ENODATA) {
908 repbody->aclsize = 0;
909 repbody->valid |= OBD_MD_FLACL;
911 } else if (rc == -EOPNOTSUPP) {
914 CERROR("got acl size: %d\n", rc);
917 repbody->aclsize = rc;
918 repbody->valid |= OBD_MD_FLACL;
925 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
926 info->mti_mdt->mdt_opts.mo_mds_capa &&
927 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
928 struct lustre_capa *capa;
930 capa = req_capsule_server_get(pill, &RMF_CAPA1);
932 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
933 rc = mo_capa_get(env, next, capa, 0);
936 repbody->valid |= OBD_MD_FLMDSCAPA;
941 mdt_counter_incr(req, LPROC_MDT_GETATTR);
946 static int mdt_renew_capa(struct mdt_thread_info *info)
948 struct mdt_object *obj = info->mti_object;
949 struct mdt_body *body;
950 struct lustre_capa *capa, *c;
954 /* if object doesn't exist, or server has disabled capability,
955 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
958 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
959 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
962 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
963 LASSERT(body != NULL);
965 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
968 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
972 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
974 body->valid |= OBD_MD_FLOSSCAPA;
978 static int mdt_getattr(struct mdt_thread_info *info)
980 struct mdt_object *obj = info->mti_object;
981 struct req_capsule *pill = info->mti_pill;
982 struct mdt_body *reqbody;
983 struct mdt_body *repbody;
988 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
991 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
992 rc = req_capsule_server_pack(pill);
994 RETURN(err_serious(rc));
995 rc = mdt_renew_capa(info);
996 GOTO(out_shrink, rc);
999 LASSERT(obj != NULL);
1000 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
1002 mode = lu_object_attr(&obj->mot_obj.mo_lu);
1004 /* old clients may not report needed easize, use max value then */
1005 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1006 reqbody->eadatasize == 0 ?
1007 info->mti_mdt->mdt_max_mdsize :
1008 reqbody->eadatasize);
1010 rc = req_capsule_server_pack(pill);
1011 if (unlikely(rc != 0))
1012 RETURN(err_serious(rc));
1014 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1015 LASSERT(repbody != NULL);
1016 repbody->eadatasize = 0;
1017 repbody->aclsize = 0;
1019 if (reqbody->valid & OBD_MD_FLRMTPERM)
1020 rc = mdt_init_ucred(info, reqbody);
1022 rc = mdt_check_ucred(info);
1024 GOTO(out_shrink, rc);
1026 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1027 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1030 * Don't check capability at all, because rename might getattr for
1031 * remote obj, and at that time no capability is available.
1033 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1034 rc = mdt_getattr_internal(info, obj, 0);
1035 if (reqbody->valid & OBD_MD_FLRMTPERM)
1036 mdt_exit_ucred(info);
1039 mdt_client_compatibility(info);
1040 rc2 = mdt_fix_reply(info);
1046 static int mdt_is_subdir(struct mdt_thread_info *info)
1048 struct mdt_object *o = info->mti_object;
1049 struct req_capsule *pill = info->mti_pill;
1050 const struct mdt_body *body = info->mti_body;
1051 struct mdt_body *repbody;
1057 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1060 * We save last checked parent fid to @repbody->fid1 for remote
1063 LASSERT(fid_is_sane(&body->fid2));
1064 LASSERT(mdt_object_exists(o) > 0);
1065 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1066 &body->fid2, &repbody->fid1);
1067 if (rc == 0 || rc == -EREMOTE)
1068 repbody->valid |= OBD_MD_FLID;
1073 static int mdt_raw_lookup(struct mdt_thread_info *info,
1074 struct mdt_object *parent,
1075 const struct lu_name *lname,
1076 struct ldlm_reply *ldlm_rep)
1078 struct md_object *next = mdt_object_child(info->mti_object);
1079 const struct mdt_body *reqbody = info->mti_body;
1080 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1081 struct mdt_body *repbody;
1085 if (reqbody->valid != OBD_MD_FLID)
1088 LASSERT(!info->mti_cross_ref);
1090 /* Only got the fid of this obj by name */
1091 fid_zero(child_fid);
1092 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1095 /* XXX is raw_lookup possible as intent operation? */
1098 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1101 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1103 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1106 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1107 repbody->fid1 = *child_fid;
1108 repbody->valid = OBD_MD_FLID;
1114 * UPDATE lock should be taken against parent, and be release before exit;
1115 * child_bits lock should be taken against child, and be returned back:
1116 * (1)normal request should release the child lock;
1117 * (2)intent request will grant the lock to client.
1119 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1120 struct mdt_lock_handle *lhc,
1122 struct ldlm_reply *ldlm_rep)
1124 struct ptlrpc_request *req = mdt_info_req(info);
1125 struct mdt_body *reqbody = NULL;
1126 struct mdt_object *parent = info->mti_object;
1127 struct mdt_object *child;
1128 struct md_object *next = mdt_object_child(parent);
1129 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1130 struct lu_name *lname = NULL;
1131 const char *name = NULL;
1133 struct mdt_lock_handle *lhp = NULL;
1134 struct ldlm_lock *lock;
1135 struct ldlm_res_id *res_id;
1142 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1143 LASSERT(ergo(is_resent,
1144 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1146 LASSERT(parent != NULL);
1147 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1149 RETURN(err_serious(-EFAULT));
1151 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1153 if (!info->mti_cross_ref) {
1155 * XXX: Check for "namelen == 0" is for getattr by fid
1156 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1157 * that is the name must contain at least one character and
1158 * the terminating '\0'
1161 reqbody = req_capsule_client_get(info->mti_pill,
1163 if (unlikely(reqbody == NULL))
1164 RETURN(err_serious(-EFAULT));
1166 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1167 RETURN(err_serious(-EINVAL));
1170 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1172 PFID(mdt_object_fid(parent)),
1173 PFID(&reqbody->fid2), ldlm_rep);
1175 lname = mdt_name(info->mti_env, (char *)name, namelen);
1176 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1177 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1181 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1183 rc = mdt_object_exists(parent);
1184 if (unlikely(rc == 0)) {
1185 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1186 &parent->mot_obj.mo_lu,
1187 "Parent doesn't exist!\n");
1189 } else if (!info->mti_cross_ref) {
1190 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
1191 PFID(mdt_object_fid(parent)));
1194 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1202 if (info->mti_cross_ref) {
1203 /* Only getattr on the child. Parent is on another node. */
1204 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1206 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1207 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1210 /* Do not take lock for resent case. */
1211 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1212 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1213 lhc->mlh_reg_lh.cookie);
1214 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1215 &lock->l_resource->lr_name));
1216 LDLM_LOCK_PUT(lock);
1219 mdt_lock_handle_init(lhc);
1220 mdt_lock_reg_init(lhc, LCK_PR);
1223 * Object's name is on another MDS, no lookup lock is
1224 * needed here but update is.
1226 child_bits &= ~MDS_INODELOCK_LOOKUP;
1227 child_bits |= MDS_INODELOCK_UPDATE;
1229 rc = mdt_object_lock(info, child, lhc, child_bits,
1233 /* Finally, we can get attr for child. */
1234 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1236 rc = mdt_getattr_internal(info, child, 0);
1237 if (unlikely(rc != 0))
1238 mdt_object_unlock(info, child, lhc, 1);
1244 /* step 1: lock parent only if parent is a directory */
1245 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1246 lhp = &info->mti_lh[MDT_LH_PARENT];
1247 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1248 rc = mdt_object_lock(info, parent, lhp,
1249 MDS_INODELOCK_UPDATE,
1251 if (unlikely(rc != 0))
1255 /* step 2: lookup child's fid by name */
1256 fid_zero(child_fid);
1257 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1262 mdt_set_disposition(info, ldlm_rep,
1264 GOTO(out_parent, rc);
1266 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1268 *child_fid = reqbody->fid2;
1269 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1273 *step 3: find the child object by fid & lock it.
1274 * regardless if it is local or remote.
1276 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1278 if (unlikely(IS_ERR(child)))
1279 GOTO(out_parent, rc = PTR_ERR(child));
1281 /* Do not take lock for resent case. */
1282 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1283 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1284 lhc->mlh_reg_lh.cookie);
1286 res_id = &lock->l_resource->lr_name;
1287 if (!fid_res_name_eq(mdt_object_fid(child),
1288 &lock->l_resource->lr_name)) {
1289 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1290 &lock->l_resource->lr_name),
1291 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1292 (unsigned long)res_id->name[0],
1293 (unsigned long)res_id->name[1],
1294 (unsigned long)res_id->name[2],
1295 PFID(mdt_object_fid(parent)));
1296 CWARN("Although resent, but still not get child lock"
1297 "parent:"DFID" child:"DFID"\n",
1298 PFID(mdt_object_fid(parent)),
1299 PFID(mdt_object_fid(child)));
1300 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1301 LDLM_LOCK_PUT(lock);
1304 LDLM_LOCK_PUT(lock);
1308 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1309 mdt_lock_handle_init(lhc);
1310 if (child_bits == MDS_INODELOCK_LAYOUT)
1311 mdt_lock_reg_init(lhc, LCK_CR);
1313 mdt_lock_reg_init(lhc, LCK_PR);
1315 if (mdt_object_exists(child) == 0) {
1316 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1317 &child->mot_obj.mo_lu,
1318 "Object doesn't exist!\n");
1319 GOTO(out_child, rc = -ENOENT);
1322 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1323 struct md_attr *ma = &info->mti_attr;
1326 ma->ma_need = MA_INODE;
1327 rc = mdt_attr_get_complex(info, child, ma);
1328 if (unlikely(rc != 0))
1329 GOTO(out_child, rc);
1331 /* layout lock is used only on regular files */
1332 if ((ma->ma_valid & MA_INODE) &&
1333 (ma->ma_attr.la_valid & LA_MODE) &&
1334 !S_ISREG(ma->ma_attr.la_mode))
1335 child_bits &= ~MDS_INODELOCK_LAYOUT;
1337 /* If the file has not been changed for some time, we
1338 * return not only a LOOKUP lock, but also an UPDATE
1339 * lock and this might save us RPC on later STAT. For
1340 * directories, it also let negative dentry starts
1341 * working for this dir. */
1342 if (ma->ma_valid & MA_INODE &&
1343 ma->ma_attr.la_valid & LA_CTIME &&
1344 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1345 ma->ma_attr.la_ctime < cfs_time_current_sec())
1346 child_bits |= MDS_INODELOCK_UPDATE;
1349 rc = mdt_object_lock(info, child, lhc, child_bits,
1352 if (unlikely(rc != 0))
1353 GOTO(out_child, rc);
1356 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1357 /* Get MA_SOM attributes if update lock is given. */
1359 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1360 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1363 /* finally, we can get attr for child. */
1364 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1365 rc = mdt_getattr_internal(info, child, ma_need);
1366 if (unlikely(rc != 0)) {
1367 mdt_object_unlock(info, child, lhc, 1);
1369 /* Debugging code. */
1370 res_id = &lock->l_resource->lr_name;
1371 LDLM_DEBUG(lock, "Returning lock to client");
1372 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1373 &lock->l_resource->lr_name),
1374 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1375 (unsigned long)res_id->name[0],
1376 (unsigned long)res_id->name[1],
1377 (unsigned long)res_id->name[2],
1378 PFID(mdt_object_fid(child)));
1379 mdt_pack_size2body(info, child);
1382 LDLM_LOCK_PUT(lock);
1386 mdt_object_put(info->mti_env, child);
1389 mdt_object_unlock(info, parent, lhp, 1);
1393 /* normal handler: should release the child lock */
1394 static int mdt_getattr_name(struct mdt_thread_info *info)
1396 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1397 struct mdt_body *reqbody;
1398 struct mdt_body *repbody;
1402 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1403 LASSERT(reqbody != NULL);
1404 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1405 LASSERT(repbody != NULL);
1407 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
1408 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1409 repbody->eadatasize = 0;
1410 repbody->aclsize = 0;
1412 rc = mdt_init_ucred(info, reqbody);
1414 GOTO(out_shrink, rc);
1416 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1417 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1418 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1419 lhc->mlh_reg_lh.cookie = 0;
1421 mdt_exit_ucred(info);
1424 mdt_client_compatibility(info);
1425 rc2 = mdt_fix_reply(info);
1431 static const struct lu_device_operations mdt_lu_ops;
1433 static int lu_device_is_mdt(struct lu_device *d)
1435 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1438 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1439 void *karg, void *uarg);
1441 static int mdt_set_info(struct mdt_thread_info *info)
1443 struct ptlrpc_request *req = mdt_info_req(info);
1446 int keylen, vallen, rc = 0;
1449 rc = req_capsule_server_pack(info->mti_pill);
1453 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1455 DEBUG_REQ(D_HA, req, "no set_info key");
1459 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1462 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1464 DEBUG_REQ(D_HA, req, "no set_info val");
1468 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1471 /* Swab any part of val you need to here */
1472 if (KEY_IS(KEY_READ_ONLY)) {
1474 lustre_msg_set_status(req->rq_repmsg, 0);
1476 cfs_spin_lock(&req->rq_export->exp_lock);
1478 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1480 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1481 cfs_spin_unlock(&req->rq_export->exp_lock);
1483 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1484 struct changelog_setinfo *cs =
1485 (struct changelog_setinfo *)val;
1486 if (vallen != sizeof(*cs)) {
1487 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1490 if (ptlrpc_req_need_swab(req)) {
1491 __swab64s(&cs->cs_recno);
1492 __swab32s(&cs->cs_id);
1495 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1497 lustre_msg_set_status(req->rq_repmsg, rc);
1505 static int mdt_connect(struct mdt_thread_info *info)
1508 struct ptlrpc_request *req;
1510 req = mdt_info_req(info);
1511 rc = target_handle_connect(req);
1513 LASSERT(req->rq_export != NULL);
1514 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1515 rc = mdt_init_sec_level(info);
1517 rc = mdt_init_idmap(info);
1519 obd_disconnect(class_export_get(req->rq_export));
1521 rc = err_serious(rc);
1526 static int mdt_disconnect(struct mdt_thread_info *info)
1531 rc = target_handle_disconnect(mdt_info_req(info));
1533 rc = err_serious(rc);
1537 static int mdt_sendpage(struct mdt_thread_info *info,
1538 struct lu_rdpg *rdpg, int nob)
1540 struct ptlrpc_request *req = mdt_info_req(info);
1541 struct obd_export *exp = req->rq_export;
1542 struct ptlrpc_bulk_desc *desc;
1543 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1550 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1555 if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1556 /* old client requires reply size in it's PAGE_SIZE,
1557 * which is rdpg->rp_count */
1558 nob = rdpg->rp_count;
1560 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1561 i++, tmpcount -= tmpsize) {
1562 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1563 ptlrpc_prep_bulk_page(desc, rdpg->rp_pages[i], 0, tmpsize);
1566 LASSERT(desc->bd_nob == nob);
1567 rc = target_bulk_io(exp, desc, lwi);
1568 ptlrpc_free_bulk(desc);
1572 #ifdef HAVE_SPLIT_SUPPORT
1574 * Retrieve dir entry from the page and insert it to the slave object, actually,
1575 * this should be in osd layer, but since it will not in the final product, so
1576 * just do it here and do not define more moo api anymore for this.
1578 static int mdt_write_dir_page(struct mdt_thread_info *info, struct page *page,
1581 struct mdt_object *object = info->mti_object;
1582 struct lu_fid *lf = &info->mti_tmp_fid2;
1583 struct md_attr *ma = &info->mti_attr;
1584 struct lu_dirpage *dp;
1585 struct lu_dirent *ent;
1586 int rc = 0, offset = 0;
1589 /* Make sure we have at least one entry. */
1594 * Disable trans for this name insert, since it will include many trans
1597 info->mti_no_need_trans = 1;
1599 * When write_dir_page, no need update parent's ctime,
1600 * and no permission check for name_insert.
1602 ma->ma_attr.la_ctime = 0;
1603 ma->ma_attr.la_valid = LA_MODE;
1604 ma->ma_valid = MA_INODE;
1607 dp = page_address(page);
1608 offset = (int)((__u32)lu_dirent_start(dp) - (__u32)dp);
1610 for (ent = lu_dirent_start(dp); ent != NULL;
1611 ent = lu_dirent_next(ent)) {
1612 struct lu_name *lname;
1615 if (le16_to_cpu(ent->lde_namelen) == 0)
1618 fid_le_to_cpu(lf, &ent->lde_fid);
1619 if (le64_to_cpu(ent->lde_hash) & MAX_HASH_HIGHEST_BIT)
1620 ma->ma_attr.la_mode = S_IFDIR;
1622 ma->ma_attr.la_mode = 0;
1623 OBD_ALLOC(name, le16_to_cpu(ent->lde_namelen) + 1);
1625 GOTO(out, rc = -ENOMEM);
1627 memcpy(name, ent->lde_name, le16_to_cpu(ent->lde_namelen));
1628 lname = mdt_name(info->mti_env, name,
1629 le16_to_cpu(ent->lde_namelen));
1630 ma->ma_attr_flags |= (MDS_PERM_BYPASS | MDS_QUOTA_IGNORE);
1631 rc = mdo_name_insert(info->mti_env,
1632 md_object_next(&object->mot_obj),
1634 OBD_FREE(name, le16_to_cpu(ent->lde_namelen) + 1);
1636 CERROR("Can't insert %*.*s, rc %d\n",
1637 le16_to_cpu(ent->lde_namelen),
1638 le16_to_cpu(ent->lde_namelen),
1643 offset += lu_dirent_size(ent);
1653 static int mdt_bulk_timeout(void *data)
1657 CERROR("mdt bulk transfer timeout \n");
1662 static int mdt_writepage(struct mdt_thread_info *info)
1664 struct ptlrpc_request *req = mdt_info_req(info);
1665 struct mdt_body *reqbody;
1666 struct l_wait_info *lwi;
1667 struct ptlrpc_bulk_desc *desc;
1673 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1674 if (reqbody == NULL)
1675 RETURN(err_serious(-EFAULT));
1677 desc = ptlrpc_prep_bulk_exp(req, 1, BULK_GET_SINK, MDS_BULK_PORTAL);
1679 RETURN(err_serious(-ENOMEM));
1681 /* allocate the page for the desc */
1682 page = cfs_alloc_page(CFS_ALLOC_STD);
1684 GOTO(desc_cleanup, rc = -ENOMEM);
1686 CDEBUG(D_INFO, "Received page offset %d size %d \n",
1687 (int)reqbody->size, (int)reqbody->nlink);
1689 ptlrpc_prep_bulk_page(desc, page, (int)reqbody->size,
1690 (int)reqbody->nlink);
1692 rc = sptlrpc_svc_prep_bulk(req, desc);
1694 GOTO(cleanup_page, rc);
1696 * Check if client was evicted while we were doing i/o before touching
1701 GOTO(cleanup_page, rc = -ENOMEM);
1703 if (desc->bd_export->exp_failed)
1706 rc = ptlrpc_start_bulk_transfer (desc);
1708 *lwi = LWI_TIMEOUT_INTERVAL(obd_timeout * CFS_HZ / 4, CFS_HZ,
1709 mdt_bulk_timeout, desc);
1710 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc) ||
1711 desc->bd_export->exp_failed, lwi);
1712 LASSERT(rc == 0 || rc == -ETIMEDOUT);
1713 if (rc == -ETIMEDOUT) {
1714 DEBUG_REQ(D_ERROR, req, "timeout on bulk GET");
1715 ptlrpc_abort_bulk(desc);
1716 } else if (desc->bd_export->exp_failed) {
1717 DEBUG_REQ(D_ERROR, req, "Eviction on bulk GET");
1719 ptlrpc_abort_bulk(desc);
1720 } else if (!desc->bd_success ||
1721 desc->bd_nob_transferred != desc->bd_nob) {
1722 DEBUG_REQ(D_ERROR, req, "%s bulk GET %d(%d)",
1724 "truncated" : "network error on",
1725 desc->bd_nob_transferred, desc->bd_nob);
1726 /* XXX should this be a different errno? */
1730 DEBUG_REQ(D_ERROR, req, "ptlrpc_bulk_get failed: rc %d", rc);
1733 GOTO(cleanup_lwi, rc);
1734 rc = mdt_write_dir_page(info, page, reqbody->nlink);
1739 cfs_free_page(page);
1741 ptlrpc_free_bulk(desc);
1746 static int mdt_readpage(struct mdt_thread_info *info)
1748 struct mdt_object *object = info->mti_object;
1749 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1750 struct mdt_body *reqbody;
1751 struct mdt_body *repbody;
1756 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1757 RETURN(err_serious(-ENOMEM));
1759 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1760 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1761 if (reqbody == NULL || repbody == NULL)
1762 RETURN(err_serious(-EFAULT));
1765 * prepare @rdpg before calling lower layers and transfer itself. Here
1766 * reqbody->size contains offset of where to start to read and
1767 * reqbody->nlink contains number bytes to read.
1769 rdpg->rp_hash = reqbody->size;
1770 if (rdpg->rp_hash != reqbody->size) {
1771 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1772 rdpg->rp_hash, reqbody->size);
1776 rdpg->rp_attrs = reqbody->mode;
1777 if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1778 rdpg->rp_attrs |= LUDA_64BITHASH;
1779 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1780 PTLRPC_MAX_BRW_SIZE);
1781 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1783 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1784 if (rdpg->rp_pages == NULL)
1787 for (i = 0; i < rdpg->rp_npages; ++i) {
1788 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1789 if (rdpg->rp_pages[i] == NULL)
1790 GOTO(free_rdpg, rc = -ENOMEM);
1793 /* call lower layers to fill allocated pages with directory data */
1794 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1796 GOTO(free_rdpg, rc);
1798 /* send pages to client */
1799 rc = mdt_sendpage(info, rdpg, rc);
1804 for (i = 0; i < rdpg->rp_npages; i++)
1805 if (rdpg->rp_pages[i] != NULL)
1806 cfs_free_page(rdpg->rp_pages[i]);
1807 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1809 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1815 static int mdt_reint_internal(struct mdt_thread_info *info,
1816 struct mdt_lock_handle *lhc,
1819 struct req_capsule *pill = info->mti_pill;
1820 struct mdt_body *repbody;
1825 rc = mdt_reint_unpack(info, op);
1827 CERROR("Can't unpack reint, rc %d\n", rc);
1828 RETURN(err_serious(rc));
1831 /* for replay (no_create) lmm is not needed, client has it already */
1832 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1833 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1834 info->mti_rr.rr_eadatalen);
1836 /* llog cookies are always 0, the field is kept for compatibility */
1837 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1838 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1840 rc = req_capsule_server_pack(pill);
1842 CERROR("Can't pack response, rc %d\n", rc);
1843 RETURN(err_serious(rc));
1846 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1847 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1849 repbody->eadatasize = 0;
1850 repbody->aclsize = 0;
1853 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1855 /* for replay no cookkie / lmm need, because client have this already */
1856 if (info->mti_spec.no_create)
1857 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1858 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1860 rc = mdt_init_ucred_reint(info);
1862 GOTO(out_shrink, rc);
1864 rc = mdt_fix_attr_ucred(info, op);
1866 GOTO(out_ucred, rc = err_serious(rc));
1868 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1869 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1870 GOTO(out_ucred, rc);
1872 rc = mdt_reint_rec(info, lhc);
1875 mdt_exit_ucred(info);
1877 mdt_client_compatibility(info);
1878 rc2 = mdt_fix_reply(info);
1884 static long mdt_reint_opcode(struct mdt_thread_info *info,
1885 const struct req_format **fmt)
1887 struct mdt_rec_reint *rec;
1890 opc = err_serious(-EFAULT);
1891 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1893 opc = rec->rr_opcode;
1894 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1895 if (opc < REINT_MAX && fmt[opc] != NULL)
1896 req_capsule_extend(info->mti_pill, fmt[opc]);
1898 CERROR("Unsupported opc: %ld\n", opc);
1899 opc = err_serious(opc);
1905 static int mdt_reint(struct mdt_thread_info *info)
1910 static const struct req_format *reint_fmts[REINT_MAX] = {
1911 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1912 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1913 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1914 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1915 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1916 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1917 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1922 opc = mdt_reint_opcode(info, reint_fmts);
1925 * No lock possible here from client to pass it to reint code
1928 rc = mdt_reint_internal(info, NULL, opc);
1933 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1937 /* this should sync the whole device */
1938 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1940 struct dt_device *dt = mdt->mdt_bottom;
1944 rc = dt->dd_ops->dt_sync(env, dt);
1948 /* this should sync this object */
1949 static int mdt_object_sync(struct mdt_thread_info *info)
1951 struct md_object *next;
1955 if (!mdt_object_exists(info->mti_object)) {
1956 CWARN("Non existing object "DFID"!\n",
1957 PFID(mdt_object_fid(info->mti_object)));
1960 next = mdt_object_child(info->mti_object);
1961 rc = mo_object_sync(info->mti_env, next);
1966 static int mdt_sync(struct mdt_thread_info *info)
1968 struct ptlrpc_request *req = mdt_info_req(info);
1969 struct req_capsule *pill = info->mti_pill;
1970 struct mdt_body *body;
1974 /* The fid may be zero, so we req_capsule_set manually */
1975 req_capsule_set(pill, &RQF_MDS_SYNC);
1977 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1979 RETURN(err_serious(-EINVAL));
1981 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1982 RETURN(err_serious(-ENOMEM));
1984 if (fid_seq(&body->fid1) == 0) {
1985 /* sync the whole device */
1986 rc = req_capsule_server_pack(pill);
1988 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1990 rc = err_serious(rc);
1992 /* sync an object */
1993 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1995 rc = mdt_object_sync(info);
1997 const struct lu_fid *fid;
1998 struct lu_attr *la = &info->mti_attr.ma_attr;
2000 info->mti_attr.ma_need = MA_INODE;
2001 info->mti_attr.ma_valid = 0;
2002 rc = mdt_attr_get_complex(info, info->mti_object,
2005 body = req_capsule_server_get(pill,
2007 fid = mdt_object_fid(info->mti_object);
2008 mdt_pack_attr2body(info, body, la, fid);
2012 rc = err_serious(rc);
2015 mdt_counter_incr(req, LPROC_MDT_SYNC);
2021 * Quotacheck handler.
2022 * in-kernel quotacheck isn't supported any more.
2024 static int mdt_quotacheck(struct mdt_thread_info *info)
2026 struct obd_quotactl *oqctl;
2030 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
2032 RETURN(err_serious(-EPROTO));
2034 rc = req_capsule_server_pack(info->mti_pill);
2036 RETURN(err_serious(rc));
2038 /* deprecated, not used any more */
2039 RETURN(-EOPNOTSUPP);
2043 * Handle quota control requests to consult current usage/limit, but also
2044 * to configure quota enforcement
2046 static int mdt_quotactl(struct mdt_thread_info *info)
2048 struct obd_export *exp = info->mti_exp;
2049 struct req_capsule *pill = info->mti_pill;
2050 struct obd_quotactl *oqctl, *repoqc;
2054 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2056 RETURN(err_serious(-EPROTO));
2058 rc = req_capsule_server_pack(pill);
2060 RETURN(err_serious(rc));
2062 switch (oqctl->qc_cmd) {
2064 case LUSTRE_Q_INVALIDATE:
2065 case LUSTRE_Q_FINVALIDATE:
2069 /* deprecated, not used any more */
2070 RETURN(-EOPNOTSUPP);
2071 /* master quotactl */
2076 /* slave quotactl */
2081 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2085 /* map uid/gid for remote client */
2087 if (exp_connect_rmtclient(exp)) {
2088 struct lustre_idmap_table *idmap;
2090 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
2092 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2093 oqctl->qc_cmd != Q_GETINFO))
2096 if (oqctl->qc_type == USRQUOTA)
2097 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2099 else if (oqctl->qc_type == GRPQUOTA)
2100 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2105 if (id == CFS_IDMAP_NOTFOUND) {
2106 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2111 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2113 RETURN(err_serious(-EFAULT));
2115 if (oqctl->qc_id != id)
2116 swap(oqctl->qc_id, id);
2118 switch (oqctl->qc_cmd) {
2120 /* master quotactl */
2124 /* XXX: not implemented yet. Will be when qmt support is
2126 CERROR("quotactl operation %d not implemented yet\n",
2128 RETURN(-EOPNOTSUPP);
2130 /* XXX: return no limit for now, just for testing purpose */
2131 memset(&oqctl->qc_dqblk, 0, sizeof(struct obd_dqblk));
2132 oqctl->qc_dqblk.dqb_valid = QIF_LIMITS;
2136 /* slave quotactl */
2139 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2144 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2148 if (oqctl->qc_id != id)
2149 swap(oqctl->qc_id, id);
2156 * OBD PING and other handlers.
2158 static int mdt_obd_ping(struct mdt_thread_info *info)
2163 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2165 rc = target_handle_ping(mdt_info_req(info));
2167 rc = err_serious(rc);
2172 * OBD_IDX_READ handler
2174 static int mdt_obd_idx_read(struct mdt_thread_info *info)
2176 struct mdt_device *mdt = info->mti_mdt;
2177 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2178 struct idx_info *req_ii, *rep_ii;
2182 memset(rdpg, 0, sizeof(*rdpg));
2183 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2185 /* extract idx_info buffer from request & reply */
2186 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2187 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2188 RETURN(err_serious(-EPROTO));
2190 rc = req_capsule_server_pack(info->mti_pill);
2192 RETURN(err_serious(rc));
2194 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2196 RETURN(err_serious(-EFAULT));
2197 rep_ii->ii_magic = IDX_INFO_MAGIC;
2199 /* extract hash to start with */
2200 rdpg->rp_hash = req_ii->ii_hash_start;
2202 /* extract requested attributes */
2203 rdpg->rp_attrs = req_ii->ii_attrs;
2205 /* check that fid packed in request is valid and supported */
2206 if (!fid_is_sane(&req_ii->ii_fid))
2208 rep_ii->ii_fid = req_ii->ii_fid;
2211 rep_ii->ii_flags = req_ii->ii_flags;
2213 /* compute number of pages to allocate, ii_count is the number of 4KB
2215 if (req_ii->ii_count <= 0)
2216 GOTO(out, rc = -EFAULT);
2217 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2218 PTLRPC_MAX_BRW_SIZE);
2219 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2221 /* allocate pages to store the containers */
2222 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2223 if (rdpg->rp_pages == NULL)
2224 GOTO(out, rc = -ENOMEM);
2225 for (i = 0; i < rdpg->rp_npages; i++) {
2226 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2227 if (rdpg->rp_pages[i] == NULL)
2228 GOTO(out, rc = -ENOMEM);
2231 /* populate pages with key/record pairs */
2232 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2236 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2237 "asked %d > %d\n", rc, rdpg->rp_count);
2239 /* send pages to client */
2240 rc = mdt_sendpage(info, rdpg, rc);
2244 if (rdpg->rp_pages) {
2245 for (i = 0; i < rdpg->rp_npages; i++)
2246 if (rdpg->rp_pages[i])
2247 cfs_free_page(rdpg->rp_pages[i]);
2248 OBD_FREE(rdpg->rp_pages,
2249 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2254 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
2256 return err_serious(-EOPNOTSUPP);
2259 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
2261 return err_serious(-EOPNOTSUPP);
2269 /** clone llog ctxt from child (mdd)
2270 * This allows remote llog (replicator) access.
2271 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2272 * context was originally set up, or we can handle them directly.
2273 * I choose the latter, but that means I need any llog
2274 * contexts set up by child to be accessable by the mdt. So we clone the
2275 * context into our context list here.
2277 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2280 struct md_device *next = mdt->mdt_child;
2281 struct llog_ctxt *ctxt;
2284 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2287 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2288 if (rc || ctxt == NULL) {
2292 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2294 CERROR("Can't set mdt ctxt %d\n", rc);
2299 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2300 struct mdt_device *mdt, int idx)
2302 struct llog_ctxt *ctxt;
2304 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2307 /* Put once for the get we just did, and once for the clone */
2308 llog_ctxt_put(ctxt);
2309 llog_ctxt_put(ctxt);
2313 static int mdt_llog_create(struct mdt_thread_info *info)
2317 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2318 rc = llog_origin_handle_open(mdt_info_req(info));
2319 return (rc < 0 ? err_serious(rc) : rc);
2322 static int mdt_llog_destroy(struct mdt_thread_info *info)
2326 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2327 rc = llog_origin_handle_destroy(mdt_info_req(info));
2328 return (rc < 0 ? err_serious(rc) : rc);
2331 static int mdt_llog_read_header(struct mdt_thread_info *info)
2335 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2336 rc = llog_origin_handle_read_header(mdt_info_req(info));
2337 return (rc < 0 ? err_serious(rc) : rc);
2340 static int mdt_llog_next_block(struct mdt_thread_info *info)
2344 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2345 rc = llog_origin_handle_next_block(mdt_info_req(info));
2346 return (rc < 0 ? err_serious(rc) : rc);
2349 static int mdt_llog_prev_block(struct mdt_thread_info *info)
2353 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2354 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2355 return (rc < 0 ? err_serious(rc) : rc);
2362 static struct ldlm_callback_suite cbs = {
2363 .lcs_completion = ldlm_server_completion_ast,
2364 .lcs_blocking = ldlm_server_blocking_ast,
2365 .lcs_glimpse = ldlm_server_glimpse_ast
2368 static int mdt_enqueue(struct mdt_thread_info *info)
2370 struct ptlrpc_request *req;
2374 * info->mti_dlm_req already contains swapped and (if necessary)
2375 * converted dlm request.
2377 LASSERT(info->mti_dlm_req != NULL);
2379 req = mdt_info_req(info);
2380 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2381 req, info->mti_dlm_req, &cbs);
2382 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2383 return rc ? err_serious(rc) : req->rq_status;
2386 static int mdt_convert(struct mdt_thread_info *info)
2389 struct ptlrpc_request *req;
2391 LASSERT(info->mti_dlm_req);
2392 req = mdt_info_req(info);
2393 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2394 return rc ? err_serious(rc) : req->rq_status;
2397 static int mdt_bl_callback(struct mdt_thread_info *info)
2399 CERROR("bl callbacks should not happen on MDS\n");
2401 return err_serious(-EOPNOTSUPP);
2404 static int mdt_cp_callback(struct mdt_thread_info *info)
2406 CERROR("cp callbacks should not happen on MDS\n");
2408 return err_serious(-EOPNOTSUPP);
2412 * sec context handlers
2414 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2418 rc = mdt_handle_idmap(info);
2421 struct ptlrpc_request *req = mdt_info_req(info);
2424 opc = lustre_msg_get_opc(req->rq_reqmsg);
2425 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2426 sptlrpc_svc_ctx_invalidate(req);
2429 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2434 static struct mdt_object *mdt_obj(struct lu_object *o)
2436 LASSERT(lu_device_is_mdt(o->lo_dev));
2437 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2440 struct mdt_object *mdt_object_new(const struct lu_env *env,
2441 struct mdt_device *d,
2442 const struct lu_fid *f)
2444 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2445 struct lu_object *o;
2446 struct mdt_object *m;
2449 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2450 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2451 if (unlikely(IS_ERR(o)))
2452 m = (struct mdt_object *)o;
2458 struct mdt_object *mdt_object_find(const struct lu_env *env,
2459 struct mdt_device *d,
2460 const struct lu_fid *f)
2462 struct lu_object *o;
2463 struct mdt_object *m;
2466 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2467 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2468 if (unlikely(IS_ERR(o)))
2469 m = (struct mdt_object *)o;
2476 * Asyncronous commit for mdt device.
2478 * Pass asynchonous commit call down the MDS stack.
2480 * \param env environment
2481 * \param mdt the mdt device
2483 static void mdt_device_commit_async(const struct lu_env *env,
2484 struct mdt_device *mdt)
2486 struct dt_device *dt = mdt->mdt_bottom;
2489 rc = dt->dd_ops->dt_commit_async(env, dt);
2490 if (unlikely(rc != 0))
2491 CWARN("async commit start failed with rc = %d", rc);
2495 * Mark the lock as "synchonous".
2497 * Mark the lock to deffer transaction commit to the unlock time.
2499 * \param lock the lock to mark as "synchonous"
2501 * \see mdt_is_lock_sync
2502 * \see mdt_save_lock
2504 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2506 lock->l_ast_data = (void*)1;
2510 * Check whehter the lock "synchonous" or not.
2512 * \param lock the lock to check
2513 * \retval 1 the lock is "synchonous"
2514 * \retval 0 the lock isn't "synchronous"
2516 * \see mdt_set_lock_sync
2517 * \see mdt_save_lock
2519 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2521 return lock->l_ast_data != NULL;
2525 * Blocking AST for mdt locks.
2527 * Starts transaction commit if in case of COS lock conflict or
2528 * deffers such a commit to the mdt_save_lock.
2530 * \param lock the lock which blocks a request or cancelling lock
2531 * \param desc unused
2532 * \param data unused
2533 * \param flag indicates whether this cancelling or blocking callback
2535 * \see ldlm_blocking_ast_nocheck
2537 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2538 void *data, int flag)
2540 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2541 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2545 if (flag == LDLM_CB_CANCELING)
2547 lock_res_and_lock(lock);
2548 if (lock->l_blocking_ast != mdt_blocking_ast) {
2549 unlock_res_and_lock(lock);
2552 if (mdt_cos_is_enabled(mdt) &&
2553 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2554 lock->l_blocking_lock != NULL &&
2555 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2556 mdt_set_lock_sync(lock);
2558 rc = ldlm_blocking_ast_nocheck(lock);
2560 /* There is no lock conflict if l_blocking_lock == NULL,
2561 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2562 * when the last reference to a local lock was released */
2563 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2566 rc = lu_env_init(&env, LCT_LOCAL);
2567 if (unlikely(rc != 0))
2568 CWARN("lu_env initialization failed with rc = %d,"
2569 "cannot start asynchronous commit\n", rc);
2571 mdt_device_commit_async(&env, mdt);
2577 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2578 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2580 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2581 ldlm_policy_data_t *policy = &info->mti_policy;
2582 struct ldlm_res_id *res_id = &info->mti_res_id;
2586 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2587 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2588 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2589 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2591 if (mdt_object_exists(o) < 0) {
2592 if (locality == MDT_CROSS_LOCK) {
2593 /* cross-ref object fix */
2594 ibits &= ~MDS_INODELOCK_UPDATE;
2595 ibits |= MDS_INODELOCK_LOOKUP;
2597 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2598 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2600 /* No PDO lock on remote object */
2601 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2604 if (lh->mlh_type == MDT_PDO_LOCK) {
2605 /* check for exists after object is locked */
2606 if (mdt_object_exists(o) == 0) {
2607 /* Non-existent object shouldn't have PDO lock */
2610 /* Non-dir object shouldn't have PDO lock */
2611 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2616 memset(policy, 0, sizeof(*policy));
2617 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2620 * Take PDO lock on whole directory and build correct @res_id for lock
2621 * on part of directory.
2623 if (lh->mlh_pdo_hash != 0) {
2624 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2625 mdt_lock_pdo_mode(info, o, lh);
2626 if (lh->mlh_pdo_mode != LCK_NL) {
2628 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2629 * is never going to be sent to client and we do not
2630 * want it slowed down due to possible cancels.
2632 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2633 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2634 policy, res_id, LDLM_FL_ATOMIC_CB,
2635 &info->mti_exp->exp_handle.h_cookie);
2641 * Finish res_id initializing by name hash marking part of
2642 * directory which is taking modification.
2644 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2647 policy->l_inodebits.bits = ibits;
2650 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2651 * going to be sent to client. If it is - mdt_intent_policy() path will
2652 * fix it up and turn FL_LOCAL flag off.
2654 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2655 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2656 &info->mti_exp->exp_handle.h_cookie);
2658 mdt_object_unlock(info, o, lh, 1);
2659 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2660 lh->mlh_pdo_hash != 0 &&
2661 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2662 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2669 * Save a lock within request object.
2671 * Keep the lock referenced until whether client ACK or transaction
2672 * commit happens or release the lock immediately depending on input
2673 * parameters. If COS is ON, a write lock is converted to COS lock
2676 * \param info thead info object
2677 * \param h lock handle
2678 * \param mode lock mode
2679 * \param decref force immediate lock releasing
2682 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2683 ldlm_mode_t mode, int decref)
2687 if (lustre_handle_is_used(h)) {
2688 if (decref || !info->mti_has_trans ||
2689 !(mode & (LCK_PW | LCK_EX))){
2690 mdt_fid_unlock(h, mode);
2692 struct mdt_device *mdt = info->mti_mdt;
2693 struct ldlm_lock *lock = ldlm_handle2lock(h);
2694 struct ptlrpc_request *req = mdt_info_req(info);
2697 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2699 CDEBUG(D_HA, "request = %p reply state = %p"
2700 " transno = "LPD64"\n",
2701 req, req->rq_reply_state, req->rq_transno);
2702 if (mdt_cos_is_enabled(mdt)) {
2704 ldlm_lock_downgrade(lock, LCK_COS);
2707 ptlrpc_save_lock(req, h, mode, no_ack);
2708 if (mdt_is_lock_sync(lock)) {
2709 CDEBUG(D_HA, "found sync-lock,"
2710 " async commit started\n");
2711 mdt_device_commit_async(info->mti_env,
2714 LDLM_LOCK_PUT(lock);
2723 * Unlock mdt object.
2725 * Immeditely release the regular lock and the PDO lock or save the
2726 * lock in reqeuest and keep them referenced until client ACK or
2727 * transaction commit.
2729 * \param info thread info object
2730 * \param o mdt object
2731 * \param lh mdt lock handle referencing regular and PDO locks
2732 * \param decref force immediate lock releasing
2734 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2735 struct mdt_lock_handle *lh, int decref)
2739 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2740 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2745 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2746 const struct lu_fid *f,
2747 struct mdt_lock_handle *lh,
2750 struct mdt_object *o;
2752 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2756 rc = mdt_object_lock(info, o, lh, ibits,
2759 mdt_object_put(info->mti_env, o);
2766 void mdt_object_unlock_put(struct mdt_thread_info * info,
2767 struct mdt_object * o,
2768 struct mdt_lock_handle *lh,
2771 mdt_object_unlock(info, o, lh, decref);
2772 mdt_object_put(info->mti_env, o);
2775 static struct mdt_handler *mdt_handler_find(__u32 opc,
2776 struct mdt_opc_slice *supported)
2778 struct mdt_opc_slice *s;
2779 struct mdt_handler *h;
2782 for (s = supported; s->mos_hs != NULL; s++) {
2783 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2784 h = s->mos_hs + (opc - s->mos_opc_start);
2785 if (likely(h->mh_opc != 0))
2786 LASSERTF(h->mh_opc == opc,
2787 "opcode mismatch %d != %d\n",
2790 h = NULL; /* unsupported opc */
2797 static int mdt_lock_resname_compat(struct mdt_device *m,
2798 struct ldlm_request *req)
2800 /* XXX something... later. */
2804 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2806 /* XXX something... later. */
2811 * Generic code handling requests that have struct mdt_body passed in:
2813 * - extract mdt_body from request and save it in @info, if present;
2815 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2818 * - if HABEO_CORPUS flag is set for this request type check whether object
2819 * actually exists on storage (lu_object_exists()).
2822 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2824 const struct mdt_body *body;
2825 struct mdt_object *obj;
2826 const struct lu_env *env;
2827 struct req_capsule *pill;
2831 env = info->mti_env;
2832 pill = info->mti_pill;
2834 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2838 if (!(body->valid & OBD_MD_FLID))
2841 if (!fid_is_sane(&body->fid1)) {
2842 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2847 * Do not get size or any capa fields before we check that request
2848 * contains capa actually. There are some requests which do not, for
2849 * instance MDS_IS_SUBDIR.
2851 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2852 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2853 mdt_set_capainfo(info, 0, &body->fid1,
2854 req_capsule_client_get(pill, &RMF_CAPA1));
2856 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2858 if ((flags & HABEO_CORPUS) &&
2859 !mdt_object_exists(obj)) {
2860 mdt_object_put(env, obj);
2861 /* for capability renew ENOENT will be handled in
2863 if (body->valid & OBD_MD_FLOSSCAPA)
2868 info->mti_object = obj;
2877 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2879 struct req_capsule *pill = info->mti_pill;
2883 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2884 rc = mdt_body_unpack(info, flags);
2888 if (rc == 0 && (flags & HABEO_REFERO)) {
2890 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2891 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2892 info->mti_body->eadatasize);
2893 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2894 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2897 rc = req_capsule_server_pack(pill);
2902 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2904 struct md_device *next = m->mdt_child;
2906 return next->md_ops->mdo_init_capa_ctxt(env, next,
2907 m->mdt_opts.mo_mds_capa,
2908 m->mdt_capa_timeout,
2914 * Invoke handler for this request opc. Also do necessary preprocessing
2915 * (according to handler ->mh_flags), and post-processing (setting of
2916 * ->last_{xid,committed}).
2918 static int mdt_req_handle(struct mdt_thread_info *info,
2919 struct mdt_handler *h, struct ptlrpc_request *req)
2921 int rc, serious = 0;
2926 LASSERT(h->mh_act != NULL);
2927 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2928 LASSERT(current->journal_info == NULL);
2931 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2932 * to put same checks into handlers like mdt_close(), mdt_reint(),
2933 * etc., without talking to mdt authors first. Checking same thing
2934 * there again is useless and returning 0 error without packing reply
2935 * is buggy! Handlers either pack reply or return error.
2937 * We return 0 here and do not send any reply in order to emulate
2938 * network failure. Do not send any reply in case any of NET related
2939 * fail_id has occured.
2941 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2945 flags = h->mh_flags;
2946 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2948 if (h->mh_fmt != NULL) {
2949 req_capsule_set(info->mti_pill, h->mh_fmt);
2950 rc = mdt_unpack_req_pack_rep(info, flags);
2953 if (rc == 0 && flags & MUTABOR &&
2954 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2955 /* should it be rq_status? */
2958 if (rc == 0 && flags & HABEO_CLAVIS) {
2959 struct ldlm_request *dlm_req;
2961 LASSERT(h->mh_fmt != NULL);
2963 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2964 if (dlm_req != NULL) {
2965 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2967 dlm_req->lock_desc.l_policy_data.\
2968 l_inodebits.bits == 0)) {
2970 * Lock without inodebits makes no sense and
2971 * will oops later in ldlm. If client miss to
2972 * set such bits, do not trigger ASSERTION.
2974 * For liblustre flock case, it maybe zero.
2978 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2979 rc = mdt_lock_resname_compat(
2982 info->mti_dlm_req = dlm_req;
2989 /* capability setting changed via /proc, needs reinitialize ctxt */
2990 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2991 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2992 info->mti_mdt->mdt_capa_conf = 0;
2995 if (likely(rc == 0)) {
2997 * Process request, there can be two types of rc:
2998 * 1) errors with msg unpack/pack, other failures outside the
2999 * operation itself. This is counted as serious errors;
3000 * 2) errors during fs operation, should be placed in rq_status
3003 rc = h->mh_act(info);
3005 !req->rq_no_reply && req->rq_reply_state == NULL) {
3006 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
3007 "pack reply and returned 0 error\n",
3011 serious = is_serious(rc);
3012 rc = clear_serious(rc);
3016 req->rq_status = rc;
3019 * ELDLM_* codes which > 0 should be in rq_status only as well as
3020 * all non-serious errors.
3022 if (rc > 0 || !serious)
3025 LASSERT(current->journal_info == NULL);
3027 if (rc == 0 && (flags & HABEO_CLAVIS) &&
3028 info->mti_mdt->mdt_opts.mo_compat_resname) {
3029 struct ldlm_reply *dlmrep;
3031 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3033 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3036 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3037 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3038 target_committed_to_req(req);
3040 if (unlikely(req_is_replay(req) &&
3041 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3042 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3046 target_send_reply(req, rc, info->mti_fail_id);
3050 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3052 lh->mlh_type = MDT_NUL_LOCK;
3053 lh->mlh_reg_lh.cookie = 0ull;
3054 lh->mlh_reg_mode = LCK_MINMODE;
3055 lh->mlh_pdo_lh.cookie = 0ull;
3056 lh->mlh_pdo_mode = LCK_MINMODE;
3059 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3061 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3062 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3066 * Initialize fields of struct mdt_thread_info. Other fields are left in
3067 * uninitialized state, because it's too expensive to zero out whole
3068 * mdt_thread_info (> 1K) on each request arrival.
3070 static void mdt_thread_info_init(struct ptlrpc_request *req,
3071 struct mdt_thread_info *info)
3074 struct md_capainfo *ci;
3076 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3077 info->mti_pill = &req->rq_pill;
3080 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3081 mdt_lock_handle_init(&info->mti_lh[i]);
3083 /* mdt device: it can be NULL while CONNECT */
3084 if (req->rq_export) {
3085 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3086 info->mti_exp = req->rq_export;
3088 info->mti_mdt = NULL;
3089 info->mti_env = req->rq_svc_thread->t_env;
3090 ci = md_capainfo(info->mti_env);
3091 memset(ci, 0, sizeof *ci);
3092 if (req->rq_export) {
3093 if (exp_connect_rmtclient(req->rq_export))
3094 ci->mc_auth = LC_ID_CONVERT;
3095 else if (req->rq_export->exp_connect_flags &
3096 OBD_CONNECT_MDS_CAPA)
3097 ci->mc_auth = LC_ID_PLAIN;
3099 ci->mc_auth = LC_ID_NONE;
3102 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3103 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3104 info->mti_mos = NULL;
3106 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3107 info->mti_body = NULL;
3108 info->mti_object = NULL;
3109 info->mti_dlm_req = NULL;
3110 info->mti_has_trans = 0;
3111 info->mti_no_need_trans = 0;
3112 info->mti_cross_ref = 0;
3113 info->mti_opdata = 0;
3114 info->mti_big_lmm_used = 0;
3116 /* To not check for split by default. */
3117 info->mti_spec.sp_ck_split = 0;
3118 info->mti_spec.no_create = 0;
3121 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3125 req_capsule_fini(info->mti_pill);
3126 if (info->mti_object != NULL) {
3128 * freeing an object may lead to OSD level transaction, do not
3129 * let it mess with MDT. bz19385.
3131 info->mti_no_need_trans = 1;
3132 mdt_object_put(info->mti_env, info->mti_object);
3133 info->mti_object = NULL;
3135 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3136 mdt_lock_handle_fini(&info->mti_lh[i]);
3137 info->mti_env = NULL;
3140 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3141 struct obd_device *obd, int *process)
3143 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3144 case MDS_CONNECT: /* This will never get here, but for completeness. */
3145 case OST_CONNECT: /* This will never get here, but for completeness. */
3146 case MDS_DISCONNECT:
3147 case OST_DISCONNECT:
3152 case MDS_DONE_WRITING:
3153 case MDS_SYNC: /* used in unmounting */
3159 *process = target_queue_recovery_request(req, obd);
3163 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3170 * Handle recovery. Return:
3171 * +1: continue request processing;
3172 * -ve: abort immediately with the given error code;
3173 * 0: send reply with error code in req->rq_status;
3175 static int mdt_recovery(struct mdt_thread_info *info)
3177 struct ptlrpc_request *req = mdt_info_req(info);
3178 struct obd_device *obd;
3182 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3185 case SEC_CTX_INIT_CONT:
3191 rc = mdt_handle_idmap(info);
3200 if (unlikely(!class_connected_export(req->rq_export))) {
3201 CERROR("operation %d on unconnected MDS from %s\n",
3202 lustre_msg_get_opc(req->rq_reqmsg),
3203 libcfs_id2str(req->rq_peer));
3204 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3205 * mds_A will get -ENOTCONN(especially for ping req),
3206 * which will cause that mds_A deactive timeout, then when
3207 * mds_A cleanup, the cleanup process will be suspended since
3208 * deactive timeout is not zero.
3210 req->rq_status = -ENOTCONN;
3211 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3215 /* sanity check: if the xid matches, the request must be marked as a
3216 * resent or replayed */
3217 if (req_xid_is_last(req)) {
3218 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3219 (MSG_RESENT | MSG_REPLAY))) {
3220 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3221 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3222 lustre_msg_get_flags(req->rq_reqmsg));
3224 req->rq_status = -ENOTCONN;
3229 /* else: note the opposite is not always true; a RESENT req after a
3230 * failover will usually not match the last_xid, since it was likely
3231 * never committed. A REPLAYed request will almost never match the
3232 * last xid, however it could for a committed, but still retained,
3235 obd = req->rq_export->exp_obd;
3237 /* Check for aborted recovery... */
3238 if (unlikely(obd->obd_recovering)) {
3241 DEBUG_REQ(D_INFO, req, "Got new replay");
3242 rc = mdt_filter_recovery_request(req, obd, &should_process);
3243 if (rc != 0 || !should_process)
3245 else if (should_process < 0) {
3246 req->rq_status = should_process;
3247 rc = ptlrpc_error(req);
3254 static int mdt_msg_check_version(struct lustre_msg *msg)
3258 switch (lustre_msg_get_opc(msg)) {
3260 case MDS_DISCONNECT:
3263 case SEC_CTX_INIT_CONT:
3266 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3268 CERROR("bad opc %u version %08x, expecting %08x\n",
3269 lustre_msg_get_opc(msg),
3270 lustre_msg_get_version(msg),
3271 LUSTRE_OBD_VERSION);
3275 case MDS_GETATTR_NAME:
3282 case MDS_DONE_WRITING:
3289 case MDS_QUOTACHECK:
3295 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3297 CERROR("bad opc %u version %08x, expecting %08x\n",
3298 lustre_msg_get_opc(msg),
3299 lustre_msg_get_version(msg),
3300 LUSTRE_MDS_VERSION);
3304 case LDLM_BL_CALLBACK:
3305 case LDLM_CP_CALLBACK:
3306 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3308 CERROR("bad opc %u version %08x, expecting %08x\n",
3309 lustre_msg_get_opc(msg),
3310 lustre_msg_get_version(msg),
3311 LUSTRE_DLM_VERSION);
3313 case OBD_LOG_CANCEL:
3314 case LLOG_ORIGIN_HANDLE_CREATE:
3315 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3316 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3317 case LLOG_ORIGIN_HANDLE_CLOSE:
3318 case LLOG_ORIGIN_HANDLE_DESTROY:
3319 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3321 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3323 CERROR("bad opc %u version %08x, expecting %08x\n",
3324 lustre_msg_get_opc(msg),
3325 lustre_msg_get_version(msg),
3326 LUSTRE_LOG_VERSION);
3329 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3335 static int mdt_handle0(struct ptlrpc_request *req,
3336 struct mdt_thread_info *info,
3337 struct mdt_opc_slice *supported)
3339 struct mdt_handler *h;
3340 struct lustre_msg *msg;
3345 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3348 LASSERT(current->journal_info == NULL);
3350 msg = req->rq_reqmsg;
3351 rc = mdt_msg_check_version(msg);
3352 if (likely(rc == 0)) {
3353 rc = mdt_recovery(info);
3354 if (likely(rc == +1)) {
3355 h = mdt_handler_find(lustre_msg_get_opc(msg),
3357 if (likely(h != NULL)) {
3358 rc = mdt_req_handle(info, h, req);
3360 CERROR("The unsupported opc: 0x%x\n",
3361 lustre_msg_get_opc(msg) );
3362 req->rq_status = -ENOTSUPP;
3363 rc = ptlrpc_error(req);
3368 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3373 * MDT handler function called by ptlrpc service thread when request comes.
3375 * XXX common "target" functionality should be factored into separate module
3376 * shared by mdt, ost and stand-alone services like fld.
3378 static int mdt_handle_common(struct ptlrpc_request *req,
3379 struct mdt_opc_slice *supported)
3382 struct mdt_thread_info *info;
3386 env = req->rq_svc_thread->t_env;
3387 LASSERT(env != NULL);
3388 LASSERT(env->le_ses != NULL);
3389 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3390 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3391 LASSERT(info != NULL);
3393 mdt_thread_info_init(req, info);
3395 rc = mdt_handle0(req, info, supported);
3397 mdt_thread_info_fini(info);
3402 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3405 int mdt_recovery_handle(struct ptlrpc_request *req)
3410 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3412 rc = mdt_handle_common(req, mdt_fld_handlers);
3415 rc = mdt_handle_common(req, mdt_seq_handlers);
3418 rc = mdt_handle_common(req, mdt_regular_handlers);
3425 static int mdt_regular_handle(struct ptlrpc_request *req)
3427 return mdt_handle_common(req, mdt_regular_handlers);
3430 static int mdt_readpage_handle(struct ptlrpc_request *req)
3432 return mdt_handle_common(req, mdt_readpage_handlers);
3435 static int mdt_xmds_handle(struct ptlrpc_request *req)
3437 return mdt_handle_common(req, mdt_xmds_handlers);
3440 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3442 return mdt_handle_common(req, mdt_seq_handlers);
3445 static int mdt_mdss_handle(struct ptlrpc_request *req)
3447 return mdt_handle_common(req, mdt_seq_handlers);
3450 static int mdt_dtss_handle(struct ptlrpc_request *req)
3452 return mdt_handle_common(req, mdt_seq_handlers);
3455 static int mdt_fld_handle(struct ptlrpc_request *req)
3457 return mdt_handle_common(req, mdt_fld_handlers);
3474 static int mdt_intent_getattr(enum mdt_it_code opcode,
3475 struct mdt_thread_info *info,
3476 struct ldlm_lock **,
3478 static int mdt_intent_reint(enum mdt_it_code opcode,
3479 struct mdt_thread_info *info,
3480 struct ldlm_lock **,
3483 static struct mdt_it_flavor {
3484 const struct req_format *it_fmt;
3486 int (*it_act)(enum mdt_it_code ,
3487 struct mdt_thread_info *,
3488 struct ldlm_lock **,
3491 } mdt_it_flavor[] = {
3493 .it_fmt = &RQF_LDLM_INTENT,
3494 /*.it_flags = HABEO_REFERO,*/
3496 .it_act = mdt_intent_reint,
3497 .it_reint = REINT_OPEN
3500 .it_fmt = &RQF_LDLM_INTENT,
3501 .it_flags = MUTABOR,
3502 .it_act = mdt_intent_reint,
3503 .it_reint = REINT_OPEN
3506 .it_fmt = &RQF_LDLM_INTENT,
3507 .it_flags = MUTABOR,
3508 .it_act = mdt_intent_reint,
3509 .it_reint = REINT_CREATE
3511 [MDT_IT_GETATTR] = {
3512 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3513 .it_flags = HABEO_REFERO,
3514 .it_act = mdt_intent_getattr
3516 [MDT_IT_READDIR] = {
3522 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3523 .it_flags = HABEO_REFERO,
3524 .it_act = mdt_intent_getattr
3527 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3528 .it_flags = MUTABOR,
3530 .it_reint = REINT_UNLINK
3534 .it_flags = MUTABOR,
3537 [MDT_IT_GETXATTR] = {
3543 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3544 .it_flags = HABEO_REFERO,
3545 .it_act = mdt_intent_getattr
3549 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3550 struct ldlm_lock **lockp,
3551 struct ldlm_lock *new_lock,
3552 struct mdt_lock_handle *lh,
3555 struct ptlrpc_request *req = mdt_info_req(info);
3556 struct ldlm_lock *lock = *lockp;
3559 * Get new lock only for cases when possible resent did not find any
3562 if (new_lock == NULL)
3563 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3565 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3566 lh->mlh_reg_lh.cookie = 0;
3570 LASSERTF(new_lock != NULL,
3571 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3574 * If we've already given this lock to a client once, then we should
3575 * have no readers or writers. Otherwise, we should have one reader
3576 * _or_ writer ref (which will be zeroed below) before returning the
3579 if (new_lock->l_export == req->rq_export) {
3580 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3582 LASSERT(new_lock->l_export == NULL);
3583 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3588 if (new_lock->l_export == req->rq_export) {
3590 * Already gave this to the client, which means that we
3591 * reconstructed a reply.
3593 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3595 lh->mlh_reg_lh.cookie = 0;
3596 RETURN(ELDLM_LOCK_REPLACED);
3600 * Fixup the lock to be given to the client.
3602 lock_res_and_lock(new_lock);
3603 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3604 * possible blocking AST. */
3605 while (new_lock->l_readers > 0) {
3606 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3607 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3608 new_lock->l_readers--;
3610 while (new_lock->l_writers > 0) {
3611 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3612 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3613 new_lock->l_writers--;
3616 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3617 new_lock->l_blocking_ast = lock->l_blocking_ast;
3618 new_lock->l_completion_ast = lock->l_completion_ast;
3619 new_lock->l_remote_handle = lock->l_remote_handle;
3620 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3622 unlock_res_and_lock(new_lock);
3624 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3625 &new_lock->l_remote_handle,
3626 &new_lock->l_exp_hash);
3628 LDLM_LOCK_RELEASE(new_lock);
3629 lh->mlh_reg_lh.cookie = 0;
3631 RETURN(ELDLM_LOCK_REPLACED);
3634 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3635 struct ldlm_lock *new_lock,
3636 struct ldlm_lock **old_lock,
3637 struct mdt_lock_handle *lh)
3639 struct ptlrpc_request *req = mdt_info_req(info);
3640 struct obd_export *exp = req->rq_export;
3641 struct lustre_handle remote_hdl;
3642 struct ldlm_request *dlmreq;
3643 struct ldlm_lock *lock;
3645 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3648 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3649 remote_hdl = dlmreq->lock_handle[0];
3651 /* In the function below, .hs_keycmp resolves to
3652 * ldlm_export_lock_keycmp() */
3653 /* coverity[overrun-buffer-val] */
3654 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3656 if (lock != new_lock) {
3657 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3658 lh->mlh_reg_mode = lock->l_granted_mode;
3660 LDLM_DEBUG(lock, "Restoring lock cookie");
3661 DEBUG_REQ(D_DLMTRACE, req,
3662 "restoring lock cookie "LPX64,
3663 lh->mlh_reg_lh.cookie);
3665 *old_lock = LDLM_LOCK_GET(lock);
3666 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3670 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3674 * If the xid matches, then we know this is a resent request, and allow
3675 * it. (It's probably an OPEN, for which we don't send a lock.
3677 if (req_xid_is_last(req))
3681 * This remote handle isn't enqueued, so we never received or processed
3682 * this request. Clear MSG_RESENT, because it can be handled like any
3683 * normal request now.
3685 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3687 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3691 static int mdt_intent_getattr(enum mdt_it_code opcode,
3692 struct mdt_thread_info *info,
3693 struct ldlm_lock **lockp,
3696 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3697 struct ldlm_lock *new_lock = NULL;
3699 struct ldlm_reply *ldlm_rep;
3700 struct ptlrpc_request *req;
3701 struct mdt_body *reqbody;
3702 struct mdt_body *repbody;
3706 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3709 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3712 info->mti_spec.sp_ck_split = !!(reqbody->valid & OBD_MD_FLCKSPLIT);
3713 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3714 repbody->eadatasize = 0;
3715 repbody->aclsize = 0;
3719 child_bits = MDS_INODELOCK_LOOKUP;
3721 case MDT_IT_GETATTR:
3722 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3724 case MDT_IT_LAYOUT: {
3725 static int printed = 0;
3728 CERROR("layout lock not supported by this version\n");
3731 GOTO(out_shrink, rc = -EINVAL);
3735 CERROR("Unsupported intent (%d)\n", opcode);
3736 GOTO(out_shrink, rc = -EINVAL);
3739 rc = mdt_init_ucred(info, reqbody);
3741 GOTO(out_shrink, rc);
3743 req = info->mti_pill->rc_req;
3744 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3745 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3747 /* Get lock from request for possible resent case. */
3748 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3750 ldlm_rep->lock_policy_res2 =
3751 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3753 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3754 ldlm_rep->lock_policy_res2 = 0;
3755 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3756 ldlm_rep->lock_policy_res2) {
3757 lhc->mlh_reg_lh.cookie = 0ull;
3758 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3761 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3764 mdt_exit_ucred(info);
3766 mdt_client_compatibility(info);
3767 rc2 = mdt_fix_reply(info);
3773 static int mdt_intent_reint(enum mdt_it_code opcode,
3774 struct mdt_thread_info *info,
3775 struct ldlm_lock **lockp,
3778 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3779 struct ldlm_reply *rep = NULL;
3783 static const struct req_format *intent_fmts[REINT_MAX] = {
3784 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3785 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3790 opc = mdt_reint_opcode(info, intent_fmts);
3794 if (mdt_it_flavor[opcode].it_reint != opc) {
3795 CERROR("Reint code %ld doesn't match intent: %d\n",
3797 RETURN(err_serious(-EPROTO));
3800 /* Get lock from request for possible resent case. */
3801 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3803 rc = mdt_reint_internal(info, lhc, opc);
3805 /* Check whether the reply has been packed successfully. */
3806 if (mdt_info_req(info)->rq_repmsg != NULL)
3807 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3809 RETURN(err_serious(-EFAULT));
3811 /* MDC expects this in any case */
3813 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3815 /* Cross-ref case, the lock should be returned to the client */
3816 if (rc == -EREMOTE) {
3817 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3818 rep->lock_policy_res2 = 0;
3819 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3822 rep->lock_policy_res2 = clear_serious(rc);
3824 if (rep->lock_policy_res2 == -ENOENT &&
3825 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3826 rep->lock_policy_res2 = 0;
3828 if (rc == -ENOTCONN || rc == -ENODEV ||
3829 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3831 * If it is the disconnect error (ENODEV & ENOCONN), the error
3832 * will be returned by rq_status, and client at ptlrpc layer
3833 * will detect this, then disconnect, reconnect the import
3834 * immediately, instead of impacting the following the rpc.
3836 lhc->mlh_reg_lh.cookie = 0ull;
3840 * For other cases, the error will be returned by intent.
3841 * and client will retrieve the result from intent.
3844 * FIXME: when open lock is finished, that should be
3847 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3848 LASSERTF(rc == 0, "Error occurred but lock handle "
3849 "is still in use\n");
3850 rep->lock_policy_res2 = 0;
3851 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3854 lhc->mlh_reg_lh.cookie = 0ull;
3855 RETURN(ELDLM_LOCK_ABORTED);
3860 static int mdt_intent_code(long itcode)
3868 case IT_OPEN|IT_CREAT:
3875 rc = MDT_IT_READDIR;
3878 rc = MDT_IT_GETATTR;
3890 rc = MDT_IT_GETXATTR;
3896 CERROR("Unknown intent opcode: %ld\n", itcode);
3903 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3904 struct ldlm_lock **lockp, int flags)
3906 struct req_capsule *pill;
3907 struct mdt_it_flavor *flv;
3912 opc = mdt_intent_code(itopc);
3916 pill = info->mti_pill;
3917 flv = &mdt_it_flavor[opc];
3919 if (flv->it_fmt != NULL)
3920 req_capsule_extend(pill, flv->it_fmt);
3922 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3924 struct ptlrpc_request *req = mdt_info_req(info);
3925 if (flv->it_flags & MUTABOR &&
3926 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3929 if (rc == 0 && flv->it_act != NULL) {
3930 /* execute policy */
3931 rc = flv->it_act(opc, info, lockp, flags);
3938 static int mdt_intent_policy(struct ldlm_namespace *ns,
3939 struct ldlm_lock **lockp, void *req_cookie,
3940 ldlm_mode_t mode, int flags, void *data)
3942 struct mdt_thread_info *info;
3943 struct ptlrpc_request *req = req_cookie;
3944 struct ldlm_intent *it;
3945 struct req_capsule *pill;
3950 LASSERT(req != NULL);
3952 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3954 LASSERT(info != NULL);
3955 pill = info->mti_pill;
3956 LASSERT(pill->rc_req == req);
3958 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3959 req_capsule_extend(pill, &RQF_LDLM_INTENT);
3960 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3962 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3966 /* Lock without inodebits makes no sense and will oops
3967 * later in ldlm. Let's check it now to see if we have
3968 * ibits corrupted somewhere in mdt_intent_opc().
3969 * The case for client miss to set ibits has been
3970 * processed by others. */
3971 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3972 lr_type == LDLM_IBITS,
3973 info->mti_dlm_req->lock_desc.\
3974 l_policy_data.l_inodebits.bits != 0));
3976 rc = err_serious(-EFAULT);
3978 /* No intent was provided */
3979 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3980 rc = req_capsule_server_pack(pill);
3982 rc = err_serious(rc);
3987 static int mdt_seq_fini(const struct lu_env *env,
3988 struct mdt_device *m)
3990 struct md_site *ms = mdt_md_site(m);
3994 if (ms->ms_server_seq) {
3995 seq_server_fini(ms->ms_server_seq, env);
3996 OBD_FREE_PTR(ms->ms_server_seq);
3997 ms->ms_server_seq = NULL;
4000 if (ms->ms_control_seq) {
4001 seq_server_fini(ms->ms_control_seq, env);
4002 OBD_FREE_PTR(ms->ms_control_seq);
4003 ms->ms_control_seq = NULL;
4006 if (ms->ms_client_seq) {
4007 seq_client_fini(ms->ms_client_seq);
4008 OBD_FREE_PTR(ms->ms_client_seq);
4009 ms->ms_client_seq = NULL;
4016 static int mdt_seq_init(const struct lu_env *env,
4018 struct mdt_device *m)
4025 ms = mdt_md_site(m);
4028 * This is sequence-controller node. Init seq-controller server on local
4031 if (ms->ms_node_id == 0) {
4032 LASSERT(ms->ms_control_seq == NULL);
4034 OBD_ALLOC_PTR(ms->ms_control_seq);
4035 if (ms->ms_control_seq == NULL)
4038 rc = seq_server_init(ms->ms_control_seq,
4039 m->mdt_bottom, uuid,
4040 LUSTRE_SEQ_CONTROLLER,
4045 GOTO(out_seq_fini, rc);
4047 OBD_ALLOC_PTR(ms->ms_client_seq);
4048 if (ms->ms_client_seq == NULL)
4049 GOTO(out_seq_fini, rc = -ENOMEM);
4051 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4052 if (prefix == NULL) {
4053 OBD_FREE_PTR(ms->ms_client_seq);
4054 GOTO(out_seq_fini, rc = -ENOMEM);
4057 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4061 * Init seq-controller client after seq-controller server is
4062 * ready. Pass ms->ms_control_seq to it for direct talking.
4064 rc = seq_client_init(ms->ms_client_seq, NULL,
4065 LUSTRE_SEQ_METADATA, prefix,
4066 ms->ms_control_seq);
4067 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4070 GOTO(out_seq_fini, rc);
4073 /* Init seq-server on local MDT */
4074 LASSERT(ms->ms_server_seq == NULL);
4076 OBD_ALLOC_PTR(ms->ms_server_seq);
4077 if (ms->ms_server_seq == NULL)
4078 GOTO(out_seq_fini, rc = -ENOMEM);
4080 rc = seq_server_init(ms->ms_server_seq,
4081 m->mdt_bottom, uuid,
4086 GOTO(out_seq_fini, rc = -ENOMEM);
4088 /* Assign seq-controller client to local seq-server. */
4089 if (ms->ms_node_id == 0) {
4090 LASSERT(ms->ms_client_seq != NULL);
4092 rc = seq_server_set_cli(ms->ms_server_seq,
4100 mdt_seq_fini(env, m);
4105 * Init client sequence manager which is used by local MDS to talk to sequence
4106 * controller on remote node.
4108 static int mdt_seq_init_cli(const struct lu_env *env,
4109 struct mdt_device *m,
4110 struct lustre_cfg *cfg)
4112 struct md_site *ms = mdt_md_site(m);
4113 struct obd_device *mdc;
4114 struct obd_uuid *uuidp, *mdcuuidp;
4115 char *uuid_str, *mdc_uuid_str;
4118 struct mdt_thread_info *info;
4119 char *p, *index_string = lustre_cfg_string(cfg, 2);
4122 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4123 uuidp = &info->mti_u.uuid[0];
4124 mdcuuidp = &info->mti_u.uuid[1];
4126 LASSERT(index_string);
4128 index = simple_strtol(index_string, &p, 10);
4130 CERROR("Invalid index in lustre_cgf, offset 2\n");
4134 /* check if this is adding the first MDC and controller is not yet
4136 if (index != 0 || ms->ms_client_seq)
4139 uuid_str = lustre_cfg_string(cfg, 1);
4140 mdc_uuid_str = lustre_cfg_string(cfg, 4);
4141 obd_str2uuid(uuidp, uuid_str);
4142 obd_str2uuid(mdcuuidp, mdc_uuid_str);
4144 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
4146 CERROR("can't find controller MDC by uuid %s\n",
4149 } else if (!mdc->obd_set_up) {
4150 CERROR("target %s not set up\n", mdc->obd_name);
4153 LASSERT(ms->ms_control_exp);
4154 OBD_ALLOC_PTR(ms->ms_client_seq);
4155 if (ms->ms_client_seq != NULL) {
4158 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4162 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4165 rc = seq_client_init(ms->ms_client_seq,
4167 LUSTRE_SEQ_METADATA,
4169 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4176 LASSERT(ms->ms_server_seq != NULL);
4177 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
4184 static void mdt_seq_fini_cli(struct mdt_device *m)
4190 ms = mdt_md_site(m);
4193 if (ms->ms_server_seq)
4194 seq_server_set_cli(ms->ms_server_seq,
4197 if (ms->ms_control_exp) {
4198 class_export_put(ms->ms_control_exp);
4199 ms->ms_control_exp = NULL;
4208 static int mdt_fld_fini(const struct lu_env *env,
4209 struct mdt_device *m)
4211 struct md_site *ms = mdt_md_site(m);
4214 if (ms && ms->ms_server_fld) {
4215 fld_server_fini(ms->ms_server_fld, env);
4216 OBD_FREE_PTR(ms->ms_server_fld);
4217 ms->ms_server_fld = NULL;
4223 static int mdt_fld_init(const struct lu_env *env,
4225 struct mdt_device *m)
4231 ms = mdt_md_site(m);
4233 OBD_ALLOC_PTR(ms->ms_server_fld);
4234 if (ms->ms_server_fld == NULL)
4235 RETURN(rc = -ENOMEM);
4237 rc = fld_server_init(ms->ms_server_fld,
4238 m->mdt_bottom, uuid,
4239 env, ms->ms_node_id);
4241 OBD_FREE_PTR(ms->ms_server_fld);
4242 ms->ms_server_fld = NULL;
4249 /* device init/fini methods */
4250 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
4253 if (m->mdt_regular_service != NULL) {
4254 ptlrpc_unregister_service(m->mdt_regular_service);
4255 m->mdt_regular_service = NULL;
4257 if (m->mdt_readpage_service != NULL) {
4258 ptlrpc_unregister_service(m->mdt_readpage_service);
4259 m->mdt_readpage_service = NULL;
4261 if (m->mdt_xmds_service != NULL) {
4262 ptlrpc_unregister_service(m->mdt_xmds_service);
4263 m->mdt_xmds_service = NULL;
4265 if (m->mdt_setattr_service != NULL) {
4266 ptlrpc_unregister_service(m->mdt_setattr_service);
4267 m->mdt_setattr_service = NULL;
4269 if (m->mdt_mdsc_service != NULL) {
4270 ptlrpc_unregister_service(m->mdt_mdsc_service);
4271 m->mdt_mdsc_service = NULL;
4273 if (m->mdt_mdss_service != NULL) {
4274 ptlrpc_unregister_service(m->mdt_mdss_service);
4275 m->mdt_mdss_service = NULL;
4277 if (m->mdt_dtss_service != NULL) {
4278 ptlrpc_unregister_service(m->mdt_dtss_service);
4279 m->mdt_dtss_service = NULL;
4281 if (m->mdt_fld_service != NULL) {
4282 ptlrpc_unregister_service(m->mdt_fld_service);
4283 m->mdt_fld_service = NULL;
4288 static int mdt_start_ptlrpc_service(struct mdt_device *m)
4290 static struct ptlrpc_service_conf conf;
4291 cfs_proc_dir_entry_t *procfs_entry;
4295 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
4296 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4297 "mdt_ldlm_client", m->mdt_ldlm_client);
4299 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
4301 conf = (typeof(conf)) {
4302 .psc_name = LUSTRE_MDT_NAME,
4303 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4305 .bc_nbufs = MDS_NBUFS,
4306 .bc_buf_size = MDS_BUFSIZE,
4307 .bc_req_max_size = MDS_MAXREQSIZE,
4308 .bc_rep_max_size = MDS_MAXREPSIZE,
4309 .bc_req_portal = MDS_REQUEST_PORTAL,
4310 .bc_rep_portal = MDC_REPLY_PORTAL,
4313 * We'd like to have a mechanism to set this on a per-device
4314 * basis, but alas...
4317 .tc_thr_name = LUSTRE_MDT_NAME,
4318 .tc_thr_factor = MDT_THR_FACTOR,
4319 .tc_nthrs_init = MDT_NTHRS_INIT,
4320 .tc_nthrs_base = MDT_NTHRS_BASE,
4321 .tc_nthrs_max = MDT_NTHRS_MAX,
4322 .tc_nthrs_user = mds_num_threads,
4323 .tc_cpu_affinity = 1,
4324 .tc_ctx_tags = LCT_MD_THREAD,
4327 .cc_pattern = mds_num_cpts,
4330 .so_req_handler = mdt_regular_handle,
4331 .so_req_printer = target_print_req,
4332 .so_hpreq_handler = ptlrpc_hpreq_handler,
4335 m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
4336 if (IS_ERR(m->mdt_regular_service)) {
4337 rc = PTR_ERR(m->mdt_regular_service);
4338 CERROR("failed to start regular mdt service: %d\n", rc);
4339 m->mdt_regular_service = NULL;
4345 * readpage service configuration. Parameters have to be adjusted,
4348 memset(&conf, 0, sizeof(conf));
4349 conf = (typeof(conf)) {
4350 .psc_name = LUSTRE_MDT_NAME "_readpage",
4351 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4353 .bc_nbufs = MDS_NBUFS,
4354 .bc_buf_size = MDS_BUFSIZE,
4355 .bc_req_max_size = MDS_MAXREQSIZE,
4356 .bc_rep_max_size = MDS_MAXREPSIZE,
4357 .bc_req_portal = MDS_READPAGE_PORTAL,
4358 .bc_rep_portal = MDC_REPLY_PORTAL,
4361 .tc_thr_name = "mdt_rdpg",
4362 .tc_thr_factor = MDT_RDPG_THR_FACTOR,
4363 .tc_nthrs_init = MDT_RDPG_NTHRS_INIT,
4364 .tc_nthrs_base = MDT_RDPG_NTHRS_BASE,
4365 .tc_nthrs_max = MDT_RDPG_NTHRS_MAX,
4366 .tc_nthrs_user = mds_rdpg_num_threads,
4367 .tc_cpu_affinity = 1,
4368 .tc_ctx_tags = LCT_MD_THREAD,
4371 .cc_pattern = mds_rdpg_num_cpts,
4374 .so_req_handler = mdt_readpage_handle,
4375 .so_req_printer = target_print_req,
4376 .so_hpreq_handler = NULL,
4379 m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
4380 if (IS_ERR(m->mdt_readpage_service)) {
4381 rc = PTR_ERR(m->mdt_readpage_service);
4382 CERROR("failed to start readpage service: %d\n", rc);
4383 m->mdt_readpage_service = NULL;
4385 GOTO(err_mdt_svc, rc);
4389 * setattr service configuration.
4391 * XXX To keep the compatibility with old client(< 2.2), we need to
4392 * preserve this portal for a certain time, it should be removed
4393 * eventually. LU-617.
4395 memset(&conf, 0, sizeof(conf));
4396 conf = (typeof(conf)) {
4397 .psc_name = LUSTRE_MDT_NAME "_setattr",
4398 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4400 .bc_nbufs = MDS_NBUFS,
4401 .bc_buf_size = MDS_BUFSIZE,
4402 .bc_req_max_size = MDS_MAXREQSIZE,
4403 .bc_rep_max_size = MDS_MAXREPSIZE,
4404 .bc_req_portal = MDS_SETATTR_PORTAL,
4405 .bc_rep_portal = MDC_REPLY_PORTAL,
4408 .tc_thr_name = "mdt_attr",
4409 .tc_thr_factor = MDT_SETA_THR_FACTOR,
4410 .tc_nthrs_init = MDT_SETA_NTHRS_INIT,
4411 .tc_nthrs_base = MDT_SETA_NTHRS_BASE,
4412 .tc_nthrs_max = MDT_SETA_NTHRS_MAX,
4413 .tc_nthrs_user = mds_attr_num_threads,
4414 .tc_cpu_affinity = 1,
4415 .tc_ctx_tags = LCT_MD_THREAD,
4418 .cc_pattern = mds_attr_num_cpts,
4421 .so_req_handler = mdt_regular_handle,
4422 .so_req_printer = target_print_req,
4423 .so_hpreq_handler = NULL,
4426 m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4427 if (IS_ERR(m->mdt_setattr_service)) {
4428 rc = PTR_ERR(m->mdt_setattr_service);
4429 CERROR("failed to start setattr service: %d\n", rc);
4430 m->mdt_setattr_service = NULL;
4432 GOTO(err_mdt_svc, rc);
4436 * sequence controller service configuration
4438 memset(&conf, 0, sizeof(conf));
4439 conf = (typeof(conf)) {
4440 .psc_name = LUSTRE_MDT_NAME "_mdsc",
4441 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4443 .bc_nbufs = MDS_NBUFS,
4444 .bc_buf_size = MDS_BUFSIZE,
4445 .bc_req_max_size = SEQ_MAXREQSIZE,
4446 .bc_rep_max_size = SEQ_MAXREPSIZE,
4447 .bc_req_portal = SEQ_CONTROLLER_PORTAL,
4448 .bc_rep_portal = MDC_REPLY_PORTAL,
4451 .tc_thr_name = "mdt_mdsc",
4452 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4453 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4454 .tc_ctx_tags = LCT_MD_THREAD,
4457 .so_req_handler = mdt_mdsc_handle,
4458 .so_req_printer = target_print_req,
4459 .so_hpreq_handler = NULL,
4462 m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4463 if (IS_ERR(m->mdt_mdsc_service)) {
4464 rc = PTR_ERR(m->mdt_mdsc_service);
4465 CERROR("failed to start seq controller service: %d\n", rc);
4466 m->mdt_mdsc_service = NULL;
4468 GOTO(err_mdt_svc, rc);
4472 * metadata sequence server service configuration
4474 memset(&conf, 0, sizeof(conf));
4475 conf = (typeof(conf)) {
4476 .psc_name = LUSTRE_MDT_NAME "_mdss",
4477 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4479 .bc_nbufs = MDS_NBUFS,
4480 .bc_buf_size = MDS_BUFSIZE,
4481 .bc_req_max_size = SEQ_MAXREQSIZE,
4482 .bc_rep_max_size = SEQ_MAXREPSIZE,
4483 .bc_req_portal = SEQ_METADATA_PORTAL,
4484 .bc_rep_portal = MDC_REPLY_PORTAL,
4487 .tc_thr_name = "mdt_mdss",
4488 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4489 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4490 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4493 .so_req_handler = mdt_mdss_handle,
4494 .so_req_printer = target_print_req,
4495 .so_hpreq_handler = NULL,
4498 m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4499 if (IS_ERR(m->mdt_mdss_service)) {
4500 rc = PTR_ERR(m->mdt_mdss_service);
4501 CERROR("failed to start metadata seq server service: %d\n", rc);
4502 m->mdt_mdss_service = NULL;
4504 GOTO(err_mdt_svc, rc);
4508 * Data sequence server service configuration. We want to have really
4509 * cluster-wide sequences space. This is why we start only one sequence
4510 * controller which manages space.
4512 memset(&conf, 0, sizeof(conf));
4513 conf = (typeof(conf)) {
4514 .psc_name = LUSTRE_MDT_NAME "_dtss",
4515 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4517 .bc_nbufs = MDS_NBUFS,
4518 .bc_buf_size = MDS_BUFSIZE,
4519 .bc_req_max_size = SEQ_MAXREQSIZE,
4520 .bc_rep_max_size = SEQ_MAXREPSIZE,
4521 .bc_req_portal = SEQ_DATA_PORTAL,
4522 .bc_rep_portal = OSC_REPLY_PORTAL,
4525 .tc_thr_name = "mdt_dtss",
4526 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4527 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4528 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4531 .so_req_handler = mdt_dtss_handle,
4532 .so_req_printer = target_print_req,
4533 .so_hpreq_handler = NULL,
4536 m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4537 if (IS_ERR(m->mdt_dtss_service)) {
4538 rc = PTR_ERR(m->mdt_dtss_service);
4539 CERROR("failed to start data seq server service: %d\n", rc);
4540 m->mdt_dtss_service = NULL;
4542 GOTO(err_mdt_svc, rc);
4545 /* FLD service start */
4546 memset(&conf, 0, sizeof(conf));
4547 conf = (typeof(conf)) {
4548 .psc_name = LUSTRE_MDT_NAME "_fld",
4549 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4551 .bc_nbufs = MDS_NBUFS,
4552 .bc_buf_size = MDS_BUFSIZE,
4553 .bc_req_max_size = FLD_MAXREQSIZE,
4554 .bc_rep_max_size = FLD_MAXREPSIZE,
4555 .bc_req_portal = FLD_REQUEST_PORTAL,
4556 .bc_rep_portal = MDC_REPLY_PORTAL,
4559 .tc_thr_name = "mdt_fld",
4560 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4561 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4562 .tc_ctx_tags = LCT_DT_THREAD | LCT_MD_THREAD
4565 .so_req_handler = mdt_fld_handle,
4566 .so_req_printer = target_print_req,
4567 .so_hpreq_handler = NULL,
4570 m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4571 if (IS_ERR(m->mdt_fld_service)) {
4572 rc = PTR_ERR(m->mdt_fld_service);
4573 CERROR("failed to start fld service: %d\n", rc);
4574 m->mdt_fld_service = NULL;
4576 GOTO(err_mdt_svc, rc);
4580 * mds-mds service configuration. Separate portal is used to allow
4581 * mds-mds requests be not blocked during recovery.
4583 memset(&conf, 0, sizeof(conf));
4584 conf = (typeof(conf)) {
4585 .psc_name = LUSTRE_MDT_NAME "_mds",
4586 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4588 .bc_nbufs = MDS_NBUFS,
4589 .bc_buf_size = MDS_BUFSIZE,
4590 .bc_req_max_size = MDS_MAXREQSIZE,
4591 .bc_rep_max_size = MDS_MAXREPSIZE,
4592 .bc_req_portal = MDS_MDS_PORTAL,
4593 .bc_rep_portal = MDC_REPLY_PORTAL,
4596 .tc_thr_name = "mdt_mds",
4597 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4598 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4599 .tc_ctx_tags = LCT_MD_THREAD,
4602 .so_req_handler = mdt_xmds_handle,
4603 .so_req_printer = target_print_req,
4604 .so_hpreq_handler = ptlrpc_hpreq_handler,
4607 m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4608 if (IS_ERR(m->mdt_xmds_service)) {
4609 rc = PTR_ERR(m->mdt_xmds_service);
4610 CERROR("failed to start xmds service: %d\n", rc);
4611 m->mdt_xmds_service = NULL;
4613 GOTO(err_mdt_svc, rc);
4619 mdt_stop_ptlrpc_service(m);
4624 static void mdt_stack_fini(const struct lu_env *env,
4625 struct mdt_device *m, struct lu_device *top)
4627 struct obd_device *obd = mdt2obd_dev(m);
4628 struct lustre_cfg_bufs *bufs;
4629 struct lustre_cfg *lcfg;
4630 struct mdt_thread_info *info;
4634 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4635 LASSERT(info != NULL);
4637 lu_dev_del_linkage(top->ld_site, top);
4639 lu_site_purge(env, top->ld_site, -1);
4641 bufs = &info->mti_u.bufs;
4642 /* process cleanup, pass mdt obd name to get obd umount flags */
4643 /* another purpose is to let all layers to release their objects */
4644 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4649 lustre_cfg_bufs_set_string(bufs, 1, flags);
4650 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4652 CERROR("Cannot alloc lcfg!\n");
4656 top->ld_ops->ldo_process_config(env, top, lcfg);
4657 lustre_cfg_free(lcfg);
4659 lu_site_purge(env, top->ld_site, -1);
4661 m->mdt_child = NULL;
4662 m->mdt_bottom = NULL;
4664 obd_disconnect(m->mdt_child_exp);
4665 m->mdt_child_exp = NULL;
4667 obd_disconnect(m->mdt_bottom_exp);
4668 m->mdt_child_exp = NULL;
4671 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4672 const char *next, struct obd_export **exp)
4674 struct obd_connect_data *data = NULL;
4675 struct obd_device *obd;
4679 OBD_ALLOC_PTR(data);
4681 GOTO(out, rc = -ENOMEM);
4683 obd = class_name2obd(next);
4685 CERROR("%s: can't locate next device: %s\n",
4686 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4687 GOTO(out, rc = -ENOTCONN);
4690 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4691 data->ocd_version = LUSTRE_VERSION_CODE;
4693 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4695 CERROR("%s: cannot connect to next dev %s (%d)\n",
4696 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4706 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4707 struct lustre_cfg *cfg)
4709 char *dev = lustre_cfg_string(cfg, 0);
4710 int rc, name_size, uuid_size;
4711 char *name, *uuid, *p;
4712 struct lustre_cfg_bufs *bufs;
4713 struct lustre_cfg *lcfg;
4714 struct obd_device *obd;
4715 struct lustre_profile *lprof;
4716 struct lu_site *site;
4719 /* in 1.8 we had the only device in the stack - MDS.
4720 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4721 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4722 * to generate names and setup MDT, MDD. MDT will be using
4723 * generated name to connect to MDD. for MDD the next device
4724 * will be LOD with name taken from so called "profile" which
4725 * is generated by mount_option line
4727 * 1.8 MGS generates config. commands like this:
4728 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4729 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4730 * 2.0 MGS generates config. commands like this:
4731 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4732 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4733 * 3:lustre-MDT0000-mdtlov 4:f
4735 * we generate MDD name from MDT one, just replacing T with D
4737 * after all the preparations, the logical equivalent will be
4738 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4739 * 3:lustre-MDT0000-mdtlov 4:f
4740 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4741 * 3:lustre-MDD0000 4:f
4743 * notice we build the stack from down to top: MDD first, then MDT */
4745 name_size = MAX_OBD_NAME;
4746 uuid_size = MAX_OBD_NAME;
4748 OBD_ALLOC(name, name_size);
4749 OBD_ALLOC(uuid, uuid_size);
4750 if (name == NULL || uuid == NULL)
4751 GOTO(cleanup_mem, rc = -ENOMEM);
4753 OBD_ALLOC_PTR(bufs);
4755 GOTO(cleanup_mem, rc = -ENOMEM);
4758 p = strstr(name, "-MDT");
4760 GOTO(cleanup_mem, rc = -ENOMEM);
4763 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4765 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4766 if (lprof == NULL || lprof->lp_dt == NULL) {
4767 CERROR("can't find the profile: %s\n",
4768 lustre_cfg_string(cfg, 0));
4769 GOTO(cleanup_mem, rc = -EINVAL);
4772 lustre_cfg_bufs_reset(bufs, name);
4773 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4774 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4775 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4777 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4779 GOTO(free_bufs, rc = -ENOMEM);
4781 rc = class_attach(lcfg);
4783 GOTO(lcfg_cleanup, rc);
4785 obd = class_name2obd(name);
4787 CERROR("Can not find obd %s (%s in config)\n",
4788 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4789 GOTO(class_detach, rc = -EINVAL);
4792 lustre_cfg_free(lcfg);
4794 lustre_cfg_bufs_reset(bufs, name);
4795 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4796 lustre_cfg_bufs_set_string(bufs, 2, dev);
4797 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4799 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4801 rc = class_setup(obd, lcfg);
4803 GOTO(class_detach, rc);
4805 /* connect to MDD we just setup */
4806 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4810 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4812 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4813 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4814 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4815 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4818 /* now connect to bottom OSD */
4819 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4820 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4824 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4827 rc = lu_env_refill((struct lu_env *)env);
4829 CERROR("Failure to refill session: '%d'\n", rc);
4831 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4836 class_detach(obd, lcfg);
4838 lustre_cfg_free(lcfg);
4843 OBD_FREE(name, name_size);
4845 OBD_FREE(uuid, uuid_size);
4850 * setup CONFIG_ORIG context, used to access local config log.
4851 * this may need to be rewrite as part of llog rewrite for lu-api.
4853 static int mdt_obd_llog_setup(struct obd_device *obd,
4854 struct lustre_sb_info *lsi)
4858 LASSERT(obd->obd_fsops == NULL);
4860 obd->obd_fsops = fsfilt_get_ops(lsi->lsi_fstype);
4861 if (IS_ERR(obd->obd_fsops))
4862 return PTR_ERR(obd->obd_fsops);
4864 rc = fsfilt_setup(obd, lsi->lsi_srv_mnt->mnt_sb);
4866 fsfilt_put_ops(obd->obd_fsops);
4870 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
4871 obd->obd_lvfs_ctxt.pwdmnt = lsi->lsi_srv_mnt;
4872 obd->obd_lvfs_ctxt.pwd = lsi->lsi_srv_mnt->mnt_root;
4873 obd->obd_lvfs_ctxt.fs = get_ds();
4875 rc = llog_setup(NULL, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT, obd,
4878 CERROR("llog_setup() failed: %d\n", rc);
4879 fsfilt_put_ops(obd->obd_fsops);
4885 static void mdt_obd_llog_cleanup(struct obd_device *obd)
4887 struct llog_ctxt *ctxt;
4889 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
4891 llog_cleanup(NULL, ctxt);
4893 if (obd->obd_fsops) {
4894 fsfilt_put_ops(obd->obd_fsops);
4895 obd->obd_fsops = NULL;
4899 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4901 struct md_device *next = m->mdt_child;
4902 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4903 struct obd_device *obd = mdt2obd_dev(m);
4906 target_recovery_fini(obd);
4908 ping_evictor_stop();
4910 mdt_stop_ptlrpc_service(m);
4911 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4912 mdt_obd_llog_cleanup(obd);
4913 obd_exports_barrier(obd);
4914 obd_zombie_barrier();
4918 lut_fini(env, &m->mdt_lut);
4919 mdt_fs_cleanup(env, m);
4920 upcall_cache_cleanup(m->mdt_identity_cache);
4921 m->mdt_identity_cache = NULL;
4923 if (m->mdt_namespace != NULL) {
4924 ldlm_namespace_free(m->mdt_namespace, NULL,
4925 d->ld_obd->obd_force);
4926 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4929 cfs_free_nidlist(&m->mdt_nosquash_nids);
4930 if (m->mdt_nosquash_str) {
4931 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4932 m->mdt_nosquash_str = NULL;
4933 m->mdt_nosquash_strlen = 0;
4936 mdt_seq_fini(env, m);
4937 mdt_seq_fini_cli(m);
4938 mdt_fld_fini(env, m);
4939 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4941 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4942 cfs_timer_disarm(&m->mdt_ck_timer);
4943 mdt_ck_thread_stop(m);
4948 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4950 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4952 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4957 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4959 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4960 struct sptlrpc_rule_set tmp_rset;
4963 sptlrpc_rule_set_init(&tmp_rset);
4964 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4966 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4971 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4973 cfs_write_lock(&m->mdt_sptlrpc_lock);
4974 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4975 m->mdt_sptlrpc_rset = tmp_rset;
4976 cfs_write_unlock(&m->mdt_sptlrpc_lock);
4981 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4983 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4984 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4986 struct mdt_thread_info *info;
4987 struct obd_device *obd;
4988 const char *dev = lustre_cfg_string(cfg, 0);
4989 const char *num = lustre_cfg_string(cfg, 2);
4990 struct lustre_mount_info *lmi = NULL;
4991 struct lustre_sb_info *lsi;
4993 struct md_site *mite;
4994 const char *identity_upcall = "NONE";
4995 struct md_device *next;
5001 md_device_init(&m->mdt_md_dev, ldt);
5003 * Environment (env) might be missing mdt_thread_key values at that
5004 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5007 * Usually device allocation path doesn't use module key values, but
5008 * mdt has to do a lot of work here, so allocate key value.
5010 rc = lu_env_refill((struct lu_env *)env);
5014 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5015 LASSERT(info != NULL);
5017 obd = class_name2obd(dev);
5018 LASSERT(obd != NULL);
5020 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
5022 m->mdt_som_conf = 0;
5024 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5025 lmi = server_get_mount(dev);
5027 CERROR("Cannot get mount info for %s!\n", dev);
5030 lsi = s2lsi(lmi->lmi_sb);
5031 /* CMD is supported only in IAM mode */
5033 node_id = simple_strtol(num, NULL, 10);
5034 if (!(lsi->lsi_flags & LDD_F_IAM_DIR) && node_id) {
5035 CERROR("CMD Operation not allowed in IOP mode\n");
5036 GOTO(err_lmi, rc = -EINVAL);
5039 obd->u.obt.obt_magic = OBT_MAGIC;
5042 cfs_rwlock_init(&m->mdt_sptlrpc_lock);
5043 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
5045 cfs_spin_lock_init(&m->mdt_ioepoch_lock);
5046 m->mdt_opts.mo_compat_resname = 0;
5047 m->mdt_opts.mo_mds_capa = 1;
5048 m->mdt_opts.mo_oss_capa = 1;
5049 m->mdt_capa_timeout = CAPA_TIMEOUT;
5050 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5051 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5052 m->mdt_squash_uid = 0;
5053 m->mdt_squash_gid = 0;
5054 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5055 m->mdt_nosquash_str = NULL;
5056 m->mdt_nosquash_strlen = 0;
5057 cfs_init_rwsem(&m->mdt_squash_sem);
5058 cfs_spin_lock_init(&m->mdt_osfs_lock);
5059 m->mdt_osfs_age = cfs_time_shift_64(-1000);
5061 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
5062 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
5063 /* set this lu_device to obd, because error handling need it */
5064 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
5066 /* init the stack */
5067 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5069 CERROR("Can't init device stack, rc %d\n", rc);
5073 s = m->mdt_md_dev.md_lu_dev.ld_site;
5074 mite = &m->mdt_mite;
5075 s->ld_md_site = mite;
5077 /* set server index */
5078 mite->ms_node_id = node_id;
5080 /* failover is the default
5081 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5082 * assumed whose ms_node_id == 0 XXX
5084 obd->obd_replayable = 1;
5085 /* No connection accepted until configurations will finish */
5086 obd->obd_no_conn = 1;
5088 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5089 char *str = lustre_cfg_string(cfg, 4);
5090 if (strchr(str, 'n')) {
5091 CWARN("%s: recovery disabled\n", obd->obd_name);
5092 obd->obd_replayable = 0;
5096 rc = lut_init(env, &m->mdt_lut, obd, m->mdt_bottom);
5098 GOTO(err_fini_stack, rc);
5100 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
5101 LUSTRE_MDT_NAME"-%p", m);
5102 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5103 LDLM_NAMESPACE_SERVER,
5104 LDLM_NAMESPACE_GREEDY,
5106 if (m->mdt_namespace == NULL)
5107 GOTO(err_fini_seq, rc = -ENOMEM);
5109 m->mdt_namespace->ns_lvbp = m;
5110 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5112 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5113 /* set obd_namespace for compatibility with old code */
5114 obd->obd_namespace = m->mdt_namespace;
5116 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5118 rc = mdt_ck_thread_start(m);
5120 GOTO(err_free_ns, rc);
5122 rc = mdt_fs_setup(env, m, obd, lsi);
5126 rc = mdt_obd_llog_setup(obd, lsi);
5128 GOTO(err_fs_cleanup, rc);
5130 mdt_adapt_sptlrpc_conf(obd, 1);
5132 next = m->mdt_child;
5134 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5137 GOTO(err_llog_cleanup, rc);
5139 if (mntopts & MNTOPT_USERXATTR)
5140 m->mdt_opts.mo_user_xattr = 1;
5142 m->mdt_opts.mo_user_xattr = 0;
5144 if (mntopts & MNTOPT_ACL)
5145 m->mdt_opts.mo_acl = 1;
5147 m->mdt_opts.mo_acl = 0;
5149 /* XXX: to support suppgid for ACL, we enable identity_upcall
5150 * by default, otherwise, maybe got unexpected -EACCESS. */
5151 if (m->mdt_opts.mo_acl)
5152 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5154 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
5155 &mdt_identity_upcall_cache_ops);
5156 if (IS_ERR(m->mdt_identity_cache)) {
5157 rc = PTR_ERR(m->mdt_identity_cache);
5158 m->mdt_identity_cache = NULL;
5159 GOTO(err_llog_cleanup, rc);
5162 rc = mdt_procfs_init(m, dev);
5164 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5165 GOTO(err_recovery, rc);
5168 rc = mdt_start_ptlrpc_service(m);
5170 GOTO(err_procfs, rc);
5172 ping_evictor_start();
5174 /* recovery will be started upon mdt_prepare()
5175 * when the whole stack is complete and ready
5176 * to serve the requests */
5178 mdt_init_capa_ctxt(env, m);
5180 /* Reduce the initial timeout on an MDS because it doesn't need such
5181 * a long timeout as an OST does. Adaptive timeouts will adjust this
5182 * value appropriately. */
5183 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5184 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5188 ping_evictor_stop();
5189 mdt_stop_ptlrpc_service(m);
5193 target_recovery_fini(obd);
5194 upcall_cache_cleanup(m->mdt_identity_cache);
5195 m->mdt_identity_cache = NULL;
5197 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5198 mdt_obd_llog_cleanup(obd);
5200 mdt_fs_cleanup(env, m);
5202 cfs_timer_disarm(&m->mdt_ck_timer);
5203 mdt_ck_thread_stop(m);
5205 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5206 obd->obd_namespace = m->mdt_namespace = NULL;
5208 mdt_seq_fini(env, m);
5209 mdt_fld_fini(env, m);
5210 lut_fini(env, &m->mdt_lut);
5212 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5215 server_put_mount(dev, lmi->lmi_mnt);
5219 /* For interoperability between 1.8 and 2.0. */
5220 static struct cfg_interop_param mdt_interop_param[] = {
5221 { "mdt.group_upcall", NULL },
5222 { "mdt.quota_type", "mdd.quota_type" },
5223 { "mdt.rootsquash", "mdt.root_squash" },
5224 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5228 /* used by MGS to process specific configurations */
5229 static int mdt_process_config(const struct lu_env *env,
5230 struct lu_device *d, struct lustre_cfg *cfg)
5232 struct mdt_device *m = mdt_dev(d);
5233 struct md_device *md_next = m->mdt_child;
5234 struct lu_device *next = md2lu_dev(md_next);
5238 switch (cfg->lcfg_command) {
5240 struct lprocfs_static_vars lvars;
5241 struct obd_device *obd = d->ld_obd;
5243 /* For interoperability between 1.8 and 2.0. */
5244 struct cfg_interop_param *ptr = NULL;
5245 struct lustre_cfg *old_cfg = NULL;
5248 param = lustre_cfg_string(cfg, 1);
5249 if (param == NULL) {
5250 CERROR("param is empty\n");
5255 ptr = class_find_old_param(param, mdt_interop_param);
5257 if (ptr->new_param == NULL) {
5258 CWARN("For 1.8 interoperability, skip this %s."
5259 " It is obsolete.\n", ptr->old_param);
5263 CWARN("Found old param %s, changed it to %s.\n",
5264 ptr->old_param, ptr->new_param);
5267 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5274 lprocfs_mdt_init_vars(&lvars);
5275 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5277 if (rc > 0 || rc == -ENOSYS)
5278 /* we don't understand; pass it on */
5279 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5281 if (old_cfg != NULL)
5282 lustre_cfg_free(cfg);
5288 * Add mdc hook to get first MDT uuid and connect it to
5289 * ls->controller to use for seq manager.
5291 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5293 CERROR("Can't add mdc, rc %d\n", rc);
5295 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
5298 /* others are passed further */
5299 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5305 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5306 const struct lu_object_header *hdr,
5307 struct lu_device *d)
5309 struct mdt_object *mo;
5313 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
5315 struct lu_object *o;
5316 struct lu_object_header *h;
5318 o = &mo->mot_obj.mo_lu;
5319 h = &mo->mot_header;
5320 lu_object_header_init(h);
5321 lu_object_init(o, h, d);
5322 lu_object_add_top(h, o);
5323 o->lo_ops = &mdt_obj_ops;
5324 cfs_mutex_init(&mo->mot_ioepoch_mutex);
5325 cfs_mutex_init(&mo->mot_lov_mutex);
5331 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5332 const struct lu_object_conf *unused)
5334 struct mdt_device *d = mdt_dev(o->lo_dev);
5335 struct lu_device *under;
5336 struct lu_object *below;
5340 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5341 PFID(lu_object_fid(o)));
5343 under = &d->mdt_child->md_lu_dev;
5344 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5345 if (below != NULL) {
5346 lu_object_add(o, below);
5353 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5355 struct mdt_object *mo = mdt_obj(o);
5356 struct lu_object_header *h;
5360 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5361 PFID(lu_object_fid(o)));
5364 lu_object_header_fini(h);
5365 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5370 static int mdt_object_print(const struct lu_env *env, void *cookie,
5371 lu_printer_t p, const struct lu_object *o)
5373 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5374 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5375 "flags="LPX64", epochcount=%d, writecount=%d)",
5376 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5377 mdto->mot_ioepoch_count, mdto->mot_writecount);
5380 static int mdt_prepare(const struct lu_env *env,
5381 struct lu_device *pdev,
5382 struct lu_device *cdev)
5384 struct mdt_device *mdt = mdt_dev(cdev);
5385 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5386 struct obd_device *obd = cdev->ld_obd;
5393 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5397 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5401 rc = mdt_fld_init(env, obd->obd_name, mdt);
5405 rc = mdt_seq_init(env, obd->obd_name, mdt);
5409 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5410 &mdt->mdt_md_root_fid);
5414 LASSERT(!cfs_test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5415 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5416 cfs_set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5417 LASSERT(obd->obd_no_conn);
5418 cfs_spin_lock(&obd->obd_dev_lock);
5419 obd->obd_no_conn = 0;
5420 cfs_spin_unlock(&obd->obd_dev_lock);
5422 if (obd->obd_recovering == 0)
5423 mdt_postrecov(env, mdt);
5428 static const struct lu_device_operations mdt_lu_ops = {
5429 .ldo_object_alloc = mdt_object_alloc,
5430 .ldo_process_config = mdt_process_config,
5431 .ldo_prepare = mdt_prepare,
5434 static const struct lu_object_operations mdt_obj_ops = {
5435 .loo_object_init = mdt_object_init,
5436 .loo_object_free = mdt_object_free,
5437 .loo_object_print = mdt_object_print
5440 static int mdt_obd_set_info_async(const struct lu_env *env,
5441 struct obd_export *exp,
5442 __u32 keylen, void *key,
5443 __u32 vallen, void *val,
5444 struct ptlrpc_request_set *set)
5446 struct obd_device *obd = exp->exp_obd;
5452 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5453 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5460 /* mds_connect_internal */
5461 static int mdt_connect_internal(struct obd_export *exp,
5462 struct mdt_device *mdt,
5463 struct obd_connect_data *data)
5466 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5467 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5469 /* If no known bits (which should not happen, probably,
5470 as everybody should support LOOKUP and UPDATE bits at least)
5471 revert to compat mode with plain locks. */
5472 if (!data->ocd_ibits_known &&
5473 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5474 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5476 if (!mdt->mdt_opts.mo_acl)
5477 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5479 if (!mdt->mdt_opts.mo_user_xattr)
5480 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5482 if (!mdt->mdt_som_conf)
5483 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5485 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5486 data->ocd_brw_size = min(data->ocd_brw_size,
5487 (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5488 if (data->ocd_brw_size == 0) {
5489 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5490 " ocd_version: %x ocd_grant: %d "
5491 "ocd_index: %u ocd_brw_size is "
5492 "unexpectedly zero, network data "
5493 "corruption? Refusing connection of this"
5495 exp->exp_obd->obd_name,
5496 exp->exp_client_uuid.uuid,
5497 exp, data->ocd_connect_flags, data->ocd_version,
5498 data->ocd_grant, data->ocd_index);
5503 cfs_spin_lock(&exp->exp_lock);
5504 exp->exp_connect_flags = data->ocd_connect_flags;
5505 cfs_spin_unlock(&exp->exp_lock);
5506 data->ocd_version = LUSTRE_VERSION_CODE;
5507 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5511 if (mdt->mdt_opts.mo_acl &&
5512 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
5513 CWARN("%s: MDS requires ACL support but client does not\n",
5514 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5519 if ((exp->exp_connect_flags & OBD_CONNECT_FID) == 0) {
5520 CWARN("%s: MDS requires FID support, but client not\n",
5521 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5525 if (mdt->mdt_som_conf && !exp_connect_som(exp) &&
5526 !(exp->exp_connect_flags & OBD_CONNECT_MDS_MDS)) {
5527 CWARN("%s: MDS has SOM enabled, but client does not support "
5528 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5535 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5536 struct obd_export *exp,
5537 struct ptlrpc_request *req)
5539 struct sptlrpc_flavor flvr;
5542 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5543 cfs_read_lock(&mdt->mdt_sptlrpc_lock);
5544 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5548 cfs_read_unlock(&mdt->mdt_sptlrpc_lock);
5550 cfs_spin_lock(&exp->exp_lock);
5552 exp->exp_sp_peer = req->rq_sp_from;
5553 exp->exp_flvr = flvr;
5555 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5556 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5557 CERROR("unauthorized rpc flavor %x from %s, "
5558 "expect %x\n", req->rq_flvr.sf_rpc,
5559 libcfs_nid2str(req->rq_peer.nid),
5560 exp->exp_flvr.sf_rpc);
5564 cfs_spin_unlock(&exp->exp_lock);
5566 if (exp->exp_sp_peer != req->rq_sp_from) {
5567 CERROR("RPC source %s doesn't match %s\n",
5568 sptlrpc_part2name(req->rq_sp_from),
5569 sptlrpc_part2name(exp->exp_sp_peer));
5572 rc = sptlrpc_target_export_check(exp, req);
5579 /* mds_connect copy */
5580 static int mdt_obd_connect(const struct lu_env *env,
5581 struct obd_export **exp, struct obd_device *obd,
5582 struct obd_uuid *cluuid,
5583 struct obd_connect_data *data,
5586 struct mdt_thread_info *info;
5587 struct obd_export *lexp;
5588 struct lustre_handle conn = { 0 };
5589 struct mdt_device *mdt;
5590 struct ptlrpc_request *req;
5594 LASSERT(env != NULL);
5595 if (!exp || !obd || !cluuid)
5598 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5599 req = info->mti_pill->rc_req;
5600 mdt = mdt_dev(obd->obd_lu_dev);
5603 * first, check whether the stack is ready to handle requests
5604 * XXX: probably not very appropriate method is used now
5605 * at some point we should find a better one
5607 if (!cfs_test_bit(MDT_FL_SYNCED, &mdt->mdt_state)) {
5608 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5611 cfs_set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5614 rc = class_connect(&conn, obd, cluuid);
5618 lexp = class_conn2export(&conn);
5619 LASSERT(lexp != NULL);
5621 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5625 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5626 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5628 rc = mdt_connect_internal(lexp, mdt, data);
5630 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5633 info->mti_exp = lexp;
5634 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5635 rc = lut_client_new(env, lexp);
5637 mdt_export_stats_init(obd, lexp, localdata);
5642 class_disconnect(lexp);
5651 static int mdt_obd_reconnect(const struct lu_env *env,
5652 struct obd_export *exp, struct obd_device *obd,
5653 struct obd_uuid *cluuid,
5654 struct obd_connect_data *data,
5657 struct mdt_thread_info *info;
5658 struct mdt_device *mdt;
5659 struct ptlrpc_request *req;
5663 if (exp == NULL || obd == NULL || cluuid == NULL)
5666 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5667 req = info->mti_pill->rc_req;
5668 mdt = mdt_dev(obd->obd_lu_dev);
5670 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5674 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5676 mdt_export_stats_init(obd, exp, localdata);
5681 static int mdt_export_cleanup(struct obd_export *exp)
5683 struct mdt_export_data *med = &exp->exp_mdt_data;
5684 struct obd_device *obd = exp->exp_obd;
5685 struct mdt_device *mdt;
5686 struct mdt_thread_info *info;
5688 CFS_LIST_HEAD(closing_list);
5689 struct mdt_file_data *mfd, *n;
5693 cfs_spin_lock(&med->med_open_lock);
5694 while (!cfs_list_empty(&med->med_open_head)) {
5695 cfs_list_t *tmp = med->med_open_head.next;
5696 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5698 /* Remove mfd handle so it can't be found again.
5699 * We are consuming the mfd_list reference here. */
5700 class_handle_unhash(&mfd->mfd_handle);
5701 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5703 cfs_spin_unlock(&med->med_open_lock);
5704 mdt = mdt_dev(obd->obd_lu_dev);
5705 LASSERT(mdt != NULL);
5707 rc = lu_env_init(&env, LCT_MD_THREAD);
5711 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5712 LASSERT(info != NULL);
5713 memset(info, 0, sizeof *info);
5714 info->mti_env = &env;
5715 info->mti_mdt = mdt;
5716 info->mti_exp = exp;
5718 if (!cfs_list_empty(&closing_list)) {
5719 struct md_attr *ma = &info->mti_attr;
5721 /* Close any open files (which may also cause orphan unlinking). */
5722 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5723 cfs_list_del_init(&mfd->mfd_list);
5724 ma->ma_need = ma->ma_valid = 0;
5725 /* Don't unlink orphan on failover umount, LU-184 */
5726 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5727 ma->ma_valid = MA_FLAGS;
5728 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5730 mdt_mfd_close(info, mfd);
5733 info->mti_mdt = NULL;
5734 /* cleanup client slot early */
5735 /* Do not erase record for recoverable client. */
5736 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5737 lut_client_del(&env, exp);
5743 static int mdt_obd_disconnect(struct obd_export *exp)
5749 class_export_get(exp);
5751 rc = server_disconnect_export(exp);
5753 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5755 rc = mdt_export_cleanup(exp);
5756 class_export_put(exp);
5760 /* FIXME: Can we avoid using these two interfaces? */
5761 static int mdt_init_export(struct obd_export *exp)
5763 struct mdt_export_data *med = &exp->exp_mdt_data;
5767 CFS_INIT_LIST_HEAD(&med->med_open_head);
5768 cfs_spin_lock_init(&med->med_open_lock);
5769 cfs_mutex_init(&med->med_idmap_mutex);
5770 med->med_idmap = NULL;
5771 cfs_spin_lock(&exp->exp_lock);
5772 exp->exp_connecting = 1;
5773 cfs_spin_unlock(&exp->exp_lock);
5775 /* self-export doesn't need client data and ldlm initialization */
5776 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5777 &exp->exp_client_uuid)))
5780 rc = lut_client_alloc(exp);
5784 rc = ldlm_init_export(exp);
5791 lut_client_free(exp);
5793 CERROR("%s: Failed to initialize export: rc = %d\n",
5794 exp->exp_obd->obd_name, rc);
5798 static int mdt_destroy_export(struct obd_export *exp)
5802 if (exp_connect_rmtclient(exp))
5803 mdt_cleanup_idmap(&exp->exp_mdt_data);
5805 target_destroy_export(exp);
5806 /* destroy can be called from failed obd_setup, so
5807 * checking uuid is safer than obd_self_export */
5808 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5809 &exp->exp_client_uuid)))
5812 ldlm_destroy_export(exp);
5813 lut_client_free(exp);
5815 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5816 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5821 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5822 void *val, int vallen)
5824 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5825 struct getinfo_fid2path *fpout, *fpin;
5828 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5831 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5832 lustre_swab_fid2path(fpin);
5834 memcpy(fpout, fpin, sizeof(*fpin));
5835 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5838 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5842 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5843 struct getinfo_fid2path *fp)
5845 struct mdt_object *obj;
5849 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5850 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5852 if (!fid_is_sane(&fp->gf_fid))
5855 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5856 if (obj == NULL || IS_ERR(obj)) {
5857 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5862 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5868 mdt_object_put(env, obj);
5869 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5870 PFID(&fp->gf_fid), rc);
5874 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5875 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5876 mdt_object_put(env, obj);
5881 static int mdt_get_info(struct mdt_thread_info *info)
5883 struct ptlrpc_request *req = mdt_info_req(info);
5891 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5893 CDEBUG(D_IOCTL, "No GETINFO key");
5896 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5899 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5900 if (vallen == NULL) {
5901 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5905 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5907 rc = req_capsule_server_pack(info->mti_pill);
5908 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5909 if (valout == NULL) {
5910 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5914 if (KEY_IS(KEY_FID2PATH))
5915 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5919 lustre_msg_set_status(req->rq_repmsg, rc);
5924 /* Pass the ioc down */
5925 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5926 unsigned int cmd, int len, void *data)
5928 struct lu_context ioctl_session;
5929 struct md_device *next = mdt->mdt_child;
5933 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5936 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5937 lu_context_enter(&ioctl_session);
5938 env->le_ses = &ioctl_session;
5940 LASSERT(next->md_ops->mdo_iocontrol);
5941 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5943 lu_context_exit(&ioctl_session);
5944 lu_context_fini(&ioctl_session);
5948 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5950 struct obd_ioctl_data *data = karg;
5951 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5953 struct mdt_object *obj;
5954 struct mdt_lock_handle *lh;
5958 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5959 if (!fid_is_sane(fid))
5962 lh = &mti->mti_lh[MDT_LH_PARENT];
5963 mdt_lock_reg_init(lh, LCK_CR);
5965 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5967 RETURN(PTR_ERR(obj));
5969 rc = mdt_object_exists(obj);
5973 * before calling version get the correct MDS should be
5974 * fid, this is error to find remote object here
5976 CERROR("nonlocal object "DFID"\n", PFID(fid));
5977 } else if (rc == 0) {
5978 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5981 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5982 *(__u64 *)data->ioc_inlbuf2 = version;
5985 mdt_object_unlock_put(mti, obj, lh, 1);
5989 /* ioctls on obd dev */
5990 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5991 void *karg, void *uarg)
5994 struct obd_device *obd = exp->exp_obd;
5995 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5996 struct dt_device *dt = mdt->mdt_bottom;
6000 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6001 rc = lu_env_init(&env, LCT_MD_THREAD);
6007 rc = mdt_device_sync(&env, mdt);
6009 case OBD_IOC_SET_READONLY:
6010 rc = dt->dd_ops->dt_ro(&env, dt);
6012 case OBD_IOC_ABORT_RECOVERY:
6013 CERROR("Aborting recovery for device %s\n", obd->obd_name);
6014 target_stop_recovery_thread(obd);
6017 case OBD_IOC_CHANGELOG_REG:
6018 case OBD_IOC_CHANGELOG_DEREG:
6019 case OBD_IOC_CHANGELOG_CLEAR:
6020 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6022 case OBD_IOC_START_LFSCK:
6023 case OBD_IOC_STOP_LFSCK: {
6024 struct md_device *next = mdt->mdt_child;
6025 struct obd_ioctl_data *data = karg;
6027 if (unlikely(data == NULL)) {
6032 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
6037 case OBD_IOC_GET_OBJ_VERSION: {
6038 struct mdt_thread_info *mti;
6039 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6040 memset(mti, 0, sizeof *mti);
6041 mti->mti_env = &env;
6045 rc = mdt_ioc_version_get(mti, karg);
6049 CERROR("Not supported cmd = %d for device %s\n",
6050 cmd, obd->obd_name);
6058 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6060 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6064 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6068 int mdt_obd_postrecov(struct obd_device *obd)
6073 rc = lu_env_init(&env, LCT_MD_THREAD);
6076 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6082 * Send a copytool req to a client
6083 * Note this sends a request RPC from a server (MDT) to a client (MDC),
6084 * backwards of normal comms.
6086 int mdt_hsm_copytool_send(struct obd_export *exp)
6089 struct hsm_action_list *hal;
6090 struct hsm_action_item *hai;
6094 CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
6095 libcfs_nid2str(exp->exp_connection->c_peer.nid));
6097 len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
6098 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
6103 lh->kuc_magic = KUC_MAGIC;
6104 lh->kuc_transport = KUC_TRANSPORT_HSM;
6105 lh->kuc_msgtype = HMT_ACTION_LIST;
6106 lh->kuc_msglen = len;
6108 hal = (struct hsm_action_list *)(lh + 1);
6109 hal->hal_version = HAL_VERSION;
6110 hal->hal_archive_num = 1;
6111 obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
6114 /* mock up an action list */
6116 hai = hai_zero(hal);
6117 hai->hai_action = HSMA_ARCHIVE;
6118 hai->hai_fid.f_oid = 0xA00A;
6119 hai->hai_len = sizeof(*hai);
6120 hai = hai_next(hai);
6121 hai->hai_action = HSMA_RESTORE;
6122 hai->hai_fid.f_oid = 0xB00B;
6123 hai->hai_len = sizeof(*hai);
6125 /* Uses the ldlm reverse import; this rpc will be seen by
6126 the ldlm_callback_handler */
6127 rc = do_set_info_async(exp->exp_imp_reverse,
6128 LDLM_SET_INFO, LUSTRE_OBD_VERSION,
6129 sizeof(KEY_HSM_COPYTOOL_SEND),
6130 KEY_HSM_COPYTOOL_SEND,
6138 static struct obd_ops mdt_obd_device_ops = {
6139 .o_owner = THIS_MODULE,
6140 .o_set_info_async = mdt_obd_set_info_async,
6141 .o_connect = mdt_obd_connect,
6142 .o_reconnect = mdt_obd_reconnect,
6143 .o_disconnect = mdt_obd_disconnect,
6144 .o_init_export = mdt_init_export,
6145 .o_destroy_export = mdt_destroy_export,
6146 .o_iocontrol = mdt_iocontrol,
6147 .o_postrecov = mdt_obd_postrecov,
6150 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6151 struct lu_device *d)
6153 struct mdt_device *m = mdt_dev(d);
6160 static struct lu_device *mdt_device_free(const struct lu_env *env,
6161 struct lu_device *d)
6163 struct mdt_device *m = mdt_dev(d);
6166 md_device_fini(&m->mdt_md_dev);
6171 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6172 struct lu_device_type *t,
6173 struct lustre_cfg *cfg)
6175 struct lu_device *l;
6176 struct mdt_device *m;
6182 l = &m->mdt_md_dev.md_lu_dev;
6183 rc = mdt_init0(env, m, t, cfg);
6185 mdt_device_free(env, l);
6190 l = ERR_PTR(-ENOMEM);
6194 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6195 LU_KEY_INIT(mdt, struct mdt_thread_info);
6197 static void mdt_key_fini(const struct lu_context *ctx,
6198 struct lu_context_key *key, void* data)
6200 struct mdt_thread_info *info = data;
6202 if (info->mti_big_lmm) {
6203 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6204 info->mti_big_lmm = NULL;
6205 info->mti_big_lmmsize = 0;
6210 /* context key: mdt_thread_key */
6211 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6213 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
6215 return md_ucred(info->mti_env);
6219 * Enable/disable COS (Commit On Sharing).
6221 * Set/Clear the COS flag in mdt options.
6223 * \param mdt mdt device
6224 * \param val 0 disables COS, other values enable COS
6226 void mdt_enable_cos(struct mdt_device *mdt, int val)
6231 mdt->mdt_opts.mo_cos = !!val;
6232 rc = lu_env_init(&env, LCT_LOCAL);
6233 if (unlikely(rc != 0)) {
6234 CWARN("lu_env initialization failed with rc = %d,"
6235 "cannot sync\n", rc);
6238 mdt_device_sync(&env, mdt);
6243 * Check COS (Commit On Sharing) status.
6245 * Return COS flag status.
6247 * \param mdt mdt device
6249 int mdt_cos_is_enabled(struct mdt_device *mdt)
6251 return mdt->mdt_opts.mo_cos != 0;
6254 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
6255 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
6257 static struct lu_device_type_operations mdt_device_type_ops = {
6258 .ldto_init = mdt_type_init,
6259 .ldto_fini = mdt_type_fini,
6261 .ldto_start = mdt_type_start,
6262 .ldto_stop = mdt_type_stop,
6264 .ldto_device_alloc = mdt_device_alloc,
6265 .ldto_device_free = mdt_device_free,
6266 .ldto_device_fini = mdt_device_fini
6269 static struct lu_device_type mdt_device_type = {
6270 .ldt_tags = LU_DEVICE_MD,
6271 .ldt_name = LUSTRE_MDT_NAME,
6272 .ldt_ops = &mdt_device_type_ops,
6273 .ldt_ctx_tags = LCT_MD_THREAD
6276 static int __init mdt_mod_init(void)
6278 struct lprocfs_static_vars lvars;
6281 rc = lu_kmem_init(mdt_caches);
6285 if (mdt_num_threads != 0 && mds_num_threads == 0) {
6286 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
6287 "use mds_num_threads instead or unset both for"
6288 "dynamic thread startup\n");
6289 mds_num_threads = mdt_num_threads;
6292 lprocfs_mdt_init_vars(&lvars);
6293 rc = class_register_type(&mdt_obd_device_ops, NULL,
6294 lvars.module_vars, LUSTRE_MDT_NAME,
6298 lu_kmem_fini(mdt_caches);
6302 static void __exit mdt_mod_exit(void)
6304 class_unregister_type(LUSTRE_MDT_NAME);
6305 lu_kmem_fini(mdt_caches);
6309 #define DEF_HNDL(prefix, base, suffix, flags, opc, fn, fmt) \
6310 [prefix ## _ ## opc - prefix ## _ ## base] = { \
6312 .mh_fail_id = OBD_FAIL_ ## prefix ## _ ## opc ## suffix, \
6313 .mh_opc = prefix ## _ ## opc, \
6314 .mh_flags = flags, \
6319 #define DEF_MDT_HNDL(flags, name, fn, fmt) \
6320 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, fmt)
6322 #define DEF_SEQ_HNDL(flags, name, fn, fmt) \
6323 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, fmt)
6325 #define DEF_FLD_HNDL(flags, name, fn, fmt) \
6326 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, fmt)
6328 * Request with a format known in advance
6330 #define DEF_MDT_HNDL_F(flags, name, fn) \
6331 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, &RQF_MDS_ ## name)
6333 #define DEF_SEQ_HNDL_F(flags, name, fn) \
6334 DEF_HNDL(SEQ, QUERY, _NET, flags, name, fn, &RQF_SEQ_ ## name)
6336 #define DEF_FLD_HNDL_F(flags, name, fn) \
6337 DEF_HNDL(FLD, QUERY, _NET, flags, name, fn, &RQF_FLD_ ## name)
6339 * Request with a format we do not yet know
6341 #define DEF_MDT_HNDL_0(flags, name, fn) \
6342 DEF_HNDL(MDS, GETATTR, _NET, flags, name, fn, NULL)
6344 static struct mdt_handler mdt_mds_ops[] = {
6345 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
6346 DEF_MDT_HNDL_F(0, DISCONNECT, mdt_disconnect),
6347 DEF_MDT_HNDL (0, SET_INFO, mdt_set_info,
6349 DEF_MDT_HNDL_F(0, GET_INFO, mdt_get_info),
6350 DEF_MDT_HNDL_F(0 |HABEO_REFERO, GETSTATUS, mdt_getstatus),
6351 DEF_MDT_HNDL_F(HABEO_CORPUS, GETATTR, mdt_getattr),
6352 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, GETATTR_NAME, mdt_getattr_name),
6353 DEF_MDT_HNDL_F(HABEO_CORPUS, GETXATTR, mdt_getxattr),
6354 DEF_MDT_HNDL_F(0 |HABEO_REFERO, STATFS, mdt_statfs),
6355 DEF_MDT_HNDL_F(0 |MUTABOR, REINT, mdt_reint),
6356 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
6357 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
6358 DEF_MDT_HNDL_F(0 |HABEO_REFERO, PIN, mdt_pin),
6359 DEF_MDT_HNDL_0(0, SYNC, mdt_sync),
6360 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
6361 DEF_MDT_HNDL_F(0, QUOTACHECK, mdt_quotacheck),
6362 DEF_MDT_HNDL_F(0, QUOTACTL, mdt_quotactl)
6365 #define DEF_OBD_HNDL(flags, name, fn) \
6366 DEF_HNDL(OBD, PING, _NET, flags, name, fn, NULL)
6369 static struct mdt_handler mdt_obd_ops[] = {
6370 DEF_OBD_HNDL(0, PING, mdt_obd_ping),
6371 DEF_OBD_HNDL(0, LOG_CANCEL, mdt_obd_log_cancel),
6372 DEF_OBD_HNDL(0, QC_CALLBACK, mdt_obd_qc_callback),
6373 DEF_OBD_HNDL(0, IDX_READ, mdt_obd_idx_read)
6376 #define DEF_DLM_HNDL_0(flags, name, fn) \
6377 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, NULL)
6378 #define DEF_DLM_HNDL_F(flags, name, fn) \
6379 DEF_HNDL(LDLM, ENQUEUE, , flags, name, fn, &RQF_LDLM_ ## name)
6381 static struct mdt_handler mdt_dlm_ops[] = {
6382 DEF_DLM_HNDL_F(HABEO_CLAVIS, ENQUEUE, mdt_enqueue),
6383 DEF_DLM_HNDL_0(HABEO_CLAVIS, CONVERT, mdt_convert),
6384 DEF_DLM_HNDL_0(0, BL_CALLBACK, mdt_bl_callback),
6385 DEF_DLM_HNDL_0(0, CP_CALLBACK, mdt_cp_callback)
6388 #define DEF_LLOG_HNDL(flags, name, fn) \
6389 DEF_HNDL(LLOG, ORIGIN_HANDLE_CREATE, _NET, flags, name, fn, NULL)
6391 static struct mdt_handler mdt_llog_ops[] = {
6392 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CREATE, mdt_llog_create),
6393 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
6394 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
6395 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_WRITE_REC, NULL),
6396 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_CLOSE, NULL),
6397 DEF_LLOG_HNDL(0, ORIGIN_CONNECT, NULL),
6398 DEF_LLOG_HNDL(0, CATINFO, NULL),
6399 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
6400 DEF_LLOG_HNDL(0, ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
6403 #define DEF_SEC_CTX_HNDL(name, fn) \
6404 DEF_HNDL(SEC_CTX, INIT, _NET, 0, name, fn, NULL)
6406 static struct mdt_handler mdt_sec_ctx_ops[] = {
6407 DEF_SEC_CTX_HNDL(INIT, mdt_sec_ctx_handle),
6408 DEF_SEC_CTX_HNDL(INIT_CONT, mdt_sec_ctx_handle),
6409 DEF_SEC_CTX_HNDL(FINI, mdt_sec_ctx_handle)
6412 static struct mdt_opc_slice mdt_regular_handlers[] = {
6414 .mos_opc_start = MDS_GETATTR,
6415 .mos_opc_end = MDS_LAST_OPC,
6416 .mos_hs = mdt_mds_ops
6419 .mos_opc_start = OBD_PING,
6420 .mos_opc_end = OBD_LAST_OPC,
6421 .mos_hs = mdt_obd_ops
6424 .mos_opc_start = LDLM_ENQUEUE,
6425 .mos_opc_end = LDLM_LAST_OPC,
6426 .mos_hs = mdt_dlm_ops
6429 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6430 .mos_opc_end = LLOG_LAST_OPC,
6431 .mos_hs = mdt_llog_ops
6434 .mos_opc_start = SEC_CTX_INIT,
6435 .mos_opc_end = SEC_LAST_OPC,
6436 .mos_hs = mdt_sec_ctx_ops
6443 static struct mdt_handler mdt_readpage_ops[] = {
6444 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
6445 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, READPAGE, mdt_readpage),
6446 #ifdef HAVE_SPLIT_SUPPORT
6447 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, WRITEPAGE, mdt_writepage),
6451 * XXX: this is ugly and should be fixed one day, see mdc_close() for
6452 * detailed comments. --umka
6454 DEF_MDT_HNDL_F(HABEO_CORPUS, CLOSE, mdt_close),
6455 DEF_MDT_HNDL_F(HABEO_CORPUS, DONE_WRITING, mdt_done_writing),
6458 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6460 .mos_opc_start = MDS_GETATTR,
6461 .mos_opc_end = MDS_LAST_OPC,
6462 .mos_hs = mdt_readpage_ops
6465 .mos_opc_start = OBD_FIRST_OPC,
6466 .mos_opc_end = OBD_LAST_OPC,
6467 .mos_hs = mdt_obd_ops
6474 static struct mdt_handler mdt_xmds_ops[] = {
6475 DEF_MDT_HNDL_F(0, CONNECT, mdt_connect),
6476 DEF_MDT_HNDL_F(HABEO_CORPUS , GETATTR, mdt_getattr),
6477 DEF_MDT_HNDL_F(0 | MUTABOR , REINT, mdt_reint),
6478 DEF_MDT_HNDL_F(HABEO_CORPUS|HABEO_REFERO, IS_SUBDIR, mdt_is_subdir),
6481 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6483 .mos_opc_start = MDS_GETATTR,
6484 .mos_opc_end = MDS_LAST_OPC,
6485 .mos_hs = mdt_xmds_ops
6488 .mos_opc_start = OBD_PING,
6489 .mos_opc_end = OBD_LAST_OPC,
6490 .mos_hs = mdt_obd_ops
6493 .mos_opc_start = SEC_CTX_INIT,
6494 .mos_opc_end = SEC_LAST_OPC,
6495 .mos_hs = mdt_sec_ctx_ops
6502 static struct mdt_handler mdt_seq_ops[] = {
6503 DEF_SEQ_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))seq_query)
6506 static struct mdt_opc_slice mdt_seq_handlers[] = {
6508 .mos_opc_start = SEQ_QUERY,
6509 .mos_opc_end = SEQ_LAST_OPC,
6510 .mos_hs = mdt_seq_ops
6517 static struct mdt_handler mdt_fld_ops[] = {
6518 DEF_FLD_HNDL_F(0, QUERY, (int (*)(struct mdt_thread_info *))fld_query)
6521 static struct mdt_opc_slice mdt_fld_handlers[] = {
6523 .mos_opc_start = FLD_QUERY,
6524 .mos_opc_end = FLD_LAST_OPC,
6525 .mos_hs = mdt_fld_ops
6532 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6533 MODULE_DESCRIPTION("Lustre Meta-data Target ("LUSTRE_MDT_NAME")");
6534 MODULE_LICENSE("GPL");
6536 cfs_module(mdt, "0.2.0", mdt_mod_init, mdt_mod_exit);