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) 2010, 2013, Intel Corporation.
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
94 static struct mdt_device *mdt_dev(struct lu_device *d);
95 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
96 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
97 struct getinfo_fid2path *fp);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static cfs_mem_cache_t *mdt_object_kmem;
104 static struct lu_kmem_descr mdt_caches[] = {
106 .ckd_cache = &mdt_object_kmem,
107 .ckd_name = "mdt_obj",
108 .ckd_size = sizeof(struct mdt_object)
115 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
119 return (rep->lock_policy_res1 & flag);
122 void mdt_clear_disposition(struct mdt_thread_info *info,
123 struct ldlm_reply *rep, int flag)
126 info->mti_opdata &= ~flag;
128 rep->lock_policy_res1 &= ~flag;
131 void mdt_set_disposition(struct mdt_thread_info *info,
132 struct ldlm_reply *rep, int flag)
135 info->mti_opdata |= flag;
137 rep->lock_policy_res1 |= flag;
140 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
142 lh->mlh_pdo_hash = 0;
143 lh->mlh_reg_mode = lm;
144 lh->mlh_rreg_mode = lm;
145 lh->mlh_type = MDT_REG_LOCK;
148 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
149 const char *name, int namelen)
151 lh->mlh_reg_mode = lm;
152 lh->mlh_rreg_mode = lm;
153 lh->mlh_type = MDT_PDO_LOCK;
155 if (name != NULL && (name[0] != '\0')) {
156 LASSERT(namelen > 0);
157 lh->mlh_pdo_hash = full_name_hash(name, namelen);
159 LASSERT(namelen == 0);
160 lh->mlh_pdo_hash = 0ull;
164 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
165 struct mdt_lock_handle *lh)
171 * Any dir access needs couple of locks:
173 * 1) on part of dir we gonna take lookup/modify;
175 * 2) on whole dir to protect it from concurrent splitting and/or to
176 * flush client's cache for readdir().
178 * so, for a given mode and object this routine decides what lock mode
179 * to use for lock #2:
181 * 1) if caller's gonna lookup in dir then we need to protect dir from
182 * being splitted only - LCK_CR
184 * 2) if caller's gonna modify dir then we need to protect dir from
185 * being splitted and to flush cache - LCK_CW
187 * 3) if caller's gonna modify dir and that dir seems ready for
188 * splitting then we need to protect it from any type of access
189 * (lookup/modify/split) - LCK_EX --bzzz
192 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
193 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
196 * Ask underlaying level its opinion about preferable PDO lock mode
197 * having access type passed as regular lock mode:
199 * - MDL_MINMODE means that lower layer does not want to specify lock
202 * - MDL_NL means that no PDO lock should be taken. This is used in some
203 * cases. Say, for non-splittable directories no need to use PDO locks
206 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
207 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
209 if (mode != MDL_MINMODE) {
210 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
213 * Lower layer does not want to specify locking mode. We do it
214 * our selves. No special protection is needed, just flush
215 * client's cache on modification and allow concurrent
218 switch (lh->mlh_reg_mode) {
220 lh->mlh_pdo_mode = LCK_EX;
223 lh->mlh_pdo_mode = LCK_CR;
226 lh->mlh_pdo_mode = LCK_CW;
229 CERROR("Not expected lock type (0x%x)\n",
230 (int)lh->mlh_reg_mode);
235 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
239 int mdt_getstatus(struct mdt_thread_info *info)
241 struct mdt_device *mdt = info->mti_mdt;
242 struct md_device *next = mdt->mdt_child;
243 struct mdt_body *repbody;
248 rc = mdt_check_ucred(info);
250 RETURN(err_serious(rc));
252 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
253 RETURN(err_serious(-ENOMEM));
255 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
256 rc = next->md_ops->mdo_root_get(info->mti_env, next, &repbody->fid1);
260 repbody->valid |= OBD_MD_FLID;
262 if (mdt->mdt_opts.mo_mds_capa &&
263 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
264 struct mdt_object *root;
265 struct lustre_capa *capa;
267 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
269 RETURN(PTR_ERR(root));
271 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
273 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
274 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
276 mdt_object_put(info->mti_env, root);
278 repbody->valid |= OBD_MD_FLMDSCAPA;
284 int mdt_statfs(struct mdt_thread_info *info)
286 struct ptlrpc_request *req = mdt_info_req(info);
287 struct md_device *next = info->mti_mdt->mdt_child;
288 struct ptlrpc_service_part *svcpt;
289 struct obd_statfs *osfs;
294 svcpt = info->mti_pill->rc_req->rq_rqbd->rqbd_svcpt;
296 /* This will trigger a watchdog timeout */
297 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
298 (MDT_SERVICE_WATCHDOG_FACTOR *
299 at_get(&svcpt->scp_at_estimate)) + 1);
301 rc = mdt_check_ucred(info);
303 RETURN(err_serious(rc));
305 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
306 RETURN(err_serious(-ENOMEM));
308 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
312 /** statfs information are cached in the mdt_device */
313 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
314 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
315 /** statfs data is too old, get up-to-date one */
316 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
319 spin_lock(&info->mti_mdt->mdt_osfs_lock);
320 info->mti_mdt->mdt_osfs = *osfs;
321 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
322 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
324 /** use cached statfs data */
325 spin_lock(&info->mti_mdt->mdt_osfs_lock);
326 *osfs = info->mti_mdt->mdt_osfs;
327 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
331 mdt_counter_incr(req, LPROC_MDT_STATFS);
337 * Pack SOM attributes into the reply.
338 * Call under a DLM UPDATE lock.
340 static void mdt_pack_size2body(struct mdt_thread_info *info,
341 struct mdt_object *mo)
344 struct md_attr *ma = &info->mti_attr;
346 LASSERT(ma->ma_attr.la_valid & LA_MODE);
347 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
349 /* Check if Size-on-MDS is supported, if this is a regular file,
350 * if SOM is enabled on the object and if SOM cache exists and valid.
351 * Otherwise do not pack Size-on-MDS attributes to the reply. */
352 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
353 !S_ISREG(ma->ma_attr.la_mode) ||
354 !mdt_object_is_som_enabled(mo) ||
355 !(ma->ma_valid & MA_SOM))
358 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
359 b->size = ma->ma_som->msd_size;
360 b->blocks = ma->ma_som->msd_blocks;
363 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
364 const struct lu_attr *attr, const struct lu_fid *fid)
366 struct md_attr *ma = &info->mti_attr;
368 LASSERT(ma->ma_valid & MA_INODE);
370 b->atime = attr->la_atime;
371 b->mtime = attr->la_mtime;
372 b->ctime = attr->la_ctime;
373 b->mode = attr->la_mode;
374 b->size = attr->la_size;
375 b->blocks = attr->la_blocks;
376 b->uid = attr->la_uid;
377 b->gid = attr->la_gid;
378 b->flags = attr->la_flags;
379 b->nlink = attr->la_nlink;
380 b->rdev = attr->la_rdev;
382 /*XXX should pack the reply body according to lu_valid*/
383 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
384 OBD_MD_FLGID | OBD_MD_FLTYPE |
385 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
386 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
388 if (!S_ISREG(attr->la_mode)) {
389 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
390 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
391 /* means no objects are allocated on osts. */
392 LASSERT(!(ma->ma_valid & MA_LOV));
393 /* just ignore blocks occupied by extend attributes on MDS */
395 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
396 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
401 b->valid |= OBD_MD_FLID;
402 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
403 PFID(fid), b->nlink, b->mode, b->size);
407 mdt_body_reverse_idmap(info, b);
409 if (b->valid & OBD_MD_FLSIZE)
410 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
411 PFID(fid), (unsigned long long)b->size);
414 static inline int mdt_body_has_lov(const struct lu_attr *la,
415 const struct mdt_body *body)
417 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
418 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
421 void mdt_client_compatibility(struct mdt_thread_info *info)
423 struct mdt_body *body;
424 struct ptlrpc_request *req = mdt_info_req(info);
425 struct obd_export *exp = req->rq_export;
426 struct md_attr *ma = &info->mti_attr;
427 struct lu_attr *la = &ma->ma_attr;
430 if (exp_connect_layout(exp))
431 /* the client can deal with 16-bit lmm_stripe_count */
434 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
436 if (!mdt_body_has_lov(la, body))
439 /* now we have a reply with a lov for a client not compatible with the
440 * layout lock so we have to clean the layout generation number */
441 if (S_ISREG(la->la_mode))
442 ma->ma_lmm->lmm_layout_gen = 0;
446 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
449 const struct lu_env *env = info->mti_env;
453 LASSERT(info->mti_big_lmm_used == 0);
454 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
458 /* big_lmm may need to be grown */
459 if (info->mti_big_lmmsize < rc) {
460 int size = size_roundup_power2(rc);
462 if (info->mti_big_lmmsize > 0) {
463 /* free old buffer */
464 LASSERT(info->mti_big_lmm);
465 OBD_FREE_LARGE(info->mti_big_lmm,
466 info->mti_big_lmmsize);
467 info->mti_big_lmm = NULL;
468 info->mti_big_lmmsize = 0;
471 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
472 if (info->mti_big_lmm == NULL)
474 info->mti_big_lmmsize = size;
476 LASSERT(info->mti_big_lmmsize >= rc);
478 info->mti_buf.lb_buf = info->mti_big_lmm;
479 info->mti_buf.lb_len = info->mti_big_lmmsize;
480 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
485 int mdt_attr_get_lov(struct mdt_thread_info *info,
486 struct mdt_object *o, struct md_attr *ma)
488 struct md_object *next = mdt_object_child(o);
489 struct lu_buf *buf = &info->mti_buf;
492 buf->lb_buf = ma->ma_lmm;
493 buf->lb_len = ma->ma_lmm_size;
494 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
496 ma->ma_lmm_size = rc;
497 ma->ma_valid |= MA_LOV;
499 } else if (rc == -ENODATA) {
502 } else if (rc == -ERANGE) {
503 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
505 info->mti_big_lmm_used = 1;
506 ma->ma_valid |= MA_LOV;
507 ma->ma_lmm = info->mti_big_lmm;
508 ma->ma_lmm_size = rc;
509 /* update mdt_max_mdsize so all clients
510 * will be aware about that */
511 if (info->mti_mdt->mdt_max_mdsize < rc)
512 info->mti_mdt->mdt_max_mdsize = rc;
520 int mdt_attr_get_pfid(struct mdt_thread_info *info,
521 struct mdt_object *o, struct lu_fid *pfid)
523 struct lu_buf *buf = &info->mti_buf;
524 struct link_ea_header *leh;
525 struct link_ea_entry *lee;
529 buf->lb_buf = info->mti_big_lmm;
530 buf->lb_len = info->mti_big_lmmsize;
531 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
532 buf, XATTR_NAME_LINK);
533 /* ignore errors, MA_PFID won't be set and it is
534 * up to the caller to treat this as an error */
535 if (rc == -ERANGE || buf->lb_len == 0) {
536 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
537 buf->lb_buf = info->mti_big_lmm;
538 buf->lb_len = info->mti_big_lmmsize;
543 if (rc < sizeof(*leh)) {
544 CERROR("short LinkEA on "DFID": rc = %d\n",
545 PFID(mdt_object_fid(o)), rc);
549 leh = (struct link_ea_header *) buf->lb_buf;
550 lee = (struct link_ea_entry *)(leh + 1);
551 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
552 leh->leh_magic = LINK_EA_MAGIC;
553 leh->leh_reccount = __swab32(leh->leh_reccount);
554 leh->leh_len = __swab64(leh->leh_len);
556 if (leh->leh_magic != LINK_EA_MAGIC)
558 if (leh->leh_reccount == 0)
561 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
562 fid_be_to_cpu(pfid, pfid);
567 int mdt_attr_get_complex(struct mdt_thread_info *info,
568 struct mdt_object *o, struct md_attr *ma)
570 const struct lu_env *env = info->mti_env;
571 struct md_object *next = mdt_object_child(o);
572 struct lu_buf *buf = &info->mti_buf;
573 u32 mode = lu_object_attr(&next->mo_lu);
574 int need = ma->ma_need;
580 if (need & MA_INODE) {
581 ma->ma_need = MA_INODE;
582 rc = mo_attr_get(env, next, ma);
585 ma->ma_valid |= MA_INODE;
588 if (need & MA_PFID) {
589 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
591 ma->ma_valid |= MA_PFID;
592 /* ignore this error, parent fid is not mandatory */
596 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
597 rc = mdt_attr_get_lov(info, o, ma);
602 if (need & MA_LMV && S_ISDIR(mode)) {
603 buf->lb_buf = ma->ma_lmv;
604 buf->lb_len = ma->ma_lmv_size;
605 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
607 ma->ma_lmv_size = rc2;
608 ma->ma_valid |= MA_LMV;
609 } else if (rc2 == -ENODATA) {
616 if (need & MA_SOM && S_ISREG(mode)) {
617 buf->lb_buf = info->mti_xattr_buf;
618 buf->lb_len = sizeof(info->mti_xattr_buf);
619 CLASSERT(sizeof(struct som_attrs) <=
620 sizeof(info->mti_xattr_buf));
621 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
622 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
624 ma->ma_valid |= MA_SOM;
625 else if (rc2 < 0 && rc2 != -ENODATA)
629 if (need & MA_HSM && S_ISREG(mode)) {
630 buf->lb_buf = info->mti_xattr_buf;
631 buf->lb_len = sizeof(info->mti_xattr_buf);
632 CLASSERT(sizeof(struct hsm_attrs) <=
633 sizeof(info->mti_xattr_buf));
634 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
635 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
637 ma->ma_valid |= MA_HSM;
638 else if (rc2 < 0 && rc2 != -ENODATA)
642 #ifdef CONFIG_FS_POSIX_ACL
643 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
644 buf->lb_buf = ma->ma_acl;
645 buf->lb_len = ma->ma_acl_size;
646 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
648 ma->ma_acl_size = rc2;
649 ma->ma_valid |= MA_ACL_DEF;
650 } else if (rc2 == -ENODATA) {
659 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
660 rc, ma->ma_valid, ma->ma_lmm);
664 static int mdt_getattr_internal(struct mdt_thread_info *info,
665 struct mdt_object *o, int ma_need)
667 struct md_object *next = mdt_object_child(o);
668 const struct mdt_body *reqbody = info->mti_body;
669 struct ptlrpc_request *req = mdt_info_req(info);
670 struct md_attr *ma = &info->mti_attr;
671 struct lu_attr *la = &ma->ma_attr;
672 struct req_capsule *pill = info->mti_pill;
673 const struct lu_env *env = info->mti_env;
674 struct mdt_body *repbody;
675 struct lu_buf *buffer = &info->mti_buf;
680 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
681 RETURN(err_serious(-ENOMEM));
683 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
687 if (mdt_object_remote(o)) {
688 /* This object is located on remote node.*/
689 repbody->fid1 = *mdt_object_fid(o);
690 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
694 buffer->lb_len = reqbody->eadatasize;
695 if (buffer->lb_len > 0)
696 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
698 buffer->lb_buf = NULL;
700 /* If it is dir object and client require MEA, then we got MEA */
701 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
702 reqbody->valid & OBD_MD_MEA) {
703 /* Assumption: MDT_MD size is enough for lmv size. */
704 ma->ma_lmv = buffer->lb_buf;
705 ma->ma_lmv_size = buffer->lb_len;
706 ma->ma_need = MA_LMV | MA_INODE;
708 ma->ma_lmm = buffer->lb_buf;
709 ma->ma_lmm_size = buffer->lb_len;
710 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
713 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
714 reqbody->valid & OBD_MD_FLDIREA &&
715 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
716 /* get default stripe info for this dir. */
717 ma->ma_need |= MA_LOV_DEF;
719 ma->ma_need |= ma_need;
720 if (ma->ma_need & MA_SOM)
721 ma->ma_som = &info->mti_u.som.data;
723 rc = mdt_attr_get_complex(info, o, ma);
725 CERROR("getattr error for "DFID": %d\n",
726 PFID(mdt_object_fid(o)), rc);
730 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
732 /* the Lustre protocol supposes to return default striping
733 * on the user-visible root if explicitly requested */
734 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
735 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
736 struct lu_fid rootfid;
737 struct mdt_object *root;
738 struct mdt_device *mdt = info->mti_mdt;
740 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
743 root = mdt_object_find(env, mdt, &rootfid);
745 RETURN(PTR_ERR(root));
746 rc = mdt_attr_get_lov(info, root, ma);
747 mdt_object_put(info->mti_env, root);
749 CERROR("getattr error for "DFID": %d\n",
750 PFID(mdt_object_fid(o)), rc);
755 if (likely(ma->ma_valid & MA_INODE))
756 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
760 if (mdt_body_has_lov(la, reqbody)) {
761 if (ma->ma_valid & MA_LOV) {
762 LASSERT(ma->ma_lmm_size);
763 mdt_dump_lmm(D_INFO, ma->ma_lmm);
764 repbody->eadatasize = ma->ma_lmm_size;
765 if (S_ISDIR(la->la_mode))
766 repbody->valid |= OBD_MD_FLDIREA;
768 repbody->valid |= OBD_MD_FLEASIZE;
770 if (ma->ma_valid & MA_LMV) {
771 LASSERT(S_ISDIR(la->la_mode));
772 repbody->eadatasize = ma->ma_lmv_size;
773 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
775 } else if (S_ISLNK(la->la_mode) &&
776 reqbody->valid & OBD_MD_LINKNAME) {
777 buffer->lb_buf = ma->ma_lmm;
778 /* eadatasize from client includes NULL-terminator, so
779 * there is no need to read it */
780 buffer->lb_len = reqbody->eadatasize - 1;
781 rc = mo_readlink(env, next, buffer);
782 if (unlikely(rc <= 0)) {
783 CERROR("readlink failed: %d\n", rc);
786 int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
788 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
790 repbody->valid |= OBD_MD_LINKNAME;
791 /* we need to report back size with NULL-terminator
792 * because client expects that */
793 repbody->eadatasize = rc + 1;
794 if (repbody->eadatasize != reqbody->eadatasize)
795 CERROR("Read shorter symlink %d, expected %d\n",
796 rc, reqbody->eadatasize - 1);
798 ((char *)ma->ma_lmm)[rc] = 0;
800 /* If the total CDEBUG() size is larger than a page, it
801 * will print a warning to the console, avoid this by
802 * printing just the last part of the symlink. */
803 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
804 print_limit < rc ? "..." : "", print_limit,
805 (char *)ma->ma_lmm + rc - print_limit, rc);
810 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
811 repbody->max_cookiesize = 0;
812 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
813 repbody->valid |= OBD_MD_FLMODEASIZE;
814 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
815 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
816 repbody->max_cookiesize);
819 if (exp_connect_rmtclient(info->mti_exp) &&
820 reqbody->valid & OBD_MD_FLRMTPERM) {
821 void *buf = req_capsule_server_get(pill, &RMF_ACL);
823 /* mdt_getattr_lock only */
824 rc = mdt_pack_remote_perm(info, o, buf);
826 repbody->valid &= ~OBD_MD_FLRMTPERM;
827 repbody->aclsize = 0;
830 repbody->valid |= OBD_MD_FLRMTPERM;
831 repbody->aclsize = sizeof(struct mdt_remote_perm);
834 #ifdef CONFIG_FS_POSIX_ACL
835 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
836 (reqbody->valid & OBD_MD_FLACL)) {
837 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
838 buffer->lb_len = req_capsule_get_size(pill,
839 &RMF_ACL, RCL_SERVER);
840 if (buffer->lb_len > 0) {
841 rc = mo_xattr_get(env, next, buffer,
842 XATTR_NAME_ACL_ACCESS);
844 if (rc == -ENODATA) {
845 repbody->aclsize = 0;
846 repbody->valid |= OBD_MD_FLACL;
848 } else if (rc == -EOPNOTSUPP) {
851 CERROR("got acl size: %d\n", rc);
854 repbody->aclsize = rc;
855 repbody->valid |= OBD_MD_FLACL;
862 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
863 info->mti_mdt->mdt_opts.mo_mds_capa &&
864 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
865 struct lustre_capa *capa;
867 capa = req_capsule_server_get(pill, &RMF_CAPA1);
869 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
870 rc = mo_capa_get(env, next, capa, 0);
873 repbody->valid |= OBD_MD_FLMDSCAPA;
878 mdt_counter_incr(req, LPROC_MDT_GETATTR);
883 static int mdt_renew_capa(struct mdt_thread_info *info)
885 struct mdt_object *obj = info->mti_object;
886 struct mdt_body *body;
887 struct lustre_capa *capa, *c;
891 /* if object doesn't exist, or server has disabled capability,
892 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
895 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
896 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
899 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
900 LASSERT(body != NULL);
902 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
905 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
909 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
911 body->valid |= OBD_MD_FLOSSCAPA;
915 int mdt_getattr(struct mdt_thread_info *info)
917 struct mdt_object *obj = info->mti_object;
918 struct req_capsule *pill = info->mti_pill;
919 struct mdt_body *reqbody;
920 struct mdt_body *repbody;
925 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
928 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
929 rc = req_capsule_server_pack(pill);
931 RETURN(err_serious(rc));
932 rc = mdt_renew_capa(info);
933 GOTO(out_shrink, rc);
936 LASSERT(obj != NULL);
937 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
939 mode = lu_object_attr(&obj->mot_obj.mo_lu);
941 /* old clients may not report needed easize, use max value then */
942 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
943 reqbody->eadatasize == 0 ?
944 info->mti_mdt->mdt_max_mdsize :
945 reqbody->eadatasize);
947 rc = req_capsule_server_pack(pill);
948 if (unlikely(rc != 0))
949 RETURN(err_serious(rc));
951 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
952 LASSERT(repbody != NULL);
953 repbody->eadatasize = 0;
954 repbody->aclsize = 0;
956 if (reqbody->valid & OBD_MD_FLRMTPERM)
957 rc = mdt_init_ucred(info, reqbody);
959 rc = mdt_check_ucred(info);
961 GOTO(out_shrink, rc);
963 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
966 * Don't check capability at all, because rename might getattr for
967 * remote obj, and at that time no capability is available.
969 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
970 rc = mdt_getattr_internal(info, obj, 0);
971 if (reqbody->valid & OBD_MD_FLRMTPERM)
972 mdt_exit_ucred(info);
975 mdt_client_compatibility(info);
976 rc2 = mdt_fix_reply(info);
982 int mdt_is_subdir(struct mdt_thread_info *info)
984 struct mdt_object *o = info->mti_object;
985 struct req_capsule *pill = info->mti_pill;
986 const struct mdt_body *body = info->mti_body;
987 struct mdt_body *repbody;
993 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
996 * We save last checked parent fid to @repbody->fid1 for remote
999 LASSERT(fid_is_sane(&body->fid2));
1000 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1001 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1002 &body->fid2, &repbody->fid1);
1003 if (rc == 0 || rc == -EREMOTE)
1004 repbody->valid |= OBD_MD_FLID;
1009 int mdt_swap_layouts(struct mdt_thread_info *info)
1011 struct ptlrpc_request *req = mdt_info_req(info);
1012 struct obd_export *exp = req->rq_export;
1013 struct mdt_object *o1, *o2, *o;
1014 struct mdt_lock_handle *lh1, *lh2;
1015 struct mdc_swap_layouts *msl;
1019 /* client does not support layout lock, so layout swaping
1021 * FIXME: there is a problem for old clients which don't support
1022 * layout lock yet. If those clients have already opened the file
1023 * they won't be notified at all so that old layout may still be
1024 * used to do IO. This can be fixed after file release is landed by
1025 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1026 if (!exp_connect_layout(exp))
1027 RETURN(-EOPNOTSUPP);
1029 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1030 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1031 req_capsule_client_get(info->mti_pill,
1034 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1035 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1036 req_capsule_client_get(info->mti_pill,
1039 o1 = info->mti_object;
1040 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1041 &info->mti_body->fid2);
1043 GOTO(out, rc = PTR_ERR(o));
1045 if (mdt_object_exists(o) < 0) /* remote object */
1046 GOTO(put, rc = -ENOENT);
1048 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1049 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1055 /* permission check. Make sure the calling process having permission
1056 * to write both files. */
1057 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1062 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1067 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1068 LASSERT(msl != NULL);
1070 lh1 = &info->mti_lh[MDT_LH_NEW];
1071 mdt_lock_reg_init(lh1, LCK_EX);
1072 lh2 = &info->mti_lh[MDT_LH_OLD];
1073 mdt_lock_reg_init(lh2, LCK_EX);
1075 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT,
1080 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT,
1085 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1086 mdt_object_child(o2), msl->msl_flags);
1089 mdt_object_unlock(info, o2, lh2, rc);
1091 mdt_object_unlock(info, o1, lh1, rc);
1093 mdt_object_put(info->mti_env, o);
1098 static int mdt_raw_lookup(struct mdt_thread_info *info,
1099 struct mdt_object *parent,
1100 const struct lu_name *lname,
1101 struct ldlm_reply *ldlm_rep)
1103 struct md_object *next = mdt_object_child(info->mti_object);
1104 const struct mdt_body *reqbody = info->mti_body;
1105 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1106 struct mdt_body *repbody;
1110 if (reqbody->valid != OBD_MD_FLID)
1113 LASSERT(!info->mti_cross_ref);
1115 /* Only got the fid of this obj by name */
1116 fid_zero(child_fid);
1117 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1120 /* XXX is raw_lookup possible as intent operation? */
1123 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1126 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1128 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1131 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1132 repbody->fid1 = *child_fid;
1133 repbody->valid = OBD_MD_FLID;
1139 * UPDATE lock should be taken against parent, and be release before exit;
1140 * child_bits lock should be taken against child, and be returned back:
1141 * (1)normal request should release the child lock;
1142 * (2)intent request will grant the lock to client.
1144 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1145 struct mdt_lock_handle *lhc,
1147 struct ldlm_reply *ldlm_rep)
1149 struct ptlrpc_request *req = mdt_info_req(info);
1150 struct mdt_body *reqbody = NULL;
1151 struct mdt_object *parent = info->mti_object;
1152 struct mdt_object *child;
1153 struct md_object *next = mdt_object_child(parent);
1154 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1155 struct lu_name *lname = NULL;
1156 const char *name = NULL;
1158 struct mdt_lock_handle *lhp = NULL;
1159 struct ldlm_lock *lock;
1160 struct ldlm_res_id *res_id;
1167 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1168 LASSERT(ergo(is_resent,
1169 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1171 LASSERT(parent != NULL);
1172 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1174 RETURN(err_serious(-EFAULT));
1176 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1178 if (!info->mti_cross_ref) {
1180 * XXX: Check for "namelen == 0" is for getattr by fid
1181 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1182 * that is the name must contain at least one character and
1183 * the terminating '\0'
1186 reqbody = req_capsule_client_get(info->mti_pill,
1188 if (unlikely(reqbody == NULL))
1189 RETURN(err_serious(-EFAULT));
1191 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1192 RETURN(err_serious(-EINVAL));
1195 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1197 PFID(mdt_object_fid(parent)),
1198 PFID(&reqbody->fid2), ldlm_rep);
1200 lname = mdt_name(info->mti_env, (char *)name, namelen);
1201 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1202 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1206 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1208 if (unlikely(!mdt_object_exists(parent))) {
1209 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1210 &parent->mot_obj.mo_lu,
1211 "Parent doesn't exist!\n");
1213 } else if (!info->mti_cross_ref) {
1214 LASSERTF(!mdt_object_remote(parent),
1215 "Parent "DFID" is on remote server\n",
1216 PFID(mdt_object_fid(parent)));
1219 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1227 if (info->mti_cross_ref) {
1228 /* Only getattr on the child. Parent is on another node. */
1229 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1231 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1232 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1235 /* Do not take lock for resent case. */
1236 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1237 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1238 lhc->mlh_reg_lh.cookie);
1239 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1240 &lock->l_resource->lr_name));
1241 LDLM_LOCK_PUT(lock);
1244 mdt_lock_handle_init(lhc);
1245 mdt_lock_reg_init(lhc, LCK_PR);
1248 * Object's name is on another MDS, no lookup lock is
1249 * needed here but update is.
1251 child_bits &= ~MDS_INODELOCK_LOOKUP;
1252 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1254 rc = mdt_object_lock(info, child, lhc, child_bits,
1258 /* Finally, we can get attr for child. */
1259 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1261 rc = mdt_getattr_internal(info, child, 0);
1262 if (unlikely(rc != 0))
1263 mdt_object_unlock(info, child, lhc, 1);
1269 /* step 1: lock parent only if parent is a directory */
1270 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1271 lhp = &info->mti_lh[MDT_LH_PARENT];
1272 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1273 rc = mdt_object_lock(info, parent, lhp,
1274 MDS_INODELOCK_UPDATE,
1276 if (unlikely(rc != 0))
1280 /* step 2: lookup child's fid by name */
1281 fid_zero(child_fid);
1282 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1287 mdt_set_disposition(info, ldlm_rep,
1289 GOTO(out_parent, rc);
1291 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1293 *child_fid = reqbody->fid2;
1294 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1298 *step 3: find the child object by fid & lock it.
1299 * regardless if it is local or remote.
1301 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1303 if (unlikely(IS_ERR(child)))
1304 GOTO(out_parent, rc = PTR_ERR(child));
1306 /* Do not take lock for resent case. */
1307 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1308 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1309 lhc->mlh_reg_lh.cookie);
1311 res_id = &lock->l_resource->lr_name;
1312 if (!fid_res_name_eq(mdt_object_fid(child),
1313 &lock->l_resource->lr_name)) {
1314 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1315 &lock->l_resource->lr_name),
1316 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1317 (unsigned long)res_id->name[0],
1318 (unsigned long)res_id->name[1],
1319 (unsigned long)res_id->name[2],
1320 PFID(mdt_object_fid(parent)));
1321 CWARN("Although resent, but still not get child lock"
1322 "parent:"DFID" child:"DFID"\n",
1323 PFID(mdt_object_fid(parent)),
1324 PFID(mdt_object_fid(child)));
1325 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1326 LDLM_LOCK_PUT(lock);
1329 LDLM_LOCK_PUT(lock);
1332 bool try_layout = false;
1335 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1336 mdt_lock_handle_init(lhc);
1337 mdt_lock_reg_init(lhc, LCK_PR);
1339 if (!mdt_object_exists(child)) {
1340 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1341 &child->mot_obj.mo_lu,
1342 "Object doesn't exist!\n");
1343 GOTO(out_child, rc = -ENOENT);
1346 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1347 mdt_object_exists(child) && !mdt_object_remote(child)) {
1348 struct md_attr *ma = &info->mti_attr;
1351 ma->ma_need = MA_INODE;
1352 rc = mdt_attr_get_complex(info, child, ma);
1353 if (unlikely(rc != 0))
1354 GOTO(out_child, rc);
1356 /* If the file has not been changed for some time, we
1357 * return not only a LOOKUP lock, but also an UPDATE
1358 * lock and this might save us RPC on later STAT. For
1359 * directories, it also let negative dentry starts
1360 * working for this dir. */
1361 if (ma->ma_valid & MA_INODE &&
1362 ma->ma_attr.la_valid & LA_CTIME &&
1363 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1364 ma->ma_attr.la_ctime < cfs_time_current_sec())
1365 child_bits |= MDS_INODELOCK_UPDATE;
1368 /* layout lock must be granted in a best-effort way
1369 * for IT operations */
1370 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1371 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1372 exp_connect_layout(info->mti_exp) &&
1373 S_ISREG(lu_object_attr(&child->mot_obj.mo_lu)) &&
1375 /* try to grant layout lock for regular file. */
1381 child_bits |= MDS_INODELOCK_LAYOUT;
1382 /* try layout lock, it may fail to be granted due to
1383 * contention at LOOKUP or UPDATE */
1384 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1386 child_bits &= ~MDS_INODELOCK_LAYOUT;
1387 LASSERT(child_bits != 0);
1388 rc = mdt_object_lock(info, child, lhc,
1389 child_bits, MDT_CROSS_LOCK);
1394 rc = mdt_object_lock(info, child, lhc, child_bits,
1397 if (unlikely(rc != 0))
1398 GOTO(out_child, rc);
1401 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1402 /* Get MA_SOM attributes if update lock is given. */
1404 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1405 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1408 /* finally, we can get attr for child. */
1409 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1410 rc = mdt_getattr_internal(info, child, ma_need);
1411 if (unlikely(rc != 0)) {
1412 mdt_object_unlock(info, child, lhc, 1);
1414 /* Debugging code. */
1415 res_id = &lock->l_resource->lr_name;
1416 LDLM_DEBUG(lock, "Returning lock to client");
1417 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1418 &lock->l_resource->lr_name),
1419 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1420 (unsigned long)res_id->name[0],
1421 (unsigned long)res_id->name[1],
1422 (unsigned long)res_id->name[2],
1423 PFID(mdt_object_fid(child)));
1424 if (mdt_object_exists(child) && !mdt_object_remote(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 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 int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1477 void *karg, void *uarg);
1479 int mdt_set_info(struct mdt_thread_info *info)
1481 struct ptlrpc_request *req = mdt_info_req(info);
1484 int keylen, vallen, rc = 0;
1487 rc = req_capsule_server_pack(info->mti_pill);
1491 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1493 DEBUG_REQ(D_HA, req, "no set_info key");
1497 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1500 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1502 DEBUG_REQ(D_HA, req, "no set_info val");
1506 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1509 /* Swab any part of val you need to here */
1510 if (KEY_IS(KEY_READ_ONLY)) {
1512 lustre_msg_set_status(req->rq_repmsg, 0);
1514 spin_lock(&req->rq_export->exp_lock);
1516 *exp_connect_flags_ptr(req->rq_export) |=
1519 *exp_connect_flags_ptr(req->rq_export) &=
1520 ~OBD_CONNECT_RDONLY;
1521 spin_unlock(&req->rq_export->exp_lock);
1523 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1524 struct changelog_setinfo *cs =
1525 (struct changelog_setinfo *)val;
1526 if (vallen != sizeof(*cs)) {
1527 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1530 if (ptlrpc_req_need_swab(req)) {
1531 __swab64s(&cs->cs_recno);
1532 __swab32s(&cs->cs_id);
1535 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1537 lustre_msg_set_status(req->rq_repmsg, rc);
1546 * Top-level handler for MDT connection requests.
1548 int mdt_connect(struct mdt_thread_info *info)
1551 struct obd_connect_data *reply;
1552 struct obd_export *exp;
1553 struct ptlrpc_request *req = mdt_info_req(info);
1555 rc = target_handle_connect(req);
1557 return err_serious(rc);
1559 LASSERT(req->rq_export != NULL);
1560 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1561 rc = mdt_init_sec_level(info);
1563 obd_disconnect(class_export_get(req->rq_export));
1567 /* To avoid exposing partially initialized connection flags, changes up
1568 * to this point have been staged in reply->ocd_connect_flags. Now that
1569 * connection handling has completed successfully, atomically update
1570 * the connect flags in the shared export data structure. LU-1623 */
1571 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1572 exp = req->rq_export;
1573 spin_lock(&exp->exp_lock);
1574 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1575 spin_unlock(&exp->exp_lock);
1577 rc = mdt_init_idmap(info);
1579 obd_disconnect(class_export_get(req->rq_export));
1584 int mdt_disconnect(struct mdt_thread_info *info)
1589 rc = target_handle_disconnect(mdt_info_req(info));
1591 rc = err_serious(rc);
1595 static int mdt_sendpage(struct mdt_thread_info *info,
1596 struct lu_rdpg *rdpg, int nob)
1598 struct ptlrpc_request *req = mdt_info_req(info);
1599 struct obd_export *exp = req->rq_export;
1600 struct ptlrpc_bulk_desc *desc;
1601 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1608 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1613 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1614 /* old client requires reply size in it's PAGE_SIZE,
1615 * which is rdpg->rp_count */
1616 nob = rdpg->rp_count;
1618 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1619 i++, tmpcount -= tmpsize) {
1620 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1621 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1624 LASSERT(desc->bd_nob == nob);
1625 rc = target_bulk_io(exp, desc, lwi);
1626 ptlrpc_free_bulk_pin(desc);
1630 int mdt_readpage(struct mdt_thread_info *info)
1632 struct mdt_object *object = info->mti_object;
1633 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1634 struct mdt_body *reqbody;
1635 struct mdt_body *repbody;
1640 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1641 RETURN(err_serious(-ENOMEM));
1643 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1644 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1645 if (reqbody == NULL || repbody == NULL)
1646 RETURN(err_serious(-EFAULT));
1649 * prepare @rdpg before calling lower layers and transfer itself. Here
1650 * reqbody->size contains offset of where to start to read and
1651 * reqbody->nlink contains number bytes to read.
1653 rdpg->rp_hash = reqbody->size;
1654 if (rdpg->rp_hash != reqbody->size) {
1655 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1656 rdpg->rp_hash, reqbody->size);
1660 rdpg->rp_attrs = reqbody->mode;
1661 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1662 rdpg->rp_attrs |= LUDA_64BITHASH;
1663 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1664 exp_max_brw_size(info->mti_exp));
1665 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1667 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1668 if (rdpg->rp_pages == NULL)
1671 for (i = 0; i < rdpg->rp_npages; ++i) {
1672 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1673 if (rdpg->rp_pages[i] == NULL)
1674 GOTO(free_rdpg, rc = -ENOMEM);
1677 /* call lower layers to fill allocated pages with directory data */
1678 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1680 GOTO(free_rdpg, rc);
1682 /* send pages to client */
1683 rc = mdt_sendpage(info, rdpg, rc);
1688 for (i = 0; i < rdpg->rp_npages; i++)
1689 if (rdpg->rp_pages[i] != NULL)
1690 cfs_free_page(rdpg->rp_pages[i]);
1691 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1693 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1699 static int mdt_reint_internal(struct mdt_thread_info *info,
1700 struct mdt_lock_handle *lhc,
1703 struct req_capsule *pill = info->mti_pill;
1704 struct mdt_body *repbody;
1709 rc = mdt_reint_unpack(info, op);
1711 CERROR("Can't unpack reint, rc %d\n", rc);
1712 RETURN(err_serious(rc));
1715 /* for replay (no_create) lmm is not needed, client has it already */
1716 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1717 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1718 info->mti_rr.rr_eadatalen);
1720 /* llog cookies are always 0, the field is kept for compatibility */
1721 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1722 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1724 rc = req_capsule_server_pack(pill);
1726 CERROR("Can't pack response, rc %d\n", rc);
1727 RETURN(err_serious(rc));
1730 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1731 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1733 repbody->eadatasize = 0;
1734 repbody->aclsize = 0;
1737 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1739 /* for replay no cookkie / lmm need, because client have this already */
1740 if (info->mti_spec.no_create)
1741 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1742 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1744 rc = mdt_init_ucred_reint(info);
1746 GOTO(out_shrink, rc);
1748 rc = mdt_fix_attr_ucred(info, op);
1750 GOTO(out_ucred, rc = err_serious(rc));
1752 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1753 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1754 GOTO(out_ucred, rc);
1756 rc = mdt_reint_rec(info, lhc);
1759 mdt_exit_ucred(info);
1761 mdt_client_compatibility(info);
1762 rc2 = mdt_fix_reply(info);
1768 static long mdt_reint_opcode(struct mdt_thread_info *info,
1769 const struct req_format **fmt)
1771 struct mdt_rec_reint *rec;
1774 opc = err_serious(-EFAULT);
1775 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1777 opc = rec->rr_opcode;
1778 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1779 if (opc < REINT_MAX && fmt[opc] != NULL)
1780 req_capsule_extend(info->mti_pill, fmt[opc]);
1782 CERROR("Unsupported opc: %ld\n", opc);
1783 opc = err_serious(opc);
1789 int mdt_reint(struct mdt_thread_info *info)
1794 static const struct req_format *reint_fmts[REINT_MAX] = {
1795 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1796 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1797 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1798 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1799 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1800 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1801 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1802 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1807 opc = mdt_reint_opcode(info, reint_fmts);
1810 * No lock possible here from client to pass it to reint code
1813 rc = mdt_reint_internal(info, NULL, opc);
1818 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1822 /* this should sync the whole device */
1823 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1825 struct dt_device *dt = mdt->mdt_bottom;
1829 rc = dt->dd_ops->dt_sync(env, dt);
1833 /* this should sync this object */
1834 static int mdt_object_sync(struct mdt_thread_info *info)
1836 struct md_object *next;
1840 if (!mdt_object_exists(info->mti_object)) {
1841 CWARN("Non existing object "DFID"!\n",
1842 PFID(mdt_object_fid(info->mti_object)));
1845 next = mdt_object_child(info->mti_object);
1846 rc = mo_object_sync(info->mti_env, next);
1851 int mdt_sync(struct mdt_thread_info *info)
1853 struct ptlrpc_request *req = mdt_info_req(info);
1854 struct req_capsule *pill = info->mti_pill;
1855 struct mdt_body *body;
1859 /* The fid may be zero, so we req_capsule_set manually */
1860 req_capsule_set(pill, &RQF_MDS_SYNC);
1862 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1864 RETURN(err_serious(-EINVAL));
1866 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1867 RETURN(err_serious(-ENOMEM));
1869 if (fid_seq(&body->fid1) == 0) {
1870 /* sync the whole device */
1871 rc = req_capsule_server_pack(pill);
1873 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1875 rc = err_serious(rc);
1877 /* sync an object */
1878 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1880 rc = mdt_object_sync(info);
1882 const struct lu_fid *fid;
1883 struct lu_attr *la = &info->mti_attr.ma_attr;
1885 info->mti_attr.ma_need = MA_INODE;
1886 info->mti_attr.ma_valid = 0;
1887 rc = mdt_attr_get_complex(info, info->mti_object,
1890 body = req_capsule_server_get(pill,
1892 fid = mdt_object_fid(info->mti_object);
1893 mdt_pack_attr2body(info, body, la, fid);
1897 rc = err_serious(rc);
1900 mdt_counter_incr(req, LPROC_MDT_SYNC);
1906 * Quotacheck handler.
1907 * in-kernel quotacheck isn't supported any more.
1909 int mdt_quotacheck(struct mdt_thread_info *info)
1911 struct obd_quotactl *oqctl;
1915 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1917 RETURN(err_serious(-EPROTO));
1919 rc = req_capsule_server_pack(info->mti_pill);
1921 RETURN(err_serious(rc));
1923 /* deprecated, not used any more */
1924 RETURN(-EOPNOTSUPP);
1928 * Handle quota control requests to consult current usage/limit, but also
1929 * to configure quota enforcement
1931 int mdt_quotactl(struct mdt_thread_info *info)
1933 struct obd_export *exp = info->mti_exp;
1934 struct req_capsule *pill = info->mti_pill;
1935 struct obd_quotactl *oqctl, *repoqc;
1937 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1940 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1942 RETURN(err_serious(-EPROTO));
1944 rc = req_capsule_server_pack(pill);
1946 RETURN(err_serious(rc));
1948 switch (oqctl->qc_cmd) {
1950 case LUSTRE_Q_INVALIDATE:
1951 case LUSTRE_Q_FINVALIDATE:
1955 /* deprecated, not used any more */
1956 RETURN(-EOPNOTSUPP);
1957 /* master quotactl */
1963 RETURN(-EOPNOTSUPP);
1964 /* slave quotactl */
1969 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1973 /* map uid/gid for remote client */
1975 if (exp_connect_rmtclient(exp)) {
1976 struct lustre_idmap_table *idmap;
1978 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1980 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1981 oqctl->qc_cmd != Q_GETINFO))
1984 if (oqctl->qc_type == USRQUOTA)
1985 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1987 else if (oqctl->qc_type == GRPQUOTA)
1988 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1993 if (id == CFS_IDMAP_NOTFOUND) {
1994 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1999 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2001 RETURN(err_serious(-EFAULT));
2003 if (oqctl->qc_id != id)
2004 swap(oqctl->qc_id, id);
2006 switch (oqctl->qc_cmd) {
2012 /* forward quotactl request to QMT */
2013 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2018 /* slave quotactl */
2019 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2024 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2028 if (oqctl->qc_id != id)
2029 swap(oqctl->qc_id, id);
2036 * OBD PING and other handlers.
2038 int mdt_obd_ping(struct mdt_thread_info *info)
2043 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2045 rc = target_handle_ping(mdt_info_req(info));
2047 rc = err_serious(rc);
2052 * OBD_IDX_READ handler
2054 int mdt_obd_idx_read(struct mdt_thread_info *info)
2056 struct mdt_device *mdt = info->mti_mdt;
2057 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2058 struct idx_info *req_ii, *rep_ii;
2062 memset(rdpg, 0, sizeof(*rdpg));
2063 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2065 /* extract idx_info buffer from request & reply */
2066 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2067 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2068 RETURN(err_serious(-EPROTO));
2070 rc = req_capsule_server_pack(info->mti_pill);
2072 RETURN(err_serious(rc));
2074 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2076 RETURN(err_serious(-EFAULT));
2077 rep_ii->ii_magic = IDX_INFO_MAGIC;
2079 /* extract hash to start with */
2080 rdpg->rp_hash = req_ii->ii_hash_start;
2082 /* extract requested attributes */
2083 rdpg->rp_attrs = req_ii->ii_attrs;
2085 /* check that fid packed in request is valid and supported */
2086 if (!fid_is_sane(&req_ii->ii_fid))
2088 rep_ii->ii_fid = req_ii->ii_fid;
2091 rep_ii->ii_flags = req_ii->ii_flags;
2093 /* compute number of pages to allocate, ii_count is the number of 4KB
2095 if (req_ii->ii_count <= 0)
2096 GOTO(out, rc = -EFAULT);
2097 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2098 exp_max_brw_size(info->mti_exp));
2099 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2101 /* allocate pages to store the containers */
2102 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2103 if (rdpg->rp_pages == NULL)
2104 GOTO(out, rc = -ENOMEM);
2105 for (i = 0; i < rdpg->rp_npages; i++) {
2106 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2107 if (rdpg->rp_pages[i] == NULL)
2108 GOTO(out, rc = -ENOMEM);
2111 /* populate pages with key/record pairs */
2112 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2116 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2117 "asked %d > %d\n", rc, rdpg->rp_count);
2119 /* send pages to client */
2120 rc = mdt_sendpage(info, rdpg, rc);
2124 if (rdpg->rp_pages) {
2125 for (i = 0; i < rdpg->rp_npages; i++)
2126 if (rdpg->rp_pages[i])
2127 cfs_free_page(rdpg->rp_pages[i]);
2128 OBD_FREE(rdpg->rp_pages,
2129 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2134 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2136 return err_serious(-EOPNOTSUPP);
2139 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2141 return err_serious(-EOPNOTSUPP);
2148 /** clone llog ctxt from child (mdd)
2149 * This allows remote llog (replicator) access.
2150 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2151 * context was originally set up, or we can handle them directly.
2152 * I choose the latter, but that means I need any llog
2153 * contexts set up by child to be accessable by the mdt. So we clone the
2154 * context into our context list here.
2156 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2159 struct md_device *next = mdt->mdt_child;
2160 struct llog_ctxt *ctxt;
2163 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2166 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2167 if (rc || ctxt == NULL) {
2171 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2173 CERROR("Can't set mdt ctxt %d\n", rc);
2178 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2179 struct mdt_device *mdt, int idx)
2181 struct llog_ctxt *ctxt;
2183 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2186 /* Put once for the get we just did, and once for the clone */
2187 llog_ctxt_put(ctxt);
2188 llog_ctxt_put(ctxt);
2192 int mdt_llog_create(struct mdt_thread_info *info)
2196 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2197 rc = llog_origin_handle_open(mdt_info_req(info));
2198 return (rc < 0 ? err_serious(rc) : rc);
2201 int mdt_llog_destroy(struct mdt_thread_info *info)
2205 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2206 rc = llog_origin_handle_destroy(mdt_info_req(info));
2207 return (rc < 0 ? err_serious(rc) : rc);
2210 int mdt_llog_read_header(struct mdt_thread_info *info)
2214 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2215 rc = llog_origin_handle_read_header(mdt_info_req(info));
2216 return (rc < 0 ? err_serious(rc) : rc);
2219 int mdt_llog_next_block(struct mdt_thread_info *info)
2223 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2224 rc = llog_origin_handle_next_block(mdt_info_req(info));
2225 return (rc < 0 ? err_serious(rc) : rc);
2228 int mdt_llog_prev_block(struct mdt_thread_info *info)
2232 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2233 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2234 return (rc < 0 ? err_serious(rc) : rc);
2242 static struct ldlm_callback_suite cbs = {
2243 .lcs_completion = ldlm_server_completion_ast,
2244 .lcs_blocking = ldlm_server_blocking_ast,
2245 .lcs_glimpse = ldlm_server_glimpse_ast
2248 int mdt_enqueue(struct mdt_thread_info *info)
2250 struct ptlrpc_request *req;
2254 * info->mti_dlm_req already contains swapped and (if necessary)
2255 * converted dlm request.
2257 LASSERT(info->mti_dlm_req != NULL);
2259 req = mdt_info_req(info);
2260 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2261 req, info->mti_dlm_req, &cbs);
2262 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2263 return rc ? err_serious(rc) : req->rq_status;
2266 int mdt_convert(struct mdt_thread_info *info)
2269 struct ptlrpc_request *req;
2271 LASSERT(info->mti_dlm_req);
2272 req = mdt_info_req(info);
2273 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2274 return rc ? err_serious(rc) : req->rq_status;
2277 int mdt_bl_callback(struct mdt_thread_info *info)
2279 CERROR("bl callbacks should not happen on MDS\n");
2281 return err_serious(-EOPNOTSUPP);
2284 int mdt_cp_callback(struct mdt_thread_info *info)
2286 CERROR("cp callbacks should not happen on MDS\n");
2288 return err_serious(-EOPNOTSUPP);
2292 * sec context handlers
2294 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2298 rc = mdt_handle_idmap(info);
2301 struct ptlrpc_request *req = mdt_info_req(info);
2304 opc = lustre_msg_get_opc(req->rq_reqmsg);
2305 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2306 sptlrpc_svc_ctx_invalidate(req);
2309 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2315 * quota request handlers
2317 int mdt_quota_dqacq(struct mdt_thread_info *info)
2319 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2324 RETURN(err_serious(-EOPNOTSUPP));
2326 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2330 static struct mdt_object *mdt_obj(struct lu_object *o)
2332 LASSERT(lu_device_is_mdt(o->lo_dev));
2333 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2336 struct mdt_object *mdt_object_new(const struct lu_env *env,
2337 struct mdt_device *d,
2338 const struct lu_fid *f)
2340 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2341 struct lu_object *o;
2342 struct mdt_object *m;
2345 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2346 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2347 if (unlikely(IS_ERR(o)))
2348 m = (struct mdt_object *)o;
2354 struct mdt_object *mdt_object_find(const struct lu_env *env,
2355 struct mdt_device *d,
2356 const struct lu_fid *f)
2358 struct lu_object *o;
2359 struct mdt_object *m;
2362 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2363 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2364 if (unlikely(IS_ERR(o)))
2365 m = (struct mdt_object *)o;
2372 * Asyncronous commit for mdt device.
2374 * Pass asynchonous commit call down the MDS stack.
2376 * \param env environment
2377 * \param mdt the mdt device
2379 static void mdt_device_commit_async(const struct lu_env *env,
2380 struct mdt_device *mdt)
2382 struct dt_device *dt = mdt->mdt_bottom;
2385 rc = dt->dd_ops->dt_commit_async(env, dt);
2386 if (unlikely(rc != 0))
2387 CWARN("async commit start failed with rc = %d", rc);
2391 * Mark the lock as "synchonous".
2393 * Mark the lock to deffer transaction commit to the unlock time.
2395 * \param lock the lock to mark as "synchonous"
2397 * \see mdt_is_lock_sync
2398 * \see mdt_save_lock
2400 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2402 lock->l_ast_data = (void*)1;
2406 * Check whehter the lock "synchonous" or not.
2408 * \param lock the lock to check
2409 * \retval 1 the lock is "synchonous"
2410 * \retval 0 the lock isn't "synchronous"
2412 * \see mdt_set_lock_sync
2413 * \see mdt_save_lock
2415 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2417 return lock->l_ast_data != NULL;
2421 * Blocking AST for mdt locks.
2423 * Starts transaction commit if in case of COS lock conflict or
2424 * deffers such a commit to the mdt_save_lock.
2426 * \param lock the lock which blocks a request or cancelling lock
2427 * \param desc unused
2428 * \param data unused
2429 * \param flag indicates whether this cancelling or blocking callback
2431 * \see ldlm_blocking_ast_nocheck
2433 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2434 void *data, int flag)
2436 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2437 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2441 if (flag == LDLM_CB_CANCELING)
2443 lock_res_and_lock(lock);
2444 if (lock->l_blocking_ast != mdt_blocking_ast) {
2445 unlock_res_and_lock(lock);
2448 if (mdt_cos_is_enabled(mdt) &&
2449 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2450 lock->l_blocking_lock != NULL &&
2451 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2452 mdt_set_lock_sync(lock);
2454 rc = ldlm_blocking_ast_nocheck(lock);
2456 /* There is no lock conflict if l_blocking_lock == NULL,
2457 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2458 * when the last reference to a local lock was released */
2459 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2462 rc = lu_env_init(&env, LCT_LOCAL);
2463 if (unlikely(rc != 0))
2464 CWARN("lu_env initialization failed with rc = %d,"
2465 "cannot start asynchronous commit\n", rc);
2467 mdt_device_commit_async(&env, mdt);
2473 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2474 void *data, int flag)
2476 struct lustre_handle lockh;
2480 case LDLM_CB_BLOCKING:
2481 ldlm_lock2handle(lock, &lockh);
2482 rc = ldlm_cli_cancel(&lockh);
2484 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2488 case LDLM_CB_CANCELING:
2489 LDLM_DEBUG(lock, "Revoke remote lock\n");
2497 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2498 struct mdt_object *o, struct lustre_handle *lh,
2499 ldlm_mode_t mode, __u64 ibits)
2501 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2502 ldlm_policy_data_t *policy = &mti->mti_policy;
2506 LASSERT(mdt_object_remote(o));
2508 LASSERT((ibits & MDS_INODELOCK_UPDATE));
2510 memset(einfo, 0, sizeof(*einfo));
2511 einfo->ei_type = LDLM_IBITS;
2512 einfo->ei_mode = mode;
2513 einfo->ei_cb_bl = mdt_md_blocking_ast;
2514 einfo->ei_cb_cp = ldlm_completion_ast;
2516 memset(policy, 0, sizeof(*policy));
2517 policy->l_inodebits.bits = ibits;
2519 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2524 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2525 struct mdt_lock_handle *lh, __u64 ibits,
2526 bool nonblock, int locality)
2528 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2529 ldlm_policy_data_t *policy = &info->mti_policy;
2530 struct ldlm_res_id *res_id = &info->mti_res_id;
2535 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2536 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2537 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2538 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2540 if (mdt_object_remote(o)) {
2541 if (locality == MDT_CROSS_LOCK) {
2542 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2543 ibits |= MDS_INODELOCK_LOOKUP;
2546 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2547 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2548 mdt_obd_name(info->mti_mdt), ibits,
2549 PFID(mdt_object_fid(o)));
2550 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2552 /* No PDO lock on remote object */
2553 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2556 if (lh->mlh_type == MDT_PDO_LOCK) {
2557 /* check for exists after object is locked */
2558 if (mdt_object_exists(o) == 0) {
2559 /* Non-existent object shouldn't have PDO lock */
2562 /* Non-dir object shouldn't have PDO lock */
2563 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2568 memset(policy, 0, sizeof(*policy));
2569 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2571 dlmflags = LDLM_FL_ATOMIC_CB;
2573 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2576 * Take PDO lock on whole directory and build correct @res_id for lock
2577 * on part of directory.
2579 if (lh->mlh_pdo_hash != 0) {
2580 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2581 mdt_lock_pdo_mode(info, o, lh);
2582 if (lh->mlh_pdo_mode != LCK_NL) {
2584 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2585 * is never going to be sent to client and we do not
2586 * want it slowed down due to possible cancels.
2588 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2589 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2590 policy, res_id, dlmflags,
2591 &info->mti_exp->exp_handle.h_cookie);
2597 * Finish res_id initializing by name hash marking part of
2598 * directory which is taking modification.
2600 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2603 policy->l_inodebits.bits = ibits;
2606 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2607 * going to be sent to client. If it is - mdt_intent_policy() path will
2608 * fix it up and turn FL_LOCAL flag off.
2610 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2611 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2612 &info->mti_exp->exp_handle.h_cookie);
2614 mdt_object_unlock(info, o, lh, 1);
2615 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2616 lh->mlh_pdo_hash != 0 &&
2617 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2618 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2624 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2625 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2627 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2630 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2631 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2633 struct mdt_lock_handle tmp = *lh;
2636 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2644 * Save a lock within request object.
2646 * Keep the lock referenced until whether client ACK or transaction
2647 * commit happens or release the lock immediately depending on input
2648 * parameters. If COS is ON, a write lock is converted to COS lock
2651 * \param info thead info object
2652 * \param h lock handle
2653 * \param mode lock mode
2654 * \param decref force immediate lock releasing
2657 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2658 ldlm_mode_t mode, int decref)
2662 if (lustre_handle_is_used(h)) {
2663 if (decref || !info->mti_has_trans ||
2664 !(mode & (LCK_PW | LCK_EX))){
2665 mdt_fid_unlock(h, mode);
2667 struct mdt_device *mdt = info->mti_mdt;
2668 struct ldlm_lock *lock = ldlm_handle2lock(h);
2669 struct ptlrpc_request *req = mdt_info_req(info);
2672 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2674 CDEBUG(D_HA, "request = %p reply state = %p"
2675 " transno = "LPD64"\n",
2676 req, req->rq_reply_state, req->rq_transno);
2677 if (mdt_cos_is_enabled(mdt)) {
2679 ldlm_lock_downgrade(lock, LCK_COS);
2682 ptlrpc_save_lock(req, h, mode, no_ack);
2683 if (mdt_is_lock_sync(lock)) {
2684 CDEBUG(D_HA, "found sync-lock,"
2685 " async commit started\n");
2686 mdt_device_commit_async(info->mti_env,
2689 LDLM_LOCK_PUT(lock);
2698 * Unlock mdt object.
2700 * Immeditely release the regular lock and the PDO lock or save the
2701 * lock in reqeuest and keep them referenced until client ACK or
2702 * transaction commit.
2704 * \param info thread info object
2705 * \param o mdt object
2706 * \param lh mdt lock handle referencing regular and PDO locks
2707 * \param decref force immediate lock releasing
2709 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2710 struct mdt_lock_handle *lh, int decref)
2714 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2715 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2717 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2718 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2723 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2724 const struct lu_fid *f,
2725 struct mdt_lock_handle *lh,
2728 struct mdt_object *o;
2730 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2734 rc = mdt_object_lock(info, o, lh, ibits,
2737 mdt_object_put(info->mti_env, o);
2744 void mdt_object_unlock_put(struct mdt_thread_info * info,
2745 struct mdt_object * o,
2746 struct mdt_lock_handle *lh,
2749 mdt_object_unlock(info, o, lh, decref);
2750 mdt_object_put(info->mti_env, o);
2753 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2755 struct mdt_opc_slice *s;
2756 struct mdt_handler *h;
2759 for (s = supported; s->mos_hs != NULL; s++) {
2760 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2761 h = s->mos_hs + (opc - s->mos_opc_start);
2762 if (likely(h->mh_opc != 0))
2763 LASSERTF(h->mh_opc == opc,
2764 "opcode mismatch %d != %d\n",
2767 h = NULL; /* unsupported opc */
2774 static int mdt_lock_resname_compat(struct mdt_device *m,
2775 struct ldlm_request *req)
2777 /* XXX something... later. */
2781 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2783 /* XXX something... later. */
2788 * Generic code handling requests that have struct mdt_body passed in:
2790 * - extract mdt_body from request and save it in @info, if present;
2792 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2795 * - if HABEO_CORPUS flag is set for this request type check whether object
2796 * actually exists on storage (lu_object_exists()).
2799 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2801 const struct mdt_body *body;
2802 struct mdt_object *obj;
2803 const struct lu_env *env;
2804 struct req_capsule *pill;
2808 env = info->mti_env;
2809 pill = info->mti_pill;
2811 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2815 if (!(body->valid & OBD_MD_FLID))
2818 if (!fid_is_sane(&body->fid1)) {
2819 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2824 * Do not get size or any capa fields before we check that request
2825 * contains capa actually. There are some requests which do not, for
2826 * instance MDS_IS_SUBDIR.
2828 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2829 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2830 mdt_set_capainfo(info, 0, &body->fid1,
2831 req_capsule_client_get(pill, &RMF_CAPA1));
2833 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2835 if ((flags & HABEO_CORPUS) &&
2836 !mdt_object_exists(obj)) {
2837 mdt_object_put(env, obj);
2838 /* for capability renew ENOENT will be handled in
2840 if (body->valid & OBD_MD_FLOSSCAPA)
2845 info->mti_object = obj;
2854 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2856 struct req_capsule *pill = info->mti_pill;
2860 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2861 rc = mdt_body_unpack(info, flags);
2865 if (rc == 0 && (flags & HABEO_REFERO)) {
2867 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2868 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2869 info->mti_body->eadatasize);
2870 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2871 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2874 rc = req_capsule_server_pack(pill);
2879 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2881 struct md_device *next = m->mdt_child;
2883 return next->md_ops->mdo_init_capa_ctxt(env, next,
2884 m->mdt_opts.mo_mds_capa,
2885 m->mdt_capa_timeout,
2891 * Invoke handler for this request opc. Also do necessary preprocessing
2892 * (according to handler ->mh_flags), and post-processing (setting of
2893 * ->last_{xid,committed}).
2895 static int mdt_req_handle(struct mdt_thread_info *info,
2896 struct mdt_handler *h, struct ptlrpc_request *req)
2898 int rc, serious = 0;
2903 LASSERT(h->mh_act != NULL);
2904 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2905 LASSERT(current->journal_info == NULL);
2908 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2909 * to put same checks into handlers like mdt_close(), mdt_reint(),
2910 * etc., without talking to mdt authors first. Checking same thing
2911 * there again is useless and returning 0 error without packing reply
2912 * is buggy! Handlers either pack reply or return error.
2914 * We return 0 here and do not send any reply in order to emulate
2915 * network failure. Do not send any reply in case any of NET related
2916 * fail_id has occured.
2918 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2922 flags = h->mh_flags;
2923 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2925 if (h->mh_fmt != NULL) {
2926 req_capsule_set(info->mti_pill, h->mh_fmt);
2927 rc = mdt_unpack_req_pack_rep(info, flags);
2930 if (rc == 0 && flags & MUTABOR &&
2931 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2932 /* should it be rq_status? */
2935 if (rc == 0 && flags & HABEO_CLAVIS) {
2936 struct ldlm_request *dlm_req;
2938 LASSERT(h->mh_fmt != NULL);
2940 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2941 if (dlm_req != NULL) {
2942 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2944 dlm_req->lock_desc.l_policy_data.\
2945 l_inodebits.bits == 0)) {
2947 * Lock without inodebits makes no sense and
2948 * will oops later in ldlm. If client miss to
2949 * set such bits, do not trigger ASSERTION.
2951 * For liblustre flock case, it maybe zero.
2955 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2956 rc = mdt_lock_resname_compat(
2959 info->mti_dlm_req = dlm_req;
2966 /* capability setting changed via /proc, needs reinitialize ctxt */
2967 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2968 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2969 info->mti_mdt->mdt_capa_conf = 0;
2972 if (likely(rc == 0)) {
2974 * Process request, there can be two types of rc:
2975 * 1) errors with msg unpack/pack, other failures outside the
2976 * operation itself. This is counted as serious errors;
2977 * 2) errors during fs operation, should be placed in rq_status
2980 rc = h->mh_act(info);
2982 !req->rq_no_reply && req->rq_reply_state == NULL) {
2983 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2984 "pack reply and returned 0 error\n",
2988 serious = is_serious(rc);
2989 rc = clear_serious(rc);
2993 req->rq_status = rc;
2996 * ELDLM_* codes which > 0 should be in rq_status only as well as
2997 * all non-serious errors.
2999 if (rc > 0 || !serious)
3002 LASSERT(current->journal_info == NULL);
3004 if (rc == 0 && (flags & HABEO_CLAVIS) &&
3005 info->mti_mdt->mdt_opts.mo_compat_resname) {
3006 struct ldlm_reply *dlmrep;
3008 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3010 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3013 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3014 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3015 target_committed_to_req(req);
3017 if (unlikely(req_is_replay(req) &&
3018 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3019 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3023 target_send_reply(req, rc, info->mti_fail_id);
3027 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3029 lh->mlh_type = MDT_NUL_LOCK;
3030 lh->mlh_reg_lh.cookie = 0ull;
3031 lh->mlh_reg_mode = LCK_MINMODE;
3032 lh->mlh_pdo_lh.cookie = 0ull;
3033 lh->mlh_pdo_mode = LCK_MINMODE;
3034 lh->mlh_rreg_lh.cookie = 0ull;
3035 lh->mlh_rreg_mode = LCK_MINMODE;
3038 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3040 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3041 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3045 * Initialize fields of struct mdt_thread_info. Other fields are left in
3046 * uninitialized state, because it's too expensive to zero out whole
3047 * mdt_thread_info (> 1K) on each request arrival.
3049 static void mdt_thread_info_init(struct ptlrpc_request *req,
3050 struct mdt_thread_info *info)
3054 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3055 info->mti_pill = &req->rq_pill;
3058 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3059 mdt_lock_handle_init(&info->mti_lh[i]);
3061 /* mdt device: it can be NULL while CONNECT */
3062 if (req->rq_export) {
3063 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3064 info->mti_exp = req->rq_export;
3066 info->mti_mdt = NULL;
3067 info->mti_env = req->rq_svc_thread->t_env;
3068 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3069 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3070 info->mti_mos = NULL;
3072 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3073 info->mti_body = NULL;
3074 info->mti_object = NULL;
3075 info->mti_dlm_req = NULL;
3076 info->mti_has_trans = 0;
3077 info->mti_cross_ref = 0;
3078 info->mti_opdata = 0;
3079 info->mti_big_lmm_used = 0;
3081 /* To not check for split by default. */
3082 info->mti_spec.no_create = 0;
3085 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3089 req_capsule_fini(info->mti_pill);
3090 if (info->mti_object != NULL) {
3091 mdt_object_put(info->mti_env, info->mti_object);
3092 info->mti_object = NULL;
3094 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3095 mdt_lock_handle_fini(&info->mti_lh[i]);
3096 info->mti_env = NULL;
3099 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3100 struct obd_device *obd, int *process)
3102 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3103 case MDS_CONNECT: /* This will never get here, but for completeness. */
3104 case OST_CONNECT: /* This will never get here, but for completeness. */
3105 case MDS_DISCONNECT:
3106 case OST_DISCONNECT:
3112 case MDS_DONE_WRITING:
3113 case MDS_SYNC: /* used in unmounting */
3119 *process = target_queue_recovery_request(req, obd);
3123 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3130 * Handle recovery. Return:
3131 * +1: continue request processing;
3132 * -ve: abort immediately with the given error code;
3133 * 0: send reply with error code in req->rq_status;
3135 static int mdt_recovery(struct mdt_thread_info *info)
3137 struct ptlrpc_request *req = mdt_info_req(info);
3138 struct obd_device *obd;
3142 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3145 case SEC_CTX_INIT_CONT:
3151 rc = mdt_handle_idmap(info);
3160 if (unlikely(!class_connected_export(req->rq_export))) {
3161 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3162 lustre_msg_get_opc(req->rq_reqmsg),
3163 libcfs_id2str(req->rq_peer));
3164 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3165 * mds_A will get -ENOTCONN(especially for ping req),
3166 * which will cause that mds_A deactive timeout, then when
3167 * mds_A cleanup, the cleanup process will be suspended since
3168 * deactive timeout is not zero.
3170 req->rq_status = -ENOTCONN;
3171 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3175 /* sanity check: if the xid matches, the request must be marked as a
3176 * resent or replayed */
3177 if (req_xid_is_last(req)) {
3178 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3179 (MSG_RESENT | MSG_REPLAY))) {
3180 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3181 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3182 lustre_msg_get_flags(req->rq_reqmsg));
3184 req->rq_status = -ENOTCONN;
3189 /* else: note the opposite is not always true; a RESENT req after a
3190 * failover will usually not match the last_xid, since it was likely
3191 * never committed. A REPLAYed request will almost never match the
3192 * last xid, however it could for a committed, but still retained,
3195 obd = req->rq_export->exp_obd;
3197 /* Check for aborted recovery... */
3198 if (unlikely(obd->obd_recovering)) {
3201 DEBUG_REQ(D_INFO, req, "Got new replay");
3202 rc = mdt_filter_recovery_request(req, obd, &should_process);
3203 if (rc != 0 || !should_process)
3205 else if (should_process < 0) {
3206 req->rq_status = should_process;
3207 rc = ptlrpc_error(req);
3214 static int mdt_msg_check_version(struct lustre_msg *msg)
3218 switch (lustre_msg_get_opc(msg)) {
3220 case MDS_DISCONNECT:
3223 case SEC_CTX_INIT_CONT:
3226 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3228 CERROR("bad opc %u version %08x, expecting %08x\n",
3229 lustre_msg_get_opc(msg),
3230 lustre_msg_get_version(msg),
3231 LUSTRE_OBD_VERSION);
3235 case MDS_GETATTR_NAME:
3242 case MDS_DONE_WRITING:
3249 case MDS_HSM_PROGRESS:
3250 case MDS_HSM_REQUEST:
3251 case MDS_HSM_CT_REGISTER:
3252 case MDS_HSM_CT_UNREGISTER:
3253 case MDS_HSM_STATE_GET:
3254 case MDS_HSM_STATE_SET:
3255 case MDS_HSM_ACTION:
3256 case MDS_QUOTACHECK:
3259 case MDS_SWAP_LAYOUTS:
3264 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3266 CERROR("bad opc %u version %08x, expecting %08x\n",
3267 lustre_msg_get_opc(msg),
3268 lustre_msg_get_version(msg),
3269 LUSTRE_MDS_VERSION);
3273 case LDLM_BL_CALLBACK:
3274 case LDLM_CP_CALLBACK:
3275 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3277 CERROR("bad opc %u version %08x, expecting %08x\n",
3278 lustre_msg_get_opc(msg),
3279 lustre_msg_get_version(msg),
3280 LUSTRE_DLM_VERSION);
3282 case OBD_LOG_CANCEL:
3283 case LLOG_ORIGIN_HANDLE_CREATE:
3284 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3285 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3286 case LLOG_ORIGIN_HANDLE_CLOSE:
3287 case LLOG_ORIGIN_HANDLE_DESTROY:
3288 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3290 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3292 CERROR("bad opc %u version %08x, expecting %08x\n",
3293 lustre_msg_get_opc(msg),
3294 lustre_msg_get_version(msg),
3295 LUSTRE_LOG_VERSION);
3298 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3304 static int mdt_handle0(struct ptlrpc_request *req,
3305 struct mdt_thread_info *info,
3306 struct mdt_opc_slice *supported)
3308 struct mdt_handler *h;
3309 struct lustre_msg *msg;
3314 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3317 LASSERT(current->journal_info == NULL);
3319 msg = req->rq_reqmsg;
3320 rc = mdt_msg_check_version(msg);
3321 if (likely(rc == 0)) {
3322 rc = mdt_recovery(info);
3323 if (likely(rc == +1)) {
3324 h = mdt_handler_find(lustre_msg_get_opc(msg),
3326 if (likely(h != NULL)) {
3327 rc = mdt_req_handle(info, h, req);
3329 CERROR("The unsupported opc: 0x%x\n",
3330 lustre_msg_get_opc(msg) );
3331 req->rq_status = -ENOTSUPP;
3332 rc = ptlrpc_error(req);
3337 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3342 * MDT handler function called by ptlrpc service thread when request comes.
3344 * XXX common "target" functionality should be factored into separate module
3345 * shared by mdt, ost and stand-alone services like fld.
3347 int mdt_handle_common(struct ptlrpc_request *req,
3348 struct mdt_opc_slice *supported)
3351 struct mdt_thread_info *info;
3355 env = req->rq_svc_thread->t_env;
3356 /* Refill(initilize) the context(mdt_thread_info), in case it is
3357 * not initialized yet. Usually it happens during start up, after
3358 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3359 * before MDT_thread_info is initialized */
3361 LASSERT(env != NULL);
3362 LASSERT(env->le_ses != NULL);
3363 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3364 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3365 LASSERT(info != NULL);
3367 mdt_thread_info_init(req, info);
3369 rc = mdt_handle0(req, info, supported);
3371 mdt_thread_info_fini(info);
3376 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3379 int mdt_recovery_handle(struct ptlrpc_request *req)
3384 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3386 rc = mdt_handle_common(req, mdt_fld_handlers);
3389 rc = mdt_handle_common(req, mdt_seq_handlers);
3392 rc = mdt_handle_common(req, mdt_regular_handlers);
3414 static int mdt_intent_getattr(enum mdt_it_code opcode,
3415 struct mdt_thread_info *info,
3416 struct ldlm_lock **,
3418 static int mdt_intent_layout(enum mdt_it_code opcode,
3419 struct mdt_thread_info *info,
3420 struct ldlm_lock **,
3422 static int mdt_intent_reint(enum mdt_it_code opcode,
3423 struct mdt_thread_info *info,
3424 struct ldlm_lock **,
3427 static struct mdt_it_flavor {
3428 const struct req_format *it_fmt;
3430 int (*it_act)(enum mdt_it_code ,
3431 struct mdt_thread_info *,
3432 struct ldlm_lock **,
3435 } mdt_it_flavor[] = {
3437 .it_fmt = &RQF_LDLM_INTENT,
3438 /*.it_flags = HABEO_REFERO,*/
3440 .it_act = mdt_intent_reint,
3441 .it_reint = REINT_OPEN
3444 .it_fmt = &RQF_LDLM_INTENT,
3445 .it_flags = MUTABOR,
3446 .it_act = mdt_intent_reint,
3447 .it_reint = REINT_OPEN
3450 .it_fmt = &RQF_LDLM_INTENT,
3451 .it_flags = MUTABOR,
3452 .it_act = mdt_intent_reint,
3453 .it_reint = REINT_CREATE
3455 [MDT_IT_GETATTR] = {
3456 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3457 .it_flags = HABEO_REFERO,
3458 .it_act = mdt_intent_getattr
3460 [MDT_IT_READDIR] = {
3466 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3467 .it_flags = HABEO_REFERO,
3468 .it_act = mdt_intent_getattr
3471 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3472 .it_flags = MUTABOR,
3474 .it_reint = REINT_UNLINK
3478 .it_flags = MUTABOR,
3481 [MDT_IT_GETXATTR] = {
3487 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3489 .it_act = mdt_intent_layout
3493 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3494 struct ldlm_lock **lockp,
3495 struct ldlm_lock *new_lock,
3496 struct mdt_lock_handle *lh,
3499 struct ptlrpc_request *req = mdt_info_req(info);
3500 struct ldlm_lock *lock = *lockp;
3503 * Get new lock only for cases when possible resent did not find any
3506 if (new_lock == NULL)
3507 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3509 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3510 lh->mlh_reg_lh.cookie = 0;
3514 LASSERTF(new_lock != NULL,
3515 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3518 * If we've already given this lock to a client once, then we should
3519 * have no readers or writers. Otherwise, we should have one reader
3520 * _or_ writer ref (which will be zeroed below) before returning the
3523 if (new_lock->l_export == req->rq_export) {
3524 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3526 LASSERT(new_lock->l_export == NULL);
3527 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3532 if (new_lock->l_export == req->rq_export) {
3534 * Already gave this to the client, which means that we
3535 * reconstructed a reply.
3537 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3539 lh->mlh_reg_lh.cookie = 0;
3540 RETURN(ELDLM_LOCK_REPLACED);
3544 * Fixup the lock to be given to the client.
3546 lock_res_and_lock(new_lock);
3547 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3548 * possible blocking AST. */
3549 while (new_lock->l_readers > 0) {
3550 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3551 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3552 new_lock->l_readers--;
3554 while (new_lock->l_writers > 0) {
3555 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3556 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3557 new_lock->l_writers--;
3560 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3561 new_lock->l_blocking_ast = lock->l_blocking_ast;
3562 new_lock->l_completion_ast = lock->l_completion_ast;
3563 new_lock->l_remote_handle = lock->l_remote_handle;
3564 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3566 unlock_res_and_lock(new_lock);
3568 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3569 &new_lock->l_remote_handle,
3570 &new_lock->l_exp_hash);
3572 LDLM_LOCK_RELEASE(new_lock);
3573 lh->mlh_reg_lh.cookie = 0;
3575 RETURN(ELDLM_LOCK_REPLACED);
3578 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3579 struct ldlm_lock *new_lock,
3580 struct ldlm_lock **old_lock,
3581 struct mdt_lock_handle *lh)
3583 struct ptlrpc_request *req = mdt_info_req(info);
3584 struct obd_export *exp = req->rq_export;
3585 struct lustre_handle remote_hdl;
3586 struct ldlm_request *dlmreq;
3587 struct ldlm_lock *lock;
3589 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3592 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3593 remote_hdl = dlmreq->lock_handle[0];
3595 /* In the function below, .hs_keycmp resolves to
3596 * ldlm_export_lock_keycmp() */
3597 /* coverity[overrun-buffer-val] */
3598 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3600 if (lock != new_lock) {
3601 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3602 lh->mlh_reg_mode = lock->l_granted_mode;
3604 LDLM_DEBUG(lock, "Restoring lock cookie");
3605 DEBUG_REQ(D_DLMTRACE, req,
3606 "restoring lock cookie "LPX64,
3607 lh->mlh_reg_lh.cookie);
3609 *old_lock = LDLM_LOCK_GET(lock);
3610 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3614 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3618 * If the xid matches, then we know this is a resent request, and allow
3619 * it. (It's probably an OPEN, for which we don't send a lock.
3621 if (req_xid_is_last(req))
3625 * This remote handle isn't enqueued, so we never received or processed
3626 * this request. Clear MSG_RESENT, because it can be handled like any
3627 * normal request now.
3629 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3631 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3635 static int mdt_intent_getattr(enum mdt_it_code opcode,
3636 struct mdt_thread_info *info,
3637 struct ldlm_lock **lockp,
3640 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3641 struct ldlm_lock *new_lock = NULL;
3643 struct ldlm_reply *ldlm_rep;
3644 struct ptlrpc_request *req;
3645 struct mdt_body *reqbody;
3646 struct mdt_body *repbody;
3650 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3653 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3656 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3657 repbody->eadatasize = 0;
3658 repbody->aclsize = 0;
3662 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3664 case MDT_IT_GETATTR:
3665 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3669 CERROR("Unsupported intent (%d)\n", opcode);
3670 GOTO(out_shrink, rc = -EINVAL);
3673 rc = mdt_init_ucred(info, reqbody);
3675 GOTO(out_shrink, rc);
3677 req = info->mti_pill->rc_req;
3678 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3679 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3681 /* Get lock from request for possible resent case. */
3682 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3684 ldlm_rep->lock_policy_res2 =
3685 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3687 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3688 ldlm_rep->lock_policy_res2 = 0;
3689 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3690 ldlm_rep->lock_policy_res2) {
3691 lhc->mlh_reg_lh.cookie = 0ull;
3692 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3695 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3698 mdt_exit_ucred(info);
3700 mdt_client_compatibility(info);
3701 rc2 = mdt_fix_reply(info);
3707 static int mdt_intent_layout(enum mdt_it_code opcode,
3708 struct mdt_thread_info *info,
3709 struct ldlm_lock **lockp,
3712 struct layout_intent *layout;
3716 if (opcode != MDT_IT_LAYOUT) {
3717 CERROR("%s: Unknown intent (%d)\n",
3718 info->mti_exp->exp_obd->obd_name, opcode);
3722 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3723 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3724 ldlm_lvbo_size(*lockp));
3725 rc = req_capsule_server_pack(info->mti_pill);
3729 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3730 LASSERT(layout != NULL);
3731 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3732 /* return to normal ldlm handling */
3735 CERROR("%s: Unsupported layout intent (%d)\n",
3736 info->mti_exp->exp_obd->obd_name, layout->li_opc);
3740 static int mdt_intent_reint(enum mdt_it_code opcode,
3741 struct mdt_thread_info *info,
3742 struct ldlm_lock **lockp,
3745 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3746 struct ldlm_reply *rep = NULL;
3750 static const struct req_format *intent_fmts[REINT_MAX] = {
3751 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3752 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3757 opc = mdt_reint_opcode(info, intent_fmts);
3761 if (mdt_it_flavor[opcode].it_reint != opc) {
3762 CERROR("Reint code %ld doesn't match intent: %d\n",
3764 RETURN(err_serious(-EPROTO));
3767 /* Get lock from request for possible resent case. */
3768 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3770 rc = mdt_reint_internal(info, lhc, opc);
3772 /* Check whether the reply has been packed successfully. */
3773 if (mdt_info_req(info)->rq_repmsg != NULL)
3774 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3776 RETURN(err_serious(-EFAULT));
3778 /* MDC expects this in any case */
3780 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3782 /* Cross-ref case, the lock should be returned to the client */
3783 if (rc == -EREMOTE) {
3784 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3785 rep->lock_policy_res2 = 0;
3786 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3789 rep->lock_policy_res2 = clear_serious(rc);
3791 if (rep->lock_policy_res2 == -ENOENT &&
3792 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3793 rep->lock_policy_res2 = 0;
3795 if (rc == -ENOTCONN || rc == -ENODEV ||
3796 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3798 * If it is the disconnect error (ENODEV & ENOCONN), the error
3799 * will be returned by rq_status, and client at ptlrpc layer
3800 * will detect this, then disconnect, reconnect the import
3801 * immediately, instead of impacting the following the rpc.
3803 lhc->mlh_reg_lh.cookie = 0ull;
3807 * For other cases, the error will be returned by intent.
3808 * and client will retrieve the result from intent.
3811 * FIXME: when open lock is finished, that should be
3814 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3815 LASSERTF(rc == 0, "Error occurred but lock handle "
3816 "is still in use, rc = %d\n", rc);
3817 rep->lock_policy_res2 = 0;
3818 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3821 lhc->mlh_reg_lh.cookie = 0ull;
3822 RETURN(ELDLM_LOCK_ABORTED);
3827 static int mdt_intent_code(long itcode)
3835 case IT_OPEN|IT_CREAT:
3842 rc = MDT_IT_READDIR;
3845 rc = MDT_IT_GETATTR;
3857 rc = MDT_IT_GETXATTR;
3862 case IT_QUOTA_DQACQ:
3867 CERROR("Unknown intent opcode: %ld\n", itcode);
3874 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3875 struct ldlm_lock **lockp, __u64 flags)
3877 struct req_capsule *pill;
3878 struct mdt_it_flavor *flv;
3883 opc = mdt_intent_code(itopc);
3887 pill = info->mti_pill;
3889 if (opc == MDT_IT_QUOTA) {
3890 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3893 RETURN(-EOPNOTSUPP);
3895 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3896 /* pass the request to quota master */
3897 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3898 mdt_info_req(info), lockp,
3903 flv = &mdt_it_flavor[opc];
3904 if (flv->it_fmt != NULL)
3905 req_capsule_extend(pill, flv->it_fmt);
3907 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3909 struct ptlrpc_request *req = mdt_info_req(info);
3910 if (flv->it_flags & MUTABOR &&
3911 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3914 if (rc == 0 && flv->it_act != NULL) {
3915 /* execute policy */
3916 rc = flv->it_act(opc, info, lockp, flags);
3923 static int mdt_intent_policy(struct ldlm_namespace *ns,
3924 struct ldlm_lock **lockp, void *req_cookie,
3925 ldlm_mode_t mode, __u64 flags, void *data)
3927 struct mdt_thread_info *info;
3928 struct ptlrpc_request *req = req_cookie;
3929 struct ldlm_intent *it;
3930 struct req_capsule *pill;
3935 LASSERT(req != NULL);
3937 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3939 LASSERT(info != NULL);
3940 pill = info->mti_pill;
3941 LASSERT(pill->rc_req == req);
3943 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3944 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3945 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3947 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3951 /* Lock without inodebits makes no sense and will oops
3952 * later in ldlm. Let's check it now to see if we have
3953 * ibits corrupted somewhere in mdt_intent_opc().
3954 * The case for client miss to set ibits has been
3955 * processed by others. */
3956 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3957 lr_type == LDLM_IBITS,
3958 info->mti_dlm_req->lock_desc.\
3959 l_policy_data.l_inodebits.bits != 0));
3961 rc = err_serious(-EFAULT);
3963 /* No intent was provided */
3964 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3965 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3966 rc = req_capsule_server_pack(pill);
3968 rc = err_serious(rc);
3973 static int mdt_seq_fini(const struct lu_env *env,
3974 struct mdt_device *m)
3976 return seq_site_fini(env, mdt_seq_site(m));
3979 static int mdt_seq_init(const struct lu_env *env,
3981 struct mdt_device *m)
3983 struct seq_server_site *ss;
3988 ss = mdt_seq_site(m);
3991 * This is sequence-controller node. Init seq-controller server on local
3994 if (ss->ss_node_id == 0) {
3995 LASSERT(ss->ss_control_seq == NULL);
3997 OBD_ALLOC_PTR(ss->ss_control_seq);
3998 if (ss->ss_control_seq == NULL)
4001 rc = seq_server_init(ss->ss_control_seq,
4002 m->mdt_bottom, uuid,
4003 LUSTRE_SEQ_CONTROLLER,
4008 GOTO(out_seq_fini, rc);
4010 OBD_ALLOC_PTR(ss->ss_client_seq);
4011 if (ss->ss_client_seq == NULL)
4012 GOTO(out_seq_fini, rc = -ENOMEM);
4014 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4015 if (prefix == NULL) {
4016 OBD_FREE_PTR(ss->ss_client_seq);
4017 GOTO(out_seq_fini, rc = -ENOMEM);
4020 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4024 * Init seq-controller client after seq-controller server is
4025 * ready. Pass ss->ss_control_seq to it for direct talking.
4027 rc = seq_client_init(ss->ss_client_seq, NULL,
4028 LUSTRE_SEQ_METADATA, prefix,
4029 ss->ss_control_seq);
4030 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4033 GOTO(out_seq_fini, rc);
4036 /* Init seq-server on local MDT */
4037 LASSERT(ss->ss_server_seq == NULL);
4039 OBD_ALLOC_PTR(ss->ss_server_seq);
4040 if (ss->ss_server_seq == NULL)
4041 GOTO(out_seq_fini, rc = -ENOMEM);
4043 rc = seq_server_init(ss->ss_server_seq,
4044 m->mdt_bottom, uuid,
4049 GOTO(out_seq_fini, rc = -ENOMEM);
4051 /* Assign seq-controller client to local seq-server. */
4052 if (ss->ss_node_id == 0) {
4053 LASSERT(ss->ss_client_seq != NULL);
4055 rc = seq_server_set_cli(ss->ss_server_seq,
4063 mdt_seq_fini(env, m);
4071 static int mdt_fld_fini(const struct lu_env *env,
4072 struct mdt_device *m)
4074 struct seq_server_site *ss = mdt_seq_site(m);
4077 if (ss && ss->ss_server_fld) {
4078 fld_server_fini(env, ss->ss_server_fld);
4079 OBD_FREE_PTR(ss->ss_server_fld);
4080 ss->ss_server_fld = NULL;
4086 static int mdt_fld_init(const struct lu_env *env,
4088 struct mdt_device *m)
4090 struct seq_server_site *ss;
4094 ss = mdt_seq_site(m);
4096 OBD_ALLOC_PTR(ss->ss_server_fld);
4097 if (ss->ss_server_fld == NULL)
4098 RETURN(rc = -ENOMEM);
4100 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4101 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4103 OBD_FREE_PTR(ss->ss_server_fld);
4104 ss->ss_server_fld = NULL;
4111 static void mdt_stack_pre_fini(const struct lu_env *env,
4112 struct mdt_device *m, struct lu_device *top)
4114 struct obd_device *obd = mdt2obd_dev(m);
4115 struct lustre_cfg_bufs *bufs;
4116 struct lustre_cfg *lcfg;
4117 struct mdt_thread_info *info;
4122 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4123 LASSERT(info != NULL);
4125 bufs = &info->mti_u.bufs;
4127 LASSERT(m->mdt_child_exp);
4128 LASSERT(m->mdt_child_exp->exp_obd);
4129 obd = m->mdt_child_exp->exp_obd;
4131 /* process cleanup, pass mdt obd name to get obd umount flags */
4132 /* XXX: this is needed because all layers are referenced by
4133 * objects (some of them are pinned by osd, for example *
4134 * the proper solution should be a model where object used
4135 * by osd only doesn't have mdt/mdd slices -bzzz */
4136 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4137 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4138 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4140 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4143 top->ld_ops->ldo_process_config(env, top, lcfg);
4144 lustre_cfg_free(lcfg);
4148 static void mdt_stack_fini(const struct lu_env *env,
4149 struct mdt_device *m, struct lu_device *top)
4151 struct obd_device *obd = mdt2obd_dev(m);
4152 struct lustre_cfg_bufs *bufs;
4153 struct lustre_cfg *lcfg;
4154 struct mdt_thread_info *info;
4158 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4159 LASSERT(info != NULL);
4161 lu_dev_del_linkage(top->ld_site, top);
4163 lu_site_purge(env, top->ld_site, -1);
4165 bufs = &info->mti_u.bufs;
4166 /* process cleanup, pass mdt obd name to get obd umount flags */
4167 /* another purpose is to let all layers to release their objects */
4168 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4173 lustre_cfg_bufs_set_string(bufs, 1, flags);
4174 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4176 CERROR("Cannot alloc lcfg!\n");
4180 top->ld_ops->ldo_process_config(env, top, lcfg);
4181 lustre_cfg_free(lcfg);
4183 lu_site_purge(env, top->ld_site, -1);
4185 m->mdt_child = NULL;
4186 m->mdt_bottom = NULL;
4188 obd_disconnect(m->mdt_child_exp);
4189 m->mdt_child_exp = NULL;
4191 obd_disconnect(m->mdt_bottom_exp);
4192 m->mdt_child_exp = NULL;
4195 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4196 const char *next, struct obd_export **exp)
4198 struct obd_connect_data *data = NULL;
4199 struct obd_device *obd;
4203 OBD_ALLOC_PTR(data);
4205 GOTO(out, rc = -ENOMEM);
4207 obd = class_name2obd(next);
4209 CERROR("%s: can't locate next device: %s\n",
4210 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4211 GOTO(out, rc = -ENOTCONN);
4214 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4215 data->ocd_version = LUSTRE_VERSION_CODE;
4217 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4219 CERROR("%s: cannot connect to next dev %s (%d)\n",
4220 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4230 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4231 struct lustre_cfg *cfg)
4233 char *dev = lustre_cfg_string(cfg, 0);
4234 int rc, name_size, uuid_size;
4235 char *name, *uuid, *p;
4236 struct lustre_cfg_bufs *bufs;
4237 struct lustre_cfg *lcfg;
4238 struct obd_device *obd;
4239 struct lustre_profile *lprof;
4240 struct lu_site *site;
4243 /* in 1.8 we had the only device in the stack - MDS.
4244 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4245 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4246 * to generate names and setup MDT, MDD. MDT will be using
4247 * generated name to connect to MDD. for MDD the next device
4248 * will be LOD with name taken from so called "profile" which
4249 * is generated by mount_option line
4251 * 1.8 MGS generates config. commands like this:
4252 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4253 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4254 * 2.0 MGS generates config. commands like this:
4255 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4256 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4257 * 3:lustre-MDT0000-mdtlov 4:f
4259 * we generate MDD name from MDT one, just replacing T with D
4261 * after all the preparations, the logical equivalent will be
4262 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4263 * 3:lustre-MDT0000-mdtlov 4:f
4264 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4265 * 3:lustre-MDD0000 4:f
4267 * notice we build the stack from down to top: MDD first, then MDT */
4269 name_size = MAX_OBD_NAME;
4270 uuid_size = MAX_OBD_NAME;
4272 OBD_ALLOC(name, name_size);
4273 OBD_ALLOC(uuid, uuid_size);
4274 if (name == NULL || uuid == NULL)
4275 GOTO(cleanup_mem, rc = -ENOMEM);
4277 OBD_ALLOC_PTR(bufs);
4279 GOTO(cleanup_mem, rc = -ENOMEM);
4282 p = strstr(name, "-MDT");
4284 GOTO(cleanup_mem, rc = -ENOMEM);
4287 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4289 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4290 if (lprof == NULL || lprof->lp_dt == NULL) {
4291 CERROR("can't find the profile: %s\n",
4292 lustre_cfg_string(cfg, 0));
4293 GOTO(cleanup_mem, rc = -EINVAL);
4296 lustre_cfg_bufs_reset(bufs, name);
4297 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4298 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4299 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4301 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4303 GOTO(free_bufs, rc = -ENOMEM);
4305 rc = class_attach(lcfg);
4307 GOTO(lcfg_cleanup, rc);
4309 obd = class_name2obd(name);
4311 CERROR("Can not find obd %s (%s in config)\n",
4312 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4313 GOTO(class_detach, rc = -EINVAL);
4316 lustre_cfg_free(lcfg);
4318 lustre_cfg_bufs_reset(bufs, name);
4319 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4320 lustre_cfg_bufs_set_string(bufs, 2, dev);
4321 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4323 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4325 rc = class_setup(obd, lcfg);
4327 GOTO(class_detach, rc);
4329 /* connect to MDD we just setup */
4330 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4334 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4336 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4337 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4338 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4339 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4342 /* now connect to bottom OSD */
4343 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4344 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4348 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4351 rc = lu_env_refill((struct lu_env *)env);
4353 CERROR("Failure to refill session: '%d'\n", rc);
4355 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4360 class_detach(obd, lcfg);
4362 lustre_cfg_free(lcfg);
4367 OBD_FREE(name, name_size);
4369 OBD_FREE(uuid, uuid_size);
4373 /* setup quota master target on MDT0 */
4374 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4375 struct lustre_cfg *cfg)
4377 struct obd_device *obd;
4378 char *dev = lustre_cfg_string(cfg, 0);
4379 char *qmtname, *uuid, *p;
4380 struct lustre_cfg_bufs *bufs;
4381 struct lustre_cfg *lcfg;
4382 struct lustre_profile *lprof;
4383 struct obd_connect_data *data;
4387 LASSERT(mdt->mdt_qmt_exp == NULL);
4388 LASSERT(mdt->mdt_qmt_dev == NULL);
4390 /* quota master is on MDT0 only for now */
4391 if (mdt->mdt_seq_site.ss_node_id != 0)
4394 /* MGS generates config commands which look as follows:
4395 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4396 * 3:lustre-MDT0000-mdtlov 4:f
4398 * We generate the QMT name from the MDT one, just replacing MD with QM
4399 * after all the preparations, the logical equivalent will be:
4400 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4401 * 3:lustre-MDT0000-osd 4:f */
4402 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4403 OBD_ALLOC(uuid, UUID_MAX);
4404 OBD_ALLOC_PTR(bufs);
4405 OBD_ALLOC_PTR(data);
4406 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4407 GOTO(cleanup_mem, rc = -ENOMEM);
4409 strcpy(qmtname, dev);
4410 p = strstr(qmtname, "-MDT");
4412 GOTO(cleanup_mem, rc = -ENOMEM);
4413 /* replace MD with QM */
4417 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4419 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4420 if (lprof == NULL || lprof->lp_dt == NULL) {
4421 CERROR("can't find profile for %s\n",
4422 lustre_cfg_string(cfg, 0));
4423 GOTO(cleanup_mem, rc = -EINVAL);
4426 lustre_cfg_bufs_reset(bufs, qmtname);
4427 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4428 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4429 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4431 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4433 GOTO(cleanup_mem, rc = -ENOMEM);
4435 rc = class_attach(lcfg);
4437 GOTO(lcfg_cleanup, rc);
4439 obd = class_name2obd(qmtname);
4441 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4442 lustre_cfg_string(cfg, 0));
4443 GOTO(class_detach, rc = -EINVAL);
4446 lustre_cfg_free(lcfg);
4448 lustre_cfg_bufs_reset(bufs, qmtname);
4449 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4450 lustre_cfg_bufs_set_string(bufs, 2, dev);
4452 /* for quota, the next device should be the OSD device */
4453 lustre_cfg_bufs_set_string(bufs, 3,
4454 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4456 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4458 rc = class_setup(obd, lcfg);
4460 GOTO(class_detach, rc);
4462 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4464 /* configure local quota objects */
4465 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4466 &mdt->mdt_md_dev.md_lu_dev,
4469 GOTO(class_cleanup, rc);
4471 /* connect to quota master target */
4472 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4473 data->ocd_version = LUSTRE_VERSION_CODE;
4474 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4477 CERROR("cannot connect to quota master device %s (%d)\n",
4479 GOTO(class_cleanup, rc);
4485 class_manual_cleanup(obd);
4486 mdt->mdt_qmt_dev = NULL;
4490 class_detach(obd, lcfg);
4492 lustre_cfg_free(lcfg);
4497 OBD_FREE(qmtname, MAX_OBD_NAME);
4499 OBD_FREE(uuid, UUID_MAX);
4505 /* Shutdown quota master target associated with mdt */
4506 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4510 if (mdt->mdt_qmt_exp == NULL)
4512 LASSERT(mdt->mdt_qmt_dev != NULL);
4514 /* the qmt automatically shuts down when the mdt disconnects */
4515 obd_disconnect(mdt->mdt_qmt_exp);
4516 mdt->mdt_qmt_exp = NULL;
4517 mdt->mdt_qmt_dev = NULL;
4521 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4523 struct md_device *next = m->mdt_child;
4524 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4525 struct obd_device *obd = mdt2obd_dev(m);
4528 target_recovery_fini(obd);
4530 ping_evictor_stop();
4533 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4534 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4535 obd_exports_barrier(obd);
4536 obd_zombie_barrier();
4540 tgt_fini(env, &m->mdt_lut);
4541 mdt_fs_cleanup(env, m);
4542 upcall_cache_cleanup(m->mdt_identity_cache);
4543 m->mdt_identity_cache = NULL;
4545 if (m->mdt_namespace != NULL) {
4546 ldlm_namespace_free(m->mdt_namespace, NULL,
4547 d->ld_obd->obd_force);
4548 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4551 mdt_quota_fini(env, m);
4553 cfs_free_nidlist(&m->mdt_nosquash_nids);
4554 if (m->mdt_nosquash_str) {
4555 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4556 m->mdt_nosquash_str = NULL;
4557 m->mdt_nosquash_strlen = 0;
4560 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK,
4562 mdt_seq_fini(env, m);
4563 mdt_fld_fini(env, m);
4564 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4566 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4567 cfs_timer_disarm(&m->mdt_ck_timer);
4568 mdt_ck_thread_stop(m);
4573 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4575 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4577 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4582 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4584 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4585 struct sptlrpc_rule_set tmp_rset;
4588 sptlrpc_rule_set_init(&tmp_rset);
4589 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4591 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4596 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4598 write_lock(&m->mdt_sptlrpc_lock);
4599 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4600 m->mdt_sptlrpc_rset = tmp_rset;
4601 write_unlock(&m->mdt_sptlrpc_lock);
4606 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4608 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4609 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4611 struct mdt_thread_info *info;
4612 struct obd_device *obd;
4613 const char *dev = lustre_cfg_string(cfg, 0);
4614 const char *num = lustre_cfg_string(cfg, 2);
4615 struct lustre_mount_info *lmi = NULL;
4616 struct lustre_sb_info *lsi;
4618 struct seq_server_site *ss_site;
4619 const char *identity_upcall = "NONE";
4620 struct md_device *next;
4626 md_device_init(&m->mdt_md_dev, ldt);
4628 * Environment (env) might be missing mdt_thread_key values at that
4629 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4632 * Usually device allocation path doesn't use module key values, but
4633 * mdt has to do a lot of work here, so allocate key value.
4635 rc = lu_env_refill((struct lu_env *)env);
4639 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4640 LASSERT(info != NULL);
4642 obd = class_name2obd(dev);
4643 LASSERT(obd != NULL);
4645 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4647 m->mdt_som_conf = 0;
4649 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4650 lmi = server_get_mount(dev);
4652 CERROR("Cannot get mount info for %s!\n", dev);
4655 lsi = s2lsi(lmi->lmi_sb);
4656 /* CMD is supported only in IAM mode */
4658 node_id = simple_strtol(num, NULL, 10);
4659 obd->u.obt.obt_magic = OBT_MAGIC;
4662 rwlock_init(&m->mdt_sptlrpc_lock);
4663 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4665 spin_lock_init(&m->mdt_ioepoch_lock);
4666 m->mdt_opts.mo_compat_resname = 0;
4667 m->mdt_opts.mo_mds_capa = 1;
4668 m->mdt_opts.mo_oss_capa = 1;
4669 m->mdt_capa_timeout = CAPA_TIMEOUT;
4670 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4671 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4672 m->mdt_squash_uid = 0;
4673 m->mdt_squash_gid = 0;
4674 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4675 m->mdt_nosquash_str = NULL;
4676 m->mdt_nosquash_strlen = 0;
4677 init_rwsem(&m->mdt_squash_sem);
4678 spin_lock_init(&m->mdt_osfs_lock);
4679 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4680 m->mdt_enable_remote_dir = 0;
4682 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4683 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4684 /* set this lu_device to obd, because error handling need it */
4685 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4687 /* init the stack */
4688 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4690 CERROR("Can't init device stack, rc %d\n", rc);
4694 s = m->mdt_md_dev.md_lu_dev.ld_site;
4695 ss_site = &m->mdt_seq_site;
4696 s->ld_seq_site = ss_site;
4699 /* set server index */
4700 ss_site->ss_node_id = node_id;
4702 /* failover is the default
4703 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4704 * assumed whose ss_node_id == 0 XXX
4706 obd->obd_replayable = 1;
4707 /* No connection accepted until configurations will finish */
4708 obd->obd_no_conn = 1;
4710 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4711 char *str = lustre_cfg_string(cfg, 4);
4712 if (strchr(str, 'n')) {
4713 CWARN("%s: recovery disabled\n", obd->obd_name);
4714 obd->obd_replayable = 0;
4718 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4720 GOTO(err_fini_stack, rc);
4722 rc = mdt_fld_init(env, obd->obd_name, m);
4726 rc = mdt_seq_init(env, obd->obd_name, m);
4728 GOTO(err_fini_fld, rc);
4730 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4731 LUSTRE_MDT_NAME"-%p", m);
4732 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4733 LDLM_NAMESPACE_SERVER,
4734 LDLM_NAMESPACE_GREEDY,
4736 if (m->mdt_namespace == NULL)
4737 GOTO(err_fini_seq, rc = -ENOMEM);
4739 m->mdt_namespace->ns_lvbp = m;
4740 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4742 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4743 /* set obd_namespace for compatibility with old code */
4744 obd->obd_namespace = m->mdt_namespace;
4746 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4748 rc = mdt_ck_thread_start(m);
4750 GOTO(err_free_ns, rc);
4752 rc = mdt_fs_setup(env, m, obd, lsi);
4756 mdt_adapt_sptlrpc_conf(obd, 1);
4758 next = m->mdt_child;
4759 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4762 GOTO(err_llog_cleanup, rc);
4764 if (mntopts & MNTOPT_USERXATTR)
4765 m->mdt_opts.mo_user_xattr = 1;
4767 m->mdt_opts.mo_user_xattr = 0;
4769 if (mntopts & MNTOPT_ACL)
4770 m->mdt_opts.mo_acl = 1;
4772 m->mdt_opts.mo_acl = 0;
4774 /* XXX: to support suppgid for ACL, we enable identity_upcall
4775 * by default, otherwise, maybe got unexpected -EACCESS. */
4776 if (m->mdt_opts.mo_acl)
4777 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4779 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
4780 &mdt_identity_upcall_cache_ops);
4781 if (IS_ERR(m->mdt_identity_cache)) {
4782 rc = PTR_ERR(m->mdt_identity_cache);
4783 m->mdt_identity_cache = NULL;
4784 GOTO(err_llog_cleanup, rc);
4787 rc = mdt_procfs_init(m, dev);
4789 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4790 GOTO(err_recovery, rc);
4793 rc = mdt_quota_init(env, m, cfg);
4795 GOTO(err_procfs, rc);
4797 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4798 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4799 "mdt_ldlm_client", m->mdt_ldlm_client);
4801 ping_evictor_start();
4803 /* recovery will be started upon mdt_prepare()
4804 * when the whole stack is complete and ready
4805 * to serve the requests */
4807 mdt_init_capa_ctxt(env, m);
4809 /* Reduce the initial timeout on an MDS because it doesn't need such
4810 * a long timeout as an OST does. Adaptive timeouts will adjust this
4811 * value appropriately. */
4812 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4813 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4820 target_recovery_fini(obd);
4821 upcall_cache_cleanup(m->mdt_identity_cache);
4822 m->mdt_identity_cache = NULL;
4824 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4825 mdt_fs_cleanup(env, m);
4827 cfs_timer_disarm(&m->mdt_ck_timer);
4828 mdt_ck_thread_stop(m);
4830 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4831 obd->obd_namespace = m->mdt_namespace = NULL;
4833 mdt_seq_fini(env, m);
4835 mdt_fld_fini(env, m);
4837 tgt_fini(env, &m->mdt_lut);
4839 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4842 server_put_mount(dev, lmi->lmi_mnt);
4846 /* For interoperability, the left element is old parameter, the right one
4847 * is the new version of the parameter, if some parameter is deprecated,
4848 * the new version should be set as NULL. */
4849 static struct cfg_interop_param mdt_interop_param[] = {
4850 { "mdt.group_upcall", NULL },
4851 { "mdt.quota_type", NULL },
4852 { "mdd.quota_type", NULL },
4853 { "mdt.rootsquash", "mdt.root_squash" },
4854 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4858 /* used by MGS to process specific configurations */
4859 static int mdt_process_config(const struct lu_env *env,
4860 struct lu_device *d, struct lustre_cfg *cfg)
4862 struct mdt_device *m = mdt_dev(d);
4863 struct md_device *md_next = m->mdt_child;
4864 struct lu_device *next = md2lu_dev(md_next);
4868 switch (cfg->lcfg_command) {
4870 struct lprocfs_static_vars lvars;
4871 struct obd_device *obd = d->ld_obd;
4873 /* For interoperability */
4874 struct cfg_interop_param *ptr = NULL;
4875 struct lustre_cfg *old_cfg = NULL;
4878 param = lustre_cfg_string(cfg, 1);
4879 if (param == NULL) {
4880 CERROR("param is empty\n");
4885 ptr = class_find_old_param(param, mdt_interop_param);
4887 if (ptr->new_param == NULL) {
4889 CWARN("For interoperability, skip this %s."
4890 " It is obsolete.\n", ptr->old_param);
4894 CWARN("Found old param %s, changed it to %s.\n",
4895 ptr->old_param, ptr->new_param);
4898 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4905 lprocfs_mdt_init_vars(&lvars);
4906 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4908 if (rc > 0 || rc == -ENOSYS)
4909 /* we don't understand; pass it on */
4910 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4912 if (old_cfg != NULL)
4913 lustre_cfg_free(cfg);
4918 /* others are passed further */
4919 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4925 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4926 const struct lu_object_header *hdr,
4927 struct lu_device *d)
4929 struct mdt_object *mo;
4933 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
4935 struct lu_object *o;
4936 struct lu_object_header *h;
4938 o = &mo->mot_obj.mo_lu;
4939 h = &mo->mot_header;
4940 lu_object_header_init(h);
4941 lu_object_init(o, h, d);
4942 lu_object_add_top(h, o);
4943 o->lo_ops = &mdt_obj_ops;
4944 mutex_init(&mo->mot_ioepoch_mutex);
4945 mutex_init(&mo->mot_lov_mutex);
4951 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4952 const struct lu_object_conf *unused)
4954 struct mdt_device *d = mdt_dev(o->lo_dev);
4955 struct lu_device *under;
4956 struct lu_object *below;
4960 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4961 PFID(lu_object_fid(o)));
4963 under = &d->mdt_child->md_lu_dev;
4964 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4965 if (below != NULL) {
4966 lu_object_add(o, below);
4973 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4975 struct mdt_object *mo = mdt_obj(o);
4976 struct lu_object_header *h;
4980 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4981 PFID(lu_object_fid(o)));
4984 lu_object_header_fini(h);
4985 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4990 static int mdt_object_print(const struct lu_env *env, void *cookie,
4991 lu_printer_t p, const struct lu_object *o)
4993 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4994 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4995 "flags="LPX64", epochcount=%d, writecount=%d)",
4996 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4997 mdto->mot_ioepoch_count, mdto->mot_writecount);
5000 static int mdt_prepare(const struct lu_env *env,
5001 struct lu_device *pdev,
5002 struct lu_device *cdev)
5004 struct mdt_device *mdt = mdt_dev(cdev);
5005 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5006 struct obd_device *obd = cdev->ld_obd;
5013 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5017 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5021 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5022 OBD_IOC_START_LFSCK,
5025 CWARN("Fail to auto trigger paused LFSCK.\n");
5027 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5028 &mdt->mdt_md_root_fid);
5032 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5033 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5034 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5035 LASSERT(obd->obd_no_conn);
5036 spin_lock(&obd->obd_dev_lock);
5037 obd->obd_no_conn = 0;
5038 spin_unlock(&obd->obd_dev_lock);
5040 if (obd->obd_recovering == 0)
5041 mdt_postrecov(env, mdt);
5046 const struct lu_device_operations mdt_lu_ops = {
5047 .ldo_object_alloc = mdt_object_alloc,
5048 .ldo_process_config = mdt_process_config,
5049 .ldo_prepare = mdt_prepare,
5052 static const struct lu_object_operations mdt_obj_ops = {
5053 .loo_object_init = mdt_object_init,
5054 .loo_object_free = mdt_object_free,
5055 .loo_object_print = mdt_object_print
5058 static int mdt_obd_set_info_async(const struct lu_env *env,
5059 struct obd_export *exp,
5060 __u32 keylen, void *key,
5061 __u32 vallen, void *val,
5062 struct ptlrpc_request_set *set)
5064 struct obd_device *obd = exp->exp_obd;
5070 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5071 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5079 * Match client and server connection feature flags.
5081 * Compute the compatibility flags for a connection request based on
5082 * features mutually supported by client and server.
5084 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5085 * must not be updated here, otherwise a partially initialized value may
5086 * be exposed. After the connection request is successfully processed,
5087 * the top-level MDT connect request handler atomically updates the export
5088 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5089 * reply. \see mdt_connect().
5091 * \param exp the obd_export associated with this client/target pair
5092 * \param mdt the target device for the connection
5093 * \param data stores data for this connect request
5096 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5097 * \retval -EBADE client and server feature requirements are incompatible
5099 static int mdt_connect_internal(struct obd_export *exp,
5100 struct mdt_device *mdt,
5101 struct obd_connect_data *data)
5103 LASSERT(data != NULL);
5105 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5106 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5108 /* If no known bits (which should not happen, probably,
5109 as everybody should support LOOKUP and UPDATE bits at least)
5110 revert to compat mode with plain locks. */
5111 if (!data->ocd_ibits_known &&
5112 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5113 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5115 if (!mdt->mdt_opts.mo_acl)
5116 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5118 if (!mdt->mdt_opts.mo_user_xattr)
5119 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5121 if (!mdt->mdt_som_conf)
5122 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5124 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5125 data->ocd_brw_size = min(data->ocd_brw_size,
5126 (__u32)MD_MAX_BRW_SIZE);
5127 if (data->ocd_brw_size == 0) {
5128 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5129 " ocd_version: %x ocd_grant: %d "
5130 "ocd_index: %u ocd_brw_size is "
5131 "unexpectedly zero, network data "
5132 "corruption? Refusing connection of this"
5134 exp->exp_obd->obd_name,
5135 exp->exp_client_uuid.uuid,
5136 exp, data->ocd_connect_flags, data->ocd_version,
5137 data->ocd_grant, data->ocd_index);
5142 /* NB: Disregard the rule against updating
5143 * exp_connect_data.ocd_connect_flags in this case, since
5144 * tgt_client_new() needs to know if this is a lightweight
5145 * connection, and it is safe to expose this flag before
5146 * connection processing completes. */
5147 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5148 spin_lock(&exp->exp_lock);
5149 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5150 spin_unlock(&exp->exp_lock);
5153 data->ocd_version = LUSTRE_VERSION_CODE;
5154 exp->exp_connect_data = *data;
5155 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5157 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5158 CWARN("%s: MDS requires FID support, but client not\n",
5159 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5163 if (mdt->mdt_som_conf &&
5164 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5165 OBD_CONNECT_MDS_MDS |
5166 OBD_CONNECT_SOM))) {
5167 CWARN("%s: MDS has SOM enabled, but client does not support "
5168 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5172 if (data->ocd_connect_flags & OBD_CONNECT_PINGLESS) {
5173 if (suppress_pings) {
5174 spin_lock(&exp->exp_obd->obd_dev_lock);
5175 list_del_init(&exp->exp_obd_chain_timed);
5176 spin_unlock(&exp->exp_obd->obd_dev_lock);
5178 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5185 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5186 struct obd_export *exp,
5187 struct ptlrpc_request *req)
5189 struct sptlrpc_flavor flvr;
5192 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5193 read_lock(&mdt->mdt_sptlrpc_lock);
5194 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5198 read_unlock(&mdt->mdt_sptlrpc_lock);
5200 spin_lock(&exp->exp_lock);
5202 exp->exp_sp_peer = req->rq_sp_from;
5203 exp->exp_flvr = flvr;
5205 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5206 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5207 CERROR("unauthorized rpc flavor %x from %s, "
5208 "expect %x\n", req->rq_flvr.sf_rpc,
5209 libcfs_nid2str(req->rq_peer.nid),
5210 exp->exp_flvr.sf_rpc);
5214 spin_unlock(&exp->exp_lock);
5216 if (exp->exp_sp_peer != req->rq_sp_from) {
5217 CERROR("RPC source %s doesn't match %s\n",
5218 sptlrpc_part2name(req->rq_sp_from),
5219 sptlrpc_part2name(exp->exp_sp_peer));
5222 rc = sptlrpc_target_export_check(exp, req);
5229 /* mds_connect copy */
5230 static int mdt_obd_connect(const struct lu_env *env,
5231 struct obd_export **exp, struct obd_device *obd,
5232 struct obd_uuid *cluuid,
5233 struct obd_connect_data *data,
5236 struct mdt_thread_info *info;
5237 struct obd_export *lexp;
5238 struct lustre_handle conn = { 0 };
5239 struct mdt_device *mdt;
5240 struct ptlrpc_request *req;
5244 LASSERT(env != NULL);
5245 if (!exp || !obd || !cluuid)
5248 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5249 req = info->mti_pill->rc_req;
5250 mdt = mdt_dev(obd->obd_lu_dev);
5253 * first, check whether the stack is ready to handle requests
5254 * XXX: probably not very appropriate method is used now
5255 * at some point we should find a better one
5257 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5258 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5259 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5262 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5265 rc = class_connect(&conn, obd, cluuid);
5269 lexp = class_conn2export(&conn);
5270 LASSERT(lexp != NULL);
5272 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5276 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5277 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5279 rc = mdt_connect_internal(lexp, mdt, data);
5281 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5284 info->mti_exp = lexp;
5285 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5286 rc = tgt_client_new(env, lexp);
5288 mdt_export_stats_init(obd, lexp, localdata);
5293 class_disconnect(lexp);
5302 static int mdt_obd_reconnect(const struct lu_env *env,
5303 struct obd_export *exp, struct obd_device *obd,
5304 struct obd_uuid *cluuid,
5305 struct obd_connect_data *data,
5308 struct mdt_thread_info *info;
5309 struct mdt_device *mdt;
5310 struct ptlrpc_request *req;
5314 if (exp == NULL || obd == NULL || cluuid == NULL)
5317 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5318 req = info->mti_pill->rc_req;
5319 mdt = mdt_dev(obd->obd_lu_dev);
5321 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5325 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5327 mdt_export_stats_init(obd, exp, localdata);
5332 static int mdt_export_cleanup(struct obd_export *exp)
5334 struct mdt_export_data *med = &exp->exp_mdt_data;
5335 struct obd_device *obd = exp->exp_obd;
5336 struct mdt_device *mdt;
5337 struct mdt_thread_info *info;
5339 CFS_LIST_HEAD(closing_list);
5340 struct mdt_file_data *mfd, *n;
5344 spin_lock(&med->med_open_lock);
5345 while (!cfs_list_empty(&med->med_open_head)) {
5346 cfs_list_t *tmp = med->med_open_head.next;
5347 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5349 /* Remove mfd handle so it can't be found again.
5350 * We are consuming the mfd_list reference here. */
5351 class_handle_unhash(&mfd->mfd_handle);
5352 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5354 spin_unlock(&med->med_open_lock);
5355 mdt = mdt_dev(obd->obd_lu_dev);
5356 LASSERT(mdt != NULL);
5358 rc = lu_env_init(&env, LCT_MD_THREAD);
5362 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5363 LASSERT(info != NULL);
5364 memset(info, 0, sizeof *info);
5365 info->mti_env = &env;
5366 info->mti_mdt = mdt;
5367 info->mti_exp = exp;
5369 if (!cfs_list_empty(&closing_list)) {
5370 struct md_attr *ma = &info->mti_attr;
5372 /* Close any open files (which may also cause orphan unlinking). */
5373 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5374 cfs_list_del_init(&mfd->mfd_list);
5375 ma->ma_need = ma->ma_valid = 0;
5376 /* Don't unlink orphan on failover umount, LU-184 */
5377 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5378 ma->ma_valid = MA_FLAGS;
5379 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5381 mdt_mfd_close(info, mfd);
5384 info->mti_mdt = NULL;
5385 /* cleanup client slot early */
5386 /* Do not erase record for recoverable client. */
5387 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5388 tgt_client_del(&env, exp);
5394 static int mdt_obd_disconnect(struct obd_export *exp)
5400 class_export_get(exp);
5402 rc = server_disconnect_export(exp);
5404 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5406 rc = mdt_export_cleanup(exp);
5407 class_export_put(exp);
5411 /* FIXME: Can we avoid using these two interfaces? */
5412 static int mdt_init_export(struct obd_export *exp)
5414 struct mdt_export_data *med = &exp->exp_mdt_data;
5418 CFS_INIT_LIST_HEAD(&med->med_open_head);
5419 spin_lock_init(&med->med_open_lock);
5420 mutex_init(&med->med_idmap_mutex);
5421 med->med_idmap = NULL;
5422 spin_lock(&exp->exp_lock);
5423 exp->exp_connecting = 1;
5424 spin_unlock(&exp->exp_lock);
5426 /* self-export doesn't need client data and ldlm initialization */
5427 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5428 &exp->exp_client_uuid)))
5431 rc = tgt_client_alloc(exp);
5435 rc = ldlm_init_export(exp);
5442 tgt_client_free(exp);
5444 CERROR("%s: Failed to initialize export: rc = %d\n",
5445 exp->exp_obd->obd_name, rc);
5449 static int mdt_destroy_export(struct obd_export *exp)
5453 if (exp_connect_rmtclient(exp))
5454 mdt_cleanup_idmap(&exp->exp_mdt_data);
5456 target_destroy_export(exp);
5457 /* destroy can be called from failed obd_setup, so
5458 * checking uuid is safer than obd_self_export */
5459 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5460 &exp->exp_client_uuid)))
5463 ldlm_destroy_export(exp);
5464 tgt_client_free(exp);
5466 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5467 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5472 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5473 void *val, int vallen)
5475 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5476 struct getinfo_fid2path *fpout, *fpin;
5479 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5482 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5483 lustre_swab_fid2path(fpin);
5485 memcpy(fpout, fpin, sizeof(*fpin));
5486 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5489 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5493 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5494 struct getinfo_fid2path *fp)
5496 struct mdt_object *obj;
5497 struct obd_device *obd = mdt2obd_dev(mdt);
5501 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5502 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5504 if (!fid_is_sane(&fp->gf_fid))
5507 if (!fid_is_client_mdt_visible(&fp->gf_fid)) {
5508 CWARN("%s: "DFID" is invalid, sequence should be "
5509 ">= "LPX64"\n", obd->obd_name,
5510 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5514 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5515 if (obj == NULL || IS_ERR(obj)) {
5516 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5521 rc = lu_object_exists(&obj->mot_obj.mo_lu);
5527 mdt_object_put(env, obj);
5528 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5529 PFID(&fp->gf_fid), rc);
5533 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5534 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5535 mdt_object_put(env, obj);
5540 int mdt_get_info(struct mdt_thread_info *info)
5542 struct ptlrpc_request *req = mdt_info_req(info);
5550 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5552 CDEBUG(D_IOCTL, "No GETINFO key");
5555 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5558 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5559 if (vallen == NULL) {
5560 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5564 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5566 rc = req_capsule_server_pack(info->mti_pill);
5567 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5568 if (valout == NULL) {
5569 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5573 if (KEY_IS(KEY_FID2PATH))
5574 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5578 lustre_msg_set_status(req->rq_repmsg, rc);
5583 /* Pass the ioc down */
5584 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5585 unsigned int cmd, int len, void *data)
5587 struct lu_context ioctl_session;
5588 struct md_device *next = mdt->mdt_child;
5592 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5595 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5596 lu_context_enter(&ioctl_session);
5597 env->le_ses = &ioctl_session;
5599 LASSERT(next->md_ops->mdo_iocontrol);
5600 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5602 lu_context_exit(&ioctl_session);
5603 lu_context_fini(&ioctl_session);
5607 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5609 struct obd_ioctl_data *data = karg;
5610 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5612 struct mdt_object *obj;
5613 struct mdt_lock_handle *lh;
5617 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5618 if (!fid_is_sane(fid))
5621 lh = &mti->mti_lh[MDT_LH_PARENT];
5622 mdt_lock_reg_init(lh, LCK_CR);
5624 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5626 RETURN(PTR_ERR(obj));
5628 if (mdt_object_remote(obj)) {
5631 * before calling version get the correct MDS should be
5632 * fid, this is error to find remote object here
5634 CERROR("nonlocal object "DFID"\n", PFID(fid));
5635 } else if (!mdt_object_exists(obj)) {
5636 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5639 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5640 *(__u64 *)data->ioc_inlbuf2 = version;
5643 mdt_object_unlock_put(mti, obj, lh, 1);
5647 /* ioctls on obd dev */
5648 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5649 void *karg, void *uarg)
5652 struct obd_device *obd = exp->exp_obd;
5653 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5654 struct dt_device *dt = mdt->mdt_bottom;
5658 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5659 rc = lu_env_init(&env, LCT_MD_THREAD);
5665 rc = mdt_device_sync(&env, mdt);
5667 case OBD_IOC_SET_READONLY:
5668 rc = dt->dd_ops->dt_ro(&env, dt);
5670 case OBD_IOC_ABORT_RECOVERY:
5671 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5672 target_stop_recovery_thread(obd);
5675 case OBD_IOC_CHANGELOG_REG:
5676 case OBD_IOC_CHANGELOG_DEREG:
5677 case OBD_IOC_CHANGELOG_CLEAR:
5678 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5680 case OBD_IOC_START_LFSCK:
5681 case OBD_IOC_STOP_LFSCK: {
5682 struct md_device *next = mdt->mdt_child;
5683 struct obd_ioctl_data *data = karg;
5685 if (unlikely(data == NULL)) {
5690 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
5695 case OBD_IOC_GET_OBJ_VERSION: {
5696 struct mdt_thread_info *mti;
5697 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5698 memset(mti, 0, sizeof *mti);
5699 mti->mti_env = &env;
5703 rc = mdt_ioc_version_get(mti, karg);
5707 CERROR("Not supported cmd = %d for device %s\n",
5708 cmd, obd->obd_name);
5716 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5718 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5722 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5726 int mdt_obd_postrecov(struct obd_device *obd)
5731 rc = lu_env_init(&env, LCT_MD_THREAD);
5734 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5739 static struct obd_ops mdt_obd_device_ops = {
5740 .o_owner = THIS_MODULE,
5741 .o_set_info_async = mdt_obd_set_info_async,
5742 .o_connect = mdt_obd_connect,
5743 .o_reconnect = mdt_obd_reconnect,
5744 .o_disconnect = mdt_obd_disconnect,
5745 .o_init_export = mdt_init_export,
5746 .o_destroy_export = mdt_destroy_export,
5747 .o_iocontrol = mdt_iocontrol,
5748 .o_postrecov = mdt_obd_postrecov,
5751 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5752 struct lu_device *d)
5754 struct mdt_device *m = mdt_dev(d);
5761 static struct lu_device *mdt_device_free(const struct lu_env *env,
5762 struct lu_device *d)
5764 struct mdt_device *m = mdt_dev(d);
5767 md_device_fini(&m->mdt_md_dev);
5772 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5773 struct lu_device_type *t,
5774 struct lustre_cfg *cfg)
5776 struct lu_device *l;
5777 struct mdt_device *m;
5783 l = &m->mdt_md_dev.md_lu_dev;
5784 rc = mdt_init0(env, m, t, cfg);
5786 mdt_device_free(env, l);
5791 l = ERR_PTR(-ENOMEM);
5795 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5796 LU_KEY_INIT(mdt, struct mdt_thread_info);
5798 static void mdt_key_fini(const struct lu_context *ctx,
5799 struct lu_context_key *key, void* data)
5801 struct mdt_thread_info *info = data;
5803 if (info->mti_big_lmm) {
5804 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5805 info->mti_big_lmm = NULL;
5806 info->mti_big_lmmsize = 0;
5811 /* context key: mdt_thread_key */
5812 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5814 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5816 return lu_ucred(info->mti_env);
5819 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5821 return lu_ucred_check(info->mti_env);
5825 * Enable/disable COS (Commit On Sharing).
5827 * Set/Clear the COS flag in mdt options.
5829 * \param mdt mdt device
5830 * \param val 0 disables COS, other values enable COS
5832 void mdt_enable_cos(struct mdt_device *mdt, int val)
5837 mdt->mdt_opts.mo_cos = !!val;
5838 rc = lu_env_init(&env, LCT_LOCAL);
5839 if (unlikely(rc != 0)) {
5840 CWARN("lu_env initialization failed with rc = %d,"
5841 "cannot sync\n", rc);
5844 mdt_device_sync(&env, mdt);
5849 * Check COS (Commit On Sharing) status.
5851 * Return COS flag status.
5853 * \param mdt mdt device
5855 int mdt_cos_is_enabled(struct mdt_device *mdt)
5857 return mdt->mdt_opts.mo_cos != 0;
5860 static struct lu_device_type_operations mdt_device_type_ops = {
5861 .ldto_device_alloc = mdt_device_alloc,
5862 .ldto_device_free = mdt_device_free,
5863 .ldto_device_fini = mdt_device_fini
5866 static struct lu_device_type mdt_device_type = {
5867 .ldt_tags = LU_DEVICE_MD,
5868 .ldt_name = LUSTRE_MDT_NAME,
5869 .ldt_ops = &mdt_device_type_ops,
5870 .ldt_ctx_tags = LCT_MD_THREAD
5873 static int __init mdt_mod_init(void)
5875 struct lprocfs_static_vars lvars;
5878 rc = lu_kmem_init(mdt_caches);
5882 rc = mds_mod_init();
5886 lprocfs_mdt_init_vars(&lvars);
5887 rc = class_register_type(&mdt_obd_device_ops, NULL,
5888 lvars.module_vars, LUSTRE_MDT_NAME,
5894 lu_kmem_fini(mdt_caches);
5901 static void __exit mdt_mod_exit(void)
5903 class_unregister_type(LUSTRE_MDT_NAME);
5905 lu_kmem_fini(mdt_caches);
5908 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5909 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5910 MODULE_LICENSE("GPL");
5912 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);