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>
70 #include <lustre_quota.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_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1029 * Don't check capability at all, because rename might getattr for
1030 * remote obj, and at that time no capability is available.
1032 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1033 rc = mdt_getattr_internal(info, obj, 0);
1034 if (reqbody->valid & OBD_MD_FLRMTPERM)
1035 mdt_exit_ucred(info);
1038 mdt_client_compatibility(info);
1039 rc2 = mdt_fix_reply(info);
1045 static int mdt_is_subdir(struct mdt_thread_info *info)
1047 struct mdt_object *o = info->mti_object;
1048 struct req_capsule *pill = info->mti_pill;
1049 const struct mdt_body *body = info->mti_body;
1050 struct mdt_body *repbody;
1056 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1059 * We save last checked parent fid to @repbody->fid1 for remote
1062 LASSERT(fid_is_sane(&body->fid2));
1063 LASSERT(mdt_object_exists(o) > 0);
1064 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1065 &body->fid2, &repbody->fid1);
1066 if (rc == 0 || rc == -EREMOTE)
1067 repbody->valid |= OBD_MD_FLID;
1072 static int mdt_raw_lookup(struct mdt_thread_info *info,
1073 struct mdt_object *parent,
1074 const struct lu_name *lname,
1075 struct ldlm_reply *ldlm_rep)
1077 struct md_object *next = mdt_object_child(info->mti_object);
1078 const struct mdt_body *reqbody = info->mti_body;
1079 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1080 struct mdt_body *repbody;
1084 if (reqbody->valid != OBD_MD_FLID)
1087 LASSERT(!info->mti_cross_ref);
1089 /* Only got the fid of this obj by name */
1090 fid_zero(child_fid);
1091 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1094 /* XXX is raw_lookup possible as intent operation? */
1097 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1100 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1102 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1105 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1106 repbody->fid1 = *child_fid;
1107 repbody->valid = OBD_MD_FLID;
1113 * UPDATE lock should be taken against parent, and be release before exit;
1114 * child_bits lock should be taken against child, and be returned back:
1115 * (1)normal request should release the child lock;
1116 * (2)intent request will grant the lock to client.
1118 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1119 struct mdt_lock_handle *lhc,
1121 struct ldlm_reply *ldlm_rep)
1123 struct ptlrpc_request *req = mdt_info_req(info);
1124 struct mdt_body *reqbody = NULL;
1125 struct mdt_object *parent = info->mti_object;
1126 struct mdt_object *child;
1127 struct md_object *next = mdt_object_child(parent);
1128 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1129 struct lu_name *lname = NULL;
1130 const char *name = NULL;
1132 struct mdt_lock_handle *lhp = NULL;
1133 struct ldlm_lock *lock;
1134 struct ldlm_res_id *res_id;
1141 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1142 LASSERT(ergo(is_resent,
1143 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1145 LASSERT(parent != NULL);
1146 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1148 RETURN(err_serious(-EFAULT));
1150 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1152 if (!info->mti_cross_ref) {
1154 * XXX: Check for "namelen == 0" is for getattr by fid
1155 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1156 * that is the name must contain at least one character and
1157 * the terminating '\0'
1160 reqbody = req_capsule_client_get(info->mti_pill,
1162 if (unlikely(reqbody == NULL))
1163 RETURN(err_serious(-EFAULT));
1165 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1166 RETURN(err_serious(-EINVAL));
1169 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1171 PFID(mdt_object_fid(parent)),
1172 PFID(&reqbody->fid2), ldlm_rep);
1174 lname = mdt_name(info->mti_env, (char *)name, namelen);
1175 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1176 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1180 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1182 rc = mdt_object_exists(parent);
1183 if (unlikely(rc == 0)) {
1184 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1185 &parent->mot_obj.mo_lu,
1186 "Parent doesn't exist!\n");
1188 } else if (!info->mti_cross_ref) {
1189 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
1190 PFID(mdt_object_fid(parent)));
1193 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1201 if (info->mti_cross_ref) {
1202 /* Only getattr on the child. Parent is on another node. */
1203 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1205 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1206 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1209 /* Do not take lock for resent case. */
1210 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1211 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1212 lhc->mlh_reg_lh.cookie);
1213 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1214 &lock->l_resource->lr_name));
1215 LDLM_LOCK_PUT(lock);
1218 mdt_lock_handle_init(lhc);
1219 mdt_lock_reg_init(lhc, LCK_PR);
1222 * Object's name is on another MDS, no lookup lock is
1223 * needed here but update is.
1225 child_bits &= ~MDS_INODELOCK_LOOKUP;
1226 child_bits |= MDS_INODELOCK_UPDATE;
1228 rc = mdt_object_lock(info, child, lhc, child_bits,
1232 /* Finally, we can get attr for child. */
1233 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1235 rc = mdt_getattr_internal(info, child, 0);
1236 if (unlikely(rc != 0))
1237 mdt_object_unlock(info, child, lhc, 1);
1243 /* step 1: lock parent only if parent is a directory */
1244 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1245 lhp = &info->mti_lh[MDT_LH_PARENT];
1246 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1247 rc = mdt_object_lock(info, parent, lhp,
1248 MDS_INODELOCK_UPDATE,
1250 if (unlikely(rc != 0))
1254 /* step 2: lookup child's fid by name */
1255 fid_zero(child_fid);
1256 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1261 mdt_set_disposition(info, ldlm_rep,
1263 GOTO(out_parent, rc);
1265 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1267 *child_fid = reqbody->fid2;
1268 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1272 *step 3: find the child object by fid & lock it.
1273 * regardless if it is local or remote.
1275 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1277 if (unlikely(IS_ERR(child)))
1278 GOTO(out_parent, rc = PTR_ERR(child));
1280 /* Do not take lock for resent case. */
1281 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1282 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1283 lhc->mlh_reg_lh.cookie);
1285 res_id = &lock->l_resource->lr_name;
1286 if (!fid_res_name_eq(mdt_object_fid(child),
1287 &lock->l_resource->lr_name)) {
1288 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1289 &lock->l_resource->lr_name),
1290 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1291 (unsigned long)res_id->name[0],
1292 (unsigned long)res_id->name[1],
1293 (unsigned long)res_id->name[2],
1294 PFID(mdt_object_fid(parent)));
1295 CWARN("Although resent, but still not get child lock"
1296 "parent:"DFID" child:"DFID"\n",
1297 PFID(mdt_object_fid(parent)),
1298 PFID(mdt_object_fid(child)));
1299 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1300 LDLM_LOCK_PUT(lock);
1303 LDLM_LOCK_PUT(lock);
1307 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1308 mdt_lock_handle_init(lhc);
1309 if (child_bits == MDS_INODELOCK_LAYOUT)
1310 mdt_lock_reg_init(lhc, LCK_CR);
1312 mdt_lock_reg_init(lhc, LCK_PR);
1314 if (mdt_object_exists(child) == 0) {
1315 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1316 &child->mot_obj.mo_lu,
1317 "Object doesn't exist!\n");
1318 GOTO(out_child, rc = -ENOENT);
1321 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1322 struct md_attr *ma = &info->mti_attr;
1325 ma->ma_need = MA_INODE;
1326 rc = mdt_attr_get_complex(info, child, ma);
1327 if (unlikely(rc != 0))
1328 GOTO(out_child, rc);
1330 /* layout lock is used only on regular files */
1331 if ((ma->ma_valid & MA_INODE) &&
1332 (ma->ma_attr.la_valid & LA_MODE) &&
1333 !S_ISREG(ma->ma_attr.la_mode))
1334 child_bits &= ~MDS_INODELOCK_LAYOUT;
1336 /* If the file has not been changed for some time, we
1337 * return not only a LOOKUP lock, but also an UPDATE
1338 * lock and this might save us RPC on later STAT. For
1339 * directories, it also let negative dentry starts
1340 * working for this dir. */
1341 if (ma->ma_valid & MA_INODE &&
1342 ma->ma_attr.la_valid & LA_CTIME &&
1343 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1344 ma->ma_attr.la_ctime < cfs_time_current_sec())
1345 child_bits |= MDS_INODELOCK_UPDATE;
1348 rc = mdt_object_lock(info, child, lhc, child_bits,
1351 if (unlikely(rc != 0))
1352 GOTO(out_child, rc);
1355 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1356 /* Get MA_SOM attributes if update lock is given. */
1358 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1359 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1362 /* finally, we can get attr for child. */
1363 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1364 rc = mdt_getattr_internal(info, child, ma_need);
1365 if (unlikely(rc != 0)) {
1366 mdt_object_unlock(info, child, lhc, 1);
1368 /* Debugging code. */
1369 res_id = &lock->l_resource->lr_name;
1370 LDLM_DEBUG(lock, "Returning lock to client");
1371 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1372 &lock->l_resource->lr_name),
1373 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1374 (unsigned long)res_id->name[0],
1375 (unsigned long)res_id->name[1],
1376 (unsigned long)res_id->name[2],
1377 PFID(mdt_object_fid(child)));
1378 mdt_pack_size2body(info, child);
1381 LDLM_LOCK_PUT(lock);
1385 mdt_object_put(info->mti_env, child);
1388 mdt_object_unlock(info, parent, lhp, 1);
1392 /* normal handler: should release the child lock */
1393 static int mdt_getattr_name(struct mdt_thread_info *info)
1395 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1396 struct mdt_body *reqbody;
1397 struct mdt_body *repbody;
1401 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1402 LASSERT(reqbody != NULL);
1403 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1404 LASSERT(repbody != NULL);
1406 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1407 repbody->eadatasize = 0;
1408 repbody->aclsize = 0;
1410 rc = mdt_init_ucred(info, reqbody);
1412 GOTO(out_shrink, rc);
1414 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1415 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1416 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1417 lhc->mlh_reg_lh.cookie = 0;
1419 mdt_exit_ucred(info);
1422 mdt_client_compatibility(info);
1423 rc2 = mdt_fix_reply(info);
1429 static const struct lu_device_operations mdt_lu_ops;
1431 static int lu_device_is_mdt(struct lu_device *d)
1433 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1436 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1437 void *karg, void *uarg);
1439 static int mdt_set_info(struct mdt_thread_info *info)
1441 struct ptlrpc_request *req = mdt_info_req(info);
1444 int keylen, vallen, rc = 0;
1447 rc = req_capsule_server_pack(info->mti_pill);
1451 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1453 DEBUG_REQ(D_HA, req, "no set_info key");
1457 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1460 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1462 DEBUG_REQ(D_HA, req, "no set_info val");
1466 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1469 /* Swab any part of val you need to here */
1470 if (KEY_IS(KEY_READ_ONLY)) {
1472 lustre_msg_set_status(req->rq_repmsg, 0);
1474 cfs_spin_lock(&req->rq_export->exp_lock);
1476 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1478 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1479 cfs_spin_unlock(&req->rq_export->exp_lock);
1481 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1482 struct changelog_setinfo *cs =
1483 (struct changelog_setinfo *)val;
1484 if (vallen != sizeof(*cs)) {
1485 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1488 if (ptlrpc_req_need_swab(req)) {
1489 __swab64s(&cs->cs_recno);
1490 __swab32s(&cs->cs_id);
1493 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1495 lustre_msg_set_status(req->rq_repmsg, rc);
1504 * Top-level handler for MDT connection requests.
1506 static int mdt_connect(struct mdt_thread_info *info)
1509 struct obd_connect_data *reply;
1510 struct obd_export *exp;
1511 struct ptlrpc_request *req = mdt_info_req(info);
1513 rc = target_handle_connect(req);
1515 return err_serious(rc);
1517 LASSERT(req->rq_export != NULL);
1518 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1519 rc = mdt_init_sec_level(info);
1521 obd_disconnect(class_export_get(req->rq_export));
1525 /* To avoid exposing partially initialized connection flags, changes up
1526 * to this point have been staged in reply->ocd_connect_flags. Now that
1527 * connection handling has completed successfully, atomically update
1528 * the connect flags in the shared export data structure. LU-1623 */
1529 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1530 exp = req->rq_export;
1531 cfs_spin_lock(&exp->exp_lock);
1532 exp->exp_connect_flags = reply->ocd_connect_flags;
1533 cfs_spin_unlock(&exp->exp_lock);
1535 rc = mdt_init_idmap(info);
1537 obd_disconnect(class_export_get(req->rq_export));
1542 static int mdt_disconnect(struct mdt_thread_info *info)
1547 rc = target_handle_disconnect(mdt_info_req(info));
1549 rc = err_serious(rc);
1553 static int mdt_sendpage(struct mdt_thread_info *info,
1554 struct lu_rdpg *rdpg, int nob)
1556 struct ptlrpc_request *req = mdt_info_req(info);
1557 struct obd_export *exp = req->rq_export;
1558 struct ptlrpc_bulk_desc *desc;
1559 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1566 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1571 if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1572 /* old client requires reply size in it's PAGE_SIZE,
1573 * which is rdpg->rp_count */
1574 nob = rdpg->rp_count;
1576 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1577 i++, tmpcount -= tmpsize) {
1578 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1579 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1582 LASSERT(desc->bd_nob == nob);
1583 rc = target_bulk_io(exp, desc, lwi);
1584 ptlrpc_free_bulk_pin(desc);
1588 static int mdt_readpage(struct mdt_thread_info *info)
1590 struct mdt_object *object = info->mti_object;
1591 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1592 struct mdt_body *reqbody;
1593 struct mdt_body *repbody;
1598 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1599 RETURN(err_serious(-ENOMEM));
1601 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1602 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1603 if (reqbody == NULL || repbody == NULL)
1604 RETURN(err_serious(-EFAULT));
1607 * prepare @rdpg before calling lower layers and transfer itself. Here
1608 * reqbody->size contains offset of where to start to read and
1609 * reqbody->nlink contains number bytes to read.
1611 rdpg->rp_hash = reqbody->size;
1612 if (rdpg->rp_hash != reqbody->size) {
1613 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1614 rdpg->rp_hash, reqbody->size);
1618 rdpg->rp_attrs = reqbody->mode;
1619 if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1620 rdpg->rp_attrs |= LUDA_64BITHASH;
1621 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1622 PTLRPC_MAX_BRW_SIZE);
1623 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1625 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1626 if (rdpg->rp_pages == NULL)
1629 for (i = 0; i < rdpg->rp_npages; ++i) {
1630 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1631 if (rdpg->rp_pages[i] == NULL)
1632 GOTO(free_rdpg, rc = -ENOMEM);
1635 /* call lower layers to fill allocated pages with directory data */
1636 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1638 GOTO(free_rdpg, rc);
1640 /* send pages to client */
1641 rc = mdt_sendpage(info, rdpg, rc);
1646 for (i = 0; i < rdpg->rp_npages; i++)
1647 if (rdpg->rp_pages[i] != NULL)
1648 cfs_free_page(rdpg->rp_pages[i]);
1649 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1651 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1657 static int mdt_reint_internal(struct mdt_thread_info *info,
1658 struct mdt_lock_handle *lhc,
1661 struct req_capsule *pill = info->mti_pill;
1662 struct mdt_body *repbody;
1667 rc = mdt_reint_unpack(info, op);
1669 CERROR("Can't unpack reint, rc %d\n", rc);
1670 RETURN(err_serious(rc));
1673 /* for replay (no_create) lmm is not needed, client has it already */
1674 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1675 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1676 info->mti_rr.rr_eadatalen);
1678 /* llog cookies are always 0, the field is kept for compatibility */
1679 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1680 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1682 rc = req_capsule_server_pack(pill);
1684 CERROR("Can't pack response, rc %d\n", rc);
1685 RETURN(err_serious(rc));
1688 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1689 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1691 repbody->eadatasize = 0;
1692 repbody->aclsize = 0;
1695 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1697 /* for replay no cookkie / lmm need, because client have this already */
1698 if (info->mti_spec.no_create)
1699 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1700 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1702 rc = mdt_init_ucred_reint(info);
1704 GOTO(out_shrink, rc);
1706 rc = mdt_fix_attr_ucred(info, op);
1708 GOTO(out_ucred, rc = err_serious(rc));
1710 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1711 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1712 GOTO(out_ucred, rc);
1714 rc = mdt_reint_rec(info, lhc);
1717 mdt_exit_ucred(info);
1719 mdt_client_compatibility(info);
1720 rc2 = mdt_fix_reply(info);
1726 static long mdt_reint_opcode(struct mdt_thread_info *info,
1727 const struct req_format **fmt)
1729 struct mdt_rec_reint *rec;
1732 opc = err_serious(-EFAULT);
1733 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1735 opc = rec->rr_opcode;
1736 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1737 if (opc < REINT_MAX && fmt[opc] != NULL)
1738 req_capsule_extend(info->mti_pill, fmt[opc]);
1740 CERROR("Unsupported opc: %ld\n", opc);
1741 opc = err_serious(opc);
1747 static int mdt_reint(struct mdt_thread_info *info)
1752 static const struct req_format *reint_fmts[REINT_MAX] = {
1753 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1754 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1755 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1756 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1757 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1758 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1759 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1764 opc = mdt_reint_opcode(info, reint_fmts);
1767 * No lock possible here from client to pass it to reint code
1770 rc = mdt_reint_internal(info, NULL, opc);
1775 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1779 /* this should sync the whole device */
1780 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1782 struct dt_device *dt = mdt->mdt_bottom;
1786 rc = dt->dd_ops->dt_sync(env, dt);
1790 /* this should sync this object */
1791 static int mdt_object_sync(struct mdt_thread_info *info)
1793 struct md_object *next;
1797 if (!mdt_object_exists(info->mti_object)) {
1798 CWARN("Non existing object "DFID"!\n",
1799 PFID(mdt_object_fid(info->mti_object)));
1802 next = mdt_object_child(info->mti_object);
1803 rc = mo_object_sync(info->mti_env, next);
1808 static int mdt_sync(struct mdt_thread_info *info)
1810 struct ptlrpc_request *req = mdt_info_req(info);
1811 struct req_capsule *pill = info->mti_pill;
1812 struct mdt_body *body;
1816 /* The fid may be zero, so we req_capsule_set manually */
1817 req_capsule_set(pill, &RQF_MDS_SYNC);
1819 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1821 RETURN(err_serious(-EINVAL));
1823 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1824 RETURN(err_serious(-ENOMEM));
1826 if (fid_seq(&body->fid1) == 0) {
1827 /* sync the whole device */
1828 rc = req_capsule_server_pack(pill);
1830 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1832 rc = err_serious(rc);
1834 /* sync an object */
1835 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1837 rc = mdt_object_sync(info);
1839 const struct lu_fid *fid;
1840 struct lu_attr *la = &info->mti_attr.ma_attr;
1842 info->mti_attr.ma_need = MA_INODE;
1843 info->mti_attr.ma_valid = 0;
1844 rc = mdt_attr_get_complex(info, info->mti_object,
1847 body = req_capsule_server_get(pill,
1849 fid = mdt_object_fid(info->mti_object);
1850 mdt_pack_attr2body(info, body, la, fid);
1854 rc = err_serious(rc);
1857 mdt_counter_incr(req, LPROC_MDT_SYNC);
1863 * Quotacheck handler.
1864 * in-kernel quotacheck isn't supported any more.
1866 static int mdt_quotacheck(struct mdt_thread_info *info)
1868 struct obd_quotactl *oqctl;
1872 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1874 RETURN(err_serious(-EPROTO));
1876 rc = req_capsule_server_pack(info->mti_pill);
1878 RETURN(err_serious(rc));
1880 /* deprecated, not used any more */
1881 RETURN(-EOPNOTSUPP);
1885 * Handle quota control requests to consult current usage/limit, but also
1886 * to configure quota enforcement
1888 static int mdt_quotactl(struct mdt_thread_info *info)
1890 struct obd_export *exp = info->mti_exp;
1891 struct req_capsule *pill = info->mti_pill;
1892 struct obd_quotactl *oqctl, *repoqc;
1894 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1897 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1899 RETURN(err_serious(-EPROTO));
1901 rc = req_capsule_server_pack(pill);
1903 RETURN(err_serious(rc));
1905 switch (oqctl->qc_cmd) {
1907 case LUSTRE_Q_INVALIDATE:
1908 case LUSTRE_Q_FINVALIDATE:
1912 /* deprecated, not used any more */
1913 RETURN(-EOPNOTSUPP);
1914 /* master quotactl */
1920 RETURN(-EOPNOTSUPP);
1921 /* slave quotactl */
1926 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1930 /* map uid/gid for remote client */
1932 if (exp_connect_rmtclient(exp)) {
1933 struct lustre_idmap_table *idmap;
1935 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1937 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1938 oqctl->qc_cmd != Q_GETINFO))
1941 if (oqctl->qc_type == USRQUOTA)
1942 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1944 else if (oqctl->qc_type == GRPQUOTA)
1945 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1950 if (id == CFS_IDMAP_NOTFOUND) {
1951 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1956 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1958 RETURN(err_serious(-EFAULT));
1960 if (oqctl->qc_id != id)
1961 swap(oqctl->qc_id, id);
1963 switch (oqctl->qc_cmd) {
1969 /* forward quotactl request to QMT */
1970 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
1975 /* slave quotactl */
1976 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
1981 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1985 if (oqctl->qc_id != id)
1986 swap(oqctl->qc_id, id);
1993 * OBD PING and other handlers.
1995 static int mdt_obd_ping(struct mdt_thread_info *info)
2000 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2002 rc = target_handle_ping(mdt_info_req(info));
2004 rc = err_serious(rc);
2009 * OBD_IDX_READ handler
2011 static int mdt_obd_idx_read(struct mdt_thread_info *info)
2013 struct mdt_device *mdt = info->mti_mdt;
2014 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2015 struct idx_info *req_ii, *rep_ii;
2019 memset(rdpg, 0, sizeof(*rdpg));
2020 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2022 /* extract idx_info buffer from request & reply */
2023 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2024 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2025 RETURN(err_serious(-EPROTO));
2027 rc = req_capsule_server_pack(info->mti_pill);
2029 RETURN(err_serious(rc));
2031 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2033 RETURN(err_serious(-EFAULT));
2034 rep_ii->ii_magic = IDX_INFO_MAGIC;
2036 /* extract hash to start with */
2037 rdpg->rp_hash = req_ii->ii_hash_start;
2039 /* extract requested attributes */
2040 rdpg->rp_attrs = req_ii->ii_attrs;
2042 /* check that fid packed in request is valid and supported */
2043 if (!fid_is_sane(&req_ii->ii_fid))
2045 rep_ii->ii_fid = req_ii->ii_fid;
2048 rep_ii->ii_flags = req_ii->ii_flags;
2050 /* compute number of pages to allocate, ii_count is the number of 4KB
2052 if (req_ii->ii_count <= 0)
2053 GOTO(out, rc = -EFAULT);
2054 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2055 PTLRPC_MAX_BRW_SIZE);
2056 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2058 /* allocate pages to store the containers */
2059 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2060 if (rdpg->rp_pages == NULL)
2061 GOTO(out, rc = -ENOMEM);
2062 for (i = 0; i < rdpg->rp_npages; i++) {
2063 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2064 if (rdpg->rp_pages[i] == NULL)
2065 GOTO(out, rc = -ENOMEM);
2068 /* populate pages with key/record pairs */
2069 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2073 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2074 "asked %d > %d\n", rc, rdpg->rp_count);
2076 /* send pages to client */
2077 rc = mdt_sendpage(info, rdpg, rc);
2081 if (rdpg->rp_pages) {
2082 for (i = 0; i < rdpg->rp_npages; i++)
2083 if (rdpg->rp_pages[i])
2084 cfs_free_page(rdpg->rp_pages[i]);
2085 OBD_FREE(rdpg->rp_pages,
2086 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2091 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
2093 return err_serious(-EOPNOTSUPP);
2096 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
2098 return err_serious(-EOPNOTSUPP);
2106 /** clone llog ctxt from child (mdd)
2107 * This allows remote llog (replicator) access.
2108 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2109 * context was originally set up, or we can handle them directly.
2110 * I choose the latter, but that means I need any llog
2111 * contexts set up by child to be accessable by the mdt. So we clone the
2112 * context into our context list here.
2114 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2117 struct md_device *next = mdt->mdt_child;
2118 struct llog_ctxt *ctxt;
2121 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2124 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2125 if (rc || ctxt == NULL) {
2129 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2131 CERROR("Can't set mdt ctxt %d\n", rc);
2136 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2137 struct mdt_device *mdt, int idx)
2139 struct llog_ctxt *ctxt;
2141 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2144 /* Put once for the get we just did, and once for the clone */
2145 llog_ctxt_put(ctxt);
2146 llog_ctxt_put(ctxt);
2150 static int mdt_llog_create(struct mdt_thread_info *info)
2154 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2155 rc = llog_origin_handle_open(mdt_info_req(info));
2156 return (rc < 0 ? err_serious(rc) : rc);
2159 static int mdt_llog_destroy(struct mdt_thread_info *info)
2163 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2164 rc = llog_origin_handle_destroy(mdt_info_req(info));
2165 return (rc < 0 ? err_serious(rc) : rc);
2168 static int mdt_llog_read_header(struct mdt_thread_info *info)
2172 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2173 rc = llog_origin_handle_read_header(mdt_info_req(info));
2174 return (rc < 0 ? err_serious(rc) : rc);
2177 static int mdt_llog_next_block(struct mdt_thread_info *info)
2181 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2182 rc = llog_origin_handle_next_block(mdt_info_req(info));
2183 return (rc < 0 ? err_serious(rc) : rc);
2186 static int mdt_llog_prev_block(struct mdt_thread_info *info)
2190 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2191 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2192 return (rc < 0 ? err_serious(rc) : rc);
2199 static struct ldlm_callback_suite cbs = {
2200 .lcs_completion = ldlm_server_completion_ast,
2201 .lcs_blocking = ldlm_server_blocking_ast,
2202 .lcs_glimpse = ldlm_server_glimpse_ast
2205 static int mdt_enqueue(struct mdt_thread_info *info)
2207 struct ptlrpc_request *req;
2211 * info->mti_dlm_req already contains swapped and (if necessary)
2212 * converted dlm request.
2214 LASSERT(info->mti_dlm_req != NULL);
2216 req = mdt_info_req(info);
2217 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2218 req, info->mti_dlm_req, &cbs);
2219 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2220 return rc ? err_serious(rc) : req->rq_status;
2223 static int mdt_convert(struct mdt_thread_info *info)
2226 struct ptlrpc_request *req;
2228 LASSERT(info->mti_dlm_req);
2229 req = mdt_info_req(info);
2230 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2231 return rc ? err_serious(rc) : req->rq_status;
2234 static int mdt_bl_callback(struct mdt_thread_info *info)
2236 CERROR("bl callbacks should not happen on MDS\n");
2238 return err_serious(-EOPNOTSUPP);
2241 static int mdt_cp_callback(struct mdt_thread_info *info)
2243 CERROR("cp callbacks should not happen on MDS\n");
2245 return err_serious(-EOPNOTSUPP);
2249 * sec context handlers
2251 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2255 rc = mdt_handle_idmap(info);
2258 struct ptlrpc_request *req = mdt_info_req(info);
2261 opc = lustre_msg_get_opc(req->rq_reqmsg);
2262 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2263 sptlrpc_svc_ctx_invalidate(req);
2266 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2272 * quota request handlers
2274 static int mdt_quota_dqacq(struct mdt_thread_info *info)
2276 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2281 RETURN(err_serious(-EOPNOTSUPP));
2283 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2287 static struct mdt_object *mdt_obj(struct lu_object *o)
2289 LASSERT(lu_device_is_mdt(o->lo_dev));
2290 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2293 struct mdt_object *mdt_object_new(const struct lu_env *env,
2294 struct mdt_device *d,
2295 const struct lu_fid *f)
2297 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2298 struct lu_object *o;
2299 struct mdt_object *m;
2302 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2303 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2304 if (unlikely(IS_ERR(o)))
2305 m = (struct mdt_object *)o;
2311 struct mdt_object *mdt_object_find(const struct lu_env *env,
2312 struct mdt_device *d,
2313 const struct lu_fid *f)
2315 struct lu_object *o;
2316 struct mdt_object *m;
2319 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2320 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2321 if (unlikely(IS_ERR(o)))
2322 m = (struct mdt_object *)o;
2329 * Asyncronous commit for mdt device.
2331 * Pass asynchonous commit call down the MDS stack.
2333 * \param env environment
2334 * \param mdt the mdt device
2336 static void mdt_device_commit_async(const struct lu_env *env,
2337 struct mdt_device *mdt)
2339 struct dt_device *dt = mdt->mdt_bottom;
2342 rc = dt->dd_ops->dt_commit_async(env, dt);
2343 if (unlikely(rc != 0))
2344 CWARN("async commit start failed with rc = %d", rc);
2348 * Mark the lock as "synchonous".
2350 * Mark the lock to deffer transaction commit to the unlock time.
2352 * \param lock the lock to mark as "synchonous"
2354 * \see mdt_is_lock_sync
2355 * \see mdt_save_lock
2357 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2359 lock->l_ast_data = (void*)1;
2363 * Check whehter the lock "synchonous" or not.
2365 * \param lock the lock to check
2366 * \retval 1 the lock is "synchonous"
2367 * \retval 0 the lock isn't "synchronous"
2369 * \see mdt_set_lock_sync
2370 * \see mdt_save_lock
2372 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2374 return lock->l_ast_data != NULL;
2378 * Blocking AST for mdt locks.
2380 * Starts transaction commit if in case of COS lock conflict or
2381 * deffers such a commit to the mdt_save_lock.
2383 * \param lock the lock which blocks a request or cancelling lock
2384 * \param desc unused
2385 * \param data unused
2386 * \param flag indicates whether this cancelling or blocking callback
2388 * \see ldlm_blocking_ast_nocheck
2390 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2391 void *data, int flag)
2393 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2394 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2398 if (flag == LDLM_CB_CANCELING)
2400 lock_res_and_lock(lock);
2401 if (lock->l_blocking_ast != mdt_blocking_ast) {
2402 unlock_res_and_lock(lock);
2405 if (mdt_cos_is_enabled(mdt) &&
2406 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2407 lock->l_blocking_lock != NULL &&
2408 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2409 mdt_set_lock_sync(lock);
2411 rc = ldlm_blocking_ast_nocheck(lock);
2413 /* There is no lock conflict if l_blocking_lock == NULL,
2414 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2415 * when the last reference to a local lock was released */
2416 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2419 rc = lu_env_init(&env, LCT_LOCAL);
2420 if (unlikely(rc != 0))
2421 CWARN("lu_env initialization failed with rc = %d,"
2422 "cannot start asynchronous commit\n", rc);
2424 mdt_device_commit_async(&env, mdt);
2430 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2431 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2433 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2434 ldlm_policy_data_t *policy = &info->mti_policy;
2435 struct ldlm_res_id *res_id = &info->mti_res_id;
2439 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2440 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2441 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2442 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2444 if (mdt_object_exists(o) < 0) {
2445 if (locality == MDT_CROSS_LOCK) {
2446 /* cross-ref object fix */
2447 ibits &= ~MDS_INODELOCK_UPDATE;
2448 ibits |= MDS_INODELOCK_LOOKUP;
2450 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2451 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2453 /* No PDO lock on remote object */
2454 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2457 if (lh->mlh_type == MDT_PDO_LOCK) {
2458 /* check for exists after object is locked */
2459 if (mdt_object_exists(o) == 0) {
2460 /* Non-existent object shouldn't have PDO lock */
2463 /* Non-dir object shouldn't have PDO lock */
2464 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2469 memset(policy, 0, sizeof(*policy));
2470 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2473 * Take PDO lock on whole directory and build correct @res_id for lock
2474 * on part of directory.
2476 if (lh->mlh_pdo_hash != 0) {
2477 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2478 mdt_lock_pdo_mode(info, o, lh);
2479 if (lh->mlh_pdo_mode != LCK_NL) {
2481 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2482 * is never going to be sent to client and we do not
2483 * want it slowed down due to possible cancels.
2485 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2486 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2487 policy, res_id, LDLM_FL_ATOMIC_CB,
2488 &info->mti_exp->exp_handle.h_cookie);
2494 * Finish res_id initializing by name hash marking part of
2495 * directory which is taking modification.
2497 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2500 policy->l_inodebits.bits = ibits;
2503 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2504 * going to be sent to client. If it is - mdt_intent_policy() path will
2505 * fix it up and turn FL_LOCAL flag off.
2507 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2508 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2509 &info->mti_exp->exp_handle.h_cookie);
2511 mdt_object_unlock(info, o, lh, 1);
2512 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2513 lh->mlh_pdo_hash != 0 &&
2514 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2515 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2522 * Save a lock within request object.
2524 * Keep the lock referenced until whether client ACK or transaction
2525 * commit happens or release the lock immediately depending on input
2526 * parameters. If COS is ON, a write lock is converted to COS lock
2529 * \param info thead info object
2530 * \param h lock handle
2531 * \param mode lock mode
2532 * \param decref force immediate lock releasing
2535 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2536 ldlm_mode_t mode, int decref)
2540 if (lustre_handle_is_used(h)) {
2541 if (decref || !info->mti_has_trans ||
2542 !(mode & (LCK_PW | LCK_EX))){
2543 mdt_fid_unlock(h, mode);
2545 struct mdt_device *mdt = info->mti_mdt;
2546 struct ldlm_lock *lock = ldlm_handle2lock(h);
2547 struct ptlrpc_request *req = mdt_info_req(info);
2550 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2552 CDEBUG(D_HA, "request = %p reply state = %p"
2553 " transno = "LPD64"\n",
2554 req, req->rq_reply_state, req->rq_transno);
2555 if (mdt_cos_is_enabled(mdt)) {
2557 ldlm_lock_downgrade(lock, LCK_COS);
2560 ptlrpc_save_lock(req, h, mode, no_ack);
2561 if (mdt_is_lock_sync(lock)) {
2562 CDEBUG(D_HA, "found sync-lock,"
2563 " async commit started\n");
2564 mdt_device_commit_async(info->mti_env,
2567 LDLM_LOCK_PUT(lock);
2576 * Unlock mdt object.
2578 * Immeditely release the regular lock and the PDO lock or save the
2579 * lock in reqeuest and keep them referenced until client ACK or
2580 * transaction commit.
2582 * \param info thread info object
2583 * \param o mdt object
2584 * \param lh mdt lock handle referencing regular and PDO locks
2585 * \param decref force immediate lock releasing
2587 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2588 struct mdt_lock_handle *lh, int decref)
2592 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2593 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2598 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2599 const struct lu_fid *f,
2600 struct mdt_lock_handle *lh,
2603 struct mdt_object *o;
2605 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2609 rc = mdt_object_lock(info, o, lh, ibits,
2612 mdt_object_put(info->mti_env, o);
2619 void mdt_object_unlock_put(struct mdt_thread_info * info,
2620 struct mdt_object * o,
2621 struct mdt_lock_handle *lh,
2624 mdt_object_unlock(info, o, lh, decref);
2625 mdt_object_put(info->mti_env, o);
2628 static struct mdt_handler *mdt_handler_find(__u32 opc,
2629 struct mdt_opc_slice *supported)
2631 struct mdt_opc_slice *s;
2632 struct mdt_handler *h;
2635 for (s = supported; s->mos_hs != NULL; s++) {
2636 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2637 h = s->mos_hs + (opc - s->mos_opc_start);
2638 if (likely(h->mh_opc != 0))
2639 LASSERTF(h->mh_opc == opc,
2640 "opcode mismatch %d != %d\n",
2643 h = NULL; /* unsupported opc */
2650 static int mdt_lock_resname_compat(struct mdt_device *m,
2651 struct ldlm_request *req)
2653 /* XXX something... later. */
2657 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2659 /* XXX something... later. */
2664 * Generic code handling requests that have struct mdt_body passed in:
2666 * - extract mdt_body from request and save it in @info, if present;
2668 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2671 * - if HABEO_CORPUS flag is set for this request type check whether object
2672 * actually exists on storage (lu_object_exists()).
2675 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2677 const struct mdt_body *body;
2678 struct mdt_object *obj;
2679 const struct lu_env *env;
2680 struct req_capsule *pill;
2684 env = info->mti_env;
2685 pill = info->mti_pill;
2687 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2691 if (!(body->valid & OBD_MD_FLID))
2694 if (!fid_is_sane(&body->fid1)) {
2695 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2700 * Do not get size or any capa fields before we check that request
2701 * contains capa actually. There are some requests which do not, for
2702 * instance MDS_IS_SUBDIR.
2704 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2705 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2706 mdt_set_capainfo(info, 0, &body->fid1,
2707 req_capsule_client_get(pill, &RMF_CAPA1));
2709 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2711 if ((flags & HABEO_CORPUS) &&
2712 !mdt_object_exists(obj)) {
2713 mdt_object_put(env, obj);
2714 /* for capability renew ENOENT will be handled in
2716 if (body->valid & OBD_MD_FLOSSCAPA)
2721 info->mti_object = obj;
2730 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2732 struct req_capsule *pill = info->mti_pill;
2736 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2737 rc = mdt_body_unpack(info, flags);
2741 if (rc == 0 && (flags & HABEO_REFERO)) {
2743 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2744 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2745 info->mti_body->eadatasize);
2746 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2747 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2750 rc = req_capsule_server_pack(pill);
2755 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2757 struct md_device *next = m->mdt_child;
2759 return next->md_ops->mdo_init_capa_ctxt(env, next,
2760 m->mdt_opts.mo_mds_capa,
2761 m->mdt_capa_timeout,
2767 * Invoke handler for this request opc. Also do necessary preprocessing
2768 * (according to handler ->mh_flags), and post-processing (setting of
2769 * ->last_{xid,committed}).
2771 static int mdt_req_handle(struct mdt_thread_info *info,
2772 struct mdt_handler *h, struct ptlrpc_request *req)
2774 int rc, serious = 0;
2779 LASSERT(h->mh_act != NULL);
2780 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2781 LASSERT(current->journal_info == NULL);
2784 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2785 * to put same checks into handlers like mdt_close(), mdt_reint(),
2786 * etc., without talking to mdt authors first. Checking same thing
2787 * there again is useless and returning 0 error without packing reply
2788 * is buggy! Handlers either pack reply or return error.
2790 * We return 0 here and do not send any reply in order to emulate
2791 * network failure. Do not send any reply in case any of NET related
2792 * fail_id has occured.
2794 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2798 flags = h->mh_flags;
2799 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2801 if (h->mh_fmt != NULL) {
2802 req_capsule_set(info->mti_pill, h->mh_fmt);
2803 rc = mdt_unpack_req_pack_rep(info, flags);
2806 if (rc == 0 && flags & MUTABOR &&
2807 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2808 /* should it be rq_status? */
2811 if (rc == 0 && flags & HABEO_CLAVIS) {
2812 struct ldlm_request *dlm_req;
2814 LASSERT(h->mh_fmt != NULL);
2816 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2817 if (dlm_req != NULL) {
2818 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2820 dlm_req->lock_desc.l_policy_data.\
2821 l_inodebits.bits == 0)) {
2823 * Lock without inodebits makes no sense and
2824 * will oops later in ldlm. If client miss to
2825 * set such bits, do not trigger ASSERTION.
2827 * For liblustre flock case, it maybe zero.
2831 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2832 rc = mdt_lock_resname_compat(
2835 info->mti_dlm_req = dlm_req;
2842 /* capability setting changed via /proc, needs reinitialize ctxt */
2843 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2844 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2845 info->mti_mdt->mdt_capa_conf = 0;
2848 if (likely(rc == 0)) {
2850 * Process request, there can be two types of rc:
2851 * 1) errors with msg unpack/pack, other failures outside the
2852 * operation itself. This is counted as serious errors;
2853 * 2) errors during fs operation, should be placed in rq_status
2856 rc = h->mh_act(info);
2858 !req->rq_no_reply && req->rq_reply_state == NULL) {
2859 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2860 "pack reply and returned 0 error\n",
2864 serious = is_serious(rc);
2865 rc = clear_serious(rc);
2869 req->rq_status = rc;
2872 * ELDLM_* codes which > 0 should be in rq_status only as well as
2873 * all non-serious errors.
2875 if (rc > 0 || !serious)
2878 LASSERT(current->journal_info == NULL);
2880 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2881 info->mti_mdt->mdt_opts.mo_compat_resname) {
2882 struct ldlm_reply *dlmrep;
2884 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2886 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2889 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2890 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2891 target_committed_to_req(req);
2893 if (unlikely(req_is_replay(req) &&
2894 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2895 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2899 target_send_reply(req, rc, info->mti_fail_id);
2903 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2905 lh->mlh_type = MDT_NUL_LOCK;
2906 lh->mlh_reg_lh.cookie = 0ull;
2907 lh->mlh_reg_mode = LCK_MINMODE;
2908 lh->mlh_pdo_lh.cookie = 0ull;
2909 lh->mlh_pdo_mode = LCK_MINMODE;
2912 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2914 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2915 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2919 * Initialize fields of struct mdt_thread_info. Other fields are left in
2920 * uninitialized state, because it's too expensive to zero out whole
2921 * mdt_thread_info (> 1K) on each request arrival.
2923 static void mdt_thread_info_init(struct ptlrpc_request *req,
2924 struct mdt_thread_info *info)
2927 struct md_capainfo *ci;
2929 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2930 info->mti_pill = &req->rq_pill;
2933 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2934 mdt_lock_handle_init(&info->mti_lh[i]);
2936 /* mdt device: it can be NULL while CONNECT */
2937 if (req->rq_export) {
2938 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2939 info->mti_exp = req->rq_export;
2941 info->mti_mdt = NULL;
2942 info->mti_env = req->rq_svc_thread->t_env;
2943 ci = md_capainfo(info->mti_env);
2944 memset(ci, 0, sizeof *ci);
2945 if (req->rq_export) {
2946 if (exp_connect_rmtclient(req->rq_export))
2947 ci->mc_auth = LC_ID_CONVERT;
2948 else if (req->rq_export->exp_connect_flags &
2949 OBD_CONNECT_MDS_CAPA)
2950 ci->mc_auth = LC_ID_PLAIN;
2952 ci->mc_auth = LC_ID_NONE;
2955 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2956 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2957 info->mti_mos = NULL;
2959 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2960 info->mti_body = NULL;
2961 info->mti_object = NULL;
2962 info->mti_dlm_req = NULL;
2963 info->mti_has_trans = 0;
2964 info->mti_cross_ref = 0;
2965 info->mti_opdata = 0;
2966 info->mti_big_lmm_used = 0;
2968 /* To not check for split by default. */
2969 info->mti_spec.no_create = 0;
2972 static void mdt_thread_info_fini(struct mdt_thread_info *info)
2976 req_capsule_fini(info->mti_pill);
2977 if (info->mti_object != NULL) {
2978 mdt_object_put(info->mti_env, info->mti_object);
2979 info->mti_object = NULL;
2981 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2982 mdt_lock_handle_fini(&info->mti_lh[i]);
2983 info->mti_env = NULL;
2986 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
2987 struct obd_device *obd, int *process)
2989 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2990 case MDS_CONNECT: /* This will never get here, but for completeness. */
2991 case OST_CONNECT: /* This will never get here, but for completeness. */
2992 case MDS_DISCONNECT:
2993 case OST_DISCONNECT:
2999 case MDS_DONE_WRITING:
3000 case MDS_SYNC: /* used in unmounting */
3006 *process = target_queue_recovery_request(req, obd);
3010 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3017 * Handle recovery. Return:
3018 * +1: continue request processing;
3019 * -ve: abort immediately with the given error code;
3020 * 0: send reply with error code in req->rq_status;
3022 static int mdt_recovery(struct mdt_thread_info *info)
3024 struct ptlrpc_request *req = mdt_info_req(info);
3025 struct obd_device *obd;
3029 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3032 case SEC_CTX_INIT_CONT:
3038 rc = mdt_handle_idmap(info);
3047 if (unlikely(!class_connected_export(req->rq_export))) {
3048 CERROR("operation %d on unconnected MDS from %s\n",
3049 lustre_msg_get_opc(req->rq_reqmsg),
3050 libcfs_id2str(req->rq_peer));
3051 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3052 * mds_A will get -ENOTCONN(especially for ping req),
3053 * which will cause that mds_A deactive timeout, then when
3054 * mds_A cleanup, the cleanup process will be suspended since
3055 * deactive timeout is not zero.
3057 req->rq_status = -ENOTCONN;
3058 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3062 /* sanity check: if the xid matches, the request must be marked as a
3063 * resent or replayed */
3064 if (req_xid_is_last(req)) {
3065 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3066 (MSG_RESENT | MSG_REPLAY))) {
3067 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3068 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3069 lustre_msg_get_flags(req->rq_reqmsg));
3071 req->rq_status = -ENOTCONN;
3076 /* else: note the opposite is not always true; a RESENT req after a
3077 * failover will usually not match the last_xid, since it was likely
3078 * never committed. A REPLAYed request will almost never match the
3079 * last xid, however it could for a committed, but still retained,
3082 obd = req->rq_export->exp_obd;
3084 /* Check for aborted recovery... */
3085 if (unlikely(obd->obd_recovering)) {
3088 DEBUG_REQ(D_INFO, req, "Got new replay");
3089 rc = mdt_filter_recovery_request(req, obd, &should_process);
3090 if (rc != 0 || !should_process)
3092 else if (should_process < 0) {
3093 req->rq_status = should_process;
3094 rc = ptlrpc_error(req);
3101 static int mdt_msg_check_version(struct lustre_msg *msg)
3105 switch (lustre_msg_get_opc(msg)) {
3107 case MDS_DISCONNECT:
3110 case SEC_CTX_INIT_CONT:
3113 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3115 CERROR("bad opc %u version %08x, expecting %08x\n",
3116 lustre_msg_get_opc(msg),
3117 lustre_msg_get_version(msg),
3118 LUSTRE_OBD_VERSION);
3122 case MDS_GETATTR_NAME:
3129 case MDS_DONE_WRITING:
3136 case MDS_QUOTACHECK:
3142 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3144 CERROR("bad opc %u version %08x, expecting %08x\n",
3145 lustre_msg_get_opc(msg),
3146 lustre_msg_get_version(msg),
3147 LUSTRE_MDS_VERSION);
3151 case LDLM_BL_CALLBACK:
3152 case LDLM_CP_CALLBACK:
3153 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3155 CERROR("bad opc %u version %08x, expecting %08x\n",
3156 lustre_msg_get_opc(msg),
3157 lustre_msg_get_version(msg),
3158 LUSTRE_DLM_VERSION);
3160 case OBD_LOG_CANCEL:
3161 case LLOG_ORIGIN_HANDLE_CREATE:
3162 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3163 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3164 case LLOG_ORIGIN_HANDLE_CLOSE:
3165 case LLOG_ORIGIN_HANDLE_DESTROY:
3166 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3168 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3170 CERROR("bad opc %u version %08x, expecting %08x\n",
3171 lustre_msg_get_opc(msg),
3172 lustre_msg_get_version(msg),
3173 LUSTRE_LOG_VERSION);
3176 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3182 static int mdt_handle0(struct ptlrpc_request *req,
3183 struct mdt_thread_info *info,
3184 struct mdt_opc_slice *supported)
3186 struct mdt_handler *h;
3187 struct lustre_msg *msg;
3192 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3195 LASSERT(current->journal_info == NULL);
3197 msg = req->rq_reqmsg;
3198 rc = mdt_msg_check_version(msg);
3199 if (likely(rc == 0)) {
3200 rc = mdt_recovery(info);
3201 if (likely(rc == +1)) {
3202 h = mdt_handler_find(lustre_msg_get_opc(msg),
3204 if (likely(h != NULL)) {
3205 rc = mdt_req_handle(info, h, req);
3207 CERROR("The unsupported opc: 0x%x\n",
3208 lustre_msg_get_opc(msg) );
3209 req->rq_status = -ENOTSUPP;
3210 rc = ptlrpc_error(req);
3215 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3220 * MDT handler function called by ptlrpc service thread when request comes.
3222 * XXX common "target" functionality should be factored into separate module
3223 * shared by mdt, ost and stand-alone services like fld.
3225 static int mdt_handle_common(struct ptlrpc_request *req,
3226 struct mdt_opc_slice *supported)
3229 struct mdt_thread_info *info;
3233 env = req->rq_svc_thread->t_env;
3234 LASSERT(env != NULL);
3235 LASSERT(env->le_ses != NULL);
3236 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3237 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3238 LASSERT(info != NULL);
3240 mdt_thread_info_init(req, info);
3242 rc = mdt_handle0(req, info, supported);
3244 mdt_thread_info_fini(info);
3249 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3252 int mdt_recovery_handle(struct ptlrpc_request *req)
3257 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3259 rc = mdt_handle_common(req, mdt_fld_handlers);
3262 rc = mdt_handle_common(req, mdt_seq_handlers);
3265 rc = mdt_handle_common(req, mdt_regular_handlers);
3272 static int mdt_regular_handle(struct ptlrpc_request *req)
3274 return mdt_handle_common(req, mdt_regular_handlers);
3277 static int mdt_readpage_handle(struct ptlrpc_request *req)
3279 return mdt_handle_common(req, mdt_readpage_handlers);
3282 static int mdt_xmds_handle(struct ptlrpc_request *req)
3284 return mdt_handle_common(req, mdt_xmds_handlers);
3287 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3289 return mdt_handle_common(req, mdt_seq_handlers);
3292 static int mdt_mdss_handle(struct ptlrpc_request *req)
3294 return mdt_handle_common(req, mdt_seq_handlers);
3297 static int mdt_dtss_handle(struct ptlrpc_request *req)
3299 return mdt_handle_common(req, mdt_seq_handlers);
3302 static int mdt_fld_handle(struct ptlrpc_request *req)
3304 return mdt_handle_common(req, mdt_fld_handlers);
3322 static int mdt_intent_getattr(enum mdt_it_code opcode,
3323 struct mdt_thread_info *info,
3324 struct ldlm_lock **,
3326 static int mdt_intent_reint(enum mdt_it_code opcode,
3327 struct mdt_thread_info *info,
3328 struct ldlm_lock **,
3331 static struct mdt_it_flavor {
3332 const struct req_format *it_fmt;
3334 int (*it_act)(enum mdt_it_code ,
3335 struct mdt_thread_info *,
3336 struct ldlm_lock **,
3339 } mdt_it_flavor[] = {
3341 .it_fmt = &RQF_LDLM_INTENT,
3342 /*.it_flags = HABEO_REFERO,*/
3344 .it_act = mdt_intent_reint,
3345 .it_reint = REINT_OPEN
3348 .it_fmt = &RQF_LDLM_INTENT,
3349 .it_flags = MUTABOR,
3350 .it_act = mdt_intent_reint,
3351 .it_reint = REINT_OPEN
3354 .it_fmt = &RQF_LDLM_INTENT,
3355 .it_flags = MUTABOR,
3356 .it_act = mdt_intent_reint,
3357 .it_reint = REINT_CREATE
3359 [MDT_IT_GETATTR] = {
3360 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3361 .it_flags = HABEO_REFERO,
3362 .it_act = mdt_intent_getattr
3364 [MDT_IT_READDIR] = {
3370 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3371 .it_flags = HABEO_REFERO,
3372 .it_act = mdt_intent_getattr
3375 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3376 .it_flags = MUTABOR,
3378 .it_reint = REINT_UNLINK
3382 .it_flags = MUTABOR,
3385 [MDT_IT_GETXATTR] = {
3391 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3392 .it_flags = HABEO_REFERO,
3393 .it_act = mdt_intent_getattr
3397 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3398 struct ldlm_lock **lockp,
3399 struct ldlm_lock *new_lock,
3400 struct mdt_lock_handle *lh,
3403 struct ptlrpc_request *req = mdt_info_req(info);
3404 struct ldlm_lock *lock = *lockp;
3407 * Get new lock only for cases when possible resent did not find any
3410 if (new_lock == NULL)
3411 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3413 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3414 lh->mlh_reg_lh.cookie = 0;
3418 LASSERTF(new_lock != NULL,
3419 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3422 * If we've already given this lock to a client once, then we should
3423 * have no readers or writers. Otherwise, we should have one reader
3424 * _or_ writer ref (which will be zeroed below) before returning the
3427 if (new_lock->l_export == req->rq_export) {
3428 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3430 LASSERT(new_lock->l_export == NULL);
3431 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3436 if (new_lock->l_export == req->rq_export) {
3438 * Already gave this to the client, which means that we
3439 * reconstructed a reply.
3441 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3443 lh->mlh_reg_lh.cookie = 0;
3444 RETURN(ELDLM_LOCK_REPLACED);
3448 * Fixup the lock to be given to the client.
3450 lock_res_and_lock(new_lock);
3451 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3452 * possible blocking AST. */
3453 while (new_lock->l_readers > 0) {
3454 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3455 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3456 new_lock->l_readers--;
3458 while (new_lock->l_writers > 0) {
3459 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3460 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3461 new_lock->l_writers--;
3464 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3465 new_lock->l_blocking_ast = lock->l_blocking_ast;
3466 new_lock->l_completion_ast = lock->l_completion_ast;
3467 new_lock->l_remote_handle = lock->l_remote_handle;
3468 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3470 unlock_res_and_lock(new_lock);
3472 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3473 &new_lock->l_remote_handle,
3474 &new_lock->l_exp_hash);
3476 LDLM_LOCK_RELEASE(new_lock);
3477 lh->mlh_reg_lh.cookie = 0;
3479 RETURN(ELDLM_LOCK_REPLACED);
3482 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3483 struct ldlm_lock *new_lock,
3484 struct ldlm_lock **old_lock,
3485 struct mdt_lock_handle *lh)
3487 struct ptlrpc_request *req = mdt_info_req(info);
3488 struct obd_export *exp = req->rq_export;
3489 struct lustre_handle remote_hdl;
3490 struct ldlm_request *dlmreq;
3491 struct ldlm_lock *lock;
3493 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3496 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3497 remote_hdl = dlmreq->lock_handle[0];
3499 /* In the function below, .hs_keycmp resolves to
3500 * ldlm_export_lock_keycmp() */
3501 /* coverity[overrun-buffer-val] */
3502 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3504 if (lock != new_lock) {
3505 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3506 lh->mlh_reg_mode = lock->l_granted_mode;
3508 LDLM_DEBUG(lock, "Restoring lock cookie");
3509 DEBUG_REQ(D_DLMTRACE, req,
3510 "restoring lock cookie "LPX64,
3511 lh->mlh_reg_lh.cookie);
3513 *old_lock = LDLM_LOCK_GET(lock);
3514 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3518 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3522 * If the xid matches, then we know this is a resent request, and allow
3523 * it. (It's probably an OPEN, for which we don't send a lock.
3525 if (req_xid_is_last(req))
3529 * This remote handle isn't enqueued, so we never received or processed
3530 * this request. Clear MSG_RESENT, because it can be handled like any
3531 * normal request now.
3533 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3535 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3539 static int mdt_intent_getattr(enum mdt_it_code opcode,
3540 struct mdt_thread_info *info,
3541 struct ldlm_lock **lockp,
3544 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3545 struct ldlm_lock *new_lock = NULL;
3547 struct ldlm_reply *ldlm_rep;
3548 struct ptlrpc_request *req;
3549 struct mdt_body *reqbody;
3550 struct mdt_body *repbody;
3554 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3557 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3560 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3561 repbody->eadatasize = 0;
3562 repbody->aclsize = 0;
3566 child_bits = MDS_INODELOCK_LOOKUP;
3568 case MDT_IT_GETATTR:
3569 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3571 case MDT_IT_LAYOUT: {
3572 static int printed = 0;
3575 CERROR("layout lock not supported by this version\n");
3578 GOTO(out_shrink, rc = -EINVAL);
3582 CERROR("Unsupported intent (%d)\n", opcode);
3583 GOTO(out_shrink, rc = -EINVAL);
3586 rc = mdt_init_ucred(info, reqbody);
3588 GOTO(out_shrink, rc);
3590 req = info->mti_pill->rc_req;
3591 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3592 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3594 /* Get lock from request for possible resent case. */
3595 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3597 ldlm_rep->lock_policy_res2 =
3598 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3600 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3601 ldlm_rep->lock_policy_res2 = 0;
3602 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3603 ldlm_rep->lock_policy_res2) {
3604 lhc->mlh_reg_lh.cookie = 0ull;
3605 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3608 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3611 mdt_exit_ucred(info);
3613 mdt_client_compatibility(info);
3614 rc2 = mdt_fix_reply(info);
3620 static int mdt_intent_reint(enum mdt_it_code opcode,
3621 struct mdt_thread_info *info,
3622 struct ldlm_lock **lockp,
3625 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3626 struct ldlm_reply *rep = NULL;
3630 static const struct req_format *intent_fmts[REINT_MAX] = {
3631 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3632 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3637 opc = mdt_reint_opcode(info, intent_fmts);
3641 if (mdt_it_flavor[opcode].it_reint != opc) {
3642 CERROR("Reint code %ld doesn't match intent: %d\n",
3644 RETURN(err_serious(-EPROTO));
3647 /* Get lock from request for possible resent case. */
3648 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3650 rc = mdt_reint_internal(info, lhc, opc);
3652 /* Check whether the reply has been packed successfully. */
3653 if (mdt_info_req(info)->rq_repmsg != NULL)
3654 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3656 RETURN(err_serious(-EFAULT));
3658 /* MDC expects this in any case */
3660 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3662 /* Cross-ref case, the lock should be returned to the client */
3663 if (rc == -EREMOTE) {
3664 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3665 rep->lock_policy_res2 = 0;
3666 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3669 rep->lock_policy_res2 = clear_serious(rc);
3671 if (rep->lock_policy_res2 == -ENOENT &&
3672 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3673 rep->lock_policy_res2 = 0;
3675 if (rc == -ENOTCONN || rc == -ENODEV ||
3676 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3678 * If it is the disconnect error (ENODEV & ENOCONN), the error
3679 * will be returned by rq_status, and client at ptlrpc layer
3680 * will detect this, then disconnect, reconnect the import
3681 * immediately, instead of impacting the following the rpc.
3683 lhc->mlh_reg_lh.cookie = 0ull;
3687 * For other cases, the error will be returned by intent.
3688 * and client will retrieve the result from intent.
3691 * FIXME: when open lock is finished, that should be
3694 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3695 LASSERTF(rc == 0, "Error occurred but lock handle "
3696 "is still in use\n");
3697 rep->lock_policy_res2 = 0;
3698 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3701 lhc->mlh_reg_lh.cookie = 0ull;
3702 RETURN(ELDLM_LOCK_ABORTED);
3707 static int mdt_intent_code(long itcode)
3715 case IT_OPEN|IT_CREAT:
3722 rc = MDT_IT_READDIR;
3725 rc = MDT_IT_GETATTR;
3737 rc = MDT_IT_GETXATTR;
3742 case IT_QUOTA_DQACQ:
3747 CERROR("Unknown intent opcode: %ld\n", itcode);
3754 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3755 struct ldlm_lock **lockp, __u64 flags)
3757 struct req_capsule *pill;
3758 struct mdt_it_flavor *flv;
3763 opc = mdt_intent_code(itopc);
3767 pill = info->mti_pill;
3769 if (opc == MDT_IT_QUOTA) {
3770 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3773 RETURN(-EOPNOTSUPP);
3775 /* pass the request to quota master */
3776 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3777 mdt_info_req(info), lockp,
3782 flv = &mdt_it_flavor[opc];
3783 if (flv->it_fmt != NULL)
3784 req_capsule_extend(pill, flv->it_fmt);
3786 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3788 struct ptlrpc_request *req = mdt_info_req(info);
3789 if (flv->it_flags & MUTABOR &&
3790 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3793 if (rc == 0 && flv->it_act != NULL) {
3794 /* execute policy */
3795 rc = flv->it_act(opc, info, lockp, flags);
3802 static int mdt_intent_policy(struct ldlm_namespace *ns,
3803 struct ldlm_lock **lockp, void *req_cookie,
3804 ldlm_mode_t mode, __u64 flags, void *data)
3806 struct mdt_thread_info *info;
3807 struct ptlrpc_request *req = req_cookie;
3808 struct ldlm_intent *it;
3809 struct req_capsule *pill;
3814 LASSERT(req != NULL);
3816 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3818 LASSERT(info != NULL);
3819 pill = info->mti_pill;
3820 LASSERT(pill->rc_req == req);
3822 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3823 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3824 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3826 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3830 /* Lock without inodebits makes no sense and will oops
3831 * later in ldlm. Let's check it now to see if we have
3832 * ibits corrupted somewhere in mdt_intent_opc().
3833 * The case for client miss to set ibits has been
3834 * processed by others. */
3835 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3836 lr_type == LDLM_IBITS,
3837 info->mti_dlm_req->lock_desc.\
3838 l_policy_data.l_inodebits.bits != 0));
3840 rc = err_serious(-EFAULT);
3842 /* No intent was provided */
3843 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3844 rc = req_capsule_server_pack(pill);
3846 rc = err_serious(rc);
3851 static int mdt_seq_fini(const struct lu_env *env,
3852 struct mdt_device *m)
3854 struct md_site *ms = mdt_md_site(m);
3858 if (ms->ms_server_seq) {
3859 seq_server_fini(ms->ms_server_seq, env);
3860 OBD_FREE_PTR(ms->ms_server_seq);
3861 ms->ms_server_seq = NULL;
3864 if (ms->ms_control_seq) {
3865 seq_server_fini(ms->ms_control_seq, env);
3866 OBD_FREE_PTR(ms->ms_control_seq);
3867 ms->ms_control_seq = NULL;
3870 if (ms->ms_client_seq) {
3871 seq_client_fini(ms->ms_client_seq);
3872 OBD_FREE_PTR(ms->ms_client_seq);
3873 ms->ms_client_seq = NULL;
3880 static int mdt_seq_init(const struct lu_env *env,
3882 struct mdt_device *m)
3889 ms = mdt_md_site(m);
3892 * This is sequence-controller node. Init seq-controller server on local
3895 if (ms->ms_node_id == 0) {
3896 LASSERT(ms->ms_control_seq == NULL);
3898 OBD_ALLOC_PTR(ms->ms_control_seq);
3899 if (ms->ms_control_seq == NULL)
3902 rc = seq_server_init(ms->ms_control_seq,
3903 m->mdt_bottom, uuid,
3904 LUSTRE_SEQ_CONTROLLER,
3909 GOTO(out_seq_fini, rc);
3911 OBD_ALLOC_PTR(ms->ms_client_seq);
3912 if (ms->ms_client_seq == NULL)
3913 GOTO(out_seq_fini, rc = -ENOMEM);
3915 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3916 if (prefix == NULL) {
3917 OBD_FREE_PTR(ms->ms_client_seq);
3918 GOTO(out_seq_fini, rc = -ENOMEM);
3921 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3925 * Init seq-controller client after seq-controller server is
3926 * ready. Pass ms->ms_control_seq to it for direct talking.
3928 rc = seq_client_init(ms->ms_client_seq, NULL,
3929 LUSTRE_SEQ_METADATA, prefix,
3930 ms->ms_control_seq);
3931 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3934 GOTO(out_seq_fini, rc);
3937 /* Init seq-server on local MDT */
3938 LASSERT(ms->ms_server_seq == NULL);
3940 OBD_ALLOC_PTR(ms->ms_server_seq);
3941 if (ms->ms_server_seq == NULL)
3942 GOTO(out_seq_fini, rc = -ENOMEM);
3944 rc = seq_server_init(ms->ms_server_seq,
3945 m->mdt_bottom, uuid,
3950 GOTO(out_seq_fini, rc = -ENOMEM);
3952 /* Assign seq-controller client to local seq-server. */
3953 if (ms->ms_node_id == 0) {
3954 LASSERT(ms->ms_client_seq != NULL);
3956 rc = seq_server_set_cli(ms->ms_server_seq,
3964 mdt_seq_fini(env, m);
3969 * Init client sequence manager which is used by local MDS to talk to sequence
3970 * controller on remote node.
3972 static int mdt_seq_init_cli(const struct lu_env *env,
3973 struct mdt_device *m,
3974 struct lustre_cfg *cfg)
3976 struct md_site *ms = mdt_md_site(m);
3977 struct obd_device *mdc;
3978 struct obd_uuid *uuidp, *mdcuuidp;
3979 char *uuid_str, *mdc_uuid_str;
3982 struct mdt_thread_info *info;
3983 char *p, *index_string = lustre_cfg_string(cfg, 2);
3986 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3987 uuidp = &info->mti_u.uuid[0];
3988 mdcuuidp = &info->mti_u.uuid[1];
3990 LASSERT(index_string);
3992 index = simple_strtol(index_string, &p, 10);
3994 CERROR("Invalid index in lustre_cgf, offset 2\n");
3998 /* check if this is adding the first MDC and controller is not yet
4000 if (index != 0 || ms->ms_client_seq)
4003 uuid_str = lustre_cfg_string(cfg, 1);
4004 mdc_uuid_str = lustre_cfg_string(cfg, 4);
4005 obd_str2uuid(uuidp, uuid_str);
4006 obd_str2uuid(mdcuuidp, mdc_uuid_str);
4008 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
4010 CERROR("can't find controller MDC by uuid %s\n",
4013 } else if (!mdc->obd_set_up) {
4014 CERROR("target %s not set up\n", mdc->obd_name);
4017 LASSERT(ms->ms_control_exp);
4018 OBD_ALLOC_PTR(ms->ms_client_seq);
4019 if (ms->ms_client_seq != NULL) {
4022 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4026 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4029 rc = seq_client_init(ms->ms_client_seq,
4031 LUSTRE_SEQ_METADATA,
4033 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4040 LASSERT(ms->ms_server_seq != NULL);
4041 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
4048 static void mdt_seq_fini_cli(struct mdt_device *m)
4054 ms = mdt_md_site(m);
4057 if (ms->ms_server_seq)
4058 seq_server_set_cli(ms->ms_server_seq,
4061 if (ms->ms_control_exp) {
4062 class_export_put(ms->ms_control_exp);
4063 ms->ms_control_exp = NULL;
4072 static int mdt_fld_fini(const struct lu_env *env,
4073 struct mdt_device *m)
4075 struct md_site *ms = mdt_md_site(m);
4078 if (ms && ms->ms_server_fld) {
4079 fld_server_fini(ms->ms_server_fld, env);
4080 OBD_FREE_PTR(ms->ms_server_fld);
4081 ms->ms_server_fld = NULL;
4087 static int mdt_fld_init(const struct lu_env *env,
4089 struct mdt_device *m)
4095 ms = mdt_md_site(m);
4097 OBD_ALLOC_PTR(ms->ms_server_fld);
4098 if (ms->ms_server_fld == NULL)
4099 RETURN(rc = -ENOMEM);
4101 rc = fld_server_init(ms->ms_server_fld,
4102 m->mdt_bottom, uuid,
4103 env, ms->ms_node_id);
4105 OBD_FREE_PTR(ms->ms_server_fld);
4106 ms->ms_server_fld = NULL;
4113 /* device init/fini methods */
4114 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
4117 if (m->mdt_regular_service != NULL) {
4118 ptlrpc_unregister_service(m->mdt_regular_service);
4119 m->mdt_regular_service = NULL;
4121 if (m->mdt_readpage_service != NULL) {
4122 ptlrpc_unregister_service(m->mdt_readpage_service);
4123 m->mdt_readpage_service = NULL;
4125 if (m->mdt_xmds_service != NULL) {
4126 ptlrpc_unregister_service(m->mdt_xmds_service);
4127 m->mdt_xmds_service = NULL;
4129 if (m->mdt_setattr_service != NULL) {
4130 ptlrpc_unregister_service(m->mdt_setattr_service);
4131 m->mdt_setattr_service = NULL;
4133 if (m->mdt_mdsc_service != NULL) {
4134 ptlrpc_unregister_service(m->mdt_mdsc_service);
4135 m->mdt_mdsc_service = NULL;
4137 if (m->mdt_mdss_service != NULL) {
4138 ptlrpc_unregister_service(m->mdt_mdss_service);
4139 m->mdt_mdss_service = NULL;
4141 if (m->mdt_dtss_service != NULL) {
4142 ptlrpc_unregister_service(m->mdt_dtss_service);
4143 m->mdt_dtss_service = NULL;
4145 if (m->mdt_fld_service != NULL) {
4146 ptlrpc_unregister_service(m->mdt_fld_service);
4147 m->mdt_fld_service = NULL;
4152 static int mdt_start_ptlrpc_service(struct mdt_device *m)
4154 static struct ptlrpc_service_conf conf;
4155 cfs_proc_dir_entry_t *procfs_entry;
4159 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
4160 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4161 "mdt_ldlm_client", m->mdt_ldlm_client);
4163 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
4165 conf = (typeof(conf)) {
4166 .psc_name = LUSTRE_MDT_NAME,
4167 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4169 .bc_nbufs = MDS_NBUFS,
4170 .bc_buf_size = MDS_BUFSIZE,
4171 .bc_req_max_size = MDS_MAXREQSIZE,
4172 .bc_rep_max_size = MDS_MAXREPSIZE,
4173 .bc_req_portal = MDS_REQUEST_PORTAL,
4174 .bc_rep_portal = MDC_REPLY_PORTAL,
4177 * We'd like to have a mechanism to set this on a per-device
4178 * basis, but alas...
4181 .tc_thr_name = LUSTRE_MDT_NAME,
4182 .tc_thr_factor = MDT_THR_FACTOR,
4183 .tc_nthrs_init = MDT_NTHRS_INIT,
4184 .tc_nthrs_base = MDT_NTHRS_BASE,
4185 .tc_nthrs_max = MDT_NTHRS_MAX,
4186 .tc_nthrs_user = mds_num_threads,
4187 .tc_cpu_affinity = 1,
4188 .tc_ctx_tags = LCT_MD_THREAD,
4191 .cc_pattern = mds_num_cpts,
4194 .so_req_handler = mdt_regular_handle,
4195 .so_req_printer = target_print_req,
4196 .so_hpreq_handler = ptlrpc_hpreq_handler,
4199 m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
4200 if (IS_ERR(m->mdt_regular_service)) {
4201 rc = PTR_ERR(m->mdt_regular_service);
4202 CERROR("failed to start regular mdt service: %d\n", rc);
4203 m->mdt_regular_service = NULL;
4209 * readpage service configuration. Parameters have to be adjusted,
4212 memset(&conf, 0, sizeof(conf));
4213 conf = (typeof(conf)) {
4214 .psc_name = LUSTRE_MDT_NAME "_readpage",
4215 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4217 .bc_nbufs = MDS_NBUFS,
4218 .bc_buf_size = MDS_BUFSIZE,
4219 .bc_req_max_size = MDS_MAXREQSIZE,
4220 .bc_rep_max_size = MDS_MAXREPSIZE,
4221 .bc_req_portal = MDS_READPAGE_PORTAL,
4222 .bc_rep_portal = MDC_REPLY_PORTAL,
4225 .tc_thr_name = "mdt_rdpg",
4226 .tc_thr_factor = MDT_RDPG_THR_FACTOR,
4227 .tc_nthrs_init = MDT_RDPG_NTHRS_INIT,
4228 .tc_nthrs_base = MDT_RDPG_NTHRS_BASE,
4229 .tc_nthrs_max = MDT_RDPG_NTHRS_MAX,
4230 .tc_nthrs_user = mds_rdpg_num_threads,
4231 .tc_cpu_affinity = 1,
4232 .tc_ctx_tags = LCT_MD_THREAD,
4235 .cc_pattern = mds_rdpg_num_cpts,
4238 .so_req_handler = mdt_readpage_handle,
4239 .so_req_printer = target_print_req,
4242 m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
4243 if (IS_ERR(m->mdt_readpage_service)) {
4244 rc = PTR_ERR(m->mdt_readpage_service);
4245 CERROR("failed to start readpage service: %d\n", rc);
4246 m->mdt_readpage_service = NULL;
4248 GOTO(err_mdt_svc, rc);
4252 * setattr service configuration.
4254 * XXX To keep the compatibility with old client(< 2.2), we need to
4255 * preserve this portal for a certain time, it should be removed
4256 * eventually. LU-617.
4258 memset(&conf, 0, sizeof(conf));
4259 conf = (typeof(conf)) {
4260 .psc_name = LUSTRE_MDT_NAME "_setattr",
4261 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4263 .bc_nbufs = MDS_NBUFS,
4264 .bc_buf_size = MDS_BUFSIZE,
4265 .bc_req_max_size = MDS_MAXREQSIZE,
4266 .bc_rep_max_size = MDS_MAXREPSIZE,
4267 .bc_req_portal = MDS_SETATTR_PORTAL,
4268 .bc_rep_portal = MDC_REPLY_PORTAL,
4271 .tc_thr_name = "mdt_attr",
4272 .tc_thr_factor = MDT_SETA_THR_FACTOR,
4273 .tc_nthrs_init = MDT_SETA_NTHRS_INIT,
4274 .tc_nthrs_base = MDT_SETA_NTHRS_BASE,
4275 .tc_nthrs_max = MDT_SETA_NTHRS_MAX,
4276 .tc_nthrs_user = mds_attr_num_threads,
4277 .tc_cpu_affinity = 1,
4278 .tc_ctx_tags = LCT_MD_THREAD,
4281 .cc_pattern = mds_attr_num_cpts,
4284 .so_req_handler = mdt_regular_handle,
4285 .so_req_printer = target_print_req,
4288 m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4289 if (IS_ERR(m->mdt_setattr_service)) {
4290 rc = PTR_ERR(m->mdt_setattr_service);
4291 CERROR("failed to start setattr service: %d\n", rc);
4292 m->mdt_setattr_service = NULL;
4294 GOTO(err_mdt_svc, rc);
4298 * sequence controller service configuration
4300 memset(&conf, 0, sizeof(conf));
4301 conf = (typeof(conf)) {
4302 .psc_name = LUSTRE_MDT_NAME "_mdsc",
4303 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4305 .bc_nbufs = MDS_NBUFS,
4306 .bc_buf_size = MDS_BUFSIZE,
4307 .bc_req_max_size = SEQ_MAXREQSIZE,
4308 .bc_rep_max_size = SEQ_MAXREPSIZE,
4309 .bc_req_portal = SEQ_CONTROLLER_PORTAL,
4310 .bc_rep_portal = MDC_REPLY_PORTAL,
4313 .tc_thr_name = "mdt_mdsc",
4314 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4315 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4316 .tc_ctx_tags = LCT_MD_THREAD,
4319 .so_req_handler = mdt_mdsc_handle,
4320 .so_req_printer = target_print_req,
4323 m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4324 if (IS_ERR(m->mdt_mdsc_service)) {
4325 rc = PTR_ERR(m->mdt_mdsc_service);
4326 CERROR("failed to start seq controller service: %d\n", rc);
4327 m->mdt_mdsc_service = NULL;
4329 GOTO(err_mdt_svc, rc);
4333 * metadata sequence server service configuration
4335 memset(&conf, 0, sizeof(conf));
4336 conf = (typeof(conf)) {
4337 .psc_name = LUSTRE_MDT_NAME "_mdss",
4338 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4340 .bc_nbufs = MDS_NBUFS,
4341 .bc_buf_size = MDS_BUFSIZE,
4342 .bc_req_max_size = SEQ_MAXREQSIZE,
4343 .bc_rep_max_size = SEQ_MAXREPSIZE,
4344 .bc_req_portal = SEQ_METADATA_PORTAL,
4345 .bc_rep_portal = MDC_REPLY_PORTAL,
4348 .tc_thr_name = "mdt_mdss",
4349 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4350 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4351 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4354 .so_req_handler = mdt_mdss_handle,
4355 .so_req_printer = target_print_req,
4358 m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4359 if (IS_ERR(m->mdt_mdss_service)) {
4360 rc = PTR_ERR(m->mdt_mdss_service);
4361 CERROR("failed to start metadata seq server service: %d\n", rc);
4362 m->mdt_mdss_service = NULL;
4364 GOTO(err_mdt_svc, rc);
4368 * Data sequence server service configuration. We want to have really
4369 * cluster-wide sequences space. This is why we start only one sequence
4370 * controller which manages space.
4372 memset(&conf, 0, sizeof(conf));
4373 conf = (typeof(conf)) {
4374 .psc_name = LUSTRE_MDT_NAME "_dtss",
4375 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4377 .bc_nbufs = MDS_NBUFS,
4378 .bc_buf_size = MDS_BUFSIZE,
4379 .bc_req_max_size = SEQ_MAXREQSIZE,
4380 .bc_rep_max_size = SEQ_MAXREPSIZE,
4381 .bc_req_portal = SEQ_DATA_PORTAL,
4382 .bc_rep_portal = OSC_REPLY_PORTAL,
4385 .tc_thr_name = "mdt_dtss",
4386 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4387 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4388 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4391 .so_req_handler = mdt_dtss_handle,
4392 .so_req_printer = target_print_req,
4395 m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4396 if (IS_ERR(m->mdt_dtss_service)) {
4397 rc = PTR_ERR(m->mdt_dtss_service);
4398 CERROR("failed to start data seq server service: %d\n", rc);
4399 m->mdt_dtss_service = NULL;
4401 GOTO(err_mdt_svc, rc);
4404 /* FLD service start */
4405 memset(&conf, 0, sizeof(conf));
4406 conf = (typeof(conf)) {
4407 .psc_name = LUSTRE_MDT_NAME "_fld",
4408 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4410 .bc_nbufs = MDS_NBUFS,
4411 .bc_buf_size = MDS_BUFSIZE,
4412 .bc_req_max_size = FLD_MAXREQSIZE,
4413 .bc_rep_max_size = FLD_MAXREPSIZE,
4414 .bc_req_portal = FLD_REQUEST_PORTAL,
4415 .bc_rep_portal = MDC_REPLY_PORTAL,
4418 .tc_thr_name = "mdt_fld",
4419 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4420 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4421 .tc_ctx_tags = LCT_DT_THREAD | LCT_MD_THREAD
4424 .so_req_handler = mdt_fld_handle,
4425 .so_req_printer = target_print_req,
4428 m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4429 if (IS_ERR(m->mdt_fld_service)) {
4430 rc = PTR_ERR(m->mdt_fld_service);
4431 CERROR("failed to start fld service: %d\n", rc);
4432 m->mdt_fld_service = NULL;
4434 GOTO(err_mdt_svc, rc);
4438 * mds-mds service configuration. Separate portal is used to allow
4439 * mds-mds requests be not blocked during recovery.
4441 memset(&conf, 0, sizeof(conf));
4442 conf = (typeof(conf)) {
4443 .psc_name = LUSTRE_MDT_NAME "_mds",
4444 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4446 .bc_nbufs = MDS_NBUFS,
4447 .bc_buf_size = MDS_BUFSIZE,
4448 .bc_req_max_size = MDS_MAXREQSIZE,
4449 .bc_rep_max_size = MDS_MAXREPSIZE,
4450 .bc_req_portal = MDS_MDS_PORTAL,
4451 .bc_rep_portal = MDC_REPLY_PORTAL,
4454 .tc_thr_name = "mdt_mds",
4455 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4456 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4457 .tc_ctx_tags = LCT_MD_THREAD,
4460 .so_req_handler = mdt_xmds_handle,
4461 .so_req_printer = target_print_req,
4462 .so_hpreq_handler = ptlrpc_hpreq_handler,
4465 m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4466 if (IS_ERR(m->mdt_xmds_service)) {
4467 rc = PTR_ERR(m->mdt_xmds_service);
4468 CERROR("failed to start xmds service: %d\n", rc);
4469 m->mdt_xmds_service = NULL;
4471 GOTO(err_mdt_svc, rc);
4477 mdt_stop_ptlrpc_service(m);
4482 static void mdt_stack_fini(const struct lu_env *env,
4483 struct mdt_device *m, struct lu_device *top)
4485 struct obd_device *obd = mdt2obd_dev(m);
4486 struct lustre_cfg_bufs *bufs;
4487 struct lustre_cfg *lcfg;
4488 struct mdt_thread_info *info;
4492 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4493 LASSERT(info != NULL);
4495 lu_dev_del_linkage(top->ld_site, top);
4497 lu_site_purge(env, top->ld_site, -1);
4499 bufs = &info->mti_u.bufs;
4500 /* process cleanup, pass mdt obd name to get obd umount flags */
4501 /* another purpose is to let all layers to release their objects */
4502 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4507 lustre_cfg_bufs_set_string(bufs, 1, flags);
4508 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4510 CERROR("Cannot alloc lcfg!\n");
4514 top->ld_ops->ldo_process_config(env, top, lcfg);
4515 lustre_cfg_free(lcfg);
4517 lu_site_purge(env, top->ld_site, -1);
4519 m->mdt_child = NULL;
4520 m->mdt_bottom = NULL;
4522 obd_disconnect(m->mdt_child_exp);
4523 m->mdt_child_exp = NULL;
4525 obd_disconnect(m->mdt_bottom_exp);
4526 m->mdt_child_exp = NULL;
4529 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4530 const char *next, struct obd_export **exp)
4532 struct obd_connect_data *data = NULL;
4533 struct obd_device *obd;
4537 OBD_ALLOC_PTR(data);
4539 GOTO(out, rc = -ENOMEM);
4541 obd = class_name2obd(next);
4543 CERROR("%s: can't locate next device: %s\n",
4544 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4545 GOTO(out, rc = -ENOTCONN);
4548 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4549 data->ocd_version = LUSTRE_VERSION_CODE;
4551 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4553 CERROR("%s: cannot connect to next dev %s (%d)\n",
4554 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4564 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4565 struct lustre_cfg *cfg)
4567 char *dev = lustre_cfg_string(cfg, 0);
4568 int rc, name_size, uuid_size;
4569 char *name, *uuid, *p;
4570 struct lustre_cfg_bufs *bufs;
4571 struct lustre_cfg *lcfg;
4572 struct obd_device *obd;
4573 struct lustre_profile *lprof;
4574 struct lu_site *site;
4577 /* in 1.8 we had the only device in the stack - MDS.
4578 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4579 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4580 * to generate names and setup MDT, MDD. MDT will be using
4581 * generated name to connect to MDD. for MDD the next device
4582 * will be LOD with name taken from so called "profile" which
4583 * is generated by mount_option line
4585 * 1.8 MGS generates config. commands like this:
4586 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4587 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4588 * 2.0 MGS generates config. commands like this:
4589 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4590 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4591 * 3:lustre-MDT0000-mdtlov 4:f
4593 * we generate MDD name from MDT one, just replacing T with D
4595 * after all the preparations, the logical equivalent will be
4596 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4597 * 3:lustre-MDT0000-mdtlov 4:f
4598 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4599 * 3:lustre-MDD0000 4:f
4601 * notice we build the stack from down to top: MDD first, then MDT */
4603 name_size = MAX_OBD_NAME;
4604 uuid_size = MAX_OBD_NAME;
4606 OBD_ALLOC(name, name_size);
4607 OBD_ALLOC(uuid, uuid_size);
4608 if (name == NULL || uuid == NULL)
4609 GOTO(cleanup_mem, rc = -ENOMEM);
4611 OBD_ALLOC_PTR(bufs);
4613 GOTO(cleanup_mem, rc = -ENOMEM);
4616 p = strstr(name, "-MDT");
4618 GOTO(cleanup_mem, rc = -ENOMEM);
4621 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4623 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4624 if (lprof == NULL || lprof->lp_dt == NULL) {
4625 CERROR("can't find the profile: %s\n",
4626 lustre_cfg_string(cfg, 0));
4627 GOTO(cleanup_mem, rc = -EINVAL);
4630 lustre_cfg_bufs_reset(bufs, name);
4631 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4632 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4633 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4635 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4637 GOTO(free_bufs, rc = -ENOMEM);
4639 rc = class_attach(lcfg);
4641 GOTO(lcfg_cleanup, rc);
4643 obd = class_name2obd(name);
4645 CERROR("Can not find obd %s (%s in config)\n",
4646 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4647 GOTO(class_detach, rc = -EINVAL);
4650 lustre_cfg_free(lcfg);
4652 lustre_cfg_bufs_reset(bufs, name);
4653 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4654 lustre_cfg_bufs_set_string(bufs, 2, dev);
4655 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4657 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4659 rc = class_setup(obd, lcfg);
4661 GOTO(class_detach, rc);
4663 /* connect to MDD we just setup */
4664 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4668 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4670 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4671 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4672 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4673 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4676 /* now connect to bottom OSD */
4677 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4678 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4682 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4685 rc = lu_env_refill((struct lu_env *)env);
4687 CERROR("Failure to refill session: '%d'\n", rc);
4689 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4694 class_detach(obd, lcfg);
4696 lustre_cfg_free(lcfg);
4701 OBD_FREE(name, name_size);
4703 OBD_FREE(uuid, uuid_size);
4707 /* setup quota master target on MDT0 */
4708 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4709 struct lustre_cfg *cfg)
4711 struct obd_device *obd;
4712 char *dev = lustre_cfg_string(cfg, 0);
4713 char *qmtname, *uuid, *p;
4714 struct lustre_cfg_bufs *bufs;
4715 struct lustre_cfg *lcfg;
4716 struct lustre_profile *lprof;
4717 struct obd_connect_data *data;
4721 LASSERT(mdt->mdt_qmt_exp == NULL);
4722 LASSERT(mdt->mdt_qmt_dev == NULL);
4724 /* quota master is on MDT0 only for now */
4725 if (mdt->mdt_mite.ms_node_id != 0)
4728 /* MGS generates config commands which look as follows:
4729 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4730 * 3:lustre-MDT0000-mdtlov 4:f
4732 * We generate the QMT name from the MDT one, just replacing MD with QM
4733 * after all the preparations, the logical equivalent will be:
4734 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4735 * 3:lustre-MDT0000-osd 4:f */
4736 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4737 OBD_ALLOC(uuid, UUID_MAX);
4738 OBD_ALLOC_PTR(bufs);
4739 OBD_ALLOC_PTR(data);
4740 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4741 GOTO(cleanup_mem, rc = -ENOMEM);
4743 strcpy(qmtname, dev);
4744 p = strstr(qmtname, "-MDT");
4746 GOTO(cleanup_mem, rc = -ENOMEM);
4747 /* replace MD with QM */
4751 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4753 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4754 if (lprof == NULL || lprof->lp_dt == NULL) {
4755 CERROR("can't find profile for %s\n",
4756 lustre_cfg_string(cfg, 0));
4757 GOTO(cleanup_mem, rc = -EINVAL);
4760 lustre_cfg_bufs_reset(bufs, qmtname);
4761 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4762 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4763 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4765 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4767 GOTO(cleanup_mem, rc = -ENOMEM);
4769 rc = class_attach(lcfg);
4771 GOTO(lcfg_cleanup, rc);
4773 obd = class_name2obd(qmtname);
4775 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4776 lustre_cfg_string(cfg, 0));
4777 GOTO(class_detach, rc = -EINVAL);
4780 lustre_cfg_free(lcfg);
4782 lustre_cfg_bufs_reset(bufs, qmtname);
4783 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4784 lustre_cfg_bufs_set_string(bufs, 2, dev);
4786 /* for quota, the next device should be the OSD device */
4787 lustre_cfg_bufs_set_string(bufs, 3,
4788 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4790 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4792 rc = class_setup(obd, lcfg);
4794 GOTO(class_detach, rc);
4796 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4798 /* configure local quota objects */
4799 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4800 &mdt->mdt_md_dev.md_lu_dev,
4803 GOTO(class_cleanup, rc);
4805 /* connect to quota master target */
4806 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4807 data->ocd_version = LUSTRE_VERSION_CODE;
4808 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4811 CERROR("cannot connect to quota master device %s (%d)\n",
4813 GOTO(class_cleanup, rc);
4819 class_manual_cleanup(obd);
4820 mdt->mdt_qmt_dev = NULL;
4824 class_detach(obd, lcfg);
4826 lustre_cfg_free(lcfg);
4831 OBD_FREE(qmtname, MAX_OBD_NAME);
4833 OBD_FREE(uuid, UUID_MAX);
4839 /* Shutdown quota master target associated with mdt */
4840 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4844 if (mdt->mdt_qmt_exp == NULL)
4846 LASSERT(mdt->mdt_qmt_dev != NULL);
4848 /* the qmt automatically shuts down when the mdt disconnects */
4849 obd_disconnect(mdt->mdt_qmt_exp);
4850 mdt->mdt_qmt_exp = NULL;
4851 mdt->mdt_qmt_dev = NULL;
4855 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4857 struct md_device *next = m->mdt_child;
4858 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4859 struct obd_device *obd = mdt2obd_dev(m);
4862 target_recovery_fini(obd);
4864 ping_evictor_stop();
4866 mdt_stop_ptlrpc_service(m);
4867 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4868 obd_exports_barrier(obd);
4869 obd_zombie_barrier();
4873 tgt_fini(env, &m->mdt_lut);
4874 mdt_fs_cleanup(env, m);
4875 upcall_cache_cleanup(m->mdt_identity_cache);
4876 m->mdt_identity_cache = NULL;
4878 if (m->mdt_namespace != NULL) {
4879 ldlm_namespace_free(m->mdt_namespace, NULL,
4880 d->ld_obd->obd_force);
4881 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4884 mdt_quota_fini(env, m);
4886 cfs_free_nidlist(&m->mdt_nosquash_nids);
4887 if (m->mdt_nosquash_str) {
4888 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4889 m->mdt_nosquash_str = NULL;
4890 m->mdt_nosquash_strlen = 0;
4893 mdt_seq_fini(env, m);
4894 mdt_seq_fini_cli(m);
4895 mdt_fld_fini(env, m);
4896 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4898 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4899 cfs_timer_disarm(&m->mdt_ck_timer);
4900 mdt_ck_thread_stop(m);
4905 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4907 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4909 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4914 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4916 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4917 struct sptlrpc_rule_set tmp_rset;
4920 sptlrpc_rule_set_init(&tmp_rset);
4921 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4923 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4928 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4930 cfs_write_lock(&m->mdt_sptlrpc_lock);
4931 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4932 m->mdt_sptlrpc_rset = tmp_rset;
4933 cfs_write_unlock(&m->mdt_sptlrpc_lock);
4938 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4940 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4941 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4943 struct mdt_thread_info *info;
4944 struct obd_device *obd;
4945 const char *dev = lustre_cfg_string(cfg, 0);
4946 const char *num = lustre_cfg_string(cfg, 2);
4947 struct lustre_mount_info *lmi = NULL;
4948 struct lustre_sb_info *lsi;
4950 struct md_site *mite;
4951 const char *identity_upcall = "NONE";
4952 struct md_device *next;
4958 md_device_init(&m->mdt_md_dev, ldt);
4960 * Environment (env) might be missing mdt_thread_key values at that
4961 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4964 * Usually device allocation path doesn't use module key values, but
4965 * mdt has to do a lot of work here, so allocate key value.
4967 rc = lu_env_refill((struct lu_env *)env);
4971 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4972 LASSERT(info != NULL);
4974 obd = class_name2obd(dev);
4975 LASSERT(obd != NULL);
4977 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4979 m->mdt_som_conf = 0;
4981 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4982 lmi = server_get_mount(dev);
4984 CERROR("Cannot get mount info for %s!\n", dev);
4987 lsi = s2lsi(lmi->lmi_sb);
4988 /* CMD is supported only in IAM mode */
4990 node_id = simple_strtol(num, NULL, 10);
4991 obd->u.obt.obt_magic = OBT_MAGIC;
4994 cfs_rwlock_init(&m->mdt_sptlrpc_lock);
4995 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4997 cfs_spin_lock_init(&m->mdt_ioepoch_lock);
4998 m->mdt_opts.mo_compat_resname = 0;
4999 m->mdt_opts.mo_mds_capa = 1;
5000 m->mdt_opts.mo_oss_capa = 1;
5001 m->mdt_capa_timeout = CAPA_TIMEOUT;
5002 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5003 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5004 m->mdt_squash_uid = 0;
5005 m->mdt_squash_gid = 0;
5006 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5007 m->mdt_nosquash_str = NULL;
5008 m->mdt_nosquash_strlen = 0;
5009 cfs_init_rwsem(&m->mdt_squash_sem);
5010 cfs_spin_lock_init(&m->mdt_osfs_lock);
5011 m->mdt_osfs_age = cfs_time_shift_64(-1000);
5013 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
5014 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
5015 /* set this lu_device to obd, because error handling need it */
5016 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
5018 /* init the stack */
5019 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5021 CERROR("Can't init device stack, rc %d\n", rc);
5025 s = m->mdt_md_dev.md_lu_dev.ld_site;
5026 mite = &m->mdt_mite;
5027 s->ld_md_site = mite;
5030 /* set server index */
5031 mite->ms_node_id = node_id;
5033 /* failover is the default
5034 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5035 * assumed whose ms_node_id == 0 XXX
5037 obd->obd_replayable = 1;
5038 /* No connection accepted until configurations will finish */
5039 obd->obd_no_conn = 1;
5041 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5042 char *str = lustre_cfg_string(cfg, 4);
5043 if (strchr(str, 'n')) {
5044 CWARN("%s: recovery disabled\n", obd->obd_name);
5045 obd->obd_replayable = 0;
5049 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
5051 GOTO(err_fini_stack, rc);
5053 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
5054 LUSTRE_MDT_NAME"-%p", m);
5055 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5056 LDLM_NAMESPACE_SERVER,
5057 LDLM_NAMESPACE_GREEDY,
5059 if (m->mdt_namespace == NULL)
5060 GOTO(err_fini_seq, rc = -ENOMEM);
5062 m->mdt_namespace->ns_lvbp = m;
5063 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5065 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5066 /* set obd_namespace for compatibility with old code */
5067 obd->obd_namespace = m->mdt_namespace;
5069 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5071 rc = mdt_ck_thread_start(m);
5073 GOTO(err_free_ns, rc);
5075 rc = mdt_fs_setup(env, m, obd, lsi);
5079 mdt_adapt_sptlrpc_conf(obd, 1);
5081 next = m->mdt_child;
5082 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5085 GOTO(err_llog_cleanup, rc);
5087 if (mntopts & MNTOPT_USERXATTR)
5088 m->mdt_opts.mo_user_xattr = 1;
5090 m->mdt_opts.mo_user_xattr = 0;
5092 if (mntopts & MNTOPT_ACL)
5093 m->mdt_opts.mo_acl = 1;
5095 m->mdt_opts.mo_acl = 0;
5097 /* XXX: to support suppgid for ACL, we enable identity_upcall
5098 * by default, otherwise, maybe got unexpected -EACCESS. */
5099 if (m->mdt_opts.mo_acl)
5100 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5102 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
5103 &mdt_identity_upcall_cache_ops);
5104 if (IS_ERR(m->mdt_identity_cache)) {
5105 rc = PTR_ERR(m->mdt_identity_cache);
5106 m->mdt_identity_cache = NULL;
5107 GOTO(err_llog_cleanup, rc);
5110 rc = mdt_procfs_init(m, dev);
5112 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5113 GOTO(err_recovery, rc);
5116 rc = mdt_quota_init(env, m, cfg);
5118 GOTO(err_procfs, rc);
5120 rc = mdt_start_ptlrpc_service(m);
5122 GOTO(err_quota, rc);
5124 ping_evictor_start();
5126 /* recovery will be started upon mdt_prepare()
5127 * when the whole stack is complete and ready
5128 * to serve the requests */
5130 mdt_init_capa_ctxt(env, m);
5132 /* Reduce the initial timeout on an MDS because it doesn't need such
5133 * a long timeout as an OST does. Adaptive timeouts will adjust this
5134 * value appropriately. */
5135 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5136 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5140 ping_evictor_stop();
5141 mdt_stop_ptlrpc_service(m);
5143 mdt_quota_fini(env, m);
5147 target_recovery_fini(obd);
5148 upcall_cache_cleanup(m->mdt_identity_cache);
5149 m->mdt_identity_cache = NULL;
5151 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5152 mdt_fs_cleanup(env, m);
5154 cfs_timer_disarm(&m->mdt_ck_timer);
5155 mdt_ck_thread_stop(m);
5157 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5158 obd->obd_namespace = m->mdt_namespace = NULL;
5160 mdt_seq_fini(env, m);
5161 mdt_fld_fini(env, m);
5162 tgt_fini(env, &m->mdt_lut);
5164 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5167 server_put_mount(dev, lmi->lmi_mnt);
5171 /* For interoperability, the left element is old parameter, the right one
5172 * is the new version of the parameter, if some parameter is deprecated,
5173 * the new version should be set as NULL. */
5174 static struct cfg_interop_param mdt_interop_param[] = {
5175 { "mdt.group_upcall", NULL },
5176 { "mdt.quota_type", NULL },
5177 { "mdd.quota_type", NULL },
5178 { "mdt.rootsquash", "mdt.root_squash" },
5179 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5183 /* used by MGS to process specific configurations */
5184 static int mdt_process_config(const struct lu_env *env,
5185 struct lu_device *d, struct lustre_cfg *cfg)
5187 struct mdt_device *m = mdt_dev(d);
5188 struct md_device *md_next = m->mdt_child;
5189 struct lu_device *next = md2lu_dev(md_next);
5193 switch (cfg->lcfg_command) {
5195 struct lprocfs_static_vars lvars;
5196 struct obd_device *obd = d->ld_obd;
5198 /* For interoperability */
5199 struct cfg_interop_param *ptr = NULL;
5200 struct lustre_cfg *old_cfg = NULL;
5203 param = lustre_cfg_string(cfg, 1);
5204 if (param == NULL) {
5205 CERROR("param is empty\n");
5210 ptr = class_find_old_param(param, mdt_interop_param);
5212 if (ptr->new_param == NULL) {
5214 CWARN("For interoperability, skip this %s."
5215 " It is obsolete.\n", ptr->old_param);
5219 CWARN("Found old param %s, changed it to %s.\n",
5220 ptr->old_param, ptr->new_param);
5223 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5230 lprocfs_mdt_init_vars(&lvars);
5231 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5233 if (rc > 0 || rc == -ENOSYS)
5234 /* we don't understand; pass it on */
5235 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5237 if (old_cfg != NULL)
5238 lustre_cfg_free(cfg);
5244 * Add mdc hook to get first MDT uuid and connect it to
5245 * ls->controller to use for seq manager.
5247 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5249 CERROR("Can't add mdc, rc %d\n", rc);
5251 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
5254 /* others are passed further */
5255 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5261 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5262 const struct lu_object_header *hdr,
5263 struct lu_device *d)
5265 struct mdt_object *mo;
5269 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
5271 struct lu_object *o;
5272 struct lu_object_header *h;
5274 o = &mo->mot_obj.mo_lu;
5275 h = &mo->mot_header;
5276 lu_object_header_init(h);
5277 lu_object_init(o, h, d);
5278 lu_object_add_top(h, o);
5279 o->lo_ops = &mdt_obj_ops;
5280 cfs_mutex_init(&mo->mot_ioepoch_mutex);
5281 cfs_mutex_init(&mo->mot_lov_mutex);
5287 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5288 const struct lu_object_conf *unused)
5290 struct mdt_device *d = mdt_dev(o->lo_dev);
5291 struct lu_device *under;
5292 struct lu_object *below;
5296 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5297 PFID(lu_object_fid(o)));
5299 under = &d->mdt_child->md_lu_dev;
5300 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5301 if (below != NULL) {
5302 lu_object_add(o, below);
5309 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5311 struct mdt_object *mo = mdt_obj(o);
5312 struct lu_object_header *h;
5316 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5317 PFID(lu_object_fid(o)));
5320 lu_object_header_fini(h);
5321 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5326 static int mdt_object_print(const struct lu_env *env, void *cookie,
5327 lu_printer_t p, const struct lu_object *o)
5329 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5330 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5331 "flags="LPX64", epochcount=%d, writecount=%d)",
5332 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5333 mdto->mot_ioepoch_count, mdto->mot_writecount);
5336 static int mdt_prepare(const struct lu_env *env,
5337 struct lu_device *pdev,
5338 struct lu_device *cdev)
5340 struct mdt_device *mdt = mdt_dev(cdev);
5341 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5342 struct obd_device *obd = cdev->ld_obd;
5349 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5353 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5357 rc = mdt_fld_init(env, obd->obd_name, mdt);
5361 rc = mdt_seq_init(env, obd->obd_name, mdt);
5365 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5366 &mdt->mdt_md_root_fid);
5370 LASSERT(!cfs_test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5371 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5372 cfs_set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5373 LASSERT(obd->obd_no_conn);
5374 cfs_spin_lock(&obd->obd_dev_lock);
5375 obd->obd_no_conn = 0;
5376 cfs_spin_unlock(&obd->obd_dev_lock);
5378 if (obd->obd_recovering == 0)
5379 mdt_postrecov(env, mdt);
5384 static const struct lu_device_operations mdt_lu_ops = {
5385 .ldo_object_alloc = mdt_object_alloc,
5386 .ldo_process_config = mdt_process_config,
5387 .ldo_prepare = mdt_prepare,
5390 static const struct lu_object_operations mdt_obj_ops = {
5391 .loo_object_init = mdt_object_init,
5392 .loo_object_free = mdt_object_free,
5393 .loo_object_print = mdt_object_print
5396 static int mdt_obd_set_info_async(const struct lu_env *env,
5397 struct obd_export *exp,
5398 __u32 keylen, void *key,
5399 __u32 vallen, void *val,
5400 struct ptlrpc_request_set *set)
5402 struct obd_device *obd = exp->exp_obd;
5408 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5409 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5417 * Match client and server connection feature flags.
5419 * Compute the compatibility flags for a connection request based on
5420 * features mutually supported by client and server.
5422 * The obd_export::exp_connect_flags field in \a exp must not be updated
5423 * here, otherwise a partially initialized value may be exposed. After
5424 * the connection request is successfully processed, the top-level MDT
5425 * connect request handler atomically updates the export connect flags
5426 * from the obd_connect_data::ocd_connect_flags field of the reply.
5427 * \see mdt_connect().
5429 * \param exp the obd_export associated with this client/target pair
5430 * \param mdt the target device for the connection
5431 * \param data stores data for this connect request
5434 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5435 * \retval -EBADE client and server feature requirements are incompatible
5437 static int mdt_connect_internal(struct obd_export *exp,
5438 struct mdt_device *mdt,
5439 struct obd_connect_data *data)
5441 LASSERT(data != NULL);
5443 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5444 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5446 /* If no known bits (which should not happen, probably,
5447 as everybody should support LOOKUP and UPDATE bits at least)
5448 revert to compat mode with plain locks. */
5449 if (!data->ocd_ibits_known &&
5450 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5451 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5453 if (!mdt->mdt_opts.mo_acl)
5454 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5456 if (!mdt->mdt_opts.mo_user_xattr)
5457 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5459 if (!mdt->mdt_som_conf)
5460 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5462 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5463 data->ocd_brw_size = min(data->ocd_brw_size,
5464 (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5465 if (data->ocd_brw_size == 0) {
5466 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5467 " ocd_version: %x ocd_grant: %d "
5468 "ocd_index: %u ocd_brw_size is "
5469 "unexpectedly zero, network data "
5470 "corruption? Refusing connection of this"
5472 exp->exp_obd->obd_name,
5473 exp->exp_client_uuid.uuid,
5474 exp, data->ocd_connect_flags, data->ocd_version,
5475 data->ocd_grant, data->ocd_index);
5480 /* NB: Disregard the rule against updating exp_connect_flags in this
5481 * case, since tgt_client_new() needs to know if this is a lightweight
5482 * connection, and it is safe to expose this flag before connection
5483 * processing completes. */
5484 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5485 cfs_spin_lock(&exp->exp_lock);
5486 exp->exp_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
5487 cfs_spin_unlock(&exp->exp_lock);
5490 data->ocd_version = LUSTRE_VERSION_CODE;
5491 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5493 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5494 CWARN("%s: MDS requires FID support, but client not\n",
5495 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5499 if (mdt->mdt_som_conf &&
5500 !(data->ocd_connect_flags & (OBD_CONNECT_MDS_MDS|OBD_CONNECT_SOM))){
5501 CWARN("%s: MDS has SOM enabled, but client does not support "
5502 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5509 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5510 struct obd_export *exp,
5511 struct ptlrpc_request *req)
5513 struct sptlrpc_flavor flvr;
5516 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5517 cfs_read_lock(&mdt->mdt_sptlrpc_lock);
5518 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5522 cfs_read_unlock(&mdt->mdt_sptlrpc_lock);
5524 cfs_spin_lock(&exp->exp_lock);
5526 exp->exp_sp_peer = req->rq_sp_from;
5527 exp->exp_flvr = flvr;
5529 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5530 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5531 CERROR("unauthorized rpc flavor %x from %s, "
5532 "expect %x\n", req->rq_flvr.sf_rpc,
5533 libcfs_nid2str(req->rq_peer.nid),
5534 exp->exp_flvr.sf_rpc);
5538 cfs_spin_unlock(&exp->exp_lock);
5540 if (exp->exp_sp_peer != req->rq_sp_from) {
5541 CERROR("RPC source %s doesn't match %s\n",
5542 sptlrpc_part2name(req->rq_sp_from),
5543 sptlrpc_part2name(exp->exp_sp_peer));
5546 rc = sptlrpc_target_export_check(exp, req);
5553 /* mds_connect copy */
5554 static int mdt_obd_connect(const struct lu_env *env,
5555 struct obd_export **exp, struct obd_device *obd,
5556 struct obd_uuid *cluuid,
5557 struct obd_connect_data *data,
5560 struct mdt_thread_info *info;
5561 struct obd_export *lexp;
5562 struct lustre_handle conn = { 0 };
5563 struct mdt_device *mdt;
5564 struct ptlrpc_request *req;
5568 LASSERT(env != NULL);
5569 if (!exp || !obd || !cluuid)
5572 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5573 req = info->mti_pill->rc_req;
5574 mdt = mdt_dev(obd->obd_lu_dev);
5577 * first, check whether the stack is ready to handle requests
5578 * XXX: probably not very appropriate method is used now
5579 * at some point we should find a better one
5581 if (!cfs_test_bit(MDT_FL_SYNCED, &mdt->mdt_state)) {
5582 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5585 cfs_set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5588 rc = class_connect(&conn, obd, cluuid);
5592 lexp = class_conn2export(&conn);
5593 LASSERT(lexp != NULL);
5595 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5599 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5600 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5602 rc = mdt_connect_internal(lexp, mdt, data);
5604 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5607 info->mti_exp = lexp;
5608 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5609 rc = tgt_client_new(env, lexp);
5611 mdt_export_stats_init(obd, lexp, localdata);
5616 class_disconnect(lexp);
5625 static int mdt_obd_reconnect(const struct lu_env *env,
5626 struct obd_export *exp, struct obd_device *obd,
5627 struct obd_uuid *cluuid,
5628 struct obd_connect_data *data,
5631 struct mdt_thread_info *info;
5632 struct mdt_device *mdt;
5633 struct ptlrpc_request *req;
5637 if (exp == NULL || obd == NULL || cluuid == NULL)
5640 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5641 req = info->mti_pill->rc_req;
5642 mdt = mdt_dev(obd->obd_lu_dev);
5644 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5648 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5650 mdt_export_stats_init(obd, exp, localdata);
5655 static int mdt_export_cleanup(struct obd_export *exp)
5657 struct mdt_export_data *med = &exp->exp_mdt_data;
5658 struct obd_device *obd = exp->exp_obd;
5659 struct mdt_device *mdt;
5660 struct mdt_thread_info *info;
5662 CFS_LIST_HEAD(closing_list);
5663 struct mdt_file_data *mfd, *n;
5667 cfs_spin_lock(&med->med_open_lock);
5668 while (!cfs_list_empty(&med->med_open_head)) {
5669 cfs_list_t *tmp = med->med_open_head.next;
5670 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5672 /* Remove mfd handle so it can't be found again.
5673 * We are consuming the mfd_list reference here. */
5674 class_handle_unhash(&mfd->mfd_handle);
5675 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5677 cfs_spin_unlock(&med->med_open_lock);
5678 mdt = mdt_dev(obd->obd_lu_dev);
5679 LASSERT(mdt != NULL);
5681 rc = lu_env_init(&env, LCT_MD_THREAD);
5685 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5686 LASSERT(info != NULL);
5687 memset(info, 0, sizeof *info);
5688 info->mti_env = &env;
5689 info->mti_mdt = mdt;
5690 info->mti_exp = exp;
5692 if (!cfs_list_empty(&closing_list)) {
5693 struct md_attr *ma = &info->mti_attr;
5695 /* Close any open files (which may also cause orphan unlinking). */
5696 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5697 cfs_list_del_init(&mfd->mfd_list);
5698 ma->ma_need = ma->ma_valid = 0;
5699 /* Don't unlink orphan on failover umount, LU-184 */
5700 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5701 ma->ma_valid = MA_FLAGS;
5702 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5704 mdt_mfd_close(info, mfd);
5707 info->mti_mdt = NULL;
5708 /* cleanup client slot early */
5709 /* Do not erase record for recoverable client. */
5710 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5711 tgt_client_del(&env, exp);
5717 static int mdt_obd_disconnect(struct obd_export *exp)
5723 class_export_get(exp);
5725 rc = server_disconnect_export(exp);
5727 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5729 rc = mdt_export_cleanup(exp);
5730 class_export_put(exp);
5734 /* FIXME: Can we avoid using these two interfaces? */
5735 static int mdt_init_export(struct obd_export *exp)
5737 struct mdt_export_data *med = &exp->exp_mdt_data;
5741 CFS_INIT_LIST_HEAD(&med->med_open_head);
5742 cfs_spin_lock_init(&med->med_open_lock);
5743 cfs_mutex_init(&med->med_idmap_mutex);
5744 med->med_idmap = NULL;
5745 cfs_spin_lock(&exp->exp_lock);
5746 exp->exp_connecting = 1;
5747 cfs_spin_unlock(&exp->exp_lock);
5749 /* self-export doesn't need client data and ldlm initialization */
5750 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5751 &exp->exp_client_uuid)))
5754 rc = tgt_client_alloc(exp);
5758 rc = ldlm_init_export(exp);
5765 tgt_client_free(exp);
5767 CERROR("%s: Failed to initialize export: rc = %d\n",
5768 exp->exp_obd->obd_name, rc);
5772 static int mdt_destroy_export(struct obd_export *exp)
5776 if (exp_connect_rmtclient(exp))
5777 mdt_cleanup_idmap(&exp->exp_mdt_data);
5779 target_destroy_export(exp);
5780 /* destroy can be called from failed obd_setup, so
5781 * checking uuid is safer than obd_self_export */
5782 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5783 &exp->exp_client_uuid)))
5786 ldlm_destroy_export(exp);
5787 tgt_client_free(exp);
5789 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5790 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5795 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5796 void *val, int vallen)
5798 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5799 struct getinfo_fid2path *fpout, *fpin;
5802 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5805 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5806 lustre_swab_fid2path(fpin);
5808 memcpy(fpout, fpin, sizeof(*fpin));
5809 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5812 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5816 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5817 struct getinfo_fid2path *fp)
5819 struct mdt_object *obj;
5820 struct obd_device *obd = mdt2obd_dev(mdt);
5824 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5825 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5827 if (!fid_is_sane(&fp->gf_fid))
5830 if (!fid_is_norm(&fp->gf_fid) && !fid_is_igif(&fp->gf_fid)) {
5831 CWARN("%s: "DFID" is invalid, sequence should be "
5832 ">= "LPX64"\n", obd->obd_name,
5833 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5837 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5838 if (obj == NULL || IS_ERR(obj)) {
5839 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5844 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5850 mdt_object_put(env, obj);
5851 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5852 PFID(&fp->gf_fid), rc);
5856 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5857 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5858 mdt_object_put(env, obj);
5863 static int mdt_get_info(struct mdt_thread_info *info)
5865 struct ptlrpc_request *req = mdt_info_req(info);
5873 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5875 CDEBUG(D_IOCTL, "No GETINFO key");
5878 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5881 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5882 if (vallen == NULL) {
5883 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5887 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5889 rc = req_capsule_server_pack(info->mti_pill);
5890 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5891 if (valout == NULL) {
5892 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5896 if (KEY_IS(KEY_FID2PATH))
5897 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5901 lustre_msg_set_status(req->rq_repmsg, rc);
5906 /* Pass the ioc down */
5907 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5908 unsigned int cmd, int len, void *data)
5910 struct lu_context ioctl_session;
5911 struct md_device *next = mdt->mdt_child;
5915 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5918 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5919 lu_context_enter(&ioctl_session);
5920 env->le_ses = &ioctl_session;
5922 LASSERT(next->md_ops->mdo_iocontrol);
5923 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5925 lu_context_exit(&ioctl_session);
5926 lu_context_fini(&ioctl_session);
5930 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5932 struct obd_ioctl_data *data = karg;
5933 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5935 struct mdt_object *obj;
5936 struct mdt_lock_handle *lh;
5940 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5941 if (!fid_is_sane(fid))
5944 lh = &mti->mti_lh[MDT_LH_PARENT];
5945 mdt_lock_reg_init(lh, LCK_CR);
5947 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5949 RETURN(PTR_ERR(obj));
5951 rc = mdt_object_exists(obj);
5955 * before calling version get the correct MDS should be
5956 * fid, this is error to find remote object here
5958 CERROR("nonlocal object "DFID"\n", PFID(fid));
5959 } else if (rc == 0) {
5960 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5963 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5964 *(__u64 *)data->ioc_inlbuf2 = version;
5967 mdt_object_unlock_put(mti, obj, lh, 1);
5971 /* ioctls on obd dev */
5972 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5973 void *karg, void *uarg)
5976 struct obd_device *obd = exp->exp_obd;
5977 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5978 struct dt_device *dt = mdt->mdt_bottom;
5982 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5983 rc = lu_env_init(&env, LCT_MD_THREAD);
5989 rc = mdt_device_sync(&env, mdt);
5991 case OBD_IOC_SET_READONLY:
5992 rc = dt->dd_ops->dt_ro(&env, dt);
5994 case OBD_IOC_ABORT_RECOVERY:
5995 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5996 target_stop_recovery_thread(obd);
5999 case OBD_IOC_CHANGELOG_REG:
6000 case OBD_IOC_CHANGELOG_DEREG:
6001 case OBD_IOC_CHANGELOG_CLEAR:
6002 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6004 case OBD_IOC_START_LFSCK:
6005 case OBD_IOC_STOP_LFSCK: {
6006 struct md_device *next = mdt->mdt_child;
6007 struct obd_ioctl_data *data = karg;
6009 if (unlikely(data == NULL)) {
6014 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
6019 case OBD_IOC_GET_OBJ_VERSION: {
6020 struct mdt_thread_info *mti;
6021 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6022 memset(mti, 0, sizeof *mti);
6023 mti->mti_env = &env;
6027 rc = mdt_ioc_version_get(mti, karg);
6031 CERROR("Not supported cmd = %d for device %s\n",
6032 cmd, obd->obd_name);
6040 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6042 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6046 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6050 int mdt_obd_postrecov(struct obd_device *obd)
6055 rc = lu_env_init(&env, LCT_MD_THREAD);
6058 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6064 * Send a copytool req to a client
6065 * Note this sends a request RPC from a server (MDT) to a client (MDC),
6066 * backwards of normal comms.
6068 int mdt_hsm_copytool_send(struct obd_export *exp)
6071 struct hsm_action_list *hal;
6072 struct hsm_action_item *hai;
6076 CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
6077 libcfs_nid2str(exp->exp_connection->c_peer.nid));
6079 len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
6080 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
6085 lh->kuc_magic = KUC_MAGIC;
6086 lh->kuc_transport = KUC_TRANSPORT_HSM;
6087 lh->kuc_msgtype = HMT_ACTION_LIST;
6088 lh->kuc_msglen = len;
6090 hal = (struct hsm_action_list *)(lh + 1);
6091 hal->hal_version = HAL_VERSION;
6092 hal->hal_archive_num = 1;
6093 obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
6096 /* mock up an action list */
6098 hai = hai_zero(hal);
6099 hai->hai_action = HSMA_ARCHIVE;
6100 hai->hai_fid.f_oid = 0xA00A;
6101 hai->hai_len = sizeof(*hai);
6102 hai = hai_next(hai);
6103 hai->hai_action = HSMA_RESTORE;
6104 hai->hai_fid.f_oid = 0xB00B;
6105 hai->hai_len = sizeof(*hai);
6107 /* Uses the ldlm reverse import; this rpc will be seen by
6108 the ldlm_callback_handler */
6109 rc = do_set_info_async(exp->exp_imp_reverse,
6110 LDLM_SET_INFO, LUSTRE_OBD_VERSION,
6111 sizeof(KEY_HSM_COPYTOOL_SEND),
6112 KEY_HSM_COPYTOOL_SEND,
6120 static struct obd_ops mdt_obd_device_ops = {
6121 .o_owner = THIS_MODULE,
6122 .o_set_info_async = mdt_obd_set_info_async,
6123 .o_connect = mdt_obd_connect,
6124 .o_reconnect = mdt_obd_reconnect,
6125 .o_disconnect = mdt_obd_disconnect,
6126 .o_init_export = mdt_init_export,
6127 .o_destroy_export = mdt_destroy_export,
6128 .o_iocontrol = mdt_iocontrol,
6129 .o_postrecov = mdt_obd_postrecov,
6132 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6133 struct lu_device *d)
6135 struct mdt_device *m = mdt_dev(d);
6142 static struct lu_device *mdt_device_free(const struct lu_env *env,
6143 struct lu_device *d)
6145 struct mdt_device *m = mdt_dev(d);
6148 md_device_fini(&m->mdt_md_dev);
6153 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6154 struct lu_device_type *t,
6155 struct lustre_cfg *cfg)
6157 struct lu_device *l;
6158 struct mdt_device *m;
6164 l = &m->mdt_md_dev.md_lu_dev;
6165 rc = mdt_init0(env, m, t, cfg);
6167 mdt_device_free(env, l);
6172 l = ERR_PTR(-ENOMEM);
6176 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6177 LU_KEY_INIT(mdt, struct mdt_thread_info);
6179 static void mdt_key_fini(const struct lu_context *ctx,
6180 struct lu_context_key *key, void* data)
6182 struct mdt_thread_info *info = data;
6184 if (info->mti_big_lmm) {
6185 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6186 info->mti_big_lmm = NULL;
6187 info->mti_big_lmmsize = 0;
6192 /* context key: mdt_thread_key */
6193 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6195 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
6197 return md_ucred(info->mti_env);
6201 * Enable/disable COS (Commit On Sharing).
6203 * Set/Clear the COS flag in mdt options.
6205 * \param mdt mdt device
6206 * \param val 0 disables COS, other values enable COS
6208 void mdt_enable_cos(struct mdt_device *mdt, int val)
6213 mdt->mdt_opts.mo_cos = !!val;
6214 rc = lu_env_init(&env, LCT_LOCAL);
6215 if (unlikely(rc != 0)) {
6216 CWARN("lu_env initialization failed with rc = %d,"
6217 "cannot sync\n", rc);
6220 mdt_device_sync(&env, mdt);
6225 * Check COS (Commit On Sharing) status.
6227 * Return COS flag status.
6229 * \param mdt mdt device
6231 int mdt_cos_is_enabled(struct mdt_device *mdt)
6233 return mdt->mdt_opts.mo_cos != 0;
6236 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
6237 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
6239 static struct lu_device_type_operations mdt_device_type_ops = {
6240 .ldto_init = mdt_type_init,
6241 .ldto_fini = mdt_type_fini,
6243 .ldto_start = mdt_type_start,
6244 .ldto_stop = mdt_type_stop,
6246 .ldto_device_alloc = mdt_device_alloc,
6247 .ldto_device_free = mdt_device_free,
6248 .ldto_device_fini = mdt_device_fini
6251 static struct lu_device_type mdt_device_type = {
6252 .ldt_tags = LU_DEVICE_MD,
6253 .ldt_name = LUSTRE_MDT_NAME,
6254 .ldt_ops = &mdt_device_type_ops,
6255 .ldt_ctx_tags = LCT_MD_THREAD
6258 static int __init mdt_mod_init(void)
6260 struct lprocfs_static_vars lvars;
6263 rc = lu_kmem_init(mdt_caches);
6267 if (mdt_num_threads != 0 && mds_num_threads == 0) {
6268 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
6269 "use mds_num_threads instead or unset both for"
6270 "dynamic thread startup\n");
6271 mds_num_threads = mdt_num_threads;
6274 lprocfs_mdt_init_vars(&lvars);
6275 rc = class_register_type(&mdt_obd_device_ops, NULL,
6276 lvars.module_vars, LUSTRE_MDT_NAME,
6280 lu_kmem_fini(mdt_caches);
6284 static void __exit mdt_mod_exit(void)
6286 class_unregister_type(LUSTRE_MDT_NAME);
6287 lu_kmem_fini(mdt_caches);
6290 #define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt) \
6293 .mh_fail_id = OBD_FAIL_ ## opc ## _NET, \
6295 .mh_flags = flags, \
6300 /* Request with a format known in advance */
6301 #define DEF_MDT_HDL(flags, name, fn) \
6302 DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
6304 /* Request with a format we do not yet know */
6305 #define DEF_MDT_HDL_VAR(flags, name, fn) \
6306 DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
6308 /* Map one non-standard request format handler. This should probably get
6309 * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
6310 #define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
6312 static struct mdt_handler mdt_mds_ops[] = {
6313 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6314 DEF_MDT_HDL(0, MDS_DISCONNECT, mdt_disconnect),
6315 DEF_MDT_HDL(0, MDS_SET_INFO, mdt_set_info),
6316 DEF_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
6317 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
6318 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
6319 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME, mdt_getattr_name),
6320 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_getxattr),
6321 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
6322 DEF_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
6323 DEF_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
6324 DEF_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING, mdt_done_writing),
6325 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_PIN, mdt_pin),
6326 DEF_MDT_HDL_VAR(0, MDS_SYNC, mdt_sync),
6327 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
6328 DEF_MDT_HDL(0, MDS_QUOTACHECK, mdt_quotacheck),
6329 DEF_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl)
6332 #define DEF_OBD_HDL(flags, name, fn) \
6333 DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
6335 static struct mdt_handler mdt_obd_ops[] = {
6336 DEF_OBD_HDL(0, OBD_PING, mdt_obd_ping),
6337 DEF_OBD_HDL(0, OBD_LOG_CANCEL, mdt_obd_log_cancel),
6338 DEF_OBD_HDL(0, OBD_QC_CALLBACK, mdt_obd_qc_callback),
6339 DEF_OBD_HDL(0, OBD_IDX_READ, mdt_obd_idx_read)
6342 #define DEF_DLM_HDL_VAR(flags, name, fn) \
6343 DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
6344 #define DEF_DLM_HDL(flags, name, fn) \
6345 DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
6347 static struct mdt_handler mdt_dlm_ops[] = {
6348 DEF_DLM_HDL (HABEO_CLAVIS, LDLM_ENQUEUE, mdt_enqueue),
6349 DEF_DLM_HDL_VAR(HABEO_CLAVIS, LDLM_CONVERT, mdt_convert),
6350 DEF_DLM_HDL_VAR(0, LDLM_BL_CALLBACK, mdt_bl_callback),
6351 DEF_DLM_HDL_VAR(0, LDLM_CP_CALLBACK, mdt_cp_callback)
6354 #define DEF_LLOG_HDL(flags, name, fn) \
6355 DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
6357 static struct mdt_handler mdt_llog_ops[] = {
6358 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_create),
6359 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
6360 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
6361 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_WRITE_REC, NULL),
6362 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CLOSE, NULL),
6363 DEF_LLOG_HDL(0, LLOG_ORIGIN_CONNECT, NULL),
6364 DEF_LLOG_HDL(0, LLOG_CATINFO, NULL),
6365 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
6366 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
6369 #define DEF_SEC_HDL(flags, name, fn) \
6370 DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
6372 static struct mdt_handler mdt_sec_ctx_ops[] = {
6373 DEF_SEC_HDL(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
6374 DEF_SEC_HDL(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
6375 DEF_SEC_HDL(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
6378 #define DEF_QUOTA_HDL(flags, name, fn) \
6379 DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
6381 static struct mdt_handler mdt_quota_ops[] = {
6382 DEF_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
6385 static struct mdt_opc_slice mdt_regular_handlers[] = {
6387 .mos_opc_start = MDS_GETATTR,
6388 .mos_opc_end = MDS_LAST_OPC,
6389 .mos_hs = mdt_mds_ops
6392 .mos_opc_start = OBD_PING,
6393 .mos_opc_end = OBD_LAST_OPC,
6394 .mos_hs = mdt_obd_ops
6397 .mos_opc_start = LDLM_ENQUEUE,
6398 .mos_opc_end = LDLM_LAST_OPC,
6399 .mos_hs = mdt_dlm_ops
6402 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6403 .mos_opc_end = LLOG_LAST_OPC,
6404 .mos_hs = mdt_llog_ops
6407 .mos_opc_start = SEC_CTX_INIT,
6408 .mos_opc_end = SEC_LAST_OPC,
6409 .mos_hs = mdt_sec_ctx_ops
6412 .mos_opc_start = QUOTA_DQACQ,
6413 .mos_opc_end = QUOTA_LAST_OPC,
6414 .mos_hs = mdt_quota_ops
6421 /* Readpage/readdir handlers */
6422 static struct mdt_handler mdt_readpage_ops[] = {
6423 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6424 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
6425 /* XXX: this is ugly and should be fixed one day, see mdc_close() for
6426 * detailed comments. --umka */
6427 DEF_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
6428 DEF_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING, mdt_done_writing),
6431 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6433 .mos_opc_start = MDS_GETATTR,
6434 .mos_opc_end = MDS_LAST_OPC,
6435 .mos_hs = mdt_readpage_ops
6438 .mos_opc_start = OBD_FIRST_OPC,
6439 .mos_opc_end = OBD_LAST_OPC,
6440 .mos_hs = mdt_obd_ops
6447 /* Cross MDT operation handlers for DNE */
6448 static struct mdt_handler mdt_xmds_ops[] = {
6449 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6450 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
6451 DEF_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
6452 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
6455 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6457 .mos_opc_start = MDS_GETATTR,
6458 .mos_opc_end = MDS_LAST_OPC,
6459 .mos_hs = mdt_xmds_ops
6462 .mos_opc_start = OBD_PING,
6463 .mos_opc_end = OBD_LAST_OPC,
6464 .mos_hs = mdt_obd_ops
6467 .mos_opc_start = SEC_CTX_INIT,
6468 .mos_opc_end = SEC_LAST_OPC,
6469 .mos_hs = mdt_sec_ctx_ops
6476 /* Sequence service handlers */
6477 #define DEF_SEQ_HDL(flags, name, fn) \
6478 DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
6480 static struct mdt_handler mdt_seq_ops[] = {
6481 DEF_SEQ_HDL(0, SEQ_QUERY, (void *)seq_query),
6484 static struct mdt_opc_slice mdt_seq_handlers[] = {
6486 .mos_opc_start = SEQ_QUERY,
6487 .mos_opc_end = SEQ_LAST_OPC,
6488 .mos_hs = mdt_seq_ops
6495 /* FID Location Database handlers */
6496 #define DEF_FLD_HDL(flags, name, fn) \
6497 DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
6499 static struct mdt_handler mdt_fld_ops[] = {
6500 DEF_FLD_HDL(0, FLD_QUERY, (void *)fld_query),
6503 static struct mdt_opc_slice mdt_fld_handlers[] = {
6505 .mos_opc_start = FLD_QUERY,
6506 .mos_opc_end = FLD_LAST_OPC,
6507 .mos_hs = mdt_fld_ops
6514 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6515 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6516 MODULE_LICENSE("GPL");
6518 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);