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 #include <lustre_ioctl.h>
57 /* struct ptlrpc_request */
58 #include <lustre_net.h>
59 /* struct obd_export */
60 #include <lustre_export.h>
61 /* struct obd_device */
64 #include <dt_object.h>
65 #include <lustre_mds.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>
71 #include <lustre_lfsck.h>
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 ldlm_mode_t mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
96 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
134 return (rep->lock_policy_res1 & flag);
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, int flag)
141 info->mti_opdata &= ~flag;
142 tgt_opdata_clear(info->mti_env, flag);
145 rep->lock_policy_res1 &= ~flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, int flag)
152 info->mti_opdata |= flag;
153 tgt_opdata_set(info->mti_env, flag);
156 rep->lock_policy_res1 |= flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
168 const struct lu_name *lname)
170 lh->mlh_reg_mode = lock_mode;
171 lh->mlh_rreg_mode = lock_mode;
172 lh->mlh_type = MDT_PDO_LOCK;
174 if (lu_name_is_valid(lname)) {
175 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
177 /* XXX Workaround for LU-2856
179 * Zero is a valid return value of full_name_hash, but
180 * several users of mlh_pdo_hash assume a non-zero
181 * hash value. We therefore map zero onto an
182 * arbitrary, but consistent value (1) to avoid
183 * problems further down the road. */
184 if (unlikely(lh->mlh_pdo_hash == 0))
185 lh->mlh_pdo_hash = 1;
187 lh->mlh_pdo_hash = 0;
191 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
192 struct mdt_lock_handle *lh)
198 * Any dir access needs couple of locks:
200 * 1) on part of dir we gonna take lookup/modify;
202 * 2) on whole dir to protect it from concurrent splitting and/or to
203 * flush client's cache for readdir().
205 * so, for a given mode and object this routine decides what lock mode
206 * to use for lock #2:
208 * 1) if caller's gonna lookup in dir then we need to protect dir from
209 * being splitted only - LCK_CR
211 * 2) if caller's gonna modify dir then we need to protect dir from
212 * being splitted and to flush cache - LCK_CW
214 * 3) if caller's gonna modify dir and that dir seems ready for
215 * splitting then we need to protect it from any type of access
216 * (lookup/modify/split) - LCK_EX --bzzz
219 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
220 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
223 * Ask underlaying level its opinion about preferable PDO lock mode
224 * having access type passed as regular lock mode:
226 * - MDL_MINMODE means that lower layer does not want to specify lock
229 * - MDL_NL means that no PDO lock should be taken. This is used in some
230 * cases. Say, for non-splittable directories no need to use PDO locks
233 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
234 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
236 if (mode != MDL_MINMODE) {
237 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
240 * Lower layer does not want to specify locking mode. We do it
241 * our selves. No special protection is needed, just flush
242 * client's cache on modification and allow concurrent
245 switch (lh->mlh_reg_mode) {
247 lh->mlh_pdo_mode = LCK_EX;
250 lh->mlh_pdo_mode = LCK_CR;
253 lh->mlh_pdo_mode = LCK_CW;
256 CERROR("Not expected lock type (0x%x)\n",
257 (int)lh->mlh_reg_mode);
262 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
266 static int mdt_getstatus(struct tgt_session_info *tsi)
268 struct mdt_thread_info *info = tsi2mdt_info(tsi);
269 struct mdt_device *mdt = info->mti_mdt;
270 struct mdt_body *repbody;
275 rc = mdt_check_ucred(info);
277 GOTO(out, rc = err_serious(rc));
279 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
280 GOTO(out, rc = err_serious(-ENOMEM));
282 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
283 repbody->fid1 = mdt->mdt_md_root_fid;
284 repbody->valid |= OBD_MD_FLID;
286 if (tsi->tsi_tgt->lut_mds_capa &&
287 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
288 struct mdt_object *root;
289 struct lustre_capa *capa;
291 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
293 GOTO(out, rc = PTR_ERR(root));
295 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
297 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
298 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
300 mdt_object_put(info->mti_env, root);
302 repbody->valid |= OBD_MD_FLMDSCAPA;
306 mdt_thread_info_fini(info);
310 static int mdt_statfs(struct tgt_session_info *tsi)
312 struct ptlrpc_request *req = tgt_ses_req(tsi);
313 struct mdt_thread_info *info = tsi2mdt_info(tsi);
314 struct md_device *next = info->mti_mdt->mdt_child;
315 struct ptlrpc_service_part *svcpt;
316 struct obd_statfs *osfs;
321 svcpt = req->rq_rqbd->rqbd_svcpt;
323 /* This will trigger a watchdog timeout */
324 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
325 (MDT_SERVICE_WATCHDOG_FACTOR *
326 at_get(&svcpt->scp_at_estimate)) + 1);
328 rc = mdt_check_ucred(info);
330 GOTO(out, rc = err_serious(rc));
332 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
333 GOTO(out, rc = err_serious(-ENOMEM));
335 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
337 GOTO(out, rc = -EPROTO);
339 /** statfs information are cached in the mdt_device */
340 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
341 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
342 /** statfs data is too old, get up-to-date one */
343 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
346 spin_lock(&info->mti_mdt->mdt_osfs_lock);
347 info->mti_mdt->mdt_osfs = *osfs;
348 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
349 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
351 /** use cached statfs data */
352 spin_lock(&info->mti_mdt->mdt_osfs_lock);
353 *osfs = info->mti_mdt->mdt_osfs;
354 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
358 mdt_counter_incr(req, LPROC_MDT_STATFS);
360 mdt_thread_info_fini(info);
365 * Pack SOM attributes into the reply.
366 * Call under a DLM UPDATE lock.
368 static void mdt_pack_size2body(struct mdt_thread_info *info,
369 struct mdt_object *mo)
372 struct md_attr *ma = &info->mti_attr;
374 LASSERT(ma->ma_attr.la_valid & LA_MODE);
375 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
377 /* Check if Size-on-MDS is supported, if this is a regular file,
378 * if SOM is enabled on the object and if SOM cache exists and valid.
379 * Otherwise do not pack Size-on-MDS attributes to the reply. */
380 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
381 !S_ISREG(ma->ma_attr.la_mode) ||
382 !mdt_object_is_som_enabled(mo) ||
383 !(ma->ma_valid & MA_SOM))
386 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
387 b->size = ma->ma_som->msd_size;
388 b->blocks = ma->ma_som->msd_blocks;
391 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
392 const struct lu_attr *attr, const struct lu_fid *fid)
394 struct md_attr *ma = &info->mti_attr;
396 LASSERT(ma->ma_valid & MA_INODE);
398 b->atime = attr->la_atime;
399 b->mtime = attr->la_mtime;
400 b->ctime = attr->la_ctime;
401 b->mode = attr->la_mode;
402 b->size = attr->la_size;
403 b->blocks = attr->la_blocks;
404 b->uid = attr->la_uid;
405 b->gid = attr->la_gid;
406 b->flags = attr->la_flags;
407 b->nlink = attr->la_nlink;
408 b->rdev = attr->la_rdev;
410 /*XXX should pack the reply body according to lu_valid*/
411 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
412 OBD_MD_FLGID | OBD_MD_FLTYPE |
413 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
414 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
416 if (!S_ISREG(attr->la_mode)) {
417 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
418 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
419 /* means no objects are allocated on osts. */
420 LASSERT(!(ma->ma_valid & MA_LOV));
421 /* just ignore blocks occupied by extend attributes on MDS */
423 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
424 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
425 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
426 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
427 /* A released file stores its size on MDS. */
428 /* But return 1 block for released file, unless tools like tar
429 * will consider it fully sparse. (LU-3864)
431 if (unlikely(b->size == 0))
435 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
440 b->valid |= OBD_MD_FLID;
441 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
442 PFID(fid), b->nlink, b->mode, b->size);
446 mdt_body_reverse_idmap(info, b);
448 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
449 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
450 PFID(fid), (unsigned long long)b->size);
453 static inline int mdt_body_has_lov(const struct lu_attr *la,
454 const struct mdt_body *body)
456 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
457 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
460 void mdt_client_compatibility(struct mdt_thread_info *info)
462 struct mdt_body *body;
463 struct ptlrpc_request *req = mdt_info_req(info);
464 struct obd_export *exp = req->rq_export;
465 struct md_attr *ma = &info->mti_attr;
466 struct lu_attr *la = &ma->ma_attr;
469 if (exp_connect_layout(exp))
470 /* the client can deal with 16-bit lmm_stripe_count */
473 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
475 if (!mdt_body_has_lov(la, body))
478 /* now we have a reply with a lov for a client not compatible with the
479 * layout lock so we have to clean the layout generation number */
480 if (S_ISREG(la->la_mode))
481 ma->ma_lmm->lmm_layout_gen = 0;
485 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
487 const struct lu_env *env = info->mti_env;
490 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
499 /* Is it a directory? Let's check for the LMV as well */
500 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
501 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
505 rc2 = mo_xattr_get(env, mdt_object_child(o),
507 XATTR_NAME_DEFAULT_LMV);
509 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
517 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
520 const struct lu_env *env = info->mti_env;
524 LASSERT(info->mti_big_lmm_used == 0);
525 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
529 /* big_lmm may need to be grown */
530 if (info->mti_big_lmmsize < rc) {
531 int size = size_roundup_power2(rc);
533 if (info->mti_big_lmmsize > 0) {
534 /* free old buffer */
535 LASSERT(info->mti_big_lmm);
536 OBD_FREE_LARGE(info->mti_big_lmm,
537 info->mti_big_lmmsize);
538 info->mti_big_lmm = NULL;
539 info->mti_big_lmmsize = 0;
542 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
543 if (info->mti_big_lmm == NULL)
545 info->mti_big_lmmsize = size;
547 LASSERT(info->mti_big_lmmsize >= rc);
549 info->mti_buf.lb_buf = info->mti_big_lmm;
550 info->mti_buf.lb_len = info->mti_big_lmmsize;
551 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
556 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
557 struct md_attr *ma, const char *name)
559 struct md_object *next = mdt_object_child(o);
560 struct lu_buf *buf = &info->mti_buf;
563 if (strcmp(name, XATTR_NAME_LOV) == 0) {
564 buf->lb_buf = ma->ma_lmm;
565 buf->lb_len = ma->ma_lmm_size;
566 LASSERT(!(ma->ma_valid & MA_LOV));
567 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
568 buf->lb_buf = ma->ma_lmv;
569 buf->lb_len = ma->ma_lmv_size;
570 LASSERT(!(ma->ma_valid & MA_LMV));
571 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
572 buf->lb_buf = ma->ma_lmv;
573 buf->lb_len = ma->ma_lmv_size;
574 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
579 rc = mo_xattr_get(info->mti_env, next, buf, name);
583 if (strcmp(name, XATTR_NAME_LOV) == 0) {
584 if (info->mti_big_lmm_used)
585 ma->ma_lmm = info->mti_big_lmm;
587 /* NOT return LOV EA with hole to old client. */
588 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
589 LOV_PATTERN_F_HOLE) &&
590 !(exp_connect_flags(info->mti_exp) &
591 OBD_CONNECT_LFSCK)) {
594 ma->ma_lmm_size = rc;
595 ma->ma_valid |= MA_LOV;
597 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
598 if (info->mti_big_lmm_used)
599 ma->ma_lmv = info->mti_big_lmm;
601 ma->ma_lmv_size = rc;
602 ma->ma_valid |= MA_LMV;
603 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
604 ma->ma_lmv_size = rc;
605 ma->ma_valid |= MA_LMV_DEF;
608 /* Update mdt_max_mdsize so all clients will be aware that */
609 if (info->mti_mdt->mdt_max_mdsize < rc)
610 info->mti_mdt->mdt_max_mdsize = rc;
613 } else if (rc == -ENODATA) {
616 } else if (rc == -ERANGE) {
617 /* Default LMV has fixed size, so it must be able to fit
618 * in the original buffer */
619 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
621 rc = mdt_big_xattr_get(info, o, name);
623 info->mti_big_lmm_used = 1;
631 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
632 struct mdt_object *o, struct lu_fid *pfid)
634 struct lu_buf *buf = &info->mti_buf;
635 struct link_ea_header *leh;
636 struct link_ea_entry *lee;
640 buf->lb_buf = info->mti_big_lmm;
641 buf->lb_len = info->mti_big_lmmsize;
642 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
643 buf, XATTR_NAME_LINK);
644 /* ignore errors, MA_PFID won't be set and it is
645 * up to the caller to treat this as an error */
646 if (rc == -ERANGE || buf->lb_len == 0) {
647 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
648 buf->lb_buf = info->mti_big_lmm;
649 buf->lb_len = info->mti_big_lmmsize;
654 if (rc < sizeof(*leh)) {
655 CERROR("short LinkEA on "DFID": rc = %d\n",
656 PFID(mdt_object_fid(o)), rc);
660 leh = (struct link_ea_header *) buf->lb_buf;
661 lee = (struct link_ea_entry *)(leh + 1);
662 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
663 leh->leh_magic = LINK_EA_MAGIC;
664 leh->leh_reccount = __swab32(leh->leh_reccount);
665 leh->leh_len = __swab64(leh->leh_len);
667 if (leh->leh_magic != LINK_EA_MAGIC)
669 if (leh->leh_reccount == 0)
672 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
673 fid_be_to_cpu(pfid, pfid);
678 int mdt_attr_get_complex(struct mdt_thread_info *info,
679 struct mdt_object *o, struct md_attr *ma)
681 const struct lu_env *env = info->mti_env;
682 struct md_object *next = mdt_object_child(o);
683 struct lu_buf *buf = &info->mti_buf;
684 u32 mode = lu_object_attr(&next->mo_lu);
685 int need = ma->ma_need;
691 if (need & MA_INODE) {
692 ma->ma_need = MA_INODE;
693 rc = mo_attr_get(env, next, ma);
696 ma->ma_valid |= MA_INODE;
699 if (need & MA_PFID) {
700 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
702 ma->ma_valid |= MA_PFID;
703 /* ignore this error, parent fid is not mandatory */
707 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
708 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
713 if (need & MA_LMV && S_ISDIR(mode)) {
714 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
719 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
720 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
725 if (need & MA_SOM && S_ISREG(mode)) {
726 buf->lb_buf = info->mti_xattr_buf;
727 buf->lb_len = sizeof(info->mti_xattr_buf);
728 CLASSERT(sizeof(struct som_attrs) <=
729 sizeof(info->mti_xattr_buf));
730 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
731 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
733 ma->ma_valid |= MA_SOM;
734 else if (rc2 < 0 && rc2 != -ENODATA)
738 if (need & MA_HSM && S_ISREG(mode)) {
739 buf->lb_buf = info->mti_xattr_buf;
740 buf->lb_len = sizeof(info->mti_xattr_buf);
741 CLASSERT(sizeof(struct hsm_attrs) <=
742 sizeof(info->mti_xattr_buf));
743 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
744 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
746 ma->ma_valid |= MA_HSM;
747 else if (rc2 < 0 && rc2 != -ENODATA)
751 #ifdef CONFIG_FS_POSIX_ACL
752 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
753 buf->lb_buf = ma->ma_acl;
754 buf->lb_len = ma->ma_acl_size;
755 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
757 ma->ma_acl_size = rc2;
758 ma->ma_valid |= MA_ACL_DEF;
759 } else if (rc2 == -ENODATA) {
768 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
769 rc, ma->ma_valid, ma->ma_lmm);
773 static int mdt_getattr_internal(struct mdt_thread_info *info,
774 struct mdt_object *o, int ma_need)
776 struct md_object *next = mdt_object_child(o);
777 const struct mdt_body *reqbody = info->mti_body;
778 struct ptlrpc_request *req = mdt_info_req(info);
779 struct md_attr *ma = &info->mti_attr;
780 struct lu_attr *la = &ma->ma_attr;
781 struct req_capsule *pill = info->mti_pill;
782 const struct lu_env *env = info->mti_env;
783 struct mdt_body *repbody;
784 struct lu_buf *buffer = &info->mti_buf;
789 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
790 RETURN(err_serious(-ENOMEM));
792 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
796 if (mdt_object_remote(o)) {
797 /* This object is located on remote node.*/
798 /* Return -ENOTSUPP for old client */
799 if (!mdt_is_dne_client(req->rq_export))
800 GOTO(out, rc = -ENOTSUPP);
802 repbody->fid1 = *mdt_object_fid(o);
803 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
807 if (reqbody->eadatasize > 0) {
808 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
809 if (buffer->lb_buf == NULL)
810 GOTO(out, rc = -EPROTO);
811 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
814 buffer->lb_buf = NULL;
816 ma_need &= ~(MA_LOV | MA_LMV);
817 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
818 mdt_obd_name(info->mti_mdt),
819 req->rq_export->exp_client_uuid.uuid);
822 /* If it is dir object and client require MEA, then we got MEA */
823 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
824 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
825 /* Assumption: MDT_MD size is enough for lmv size. */
826 ma->ma_lmv = buffer->lb_buf;
827 ma->ma_lmv_size = buffer->lb_len;
828 ma->ma_need = MA_INODE;
829 if (ma->ma_lmv_size > 0) {
830 if (reqbody->valid & OBD_MD_MEA)
831 ma->ma_need |= MA_LMV;
832 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
833 ma->ma_need |= MA_LMV_DEF;
836 ma->ma_lmm = buffer->lb_buf;
837 ma->ma_lmm_size = buffer->lb_len;
838 ma->ma_need = MA_INODE | MA_HSM;
839 if (ma->ma_lmm_size > 0)
840 ma->ma_need |= MA_LOV;
843 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
844 reqbody->valid & OBD_MD_FLDIREA &&
845 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
846 /* get default stripe info for this dir. */
847 ma->ma_need |= MA_LOV_DEF;
849 ma->ma_need |= ma_need;
850 if (ma->ma_need & MA_SOM)
851 ma->ma_som = &info->mti_u.som.data;
853 rc = mdt_attr_get_complex(info, o, ma);
855 CERROR("%s: getattr error for "DFID": rc = %d\n",
856 mdt_obd_name(info->mti_mdt),
857 PFID(mdt_object_fid(o)), rc);
861 /* if file is released, check if a restore is running */
862 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
863 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
864 repbody->t_state = MS_RESTORE;
865 repbody->valid |= OBD_MD_TSTATE;
868 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
870 /* the Lustre protocol supposes to return default striping
871 * on the user-visible root if explicitly requested */
872 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
873 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
874 struct lu_fid rootfid;
875 struct mdt_object *root;
876 struct mdt_device *mdt = info->mti_mdt;
878 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
881 root = mdt_object_find(env, mdt, &rootfid);
883 RETURN(PTR_ERR(root));
884 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
885 mdt_object_put(info->mti_env, root);
887 CERROR("%s: getattr error for "DFID": rc = %d\n",
888 mdt_obd_name(info->mti_mdt),
889 PFID(mdt_object_fid(o)), rc);
894 if (likely(ma->ma_valid & MA_INODE))
895 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
899 if (mdt_body_has_lov(la, reqbody)) {
900 if (ma->ma_valid & MA_LOV) {
901 LASSERT(ma->ma_lmm_size);
902 repbody->eadatasize = ma->ma_lmm_size;
903 if (S_ISDIR(la->la_mode))
904 repbody->valid |= OBD_MD_FLDIREA;
906 repbody->valid |= OBD_MD_FLEASIZE;
907 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
909 if (ma->ma_valid & MA_LMV) {
910 /* Return -ENOTSUPP for old client */
911 if (!mdt_is_striped_client(req->rq_export))
914 LASSERT(S_ISDIR(la->la_mode));
915 mdt_dump_lmv(D_INFO, ma->ma_lmv);
916 repbody->eadatasize = ma->ma_lmv_size;
917 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
919 if (ma->ma_valid & MA_LMV_DEF) {
920 /* Return -ENOTSUPP for old client */
921 if (!mdt_is_striped_client(req->rq_export))
923 LASSERT(S_ISDIR(la->la_mode));
924 repbody->eadatasize = ma->ma_lmv_size;
925 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
927 } else if (S_ISLNK(la->la_mode) &&
928 reqbody->valid & OBD_MD_LINKNAME) {
929 buffer->lb_buf = ma->ma_lmm;
930 /* eadatasize from client includes NULL-terminator, so
931 * there is no need to read it */
932 buffer->lb_len = reqbody->eadatasize - 1;
933 rc = mo_readlink(env, next, buffer);
934 if (unlikely(rc <= 0)) {
935 CERROR("%s: readlink failed for "DFID": rc = %d\n",
936 mdt_obd_name(info->mti_mdt),
937 PFID(mdt_object_fid(o)), rc);
940 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
942 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
944 repbody->valid |= OBD_MD_LINKNAME;
945 /* we need to report back size with NULL-terminator
946 * because client expects that */
947 repbody->eadatasize = rc + 1;
948 if (repbody->eadatasize != reqbody->eadatasize)
949 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
950 "on "DFID ", expected %d\n",
951 mdt_obd_name(info->mti_mdt),
952 rc, PFID(mdt_object_fid(o)),
953 reqbody->eadatasize - 1);
955 ((char *)ma->ma_lmm)[rc] = 0;
957 /* If the total CDEBUG() size is larger than a page, it
958 * will print a warning to the console, avoid this by
959 * printing just the last part of the symlink. */
960 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
961 print_limit < rc ? "..." : "", print_limit,
962 (char *)ma->ma_lmm + rc - print_limit, rc);
967 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
968 repbody->max_cookiesize = 0;
969 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
970 repbody->valid |= OBD_MD_FLMODEASIZE;
971 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
972 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
973 repbody->max_cookiesize);
976 if (exp_connect_rmtclient(info->mti_exp) &&
977 reqbody->valid & OBD_MD_FLRMTPERM) {
978 void *buf = req_capsule_server_get(pill, &RMF_ACL);
980 /* mdt_getattr_lock only */
981 rc = mdt_pack_remote_perm(info, o, buf);
983 repbody->valid &= ~OBD_MD_FLRMTPERM;
984 repbody->aclsize = 0;
987 repbody->valid |= OBD_MD_FLRMTPERM;
988 repbody->aclsize = sizeof(struct mdt_remote_perm);
991 #ifdef CONFIG_FS_POSIX_ACL
992 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
993 (reqbody->valid & OBD_MD_FLACL)) {
994 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
995 buffer->lb_len = req_capsule_get_size(pill,
996 &RMF_ACL, RCL_SERVER);
997 if (buffer->lb_len > 0) {
998 rc = mo_xattr_get(env, next, buffer,
999 XATTR_NAME_ACL_ACCESS);
1001 if (rc == -ENODATA) {
1002 repbody->aclsize = 0;
1003 repbody->valid |= OBD_MD_FLACL;
1005 } else if (rc == -EOPNOTSUPP) {
1008 CERROR("%s: unable to read "DFID
1010 mdt_obd_name(info->mti_mdt),
1011 PFID(mdt_object_fid(o)), rc);
1014 repbody->aclsize = rc;
1015 repbody->valid |= OBD_MD_FLACL;
1022 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
1023 info->mti_mdt->mdt_lut.lut_mds_capa &&
1024 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1025 struct lustre_capa *capa;
1027 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1029 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1030 rc = mo_capa_get(env, next, capa, 0);
1033 repbody->valid |= OBD_MD_FLMDSCAPA;
1038 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1043 static int mdt_renew_capa(struct mdt_thread_info *info)
1045 struct mdt_object *obj = info->mti_object;
1046 struct mdt_body *body;
1047 struct lustre_capa *capa, *c;
1051 /* if object doesn't exist, or server has disabled capability,
1052 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1055 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1056 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1059 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1060 LASSERT(body != NULL);
1062 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1065 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1069 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1071 body->valid |= OBD_MD_FLOSSCAPA;
1075 static int mdt_getattr(struct tgt_session_info *tsi)
1077 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1078 struct mdt_object *obj = info->mti_object;
1079 struct req_capsule *pill = info->mti_pill;
1080 struct mdt_body *reqbody;
1081 struct mdt_body *repbody;
1085 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1088 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1089 rc = req_capsule_server_pack(pill);
1091 RETURN(err_serious(rc));
1092 rc = mdt_renew_capa(info);
1093 GOTO(out_shrink, rc);
1096 LASSERT(obj != NULL);
1097 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1099 /* Unlike intent case where we need to pre-fill out buffers early on
1100 * in intent policy for ldlm reasons, here we can have a much better
1101 * guess at EA size by just reading it from disk.
1102 * Exceptions are readdir and (missing) directory striping */
1104 if (reqbody->valid & OBD_MD_LINKNAME) {
1105 /* No easy way to know how long is the symlink, but it cannot
1106 * be more than PATH_MAX, so we allocate +1 */
1109 /* A special case for fs ROOT: getattr there might fetch
1110 * default EA for entire fs, not just for this dir!
1112 } else if (lu_fid_eq(mdt_object_fid(obj),
1113 &info->mti_mdt->mdt_md_root_fid) &&
1114 (reqbody->valid & OBD_MD_FLDIREA) &&
1115 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1117 /* Should the default strping be bigger, mdt_fix_reply
1118 * will reallocate */
1119 rc = DEF_REP_MD_SIZE;
1121 /* Read the actual EA size from disk */
1122 rc = mdt_attr_get_eabuf_size(info, obj);
1126 GOTO(out_shrink, rc);
1128 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1130 rc = req_capsule_server_pack(pill);
1131 if (unlikely(rc != 0))
1132 GOTO(out, rc = err_serious(rc));
1134 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1135 LASSERT(repbody != NULL);
1136 repbody->eadatasize = 0;
1137 repbody->aclsize = 0;
1139 if (reqbody->valid & OBD_MD_FLRMTPERM)
1140 rc = mdt_init_ucred(info, reqbody);
1142 rc = mdt_check_ucred(info);
1144 GOTO(out_shrink, rc);
1146 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1149 * Don't check capability at all, because rename might getattr for
1150 * remote obj, and at that time no capability is available.
1152 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1153 rc = mdt_getattr_internal(info, obj, 0);
1154 if (reqbody->valid & OBD_MD_FLRMTPERM)
1155 mdt_exit_ucred(info);
1158 mdt_client_compatibility(info);
1159 rc2 = mdt_fix_reply(info);
1163 mdt_thread_info_fini(info);
1167 static int mdt_is_subdir(struct tgt_session_info *tsi)
1169 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1170 struct mdt_object *o = info->mti_object;
1171 struct req_capsule *pill = info->mti_pill;
1172 const struct mdt_body *body = info->mti_body;
1173 struct mdt_body *repbody;
1179 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1182 * We save last checked parent fid to @repbody->fid1 for remote
1185 LASSERT(fid_is_sane(&body->fid2));
1186 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1187 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1188 &body->fid2, &repbody->fid1);
1189 if (rc == 0 || rc == -EREMOTE)
1190 repbody->valid |= OBD_MD_FLID;
1192 mdt_thread_info_fini(info);
1196 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1198 struct mdt_thread_info *info;
1199 struct ptlrpc_request *req = tgt_ses_req(tsi);
1200 struct obd_export *exp = req->rq_export;
1201 struct mdt_object *o1, *o2, *o;
1202 struct mdt_lock_handle *lh1, *lh2;
1203 struct mdc_swap_layouts *msl;
1207 /* client does not support layout lock, so layout swaping
1209 * FIXME: there is a problem for old clients which don't support
1210 * layout lock yet. If those clients have already opened the file
1211 * they won't be notified at all so that old layout may still be
1212 * used to do IO. This can be fixed after file release is landed by
1213 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1214 if (!exp_connect_layout(exp))
1215 RETURN(-EOPNOTSUPP);
1217 info = tsi2mdt_info(tsi);
1219 if (info->mti_dlm_req != NULL)
1220 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1222 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1223 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1224 req_capsule_client_get(info->mti_pill,
1227 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1228 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1229 req_capsule_client_get(info->mti_pill,
1232 o1 = info->mti_object;
1233 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1234 &info->mti_body->fid2);
1236 GOTO(out, rc = PTR_ERR(o));
1238 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1239 GOTO(put, rc = -ENOENT);
1241 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1242 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1248 /* permission check. Make sure the calling process having permission
1249 * to write both files. */
1250 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1255 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1260 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1262 GOTO(put, rc = -EPROTO);
1264 lh1 = &info->mti_lh[MDT_LH_NEW];
1265 mdt_lock_reg_init(lh1, LCK_EX);
1266 lh2 = &info->mti_lh[MDT_LH_OLD];
1267 mdt_lock_reg_init(lh2, LCK_EX);
1269 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1270 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1274 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1275 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1279 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1280 mdt_object_child(o2), msl->msl_flags);
1283 mdt_object_unlock(info, o2, lh2, rc);
1285 mdt_object_unlock(info, o1, lh1, rc);
1287 mdt_object_put(info->mti_env, o);
1289 mdt_thread_info_fini(info);
1293 static int mdt_raw_lookup(struct mdt_thread_info *info,
1294 struct mdt_object *parent,
1295 const struct lu_name *lname,
1296 struct ldlm_reply *ldlm_rep)
1298 struct md_object *next = mdt_object_child(info->mti_object);
1299 const struct mdt_body *reqbody = info->mti_body;
1300 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1301 struct mdt_body *repbody;
1305 if (reqbody->valid != OBD_MD_FLID)
1308 LASSERT(!info->mti_cross_ref);
1310 /* Only got the fid of this obj by name */
1311 fid_zero(child_fid);
1312 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1315 /* XXX is raw_lookup possible as intent operation? */
1318 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1321 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1323 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1326 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1327 repbody->fid1 = *child_fid;
1328 repbody->valid = OBD_MD_FLID;
1334 * UPDATE lock should be taken against parent, and be release before exit;
1335 * child_bits lock should be taken against child, and be returned back:
1336 * (1)normal request should release the child lock;
1337 * (2)intent request will grant the lock to client.
1339 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1340 struct mdt_lock_handle *lhc,
1342 struct ldlm_reply *ldlm_rep)
1344 struct ptlrpc_request *req = mdt_info_req(info);
1345 struct mdt_body *reqbody = NULL;
1346 struct mdt_object *parent = info->mti_object;
1347 struct mdt_object *child;
1348 struct md_object *next = mdt_object_child(parent);
1349 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1350 struct lu_name *lname = NULL;
1351 struct mdt_lock_handle *lhp = NULL;
1352 struct ldlm_lock *lock;
1359 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1360 LASSERT(ergo(is_resent,
1361 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1363 LASSERT(parent != NULL);
1365 if (info->mti_cross_ref) {
1366 /* Only getattr on the child. Parent is on another node. */
1367 mdt_set_disposition(info, ldlm_rep,
1368 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1370 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1372 PFID(mdt_object_fid(child)), ldlm_rep);
1375 /* Do not take lock for resent case. */
1376 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1377 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1378 lhc->mlh_reg_lh.cookie);
1379 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1380 &lock->l_resource->lr_name));
1381 LDLM_LOCK_PUT(lock);
1383 mdt_lock_handle_init(lhc);
1384 mdt_lock_reg_init(lhc, LCK_PR);
1387 * Object's name is on another MDS, no lookup or layout
1388 * lock is needed here but update lock is.
1390 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1391 MDS_INODELOCK_LAYOUT);
1392 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1394 rc = mdt_object_lock(info, child, lhc, child_bits,
1400 /* Finally, we can get attr for child. */
1401 if (!mdt_object_exists(child)) {
1402 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1404 "remote object doesn't exist.\n");
1405 mdt_object_unlock(info, child, lhc, 1);
1409 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1410 rc = mdt_getattr_internal(info, child, 0);
1411 if (unlikely(rc != 0))
1412 mdt_object_unlock(info, child, lhc, 1);
1417 lname = &info->mti_name;
1418 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1420 if (lu_name_is_valid(lname)) {
1421 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1422 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1423 PNAME(lname), ldlm_rep);
1425 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1426 if (unlikely(reqbody == NULL))
1427 RETURN(err_serious(-EPROTO));
1429 *child_fid = reqbody->fid2;
1431 if (unlikely(!fid_is_sane(child_fid)))
1432 RETURN(err_serious(-EINVAL));
1434 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1436 PFID(mdt_object_fid(parent)),
1437 PFID(&reqbody->fid2), ldlm_rep);
1440 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1442 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1443 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1445 "Parent doesn't exist!\n");
1449 if (mdt_object_remote(parent)) {
1450 CERROR("%s: parent "DFID" is on remote target\n",
1451 mdt_obd_name(info->mti_mdt),
1452 PFID(mdt_object_fid(parent)));
1456 if (lu_name_is_valid(lname)) {
1457 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1464 /* step 1: lock parent only if parent is a directory */
1465 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1466 lhp = &info->mti_lh[MDT_LH_PARENT];
1467 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1468 rc = mdt_object_lock(info, parent, lhp,
1469 MDS_INODELOCK_UPDATE,
1471 if (unlikely(rc != 0))
1475 /* step 2: lookup child's fid by name */
1476 fid_zero(child_fid);
1477 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1480 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1483 GOTO(out_parent, rc);
1486 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1489 *step 3: find the child object by fid & lock it.
1490 * regardless if it is local or remote.
1492 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1493 * set parent dir fid the same as child fid in getattr by fid case
1494 * we should not lu_object_find() the object again, could lead
1495 * to hung if there is a concurrent unlink destroyed the object.
1497 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1498 mdt_object_get(info->mti_env, parent);
1501 child = mdt_object_find(info->mti_env, info->mti_mdt,
1505 if (unlikely(IS_ERR(child)))
1506 GOTO(out_parent, rc = PTR_ERR(child));
1508 /* Do not take lock for resent case. */
1509 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1510 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1511 lhc->mlh_reg_lh.cookie);
1513 if (!fid_res_name_eq(mdt_object_fid(child),
1514 &lock->l_resource->lr_name)) {
1515 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1516 &lock->l_resource->lr_name),
1517 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1518 PLDLMRES(lock->l_resource),
1519 PFID(mdt_object_fid(parent)));
1520 CWARN("Although resent, but still not get child lock"
1521 "parent:"DFID" child:"DFID"\n",
1522 PFID(mdt_object_fid(parent)),
1523 PFID(mdt_object_fid(child)));
1524 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1525 LDLM_LOCK_PUT(lock);
1528 LDLM_LOCK_PUT(lock);
1531 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1532 mdt_lock_handle_init(lhc);
1533 mdt_lock_reg_init(lhc, LCK_PR);
1536 if (!mdt_object_exists(child)) {
1537 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1539 "Object doesn't exist!\n");
1540 GOTO(out_child, rc = -ENOENT);
1543 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1544 mdt_object_exists(child) && !mdt_object_remote(child)) {
1545 struct md_attr *ma = &info->mti_attr;
1548 ma->ma_need = MA_INODE;
1549 rc = mdt_attr_get_complex(info, child, ma);
1550 if (unlikely(rc != 0))
1551 GOTO(out_child, rc);
1553 /* If the file has not been changed for some time, we
1554 * return not only a LOOKUP lock, but also an UPDATE
1555 * lock and this might save us RPC on later STAT. For
1556 * directories, it also let negative dentry cache start
1557 * working for this dir. */
1558 if (ma->ma_valid & MA_INODE &&
1559 ma->ma_attr.la_valid & LA_CTIME &&
1560 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1561 ma->ma_attr.la_ctime < cfs_time_current_sec())
1562 child_bits |= MDS_INODELOCK_UPDATE;
1565 /* layout lock must be granted in a best-effort way
1566 * for IT operations */
1567 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1568 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1569 exp_connect_layout(info->mti_exp) &&
1570 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1571 !mdt_object_remote(child) && ldlm_rep != NULL) {
1572 /* try to grant layout lock for regular file. */
1578 child_bits |= MDS_INODELOCK_LAYOUT;
1579 /* try layout lock, it may fail to be granted due to
1580 * contention at LOOKUP or UPDATE */
1581 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1583 child_bits &= ~MDS_INODELOCK_LAYOUT;
1584 LASSERT(child_bits != 0);
1585 rc = mdt_object_lock(info, child, lhc,
1586 child_bits, MDT_CROSS_LOCK);
1591 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1592 * client will enqueue the lock to the remote MDT */
1593 if (mdt_object_remote(child))
1594 child_bits &= ~MDS_INODELOCK_UPDATE;
1595 rc = mdt_object_lock(info, child, lhc, child_bits,
1598 if (unlikely(rc != 0))
1599 GOTO(out_child, rc);
1602 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1603 /* Get MA_SOM attributes if update lock is given. */
1605 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1606 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1609 /* finally, we can get attr for child. */
1610 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1611 rc = mdt_getattr_internal(info, child, ma_need);
1612 if (unlikely(rc != 0)) {
1613 mdt_object_unlock(info, child, lhc, 1);
1615 /* Debugging code. */
1616 LDLM_DEBUG(lock, "Returning lock to client");
1617 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1618 &lock->l_resource->lr_name),
1619 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1620 PLDLMRES(lock->l_resource),
1621 PFID(mdt_object_fid(child)));
1622 if (mdt_object_exists(child) && !mdt_object_remote(child))
1623 mdt_pack_size2body(info, child);
1626 LDLM_LOCK_PUT(lock);
1630 mdt_object_put(info->mti_env, child);
1633 mdt_object_unlock(info, parent, lhp, 1);
1637 /* normal handler: should release the child lock */
1638 static int mdt_getattr_name(struct tgt_session_info *tsi)
1640 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1641 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1642 struct mdt_body *reqbody;
1643 struct mdt_body *repbody;
1647 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1648 LASSERT(reqbody != NULL);
1649 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1650 LASSERT(repbody != NULL);
1652 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1653 repbody->eadatasize = 0;
1654 repbody->aclsize = 0;
1656 rc = mdt_init_ucred(info, reqbody);
1658 GOTO(out_shrink, rc);
1660 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1661 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1662 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1663 lhc->mlh_reg_lh.cookie = 0;
1665 mdt_exit_ucred(info);
1668 mdt_client_compatibility(info);
1669 rc2 = mdt_fix_reply(info);
1672 mdt_thread_info_fini(info);
1676 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1677 void *karg, void *uarg);
1679 static int mdt_set_info(struct tgt_session_info *tsi)
1681 struct ptlrpc_request *req = tgt_ses_req(tsi);
1684 int keylen, vallen, rc = 0;
1688 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1690 DEBUG_REQ(D_HA, req, "no set_info key");
1691 RETURN(err_serious(-EFAULT));
1694 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1697 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1699 DEBUG_REQ(D_HA, req, "no set_info val");
1700 RETURN(err_serious(-EFAULT));
1703 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1706 /* Swab any part of val you need to here */
1707 if (KEY_IS(KEY_READ_ONLY)) {
1708 spin_lock(&req->rq_export->exp_lock);
1710 *exp_connect_flags_ptr(req->rq_export) |=
1713 *exp_connect_flags_ptr(req->rq_export) &=
1714 ~OBD_CONNECT_RDONLY;
1715 spin_unlock(&req->rq_export->exp_lock);
1716 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1717 struct changelog_setinfo *cs = val;
1719 if (vallen != sizeof(*cs)) {
1720 CERROR("%s: bad changelog_clear setinfo size %d\n",
1721 tgt_name(tsi->tsi_tgt), vallen);
1724 if (ptlrpc_req_need_swab(req)) {
1725 __swab64s(&cs->cs_recno);
1726 __swab32s(&cs->cs_id);
1729 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1737 static int mdt_readpage(struct tgt_session_info *tsi)
1739 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1740 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1741 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1742 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1743 struct mdt_body *repbody;
1749 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1750 RETURN(err_serious(-ENOMEM));
1752 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1753 if (repbody == NULL || reqbody == NULL)
1754 RETURN(err_serious(-EFAULT));
1757 * prepare @rdpg before calling lower layers and transfer itself. Here
1758 * reqbody->size contains offset of where to start to read and
1759 * reqbody->nlink contains number bytes to read.
1761 rdpg->rp_hash = reqbody->size;
1762 if (rdpg->rp_hash != reqbody->size) {
1763 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1764 rdpg->rp_hash, reqbody->size);
1768 rdpg->rp_attrs = reqbody->mode;
1769 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1770 rdpg->rp_attrs |= LUDA_64BITHASH;
1771 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1772 exp_max_brw_size(tsi->tsi_exp));
1773 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1775 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1776 if (rdpg->rp_pages == NULL)
1779 for (i = 0; i < rdpg->rp_npages; ++i) {
1780 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1781 if (rdpg->rp_pages[i] == NULL)
1782 GOTO(free_rdpg, rc = -ENOMEM);
1785 /* call lower layers to fill allocated pages with directory data */
1786 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1788 GOTO(free_rdpg, rc);
1790 /* send pages to client */
1791 rc = tgt_sendpage(tsi, rdpg, rc);
1796 for (i = 0; i < rdpg->rp_npages; i++)
1797 if (rdpg->rp_pages[i] != NULL)
1798 __free_page(rdpg->rp_pages[i]);
1799 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1801 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1807 static int mdt_reint_internal(struct mdt_thread_info *info,
1808 struct mdt_lock_handle *lhc,
1811 struct req_capsule *pill = info->mti_pill;
1812 struct mdt_body *repbody;
1817 rc = mdt_reint_unpack(info, op);
1819 CERROR("Can't unpack reint, rc %d\n", rc);
1820 RETURN(err_serious(rc));
1823 /* for replay (no_create) lmm is not needed, client has it already */
1824 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1825 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1828 /* llog cookies are always 0, the field is kept for compatibility */
1829 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1830 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1832 rc = req_capsule_server_pack(pill);
1834 CERROR("Can't pack response, rc %d\n", rc);
1835 RETURN(err_serious(rc));
1838 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1839 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1841 repbody->eadatasize = 0;
1842 repbody->aclsize = 0;
1845 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1847 /* for replay no cookkie / lmm need, because client have this already */
1848 if (info->mti_spec.no_create)
1849 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1850 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1852 rc = mdt_init_ucred_reint(info);
1854 GOTO(out_shrink, rc);
1856 rc = mdt_fix_attr_ucred(info, op);
1858 GOTO(out_ucred, rc = err_serious(rc));
1860 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1861 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1862 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1863 GOTO(out_ucred, rc);
1865 rc = mdt_reint_rec(info, lhc);
1868 mdt_exit_ucred(info);
1870 mdt_client_compatibility(info);
1871 rc2 = mdt_fix_reply(info);
1877 static long mdt_reint_opcode(struct ptlrpc_request *req,
1878 const struct req_format **fmt)
1880 struct mdt_device *mdt;
1881 struct mdt_rec_reint *rec;
1884 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1886 opc = rec->rr_opcode;
1887 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1888 if (opc < REINT_MAX && fmt[opc] != NULL)
1889 req_capsule_extend(&req->rq_pill, fmt[opc]);
1891 mdt = mdt_exp2dev(req->rq_export);
1892 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1893 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1894 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1895 opc = err_serious(-EFAULT);
1898 opc = err_serious(-EFAULT);
1903 static int mdt_reint(struct tgt_session_info *tsi)
1907 static const struct req_format *reint_fmts[REINT_MAX] = {
1908 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1909 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1910 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1911 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1912 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1913 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1914 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1915 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1916 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1921 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1923 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1925 * No lock possible here from client to pass it to reint code
1928 rc = mdt_reint_internal(info, NULL, opc);
1929 mdt_thread_info_fini(info);
1934 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1938 /* this should sync the whole device */
1939 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1941 struct dt_device *dt = mdt->mdt_bottom;
1945 rc = dt->dd_ops->dt_sync(env, dt);
1949 /* this should sync this object */
1950 static int mdt_object_sync(struct mdt_thread_info *info)
1952 struct md_object *next;
1956 if (!mdt_object_exists(info->mti_object)) {
1957 CWARN("Non existing object "DFID"!\n",
1958 PFID(mdt_object_fid(info->mti_object)));
1961 next = mdt_object_child(info->mti_object);
1962 rc = mo_object_sync(info->mti_env, next);
1967 static int mdt_sync(struct tgt_session_info *tsi)
1969 struct ptlrpc_request *req = tgt_ses_req(tsi);
1970 struct req_capsule *pill = tsi->tsi_pill;
1971 struct mdt_body *body;
1976 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1977 RETURN(err_serious(-ENOMEM));
1979 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1980 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1982 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1984 /* sync an object */
1985 rc = mdt_object_sync(info);
1987 const struct lu_fid *fid;
1988 struct lu_attr *la = &info->mti_attr.ma_attr;
1990 info->mti_attr.ma_need = MA_INODE;
1991 info->mti_attr.ma_valid = 0;
1992 rc = mdt_attr_get_complex(info, info->mti_object,
1995 body = req_capsule_server_get(pill,
1997 fid = mdt_object_fid(info->mti_object);
1998 mdt_pack_attr2body(info, body, la, fid);
2001 mdt_thread_info_fini(info);
2004 mdt_counter_incr(req, LPROC_MDT_SYNC);
2010 * Handle quota control requests to consult current usage/limit, but also
2011 * to configure quota enforcement
2013 static int mdt_quotactl(struct tgt_session_info *tsi)
2015 struct obd_export *exp = tsi->tsi_exp;
2016 struct req_capsule *pill = tsi->tsi_pill;
2017 struct obd_quotactl *oqctl, *repoqc;
2019 struct mdt_device *mdt = mdt_exp2dev(exp);
2020 struct lu_device *qmt = mdt->mdt_qmt_dev;
2023 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2025 RETURN(err_serious(-EPROTO));
2027 rc = req_capsule_server_pack(pill);
2029 RETURN(err_serious(rc));
2031 switch (oqctl->qc_cmd) {
2033 case LUSTRE_Q_INVALIDATE:
2034 case LUSTRE_Q_FINVALIDATE:
2038 /* deprecated, not used any more */
2039 RETURN(-EOPNOTSUPP);
2040 /* master quotactl */
2046 RETURN(-EOPNOTSUPP);
2047 /* slave quotactl */
2052 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2056 /* map uid/gid for remote client */
2058 if (exp_connect_rmtclient(exp)) {
2059 struct lustre_idmap_table *idmap;
2061 idmap = exp->exp_mdt_data.med_idmap;
2063 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2064 oqctl->qc_cmd != Q_GETINFO))
2067 if (oqctl->qc_type == USRQUOTA)
2068 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2070 else if (oqctl->qc_type == GRPQUOTA)
2071 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2076 if (id == CFS_IDMAP_NOTFOUND) {
2077 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2082 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2084 RETURN(err_serious(-EFAULT));
2086 if (oqctl->qc_id != id)
2087 swap(oqctl->qc_id, id);
2089 switch (oqctl->qc_cmd) {
2095 /* forward quotactl request to QMT */
2096 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2101 /* slave quotactl */
2102 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2107 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2111 if (oqctl->qc_id != id)
2112 swap(oqctl->qc_id, id);
2118 /** clone llog ctxt from child (mdd)
2119 * This allows remote llog (replicator) access.
2120 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2121 * context was originally set up, or we can handle them directly.
2122 * I choose the latter, but that means I need any llog
2123 * contexts set up by child to be accessable by the mdt. So we clone the
2124 * context into our context list here.
2126 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2129 struct md_device *next = mdt->mdt_child;
2130 struct llog_ctxt *ctxt;
2133 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2136 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2137 if (rc || ctxt == NULL) {
2141 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2143 CERROR("Can't set mdt ctxt %d\n", rc);
2148 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2149 struct mdt_device *mdt, int idx)
2151 struct llog_ctxt *ctxt;
2153 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2156 /* Put once for the get we just did, and once for the clone */
2157 llog_ctxt_put(ctxt);
2158 llog_ctxt_put(ctxt);
2163 * sec context handlers
2165 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2169 rc = mdt_handle_idmap(tsi);
2171 struct ptlrpc_request *req = tgt_ses_req(tsi);
2174 opc = lustre_msg_get_opc(req->rq_reqmsg);
2175 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2176 sptlrpc_svc_ctx_invalidate(req);
2179 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2185 * quota request handlers
2187 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2189 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2190 struct lu_device *qmt = mdt->mdt_qmt_dev;
2195 RETURN(err_serious(-EOPNOTSUPP));
2197 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2201 struct mdt_object *mdt_object_new(const struct lu_env *env,
2202 struct mdt_device *d,
2203 const struct lu_fid *f)
2205 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2206 struct lu_object *o;
2207 struct mdt_object *m;
2210 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2211 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2212 if (unlikely(IS_ERR(o)))
2213 m = (struct mdt_object *)o;
2219 struct mdt_object *mdt_object_find(const struct lu_env *env,
2220 struct mdt_device *d,
2221 const struct lu_fid *f)
2223 struct lu_object *o;
2224 struct mdt_object *m;
2227 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2228 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2229 if (unlikely(IS_ERR(o)))
2230 m = (struct mdt_object *)o;
2238 * Asyncronous commit for mdt device.
2240 * Pass asynchonous commit call down the MDS stack.
2242 * \param env environment
2243 * \param mdt the mdt device
2245 static void mdt_device_commit_async(const struct lu_env *env,
2246 struct mdt_device *mdt)
2248 struct dt_device *dt = mdt->mdt_bottom;
2251 rc = dt->dd_ops->dt_commit_async(env, dt);
2252 if (unlikely(rc != 0))
2253 CWARN("async commit start failed with rc = %d", rc);
2257 * Mark the lock as "synchonous".
2259 * Mark the lock to deffer transaction commit to the unlock time.
2261 * \param lock the lock to mark as "synchonous"
2263 * \see mdt_is_lock_sync
2264 * \see mdt_save_lock
2266 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2268 lock->l_ast_data = (void*)1;
2272 * Check whehter the lock "synchonous" or not.
2274 * \param lock the lock to check
2275 * \retval 1 the lock is "synchonous"
2276 * \retval 0 the lock isn't "synchronous"
2278 * \see mdt_set_lock_sync
2279 * \see mdt_save_lock
2281 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2283 return lock->l_ast_data != NULL;
2287 * Blocking AST for mdt locks.
2289 * Starts transaction commit if in case of COS lock conflict or
2290 * deffers such a commit to the mdt_save_lock.
2292 * \param lock the lock which blocks a request or cancelling lock
2293 * \param desc unused
2294 * \param data unused
2295 * \param flag indicates whether this cancelling or blocking callback
2297 * \see ldlm_blocking_ast_nocheck
2299 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2300 void *data, int flag)
2302 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2303 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2307 if (flag == LDLM_CB_CANCELING)
2309 lock_res_and_lock(lock);
2310 if (lock->l_blocking_ast != mdt_blocking_ast) {
2311 unlock_res_and_lock(lock);
2314 if (mdt_cos_is_enabled(mdt) &&
2315 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2316 lock->l_blocking_lock != NULL &&
2317 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2318 mdt_set_lock_sync(lock);
2320 rc = ldlm_blocking_ast_nocheck(lock);
2322 /* There is no lock conflict if l_blocking_lock == NULL,
2323 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2324 * when the last reference to a local lock was released */
2325 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2328 rc = lu_env_init(&env, LCT_LOCAL);
2329 if (unlikely(rc != 0))
2330 CWARN("lu_env initialization failed with rc = %d,"
2331 "cannot start asynchronous commit\n", rc);
2333 mdt_device_commit_async(&env, mdt);
2339 /* Used for cross-MDT lock */
2340 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2341 void *data, int flag)
2343 struct lustre_handle lockh;
2347 case LDLM_CB_BLOCKING:
2348 ldlm_lock2handle(lock, &lockh);
2349 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2351 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2355 case LDLM_CB_CANCELING:
2356 LDLM_DEBUG(lock, "Revoke remote lock\n");
2364 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2365 struct mdt_object *o, const struct lu_fid *fid,
2366 struct lustre_handle *lh, ldlm_mode_t mode,
2369 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2370 ldlm_policy_data_t *policy = &mti->mti_policy;
2371 struct ldlm_res_id *res_id = &mti->mti_res_id;
2375 LASSERT(mdt_object_remote(o));
2377 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2379 fid_build_reg_res_name(fid, res_id);
2381 memset(einfo, 0, sizeof(*einfo));
2382 einfo->ei_type = LDLM_IBITS;
2383 einfo->ei_mode = mode;
2384 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2385 einfo->ei_cb_cp = ldlm_completion_ast;
2386 einfo->ei_enq_slave = 0;
2387 einfo->ei_res_id = res_id;
2389 memset(policy, 0, sizeof(*policy));
2390 policy->l_inodebits.bits = ibits;
2392 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2397 static int mdt_object_local_lock(struct mdt_thread_info *info,
2398 struct mdt_object *o,
2399 struct mdt_lock_handle *lh, __u64 ibits,
2400 bool nonblock, int locality)
2402 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2403 ldlm_policy_data_t *policy = &info->mti_policy;
2404 struct ldlm_res_id *res_id = &info->mti_res_id;
2409 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2410 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2411 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2412 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2414 /* Only enqueue LOOKUP lock for remote object */
2415 if (mdt_object_remote(o))
2416 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2418 if (lh->mlh_type == MDT_PDO_LOCK) {
2419 /* check for exists after object is locked */
2420 if (mdt_object_exists(o) == 0) {
2421 /* Non-existent object shouldn't have PDO lock */
2424 /* Non-dir object shouldn't have PDO lock */
2425 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2430 memset(policy, 0, sizeof(*policy));
2431 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2433 dlmflags = LDLM_FL_ATOMIC_CB;
2435 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2438 * Take PDO lock on whole directory and build correct @res_id for lock
2439 * on part of directory.
2441 if (lh->mlh_pdo_hash != 0) {
2442 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2443 mdt_lock_pdo_mode(info, o, lh);
2444 if (lh->mlh_pdo_mode != LCK_NL) {
2446 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2447 * is never going to be sent to client and we do not
2448 * want it slowed down due to possible cancels.
2450 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2451 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2452 policy, res_id, dlmflags,
2453 info->mti_exp == NULL ? NULL :
2454 &info->mti_exp->exp_handle.h_cookie);
2460 * Finish res_id initializing by name hash marking part of
2461 * directory which is taking modification.
2463 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2466 policy->l_inodebits.bits = ibits;
2469 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2470 * going to be sent to client. If it is - mdt_intent_policy() path will
2471 * fix it up and turn FL_LOCAL flag off.
2473 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2474 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2475 info->mti_exp == NULL ? NULL :
2476 &info->mti_exp->exp_handle.h_cookie);
2478 mdt_object_unlock(info, o, lh, 1);
2479 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2480 lh->mlh_pdo_hash != 0 &&
2481 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2482 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2489 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2490 struct mdt_lock_handle *lh, __u64 ibits,
2491 bool nonblock, int locality)
2496 if (!mdt_object_remote(o))
2497 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2500 if (locality == MDT_LOCAL_LOCK) {
2501 CERROR("%s: try to get local lock for remote object"
2502 DFID".\n", mdt_obd_name(info->mti_mdt),
2503 PFID(mdt_object_fid(o)));
2507 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2508 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2509 MDS_INODELOCK_XATTR);
2510 if (ibits & MDS_INODELOCK_UPDATE) {
2511 /* Sigh, PDO needs to enqueue 2 locks right now, but
2512 * enqueue RPC can only request 1 lock, to avoid extra
2513 * RPC, so it will instead enqueue EX lock for remote
2514 * object anyway XXX*/
2515 if (lh->mlh_type == MDT_PDO_LOCK &&
2516 lh->mlh_pdo_hash != 0) {
2517 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2518 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2519 PFID(mdt_object_fid(o)));
2520 lh->mlh_pdo_hash = 0;
2521 lh->mlh_rreg_mode = LCK_EX;
2522 lh->mlh_type = MDT_REG_LOCK;
2524 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2527 MDS_INODELOCK_UPDATE);
2532 /* Only enqueue LOOKUP lock for remote object */
2533 if (ibits & MDS_INODELOCK_LOOKUP) {
2534 rc = mdt_object_local_lock(info, o, lh,
2535 MDS_INODELOCK_LOOKUP,
2536 nonblock, locality);
2544 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2545 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2547 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2550 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2551 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2553 struct mdt_lock_handle tmp = *lh;
2556 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2564 * Save a lock within request object.
2566 * Keep the lock referenced until whether client ACK or transaction
2567 * commit happens or release the lock immediately depending on input
2568 * parameters. If COS is ON, a write lock is converted to COS lock
2571 * \param info thead info object
2572 * \param h lock handle
2573 * \param mode lock mode
2574 * \param decref force immediate lock releasing
2577 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2578 ldlm_mode_t mode, int decref)
2582 if (lustre_handle_is_used(h)) {
2583 if (decref || !info->mti_has_trans ||
2584 !(mode & (LCK_PW | LCK_EX))){
2585 mdt_fid_unlock(h, mode);
2587 struct mdt_device *mdt = info->mti_mdt;
2588 struct ldlm_lock *lock = ldlm_handle2lock(h);
2589 struct ptlrpc_request *req = mdt_info_req(info);
2592 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2594 /* there is no request if mdt_object_unlock() is called
2595 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2596 if (likely(req != NULL)) {
2597 CDEBUG(D_HA, "request = %p reply state = %p"
2598 " transno = "LPD64"\n", req,
2599 req->rq_reply_state, req->rq_transno);
2600 if (mdt_cos_is_enabled(mdt)) {
2602 ldlm_lock_downgrade(lock, LCK_COS);
2605 ptlrpc_save_lock(req, h, mode, no_ack);
2607 ldlm_lock_decref(h, mode);
2609 if (mdt_is_lock_sync(lock)) {
2610 CDEBUG(D_HA, "found sync-lock,"
2611 " async commit started\n");
2612 mdt_device_commit_async(info->mti_env,
2615 LDLM_LOCK_PUT(lock);
2624 * Unlock mdt object.
2626 * Immeditely release the regular lock and the PDO lock or save the
2627 * lock in reqeuest and keep them referenced until client ACK or
2628 * transaction commit.
2630 * \param info thread info object
2631 * \param o mdt object
2632 * \param lh mdt lock handle referencing regular and PDO locks
2633 * \param decref force immediate lock releasing
2635 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2636 struct mdt_lock_handle *lh, int decref)
2640 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2641 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2643 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2644 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2649 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2650 const struct lu_fid *f,
2651 struct mdt_lock_handle *lh,
2654 struct mdt_object *o;
2656 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2660 rc = mdt_object_lock(info, o, lh, ibits,
2663 mdt_object_put(info->mti_env, o);
2670 void mdt_object_unlock_put(struct mdt_thread_info * info,
2671 struct mdt_object * o,
2672 struct mdt_lock_handle *lh,
2675 mdt_object_unlock(info, o, lh, decref);
2676 mdt_object_put(info->mti_env, o);
2680 * Generic code handling requests that have struct mdt_body passed in:
2682 * - extract mdt_body from request and save it in @info, if present;
2684 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2687 * - if HABEO_CORPUS flag is set for this request type check whether object
2688 * actually exists on storage (lu_object_exists()).
2691 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2693 const struct mdt_body *body;
2694 struct mdt_object *obj;
2695 const struct lu_env *env;
2696 struct req_capsule *pill;
2700 env = info->mti_env;
2701 pill = info->mti_pill;
2703 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2707 if (!(body->valid & OBD_MD_FLID))
2710 if (!fid_is_sane(&body->fid1)) {
2711 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2716 * Do not get size or any capa fields before we check that request
2717 * contains capa actually. There are some requests which do not, for
2718 * instance MDS_IS_SUBDIR.
2720 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2721 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2722 mdt_set_capainfo(info, 0, &body->fid1,
2723 req_capsule_client_get(pill, &RMF_CAPA1));
2725 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2727 if ((flags & HABEO_CORPUS) &&
2728 !mdt_object_exists(obj)) {
2729 mdt_object_put(env, obj);
2730 /* for capability renew ENOENT will be handled in
2732 if (body->valid & OBD_MD_FLOSSCAPA)
2737 info->mti_object = obj;
2746 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2748 struct req_capsule *pill = info->mti_pill;
2752 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2753 rc = mdt_body_unpack(info, flags);
2757 if (rc == 0 && (flags & HABEO_REFERO)) {
2759 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2760 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2762 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2763 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2766 rc = req_capsule_server_pack(pill);
2771 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2773 struct md_device *next = m->mdt_child;
2775 return next->md_ops->mdo_init_capa_ctxt(env, next,
2776 m->mdt_lut.lut_mds_capa,
2777 m->mdt_capa_timeout,
2782 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2784 lh->mlh_type = MDT_NUL_LOCK;
2785 lh->mlh_reg_lh.cookie = 0ull;
2786 lh->mlh_reg_mode = LCK_MINMODE;
2787 lh->mlh_pdo_lh.cookie = 0ull;
2788 lh->mlh_pdo_mode = LCK_MINMODE;
2789 lh->mlh_rreg_lh.cookie = 0ull;
2790 lh->mlh_rreg_mode = LCK_MINMODE;
2793 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2795 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2796 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2800 * Initialize fields of struct mdt_thread_info. Other fields are left in
2801 * uninitialized state, because it's too expensive to zero out whole
2802 * mdt_thread_info (> 1K) on each request arrival.
2804 void mdt_thread_info_init(struct ptlrpc_request *req,
2805 struct mdt_thread_info *info)
2809 info->mti_pill = &req->rq_pill;
2812 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2813 mdt_lock_handle_init(&info->mti_lh[i]);
2815 /* mdt device: it can be NULL while CONNECT */
2816 if (req->rq_export) {
2817 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2818 info->mti_exp = req->rq_export;
2820 info->mti_mdt = NULL;
2821 info->mti_env = req->rq_svc_thread->t_env;
2822 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2824 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2825 info->mti_big_buf = LU_BUF_NULL;
2826 info->mti_body = NULL;
2827 info->mti_object = NULL;
2828 info->mti_dlm_req = NULL;
2829 info->mti_has_trans = 0;
2830 info->mti_cross_ref = 0;
2831 info->mti_opdata = 0;
2832 info->mti_big_lmm_used = 0;
2834 info->mti_spec.no_create = 0;
2835 info->mti_spec.sp_rm_entry = 0;
2837 info->mti_spec.u.sp_ea.eadata = NULL;
2838 info->mti_spec.u.sp_ea.eadatalen = 0;
2841 void mdt_thread_info_fini(struct mdt_thread_info *info)
2845 if (info->mti_object != NULL) {
2846 mdt_object_put(info->mti_env, info->mti_object);
2847 info->mti_object = NULL;
2850 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2851 mdt_lock_handle_fini(&info->mti_lh[i]);
2852 info->mti_env = NULL;
2853 info->mti_pill = NULL;
2854 info->mti_exp = NULL;
2856 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2857 lu_buf_free(&info->mti_big_buf);
2860 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2862 struct mdt_thread_info *mti;
2863 struct lustre_capa *lc;
2865 mti = mdt_th_info(tsi->tsi_env);
2866 LASSERT(mti != NULL);
2868 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2869 if (tsi->tsi_corpus != NULL) {
2870 struct req_capsule *pill = tsi->tsi_pill;
2872 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2873 lu_object_get(tsi->tsi_corpus);
2876 * XXX: must be part of tgt_mdt_body_unpack but moved here
2877 * due to mdt_set_capainfo().
2879 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2880 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2881 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2882 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2885 mti->mti_body = tsi->tsi_mdt_body;
2886 mti->mti_dlm_req = tsi->tsi_dlm_req;
2891 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2893 struct ptlrpc_request *req = tgt_ses_req(tsi);
2898 rc = tgt_connect(tsi);
2902 rc = mdt_init_idmap(tsi);
2907 obd_disconnect(class_export_get(req->rq_export));
2926 static int mdt_intent_getattr(enum mdt_it_code opcode,
2927 struct mdt_thread_info *info,
2928 struct ldlm_lock **,
2931 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2932 struct mdt_thread_info *info,
2933 struct ldlm_lock **lockp,
2936 static int mdt_intent_layout(enum mdt_it_code opcode,
2937 struct mdt_thread_info *info,
2938 struct ldlm_lock **,
2940 static int mdt_intent_reint(enum mdt_it_code opcode,
2941 struct mdt_thread_info *info,
2942 struct ldlm_lock **,
2945 static struct mdt_it_flavor {
2946 const struct req_format *it_fmt;
2948 int (*it_act)(enum mdt_it_code ,
2949 struct mdt_thread_info *,
2950 struct ldlm_lock **,
2953 } mdt_it_flavor[] = {
2955 .it_fmt = &RQF_LDLM_INTENT,
2956 /*.it_flags = HABEO_REFERO,*/
2958 .it_act = mdt_intent_reint,
2959 .it_reint = REINT_OPEN
2962 .it_fmt = &RQF_LDLM_INTENT,
2964 * OCREAT is not a MUTABOR request as if the file
2966 * We do the extra check of OBD_CONNECT_RDONLY in
2967 * mdt_reint_open() when we really need to create
2971 .it_act = mdt_intent_reint,
2972 .it_reint = REINT_OPEN
2975 .it_fmt = &RQF_LDLM_INTENT,
2976 .it_flags = MUTABOR,
2977 .it_act = mdt_intent_reint,
2978 .it_reint = REINT_CREATE
2980 [MDT_IT_GETATTR] = {
2981 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2982 .it_flags = HABEO_REFERO,
2983 .it_act = mdt_intent_getattr
2985 [MDT_IT_READDIR] = {
2991 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2992 .it_flags = HABEO_REFERO,
2993 .it_act = mdt_intent_getattr
2996 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2997 .it_flags = MUTABOR,
2999 .it_reint = REINT_UNLINK
3003 .it_flags = MUTABOR,
3006 [MDT_IT_GETXATTR] = {
3007 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3008 .it_flags = HABEO_CORPUS,
3009 .it_act = mdt_intent_getxattr
3012 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3014 .it_act = mdt_intent_layout
3019 mdt_intent_lock_replace(struct mdt_thread_info *info, struct ldlm_lock **lockp,
3020 struct ldlm_lock *new_lock, struct mdt_lock_handle *lh,
3023 struct ptlrpc_request *req = mdt_info_req(info);
3024 struct ldlm_lock *lock = *lockp;
3027 * Get new lock only for cases when possible resent did not find any
3030 if (new_lock == NULL)
3031 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3033 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3034 lh->mlh_reg_lh.cookie = 0;
3038 LASSERTF(new_lock != NULL,
3039 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3042 * If we've already given this lock to a client once, then we should
3043 * have no readers or writers. Otherwise, we should have one reader
3044 * _or_ writer ref (which will be zeroed below) before returning the
3047 if (new_lock->l_export == req->rq_export) {
3048 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3050 LASSERT(new_lock->l_export == NULL);
3051 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3056 if (new_lock->l_export == req->rq_export) {
3058 * Already gave this to the client, which means that we
3059 * reconstructed a reply.
3061 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3063 lh->mlh_reg_lh.cookie = 0;
3064 RETURN(ELDLM_LOCK_REPLACED);
3068 * Fixup the lock to be given to the client.
3070 lock_res_and_lock(new_lock);
3071 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3072 * possible blocking AST. */
3073 while (new_lock->l_readers > 0) {
3074 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3075 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3076 new_lock->l_readers--;
3078 while (new_lock->l_writers > 0) {
3079 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3080 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3081 new_lock->l_writers--;
3084 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3085 new_lock->l_blocking_ast = lock->l_blocking_ast;
3086 new_lock->l_completion_ast = lock->l_completion_ast;
3087 new_lock->l_remote_handle = lock->l_remote_handle;
3088 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3090 unlock_res_and_lock(new_lock);
3092 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3093 &new_lock->l_remote_handle,
3094 &new_lock->l_exp_hash);
3096 LDLM_LOCK_RELEASE(new_lock);
3097 lh->mlh_reg_lh.cookie = 0;
3099 RETURN(ELDLM_LOCK_REPLACED);
3102 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3103 struct ldlm_lock *new_lock,
3104 struct ldlm_lock **old_lock,
3105 struct mdt_lock_handle *lh,
3106 enum mdt_it_code opcode)
3108 struct ptlrpc_request *req = mdt_info_req(info);
3109 struct obd_export *exp = req->rq_export;
3110 struct lustre_handle remote_hdl;
3111 struct ldlm_request *dlmreq;
3112 struct ldlm_lock *lock;
3114 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3117 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3118 remote_hdl = dlmreq->lock_handle[0];
3119 /* If the client does not require open lock, it does not need to
3120 * search lock in exp_lock_hash, since the server thread will
3121 * make sure the lock will be released, and the resend request
3122 * can always re-enqueue the lock */
3123 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3124 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3125 /* In the function below, .hs_keycmp resolves to
3126 * ldlm_export_lock_keycmp() */
3127 /* coverity[overrun-buffer-val] */
3128 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3130 lock_res_and_lock(lock);
3131 if (lock != new_lock) {
3132 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3133 lh->mlh_reg_mode = lock->l_granted_mode;
3135 LDLM_DEBUG(lock, "Restoring lock cookie");
3136 DEBUG_REQ(D_DLMTRACE, req,
3137 "restoring lock cookie "LPX64,
3138 lh->mlh_reg_lh.cookie);
3140 *old_lock = LDLM_LOCK_GET(lock);
3141 cfs_hash_put(exp->exp_lock_hash,
3143 unlock_res_and_lock(lock);
3146 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3147 unlock_res_and_lock(lock);
3151 * If the xid matches, then we know this is a resent request, and allow
3152 * it. (It's probably an OPEN, for which we don't send a lock.
3154 if (req_xid_is_last(req))
3158 * This remote handle isn't enqueued, so we never received or processed
3159 * this request. Clear MSG_RESENT, because it can be handled like any
3160 * normal request now.
3162 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3164 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3168 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3169 struct mdt_thread_info *info,
3170 struct ldlm_lock **lockp,
3173 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3174 struct ldlm_reply *ldlm_rep = NULL;
3178 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3179 * (for the resend case) or a new lock. Below we will use it to
3180 * replace the original lock.
3182 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3183 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3184 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3185 rc = mdt_object_lock(info, info->mti_object, lhc,
3186 MDS_INODELOCK_XATTR,
3192 grc = mdt_getxattr(info);
3194 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3196 if (mdt_info_req(info)->rq_repmsg != NULL)
3197 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3198 if (ldlm_rep == NULL)
3199 RETURN(err_serious(-EFAULT));
3201 ldlm_rep->lock_policy_res2 = grc;
3206 static int mdt_intent_getattr(enum mdt_it_code opcode,
3207 struct mdt_thread_info *info,
3208 struct ldlm_lock **lockp,
3211 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3212 struct ldlm_lock *new_lock = NULL;
3214 struct ldlm_reply *ldlm_rep;
3215 struct mdt_body *reqbody;
3216 struct mdt_body *repbody;
3220 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3223 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3226 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3227 repbody->eadatasize = 0;
3228 repbody->aclsize = 0;
3232 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3234 case MDT_IT_GETATTR:
3235 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3239 CERROR("Unsupported intent (%d)\n", opcode);
3240 GOTO(out_shrink, rc = -EINVAL);
3243 rc = mdt_init_ucred(info, reqbody);
3245 GOTO(out_shrink, rc);
3247 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3248 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3250 /* Get lock from request for possible resent case. */
3251 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3253 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3254 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3256 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3257 ldlm_rep->lock_policy_res2 = 0;
3258 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3259 ldlm_rep->lock_policy_res2) {
3260 lhc->mlh_reg_lh.cookie = 0ull;
3261 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3264 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3267 mdt_exit_ucred(info);
3269 mdt_client_compatibility(info);
3270 rc2 = mdt_fix_reply(info);
3276 static int mdt_intent_layout(enum mdt_it_code opcode,
3277 struct mdt_thread_info *info,
3278 struct ldlm_lock **lockp,
3281 struct layout_intent *layout;
3283 struct mdt_object *obj = NULL;
3287 if (opcode != MDT_IT_LAYOUT) {
3288 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3293 fid = &info->mti_tmp_fid2;
3294 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3296 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3298 RETURN(PTR_ERR(obj));
3300 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3301 /* get the length of lsm */
3302 rc = mdt_attr_get_eabuf_size(info, obj);
3306 if (rc > info->mti_mdt->mdt_max_mdsize)
3307 info->mti_mdt->mdt_max_mdsize = rc;
3310 mdt_object_put(info->mti_env, obj);
3312 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3313 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
3314 rc = req_capsule_server_pack(info->mti_pill);
3318 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3319 LASSERT(layout != NULL);
3320 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3321 /* return to normal ldlm handling */
3324 CERROR("%s: Unsupported layout intent (%d)\n",
3325 mdt_obd_name(info->mti_mdt), layout->li_opc);
3329 static int mdt_intent_reint(enum mdt_it_code opcode,
3330 struct mdt_thread_info *info,
3331 struct ldlm_lock **lockp,
3334 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3335 struct ldlm_reply *rep = NULL;
3339 static const struct req_format *intent_fmts[REINT_MAX] = {
3340 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3341 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3346 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3350 if (mdt_it_flavor[opcode].it_reint != opc) {
3351 CERROR("Reint code %ld doesn't match intent: %d\n",
3353 RETURN(err_serious(-EPROTO));
3356 /* Get lock from request for possible resent case. */
3357 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3359 rc = mdt_reint_internal(info, lhc, opc);
3361 /* Check whether the reply has been packed successfully. */
3362 if (mdt_info_req(info)->rq_repmsg != NULL)
3363 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3365 RETURN(err_serious(-EFAULT));
3367 /* MDC expects this in any case */
3369 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3371 /* the open lock or the lock for cross-ref object should be
3372 * returned to the client */
3373 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3374 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3375 rep->lock_policy_res2 = 0;
3376 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3380 rep->lock_policy_res2 = clear_serious(rc);
3382 if (rep->lock_policy_res2 == -ENOENT &&
3383 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3384 rep->lock_policy_res2 = 0;
3386 if (rc == -ENOTCONN || rc == -ENODEV ||
3387 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3389 * If it is the disconnect error (ENODEV & ENOCONN), the error
3390 * will be returned by rq_status, and client at ptlrpc layer
3391 * will detect this, then disconnect, reconnect the import
3392 * immediately, instead of impacting the following the rpc.
3394 lhc->mlh_reg_lh.cookie = 0ull;
3398 * For other cases, the error will be returned by intent.
3399 * and client will retrieve the result from intent.
3402 * FIXME: when open lock is finished, that should be
3405 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3406 LASSERTF(rc == 0, "Error occurred but lock handle "
3407 "is still in use, rc = %d\n", rc);
3408 rep->lock_policy_res2 = 0;
3409 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3412 lhc->mlh_reg_lh.cookie = 0ull;
3413 RETURN(ELDLM_LOCK_ABORTED);
3418 static int mdt_intent_code(long itcode)
3426 case IT_OPEN|IT_CREAT:
3433 rc = MDT_IT_READDIR;
3436 rc = MDT_IT_GETATTR;
3448 rc = MDT_IT_GETXATTR;
3453 case IT_QUOTA_DQACQ:
3458 CERROR("Unknown intent opcode: %ld\n", itcode);
3465 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3466 struct ldlm_lock **lockp, __u64 flags)
3468 struct req_capsule *pill;
3469 struct mdt_it_flavor *flv;
3474 opc = mdt_intent_code(itopc);
3478 pill = info->mti_pill;
3480 if (opc == MDT_IT_QUOTA) {
3481 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3484 RETURN(-EOPNOTSUPP);
3486 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3487 /* pass the request to quota master */
3488 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3489 mdt_info_req(info), lockp,
3494 flv = &mdt_it_flavor[opc];
3495 if (flv->it_fmt != NULL)
3496 req_capsule_extend(pill, flv->it_fmt);
3498 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3500 struct ptlrpc_request *req = mdt_info_req(info);
3501 if (flv->it_flags & MUTABOR &&
3502 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3505 if (rc == 0 && flv->it_act != NULL) {
3506 struct ldlm_reply *rep;
3508 /* execute policy */
3509 rc = flv->it_act(opc, info, lockp, flags);
3511 /* Check whether the reply has been packed successfully. */
3512 if (mdt_info_req(info)->rq_repmsg != NULL) {
3513 rep = req_capsule_server_get(info->mti_pill,
3515 rep->lock_policy_res2 =
3516 ptlrpc_status_hton(rep->lock_policy_res2);
3524 static int mdt_intent_policy(struct ldlm_namespace *ns,
3525 struct ldlm_lock **lockp, void *req_cookie,
3526 ldlm_mode_t mode, __u64 flags, void *data)
3528 struct tgt_session_info *tsi;
3529 struct mdt_thread_info *info;
3530 struct ptlrpc_request *req = req_cookie;
3531 struct ldlm_intent *it;
3532 struct req_capsule *pill;
3537 LASSERT(req != NULL);
3539 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3541 info = tsi2mdt_info(tsi);
3542 LASSERT(info != NULL);
3543 pill = info->mti_pill;
3544 LASSERT(pill->rc_req == req);
3546 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3547 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3548 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3550 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3554 /* Lock without inodebits makes no sense and will oops
3555 * later in ldlm. Let's check it now to see if we have
3556 * ibits corrupted somewhere in mdt_intent_opc().
3557 * The case for client miss to set ibits has been
3558 * processed by others. */
3559 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3560 lr_type == LDLM_IBITS,
3561 info->mti_dlm_req->lock_desc.\
3562 l_policy_data.l_inodebits.bits != 0));
3564 rc = err_serious(-EFAULT);
3566 /* No intent was provided */
3567 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3568 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3569 rc = req_capsule_server_pack(pill);
3571 rc = err_serious(rc);
3573 mdt_thread_info_fini(info);
3577 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3579 struct seq_server_site *ss = mdt_seq_site(mdt);
3581 if (ss->ss_node_id == 0)
3584 if (ss->ss_client_seq != NULL) {
3585 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3586 ss->ss_client_seq->lcs_exp = NULL;
3589 if (ss->ss_server_fld != NULL) {
3590 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3591 ss->ss_server_fld->lsf_control_exp = NULL;
3595 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3597 struct seq_server_site *ss = mdt_seq_site(mdt);
3602 if (ss->ss_server_seq != NULL)
3603 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3606 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3608 mdt_seq_fini_cli(mdt);
3609 mdt_deregister_seq_exp(mdt);
3611 return seq_site_fini(env, mdt_seq_site(mdt));
3615 * It will retrieve its FLDB entries from MDT0, and it only happens
3616 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3617 * and it needs to refresh FLDB from the MDT0.
3619 static int mdt_register_lwp_callback(void *data)
3622 struct mdt_device *mdt = data;
3623 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3627 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3629 if (!likely(fld->lsf_new))
3632 rc = lu_env_init(&env, LCT_MD_THREAD);
3634 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3638 rc = fld_update_from_controller(&env, fld);
3640 CERROR("%s: cannot update controller: rc = %d\n",
3641 mdt_obd_name(mdt), rc);
3649 static int mdt_register_seq_exp(struct mdt_device *mdt)
3651 struct seq_server_site *ss = mdt_seq_site(mdt);
3652 char *lwp_name = NULL;
3655 if (ss->ss_node_id == 0)
3658 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3659 if (lwp_name == NULL)
3660 GOTO(out_free, rc = -ENOMEM);
3662 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3666 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3671 rc = lustre_register_lwp_item(lwp_name,
3672 &ss->ss_server_fld->lsf_control_exp,
3673 mdt_register_lwp_callback, mdt);
3675 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3676 ss->ss_client_seq->lcs_exp = NULL;
3680 if (lwp_name != NULL)
3681 OBD_FREE(lwp_name, MAX_OBD_NAME);
3687 * Init client sequence manager which is used by local MDS to talk to sequence
3688 * controller on remote node.
3690 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3692 struct seq_server_site *ss = mdt_seq_site(mdt);
3697 /* check if this is adding the first MDC and controller is not yet
3699 OBD_ALLOC_PTR(ss->ss_client_seq);
3700 if (ss->ss_client_seq == NULL)
3703 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3704 if (prefix == NULL) {
3705 OBD_FREE_PTR(ss->ss_client_seq);
3706 ss->ss_client_seq = NULL;
3710 /* Note: seq_client_fini will be called in seq_site_fini */
3711 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3712 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3713 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3715 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3717 OBD_FREE_PTR(ss->ss_client_seq);
3718 ss->ss_client_seq = NULL;
3722 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3727 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3729 struct seq_server_site *ss;
3733 ss = mdt_seq_site(mdt);
3734 /* init sequence controller server(MDT0) */
3735 if (ss->ss_node_id == 0) {
3736 OBD_ALLOC_PTR(ss->ss_control_seq);
3737 if (ss->ss_control_seq == NULL)
3740 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3741 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3744 GOTO(out_seq_fini, rc);
3747 /* Init normal sequence server */
3748 OBD_ALLOC_PTR(ss->ss_server_seq);
3749 if (ss->ss_server_seq == NULL)
3750 GOTO(out_seq_fini, rc = -ENOMEM);
3752 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3753 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3755 GOTO(out_seq_fini, rc);
3757 /* init seq client for seq server to talk to seq controller(MDT0) */
3758 rc = mdt_seq_init_cli(env, mdt);
3760 GOTO(out_seq_fini, rc);
3762 if (ss->ss_node_id != 0)
3763 /* register controler export through lwp */
3764 rc = mdt_register_seq_exp(mdt);
3769 mdt_seq_fini(env, mdt);
3777 static int mdt_fld_fini(const struct lu_env *env,
3778 struct mdt_device *m)
3780 struct seq_server_site *ss = mdt_seq_site(m);
3783 if (ss && ss->ss_server_fld) {
3784 fld_server_fini(env, ss->ss_server_fld);
3785 OBD_FREE_PTR(ss->ss_server_fld);
3786 ss->ss_server_fld = NULL;
3792 static int mdt_fld_init(const struct lu_env *env,
3794 struct mdt_device *m)
3796 struct seq_server_site *ss;
3800 ss = mdt_seq_site(m);
3802 OBD_ALLOC_PTR(ss->ss_server_fld);
3803 if (ss->ss_server_fld == NULL)
3804 RETURN(rc = -ENOMEM);
3806 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3809 OBD_FREE_PTR(ss->ss_server_fld);
3810 ss->ss_server_fld = NULL;
3817 static void mdt_stack_pre_fini(const struct lu_env *env,
3818 struct mdt_device *m, struct lu_device *top)
3820 struct lustre_cfg_bufs *bufs;
3821 struct lustre_cfg *lcfg;
3822 struct mdt_thread_info *info;
3827 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3828 LASSERT(info != NULL);
3830 bufs = &info->mti_u.bufs;
3832 LASSERT(m->mdt_child_exp);
3833 LASSERT(m->mdt_child_exp->exp_obd);
3835 /* process cleanup, pass mdt obd name to get obd umount flags */
3836 /* XXX: this is needed because all layers are referenced by
3837 * objects (some of them are pinned by osd, for example *
3838 * the proper solution should be a model where object used
3839 * by osd only doesn't have mdt/mdd slices -bzzz */
3840 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3841 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3842 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3844 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3847 top->ld_ops->ldo_process_config(env, top, lcfg);
3848 lustre_cfg_free(lcfg);
3852 static void mdt_stack_fini(const struct lu_env *env,
3853 struct mdt_device *m, struct lu_device *top)
3855 struct obd_device *obd = mdt2obd_dev(m);
3856 struct lustre_cfg_bufs *bufs;
3857 struct lustre_cfg *lcfg;
3858 struct mdt_thread_info *info;
3862 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3863 LASSERT(info != NULL);
3865 lu_dev_del_linkage(top->ld_site, top);
3867 lu_site_purge(env, top->ld_site, -1);
3869 bufs = &info->mti_u.bufs;
3870 /* process cleanup, pass mdt obd name to get obd umount flags */
3871 /* another purpose is to let all layers to release their objects */
3872 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3877 lustre_cfg_bufs_set_string(bufs, 1, flags);
3878 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3880 CERROR("Cannot alloc lcfg!\n");
3884 top->ld_ops->ldo_process_config(env, top, lcfg);
3885 lustre_cfg_free(lcfg);
3887 lu_site_purge(env, top->ld_site, -1);
3889 m->mdt_child = NULL;
3890 m->mdt_bottom = NULL;
3892 obd_disconnect(m->mdt_child_exp);
3893 m->mdt_child_exp = NULL;
3895 obd_disconnect(m->mdt_bottom_exp);
3896 m->mdt_child_exp = NULL;
3899 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3900 const char *next, struct obd_export **exp)
3902 struct obd_connect_data *data = NULL;
3903 struct obd_device *obd;
3907 OBD_ALLOC_PTR(data);
3909 GOTO(out, rc = -ENOMEM);
3911 obd = class_name2obd(next);
3913 CERROR("%s: can't locate next device: %s\n",
3914 mdt_obd_name(m), next);
3915 GOTO(out, rc = -ENOTCONN);
3918 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3919 data->ocd_version = LUSTRE_VERSION_CODE;
3921 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3923 CERROR("%s: cannot connect to next dev %s (%d)\n",
3924 mdt_obd_name(m), next, rc);
3934 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3935 struct lustre_cfg *cfg)
3937 char *dev = lustre_cfg_string(cfg, 0);
3938 int rc, name_size, uuid_size;
3939 char *name, *uuid, *p;
3940 struct lustre_cfg_bufs *bufs;
3941 struct lustre_cfg *lcfg;
3942 struct obd_device *obd;
3943 struct lustre_profile *lprof;
3944 struct lu_site *site;
3947 /* in 1.8 we had the only device in the stack - MDS.
3948 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3949 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3950 * to generate names and setup MDT, MDD. MDT will be using
3951 * generated name to connect to MDD. for MDD the next device
3952 * will be LOD with name taken from so called "profile" which
3953 * is generated by mount_option line
3955 * 1.8 MGS generates config. commands like this:
3956 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3957 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3958 * 2.0 MGS generates config. commands like this:
3959 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3960 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3961 * 3:lustre-MDT0000-mdtlov 4:f
3963 * we generate MDD name from MDT one, just replacing T with D
3965 * after all the preparations, the logical equivalent will be
3966 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3967 * 3:lustre-MDT0000-mdtlov 4:f
3968 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3969 * 3:lustre-MDD0000 4:f
3971 * notice we build the stack from down to top: MDD first, then MDT */
3973 name_size = MAX_OBD_NAME;
3974 uuid_size = MAX_OBD_NAME;
3976 OBD_ALLOC(name, name_size);
3977 OBD_ALLOC(uuid, uuid_size);
3978 if (name == NULL || uuid == NULL)
3979 GOTO(cleanup_mem, rc = -ENOMEM);
3981 OBD_ALLOC_PTR(bufs);
3983 GOTO(cleanup_mem, rc = -ENOMEM);
3986 p = strstr(name, "-MDT");
3988 GOTO(free_bufs, rc = -ENOMEM);
3991 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3993 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3994 if (lprof == NULL || lprof->lp_dt == NULL) {
3995 CERROR("can't find the profile: %s\n",
3996 lustre_cfg_string(cfg, 0));
3997 GOTO(free_bufs, rc = -EINVAL);
4000 lustre_cfg_bufs_reset(bufs, name);
4001 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4002 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4003 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4005 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4007 GOTO(free_bufs, rc = -ENOMEM);
4009 rc = class_attach(lcfg);
4011 GOTO(lcfg_cleanup, rc);
4013 obd = class_name2obd(name);
4015 CERROR("Can not find obd %s (%s in config)\n",
4016 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4017 GOTO(class_detach, rc = -EINVAL);
4020 lustre_cfg_free(lcfg);
4022 lustre_cfg_bufs_reset(bufs, name);
4023 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4024 lustre_cfg_bufs_set_string(bufs, 2, dev);
4025 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4027 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4029 rc = class_setup(obd, lcfg);
4031 GOTO(class_detach, rc);
4033 /* connect to MDD we just setup */
4034 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4036 GOTO(class_detach, rc);
4038 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4040 LASSERT(mdt_lu_site(mdt) == NULL);
4041 mdt->mdt_lu_dev.ld_site = site;
4042 site->ls_top_dev = &mdt->mdt_lu_dev;
4043 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4045 /* now connect to bottom OSD */
4046 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4047 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4049 GOTO(class_detach, rc);
4051 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4053 rc = lu_env_refill((struct lu_env *)env);
4055 CERROR("Failure to refill session: '%d'\n", rc);
4057 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4062 class_detach(obd, lcfg);
4064 lustre_cfg_free(lcfg);
4069 OBD_FREE(name, name_size);
4071 OBD_FREE(uuid, uuid_size);
4075 /* setup quota master target on MDT0 */
4076 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4077 struct lustre_cfg *cfg)
4079 struct obd_device *obd;
4080 char *dev = lustre_cfg_string(cfg, 0);
4081 char *qmtname, *uuid, *p;
4082 struct lustre_cfg_bufs *bufs;
4083 struct lustre_cfg *lcfg;
4084 struct lustre_profile *lprof;
4085 struct obd_connect_data *data;
4089 LASSERT(mdt->mdt_qmt_exp == NULL);
4090 LASSERT(mdt->mdt_qmt_dev == NULL);
4092 /* quota master is on MDT0 only for now */
4093 if (mdt->mdt_seq_site.ss_node_id != 0)
4096 /* MGS generates config commands which look as follows:
4097 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4098 * 3:lustre-MDT0000-mdtlov 4:f
4100 * We generate the QMT name from the MDT one, just replacing MD with QM
4101 * after all the preparations, the logical equivalent will be:
4102 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4103 * 3:lustre-MDT0000-osd 4:f */
4104 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4105 OBD_ALLOC(uuid, UUID_MAX);
4106 OBD_ALLOC_PTR(bufs);
4107 OBD_ALLOC_PTR(data);
4108 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4109 GOTO(cleanup_mem, rc = -ENOMEM);
4111 strcpy(qmtname, dev);
4112 p = strstr(qmtname, "-MDT");
4114 GOTO(cleanup_mem, rc = -ENOMEM);
4115 /* replace MD with QM */
4119 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4121 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4122 if (lprof == NULL || lprof->lp_dt == NULL) {
4123 CERROR("can't find profile for %s\n",
4124 lustre_cfg_string(cfg, 0));
4125 GOTO(cleanup_mem, rc = -EINVAL);
4128 lustre_cfg_bufs_reset(bufs, qmtname);
4129 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4130 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4131 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4133 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4135 GOTO(cleanup_mem, rc = -ENOMEM);
4137 rc = class_attach(lcfg);
4139 GOTO(lcfg_cleanup, rc);
4141 obd = class_name2obd(qmtname);
4143 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4144 lustre_cfg_string(cfg, 0));
4145 GOTO(class_detach, rc = -EINVAL);
4148 lustre_cfg_free(lcfg);
4150 lustre_cfg_bufs_reset(bufs, qmtname);
4151 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4152 lustre_cfg_bufs_set_string(bufs, 2, dev);
4154 /* for quota, the next device should be the OSD device */
4155 lustre_cfg_bufs_set_string(bufs, 3,
4156 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4158 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4160 rc = class_setup(obd, lcfg);
4162 GOTO(class_detach, rc);
4164 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4166 /* configure local quota objects */
4167 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4171 GOTO(class_cleanup, rc);
4173 /* connect to quota master target */
4174 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4175 data->ocd_version = LUSTRE_VERSION_CODE;
4176 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4179 CERROR("cannot connect to quota master device %s (%d)\n",
4181 GOTO(class_cleanup, rc);
4187 class_manual_cleanup(obd);
4188 mdt->mdt_qmt_dev = NULL;
4192 class_detach(obd, lcfg);
4194 lustre_cfg_free(lcfg);
4199 OBD_FREE(qmtname, MAX_OBD_NAME);
4201 OBD_FREE(uuid, UUID_MAX);
4207 /* Shutdown quota master target associated with mdt */
4208 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4212 if (mdt->mdt_qmt_exp == NULL)
4214 LASSERT(mdt->mdt_qmt_dev != NULL);
4216 /* the qmt automatically shuts down when the mdt disconnects */
4217 obd_disconnect(mdt->mdt_qmt_exp);
4218 mdt->mdt_qmt_exp = NULL;
4219 mdt->mdt_qmt_dev = NULL;
4223 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4224 * for now. This will be removed along with converting rest of MDT code
4225 * to use tgt_session_info */
4226 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4228 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4231 rc = mdt_getxattr(info);
4233 mdt_thread_info_fini(info);
4237 static struct tgt_handler mdt_tgt_handlers[] = {
4238 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4239 0, MDS_CONNECT, mdt_tgt_connect,
4240 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4241 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4242 0, MDS_DISCONNECT, tgt_disconnect,
4243 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4244 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4245 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4246 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4247 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4248 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4249 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4250 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4252 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4253 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4254 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4255 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4256 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4258 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4259 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4260 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4261 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4262 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4264 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4265 mdt_hsm_ct_register),
4266 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4267 mdt_hsm_ct_unregister),
4268 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4270 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4272 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4273 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4275 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4280 static struct tgt_handler mdt_sec_ctx_ops[] = {
4281 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4282 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4283 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4286 static struct tgt_handler mdt_quota_ops[] = {
4287 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4290 static struct tgt_opc_slice mdt_common_slice[] = {
4292 .tos_opc_start = MDS_FIRST_OPC,
4293 .tos_opc_end = MDS_LAST_OPC,
4294 .tos_hs = mdt_tgt_handlers
4297 .tos_opc_start = OBD_FIRST_OPC,
4298 .tos_opc_end = OBD_LAST_OPC,
4299 .tos_hs = tgt_obd_handlers
4302 .tos_opc_start = LDLM_FIRST_OPC,
4303 .tos_opc_end = LDLM_LAST_OPC,
4304 .tos_hs = tgt_dlm_handlers
4307 .tos_opc_start = SEC_FIRST_OPC,
4308 .tos_opc_end = SEC_LAST_OPC,
4309 .tos_hs = mdt_sec_ctx_ops
4312 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4313 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4314 .tos_hs = tgt_out_handlers
4317 .tos_opc_start = FLD_FIRST_OPC,
4318 .tos_opc_end = FLD_LAST_OPC,
4319 .tos_hs = fld_handlers
4322 .tos_opc_start = SEQ_FIRST_OPC,
4323 .tos_opc_end = SEQ_LAST_OPC,
4324 .tos_hs = seq_handlers
4327 .tos_opc_start = QUOTA_DQACQ,
4328 .tos_opc_end = QUOTA_LAST_OPC,
4329 .tos_hs = mdt_quota_ops
4332 .tos_opc_start = LLOG_FIRST_OPC,
4333 .tos_opc_end = LLOG_LAST_OPC,
4334 .tos_hs = tgt_llog_handlers
4337 .tos_opc_start = LFSCK_FIRST_OPC,
4338 .tos_opc_end = LFSCK_LAST_OPC,
4339 .tos_hs = tgt_lfsck_handlers
4347 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4349 struct md_device *next = m->mdt_child;
4350 struct lu_device *d = &m->mdt_lu_dev;
4351 struct obd_device *obd = mdt2obd_dev(m);
4352 struct lfsck_stop stop;
4355 stop.ls_status = LS_PAUSED;
4357 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4359 target_recovery_fini(obd);
4360 ping_evictor_stop();
4361 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4363 if (m->mdt_opts.mo_coordinator)
4364 mdt_hsm_cdt_stop(m);
4366 mdt_hsm_cdt_fini(m);
4368 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4369 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4370 obd_exports_barrier(obd);
4371 obd_zombie_barrier();
4375 tgt_fini(env, &m->mdt_lut);
4376 mdt_fs_cleanup(env, m);
4377 upcall_cache_cleanup(m->mdt_identity_cache);
4378 m->mdt_identity_cache = NULL;
4380 if (m->mdt_namespace != NULL) {
4381 ldlm_namespace_free(m->mdt_namespace, NULL,
4382 d->ld_obd->obd_force);
4383 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4386 mdt_quota_fini(env, m);
4388 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4390 mdt_seq_fini(env, m);
4391 mdt_fld_fini(env, m);
4393 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4394 cfs_timer_disarm(&m->mdt_ck_timer);
4395 mdt_ck_thread_stop(m);
4400 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4402 LASSERT(atomic_read(&d->ld_ref) == 0);
4404 server_put_mount(mdt_obd_name(m), true);
4409 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4411 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4412 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4414 struct mdt_thread_info *info;
4415 struct obd_device *obd;
4416 const char *dev = lustre_cfg_string(cfg, 0);
4417 const char *num = lustre_cfg_string(cfg, 2);
4418 struct lustre_mount_info *lmi = NULL;
4419 struct lustre_sb_info *lsi;
4421 struct seq_server_site *ss_site;
4422 const char *identity_upcall = "NONE";
4423 struct md_device *next;
4429 lu_device_init(&m->mdt_lu_dev, ldt);
4431 * Environment (env) might be missing mdt_thread_key values at that
4432 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4435 * Usually device allocation path doesn't use module key values, but
4436 * mdt has to do a lot of work here, so allocate key value.
4438 rc = lu_env_refill((struct lu_env *)env);
4442 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4443 LASSERT(info != NULL);
4445 obd = class_name2obd(dev);
4446 LASSERT(obd != NULL);
4448 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4450 m->mdt_som_conf = 0;
4452 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4454 /* default is coordinator off, it is started through conf_param
4456 m->mdt_opts.mo_coordinator = 0;
4458 lmi = server_get_mount(dev);
4460 CERROR("Cannot get mount info for %s!\n", dev);
4463 lsi = s2lsi(lmi->lmi_sb);
4464 /* CMD is supported only in IAM mode */
4466 node_id = simple_strtol(num, NULL, 10);
4467 obd->u.obt.obt_magic = OBT_MAGIC;
4470 spin_lock_init(&m->mdt_ioepoch_lock);
4471 m->mdt_capa_timeout = CAPA_TIMEOUT;
4472 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4473 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4474 m->mdt_squash.rsi_uid = 0;
4475 m->mdt_squash.rsi_gid = 0;
4476 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4477 init_rwsem(&m->mdt_squash.rsi_sem);
4478 spin_lock_init(&m->mdt_osfs_lock);
4479 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4480 m->mdt_enable_remote_dir = 0;
4481 m->mdt_enable_remote_dir_gid = 0;
4483 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4484 m->mdt_lu_dev.ld_obd = obd;
4485 /* Set this lu_device to obd for error handling purposes. */
4486 obd->obd_lu_dev = &m->mdt_lu_dev;
4488 /* init the stack */
4489 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4491 CERROR("%s: Can't init device stack, rc %d\n",
4492 mdt_obd_name(m), rc);
4497 ss_site = mdt_seq_site(m);
4498 s->ld_seq_site = ss_site;
4501 /* set server index */
4502 ss_site->ss_node_id = node_id;
4504 /* failover is the default
4505 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4506 * assumed whose ss_node_id == 0 XXX
4508 obd->obd_replayable = 1;
4509 /* No connection accepted until configurations will finish */
4510 obd->obd_no_conn = 1;
4512 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4513 char *str = lustre_cfg_string(cfg, 4);
4514 if (strchr(str, 'n')) {
4515 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4516 obd->obd_replayable = 0;
4520 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4522 GOTO(err_fini_stack, rc);
4524 rc = mdt_seq_init(env, m);
4526 GOTO(err_fini_fld, rc);
4528 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4529 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4530 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4531 LDLM_NAMESPACE_SERVER,
4532 LDLM_NAMESPACE_GREEDY,
4534 if (m->mdt_namespace == NULL)
4535 GOTO(err_fini_seq, rc = -ENOMEM);
4537 m->mdt_namespace->ns_lvbp = m;
4538 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4540 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4541 /* set obd_namespace for compatibility with old code */
4542 obd->obd_namespace = m->mdt_namespace;
4544 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4546 rc = mdt_hsm_cdt_init(m);
4548 CERROR("%s: error initializing coordinator, rc %d\n",
4549 mdt_obd_name(m), rc);
4550 GOTO(err_free_ns, rc);
4553 rc = mdt_ck_thread_start(m);
4555 GOTO(err_free_hsm, rc);
4557 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4558 OBD_FAIL_MDS_ALL_REQUEST_NET,
4559 OBD_FAIL_MDS_ALL_REPLY_NET);
4563 rc = mdt_fs_setup(env, m, obd, lsi);
4567 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4569 next = m->mdt_child;
4570 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4573 GOTO(err_fs_cleanup, rc);
4575 if (mntopts & MNTOPT_USERXATTR)
4576 m->mdt_opts.mo_user_xattr = 1;
4578 m->mdt_opts.mo_user_xattr = 0;
4580 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4582 GOTO(err_fs_cleanup, rc);
4584 if (mntopts & MNTOPT_ACL)
4585 m->mdt_opts.mo_acl = 1;
4587 m->mdt_opts.mo_acl = 0;
4589 /* XXX: to support suppgid for ACL, we enable identity_upcall
4590 * by default, otherwise, maybe got unexpected -EACCESS. */
4591 if (m->mdt_opts.mo_acl)
4592 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4594 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4596 &mdt_identity_upcall_cache_ops);
4597 if (IS_ERR(m->mdt_identity_cache)) {
4598 rc = PTR_ERR(m->mdt_identity_cache);
4599 m->mdt_identity_cache = NULL;
4600 GOTO(err_fs_cleanup, rc);
4603 rc = mdt_procfs_init(m, dev);
4605 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4606 GOTO(err_recovery, rc);
4609 rc = mdt_quota_init(env, m, cfg);
4611 GOTO(err_procfs, rc);
4613 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4614 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4615 "mdt_ldlm_client", m->mdt_ldlm_client);
4617 ping_evictor_start();
4619 /* recovery will be started upon mdt_prepare()
4620 * when the whole stack is complete and ready
4621 * to serve the requests */
4623 mdt_init_capa_ctxt(env, m);
4625 /* Reduce the initial timeout on an MDS because it doesn't need such
4626 * a long timeout as an OST does. Adaptive timeouts will adjust this
4627 * value appropriately. */
4628 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4629 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4635 target_recovery_fini(obd);
4636 upcall_cache_cleanup(m->mdt_identity_cache);
4637 m->mdt_identity_cache = NULL;
4639 mdt_fs_cleanup(env, m);
4641 tgt_fini(env, &m->mdt_lut);
4643 cfs_timer_disarm(&m->mdt_ck_timer);
4644 mdt_ck_thread_stop(m);
4646 mdt_hsm_cdt_fini(m);
4648 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4649 obd->obd_namespace = m->mdt_namespace = NULL;
4651 mdt_seq_fini(env, m);
4653 mdt_fld_fini(env, m);
4655 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4658 server_put_mount(dev, true);
4662 /* For interoperability, the left element is old parameter, the right one
4663 * is the new version of the parameter, if some parameter is deprecated,
4664 * the new version should be set as NULL. */
4665 static struct cfg_interop_param mdt_interop_param[] = {
4666 { "mdt.group_upcall", NULL },
4667 { "mdt.quota_type", NULL },
4668 { "mdd.quota_type", NULL },
4669 { "mdt.rootsquash", "mdt.root_squash" },
4670 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4674 /* used by MGS to process specific configurations */
4675 static int mdt_process_config(const struct lu_env *env,
4676 struct lu_device *d, struct lustre_cfg *cfg)
4678 struct mdt_device *m = mdt_dev(d);
4679 struct md_device *md_next = m->mdt_child;
4680 struct lu_device *next = md2lu_dev(md_next);
4684 switch (cfg->lcfg_command) {
4686 struct obd_device *obd = d->ld_obd;
4688 /* For interoperability */
4689 struct cfg_interop_param *ptr = NULL;
4690 struct lustre_cfg *old_cfg = NULL;
4693 param = lustre_cfg_string(cfg, 1);
4694 if (param == NULL) {
4695 CERROR("param is empty\n");
4700 ptr = class_find_old_param(param, mdt_interop_param);
4702 if (ptr->new_param == NULL) {
4704 CWARN("For interoperability, skip this %s."
4705 " It is obsolete.\n", ptr->old_param);
4709 CWARN("Found old param %s, changed it to %s.\n",
4710 ptr->old_param, ptr->new_param);
4713 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4720 rc = class_process_proc_seq_param(PARAM_MDT, obd->obd_vars,
4722 if (rc > 0 || rc == -ENOSYS) {
4723 /* is it an HSM var ? */
4724 rc = class_process_proc_seq_param(PARAM_HSM,
4725 hsm_cdt_get_proc_vars(),
4727 if (rc > 0 || rc == -ENOSYS)
4728 /* we don't understand; pass it on */
4729 rc = next->ld_ops->ldo_process_config(env, next,
4733 if (old_cfg != NULL)
4734 lustre_cfg_free(cfg);
4739 /* others are passed further */
4740 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4746 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4747 const struct lu_object_header *hdr,
4748 struct lu_device *d)
4750 struct mdt_object *mo;
4754 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4756 struct lu_object *o;
4757 struct lu_object_header *h;
4760 h = &mo->mot_header;
4761 lu_object_header_init(h);
4762 lu_object_init(o, h, d);
4763 lu_object_add_top(h, o);
4764 o->lo_ops = &mdt_obj_ops;
4765 mutex_init(&mo->mot_ioepoch_mutex);
4766 mutex_init(&mo->mot_lov_mutex);
4767 init_rwsem(&mo->mot_open_sem);
4773 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4774 const struct lu_object_conf *unused)
4776 struct mdt_device *d = mdt_dev(o->lo_dev);
4777 struct lu_device *under;
4778 struct lu_object *below;
4782 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4783 PFID(lu_object_fid(o)));
4785 under = &d->mdt_child->md_lu_dev;
4786 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4787 if (below != NULL) {
4788 lu_object_add(o, below);
4795 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4797 struct mdt_object *mo = mdt_obj(o);
4798 struct lu_object_header *h;
4802 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4803 PFID(lu_object_fid(o)));
4805 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4806 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4809 lu_object_header_fini(h);
4810 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4815 static int mdt_object_print(const struct lu_env *env, void *cookie,
4816 lu_printer_t p, const struct lu_object *o)
4818 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4819 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4820 "flags="LPX64", epochcount=%d, writecount=%d)",
4821 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4822 mdto->mot_ioepoch_count, mdto->mot_writecount);
4825 static int mdt_prepare(const struct lu_env *env,
4826 struct lu_device *pdev,
4827 struct lu_device *cdev)
4829 struct mdt_device *mdt = mdt_dev(cdev);
4830 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4831 struct obd_device *obd = cdev->ld_obd;
4838 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4842 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4846 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4850 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4851 /* The LFSCK instance is registered just now, so it must be there when
4852 * register the namespace to such instance. */
4853 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4855 if (mdt->mdt_seq_site.ss_node_id == 0) {
4856 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4857 &mdt->mdt_md_root_fid);
4862 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4863 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4864 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4865 LASSERT(obd->obd_no_conn);
4866 spin_lock(&obd->obd_dev_lock);
4867 obd->obd_no_conn = 0;
4868 spin_unlock(&obd->obd_dev_lock);
4870 if (obd->obd_recovering == 0)
4871 mdt_postrecov(env, mdt);
4876 const struct lu_device_operations mdt_lu_ops = {
4877 .ldo_object_alloc = mdt_object_alloc,
4878 .ldo_process_config = mdt_process_config,
4879 .ldo_prepare = mdt_prepare,
4882 static const struct lu_object_operations mdt_obj_ops = {
4883 .loo_object_init = mdt_object_init,
4884 .loo_object_free = mdt_object_free,
4885 .loo_object_print = mdt_object_print
4888 static int mdt_obd_set_info_async(const struct lu_env *env,
4889 struct obd_export *exp,
4890 __u32 keylen, void *key,
4891 __u32 vallen, void *val,
4892 struct ptlrpc_request_set *set)
4898 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4899 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4907 * Match client and server connection feature flags.
4909 * Compute the compatibility flags for a connection request based on
4910 * features mutually supported by client and server.
4912 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4913 * must not be updated here, otherwise a partially initialized value may
4914 * be exposed. After the connection request is successfully processed,
4915 * the top-level MDT connect request handler atomically updates the export
4916 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4917 * reply. \see mdt_connect().
4919 * \param exp the obd_export associated with this client/target pair
4920 * \param mdt the target device for the connection
4921 * \param data stores data for this connect request
4924 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4925 * \retval -EBADE client and server feature requirements are incompatible
4927 static int mdt_connect_internal(struct obd_export *exp,
4928 struct mdt_device *mdt,
4929 struct obd_connect_data *data)
4931 LASSERT(data != NULL);
4933 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4934 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4936 /* If no known bits (which should not happen, probably,
4937 as everybody should support LOOKUP and UPDATE bits at least)
4938 revert to compat mode with plain locks. */
4939 if (!data->ocd_ibits_known &&
4940 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4941 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4943 if (!mdt->mdt_opts.mo_acl)
4944 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4946 if (!mdt->mdt_opts.mo_user_xattr)
4947 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4949 if (!mdt->mdt_som_conf)
4950 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4952 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4953 data->ocd_brw_size = min(data->ocd_brw_size,
4954 (__u32)MD_MAX_BRW_SIZE);
4955 if (data->ocd_brw_size == 0) {
4956 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4957 " ocd_version: %x ocd_grant: %d "
4958 "ocd_index: %u ocd_brw_size is "
4959 "unexpectedly zero, network data "
4960 "corruption? Refusing connection of this"
4963 exp->exp_client_uuid.uuid,
4964 exp, data->ocd_connect_flags, data->ocd_version,
4965 data->ocd_grant, data->ocd_index);
4970 /* NB: Disregard the rule against updating
4971 * exp_connect_data.ocd_connect_flags in this case, since
4972 * tgt_client_new() needs to know if this is a lightweight
4973 * connection, and it is safe to expose this flag before
4974 * connection processing completes. */
4975 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4976 spin_lock(&exp->exp_lock);
4977 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4978 spin_unlock(&exp->exp_lock);
4981 data->ocd_version = LUSTRE_VERSION_CODE;
4983 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4984 CWARN("%s: MDS requires FID support, but client not\n",
4989 if (mdt->mdt_som_conf &&
4990 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4991 OBD_CONNECT_MDS_MDS |
4992 OBD_CONNECT_SOM))) {
4993 CWARN("%s: MDS has SOM enabled, but client does not support "
4994 "it\n", mdt_obd_name(mdt));
4998 if (OCD_HAS_FLAG(data, PINGLESS)) {
4999 if (ptlrpc_pinger_suppress_pings()) {
5000 spin_lock(&exp->exp_obd->obd_dev_lock);
5001 list_del_init(&exp->exp_obd_chain_timed);
5002 spin_unlock(&exp->exp_obd->obd_dev_lock);
5004 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5008 data->ocd_max_easize = mdt->mdt_max_ea_size;
5013 /* mds_connect copy */
5014 static int mdt_obd_connect(const struct lu_env *env,
5015 struct obd_export **exp, struct obd_device *obd,
5016 struct obd_uuid *cluuid,
5017 struct obd_connect_data *data,
5020 struct obd_export *lexp;
5021 struct lustre_handle conn = { 0 };
5022 struct mdt_device *mdt;
5026 LASSERT(env != NULL);
5027 if (!exp || !obd || !cluuid)
5030 mdt = mdt_dev(obd->obd_lu_dev);
5033 * first, check whether the stack is ready to handle requests
5034 * XXX: probably not very appropriate method is used now
5035 * at some point we should find a better one
5037 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5038 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5039 rc = obd_get_info(env, mdt->mdt_child_exp,
5040 sizeof(KEY_OSP_CONNECTED),
5041 KEY_OSP_CONNECTED, NULL, NULL, NULL);
5044 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5047 rc = class_connect(&conn, obd, cluuid);
5051 lexp = class_conn2export(&conn);
5052 LASSERT(lexp != NULL);
5054 rc = mdt_connect_internal(lexp, mdt, data);
5056 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5059 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5060 rc = tgt_client_new(env, lexp);
5062 mdt_export_stats_init(obd, lexp, localdata);
5064 /* For phase I, sync for cross-ref operation. */
5065 lexp->exp_keep_sync = 1;
5069 class_disconnect(lexp);
5078 static int mdt_obd_reconnect(const struct lu_env *env,
5079 struct obd_export *exp, struct obd_device *obd,
5080 struct obd_uuid *cluuid,
5081 struct obd_connect_data *data,
5087 if (exp == NULL || obd == NULL || cluuid == NULL)
5090 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5092 mdt_export_stats_init(obd, exp, localdata);
5097 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5098 struct mdt_thread_info *info,
5099 struct mdt_file_data *mfd)
5101 struct lu_context ses;
5105 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5110 lu_context_enter(&ses);
5112 mdt_ucred(info)->uc_valid = UCRED_OLD;
5113 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5115 lu_context_exit(&ses);
5116 lu_context_fini(&ses);
5122 static int mdt_export_cleanup(struct obd_export *exp)
5124 struct mdt_export_data *med = &exp->exp_mdt_data;
5125 struct obd_device *obd = exp->exp_obd;
5126 struct mdt_device *mdt;
5127 struct mdt_thread_info *info;
5129 CFS_LIST_HEAD(closing_list);
5130 struct mdt_file_data *mfd, *n;
5134 spin_lock(&med->med_open_lock);
5135 while (!cfs_list_empty(&med->med_open_head)) {
5136 cfs_list_t *tmp = med->med_open_head.next;
5137 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5139 /* Remove mfd handle so it can't be found again.
5140 * We are consuming the mfd_list reference here. */
5141 class_handle_unhash(&mfd->mfd_handle);
5142 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5144 spin_unlock(&med->med_open_lock);
5145 mdt = mdt_dev(obd->obd_lu_dev);
5146 LASSERT(mdt != NULL);
5148 rc = lu_env_init(&env, LCT_MD_THREAD);
5152 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5153 LASSERT(info != NULL);
5154 memset(info, 0, sizeof *info);
5155 info->mti_env = &env;
5156 info->mti_mdt = mdt;
5157 info->mti_exp = exp;
5159 if (!cfs_list_empty(&closing_list)) {
5160 struct md_attr *ma = &info->mti_attr;
5162 /* Close any open files (which may also cause orphan unlinking). */
5163 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5164 cfs_list_del_init(&mfd->mfd_list);
5165 ma->ma_need = ma->ma_valid = 0;
5167 /* This file is being closed due to an eviction, it
5168 * could have been modified and now dirty regarding to
5169 * HSM archive, check this!
5170 * The logic here is to mark a file dirty if there's a
5171 * chance it was dirtied before the client was evicted,
5172 * so that we don't have to wait for a release attempt
5173 * before finding out the file was actually dirty and
5174 * fail the release. Aggressively marking it dirty here
5175 * will cause the policy engine to attempt to
5176 * re-archive it; when rearchiving, we can compare the
5177 * current version to the HSM data_version and make the
5178 * archive request into a noop if it's not actually
5181 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5182 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5184 /* Don't unlink orphan on failover umount, LU-184 */
5185 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5186 ma->ma_valid = MA_FLAGS;
5187 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5189 mdt_mfd_close(info, mfd);
5192 info->mti_mdt = NULL;
5193 /* cleanup client slot early */
5194 /* Do not erase record for recoverable client. */
5195 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5196 tgt_client_del(&env, exp);
5202 static int mdt_obd_disconnect(struct obd_export *exp)
5208 class_export_get(exp);
5210 rc = server_disconnect_export(exp);
5212 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5214 rc = mdt_export_cleanup(exp);
5215 class_export_put(exp);
5219 /* FIXME: Can we avoid using these two interfaces? */
5220 static int mdt_init_export(struct obd_export *exp)
5222 struct mdt_export_data *med = &exp->exp_mdt_data;
5226 CFS_INIT_LIST_HEAD(&med->med_open_head);
5227 spin_lock_init(&med->med_open_lock);
5228 mutex_init(&med->med_idmap_mutex);
5229 med->med_idmap = NULL;
5230 spin_lock(&exp->exp_lock);
5231 exp->exp_connecting = 1;
5232 spin_unlock(&exp->exp_lock);
5234 /* self-export doesn't need client data and ldlm initialization */
5235 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5236 &exp->exp_client_uuid)))
5239 rc = tgt_client_alloc(exp);
5243 rc = ldlm_init_export(exp);
5250 tgt_client_free(exp);
5252 CERROR("%s: Failed to initialize export: rc = %d\n",
5253 exp->exp_obd->obd_name, rc);
5257 static int mdt_destroy_export(struct obd_export *exp)
5261 if (exp_connect_rmtclient(exp))
5262 mdt_cleanup_idmap(&exp->exp_mdt_data);
5264 target_destroy_export(exp);
5265 /* destroy can be called from failed obd_setup, so
5266 * checking uuid is safer than obd_self_export */
5267 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5268 &exp->exp_client_uuid)))
5271 ldlm_destroy_export(exp);
5272 tgt_client_free(exp);
5274 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5275 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5280 /** The maximum depth that fid2path() will search.
5281 * This is limited only because we want to store the fids for
5282 * historical path lookup purposes.
5284 #define MAX_PATH_DEPTH 100
5286 /** mdt_path() lookup structure. */
5287 struct path_lookup_info {
5288 __u64 pli_recno; /**< history point */
5289 __u64 pli_currec; /**< current record */
5290 struct lu_fid pli_fid;
5291 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5292 struct mdt_object *pli_mdt_obj;
5293 char *pli_path; /**< full path */
5295 int pli_linkno; /**< which hardlink to follow */
5296 int pli_fidcount; /**< number of \a pli_fids */
5299 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5300 struct linkea_data *ldata)
5304 LASSERT(ldata->ld_buf->lb_buf != NULL);
5306 if (!mdt_object_exists(mdt_obj))
5309 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5310 ldata->ld_buf, XATTR_NAME_LINK);
5311 if (rc == -ERANGE) {
5312 /* Buf was too small, figure out what we need. */
5313 lu_buf_free(ldata->ld_buf);
5314 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5315 ldata->ld_buf, XATTR_NAME_LINK);
5318 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5319 if (ldata->ld_buf->lb_buf == NULL)
5321 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5322 ldata->ld_buf, XATTR_NAME_LINK);
5327 return linkea_init(ldata);
5330 static int mdt_path_current(struct mdt_thread_info *info,
5331 struct path_lookup_info *pli)
5333 struct mdt_device *mdt = info->mti_mdt;
5334 struct mdt_object *mdt_obj;
5335 struct link_ea_header *leh;
5336 struct link_ea_entry *lee;
5337 struct lu_name *tmpname = &info->mti_name;
5338 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5339 struct lu_buf *buf = &info->mti_big_buf;
5342 struct linkea_data ldata = { 0 };
5346 /* temp buffer for path element, the buffer will be finally freed
5347 * in mdt_thread_info_fini */
5348 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5349 if (buf->lb_buf == NULL)
5353 ptr = pli->pli_path + pli->pli_pathlen - 1;
5356 pli->pli_fidcount = 0;
5357 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5358 *tmpfid = pli->pli_fids[0];
5359 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5360 * so checking root_fid can only happen on MDT0. */
5361 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5362 &pli->pli_fids[pli->pli_fidcount])) {
5363 struct lu_buf lmv_buf;
5365 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5366 if (IS_ERR(mdt_obj))
5367 GOTO(out, rc = PTR_ERR(mdt_obj));
5369 if (mdt_object_remote(mdt_obj)) {
5370 mdt_object_put(info->mti_env, mdt_obj);
5371 GOTO(remote_out, rc = -EREMOTE);
5374 lmv_buf.lb_buf = info->mti_xattr_buf;
5375 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5377 /* Check if it is slave stripes */
5378 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5379 &lmv_buf, XATTR_NAME_LMV);
5381 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5383 /* For slave stripes, get its master */
5384 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5385 struct lmv_mds_md_v1 *lmm1 = &lmm->lmv_md_v1;
5387 fid_le_to_cpu(tmpfid, &lmm1->lmv_master_fid);
5388 if (!fid_is_sane(tmpfid)) {
5389 mdt_object_put(info->mti_env, mdt_obj);
5390 GOTO(out, rc = -EINVAL);
5392 mdt_object_put(info->mti_env, mdt_obj);
5393 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5398 if (!mdt_object_exists(mdt_obj)) {
5399 mdt_object_put(info->mti_env, mdt_obj);
5400 GOTO(out, rc = -ENOENT);
5403 rc = mdt_links_read(info, mdt_obj, &ldata);
5404 mdt_object_put(info->mti_env, mdt_obj);
5409 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5410 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5411 /* If set, use link #linkno for path lookup, otherwise use
5412 link #0. Only do this for the final path element. */
5413 if (pli->pli_fidcount == 0 &&
5414 pli->pli_linkno < leh->leh_reccount) {
5416 for (count = 0; count < pli->pli_linkno; count++) {
5417 lee = (struct link_ea_entry *)
5418 ((char *)lee + reclen);
5419 linkea_entry_unpack(lee, &reclen, tmpname,
5422 if (pli->pli_linkno < leh->leh_reccount - 1)
5423 /* indicate to user there are more links */
5427 /* Pack the name in the end of the buffer */
5428 ptr -= tmpname->ln_namelen;
5429 if (ptr - 1 <= pli->pli_path)
5430 GOTO(out, rc = -EOVERFLOW);
5431 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5434 /* Store the parent fid for historic lookup */
5435 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5436 GOTO(out, rc = -EOVERFLOW);
5437 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5441 ptr++; /* skip leading / */
5442 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5449 /* Returns the full path to this fid, as of changelog record recno. */
5450 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5451 char *path, int pathlen, __u64 *recno, int *linkno,
5454 struct mdt_device *mdt = info->mti_mdt;
5455 struct path_lookup_info *pli;
5463 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5472 pli->pli_mdt_obj = obj;
5473 pli->pli_recno = *recno;
5474 pli->pli_path = path;
5475 pli->pli_pathlen = pathlen;
5476 pli->pli_linkno = *linkno;
5478 /* Retry multiple times in case file is being moved */
5479 while (tries-- && rc == -EAGAIN)
5480 rc = mdt_path_current(info, pli);
5482 /* return the last resolved fids to the client, so the client will
5483 * build the left path on another MDT for remote object */
5484 *fid = pli->pli_fids[pli->pli_fidcount];
5486 *recno = pli->pli_currec;
5487 /* Return next link index to caller */
5488 *linkno = pli->pli_linkno;
5495 static int mdt_fid2path(struct mdt_thread_info *info,
5496 struct getinfo_fid2path *fp)
5498 struct mdt_device *mdt = info->mti_mdt;
5499 struct mdt_object *obj;
5503 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5504 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5506 if (!fid_is_sane(&fp->gf_fid))
5509 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5510 CWARN("%s: "DFID" is invalid, sequence should be "
5511 ">= "LPX64"\n", mdt_obd_name(mdt),
5512 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5516 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5517 if (obj == NULL || IS_ERR(obj)) {
5518 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5523 if (mdt_object_remote(obj))
5525 else if (!mdt_object_exists(obj))
5531 mdt_object_put(info->mti_env, obj);
5532 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5533 PFID(&fp->gf_fid), rc);
5537 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5538 &fp->gf_linkno, &fp->gf_fid);
5540 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5541 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5543 mdt_object_put(info->mti_env, obj);
5548 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5549 void *val, int vallen)
5551 struct getinfo_fid2path *fpout, *fpin;
5554 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5557 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5558 lustre_swab_fid2path(fpin);
5560 memcpy(fpout, fpin, sizeof(*fpin));
5561 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5564 rc = mdt_fid2path(info, fpout);
5568 int mdt_get_info(struct tgt_session_info *tsi)
5578 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5580 CDEBUG(D_IOCTL, "No GETINFO key");
5581 RETURN(err_serious(-EFAULT));
5583 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5586 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5587 if (vallen == NULL) {
5588 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5589 RETURN(err_serious(-EFAULT));
5592 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5594 rc = req_capsule_server_pack(tsi->tsi_pill);
5596 RETURN(err_serious(rc));
5598 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5599 if (valout == NULL) {
5600 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5601 RETURN(err_serious(-EFAULT));
5604 if (KEY_IS(KEY_FID2PATH)) {
5605 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5607 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5608 mdt_thread_info_fini(info);
5615 /* Pass the ioc down */
5616 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5617 unsigned int cmd, int len, void *data)
5619 struct lu_context ioctl_session;
5620 struct md_device *next = mdt->mdt_child;
5624 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5627 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5628 lu_context_enter(&ioctl_session);
5629 env->le_ses = &ioctl_session;
5631 LASSERT(next->md_ops->mdo_iocontrol);
5632 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5634 lu_context_exit(&ioctl_session);
5635 lu_context_fini(&ioctl_session);
5639 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5641 struct obd_ioctl_data *data = karg;
5644 struct mdt_object *obj;
5645 struct mdt_lock_handle *lh;
5649 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5650 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5653 fid = (struct lu_fid *)data->ioc_inlbuf1;
5655 if (!fid_is_sane(fid))
5658 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5660 lh = &mti->mti_lh[MDT_LH_PARENT];
5661 mdt_lock_reg_init(lh, LCK_CR);
5663 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5665 RETURN(PTR_ERR(obj));
5667 if (mdt_object_remote(obj)) {
5670 * before calling version get the correct MDS should be
5671 * fid, this is error to find remote object here
5673 CERROR("nonlocal object "DFID"\n", PFID(fid));
5674 } else if (!mdt_object_exists(obj)) {
5675 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5678 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5679 *(__u64 *)data->ioc_inlbuf2 = version;
5682 mdt_object_unlock_put(mti, obj, lh, 1);
5686 /* ioctls on obd dev */
5687 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5688 void *karg, void *uarg)
5691 struct obd_device *obd = exp->exp_obd;
5692 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5693 struct dt_device *dt = mdt->mdt_bottom;
5697 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5698 rc = lu_env_init(&env, LCT_MD_THREAD);
5704 rc = mdt_device_sync(&env, mdt);
5706 case OBD_IOC_SET_READONLY:
5707 rc = dt->dd_ops->dt_ro(&env, dt);
5709 case OBD_IOC_ABORT_RECOVERY:
5710 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5711 target_stop_recovery_thread(obd);
5714 case OBD_IOC_CHANGELOG_REG:
5715 case OBD_IOC_CHANGELOG_DEREG:
5716 case OBD_IOC_CHANGELOG_CLEAR:
5717 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5719 case OBD_IOC_START_LFSCK: {
5720 struct md_device *next = mdt->mdt_child;
5721 struct obd_ioctl_data *data = karg;
5722 struct lfsck_start_param lsp;
5724 if (unlikely(data == NULL)) {
5729 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5730 lsp.lsp_index_valid = 0;
5731 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5734 case OBD_IOC_STOP_LFSCK: {
5735 struct md_device *next = mdt->mdt_child;
5736 struct obd_ioctl_data *data = karg;
5737 struct lfsck_stop stop;
5739 stop.ls_status = LS_STOPPED;
5740 /* Old lfsck utils may pass NULL @stop. */
5741 if (data->ioc_inlbuf1 == NULL)
5745 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5747 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5750 case OBD_IOC_GET_OBJ_VERSION: {
5751 struct mdt_thread_info *mti;
5752 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5753 memset(mti, 0, sizeof *mti);
5754 mti->mti_env = &env;
5758 rc = mdt_ioc_version_get(mti, karg);
5761 case OBD_IOC_CATLOGLIST: {
5762 struct mdt_thread_info *mti;
5764 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5765 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5766 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5767 &mti->mti_tmp_fid1);
5772 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5773 mdt_obd_name(mdt), cmd, rc);
5780 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5782 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5783 struct lfsck_start_param lsp;
5787 lsp.lsp_start = NULL;
5788 lsp.lsp_index_valid = 0;
5789 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5790 OBD_IOC_START_LFSCK,
5792 if (rc != 0 && rc != -EALREADY)
5793 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5794 mdt_obd_name(mdt), rc);
5796 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5800 static int mdt_obd_postrecov(struct obd_device *obd)
5805 rc = lu_env_init(&env, LCT_MD_THREAD);
5808 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5813 static struct obd_ops mdt_obd_device_ops = {
5814 .o_owner = THIS_MODULE,
5815 .o_set_info_async = mdt_obd_set_info_async,
5816 .o_connect = mdt_obd_connect,
5817 .o_reconnect = mdt_obd_reconnect,
5818 .o_disconnect = mdt_obd_disconnect,
5819 .o_init_export = mdt_init_export,
5820 .o_destroy_export = mdt_destroy_export,
5821 .o_iocontrol = mdt_iocontrol,
5822 .o_postrecov = mdt_obd_postrecov,
5825 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5826 struct lu_device *d)
5828 struct mdt_device *m = mdt_dev(d);
5835 static struct lu_device *mdt_device_free(const struct lu_env *env,
5836 struct lu_device *d)
5838 struct mdt_device *m = mdt_dev(d);
5841 lu_device_fini(&m->mdt_lu_dev);
5847 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5848 struct lu_device_type *t,
5849 struct lustre_cfg *cfg)
5851 struct lu_device *l;
5852 struct mdt_device *m;
5859 rc = mdt_init0(env, m, t, cfg);
5861 mdt_device_free(env, l);
5866 l = ERR_PTR(-ENOMEM);
5870 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5871 LU_KEY_INIT(mdt, struct mdt_thread_info);
5873 static void mdt_key_fini(const struct lu_context *ctx,
5874 struct lu_context_key *key, void* data)
5876 struct mdt_thread_info *info = data;
5878 if (info->mti_big_lmm) {
5879 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5880 info->mti_big_lmm = NULL;
5881 info->mti_big_lmmsize = 0;
5886 /* context key: mdt_thread_key */
5887 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5889 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5891 return lu_ucred(info->mti_env);
5894 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5896 return lu_ucred_check(info->mti_env);
5900 * Enable/disable COS (Commit On Sharing).
5902 * Set/Clear the COS flag in mdt options.
5904 * \param mdt mdt device
5905 * \param val 0 disables COS, other values enable COS
5907 void mdt_enable_cos(struct mdt_device *mdt, int val)
5912 mdt->mdt_opts.mo_cos = !!val;
5913 rc = lu_env_init(&env, LCT_LOCAL);
5914 if (unlikely(rc != 0)) {
5915 CWARN("lu_env initialization failed with rc = %d,"
5916 "cannot sync\n", rc);
5919 mdt_device_sync(&env, mdt);
5924 * Check COS (Commit On Sharing) status.
5926 * Return COS flag status.
5928 * \param mdt mdt device
5930 int mdt_cos_is_enabled(struct mdt_device *mdt)
5932 return mdt->mdt_opts.mo_cos != 0;
5935 static struct lu_device_type_operations mdt_device_type_ops = {
5936 .ldto_device_alloc = mdt_device_alloc,
5937 .ldto_device_free = mdt_device_free,
5938 .ldto_device_fini = mdt_device_fini
5941 static struct lu_device_type mdt_device_type = {
5942 .ldt_tags = LU_DEVICE_MD,
5943 .ldt_name = LUSTRE_MDT_NAME,
5944 .ldt_ops = &mdt_device_type_ops,
5945 .ldt_ctx_tags = LCT_MD_THREAD
5948 static int __init mdt_mod_init(void)
5952 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5953 FID_NOBRACE_LEN + 1);
5954 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5956 rc = lu_kmem_init(mdt_caches);
5960 rc = mds_mod_init();
5964 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
5965 #ifndef HAVE_ONLY_PROCFS_SEQ
5968 LUSTRE_MDT_NAME, &mdt_device_type);
5973 lu_kmem_fini(mdt_caches);
5980 static void __exit mdt_mod_exit(void)
5982 class_unregister_type(LUSTRE_MDT_NAME);
5984 lu_kmem_fini(mdt_caches);
5987 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5988 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5989 MODULE_LICENSE("GPL");
5991 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);