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 /* Return -EIO for old client */
690 if (!mdt_is_dne_client(req->rq_export))
691 GOTO(out, rc = -EIO);
693 repbody->fid1 = *mdt_object_fid(o);
694 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
698 buffer->lb_len = reqbody->eadatasize;
699 if (buffer->lb_len > 0)
700 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
702 buffer->lb_buf = NULL;
704 /* If it is dir object and client require MEA, then we got MEA */
705 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
706 reqbody->valid & OBD_MD_MEA) {
707 /* Assumption: MDT_MD size is enough for lmv size. */
708 ma->ma_lmv = buffer->lb_buf;
709 ma->ma_lmv_size = buffer->lb_len;
710 ma->ma_need = MA_LMV | MA_INODE;
712 ma->ma_lmm = buffer->lb_buf;
713 ma->ma_lmm_size = buffer->lb_len;
714 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
717 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
718 reqbody->valid & OBD_MD_FLDIREA &&
719 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
720 /* get default stripe info for this dir. */
721 ma->ma_need |= MA_LOV_DEF;
723 ma->ma_need |= ma_need;
724 if (ma->ma_need & MA_SOM)
725 ma->ma_som = &info->mti_u.som.data;
727 rc = mdt_attr_get_complex(info, o, ma);
729 CERROR("getattr error for "DFID": %d\n",
730 PFID(mdt_object_fid(o)), rc);
734 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
736 /* the Lustre protocol supposes to return default striping
737 * on the user-visible root if explicitly requested */
738 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
739 (ma->ma_need & MA_LOV_DEF && is_root) && (ma->ma_need & MA_LOV)) {
740 struct lu_fid rootfid;
741 struct mdt_object *root;
742 struct mdt_device *mdt = info->mti_mdt;
744 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
747 root = mdt_object_find(env, mdt, &rootfid);
749 RETURN(PTR_ERR(root));
750 rc = mdt_attr_get_lov(info, root, ma);
751 mdt_object_put(info->mti_env, root);
753 CERROR("getattr error for "DFID": %d\n",
754 PFID(mdt_object_fid(o)), rc);
759 if (likely(ma->ma_valid & MA_INODE))
760 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
764 if (mdt_body_has_lov(la, reqbody)) {
765 if (ma->ma_valid & MA_LOV) {
766 LASSERT(ma->ma_lmm_size);
767 mdt_dump_lmm(D_INFO, ma->ma_lmm);
768 repbody->eadatasize = ma->ma_lmm_size;
769 if (S_ISDIR(la->la_mode))
770 repbody->valid |= OBD_MD_FLDIREA;
772 repbody->valid |= OBD_MD_FLEASIZE;
774 if (ma->ma_valid & MA_LMV) {
775 LASSERT(S_ISDIR(la->la_mode));
776 repbody->eadatasize = ma->ma_lmv_size;
777 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
779 } else if (S_ISLNK(la->la_mode) &&
780 reqbody->valid & OBD_MD_LINKNAME) {
781 buffer->lb_buf = ma->ma_lmm;
782 /* eadatasize from client includes NULL-terminator, so
783 * there is no need to read it */
784 buffer->lb_len = reqbody->eadatasize - 1;
785 rc = mo_readlink(env, next, buffer);
786 if (unlikely(rc <= 0)) {
787 CERROR("readlink failed: %d\n", rc);
790 int print_limit = min_t(int, CFS_PAGE_SIZE - 128, rc);
792 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
794 repbody->valid |= OBD_MD_LINKNAME;
795 /* we need to report back size with NULL-terminator
796 * because client expects that */
797 repbody->eadatasize = rc + 1;
798 if (repbody->eadatasize != reqbody->eadatasize)
799 CERROR("Read shorter symlink %d, expected %d\n",
800 rc, reqbody->eadatasize - 1);
802 ((char *)ma->ma_lmm)[rc] = 0;
804 /* If the total CDEBUG() size is larger than a page, it
805 * will print a warning to the console, avoid this by
806 * printing just the last part of the symlink. */
807 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
808 print_limit < rc ? "..." : "", print_limit,
809 (char *)ma->ma_lmm + rc - print_limit, rc);
814 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
815 repbody->max_cookiesize = 0;
816 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
817 repbody->valid |= OBD_MD_FLMODEASIZE;
818 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
819 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
820 repbody->max_cookiesize);
823 if (exp_connect_rmtclient(info->mti_exp) &&
824 reqbody->valid & OBD_MD_FLRMTPERM) {
825 void *buf = req_capsule_server_get(pill, &RMF_ACL);
827 /* mdt_getattr_lock only */
828 rc = mdt_pack_remote_perm(info, o, buf);
830 repbody->valid &= ~OBD_MD_FLRMTPERM;
831 repbody->aclsize = 0;
834 repbody->valid |= OBD_MD_FLRMTPERM;
835 repbody->aclsize = sizeof(struct mdt_remote_perm);
838 #ifdef CONFIG_FS_POSIX_ACL
839 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
840 (reqbody->valid & OBD_MD_FLACL)) {
841 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
842 buffer->lb_len = req_capsule_get_size(pill,
843 &RMF_ACL, RCL_SERVER);
844 if (buffer->lb_len > 0) {
845 rc = mo_xattr_get(env, next, buffer,
846 XATTR_NAME_ACL_ACCESS);
848 if (rc == -ENODATA) {
849 repbody->aclsize = 0;
850 repbody->valid |= OBD_MD_FLACL;
852 } else if (rc == -EOPNOTSUPP) {
855 CERROR("got acl size: %d\n", rc);
858 repbody->aclsize = rc;
859 repbody->valid |= OBD_MD_FLACL;
866 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
867 info->mti_mdt->mdt_opts.mo_mds_capa &&
868 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
869 struct lustre_capa *capa;
871 capa = req_capsule_server_get(pill, &RMF_CAPA1);
873 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
874 rc = mo_capa_get(env, next, capa, 0);
877 repbody->valid |= OBD_MD_FLMDSCAPA;
882 mdt_counter_incr(req, LPROC_MDT_GETATTR);
887 static int mdt_renew_capa(struct mdt_thread_info *info)
889 struct mdt_object *obj = info->mti_object;
890 struct mdt_body *body;
891 struct lustre_capa *capa, *c;
895 /* if object doesn't exist, or server has disabled capability,
896 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
899 if (!obj || !info->mti_mdt->mdt_opts.mo_oss_capa ||
900 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
903 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
904 LASSERT(body != NULL);
906 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
909 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
913 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
915 body->valid |= OBD_MD_FLOSSCAPA;
919 int mdt_getattr(struct mdt_thread_info *info)
921 struct mdt_object *obj = info->mti_object;
922 struct req_capsule *pill = info->mti_pill;
923 struct mdt_body *reqbody;
924 struct mdt_body *repbody;
929 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
932 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
933 rc = req_capsule_server_pack(pill);
935 RETURN(err_serious(rc));
936 rc = mdt_renew_capa(info);
937 GOTO(out_shrink, rc);
940 LASSERT(obj != NULL);
941 LASSERT(lu_object_assert_exists(&obj->mot_obj.mo_lu));
943 mode = lu_object_attr(&obj->mot_obj.mo_lu);
945 /* old clients may not report needed easize, use max value then */
946 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
947 reqbody->eadatasize == 0 ?
948 info->mti_mdt->mdt_max_mdsize :
949 reqbody->eadatasize);
951 rc = req_capsule_server_pack(pill);
952 if (unlikely(rc != 0))
953 RETURN(err_serious(rc));
955 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
956 LASSERT(repbody != NULL);
957 repbody->eadatasize = 0;
958 repbody->aclsize = 0;
960 if (reqbody->valid & OBD_MD_FLRMTPERM)
961 rc = mdt_init_ucred(info, reqbody);
963 rc = mdt_check_ucred(info);
965 GOTO(out_shrink, rc);
967 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
970 * Don't check capability at all, because rename might getattr for
971 * remote obj, and at that time no capability is available.
973 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
974 rc = mdt_getattr_internal(info, obj, 0);
975 if (reqbody->valid & OBD_MD_FLRMTPERM)
976 mdt_exit_ucred(info);
979 mdt_client_compatibility(info);
980 rc2 = mdt_fix_reply(info);
986 int mdt_is_subdir(struct mdt_thread_info *info)
988 struct mdt_object *o = info->mti_object;
989 struct req_capsule *pill = info->mti_pill;
990 const struct mdt_body *body = info->mti_body;
991 struct mdt_body *repbody;
997 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1000 * We save last checked parent fid to @repbody->fid1 for remote
1003 LASSERT(fid_is_sane(&body->fid2));
1004 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1005 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1006 &body->fid2, &repbody->fid1);
1007 if (rc == 0 || rc == -EREMOTE)
1008 repbody->valid |= OBD_MD_FLID;
1013 int mdt_swap_layouts(struct mdt_thread_info *info)
1015 struct ptlrpc_request *req = mdt_info_req(info);
1016 struct obd_export *exp = req->rq_export;
1017 struct mdt_object *o1, *o2, *o;
1018 struct mdt_lock_handle *lh1, *lh2;
1019 struct mdc_swap_layouts *msl;
1023 /* client does not support layout lock, so layout swaping
1025 * FIXME: there is a problem for old clients which don't support
1026 * layout lock yet. If those clients have already opened the file
1027 * they won't be notified at all so that old layout may still be
1028 * used to do IO. This can be fixed after file release is landed by
1029 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1030 if (!exp_connect_layout(exp))
1031 RETURN(-EOPNOTSUPP);
1033 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1034 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1035 req_capsule_client_get(info->mti_pill,
1038 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1039 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1040 req_capsule_client_get(info->mti_pill,
1043 o1 = info->mti_object;
1044 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1045 &info->mti_body->fid2);
1047 GOTO(out, rc = PTR_ERR(o));
1049 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1050 GOTO(put, rc = -ENOENT);
1052 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1053 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1059 /* permission check. Make sure the calling process having permission
1060 * to write both files. */
1061 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1066 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1071 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1072 LASSERT(msl != NULL);
1074 lh1 = &info->mti_lh[MDT_LH_NEW];
1075 mdt_lock_reg_init(lh1, LCK_EX);
1076 lh2 = &info->mti_lh[MDT_LH_OLD];
1077 mdt_lock_reg_init(lh2, LCK_EX);
1079 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT,
1084 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT,
1089 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1090 mdt_object_child(o2), msl->msl_flags);
1093 mdt_object_unlock(info, o2, lh2, rc);
1095 mdt_object_unlock(info, o1, lh1, rc);
1097 mdt_object_put(info->mti_env, o);
1102 static int mdt_raw_lookup(struct mdt_thread_info *info,
1103 struct mdt_object *parent,
1104 const struct lu_name *lname,
1105 struct ldlm_reply *ldlm_rep)
1107 struct md_object *next = mdt_object_child(info->mti_object);
1108 const struct mdt_body *reqbody = info->mti_body;
1109 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1110 struct mdt_body *repbody;
1114 if (reqbody->valid != OBD_MD_FLID)
1117 LASSERT(!info->mti_cross_ref);
1119 /* Only got the fid of this obj by name */
1120 fid_zero(child_fid);
1121 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1124 /* XXX is raw_lookup possible as intent operation? */
1127 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1130 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1132 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1135 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1136 repbody->fid1 = *child_fid;
1137 repbody->valid = OBD_MD_FLID;
1143 * UPDATE lock should be taken against parent, and be release before exit;
1144 * child_bits lock should be taken against child, and be returned back:
1145 * (1)normal request should release the child lock;
1146 * (2)intent request will grant the lock to client.
1148 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1149 struct mdt_lock_handle *lhc,
1151 struct ldlm_reply *ldlm_rep)
1153 struct ptlrpc_request *req = mdt_info_req(info);
1154 struct mdt_body *reqbody = NULL;
1155 struct mdt_object *parent = info->mti_object;
1156 struct mdt_object *child;
1157 struct md_object *next = mdt_object_child(parent);
1158 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1159 struct lu_name *lname = NULL;
1160 const char *name = NULL;
1162 struct mdt_lock_handle *lhp = NULL;
1163 struct ldlm_lock *lock;
1164 struct ldlm_res_id *res_id;
1171 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1172 LASSERT(ergo(is_resent,
1173 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1175 LASSERT(parent != NULL);
1176 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1178 RETURN(err_serious(-EFAULT));
1180 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1182 if (!info->mti_cross_ref) {
1184 * XXX: Check for "namelen == 0" is for getattr by fid
1185 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1186 * that is the name must contain at least one character and
1187 * the terminating '\0'
1190 reqbody = req_capsule_client_get(info->mti_pill,
1192 if (unlikely(reqbody == NULL))
1193 RETURN(err_serious(-EFAULT));
1195 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1196 RETURN(err_serious(-EINVAL));
1199 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1201 PFID(mdt_object_fid(parent)),
1202 PFID(&reqbody->fid2), ldlm_rep);
1204 lname = mdt_name(info->mti_env, (char *)name, namelen);
1205 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1206 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1210 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1212 if (unlikely(!mdt_object_exists(parent))) {
1213 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1214 &parent->mot_obj.mo_lu,
1215 "Parent doesn't exist!\n");
1217 } else if (!info->mti_cross_ref) {
1218 LASSERTF(!mdt_object_remote(parent),
1219 "Parent "DFID" is on remote server\n",
1220 PFID(mdt_object_fid(parent)));
1223 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1231 if (info->mti_cross_ref) {
1232 /* Only getattr on the child. Parent is on another node. */
1233 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1235 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1236 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1239 /* Do not take lock for resent case. */
1240 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1241 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1242 lhc->mlh_reg_lh.cookie);
1243 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1244 &lock->l_resource->lr_name));
1245 LDLM_LOCK_PUT(lock);
1248 mdt_lock_handle_init(lhc);
1249 mdt_lock_reg_init(lhc, LCK_PR);
1252 * Object's name is on another MDS, no lookup lock is
1253 * needed here but update is.
1255 child_bits &= ~MDS_INODELOCK_LOOKUP;
1256 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1258 rc = mdt_object_lock(info, child, lhc, child_bits,
1262 /* Finally, we can get attr for child. */
1263 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1265 rc = mdt_getattr_internal(info, child, 0);
1266 if (unlikely(rc != 0))
1267 mdt_object_unlock(info, child, lhc, 1);
1273 /* step 1: lock parent only if parent is a directory */
1274 if (S_ISDIR(lu_object_attr(&parent->mot_obj.mo_lu))) {
1275 lhp = &info->mti_lh[MDT_LH_PARENT];
1276 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1277 rc = mdt_object_lock(info, parent, lhp,
1278 MDS_INODELOCK_UPDATE,
1280 if (unlikely(rc != 0))
1284 /* step 2: lookup child's fid by name */
1285 fid_zero(child_fid);
1286 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1291 mdt_set_disposition(info, ldlm_rep,
1293 GOTO(out_parent, rc);
1295 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1297 *child_fid = reqbody->fid2;
1298 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1302 *step 3: find the child object by fid & lock it.
1303 * regardless if it is local or remote.
1305 child = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1307 if (unlikely(IS_ERR(child)))
1308 GOTO(out_parent, rc = PTR_ERR(child));
1310 /* Do not take lock for resent case. */
1311 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1312 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1313 lhc->mlh_reg_lh.cookie);
1315 res_id = &lock->l_resource->lr_name;
1316 if (!fid_res_name_eq(mdt_object_fid(child),
1317 &lock->l_resource->lr_name)) {
1318 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1319 &lock->l_resource->lr_name),
1320 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1321 (unsigned long)res_id->name[0],
1322 (unsigned long)res_id->name[1],
1323 (unsigned long)res_id->name[2],
1324 PFID(mdt_object_fid(parent)));
1325 CWARN("Although resent, but still not get child lock"
1326 "parent:"DFID" child:"DFID"\n",
1327 PFID(mdt_object_fid(parent)),
1328 PFID(mdt_object_fid(child)));
1329 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1330 LDLM_LOCK_PUT(lock);
1333 LDLM_LOCK_PUT(lock);
1336 bool try_layout = false;
1339 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1340 mdt_lock_handle_init(lhc);
1341 mdt_lock_reg_init(lhc, LCK_PR);
1343 if (!mdt_object_exists(child)) {
1344 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1345 &child->mot_obj.mo_lu,
1346 "Object doesn't exist!\n");
1347 GOTO(out_child, rc = -ENOENT);
1350 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1351 mdt_object_exists(child) && !mdt_object_remote(child)) {
1352 struct md_attr *ma = &info->mti_attr;
1355 ma->ma_need = MA_INODE;
1356 rc = mdt_attr_get_complex(info, child, ma);
1357 if (unlikely(rc != 0))
1358 GOTO(out_child, rc);
1360 /* If the file has not been changed for some time, we
1361 * return not only a LOOKUP lock, but also an UPDATE
1362 * lock and this might save us RPC on later STAT. For
1363 * directories, it also let negative dentry starts
1364 * working for this dir. */
1365 if (ma->ma_valid & MA_INODE &&
1366 ma->ma_attr.la_valid & LA_CTIME &&
1367 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1368 ma->ma_attr.la_ctime < cfs_time_current_sec())
1369 child_bits |= MDS_INODELOCK_UPDATE;
1372 /* layout lock must be granted in a best-effort way
1373 * for IT operations */
1374 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1375 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1376 exp_connect_layout(info->mti_exp) &&
1377 S_ISREG(lu_object_attr(&child->mot_obj.mo_lu)) &&
1379 /* try to grant layout lock for regular file. */
1385 child_bits |= MDS_INODELOCK_LAYOUT;
1386 /* try layout lock, it may fail to be granted due to
1387 * contention at LOOKUP or UPDATE */
1388 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1390 child_bits &= ~MDS_INODELOCK_LAYOUT;
1391 LASSERT(child_bits != 0);
1392 rc = mdt_object_lock(info, child, lhc,
1393 child_bits, MDT_CROSS_LOCK);
1398 rc = mdt_object_lock(info, child, lhc, child_bits,
1401 if (unlikely(rc != 0))
1402 GOTO(out_child, rc);
1405 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1406 /* Get MA_SOM attributes if update lock is given. */
1408 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1409 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1412 /* finally, we can get attr for child. */
1413 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1414 rc = mdt_getattr_internal(info, child, ma_need);
1415 if (unlikely(rc != 0)) {
1416 mdt_object_unlock(info, child, lhc, 1);
1418 /* Debugging code. */
1419 res_id = &lock->l_resource->lr_name;
1420 LDLM_DEBUG(lock, "Returning lock to client");
1421 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1422 &lock->l_resource->lr_name),
1423 "Lock res_id: %lu/%lu/%lu, Fid: "DFID".\n",
1424 (unsigned long)res_id->name[0],
1425 (unsigned long)res_id->name[1],
1426 (unsigned long)res_id->name[2],
1427 PFID(mdt_object_fid(child)));
1428 if (mdt_object_exists(child) && !mdt_object_remote(child))
1429 mdt_pack_size2body(info, child);
1432 LDLM_LOCK_PUT(lock);
1436 mdt_object_put(info->mti_env, child);
1439 mdt_object_unlock(info, parent, lhp, 1);
1443 /* normal handler: should release the child lock */
1444 int mdt_getattr_name(struct mdt_thread_info *info)
1446 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1447 struct mdt_body *reqbody;
1448 struct mdt_body *repbody;
1452 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1453 LASSERT(reqbody != NULL);
1454 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1455 LASSERT(repbody != NULL);
1457 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1458 repbody->eadatasize = 0;
1459 repbody->aclsize = 0;
1461 rc = mdt_init_ucred(info, reqbody);
1463 GOTO(out_shrink, rc);
1465 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1466 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1467 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1468 lhc->mlh_reg_lh.cookie = 0;
1470 mdt_exit_ucred(info);
1473 mdt_client_compatibility(info);
1474 rc2 = mdt_fix_reply(info);
1480 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1481 void *karg, void *uarg);
1483 int mdt_set_info(struct mdt_thread_info *info)
1485 struct ptlrpc_request *req = mdt_info_req(info);
1488 int keylen, vallen, rc = 0;
1491 rc = req_capsule_server_pack(info->mti_pill);
1495 key = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_KEY);
1497 DEBUG_REQ(D_HA, req, "no set_info key");
1501 keylen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_KEY,
1504 val = req_capsule_client_get(info->mti_pill, &RMF_SETINFO_VAL);
1506 DEBUG_REQ(D_HA, req, "no set_info val");
1510 vallen = req_capsule_get_size(info->mti_pill, &RMF_SETINFO_VAL,
1513 /* Swab any part of val you need to here */
1514 if (KEY_IS(KEY_READ_ONLY)) {
1516 lustre_msg_set_status(req->rq_repmsg, 0);
1518 spin_lock(&req->rq_export->exp_lock);
1520 *exp_connect_flags_ptr(req->rq_export) |=
1523 *exp_connect_flags_ptr(req->rq_export) &=
1524 ~OBD_CONNECT_RDONLY;
1525 spin_unlock(&req->rq_export->exp_lock);
1527 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1528 struct changelog_setinfo *cs =
1529 (struct changelog_setinfo *)val;
1530 if (vallen != sizeof(*cs)) {
1531 CERROR("Bad changelog_clear setinfo size %d\n", vallen);
1534 if (ptlrpc_req_need_swab(req)) {
1535 __swab64s(&cs->cs_recno);
1536 __swab32s(&cs->cs_id);
1539 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, info->mti_exp,
1541 lustre_msg_set_status(req->rq_repmsg, rc);
1550 * Top-level handler for MDT connection requests.
1552 int mdt_connect(struct mdt_thread_info *info)
1555 struct obd_connect_data *reply;
1556 struct obd_export *exp;
1557 struct ptlrpc_request *req = mdt_info_req(info);
1559 rc = target_handle_connect(req);
1561 return err_serious(rc);
1563 LASSERT(req->rq_export != NULL);
1564 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
1565 rc = mdt_init_sec_level(info);
1567 obd_disconnect(class_export_get(req->rq_export));
1571 /* To avoid exposing partially initialized connection flags, changes up
1572 * to this point have been staged in reply->ocd_connect_flags. Now that
1573 * connection handling has completed successfully, atomically update
1574 * the connect flags in the shared export data structure. LU-1623 */
1575 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
1576 exp = req->rq_export;
1577 spin_lock(&exp->exp_lock);
1578 *exp_connect_flags_ptr(exp) = reply->ocd_connect_flags;
1579 spin_unlock(&exp->exp_lock);
1581 rc = mdt_init_idmap(info);
1583 obd_disconnect(class_export_get(req->rq_export));
1588 int mdt_disconnect(struct mdt_thread_info *info)
1593 rc = target_handle_disconnect(mdt_info_req(info));
1595 rc = err_serious(rc);
1599 static int mdt_sendpage(struct mdt_thread_info *info,
1600 struct lu_rdpg *rdpg, int nob)
1602 struct ptlrpc_request *req = mdt_info_req(info);
1603 struct obd_export *exp = req->rq_export;
1604 struct ptlrpc_bulk_desc *desc;
1605 struct l_wait_info *lwi = &info->mti_u.rdpg.mti_wait_info;
1612 desc = ptlrpc_prep_bulk_exp(req, rdpg->rp_npages, 1, BULK_PUT_SOURCE,
1617 if (!(exp_connect_flags(exp) & OBD_CONNECT_BRW_SIZE))
1618 /* old client requires reply size in it's PAGE_SIZE,
1619 * which is rdpg->rp_count */
1620 nob = rdpg->rp_count;
1622 for (i = 0, tmpcount = nob; i < rdpg->rp_npages && tmpcount > 0;
1623 i++, tmpcount -= tmpsize) {
1624 tmpsize = min_t(int, tmpcount, CFS_PAGE_SIZE);
1625 ptlrpc_prep_bulk_page_pin(desc, rdpg->rp_pages[i], 0, tmpsize);
1628 LASSERT(desc->bd_nob == nob);
1629 rc = target_bulk_io(exp, desc, lwi);
1630 ptlrpc_free_bulk_pin(desc);
1634 int mdt_readpage(struct mdt_thread_info *info)
1636 struct mdt_object *object = info->mti_object;
1637 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1638 struct mdt_body *reqbody;
1639 struct mdt_body *repbody;
1644 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1645 RETURN(err_serious(-ENOMEM));
1647 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1648 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1649 if (reqbody == NULL || repbody == NULL)
1650 RETURN(err_serious(-EFAULT));
1653 * prepare @rdpg before calling lower layers and transfer itself. Here
1654 * reqbody->size contains offset of where to start to read and
1655 * reqbody->nlink contains number bytes to read.
1657 rdpg->rp_hash = reqbody->size;
1658 if (rdpg->rp_hash != reqbody->size) {
1659 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1660 rdpg->rp_hash, reqbody->size);
1664 rdpg->rp_attrs = reqbody->mode;
1665 if (exp_connect_flags(info->mti_exp) & OBD_CONNECT_64BITHASH)
1666 rdpg->rp_attrs |= LUDA_64BITHASH;
1667 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1668 exp_max_brw_size(info->mti_exp));
1669 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE - 1) >>
1671 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1672 if (rdpg->rp_pages == NULL)
1675 for (i = 0; i < rdpg->rp_npages; ++i) {
1676 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1677 if (rdpg->rp_pages[i] == NULL)
1678 GOTO(free_rdpg, rc = -ENOMEM);
1681 /* call lower layers to fill allocated pages with directory data */
1682 rc = mo_readpage(info->mti_env, mdt_object_child(object), rdpg);
1684 GOTO(free_rdpg, rc);
1686 /* send pages to client */
1687 rc = mdt_sendpage(info, rdpg, rc);
1692 for (i = 0; i < rdpg->rp_npages; i++)
1693 if (rdpg->rp_pages[i] != NULL)
1694 cfs_free_page(rdpg->rp_pages[i]);
1695 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1697 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1703 static int mdt_reint_internal(struct mdt_thread_info *info,
1704 struct mdt_lock_handle *lhc,
1707 struct req_capsule *pill = info->mti_pill;
1708 struct mdt_body *repbody;
1713 rc = mdt_reint_unpack(info, op);
1715 CERROR("Can't unpack reint, rc %d\n", rc);
1716 RETURN(err_serious(rc));
1719 /* for replay (no_create) lmm is not needed, client has it already */
1720 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1721 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1722 info->mti_rr.rr_eadatalen);
1724 /* llog cookies are always 0, the field is kept for compatibility */
1725 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1726 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1728 rc = req_capsule_server_pack(pill);
1730 CERROR("Can't pack response, rc %d\n", rc);
1731 RETURN(err_serious(rc));
1734 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1735 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1737 repbody->eadatasize = 0;
1738 repbody->aclsize = 0;
1741 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1743 /* for replay no cookkie / lmm need, because client have this already */
1744 if (info->mti_spec.no_create)
1745 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1746 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1748 rc = mdt_init_ucred_reint(info);
1750 GOTO(out_shrink, rc);
1752 rc = mdt_fix_attr_ucred(info, op);
1754 GOTO(out_ucred, rc = err_serious(rc));
1756 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1757 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1758 GOTO(out_ucred, rc);
1760 rc = mdt_reint_rec(info, lhc);
1763 mdt_exit_ucred(info);
1765 mdt_client_compatibility(info);
1766 rc2 = mdt_fix_reply(info);
1772 static long mdt_reint_opcode(struct mdt_thread_info *info,
1773 const struct req_format **fmt)
1775 struct mdt_rec_reint *rec;
1778 opc = err_serious(-EFAULT);
1779 rec = req_capsule_client_get(info->mti_pill, &RMF_REC_REINT);
1781 opc = rec->rr_opcode;
1782 DEBUG_REQ(D_INODE, mdt_info_req(info), "reint opt = %ld", opc);
1783 if (opc < REINT_MAX && fmt[opc] != NULL)
1784 req_capsule_extend(info->mti_pill, fmt[opc]);
1786 CERROR("Unsupported opc: %ld\n", opc);
1787 opc = err_serious(opc);
1793 int mdt_reint(struct mdt_thread_info *info)
1798 static const struct req_format *reint_fmts[REINT_MAX] = {
1799 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1800 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1801 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1802 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1803 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1804 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1805 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1806 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1811 opc = mdt_reint_opcode(info, reint_fmts);
1814 * No lock possible here from client to pass it to reint code
1817 rc = mdt_reint_internal(info, NULL, opc);
1822 info->mti_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1826 /* this should sync the whole device */
1827 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1829 struct dt_device *dt = mdt->mdt_bottom;
1833 rc = dt->dd_ops->dt_sync(env, dt);
1837 /* this should sync this object */
1838 static int mdt_object_sync(struct mdt_thread_info *info)
1840 struct md_object *next;
1844 if (!mdt_object_exists(info->mti_object)) {
1845 CWARN("Non existing object "DFID"!\n",
1846 PFID(mdt_object_fid(info->mti_object)));
1849 next = mdt_object_child(info->mti_object);
1850 rc = mo_object_sync(info->mti_env, next);
1855 int mdt_sync(struct mdt_thread_info *info)
1857 struct ptlrpc_request *req = mdt_info_req(info);
1858 struct req_capsule *pill = info->mti_pill;
1859 struct mdt_body *body;
1863 /* The fid may be zero, so we req_capsule_set manually */
1864 req_capsule_set(pill, &RQF_MDS_SYNC);
1866 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
1868 RETURN(err_serious(-EINVAL));
1870 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1871 RETURN(err_serious(-ENOMEM));
1873 if (fid_seq(&body->fid1) == 0) {
1874 /* sync the whole device */
1875 rc = req_capsule_server_pack(pill);
1877 rc = mdt_device_sync(info->mti_env, info->mti_mdt);
1879 rc = err_serious(rc);
1881 /* sync an object */
1882 rc = mdt_unpack_req_pack_rep(info, HABEO_CORPUS|HABEO_REFERO);
1884 rc = mdt_object_sync(info);
1886 const struct lu_fid *fid;
1887 struct lu_attr *la = &info->mti_attr.ma_attr;
1889 info->mti_attr.ma_need = MA_INODE;
1890 info->mti_attr.ma_valid = 0;
1891 rc = mdt_attr_get_complex(info, info->mti_object,
1894 body = req_capsule_server_get(pill,
1896 fid = mdt_object_fid(info->mti_object);
1897 mdt_pack_attr2body(info, body, la, fid);
1901 rc = err_serious(rc);
1904 mdt_counter_incr(req, LPROC_MDT_SYNC);
1910 * Quotacheck handler.
1911 * in-kernel quotacheck isn't supported any more.
1913 int mdt_quotacheck(struct mdt_thread_info *info)
1915 struct obd_quotactl *oqctl;
1919 oqctl = req_capsule_client_get(info->mti_pill, &RMF_OBD_QUOTACTL);
1921 RETURN(err_serious(-EPROTO));
1923 rc = req_capsule_server_pack(info->mti_pill);
1925 RETURN(err_serious(rc));
1927 /* deprecated, not used any more */
1928 RETURN(-EOPNOTSUPP);
1932 * Handle quota control requests to consult current usage/limit, but also
1933 * to configure quota enforcement
1935 int mdt_quotactl(struct mdt_thread_info *info)
1937 struct obd_export *exp = info->mti_exp;
1938 struct req_capsule *pill = info->mti_pill;
1939 struct obd_quotactl *oqctl, *repoqc;
1941 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
1944 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1946 RETURN(err_serious(-EPROTO));
1948 rc = req_capsule_server_pack(pill);
1950 RETURN(err_serious(rc));
1952 switch (oqctl->qc_cmd) {
1954 case LUSTRE_Q_INVALIDATE:
1955 case LUSTRE_Q_FINVALIDATE:
1959 /* deprecated, not used any more */
1960 RETURN(-EOPNOTSUPP);
1961 /* master quotactl */
1967 RETURN(-EOPNOTSUPP);
1968 /* slave quotactl */
1973 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1977 /* map uid/gid for remote client */
1979 if (exp_connect_rmtclient(exp)) {
1980 struct lustre_idmap_table *idmap;
1982 idmap = mdt_req2med(mdt_info_req(info))->med_idmap;
1984 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1985 oqctl->qc_cmd != Q_GETINFO))
1988 if (oqctl->qc_type == USRQUOTA)
1989 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1991 else if (oqctl->qc_type == GRPQUOTA)
1992 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1997 if (id == CFS_IDMAP_NOTFOUND) {
1998 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2003 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2005 RETURN(err_serious(-EFAULT));
2007 if (oqctl->qc_id != id)
2008 swap(oqctl->qc_id, id);
2010 switch (oqctl->qc_cmd) {
2016 /* forward quotactl request to QMT */
2017 rc = qmt_hdls.qmth_quotactl(info->mti_env, qmt, oqctl);
2022 /* slave quotactl */
2023 rc = lquotactl_slv(info->mti_env, info->mti_mdt->mdt_bottom,
2028 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2032 if (oqctl->qc_id != id)
2033 swap(oqctl->qc_id, id);
2040 * OBD PING and other handlers.
2042 int mdt_obd_ping(struct mdt_thread_info *info)
2047 req_capsule_set(info->mti_pill, &RQF_OBD_PING);
2049 rc = target_handle_ping(mdt_info_req(info));
2051 rc = err_serious(rc);
2056 * OBD_IDX_READ handler
2058 int mdt_obd_idx_read(struct mdt_thread_info *info)
2060 struct mdt_device *mdt = info->mti_mdt;
2061 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2062 struct idx_info *req_ii, *rep_ii;
2066 memset(rdpg, 0, sizeof(*rdpg));
2067 req_capsule_set(info->mti_pill, &RQF_OBD_IDX_READ);
2069 /* extract idx_info buffer from request & reply */
2070 req_ii = req_capsule_client_get(info->mti_pill, &RMF_IDX_INFO);
2071 if (req_ii == NULL || req_ii->ii_magic != IDX_INFO_MAGIC)
2072 RETURN(err_serious(-EPROTO));
2074 rc = req_capsule_server_pack(info->mti_pill);
2076 RETURN(err_serious(rc));
2078 rep_ii = req_capsule_server_get(info->mti_pill, &RMF_IDX_INFO);
2080 RETURN(err_serious(-EFAULT));
2081 rep_ii->ii_magic = IDX_INFO_MAGIC;
2083 /* extract hash to start with */
2084 rdpg->rp_hash = req_ii->ii_hash_start;
2086 /* extract requested attributes */
2087 rdpg->rp_attrs = req_ii->ii_attrs;
2089 /* check that fid packed in request is valid and supported */
2090 if (!fid_is_sane(&req_ii->ii_fid))
2092 rep_ii->ii_fid = req_ii->ii_fid;
2095 rep_ii->ii_flags = req_ii->ii_flags;
2097 /* compute number of pages to allocate, ii_count is the number of 4KB
2099 if (req_ii->ii_count <= 0)
2100 GOTO(out, rc = -EFAULT);
2101 rdpg->rp_count = min_t(unsigned int, req_ii->ii_count << LU_PAGE_SHIFT,
2102 exp_max_brw_size(info->mti_exp));
2103 rdpg->rp_npages = (rdpg->rp_count + CFS_PAGE_SIZE -1) >> CFS_PAGE_SHIFT;
2105 /* allocate pages to store the containers */
2106 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2107 if (rdpg->rp_pages == NULL)
2108 GOTO(out, rc = -ENOMEM);
2109 for (i = 0; i < rdpg->rp_npages; i++) {
2110 rdpg->rp_pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
2111 if (rdpg->rp_pages[i] == NULL)
2112 GOTO(out, rc = -ENOMEM);
2115 /* populate pages with key/record pairs */
2116 rc = dt_index_read(info->mti_env, mdt->mdt_bottom, rep_ii, rdpg);
2120 LASSERTF(rc <= rdpg->rp_count, "dt_index_read() returned more than "
2121 "asked %d > %d\n", rc, rdpg->rp_count);
2123 /* send pages to client */
2124 rc = mdt_sendpage(info, rdpg, rc);
2128 if (rdpg->rp_pages) {
2129 for (i = 0; i < rdpg->rp_npages; i++)
2130 if (rdpg->rp_pages[i])
2131 cfs_free_page(rdpg->rp_pages[i]);
2132 OBD_FREE(rdpg->rp_pages,
2133 rdpg->rp_npages * sizeof(rdpg->rp_pages[0]));
2138 int mdt_obd_log_cancel(struct mdt_thread_info *info)
2140 return err_serious(-EOPNOTSUPP);
2143 int mdt_obd_qc_callback(struct mdt_thread_info *info)
2145 return err_serious(-EOPNOTSUPP);
2152 /** clone llog ctxt from child (mdd)
2153 * This allows remote llog (replicator) access.
2154 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2155 * context was originally set up, or we can handle them directly.
2156 * I choose the latter, but that means I need any llog
2157 * contexts set up by child to be accessable by the mdt. So we clone the
2158 * context into our context list here.
2160 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2163 struct md_device *next = mdt->mdt_child;
2164 struct llog_ctxt *ctxt;
2167 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2170 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2171 if (rc || ctxt == NULL) {
2175 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2177 CERROR("Can't set mdt ctxt %d\n", rc);
2182 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2183 struct mdt_device *mdt, int idx)
2185 struct llog_ctxt *ctxt;
2187 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2190 /* Put once for the get we just did, and once for the clone */
2191 llog_ctxt_put(ctxt);
2192 llog_ctxt_put(ctxt);
2196 int mdt_llog_create(struct mdt_thread_info *info)
2200 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2201 rc = llog_origin_handle_open(mdt_info_req(info));
2202 return (rc < 0 ? err_serious(rc) : rc);
2205 int mdt_llog_destroy(struct mdt_thread_info *info)
2209 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_DESTROY);
2210 rc = llog_origin_handle_destroy(mdt_info_req(info));
2211 return (rc < 0 ? err_serious(rc) : rc);
2214 int mdt_llog_read_header(struct mdt_thread_info *info)
2218 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2219 rc = llog_origin_handle_read_header(mdt_info_req(info));
2220 return (rc < 0 ? err_serious(rc) : rc);
2223 int mdt_llog_next_block(struct mdt_thread_info *info)
2227 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2228 rc = llog_origin_handle_next_block(mdt_info_req(info));
2229 return (rc < 0 ? err_serious(rc) : rc);
2232 int mdt_llog_prev_block(struct mdt_thread_info *info)
2236 req_capsule_set(info->mti_pill, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
2237 rc = llog_origin_handle_prev_block(mdt_info_req(info));
2238 return (rc < 0 ? err_serious(rc) : rc);
2246 static struct ldlm_callback_suite cbs = {
2247 .lcs_completion = ldlm_server_completion_ast,
2248 .lcs_blocking = ldlm_server_blocking_ast,
2249 .lcs_glimpse = ldlm_server_glimpse_ast
2252 int mdt_enqueue(struct mdt_thread_info *info)
2254 struct ptlrpc_request *req;
2258 * info->mti_dlm_req already contains swapped and (if necessary)
2259 * converted dlm request.
2261 LASSERT(info->mti_dlm_req != NULL);
2263 req = mdt_info_req(info);
2264 rc = ldlm_handle_enqueue0(info->mti_mdt->mdt_namespace,
2265 req, info->mti_dlm_req, &cbs);
2266 info->mti_fail_id = OBD_FAIL_LDLM_REPLY;
2267 return rc ? err_serious(rc) : req->rq_status;
2270 int mdt_convert(struct mdt_thread_info *info)
2273 struct ptlrpc_request *req;
2275 LASSERT(info->mti_dlm_req);
2276 req = mdt_info_req(info);
2277 rc = ldlm_handle_convert0(req, info->mti_dlm_req);
2278 return rc ? err_serious(rc) : req->rq_status;
2281 int mdt_bl_callback(struct mdt_thread_info *info)
2283 CERROR("bl callbacks should not happen on MDS\n");
2285 return err_serious(-EOPNOTSUPP);
2288 int mdt_cp_callback(struct mdt_thread_info *info)
2290 CERROR("cp callbacks should not happen on MDS\n");
2292 return err_serious(-EOPNOTSUPP);
2296 * sec context handlers
2298 int mdt_sec_ctx_handle(struct mdt_thread_info *info)
2302 rc = mdt_handle_idmap(info);
2305 struct ptlrpc_request *req = mdt_info_req(info);
2308 opc = lustre_msg_get_opc(req->rq_reqmsg);
2309 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2310 sptlrpc_svc_ctx_invalidate(req);
2313 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2319 * quota request handlers
2321 int mdt_quota_dqacq(struct mdt_thread_info *info)
2323 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
2328 RETURN(err_serious(-EOPNOTSUPP));
2330 rc = qmt_hdls.qmth_dqacq(info->mti_env, qmt, mdt_info_req(info));
2334 static struct mdt_object *mdt_obj(struct lu_object *o)
2336 LASSERT(lu_device_is_mdt(o->lo_dev));
2337 return container_of0(o, struct mdt_object, mot_obj.mo_lu);
2340 struct mdt_object *mdt_object_new(const struct lu_env *env,
2341 struct mdt_device *d,
2342 const struct lu_fid *f)
2344 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2345 struct lu_object *o;
2346 struct mdt_object *m;
2349 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2350 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, &conf);
2351 if (unlikely(IS_ERR(o)))
2352 m = (struct mdt_object *)o;
2358 struct mdt_object *mdt_object_find(const struct lu_env *env,
2359 struct mdt_device *d,
2360 const struct lu_fid *f)
2362 struct lu_object *o;
2363 struct mdt_object *m;
2366 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2367 o = lu_object_find(env, &d->mdt_md_dev.md_lu_dev, f, NULL);
2368 if (unlikely(IS_ERR(o)))
2369 m = (struct mdt_object *)o;
2376 * Asyncronous commit for mdt device.
2378 * Pass asynchonous commit call down the MDS stack.
2380 * \param env environment
2381 * \param mdt the mdt device
2383 static void mdt_device_commit_async(const struct lu_env *env,
2384 struct mdt_device *mdt)
2386 struct dt_device *dt = mdt->mdt_bottom;
2389 rc = dt->dd_ops->dt_commit_async(env, dt);
2390 if (unlikely(rc != 0))
2391 CWARN("async commit start failed with rc = %d", rc);
2395 * Mark the lock as "synchonous".
2397 * Mark the lock to deffer transaction commit to the unlock time.
2399 * \param lock the lock to mark as "synchonous"
2401 * \see mdt_is_lock_sync
2402 * \see mdt_save_lock
2404 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2406 lock->l_ast_data = (void*)1;
2410 * Check whehter the lock "synchonous" or not.
2412 * \param lock the lock to check
2413 * \retval 1 the lock is "synchonous"
2414 * \retval 0 the lock isn't "synchronous"
2416 * \see mdt_set_lock_sync
2417 * \see mdt_save_lock
2419 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2421 return lock->l_ast_data != NULL;
2425 * Blocking AST for mdt locks.
2427 * Starts transaction commit if in case of COS lock conflict or
2428 * deffers such a commit to the mdt_save_lock.
2430 * \param lock the lock which blocks a request or cancelling lock
2431 * \param desc unused
2432 * \param data unused
2433 * \param flag indicates whether this cancelling or blocking callback
2435 * \see ldlm_blocking_ast_nocheck
2437 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2438 void *data, int flag)
2440 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2441 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2445 if (flag == LDLM_CB_CANCELING)
2447 lock_res_and_lock(lock);
2448 if (lock->l_blocking_ast != mdt_blocking_ast) {
2449 unlock_res_and_lock(lock);
2452 if (mdt_cos_is_enabled(mdt) &&
2453 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2454 lock->l_blocking_lock != NULL &&
2455 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2456 mdt_set_lock_sync(lock);
2458 rc = ldlm_blocking_ast_nocheck(lock);
2460 /* There is no lock conflict if l_blocking_lock == NULL,
2461 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2462 * when the last reference to a local lock was released */
2463 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2466 rc = lu_env_init(&env, LCT_LOCAL);
2467 if (unlikely(rc != 0))
2468 CWARN("lu_env initialization failed with rc = %d,"
2469 "cannot start asynchronous commit\n", rc);
2471 mdt_device_commit_async(&env, mdt);
2477 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2478 void *data, int flag)
2480 struct lustre_handle lockh;
2484 case LDLM_CB_BLOCKING:
2485 ldlm_lock2handle(lock, &lockh);
2486 rc = ldlm_cli_cancel(&lockh);
2488 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2492 case LDLM_CB_CANCELING:
2493 LDLM_DEBUG(lock, "Revoke remote lock\n");
2501 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2502 struct mdt_object *o, struct lustre_handle *lh,
2503 ldlm_mode_t mode, __u64 ibits)
2505 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2506 ldlm_policy_data_t *policy = &mti->mti_policy;
2510 LASSERT(mdt_object_remote(o));
2512 LASSERT((ibits & MDS_INODELOCK_UPDATE));
2514 memset(einfo, 0, sizeof(*einfo));
2515 einfo->ei_type = LDLM_IBITS;
2516 einfo->ei_mode = mode;
2517 einfo->ei_cb_bl = mdt_md_blocking_ast;
2518 einfo->ei_cb_cp = ldlm_completion_ast;
2520 memset(policy, 0, sizeof(*policy));
2521 policy->l_inodebits.bits = ibits;
2523 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2528 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2529 struct mdt_lock_handle *lh, __u64 ibits,
2530 bool nonblock, int locality)
2532 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2533 ldlm_policy_data_t *policy = &info->mti_policy;
2534 struct ldlm_res_id *res_id = &info->mti_res_id;
2539 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2540 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2541 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2542 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2544 if (mdt_object_remote(o)) {
2545 if (locality == MDT_CROSS_LOCK) {
2546 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM);
2547 ibits |= MDS_INODELOCK_LOOKUP;
2550 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM)),
2551 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2552 mdt_obd_name(info->mti_mdt), ibits,
2553 PFID(mdt_object_fid(o)));
2554 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2556 /* No PDO lock on remote object */
2557 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2560 if (lh->mlh_type == MDT_PDO_LOCK) {
2561 /* check for exists after object is locked */
2562 if (mdt_object_exists(o) == 0) {
2563 /* Non-existent object shouldn't have PDO lock */
2566 /* Non-dir object shouldn't have PDO lock */
2567 if (!S_ISDIR(lu_object_attr(&o->mot_obj.mo_lu)))
2572 memset(policy, 0, sizeof(*policy));
2573 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2575 dlmflags = LDLM_FL_ATOMIC_CB;
2577 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2580 * Take PDO lock on whole directory and build correct @res_id for lock
2581 * on part of directory.
2583 if (lh->mlh_pdo_hash != 0) {
2584 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2585 mdt_lock_pdo_mode(info, o, lh);
2586 if (lh->mlh_pdo_mode != LCK_NL) {
2588 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2589 * is never going to be sent to client and we do not
2590 * want it slowed down due to possible cancels.
2592 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2593 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2594 policy, res_id, dlmflags,
2595 &info->mti_exp->exp_handle.h_cookie);
2601 * Finish res_id initializing by name hash marking part of
2602 * directory which is taking modification.
2604 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2607 policy->l_inodebits.bits = ibits;
2610 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2611 * going to be sent to client. If it is - mdt_intent_policy() path will
2612 * fix it up and turn FL_LOCAL flag off.
2614 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2615 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2616 &info->mti_exp->exp_handle.h_cookie);
2618 mdt_object_unlock(info, o, lh, 1);
2619 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2620 lh->mlh_pdo_hash != 0 &&
2621 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2622 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2628 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2629 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2631 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2634 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2635 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2637 struct mdt_lock_handle tmp = *lh;
2640 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2648 * Save a lock within request object.
2650 * Keep the lock referenced until whether client ACK or transaction
2651 * commit happens or release the lock immediately depending on input
2652 * parameters. If COS is ON, a write lock is converted to COS lock
2655 * \param info thead info object
2656 * \param h lock handle
2657 * \param mode lock mode
2658 * \param decref force immediate lock releasing
2661 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2662 ldlm_mode_t mode, int decref)
2666 if (lustre_handle_is_used(h)) {
2667 if (decref || !info->mti_has_trans ||
2668 !(mode & (LCK_PW | LCK_EX))){
2669 mdt_fid_unlock(h, mode);
2671 struct mdt_device *mdt = info->mti_mdt;
2672 struct ldlm_lock *lock = ldlm_handle2lock(h);
2673 struct ptlrpc_request *req = mdt_info_req(info);
2676 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2678 CDEBUG(D_HA, "request = %p reply state = %p"
2679 " transno = "LPD64"\n",
2680 req, req->rq_reply_state, req->rq_transno);
2681 if (mdt_cos_is_enabled(mdt)) {
2683 ldlm_lock_downgrade(lock, LCK_COS);
2686 ptlrpc_save_lock(req, h, mode, no_ack);
2687 if (mdt_is_lock_sync(lock)) {
2688 CDEBUG(D_HA, "found sync-lock,"
2689 " async commit started\n");
2690 mdt_device_commit_async(info->mti_env,
2693 LDLM_LOCK_PUT(lock);
2702 * Unlock mdt object.
2704 * Immeditely release the regular lock and the PDO lock or save the
2705 * lock in reqeuest and keep them referenced until client ACK or
2706 * transaction commit.
2708 * \param info thread info object
2709 * \param o mdt object
2710 * \param lh mdt lock handle referencing regular and PDO locks
2711 * \param decref force immediate lock releasing
2713 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2714 struct mdt_lock_handle *lh, int decref)
2718 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2719 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2721 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2722 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2727 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2728 const struct lu_fid *f,
2729 struct mdt_lock_handle *lh,
2732 struct mdt_object *o;
2734 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2738 rc = mdt_object_lock(info, o, lh, ibits,
2741 mdt_object_put(info->mti_env, o);
2748 void mdt_object_unlock_put(struct mdt_thread_info * info,
2749 struct mdt_object * o,
2750 struct mdt_lock_handle *lh,
2753 mdt_object_unlock(info, o, lh, decref);
2754 mdt_object_put(info->mti_env, o);
2757 struct mdt_handler *mdt_handler_find(__u32 opc, struct mdt_opc_slice *supported)
2759 struct mdt_opc_slice *s;
2760 struct mdt_handler *h;
2763 for (s = supported; s->mos_hs != NULL; s++) {
2764 if (s->mos_opc_start <= opc && opc < s->mos_opc_end) {
2765 h = s->mos_hs + (opc - s->mos_opc_start);
2766 if (likely(h->mh_opc != 0))
2767 LASSERTF(h->mh_opc == opc,
2768 "opcode mismatch %d != %d\n",
2771 h = NULL; /* unsupported opc */
2778 static int mdt_lock_resname_compat(struct mdt_device *m,
2779 struct ldlm_request *req)
2781 /* XXX something... later. */
2785 static int mdt_lock_reply_compat(struct mdt_device *m, struct ldlm_reply *rep)
2787 /* XXX something... later. */
2792 * Generic code handling requests that have struct mdt_body passed in:
2794 * - extract mdt_body from request and save it in @info, if present;
2796 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2799 * - if HABEO_CORPUS flag is set for this request type check whether object
2800 * actually exists on storage (lu_object_exists()).
2803 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2805 const struct mdt_body *body;
2806 struct mdt_object *obj;
2807 const struct lu_env *env;
2808 struct req_capsule *pill;
2812 env = info->mti_env;
2813 pill = info->mti_pill;
2815 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2819 if (!(body->valid & OBD_MD_FLID))
2822 if (!fid_is_sane(&body->fid1)) {
2823 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2828 * Do not get size or any capa fields before we check that request
2829 * contains capa actually. There are some requests which do not, for
2830 * instance MDS_IS_SUBDIR.
2832 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2833 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2834 mdt_set_capainfo(info, 0, &body->fid1,
2835 req_capsule_client_get(pill, &RMF_CAPA1));
2837 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2839 if ((flags & HABEO_CORPUS) &&
2840 !mdt_object_exists(obj)) {
2841 mdt_object_put(env, obj);
2842 /* for capability renew ENOENT will be handled in
2844 if (body->valid & OBD_MD_FLOSSCAPA)
2849 info->mti_object = obj;
2858 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2860 struct req_capsule *pill = info->mti_pill;
2864 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2865 rc = mdt_body_unpack(info, flags);
2869 if (rc == 0 && (flags & HABEO_REFERO)) {
2871 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2872 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2873 info->mti_body->eadatasize);
2874 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2875 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2878 rc = req_capsule_server_pack(pill);
2883 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2885 struct md_device *next = m->mdt_child;
2887 return next->md_ops->mdo_init_capa_ctxt(env, next,
2888 m->mdt_opts.mo_mds_capa,
2889 m->mdt_capa_timeout,
2895 * Invoke handler for this request opc. Also do necessary preprocessing
2896 * (according to handler ->mh_flags), and post-processing (setting of
2897 * ->last_{xid,committed}).
2899 static int mdt_req_handle(struct mdt_thread_info *info,
2900 struct mdt_handler *h, struct ptlrpc_request *req)
2902 int rc, serious = 0;
2907 LASSERT(h->mh_act != NULL);
2908 LASSERT(h->mh_opc == lustre_msg_get_opc(req->rq_reqmsg));
2909 LASSERT(current->journal_info == NULL);
2912 * Checking for various OBD_FAIL_$PREF_$OPC_NET codes. _Do_ not try
2913 * to put same checks into handlers like mdt_close(), mdt_reint(),
2914 * etc., without talking to mdt authors first. Checking same thing
2915 * there again is useless and returning 0 error without packing reply
2916 * is buggy! Handlers either pack reply or return error.
2918 * We return 0 here and do not send any reply in order to emulate
2919 * network failure. Do not send any reply in case any of NET related
2920 * fail_id has occured.
2922 if (OBD_FAIL_CHECK_ORSET(h->mh_fail_id, OBD_FAIL_ONCE))
2926 flags = h->mh_flags;
2927 LASSERT(ergo(flags & (HABEO_CORPUS|HABEO_REFERO), h->mh_fmt != NULL));
2929 if (h->mh_fmt != NULL) {
2930 req_capsule_set(info->mti_pill, h->mh_fmt);
2931 rc = mdt_unpack_req_pack_rep(info, flags);
2934 if (rc == 0 && flags & MUTABOR &&
2935 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
2936 /* should it be rq_status? */
2939 if (rc == 0 && flags & HABEO_CLAVIS) {
2940 struct ldlm_request *dlm_req;
2942 LASSERT(h->mh_fmt != NULL);
2944 dlm_req = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2945 if (dlm_req != NULL) {
2946 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
2948 dlm_req->lock_desc.l_policy_data.\
2949 l_inodebits.bits == 0)) {
2951 * Lock without inodebits makes no sense and
2952 * will oops later in ldlm. If client miss to
2953 * set such bits, do not trigger ASSERTION.
2955 * For liblustre flock case, it maybe zero.
2959 if (info->mti_mdt->mdt_opts.mo_compat_resname)
2960 rc = mdt_lock_resname_compat(
2963 info->mti_dlm_req = dlm_req;
2970 /* capability setting changed via /proc, needs reinitialize ctxt */
2971 if (info->mti_mdt && info->mti_mdt->mdt_capa_conf) {
2972 mdt_init_capa_ctxt(info->mti_env, info->mti_mdt);
2973 info->mti_mdt->mdt_capa_conf = 0;
2976 if (likely(rc == 0)) {
2978 * Process request, there can be two types of rc:
2979 * 1) errors with msg unpack/pack, other failures outside the
2980 * operation itself. This is counted as serious errors;
2981 * 2) errors during fs operation, should be placed in rq_status
2984 rc = h->mh_act(info);
2986 !req->rq_no_reply && req->rq_reply_state == NULL) {
2987 DEBUG_REQ(D_ERROR, req, "MDT \"handler\" %s did not "
2988 "pack reply and returned 0 error\n",
2992 serious = is_serious(rc);
2993 rc = clear_serious(rc);
2997 req->rq_status = rc;
3000 * ELDLM_* codes which > 0 should be in rq_status only as well as
3001 * all non-serious errors.
3003 if (rc > 0 || !serious)
3006 LASSERT(current->journal_info == NULL);
3008 if (rc == 0 && (flags & HABEO_CLAVIS) &&
3009 info->mti_mdt->mdt_opts.mo_compat_resname) {
3010 struct ldlm_reply *dlmrep;
3012 dlmrep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3014 rc = mdt_lock_reply_compat(info->mti_mdt, dlmrep);
3017 /* If we're DISCONNECTing, the mdt_export_data is already freed */
3018 if (likely(rc == 0 && req->rq_export && h->mh_opc != MDS_DISCONNECT))
3019 target_committed_to_req(req);
3021 if (unlikely(req_is_replay(req) &&
3022 lustre_msg_get_transno(req->rq_reqmsg) == 0)) {
3023 DEBUG_REQ(D_ERROR, req, "transno is 0 during REPLAY");
3027 target_send_reply(req, rc, info->mti_fail_id);
3031 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3033 lh->mlh_type = MDT_NUL_LOCK;
3034 lh->mlh_reg_lh.cookie = 0ull;
3035 lh->mlh_reg_mode = LCK_MINMODE;
3036 lh->mlh_pdo_lh.cookie = 0ull;
3037 lh->mlh_pdo_mode = LCK_MINMODE;
3038 lh->mlh_rreg_lh.cookie = 0ull;
3039 lh->mlh_rreg_mode = LCK_MINMODE;
3042 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3044 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3045 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3049 * Initialize fields of struct mdt_thread_info. Other fields are left in
3050 * uninitialized state, because it's too expensive to zero out whole
3051 * mdt_thread_info (> 1K) on each request arrival.
3053 static void mdt_thread_info_init(struct ptlrpc_request *req,
3054 struct mdt_thread_info *info)
3058 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
3059 info->mti_pill = &req->rq_pill;
3062 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3063 mdt_lock_handle_init(&info->mti_lh[i]);
3065 /* mdt device: it can be NULL while CONNECT */
3066 if (req->rq_export) {
3067 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3068 info->mti_exp = req->rq_export;
3070 info->mti_mdt = NULL;
3071 info->mti_env = req->rq_svc_thread->t_env;
3072 info->mti_fail_id = OBD_FAIL_MDS_ALL_REPLY_NET;
3073 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3074 info->mti_mos = NULL;
3076 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3077 info->mti_body = NULL;
3078 info->mti_object = NULL;
3079 info->mti_dlm_req = NULL;
3080 info->mti_has_trans = 0;
3081 info->mti_cross_ref = 0;
3082 info->mti_opdata = 0;
3083 info->mti_big_lmm_used = 0;
3085 /* To not check for split by default. */
3086 info->mti_spec.no_create = 0;
3087 info->mti_spec.sp_rm_entry = 0;
3090 static void mdt_thread_info_fini(struct mdt_thread_info *info)
3094 req_capsule_fini(info->mti_pill);
3095 if (info->mti_object != NULL) {
3096 mdt_object_put(info->mti_env, info->mti_object);
3097 info->mti_object = NULL;
3099 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3100 mdt_lock_handle_fini(&info->mti_lh[i]);
3101 info->mti_env = NULL;
3104 static int mdt_filter_recovery_request(struct ptlrpc_request *req,
3105 struct obd_device *obd, int *process)
3107 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3108 case MDS_CONNECT: /* This will never get here, but for completeness. */
3109 case OST_CONNECT: /* This will never get here, but for completeness. */
3110 case MDS_DISCONNECT:
3111 case OST_DISCONNECT:
3117 case MDS_DONE_WRITING:
3118 case MDS_SYNC: /* used in unmounting */
3124 *process = target_queue_recovery_request(req, obd);
3128 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
3135 * Handle recovery. Return:
3136 * +1: continue request processing;
3137 * -ve: abort immediately with the given error code;
3138 * 0: send reply with error code in req->rq_status;
3140 static int mdt_recovery(struct mdt_thread_info *info)
3142 struct ptlrpc_request *req = mdt_info_req(info);
3143 struct obd_device *obd;
3147 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3150 case SEC_CTX_INIT_CONT:
3156 rc = mdt_handle_idmap(info);
3165 if (unlikely(!class_connected_export(req->rq_export))) {
3166 CDEBUG(D_HA, "operation %d on unconnected MDS from %s\n",
3167 lustre_msg_get_opc(req->rq_reqmsg),
3168 libcfs_id2str(req->rq_peer));
3169 /* FIXME: For CMD cleanup, when mds_B stop, the req from
3170 * mds_A will get -ENOTCONN(especially for ping req),
3171 * which will cause that mds_A deactive timeout, then when
3172 * mds_A cleanup, the cleanup process will be suspended since
3173 * deactive timeout is not zero.
3175 req->rq_status = -ENOTCONN;
3176 target_send_reply(req, -ENOTCONN, info->mti_fail_id);
3180 /* sanity check: if the xid matches, the request must be marked as a
3181 * resent or replayed */
3182 if (req_xid_is_last(req)) {
3183 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
3184 (MSG_RESENT | MSG_REPLAY))) {
3185 DEBUG_REQ(D_WARNING, req, "rq_xid "LPU64" matches last_xid, "
3186 "expected REPLAY or RESENT flag (%x)", req->rq_xid,
3187 lustre_msg_get_flags(req->rq_reqmsg));
3189 req->rq_status = -ENOTCONN;
3194 /* else: note the opposite is not always true; a RESENT req after a
3195 * failover will usually not match the last_xid, since it was likely
3196 * never committed. A REPLAYed request will almost never match the
3197 * last xid, however it could for a committed, but still retained,
3200 obd = req->rq_export->exp_obd;
3202 /* Check for aborted recovery... */
3203 if (unlikely(obd->obd_recovering)) {
3206 DEBUG_REQ(D_INFO, req, "Got new replay");
3207 rc = mdt_filter_recovery_request(req, obd, &should_process);
3208 if (rc != 0 || !should_process)
3210 else if (should_process < 0) {
3211 req->rq_status = should_process;
3212 rc = ptlrpc_error(req);
3219 static int mdt_msg_check_version(struct lustre_msg *msg)
3223 switch (lustre_msg_get_opc(msg)) {
3225 case MDS_DISCONNECT:
3228 case SEC_CTX_INIT_CONT:
3231 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
3233 CERROR("bad opc %u version %08x, expecting %08x\n",
3234 lustre_msg_get_opc(msg),
3235 lustre_msg_get_version(msg),
3236 LUSTRE_OBD_VERSION);
3240 case MDS_GETATTR_NAME:
3247 case MDS_DONE_WRITING:
3254 case MDS_HSM_PROGRESS:
3255 case MDS_HSM_REQUEST:
3256 case MDS_HSM_CT_REGISTER:
3257 case MDS_HSM_CT_UNREGISTER:
3258 case MDS_HSM_STATE_GET:
3259 case MDS_HSM_STATE_SET:
3260 case MDS_HSM_ACTION:
3261 case MDS_QUOTACHECK:
3264 case MDS_SWAP_LAYOUTS:
3269 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
3271 CERROR("bad opc %u version %08x, expecting %08x\n",
3272 lustre_msg_get_opc(msg),
3273 lustre_msg_get_version(msg),
3274 LUSTRE_MDS_VERSION);
3278 case LDLM_BL_CALLBACK:
3279 case LDLM_CP_CALLBACK:
3280 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
3282 CERROR("bad opc %u version %08x, expecting %08x\n",
3283 lustre_msg_get_opc(msg),
3284 lustre_msg_get_version(msg),
3285 LUSTRE_DLM_VERSION);
3287 case OBD_LOG_CANCEL:
3288 case LLOG_ORIGIN_HANDLE_CREATE:
3289 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
3290 case LLOG_ORIGIN_HANDLE_READ_HEADER:
3291 case LLOG_ORIGIN_HANDLE_CLOSE:
3292 case LLOG_ORIGIN_HANDLE_DESTROY:
3293 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
3295 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
3297 CERROR("bad opc %u version %08x, expecting %08x\n",
3298 lustre_msg_get_opc(msg),
3299 lustre_msg_get_version(msg),
3300 LUSTRE_LOG_VERSION);
3303 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
3309 static int mdt_handle0(struct ptlrpc_request *req,
3310 struct mdt_thread_info *info,
3311 struct mdt_opc_slice *supported)
3313 struct mdt_handler *h;
3314 struct lustre_msg *msg;
3319 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_MDS_ALL_REQUEST_NET, OBD_FAIL_ONCE))
3322 LASSERT(current->journal_info == NULL);
3324 msg = req->rq_reqmsg;
3325 rc = mdt_msg_check_version(msg);
3326 if (likely(rc == 0)) {
3327 rc = mdt_recovery(info);
3328 if (likely(rc == +1)) {
3329 h = mdt_handler_find(lustre_msg_get_opc(msg),
3331 if (likely(h != NULL)) {
3332 rc = mdt_req_handle(info, h, req);
3334 CERROR("The unsupported opc: 0x%x\n",
3335 lustre_msg_get_opc(msg) );
3336 req->rq_status = -ENOTSUPP;
3337 rc = ptlrpc_error(req);
3342 CERROR(LUSTRE_MDT_NAME" drops mal-formed request\n");
3347 * MDT handler function called by ptlrpc service thread when request comes.
3349 * XXX common "target" functionality should be factored into separate module
3350 * shared by mdt, ost and stand-alone services like fld.
3352 int mdt_handle_common(struct ptlrpc_request *req,
3353 struct mdt_opc_slice *supported)
3356 struct mdt_thread_info *info;
3360 env = req->rq_svc_thread->t_env;
3361 /* Refill(initilize) the context(mdt_thread_info), in case it is
3362 * not initialized yet. Usually it happens during start up, after
3363 * MDS(ptlrpc threads) is start up, it gets the first CONNECT request,
3364 * before MDT_thread_info is initialized */
3366 LASSERT(env != NULL);
3367 LASSERT(env->le_ses != NULL);
3368 LASSERT(env->le_ctx.lc_thread == req->rq_svc_thread);
3369 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3370 LASSERT(info != NULL);
3372 mdt_thread_info_init(req, info);
3374 rc = mdt_handle0(req, info, supported);
3376 mdt_thread_info_fini(info);
3381 * This is called from recovery code as handler of _all_ RPC types, FLD and SEQ
3384 int mdt_recovery_handle(struct ptlrpc_request *req)
3389 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
3391 rc = mdt_handle_common(req, mdt_fld_handlers);
3394 rc = mdt_handle_common(req, mdt_seq_handlers);
3397 rc = mdt_handle_common(req, mdt_regular_handlers);
3419 static int mdt_intent_getattr(enum mdt_it_code opcode,
3420 struct mdt_thread_info *info,
3421 struct ldlm_lock **,
3423 static int mdt_intent_layout(enum mdt_it_code opcode,
3424 struct mdt_thread_info *info,
3425 struct ldlm_lock **,
3427 static int mdt_intent_reint(enum mdt_it_code opcode,
3428 struct mdt_thread_info *info,
3429 struct ldlm_lock **,
3432 static struct mdt_it_flavor {
3433 const struct req_format *it_fmt;
3435 int (*it_act)(enum mdt_it_code ,
3436 struct mdt_thread_info *,
3437 struct ldlm_lock **,
3440 } mdt_it_flavor[] = {
3442 .it_fmt = &RQF_LDLM_INTENT,
3443 /*.it_flags = HABEO_REFERO,*/
3445 .it_act = mdt_intent_reint,
3446 .it_reint = REINT_OPEN
3449 .it_fmt = &RQF_LDLM_INTENT,
3450 .it_flags = MUTABOR,
3451 .it_act = mdt_intent_reint,
3452 .it_reint = REINT_OPEN
3455 .it_fmt = &RQF_LDLM_INTENT,
3456 .it_flags = MUTABOR,
3457 .it_act = mdt_intent_reint,
3458 .it_reint = REINT_CREATE
3460 [MDT_IT_GETATTR] = {
3461 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3462 .it_flags = HABEO_REFERO,
3463 .it_act = mdt_intent_getattr
3465 [MDT_IT_READDIR] = {
3471 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3472 .it_flags = HABEO_REFERO,
3473 .it_act = mdt_intent_getattr
3476 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3477 .it_flags = MUTABOR,
3479 .it_reint = REINT_UNLINK
3483 .it_flags = MUTABOR,
3486 [MDT_IT_GETXATTR] = {
3492 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3494 .it_act = mdt_intent_layout
3498 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3499 struct ldlm_lock **lockp,
3500 struct ldlm_lock *new_lock,
3501 struct mdt_lock_handle *lh,
3504 struct ptlrpc_request *req = mdt_info_req(info);
3505 struct ldlm_lock *lock = *lockp;
3508 * Get new lock only for cases when possible resent did not find any
3511 if (new_lock == NULL)
3512 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3514 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3515 lh->mlh_reg_lh.cookie = 0;
3519 LASSERTF(new_lock != NULL,
3520 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3523 * If we've already given this lock to a client once, then we should
3524 * have no readers or writers. Otherwise, we should have one reader
3525 * _or_ writer ref (which will be zeroed below) before returning the
3528 if (new_lock->l_export == req->rq_export) {
3529 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3531 LASSERT(new_lock->l_export == NULL);
3532 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3537 if (new_lock->l_export == req->rq_export) {
3539 * Already gave this to the client, which means that we
3540 * reconstructed a reply.
3542 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3544 lh->mlh_reg_lh.cookie = 0;
3545 RETURN(ELDLM_LOCK_REPLACED);
3549 * Fixup the lock to be given to the client.
3551 lock_res_and_lock(new_lock);
3552 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3553 * possible blocking AST. */
3554 while (new_lock->l_readers > 0) {
3555 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3556 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3557 new_lock->l_readers--;
3559 while (new_lock->l_writers > 0) {
3560 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3561 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3562 new_lock->l_writers--;
3565 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3566 new_lock->l_blocking_ast = lock->l_blocking_ast;
3567 new_lock->l_completion_ast = lock->l_completion_ast;
3568 new_lock->l_remote_handle = lock->l_remote_handle;
3569 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3571 unlock_res_and_lock(new_lock);
3573 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3574 &new_lock->l_remote_handle,
3575 &new_lock->l_exp_hash);
3577 LDLM_LOCK_RELEASE(new_lock);
3578 lh->mlh_reg_lh.cookie = 0;
3580 RETURN(ELDLM_LOCK_REPLACED);
3583 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3584 struct ldlm_lock *new_lock,
3585 struct ldlm_lock **old_lock,
3586 struct mdt_lock_handle *lh)
3588 struct ptlrpc_request *req = mdt_info_req(info);
3589 struct obd_export *exp = req->rq_export;
3590 struct lustre_handle remote_hdl;
3591 struct ldlm_request *dlmreq;
3592 struct ldlm_lock *lock;
3594 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3597 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3598 remote_hdl = dlmreq->lock_handle[0];
3600 /* In the function below, .hs_keycmp resolves to
3601 * ldlm_export_lock_keycmp() */
3602 /* coverity[overrun-buffer-val] */
3603 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3605 if (lock != new_lock) {
3606 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3607 lh->mlh_reg_mode = lock->l_granted_mode;
3609 LDLM_DEBUG(lock, "Restoring lock cookie");
3610 DEBUG_REQ(D_DLMTRACE, req,
3611 "restoring lock cookie "LPX64,
3612 lh->mlh_reg_lh.cookie);
3614 *old_lock = LDLM_LOCK_GET(lock);
3615 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3619 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3623 * If the xid matches, then we know this is a resent request, and allow
3624 * it. (It's probably an OPEN, for which we don't send a lock.
3626 if (req_xid_is_last(req))
3630 * This remote handle isn't enqueued, so we never received or processed
3631 * this request. Clear MSG_RESENT, because it can be handled like any
3632 * normal request now.
3634 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3636 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3640 static int mdt_intent_getattr(enum mdt_it_code opcode,
3641 struct mdt_thread_info *info,
3642 struct ldlm_lock **lockp,
3645 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3646 struct ldlm_lock *new_lock = NULL;
3648 struct ldlm_reply *ldlm_rep;
3649 struct ptlrpc_request *req;
3650 struct mdt_body *reqbody;
3651 struct mdt_body *repbody;
3655 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3658 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3661 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3662 repbody->eadatasize = 0;
3663 repbody->aclsize = 0;
3667 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3669 case MDT_IT_GETATTR:
3670 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3674 CERROR("Unsupported intent (%d)\n", opcode);
3675 GOTO(out_shrink, rc = -EINVAL);
3678 rc = mdt_init_ucred(info, reqbody);
3680 GOTO(out_shrink, rc);
3682 req = info->mti_pill->rc_req;
3683 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3684 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3686 /* Get lock from request for possible resent case. */
3687 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3689 ldlm_rep->lock_policy_res2 =
3690 mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3692 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3693 ldlm_rep->lock_policy_res2 = 0;
3694 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3695 ldlm_rep->lock_policy_res2) {
3696 lhc->mlh_reg_lh.cookie = 0ull;
3697 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3700 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3703 mdt_exit_ucred(info);
3705 mdt_client_compatibility(info);
3706 rc2 = mdt_fix_reply(info);
3712 static int mdt_intent_layout(enum mdt_it_code opcode,
3713 struct mdt_thread_info *info,
3714 struct ldlm_lock **lockp,
3717 struct layout_intent *layout;
3721 if (opcode != MDT_IT_LAYOUT) {
3722 CERROR("%s: Unknown intent (%d)\n",
3723 info->mti_exp->exp_obd->obd_name, opcode);
3727 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3728 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3729 ldlm_lvbo_size(*lockp));
3730 rc = req_capsule_server_pack(info->mti_pill);
3734 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3735 LASSERT(layout != NULL);
3736 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3737 /* return to normal ldlm handling */
3740 CERROR("%s: Unsupported layout intent (%d)\n",
3741 info->mti_exp->exp_obd->obd_name, layout->li_opc);
3745 static int mdt_intent_reint(enum mdt_it_code opcode,
3746 struct mdt_thread_info *info,
3747 struct ldlm_lock **lockp,
3750 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3751 struct ldlm_reply *rep = NULL;
3755 static const struct req_format *intent_fmts[REINT_MAX] = {
3756 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3757 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3762 opc = mdt_reint_opcode(info, intent_fmts);
3766 if (mdt_it_flavor[opcode].it_reint != opc) {
3767 CERROR("Reint code %ld doesn't match intent: %d\n",
3769 RETURN(err_serious(-EPROTO));
3772 /* Get lock from request for possible resent case. */
3773 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3775 rc = mdt_reint_internal(info, lhc, opc);
3777 /* Check whether the reply has been packed successfully. */
3778 if (mdt_info_req(info)->rq_repmsg != NULL)
3779 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3781 RETURN(err_serious(-EFAULT));
3783 /* MDC expects this in any case */
3785 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3787 /* Cross-ref case, the lock should be returned to the client */
3788 if (rc == -EREMOTE) {
3789 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3790 rep->lock_policy_res2 = 0;
3791 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3794 rep->lock_policy_res2 = clear_serious(rc);
3796 if (rep->lock_policy_res2 == -ENOENT &&
3797 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3798 rep->lock_policy_res2 = 0;
3800 if (rc == -ENOTCONN || rc == -ENODEV ||
3801 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3803 * If it is the disconnect error (ENODEV & ENOCONN), the error
3804 * will be returned by rq_status, and client at ptlrpc layer
3805 * will detect this, then disconnect, reconnect the import
3806 * immediately, instead of impacting the following the rpc.
3808 lhc->mlh_reg_lh.cookie = 0ull;
3812 * For other cases, the error will be returned by intent.
3813 * and client will retrieve the result from intent.
3816 * FIXME: when open lock is finished, that should be
3819 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3820 LASSERTF(rc == 0, "Error occurred but lock handle "
3821 "is still in use, rc = %d\n", rc);
3822 rep->lock_policy_res2 = 0;
3823 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3826 lhc->mlh_reg_lh.cookie = 0ull;
3827 RETURN(ELDLM_LOCK_ABORTED);
3832 static int mdt_intent_code(long itcode)
3840 case IT_OPEN|IT_CREAT:
3847 rc = MDT_IT_READDIR;
3850 rc = MDT_IT_GETATTR;
3862 rc = MDT_IT_GETXATTR;
3867 case IT_QUOTA_DQACQ:
3872 CERROR("Unknown intent opcode: %ld\n", itcode);
3879 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3880 struct ldlm_lock **lockp, __u64 flags)
3882 struct req_capsule *pill;
3883 struct mdt_it_flavor *flv;
3888 opc = mdt_intent_code(itopc);
3892 pill = info->mti_pill;
3894 if (opc == MDT_IT_QUOTA) {
3895 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3898 RETURN(-EOPNOTSUPP);
3900 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3901 /* pass the request to quota master */
3902 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3903 mdt_info_req(info), lockp,
3908 flv = &mdt_it_flavor[opc];
3909 if (flv->it_fmt != NULL)
3910 req_capsule_extend(pill, flv->it_fmt);
3912 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3914 struct ptlrpc_request *req = mdt_info_req(info);
3915 if (flv->it_flags & MUTABOR &&
3916 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3919 if (rc == 0 && flv->it_act != NULL) {
3920 /* execute policy */
3921 rc = flv->it_act(opc, info, lockp, flags);
3928 static int mdt_intent_policy(struct ldlm_namespace *ns,
3929 struct ldlm_lock **lockp, void *req_cookie,
3930 ldlm_mode_t mode, __u64 flags, void *data)
3932 struct mdt_thread_info *info;
3933 struct ptlrpc_request *req = req_cookie;
3934 struct ldlm_intent *it;
3935 struct req_capsule *pill;
3940 LASSERT(req != NULL);
3942 info = lu_context_key_get(&req->rq_svc_thread->t_env->le_ctx,
3944 LASSERT(info != NULL);
3945 pill = info->mti_pill;
3946 LASSERT(pill->rc_req == req);
3948 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3949 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3950 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3952 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3956 /* Lock without inodebits makes no sense and will oops
3957 * later in ldlm. Let's check it now to see if we have
3958 * ibits corrupted somewhere in mdt_intent_opc().
3959 * The case for client miss to set ibits has been
3960 * processed by others. */
3961 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3962 lr_type == LDLM_IBITS,
3963 info->mti_dlm_req->lock_desc.\
3964 l_policy_data.l_inodebits.bits != 0));
3966 rc = err_serious(-EFAULT);
3968 /* No intent was provided */
3969 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3970 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3971 rc = req_capsule_server_pack(pill);
3973 rc = err_serious(rc);
3978 static int mdt_seq_fini(const struct lu_env *env,
3979 struct mdt_device *m)
3981 return seq_site_fini(env, mdt_seq_site(m));
3984 static int mdt_seq_init(const struct lu_env *env,
3986 struct mdt_device *m)
3988 struct seq_server_site *ss;
3993 ss = mdt_seq_site(m);
3996 * This is sequence-controller node. Init seq-controller server on local
3999 if (ss->ss_node_id == 0) {
4000 LASSERT(ss->ss_control_seq == NULL);
4002 OBD_ALLOC_PTR(ss->ss_control_seq);
4003 if (ss->ss_control_seq == NULL)
4006 rc = seq_server_init(ss->ss_control_seq,
4007 m->mdt_bottom, uuid,
4008 LUSTRE_SEQ_CONTROLLER,
4013 GOTO(out_seq_fini, rc);
4015 OBD_ALLOC_PTR(ss->ss_client_seq);
4016 if (ss->ss_client_seq == NULL)
4017 GOTO(out_seq_fini, rc = -ENOMEM);
4019 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4020 if (prefix == NULL) {
4021 OBD_FREE_PTR(ss->ss_client_seq);
4022 GOTO(out_seq_fini, rc = -ENOMEM);
4025 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s",
4029 * Init seq-controller client after seq-controller server is
4030 * ready. Pass ss->ss_control_seq to it for direct talking.
4032 rc = seq_client_init(ss->ss_client_seq, NULL,
4033 LUSTRE_SEQ_METADATA, prefix,
4034 ss->ss_control_seq);
4035 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4038 GOTO(out_seq_fini, rc);
4041 /* Init seq-server on local MDT */
4042 LASSERT(ss->ss_server_seq == NULL);
4044 OBD_ALLOC_PTR(ss->ss_server_seq);
4045 if (ss->ss_server_seq == NULL)
4046 GOTO(out_seq_fini, rc = -ENOMEM);
4048 rc = seq_server_init(ss->ss_server_seq,
4049 m->mdt_bottom, uuid,
4054 GOTO(out_seq_fini, rc = -ENOMEM);
4056 /* Assign seq-controller client to local seq-server. */
4057 if (ss->ss_node_id == 0) {
4058 LASSERT(ss->ss_client_seq != NULL);
4060 rc = seq_server_set_cli(ss->ss_server_seq,
4068 mdt_seq_fini(env, m);
4076 static int mdt_fld_fini(const struct lu_env *env,
4077 struct mdt_device *m)
4079 struct seq_server_site *ss = mdt_seq_site(m);
4082 if (ss && ss->ss_server_fld) {
4083 fld_server_fini(env, ss->ss_server_fld);
4084 OBD_FREE_PTR(ss->ss_server_fld);
4085 ss->ss_server_fld = NULL;
4091 static int mdt_fld_init(const struct lu_env *env,
4093 struct mdt_device *m)
4095 struct seq_server_site *ss;
4099 ss = mdt_seq_site(m);
4101 OBD_ALLOC_PTR(ss->ss_server_fld);
4102 if (ss->ss_server_fld == NULL)
4103 RETURN(rc = -ENOMEM);
4105 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4106 ss->ss_node_id, LU_SEQ_RANGE_MDT);
4108 OBD_FREE_PTR(ss->ss_server_fld);
4109 ss->ss_server_fld = NULL;
4116 static void mdt_stack_pre_fini(const struct lu_env *env,
4117 struct mdt_device *m, struct lu_device *top)
4119 struct obd_device *obd = mdt2obd_dev(m);
4120 struct lustre_cfg_bufs *bufs;
4121 struct lustre_cfg *lcfg;
4122 struct mdt_thread_info *info;
4127 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4128 LASSERT(info != NULL);
4130 bufs = &info->mti_u.bufs;
4132 LASSERT(m->mdt_child_exp);
4133 LASSERT(m->mdt_child_exp->exp_obd);
4134 obd = m->mdt_child_exp->exp_obd;
4136 /* process cleanup, pass mdt obd name to get obd umount flags */
4137 /* XXX: this is needed because all layers are referenced by
4138 * objects (some of them are pinned by osd, for example *
4139 * the proper solution should be a model where object used
4140 * by osd only doesn't have mdt/mdd slices -bzzz */
4141 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4142 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4143 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4145 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
4148 top->ld_ops->ldo_process_config(env, top, lcfg);
4149 lustre_cfg_free(lcfg);
4153 static void mdt_stack_fini(const struct lu_env *env,
4154 struct mdt_device *m, struct lu_device *top)
4156 struct obd_device *obd = mdt2obd_dev(m);
4157 struct lustre_cfg_bufs *bufs;
4158 struct lustre_cfg *lcfg;
4159 struct mdt_thread_info *info;
4163 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4164 LASSERT(info != NULL);
4166 lu_dev_del_linkage(top->ld_site, top);
4168 lu_site_purge(env, top->ld_site, -1);
4170 bufs = &info->mti_u.bufs;
4171 /* process cleanup, pass mdt obd name to get obd umount flags */
4172 /* another purpose is to let all layers to release their objects */
4173 lustre_cfg_bufs_reset(bufs, obd->obd_name);
4178 lustre_cfg_bufs_set_string(bufs, 1, flags);
4179 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4181 CERROR("Cannot alloc lcfg!\n");
4185 top->ld_ops->ldo_process_config(env, top, lcfg);
4186 lustre_cfg_free(lcfg);
4188 lu_site_purge(env, top->ld_site, -1);
4190 m->mdt_child = NULL;
4191 m->mdt_bottom = NULL;
4193 obd_disconnect(m->mdt_child_exp);
4194 m->mdt_child_exp = NULL;
4196 obd_disconnect(m->mdt_bottom_exp);
4197 m->mdt_child_exp = NULL;
4200 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4201 const char *next, struct obd_export **exp)
4203 struct obd_connect_data *data = NULL;
4204 struct obd_device *obd;
4208 OBD_ALLOC_PTR(data);
4210 GOTO(out, rc = -ENOMEM);
4212 obd = class_name2obd(next);
4214 CERROR("%s: can't locate next device: %s\n",
4215 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next);
4216 GOTO(out, rc = -ENOTCONN);
4219 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4220 data->ocd_version = LUSTRE_VERSION_CODE;
4222 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4224 CERROR("%s: cannot connect to next dev %s (%d)\n",
4225 m->mdt_md_dev.md_lu_dev.ld_obd->obd_name, next, rc);
4235 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4236 struct lustre_cfg *cfg)
4238 char *dev = lustre_cfg_string(cfg, 0);
4239 int rc, name_size, uuid_size;
4240 char *name, *uuid, *p;
4241 struct lustre_cfg_bufs *bufs;
4242 struct lustre_cfg *lcfg;
4243 struct obd_device *obd;
4244 struct lustre_profile *lprof;
4245 struct lu_site *site;
4248 /* in 1.8 we had the only device in the stack - MDS.
4249 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4250 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4251 * to generate names and setup MDT, MDD. MDT will be using
4252 * generated name to connect to MDD. for MDD the next device
4253 * will be LOD with name taken from so called "profile" which
4254 * is generated by mount_option line
4256 * 1.8 MGS generates config. commands like this:
4257 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4258 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4259 * 2.0 MGS generates config. commands like this:
4260 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4261 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4262 * 3:lustre-MDT0000-mdtlov 4:f
4264 * we generate MDD name from MDT one, just replacing T with D
4266 * after all the preparations, the logical equivalent will be
4267 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4268 * 3:lustre-MDT0000-mdtlov 4:f
4269 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4270 * 3:lustre-MDD0000 4:f
4272 * notice we build the stack from down to top: MDD first, then MDT */
4274 name_size = MAX_OBD_NAME;
4275 uuid_size = MAX_OBD_NAME;
4277 OBD_ALLOC(name, name_size);
4278 OBD_ALLOC(uuid, uuid_size);
4279 if (name == NULL || uuid == NULL)
4280 GOTO(cleanup_mem, rc = -ENOMEM);
4282 OBD_ALLOC_PTR(bufs);
4284 GOTO(cleanup_mem, rc = -ENOMEM);
4287 p = strstr(name, "-MDT");
4289 GOTO(cleanup_mem, rc = -ENOMEM);
4292 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4294 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4295 if (lprof == NULL || lprof->lp_dt == NULL) {
4296 CERROR("can't find the profile: %s\n",
4297 lustre_cfg_string(cfg, 0));
4298 GOTO(cleanup_mem, rc = -EINVAL);
4301 lustre_cfg_bufs_reset(bufs, name);
4302 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4303 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4304 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4306 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4308 GOTO(free_bufs, rc = -ENOMEM);
4310 rc = class_attach(lcfg);
4312 GOTO(lcfg_cleanup, rc);
4314 obd = class_name2obd(name);
4316 CERROR("Can not find obd %s (%s in config)\n",
4317 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4318 GOTO(class_detach, rc = -EINVAL);
4321 lustre_cfg_free(lcfg);
4323 lustre_cfg_bufs_reset(bufs, name);
4324 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4325 lustre_cfg_bufs_set_string(bufs, 2, dev);
4326 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4328 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4330 rc = class_setup(obd, lcfg);
4332 GOTO(class_detach, rc);
4334 /* connect to MDD we just setup */
4335 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4339 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4341 LASSERT(mdt->mdt_md_dev.md_lu_dev.ld_site == NULL);
4342 mdt->mdt_md_dev.md_lu_dev.ld_site = site;
4343 site->ls_top_dev = &mdt->mdt_md_dev.md_lu_dev;
4344 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4347 /* now connect to bottom OSD */
4348 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4349 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4353 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4356 rc = lu_env_refill((struct lu_env *)env);
4358 CERROR("Failure to refill session: '%d'\n", rc);
4360 lu_dev_add_linkage(site, &mdt->mdt_md_dev.md_lu_dev);
4365 class_detach(obd, lcfg);
4367 lustre_cfg_free(lcfg);
4372 OBD_FREE(name, name_size);
4374 OBD_FREE(uuid, uuid_size);
4378 /* setup quota master target on MDT0 */
4379 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4380 struct lustre_cfg *cfg)
4382 struct obd_device *obd;
4383 char *dev = lustre_cfg_string(cfg, 0);
4384 char *qmtname, *uuid, *p;
4385 struct lustre_cfg_bufs *bufs;
4386 struct lustre_cfg *lcfg;
4387 struct lustre_profile *lprof;
4388 struct obd_connect_data *data;
4392 LASSERT(mdt->mdt_qmt_exp == NULL);
4393 LASSERT(mdt->mdt_qmt_dev == NULL);
4395 /* quota master is on MDT0 only for now */
4396 if (mdt->mdt_seq_site.ss_node_id != 0)
4399 /* MGS generates config commands which look as follows:
4400 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4401 * 3:lustre-MDT0000-mdtlov 4:f
4403 * We generate the QMT name from the MDT one, just replacing MD with QM
4404 * after all the preparations, the logical equivalent will be:
4405 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4406 * 3:lustre-MDT0000-osd 4:f */
4407 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4408 OBD_ALLOC(uuid, UUID_MAX);
4409 OBD_ALLOC_PTR(bufs);
4410 OBD_ALLOC_PTR(data);
4411 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4412 GOTO(cleanup_mem, rc = -ENOMEM);
4414 strcpy(qmtname, dev);
4415 p = strstr(qmtname, "-MDT");
4417 GOTO(cleanup_mem, rc = -ENOMEM);
4418 /* replace MD with QM */
4422 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4424 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4425 if (lprof == NULL || lprof->lp_dt == NULL) {
4426 CERROR("can't find profile for %s\n",
4427 lustre_cfg_string(cfg, 0));
4428 GOTO(cleanup_mem, rc = -EINVAL);
4431 lustre_cfg_bufs_reset(bufs, qmtname);
4432 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4433 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4434 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4436 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4438 GOTO(cleanup_mem, rc = -ENOMEM);
4440 rc = class_attach(lcfg);
4442 GOTO(lcfg_cleanup, rc);
4444 obd = class_name2obd(qmtname);
4446 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4447 lustre_cfg_string(cfg, 0));
4448 GOTO(class_detach, rc = -EINVAL);
4451 lustre_cfg_free(lcfg);
4453 lustre_cfg_bufs_reset(bufs, qmtname);
4454 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4455 lustre_cfg_bufs_set_string(bufs, 2, dev);
4457 /* for quota, the next device should be the OSD device */
4458 lustre_cfg_bufs_set_string(bufs, 3,
4459 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4461 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4463 rc = class_setup(obd, lcfg);
4465 GOTO(class_detach, rc);
4467 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4469 /* configure local quota objects */
4470 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4471 &mdt->mdt_md_dev.md_lu_dev,
4474 GOTO(class_cleanup, rc);
4476 /* connect to quota master target */
4477 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4478 data->ocd_version = LUSTRE_VERSION_CODE;
4479 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4482 CERROR("cannot connect to quota master device %s (%d)\n",
4484 GOTO(class_cleanup, rc);
4490 class_manual_cleanup(obd);
4491 mdt->mdt_qmt_dev = NULL;
4495 class_detach(obd, lcfg);
4497 lustre_cfg_free(lcfg);
4502 OBD_FREE(qmtname, MAX_OBD_NAME);
4504 OBD_FREE(uuid, UUID_MAX);
4510 /* Shutdown quota master target associated with mdt */
4511 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4515 if (mdt->mdt_qmt_exp == NULL)
4517 LASSERT(mdt->mdt_qmt_dev != NULL);
4519 /* the qmt automatically shuts down when the mdt disconnects */
4520 obd_disconnect(mdt->mdt_qmt_exp);
4521 mdt->mdt_qmt_exp = NULL;
4522 mdt->mdt_qmt_dev = NULL;
4526 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4528 struct md_device *next = m->mdt_child;
4529 struct lu_device *d = &m->mdt_md_dev.md_lu_dev;
4530 struct obd_device *obd = mdt2obd_dev(m);
4533 target_recovery_fini(obd);
4535 ping_evictor_stop();
4538 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4539 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4540 obd_exports_barrier(obd);
4541 obd_zombie_barrier();
4545 tgt_fini(env, &m->mdt_lut);
4546 mdt_fs_cleanup(env, m);
4547 upcall_cache_cleanup(m->mdt_identity_cache);
4548 m->mdt_identity_cache = NULL;
4550 if (m->mdt_namespace != NULL) {
4551 ldlm_namespace_free(m->mdt_namespace, NULL,
4552 d->ld_obd->obd_force);
4553 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4556 mdt_quota_fini(env, m);
4558 cfs_free_nidlist(&m->mdt_nosquash_nids);
4559 if (m->mdt_nosquash_str) {
4560 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4561 m->mdt_nosquash_str = NULL;
4562 m->mdt_nosquash_strlen = 0;
4565 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK,
4567 mdt_seq_fini(env, m);
4568 mdt_fld_fini(env, m);
4569 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4571 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4572 cfs_timer_disarm(&m->mdt_ck_timer);
4573 mdt_ck_thread_stop(m);
4578 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4580 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4582 server_put_mount(mdt2obd_dev(m)->obd_name, NULL);
4587 static int mdt_adapt_sptlrpc_conf(struct obd_device *obd, int initial)
4589 struct mdt_device *m = mdt_dev(obd->obd_lu_dev);
4590 struct sptlrpc_rule_set tmp_rset;
4593 sptlrpc_rule_set_init(&tmp_rset);
4594 rc = sptlrpc_conf_target_get_rules(obd, &tmp_rset, initial);
4596 CERROR("mdt %s: failed get sptlrpc rules: %d\n",
4601 sptlrpc_target_update_exp_flavor(obd, &tmp_rset);
4603 write_lock(&m->mdt_sptlrpc_lock);
4604 sptlrpc_rule_set_free(&m->mdt_sptlrpc_rset);
4605 m->mdt_sptlrpc_rset = tmp_rset;
4606 write_unlock(&m->mdt_sptlrpc_lock);
4611 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4613 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4614 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4616 struct mdt_thread_info *info;
4617 struct obd_device *obd;
4618 const char *dev = lustre_cfg_string(cfg, 0);
4619 const char *num = lustre_cfg_string(cfg, 2);
4620 struct lustre_mount_info *lmi = NULL;
4621 struct lustre_sb_info *lsi;
4623 struct seq_server_site *ss_site;
4624 const char *identity_upcall = "NONE";
4625 struct md_device *next;
4631 md_device_init(&m->mdt_md_dev, ldt);
4633 * Environment (env) might be missing mdt_thread_key values at that
4634 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4637 * Usually device allocation path doesn't use module key values, but
4638 * mdt has to do a lot of work here, so allocate key value.
4640 rc = lu_env_refill((struct lu_env *)env);
4644 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4645 LASSERT(info != NULL);
4647 obd = class_name2obd(dev);
4648 LASSERT(obd != NULL);
4650 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4652 m->mdt_som_conf = 0;
4654 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4655 lmi = server_get_mount(dev);
4657 CERROR("Cannot get mount info for %s!\n", dev);
4660 lsi = s2lsi(lmi->lmi_sb);
4661 /* CMD is supported only in IAM mode */
4663 node_id = simple_strtol(num, NULL, 10);
4664 obd->u.obt.obt_magic = OBT_MAGIC;
4667 rwlock_init(&m->mdt_sptlrpc_lock);
4668 sptlrpc_rule_set_init(&m->mdt_sptlrpc_rset);
4670 spin_lock_init(&m->mdt_ioepoch_lock);
4671 m->mdt_opts.mo_compat_resname = 0;
4672 m->mdt_opts.mo_mds_capa = 1;
4673 m->mdt_opts.mo_oss_capa = 1;
4674 m->mdt_capa_timeout = CAPA_TIMEOUT;
4675 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4676 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4677 m->mdt_squash_uid = 0;
4678 m->mdt_squash_gid = 0;
4679 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4680 m->mdt_nosquash_str = NULL;
4681 m->mdt_nosquash_strlen = 0;
4682 init_rwsem(&m->mdt_squash_sem);
4683 spin_lock_init(&m->mdt_osfs_lock);
4684 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4685 m->mdt_enable_remote_dir = 0;
4686 m->mdt_enable_remote_dir_gid = 0;
4688 m->mdt_md_dev.md_lu_dev.ld_ops = &mdt_lu_ops;
4689 m->mdt_md_dev.md_lu_dev.ld_obd = obd;
4690 /* set this lu_device to obd, because error handling need it */
4691 obd->obd_lu_dev = &m->mdt_md_dev.md_lu_dev;
4693 /* init the stack */
4694 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4696 CERROR("Can't init device stack, rc %d\n", rc);
4700 s = m->mdt_md_dev.md_lu_dev.ld_site;
4701 ss_site = &m->mdt_seq_site;
4702 s->ld_seq_site = ss_site;
4705 /* set server index */
4706 ss_site->ss_node_id = node_id;
4708 /* failover is the default
4709 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4710 * assumed whose ss_node_id == 0 XXX
4712 obd->obd_replayable = 1;
4713 /* No connection accepted until configurations will finish */
4714 obd->obd_no_conn = 1;
4716 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4717 char *str = lustre_cfg_string(cfg, 4);
4718 if (strchr(str, 'n')) {
4719 CWARN("%s: recovery disabled\n", obd->obd_name);
4720 obd->obd_replayable = 0;
4724 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom);
4726 GOTO(err_fini_stack, rc);
4728 rc = mdt_fld_init(env, obd->obd_name, m);
4732 rc = mdt_seq_init(env, obd->obd_name, m);
4734 GOTO(err_fini_fld, rc);
4736 snprintf(info->mti_u.ns_name, sizeof info->mti_u.ns_name,
4737 LUSTRE_MDT_NAME"-%p", m);
4738 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4739 LDLM_NAMESPACE_SERVER,
4740 LDLM_NAMESPACE_GREEDY,
4742 if (m->mdt_namespace == NULL)
4743 GOTO(err_fini_seq, rc = -ENOMEM);
4745 m->mdt_namespace->ns_lvbp = m;
4746 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4748 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4749 /* set obd_namespace for compatibility with old code */
4750 obd->obd_namespace = m->mdt_namespace;
4752 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4754 rc = mdt_ck_thread_start(m);
4756 GOTO(err_free_ns, rc);
4758 rc = mdt_fs_setup(env, m, obd, lsi);
4762 mdt_adapt_sptlrpc_conf(obd, 1);
4764 next = m->mdt_child;
4765 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4768 GOTO(err_llog_cleanup, rc);
4770 if (mntopts & MNTOPT_USERXATTR)
4771 m->mdt_opts.mo_user_xattr = 1;
4773 m->mdt_opts.mo_user_xattr = 0;
4775 if (mntopts & MNTOPT_ACL)
4776 m->mdt_opts.mo_acl = 1;
4778 m->mdt_opts.mo_acl = 0;
4780 /* XXX: to support suppgid for ACL, we enable identity_upcall
4781 * by default, otherwise, maybe got unexpected -EACCESS. */
4782 if (m->mdt_opts.mo_acl)
4783 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4785 m->mdt_identity_cache = upcall_cache_init(obd->obd_name,identity_upcall,
4786 &mdt_identity_upcall_cache_ops);
4787 if (IS_ERR(m->mdt_identity_cache)) {
4788 rc = PTR_ERR(m->mdt_identity_cache);
4789 m->mdt_identity_cache = NULL;
4790 GOTO(err_llog_cleanup, rc);
4793 rc = mdt_procfs_init(m, dev);
4795 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4796 GOTO(err_recovery, rc);
4799 rc = mdt_quota_init(env, m, cfg);
4801 GOTO(err_procfs, rc);
4803 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4804 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4805 "mdt_ldlm_client", m->mdt_ldlm_client);
4807 ping_evictor_start();
4809 /* recovery will be started upon mdt_prepare()
4810 * when the whole stack is complete and ready
4811 * to serve the requests */
4813 mdt_init_capa_ctxt(env, m);
4815 /* Reduce the initial timeout on an MDS because it doesn't need such
4816 * a long timeout as an OST does. Adaptive timeouts will adjust this
4817 * value appropriately. */
4818 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4819 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4826 target_recovery_fini(obd);
4827 upcall_cache_cleanup(m->mdt_identity_cache);
4828 m->mdt_identity_cache = NULL;
4830 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4831 mdt_fs_cleanup(env, m);
4833 cfs_timer_disarm(&m->mdt_ck_timer);
4834 mdt_ck_thread_stop(m);
4836 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4837 obd->obd_namespace = m->mdt_namespace = NULL;
4839 mdt_seq_fini(env, m);
4841 mdt_fld_fini(env, m);
4843 tgt_fini(env, &m->mdt_lut);
4845 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4848 server_put_mount(dev, lmi->lmi_mnt);
4852 /* For interoperability, the left element is old parameter, the right one
4853 * is the new version of the parameter, if some parameter is deprecated,
4854 * the new version should be set as NULL. */
4855 static struct cfg_interop_param mdt_interop_param[] = {
4856 { "mdt.group_upcall", NULL },
4857 { "mdt.quota_type", NULL },
4858 { "mdd.quota_type", NULL },
4859 { "mdt.rootsquash", "mdt.root_squash" },
4860 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4864 /* used by MGS to process specific configurations */
4865 static int mdt_process_config(const struct lu_env *env,
4866 struct lu_device *d, struct lustre_cfg *cfg)
4868 struct mdt_device *m = mdt_dev(d);
4869 struct md_device *md_next = m->mdt_child;
4870 struct lu_device *next = md2lu_dev(md_next);
4874 switch (cfg->lcfg_command) {
4876 struct lprocfs_static_vars lvars;
4877 struct obd_device *obd = d->ld_obd;
4879 /* For interoperability */
4880 struct cfg_interop_param *ptr = NULL;
4881 struct lustre_cfg *old_cfg = NULL;
4884 param = lustre_cfg_string(cfg, 1);
4885 if (param == NULL) {
4886 CERROR("param is empty\n");
4891 ptr = class_find_old_param(param, mdt_interop_param);
4893 if (ptr->new_param == NULL) {
4895 CWARN("For interoperability, skip this %s."
4896 " It is obsolete.\n", ptr->old_param);
4900 CWARN("Found old param %s, changed it to %s.\n",
4901 ptr->old_param, ptr->new_param);
4904 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4911 lprocfs_mdt_init_vars(&lvars);
4912 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4914 if (rc > 0 || rc == -ENOSYS)
4915 /* we don't understand; pass it on */
4916 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4918 if (old_cfg != NULL)
4919 lustre_cfg_free(cfg);
4924 /* others are passed further */
4925 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4931 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4932 const struct lu_object_header *hdr,
4933 struct lu_device *d)
4935 struct mdt_object *mo;
4939 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, CFS_ALLOC_IO);
4941 struct lu_object *o;
4942 struct lu_object_header *h;
4944 o = &mo->mot_obj.mo_lu;
4945 h = &mo->mot_header;
4946 lu_object_header_init(h);
4947 lu_object_init(o, h, d);
4948 lu_object_add_top(h, o);
4949 o->lo_ops = &mdt_obj_ops;
4950 mutex_init(&mo->mot_ioepoch_mutex);
4951 mutex_init(&mo->mot_lov_mutex);
4957 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4958 const struct lu_object_conf *unused)
4960 struct mdt_device *d = mdt_dev(o->lo_dev);
4961 struct lu_device *under;
4962 struct lu_object *below;
4966 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4967 PFID(lu_object_fid(o)));
4969 under = &d->mdt_child->md_lu_dev;
4970 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4971 if (below != NULL) {
4972 lu_object_add(o, below);
4979 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4981 struct mdt_object *mo = mdt_obj(o);
4982 struct lu_object_header *h;
4986 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4987 PFID(lu_object_fid(o)));
4990 lu_object_header_fini(h);
4991 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4996 static int mdt_object_print(const struct lu_env *env, void *cookie,
4997 lu_printer_t p, const struct lu_object *o)
4999 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5000 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
5001 "flags="LPX64", epochcount=%d, writecount=%d)",
5002 mdto, mdto->mot_ioepoch, mdto->mot_flags,
5003 mdto->mot_ioepoch_count, mdto->mot_writecount);
5006 static int mdt_prepare(const struct lu_env *env,
5007 struct lu_device *pdev,
5008 struct lu_device *cdev)
5010 struct mdt_device *mdt = mdt_dev(cdev);
5011 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5012 struct obd_device *obd = cdev->ld_obd;
5019 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5023 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5027 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5028 OBD_IOC_START_LFSCK,
5031 CWARN("Fail to auto trigger paused LFSCK.\n");
5033 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5034 &mdt->mdt_md_root_fid);
5038 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5039 target_recovery_init(&mdt->mdt_lut, mdt_recovery_handle);
5040 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5041 LASSERT(obd->obd_no_conn);
5042 spin_lock(&obd->obd_dev_lock);
5043 obd->obd_no_conn = 0;
5044 spin_unlock(&obd->obd_dev_lock);
5046 if (obd->obd_recovering == 0)
5047 mdt_postrecov(env, mdt);
5052 const struct lu_device_operations mdt_lu_ops = {
5053 .ldo_object_alloc = mdt_object_alloc,
5054 .ldo_process_config = mdt_process_config,
5055 .ldo_prepare = mdt_prepare,
5058 static const struct lu_object_operations mdt_obj_ops = {
5059 .loo_object_init = mdt_object_init,
5060 .loo_object_free = mdt_object_free,
5061 .loo_object_print = mdt_object_print
5064 static int mdt_obd_set_info_async(const struct lu_env *env,
5065 struct obd_export *exp,
5066 __u32 keylen, void *key,
5067 __u32 vallen, void *val,
5068 struct ptlrpc_request_set *set)
5070 struct obd_device *obd = exp->exp_obd;
5076 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5077 rc = mdt_adapt_sptlrpc_conf(obd, 0);
5085 * Match client and server connection feature flags.
5087 * Compute the compatibility flags for a connection request based on
5088 * features mutually supported by client and server.
5090 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5091 * must not be updated here, otherwise a partially initialized value may
5092 * be exposed. After the connection request is successfully processed,
5093 * the top-level MDT connect request handler atomically updates the export
5094 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5095 * reply. \see mdt_connect().
5097 * \param exp the obd_export associated with this client/target pair
5098 * \param mdt the target device for the connection
5099 * \param data stores data for this connect request
5102 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5103 * \retval -EBADE client and server feature requirements are incompatible
5105 static int mdt_connect_internal(struct obd_export *exp,
5106 struct mdt_device *mdt,
5107 struct obd_connect_data *data)
5109 LASSERT(data != NULL);
5111 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5112 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5114 /* If no known bits (which should not happen, probably,
5115 as everybody should support LOOKUP and UPDATE bits at least)
5116 revert to compat mode with plain locks. */
5117 if (!data->ocd_ibits_known &&
5118 data->ocd_connect_flags & OBD_CONNECT_IBITS)
5119 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
5121 if (!mdt->mdt_opts.mo_acl)
5122 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5124 if (!mdt->mdt_opts.mo_user_xattr)
5125 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5127 if (!mdt->mdt_som_conf)
5128 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
5130 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5131 data->ocd_brw_size = min(data->ocd_brw_size,
5132 (__u32)MD_MAX_BRW_SIZE);
5133 if (data->ocd_brw_size == 0) {
5134 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5135 " ocd_version: %x ocd_grant: %d "
5136 "ocd_index: %u ocd_brw_size is "
5137 "unexpectedly zero, network data "
5138 "corruption? Refusing connection of this"
5140 exp->exp_obd->obd_name,
5141 exp->exp_client_uuid.uuid,
5142 exp, data->ocd_connect_flags, data->ocd_version,
5143 data->ocd_grant, data->ocd_index);
5148 /* NB: Disregard the rule against updating
5149 * exp_connect_data.ocd_connect_flags in this case, since
5150 * tgt_client_new() needs to know if this is a lightweight
5151 * connection, and it is safe to expose this flag before
5152 * connection processing completes. */
5153 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5154 spin_lock(&exp->exp_lock);
5155 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5156 spin_unlock(&exp->exp_lock);
5159 data->ocd_version = LUSTRE_VERSION_CODE;
5160 exp->exp_connect_data = *data;
5161 exp->exp_mdt_data.med_ibits_known = data->ocd_ibits_known;
5163 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5164 CWARN("%s: MDS requires FID support, but client not\n",
5165 mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5169 if (mdt->mdt_som_conf &&
5170 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5171 OBD_CONNECT_MDS_MDS |
5172 OBD_CONNECT_SOM))) {
5173 CWARN("%s: MDS has SOM enabled, but client does not support "
5174 "it\n", mdt->mdt_md_dev.md_lu_dev.ld_obd->obd_name);
5178 if (data->ocd_connect_flags & OBD_CONNECT_PINGLESS) {
5179 if (suppress_pings) {
5180 spin_lock(&exp->exp_obd->obd_dev_lock);
5181 list_del_init(&exp->exp_obd_chain_timed);
5182 spin_unlock(&exp->exp_obd->obd_dev_lock);
5184 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5191 static int mdt_connect_check_sptlrpc(struct mdt_device *mdt,
5192 struct obd_export *exp,
5193 struct ptlrpc_request *req)
5195 struct sptlrpc_flavor flvr;
5198 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
5199 read_lock(&mdt->mdt_sptlrpc_lock);
5200 sptlrpc_target_choose_flavor(&mdt->mdt_sptlrpc_rset,
5204 read_unlock(&mdt->mdt_sptlrpc_lock);
5206 spin_lock(&exp->exp_lock);
5208 exp->exp_sp_peer = req->rq_sp_from;
5209 exp->exp_flvr = flvr;
5211 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
5212 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
5213 CERROR("unauthorized rpc flavor %x from %s, "
5214 "expect %x\n", req->rq_flvr.sf_rpc,
5215 libcfs_nid2str(req->rq_peer.nid),
5216 exp->exp_flvr.sf_rpc);
5220 spin_unlock(&exp->exp_lock);
5222 if (exp->exp_sp_peer != req->rq_sp_from) {
5223 CERROR("RPC source %s doesn't match %s\n",
5224 sptlrpc_part2name(req->rq_sp_from),
5225 sptlrpc_part2name(exp->exp_sp_peer));
5228 rc = sptlrpc_target_export_check(exp, req);
5235 /* mds_connect copy */
5236 static int mdt_obd_connect(const struct lu_env *env,
5237 struct obd_export **exp, struct obd_device *obd,
5238 struct obd_uuid *cluuid,
5239 struct obd_connect_data *data,
5242 struct mdt_thread_info *info;
5243 struct obd_export *lexp;
5244 struct lustre_handle conn = { 0 };
5245 struct mdt_device *mdt;
5246 struct ptlrpc_request *req;
5250 LASSERT(env != NULL);
5251 if (!exp || !obd || !cluuid)
5254 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5255 req = info->mti_pill->rc_req;
5256 mdt = mdt_dev(obd->obd_lu_dev);
5259 * first, check whether the stack is ready to handle requests
5260 * XXX: probably not very appropriate method is used now
5261 * at some point we should find a better one
5263 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5264 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5265 rc = obd_health_check(env, mdt->mdt_child_exp->exp_obd);
5268 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5271 rc = class_connect(&conn, obd, cluuid);
5275 lexp = class_conn2export(&conn);
5276 LASSERT(lexp != NULL);
5278 rc = mdt_connect_check_sptlrpc(mdt, lexp, req);
5282 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
5283 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
5285 rc = mdt_connect_internal(lexp, mdt, data);
5287 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5290 info->mti_exp = lexp;
5291 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5292 rc = tgt_client_new(env, lexp);
5294 mdt_export_stats_init(obd, lexp, localdata);
5299 class_disconnect(lexp);
5308 static int mdt_obd_reconnect(const struct lu_env *env,
5309 struct obd_export *exp, struct obd_device *obd,
5310 struct obd_uuid *cluuid,
5311 struct obd_connect_data *data,
5314 struct mdt_thread_info *info;
5315 struct mdt_device *mdt;
5316 struct ptlrpc_request *req;
5320 if (exp == NULL || obd == NULL || cluuid == NULL)
5323 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5324 req = info->mti_pill->rc_req;
5325 mdt = mdt_dev(obd->obd_lu_dev);
5327 rc = mdt_connect_check_sptlrpc(mdt, exp, req);
5331 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5333 mdt_export_stats_init(obd, exp, localdata);
5338 static int mdt_export_cleanup(struct obd_export *exp)
5340 struct mdt_export_data *med = &exp->exp_mdt_data;
5341 struct obd_device *obd = exp->exp_obd;
5342 struct mdt_device *mdt;
5343 struct mdt_thread_info *info;
5345 CFS_LIST_HEAD(closing_list);
5346 struct mdt_file_data *mfd, *n;
5350 spin_lock(&med->med_open_lock);
5351 while (!cfs_list_empty(&med->med_open_head)) {
5352 cfs_list_t *tmp = med->med_open_head.next;
5353 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5355 /* Remove mfd handle so it can't be found again.
5356 * We are consuming the mfd_list reference here. */
5357 class_handle_unhash(&mfd->mfd_handle);
5358 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5360 spin_unlock(&med->med_open_lock);
5361 mdt = mdt_dev(obd->obd_lu_dev);
5362 LASSERT(mdt != NULL);
5364 rc = lu_env_init(&env, LCT_MD_THREAD);
5368 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5369 LASSERT(info != NULL);
5370 memset(info, 0, sizeof *info);
5371 info->mti_env = &env;
5372 info->mti_mdt = mdt;
5373 info->mti_exp = exp;
5375 if (!cfs_list_empty(&closing_list)) {
5376 struct md_attr *ma = &info->mti_attr;
5378 /* Close any open files (which may also cause orphan unlinking). */
5379 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5380 cfs_list_del_init(&mfd->mfd_list);
5381 ma->ma_need = ma->ma_valid = 0;
5382 /* Don't unlink orphan on failover umount, LU-184 */
5383 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5384 ma->ma_valid = MA_FLAGS;
5385 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5387 mdt_mfd_close(info, mfd);
5390 info->mti_mdt = NULL;
5391 /* cleanup client slot early */
5392 /* Do not erase record for recoverable client. */
5393 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5394 tgt_client_del(&env, exp);
5400 static int mdt_obd_disconnect(struct obd_export *exp)
5406 class_export_get(exp);
5408 rc = server_disconnect_export(exp);
5410 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5412 rc = mdt_export_cleanup(exp);
5413 class_export_put(exp);
5417 /* FIXME: Can we avoid using these two interfaces? */
5418 static int mdt_init_export(struct obd_export *exp)
5420 struct mdt_export_data *med = &exp->exp_mdt_data;
5424 CFS_INIT_LIST_HEAD(&med->med_open_head);
5425 spin_lock_init(&med->med_open_lock);
5426 mutex_init(&med->med_idmap_mutex);
5427 med->med_idmap = NULL;
5428 spin_lock(&exp->exp_lock);
5429 exp->exp_connecting = 1;
5430 spin_unlock(&exp->exp_lock);
5432 /* self-export doesn't need client data and ldlm initialization */
5433 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5434 &exp->exp_client_uuid)))
5437 rc = tgt_client_alloc(exp);
5441 rc = ldlm_init_export(exp);
5448 tgt_client_free(exp);
5450 CERROR("%s: Failed to initialize export: rc = %d\n",
5451 exp->exp_obd->obd_name, rc);
5455 static int mdt_destroy_export(struct obd_export *exp)
5459 if (exp_connect_rmtclient(exp))
5460 mdt_cleanup_idmap(&exp->exp_mdt_data);
5462 target_destroy_export(exp);
5463 /* destroy can be called from failed obd_setup, so
5464 * checking uuid is safer than obd_self_export */
5465 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5466 &exp->exp_client_uuid)))
5469 ldlm_destroy_export(exp);
5470 tgt_client_free(exp);
5472 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5473 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5478 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5479 void *val, int vallen)
5481 struct mdt_device *mdt = mdt_dev(info->mti_exp->exp_obd->obd_lu_dev);
5482 struct getinfo_fid2path *fpout, *fpin;
5485 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5488 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5489 lustre_swab_fid2path(fpin);
5491 memcpy(fpout, fpin, sizeof(*fpin));
5492 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5495 rc = mdt_fid2path(info->mti_env, mdt, fpout);
5499 static int mdt_fid2path(const struct lu_env *env, struct mdt_device *mdt,
5500 struct getinfo_fid2path *fp)
5502 struct mdt_object *obj;
5503 struct obd_device *obd = mdt2obd_dev(mdt);
5507 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5508 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5510 if (!fid_is_sane(&fp->gf_fid))
5513 if (!fid_is_client_mdt_visible(&fp->gf_fid)) {
5514 CWARN("%s: "DFID" is invalid, sequence should be "
5515 ">= "LPX64"\n", obd->obd_name,
5516 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5520 obj = mdt_object_find(env, mdt, &fp->gf_fid);
5521 if (obj == NULL || IS_ERR(obj)) {
5522 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5527 if (mdt_object_remote(obj))
5529 else if (!mdt_object_exists(obj))
5533 mdt_object_put(env, obj);
5534 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5535 PFID(&fp->gf_fid), rc);
5539 rc = mo_path(env, md_object_next(&obj->mot_obj), fp->gf_path,
5540 fp->gf_pathlen, &fp->gf_recno, &fp->gf_linkno);
5541 mdt_object_put(env, obj);
5546 int mdt_get_info(struct mdt_thread_info *info)
5548 struct ptlrpc_request *req = mdt_info_req(info);
5556 key = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_KEY);
5558 CDEBUG(D_IOCTL, "No GETINFO key");
5561 keylen = req_capsule_get_size(info->mti_pill, &RMF_GETINFO_KEY,
5564 vallen = req_capsule_client_get(info->mti_pill, &RMF_GETINFO_VALLEN);
5565 if (vallen == NULL) {
5566 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5570 req_capsule_set_size(info->mti_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5572 rc = req_capsule_server_pack(info->mti_pill);
5573 valout = req_capsule_server_get(info->mti_pill, &RMF_GETINFO_VAL);
5574 if (valout == NULL) {
5575 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5579 if (KEY_IS(KEY_FID2PATH))
5580 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5584 lustre_msg_set_status(req->rq_repmsg, rc);
5589 /* Pass the ioc down */
5590 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5591 unsigned int cmd, int len, void *data)
5593 struct lu_context ioctl_session;
5594 struct md_device *next = mdt->mdt_child;
5598 rc = lu_context_init(&ioctl_session, LCT_SESSION);
5601 ioctl_session.lc_thread = (struct ptlrpc_thread *)cfs_current();
5602 lu_context_enter(&ioctl_session);
5603 env->le_ses = &ioctl_session;
5605 LASSERT(next->md_ops->mdo_iocontrol);
5606 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5608 lu_context_exit(&ioctl_session);
5609 lu_context_fini(&ioctl_session);
5613 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5615 struct obd_ioctl_data *data = karg;
5616 struct lu_fid *fid = (struct lu_fid *)data->ioc_inlbuf1;
5618 struct mdt_object *obj;
5619 struct mdt_lock_handle *lh;
5623 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5624 if (!fid_is_sane(fid))
5627 lh = &mti->mti_lh[MDT_LH_PARENT];
5628 mdt_lock_reg_init(lh, LCK_CR);
5630 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5632 RETURN(PTR_ERR(obj));
5634 if (mdt_object_remote(obj)) {
5637 * before calling version get the correct MDS should be
5638 * fid, this is error to find remote object here
5640 CERROR("nonlocal object "DFID"\n", PFID(fid));
5641 } else if (!mdt_object_exists(obj)) {
5642 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5645 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5646 *(__u64 *)data->ioc_inlbuf2 = version;
5649 mdt_object_unlock_put(mti, obj, lh, 1);
5653 /* ioctls on obd dev */
5654 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5655 void *karg, void *uarg)
5658 struct obd_device *obd = exp->exp_obd;
5659 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5660 struct dt_device *dt = mdt->mdt_bottom;
5664 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5665 rc = lu_env_init(&env, LCT_MD_THREAD);
5671 rc = mdt_device_sync(&env, mdt);
5673 case OBD_IOC_SET_READONLY:
5674 rc = dt->dd_ops->dt_ro(&env, dt);
5676 case OBD_IOC_ABORT_RECOVERY:
5677 CERROR("Aborting recovery for device %s\n", obd->obd_name);
5678 target_stop_recovery_thread(obd);
5681 case OBD_IOC_CHANGELOG_REG:
5682 case OBD_IOC_CHANGELOG_DEREG:
5683 case OBD_IOC_CHANGELOG_CLEAR:
5684 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5686 case OBD_IOC_START_LFSCK:
5687 case OBD_IOC_STOP_LFSCK: {
5688 struct md_device *next = mdt->mdt_child;
5689 struct obd_ioctl_data *data = karg;
5691 if (unlikely(data == NULL)) {
5696 rc = next->md_ops->mdo_iocontrol(&env, next, cmd,
5701 case OBD_IOC_GET_OBJ_VERSION: {
5702 struct mdt_thread_info *mti;
5703 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5704 memset(mti, 0, sizeof *mti);
5705 mti->mti_env = &env;
5709 rc = mdt_ioc_version_get(mti, karg);
5713 CERROR("Not supported cmd = %d for device %s\n",
5714 cmd, obd->obd_name);
5722 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5724 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5728 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5732 int mdt_obd_postrecov(struct obd_device *obd)
5737 rc = lu_env_init(&env, LCT_MD_THREAD);
5740 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5745 static struct obd_ops mdt_obd_device_ops = {
5746 .o_owner = THIS_MODULE,
5747 .o_set_info_async = mdt_obd_set_info_async,
5748 .o_connect = mdt_obd_connect,
5749 .o_reconnect = mdt_obd_reconnect,
5750 .o_disconnect = mdt_obd_disconnect,
5751 .o_init_export = mdt_init_export,
5752 .o_destroy_export = mdt_destroy_export,
5753 .o_iocontrol = mdt_iocontrol,
5754 .o_postrecov = mdt_obd_postrecov,
5757 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5758 struct lu_device *d)
5760 struct mdt_device *m = mdt_dev(d);
5767 static struct lu_device *mdt_device_free(const struct lu_env *env,
5768 struct lu_device *d)
5770 struct mdt_device *m = mdt_dev(d);
5773 md_device_fini(&m->mdt_md_dev);
5778 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5779 struct lu_device_type *t,
5780 struct lustre_cfg *cfg)
5782 struct lu_device *l;
5783 struct mdt_device *m;
5789 l = &m->mdt_md_dev.md_lu_dev;
5790 rc = mdt_init0(env, m, t, cfg);
5792 mdt_device_free(env, l);
5797 l = ERR_PTR(-ENOMEM);
5801 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5802 LU_KEY_INIT(mdt, struct mdt_thread_info);
5804 static void mdt_key_fini(const struct lu_context *ctx,
5805 struct lu_context_key *key, void* data)
5807 struct mdt_thread_info *info = data;
5809 if (info->mti_big_lmm) {
5810 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5811 info->mti_big_lmm = NULL;
5812 info->mti_big_lmmsize = 0;
5817 /* context key: mdt_thread_key */
5818 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5820 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5822 return lu_ucred(info->mti_env);
5825 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5827 return lu_ucred_check(info->mti_env);
5831 * Enable/disable COS (Commit On Sharing).
5833 * Set/Clear the COS flag in mdt options.
5835 * \param mdt mdt device
5836 * \param val 0 disables COS, other values enable COS
5838 void mdt_enable_cos(struct mdt_device *mdt, int val)
5843 mdt->mdt_opts.mo_cos = !!val;
5844 rc = lu_env_init(&env, LCT_LOCAL);
5845 if (unlikely(rc != 0)) {
5846 CWARN("lu_env initialization failed with rc = %d,"
5847 "cannot sync\n", rc);
5850 mdt_device_sync(&env, mdt);
5855 * Check COS (Commit On Sharing) status.
5857 * Return COS flag status.
5859 * \param mdt mdt device
5861 int mdt_cos_is_enabled(struct mdt_device *mdt)
5863 return mdt->mdt_opts.mo_cos != 0;
5866 static struct lu_device_type_operations mdt_device_type_ops = {
5867 .ldto_device_alloc = mdt_device_alloc,
5868 .ldto_device_free = mdt_device_free,
5869 .ldto_device_fini = mdt_device_fini
5872 static struct lu_device_type mdt_device_type = {
5873 .ldt_tags = LU_DEVICE_MD,
5874 .ldt_name = LUSTRE_MDT_NAME,
5875 .ldt_ops = &mdt_device_type_ops,
5876 .ldt_ctx_tags = LCT_MD_THREAD
5879 static int __init mdt_mod_init(void)
5881 struct lprocfs_static_vars lvars;
5884 rc = lu_kmem_init(mdt_caches);
5888 rc = mds_mod_init();
5892 lprocfs_mdt_init_vars(&lvars);
5893 rc = class_register_type(&mdt_obd_device_ops, NULL,
5894 lvars.module_vars, LUSTRE_MDT_NAME,
5900 lu_kmem_fini(mdt_caches);
5907 static void __exit mdt_mod_exit(void)
5909 class_unregister_type(LUSTRE_MDT_NAME);
5911 lu_kmem_fini(mdt_caches);
5914 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5915 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5916 MODULE_LICENSE("GPL");
5918 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);