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) {
716 if (rc == 0 && S_ISREG(mode) && (need & (MA_HSM | MA_SOM))) {
717 struct lustre_mdt_attrs *lma;
719 lma = (struct lustre_mdt_attrs *)info->mti_xattr_buf;
720 CLASSERT(sizeof(*lma) <= sizeof(info->mti_xattr_buf));
723 buf->lb_len = sizeof(info->mti_xattr_buf);
724 rc = mo_xattr_get(env, next, buf, XATTR_NAME_LMA);
726 lustre_lma_swab(lma);
727 /* Swab and copy LMA */
729 if (lma->lma_compat & LMAC_HSM)
730 ma->ma_hsm.mh_flags =
731 lma->lma_flags & HSM_FLAGS_MASK;
733 ma->ma_hsm.mh_flags = 0;
734 ma->ma_valid |= MA_HSM;
737 if (need & MA_SOM && lma->lma_compat & LMAC_SOM) {
738 LASSERT(ma->ma_som != NULL);
739 ma->ma_som->msd_ioepoch = lma->lma_ioepoch;
740 ma->ma_som->msd_size = lma->lma_som_size;
741 ma->ma_som->msd_blocks = lma->lma_som_blocks;
742 ma->ma_som->msd_mountid = lma->lma_som_mountid;
743 ma->ma_valid |= MA_SOM;
746 } else if (rc == -ENODATA) {
751 #ifdef CONFIG_FS_POSIX_ACL
752 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
753 buf->lb_buf = ma->ma_acl;
754 buf->lb_len = ma->ma_acl_size;
755 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
757 ma->ma_acl_size = rc2;
758 ma->ma_valid |= MA_ACL_DEF;
759 } else if (rc2 == -ENODATA) {
768 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
769 rc, ma->ma_valid, ma->ma_lmm);
773 static int mdt_getattr_internal(struct mdt_thread_info *info,
774 struct mdt_object *o, int ma_need)
776 struct md_object *next = mdt_object_child(o);
777 const struct mdt_body *reqbody = info->mti_body;
778 struct ptlrpc_request *req = mdt_info_req(info);
779 struct md_attr *ma = &info->mti_attr;
780 struct lu_attr *la = &ma->ma_attr;
781 struct req_capsule *pill = info->mti_pill;
782 const struct lu_env *env = info->mti_env;
783 struct mdt_body *repbody;
784 struct lu_buf *buffer = &info->mti_buf;
789 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
790 RETURN(err_serious(-ENOMEM));
792 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
796 rc = mdt_object_exists(o);
798 /* This object is located on remote node.*/
799 repbody->fid1 = *mdt_object_fid(o);
800 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
804 buffer->lb_len = reqbody->eadatasize;
805 if (buffer->lb_len > 0)
806 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
808 buffer->lb_buf = NULL;
810 /* If it is dir object and client require MEA, then we got MEA */
811 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
812 reqbody->valid & OBD_MD_MEA) {
813 /* Assumption: MDT_MD size is enough for lmv size. */
814 ma->ma_lmv = buffer->lb_buf;
815 ma->ma_lmv_size = buffer->lb_len;
816 ma->ma_need = MA_LMV | MA_INODE;
818 ma->ma_lmm = buffer->lb_buf;
819 ma->ma_lmm_size = buffer->lb_len;
820 ma->ma_need = MA_LOV | MA_INODE;
823 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
824 reqbody->valid & OBD_MD_FLDIREA &&
825 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
826 /* get default stripe info for this dir. */
827 ma->ma_need |= MA_LOV_DEF;
829 ma->ma_need |= ma_need;
830 if (ma->ma_need & MA_SOM)
831 ma->ma_som = &info->mti_u.som.data;
833 rc = mdt_attr_get_complex(info, o, ma);
835 CERROR("getattr error for "DFID": %d\n",
836 PFID(mdt_object_fid(o)), rc);
840 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
842 /* the Lustre protocol supposes to return default striping
843 * on the user-visible root if explicitly requested */
844 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
845 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
846 struct lu_fid rootfid;
847 struct mdt_object *root;
848 struct mdt_device *mdt = info->mti_mdt;
850 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
853 root = mdt_object_find(env, mdt, &rootfid);
855 RETURN(PTR_ERR(root));
856 rc = mdt_attr_get_lov(info, root, ma);
857 mdt_object_put(info->mti_env, root);
859 CERROR("getattr error for "DFID": %d\n",
860 PFID(mdt_object_fid(o)), rc);
865 if (likely(ma->ma_valid & MA_INODE))
866 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
870 if (mdt_body_has_lov(la, reqbody)) {
871 if (ma->ma_valid & MA_LOV) {
872 LASSERT(ma->ma_lmm_size);
873 mdt_dump_lmm(D_INFO, ma->ma_lmm);
874 repbody->eadatasize = ma->ma_lmm_size;
875 if (S_ISDIR(la->la_mode))
876 repbody->valid |= OBD_MD_FLDIREA;
878 repbody->valid |= OBD_MD_FLEASIZE;
880 if (ma->ma_valid & MA_LMV) {
881 LASSERT(S_ISDIR(la->la_mode));
882 repbody->eadatasize = ma->ma_lmv_size;
883 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
885 } else if (S_ISLNK(la->la_mode) &&
886 reqbody->valid & OBD_MD_LINKNAME) {
887 buffer->lb_buf = ma->ma_lmm;
888 /* eadatasize from client includes NULL-terminator, so
889 * there is no need to read it */
890 buffer->lb_len = reqbody->eadatasize - 1;
891 rc = mo_readlink(env, next, buffer);
892 if (unlikely(rc <= 0)) {
893 CERROR("readlink failed: %d\n", rc);
896 int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
898 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
900 repbody->valid |= OBD_MD_LINKNAME;
901 /* we need to report back size with NULL-terminator
902 * because client expects that */
903 repbody->eadatasize = rc + 1;
904 if (repbody->eadatasize != reqbody->eadatasize)
905 CERROR("Read shorter symlink %d, expected %d\n",
906 rc, reqbody->eadatasize - 1);
908 ((char *)ma->ma_lmm)[rc] = 0;
910 /* If the total CDEBUG() size is larger than a page, it
911 * will print a warning to the console, avoid this by
912 * printing just the last part of the symlink. */
913 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
914 print_limit < rc ? "..." : "", print_limit,
915 (char *)ma->ma_lmm + rc - print_limit, rc);
920 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
921 repbody->max_cookiesize = 0;
922 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
923 repbody->valid |= OBD_MD_FLMODEASIZE;
924 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
925 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
926 repbody->max_cookiesize);
929 if (exp_connect_rmtclient(info->mti_exp) &&
930 reqbody->valid & OBD_MD_FLRMTPERM) {
931 void *buf = req_capsule_server_get(pill, &RMF_ACL);
933 /* mdt_getattr_lock only */
934 rc = mdt_pack_remote_perm(info, o, buf);
936 repbody->valid &= ~OBD_MD_FLRMTPERM;
937 repbody->aclsize = 0;
940 repbody->valid |= OBD_MD_FLRMTPERM;
941 repbody->aclsize = sizeof(struct mdt_remote_perm);
944 #ifdef CONFIG_FS_POSIX_ACL
945 else if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
946 (reqbody->valid & OBD_MD_FLACL)) {
947 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
948 buffer->lb_len = req_capsule_get_size(pill,
949 &RMF_ACL, RCL_SERVER);
950 if (buffer->lb_len > 0) {
951 rc = mo_xattr_get(env, next, buffer,
952 XATTR_NAME_ACL_ACCESS);
954 if (rc == -ENODATA) {
955 repbody->aclsize = 0;
956 repbody->valid |= OBD_MD_FLACL;
958 } else if (rc == -EOPNOTSUPP) {
961 CERROR("got acl size: %d\n", rc);
964 repbody->aclsize = rc;
965 repbody->valid |= OBD_MD_FLACL;
972 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
973 info->mti_mdt->mdt_opts.mo_mds_capa &&
974 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
975 struct lustre_capa *capa;
977 capa = req_capsule_server_get(pill, &RMF_CAPA1);
979 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
980 rc = mo_capa_get(env, next, capa, 0);
983 repbody->valid |= OBD_MD_FLMDSCAPA;
988 mdt_counter_incr(req, LPROC_MDT_GETATTR);
993 static int mdt_renew_capa(struct mdt_thread_info *info)
995 struct mdt_object *obj = info->mti_object;
996 struct mdt_body *body;
997 struct lustre_capa *capa, *c;
1001 /* if object doesn't exist, or server has disabled capability,
1002 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1005 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
1006 !(info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA))
1009 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1010 LASSERT(body != NULL);
1012 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1015 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1019 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1021 body->valid |= OBD_MD_FLOSSCAPA;
1025 static int mdt_getattr(struct mdt_thread_info *info)
1027 struct mdt_object *obj = info->mti_object;
1028 struct req_capsule *pill = info->mti_pill;
1029 struct mdt_body *reqbody;
1030 struct mdt_body *repbody;
1035 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1038 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1039 rc = req_capsule_server_pack(pill);
1041 RETURN(err_serious(rc));
1042 rc = mdt_renew_capa(info);
1043 GOTO(out_shrink, rc);
1046 LASSERT(obj != NULL);
1047 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
1049 mode = lu_object_attr(&obj->mot_obj.mo_lu);
1051 /* old clients may not report needed easize, use max value then */
1052 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1053 reqbody->eadatasize == 0 ?
1054 info->mti_mdt->mdt_max_mdsize :
1055 reqbody->eadatasize);
1057 rc = req_capsule_server_pack(pill);
1058 if (unlikely(rc != 0))
1059 RETURN(err_serious(rc));
1061 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1062 LASSERT(repbody != NULL);
1063 repbody->eadatasize = 0;
1064 repbody->aclsize = 0;
1066 if (reqbody->valid & OBD_MD_FLRMTPERM)
1067 rc = mdt_init_ucred(info, reqbody);
1069 rc = mdt_check_ucred(info);
1071 GOTO(out_shrink, rc);
1073 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1076 * Don't check capability at all, because rename might getattr for
1077 * remote obj, and at that time no capability is available.
1079 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1080 rc = mdt_getattr_internal(info, obj, 0);
1081 if (reqbody->valid & OBD_MD_FLRMTPERM)
1082 mdt_exit_ucred(info);
1085 mdt_client_compatibility(info);
1086 rc2 = mdt_fix_reply(info);
1092 static int mdt_is_subdir(struct mdt_thread_info *info)
1094 struct mdt_object *o = info->mti_object;
1095 struct req_capsule *pill = info->mti_pill;
1096 const struct mdt_body *body = info->mti_body;
1097 struct mdt_body *repbody;
1103 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1106 * We save last checked parent fid to @repbody->fid1 for remote
1109 LASSERT(fid_is_sane(&body->fid2));
1110 LASSERT(mdt_object_exists(o) > 0);
1111 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1112 &body->fid2, &repbody->fid1);
1113 if (rc == 0 || rc == -EREMOTE)
1114 repbody->valid |= OBD_MD_FLID;
1119 static int mdt_raw_lookup(struct mdt_thread_info *info,
1120 struct mdt_object *parent,
1121 const struct lu_name *lname,
1122 struct ldlm_reply *ldlm_rep)
1124 struct md_object *next = mdt_object_child(info->mti_object);
1125 const struct mdt_body *reqbody = info->mti_body;
1126 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1127 struct mdt_body *repbody;
1131 if (reqbody->valid != OBD_MD_FLID)
1134 LASSERT(!info->mti_cross_ref);
1136 /* Only got the fid of this obj by name */
1137 fid_zero(child_fid);
1138 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1141 /* XXX is raw_lookup possible as intent operation? */
1144 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1147 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1149 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1152 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1153 repbody->fid1 = *child_fid;
1154 repbody->valid = OBD_MD_FLID;
1160 * UPDATE lock should be taken against parent, and be release before exit;
1161 * child_bits lock should be taken against child, and be returned back:
1162 * (1)normal request should release the child lock;
1163 * (2)intent request will grant the lock to client.
1165 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1166 struct mdt_lock_handle *lhc,
1168 struct ldlm_reply *ldlm_rep)
1170 struct ptlrpc_request *req = mdt_info_req(info);
1171 struct mdt_body *reqbody = NULL;
1172 struct mdt_object *parent = info->mti_object;
1173 struct mdt_object *child;
1174 struct md_object *next = mdt_object_child(parent);
1175 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1176 struct lu_name *lname = NULL;
1177 const char *name = NULL;
1179 struct mdt_lock_handle *lhp = NULL;
1180 struct ldlm_lock *lock;
1181 struct ldlm_res_id *res_id;
1188 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1189 LASSERT(ergo(is_resent,
1190 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1192 LASSERT(parent != NULL);
1193 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1195 RETURN(err_serious(-EFAULT));
1197 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1199 if (!info->mti_cross_ref) {
1201 * XXX: Check for "namelen == 0" is for getattr by fid
1202 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1203 * that is the name must contain at least one character and
1204 * the terminating '\0'
1207 reqbody = req_capsule_client_get(info->mti_pill,
1209 if (unlikely(reqbody == NULL))
1210 RETURN(err_serious(-EFAULT));
1212 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1213 RETURN(err_serious(-EINVAL));
1216 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1218 PFID(mdt_object_fid(parent)),
1219 PFID(&reqbody->fid2), ldlm_rep);
1221 lname = mdt_name(info->mti_env, (char *)name, namelen);
1222 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1223 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1227 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1229 rc = mdt_object_exists(parent);
1230 if (unlikely(rc == 0)) {
1231 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1232 &parent->mot_obj.mo_lu,
1233 "Parent doesn't exist!\n");
1235 } else if (!info->mti_cross_ref) {
1236 LASSERTF(rc > 0, "Parent "DFID" is on remote server\n",
1237 PFID(mdt_object_fid(parent)));
1240 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1248 if (info->mti_cross_ref) {
1249 /* Only getattr on the child. Parent is on another node. */
1250 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1252 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1253 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1256 /* Do not take lock for resent case. */
1257 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1258 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1259 lhc->mlh_reg_lh.cookie);
1260 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1261 &lock->l_resource->lr_name));
1262 LDLM_LOCK_PUT(lock);
1265 mdt_lock_handle_init(lhc);
1266 mdt_lock_reg_init(lhc, LCK_PR);
1269 * Object's name is on another MDS, no lookup lock is
1270 * needed here but update is.
1272 child_bits &= ~MDS_INODELOCK_LOOKUP;
1273 child_bits |= MDS_INODELOCK_UPDATE;
1275 rc = mdt_object_lock(info, child, lhc, child_bits,
1279 /* Finally, we can get attr for child. */
1280 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1282 rc = mdt_getattr_internal(info, child, 0);
1283 if (unlikely(rc != 0))
1284 mdt_object_unlock(info, child, lhc, 1);
1290 /* step 1: lock parent only if parent is a directory */
1291 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1292 lhp = &info->mti_lh[MDT_LH_PARENT];
1293 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1294 rc = mdt_object_lock(info, parent, lhp,
1295 MDS_INODELOCK_UPDATE,
1297 if (unlikely(rc != 0))
1301 /* step 2: lookup child's fid by name */
1302 fid_zero(child_fid);
1303 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1308 mdt_set_disposition(info, ldlm_rep,
1310 GOTO(out_parent, rc);
1312 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1314 *child_fid = reqbody->fid2;
1315 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1319 *step 3: find the child object by fid & lock it.
1320 * regardless if it is local or remote.
1322 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1324 if (unlikely(IS_ERR(child)))
1325 GOTO(out_parent, rc = PTR_ERR(child));
1327 /* Do not take lock for resent case. */
1328 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1329 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1330 lhc->mlh_reg_lh.cookie);
1332 res_id = &lock->l_resource->lr_name;
1333 if (!fid_res_name_eq(mdt_object_fid(child),
1334 &lock->l_resource->lr_name)) {
1335 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1336 &lock->l_resource->lr_name),
1337 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1338 (unsigned long)res_id->name[0],
1339 (unsigned long)res_id->name[1],
1340 (unsigned long)res_id->name[2],
1341 PFID(mdt_object_fid(parent)));
1342 CWARN("Although resent, but still not get child lock"
1343 "parent:"DFID" child:"DFID"\n",
1344 PFID(mdt_object_fid(parent)),
1345 PFID(mdt_object_fid(child)));
1346 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1347 LDLM_LOCK_PUT(lock);
1350 LDLM_LOCK_PUT(lock);
1354 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1355 mdt_lock_handle_init(lhc);
1356 if (child_bits == MDS_INODELOCK_LAYOUT)
1357 mdt_lock_reg_init(lhc, LCK_CR);
1359 mdt_lock_reg_init(lhc, LCK_PR);
1361 if (mdt_object_exists(child) == 0) {
1362 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1363 &child->mot_obj.mo_lu,
1364 "Object doesn't exist!\n");
1365 GOTO(out_child, rc = -ENOENT);
1368 if (!(child_bits & MDS_INODELOCK_UPDATE)) {
1369 struct md_attr *ma = &info->mti_attr;
1372 ma->ma_need = MA_INODE;
1373 rc = mdt_attr_get_complex(info, child, ma);
1374 if (unlikely(rc != 0))
1375 GOTO(out_child, rc);
1377 /* layout lock is used only on regular files */
1378 if ((ma->ma_valid & MA_INODE) &&
1379 (ma->ma_attr.la_valid & LA_MODE) &&
1380 !S_ISREG(ma->ma_attr.la_mode))
1381 child_bits &= ~MDS_INODELOCK_LAYOUT;
1383 /* If the file has not been changed for some time, we
1384 * return not only a LOOKUP lock, but also an UPDATE
1385 * lock and this might save us RPC on later STAT. For
1386 * directories, it also let negative dentry starts
1387 * working for this dir. */
1388 if (ma->ma_valid & MA_INODE &&
1389 ma->ma_attr.la_valid & LA_CTIME &&
1390 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1391 ma->ma_attr.la_ctime < cfs_time_current_sec())
1392 child_bits |= MDS_INODELOCK_UPDATE;
1395 rc = mdt_object_lock(info, child, lhc, child_bits,
1398 if (unlikely(rc != 0))
1399 GOTO(out_child, rc);
1402 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1403 /* Get MA_SOM attributes if update lock is given. */
1405 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1406 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1409 /* finally, we can get attr for child. */
1410 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1411 rc = mdt_getattr_internal(info, child, ma_need);
1412 if (unlikely(rc != 0)) {
1413 mdt_object_unlock(info, child, lhc, 1);
1415 /* Debugging code. */
1416 res_id = &lock->l_resource->lr_name;
1417 LDLM_DEBUG(lock, "Returning lock to client");
1418 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1419 &lock->l_resource->lr_name),
1420 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1421 (unsigned long)res_id->name[0],
1422 (unsigned long)res_id->name[1],
1423 (unsigned long)res_id->name[2],
1424 PFID(mdt_object_fid(child)));
1425 mdt_pack_size2body(info, child);
1428 LDLM_LOCK_PUT(lock);
1432 mdt_object_put(info->mti_env, child);
1435 mdt_object_unlock(info, parent, lhp, 1);
1439 /* normal handler: should release the child lock */
1440 static int mdt_getattr_name(struct mdt_thread_info *info)
1442 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1443 struct mdt_body *reqbody;
1444 struct mdt_body *repbody;
1448 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1449 LASSERT(reqbody != NULL);
1450 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1451 LASSERT(repbody != NULL);
1453 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1454 repbody->eadatasize = 0;
1455 repbody->aclsize = 0;
1457 rc = mdt_init_ucred(info, reqbody);
1459 GOTO(out_shrink, rc);
1461 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1462 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1463 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1464 lhc->mlh_reg_lh.cookie = 0;
1466 mdt_exit_ucred(info);
1469 mdt_client_compatibility(info);
1470 rc2 = mdt_fix_reply(info);
1476 static const struct lu_device_operations mdt_lu_ops;
1478 static int lu_device_is_mdt(struct lu_device *d)
1480 return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &mdt_lu_ops);
1483 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1484 void *karg, void *uarg);
1486 static int mdt_set_info(struct mdt_thread_info *info)
1488 struct ptlrpc_request *req = mdt_info_req(info);
1491 int keylen, vallen, rc = 0;
1494 rc = req_capsule_server_pack(info->mti_pill);
1498 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1500 DEBUG_REQ(D_HA, req, "no set_info key");
1504 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1507 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1509 DEBUG_REQ(D_HA, req, "no set_info val");
1513 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1516 /* Swab any part of val you need to here */
1517 if (KEY_IS(KEY_READ_ONLY)) {
1519 lustre_msg_set_status(req->rq_repmsg, 0);
1521 spin_lock(&req->rq_export->exp_lock);
1523 req->rq_export->exp_connect_flags |= OBD_CONNECT_RDONLY;
1525 req->rq_export->exp_connect_flags &=~OBD_CONNECT_RDONLY;
1526 spin_unlock(&req->rq_export->exp_lock);
1528 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1529 struct changelog_setinfo *cs =
1530 (struct changelog_setinfo *)val;
1531 if (vallen != sizeof(*cs)) {
1532 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1535 if (ptlrpc_req_need_swab(req)) {
1536 __swab64s(&cs->cs_recno);
1537 __swab32s(&cs->cs_id);
1540 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1542 lustre_msg_set_status(req->rq_repmsg, rc);
1551 * Top-level handler for MDT connection requests.
1553 static int mdt_connect(struct mdt_thread_info *info)
1556 struct obd_connect_data *reply;
1557 struct obd_export *exp;
1558 struct ptlrpc_request *req = mdt_info_req(info);
1560 rc = target_handle_connect(req);
1562 return err_serious(rc);
1564 LASSERT(req->rq_export != NULL);
1565 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1566 rc = mdt_init_sec_level(info);
1568 obd_disconnect(class_export_get(req->rq_export));
1572 /* To avoid exposing partially initialized connection flags, changes up
1573 * to this point have been staged in reply->ocd_connect_flags. Now that
1574 * connection handling has completed successfully, atomically update
1575 * the connect flags in the shared export data structure. LU-1623 */
1576 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1577 exp = req->rq_export;
1578 spin_lock(&exp->exp_lock);
1579 exp->exp_connect_flags = reply->ocd_connect_flags;
1580 spin_unlock(&exp->exp_lock);
1582 rc = mdt_init_idmap(info);
1584 obd_disconnect(class_export_get(req->rq_export));
1589 static int mdt_disconnect(struct mdt_thread_info *info)
1594 rc = target_handle_disconnect(mdt_info_req(info));
1596 rc = err_serious(rc);
1600 static int mdt_sendpage(struct mdt_thread_info *info,
1601 struct lu_rdpg *rdpg, int nob)
1603 struct ptlrpc_request *req = mdt_info_req(info);
1604 struct obd_export *exp = req->rq_export;
1605 struct ptlrpc_bulk_desc *desc;
1606 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1613 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, BULK_PUT_SOURCE,
1618 if (!(exp->exp_connect_flags & OBD_CONNECT_BRW_SIZE))
1619 /* old client requires reply size in it's PAGE_SIZE,
1620 * which is rdpg->rp_count */
1621 nob = rdpg->rp_count;
1623 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1624 i++, tmpcount -= tmpsize) {
1625 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1626 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1629 LASSERT(desc->bd_nob == nob);
1630 rc = target_bulk_io(exp, desc, lwi);
1631 ptlrpc_free_bulk_pin(desc);
1635 static int mdt_readpage(struct mdt_thread_info *info)
1637 struct mdt_object *object = info->mti_object;
1638 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1639 struct mdt_body *reqbody;
1640 struct mdt_body *repbody;
1645 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1646 RETURN(err_serious(-ENOMEM));
1648 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1649 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1650 if (reqbody == NULL || repbody == NULL)
1651 RETURN(err_serious(-EFAULT));
1654 * prepare @rdpg before calling lower layers and transfer itself. Here
1655 * reqbody->size contains offset of where to start to read and
1656 * reqbody->nlink contains number bytes to read.
1658 rdpg->rp_hash = reqbody->size;
1659 if (rdpg->rp_hash != reqbody->size) {
1660 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1661 rdpg->rp_hash, reqbody->size);
1665 rdpg->rp_attrs = reqbody->mode;
1666 if (info->mti_exp->exp_connect_flags & OBD_CONNECT_64BITHASH)
1667 rdpg->rp_attrs |= LUDA_64BITHASH;
1668 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1669 PTLRPC_MAX_BRW_SIZE);
1670 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1672 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1673 if (rdpg->rp_pages == NULL)
1676 for (i = 0; i < rdpg->rp_npages; ++i) {
1677 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1678 if (rdpg->rp_pages[i] == NULL)
1679 GOTO(free_rdpg, rc = -ENOMEM);
1682 /* call lower layers to fill allocated pages with directory data */
1683 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1685 GOTO(free_rdpg, rc);
1687 /* send pages to client */
1688 rc = mdt_sendpage(info, rdpg, rc);
1693 for (i = 0; i < rdpg->rp_npages; i++)
1694 if (rdpg->rp_pages[i] != NULL)
1695 cfs_free_page(rdpg->rp_pages[i]);
1696 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1698 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1704 static int mdt_reint_internal(struct mdt_thread_info *info,
1705 struct mdt_lock_handle *lhc,
1708 struct req_capsule *pill = info->mti_pill;
1709 struct mdt_body *repbody;
1714 rc = mdt_reint_unpack(info, op);
1716 CERROR("Can't unpack reint, rc %d\n", rc);
1717 RETURN(err_serious(rc));
1720 /* for replay (no_create) lmm is not needed, client has it already */
1721 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1722 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1723 info->mti_rr.rr_eadatalen);
1725 /* llog cookies are always 0, the field is kept for compatibility */
1726 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1727 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1729 rc = req_capsule_server_pack(pill);
1731 CERROR("Can't pack response, rc %d\n", rc);
1732 RETURN(err_serious(rc));
1735 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1736 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1738 repbody->eadatasize = 0;
1739 repbody->aclsize = 0;
1742 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1744 /* for replay no cookkie / lmm need, because client have this already */
1745 if (info->mti_spec.no_create)
1746 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1747 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1749 rc = mdt_init_ucred_reint(info);
1751 GOTO(out_shrink, rc);
1753 rc = mdt_fix_attr_ucred(info, op);
1755 GOTO(out_ucred, rc = err_serious(rc));
1757 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1758 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1759 GOTO(out_ucred, rc);
1761 rc = mdt_reint_rec(info, lhc);
1764 mdt_exit_ucred(info);
1766 mdt_client_compatibility(info);
1767 rc2 = mdt_fix_reply(info);
1773 static long mdt_reint_opcode(struct mdt_thread_info *info,
1774 const struct req_format **fmt)
1776 struct mdt_rec_reint *rec;
1779 opc = err_serious(-EFAULT);
1780 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1782 opc = rec->rr_opcode;
1783 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1784 if (opc < REINT_MAX && fmt[opc] != NULL)
1785 req_capsule_extend(info->mti_pill, fmt[opc]);
1787 CERROR("Unsupported opc: %ld\n", opc);
1788 opc = err_serious(opc);
1794 static int mdt_reint(struct mdt_thread_info *info)
1799 static const struct req_format *reint_fmts[REINT_MAX] = {
1800 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1801 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1802 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1803 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1804 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1805 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1806 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR
1811 opc = mdt_reint_opcode(info, reint_fmts);
1814 * No lock possible here from client to pass it to reint code
1817 rc = mdt_reint_internal(info, NULL, opc);
1822 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1826 /* this should sync the whole device */
1827 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1829 struct dt_device *dt = mdt->mdt_bottom;
1833 rc = dt->dd_ops->dt_sync(env, dt);
1837 /* this should sync this object */
1838 static int mdt_object_sync(struct mdt_thread_info *info)
1840 struct md_object *next;
1844 if (!mdt_object_exists(info->mti_object)) {
1845 CWARN("Non existing object "DFID"!\n",
1846 PFID(mdt_object_fid(info->mti_object)));
1849 next = mdt_object_child(info->mti_object);
1850 rc = mo_object_sync(info->mti_env, next);
1855 static int mdt_sync(struct mdt_thread_info *info)
1857 struct ptlrpc_request *req = mdt_info_req(info);
1858 struct req_capsule *pill = info->mti_pill;
1859 struct mdt_body *body;
1863 /* The fid may be zero, so we req_capsule_set manually */
1864 req_capsule_set(pill, &RQF_MDS_SYNC);
1866 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1868 RETURN(err_serious(-EINVAL));
1870 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1871 RETURN(err_serious(-ENOMEM));
1873 if (fid_seq(&body->fid1) == 0) {
1874 /* sync the whole device */
1875 rc = req_capsule_server_pack(pill);
1877 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1879 rc = err_serious(rc);
1881 /* sync an object */
1882 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1884 rc = mdt_object_sync(info);
1886 const struct lu_fid *fid;
1887 struct lu_attr *la = &info->mti_attr.ma_attr;
1889 info->mti_attr.ma_need = MA_INODE;
1890 info->mti_attr.ma_valid = 0;
1891 rc = mdt_attr_get_complex(info, info->mti_object,
1894 body = req_capsule_server_get(pill,
1896 fid = mdt_object_fid(info->mti_object);
1897 mdt_pack_attr2body(info, body, la, fid);
1901 rc = err_serious(rc);
1904 mdt_counter_incr(req, LPROC_MDT_SYNC);
1910 * Quotacheck handler.
1911 * in-kernel quotacheck isn't supported any more.
1913 static int mdt_quotacheck(struct mdt_thread_info *info)
1915 struct obd_quotactl *oqctl;
1919 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1921 RETURN(err_serious(-EPROTO));
1923 rc = req_capsule_server_pack(info->mti_pill);
1925 RETURN(err_serious(rc));
1927 /* deprecated, not used any more */
1928 RETURN(-EOPNOTSUPP);
1932 * Handle quota control requests to consult current usage/limit, but also
1933 * to configure quota enforcement
1935 static int mdt_quotactl(struct mdt_thread_info *info)
1937 struct obd_export *exp = info->mti_exp;
1938 struct req_capsule *pill = info->mti_pill;
1939 struct obd_quotactl *oqctl, *repoqc;
1941 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1944 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1946 RETURN(err_serious(-EPROTO));
1948 rc = req_capsule_server_pack(pill);
1950 RETURN(err_serious(rc));
1952 switch (oqctl->qc_cmd) {
1954 case LUSTRE_Q_INVALIDATE:
1955 case LUSTRE_Q_FINVALIDATE:
1959 /* deprecated, not used any more */
1960 RETURN(-EOPNOTSUPP);
1961 /* master quotactl */
1967 RETURN(-EOPNOTSUPP);
1968 /* slave quotactl */
1973 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1977 /* map uid/gid for remote client */
1979 if (exp_connect_rmtclient(exp)) {
1980 struct lustre_idmap_table *idmap;
1982 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1984 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1985 oqctl->qc_cmd != Q_GETINFO))
1988 if (oqctl->qc_type == USRQUOTA)
1989 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1991 else if (oqctl->qc_type == GRPQUOTA)
1992 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1997 if (id == CFS_IDMAP_NOTFOUND) {
1998 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2003 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2005 RETURN(err_serious(-EFAULT));
2007 if (oqctl->qc_id != id)
2008 swap(oqctl->qc_id, id);
2010 switch (oqctl->qc_cmd) {
2016 /* forward quotactl request to QMT */
2017 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2022 /* slave quotactl */
2023 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2028 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2032 if (oqctl->qc_id != id)
2033 swap(oqctl->qc_id, id);
2040 * OBD PING and other handlers.
2042 static int mdt_obd_ping(struct mdt_thread_info *info)
2047 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2049 rc = target_handle_ping(mdt_info_req(info));
2051 rc = err_serious(rc);
2056 * OBD_IDX_READ handler
2058 static int mdt_obd_idx_read(struct mdt_thread_info *info)
2060 struct mdt_device *mdt = info->mti_mdt;
2061 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2062 struct idx_info *req_ii, *rep_ii;
2066 memset(rdpg, 0, sizeof(*rdpg));
2067 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2069 /* extract idx_info buffer from request & reply */
2070 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2071 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2072 RETURN(err_serious(-EPROTO));
2074 rc = req_capsule_server_pack(info->mti_pill);
2076 RETURN(err_serious(rc));
2078 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2080 RETURN(err_serious(-EFAULT));
2081 rep_ii->ii_magic = IDX_INFO_MAGIC;
2083 /* extract hash to start with */
2084 rdpg->rp_hash = req_ii->ii_hash_start;
2086 /* extract requested attributes */
2087 rdpg->rp_attrs = req_ii->ii_attrs;
2089 /* check that fid packed in request is valid and supported */
2090 if (!fid_is_sane(&req_ii->ii_fid))
2092 rep_ii->ii_fid = req_ii->ii_fid;
2095 rep_ii->ii_flags = req_ii->ii_flags;
2097 /* compute number of pages to allocate, ii_count is the number of 4KB
2099 if (req_ii->ii_count <= 0)
2100 GOTO(out, rc = -EFAULT);
2101 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2102 PTLRPC_MAX_BRW_SIZE);
2103 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2105 /* allocate pages to store the containers */
2106 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2107 if (rdpg->rp_pages == NULL)
2108 GOTO(out, rc = -ENOMEM);
2109 for (i = 0; i < rdpg->rp_npages; i++) {
2110 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2111 if (rdpg->rp_pages[i] == NULL)
2112 GOTO(out, rc = -ENOMEM);
2115 /* populate pages with key/record pairs */
2116 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2120 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2121 "asked %d > %d\n", rc, rdpg->rp_count);
2123 /* send pages to client */
2124 rc = mdt_sendpage(info, rdpg, rc);
2128 if (rdpg->rp_pages) {
2129 for (i = 0; i < rdpg->rp_npages; i++)
2130 if (rdpg->rp_pages[i])
2131 cfs_free_page(rdpg->rp_pages[i]);
2132 OBD_FREE(rdpg->rp_pages,
2133 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2138 static int mdt_obd_log_cancel(struct mdt_thread_info *info)
2140 return err_serious(-EOPNOTSUPP);
2143 static int mdt_obd_qc_callback(struct mdt_thread_info *info)
2145 return err_serious(-EOPNOTSUPP);
2153 /** clone llog ctxt from child (mdd)
2154 * This allows remote llog (replicator) access.
2155 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2156 * context was originally set up, or we can handle them directly.
2157 * I choose the latter, but that means I need any llog
2158 * contexts set up by child to be accessable by the mdt. So we clone the
2159 * context into our context list here.
2161 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2164 struct md_device *next = mdt->mdt_child;
2165 struct llog_ctxt *ctxt;
2168 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2171 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2172 if (rc || ctxt == NULL) {
2176 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2178 CERROR("Can't set mdt ctxt %d\n", rc);
2183 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2184 struct mdt_device *mdt, int idx)
2186 struct llog_ctxt *ctxt;
2188 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2191 /* Put once for the get we just did, and once for the clone */
2192 llog_ctxt_put(ctxt);
2193 llog_ctxt_put(ctxt);
2197 static int mdt_llog_create(struct mdt_thread_info *info)
2201 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2202 rc = llog_origin_handle_open(mdt_info_req(info));
2203 return (rc < 0 ? err_serious(rc) : rc);
2206 static int mdt_llog_destroy(struct mdt_thread_info *info)
2210 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2211 rc = llog_origin_handle_destroy(mdt_info_req(info));
2212 return (rc < 0 ? err_serious(rc) : rc);
2215 static int mdt_llog_read_header(struct mdt_thread_info *info)
2219 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2220 rc = llog_origin_handle_read_header(mdt_info_req(info));
2221 return (rc < 0 ? err_serious(rc) : rc);
2224 static int mdt_llog_next_block(struct mdt_thread_info *info)
2228 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2229 rc = llog_origin_handle_next_block(mdt_info_req(info));
2230 return (rc < 0 ? err_serious(rc) : rc);
2233 static int mdt_llog_prev_block(struct mdt_thread_info *info)
2237 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2238 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2239 return (rc < 0 ? err_serious(rc) : rc);
2246 static struct ldlm_callback_suite cbs = {
2247 .lcs_completion = ldlm_server_completion_ast,
2248 .lcs_blocking = ldlm_server_blocking_ast,
2249 .lcs_glimpse = ldlm_server_glimpse_ast
2252 static int mdt_enqueue(struct mdt_thread_info *info)
2254 struct ptlrpc_request *req;
2258 * info->mti_dlm_req already contains swapped and (if necessary)
2259 * converted dlm request.
2261 LASSERT(info->mti_dlm_req != NULL);
2263 req = mdt_info_req(info);
2264 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2265 req, info->mti_dlm_req, &cbs);
2266 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2267 return rc ? err_serious(rc) : req->rq_status;
2270 static int mdt_convert(struct mdt_thread_info *info)
2273 struct ptlrpc_request *req;
2275 LASSERT(info->mti_dlm_req);
2276 req = mdt_info_req(info);
2277 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2278 return rc ? err_serious(rc) : req->rq_status;
2281 static int mdt_bl_callback(struct mdt_thread_info *info)
2283 CERROR("bl callbacks should not happen on MDS\n");
2285 return err_serious(-EOPNOTSUPP);
2288 static int mdt_cp_callback(struct mdt_thread_info *info)
2290 CERROR("cp callbacks should not happen on MDS\n");
2292 return err_serious(-EOPNOTSUPP);
2296 * sec context handlers
2298 static int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2302 rc = mdt_handle_idmap(info);
2305 struct ptlrpc_request *req = mdt_info_req(info);
2308 opc = lustre_msg_get_opc(req->rq_reqmsg);
2309 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2310 sptlrpc_svc_ctx_invalidate(req);
2313 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2319 * quota request handlers
2321 static int mdt_quota_dqacq(struct mdt_thread_info *info)
2323 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2328 RETURN(err_serious(-EOPNOTSUPP));
2330 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2334 static struct mdt_object *mdt_obj(struct lu_object *o)
2336 LASSERT(lu_device_is_mdt(o->lo_dev));
2337 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2340 struct mdt_object *mdt_object_new(const struct lu_env *env,
2341 struct mdt_device *d,
2342 const struct lu_fid *f)
2344 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2345 struct lu_object *o;
2346 struct mdt_object *m;
2349 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2350 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2351 if (unlikely(IS_ERR(o)))
2352 m = (struct mdt_object *)o;
2358 struct mdt_object *mdt_object_find(const struct lu_env *env,
2359 struct mdt_device *d,
2360 const struct lu_fid *f)
2362 struct lu_object *o;
2363 struct mdt_object *m;
2366 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2367 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2368 if (unlikely(IS_ERR(o)))
2369 m = (struct mdt_object *)o;
2376 * Asyncronous commit for mdt device.
2378 * Pass asynchonous commit call down the MDS stack.
2380 * \param env environment
2381 * \param mdt the mdt device
2383 static void mdt_device_commit_async(const struct lu_env *env,
2384 struct mdt_device *mdt)
2386 struct dt_device *dt = mdt->mdt_bottom;
2389 rc = dt->dd_ops->dt_commit_async(env, dt);
2390 if (unlikely(rc != 0))
2391 CWARN("async commit start failed with rc = %d", rc);
2395 * Mark the lock as "synchonous".
2397 * Mark the lock to deffer transaction commit to the unlock time.
2399 * \param lock the lock to mark as "synchonous"
2401 * \see mdt_is_lock_sync
2402 * \see mdt_save_lock
2404 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2406 lock->l_ast_data = (void*)1;
2410 * Check whehter the lock "synchonous" or not.
2412 * \param lock the lock to check
2413 * \retval 1 the lock is "synchonous"
2414 * \retval 0 the lock isn't "synchronous"
2416 * \see mdt_set_lock_sync
2417 * \see mdt_save_lock
2419 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2421 return lock->l_ast_data != NULL;
2425 * Blocking AST for mdt locks.
2427 * Starts transaction commit if in case of COS lock conflict or
2428 * deffers such a commit to the mdt_save_lock.
2430 * \param lock the lock which blocks a request or cancelling lock
2431 * \param desc unused
2432 * \param data unused
2433 * \param flag indicates whether this cancelling or blocking callback
2435 * \see ldlm_blocking_ast_nocheck
2437 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2438 void *data, int flag)
2440 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2441 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2445 if (flag == LDLM_CB_CANCELING)
2447 lock_res_and_lock(lock);
2448 if (lock->l_blocking_ast != mdt_blocking_ast) {
2449 unlock_res_and_lock(lock);
2452 if (mdt_cos_is_enabled(mdt) &&
2453 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2454 lock->l_blocking_lock != NULL &&
2455 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2456 mdt_set_lock_sync(lock);
2458 rc = ldlm_blocking_ast_nocheck(lock);
2460 /* There is no lock conflict if l_blocking_lock == NULL,
2461 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2462 * when the last reference to a local lock was released */
2463 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2466 rc = lu_env_init(&env, LCT_LOCAL);
2467 if (unlikely(rc != 0))
2468 CWARN("lu_env initialization failed with rc = %d,"
2469 "cannot start asynchronous commit\n", rc);
2471 mdt_device_commit_async(&env, mdt);
2477 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2478 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2480 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2481 ldlm_policy_data_t *policy = &info->mti_policy;
2482 struct ldlm_res_id *res_id = &info->mti_res_id;
2486 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2487 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2488 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2489 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2491 if (mdt_object_exists(o) < 0) {
2492 if (locality == MDT_CROSS_LOCK) {
2493 /* cross-ref object fix */
2494 ibits &= ~MDS_INODELOCK_UPDATE;
2495 ibits |= MDS_INODELOCK_LOOKUP;
2497 LASSERT(!(ibits & MDS_INODELOCK_UPDATE));
2498 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2500 /* No PDO lock on remote object */
2501 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2504 if (lh->mlh_type == MDT_PDO_LOCK) {
2505 /* check for exists after object is locked */
2506 if (mdt_object_exists(o) == 0) {
2507 /* Non-existent object shouldn't have PDO lock */
2510 /* Non-dir object shouldn't have PDO lock */
2511 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2516 memset(policy, 0, sizeof(*policy));
2517 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2520 * Take PDO lock on whole directory and build correct @res_id for lock
2521 * on part of directory.
2523 if (lh->mlh_pdo_hash != 0) {
2524 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2525 mdt_lock_pdo_mode(info, o, lh);
2526 if (lh->mlh_pdo_mode != LCK_NL) {
2528 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2529 * is never going to be sent to client and we do not
2530 * want it slowed down due to possible cancels.
2532 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2533 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2534 policy, res_id, LDLM_FL_ATOMIC_CB,
2535 &info->mti_exp->exp_handle.h_cookie);
2541 * Finish res_id initializing by name hash marking part of
2542 * directory which is taking modification.
2544 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2547 policy->l_inodebits.bits = ibits;
2550 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2551 * going to be sent to client. If it is - mdt_intent_policy() path will
2552 * fix it up and turn FL_LOCAL flag off.
2554 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2555 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
2556 &info->mti_exp->exp_handle.h_cookie);
2558 mdt_object_unlock(info, o, lh, 1);
2559 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2560 lh->mlh_pdo_hash != 0 &&
2561 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2562 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2569 * Save a lock within request object.
2571 * Keep the lock referenced until whether client ACK or transaction
2572 * commit happens or release the lock immediately depending on input
2573 * parameters. If COS is ON, a write lock is converted to COS lock
2576 * \param info thead info object
2577 * \param h lock handle
2578 * \param mode lock mode
2579 * \param decref force immediate lock releasing
2582 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2583 ldlm_mode_t mode, int decref)
2587 if (lustre_handle_is_used(h)) {
2588 if (decref || !info->mti_has_trans ||
2589 !(mode & (LCK_PW | LCK_EX))){
2590 mdt_fid_unlock(h, mode);
2592 struct mdt_device *mdt = info->mti_mdt;
2593 struct ldlm_lock *lock = ldlm_handle2lock(h);
2594 struct ptlrpc_request *req = mdt_info_req(info);
2597 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2599 CDEBUG(D_HA, "request = %p reply state = %p"
2600 " transno = "LPD64"\n",
2601 req, req->rq_reply_state, req->rq_transno);
2602 if (mdt_cos_is_enabled(mdt)) {
2604 ldlm_lock_downgrade(lock, LCK_COS);
2607 ptlrpc_save_lock(req, h, mode, no_ack);
2608 if (mdt_is_lock_sync(lock)) {
2609 CDEBUG(D_HA, "found sync-lock,"
2610 " async commit started\n");
2611 mdt_device_commit_async(info->mti_env,
2614 LDLM_LOCK_PUT(lock);
2623 * Unlock mdt object.
2625 * Immeditely release the regular lock and the PDO lock or save the
2626 * lock in reqeuest and keep them referenced until client ACK or
2627 * transaction commit.
2629 * \param info thread info object
2630 * \param o mdt object
2631 * \param lh mdt lock handle referencing regular and PDO locks
2632 * \param decref force immediate lock releasing
2634 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2635 struct mdt_lock_handle *lh, int decref)
2639 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2640 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2645 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2646 const struct lu_fid *f,
2647 struct mdt_lock_handle *lh,
2650 struct mdt_object *o;
2652 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2656 rc = mdt_object_lock(info, o, lh, ibits,
2659 mdt_object_put(info->mti_env, o);
2666 void mdt_object_unlock_put(struct mdt_thread_info * info,
2667 struct mdt_object * o,
2668 struct mdt_lock_handle *lh,
2671 mdt_object_unlock(info, o, lh, decref);
2672 mdt_object_put(info->mti_env, o);
2675 static struct mdt_handler *mdt_handler_find(__u32 opc,
2676 struct mdt_opc_slice *supported)
2678 struct mdt_opc_slice *s;
2679 struct mdt_handler *h;
2682 for (s = supported; s->mos_hs != NULL; s++) {
2683 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2684 h = s->mos_hs + (opc - s->mos_opc_start);
2685 if (likely(h->mh_opc != 0))
2686 LASSERTF(h->mh_opc == opc,
2687 "opcode mismatch %d != %d\n",
2690 h = NULL; /* unsupported opc */
2697 static int mdt_lock_resname_compat(struct mdt_device *m,
2698 struct ldlm_request *req)
2700 /* XXX something... later. */
2704 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2706 /* XXX something... later. */
2711 * Generic code handling requests that have struct mdt_body passed in:
2713 * - extract mdt_body from request and save it in @info, if present;
2715 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2718 * - if HABEO_CORPUS flag is set for this request type check whether object
2719 * actually exists on storage (lu_object_exists()).
2722 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2724 const struct mdt_body *body;
2725 struct mdt_object *obj;
2726 const struct lu_env *env;
2727 struct req_capsule *pill;
2731 env = info->mti_env;
2732 pill = info->mti_pill;
2734 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2738 if (!(body->valid & OBD_MD_FLID))
2741 if (!fid_is_sane(&body->fid1)) {
2742 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2747 * Do not get size or any capa fields before we check that request
2748 * contains capa actually. There are some requests which do not, for
2749 * instance MDS_IS_SUBDIR.
2751 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2752 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2753 mdt_set_capainfo(info, 0, &body->fid1,
2754 req_capsule_client_get(pill, &RMF_CAPA1));
2756 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2758 if ((flags & HABEO_CORPUS) &&
2759 !mdt_object_exists(obj)) {
2760 mdt_object_put(env, obj);
2761 /* for capability renew ENOENT will be handled in
2763 if (body->valid & OBD_MD_FLOSSCAPA)
2768 info->mti_object = obj;
2777 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2779 struct req_capsule *pill = info->mti_pill;
2783 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2784 rc = mdt_body_unpack(info, flags);
2788 if (rc == 0 && (flags & HABEO_REFERO)) {
2790 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2791 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2792 info->mti_body->eadatasize);
2793 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2794 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2797 rc = req_capsule_server_pack(pill);
2802 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2804 struct md_device *next = m->mdt_child;
2806 return next->md_ops->mdo_init_capa_ctxt(env, next,
2807 m->mdt_opts.mo_mds_capa,
2808 m->mdt_capa_timeout,
2814 * Invoke handler for this request opc. Also do necessary preprocessing
2815 * (according to handler ->mh_flags), and post-processing (setting of
2816 * ->last_{xid,committed}).
2818 static int mdt_req_handle(struct mdt_thread_info *info,
2819 struct mdt_handler *h, struct ptlrpc_request *req)
2821 int rc, serious = 0;
2826 LASSERT(h->mh_act != NULL);
2827 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2828 LASSERT(current->journal_info == NULL);
2831 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2832 * to put same checks into handlers like mdt_close(), mdt_reint(),
2833 * etc., without talking to mdt authors first. Checking same thing
2834 * there again is useless and returning 0 error without packing reply
2835 * is buggy! Handlers either pack reply or return error.
2837 * We return 0 here and do not send any reply in order to emulate
2838 * network failure. Do not send any reply in case any of NET related
2839 * fail_id has occured.
2841 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2845 flags = h->mh_flags;
2846 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2848 if (h->mh_fmt != NULL) {
2849 req_capsule_set(info->mti_pill, h->mh_fmt);
2850 rc = mdt_unpack_req_pack_rep(info, flags);
2853 if (rc == 0 && flags & MUTABOR &&
2854 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
2855 /* should it be rq_status? */
2858 if (rc == 0 && flags & HABEO_CLAVIS) {
2859 struct ldlm_request *dlm_req;
2861 LASSERT(h->mh_fmt != NULL);
2863 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2864 if (dlm_req != NULL) {
2865 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2867 dlm_req->lock_desc.l_policy_data.\
2868 l_inodebits.bits == 0)) {
2870 * Lock without inodebits makes no sense and
2871 * will oops later in ldlm. If client miss to
2872 * set such bits, do not trigger ASSERTION.
2874 * For liblustre flock case, it maybe zero.
2878 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2879 rc = mdt_lock_resname_compat(
2882 info->mti_dlm_req = dlm_req;
2889 /* capability setting changed via /proc, needs reinitialize ctxt */
2890 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2891 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2892 info->mti_mdt->mdt_capa_conf = 0;
2895 if (likely(rc == 0)) {
2897 * Process request, there can be two types of rc:
2898 * 1) errors with msg unpack/pack, other failures outside the
2899 * operation itself. This is counted as serious errors;
2900 * 2) errors during fs operation, should be placed in rq_status
2903 rc = h->mh_act(info);
2905 !req->rq_no_reply && req->rq_reply_state == NULL) {
2906 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2907 "pack reply and returned 0 error\n",
2911 serious = is_serious(rc);
2912 rc = clear_serious(rc);
2916 req->rq_status = rc;
2919 * ELDLM_* codes which > 0 should be in rq_status only as well as
2920 * all non-serious errors.
2922 if (rc > 0 || !serious)
2925 LASSERT(current->journal_info == NULL);
2927 if (rc == 0 && (flags & HABEO_CLAVIS) &&
2928 info->mti_mdt->mdt_opts.mo_compat_resname) {
2929 struct ldlm_reply *dlmrep;
2931 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
2933 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
2936 /* If we're DISCONNECTing, the mdt_export_data is already freed */
2937 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
2938 target_committed_to_req(req);
2940 if (unlikely(req_is_replay(req) &&
2941 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
2942 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
2946 target_send_reply(req, rc, info->mti_fail_id);
2950 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2952 lh->mlh_type = MDT_NUL_LOCK;
2953 lh->mlh_reg_lh.cookie = 0ull;
2954 lh->mlh_reg_mode = LCK_MINMODE;
2955 lh->mlh_pdo_lh.cookie = 0ull;
2956 lh->mlh_pdo_mode = LCK_MINMODE;
2959 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2961 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2962 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2966 * Initialize fields of struct mdt_thread_info. Other fields are left in
2967 * uninitialized state, because it's too expensive to zero out whole
2968 * mdt_thread_info (> 1K) on each request arrival.
2970 static void mdt_thread_info_init(struct ptlrpc_request *req,
2971 struct mdt_thread_info *info)
2974 struct md_capainfo *ci;
2976 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2977 info->mti_pill = &req->rq_pill;
2980 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2981 mdt_lock_handle_init(&info->mti_lh[i]);
2983 /* mdt device: it can be NULL while CONNECT */
2984 if (req->rq_export) {
2985 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2986 info->mti_exp = req->rq_export;
2988 info->mti_mdt = NULL;
2989 info->mti_env = req->rq_svc_thread->t_env;
2990 ci = md_capainfo(info->mti_env);
2991 memset(ci, 0, sizeof *ci);
2992 if (req->rq_export) {
2993 if (exp_connect_rmtclient(req->rq_export))
2994 ci->mc_auth = LC_ID_CONVERT;
2995 else if (req->rq_export->exp_connect_flags &
2996 OBD_CONNECT_MDS_CAPA)
2997 ci->mc_auth = LC_ID_PLAIN;
2999 ci->mc_auth = LC_ID_NONE;
3002 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3003 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3004 info->mti_mos = NULL;
3006 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3007 info->mti_body = NULL;
3008 info->mti_object = NULL;
3009 info->mti_dlm_req = NULL;
3010 info->mti_has_trans = 0;
3011 info->mti_cross_ref = 0;
3012 info->mti_opdata = 0;
3013 info->mti_big_lmm_used = 0;
3015 /* To not check for split by default. */
3016 info->mti_spec.no_create = 0;
3019 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3023 req_capsule_fini(info->mti_pill);
3024 if (info->mti_object != NULL) {
3025 mdt_object_put(info->mti_env, info->mti_object);
3026 info->mti_object = NULL;
3028 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3029 mdt_lock_handle_fini(&info->mti_lh[i]);
3030 info->mti_env = NULL;
3033 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3034 struct obd_device *obd, int *process)
3036 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3037 case MDS_CONNECT: /* This will never get here, but for completeness. */
3038 case OST_CONNECT: /* This will never get here, but for completeness. */
3039 case MDS_DISCONNECT:
3040 case OST_DISCONNECT:
3046 case MDS_DONE_WRITING:
3047 case MDS_SYNC: /* used in unmounting */
3053 *process = target_queue_recovery_request(req, obd);
3057 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3064 * Handle recovery. Return:
3065 * +1: continue request processing;
3066 * -ve: abort immediately with the given error code;
3067 * 0: send reply with error code in req->rq_status;
3069 static int mdt_recovery(struct mdt_thread_info *info)
3071 struct ptlrpc_request *req = mdt_info_req(info);
3072 struct obd_device *obd;
3076 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3079 case SEC_CTX_INIT_CONT:
3085 rc = mdt_handle_idmap(info);
3094 if (unlikely(!class_connected_export(req->rq_export))) {
3095 CERROR("operation %d on unconnected MDS from %s\n",
3096 lustre_msg_get_opc(req->rq_reqmsg),
3097 libcfs_id2str(req->rq_peer));
3098 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3099 * mds_A will get -ENOTCONN(especially for ping req),
3100 * which will cause that mds_A deactive timeout, then when
3101 * mds_A cleanup, the cleanup process will be suspended since
3102 * deactive timeout is not zero.
3104 req->rq_status = -ENOTCONN;
3105 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3109 /* sanity check: if the xid matches, the request must be marked as a
3110 * resent or replayed */
3111 if (req_xid_is_last(req)) {
3112 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3113 (MSG_RESENT | MSG_REPLAY))) {
3114 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3115 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3116 lustre_msg_get_flags(req->rq_reqmsg));
3118 req->rq_status = -ENOTCONN;
3123 /* else: note the opposite is not always true; a RESENT req after a
3124 * failover will usually not match the last_xid, since it was likely
3125 * never committed. A REPLAYed request will almost never match the
3126 * last xid, however it could for a committed, but still retained,
3129 obd = req->rq_export->exp_obd;
3131 /* Check for aborted recovery... */
3132 if (unlikely(obd->obd_recovering)) {
3135 DEBUG_REQ(D_INFO, req, "Got new replay");
3136 rc = mdt_filter_recovery_request(req, obd, &should_process);
3137 if (rc != 0 || !should_process)
3139 else if (should_process < 0) {
3140 req->rq_status = should_process;
3141 rc = ptlrpc_error(req);
3148 static int mdt_msg_check_version(struct lustre_msg *msg)
3152 switch (lustre_msg_get_opc(msg)) {
3154 case MDS_DISCONNECT:
3157 case SEC_CTX_INIT_CONT:
3160 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3162 CERROR("bad opc %u version %08x, expecting %08x\n",
3163 lustre_msg_get_opc(msg),
3164 lustre_msg_get_version(msg),
3165 LUSTRE_OBD_VERSION);
3169 case MDS_GETATTR_NAME:
3176 case MDS_DONE_WRITING:
3183 case MDS_QUOTACHECK:
3189 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3191 CERROR("bad opc %u version %08x, expecting %08x\n",
3192 lustre_msg_get_opc(msg),
3193 lustre_msg_get_version(msg),
3194 LUSTRE_MDS_VERSION);
3198 case LDLM_BL_CALLBACK:
3199 case LDLM_CP_CALLBACK:
3200 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3202 CERROR("bad opc %u version %08x, expecting %08x\n",
3203 lustre_msg_get_opc(msg),
3204 lustre_msg_get_version(msg),
3205 LUSTRE_DLM_VERSION);
3207 case OBD_LOG_CANCEL:
3208 case LLOG_ORIGIN_HANDLE_CREATE:
3209 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3210 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3211 case LLOG_ORIGIN_HANDLE_CLOSE:
3212 case LLOG_ORIGIN_HANDLE_DESTROY:
3213 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3215 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3217 CERROR("bad opc %u version %08x, expecting %08x\n",
3218 lustre_msg_get_opc(msg),
3219 lustre_msg_get_version(msg),
3220 LUSTRE_LOG_VERSION);
3223 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3229 static int mdt_handle0(struct ptlrpc_request *req,
3230 struct mdt_thread_info *info,
3231 struct mdt_opc_slice *supported)
3233 struct mdt_handler *h;
3234 struct lustre_msg *msg;
3239 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3242 LASSERT(current->journal_info == NULL);
3244 msg = req->rq_reqmsg;
3245 rc = mdt_msg_check_version(msg);
3246 if (likely(rc == 0)) {
3247 rc = mdt_recovery(info);
3248 if (likely(rc == +1)) {
3249 h = mdt_handler_find(lustre_msg_get_opc(msg),
3251 if (likely(h != NULL)) {
3252 rc = mdt_req_handle(info, h, req);
3254 CERROR("The unsupported opc: 0x%x\n",
3255 lustre_msg_get_opc(msg) );
3256 req->rq_status = -ENOTSUPP;
3257 rc = ptlrpc_error(req);
3262 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3267 * MDT handler function called by ptlrpc service thread when request comes.
3269 * XXX common "target" functionality should be factored into separate module
3270 * shared by mdt, ost and stand-alone services like fld.
3272 static int mdt_handle_common(struct ptlrpc_request *req,
3273 struct mdt_opc_slice *supported)
3276 struct mdt_thread_info *info;
3280 env = req->rq_svc_thread->t_env;
3281 LASSERT(env != NULL);
3282 LASSERT(env->le_ses != NULL);
3283 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3284 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3285 LASSERT(info != NULL);
3287 mdt_thread_info_init(req, info);
3289 rc = mdt_handle0(req, info, supported);
3291 mdt_thread_info_fini(info);
3296 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3299 int mdt_recovery_handle(struct ptlrpc_request *req)
3304 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3306 rc = mdt_handle_common(req, mdt_fld_handlers);
3309 rc = mdt_handle_common(req, mdt_seq_handlers);
3312 rc = mdt_handle_common(req, mdt_regular_handlers);
3319 static int mdt_regular_handle(struct ptlrpc_request *req)
3321 return mdt_handle_common(req, mdt_regular_handlers);
3324 static int mdt_readpage_handle(struct ptlrpc_request *req)
3326 return mdt_handle_common(req, mdt_readpage_handlers);
3329 static int mdt_xmds_handle(struct ptlrpc_request *req)
3331 return mdt_handle_common(req, mdt_xmds_handlers);
3334 static int mdt_mdsc_handle(struct ptlrpc_request *req)
3336 return mdt_handle_common(req, mdt_seq_handlers);
3339 static int mdt_mdss_handle(struct ptlrpc_request *req)
3341 return mdt_handle_common(req, mdt_seq_handlers);
3344 static int mdt_dtss_handle(struct ptlrpc_request *req)
3346 return mdt_handle_common(req, mdt_seq_handlers);
3349 static int mdt_fld_handle(struct ptlrpc_request *req)
3351 return mdt_handle_common(req, mdt_fld_handlers);
3369 static int mdt_intent_getattr(enum mdt_it_code opcode,
3370 struct mdt_thread_info *info,
3371 struct ldlm_lock **,
3373 static int mdt_intent_reint(enum mdt_it_code opcode,
3374 struct mdt_thread_info *info,
3375 struct ldlm_lock **,
3378 static struct mdt_it_flavor {
3379 const struct req_format *it_fmt;
3381 int (*it_act)(enum mdt_it_code ,
3382 struct mdt_thread_info *,
3383 struct ldlm_lock **,
3386 } mdt_it_flavor[] = {
3388 .it_fmt = &RQF_LDLM_INTENT,
3389 /*.it_flags = HABEO_REFERO,*/
3391 .it_act = mdt_intent_reint,
3392 .it_reint = REINT_OPEN
3395 .it_fmt = &RQF_LDLM_INTENT,
3396 .it_flags = MUTABOR,
3397 .it_act = mdt_intent_reint,
3398 .it_reint = REINT_OPEN
3401 .it_fmt = &RQF_LDLM_INTENT,
3402 .it_flags = MUTABOR,
3403 .it_act = mdt_intent_reint,
3404 .it_reint = REINT_CREATE
3406 [MDT_IT_GETATTR] = {
3407 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3408 .it_flags = HABEO_REFERO,
3409 .it_act = mdt_intent_getattr
3411 [MDT_IT_READDIR] = {
3417 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3418 .it_flags = HABEO_REFERO,
3419 .it_act = mdt_intent_getattr
3422 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3423 .it_flags = MUTABOR,
3425 .it_reint = REINT_UNLINK
3429 .it_flags = MUTABOR,
3432 [MDT_IT_GETXATTR] = {
3438 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3439 .it_flags = HABEO_REFERO,
3440 .it_act = mdt_intent_getattr
3444 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3445 struct ldlm_lock **lockp,
3446 struct ldlm_lock *new_lock,
3447 struct mdt_lock_handle *lh,
3450 struct ptlrpc_request *req = mdt_info_req(info);
3451 struct ldlm_lock *lock = *lockp;
3454 * Get new lock only for cases when possible resent did not find any
3457 if (new_lock == NULL)
3458 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3460 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3461 lh->mlh_reg_lh.cookie = 0;
3465 LASSERTF(new_lock != NULL,
3466 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3469 * If we've already given this lock to a client once, then we should
3470 * have no readers or writers. Otherwise, we should have one reader
3471 * _or_ writer ref (which will be zeroed below) before returning the
3474 if (new_lock->l_export == req->rq_export) {
3475 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3477 LASSERT(new_lock->l_export == NULL);
3478 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3483 if (new_lock->l_export == req->rq_export) {
3485 * Already gave this to the client, which means that we
3486 * reconstructed a reply.
3488 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3490 lh->mlh_reg_lh.cookie = 0;
3491 RETURN(ELDLM_LOCK_REPLACED);
3495 * Fixup the lock to be given to the client.
3497 lock_res_and_lock(new_lock);
3498 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3499 * possible blocking AST. */
3500 while (new_lock->l_readers > 0) {
3501 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3502 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3503 new_lock->l_readers--;
3505 while (new_lock->l_writers > 0) {
3506 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3507 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3508 new_lock->l_writers--;
3511 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3512 new_lock->l_blocking_ast = lock->l_blocking_ast;
3513 new_lock->l_completion_ast = lock->l_completion_ast;
3514 new_lock->l_remote_handle = lock->l_remote_handle;
3515 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3517 unlock_res_and_lock(new_lock);
3519 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3520 &new_lock->l_remote_handle,
3521 &new_lock->l_exp_hash);
3523 LDLM_LOCK_RELEASE(new_lock);
3524 lh->mlh_reg_lh.cookie = 0;
3526 RETURN(ELDLM_LOCK_REPLACED);
3529 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3530 struct ldlm_lock *new_lock,
3531 struct ldlm_lock **old_lock,
3532 struct mdt_lock_handle *lh)
3534 struct ptlrpc_request *req = mdt_info_req(info);
3535 struct obd_export *exp = req->rq_export;
3536 struct lustre_handle remote_hdl;
3537 struct ldlm_request *dlmreq;
3538 struct ldlm_lock *lock;
3540 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3543 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3544 remote_hdl = dlmreq->lock_handle[0];
3546 /* In the function below, .hs_keycmp resolves to
3547 * ldlm_export_lock_keycmp() */
3548 /* coverity[overrun-buffer-val] */
3549 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3551 if (lock != new_lock) {
3552 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3553 lh->mlh_reg_mode = lock->l_granted_mode;
3555 LDLM_DEBUG(lock, "Restoring lock cookie");
3556 DEBUG_REQ(D_DLMTRACE, req,
3557 "restoring lock cookie "LPX64,
3558 lh->mlh_reg_lh.cookie);
3560 *old_lock = LDLM_LOCK_GET(lock);
3561 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3565 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3569 * If the xid matches, then we know this is a resent request, and allow
3570 * it. (It's probably an OPEN, for which we don't send a lock.
3572 if (req_xid_is_last(req))
3576 * This remote handle isn't enqueued, so we never received or processed
3577 * this request. Clear MSG_RESENT, because it can be handled like any
3578 * normal request now.
3580 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3582 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3586 static int mdt_intent_getattr(enum mdt_it_code opcode,
3587 struct mdt_thread_info *info,
3588 struct ldlm_lock **lockp,
3591 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3592 struct ldlm_lock *new_lock = NULL;
3594 struct ldlm_reply *ldlm_rep;
3595 struct ptlrpc_request *req;
3596 struct mdt_body *reqbody;
3597 struct mdt_body *repbody;
3601 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3604 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3607 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3608 repbody->eadatasize = 0;
3609 repbody->aclsize = 0;
3613 child_bits = MDS_INODELOCK_LOOKUP;
3615 case MDT_IT_GETATTR:
3616 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
3618 case MDT_IT_LAYOUT: {
3619 static int printed = 0;
3622 CERROR("layout lock not supported by this version\n");
3625 GOTO(out_shrink, rc = -EINVAL);
3629 CERROR("Unsupported intent (%d)\n", opcode);
3630 GOTO(out_shrink, rc = -EINVAL);
3633 rc = mdt_init_ucred(info, reqbody);
3635 GOTO(out_shrink, rc);
3637 req = info->mti_pill->rc_req;
3638 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3639 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3641 /* Get lock from request for possible resent case. */
3642 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3644 ldlm_rep->lock_policy_res2 =
3645 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3647 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3648 ldlm_rep->lock_policy_res2 = 0;
3649 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3650 ldlm_rep->lock_policy_res2) {
3651 lhc->mlh_reg_lh.cookie = 0ull;
3652 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3655 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3658 mdt_exit_ucred(info);
3660 mdt_client_compatibility(info);
3661 rc2 = mdt_fix_reply(info);
3667 static int mdt_intent_reint(enum mdt_it_code opcode,
3668 struct mdt_thread_info *info,
3669 struct ldlm_lock **lockp,
3672 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3673 struct ldlm_reply *rep = NULL;
3677 static const struct req_format *intent_fmts[REINT_MAX] = {
3678 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3679 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3684 opc = mdt_reint_opcode(info, intent_fmts);
3688 if (mdt_it_flavor[opcode].it_reint != opc) {
3689 CERROR("Reint code %ld doesn't match intent: %d\n",
3691 RETURN(err_serious(-EPROTO));
3694 /* Get lock from request for possible resent case. */
3695 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3697 rc = mdt_reint_internal(info, lhc, opc);
3699 /* Check whether the reply has been packed successfully. */
3700 if (mdt_info_req(info)->rq_repmsg != NULL)
3701 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3703 RETURN(err_serious(-EFAULT));
3705 /* MDC expects this in any case */
3707 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3709 /* Cross-ref case, the lock should be returned to the client */
3710 if (rc == -EREMOTE) {
3711 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3712 rep->lock_policy_res2 = 0;
3713 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3716 rep->lock_policy_res2 = clear_serious(rc);
3718 if (rep->lock_policy_res2 == -ENOENT &&
3719 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3720 rep->lock_policy_res2 = 0;
3722 if (rc == -ENOTCONN || rc == -ENODEV ||
3723 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3725 * If it is the disconnect error (ENODEV & ENOCONN), the error
3726 * will be returned by rq_status, and client at ptlrpc layer
3727 * will detect this, then disconnect, reconnect the import
3728 * immediately, instead of impacting the following the rpc.
3730 lhc->mlh_reg_lh.cookie = 0ull;
3734 * For other cases, the error will be returned by intent.
3735 * and client will retrieve the result from intent.
3738 * FIXME: when open lock is finished, that should be
3741 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3742 LASSERTF(rc == 0, "Error occurred but lock handle "
3743 "is still in use\n");
3744 rep->lock_policy_res2 = 0;
3745 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3748 lhc->mlh_reg_lh.cookie = 0ull;
3749 RETURN(ELDLM_LOCK_ABORTED);
3754 static int mdt_intent_code(long itcode)
3762 case IT_OPEN|IT_CREAT:
3769 rc = MDT_IT_READDIR;
3772 rc = MDT_IT_GETATTR;
3784 rc = MDT_IT_GETXATTR;
3789 case IT_QUOTA_DQACQ:
3794 CERROR("Unknown intent opcode: %ld\n", itcode);
3801 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3802 struct ldlm_lock **lockp, __u64 flags)
3804 struct req_capsule *pill;
3805 struct mdt_it_flavor *flv;
3810 opc = mdt_intent_code(itopc);
3814 pill = info->mti_pill;
3816 if (opc == MDT_IT_QUOTA) {
3817 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3820 RETURN(-EOPNOTSUPP);
3822 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3823 /* pass the request to quota master */
3824 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3825 mdt_info_req(info), lockp,
3830 if (opc == MDT_IT_LAYOUT) {
3831 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3832 /* XXX: set replay RMF_DLM_LVB as the real EA size when LAYOUT
3834 } else if (opc == MDT_IT_READDIR) {
3835 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3838 flv = &mdt_it_flavor[opc];
3839 if (flv->it_fmt != NULL)
3840 req_capsule_extend(pill, flv->it_fmt);
3842 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3844 struct ptlrpc_request *req = mdt_info_req(info);
3845 if (flv->it_flags & MUTABOR &&
3846 req->rq_export->exp_connect_flags & OBD_CONNECT_RDONLY)
3849 if (rc == 0 && flv->it_act != NULL) {
3850 /* execute policy */
3851 rc = flv->it_act(opc, info, lockp, flags);
3858 static int mdt_intent_policy(struct ldlm_namespace *ns,
3859 struct ldlm_lock **lockp, void *req_cookie,
3860 ldlm_mode_t mode, __u64 flags, void *data)
3862 struct mdt_thread_info *info;
3863 struct ptlrpc_request *req = req_cookie;
3864 struct ldlm_intent *it;
3865 struct req_capsule *pill;
3870 LASSERT(req != NULL);
3872 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3874 LASSERT(info != NULL);
3875 pill = info->mti_pill;
3876 LASSERT(pill->rc_req == req);
3878 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3879 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3880 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3882 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3886 /* Lock without inodebits makes no sense and will oops
3887 * later in ldlm. Let's check it now to see if we have
3888 * ibits corrupted somewhere in mdt_intent_opc().
3889 * The case for client miss to set ibits has been
3890 * processed by others. */
3891 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3892 lr_type == LDLM_IBITS,
3893 info->mti_dlm_req->lock_desc.\
3894 l_policy_data.l_inodebits.bits != 0));
3896 rc = err_serious(-EFAULT);
3898 /* No intent was provided */
3899 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3900 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3901 rc = req_capsule_server_pack(pill);
3903 rc = err_serious(rc);
3908 static int mdt_seq_fini(const struct lu_env *env,
3909 struct mdt_device *m)
3911 struct md_site *ms = mdt_md_site(m);
3915 if (ms->ms_server_seq) {
3916 seq_server_fini(ms->ms_server_seq, env);
3917 OBD_FREE_PTR(ms->ms_server_seq);
3918 ms->ms_server_seq = NULL;
3921 if (ms->ms_control_seq) {
3922 seq_server_fini(ms->ms_control_seq, env);
3923 OBD_FREE_PTR(ms->ms_control_seq);
3924 ms->ms_control_seq = NULL;
3927 if (ms->ms_client_seq) {
3928 seq_client_fini(ms->ms_client_seq);
3929 OBD_FREE_PTR(ms->ms_client_seq);
3930 ms->ms_client_seq = NULL;
3937 static int mdt_seq_init(const struct lu_env *env,
3939 struct mdt_device *m)
3946 ms = mdt_md_site(m);
3949 * This is sequence-controller node. Init seq-controller server on local
3952 if (ms->ms_node_id == 0) {
3953 LASSERT(ms->ms_control_seq == NULL);
3955 OBD_ALLOC_PTR(ms->ms_control_seq);
3956 if (ms->ms_control_seq == NULL)
3959 rc = seq_server_init(ms->ms_control_seq,
3960 m->mdt_bottom, uuid,
3961 LUSTRE_SEQ_CONTROLLER,
3966 GOTO(out_seq_fini, rc);
3968 OBD_ALLOC_PTR(ms->ms_client_seq);
3969 if (ms->ms_client_seq == NULL)
3970 GOTO(out_seq_fini, rc = -ENOMEM);
3972 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3973 if (prefix == NULL) {
3974 OBD_FREE_PTR(ms->ms_client_seq);
3975 GOTO(out_seq_fini, rc = -ENOMEM);
3978 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
3982 * Init seq-controller client after seq-controller server is
3983 * ready. Pass ms->ms_control_seq to it for direct talking.
3985 rc = seq_client_init(ms->ms_client_seq, NULL,
3986 LUSTRE_SEQ_METADATA, prefix,
3987 ms->ms_control_seq);
3988 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3991 GOTO(out_seq_fini, rc);
3994 /* Init seq-server on local MDT */
3995 LASSERT(ms->ms_server_seq == NULL);
3997 OBD_ALLOC_PTR(ms->ms_server_seq);
3998 if (ms->ms_server_seq == NULL)
3999 GOTO(out_seq_fini, rc = -ENOMEM);
4001 rc = seq_server_init(ms->ms_server_seq,
4002 m->mdt_bottom, uuid,
4007 GOTO(out_seq_fini, rc = -ENOMEM);
4009 /* Assign seq-controller client to local seq-server. */
4010 if (ms->ms_node_id == 0) {
4011 LASSERT(ms->ms_client_seq != NULL);
4013 rc = seq_server_set_cli(ms->ms_server_seq,
4021 mdt_seq_fini(env, m);
4026 * Init client sequence manager which is used by local MDS to talk to sequence
4027 * controller on remote node.
4029 static int mdt_seq_init_cli(const struct lu_env *env,
4030 struct mdt_device *m,
4031 struct lustre_cfg *cfg)
4033 struct md_site *ms = mdt_md_site(m);
4034 struct obd_device *mdc;
4035 struct obd_uuid *uuidp, *mdcuuidp;
4036 char *uuid_str, *mdc_uuid_str;
4039 struct mdt_thread_info *info;
4040 char *p, *index_string = lustre_cfg_string(cfg, 2);
4043 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4044 uuidp = &info->mti_u.uuid[0];
4045 mdcuuidp = &info->mti_u.uuid[1];
4047 LASSERT(index_string);
4049 index = simple_strtol(index_string, &p, 10);
4051 CERROR("Invalid index in lustre_cgf, offset 2\n");
4055 /* check if this is adding the first MDC and controller is not yet
4057 if (index != 0 || ms->ms_client_seq)
4060 uuid_str = lustre_cfg_string(cfg, 1);
4061 mdc_uuid_str = lustre_cfg_string(cfg, 4);
4062 obd_str2uuid(uuidp, uuid_str);
4063 obd_str2uuid(mdcuuidp, mdc_uuid_str);
4065 mdc = class_find_client_obd(uuidp, LUSTRE_MDC_NAME, mdcuuidp);
4067 CERROR("can't find controller MDC by uuid %s\n",
4070 } else if (!mdc->obd_set_up) {
4071 CERROR("target %s not set up\n", mdc->obd_name);
4074 LASSERT(ms->ms_control_exp);
4075 OBD_ALLOC_PTR(ms->ms_client_seq);
4076 if (ms->ms_client_seq != NULL) {
4079 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4083 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4086 rc = seq_client_init(ms->ms_client_seq,
4088 LUSTRE_SEQ_METADATA,
4090 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4097 LASSERT(ms->ms_server_seq != NULL);
4098 rc = seq_server_set_cli(ms->ms_server_seq, ms->ms_client_seq,
4105 static void mdt_seq_fini_cli(struct mdt_device *m)
4111 ms = mdt_md_site(m);
4114 if (ms->ms_server_seq)
4115 seq_server_set_cli(ms->ms_server_seq,
4118 if (ms->ms_control_exp) {
4119 class_export_put(ms->ms_control_exp);
4120 ms->ms_control_exp = NULL;
4129 static int mdt_fld_fini(const struct lu_env *env,
4130 struct mdt_device *m)
4132 struct md_site *ms = mdt_md_site(m);
4135 if (ms && ms->ms_server_fld) {
4136 fld_server_fini(ms->ms_server_fld, env);
4137 OBD_FREE_PTR(ms->ms_server_fld);
4138 ms->ms_server_fld = NULL;
4144 static int mdt_fld_init(const struct lu_env *env,
4146 struct mdt_device *m)
4152 ms = mdt_md_site(m);
4154 OBD_ALLOC_PTR(ms->ms_server_fld);
4155 if (ms->ms_server_fld == NULL)
4156 RETURN(rc = -ENOMEM);
4158 rc = fld_server_init(ms->ms_server_fld,
4159 m->mdt_bottom, uuid,
4160 env, ms->ms_node_id);
4162 OBD_FREE_PTR(ms->ms_server_fld);
4163 ms->ms_server_fld = NULL;
4170 /* device init/fini methods */
4171 static void mdt_stop_ptlrpc_service(struct mdt_device *m)
4174 if (m->mdt_regular_service != NULL) {
4175 ptlrpc_unregister_service(m->mdt_regular_service);
4176 m->mdt_regular_service = NULL;
4178 if (m->mdt_readpage_service != NULL) {
4179 ptlrpc_unregister_service(m->mdt_readpage_service);
4180 m->mdt_readpage_service = NULL;
4182 if (m->mdt_xmds_service != NULL) {
4183 ptlrpc_unregister_service(m->mdt_xmds_service);
4184 m->mdt_xmds_service = NULL;
4186 if (m->mdt_setattr_service != NULL) {
4187 ptlrpc_unregister_service(m->mdt_setattr_service);
4188 m->mdt_setattr_service = NULL;
4190 if (m->mdt_mdsc_service != NULL) {
4191 ptlrpc_unregister_service(m->mdt_mdsc_service);
4192 m->mdt_mdsc_service = NULL;
4194 if (m->mdt_mdss_service != NULL) {
4195 ptlrpc_unregister_service(m->mdt_mdss_service);
4196 m->mdt_mdss_service = NULL;
4198 if (m->mdt_dtss_service != NULL) {
4199 ptlrpc_unregister_service(m->mdt_dtss_service);
4200 m->mdt_dtss_service = NULL;
4202 if (m->mdt_fld_service != NULL) {
4203 ptlrpc_unregister_service(m->mdt_fld_service);
4204 m->mdt_fld_service = NULL;
4209 static int mdt_start_ptlrpc_service(struct mdt_device *m)
4211 static struct ptlrpc_service_conf conf;
4212 cfs_proc_dir_entry_t *procfs_entry;
4216 m->mdt_ldlm_client = &m->mdt_md_dev.md_lu_dev.ld_obd->obd_ldlm_client;
4217 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4218 "mdt_ldlm_client", m->mdt_ldlm_client);
4220 procfs_entry = m->mdt_md_dev.md_lu_dev.ld_obd->obd_proc_entry;
4222 conf = (typeof(conf)) {
4223 .psc_name = LUSTRE_MDT_NAME,
4224 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4226 .bc_nbufs = MDS_NBUFS,
4227 .bc_buf_size = MDS_BUFSIZE,
4228 .bc_req_max_size = MDS_MAXREQSIZE,
4229 .bc_rep_max_size = MDS_MAXREPSIZE,
4230 .bc_req_portal = MDS_REQUEST_PORTAL,
4231 .bc_rep_portal = MDC_REPLY_PORTAL,
4234 * We'd like to have a mechanism to set this on a per-device
4235 * basis, but alas...
4238 .tc_thr_name = LUSTRE_MDT_NAME,
4239 .tc_thr_factor = MDT_THR_FACTOR,
4240 .tc_nthrs_init = MDT_NTHRS_INIT,
4241 .tc_nthrs_base = MDT_NTHRS_BASE,
4242 .tc_nthrs_max = MDT_NTHRS_MAX,
4243 .tc_nthrs_user = mds_num_threads,
4244 .tc_cpu_affinity = 1,
4245 .tc_ctx_tags = LCT_MD_THREAD,
4248 .cc_pattern = mds_num_cpts,
4251 .so_req_handler = mdt_regular_handle,
4252 .so_req_printer = target_print_req,
4253 .so_hpreq_handler = ptlrpc_hpreq_handler,
4256 m->mdt_regular_service = ptlrpc_register_service(&conf, procfs_entry);
4257 if (IS_ERR(m->mdt_regular_service)) {
4258 rc = PTR_ERR(m->mdt_regular_service);
4259 CERROR("failed to start regular mdt service: %d\n", rc);
4260 m->mdt_regular_service = NULL;
4266 * readpage service configuration. Parameters have to be adjusted,
4269 memset(&conf, 0, sizeof(conf));
4270 conf = (typeof(conf)) {
4271 .psc_name = LUSTRE_MDT_NAME "_readpage",
4272 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4274 .bc_nbufs = MDS_NBUFS,
4275 .bc_buf_size = MDS_BUFSIZE,
4276 .bc_req_max_size = MDS_MAXREQSIZE,
4277 .bc_rep_max_size = MDS_MAXREPSIZE,
4278 .bc_req_portal = MDS_READPAGE_PORTAL,
4279 .bc_rep_portal = MDC_REPLY_PORTAL,
4282 .tc_thr_name = "mdt_rdpg",
4283 .tc_thr_factor = MDT_RDPG_THR_FACTOR,
4284 .tc_nthrs_init = MDT_RDPG_NTHRS_INIT,
4285 .tc_nthrs_base = MDT_RDPG_NTHRS_BASE,
4286 .tc_nthrs_max = MDT_RDPG_NTHRS_MAX,
4287 .tc_nthrs_user = mds_rdpg_num_threads,
4288 .tc_cpu_affinity = 1,
4289 .tc_ctx_tags = LCT_MD_THREAD,
4292 .cc_pattern = mds_rdpg_num_cpts,
4295 .so_req_handler = mdt_readpage_handle,
4296 .so_req_printer = target_print_req,
4299 m->mdt_readpage_service = ptlrpc_register_service(&conf, procfs_entry);
4300 if (IS_ERR(m->mdt_readpage_service)) {
4301 rc = PTR_ERR(m->mdt_readpage_service);
4302 CERROR("failed to start readpage service: %d\n", rc);
4303 m->mdt_readpage_service = NULL;
4305 GOTO(err_mdt_svc, rc);
4309 * setattr service configuration.
4311 * XXX To keep the compatibility with old client(< 2.2), we need to
4312 * preserve this portal for a certain time, it should be removed
4313 * eventually. LU-617.
4315 memset(&conf, 0, sizeof(conf));
4316 conf = (typeof(conf)) {
4317 .psc_name = LUSTRE_MDT_NAME "_setattr",
4318 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4320 .bc_nbufs = MDS_NBUFS,
4321 .bc_buf_size = MDS_BUFSIZE,
4322 .bc_req_max_size = MDS_MAXREQSIZE,
4323 .bc_rep_max_size = MDS_MAXREPSIZE,
4324 .bc_req_portal = MDS_SETATTR_PORTAL,
4325 .bc_rep_portal = MDC_REPLY_PORTAL,
4328 .tc_thr_name = "mdt_attr",
4329 .tc_thr_factor = MDT_SETA_THR_FACTOR,
4330 .tc_nthrs_init = MDT_SETA_NTHRS_INIT,
4331 .tc_nthrs_base = MDT_SETA_NTHRS_BASE,
4332 .tc_nthrs_max = MDT_SETA_NTHRS_MAX,
4333 .tc_nthrs_user = mds_attr_num_threads,
4334 .tc_cpu_affinity = 1,
4335 .tc_ctx_tags = LCT_MD_THREAD,
4338 .cc_pattern = mds_attr_num_cpts,
4341 .so_req_handler = mdt_regular_handle,
4342 .so_req_printer = target_print_req,
4345 m->mdt_setattr_service = ptlrpc_register_service(&conf, procfs_entry);
4346 if (IS_ERR(m->mdt_setattr_service)) {
4347 rc = PTR_ERR(m->mdt_setattr_service);
4348 CERROR("failed to start setattr service: %d\n", rc);
4349 m->mdt_setattr_service = NULL;
4351 GOTO(err_mdt_svc, rc);
4355 * sequence controller service configuration
4357 memset(&conf, 0, sizeof(conf));
4358 conf = (typeof(conf)) {
4359 .psc_name = LUSTRE_MDT_NAME "_mdsc",
4360 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4362 .bc_nbufs = MDS_NBUFS,
4363 .bc_buf_size = MDS_BUFSIZE,
4364 .bc_req_max_size = SEQ_MAXREQSIZE,
4365 .bc_rep_max_size = SEQ_MAXREPSIZE,
4366 .bc_req_portal = SEQ_CONTROLLER_PORTAL,
4367 .bc_rep_portal = MDC_REPLY_PORTAL,
4370 .tc_thr_name = "mdt_mdsc",
4371 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4372 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4373 .tc_ctx_tags = LCT_MD_THREAD,
4376 .so_req_handler = mdt_mdsc_handle,
4377 .so_req_printer = target_print_req,
4380 m->mdt_mdsc_service = ptlrpc_register_service(&conf, procfs_entry);
4381 if (IS_ERR(m->mdt_mdsc_service)) {
4382 rc = PTR_ERR(m->mdt_mdsc_service);
4383 CERROR("failed to start seq controller service: %d\n", rc);
4384 m->mdt_mdsc_service = NULL;
4386 GOTO(err_mdt_svc, rc);
4390 * metadata sequence server service configuration
4392 memset(&conf, 0, sizeof(conf));
4393 conf = (typeof(conf)) {
4394 .psc_name = LUSTRE_MDT_NAME "_mdss",
4395 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4397 .bc_nbufs = MDS_NBUFS,
4398 .bc_buf_size = MDS_BUFSIZE,
4399 .bc_req_max_size = SEQ_MAXREQSIZE,
4400 .bc_rep_max_size = SEQ_MAXREPSIZE,
4401 .bc_req_portal = SEQ_METADATA_PORTAL,
4402 .bc_rep_portal = MDC_REPLY_PORTAL,
4405 .tc_thr_name = "mdt_mdss",
4406 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4407 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4408 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4411 .so_req_handler = mdt_mdss_handle,
4412 .so_req_printer = target_print_req,
4415 m->mdt_mdss_service = ptlrpc_register_service(&conf, procfs_entry);
4416 if (IS_ERR(m->mdt_mdss_service)) {
4417 rc = PTR_ERR(m->mdt_mdss_service);
4418 CERROR("failed to start metadata seq server service: %d\n", rc);
4419 m->mdt_mdss_service = NULL;
4421 GOTO(err_mdt_svc, rc);
4425 * Data sequence server service configuration. We want to have really
4426 * cluster-wide sequences space. This is why we start only one sequence
4427 * controller which manages space.
4429 memset(&conf, 0, sizeof(conf));
4430 conf = (typeof(conf)) {
4431 .psc_name = LUSTRE_MDT_NAME "_dtss",
4432 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4434 .bc_nbufs = MDS_NBUFS,
4435 .bc_buf_size = MDS_BUFSIZE,
4436 .bc_req_max_size = SEQ_MAXREQSIZE,
4437 .bc_rep_max_size = SEQ_MAXREPSIZE,
4438 .bc_req_portal = SEQ_DATA_PORTAL,
4439 .bc_rep_portal = OSC_REPLY_PORTAL,
4442 .tc_thr_name = "mdt_dtss",
4443 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4444 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4445 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD
4448 .so_req_handler = mdt_dtss_handle,
4449 .so_req_printer = target_print_req,
4452 m->mdt_dtss_service = ptlrpc_register_service(&conf, procfs_entry);
4453 if (IS_ERR(m->mdt_dtss_service)) {
4454 rc = PTR_ERR(m->mdt_dtss_service);
4455 CERROR("failed to start data seq server service: %d\n", rc);
4456 m->mdt_dtss_service = NULL;
4458 GOTO(err_mdt_svc, rc);
4461 /* FLD service start */
4462 memset(&conf, 0, sizeof(conf));
4463 conf = (typeof(conf)) {
4464 .psc_name = LUSTRE_MDT_NAME "_fld",
4465 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4467 .bc_nbufs = MDS_NBUFS,
4468 .bc_buf_size = MDS_BUFSIZE,
4469 .bc_req_max_size = FLD_MAXREQSIZE,
4470 .bc_rep_max_size = FLD_MAXREPSIZE,
4471 .bc_req_portal = FLD_REQUEST_PORTAL,
4472 .bc_rep_portal = MDC_REPLY_PORTAL,
4475 .tc_thr_name = "mdt_fld",
4476 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4477 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4478 .tc_ctx_tags = LCT_DT_THREAD | LCT_MD_THREAD
4481 .so_req_handler = mdt_fld_handle,
4482 .so_req_printer = target_print_req,
4485 m->mdt_fld_service = ptlrpc_register_service(&conf, procfs_entry);
4486 if (IS_ERR(m->mdt_fld_service)) {
4487 rc = PTR_ERR(m->mdt_fld_service);
4488 CERROR("failed to start fld service: %d\n", rc);
4489 m->mdt_fld_service = NULL;
4491 GOTO(err_mdt_svc, rc);
4495 * mds-mds service configuration. Separate portal is used to allow
4496 * mds-mds requests be not blocked during recovery.
4498 memset(&conf, 0, sizeof(conf));
4499 conf = (typeof(conf)) {
4500 .psc_name = LUSTRE_MDT_NAME "_mds",
4501 .psc_watchdog_factor = MDT_SERVICE_WATCHDOG_FACTOR,
4503 .bc_nbufs = MDS_NBUFS,
4504 .bc_buf_size = MDS_BUFSIZE,
4505 .bc_req_max_size = MDS_MAXREQSIZE,
4506 .bc_rep_max_size = MDS_MAXREPSIZE,
4507 .bc_req_portal = MDS_MDS_PORTAL,
4508 .bc_rep_portal = MDC_REPLY_PORTAL,
4511 .tc_thr_name = "mdt_mds",
4512 .tc_nthrs_init = MDT_OTHR_NTHRS_INIT,
4513 .tc_nthrs_max = MDT_OTHR_NTHRS_MAX,
4514 .tc_ctx_tags = LCT_MD_THREAD,
4517 .so_req_handler = mdt_xmds_handle,
4518 .so_req_printer = target_print_req,
4519 .so_hpreq_handler = ptlrpc_hpreq_handler,
4522 m->mdt_xmds_service = ptlrpc_register_service(&conf, procfs_entry);
4523 if (IS_ERR(m->mdt_xmds_service)) {
4524 rc = PTR_ERR(m->mdt_xmds_service);
4525 CERROR("failed to start xmds service: %d\n", rc);
4526 m->mdt_xmds_service = NULL;
4528 GOTO(err_mdt_svc, rc);
4534 mdt_stop_ptlrpc_service(m);
4539 static void mdt_stack_fini(const struct lu_env *env,
4540 struct mdt_device *m, struct lu_device *top)
4542 struct obd_device *obd = mdt2obd_dev(m);
4543 struct lustre_cfg_bufs *bufs;
4544 struct lustre_cfg *lcfg;
4545 struct mdt_thread_info *info;
4549 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4550 LASSERT(info != NULL);
4552 lu_dev_del_linkage(top->ld_site, top);
4554 lu_site_purge(env, top->ld_site, -1);
4556 bufs = &info->mti_u.bufs;
4557 /* process cleanup, pass mdt obd name to get obd umount flags */
4558 /* another purpose is to let all layers to release their objects */
4559 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4564 lustre_cfg_bufs_set_string(bufs, 1, flags);
4565 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4567 CERROR("Cannot alloc lcfg!\n");
4571 top->ld_ops->ldo_process_config(env, top, lcfg);
4572 lustre_cfg_free(lcfg);
4574 lu_site_purge(env, top->ld_site, -1);
4576 m->mdt_child = NULL;
4577 m->mdt_bottom = NULL;
4579 obd_disconnect(m->mdt_child_exp);
4580 m->mdt_child_exp = NULL;
4582 obd_disconnect(m->mdt_bottom_exp);
4583 m->mdt_child_exp = NULL;
4586 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4587 const char *next, struct obd_export **exp)
4589 struct obd_connect_data *data = NULL;
4590 struct obd_device *obd;
4594 OBD_ALLOC_PTR(data);
4596 GOTO(out, rc = -ENOMEM);
4598 obd = class_name2obd(next);
4600 CERROR("%s: can't locate next device: %s\n",
4601 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4602 GOTO(out, rc = -ENOTCONN);
4605 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4606 data->ocd_version = LUSTRE_VERSION_CODE;
4608 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4610 CERROR("%s: cannot connect to next dev %s (%d)\n",
4611 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4621 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4622 struct lustre_cfg *cfg)
4624 char *dev = lustre_cfg_string(cfg, 0);
4625 int rc, name_size, uuid_size;
4626 char *name, *uuid, *p;
4627 struct lustre_cfg_bufs *bufs;
4628 struct lustre_cfg *lcfg;
4629 struct obd_device *obd;
4630 struct lustre_profile *lprof;
4631 struct lu_site *site;
4634 /* in 1.8 we had the only device in the stack - MDS.
4635 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4636 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4637 * to generate names and setup MDT, MDD. MDT will be using
4638 * generated name to connect to MDD. for MDD the next device
4639 * will be LOD with name taken from so called "profile" which
4640 * is generated by mount_option line
4642 * 1.8 MGS generates config. commands like this:
4643 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4644 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4645 * 2.0 MGS generates config. commands like this:
4646 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4647 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4648 * 3:lustre-MDT0000-mdtlov 4:f
4650 * we generate MDD name from MDT one, just replacing T with D
4652 * after all the preparations, the logical equivalent will be
4653 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4654 * 3:lustre-MDT0000-mdtlov 4:f
4655 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4656 * 3:lustre-MDD0000 4:f
4658 * notice we build the stack from down to top: MDD first, then MDT */
4660 name_size = MAX_OBD_NAME;
4661 uuid_size = MAX_OBD_NAME;
4663 OBD_ALLOC(name, name_size);
4664 OBD_ALLOC(uuid, uuid_size);
4665 if (name == NULL || uuid == NULL)
4666 GOTO(cleanup_mem, rc = -ENOMEM);
4668 OBD_ALLOC_PTR(bufs);
4670 GOTO(cleanup_mem, rc = -ENOMEM);
4673 p = strstr(name, "-MDT");
4675 GOTO(cleanup_mem, rc = -ENOMEM);
4678 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4680 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4681 if (lprof == NULL || lprof->lp_dt == NULL) {
4682 CERROR("can't find the profile: %s\n",
4683 lustre_cfg_string(cfg, 0));
4684 GOTO(cleanup_mem, rc = -EINVAL);
4687 lustre_cfg_bufs_reset(bufs, name);
4688 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4689 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4690 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4692 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4694 GOTO(free_bufs, rc = -ENOMEM);
4696 rc = class_attach(lcfg);
4698 GOTO(lcfg_cleanup, rc);
4700 obd = class_name2obd(name);
4702 CERROR("Can not find obd %s (%s in config)\n",
4703 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4704 GOTO(class_detach, rc = -EINVAL);
4707 lustre_cfg_free(lcfg);
4709 lustre_cfg_bufs_reset(bufs, name);
4710 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4711 lustre_cfg_bufs_set_string(bufs, 2, dev);
4712 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4714 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4716 rc = class_setup(obd, lcfg);
4718 GOTO(class_detach, rc);
4720 /* connect to MDD we just setup */
4721 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4725 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4727 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4728 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4729 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4730 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4733 /* now connect to bottom OSD */
4734 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4735 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4739 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4742 rc = lu_env_refill((struct lu_env *)env);
4744 CERROR("Failure to refill session: '%d'\n", rc);
4746 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4751 class_detach(obd, lcfg);
4753 lustre_cfg_free(lcfg);
4758 OBD_FREE(name, name_size);
4760 OBD_FREE(uuid, uuid_size);
4764 /* setup quota master target on MDT0 */
4765 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4766 struct lustre_cfg *cfg)
4768 struct obd_device *obd;
4769 char *dev = lustre_cfg_string(cfg, 0);
4770 char *qmtname, *uuid, *p;
4771 struct lustre_cfg_bufs *bufs;
4772 struct lustre_cfg *lcfg;
4773 struct lustre_profile *lprof;
4774 struct obd_connect_data *data;
4778 LASSERT(mdt->mdt_qmt_exp == NULL);
4779 LASSERT(mdt->mdt_qmt_dev == NULL);
4781 /* quota master is on MDT0 only for now */
4782 if (mdt->mdt_mite.ms_node_id != 0)
4785 /* MGS generates config commands which look as follows:
4786 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4787 * 3:lustre-MDT0000-mdtlov 4:f
4789 * We generate the QMT name from the MDT one, just replacing MD with QM
4790 * after all the preparations, the logical equivalent will be:
4791 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4792 * 3:lustre-MDT0000-osd 4:f */
4793 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4794 OBD_ALLOC(uuid, UUID_MAX);
4795 OBD_ALLOC_PTR(bufs);
4796 OBD_ALLOC_PTR(data);
4797 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4798 GOTO(cleanup_mem, rc = -ENOMEM);
4800 strcpy(qmtname, dev);
4801 p = strstr(qmtname, "-MDT");
4803 GOTO(cleanup_mem, rc = -ENOMEM);
4804 /* replace MD with QM */
4808 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4810 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4811 if (lprof == NULL || lprof->lp_dt == NULL) {
4812 CERROR("can't find profile for %s\n",
4813 lustre_cfg_string(cfg, 0));
4814 GOTO(cleanup_mem, rc = -EINVAL);
4817 lustre_cfg_bufs_reset(bufs, qmtname);
4818 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4819 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4820 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4822 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4824 GOTO(cleanup_mem, rc = -ENOMEM);
4826 rc = class_attach(lcfg);
4828 GOTO(lcfg_cleanup, rc);
4830 obd = class_name2obd(qmtname);
4832 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4833 lustre_cfg_string(cfg, 0));
4834 GOTO(class_detach, rc = -EINVAL);
4837 lustre_cfg_free(lcfg);
4839 lustre_cfg_bufs_reset(bufs, qmtname);
4840 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4841 lustre_cfg_bufs_set_string(bufs, 2, dev);
4843 /* for quota, the next device should be the OSD device */
4844 lustre_cfg_bufs_set_string(bufs, 3,
4845 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4847 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4849 rc = class_setup(obd, lcfg);
4851 GOTO(class_detach, rc);
4853 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4855 /* configure local quota objects */
4856 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4857 &mdt->mdt_md_dev.md_lu_dev,
4860 GOTO(class_cleanup, rc);
4862 /* connect to quota master target */
4863 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4864 data->ocd_version = LUSTRE_VERSION_CODE;
4865 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4868 CERROR("cannot connect to quota master device %s (%d)\n",
4870 GOTO(class_cleanup, rc);
4876 class_manual_cleanup(obd);
4877 mdt->mdt_qmt_dev = NULL;
4881 class_detach(obd, lcfg);
4883 lustre_cfg_free(lcfg);
4888 OBD_FREE(qmtname, MAX_OBD_NAME);
4890 OBD_FREE(uuid, UUID_MAX);
4896 /* Shutdown quota master target associated with mdt */
4897 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4901 if (mdt->mdt_qmt_exp == NULL)
4903 LASSERT(mdt->mdt_qmt_dev != NULL);
4905 /* the qmt automatically shuts down when the mdt disconnects */
4906 obd_disconnect(mdt->mdt_qmt_exp);
4907 mdt->mdt_qmt_exp = NULL;
4908 mdt->mdt_qmt_dev = NULL;
4912 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4914 struct md_device *next = m->mdt_child;
4915 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4916 struct obd_device *obd = mdt2obd_dev(m);
4919 target_recovery_fini(obd);
4921 ping_evictor_stop();
4923 mdt_stop_ptlrpc_service(m);
4924 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4925 obd_exports_barrier(obd);
4926 obd_zombie_barrier();
4930 tgt_fini(env, &m->mdt_lut);
4931 mdt_fs_cleanup(env, m);
4932 upcall_cache_cleanup(m->mdt_identity_cache);
4933 m->mdt_identity_cache = NULL;
4935 if (m->mdt_namespace != NULL) {
4936 ldlm_namespace_free(m->mdt_namespace, NULL,
4937 d->ld_obd->obd_force);
4938 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4941 mdt_quota_fini(env, m);
4943 cfs_free_nidlist(&m->mdt_nosquash_nids);
4944 if (m->mdt_nosquash_str) {
4945 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4946 m->mdt_nosquash_str = NULL;
4947 m->mdt_nosquash_strlen = 0;
4950 mdt_seq_fini(env, m);
4951 mdt_seq_fini_cli(m);
4952 mdt_fld_fini(env, m);
4953 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4955 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4956 cfs_timer_disarm(&m->mdt_ck_timer);
4957 mdt_ck_thread_stop(m);
4962 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4964 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4966 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4971 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4973 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4974 struct sptlrpc_rule_set tmp_rset;
4977 sptlrpc_rule_set_init(&tmp_rset);
4978 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4980 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4985 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4987 write_lock(&m->mdt_sptlrpc_lock);
4988 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4989 m->mdt_sptlrpc_rset = tmp_rset;
4990 write_unlock(&m->mdt_sptlrpc_lock);
4995 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4997 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4998 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5000 struct mdt_thread_info *info;
5001 struct obd_device *obd;
5002 const char *dev = lustre_cfg_string(cfg, 0);
5003 const char *num = lustre_cfg_string(cfg, 2);
5004 struct lustre_mount_info *lmi = NULL;
5005 struct lustre_sb_info *lsi;
5007 struct md_site *mite;
5008 const char *identity_upcall = "NONE";
5009 struct md_device *next;
5015 md_device_init(&m->mdt_md_dev, ldt);
5017 * Environment (env) might be missing mdt_thread_key values at that
5018 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5021 * Usually device allocation path doesn't use module key values, but
5022 * mdt has to do a lot of work here, so allocate key value.
5024 rc = lu_env_refill((struct lu_env *)env);
5028 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5029 LASSERT(info != NULL);
5031 obd = class_name2obd(dev);
5032 LASSERT(obd != NULL);
5034 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
5036 m->mdt_som_conf = 0;
5038 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5039 lmi = server_get_mount(dev);
5041 CERROR("Cannot get mount info for %s!\n", dev);
5044 lsi = s2lsi(lmi->lmi_sb);
5045 /* CMD is supported only in IAM mode */
5047 node_id = simple_strtol(num, NULL, 10);
5048 obd->u.obt.obt_magic = OBT_MAGIC;
5051 rwlock_init(&m->mdt_sptlrpc_lock);
5052 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
5054 spin_lock_init(&m->mdt_ioepoch_lock);
5055 m->mdt_opts.mo_compat_resname = 0;
5056 m->mdt_opts.mo_mds_capa = 1;
5057 m->mdt_opts.mo_oss_capa = 1;
5058 m->mdt_capa_timeout = CAPA_TIMEOUT;
5059 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
5060 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
5061 m->mdt_squash_uid = 0;
5062 m->mdt_squash_gid = 0;
5063 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
5064 m->mdt_nosquash_str = NULL;
5065 m->mdt_nosquash_strlen = 0;
5066 init_rwsem(&m->mdt_squash_sem);
5067 spin_lock_init(&m->mdt_osfs_lock);
5068 m->mdt_osfs_age = cfs_time_shift_64(-1000);
5070 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
5071 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
5072 /* set this lu_device to obd, because error handling need it */
5073 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
5075 /* init the stack */
5076 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5078 CERROR("Can't init device stack, rc %d\n", rc);
5082 s = m->mdt_md_dev.md_lu_dev.ld_site;
5083 mite = &m->mdt_mite;
5084 s->ld_md_site = mite;
5087 /* set server index */
5088 mite->ms_node_id = node_id;
5090 /* failover is the default
5091 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5092 * assumed whose ms_node_id == 0 XXX
5094 obd->obd_replayable = 1;
5095 /* No connection accepted until configurations will finish */
5096 obd->obd_no_conn = 1;
5098 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5099 char *str = lustre_cfg_string(cfg, 4);
5100 if (strchr(str, 'n')) {
5101 CWARN("%s: recovery disabled\n", obd->obd_name);
5102 obd->obd_replayable = 0;
5106 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
5108 GOTO(err_fini_stack, rc);
5110 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
5111 LUSTRE_MDT_NAME"-%p", m);
5112 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5113 LDLM_NAMESPACE_SERVER,
5114 LDLM_NAMESPACE_GREEDY,
5116 if (m->mdt_namespace == NULL)
5117 GOTO(err_fini_seq, rc = -ENOMEM);
5119 m->mdt_namespace->ns_lvbp = m;
5120 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5122 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5123 /* set obd_namespace for compatibility with old code */
5124 obd->obd_namespace = m->mdt_namespace;
5126 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
5128 rc = mdt_ck_thread_start(m);
5130 GOTO(err_free_ns, rc);
5132 rc = mdt_fs_setup(env, m, obd, lsi);
5136 mdt_adapt_sptlrpc_conf(obd, 1);
5138 next = m->mdt_child;
5139 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5142 GOTO(err_llog_cleanup, rc);
5144 if (mntopts & MNTOPT_USERXATTR)
5145 m->mdt_opts.mo_user_xattr = 1;
5147 m->mdt_opts.mo_user_xattr = 0;
5149 if (mntopts & MNTOPT_ACL)
5150 m->mdt_opts.mo_acl = 1;
5152 m->mdt_opts.mo_acl = 0;
5154 /* XXX: to support suppgid for ACL, we enable identity_upcall
5155 * by default, otherwise, maybe got unexpected -EACCESS. */
5156 if (m->mdt_opts.mo_acl)
5157 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5159 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
5160 &mdt_identity_upcall_cache_ops);
5161 if (IS_ERR(m->mdt_identity_cache)) {
5162 rc = PTR_ERR(m->mdt_identity_cache);
5163 m->mdt_identity_cache = NULL;
5164 GOTO(err_llog_cleanup, rc);
5167 rc = mdt_procfs_init(m, dev);
5169 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5170 GOTO(err_recovery, rc);
5173 rc = mdt_quota_init(env, m, cfg);
5175 GOTO(err_procfs, rc);
5177 rc = mdt_start_ptlrpc_service(m);
5179 GOTO(err_quota, rc);
5181 ping_evictor_start();
5183 /* recovery will be started upon mdt_prepare()
5184 * when the whole stack is complete and ready
5185 * to serve the requests */
5187 mdt_init_capa_ctxt(env, m);
5189 /* Reduce the initial timeout on an MDS because it doesn't need such
5190 * a long timeout as an OST does. Adaptive timeouts will adjust this
5191 * value appropriately. */
5192 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5193 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5197 ping_evictor_stop();
5198 mdt_stop_ptlrpc_service(m);
5200 mdt_quota_fini(env, m);
5204 target_recovery_fini(obd);
5205 upcall_cache_cleanup(m->mdt_identity_cache);
5206 m->mdt_identity_cache = NULL;
5208 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5209 mdt_fs_cleanup(env, m);
5211 cfs_timer_disarm(&m->mdt_ck_timer);
5212 mdt_ck_thread_stop(m);
5214 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5215 obd->obd_namespace = m->mdt_namespace = NULL;
5217 mdt_seq_fini(env, m);
5218 mdt_fld_fini(env, m);
5219 tgt_fini(env, &m->mdt_lut);
5221 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5224 server_put_mount(dev, lmi->lmi_mnt);
5228 /* For interoperability, the left element is old parameter, the right one
5229 * is the new version of the parameter, if some parameter is deprecated,
5230 * the new version should be set as NULL. */
5231 static struct cfg_interop_param mdt_interop_param[] = {
5232 { "mdt.group_upcall", NULL },
5233 { "mdt.quota_type", NULL },
5234 { "mdd.quota_type", NULL },
5235 { "mdt.rootsquash", "mdt.root_squash" },
5236 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5240 /* used by MGS to process specific configurations */
5241 static int mdt_process_config(const struct lu_env *env,
5242 struct lu_device *d, struct lustre_cfg *cfg)
5244 struct mdt_device *m = mdt_dev(d);
5245 struct md_device *md_next = m->mdt_child;
5246 struct lu_device *next = md2lu_dev(md_next);
5250 switch (cfg->lcfg_command) {
5252 struct lprocfs_static_vars lvars;
5253 struct obd_device *obd = d->ld_obd;
5255 /* For interoperability */
5256 struct cfg_interop_param *ptr = NULL;
5257 struct lustre_cfg *old_cfg = NULL;
5260 param = lustre_cfg_string(cfg, 1);
5261 if (param == NULL) {
5262 CERROR("param is empty\n");
5267 ptr = class_find_old_param(param, mdt_interop_param);
5269 if (ptr->new_param == NULL) {
5271 CWARN("For interoperability, skip this %s."
5272 " It is obsolete.\n", ptr->old_param);
5276 CWARN("Found old param %s, changed it to %s.\n",
5277 ptr->old_param, ptr->new_param);
5280 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5287 lprocfs_mdt_init_vars(&lvars);
5288 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
5290 if (rc > 0 || rc == -ENOSYS)
5291 /* we don't understand; pass it on */
5292 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5294 if (old_cfg != NULL)
5295 lustre_cfg_free(cfg);
5301 * Add mdc hook to get first MDT uuid and connect it to
5302 * ls->controller to use for seq manager.
5304 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5306 CERROR("Can't add mdc, rc %d\n", rc);
5308 rc = mdt_seq_init_cli(env, mdt_dev(d), cfg);
5311 /* others are passed further */
5312 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5318 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5319 const struct lu_object_header *hdr,
5320 struct lu_device *d)
5322 struct mdt_object *mo;
5326 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
5328 struct lu_object *o;
5329 struct lu_object_header *h;
5331 o = &mo->mot_obj.mo_lu;
5332 h = &mo->mot_header;
5333 lu_object_header_init(h);
5334 lu_object_init(o, h, d);
5335 lu_object_add_top(h, o);
5336 o->lo_ops = &mdt_obj_ops;
5337 mutex_init(&mo->mot_ioepoch_mutex);
5338 mutex_init(&mo->mot_lov_mutex);
5344 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5345 const struct lu_object_conf *unused)
5347 struct mdt_device *d = mdt_dev(o->lo_dev);
5348 struct lu_device *under;
5349 struct lu_object *below;
5353 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5354 PFID(lu_object_fid(o)));
5356 under = &d->mdt_child->md_lu_dev;
5357 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5358 if (below != NULL) {
5359 lu_object_add(o, below);
5366 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5368 struct mdt_object *mo = mdt_obj(o);
5369 struct lu_object_header *h;
5373 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5374 PFID(lu_object_fid(o)));
5377 lu_object_header_fini(h);
5378 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5383 static int mdt_object_print(const struct lu_env *env, void *cookie,
5384 lu_printer_t p, const struct lu_object *o)
5386 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5387 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5388 "flags="LPX64", epochcount=%d, writecount=%d)",
5389 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5390 mdto->mot_ioepoch_count, mdto->mot_writecount);
5393 static int mdt_prepare(const struct lu_env *env,
5394 struct lu_device *pdev,
5395 struct lu_device *cdev)
5397 struct mdt_device *mdt = mdt_dev(cdev);
5398 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5399 struct obd_device *obd = cdev->ld_obd;
5406 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5410 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5414 rc = mdt_fld_init(env, obd->obd_name, mdt);
5418 rc = mdt_seq_init(env, obd->obd_name, mdt);
5422 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5423 &mdt->mdt_md_root_fid);
5427 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5428 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5429 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5430 LASSERT(obd->obd_no_conn);
5431 spin_lock(&obd->obd_dev_lock);
5432 obd->obd_no_conn = 0;
5433 spin_unlock(&obd->obd_dev_lock);
5435 if (obd->obd_recovering == 0)
5436 mdt_postrecov(env, mdt);
5441 static const struct lu_device_operations mdt_lu_ops = {
5442 .ldo_object_alloc = mdt_object_alloc,
5443 .ldo_process_config = mdt_process_config,
5444 .ldo_prepare = mdt_prepare,
5447 static const struct lu_object_operations mdt_obj_ops = {
5448 .loo_object_init = mdt_object_init,
5449 .loo_object_free = mdt_object_free,
5450 .loo_object_print = mdt_object_print
5453 static int mdt_obd_set_info_async(const struct lu_env *env,
5454 struct obd_export *exp,
5455 __u32 keylen, void *key,
5456 __u32 vallen, void *val,
5457 struct ptlrpc_request_set *set)
5459 struct obd_device *obd = exp->exp_obd;
5465 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5466 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5474 * Match client and server connection feature flags.
5476 * Compute the compatibility flags for a connection request based on
5477 * features mutually supported by client and server.
5479 * The obd_export::exp_connect_flags field in \a exp must not be updated
5480 * here, otherwise a partially initialized value may be exposed. After
5481 * the connection request is successfully processed, the top-level MDT
5482 * connect request handler atomically updates the export connect flags
5483 * from the obd_connect_data::ocd_connect_flags field of the reply.
5484 * \see mdt_connect().
5486 * \param exp the obd_export associated with this client/target pair
5487 * \param mdt the target device for the connection
5488 * \param data stores data for this connect request
5491 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5492 * \retval -EBADE client and server feature requirements are incompatible
5494 static int mdt_connect_internal(struct obd_export *exp,
5495 struct mdt_device *mdt,
5496 struct obd_connect_data *data)
5498 LASSERT(data != NULL);
5500 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5501 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5503 /* If no known bits (which should not happen, probably,
5504 as everybody should support LOOKUP and UPDATE bits at least)
5505 revert to compat mode with plain locks. */
5506 if (!data->ocd_ibits_known &&
5507 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5508 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5510 if (!mdt->mdt_opts.mo_acl)
5511 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5513 if (!mdt->mdt_opts.mo_user_xattr)
5514 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5516 if (!mdt->mdt_som_conf)
5517 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5519 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5520 data->ocd_brw_size = min(data->ocd_brw_size,
5521 (__u32)(PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT));
5522 if (data->ocd_brw_size == 0) {
5523 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5524 " ocd_version: %x ocd_grant: %d "
5525 "ocd_index: %u ocd_brw_size is "
5526 "unexpectedly zero, network data "
5527 "corruption? Refusing connection of this"
5529 exp->exp_obd->obd_name,
5530 exp->exp_client_uuid.uuid,
5531 exp, data->ocd_connect_flags, data->ocd_version,
5532 data->ocd_grant, data->ocd_index);
5537 /* NB: Disregard the rule against updating exp_connect_flags in this
5538 * case, since tgt_client_new() needs to know if this is a lightweight
5539 * connection, and it is safe to expose this flag before connection
5540 * processing completes. */
5541 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5542 spin_lock(&exp->exp_lock);
5543 exp->exp_connect_flags |= OBD_CONNECT_LIGHTWEIGHT;
5544 spin_unlock(&exp->exp_lock);
5547 data->ocd_version = LUSTRE_VERSION_CODE;
5548 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5550 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5551 CWARN("%s: MDS requires FID support, but client not\n",
5552 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5556 if (mdt->mdt_som_conf &&
5557 !(data->ocd_connect_flags & (OBD_CONNECT_MDS_MDS|OBD_CONNECT_SOM))){
5558 CWARN("%s: MDS has SOM enabled, but client does not support "
5559 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5566 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5567 struct obd_export *exp,
5568 struct ptlrpc_request *req)
5570 struct sptlrpc_flavor flvr;
5573 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5574 read_lock(&mdt->mdt_sptlrpc_lock);
5575 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5579 read_unlock(&mdt->mdt_sptlrpc_lock);
5581 spin_lock(&exp->exp_lock);
5583 exp->exp_sp_peer = req->rq_sp_from;
5584 exp->exp_flvr = flvr;
5586 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5587 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5588 CERROR("unauthorized rpc flavor %x from %s, "
5589 "expect %x\n", req->rq_flvr.sf_rpc,
5590 libcfs_nid2str(req->rq_peer.nid),
5591 exp->exp_flvr.sf_rpc);
5595 spin_unlock(&exp->exp_lock);
5597 if (exp->exp_sp_peer != req->rq_sp_from) {
5598 CERROR("RPC source %s doesn't match %s\n",
5599 sptlrpc_part2name(req->rq_sp_from),
5600 sptlrpc_part2name(exp->exp_sp_peer));
5603 rc = sptlrpc_target_export_check(exp, req);
5610 /* mds_connect copy */
5611 static int mdt_obd_connect(const struct lu_env *env,
5612 struct obd_export **exp, struct obd_device *obd,
5613 struct obd_uuid *cluuid,
5614 struct obd_connect_data *data,
5617 struct mdt_thread_info *info;
5618 struct obd_export *lexp;
5619 struct lustre_handle conn = { 0 };
5620 struct mdt_device *mdt;
5621 struct ptlrpc_request *req;
5625 LASSERT(env != NULL);
5626 if (!exp || !obd || !cluuid)
5629 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5630 req = info->mti_pill->rc_req;
5631 mdt = mdt_dev(obd->obd_lu_dev);
5634 * first, check whether the stack is ready to handle requests
5635 * XXX: probably not very appropriate method is used now
5636 * at some point we should find a better one
5638 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state)) {
5639 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5642 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5645 rc = class_connect(&conn, obd, cluuid);
5649 lexp = class_conn2export(&conn);
5650 LASSERT(lexp != NULL);
5652 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5656 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5657 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5659 rc = mdt_connect_internal(lexp, mdt, data);
5661 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5664 info->mti_exp = lexp;
5665 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5666 rc = tgt_client_new(env, lexp);
5668 mdt_export_stats_init(obd, lexp, localdata);
5673 class_disconnect(lexp);
5682 static int mdt_obd_reconnect(const struct lu_env *env,
5683 struct obd_export *exp, struct obd_device *obd,
5684 struct obd_uuid *cluuid,
5685 struct obd_connect_data *data,
5688 struct mdt_thread_info *info;
5689 struct mdt_device *mdt;
5690 struct ptlrpc_request *req;
5694 if (exp == NULL || obd == NULL || cluuid == NULL)
5697 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5698 req = info->mti_pill->rc_req;
5699 mdt = mdt_dev(obd->obd_lu_dev);
5701 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5705 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5707 mdt_export_stats_init(obd, exp, localdata);
5712 static int mdt_export_cleanup(struct obd_export *exp)
5714 struct mdt_export_data *med = &exp->exp_mdt_data;
5715 struct obd_device *obd = exp->exp_obd;
5716 struct mdt_device *mdt;
5717 struct mdt_thread_info *info;
5719 CFS_LIST_HEAD(closing_list);
5720 struct mdt_file_data *mfd, *n;
5724 spin_lock(&med->med_open_lock);
5725 while (!cfs_list_empty(&med->med_open_head)) {
5726 cfs_list_t *tmp = med->med_open_head.next;
5727 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5729 /* Remove mfd handle so it can't be found again.
5730 * We are consuming the mfd_list reference here. */
5731 class_handle_unhash(&mfd->mfd_handle);
5732 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5734 spin_unlock(&med->med_open_lock);
5735 mdt = mdt_dev(obd->obd_lu_dev);
5736 LASSERT(mdt != NULL);
5738 rc = lu_env_init(&env, LCT_MD_THREAD);
5742 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5743 LASSERT(info != NULL);
5744 memset(info, 0, sizeof *info);
5745 info->mti_env = &env;
5746 info->mti_mdt = mdt;
5747 info->mti_exp = exp;
5749 if (!cfs_list_empty(&closing_list)) {
5750 struct md_attr *ma = &info->mti_attr;
5752 /* Close any open files (which may also cause orphan unlinking). */
5753 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5754 cfs_list_del_init(&mfd->mfd_list);
5755 ma->ma_need = ma->ma_valid = 0;
5756 /* Don't unlink orphan on failover umount, LU-184 */
5757 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5758 ma->ma_valid = MA_FLAGS;
5759 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5761 mdt_mfd_close(info, mfd);
5764 info->mti_mdt = NULL;
5765 /* cleanup client slot early */
5766 /* Do not erase record for recoverable client. */
5767 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5768 tgt_client_del(&env, exp);
5774 static int mdt_obd_disconnect(struct obd_export *exp)
5780 class_export_get(exp);
5782 rc = server_disconnect_export(exp);
5784 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5786 rc = mdt_export_cleanup(exp);
5787 class_export_put(exp);
5791 /* FIXME: Can we avoid using these two interfaces? */
5792 static int mdt_init_export(struct obd_export *exp)
5794 struct mdt_export_data *med = &exp->exp_mdt_data;
5798 CFS_INIT_LIST_HEAD(&med->med_open_head);
5799 spin_lock_init(&med->med_open_lock);
5800 mutex_init(&med->med_idmap_mutex);
5801 med->med_idmap = NULL;
5802 spin_lock(&exp->exp_lock);
5803 exp->exp_connecting = 1;
5804 spin_unlock(&exp->exp_lock);
5806 /* self-export doesn't need client data and ldlm initialization */
5807 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5808 &exp->exp_client_uuid)))
5811 rc = tgt_client_alloc(exp);
5815 rc = ldlm_init_export(exp);
5822 tgt_client_free(exp);
5824 CERROR("%s: Failed to initialize export: rc = %d\n",
5825 exp->exp_obd->obd_name, rc);
5829 static int mdt_destroy_export(struct obd_export *exp)
5833 if (exp_connect_rmtclient(exp))
5834 mdt_cleanup_idmap(&exp->exp_mdt_data);
5836 target_destroy_export(exp);
5837 /* destroy can be called from failed obd_setup, so
5838 * checking uuid is safer than obd_self_export */
5839 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5840 &exp->exp_client_uuid)))
5843 ldlm_destroy_export(exp);
5844 tgt_client_free(exp);
5846 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5847 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5852 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5853 void *val, int vallen)
5855 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5856 struct getinfo_fid2path *fpout, *fpin;
5859 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5862 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5863 lustre_swab_fid2path(fpin);
5865 memcpy(fpout, fpin, sizeof(*fpin));
5866 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5869 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5873 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5874 struct getinfo_fid2path *fp)
5876 struct mdt_object *obj;
5877 struct obd_device *obd = mdt2obd_dev(mdt);
5881 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5882 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5884 if (!fid_is_sane(&fp->gf_fid))
5887 if (!fid_is_norm(&fp->gf_fid) && !fid_is_igif(&fp->gf_fid)) {
5888 CWARN("%s: "DFID" is invalid, sequence should be "
5889 ">= "LPX64"\n", obd->obd_name,
5890 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5894 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5895 if (obj == NULL || IS_ERR(obj)) {
5896 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5901 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5907 mdt_object_put(env, obj);
5908 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5909 PFID(&fp->gf_fid), rc);
5913 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5914 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5915 mdt_object_put(env, obj);
5920 static int mdt_get_info(struct mdt_thread_info *info)
5922 struct ptlrpc_request *req = mdt_info_req(info);
5930 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5932 CDEBUG(D_IOCTL, "No GETINFO key");
5935 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5938 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5939 if (vallen == NULL) {
5940 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5944 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5946 rc = req_capsule_server_pack(info->mti_pill);
5947 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5948 if (valout == NULL) {
5949 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5953 if (KEY_IS(KEY_FID2PATH))
5954 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5958 lustre_msg_set_status(req->rq_repmsg, rc);
5963 /* Pass the ioc down */
5964 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5965 unsigned int cmd, int len, void *data)
5967 struct lu_context ioctl_session;
5968 struct md_device *next = mdt->mdt_child;
5972 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5975 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5976 lu_context_enter(&ioctl_session);
5977 env->le_ses = &ioctl_session;
5979 LASSERT(next->md_ops->mdo_iocontrol);
5980 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5982 lu_context_exit(&ioctl_session);
5983 lu_context_fini(&ioctl_session);
5987 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5989 struct obd_ioctl_data *data = karg;
5990 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5992 struct mdt_object *obj;
5993 struct mdt_lock_handle *lh;
5997 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5998 if (!fid_is_sane(fid))
6001 lh = &mti->mti_lh[MDT_LH_PARENT];
6002 mdt_lock_reg_init(lh, LCK_CR);
6004 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6006 RETURN(PTR_ERR(obj));
6008 rc = mdt_object_exists(obj);
6012 * before calling version get the correct MDS should be
6013 * fid, this is error to find remote object here
6015 CERROR("nonlocal object "DFID"\n", PFID(fid));
6016 } else if (rc == 0) {
6017 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6020 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6021 *(__u64 *)data->ioc_inlbuf2 = version;
6024 mdt_object_unlock_put(mti, obj, lh, 1);
6028 /* ioctls on obd dev */
6029 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6030 void *karg, void *uarg)
6033 struct obd_device *obd = exp->exp_obd;
6034 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6035 struct dt_device *dt = mdt->mdt_bottom;
6039 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6040 rc = lu_env_init(&env, LCT_MD_THREAD);
6046 rc = mdt_device_sync(&env, mdt);
6048 case OBD_IOC_SET_READONLY:
6049 rc = dt->dd_ops->dt_ro(&env, dt);
6051 case OBD_IOC_ABORT_RECOVERY:
6052 CERROR("Aborting recovery for device %s\n", obd->obd_name);
6053 target_stop_recovery_thread(obd);
6056 case OBD_IOC_CHANGELOG_REG:
6057 case OBD_IOC_CHANGELOG_DEREG:
6058 case OBD_IOC_CHANGELOG_CLEAR:
6059 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
6061 case OBD_IOC_START_LFSCK:
6062 case OBD_IOC_STOP_LFSCK: {
6063 struct md_device *next = mdt->mdt_child;
6064 struct obd_ioctl_data *data = karg;
6066 if (unlikely(data == NULL)) {
6071 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
6076 case OBD_IOC_GET_OBJ_VERSION: {
6077 struct mdt_thread_info *mti;
6078 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6079 memset(mti, 0, sizeof *mti);
6080 mti->mti_env = &env;
6084 rc = mdt_ioc_version_get(mti, karg);
6088 CERROR("Not supported cmd = %d for device %s\n",
6089 cmd, obd->obd_name);
6097 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6099 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6103 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6107 int mdt_obd_postrecov(struct obd_device *obd)
6112 rc = lu_env_init(&env, LCT_MD_THREAD);
6115 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6121 * Send a copytool req to a client
6122 * Note this sends a request RPC from a server (MDT) to a client (MDC),
6123 * backwards of normal comms.
6125 int mdt_hsm_copytool_send(struct obd_export *exp)
6128 struct hsm_action_list *hal;
6129 struct hsm_action_item *hai;
6133 CWARN("%s: writing to mdc at %s\n", exp->exp_obd->obd_name,
6134 libcfs_nid2str(exp->exp_connection->c_peer.nid));
6136 len = sizeof(*lh) + sizeof(*hal) + MTI_NAME_MAXLEN +
6137 /* for mockup below */ 2 * cfs_size_round(sizeof(*hai));
6142 lh->kuc_magic = KUC_MAGIC;
6143 lh->kuc_transport = KUC_TRANSPORT_HSM;
6144 lh->kuc_msgtype = HMT_ACTION_LIST;
6145 lh->kuc_msglen = len;
6147 hal = (struct hsm_action_list *)(lh + 1);
6148 hal->hal_version = HAL_VERSION;
6149 hal->hal_archive_num = 1;
6150 obd_uuid2fsname(hal->hal_fsname, exp->exp_obd->obd_name,
6153 /* mock up an action list */
6155 hai = hai_zero(hal);
6156 hai->hai_action = HSMA_ARCHIVE;
6157 hai->hai_fid.f_oid = 0xA00A;
6158 hai->hai_len = sizeof(*hai);
6159 hai = hai_next(hai);
6160 hai->hai_action = HSMA_RESTORE;
6161 hai->hai_fid.f_oid = 0xB00B;
6162 hai->hai_len = sizeof(*hai);
6164 /* Uses the ldlm reverse import; this rpc will be seen by
6165 the ldlm_callback_handler */
6166 rc = do_set_info_async(exp->exp_imp_reverse,
6167 LDLM_SET_INFO, LUSTRE_OBD_VERSION,
6168 sizeof(KEY_HSM_COPYTOOL_SEND),
6169 KEY_HSM_COPYTOOL_SEND,
6177 static struct obd_ops mdt_obd_device_ops = {
6178 .o_owner = THIS_MODULE,
6179 .o_set_info_async = mdt_obd_set_info_async,
6180 .o_connect = mdt_obd_connect,
6181 .o_reconnect = mdt_obd_reconnect,
6182 .o_disconnect = mdt_obd_disconnect,
6183 .o_init_export = mdt_init_export,
6184 .o_destroy_export = mdt_destroy_export,
6185 .o_iocontrol = mdt_iocontrol,
6186 .o_postrecov = mdt_obd_postrecov,
6189 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6190 struct lu_device *d)
6192 struct mdt_device *m = mdt_dev(d);
6199 static struct lu_device *mdt_device_free(const struct lu_env *env,
6200 struct lu_device *d)
6202 struct mdt_device *m = mdt_dev(d);
6205 md_device_fini(&m->mdt_md_dev);
6210 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6211 struct lu_device_type *t,
6212 struct lustre_cfg *cfg)
6214 struct lu_device *l;
6215 struct mdt_device *m;
6221 l = &m->mdt_md_dev.md_lu_dev;
6222 rc = mdt_init0(env, m, t, cfg);
6224 mdt_device_free(env, l);
6229 l = ERR_PTR(-ENOMEM);
6233 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6234 LU_KEY_INIT(mdt, struct mdt_thread_info);
6236 static void mdt_key_fini(const struct lu_context *ctx,
6237 struct lu_context_key *key, void* data)
6239 struct mdt_thread_info *info = data;
6241 if (info->mti_big_lmm) {
6242 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6243 info->mti_big_lmm = NULL;
6244 info->mti_big_lmmsize = 0;
6249 /* context key: mdt_thread_key */
6250 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6252 struct md_ucred *mdt_ucred(const struct mdt_thread_info *info)
6254 return md_ucred(info->mti_env);
6258 * Enable/disable COS (Commit On Sharing).
6260 * Set/Clear the COS flag in mdt options.
6262 * \param mdt mdt device
6263 * \param val 0 disables COS, other values enable COS
6265 void mdt_enable_cos(struct mdt_device *mdt, int val)
6270 mdt->mdt_opts.mo_cos = !!val;
6271 rc = lu_env_init(&env, LCT_LOCAL);
6272 if (unlikely(rc != 0)) {
6273 CWARN("lu_env initialization failed with rc = %d,"
6274 "cannot sync\n", rc);
6277 mdt_device_sync(&env, mdt);
6282 * Check COS (Commit On Sharing) status.
6284 * Return COS flag status.
6286 * \param mdt mdt device
6288 int mdt_cos_is_enabled(struct mdt_device *mdt)
6290 return mdt->mdt_opts.mo_cos != 0;
6293 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
6294 LU_TYPE_INIT_FINI(mdt, &mdt_thread_key);
6296 static struct lu_device_type_operations mdt_device_type_ops = {
6297 .ldto_init = mdt_type_init,
6298 .ldto_fini = mdt_type_fini,
6300 .ldto_start = mdt_type_start,
6301 .ldto_stop = mdt_type_stop,
6303 .ldto_device_alloc = mdt_device_alloc,
6304 .ldto_device_free = mdt_device_free,
6305 .ldto_device_fini = mdt_device_fini
6308 static struct lu_device_type mdt_device_type = {
6309 .ldt_tags = LU_DEVICE_MD,
6310 .ldt_name = LUSTRE_MDT_NAME,
6311 .ldt_ops = &mdt_device_type_ops,
6312 .ldt_ctx_tags = LCT_MD_THREAD
6315 static int __init mdt_mod_init(void)
6317 struct lprocfs_static_vars lvars;
6320 rc = lu_kmem_init(mdt_caches);
6324 if (mdt_num_threads != 0 && mds_num_threads == 0) {
6325 LCONSOLE_INFO("mdt_num_threads module parameter is deprecated,"
6326 "use mds_num_threads instead or unset both for"
6327 "dynamic thread startup\n");
6328 mds_num_threads = mdt_num_threads;
6331 lprocfs_mdt_init_vars(&lvars);
6332 rc = class_register_type(&mdt_obd_device_ops, NULL,
6333 lvars.module_vars, LUSTRE_MDT_NAME,
6337 lu_kmem_fini(mdt_caches);
6341 static void __exit mdt_mod_exit(void)
6343 class_unregister_type(LUSTRE_MDT_NAME);
6344 lu_kmem_fini(mdt_caches);
6347 #define DEFINE_RPC_HANDLER(base, flags, opc, fn, fmt) \
6350 .mh_fail_id = OBD_FAIL_ ## opc ## _NET, \
6352 .mh_flags = flags, \
6357 /* Request with a format known in advance */
6358 #define DEF_MDT_HDL(flags, name, fn) \
6359 DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, &RQF_ ## name)
6361 /* Request with a format we do not yet know */
6362 #define DEF_MDT_HDL_VAR(flags, name, fn) \
6363 DEFINE_RPC_HANDLER(MDS_GETATTR, flags, name, fn, NULL)
6365 /* Map one non-standard request format handler. This should probably get
6366 * a common OBD_SET_INFO RPC opcode instead of this mismatch. */
6367 #define RQF_MDS_SET_INFO RQF_OBD_SET_INFO
6369 static struct mdt_handler mdt_mds_ops[] = {
6370 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6371 DEF_MDT_HDL(0, MDS_DISCONNECT, mdt_disconnect),
6372 DEF_MDT_HDL(0, MDS_SET_INFO, mdt_set_info),
6373 DEF_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
6374 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
6375 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
6376 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME, mdt_getattr_name),
6377 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_getxattr),
6378 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
6379 DEF_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
6380 DEF_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
6381 DEF_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING, mdt_done_writing),
6382 DEF_MDT_HDL(0 | HABEO_REFERO, MDS_PIN, mdt_pin),
6383 DEF_MDT_HDL_VAR(0, MDS_SYNC, mdt_sync),
6384 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
6385 DEF_MDT_HDL(0, MDS_QUOTACHECK, mdt_quotacheck),
6386 DEF_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl)
6389 #define DEF_OBD_HDL(flags, name, fn) \
6390 DEFINE_RPC_HANDLER(OBD_PING, flags, name, fn, NULL)
6392 static struct mdt_handler mdt_obd_ops[] = {
6393 DEF_OBD_HDL(0, OBD_PING, mdt_obd_ping),
6394 DEF_OBD_HDL(0, OBD_LOG_CANCEL, mdt_obd_log_cancel),
6395 DEF_OBD_HDL(0, OBD_QC_CALLBACK, mdt_obd_qc_callback),
6396 DEF_OBD_HDL(0, OBD_IDX_READ, mdt_obd_idx_read)
6399 #define DEF_DLM_HDL_VAR(flags, name, fn) \
6400 DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, NULL)
6401 #define DEF_DLM_HDL(flags, name, fn) \
6402 DEFINE_RPC_HANDLER(LDLM_ENQUEUE, flags, name, fn, &RQF_ ## name)
6404 static struct mdt_handler mdt_dlm_ops[] = {
6405 DEF_DLM_HDL (HABEO_CLAVIS, LDLM_ENQUEUE, mdt_enqueue),
6406 DEF_DLM_HDL_VAR(HABEO_CLAVIS, LDLM_CONVERT, mdt_convert),
6407 DEF_DLM_HDL_VAR(0, LDLM_BL_CALLBACK, mdt_bl_callback),
6408 DEF_DLM_HDL_VAR(0, LDLM_CP_CALLBACK, mdt_cp_callback)
6411 #define DEF_LLOG_HDL(flags, name, fn) \
6412 DEFINE_RPC_HANDLER(LLOG_ORIGIN_HANDLE_CREATE, flags, name, fn, NULL)
6414 static struct mdt_handler mdt_llog_ops[] = {
6415 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_create),
6416 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, mdt_llog_next_block),
6417 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, mdt_llog_read_header),
6418 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_WRITE_REC, NULL),
6419 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CLOSE, NULL),
6420 DEF_LLOG_HDL(0, LLOG_ORIGIN_CONNECT, NULL),
6421 DEF_LLOG_HDL(0, LLOG_CATINFO, NULL),
6422 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, mdt_llog_prev_block),
6423 DEF_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_DESTROY, mdt_llog_destroy),
6426 #define DEF_SEC_HDL(flags, name, fn) \
6427 DEFINE_RPC_HANDLER(SEC_CTX_INIT, flags, name, fn, NULL)
6429 static struct mdt_handler mdt_sec_ctx_ops[] = {
6430 DEF_SEC_HDL(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
6431 DEF_SEC_HDL(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
6432 DEF_SEC_HDL(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
6435 #define DEF_QUOTA_HDL(flags, name, fn) \
6436 DEFINE_RPC_HANDLER(QUOTA_DQACQ, flags, name, fn, &RQF_ ## name)
6438 static struct mdt_handler mdt_quota_ops[] = {
6439 DEF_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
6442 static struct mdt_opc_slice mdt_regular_handlers[] = {
6444 .mos_opc_start = MDS_GETATTR,
6445 .mos_opc_end = MDS_LAST_OPC,
6446 .mos_hs = mdt_mds_ops
6449 .mos_opc_start = OBD_PING,
6450 .mos_opc_end = OBD_LAST_OPC,
6451 .mos_hs = mdt_obd_ops
6454 .mos_opc_start = LDLM_ENQUEUE,
6455 .mos_opc_end = LDLM_LAST_OPC,
6456 .mos_hs = mdt_dlm_ops
6459 .mos_opc_start = LLOG_ORIGIN_HANDLE_CREATE,
6460 .mos_opc_end = LLOG_LAST_OPC,
6461 .mos_hs = mdt_llog_ops
6464 .mos_opc_start = SEC_CTX_INIT,
6465 .mos_opc_end = SEC_LAST_OPC,
6466 .mos_hs = mdt_sec_ctx_ops
6469 .mos_opc_start = QUOTA_DQACQ,
6470 .mos_opc_end = QUOTA_LAST_OPC,
6471 .mos_hs = mdt_quota_ops
6478 /* Readpage/readdir handlers */
6479 static struct mdt_handler mdt_readpage_ops[] = {
6480 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6481 DEF_MDT_HDL(HABEO_CORPUS | HABEO_REFERO, MDS_READPAGE, mdt_readpage),
6482 /* XXX: this is ugly and should be fixed one day, see mdc_close() for
6483 * detailed comments. --umka */
6484 DEF_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
6485 DEF_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING, mdt_done_writing),
6488 static struct mdt_opc_slice mdt_readpage_handlers[] = {
6490 .mos_opc_start = MDS_GETATTR,
6491 .mos_opc_end = MDS_LAST_OPC,
6492 .mos_hs = mdt_readpage_ops
6495 .mos_opc_start = OBD_FIRST_OPC,
6496 .mos_opc_end = OBD_LAST_OPC,
6497 .mos_hs = mdt_obd_ops
6504 /* Cross MDT operation handlers for DNE */
6505 static struct mdt_handler mdt_xmds_ops[] = {
6506 DEF_MDT_HDL(0, MDS_CONNECT, mdt_connect),
6507 DEF_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
6508 DEF_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
6509 DEF_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
6512 static struct mdt_opc_slice mdt_xmds_handlers[] = {
6514 .mos_opc_start = MDS_GETATTR,
6515 .mos_opc_end = MDS_LAST_OPC,
6516 .mos_hs = mdt_xmds_ops
6519 .mos_opc_start = OBD_PING,
6520 .mos_opc_end = OBD_LAST_OPC,
6521 .mos_hs = mdt_obd_ops
6524 .mos_opc_start = SEC_CTX_INIT,
6525 .mos_opc_end = SEC_LAST_OPC,
6526 .mos_hs = mdt_sec_ctx_ops
6533 /* Sequence service handlers */
6534 #define DEF_SEQ_HDL(flags, name, fn) \
6535 DEFINE_RPC_HANDLER(SEQ_QUERY, flags, name, fn, &RQF_ ## name)
6537 static struct mdt_handler mdt_seq_ops[] = {
6538 DEF_SEQ_HDL(0, SEQ_QUERY, (void *)seq_query),
6541 static struct mdt_opc_slice mdt_seq_handlers[] = {
6543 .mos_opc_start = SEQ_QUERY,
6544 .mos_opc_end = SEQ_LAST_OPC,
6545 .mos_hs = mdt_seq_ops
6552 /* FID Location Database handlers */
6553 #define DEF_FLD_HDL(flags, name, fn) \
6554 DEFINE_RPC_HANDLER(FLD_QUERY, flags, name, fn, &RQF_ ## name)
6556 static struct mdt_handler mdt_fld_ops[] = {
6557 DEF_FLD_HDL(0, FLD_QUERY, (void *)fld_query),
6560 static struct mdt_opc_slice mdt_fld_handlers[] = {
6562 .mos_opc_start = FLD_QUERY,
6563 .mos_opc_end = FLD_LAST_OPC,
6564 .mos_hs = mdt_fld_ops
6571 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6572 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6573 MODULE_LICENSE("GPL");
6575 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);