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 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 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
417 /** use cached statfs data */
418 spin_lock(&info->mti_mdt->mdt_osfs_lock);
419 *osfs = info->mti_mdt->mdt_osfs;
420 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_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
548 const struct lu_env *env = info->mti_env;
552 LASSERT(info->mti_big_lmm_used == 0);
553 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
557 /* big_lmm may need to be grown */
558 if (info->mti_big_lmmsize < rc) {
559 int size = size_roundup_power2(rc);
561 if (info->mti_big_lmmsize > 0) {
562 /* free old buffer */
563 LASSERT(info->mti_big_lmm);
564 OBD_FREE_LARGE(info->mti_big_lmm,
565 info->mti_big_lmmsize);
566 info->mti_big_lmm = NULL;
567 info->mti_big_lmmsize = 0;
570 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
571 if (info->mti_big_lmm == NULL)
573 info->mti_big_lmmsize = size;
575 LASSERT(info->mti_big_lmmsize >= rc);
577 info->mti_buf.lb_buf = info->mti_big_lmm;
578 info->mti_buf.lb_len = info->mti_big_lmmsize;
579 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
584 int mdt_attr_get_lov(struct mdt_thread_info *info,
585 struct mdt_object *o, struct md_attr *ma)
587 struct md_object *next = mdt_object_child(o);
588 struct lu_buf *buf = &info->mti_buf;
591 buf->lb_buf = ma->ma_lmm;
592 buf->lb_len = ma->ma_lmm_size;
593 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
595 ma->ma_lmm_size = rc;
596 ma->ma_valid |= MA_LOV;
598 } else if (rc == -ENODATA) {
601 } else if (rc == -ERANGE) {
602 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
604 info->mti_big_lmm_used = 1;
605 ma->ma_valid |= MA_LOV;
606 ma->ma_lmm = info->mti_big_lmm;
607 ma->ma_lmm_size = rc;
608 /* update mdt_max_mdsize so all clients
609 * will be aware about that */
610 if (info->mti_mdt->mdt_max_mdsize < rc)
611 info->mti_mdt->mdt_max_mdsize = rc;
619 int mdt_attr_get_pfid(struct mdt_thread_info *info,
620 struct mdt_object *o, struct lu_fid *pfid)
622 struct lu_buf *buf = &info->mti_buf;
623 struct link_ea_header *leh;
624 struct link_ea_entry *lee;
628 buf->lb_buf = info->mti_big_lmm;
629 buf->lb_len = info->mti_big_lmmsize;
630 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
631 buf, XATTR_NAME_LINK);
632 /* ignore errors, MA_PFID won't be set and it is
633 * up to the caller to treat this as an error */
634 if (rc == -ERANGE || buf->lb_len == 0) {
635 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
636 buf->lb_buf = info->mti_big_lmm;
637 buf->lb_len = info->mti_big_lmmsize;
642 if (rc < sizeof(*leh)) {
643 CERROR("short LinkEA on "DFID": rc = %d\n",
644 PFID(mdt_object_fid(o)), rc);
648 leh = (struct link_ea_header *) buf->lb_buf;
649 lee = (struct link_ea_entry *)(leh + 1);
650 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
651 leh->leh_magic = LINK_EA_MAGIC;
652 leh->leh_reccount = __swab32(leh->leh_reccount);
653 leh->leh_len = __swab64(leh->leh_len);
655 if (leh->leh_magic != LINK_EA_MAGIC)
657 if (leh->leh_reccount == 0)
660 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
661 fid_be_to_cpu(pfid, pfid);
666 int mdt_attr_get_complex(struct mdt_thread_info *info,
667 struct mdt_object *o, struct md_attr *ma)
669 const struct lu_env *env = info->mti_env;
670 struct md_object *next = mdt_object_child(o);
671 struct lu_buf *buf = &info->mti_buf;
672 u32 mode = lu_object_attr(&next->mo_lu);
673 int need = ma->ma_need;
679 if (need & MA_INODE) {
680 ma->ma_need = MA_INODE;
681 rc = mo_attr_get(env, next, ma);
684 ma->ma_valid |= MA_INODE;
687 if (need & MA_PFID) {
688 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
690 ma->ma_valid |= MA_PFID;
691 /* ignore this error, parent fid is not mandatory */
695 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
696 rc = mdt_attr_get_lov(info, o, ma);
701 if (need & MA_LMV && S_ISDIR(mode)) {
702 buf->lb_buf = ma->ma_lmv;
703 buf->lb_len = ma->ma_lmv_size;
704 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
706 ma->ma_lmv_size = rc2;
707 ma->ma_valid |= MA_LMV;
708 } else if (rc2 == -ENODATA) {
715 if (need & MA_SOM && S_ISREG(mode)) {
716 buf->lb_buf = info->mti_xattr_buf;
717 buf->lb_len = sizeof(info->mti_xattr_buf);
718 CLASSERT(sizeof(struct som_attrs) <=
719 sizeof(info->mti_xattr_buf));
720 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
721 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
723 ma->ma_valid |= MA_SOM;
724 else if (rc2 < 0 && rc2 != -ENODATA)
728 if (need & MA_HSM && S_ISREG(mode)) {
729 buf->lb_buf = info->mti_xattr_buf;
730 buf->lb_len = sizeof(info->mti_xattr_buf);
731 CLASSERT(sizeof(struct hsm_attrs) <=
732 sizeof(info->mti_xattr_buf));
733 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
734 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
736 ma->ma_valid |= MA_HSM;
737 else if (rc2 < 0 && rc2 != -ENODATA)
741 #ifdef CONFIG_FS_POSIX_ACL
742 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
743 buf->lb_buf = ma->ma_acl;
744 buf->lb_len = ma->ma_acl_size;
745 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
747 ma->ma_acl_size = rc2;
748 ma->ma_valid |= MA_ACL_DEF;
749 } else if (rc2 == -ENODATA) {
758 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
759 rc, ma->ma_valid, ma->ma_lmm);
763 static int mdt_getattr_internal(struct mdt_thread_info *info,
764 struct mdt_object *o, int ma_need)
766 struct md_object *next = mdt_object_child(o);
767 const struct mdt_body *reqbody = info->mti_body;
768 struct ptlrpc_request *req = mdt_info_req(info);
769 struct md_attr *ma = &info->mti_attr;
770 struct lu_attr *la = &ma->ma_attr;
771 struct req_capsule *pill = info->mti_pill;
772 const struct lu_env *env = info->mti_env;
773 struct mdt_body *repbody;
774 struct lu_buf *buffer = &info->mti_buf;
779 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
780 RETURN(err_serious(-ENOMEM));
782 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
786 rc = mdt_object_exists(o);
788 /* This object is located on remote node.*/
789 repbody->fid1 = *mdt_object_fid(o);
790 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
794 buffer->lb_len = reqbody->eadatasize;
795 if (buffer->lb_len > 0)
796 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
798 buffer->lb_buf = NULL;
800 /* If it is dir object and client require MEA, then we got MEA */
801 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
802 reqbody->valid & OBD_MD_MEA) {
803 /* Assumption: MDT_MD size is enough for lmv size. */
804 ma->ma_lmv = buffer->lb_buf;
805 ma->ma_lmv_size = buffer->lb_len;
806 ma->ma_need = MA_LMV | MA_INODE;
808 ma->ma_lmm = buffer->lb_buf;
809 ma->ma_lmm_size = buffer->lb_len;
810 ma->ma_need = MA_LOV | MA_INODE;
813 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
814 reqbody->valid & OBD_MD_FLDIREA &&
815 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
816 /* get default stripe info for this dir. */
817 ma->ma_need |= MA_LOV_DEF;
819 ma->ma_need |= ma_need;
820 if (ma->ma_need & MA_SOM)
821 ma->ma_som = &info->mti_u.som.data;
823 rc = mdt_attr_get_complex(info, o, ma);
825 CERROR("getattr error for "DFID": %d\n",
826 PFID(mdt_object_fid(o)), rc);
830 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
832 /* the Lustre protocol supposes to return default striping
833 * on the user-visible root if explicitly requested */
834 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
835 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
836 struct lu_fid rootfid;
837 struct mdt_object *root;
838 struct mdt_device *mdt = info->mti_mdt;
840 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
843 root = mdt_object_find(env, mdt, &rootfid);
845 RETURN(PTR_ERR(root));
846 rc = mdt_attr_get_lov(info, root, ma);
847 mdt_object_put(info->mti_env, root);
849 CERROR("getattr error for "DFID": %d\n",
850 PFID(mdt_object_fid(o)), rc);
855 if (likely(ma->ma_valid & MA_INODE))
856 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
860 if (mdt_body_has_lov(la, reqbody)) {
861 if (ma->ma_valid & MA_LOV) {
862 LASSERT(ma->ma_lmm_size);
863 mdt_dump_lmm(D_INFO, ma->ma_lmm);
864 repbody->eadatasize = ma->ma_lmm_size;
865 if (S_ISDIR(la->la_mode))
866 repbody->valid |= OBD_MD_FLDIREA;
868 repbody->valid |= OBD_MD_FLEASIZE;
870 if (ma->ma_valid & MA_LMV) {
871 LASSERT(S_ISDIR(la->la_mode));
872 repbody->eadatasize = ma->ma_lmv_size;
873 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
875 } else if (S_ISLNK(la->la_mode) &&
876 reqbody->valid & OBD_MD_LINKNAME) {
877 buffer->lb_buf = ma->ma_lmm;
878 /* eadatasize from client includes NULL-terminator, so
879 * there is no need to read it */
880 buffer->lb_len = reqbody->eadatasize - 1;
881 rc = mo_readlink(env, next, buffer);
882 if (unlikely(rc <= 0)) {
883 CERROR("readlink failed: %d\n", rc);
886 int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
888 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
890 repbody->valid |= OBD_MD_LINKNAME;
891 /* we need to report back size with NULL-terminator
892 * because client expects that */
893 repbody->eadatasize = rc + 1;
894 if (repbody->eadatasize != reqbody->eadatasize)
895 CERROR("Read shorter symlink %d, expected %d\n",
896 rc, reqbody->eadatasize - 1);
898 ((char *)ma->ma_lmm)[rc] = 0;
900 /* If the total CDEBUG() size is larger than a page, it
901 * will print a warning to the console, avoid this by
902 * printing just the last part of the symlink. */
903 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
904 print_limit < rc ? "..." : "", print_limit,
905 (char *)ma->ma_lmm + rc - print_limit, rc);
910 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
911 repbody->max_cookiesize = 0;
912 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
913 repbody->valid |= OBD_MD_FLMODEASIZE;
914 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
915 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
916 repbody->max_cookiesize);
919 if (exp_connect_rmtclient(info->mti_exp) &&
920 reqbody->valid & OBD_MD_FLRMTPERM) {
921 void *buf = req_capsule_server_get(pill, &RMF_ACL);
923 /* mdt_getattr_lock only */
924 rc = mdt_pack_remote_perm(info, o, buf);
926 repbody->valid &= ~OBD_MD_FLRMTPERM;
927 repbody->aclsize = 0;
930 repbody->valid |= OBD_MD_FLRMTPERM;
931 repbody->aclsize = sizeof(struct mdt_remote_perm);
934 #ifdef CONFIG_FS_POSIX_ACL
935 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
936 (reqbody->valid & OBD_MD_FLACL)) {
937 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
938 buffer->lb_len = req_capsule_get_size(pill,
939 &RMF_ACL, RCL_SERVER);
940 if (buffer->lb_len > 0) {
941 rc = mo_xattr_get(env, next, buffer,
942 XATTR_NAME_ACL_ACCESS);
944 if (rc == -ENODATA) {
945 repbody->aclsize = 0;
946 repbody->valid |= OBD_MD_FLACL;
948 } else if (rc == -EOPNOTSUPP) {
951 CERROR("got acl size: %d\n", rc);
954 repbody->aclsize = rc;
955 repbody->valid |= OBD_MD_FLACL;
962 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
963 info->mti_mdt->mdt_opts.mo_mds_capa &&
964 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
965 struct lustre_capa *capa;
967 capa = req_capsule_server_get(pill, &RMF_CAPA1);
969 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
970 rc = mo_capa_get(env, next, capa, 0);
973 repbody->valid |= OBD_MD_FLMDSCAPA;
978 mdt_counter_incr(req, LPROC_MDT_GETATTR);
983 static int mdt_renew_capa(struct mdt_thread_info *info)
985 struct mdt_object *obj = info->mti_object;
986 struct mdt_body *body;
987 struct lustre_capa *capa, *c;
991 /* if object doesn't exist, or server has disabled capability,
992 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
995 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
996 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
999 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1000 LASSERT(body != NULL);
1002 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1005 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1009 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1011 body->valid |= OBD_MD_FLOSSCAPA;
1015 static int mdt_getattr(struct mdt_thread_info *info)
1017 struct mdt_object *obj = info->mti_object;
1018 struct req_capsule *pill = info->mti_pill;
1019 struct mdt_body *reqbody;
1020 struct mdt_body *repbody;
1025 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1028 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1029 rc = req_capsule_server_pack(pill);
1031 RETURN(err_serious(rc));
1032 rc = mdt_renew_capa(info);
1033 GOTO(out_shrink, rc);
1036 LASSERT(obj != NULL);
1037 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
1039 mode = lu_object_attr(&obj->mot_obj.mo_lu);
1041 /* old clients may not report needed easize, use max value then */
1042 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1043 reqbody->eadatasize == 0 ?
1044 info->mti_mdt->mdt_max_mdsize :
1045 reqbody->eadatasize);
1047 rc = req_capsule_server_pack(pill);
1048 if (unlikely(rc != 0))
1049 RETURN(err_serious(rc));
1051 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1052 LASSERT(repbody != NULL);
1053 repbody->eadatasize = 0;
1054 repbody->aclsize = 0;
1056 if (reqbody->valid & OBD_MD_FLRMTPERM)
1057 rc = mdt_init_ucred(info, reqbody);
1059 rc = mdt_check_ucred(info);
1061 GOTO(out_shrink, rc);
1063 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1066 * Don't check capability at all, because rename might getattr for
1067 * remote obj, and at that time no capability is available.
1069 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1070 rc = mdt_getattr_internal(info, obj, 0);
1071 if (reqbody->valid & OBD_MD_FLRMTPERM)
1072 mdt_exit_ucred(info);
1075 mdt_client_compatibility(info);
1076 rc2 = mdt_fix_reply(info);
1082 static int mdt_is_subdir(struct mdt_thread_info *info)
1084 struct mdt_object *o = info->mti_object;
1085 struct req_capsule *pill = info->mti_pill;
1086 const struct mdt_body *body = info->mti_body;
1087 struct mdt_body *repbody;
1093 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1096 * We save last checked parent fid to @repbody->fid1 for remote
1099 LASSERT(fid_is_sane(&body->fid2));
1100 LASSERT(mdt_object_exists(o) > 0);
1101 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1102 &body->fid2, &repbody->fid1);
1103 if (rc == 0 || rc == -EREMOTE)
1104 repbody->valid |= OBD_MD_FLID;
1109 static int mdt_raw_lookup(struct mdt_thread_info *info,
1110 struct mdt_object *parent,
1111 const struct lu_name *lname,
1112 struct ldlm_reply *ldlm_rep)
1114 struct md_object *next = mdt_object_child(info->mti_object);
1115 const struct mdt_body *reqbody = info->mti_body;
1116 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1117 struct mdt_body *repbody;
1121 if (reqbody->valid != OBD_MD_FLID)
1124 LASSERT(!info->mti_cross_ref);
1126 /* Only got the fid of this obj by name */
1127 fid_zero(child_fid);
1128 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1131 /* XXX is raw_lookup possible as intent operation? */
1134 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1137 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1139 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1142 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1143 repbody->fid1 = *child_fid;
1144 repbody->valid = OBD_MD_FLID;
1150 * UPDATE lock should be taken against parent, and be release before exit;
1151 * child_bits lock should be taken against child, and be returned back:
1152 * (1)normal request should release the child lock;
1153 * (2)intent request will grant the lock to client.
1155 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1156 struct mdt_lock_handle *lhc,
1158 struct ldlm_reply *ldlm_rep)
1160 struct ptlrpc_request *req = mdt_info_req(info);
1161 struct mdt_body *reqbody = NULL;
1162 struct mdt_object *parent = info->mti_object;
1163 struct mdt_object *child;
1164 struct md_object *next = mdt_object_child(parent);
1165 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1166 struct lu_name *lname = NULL;
1167 const char *name = NULL;
1169 struct mdt_lock_handle *lhp = NULL;
1170 struct ldlm_lock *lock;
1171 struct ldlm_res_id *res_id;
1178 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1179 LASSERT(ergo(is_resent,
1180 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1182 LASSERT(parent != NULL);
1183 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1185 RETURN(err_serious(-EFAULT));
1187 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1189 if (!info->mti_cross_ref) {
1191 * XXX: Check for "namelen == 0" is for getattr by fid
1192 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1193 * that is the name must contain at least one character and
1194 * the terminating '\0'
1197 reqbody = req_capsule_client_get(info->mti_pill,
1199 if (unlikely(reqbody == NULL))
1200 RETURN(err_serious(-EFAULT));
1202 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1203 RETURN(err_serious(-EINVAL));
1206 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1208 PFID(mdt_object_fid(parent)),
1209 PFID(&reqbody->fid2), ldlm_rep);
1211 lname = mdt_name(info->mti_env, (char *)name, namelen);
1212 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1213 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1217 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1219 rc = mdt_object_exists(parent);
1220 if (unlikely(rc == 0)) {
1221 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1222 &parent->mot_obj.mo_lu,
1223 "Parent doesn't exist!\n");
1225 } else if (!info->mti_cross_ref) {
1226 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
1227 PFID(mdt_object_fid(parent)));
1230 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1238 if (info->mti_cross_ref) {
1239 /* Only getattr on the child. Parent is on another node. */
1240 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1242 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1243 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1246 /* Do not take lock for resent case. */
1247 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1248 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1249 lhc->mlh_reg_lh.cookie);
1250 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1251 &lock->l_resource->lr_name));
1252 LDLM_LOCK_PUT(lock);
1255 mdt_lock_handle_init(lhc);
1256 mdt_lock_reg_init(lhc, LCK_PR);
1259 * Object's name is on another MDS, no lookup lock is
1260 * needed here but update is.
1262 child_bits &= ~MDS_INODELOCK_LOOKUP;
1263 child_bits |= MDS_INODELOCK_UPDATE;
1265 rc = mdt_object_lock(info, child, lhc, child_bits,
1269 /* Finally, we can get attr for child. */
1270 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1272 rc = mdt_getattr_internal(info, child, 0);
1273 if (unlikely(rc != 0))
1274 mdt_object_unlock(info, child, lhc, 1);
1280 /* step 1: lock parent only if parent is a directory */
1281 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1282 lhp = &info->mti_lh[MDT_LH_PARENT];
1283 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1284 rc = mdt_object_lock(info, parent, lhp,
1285 MDS_INODELOCK_UPDATE,
1287 if (unlikely(rc != 0))
1291 /* step 2: lookup child's fid by name */
1292 fid_zero(child_fid);
1293 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1298 mdt_set_disposition(info, ldlm_rep,
1300 GOTO(out_parent, rc);
1302 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1304 *child_fid = reqbody->fid2;
1305 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1309 *step 3: find the child object by fid & lock it.
1310 * regardless if it is local or remote.
1312 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1314 if (unlikely(IS_ERR(child)))
1315 GOTO(out_parent, rc = PTR_ERR(child));
1317 /* Do not take lock for resent case. */
1318 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1319 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1320 lhc->mlh_reg_lh.cookie);
1322 res_id = &lock->l_resource->lr_name;
1323 if (!fid_res_name_eq(mdt_object_fid(child),
1324 &lock->l_resource->lr_name)) {
1325 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1326 &lock->l_resource->lr_name),
1327 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1328 (unsigned long)res_id->name[0],
1329 (unsigned long)res_id->name[1],
1330 (unsigned long)res_id->name[2],
1331 PFID(mdt_object_fid(parent)));
1332 CWARN("Although resent, but still not get child lock"
1333 "parent:"DFID" child:"DFID"\n",
1334 PFID(mdt_object_fid(parent)),
1335 PFID(mdt_object_fid(child)));
1336 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1337 LDLM_LOCK_PUT(lock);
1340 LDLM_LOCK_PUT(lock);
1344 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1345 mdt_lock_handle_init(lhc);
1346 if (child_bits == MDS_INODELOCK_LAYOUT)
1347 mdt_lock_reg_init(lhc, LCK_CR);
1349 mdt_lock_reg_init(lhc, LCK_PR);
1351 if (mdt_object_exists(child) == 0) {
1352 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1353 &child->mot_obj.mo_lu,
1354 "Object doesn't exist!\n");
1355 GOTO(out_child, rc = -ENOENT);
1358 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1359 struct md_attr *ma = &info->mti_attr;
1362 ma->ma_need = MA_INODE;
1363 rc = mdt_attr_get_complex(info, child, ma);
1364 if (unlikely(rc != 0))
1365 GOTO(out_child, rc);
1367 /* layout lock is used only on regular files */
1368 if ((ma->ma_valid & MA_INODE) &&
1369 (ma->ma_attr.la_valid & LA_MODE) &&
1370 !S_ISREG(ma->ma_attr.la_mode))
1371 child_bits &= ~MDS_INODELOCK_LAYOUT;
1373 /* If the file has not been changed for some time, we
1374 * return not only a LOOKUP lock, but also an UPDATE
1375 * lock and this might save us RPC on later STAT. For
1376 * directories, it also let negative dentry starts
1377 * working for this dir. */
1378 if (ma->ma_valid & MA_INODE &&
1379 ma->ma_attr.la_valid & LA_CTIME &&
1380 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1381 ma->ma_attr.la_ctime < cfs_time_current_sec())
1382 child_bits |= MDS_INODELOCK_UPDATE;
1385 rc = mdt_object_lock(info, child, lhc, child_bits,
1388 if (unlikely(rc != 0))
1389 GOTO(out_child, rc);
1392 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1393 /* Get MA_SOM attributes if update lock is given. */
1395 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1396 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1399 /* finally, we can get attr for child. */
1400 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1401 rc = mdt_getattr_internal(info, child, ma_need);
1402 if (unlikely(rc != 0)) {
1403 mdt_object_unlock(info, child, lhc, 1);
1405 /* Debugging code. */
1406 res_id = &lock->l_resource->lr_name;
1407 LDLM_DEBUG(lock, "Returning lock to client");
1408 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1409 &lock->l_resource->lr_name),
1410 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1411 (unsigned long)res_id->name[0],
1412 (unsigned long)res_id->name[1],
1413 (unsigned long)res_id->name[2],
1414 PFID(mdt_object_fid(child)));
1415 mdt_pack_size2body(info, child);
1418 LDLM_LOCK_PUT(lock);
1422 mdt_object_put(info->mti_env, child);
1425 mdt_object_unlock(info, parent, lhp, 1);
1429 /* normal handler: should release the child lock */
1430 static int mdt_getattr_name(struct mdt_thread_info *info)
1432 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1433 struct mdt_body *reqbody;
1434 struct mdt_body *repbody;
1438 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1439 LASSERT(reqbody != NULL);
1440 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1441 LASSERT(repbody != NULL);
1443 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1444 repbody->eadatasize = 0;
1445 repbody->aclsize = 0;
1447 rc = mdt_init_ucred(info, reqbody);
1449 GOTO(out_shrink, rc);
1451 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1452 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1453 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1454 lhc->mlh_reg_lh.cookie = 0;
1456 mdt_exit_ucred(info);
1459 mdt_client_compatibility(info);
1460 rc2 = mdt_fix_reply(info);
1466 static const struct lu_device_operations mdt_lu_ops;
1468 static int lu_device_is_mdt(struct lu_device *d)
1470 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1473 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1474 void *karg, void *uarg);
1476 static int mdt_set_info(struct mdt_thread_info *info)
1478 struct ptlrpc_request *req = mdt_info_req(info);
1481 int keylen, vallen, rc = 0;
1484 rc = req_capsule_server_pack(info->mti_pill);
1488 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1490 DEBUG_REQ(D_HA, req, "no set_info key");
1494 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1497 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1499 DEBUG_REQ(D_HA, req, "no set_info val");
1503 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1506 /* Swab any part of val you need to here */
1507 if (KEY_IS(KEY_READ_ONLY)) {
1509 lustre_msg_set_status(req->rq_repmsg, 0);
1511 spin_lock(&req->rq_export->exp_lock);
1513 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1515 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1516 spin_unlock(&req->rq_export->exp_lock);
1518 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1519 struct changelog_setinfo *cs =
1520 (struct changelog_setinfo *)val;
1521 if (vallen != sizeof(*cs)) {
1522 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1525 if (ptlrpc_req_need_swab(req)) {
1526 __swab64s(&cs->cs_recno);
1527 __swab32s(&cs->cs_id);
1530 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1532 lustre_msg_set_status(req->rq_repmsg, rc);
1541 * Top-level handler for MDT connection requests.
1543 static int mdt_connect(struct mdt_thread_info *info)
1546 struct obd_connect_data *reply;
1547 struct obd_export *exp;
1548 struct ptlrpc_request *req = mdt_info_req(info);
1550 rc = target_handle_connect(req);
1552 return err_serious(rc);
1554 LASSERT(req->rq_export != NULL);
1555 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1556 rc = mdt_init_sec_level(info);
1558 obd_disconnect(class_export_get(req->rq_export));
1562 /* To avoid exposing partially initialized connection flags, changes up
1563 * to this point have been staged in reply->ocd_connect_flags. Now that
1564 * connection handling has completed successfully, atomically update
1565 * the connect flags in the shared export data structure. LU-1623 */
1566 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1567 exp = req->rq_export;
1568 spin_lock(&exp->exp_lock);
1569 exp->exp_connect_flags = reply->ocd_connect_flags;
1570 spin_unlock(&exp->exp_lock);
1572 rc = mdt_init_idmap(info);
1574 obd_disconnect(class_export_get(req->rq_export));
1579 static int mdt_disconnect(struct mdt_thread_info *info)
1584 rc = target_handle_disconnect(mdt_info_req(info));
1586 rc = err_serious(rc);
1590 static int mdt_sendpage(struct mdt_thread_info *info,
1591 struct lu_rdpg *rdpg, int nob)
1593 struct ptlrpc_request *req = mdt_info_req(info);
1594 struct obd_export *exp = req->rq_export;
1595 struct ptlrpc_bulk_desc *desc;
1596 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1603 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1608 if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1609 /* old client requires reply size in it's PAGE_SIZE,
1610 * which is rdpg->rp_count */
1611 nob = rdpg->rp_count;
1613 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1614 i++, tmpcount -= tmpsize) {
1615 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1616 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1619 LASSERT(desc->bd_nob == nob);
1620 rc = target_bulk_io(exp, desc, lwi);
1621 ptlrpc_free_bulk_pin(desc);
1625 static int mdt_readpage(struct mdt_thread_info *info)
1627 struct mdt_object *object = info->mti_object;
1628 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1629 struct mdt_body *reqbody;
1630 struct mdt_body *repbody;
1635 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1636 RETURN(err_serious(-ENOMEM));
1638 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1639 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1640 if (reqbody == NULL || repbody == NULL)
1641 RETURN(err_serious(-EFAULT));
1644 * prepare @rdpg before calling lower layers and transfer itself. Here
1645 * reqbody->size contains offset of where to start to read and
1646 * reqbody->nlink contains number bytes to read.
1648 rdpg->rp_hash = reqbody->size;
1649 if (rdpg->rp_hash != reqbody->size) {
1650 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1651 rdpg->rp_hash, reqbody->size);
1655 rdpg->rp_attrs = reqbody->mode;
1656 if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1657 rdpg->rp_attrs |= LUDA_64BITHASH;
1658 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1659 PTLRPC_MAX_BRW_SIZE);
1660 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1662 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1663 if (rdpg->rp_pages == NULL)
1666 for (i = 0; i < rdpg->rp_npages; ++i) {
1667 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1668 if (rdpg->rp_pages[i] == NULL)
1669 GOTO(free_rdpg, rc = -ENOMEM);
1672 /* call lower layers to fill allocated pages with directory data */
1673 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1675 GOTO(free_rdpg, rc);
1677 /* send pages to client */
1678 rc = mdt_sendpage(info, rdpg, rc);
1683 for (i = 0; i < rdpg->rp_npages; i++)
1684 if (rdpg->rp_pages[i] != NULL)
1685 cfs_free_page(rdpg->rp_pages[i]);
1686 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1688 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1694 static int mdt_reint_internal(struct mdt_thread_info *info,
1695 struct mdt_lock_handle *lhc,
1698 struct req_capsule *pill = info->mti_pill;
1699 struct mdt_body *repbody;
1704 rc = mdt_reint_unpack(info, op);
1706 CERROR("Can't unpack reint, rc %d\n", rc);
1707 RETURN(err_serious(rc));
1710 /* for replay (no_create) lmm is not needed, client has it already */
1711 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1712 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1713 info->mti_rr.rr_eadatalen);
1715 /* llog cookies are always 0, the field is kept for compatibility */
1716 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1717 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1719 rc = req_capsule_server_pack(pill);
1721 CERROR("Can't pack response, rc %d\n", rc);
1722 RETURN(err_serious(rc));
1725 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1726 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1728 repbody->eadatasize = 0;
1729 repbody->aclsize = 0;
1732 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1734 /* for replay no cookkie / lmm need, because client have this already */
1735 if (info->mti_spec.no_create)
1736 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1737 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1739 rc = mdt_init_ucred_reint(info);
1741 GOTO(out_shrink, rc);
1743 rc = mdt_fix_attr_ucred(info, op);
1745 GOTO(out_ucred, rc = err_serious(rc));
1747 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1748 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1749 GOTO(out_ucred, rc);
1751 rc = mdt_reint_rec(info, lhc);
1754 mdt_exit_ucred(info);
1756 mdt_client_compatibility(info);
1757 rc2 = mdt_fix_reply(info);
1763 static long mdt_reint_opcode(struct mdt_thread_info *info,
1764 const struct req_format **fmt)
1766 struct mdt_rec_reint *rec;
1769 opc = err_serious(-EFAULT);
1770 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1772 opc = rec->rr_opcode;
1773 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1774 if (opc < REINT_MAX && fmt[opc] != NULL)
1775 req_capsule_extend(info->mti_pill, fmt[opc]);
1777 CERROR("Unsupported opc: %ld\n", opc);
1778 opc = err_serious(opc);
1784 static int mdt_reint(struct mdt_thread_info *info)
1789 static const struct req_format *reint_fmts[REINT_MAX] = {
1790 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1791 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1792 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1793 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1794 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1795 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1796 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1801 opc = mdt_reint_opcode(info, reint_fmts);
1804 * No lock possible here from client to pass it to reint code
1807 rc = mdt_reint_internal(info, NULL, opc);
1812 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1816 /* this should sync the whole device */
1817 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1819 struct dt_device *dt = mdt->mdt_bottom;
1823 rc = dt->dd_ops->dt_sync(env, dt);
1827 /* this should sync this object */
1828 static int mdt_object_sync(struct mdt_thread_info *info)
1830 struct md_object *next;
1834 if (!mdt_object_exists(info->mti_object)) {
1835 CWARN("Non existing object "DFID"!\n",
1836 PFID(mdt_object_fid(info->mti_object)));
1839 next = mdt_object_child(info->mti_object);
1840 rc = mo_object_sync(info->mti_env, next);
1845 static int mdt_sync(struct mdt_thread_info *info)
1847 struct ptlrpc_request *req = mdt_info_req(info);
1848 struct req_capsule *pill = info->mti_pill;
1849 struct mdt_body *body;
1853 /* The fid may be zero, so we req_capsule_set manually */
1854 req_capsule_set(pill, &RQF_MDS_SYNC);
1856 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1858 RETURN(err_serious(-EINVAL));
1860 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1861 RETURN(err_serious(-ENOMEM));
1863 if (fid_seq(&body->fid1) == 0) {
1864 /* sync the whole device */
1865 rc = req_capsule_server_pack(pill);
1867 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1869 rc = err_serious(rc);
1871 /* sync an object */
1872 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1874 rc = mdt_object_sync(info);
1876 const struct lu_fid *fid;
1877 struct lu_attr *la = &info->mti_attr.ma_attr;
1879 info->mti_attr.ma_need = MA_INODE;
1880 info->mti_attr.ma_valid = 0;
1881 rc = mdt_attr_get_complex(info, info->mti_object,
1884 body = req_capsule_server_get(pill,
1886 fid = mdt_object_fid(info->mti_object);
1887 mdt_pack_attr2body(info, body, la, fid);
1891 rc = err_serious(rc);
1894 mdt_counter_incr(req, LPROC_MDT_SYNC);
1900 * Quotacheck handler.
1901 * in-kernel quotacheck isn't supported any more.
1903 static int mdt_quotacheck(struct mdt_thread_info *info)
1905 struct obd_quotactl *oqctl;
1909 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1911 RETURN(err_serious(-EPROTO));
1913 rc = req_capsule_server_pack(info->mti_pill);
1915 RETURN(err_serious(rc));
1917 /* deprecated, not used any more */
1918 RETURN(-EOPNOTSUPP);
1922 * Handle quota control requests to consult current usage/limit, but also
1923 * to configure quota enforcement
1925 static int mdt_quotactl(struct mdt_thread_info *info)
1927 struct obd_export *exp = info->mti_exp;
1928 struct req_capsule *pill = info->mti_pill;
1929 struct obd_quotactl *oqctl, *repoqc;
1931 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1934 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1936 RETURN(err_serious(-EPROTO));
1938 rc = req_capsule_server_pack(pill);
1940 RETURN(err_serious(rc));
1942 switch (oqctl->qc_cmd) {
1944 case LUSTRE_Q_INVALIDATE:
1945 case LUSTRE_Q_FINVALIDATE:
1949 /* deprecated, not used any more */
1950 RETURN(-EOPNOTSUPP);
1951 /* master quotactl */
1957 RETURN(-EOPNOTSUPP);
1958 /* slave quotactl */
1963 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1967 /* map uid/gid for remote client */
1969 if (exp_connect_rmtclient(exp)) {
1970 struct lustre_idmap_table *idmap;
1972 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1974 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1975 oqctl->qc_cmd != Q_GETINFO))
1978 if (oqctl->qc_type == USRQUOTA)
1979 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1981 else if (oqctl->qc_type == GRPQUOTA)
1982 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1987 if (id == CFS_IDMAP_NOTFOUND) {
1988 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1993 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1995 RETURN(err_serious(-EFAULT));
1997 if (oqctl->qc_id != id)
1998 swap(oqctl->qc_id, id);
2000 switch (oqctl->qc_cmd) {
2006 /* forward quotactl request to QMT */
2007 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2012 /* slave quotactl */
2013 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2018 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2022 if (oqctl->qc_id != id)
2023 swap(oqctl->qc_id, id);
2030 * OBD PING and other handlers.
2032 static int mdt_obd_ping(struct mdt_thread_info *info)
2037 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2039 rc = target_handle_ping(mdt_info_req(info));
2041 rc = err_serious(rc);
2046 * OBD_IDX_READ handler
2048 static int mdt_obd_idx_read(struct mdt_thread_info *info)
2050 struct mdt_device *mdt = info->mti_mdt;
2051 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2052 struct idx_info *req_ii, *rep_ii;
2056 memset(rdpg, 0, sizeof(*rdpg));
2057 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2059 /* extract idx_info buffer from request & reply */
2060 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2061 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2062 RETURN(err_serious(-EPROTO));
2064 rc = req_capsule_server_pack(info->mti_pill);
2066 RETURN(err_serious(rc));
2068 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2070 RETURN(err_serious(-EFAULT));
2071 rep_ii->ii_magic = IDX_INFO_MAGIC;
2073 /* extract hash to start with */
2074 rdpg->rp_hash = req_ii->ii_hash_start;
2076 /* extract requested attributes */
2077 rdpg->rp_attrs = req_ii->ii_attrs;
2079 /* check that fid packed in request is valid and supported */
2080 if (!fid_is_sane(&req_ii->ii_fid))
2082 rep_ii->ii_fid = req_ii->ii_fid;
2085 rep_ii->ii_flags = req_ii->ii_flags;
2087 /* compute number of pages to allocate, ii_count is the number of 4KB
2089 if (req_ii->ii_count <= 0)
2090 GOTO(out, rc = -EFAULT);
2091 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2092 PTLRPC_MAX_BRW_SIZE);
2093 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2095 /* allocate pages to store the containers */
2096 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2097 if (rdpg->rp_pages == NULL)
2098 GOTO(out, rc = -ENOMEM);
2099 for (i = 0; i < rdpg->rp_npages; i++) {
2100 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2101 if (rdpg->rp_pages[i] == NULL)
2102 GOTO(out, rc = -ENOMEM);
2105 /* populate pages with key/record pairs */
2106 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2110 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2111 "asked %d > %d\n", rc, rdpg->rp_count);
2113 /* send pages to client */
2114 rc = mdt_sendpage(info, rdpg, rc);
2118 if (rdpg->rp_pages) {
2119 for (i = 0; i < rdpg->rp_npages; i++)
2120 if (rdpg->rp_pages[i])
2121 cfs_free_page(rdpg->rp_pages[i]);
2122 OBD_FREE(rdpg->rp_pages,
2123 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2128 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
2130 return err_serious(-EOPNOTSUPP);
2133 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
2135 return err_serious(-EOPNOTSUPP);
2143 /** clone llog ctxt from child (mdd)
2144 * This allows remote llog (replicator) access.
2145 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2146 * context was originally set up, or we can handle them directly.
2147 * I choose the latter, but that means I need any llog
2148 * contexts set up by child to be accessable by the mdt. So we clone the
2149 * context into our context list here.
2151 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2154 struct md_device *next = mdt->mdt_child;
2155 struct llog_ctxt *ctxt;
2158 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2161 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2162 if (rc || ctxt == NULL) {
2166 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2168 CERROR("Can't set mdt ctxt %d\n", rc);
2173 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2174 struct mdt_device *mdt, int idx)
2176 struct llog_ctxt *ctxt;
2178 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2181 /* Put once for the get we just did, and once for the clone */
2182 llog_ctxt_put(ctxt);
2183 llog_ctxt_put(ctxt);
2187 static int mdt_llog_create(struct mdt_thread_info *info)
2191 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2192 rc = llog_origin_handle_open(mdt_info_req(info));
2193 return (rc < 0 ? err_serious(rc) : rc);
2196 static int mdt_llog_destroy(struct mdt_thread_info *info)
2200 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2201 rc = llog_origin_handle_destroy(mdt_info_req(info));
2202 return (rc < 0 ? err_serious(rc) : rc);
2205 static int mdt_llog_read_header(struct mdt_thread_info *info)
2209 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2210 rc = llog_origin_handle_read_header(mdt_info_req(info));
2211 return (rc < 0 ? err_serious(rc) : rc);
2214 static int mdt_llog_next_block(struct mdt_thread_info *info)
2218 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2219 rc = llog_origin_handle_next_block(mdt_info_req(info));
2220 return (rc < 0 ? err_serious(rc) : rc);
2223 static int mdt_llog_prev_block(struct mdt_thread_info *info)
2227 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2228 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2229 return (rc < 0 ? err_serious(rc) : rc);
2236 static struct ldlm_callback_suite cbs = {
2237 .lcs_completion = ldlm_server_completion_ast,
2238 .lcs_blocking = ldlm_server_blocking_ast,
2239 .lcs_glimpse = ldlm_server_glimpse_ast
2242 static int mdt_enqueue(struct mdt_thread_info *info)
2244 struct ptlrpc_request *req;
2248 * info->mti_dlm_req already contains swapped and (if necessary)
2249 * converted dlm request.
2251 LASSERT(info->mti_dlm_req != NULL);
2253 req = mdt_info_req(info);
2254 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2255 req, info->mti_dlm_req, &cbs);
2256 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2257 return rc ? err_serious(rc) : req->rq_status;
2260 static int mdt_convert(struct mdt_thread_info *info)
2263 struct ptlrpc_request *req;
2265 LASSERT(info->mti_dlm_req);
2266 req = mdt_info_req(info);
2267 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2268 return rc ? err_serious(rc) : req->rq_status;
2271 static int mdt_bl_callback(struct mdt_thread_info *info)
2273 CERROR("bl callbacks should not happen on MDS\n");
2275 return err_serious(-EOPNOTSUPP);
2278 static int mdt_cp_callback(struct mdt_thread_info *info)
2280 CERROR("cp callbacks should not happen on MDS\n");
2282 return err_serious(-EOPNOTSUPP);
2286 * sec context handlers
2288 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2292 rc = mdt_handle_idmap(info);
2295 struct ptlrpc_request *req = mdt_info_req(info);
2298 opc = lustre_msg_get_opc(req->rq_reqmsg);
2299 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2300 sptlrpc_svc_ctx_invalidate(req);
2303 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2309 * quota request handlers
2311 static int mdt_quota_dqacq(struct mdt_thread_info *info)
2313 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2318 RETURN(err_serious(-EOPNOTSUPP));
2320 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2324 static struct mdt_object *mdt_obj(struct lu_object *o)
2326 LASSERT(lu_device_is_mdt(o->lo_dev));
2327 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2330 struct mdt_object *mdt_object_new(const struct lu_env *env,
2331 struct mdt_device *d,
2332 const struct lu_fid *f)
2334 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2335 struct lu_object *o;
2336 struct mdt_object *m;
2339 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2340 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2341 if (unlikely(IS_ERR(o)))
2342 m = (struct mdt_object *)o;
2348 struct mdt_object *mdt_object_find(const struct lu_env *env,
2349 struct mdt_device *d,
2350 const struct lu_fid *f)
2352 struct lu_object *o;
2353 struct mdt_object *m;
2356 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2357 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2358 if (unlikely(IS_ERR(o)))
2359 m = (struct mdt_object *)o;
2366 * Asyncronous commit for mdt device.
2368 * Pass asynchonous commit call down the MDS stack.
2370 * \param env environment
2371 * \param mdt the mdt device
2373 static void mdt_device_commit_async(const struct lu_env *env,
2374 struct mdt_device *mdt)
2376 struct dt_device *dt = mdt->mdt_bottom;
2379 rc = dt->dd_ops->dt_commit_async(env, dt);
2380 if (unlikely(rc != 0))
2381 CWARN("async commit start failed with rc = %d", rc);
2385 * Mark the lock as "synchonous".
2387 * Mark the lock to deffer transaction commit to the unlock time.
2389 * \param lock the lock to mark as "synchonous"
2391 * \see mdt_is_lock_sync
2392 * \see mdt_save_lock
2394 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2396 lock->l_ast_data = (void*)1;
2400 * Check whehter the lock "synchonous" or not.
2402 * \param lock the lock to check
2403 * \retval 1 the lock is "synchonous"
2404 * \retval 0 the lock isn't "synchronous"
2406 * \see mdt_set_lock_sync
2407 * \see mdt_save_lock
2409 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2411 return lock->l_ast_data != NULL;
2415 * Blocking AST for mdt locks.
2417 * Starts transaction commit if in case of COS lock conflict or
2418 * deffers such a commit to the mdt_save_lock.
2420 * \param lock the lock which blocks a request or cancelling lock
2421 * \param desc unused
2422 * \param data unused
2423 * \param flag indicates whether this cancelling or blocking callback
2425 * \see ldlm_blocking_ast_nocheck
2427 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2428 void *data, int flag)
2430 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2431 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2435 if (flag == LDLM_CB_CANCELING)
2437 lock_res_and_lock(lock);
2438 if (lock->l_blocking_ast != mdt_blocking_ast) {
2439 unlock_res_and_lock(lock);
2442 if (mdt_cos_is_enabled(mdt) &&
2443 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2444 lock->l_blocking_lock != NULL &&
2445 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2446 mdt_set_lock_sync(lock);
2448 rc = ldlm_blocking_ast_nocheck(lock);
2450 /* There is no lock conflict if l_blocking_lock == NULL,
2451 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2452 * when the last reference to a local lock was released */
2453 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2456 rc = lu_env_init(&env, LCT_LOCAL);
2457 if (unlikely(rc != 0))
2458 CWARN("lu_env initialization failed with rc = %d,"
2459 "cannot start asynchronous commit\n", rc);
2461 mdt_device_commit_async(&env, mdt);
2467 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2468 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2470 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2471 ldlm_policy_data_t *policy = &info->mti_policy;
2472 struct ldlm_res_id *res_id = &info->mti_res_id;
2476 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2477 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2478 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2479 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2481 if (mdt_object_exists(o) < 0) {
2482 if (locality == MDT_CROSS_LOCK) {
2483 /* cross-ref object fix */
2484 ibits &= ~MDS_INODELOCK_UPDATE;
2485 ibits |= MDS_INODELOCK_LOOKUP;
2487 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2488 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2490 /* No PDO lock on remote object */
2491 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2494 if (lh->mlh_type == MDT_PDO_LOCK) {
2495 /* check for exists after object is locked */
2496 if (mdt_object_exists(o) == 0) {
2497 /* Non-existent object shouldn't have PDO lock */
2500 /* Non-dir object shouldn't have PDO lock */
2501 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2506 memset(policy, 0, sizeof(*policy));
2507 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2510 * Take PDO lock on whole directory and build correct @res_id for lock
2511 * on part of directory.
2513 if (lh->mlh_pdo_hash != 0) {
2514 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2515 mdt_lock_pdo_mode(info, o, lh);
2516 if (lh->mlh_pdo_mode != LCK_NL) {
2518 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2519 * is never going to be sent to client and we do not
2520 * want it slowed down due to possible cancels.
2522 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2523 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2524 policy, res_id, LDLM_FL_ATOMIC_CB,
2525 &info->mti_exp->exp_handle.h_cookie);
2531 * Finish res_id initializing by name hash marking part of
2532 * directory which is taking modification.
2534 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2537 policy->l_inodebits.bits = ibits;
2540 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2541 * going to be sent to client. If it is - mdt_intent_policy() path will
2542 * fix it up and turn FL_LOCAL flag off.
2544 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2545 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2546 &info->mti_exp->exp_handle.h_cookie);
2548 mdt_object_unlock(info, o, lh, 1);
2549 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2550 lh->mlh_pdo_hash != 0 &&
2551 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2552 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2559 * Save a lock within request object.
2561 * Keep the lock referenced until whether client ACK or transaction
2562 * commit happens or release the lock immediately depending on input
2563 * parameters. If COS is ON, a write lock is converted to COS lock
2566 * \param info thead info object
2567 * \param h lock handle
2568 * \param mode lock mode
2569 * \param decref force immediate lock releasing
2572 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2573 ldlm_mode_t mode, int decref)
2577 if (lustre_handle_is_used(h)) {
2578 if (decref || !info->mti_has_trans ||
2579 !(mode & (LCK_PW | LCK_EX))){
2580 mdt_fid_unlock(h, mode);
2582 struct mdt_device *mdt = info->mti_mdt;
2583 struct ldlm_lock *lock = ldlm_handle2lock(h);
2584 struct ptlrpc_request *req = mdt_info_req(info);
2587 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2589 CDEBUG(D_HA, "request = %p reply state = %p"
2590 " transno = "LPD64"\n",
2591 req, req->rq_reply_state, req->rq_transno);
2592 if (mdt_cos_is_enabled(mdt)) {
2594 ldlm_lock_downgrade(lock, LCK_COS);
2597 ptlrpc_save_lock(req, h, mode, no_ack);
2598 if (mdt_is_lock_sync(lock)) {
2599 CDEBUG(D_HA, "found sync-lock,"
2600 " async commit started\n");
2601 mdt_device_commit_async(info->mti_env,
2604 LDLM_LOCK_PUT(lock);
2613 * Unlock mdt object.
2615 * Immeditely release the regular lock and the PDO lock or save the
2616 * lock in reqeuest and keep them referenced until client ACK or
2617 * transaction commit.
2619 * \param info thread info object
2620 * \param o mdt object
2621 * \param lh mdt lock handle referencing regular and PDO locks
2622 * \param decref force immediate lock releasing
2624 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2625 struct mdt_lock_handle *lh, int decref)
2629 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2630 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2635 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2636 const struct lu_fid *f,
2637 struct mdt_lock_handle *lh,
2640 struct mdt_object *o;
2642 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2646 rc = mdt_object_lock(info, o, lh, ibits,
2649 mdt_object_put(info->mti_env, o);
2656 void mdt_object_unlock_put(struct mdt_thread_info * info,
2657 struct mdt_object * o,
2658 struct mdt_lock_handle *lh,
2661 mdt_object_unlock(info, o, lh, decref);
2662 mdt_object_put(info->mti_env, o);
2665 static struct mdt_handler *mdt_handler_find(__u32 opc,
2666 struct mdt_opc_slice *supported)
2668 struct mdt_opc_slice *s;
2669 struct mdt_handler *h;
2672 for (s = supported; s->mos_hs != NULL; s++) {
2673 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2674 h = s->mos_hs + (opc - s->mos_opc_start);
2675 if (likely(h->mh_opc != 0))
2676 LASSERTF(h->mh_opc == opc,
2677 "opcode mismatch %d != %d\n",
2680 h = NULL; /* unsupported opc */
2687 static int mdt_lock_resname_compat(struct mdt_device *m,
2688 struct ldlm_request *req)
2690 /* XXX something... later. */
2694 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2696 /* XXX something... later. */
2701 * Generic code handling requests that have struct mdt_body passed in:
2703 * - extract mdt_body from request and save it in @info, if present;
2705 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2708 * - if HABEO_CORPUS flag is set for this request type check whether object
2709 * actually exists on storage (lu_object_exists()).
2712 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2714 const struct mdt_body *body;
2715 struct mdt_object *obj;
2716 const struct lu_env *env;
2717 struct req_capsule *pill;
2721 env = info->mti_env;
2722 pill = info->mti_pill;
2724 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2728 if (!(body->valid & OBD_MD_FLID))
2731 if (!fid_is_sane(&body->fid1)) {
2732 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2737 * Do not get size or any capa fields before we check that request
2738 * contains capa actually. There are some requests which do not, for
2739 * instance MDS_IS_SUBDIR.
2741 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2742 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2743 mdt_set_capainfo(info, 0, &body->fid1,
2744 req_capsule_client_get(pill, &RMF_CAPA1));
2746 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2748 if ((flags & HABEO_CORPUS) &&
2749 !mdt_object_exists(obj)) {
2750 mdt_object_put(env, obj);
2751 /* for capability renew ENOENT will be handled in
2753 if (body->valid & OBD_MD_FLOSSCAPA)
2758 info->mti_object = obj;
2767 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2769 struct req_capsule *pill = info->mti_pill;
2773 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2774 rc = mdt_body_unpack(info, flags);
2778 if (rc == 0 && (flags & HABEO_REFERO)) {
2780 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2781 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2782 info->mti_body->eadatasize);
2783 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2784 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2787 rc = req_capsule_server_pack(pill);
2792 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2794 struct md_device *next = m->mdt_child;
2796 return next->md_ops->mdo_init_capa_ctxt(env, next,
2797 m->mdt_opts.mo_mds_capa,
2798 m->mdt_capa_timeout,
2804 * Invoke handler for this request opc. Also do necessary preprocessing
2805 * (according to handler ->mh_flags), and post-processing (setting of
2806 * ->last_{xid,committed}).
2808 static int mdt_req_handle(struct mdt_thread_info *info,
2809 struct mdt_handler *h, struct ptlrpc_request *req)
2811 int rc, serious = 0;
2816 LASSERT(h->mh_act != NULL);
2817 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2818 LASSERT(current->journal_info == NULL);
2821 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2822 * to put same checks into handlers like mdt_close(), mdt_reint(),
2823 * etc., without talking to mdt authors first. Checking same thing
2824 * there again is useless and returning 0 error without packing reply
2825 * is buggy! Handlers either pack reply or return error.
2827 * We return 0 here and do not send any reply in order to emulate
2828 * network failure. Do not send any reply in case any of NET related
2829 * fail_id has occured.
2831 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2835 flags = h->mh_flags;
2836 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2838 if (h->mh_fmt != NULL) {
2839 req_capsule_set(info->mti_pill, h->mh_fmt);
2840 rc = mdt_unpack_req_pack_rep(info, flags);
2843 if (rc == 0 && flags & MUTABOR &&
2844 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2845 /* should it be rq_status? */
2848 if (rc == 0 && flags & HABEO_CLAVIS) {
2849 struct ldlm_request *dlm_req;
2851 LASSERT(h->mh_fmt != NULL);
2853 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2854 if (dlm_req != NULL) {
2855 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2857 dlm_req->lock_desc.l_policy_data.\
2858 l_inodebits.bits == 0)) {
2860 * Lock without inodebits makes no sense and
2861 * will oops later in ldlm. If client miss to
2862 * set such bits, do not trigger ASSERTION.
2864 * For liblustre flock case, it maybe zero.
2868 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2869 rc = mdt_lock_resname_compat(
2872 info->mti_dlm_req = dlm_req;
2879 /* capability setting changed via /proc, needs reinitialize ctxt */
2880 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2881 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2882 info->mti_mdt->mdt_capa_conf = 0;
2885 if (likely(rc == 0)) {
2887 * Process request, there can be two types of rc:
2888 * 1) errors with msg unpack/pack, other failures outside the
2889 * operation itself. This is counted as serious errors;
2890 * 2) errors during fs operation, should be placed in rq_status
2893 rc = h->mh_act(info);
2895 !req->rq_no_reply && req->rq_reply_state == NULL) {
2896 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2897 "pack reply and returned 0 error\n",
2901 serious = is_serious(rc);
2902 rc = clear_serious(rc);
2906 req->rq_status = rc;
2909 * ELDLM_* codes which > 0 should be in rq_status only as well as
2910 * all non-serious errors.
2912 if (rc > 0 || !serious)
2915 LASSERT(current->journal_info == NULL);
2917 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2918 info->mti_mdt->mdt_opts.mo_compat_resname) {
2919 struct ldlm_reply *dlmrep;
2921 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2923 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2926 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2927 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2928 target_committed_to_req(req);
2930 if (unlikely(req_is_replay(req) &&
2931 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2932 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2936 target_send_reply(req, rc, info->mti_fail_id);
2940 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2942 lh->mlh_type = MDT_NUL_LOCK;
2943 lh->mlh_reg_lh.cookie = 0ull;
2944 lh->mlh_reg_mode = LCK_MINMODE;
2945 lh->mlh_pdo_lh.cookie = 0ull;
2946 lh->mlh_pdo_mode = LCK_MINMODE;
2949 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2951 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2952 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2956 * Initialize fields of struct mdt_thread_info. Other fields are left in
2957 * uninitialized state, because it's too expensive to zero out whole
2958 * mdt_thread_info (> 1K) on each request arrival.
2960 static void mdt_thread_info_init(struct ptlrpc_request *req,
2961 struct mdt_thread_info *info)
2964 struct md_capainfo *ci;
2966 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2967 info->mti_pill = &req->rq_pill;
2970 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2971 mdt_lock_handle_init(&info->mti_lh[i]);
2973 /* mdt device: it can be NULL while CONNECT */
2974 if (req->rq_export) {
2975 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2976 info->mti_exp = req->rq_export;
2978 info->mti_mdt = NULL;
2979 info->mti_env = req->rq_svc_thread->t_env;
2980 ci = md_capainfo(info->mti_env);
2981 memset(ci, 0, sizeof *ci);
2982 if (req->rq_export) {
2983 if (exp_connect_rmtclient(req->rq_export))
2984 ci->mc_auth = LC_ID_CONVERT;
2985 else if (req->rq_export->exp_connect_flags &
2986 OBD_CONNECT_MDS_CAPA)
2987 ci->mc_auth = LC_ID_PLAIN;
2989 ci->mc_auth = LC_ID_NONE;
2992 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
2993 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2994 info->mti_mos = NULL;
2996 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2997 info->mti_body = NULL;
2998 info->mti_object = NULL;
2999 info->mti_dlm_req = NULL;
3000 info->mti_has_trans = 0;
3001 info->mti_cross_ref = 0;
3002 info->mti_opdata = 0;
3003 info->mti_big_lmm_used = 0;
3005 /* To not check for split by default. */
3006 info->mti_spec.no_create = 0;
3009 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3013 req_capsule_fini(info->mti_pill);
3014 if (info->mti_object != NULL) {
3015 mdt_object_put(info->mti_env, info->mti_object);
3016 info->mti_object = NULL;
3018 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3019 mdt_lock_handle_fini(&info->mti_lh[i]);
3020 info->mti_env = NULL;
3023 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3024 struct obd_device *obd, int *process)
3026 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3027 case MDS_CONNECT: /* This will never get here, but for completeness. */
3028 case OST_CONNECT: /* This will never get here, but for completeness. */
3029 case MDS_DISCONNECT:
3030 case OST_DISCONNECT:
3036 case MDS_DONE_WRITING:
3037 case MDS_SYNC: /* used in unmounting */
3043 *process = target_queue_recovery_request(req, obd);
3047 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3054 * Handle recovery. Return:
3055 * +1: continue request processing;
3056 * -ve: abort immediately with the given error code;
3057 * 0: send reply with error code in req->rq_status;
3059 static int mdt_recovery(struct mdt_thread_info *info)
3061 struct ptlrpc_request *req = mdt_info_req(info);
3062 struct obd_device *obd;
3066 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3069 case SEC_CTX_INIT_CONT:
3075 rc = mdt_handle_idmap(info);
3084 if (unlikely(!class_connected_export(req->rq_export))) {
3085 CERROR("operation %d on unconnected MDS from %s\n",
3086 lustre_msg_get_opc(req->rq_reqmsg),
3087 libcfs_id2str(req->rq_peer));
3088 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3089 * mds_A will get -ENOTCONN(especially for ping req),
3090 * which will cause that mds_A deactive timeout, then when
3091 * mds_A cleanup, the cleanup process will be suspended since
3092 * deactive timeout is not zero.
3094 req->rq_status = -ENOTCONN;
3095 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3099 /* sanity check: if the xid matches, the request must be marked as a
3100 * resent or replayed */
3101 if (req_xid_is_last(req)) {
3102 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3103 (MSG_RESENT | MSG_REPLAY))) {
3104 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3105 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3106 lustre_msg_get_flags(req->rq_reqmsg));
3108 req->rq_status = -ENOTCONN;
3113 /* else: note the opposite is not always true; a RESENT req after a
3114 * failover will usually not match the last_xid, since it was likely
3115 * never committed. A REPLAYed request will almost never match the
3116 * last xid, however it could for a committed, but still retained,
3119 obd = req->rq_export->exp_obd;
3121 /* Check for aborted recovery... */
3122 if (unlikely(obd->obd_recovering)) {
3125 DEBUG_REQ(D_INFO, req, "Got new replay");
3126 rc = mdt_filter_recovery_request(req, obd, &should_process);
3127 if (rc != 0 || !should_process)
3129 else if (should_process < 0) {
3130 req->rq_status = should_process;
3131 rc = ptlrpc_error(req);
3138 static int mdt_msg_check_version(struct lustre_msg *msg)
3142 switch (lustre_msg_get_opc(msg)) {
3144 case MDS_DISCONNECT:
3147 case SEC_CTX_INIT_CONT:
3150 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3152 CERROR("bad opc %u version %08x, expecting %08x\n",
3153 lustre_msg_get_opc(msg),
3154 lustre_msg_get_version(msg),
3155 LUSTRE_OBD_VERSION);
3159 case MDS_GETATTR_NAME:
3166 case MDS_DONE_WRITING:
3173 case MDS_QUOTACHECK:
3179 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3181 CERROR("bad opc %u version %08x, expecting %08x\n",
3182 lustre_msg_get_opc(msg),
3183 lustre_msg_get_version(msg),
3184 LUSTRE_MDS_VERSION);
3188 case LDLM_BL_CALLBACK:
3189 case LDLM_CP_CALLBACK:
3190 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3192 CERROR("bad opc %u version %08x, expecting %08x\n",
3193 lustre_msg_get_opc(msg),
3194 lustre_msg_get_version(msg),
3195 LUSTRE_DLM_VERSION);
3197 case OBD_LOG_CANCEL:
3198 case LLOG_ORIGIN_HANDLE_CREATE:
3199 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3200 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3201 case LLOG_ORIGIN_HANDLE_CLOSE:
3202 case LLOG_ORIGIN_HANDLE_DESTROY:
3203 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3205 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3207 CERROR("bad opc %u version %08x, expecting %08x\n",
3208 lustre_msg_get_opc(msg),
3209 lustre_msg_get_version(msg),
3210 LUSTRE_LOG_VERSION);
3213 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3219 static int mdt_handle0(struct ptlrpc_request *req,
3220 struct mdt_thread_info *info,
3221 struct mdt_opc_slice *supported)
3223 struct mdt_handler *h;
3224 struct lustre_msg *msg;
3229 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3232 LASSERT(current->journal_info == NULL);
3234 msg = req->rq_reqmsg;
3235 rc = mdt_msg_check_version(msg);
3236 if (likely(rc == 0)) {
3237 rc = mdt_recovery(info);
3238 if (likely(rc == +1)) {
3239 h = mdt_handler_find(lustre_msg_get_opc(msg),
3241 if (likely(h != NULL)) {
3242 rc = mdt_req_handle(info, h, req);
3244 CERROR("The unsupported opc: 0x%x\n",
3245 lustre_msg_get_opc(msg) );
3246 req->rq_status = -ENOTSUPP;
3247 rc = ptlrpc_error(req);
3252 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3257 * MDT handler function called by ptlrpc service thread when request comes.
3259 * XXX common "target" functionality should be factored into separate module
3260 * shared by mdt, ost and stand-alone services like fld.
3262 static int mdt_handle_common(struct ptlrpc_request *req,
3263 struct mdt_opc_slice *supported)
3266 struct mdt_thread_info *info;
3270 env = req->rq_svc_thread->t_env;
3271 LASSERT(env != NULL);
3272 LASSERT(env->le_ses != NULL);
3273 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3274 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3275 LASSERT(info != NULL);
3277 mdt_thread_info_init(req, info);
3279 rc = mdt_handle0(req, info, supported);
3281 mdt_thread_info_fini(info);
3286 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3289 int mdt_recovery_handle(struct ptlrpc_request *req)
3294 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3296 rc = mdt_handle_common(req, mdt_fld_handlers);
3299 rc = mdt_handle_common(req, mdt_seq_handlers);
3302 rc = mdt_handle_common(req, mdt_regular_handlers);
3309 static int mdt_regular_handle(struct ptlrpc_request *req)
3311 return mdt_handle_common(req, mdt_regular_handlers);
3314 static int mdt_readpage_handle(struct ptlrpc_request *req)
3316 return mdt_handle_common(req, mdt_readpage_handlers);
3319 static int mdt_xmds_handle(struct ptlrpc_request *req)
3321 return mdt_handle_common(req, mdt_xmds_handlers);
3324 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3326 return mdt_handle_common(req, mdt_seq_handlers);
3329 static int mdt_mdss_handle(struct ptlrpc_request *req)
3331 return mdt_handle_common(req, mdt_seq_handlers);
3334 static int mdt_dtss_handle(struct ptlrpc_request *req)
3336 return mdt_handle_common(req, mdt_seq_handlers);
3339 static int mdt_fld_handle(struct ptlrpc_request *req)
3341 return mdt_handle_common(req, mdt_fld_handlers);
3359 static int mdt_intent_getattr(enum mdt_it_code opcode,
3360 struct mdt_thread_info *info,
3361 struct ldlm_lock **,
3363 static int mdt_intent_reint(enum mdt_it_code opcode,
3364 struct mdt_thread_info *info,
3365 struct ldlm_lock **,
3368 static struct mdt_it_flavor {
3369 const struct req_format *it_fmt;
3371 int (*it_act)(enum mdt_it_code ,
3372 struct mdt_thread_info *,
3373 struct ldlm_lock **,
3376 } mdt_it_flavor[] = {
3378 .it_fmt = &RQF_LDLM_INTENT,
3379 /*.it_flags = HABEO_REFERO,*/
3381 .it_act = mdt_intent_reint,
3382 .it_reint = REINT_OPEN
3385 .it_fmt = &RQF_LDLM_INTENT,
3386 .it_flags = MUTABOR,
3387 .it_act = mdt_intent_reint,
3388 .it_reint = REINT_OPEN
3391 .it_fmt = &RQF_LDLM_INTENT,
3392 .it_flags = MUTABOR,
3393 .it_act = mdt_intent_reint,
3394 .it_reint = REINT_CREATE
3396 [MDT_IT_GETATTR] = {
3397 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3398 .it_flags = HABEO_REFERO,
3399 .it_act = mdt_intent_getattr
3401 [MDT_IT_READDIR] = {
3407 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3408 .it_flags = HABEO_REFERO,
3409 .it_act = mdt_intent_getattr
3412 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3413 .it_flags = MUTABOR,
3415 .it_reint = REINT_UNLINK
3419 .it_flags = MUTABOR,
3422 [MDT_IT_GETXATTR] = {
3428 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3429 .it_flags = HABEO_REFERO,
3430 .it_act = mdt_intent_getattr
3434 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3435 struct ldlm_lock **lockp,
3436 struct ldlm_lock *new_lock,
3437 struct mdt_lock_handle *lh,
3440 struct ptlrpc_request *req = mdt_info_req(info);
3441 struct ldlm_lock *lock = *lockp;
3444 * Get new lock only for cases when possible resent did not find any
3447 if (new_lock == NULL)
3448 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3450 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3451 lh->mlh_reg_lh.cookie = 0;
3455 LASSERTF(new_lock != NULL,
3456 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3459 * If we've already given this lock to a client once, then we should
3460 * have no readers or writers. Otherwise, we should have one reader
3461 * _or_ writer ref (which will be zeroed below) before returning the
3464 if (new_lock->l_export == req->rq_export) {
3465 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3467 LASSERT(new_lock->l_export == NULL);
3468 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3473 if (new_lock->l_export == req->rq_export) {
3475 * Already gave this to the client, which means that we
3476 * reconstructed a reply.
3478 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3480 lh->mlh_reg_lh.cookie = 0;
3481 RETURN(ELDLM_LOCK_REPLACED);
3485 * Fixup the lock to be given to the client.
3487 lock_res_and_lock(new_lock);
3488 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3489 * possible blocking AST. */
3490 while (new_lock->l_readers > 0) {
3491 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3492 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3493 new_lock->l_readers--;
3495 while (new_lock->l_writers > 0) {
3496 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3497 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3498 new_lock->l_writers--;
3501 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3502 new_lock->l_blocking_ast = lock->l_blocking_ast;
3503 new_lock->l_completion_ast = lock->l_completion_ast;
3504 new_lock->l_remote_handle = lock->l_remote_handle;
3505 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3507 unlock_res_and_lock(new_lock);
3509 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3510 &new_lock->l_remote_handle,
3511 &new_lock->l_exp_hash);
3513 LDLM_LOCK_RELEASE(new_lock);
3514 lh->mlh_reg_lh.cookie = 0;
3516 RETURN(ELDLM_LOCK_REPLACED);
3519 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3520 struct ldlm_lock *new_lock,
3521 struct ldlm_lock **old_lock,
3522 struct mdt_lock_handle *lh)
3524 struct ptlrpc_request *req = mdt_info_req(info);
3525 struct obd_export *exp = req->rq_export;
3526 struct lustre_handle remote_hdl;
3527 struct ldlm_request *dlmreq;
3528 struct ldlm_lock *lock;
3530 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3533 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3534 remote_hdl = dlmreq->lock_handle[0];
3536 /* In the function below, .hs_keycmp resolves to
3537 * ldlm_export_lock_keycmp() */
3538 /* coverity[overrun-buffer-val] */
3539 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3541 if (lock != new_lock) {
3542 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3543 lh->mlh_reg_mode = lock->l_granted_mode;
3545 LDLM_DEBUG(lock, "Restoring lock cookie");
3546 DEBUG_REQ(D_DLMTRACE, req,
3547 "restoring lock cookie "LPX64,
3548 lh->mlh_reg_lh.cookie);
3550 *old_lock = LDLM_LOCK_GET(lock);
3551 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3555 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3559 * If the xid matches, then we know this is a resent request, and allow
3560 * it. (It's probably an OPEN, for which we don't send a lock.
3562 if (req_xid_is_last(req))
3566 * This remote handle isn't enqueued, so we never received or processed
3567 * this request. Clear MSG_RESENT, because it can be handled like any
3568 * normal request now.
3570 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3572 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3576 static int mdt_intent_getattr(enum mdt_it_code opcode,
3577 struct mdt_thread_info *info,
3578 struct ldlm_lock **lockp,
3581 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3582 struct ldlm_lock *new_lock = NULL;
3584 struct ldlm_reply *ldlm_rep;
3585 struct ptlrpc_request *req;
3586 struct mdt_body *reqbody;
3587 struct mdt_body *repbody;
3591 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3594 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3597 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3598 repbody->eadatasize = 0;
3599 repbody->aclsize = 0;
3603 child_bits = MDS_INODELOCK_LOOKUP;
3605 case MDT_IT_GETATTR:
3606 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3608 case MDT_IT_LAYOUT: {
3609 static int printed = 0;
3612 CERROR("layout lock not supported by this version\n");
3615 GOTO(out_shrink, rc = -EINVAL);
3619 CERROR("Unsupported intent (%d)\n", opcode);
3620 GOTO(out_shrink, rc = -EINVAL);
3623 rc = mdt_init_ucred(info, reqbody);
3625 GOTO(out_shrink, rc);
3627 req = info->mti_pill->rc_req;
3628 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3629 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3631 /* Get lock from request for possible resent case. */
3632 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3634 ldlm_rep->lock_policy_res2 =
3635 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3637 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3638 ldlm_rep->lock_policy_res2 = 0;
3639 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3640 ldlm_rep->lock_policy_res2) {
3641 lhc->mlh_reg_lh.cookie = 0ull;
3642 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3645 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3648 mdt_exit_ucred(info);
3650 mdt_client_compatibility(info);
3651 rc2 = mdt_fix_reply(info);
3657 static int mdt_intent_reint(enum mdt_it_code opcode,
3658 struct mdt_thread_info *info,
3659 struct ldlm_lock **lockp,
3662 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3663 struct ldlm_reply *rep = NULL;
3667 static const struct req_format *intent_fmts[REINT_MAX] = {
3668 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3669 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3674 opc = mdt_reint_opcode(info, intent_fmts);
3678 if (mdt_it_flavor[opcode].it_reint != opc) {
3679 CERROR("Reint code %ld doesn't match intent: %d\n",
3681 RETURN(err_serious(-EPROTO));
3684 /* Get lock from request for possible resent case. */
3685 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3687 rc = mdt_reint_internal(info, lhc, opc);
3689 /* Check whether the reply has been packed successfully. */
3690 if (mdt_info_req(info)->rq_repmsg != NULL)
3691 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3693 RETURN(err_serious(-EFAULT));
3695 /* MDC expects this in any case */
3697 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3699 /* Cross-ref case, the lock should be returned to the client */
3700 if (rc == -EREMOTE) {
3701 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3702 rep->lock_policy_res2 = 0;
3703 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3706 rep->lock_policy_res2 = clear_serious(rc);
3708 if (rep->lock_policy_res2 == -ENOENT &&
3709 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3710 rep->lock_policy_res2 = 0;
3712 if (rc == -ENOTCONN || rc == -ENODEV ||
3713 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3715 * If it is the disconnect error (ENODEV & ENOCONN), the error
3716 * will be returned by rq_status, and client at ptlrpc layer
3717 * will detect this, then disconnect, reconnect the import
3718 * immediately, instead of impacting the following the rpc.
3720 lhc->mlh_reg_lh.cookie = 0ull;
3724 * For other cases, the error will be returned by intent.
3725 * and client will retrieve the result from intent.
3728 * FIXME: when open lock is finished, that should be
3731 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3732 LASSERTF(rc == 0, "Error occurred but lock handle "
3733 "is still in use\n");
3734 rep->lock_policy_res2 = 0;
3735 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3738 lhc->mlh_reg_lh.cookie = 0ull;
3739 RETURN(ELDLM_LOCK_ABORTED);
3744 static int mdt_intent_code(long itcode)
3752 case IT_OPEN|IT_CREAT:
3759 rc = MDT_IT_READDIR;
3762 rc = MDT_IT_GETATTR;
3774 rc = MDT_IT_GETXATTR;
3779 case IT_QUOTA_DQACQ:
3784 CERROR("Unknown intent opcode: %ld\n", itcode);
3791 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3792 struct ldlm_lock **lockp, __u64 flags)
3794 struct req_capsule *pill;
3795 struct mdt_it_flavor *flv;
3800 opc = mdt_intent_code(itopc);
3804 pill = info->mti_pill;
3806 if (opc == MDT_IT_QUOTA) {
3807 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3810 RETURN(-EOPNOTSUPP);
3812 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3813 /* pass the request to quota master */
3814 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3815 mdt_info_req(info), lockp,
3820 if (opc == MDT_IT_LAYOUT) {
3821 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3822 /* XXX: set replay RMF_DLM_LVB as the real EA size when LAYOUT
3824 } else if (opc == MDT_IT_READDIR) {
3825 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3828 flv = &mdt_it_flavor[opc];
3829 if (flv->it_fmt != NULL)
3830 req_capsule_extend(pill, flv->it_fmt);
3832 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3834 struct ptlrpc_request *req = mdt_info_req(info);
3835 if (flv->it_flags & MUTABOR &&
3836 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3839 if (rc == 0 && flv->it_act != NULL) {
3840 /* execute policy */
3841 rc = flv->it_act(opc, info, lockp, flags);
3848 static int mdt_intent_policy(struct ldlm_namespace *ns,
3849 struct ldlm_lock **lockp, void *req_cookie,
3850 ldlm_mode_t mode, __u64 flags, void *data)
3852 struct mdt_thread_info *info;
3853 struct ptlrpc_request *req = req_cookie;
3854 struct ldlm_intent *it;
3855 struct req_capsule *pill;
3860 LASSERT(req != NULL);
3862 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3864 LASSERT(info != NULL);
3865 pill = info->mti_pill;
3866 LASSERT(pill->rc_req == req);
3868 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3869 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3870 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3872 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3876 /* Lock without inodebits makes no sense and will oops
3877 * later in ldlm. Let's check it now to see if we have
3878 * ibits corrupted somewhere in mdt_intent_opc().
3879 * The case for client miss to set ibits has been
3880 * processed by others. */
3881 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3882 lr_type == LDLM_IBITS,
3883 info->mti_dlm_req->lock_desc.\
3884 l_policy_data.l_inodebits.bits != 0));
3886 rc = err_serious(-EFAULT);
3888 /* No intent was provided */
3889 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3890 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3891 rc = req_capsule_server_pack(pill);
3893 rc = err_serious(rc);
3898 static int mdt_seq_fini(const struct lu_env *env,
3899 struct mdt_device *m)
3901 struct md_site *ms = mdt_md_site(m);
3905 if (ms->ms_server_seq) {
3906 seq_server_fini(ms->ms_server_seq, env);
3907 OBD_FREE_PTR(ms->ms_server_seq);
3908 ms->ms_server_seq = NULL;
3911 if (ms->ms_control_seq) {
3912 seq_server_fini(ms->ms_control_seq, env);
3913 OBD_FREE_PTR(ms->ms_control_seq);
3914 ms->ms_control_seq = NULL;
3917 if (ms->ms_client_seq) {
3918 seq_client_fini(ms->ms_client_seq);
3919 OBD_FREE_PTR(ms->ms_client_seq);
3920 ms->ms_client_seq = NULL;
3927 static int mdt_seq_init(const struct lu_env *env,
3929 struct mdt_device *m)
3936 ms = mdt_md_site(m);
3939 * This is sequence-controller node. Init seq-controller server on local
3942 if (ms->ms_node_id == 0) {
3943 LASSERT(ms->ms_control_seq == NULL);
3945 OBD_ALLOC_PTR(ms->ms_control_seq);
3946 if (ms->ms_control_seq == NULL)
3949 rc = seq_server_init(ms->ms_control_seq,
3950 m->mdt_bottom, uuid,
3951 LUSTRE_SEQ_CONTROLLER,
3956 GOTO(out_seq_fini, rc);
3958 OBD_ALLOC_PTR(ms->ms_client_seq);
3959 if (ms->ms_client_seq == NULL)
3960 GOTO(out_seq_fini, rc = -ENOMEM);
3962 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3963 if (prefix == NULL) {
3964 OBD_FREE_PTR(ms->ms_client_seq);
3965 GOTO(out_seq_fini, rc = -ENOMEM);
3968 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3972 * Init seq-controller client after seq-controller server is
3973 * ready. Pass ms->ms_control_seq to it for direct talking.
3975 rc = seq_client_init(ms->ms_client_seq, NULL,
3976 LUSTRE_SEQ_METADATA, prefix,
3977 ms->ms_control_seq);
3978 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3981 GOTO(out_seq_fini, rc);
3984 /* Init seq-server on local MDT */
3985 LASSERT(ms->ms_server_seq == NULL);
3987 OBD_ALLOC_PTR(ms->ms_server_seq);
3988 if (ms->ms_server_seq == NULL)
3989 GOTO(out_seq_fini, rc = -ENOMEM);
3991 rc = seq_server_init(ms->ms_server_seq,
3992 m->mdt_bottom, uuid,
3997 GOTO(out_seq_fini, rc = -ENOMEM);
3999 /* Assign seq-controller client to local seq-server. */
4000 if (ms->ms_node_id == 0) {
4001 LASSERT(ms->ms_client_seq != NULL);
4003 rc = seq_server_set_cli(ms->ms_server_seq,
4011 mdt_seq_fini(env, m);
4016 * Init client sequence manager which is used by local MDS to talk to sequence
4017 * controller on remote node.
4019 static int mdt_seq_init_cli(const struct lu_env *env,
4020 struct mdt_device *m,
4021 struct lustre_cfg *cfg)
4023 struct md_site *ms = mdt_md_site(m);
4024 struct obd_device *mdc;
4025 struct obd_uuid *uuidp, *mdcuuidp;
4026 char *uuid_str, *mdc_uuid_str;
4029 struct mdt_thread_info *info;
4030 char *p, *index_string = lustre_cfg_string(cfg, 2);
4033 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4034 uuidp = &info->mti_u.uuid[0];
4035 mdcuuidp = &info->mti_u.uuid[1];
4037 LASSERT(index_string);
4039 index = simple_strtol(index_string, &p, 10);
4041 CERROR("Invalid index in lustre_cgf, offset 2\n");
4045 /* check if this is adding the first MDC and controller is not yet
4047 if (index != 0 || ms->ms_client_seq)
4050 uuid_str = lustre_cfg_string(cfg, 1);
4051 mdc_uuid_str = lustre_cfg_string(cfg, 4);
4052 obd_str2uuid(uuidp, uuid_str);
4053 obd_str2uuid(mdcuuidp, mdc_uuid_str);
4055 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
4057 CERROR("can't find controller MDC by uuid %s\n",
4060 } else if (!mdc->obd_set_up) {
4061 CERROR("target %s not set up\n", mdc->obd_name);
4064 LASSERT(ms->ms_control_exp);
4065 OBD_ALLOC_PTR(ms->ms_client_seq);
4066 if (ms->ms_client_seq != NULL) {
4069 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4073 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4076 rc = seq_client_init(ms->ms_client_seq,
4078 LUSTRE_SEQ_METADATA,
4080 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4087 LASSERT(ms->ms_server_seq != NULL);
4088 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
4095 static void mdt_seq_fini_cli(struct mdt_device *m)
4101 ms = mdt_md_site(m);
4104 if (ms->ms_server_seq)
4105 seq_server_set_cli(ms->ms_server_seq,
4108 if (ms->ms_control_exp) {
4109 class_export_put(ms->ms_control_exp);
4110 ms->ms_control_exp = NULL;
4119 static int mdt_fld_fini(const struct lu_env *env,
4120 struct mdt_device *m)
4122 struct md_site *ms = mdt_md_site(m);
4125 if (ms && ms->ms_server_fld) {
4126 fld_server_fini(ms->ms_server_fld, env);
4127 OBD_FREE_PTR(ms->ms_server_fld);
4128 ms->ms_server_fld = NULL;
4134 static int mdt_fld_init(const struct lu_env *env,
4136 struct mdt_device *m)
4142 ms = mdt_md_site(m);
4144 OBD_ALLOC_PTR(ms->ms_server_fld);
4145 if (ms->ms_server_fld == NULL)
4146 RETURN(rc = -ENOMEM);
4148 rc = fld_server_init(ms->ms_server_fld,
4149 m->mdt_bottom, uuid,
4150 env, ms->ms_node_id);
4152 OBD_FREE_PTR(ms->ms_server_fld);
4153 ms->ms_server_fld = NULL;
4160 /* device init/fini methods */
4161 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
4164 if (m->mdt_regular_service != NULL) {
4165 ptlrpc_unregister_service(m->mdt_regular_service);
4166 m->mdt_regular_service = NULL;
4168 if (m->mdt_readpage_service != NULL) {
4169 ptlrpc_unregister_service(m->mdt_readpage_service);
4170 m->mdt_readpage_service = NULL;
4172 if (m->mdt_xmds_service != NULL) {
4173 ptlrpc_unregister_service(m->mdt_xmds_service);
4174 m->mdt_xmds_service = NULL;
4176 if (m->mdt_setattr_service != NULL) {
4177 ptlrpc_unregister_service(m->mdt_setattr_service);
4178 m->mdt_setattr_service = NULL;
4180 if (m->mdt_mdsc_service != NULL) {
4181 ptlrpc_unregister_service(m->mdt_mdsc_service);
4182 m->mdt_mdsc_service = NULL;
4184 if (m->mdt_mdss_service != NULL) {
4185 ptlrpc_unregister_service(m->mdt_mdss_service);
4186 m->mdt_mdss_service = NULL;
4188 if (m->mdt_dtss_service != NULL) {
4189 ptlrpc_unregister_service(m->mdt_dtss_service);
4190 m->mdt_dtss_service = NULL;
4192 if (m->mdt_fld_service != NULL) {
4193 ptlrpc_unregister_service(m->mdt_fld_service);
4194 m->mdt_fld_service = NULL;
4199 static int mdt_start_ptlrpc_service(struct mdt_device *m)
4201 static struct ptlrpc_service_conf conf;
4202 cfs_proc_dir_entry_t *procfs_entry;
4206 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
4207 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4208 "mdt_ldlm_client", m->mdt_ldlm_client);
4210 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
4212 conf = (typeof(conf)) {
4213 .psc_name = LUSTRE_MDT_NAME,
4214 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4216 .bc_nbufs = MDS_NBUFS,
4217 .bc_buf_size = MDS_BUFSIZE,
4218 .bc_req_max_size = MDS_MAXREQSIZE,
4219 .bc_rep_max_size = MDS_MAXREPSIZE,
4220 .bc_req_portal = MDS_REQUEST_PORTAL,
4221 .bc_rep_portal = MDC_REPLY_PORTAL,
4224 * We'd like to have a mechanism to set this on a per-device
4225 * basis, but alas...
4228 .tc_thr_name = LUSTRE_MDT_NAME,
4229 .tc_thr_factor = MDT_THR_FACTOR,
4230 .tc_nthrs_init = MDT_NTHRS_INIT,
4231 .tc_nthrs_base = MDT_NTHRS_BASE,
4232 .tc_nthrs_max = MDT_NTHRS_MAX,
4233 .tc_nthrs_user = mds_num_threads,
4234 .tc_cpu_affinity = 1,
4235 .tc_ctx_tags = LCT_MD_THREAD,
4238 .cc_pattern = mds_num_cpts,
4241 .so_req_handler = mdt_regular_handle,
4242 .so_req_printer = target_print_req,
4243 .so_hpreq_handler = ptlrpc_hpreq_handler,
4246 m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
4247 if (IS_ERR(m->mdt_regular_service)) {
4248 rc = PTR_ERR(m->mdt_regular_service);
4249 CERROR("failed to start regular mdt service: %d\n", rc);
4250 m->mdt_regular_service = NULL;
4256 * readpage service configuration. Parameters have to be adjusted,
4259 memset(&conf, 0, sizeof(conf));
4260 conf = (typeof(conf)) {
4261 .psc_name = LUSTRE_MDT_NAME "_readpage",
4262 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4264 .bc_nbufs = MDS_NBUFS,
4265 .bc_buf_size = MDS_BUFSIZE,
4266 .bc_req_max_size = MDS_MAXREQSIZE,
4267 .bc_rep_max_size = MDS_MAXREPSIZE,
4268 .bc_req_portal = MDS_READPAGE_PORTAL,
4269 .bc_rep_portal = MDC_REPLY_PORTAL,
4272 .tc_thr_name = "mdt_rdpg",
4273 .tc_thr_factor = MDT_RDPG_THR_FACTOR,
4274 .tc_nthrs_init = MDT_RDPG_NTHRS_INIT,
4275 .tc_nthrs_base = MDT_RDPG_NTHRS_BASE,
4276 .tc_nthrs_max = MDT_RDPG_NTHRS_MAX,
4277 .tc_nthrs_user = mds_rdpg_num_threads,
4278 .tc_cpu_affinity = 1,
4279 .tc_ctx_tags = LCT_MD_THREAD,
4282 .cc_pattern = mds_rdpg_num_cpts,
4285 .so_req_handler = mdt_readpage_handle,
4286 .so_req_printer = target_print_req,
4289 m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
4290 if (IS_ERR(m->mdt_readpage_service)) {
4291 rc = PTR_ERR(m->mdt_readpage_service);
4292 CERROR("failed to start readpage service: %d\n", rc);
4293 m->mdt_readpage_service = NULL;
4295 GOTO(err_mdt_svc, rc);
4299 * setattr service configuration.
4301 * XXX To keep the compatibility with old client(< 2.2), we need to
4302 * preserve this portal for a certain time, it should be removed
4303 * eventually. LU-617.
4305 memset(&conf, 0, sizeof(conf));
4306 conf = (typeof(conf)) {
4307 .psc_name = LUSTRE_MDT_NAME "_setattr",
4308 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4310 .bc_nbufs = MDS_NBUFS,
4311 .bc_buf_size = MDS_BUFSIZE,
4312 .bc_req_max_size = MDS_MAXREQSIZE,
4313 .bc_rep_max_size = MDS_MAXREPSIZE,
4314 .bc_req_portal = MDS_SETATTR_PORTAL,
4315 .bc_rep_portal = MDC_REPLY_PORTAL,
4318 .tc_thr_name = "mdt_attr",
4319 .tc_thr_factor = MDT_SETA_THR_FACTOR,
4320 .tc_nthrs_init = MDT_SETA_NTHRS_INIT,
4321 .tc_nthrs_base = MDT_SETA_NTHRS_BASE,
4322 .tc_nthrs_max = MDT_SETA_NTHRS_MAX,
4323 .tc_nthrs_user = mds_attr_num_threads,
4324 .tc_cpu_affinity = 1,
4325 .tc_ctx_tags = LCT_MD_THREAD,
4328 .cc_pattern = mds_attr_num_cpts,
4331 .so_req_handler = mdt_regular_handle,
4332 .so_req_printer = target_print_req,
4335 m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4336 if (IS_ERR(m->mdt_setattr_service)) {
4337 rc = PTR_ERR(m->mdt_setattr_service);
4338 CERROR("failed to start setattr service: %d\n", rc);
4339 m->mdt_setattr_service = NULL;
4341 GOTO(err_mdt_svc, rc);
4345 * sequence controller service configuration
4347 memset(&conf, 0, sizeof(conf));
4348 conf = (typeof(conf)) {
4349 .psc_name = LUSTRE_MDT_NAME "_mdsc",
4350 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4352 .bc_nbufs = MDS_NBUFS,
4353 .bc_buf_size = MDS_BUFSIZE,
4354 .bc_req_max_size = SEQ_MAXREQSIZE,
4355 .bc_rep_max_size = SEQ_MAXREPSIZE,
4356 .bc_req_portal = SEQ_CONTROLLER_PORTAL,
4357 .bc_rep_portal = MDC_REPLY_PORTAL,
4360 .tc_thr_name = "mdt_mdsc",
4361 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4362 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4363 .tc_ctx_tags = LCT_MD_THREAD,
4366 .so_req_handler = mdt_mdsc_handle,
4367 .so_req_printer = target_print_req,
4370 m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4371 if (IS_ERR(m->mdt_mdsc_service)) {
4372 rc = PTR_ERR(m->mdt_mdsc_service);
4373 CERROR("failed to start seq controller service: %d\n", rc);
4374 m->mdt_mdsc_service = NULL;
4376 GOTO(err_mdt_svc, rc);
4380 * metadata sequence server service configuration
4382 memset(&conf, 0, sizeof(conf));
4383 conf = (typeof(conf)) {
4384 .psc_name = LUSTRE_MDT_NAME "_mdss",
4385 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4387 .bc_nbufs = MDS_NBUFS,
4388 .bc_buf_size = MDS_BUFSIZE,
4389 .bc_req_max_size = SEQ_MAXREQSIZE,
4390 .bc_rep_max_size = SEQ_MAXREPSIZE,
4391 .bc_req_portal = SEQ_METADATA_PORTAL,
4392 .bc_rep_portal = MDC_REPLY_PORTAL,
4395 .tc_thr_name = "mdt_mdss",
4396 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4397 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4398 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4401 .so_req_handler = mdt_mdss_handle,
4402 .so_req_printer = target_print_req,
4405 m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4406 if (IS_ERR(m->mdt_mdss_service)) {
4407 rc = PTR_ERR(m->mdt_mdss_service);
4408 CERROR("failed to start metadata seq server service: %d\n", rc);
4409 m->mdt_mdss_service = NULL;
4411 GOTO(err_mdt_svc, rc);
4415 * Data sequence server service configuration. We want to have really
4416 * cluster-wide sequences space. This is why we start only one sequence
4417 * controller which manages space.
4419 memset(&conf, 0, sizeof(conf));
4420 conf = (typeof(conf)) {
4421 .psc_name = LUSTRE_MDT_NAME "_dtss",
4422 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4424 .bc_nbufs = MDS_NBUFS,
4425 .bc_buf_size = MDS_BUFSIZE,
4426 .bc_req_max_size = SEQ_MAXREQSIZE,
4427 .bc_rep_max_size = SEQ_MAXREPSIZE,
4428 .bc_req_portal = SEQ_DATA_PORTAL,
4429 .bc_rep_portal = OSC_REPLY_PORTAL,
4432 .tc_thr_name = "mdt_dtss",
4433 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4434 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4435 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4438 .so_req_handler = mdt_dtss_handle,
4439 .so_req_printer = target_print_req,
4442 m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4443 if (IS_ERR(m->mdt_dtss_service)) {
4444 rc = PTR_ERR(m->mdt_dtss_service);
4445 CERROR("failed to start data seq server service: %d\n", rc);
4446 m->mdt_dtss_service = NULL;
4448 GOTO(err_mdt_svc, rc);
4451 /* FLD service start */
4452 memset(&conf, 0, sizeof(conf));
4453 conf = (typeof(conf)) {
4454 .psc_name = LUSTRE_MDT_NAME "_fld",
4455 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4457 .bc_nbufs = MDS_NBUFS,
4458 .bc_buf_size = MDS_BUFSIZE,
4459 .bc_req_max_size = FLD_MAXREQSIZE,
4460 .bc_rep_max_size = FLD_MAXREPSIZE,
4461 .bc_req_portal = FLD_REQUEST_PORTAL,
4462 .bc_rep_portal = MDC_REPLY_PORTAL,
4465 .tc_thr_name = "mdt_fld",
4466 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4467 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4468 .tc_ctx_tags = LCT_DT_THREAD | LCT_MD_THREAD
4471 .so_req_handler = mdt_fld_handle,
4472 .so_req_printer = target_print_req,
4475 m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4476 if (IS_ERR(m->mdt_fld_service)) {
4477 rc = PTR_ERR(m->mdt_fld_service);
4478 CERROR("failed to start fld service: %d\n", rc);
4479 m->mdt_fld_service = NULL;
4481 GOTO(err_mdt_svc, rc);
4485 * mds-mds service configuration. Separate portal is used to allow
4486 * mds-mds requests be not blocked during recovery.
4488 memset(&conf, 0, sizeof(conf));
4489 conf = (typeof(conf)) {
4490 .psc_name = LUSTRE_MDT_NAME "_mds",
4491 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4493 .bc_nbufs = MDS_NBUFS,
4494 .bc_buf_size = MDS_BUFSIZE,
4495 .bc_req_max_size = MDS_MAXREQSIZE,
4496 .bc_rep_max_size = MDS_MAXREPSIZE,
4497 .bc_req_portal = MDS_MDS_PORTAL,
4498 .bc_rep_portal = MDC_REPLY_PORTAL,
4501 .tc_thr_name = "mdt_mds",
4502 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4503 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4504 .tc_ctx_tags = LCT_MD_THREAD,
4507 .so_req_handler = mdt_xmds_handle,
4508 .so_req_printer = target_print_req,
4509 .so_hpreq_handler = ptlrpc_hpreq_handler,
4512 m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4513 if (IS_ERR(m->mdt_xmds_service)) {
4514 rc = PTR_ERR(m->mdt_xmds_service);
4515 CERROR("failed to start xmds service: %d\n", rc);
4516 m->mdt_xmds_service = NULL;
4518 GOTO(err_mdt_svc, rc);
4524 mdt_stop_ptlrpc_service(m);
4529 static void mdt_stack_fini(const struct lu_env *env,
4530 struct mdt_device *m, struct lu_device *top)
4532 struct obd_device *obd = mdt2obd_dev(m);
4533 struct lustre_cfg_bufs *bufs;
4534 struct lustre_cfg *lcfg;
4535 struct mdt_thread_info *info;
4539 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4540 LASSERT(info != NULL);
4542 lu_dev_del_linkage(top->ld_site, top);
4544 lu_site_purge(env, top->ld_site, -1);
4546 bufs = &info->mti_u.bufs;
4547 /* process cleanup, pass mdt obd name to get obd umount flags */
4548 /* another purpose is to let all layers to release their objects */
4549 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4554 lustre_cfg_bufs_set_string(bufs, 1, flags);
4555 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4557 CERROR("Cannot alloc lcfg!\n");
4561 top->ld_ops->ldo_process_config(env, top, lcfg);
4562 lustre_cfg_free(lcfg);
4564 lu_site_purge(env, top->ld_site, -1);
4566 m->mdt_child = NULL;
4567 m->mdt_bottom = NULL;
4569 obd_disconnect(m->mdt_child_exp);
4570 m->mdt_child_exp = NULL;
4572 obd_disconnect(m->mdt_bottom_exp);
4573 m->mdt_child_exp = NULL;
4576 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4577 const char *next, struct obd_export **exp)
4579 struct obd_connect_data *data = NULL;
4580 struct obd_device *obd;
4584 OBD_ALLOC_PTR(data);
4586 GOTO(out, rc = -ENOMEM);
4588 obd = class_name2obd(next);
4590 CERROR("%s: can't locate next device: %s\n",
4591 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4592 GOTO(out, rc = -ENOTCONN);
4595 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4596 data->ocd_version = LUSTRE_VERSION_CODE;
4598 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4600 CERROR("%s: cannot connect to next dev %s (%d)\n",
4601 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4611 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4612 struct lustre_cfg *cfg)
4614 char *dev = lustre_cfg_string(cfg, 0);
4615 int rc, name_size, uuid_size;
4616 char *name, *uuid, *p;
4617 struct lustre_cfg_bufs *bufs;
4618 struct lustre_cfg *lcfg;
4619 struct obd_device *obd;
4620 struct lustre_profile *lprof;
4621 struct lu_site *site;
4624 /* in 1.8 we had the only device in the stack - MDS.
4625 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4626 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4627 * to generate names and setup MDT, MDD. MDT will be using
4628 * generated name to connect to MDD. for MDD the next device
4629 * will be LOD with name taken from so called "profile" which
4630 * is generated by mount_option line
4632 * 1.8 MGS generates config. commands like this:
4633 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4634 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4635 * 2.0 MGS generates config. commands like this:
4636 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4637 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4638 * 3:lustre-MDT0000-mdtlov 4:f
4640 * we generate MDD name from MDT one, just replacing T with D
4642 * after all the preparations, the logical equivalent will be
4643 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4644 * 3:lustre-MDT0000-mdtlov 4:f
4645 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4646 * 3:lustre-MDD0000 4:f
4648 * notice we build the stack from down to top: MDD first, then MDT */
4650 name_size = MAX_OBD_NAME;
4651 uuid_size = MAX_OBD_NAME;
4653 OBD_ALLOC(name, name_size);
4654 OBD_ALLOC(uuid, uuid_size);
4655 if (name == NULL || uuid == NULL)
4656 GOTO(cleanup_mem, rc = -ENOMEM);
4658 OBD_ALLOC_PTR(bufs);
4660 GOTO(cleanup_mem, rc = -ENOMEM);
4663 p = strstr(name, "-MDT");
4665 GOTO(cleanup_mem, rc = -ENOMEM);
4668 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4670 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4671 if (lprof == NULL || lprof->lp_dt == NULL) {
4672 CERROR("can't find the profile: %s\n",
4673 lustre_cfg_string(cfg, 0));
4674 GOTO(cleanup_mem, rc = -EINVAL);
4677 lustre_cfg_bufs_reset(bufs, name);
4678 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4679 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4680 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4682 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4684 GOTO(free_bufs, rc = -ENOMEM);
4686 rc = class_attach(lcfg);
4688 GOTO(lcfg_cleanup, rc);
4690 obd = class_name2obd(name);
4692 CERROR("Can not find obd %s (%s in config)\n",
4693 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4694 GOTO(class_detach, rc = -EINVAL);
4697 lustre_cfg_free(lcfg);
4699 lustre_cfg_bufs_reset(bufs, name);
4700 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4701 lustre_cfg_bufs_set_string(bufs, 2, dev);
4702 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4704 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4706 rc = class_setup(obd, lcfg);
4708 GOTO(class_detach, rc);
4710 /* connect to MDD we just setup */
4711 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4715 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4717 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4718 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4719 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4720 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4723 /* now connect to bottom OSD */
4724 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4725 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4729 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4732 rc = lu_env_refill((struct lu_env *)env);
4734 CERROR("Failure to refill session: '%d'\n", rc);
4736 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4741 class_detach(obd, lcfg);
4743 lustre_cfg_free(lcfg);
4748 OBD_FREE(name, name_size);
4750 OBD_FREE(uuid, uuid_size);
4754 /* setup quota master target on MDT0 */
4755 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4756 struct lustre_cfg *cfg)
4758 struct obd_device *obd;
4759 char *dev = lustre_cfg_string(cfg, 0);
4760 char *qmtname, *uuid, *p;
4761 struct lustre_cfg_bufs *bufs;
4762 struct lustre_cfg *lcfg;
4763 struct lustre_profile *lprof;
4764 struct obd_connect_data *data;
4768 LASSERT(mdt->mdt_qmt_exp == NULL);
4769 LASSERT(mdt->mdt_qmt_dev == NULL);
4771 /* quota master is on MDT0 only for now */
4772 if (mdt->mdt_mite.ms_node_id != 0)
4775 /* MGS generates config commands which look as follows:
4776 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4777 * 3:lustre-MDT0000-mdtlov 4:f
4779 * We generate the QMT name from the MDT one, just replacing MD with QM
4780 * after all the preparations, the logical equivalent will be:
4781 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4782 * 3:lustre-MDT0000-osd 4:f */
4783 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4784 OBD_ALLOC(uuid, UUID_MAX);
4785 OBD_ALLOC_PTR(bufs);
4786 OBD_ALLOC_PTR(data);
4787 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4788 GOTO(cleanup_mem, rc = -ENOMEM);
4790 strcpy(qmtname, dev);
4791 p = strstr(qmtname, "-MDT");
4793 GOTO(cleanup_mem, rc = -ENOMEM);
4794 /* replace MD with QM */
4798 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4800 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4801 if (lprof == NULL || lprof->lp_dt == NULL) {
4802 CERROR("can't find profile for %s\n",
4803 lustre_cfg_string(cfg, 0));
4804 GOTO(cleanup_mem, rc = -EINVAL);
4807 lustre_cfg_bufs_reset(bufs, qmtname);
4808 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4809 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4810 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4812 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4814 GOTO(cleanup_mem, rc = -ENOMEM);
4816 rc = class_attach(lcfg);
4818 GOTO(lcfg_cleanup, rc);
4820 obd = class_name2obd(qmtname);
4822 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4823 lustre_cfg_string(cfg, 0));
4824 GOTO(class_detach, rc = -EINVAL);
4827 lustre_cfg_free(lcfg);
4829 lustre_cfg_bufs_reset(bufs, qmtname);
4830 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4831 lustre_cfg_bufs_set_string(bufs, 2, dev);
4833 /* for quota, the next device should be the OSD device */
4834 lustre_cfg_bufs_set_string(bufs, 3,
4835 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4837 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4839 rc = class_setup(obd, lcfg);
4841 GOTO(class_detach, rc);
4843 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4845 /* configure local quota objects */
4846 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4847 &mdt->mdt_md_dev.md_lu_dev,
4850 GOTO(class_cleanup, rc);
4852 /* connect to quota master target */
4853 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4854 data->ocd_version = LUSTRE_VERSION_CODE;
4855 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4858 CERROR("cannot connect to quota master device %s (%d)\n",
4860 GOTO(class_cleanup, rc);
4866 class_manual_cleanup(obd);
4867 mdt->mdt_qmt_dev = NULL;
4871 class_detach(obd, lcfg);
4873 lustre_cfg_free(lcfg);
4878 OBD_FREE(qmtname, MAX_OBD_NAME);
4880 OBD_FREE(uuid, UUID_MAX);
4886 /* Shutdown quota master target associated with mdt */
4887 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4891 if (mdt->mdt_qmt_exp == NULL)
4893 LASSERT(mdt->mdt_qmt_dev != NULL);
4895 /* the qmt automatically shuts down when the mdt disconnects */
4896 obd_disconnect(mdt->mdt_qmt_exp);
4897 mdt->mdt_qmt_exp = NULL;
4898 mdt->mdt_qmt_dev = NULL;
4902 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4904 struct md_device *next = m->mdt_child;
4905 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4906 struct obd_device *obd = mdt2obd_dev(m);
4909 target_recovery_fini(obd);
4911 ping_evictor_stop();
4913 mdt_stop_ptlrpc_service(m);
4914 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4915 obd_exports_barrier(obd);
4916 obd_zombie_barrier();
4920 tgt_fini(env, &m->mdt_lut);
4921 mdt_fs_cleanup(env, m);
4922 upcall_cache_cleanup(m->mdt_identity_cache);
4923 m->mdt_identity_cache = NULL;
4925 if (m->mdt_namespace != NULL) {
4926 ldlm_namespace_free(m->mdt_namespace, NULL,
4927 d->ld_obd->obd_force);
4928 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4931 mdt_quota_fini(env, m);
4933 cfs_free_nidlist(&m->mdt_nosquash_nids);
4934 if (m->mdt_nosquash_str) {
4935 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4936 m->mdt_nosquash_str = NULL;
4937 m->mdt_nosquash_strlen = 0;
4940 mdt_seq_fini(env, m);
4941 mdt_seq_fini_cli(m);
4942 mdt_fld_fini(env, m);
4943 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4945 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4946 cfs_timer_disarm(&m->mdt_ck_timer);
4947 mdt_ck_thread_stop(m);
4952 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4954 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4956 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4961 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4963 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4964 struct sptlrpc_rule_set tmp_rset;
4967 sptlrpc_rule_set_init(&tmp_rset);
4968 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4970 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4975 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4977 write_lock(&m->mdt_sptlrpc_lock);
4978 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4979 m->mdt_sptlrpc_rset = tmp_rset;
4980 write_unlock(&m->mdt_sptlrpc_lock);
4985 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4987 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4988 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4990 struct mdt_thread_info *info;
4991 struct obd_device *obd;
4992 const char *dev = lustre_cfg_string(cfg, 0);
4993 const char *num = lustre_cfg_string(cfg, 2);
4994 struct lustre_mount_info *lmi = NULL;
4995 struct lustre_sb_info *lsi;
4997 struct md_site *mite;
4998 const char *identity_upcall = "NONE";
4999 struct md_device *next;
5005 md_device_init(&m->mdt_md_dev, ldt);
5007 * Environment (env) might be missing mdt_thread_key values at that
5008 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5011 * Usually device allocation path doesn't use module key values, but
5012 * mdt has to do a lot of work here, so allocate key value.
5014 rc = lu_env_refill((struct lu_env *)env);
5018 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5019 LASSERT(info != NULL);
5021 obd = class_name2obd(dev);
5022 LASSERT(obd != NULL);
5024 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
5026 m->mdt_som_conf = 0;
5028 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5029 lmi = server_get_mount(dev);
5031 CERROR("Cannot get mount info for %s!\n", dev);
5034 lsi = s2lsi(lmi->lmi_sb);
5035 /* CMD is supported only in IAM mode */
5037 node_id = simple_strtol(num, NULL, 10);
5038 obd->u.obt.obt_magic = OBT_MAGIC;
5041 rwlock_init(&m->mdt_sptlrpc_lock);
5042 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
5044 spin_lock_init(&m->mdt_ioepoch_lock);
5045 m->mdt_opts.mo_compat_resname = 0;
5046 m->mdt_opts.mo_mds_capa = 1;
5047 m->mdt_opts.mo_oss_capa = 1;
5048 m->mdt_capa_timeout = CAPA_TIMEOUT;
5049 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5050 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5051 m->mdt_squash_uid = 0;
5052 m->mdt_squash_gid = 0;
5053 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5054 m->mdt_nosquash_str = NULL;
5055 m->mdt_nosquash_strlen = 0;
5056 init_rwsem(&m->mdt_squash_sem);
5057 spin_lock_init(&m->mdt_osfs_lock);
5058 m->mdt_osfs_age = cfs_time_shift_64(-1000);
5060 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
5061 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
5062 /* set this lu_device to obd, because error handling need it */
5063 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
5065 /* init the stack */
5066 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5068 CERROR("Can't init device stack, rc %d\n", rc);
5072 s = m->mdt_md_dev.md_lu_dev.ld_site;
5073 mite = &m->mdt_mite;
5074 s->ld_md_site = mite;
5077 /* set server index */
5078 mite->ms_node_id = node_id;
5080 /* failover is the default
5081 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5082 * assumed whose ms_node_id == 0 XXX
5084 obd->obd_replayable = 1;
5085 /* No connection accepted until configurations will finish */
5086 obd->obd_no_conn = 1;
5088 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5089 char *str = lustre_cfg_string(cfg, 4);
5090 if (strchr(str, 'n')) {
5091 CWARN("%s: recovery disabled\n", obd->obd_name);
5092 obd->obd_replayable = 0;
5096 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
5098 GOTO(err_fini_stack, rc);
5100 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
5101 LUSTRE_MDT_NAME"-%p", m);
5102 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5103 LDLM_NAMESPACE_SERVER,
5104 LDLM_NAMESPACE_GREEDY,
5106 if (m->mdt_namespace == NULL)
5107 GOTO(err_fini_seq, rc = -ENOMEM);
5109 m->mdt_namespace->ns_lvbp = m;
5110 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5112 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5113 /* set obd_namespace for compatibility with old code */
5114 obd->obd_namespace = m->mdt_namespace;
5116 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5118 rc = mdt_ck_thread_start(m);
5120 GOTO(err_free_ns, rc);
5122 rc = mdt_fs_setup(env, m, obd, lsi);
5126 mdt_adapt_sptlrpc_conf(obd, 1);
5128 next = m->mdt_child;
5129 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5132 GOTO(err_llog_cleanup, rc);
5134 if (mntopts & MNTOPT_USERXATTR)
5135 m->mdt_opts.mo_user_xattr = 1;
5137 m->mdt_opts.mo_user_xattr = 0;
5139 if (mntopts & MNTOPT_ACL)
5140 m->mdt_opts.mo_acl = 1;
5142 m->mdt_opts.mo_acl = 0;
5144 /* XXX: to support suppgid for ACL, we enable identity_upcall
5145 * by default, otherwise, maybe got unexpected -EACCESS. */
5146 if (m->mdt_opts.mo_acl)
5147 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5149 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
5150 &mdt_identity_upcall_cache_ops);
5151 if (IS_ERR(m->mdt_identity_cache)) {
5152 rc = PTR_ERR(m->mdt_identity_cache);
5153 m->mdt_identity_cache = NULL;
5154 GOTO(err_llog_cleanup, rc);
5157 rc = mdt_procfs_init(m, dev);
5159 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5160 GOTO(err_recovery, rc);
5163 rc = mdt_quota_init(env, m, cfg);
5165 GOTO(err_procfs, rc);
5167 rc = mdt_start_ptlrpc_service(m);
5169 GOTO(err_quota, rc);
5171 ping_evictor_start();
5173 /* recovery will be started upon mdt_prepare()
5174 * when the whole stack is complete and ready
5175 * to serve the requests */
5177 mdt_init_capa_ctxt(env, m);
5179 /* Reduce the initial timeout on an MDS because it doesn't need such
5180 * a long timeout as an OST does. Adaptive timeouts will adjust this
5181 * value appropriately. */
5182 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5183 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5187 ping_evictor_stop();
5188 mdt_stop_ptlrpc_service(m);
5190 mdt_quota_fini(env, m);
5194 target_recovery_fini(obd);
5195 upcall_cache_cleanup(m->mdt_identity_cache);
5196 m->mdt_identity_cache = NULL;
5198 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5199 mdt_fs_cleanup(env, m);
5201 cfs_timer_disarm(&m->mdt_ck_timer);
5202 mdt_ck_thread_stop(m);
5204 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5205 obd->obd_namespace = m->mdt_namespace = NULL;
5207 mdt_seq_fini(env, m);
5208 mdt_fld_fini(env, m);
5209 tgt_fini(env, &m->mdt_lut);
5211 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5214 server_put_mount(dev, lmi->lmi_mnt);
5218 /* For interoperability, the left element is old parameter, the right one
5219 * is the new version of the parameter, if some parameter is deprecated,
5220 * the new version should be set as NULL. */
5221 static struct cfg_interop_param mdt_interop_param[] = {
5222 { "mdt.group_upcall", NULL },
5223 { "mdt.quota_type", NULL },
5224 { "mdd.quota_type", NULL },
5225 { "mdt.rootsquash", "mdt.root_squash" },
5226 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5230 /* used by MGS to process specific configurations */
5231 static int mdt_process_config(const struct lu_env *env,
5232 struct lu_device *d, struct lustre_cfg *cfg)
5234 struct mdt_device *m = mdt_dev(d);
5235 struct md_device *md_next = m->mdt_child;
5236 struct lu_device *next = md2lu_dev(md_next);
5240 switch (cfg->lcfg_command) {
5242 struct lprocfs_static_vars lvars;
5243 struct obd_device *obd = d->ld_obd;
5245 /* For interoperability */
5246 struct cfg_interop_param *ptr = NULL;
5247 struct lustre_cfg *old_cfg = NULL;
5250 param = lustre_cfg_string(cfg, 1);
5251 if (param == NULL) {
5252 CERROR("param is empty\n");
5257 ptr = class_find_old_param(param, mdt_interop_param);
5259 if (ptr->new_param == NULL) {
5261 CWARN("For interoperability, skip this %s."
5262 " It is obsolete.\n", ptr->old_param);
5266 CWARN("Found old param %s, changed it to %s.\n",
5267 ptr->old_param, ptr->new_param);
5270 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5277 lprocfs_mdt_init_vars(&lvars);
5278 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5280 if (rc > 0 || rc == -ENOSYS)
5281 /* we don't understand; pass it on */
5282 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5284 if (old_cfg != NULL)
5285 lustre_cfg_free(cfg);
5291 * Add mdc hook to get first MDT uuid and connect it to
5292 * ls->controller to use for seq manager.
5294 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5296 CERROR("Can't add mdc, rc %d\n", rc);
5298 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
5301 /* others are passed further */
5302 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5308 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5309 const struct lu_object_header *hdr,
5310 struct lu_device *d)
5312 struct mdt_object *mo;
5316 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
5318 struct lu_object *o;
5319 struct lu_object_header *h;
5321 o = &mo->mot_obj.mo_lu;
5322 h = &mo->mot_header;
5323 lu_object_header_init(h);
5324 lu_object_init(o, h, d);
5325 lu_object_add_top(h, o);
5326 o->lo_ops = &mdt_obj_ops;
5327 mutex_init(&mo->mot_ioepoch_mutex);
5328 mutex_init(&mo->mot_lov_mutex);
5334 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5335 const struct lu_object_conf *unused)
5337 struct mdt_device *d = mdt_dev(o->lo_dev);
5338 struct lu_device *under;
5339 struct lu_object *below;
5343 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5344 PFID(lu_object_fid(o)));
5346 under = &d->mdt_child->md_lu_dev;
5347 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5348 if (below != NULL) {
5349 lu_object_add(o, below);
5356 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5358 struct mdt_object *mo = mdt_obj(o);
5359 struct lu_object_header *h;
5363 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5364 PFID(lu_object_fid(o)));
5367 lu_object_header_fini(h);
5368 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5373 static int mdt_object_print(const struct lu_env *env, void *cookie,
5374 lu_printer_t p, const struct lu_object *o)
5376 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5377 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5378 "flags="LPX64", epochcount=%d, writecount=%d)",
5379 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5380 mdto->mot_ioepoch_count, mdto->mot_writecount);
5383 static int mdt_prepare(const struct lu_env *env,
5384 struct lu_device *pdev,
5385 struct lu_device *cdev)
5387 struct mdt_device *mdt = mdt_dev(cdev);
5388 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5389 struct obd_device *obd = cdev->ld_obd;
5396 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5400 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5404 rc = mdt_fld_init(env, obd->obd_name, mdt);
5408 rc = mdt_seq_init(env, obd->obd_name, mdt);
5412 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5413 &mdt->mdt_md_root_fid);
5417 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5418 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5419 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5420 LASSERT(obd->obd_no_conn);
5421 spin_lock(&obd->obd_dev_lock);
5422 obd->obd_no_conn = 0;
5423 spin_unlock(&obd->obd_dev_lock);
5425 if (obd->obd_recovering == 0)
5426 mdt_postrecov(env, mdt);
5431 static const struct lu_device_operations mdt_lu_ops = {
5432 .ldo_object_alloc = mdt_object_alloc,
5433 .ldo_process_config = mdt_process_config,
5434 .ldo_prepare = mdt_prepare,
5437 static const struct lu_object_operations mdt_obj_ops = {
5438 .loo_object_init = mdt_object_init,
5439 .loo_object_free = mdt_object_free,
5440 .loo_object_print = mdt_object_print
5443 static int mdt_obd_set_info_async(const struct lu_env *env,
5444 struct obd_export *exp,
5445 __u32 keylen, void *key,
5446 __u32 vallen, void *val,
5447 struct ptlrpc_request_set *set)
5449 struct obd_device *obd = exp->exp_obd;
5455 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5456 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5464 * Match client and server connection feature flags.
5466 * Compute the compatibility flags for a connection request based on
5467 * features mutually supported by client and server.
5469 * The obd_export::exp_connect_flags field in \a exp must not be updated
5470 * here, otherwise a partially initialized value may be exposed. After
5471 * the connection request is successfully processed, the top-level MDT
5472 * connect request handler atomically updates the export connect flags
5473 * from the obd_connect_data::ocd_connect_flags field of the reply.
5474 * \see mdt_connect().
5476 * \param exp the obd_export associated with this client/target pair
5477 * \param mdt the target device for the connection
5478 * \param data stores data for this connect request
5481 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5482 * \retval -EBADE client and server feature requirements are incompatible
5484 static int mdt_connect_internal(struct obd_export *exp,
5485 struct mdt_device *mdt,
5486 struct obd_connect_data *data)
5488 LASSERT(data != NULL);
5490 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5491 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5493 /* If no known bits (which should not happen, probably,
5494 as everybody should support LOOKUP and UPDATE bits at least)
5495 revert to compat mode with plain locks. */
5496 if (!data->ocd_ibits_known &&
5497 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5498 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5500 if (!mdt->mdt_opts.mo_acl)
5501 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5503 if (!mdt->mdt_opts.mo_user_xattr)
5504 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5506 if (!mdt->mdt_som_conf)
5507 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5509 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5510 data->ocd_brw_size = min(data->ocd_brw_size,
5511 (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5512 if (data->ocd_brw_size == 0) {
5513 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5514 " ocd_version: %x ocd_grant: %d "
5515 "ocd_index: %u ocd_brw_size is "
5516 "unexpectedly zero, network data "
5517 "corruption? Refusing connection of this"
5519 exp->exp_obd->obd_name,
5520 exp->exp_client_uuid.uuid,
5521 exp, data->ocd_connect_flags, data->ocd_version,
5522 data->ocd_grant, data->ocd_index);
5527 /* NB: Disregard the rule against updating exp_connect_flags in this
5528 * case, since tgt_client_new() needs to know if this is a lightweight
5529 * connection, and it is safe to expose this flag before connection
5530 * processing completes. */
5531 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5532 spin_lock(&exp->exp_lock);
5533 exp->exp_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
5534 spin_unlock(&exp->exp_lock);
5537 data->ocd_version = LUSTRE_VERSION_CODE;
5538 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5540 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5541 CWARN("%s: MDS requires FID support, but client not\n",
5542 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5546 if (mdt->mdt_som_conf &&
5547 !(data->ocd_connect_flags & (OBD_CONNECT_MDS_MDS|OBD_CONNECT_SOM))){
5548 CWARN("%s: MDS has SOM enabled, but client does not support "
5549 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5556 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5557 struct obd_export *exp,
5558 struct ptlrpc_request *req)
5560 struct sptlrpc_flavor flvr;
5563 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5564 read_lock(&mdt->mdt_sptlrpc_lock);
5565 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5569 read_unlock(&mdt->mdt_sptlrpc_lock);
5571 spin_lock(&exp->exp_lock);
5573 exp->exp_sp_peer = req->rq_sp_from;
5574 exp->exp_flvr = flvr;
5576 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5577 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5578 CERROR("unauthorized rpc flavor %x from %s, "
5579 "expect %x\n", req->rq_flvr.sf_rpc,
5580 libcfs_nid2str(req->rq_peer.nid),
5581 exp->exp_flvr.sf_rpc);
5585 spin_unlock(&exp->exp_lock);
5587 if (exp->exp_sp_peer != req->rq_sp_from) {
5588 CERROR("RPC source %s doesn't match %s\n",
5589 sptlrpc_part2name(req->rq_sp_from),
5590 sptlrpc_part2name(exp->exp_sp_peer));
5593 rc = sptlrpc_target_export_check(exp, req);
5600 /* mds_connect copy */
5601 static int mdt_obd_connect(const struct lu_env *env,
5602 struct obd_export **exp, struct obd_device *obd,
5603 struct obd_uuid *cluuid,
5604 struct obd_connect_data *data,
5607 struct mdt_thread_info *info;
5608 struct obd_export *lexp;
5609 struct lustre_handle conn = { 0 };
5610 struct mdt_device *mdt;
5611 struct ptlrpc_request *req;
5615 LASSERT(env != NULL);
5616 if (!exp || !obd || !cluuid)
5619 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5620 req = info->mti_pill->rc_req;
5621 mdt = mdt_dev(obd->obd_lu_dev);
5624 * first, check whether the stack is ready to handle requests
5625 * XXX: probably not very appropriate method is used now
5626 * at some point we should find a better one
5628 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state)) {
5629 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5632 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5635 rc = class_connect(&conn, obd, cluuid);
5639 lexp = class_conn2export(&conn);
5640 LASSERT(lexp != NULL);
5642 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5646 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5647 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5649 rc = mdt_connect_internal(lexp, mdt, data);
5651 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5654 info->mti_exp = lexp;
5655 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5656 rc = tgt_client_new(env, lexp);
5658 mdt_export_stats_init(obd, lexp, localdata);
5663 class_disconnect(lexp);
5672 static int mdt_obd_reconnect(const struct lu_env *env,
5673 struct obd_export *exp, struct obd_device *obd,
5674 struct obd_uuid *cluuid,
5675 struct obd_connect_data *data,
5678 struct mdt_thread_info *info;
5679 struct mdt_device *mdt;
5680 struct ptlrpc_request *req;
5684 if (exp == NULL || obd == NULL || cluuid == NULL)
5687 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5688 req = info->mti_pill->rc_req;
5689 mdt = mdt_dev(obd->obd_lu_dev);
5691 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5695 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5697 mdt_export_stats_init(obd, exp, localdata);
5702 static int mdt_export_cleanup(struct obd_export *exp)
5704 struct mdt_export_data *med = &exp->exp_mdt_data;
5705 struct obd_device *obd = exp->exp_obd;
5706 struct mdt_device *mdt;
5707 struct mdt_thread_info *info;
5709 CFS_LIST_HEAD(closing_list);
5710 struct mdt_file_data *mfd, *n;
5714 spin_lock(&med->med_open_lock);
5715 while (!cfs_list_empty(&med->med_open_head)) {
5716 cfs_list_t *tmp = med->med_open_head.next;
5717 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5719 /* Remove mfd handle so it can't be found again.
5720 * We are consuming the mfd_list reference here. */
5721 class_handle_unhash(&mfd->mfd_handle);
5722 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5724 spin_unlock(&med->med_open_lock);
5725 mdt = mdt_dev(obd->obd_lu_dev);
5726 LASSERT(mdt != NULL);
5728 rc = lu_env_init(&env, LCT_MD_THREAD);
5732 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5733 LASSERT(info != NULL);
5734 memset(info, 0, sizeof *info);
5735 info->mti_env = &env;
5736 info->mti_mdt = mdt;
5737 info->mti_exp = exp;
5739 if (!cfs_list_empty(&closing_list)) {
5740 struct md_attr *ma = &info->mti_attr;
5742 /* Close any open files (which may also cause orphan unlinking). */
5743 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5744 cfs_list_del_init(&mfd->mfd_list);
5745 ma->ma_need = ma->ma_valid = 0;
5746 /* Don't unlink orphan on failover umount, LU-184 */
5747 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5748 ma->ma_valid = MA_FLAGS;
5749 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5751 mdt_mfd_close(info, mfd);
5754 info->mti_mdt = NULL;
5755 /* cleanup client slot early */
5756 /* Do not erase record for recoverable client. */
5757 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5758 tgt_client_del(&env, exp);
5764 static int mdt_obd_disconnect(struct obd_export *exp)
5770 class_export_get(exp);
5772 rc = server_disconnect_export(exp);
5774 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5776 rc = mdt_export_cleanup(exp);
5777 class_export_put(exp);
5781 /* FIXME: Can we avoid using these two interfaces? */
5782 static int mdt_init_export(struct obd_export *exp)
5784 struct mdt_export_data *med = &exp->exp_mdt_data;
5788 CFS_INIT_LIST_HEAD(&med->med_open_head);
5789 spin_lock_init(&med->med_open_lock);
5790 mutex_init(&med->med_idmap_mutex);
5791 med->med_idmap = NULL;
5792 spin_lock(&exp->exp_lock);
5793 exp->exp_connecting = 1;
5794 spin_unlock(&exp->exp_lock);
5796 /* self-export doesn't need client data and ldlm initialization */
5797 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5798 &exp->exp_client_uuid)))
5801 rc = tgt_client_alloc(exp);
5805 rc = ldlm_init_export(exp);
5812 tgt_client_free(exp);
5814 CERROR("%s: Failed to initialize export: rc = %d\n",
5815 exp->exp_obd->obd_name, rc);
5819 static int mdt_destroy_export(struct obd_export *exp)
5823 if (exp_connect_rmtclient(exp))
5824 mdt_cleanup_idmap(&exp->exp_mdt_data);
5826 target_destroy_export(exp);
5827 /* destroy can be called from failed obd_setup, so
5828 * checking uuid is safer than obd_self_export */
5829 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5830 &exp->exp_client_uuid)))
5833 ldlm_destroy_export(exp);
5834 tgt_client_free(exp);
5836 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5837 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5842 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5843 void *val, int vallen)
5845 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5846 struct getinfo_fid2path *fpout, *fpin;
5849 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5852 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5853 lustre_swab_fid2path(fpin);
5855 memcpy(fpout, fpin, sizeof(*fpin));
5856 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5859 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5863 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5864 struct getinfo_fid2path *fp)
5866 struct mdt_object *obj;
5867 struct obd_device *obd = mdt2obd_dev(mdt);
5871 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5872 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5874 if (!fid_is_sane(&fp->gf_fid))
5877 if (!fid_is_norm(&fp->gf_fid) && !fid_is_igif(&fp->gf_fid)) {
5878 CWARN("%s: "DFID" is invalid, sequence should be "
5879 ">= "LPX64"\n", obd->obd_name,
5880 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5884 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5885 if (obj == NULL || IS_ERR(obj)) {
5886 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5891 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5897 mdt_object_put(env, obj);
5898 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5899 PFID(&fp->gf_fid), rc);
5903 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5904 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5905 mdt_object_put(env, obj);
5910 static int mdt_get_info(struct mdt_thread_info *info)
5912 struct ptlrpc_request *req = mdt_info_req(info);
5920 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5922 CDEBUG(D_IOCTL, "No GETINFO key");
5925 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5928 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5929 if (vallen == NULL) {
5930 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5934 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5936 rc = req_capsule_server_pack(info->mti_pill);
5937 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5938 if (valout == NULL) {
5939 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5943 if (KEY_IS(KEY_FID2PATH))
5944 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5948 lustre_msg_set_status(req->rq_repmsg, rc);
5953 /* Pass the ioc down */
5954 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5955 unsigned int cmd, int len, void *data)
5957 struct lu_context ioctl_session;
5958 struct md_device *next = mdt->mdt_child;
5962 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5965 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5966 lu_context_enter(&ioctl_session);
5967 env->le_ses = &ioctl_session;
5969 LASSERT(next->md_ops->mdo_iocontrol);
5970 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5972 lu_context_exit(&ioctl_session);
5973 lu_context_fini(&ioctl_session);
5977 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5979 struct obd_ioctl_data *data = karg;
5980 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5982 struct mdt_object *obj;
5983 struct mdt_lock_handle *lh;
5987 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5988 if (!fid_is_sane(fid))
5991 lh = &mti->mti_lh[MDT_LH_PARENT];
5992 mdt_lock_reg_init(lh, LCK_CR);
5994 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5996 RETURN(PTR_ERR(obj));
5998 rc = mdt_object_exists(obj);
6002 * before calling version get the correct MDS should be
6003 * fid, this is error to find remote object here
6005 CERROR("nonlocal object "DFID"\n", PFID(fid));
6006 } else if (rc == 0) {
6007 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6010 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6011 *(__u64 *)data->ioc_inlbuf2 = version;
6014 mdt_object_unlock_put(mti, obj, lh, 1);
6018 /* ioctls on obd dev */
6019 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6020 void *karg, void *uarg)
6023 struct obd_device *obd = exp->exp_obd;
6024 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6025 struct dt_device *dt = mdt->mdt_bottom;
6029 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6030 rc = lu_env_init(&env, LCT_MD_THREAD);
6036 rc = mdt_device_sync(&env, mdt);
6038 case OBD_IOC_SET_READONLY:
6039 rc = dt->dd_ops->dt_ro(&env, dt);
6041 case OBD_IOC_ABORT_RECOVERY:
6042 CERROR("Aborting recovery for device %s\n", obd->obd_name);
6043 target_stop_recovery_thread(obd);
6046 case OBD_IOC_CHANGELOG_REG:
6047 case OBD_IOC_CHANGELOG_DEREG:
6048 case OBD_IOC_CHANGELOG_CLEAR:
6049 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6051 case OBD_IOC_START_LFSCK:
6052 case OBD_IOC_STOP_LFSCK: {
6053 struct md_device *next = mdt->mdt_child;
6054 struct obd_ioctl_data *data = karg;
6056 if (unlikely(data == NULL)) {
6061 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
6066 case OBD_IOC_GET_OBJ_VERSION: {
6067 struct mdt_thread_info *mti;
6068 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6069 memset(mti, 0, sizeof *mti);
6070 mti->mti_env = &env;
6074 rc = mdt_ioc_version_get(mti, karg);
6078 CERROR("Not supported cmd = %d for device %s\n",
6079 cmd, obd->obd_name);
6087 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6089 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6093 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6097 int mdt_obd_postrecov(struct obd_device *obd)
6102 rc = lu_env_init(&env, LCT_MD_THREAD);
6105 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6111 * Send a copytool req to a client
6112 * Note this sends a request RPC from a server (MDT) to a client (MDC),
6113 * backwards of normal comms.
6115 int mdt_hsm_copytool_send(struct obd_export *exp)
6118 struct hsm_action_list *hal;
6119 struct hsm_action_item *hai;
6123 CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
6124 libcfs_nid2str(exp->exp_connection->c_peer.nid));
6126 len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
6127 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
6132 lh->kuc_magic = KUC_MAGIC;
6133 lh->kuc_transport = KUC_TRANSPORT_HSM;
6134 lh->kuc_msgtype = HMT_ACTION_LIST;
6135 lh->kuc_msglen = len;
6137 hal = (struct hsm_action_list *)(lh + 1);
6138 hal->hal_version = HAL_VERSION;
6139 hal->hal_archive_num = 1;
6140 obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
6143 /* mock up an action list */
6145 hai = hai_zero(hal);
6146 hai->hai_action = HSMA_ARCHIVE;
6147 hai->hai_fid.f_oid = 0xA00A;
6148 hai->hai_len = sizeof(*hai);
6149 hai = hai_next(hai);
6150 hai->hai_action = HSMA_RESTORE;
6151 hai->hai_fid.f_oid = 0xB00B;
6152 hai->hai_len = sizeof(*hai);
6154 /* Uses the ldlm reverse import; this rpc will be seen by
6155 the ldlm_callback_handler */
6156 rc = do_set_info_async(exp->exp_imp_reverse,
6157 LDLM_SET_INFO, LUSTRE_OBD_VERSION,
6158 sizeof(KEY_HSM_COPYTOOL_SEND),
6159 KEY_HSM_COPYTOOL_SEND,
6167 static struct obd_ops mdt_obd_device_ops = {
6168 .o_owner = THIS_MODULE,
6169 .o_set_info_async = mdt_obd_set_info_async,
6170 .o_connect = mdt_obd_connect,
6171 .o_reconnect = mdt_obd_reconnect,
6172 .o_disconnect = mdt_obd_disconnect,
6173 .o_init_export = mdt_init_export,
6174 .o_destroy_export = mdt_destroy_export,
6175 .o_iocontrol = mdt_iocontrol,
6176 .o_postrecov = mdt_obd_postrecov,
6179 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6180 struct lu_device *d)
6182 struct mdt_device *m = mdt_dev(d);
6189 static struct lu_device *mdt_device_free(const struct lu_env *env,
6190 struct lu_device *d)
6192 struct mdt_device *m = mdt_dev(d);
6195 md_device_fini(&m->mdt_md_dev);
6200 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6201 struct lu_device_type *t,
6202 struct lustre_cfg *cfg)
6204 struct lu_device *l;
6205 struct mdt_device *m;
6211 l = &m->mdt_md_dev.md_lu_dev;
6212 rc = mdt_init0(env, m, t, cfg);
6214 mdt_device_free(env, l);
6219 l = ERR_PTR(-ENOMEM);
6223 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6224 LU_KEY_INIT(mdt, struct mdt_thread_info);
6226 static void mdt_key_fini(const struct lu_context *ctx,
6227 struct lu_context_key *key, void* data)
6229 struct mdt_thread_info *info = data;
6231 if (info->mti_big_lmm) {
6232 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6233 info->mti_big_lmm = NULL;
6234 info->mti_big_lmmsize = 0;
6239 /* context key: mdt_thread_key */
6240 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6242 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6244 return lu_ucred(info->mti_env);
6247 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6249 return lu_ucred_check(info->mti_env);
6253 * Enable/disable COS (Commit On Sharing).
6255 * Set/Clear the COS flag in mdt options.
6257 * \param mdt mdt device
6258 * \param val 0 disables COS, other values enable COS
6260 void mdt_enable_cos(struct mdt_device *mdt, int val)
6265 mdt->mdt_opts.mo_cos = !!val;
6266 rc = lu_env_init(&env, LCT_LOCAL);
6267 if (unlikely(rc != 0)) {
6268 CWARN("lu_env initialization failed with rc = %d,"
6269 "cannot sync\n", rc);
6272 mdt_device_sync(&env, mdt);
6277 * Check COS (Commit On Sharing) status.
6279 * Return COS flag status.
6281 * \param mdt mdt device
6283 int mdt_cos_is_enabled(struct mdt_device *mdt)
6285 return mdt->mdt_opts.mo_cos != 0;
6288 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
6289 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
6291 static struct lu_device_type_operations mdt_device_type_ops = {
6292 .ldto_init = mdt_type_init,
6293 .ldto_fini = mdt_type_fini,
6295 .ldto_start = mdt_type_start,
6296 .ldto_stop = mdt_type_stop,
6298 .ldto_device_alloc = mdt_device_alloc,
6299 .ldto_device_free = mdt_device_free,
6300 .ldto_device_fini = mdt_device_fini
6303 static struct lu_device_type mdt_device_type = {
6304 .ldt_tags = LU_DEVICE_MD,
6305 .ldt_name = LUSTRE_MDT_NAME,
6306 .ldt_ops = &mdt_device_type_ops,
6307 .ldt_ctx_tags = LCT_MD_THREAD
6310 static int __init mdt_mod_init(void)
6312 struct lprocfs_static_vars lvars;
6315 rc = lu_kmem_init(mdt_caches);
6319 if (mdt_num_threads != 0 && mds_num_threads == 0) {
6320 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
6321 "use mds_num_threads instead or unset both for"
6322 "dynamic thread startup\n");
6323 mds_num_threads = mdt_num_threads;
6326 lprocfs_mdt_init_vars(&lvars);
6327 rc = class_register_type(&mdt_obd_device_ops, NULL,
6328 lvars.module_vars, LUSTRE_MDT_NAME,
6332 lu_kmem_fini(mdt_caches);
6336 static void __exit mdt_mod_exit(void)
6338 class_unregister_type(LUSTRE_MDT_NAME);
6339 lu_kmem_fini(mdt_caches);
6342 #define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt) \
6345 .mh_fail_id = OBD_FAIL_ ## opc ## _NET, \
6347 .mh_flags = flags, \
6352 /* Request with a format known in advance */
6353 #define DEF_MDT_HDL(flags, name, fn) \
6354 DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
6356 /* Request with a format we do not yet know */
6357 #define DEF_MDT_HDL_VAR(flags, name, fn) \
6358 DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
6360 /* Map one non-standard request format handler. This should probably get
6361 * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
6362 #define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
6364 static struct mdt_handler mdt_mds_ops[] = {
6365 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6366 DEF_MDT_HDL(0, MDS_DISCONNECT, mdt_disconnect),
6367 DEF_MDT_HDL(0, MDS_SET_INFO, mdt_set_info),
6368 DEF_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
6369 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
6370 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
6371 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME, mdt_getattr_name),
6372 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_getxattr),
6373 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
6374 DEF_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
6375 DEF_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
6376 DEF_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING, mdt_done_writing),
6377 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_PIN, mdt_pin),
6378 DEF_MDT_HDL_VAR(0, MDS_SYNC, mdt_sync),
6379 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
6380 DEF_MDT_HDL(0, MDS_QUOTACHECK, mdt_quotacheck),
6381 DEF_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl)
6384 #define DEF_OBD_HDL(flags, name, fn) \
6385 DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
6387 static struct mdt_handler mdt_obd_ops[] = {
6388 DEF_OBD_HDL(0, OBD_PING, mdt_obd_ping),
6389 DEF_OBD_HDL(0, OBD_LOG_CANCEL, mdt_obd_log_cancel),
6390 DEF_OBD_HDL(0, OBD_QC_CALLBACK, mdt_obd_qc_callback),
6391 DEF_OBD_HDL(0, OBD_IDX_READ, mdt_obd_idx_read)
6394 #define DEF_DLM_HDL_VAR(flags, name, fn) \
6395 DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
6396 #define DEF_DLM_HDL(flags, name, fn) \
6397 DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
6399 static struct mdt_handler mdt_dlm_ops[] = {
6400 DEF_DLM_HDL (HABEO_CLAVIS, LDLM_ENQUEUE, mdt_enqueue),
6401 DEF_DLM_HDL_VAR(HABEO_CLAVIS, LDLM_CONVERT, mdt_convert),
6402 DEF_DLM_HDL_VAR(0, LDLM_BL_CALLBACK, mdt_bl_callback),
6403 DEF_DLM_HDL_VAR(0, LDLM_CP_CALLBACK, mdt_cp_callback)
6406 #define DEF_LLOG_HDL(flags, name, fn) \
6407 DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
6409 static struct mdt_handler mdt_llog_ops[] = {
6410 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_create),
6411 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
6412 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
6413 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_WRITE_REC, NULL),
6414 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CLOSE, NULL),
6415 DEF_LLOG_HDL(0, LLOG_ORIGIN_CONNECT, NULL),
6416 DEF_LLOG_HDL(0, LLOG_CATINFO, NULL),
6417 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
6418 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
6421 #define DEF_SEC_HDL(flags, name, fn) \
6422 DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
6424 static struct mdt_handler mdt_sec_ctx_ops[] = {
6425 DEF_SEC_HDL(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
6426 DEF_SEC_HDL(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
6427 DEF_SEC_HDL(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
6430 #define DEF_QUOTA_HDL(flags, name, fn) \
6431 DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
6433 static struct mdt_handler mdt_quota_ops[] = {
6434 DEF_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
6437 static struct mdt_opc_slice mdt_regular_handlers[] = {
6439 .mos_opc_start = MDS_GETATTR,
6440 .mos_opc_end = MDS_LAST_OPC,
6441 .mos_hs = mdt_mds_ops
6444 .mos_opc_start = OBD_PING,
6445 .mos_opc_end = OBD_LAST_OPC,
6446 .mos_hs = mdt_obd_ops
6449 .mos_opc_start = LDLM_ENQUEUE,
6450 .mos_opc_end = LDLM_LAST_OPC,
6451 .mos_hs = mdt_dlm_ops
6454 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6455 .mos_opc_end = LLOG_LAST_OPC,
6456 .mos_hs = mdt_llog_ops
6459 .mos_opc_start = SEC_CTX_INIT,
6460 .mos_opc_end = SEC_LAST_OPC,
6461 .mos_hs = mdt_sec_ctx_ops
6464 .mos_opc_start = QUOTA_DQACQ,
6465 .mos_opc_end = QUOTA_LAST_OPC,
6466 .mos_hs = mdt_quota_ops
6473 /* Readpage/readdir handlers */
6474 static struct mdt_handler mdt_readpage_ops[] = {
6475 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6476 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
6477 /* XXX: this is ugly and should be fixed one day, see mdc_close() for
6478 * detailed comments. --umka */
6479 DEF_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
6480 DEF_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING, mdt_done_writing),
6483 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6485 .mos_opc_start = MDS_GETATTR,
6486 .mos_opc_end = MDS_LAST_OPC,
6487 .mos_hs = mdt_readpage_ops
6490 .mos_opc_start = OBD_FIRST_OPC,
6491 .mos_opc_end = OBD_LAST_OPC,
6492 .mos_hs = mdt_obd_ops
6499 /* Cross MDT operation handlers for DNE */
6500 static struct mdt_handler mdt_xmds_ops[] = {
6501 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6502 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
6503 DEF_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
6504 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
6507 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6509 .mos_opc_start = MDS_GETATTR,
6510 .mos_opc_end = MDS_LAST_OPC,
6511 .mos_hs = mdt_xmds_ops
6514 .mos_opc_start = OBD_PING,
6515 .mos_opc_end = OBD_LAST_OPC,
6516 .mos_hs = mdt_obd_ops
6519 .mos_opc_start = SEC_CTX_INIT,
6520 .mos_opc_end = SEC_LAST_OPC,
6521 .mos_hs = mdt_sec_ctx_ops
6528 /* Sequence service handlers */
6529 #define DEF_SEQ_HDL(flags, name, fn) \
6530 DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
6532 static struct mdt_handler mdt_seq_ops[] = {
6533 DEF_SEQ_HDL(0, SEQ_QUERY, (void *)seq_query),
6536 static struct mdt_opc_slice mdt_seq_handlers[] = {
6538 .mos_opc_start = SEQ_QUERY,
6539 .mos_opc_end = SEQ_LAST_OPC,
6540 .mos_hs = mdt_seq_ops
6547 /* FID Location Database handlers */
6548 #define DEF_FLD_HDL(flags, name, fn) \
6549 DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
6551 static struct mdt_handler mdt_fld_ops[] = {
6552 DEF_FLD_HDL(0, FLD_QUERY, (void *)fld_query),
6555 static struct mdt_opc_slice mdt_fld_handlers[] = {
6557 .mos_opc_start = FLD_QUERY,
6558 .mos_opc_end = FLD_LAST_OPC,
6559 .mos_hs = mdt_fld_ops
6566 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6567 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6568 MODULE_LICENSE("GPL");
6570 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);