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,
503 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
511 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
514 const struct lu_env *env = info->mti_env;
518 LASSERT(info->mti_big_lmm_used == 0);
519 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
523 /* big_lmm may need to be grown */
524 if (info->mti_big_lmmsize < rc) {
525 int size = size_roundup_power2(rc);
527 if (info->mti_big_lmmsize > 0) {
528 /* free old buffer */
529 LASSERT(info->mti_big_lmm);
530 OBD_FREE_LARGE(info->mti_big_lmm,
531 info->mti_big_lmmsize);
532 info->mti_big_lmm = NULL;
533 info->mti_big_lmmsize = 0;
536 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
537 if (info->mti_big_lmm == NULL)
539 info->mti_big_lmmsize = size;
541 LASSERT(info->mti_big_lmmsize >= rc);
543 info->mti_buf.lb_buf = info->mti_big_lmm;
544 info->mti_buf.lb_len = info->mti_big_lmmsize;
545 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
550 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
551 struct md_attr *ma, const char *name)
553 struct md_object *next = mdt_object_child(o);
554 struct lu_buf *buf = &info->mti_buf;
557 if (strcmp(name, XATTR_NAME_LOV) == 0) {
558 buf->lb_buf = ma->ma_lmm;
559 buf->lb_len = ma->ma_lmm_size;
560 LASSERT(!(ma->ma_valid & MA_LOV));
561 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
562 buf->lb_buf = ma->ma_lmv;
563 buf->lb_len = ma->ma_lmv_size;
564 LASSERT(!(ma->ma_valid & MA_LMV));
565 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
566 buf->lb_buf = ma->ma_lmv;
567 buf->lb_len = ma->ma_lmv_size;
568 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
573 rc = mo_xattr_get(info->mti_env, next, buf, name);
575 if (strcmp(name, XATTR_NAME_LOV) == 0) {
576 ma->ma_lmm_size = rc;
577 ma->ma_valid |= MA_LOV;
578 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
579 ma->ma_lmv_size = rc;
580 ma->ma_valid |= MA_LMV;
581 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
582 ma->ma_lmv_size = rc;
583 ma->ma_valid |= MA_LMV_DEF;
587 } else if (rc == -ENODATA) {
590 } else if (rc == -ERANGE) {
591 /* Default LMV has fixed size, so it must be able to fit
592 * in the original buffer */
593 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
595 rc = mdt_big_xattr_get(info, o, name);
597 info->mti_big_lmm_used = 1;
598 if (!strcmp(name, XATTR_NAME_LOV)) {
599 ma->ma_valid |= MA_LOV;
600 ma->ma_lmm = info->mti_big_lmm;
601 ma->ma_lmm_size = rc;
602 } else if (!strcmp(name, XATTR_NAME_LMV)) {
603 ma->ma_valid |= MA_LMV;
604 ma->ma_lmv = info->mti_big_lmm;
605 ma->ma_lmv_size = rc;
610 /* update mdt_max_mdsize so all clients
611 * will be aware about that */
612 if (info->mti_mdt->mdt_max_mdsize < rc)
613 info->mti_mdt->mdt_max_mdsize = rc;
621 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
622 struct mdt_object *o, struct lu_fid *pfid)
624 struct lu_buf *buf = &info->mti_buf;
625 struct link_ea_header *leh;
626 struct link_ea_entry *lee;
630 buf->lb_buf = info->mti_big_lmm;
631 buf->lb_len = info->mti_big_lmmsize;
632 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
633 buf, XATTR_NAME_LINK);
634 /* ignore errors, MA_PFID won't be set and it is
635 * up to the caller to treat this as an error */
636 if (rc == -ERANGE || buf->lb_len == 0) {
637 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
638 buf->lb_buf = info->mti_big_lmm;
639 buf->lb_len = info->mti_big_lmmsize;
644 if (rc < sizeof(*leh)) {
645 CERROR("short LinkEA on "DFID": rc = %d\n",
646 PFID(mdt_object_fid(o)), rc);
650 leh = (struct link_ea_header *) buf->lb_buf;
651 lee = (struct link_ea_entry *)(leh + 1);
652 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
653 leh->leh_magic = LINK_EA_MAGIC;
654 leh->leh_reccount = __swab32(leh->leh_reccount);
655 leh->leh_len = __swab64(leh->leh_len);
657 if (leh->leh_magic != LINK_EA_MAGIC)
659 if (leh->leh_reccount == 0)
662 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
663 fid_be_to_cpu(pfid, pfid);
668 int mdt_attr_get_complex(struct mdt_thread_info *info,
669 struct mdt_object *o, struct md_attr *ma)
671 const struct lu_env *env = info->mti_env;
672 struct md_object *next = mdt_object_child(o);
673 struct lu_buf *buf = &info->mti_buf;
674 u32 mode = lu_object_attr(&next->mo_lu);
675 int need = ma->ma_need;
681 if (need & MA_INODE) {
682 ma->ma_need = MA_INODE;
683 rc = mo_attr_get(env, next, ma);
686 ma->ma_valid |= MA_INODE;
689 if (need & MA_PFID) {
690 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
692 ma->ma_valid |= MA_PFID;
693 /* ignore this error, parent fid is not mandatory */
697 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
698 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
703 if (need & MA_LMV && S_ISDIR(mode)) {
704 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
709 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
710 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
715 if (need & MA_SOM && S_ISREG(mode)) {
716 buf->lb_buf = info->mti_xattr_buf;
717 buf->lb_len = sizeof(info->mti_xattr_buf);
718 CLASSERT(sizeof(struct som_attrs) <=
719 sizeof(info->mti_xattr_buf));
720 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
721 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
723 ma->ma_valid |= MA_SOM;
724 else if (rc2 < 0 && rc2 != -ENODATA)
728 if (need & MA_HSM && S_ISREG(mode)) {
729 buf->lb_buf = info->mti_xattr_buf;
730 buf->lb_len = sizeof(info->mti_xattr_buf);
731 CLASSERT(sizeof(struct hsm_attrs) <=
732 sizeof(info->mti_xattr_buf));
733 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
734 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
736 ma->ma_valid |= MA_HSM;
737 else if (rc2 < 0 && rc2 != -ENODATA)
741 #ifdef CONFIG_FS_POSIX_ACL
742 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
743 buf->lb_buf = ma->ma_acl;
744 buf->lb_len = ma->ma_acl_size;
745 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
747 ma->ma_acl_size = rc2;
748 ma->ma_valid |= MA_ACL_DEF;
749 } else if (rc2 == -ENODATA) {
758 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
759 rc, ma->ma_valid, ma->ma_lmm);
763 static int mdt_getattr_internal(struct mdt_thread_info *info,
764 struct mdt_object *o, int ma_need)
766 struct md_object *next = mdt_object_child(o);
767 const struct mdt_body *reqbody = info->mti_body;
768 struct ptlrpc_request *req = mdt_info_req(info);
769 struct md_attr *ma = &info->mti_attr;
770 struct lu_attr *la = &ma->ma_attr;
771 struct req_capsule *pill = info->mti_pill;
772 const struct lu_env *env = info->mti_env;
773 struct mdt_body *repbody;
774 struct lu_buf *buffer = &info->mti_buf;
779 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
780 RETURN(err_serious(-ENOMEM));
782 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
786 if (mdt_object_remote(o)) {
787 /* This object is located on remote node.*/
788 /* Return -ENOTSUPP for old client */
789 if (!mdt_is_dne_client(req->rq_export))
790 GOTO(out, rc = -ENOTSUPP);
792 repbody->fid1 = *mdt_object_fid(o);
793 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
797 if (reqbody->eadatasize > 0) {
798 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
799 if (buffer->lb_buf == NULL)
800 GOTO(out, rc = -EPROTO);
801 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
804 buffer->lb_buf = NULL;
806 ma_need &= ~(MA_LOV | MA_LMV);
807 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
808 mdt_obd_name(info->mti_mdt),
809 req->rq_export->exp_client_uuid.uuid);
812 /* If it is dir object and client require MEA, then we got MEA */
813 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
814 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
815 /* Assumption: MDT_MD size is enough for lmv size. */
816 ma->ma_lmv = buffer->lb_buf;
817 ma->ma_lmv_size = buffer->lb_len;
818 ma->ma_need = MA_INODE;
819 if (ma->ma_lmv_size > 0) {
820 if (reqbody->valid & OBD_MD_MEA)
821 ma->ma_need |= MA_LMV;
822 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
823 ma->ma_need |= MA_LMV_DEF;
826 ma->ma_lmm = buffer->lb_buf;
827 ma->ma_lmm_size = buffer->lb_len;
828 ma->ma_need = MA_INODE | MA_HSM;
829 if (ma->ma_lmm_size > 0)
830 ma->ma_need |= MA_LOV;
833 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
834 reqbody->valid & OBD_MD_FLDIREA &&
835 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
836 /* get default stripe info for this dir. */
837 ma->ma_need |= MA_LOV_DEF;
839 ma->ma_need |= ma_need;
840 if (ma->ma_need & MA_SOM)
841 ma->ma_som = &info->mti_u.som.data;
843 rc = mdt_attr_get_complex(info, o, ma);
845 CERROR("%s: getattr error for "DFID": rc = %d\n",
846 mdt_obd_name(info->mti_mdt),
847 PFID(mdt_object_fid(o)), rc);
851 /* if file is released, check if a restore is running */
852 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
853 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
854 repbody->t_state = MS_RESTORE;
855 repbody->valid |= OBD_MD_TSTATE;
858 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
860 /* the Lustre protocol supposes to return default striping
861 * on the user-visible root if explicitly requested */
862 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
863 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
864 struct lu_fid rootfid;
865 struct mdt_object *root;
866 struct mdt_device *mdt = info->mti_mdt;
868 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
871 root = mdt_object_find(env, mdt, &rootfid);
873 RETURN(PTR_ERR(root));
874 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
875 mdt_object_put(info->mti_env, root);
877 CERROR("%s: getattr error for "DFID": rc = %d\n",
878 mdt_obd_name(info->mti_mdt),
879 PFID(mdt_object_fid(o)), rc);
884 if (likely(ma->ma_valid & MA_INODE))
885 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
889 if (mdt_body_has_lov(la, reqbody)) {
890 if (ma->ma_valid & MA_LOV) {
891 LASSERT(ma->ma_lmm_size);
892 repbody->eadatasize = ma->ma_lmm_size;
893 if (S_ISDIR(la->la_mode))
894 repbody->valid |= OBD_MD_FLDIREA;
896 repbody->valid |= OBD_MD_FLEASIZE;
897 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
899 if (ma->ma_valid & MA_LMV) {
900 /* Return -ENOTSUPP for old client */
901 if (!mdt_is_striped_client(req->rq_export))
904 LASSERT(S_ISDIR(la->la_mode));
905 mdt_dump_lmv(D_INFO, ma->ma_lmv);
906 repbody->eadatasize = ma->ma_lmv_size;
907 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
909 if (ma->ma_valid & MA_LMV_DEF) {
910 /* Return -ENOTSUPP for old client */
911 if (!mdt_is_striped_client(req->rq_export))
913 LASSERT(S_ISDIR(la->la_mode));
914 repbody->eadatasize = ma->ma_lmv_size;
915 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
917 } else if (S_ISLNK(la->la_mode) &&
918 reqbody->valid & OBD_MD_LINKNAME) {
919 buffer->lb_buf = ma->ma_lmm;
920 /* eadatasize from client includes NULL-terminator, so
921 * there is no need to read it */
922 buffer->lb_len = reqbody->eadatasize - 1;
923 rc = mo_readlink(env, next, buffer);
924 if (unlikely(rc <= 0)) {
925 CERROR("%s: readlink failed for "DFID": rc = %d\n",
926 mdt_obd_name(info->mti_mdt),
927 PFID(mdt_object_fid(o)), rc);
930 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
932 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
934 repbody->valid |= OBD_MD_LINKNAME;
935 /* we need to report back size with NULL-terminator
936 * because client expects that */
937 repbody->eadatasize = rc + 1;
938 if (repbody->eadatasize != reqbody->eadatasize)
939 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
940 "on "DFID ", expected %d\n",
941 mdt_obd_name(info->mti_mdt),
942 rc, PFID(mdt_object_fid(o)),
943 reqbody->eadatasize - 1);
945 ((char *)ma->ma_lmm)[rc] = 0;
947 /* If the total CDEBUG() size is larger than a page, it
948 * will print a warning to the console, avoid this by
949 * printing just the last part of the symlink. */
950 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
951 print_limit < rc ? "..." : "", print_limit,
952 (char *)ma->ma_lmm + rc - print_limit, rc);
957 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
958 repbody->max_cookiesize = 0;
959 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
960 repbody->valid |= OBD_MD_FLMODEASIZE;
961 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
962 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
963 repbody->max_cookiesize);
966 if (exp_connect_rmtclient(info->mti_exp) &&
967 reqbody->valid & OBD_MD_FLRMTPERM) {
968 void *buf = req_capsule_server_get(pill, &RMF_ACL);
970 /* mdt_getattr_lock only */
971 rc = mdt_pack_remote_perm(info, o, buf);
973 repbody->valid &= ~OBD_MD_FLRMTPERM;
974 repbody->aclsize = 0;
977 repbody->valid |= OBD_MD_FLRMTPERM;
978 repbody->aclsize = sizeof(struct mdt_remote_perm);
981 #ifdef CONFIG_FS_POSIX_ACL
982 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
983 (reqbody->valid & OBD_MD_FLACL)) {
984 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
985 buffer->lb_len = req_capsule_get_size(pill,
986 &RMF_ACL, RCL_SERVER);
987 if (buffer->lb_len > 0) {
988 rc = mo_xattr_get(env, next, buffer,
989 XATTR_NAME_ACL_ACCESS);
991 if (rc == -ENODATA) {
992 repbody->aclsize = 0;
993 repbody->valid |= OBD_MD_FLACL;
995 } else if (rc == -EOPNOTSUPP) {
998 CERROR("%s: unable to read "DFID
1000 mdt_obd_name(info->mti_mdt),
1001 PFID(mdt_object_fid(o)), rc);
1004 repbody->aclsize = rc;
1005 repbody->valid |= OBD_MD_FLACL;
1012 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
1013 info->mti_mdt->mdt_lut.lut_mds_capa &&
1014 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1015 struct lustre_capa *capa;
1017 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1019 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1020 rc = mo_capa_get(env, next, capa, 0);
1023 repbody->valid |= OBD_MD_FLMDSCAPA;
1028 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1033 static int mdt_renew_capa(struct mdt_thread_info *info)
1035 struct mdt_object *obj = info->mti_object;
1036 struct mdt_body *body;
1037 struct lustre_capa *capa, *c;
1041 /* if object doesn't exist, or server has disabled capability,
1042 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1045 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1046 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1049 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1050 LASSERT(body != NULL);
1052 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1055 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1059 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1061 body->valid |= OBD_MD_FLOSSCAPA;
1065 static int mdt_getattr(struct tgt_session_info *tsi)
1067 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1068 struct mdt_object *obj = info->mti_object;
1069 struct req_capsule *pill = info->mti_pill;
1070 struct mdt_body *reqbody;
1071 struct mdt_body *repbody;
1075 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1078 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1079 rc = req_capsule_server_pack(pill);
1081 RETURN(err_serious(rc));
1082 rc = mdt_renew_capa(info);
1083 GOTO(out_shrink, rc);
1086 LASSERT(obj != NULL);
1087 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1089 /* Unlike intent case where we need to pre-fill out buffers early on
1090 * in intent policy for ldlm reasons, here we can have a much better
1091 * guess at EA size by just reading it from disk.
1092 * Exceptions are readdir and (missing) directory striping */
1094 if (reqbody->valid & OBD_MD_LINKNAME) {
1095 /* No easy way to know how long is the symlink, but it cannot
1096 * be more than PATH_MAX, so we allocate +1 */
1099 /* A special case for fs ROOT: getattr there might fetch
1100 * default EA for entire fs, not just for this dir!
1102 } else if (lu_fid_eq(mdt_object_fid(obj),
1103 &info->mti_mdt->mdt_md_root_fid) &&
1104 (reqbody->valid & OBD_MD_FLDIREA) &&
1105 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1107 /* Should the default strping be bigger, mdt_fix_reply
1108 * will reallocate */
1109 rc = DEF_REP_MD_SIZE;
1111 /* Read the actual EA size from disk */
1112 rc = mdt_attr_get_eabuf_size(info, obj);
1116 GOTO(out_shrink, rc);
1118 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1120 rc = req_capsule_server_pack(pill);
1121 if (unlikely(rc != 0))
1122 GOTO(out, rc = err_serious(rc));
1124 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1125 LASSERT(repbody != NULL);
1126 repbody->eadatasize = 0;
1127 repbody->aclsize = 0;
1129 if (reqbody->valid & OBD_MD_FLRMTPERM)
1130 rc = mdt_init_ucred(info, reqbody);
1132 rc = mdt_check_ucred(info);
1134 GOTO(out_shrink, rc);
1136 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1139 * Don't check capability at all, because rename might getattr for
1140 * remote obj, and at that time no capability is available.
1142 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1143 rc = mdt_getattr_internal(info, obj, 0);
1144 if (reqbody->valid & OBD_MD_FLRMTPERM)
1145 mdt_exit_ucred(info);
1148 mdt_client_compatibility(info);
1149 rc2 = mdt_fix_reply(info);
1153 mdt_thread_info_fini(info);
1157 static int mdt_is_subdir(struct tgt_session_info *tsi)
1159 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1160 struct mdt_object *o = info->mti_object;
1161 struct req_capsule *pill = info->mti_pill;
1162 const struct mdt_body *body = info->mti_body;
1163 struct mdt_body *repbody;
1169 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1172 * We save last checked parent fid to @repbody->fid1 for remote
1175 LASSERT(fid_is_sane(&body->fid2));
1176 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1177 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1178 &body->fid2, &repbody->fid1);
1179 if (rc == 0 || rc == -EREMOTE)
1180 repbody->valid |= OBD_MD_FLID;
1182 mdt_thread_info_fini(info);
1186 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1188 struct mdt_thread_info *info;
1189 struct ptlrpc_request *req = tgt_ses_req(tsi);
1190 struct obd_export *exp = req->rq_export;
1191 struct mdt_object *o1, *o2, *o;
1192 struct mdt_lock_handle *lh1, *lh2;
1193 struct mdc_swap_layouts *msl;
1197 /* client does not support layout lock, so layout swaping
1199 * FIXME: there is a problem for old clients which don't support
1200 * layout lock yet. If those clients have already opened the file
1201 * they won't be notified at all so that old layout may still be
1202 * used to do IO. This can be fixed after file release is landed by
1203 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1204 if (!exp_connect_layout(exp))
1205 RETURN(-EOPNOTSUPP);
1207 info = tsi2mdt_info(tsi);
1209 if (info->mti_dlm_req != NULL)
1210 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1212 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1213 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1214 req_capsule_client_get(info->mti_pill,
1217 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1218 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1219 req_capsule_client_get(info->mti_pill,
1222 o1 = info->mti_object;
1223 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1224 &info->mti_body->fid2);
1226 GOTO(out, rc = PTR_ERR(o));
1228 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1229 GOTO(put, rc = -ENOENT);
1231 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1232 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1238 /* permission check. Make sure the calling process having permission
1239 * to write both files. */
1240 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1245 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1250 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1252 GOTO(put, rc = -EPROTO);
1254 lh1 = &info->mti_lh[MDT_LH_NEW];
1255 mdt_lock_reg_init(lh1, LCK_EX);
1256 lh2 = &info->mti_lh[MDT_LH_OLD];
1257 mdt_lock_reg_init(lh2, LCK_EX);
1259 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1260 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1264 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1265 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1269 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1270 mdt_object_child(o2), msl->msl_flags);
1273 mdt_object_unlock(info, o2, lh2, rc);
1275 mdt_object_unlock(info, o1, lh1, rc);
1277 mdt_object_put(info->mti_env, o);
1279 mdt_thread_info_fini(info);
1283 static int mdt_raw_lookup(struct mdt_thread_info *info,
1284 struct mdt_object *parent,
1285 const struct lu_name *lname,
1286 struct ldlm_reply *ldlm_rep)
1288 struct md_object *next = mdt_object_child(info->mti_object);
1289 const struct mdt_body *reqbody = info->mti_body;
1290 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1291 struct mdt_body *repbody;
1295 if (reqbody->valid != OBD_MD_FLID)
1298 LASSERT(!info->mti_cross_ref);
1300 /* Only got the fid of this obj by name */
1301 fid_zero(child_fid);
1302 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1305 /* XXX is raw_lookup possible as intent operation? */
1308 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1311 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1313 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1316 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1317 repbody->fid1 = *child_fid;
1318 repbody->valid = OBD_MD_FLID;
1324 * UPDATE lock should be taken against parent, and be release before exit;
1325 * child_bits lock should be taken against child, and be returned back:
1326 * (1)normal request should release the child lock;
1327 * (2)intent request will grant the lock to client.
1329 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1330 struct mdt_lock_handle *lhc,
1332 struct ldlm_reply *ldlm_rep)
1334 struct ptlrpc_request *req = mdt_info_req(info);
1335 struct mdt_body *reqbody = NULL;
1336 struct mdt_object *parent = info->mti_object;
1337 struct mdt_object *child;
1338 struct md_object *next = mdt_object_child(parent);
1339 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1340 struct lu_name *lname = NULL;
1341 struct mdt_lock_handle *lhp = NULL;
1342 struct ldlm_lock *lock;
1349 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1350 LASSERT(ergo(is_resent,
1351 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1353 LASSERT(parent != NULL);
1355 if (info->mti_cross_ref) {
1356 /* Only getattr on the child. Parent is on another node. */
1357 mdt_set_disposition(info, ldlm_rep,
1358 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1360 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1362 PFID(mdt_object_fid(child)), ldlm_rep);
1365 /* Do not take lock for resent case. */
1366 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1367 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1368 lhc->mlh_reg_lh.cookie);
1369 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1370 &lock->l_resource->lr_name));
1371 LDLM_LOCK_PUT(lock);
1373 mdt_lock_handle_init(lhc);
1374 mdt_lock_reg_init(lhc, LCK_PR);
1377 * Object's name is on another MDS, no lookup or layout
1378 * lock is needed here but update lock is.
1380 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1381 MDS_INODELOCK_LAYOUT);
1382 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1384 rc = mdt_object_lock(info, child, lhc, child_bits,
1390 /* Finally, we can get attr for child. */
1391 if (!mdt_object_exists(child)) {
1392 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1394 "remote object doesn't exist.\n");
1395 mdt_object_unlock(info, child, lhc, 1);
1399 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1400 rc = mdt_getattr_internal(info, child, 0);
1401 if (unlikely(rc != 0))
1402 mdt_object_unlock(info, child, lhc, 1);
1407 lname = &info->mti_name;
1408 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1410 if (lu_name_is_valid(lname)) {
1411 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1412 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1413 PNAME(lname), ldlm_rep);
1415 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1416 if (unlikely(reqbody == NULL))
1417 RETURN(err_serious(-EPROTO));
1419 *child_fid = reqbody->fid2;
1421 if (unlikely(!fid_is_sane(child_fid)))
1422 RETURN(err_serious(-EINVAL));
1424 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1426 PFID(mdt_object_fid(parent)),
1427 PFID(&reqbody->fid2), ldlm_rep);
1430 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1432 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1433 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1435 "Parent doesn't exist!\n");
1439 if (mdt_object_remote(parent)) {
1440 CERROR("%s: parent "DFID" is on remote target\n",
1441 mdt_obd_name(info->mti_mdt),
1442 PFID(mdt_object_fid(parent)));
1446 if (lu_name_is_valid(lname)) {
1447 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1454 /* step 1: lock parent only if parent is a directory */
1455 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1456 lhp = &info->mti_lh[MDT_LH_PARENT];
1457 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1458 rc = mdt_object_lock(info, parent, lhp,
1459 MDS_INODELOCK_UPDATE,
1461 if (unlikely(rc != 0))
1465 /* step 2: lookup child's fid by name */
1466 fid_zero(child_fid);
1467 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1470 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1473 GOTO(out_parent, rc);
1476 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1479 *step 3: find the child object by fid & lock it.
1480 * regardless if it is local or remote.
1482 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1483 * set parent dir fid the same as child fid in getattr by fid case
1484 * we should not lu_object_find() the object again, could lead
1485 * to hung if there is a concurrent unlink destroyed the object.
1487 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1488 mdt_object_get(info->mti_env, parent);
1491 child = mdt_object_find(info->mti_env, info->mti_mdt,
1495 if (unlikely(IS_ERR(child)))
1496 GOTO(out_parent, rc = PTR_ERR(child));
1498 /* Do not take lock for resent case. */
1499 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1500 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1501 lhc->mlh_reg_lh.cookie);
1503 if (!fid_res_name_eq(mdt_object_fid(child),
1504 &lock->l_resource->lr_name)) {
1505 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1506 &lock->l_resource->lr_name),
1507 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1508 PLDLMRES(lock->l_resource),
1509 PFID(mdt_object_fid(parent)));
1510 CWARN("Although resent, but still not get child lock"
1511 "parent:"DFID" child:"DFID"\n",
1512 PFID(mdt_object_fid(parent)),
1513 PFID(mdt_object_fid(child)));
1514 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1515 LDLM_LOCK_PUT(lock);
1518 LDLM_LOCK_PUT(lock);
1521 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1522 mdt_lock_handle_init(lhc);
1523 mdt_lock_reg_init(lhc, LCK_PR);
1526 if (!mdt_object_exists(child)) {
1527 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1529 "Object doesn't exist!\n");
1530 GOTO(out_child, rc = -ENOENT);
1533 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1534 mdt_object_exists(child) && !mdt_object_remote(child)) {
1535 struct md_attr *ma = &info->mti_attr;
1538 ma->ma_need = MA_INODE;
1539 rc = mdt_attr_get_complex(info, child, ma);
1540 if (unlikely(rc != 0))
1541 GOTO(out_child, rc);
1543 /* If the file has not been changed for some time, we
1544 * return not only a LOOKUP lock, but also an UPDATE
1545 * lock and this might save us RPC on later STAT. For
1546 * directories, it also let negative dentry cache start
1547 * working for this dir. */
1548 if (ma->ma_valid & MA_INODE &&
1549 ma->ma_attr.la_valid & LA_CTIME &&
1550 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1551 ma->ma_attr.la_ctime < cfs_time_current_sec())
1552 child_bits |= MDS_INODELOCK_UPDATE;
1555 /* layout lock must be granted in a best-effort way
1556 * for IT operations */
1557 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1558 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1559 exp_connect_layout(info->mti_exp) &&
1560 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1561 !mdt_object_remote(child) && ldlm_rep != NULL) {
1562 /* try to grant layout lock for regular file. */
1568 child_bits |= MDS_INODELOCK_LAYOUT;
1569 /* try layout lock, it may fail to be granted due to
1570 * contention at LOOKUP or UPDATE */
1571 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1573 child_bits &= ~MDS_INODELOCK_LAYOUT;
1574 LASSERT(child_bits != 0);
1575 rc = mdt_object_lock(info, child, lhc,
1576 child_bits, MDT_CROSS_LOCK);
1581 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1582 * client will enqueue the lock to the remote MDT */
1583 if (mdt_object_remote(child))
1584 child_bits &= ~MDS_INODELOCK_UPDATE;
1585 rc = mdt_object_lock(info, child, lhc, child_bits,
1588 if (unlikely(rc != 0))
1589 GOTO(out_child, rc);
1592 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1593 /* Get MA_SOM attributes if update lock is given. */
1595 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1596 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1599 /* finally, we can get attr for child. */
1600 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1601 rc = mdt_getattr_internal(info, child, ma_need);
1602 if (unlikely(rc != 0)) {
1603 mdt_object_unlock(info, child, lhc, 1);
1605 /* Debugging code. */
1606 LDLM_DEBUG(lock, "Returning lock to client");
1607 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1608 &lock->l_resource->lr_name),
1609 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1610 PLDLMRES(lock->l_resource),
1611 PFID(mdt_object_fid(child)));
1612 if (mdt_object_exists(child) && !mdt_object_remote(child))
1613 mdt_pack_size2body(info, child);
1616 LDLM_LOCK_PUT(lock);
1620 mdt_object_put(info->mti_env, child);
1623 mdt_object_unlock(info, parent, lhp, 1);
1627 /* normal handler: should release the child lock */
1628 static int mdt_getattr_name(struct tgt_session_info *tsi)
1630 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1631 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1632 struct mdt_body *reqbody;
1633 struct mdt_body *repbody;
1637 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1638 LASSERT(reqbody != NULL);
1639 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1640 LASSERT(repbody != NULL);
1642 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1643 repbody->eadatasize = 0;
1644 repbody->aclsize = 0;
1646 rc = mdt_init_ucred(info, reqbody);
1648 GOTO(out_shrink, rc);
1650 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1651 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1652 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1653 lhc->mlh_reg_lh.cookie = 0;
1655 mdt_exit_ucred(info);
1658 mdt_client_compatibility(info);
1659 rc2 = mdt_fix_reply(info);
1662 mdt_thread_info_fini(info);
1666 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1667 void *karg, void *uarg);
1669 static int mdt_set_info(struct tgt_session_info *tsi)
1671 struct ptlrpc_request *req = tgt_ses_req(tsi);
1674 int keylen, vallen, rc = 0;
1678 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1680 DEBUG_REQ(D_HA, req, "no set_info key");
1681 RETURN(err_serious(-EFAULT));
1684 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1687 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1689 DEBUG_REQ(D_HA, req, "no set_info val");
1690 RETURN(err_serious(-EFAULT));
1693 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1696 /* Swab any part of val you need to here */
1697 if (KEY_IS(KEY_READ_ONLY)) {
1698 spin_lock(&req->rq_export->exp_lock);
1700 *exp_connect_flags_ptr(req->rq_export) |=
1703 *exp_connect_flags_ptr(req->rq_export) &=
1704 ~OBD_CONNECT_RDONLY;
1705 spin_unlock(&req->rq_export->exp_lock);
1706 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1707 struct changelog_setinfo *cs = val;
1709 if (vallen != sizeof(*cs)) {
1710 CERROR("%s: bad changelog_clear setinfo size %d\n",
1711 tgt_name(tsi->tsi_tgt), vallen);
1714 if (ptlrpc_req_need_swab(req)) {
1715 __swab64s(&cs->cs_recno);
1716 __swab32s(&cs->cs_id);
1719 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1727 static int mdt_readpage(struct tgt_session_info *tsi)
1729 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1730 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1731 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1732 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1733 struct mdt_body *repbody;
1739 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1740 RETURN(err_serious(-ENOMEM));
1742 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1743 if (repbody == NULL || reqbody == NULL)
1744 RETURN(err_serious(-EFAULT));
1747 * prepare @rdpg before calling lower layers and transfer itself. Here
1748 * reqbody->size contains offset of where to start to read and
1749 * reqbody->nlink contains number bytes to read.
1751 rdpg->rp_hash = reqbody->size;
1752 if (rdpg->rp_hash != reqbody->size) {
1753 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1754 rdpg->rp_hash, reqbody->size);
1758 rdpg->rp_attrs = reqbody->mode;
1759 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1760 rdpg->rp_attrs |= LUDA_64BITHASH;
1761 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1762 exp_max_brw_size(tsi->tsi_exp));
1763 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1765 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1766 if (rdpg->rp_pages == NULL)
1769 for (i = 0; i < rdpg->rp_npages; ++i) {
1770 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1771 if (rdpg->rp_pages[i] == NULL)
1772 GOTO(free_rdpg, rc = -ENOMEM);
1775 /* call lower layers to fill allocated pages with directory data */
1776 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1778 GOTO(free_rdpg, rc);
1780 /* send pages to client */
1781 rc = tgt_sendpage(tsi, rdpg, rc);
1786 for (i = 0; i < rdpg->rp_npages; i++)
1787 if (rdpg->rp_pages[i] != NULL)
1788 __free_page(rdpg->rp_pages[i]);
1789 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1791 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1797 static int mdt_reint_internal(struct mdt_thread_info *info,
1798 struct mdt_lock_handle *lhc,
1801 struct req_capsule *pill = info->mti_pill;
1802 struct mdt_body *repbody;
1807 rc = mdt_reint_unpack(info, op);
1809 CERROR("Can't unpack reint, rc %d\n", rc);
1810 RETURN(err_serious(rc));
1813 /* for replay (no_create) lmm is not needed, client has it already */
1814 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1815 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1818 /* llog cookies are always 0, the field is kept for compatibility */
1819 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1820 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1822 rc = req_capsule_server_pack(pill);
1824 CERROR("Can't pack response, rc %d\n", rc);
1825 RETURN(err_serious(rc));
1828 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1829 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1831 repbody->eadatasize = 0;
1832 repbody->aclsize = 0;
1835 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1837 /* for replay no cookkie / lmm need, because client have this already */
1838 if (info->mti_spec.no_create)
1839 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1840 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1842 rc = mdt_init_ucred_reint(info);
1844 GOTO(out_shrink, rc);
1846 rc = mdt_fix_attr_ucred(info, op);
1848 GOTO(out_ucred, rc = err_serious(rc));
1850 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1851 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1852 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1853 GOTO(out_ucred, rc);
1855 rc = mdt_reint_rec(info, lhc);
1858 mdt_exit_ucred(info);
1860 mdt_client_compatibility(info);
1861 rc2 = mdt_fix_reply(info);
1867 static long mdt_reint_opcode(struct ptlrpc_request *req,
1868 const struct req_format **fmt)
1870 struct mdt_device *mdt;
1871 struct mdt_rec_reint *rec;
1874 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1876 opc = rec->rr_opcode;
1877 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1878 if (opc < REINT_MAX && fmt[opc] != NULL)
1879 req_capsule_extend(&req->rq_pill, fmt[opc]);
1881 mdt = mdt_exp2dev(req->rq_export);
1882 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1883 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1884 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1885 opc = err_serious(-EFAULT);
1888 opc = err_serious(-EFAULT);
1893 static int mdt_reint(struct tgt_session_info *tsi)
1897 static const struct req_format *reint_fmts[REINT_MAX] = {
1898 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1899 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1900 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1901 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1902 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1903 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1904 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1905 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1906 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1911 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1913 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1915 * No lock possible here from client to pass it to reint code
1918 rc = mdt_reint_internal(info, NULL, opc);
1919 mdt_thread_info_fini(info);
1924 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1928 /* this should sync the whole device */
1929 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1931 struct dt_device *dt = mdt->mdt_bottom;
1935 rc = dt->dd_ops->dt_sync(env, dt);
1939 /* this should sync this object */
1940 static int mdt_object_sync(struct mdt_thread_info *info)
1942 struct md_object *next;
1946 if (!mdt_object_exists(info->mti_object)) {
1947 CWARN("Non existing object "DFID"!\n",
1948 PFID(mdt_object_fid(info->mti_object)));
1951 next = mdt_object_child(info->mti_object);
1952 rc = mo_object_sync(info->mti_env, next);
1957 static int mdt_sync(struct tgt_session_info *tsi)
1959 struct ptlrpc_request *req = tgt_ses_req(tsi);
1960 struct req_capsule *pill = tsi->tsi_pill;
1961 struct mdt_body *body;
1966 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1967 RETURN(err_serious(-ENOMEM));
1969 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1970 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1972 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1974 /* sync an object */
1975 rc = mdt_object_sync(info);
1977 const struct lu_fid *fid;
1978 struct lu_attr *la = &info->mti_attr.ma_attr;
1980 info->mti_attr.ma_need = MA_INODE;
1981 info->mti_attr.ma_valid = 0;
1982 rc = mdt_attr_get_complex(info, info->mti_object,
1985 body = req_capsule_server_get(pill,
1987 fid = mdt_object_fid(info->mti_object);
1988 mdt_pack_attr2body(info, body, la, fid);
1991 mdt_thread_info_fini(info);
1994 mdt_counter_incr(req, LPROC_MDT_SYNC);
2000 * Handle quota control requests to consult current usage/limit, but also
2001 * to configure quota enforcement
2003 static int mdt_quotactl(struct tgt_session_info *tsi)
2005 struct obd_export *exp = tsi->tsi_exp;
2006 struct req_capsule *pill = tsi->tsi_pill;
2007 struct obd_quotactl *oqctl, *repoqc;
2009 struct mdt_device *mdt = mdt_exp2dev(exp);
2010 struct lu_device *qmt = mdt->mdt_qmt_dev;
2013 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2015 RETURN(err_serious(-EPROTO));
2017 rc = req_capsule_server_pack(pill);
2019 RETURN(err_serious(rc));
2021 switch (oqctl->qc_cmd) {
2023 case LUSTRE_Q_INVALIDATE:
2024 case LUSTRE_Q_FINVALIDATE:
2028 /* deprecated, not used any more */
2029 RETURN(-EOPNOTSUPP);
2030 /* master quotactl */
2036 RETURN(-EOPNOTSUPP);
2037 /* slave quotactl */
2042 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2046 /* map uid/gid for remote client */
2048 if (exp_connect_rmtclient(exp)) {
2049 struct lustre_idmap_table *idmap;
2051 idmap = exp->exp_mdt_data.med_idmap;
2053 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2054 oqctl->qc_cmd != Q_GETINFO))
2057 if (oqctl->qc_type == USRQUOTA)
2058 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2060 else if (oqctl->qc_type == GRPQUOTA)
2061 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2066 if (id == CFS_IDMAP_NOTFOUND) {
2067 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2072 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2074 RETURN(err_serious(-EFAULT));
2076 if (oqctl->qc_id != id)
2077 swap(oqctl->qc_id, id);
2079 switch (oqctl->qc_cmd) {
2085 /* forward quotactl request to QMT */
2086 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2091 /* slave quotactl */
2092 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2097 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2101 if (oqctl->qc_id != id)
2102 swap(oqctl->qc_id, id);
2108 /** clone llog ctxt from child (mdd)
2109 * This allows remote llog (replicator) access.
2110 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2111 * context was originally set up, or we can handle them directly.
2112 * I choose the latter, but that means I need any llog
2113 * contexts set up by child to be accessable by the mdt. So we clone the
2114 * context into our context list here.
2116 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2119 struct md_device *next = mdt->mdt_child;
2120 struct llog_ctxt *ctxt;
2123 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2126 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2127 if (rc || ctxt == NULL) {
2131 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2133 CERROR("Can't set mdt ctxt %d\n", rc);
2138 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2139 struct mdt_device *mdt, int idx)
2141 struct llog_ctxt *ctxt;
2143 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2146 /* Put once for the get we just did, and once for the clone */
2147 llog_ctxt_put(ctxt);
2148 llog_ctxt_put(ctxt);
2153 * sec context handlers
2155 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2159 rc = mdt_handle_idmap(tsi);
2161 struct ptlrpc_request *req = tgt_ses_req(tsi);
2164 opc = lustre_msg_get_opc(req->rq_reqmsg);
2165 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2166 sptlrpc_svc_ctx_invalidate(req);
2169 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2175 * quota request handlers
2177 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2179 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2180 struct lu_device *qmt = mdt->mdt_qmt_dev;
2185 RETURN(err_serious(-EOPNOTSUPP));
2187 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2191 struct mdt_object *mdt_object_new(const struct lu_env *env,
2192 struct mdt_device *d,
2193 const struct lu_fid *f)
2195 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2196 struct lu_object *o;
2197 struct mdt_object *m;
2200 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2201 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2202 if (unlikely(IS_ERR(o)))
2203 m = (struct mdt_object *)o;
2209 struct mdt_object *mdt_object_find(const struct lu_env *env,
2210 struct mdt_device *d,
2211 const struct lu_fid *f)
2213 struct lu_object *o;
2214 struct mdt_object *m;
2217 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2218 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2219 if (unlikely(IS_ERR(o)))
2220 m = (struct mdt_object *)o;
2228 * Asyncronous commit for mdt device.
2230 * Pass asynchonous commit call down the MDS stack.
2232 * \param env environment
2233 * \param mdt the mdt device
2235 static void mdt_device_commit_async(const struct lu_env *env,
2236 struct mdt_device *mdt)
2238 struct dt_device *dt = mdt->mdt_bottom;
2241 rc = dt->dd_ops->dt_commit_async(env, dt);
2242 if (unlikely(rc != 0))
2243 CWARN("async commit start failed with rc = %d", rc);
2247 * Mark the lock as "synchonous".
2249 * Mark the lock to deffer transaction commit to the unlock time.
2251 * \param lock the lock to mark as "synchonous"
2253 * \see mdt_is_lock_sync
2254 * \see mdt_save_lock
2256 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2258 lock->l_ast_data = (void*)1;
2262 * Check whehter the lock "synchonous" or not.
2264 * \param lock the lock to check
2265 * \retval 1 the lock is "synchonous"
2266 * \retval 0 the lock isn't "synchronous"
2268 * \see mdt_set_lock_sync
2269 * \see mdt_save_lock
2271 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2273 return lock->l_ast_data != NULL;
2277 * Blocking AST for mdt locks.
2279 * Starts transaction commit if in case of COS lock conflict or
2280 * deffers such a commit to the mdt_save_lock.
2282 * \param lock the lock which blocks a request or cancelling lock
2283 * \param desc unused
2284 * \param data unused
2285 * \param flag indicates whether this cancelling or blocking callback
2287 * \see ldlm_blocking_ast_nocheck
2289 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2290 void *data, int flag)
2292 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2293 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2297 if (flag == LDLM_CB_CANCELING)
2299 lock_res_and_lock(lock);
2300 if (lock->l_blocking_ast != mdt_blocking_ast) {
2301 unlock_res_and_lock(lock);
2304 if (mdt_cos_is_enabled(mdt) &&
2305 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2306 lock->l_blocking_lock != NULL &&
2307 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2308 mdt_set_lock_sync(lock);
2310 rc = ldlm_blocking_ast_nocheck(lock);
2312 /* There is no lock conflict if l_blocking_lock == NULL,
2313 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2314 * when the last reference to a local lock was released */
2315 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2318 rc = lu_env_init(&env, LCT_LOCAL);
2319 if (unlikely(rc != 0))
2320 CWARN("lu_env initialization failed with rc = %d,"
2321 "cannot start asynchronous commit\n", rc);
2323 mdt_device_commit_async(&env, mdt);
2329 /* Used for cross-MDT lock */
2330 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2331 void *data, int flag)
2333 struct lustre_handle lockh;
2337 case LDLM_CB_BLOCKING:
2338 ldlm_lock2handle(lock, &lockh);
2339 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2341 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2345 case LDLM_CB_CANCELING:
2346 LDLM_DEBUG(lock, "Revoke remote lock\n");
2354 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2355 struct mdt_object *o, const struct lu_fid *fid,
2356 struct lustre_handle *lh, ldlm_mode_t mode,
2359 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2360 ldlm_policy_data_t *policy = &mti->mti_policy;
2361 struct ldlm_res_id *res_id = &mti->mti_res_id;
2365 LASSERT(mdt_object_remote(o));
2367 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2369 fid_build_reg_res_name(fid, res_id);
2371 memset(einfo, 0, sizeof(*einfo));
2372 einfo->ei_type = LDLM_IBITS;
2373 einfo->ei_mode = mode;
2374 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2375 einfo->ei_cb_cp = ldlm_completion_ast;
2376 einfo->ei_enq_slave = 0;
2377 einfo->ei_res_id = res_id;
2379 memset(policy, 0, sizeof(*policy));
2380 policy->l_inodebits.bits = ibits;
2382 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2387 static int mdt_object_local_lock(struct mdt_thread_info *info,
2388 struct mdt_object *o,
2389 struct mdt_lock_handle *lh, __u64 ibits,
2390 bool nonblock, int locality)
2392 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2393 ldlm_policy_data_t *policy = &info->mti_policy;
2394 struct ldlm_res_id *res_id = &info->mti_res_id;
2399 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2400 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2401 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2402 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2404 /* Only enqueue LOOKUP lock for remote object */
2405 if (mdt_object_remote(o))
2406 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2408 if (lh->mlh_type == MDT_PDO_LOCK) {
2409 /* check for exists after object is locked */
2410 if (mdt_object_exists(o) == 0) {
2411 /* Non-existent object shouldn't have PDO lock */
2414 /* Non-dir object shouldn't have PDO lock */
2415 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2420 memset(policy, 0, sizeof(*policy));
2421 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2423 dlmflags = LDLM_FL_ATOMIC_CB;
2425 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2428 * Take PDO lock on whole directory and build correct @res_id for lock
2429 * on part of directory.
2431 if (lh->mlh_pdo_hash != 0) {
2432 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2433 mdt_lock_pdo_mode(info, o, lh);
2434 if (lh->mlh_pdo_mode != LCK_NL) {
2436 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2437 * is never going to be sent to client and we do not
2438 * want it slowed down due to possible cancels.
2440 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2441 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2442 policy, res_id, dlmflags,
2443 info->mti_exp == NULL ? NULL :
2444 &info->mti_exp->exp_handle.h_cookie);
2450 * Finish res_id initializing by name hash marking part of
2451 * directory which is taking modification.
2453 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2456 policy->l_inodebits.bits = ibits;
2459 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2460 * going to be sent to client. If it is - mdt_intent_policy() path will
2461 * fix it up and turn FL_LOCAL flag off.
2463 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2464 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2465 info->mti_exp == NULL ? NULL :
2466 &info->mti_exp->exp_handle.h_cookie);
2468 mdt_object_unlock(info, o, lh, 1);
2469 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2470 lh->mlh_pdo_hash != 0 &&
2471 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2472 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2479 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2480 struct mdt_lock_handle *lh, __u64 ibits,
2481 bool nonblock, int locality)
2486 if (!mdt_object_remote(o))
2487 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2490 if (locality == MDT_LOCAL_LOCK) {
2491 CERROR("%s: try to get local lock for remote object"
2492 DFID".\n", mdt_obd_name(info->mti_mdt),
2493 PFID(mdt_object_fid(o)));
2497 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2498 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2499 MDS_INODELOCK_XATTR);
2500 if (ibits & MDS_INODELOCK_UPDATE) {
2501 /* Sigh, PDO needs to enqueue 2 locks right now, but
2502 * enqueue RPC can only request 1 lock, to avoid extra
2503 * RPC, so it will instead enqueue EX lock for remote
2504 * object anyway XXX*/
2505 if (lh->mlh_type == MDT_PDO_LOCK &&
2506 lh->mlh_pdo_hash != 0) {
2507 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2508 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2509 PFID(mdt_object_fid(o)));
2510 lh->mlh_pdo_hash = 0;
2511 lh->mlh_rreg_mode = LCK_EX;
2512 lh->mlh_type = MDT_REG_LOCK;
2514 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2517 MDS_INODELOCK_UPDATE);
2522 /* Only enqueue LOOKUP lock for remote object */
2523 if (ibits & MDS_INODELOCK_LOOKUP) {
2524 rc = mdt_object_local_lock(info, o, lh,
2525 MDS_INODELOCK_LOOKUP,
2526 nonblock, locality);
2534 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2535 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2537 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2540 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2541 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2543 struct mdt_lock_handle tmp = *lh;
2546 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2554 * Save a lock within request object.
2556 * Keep the lock referenced until whether client ACK or transaction
2557 * commit happens or release the lock immediately depending on input
2558 * parameters. If COS is ON, a write lock is converted to COS lock
2561 * \param info thead info object
2562 * \param h lock handle
2563 * \param mode lock mode
2564 * \param decref force immediate lock releasing
2567 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2568 ldlm_mode_t mode, int decref)
2572 if (lustre_handle_is_used(h)) {
2573 if (decref || !info->mti_has_trans ||
2574 !(mode & (LCK_PW | LCK_EX))){
2575 mdt_fid_unlock(h, mode);
2577 struct mdt_device *mdt = info->mti_mdt;
2578 struct ldlm_lock *lock = ldlm_handle2lock(h);
2579 struct ptlrpc_request *req = mdt_info_req(info);
2582 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2584 /* there is no request if mdt_object_unlock() is called
2585 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2586 if (likely(req != NULL)) {
2587 CDEBUG(D_HA, "request = %p reply state = %p"
2588 " transno = "LPD64"\n", req,
2589 req->rq_reply_state, req->rq_transno);
2590 if (mdt_cos_is_enabled(mdt)) {
2592 ldlm_lock_downgrade(lock, LCK_COS);
2595 ptlrpc_save_lock(req, h, mode, no_ack);
2597 ldlm_lock_decref(h, mode);
2599 if (mdt_is_lock_sync(lock)) {
2600 CDEBUG(D_HA, "found sync-lock,"
2601 " async commit started\n");
2602 mdt_device_commit_async(info->mti_env,
2605 LDLM_LOCK_PUT(lock);
2614 * Unlock mdt object.
2616 * Immeditely release the regular lock and the PDO lock or save the
2617 * lock in reqeuest and keep them referenced until client ACK or
2618 * transaction commit.
2620 * \param info thread info object
2621 * \param o mdt object
2622 * \param lh mdt lock handle referencing regular and PDO locks
2623 * \param decref force immediate lock releasing
2625 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2626 struct mdt_lock_handle *lh, int decref)
2630 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2631 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2633 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2634 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2639 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2640 const struct lu_fid *f,
2641 struct mdt_lock_handle *lh,
2644 struct mdt_object *o;
2646 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2650 rc = mdt_object_lock(info, o, lh, ibits,
2653 mdt_object_put(info->mti_env, o);
2660 void mdt_object_unlock_put(struct mdt_thread_info * info,
2661 struct mdt_object * o,
2662 struct mdt_lock_handle *lh,
2665 mdt_object_unlock(info, o, lh, decref);
2666 mdt_object_put(info->mti_env, o);
2670 * Generic code handling requests that have struct mdt_body passed in:
2672 * - extract mdt_body from request and save it in @info, if present;
2674 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2677 * - if HABEO_CORPUS flag is set for this request type check whether object
2678 * actually exists on storage (lu_object_exists()).
2681 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2683 const struct mdt_body *body;
2684 struct mdt_object *obj;
2685 const struct lu_env *env;
2686 struct req_capsule *pill;
2690 env = info->mti_env;
2691 pill = info->mti_pill;
2693 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2697 if (!(body->valid & OBD_MD_FLID))
2700 if (!fid_is_sane(&body->fid1)) {
2701 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2706 * Do not get size or any capa fields before we check that request
2707 * contains capa actually. There are some requests which do not, for
2708 * instance MDS_IS_SUBDIR.
2710 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2711 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2712 mdt_set_capainfo(info, 0, &body->fid1,
2713 req_capsule_client_get(pill, &RMF_CAPA1));
2715 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2717 if ((flags & HABEO_CORPUS) &&
2718 !mdt_object_exists(obj)) {
2719 mdt_object_put(env, obj);
2720 /* for capability renew ENOENT will be handled in
2722 if (body->valid & OBD_MD_FLOSSCAPA)
2727 info->mti_object = obj;
2736 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2738 struct req_capsule *pill = info->mti_pill;
2742 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2743 rc = mdt_body_unpack(info, flags);
2747 if (rc == 0 && (flags & HABEO_REFERO)) {
2749 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2750 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2752 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2753 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2756 rc = req_capsule_server_pack(pill);
2761 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2763 struct md_device *next = m->mdt_child;
2765 return next->md_ops->mdo_init_capa_ctxt(env, next,
2766 m->mdt_lut.lut_mds_capa,
2767 m->mdt_capa_timeout,
2772 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2774 lh->mlh_type = MDT_NUL_LOCK;
2775 lh->mlh_reg_lh.cookie = 0ull;
2776 lh->mlh_reg_mode = LCK_MINMODE;
2777 lh->mlh_pdo_lh.cookie = 0ull;
2778 lh->mlh_pdo_mode = LCK_MINMODE;
2779 lh->mlh_rreg_lh.cookie = 0ull;
2780 lh->mlh_rreg_mode = LCK_MINMODE;
2783 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2785 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2786 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2790 * Initialize fields of struct mdt_thread_info. Other fields are left in
2791 * uninitialized state, because it's too expensive to zero out whole
2792 * mdt_thread_info (> 1K) on each request arrival.
2794 void mdt_thread_info_init(struct ptlrpc_request *req,
2795 struct mdt_thread_info *info)
2799 info->mti_pill = &req->rq_pill;
2802 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2803 mdt_lock_handle_init(&info->mti_lh[i]);
2805 /* mdt device: it can be NULL while CONNECT */
2806 if (req->rq_export) {
2807 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2808 info->mti_exp = req->rq_export;
2810 info->mti_mdt = NULL;
2811 info->mti_env = req->rq_svc_thread->t_env;
2812 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2814 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2815 info->mti_big_buf = LU_BUF_NULL;
2816 info->mti_body = NULL;
2817 info->mti_object = NULL;
2818 info->mti_dlm_req = NULL;
2819 info->mti_has_trans = 0;
2820 info->mti_cross_ref = 0;
2821 info->mti_opdata = 0;
2822 info->mti_big_lmm_used = 0;
2824 info->mti_spec.no_create = 0;
2825 info->mti_spec.sp_rm_entry = 0;
2827 info->mti_spec.u.sp_ea.eadata = NULL;
2828 info->mti_spec.u.sp_ea.eadatalen = 0;
2831 void mdt_thread_info_fini(struct mdt_thread_info *info)
2835 if (info->mti_object != NULL) {
2836 mdt_object_put(info->mti_env, info->mti_object);
2837 info->mti_object = NULL;
2840 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2841 mdt_lock_handle_fini(&info->mti_lh[i]);
2842 info->mti_env = NULL;
2843 info->mti_pill = NULL;
2844 info->mti_exp = NULL;
2846 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2847 lu_buf_free(&info->mti_big_buf);
2850 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2852 struct mdt_thread_info *mti;
2853 struct lustre_capa *lc;
2855 mti = mdt_th_info(tsi->tsi_env);
2856 LASSERT(mti != NULL);
2858 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2859 if (tsi->tsi_corpus != NULL) {
2860 struct req_capsule *pill = tsi->tsi_pill;
2862 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2863 lu_object_get(tsi->tsi_corpus);
2866 * XXX: must be part of tgt_mdt_body_unpack but moved here
2867 * due to mdt_set_capainfo().
2869 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2870 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2871 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2872 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2875 mti->mti_body = tsi->tsi_mdt_body;
2876 mti->mti_dlm_req = tsi->tsi_dlm_req;
2881 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2883 struct ptlrpc_request *req = tgt_ses_req(tsi);
2888 rc = tgt_connect(tsi);
2892 rc = mdt_init_idmap(tsi);
2897 obd_disconnect(class_export_get(req->rq_export));
2916 static int mdt_intent_getattr(enum mdt_it_code opcode,
2917 struct mdt_thread_info *info,
2918 struct ldlm_lock **,
2921 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2922 struct mdt_thread_info *info,
2923 struct ldlm_lock **lockp,
2926 static int mdt_intent_layout(enum mdt_it_code opcode,
2927 struct mdt_thread_info *info,
2928 struct ldlm_lock **,
2930 static int mdt_intent_reint(enum mdt_it_code opcode,
2931 struct mdt_thread_info *info,
2932 struct ldlm_lock **,
2935 static struct mdt_it_flavor {
2936 const struct req_format *it_fmt;
2938 int (*it_act)(enum mdt_it_code ,
2939 struct mdt_thread_info *,
2940 struct ldlm_lock **,
2943 } mdt_it_flavor[] = {
2945 .it_fmt = &RQF_LDLM_INTENT,
2946 /*.it_flags = HABEO_REFERO,*/
2948 .it_act = mdt_intent_reint,
2949 .it_reint = REINT_OPEN
2952 .it_fmt = &RQF_LDLM_INTENT,
2954 * OCREAT is not a MUTABOR request as if the file
2956 * We do the extra check of OBD_CONNECT_RDONLY in
2957 * mdt_reint_open() when we really need to create
2961 .it_act = mdt_intent_reint,
2962 .it_reint = REINT_OPEN
2965 .it_fmt = &RQF_LDLM_INTENT,
2966 .it_flags = MUTABOR,
2967 .it_act = mdt_intent_reint,
2968 .it_reint = REINT_CREATE
2970 [MDT_IT_GETATTR] = {
2971 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2972 .it_flags = HABEO_REFERO,
2973 .it_act = mdt_intent_getattr
2975 [MDT_IT_READDIR] = {
2981 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2982 .it_flags = HABEO_REFERO,
2983 .it_act = mdt_intent_getattr
2986 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2987 .it_flags = MUTABOR,
2989 .it_reint = REINT_UNLINK
2993 .it_flags = MUTABOR,
2996 [MDT_IT_GETXATTR] = {
2997 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2998 .it_flags = HABEO_CORPUS,
2999 .it_act = mdt_intent_getxattr
3002 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3004 .it_act = mdt_intent_layout
3009 mdt_intent_lock_replace(struct mdt_thread_info *info, struct ldlm_lock **lockp,
3010 struct ldlm_lock *new_lock, struct mdt_lock_handle *lh,
3013 struct ptlrpc_request *req = mdt_info_req(info);
3014 struct ldlm_lock *lock = *lockp;
3017 * Get new lock only for cases when possible resent did not find any
3020 if (new_lock == NULL)
3021 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3023 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3024 lh->mlh_reg_lh.cookie = 0;
3028 LASSERTF(new_lock != NULL,
3029 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3032 * If we've already given this lock to a client once, then we should
3033 * have no readers or writers. Otherwise, we should have one reader
3034 * _or_ writer ref (which will be zeroed below) before returning the
3037 if (new_lock->l_export == req->rq_export) {
3038 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3040 LASSERT(new_lock->l_export == NULL);
3041 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3046 if (new_lock->l_export == req->rq_export) {
3048 * Already gave this to the client, which means that we
3049 * reconstructed a reply.
3051 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3053 lh->mlh_reg_lh.cookie = 0;
3054 RETURN(ELDLM_LOCK_REPLACED);
3058 * Fixup the lock to be given to the client.
3060 lock_res_and_lock(new_lock);
3061 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3062 * possible blocking AST. */
3063 while (new_lock->l_readers > 0) {
3064 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3065 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3066 new_lock->l_readers--;
3068 while (new_lock->l_writers > 0) {
3069 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3070 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3071 new_lock->l_writers--;
3074 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3075 new_lock->l_blocking_ast = lock->l_blocking_ast;
3076 new_lock->l_completion_ast = lock->l_completion_ast;
3077 new_lock->l_remote_handle = lock->l_remote_handle;
3078 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3080 unlock_res_and_lock(new_lock);
3082 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3083 &new_lock->l_remote_handle,
3084 &new_lock->l_exp_hash);
3086 LDLM_LOCK_RELEASE(new_lock);
3087 lh->mlh_reg_lh.cookie = 0;
3089 RETURN(ELDLM_LOCK_REPLACED);
3092 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3093 struct ldlm_lock *new_lock,
3094 struct ldlm_lock **old_lock,
3095 struct mdt_lock_handle *lh,
3096 enum mdt_it_code opcode)
3098 struct ptlrpc_request *req = mdt_info_req(info);
3099 struct obd_export *exp = req->rq_export;
3100 struct lustre_handle remote_hdl;
3101 struct ldlm_request *dlmreq;
3102 struct ldlm_lock *lock;
3104 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3107 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3108 remote_hdl = dlmreq->lock_handle[0];
3109 /* If the client does not require open lock, it does not need to
3110 * search lock in exp_lock_hash, since the server thread will
3111 * make sure the lock will be released, and the resend request
3112 * can always re-enqueue the lock */
3113 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3114 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3115 /* In the function below, .hs_keycmp resolves to
3116 * ldlm_export_lock_keycmp() */
3117 /* coverity[overrun-buffer-val] */
3118 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3120 lock_res_and_lock(lock);
3121 if (lock != new_lock) {
3122 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3123 lh->mlh_reg_mode = lock->l_granted_mode;
3125 LDLM_DEBUG(lock, "Restoring lock cookie");
3126 DEBUG_REQ(D_DLMTRACE, req,
3127 "restoring lock cookie "LPX64,
3128 lh->mlh_reg_lh.cookie);
3130 *old_lock = LDLM_LOCK_GET(lock);
3131 cfs_hash_put(exp->exp_lock_hash,
3133 unlock_res_and_lock(lock);
3136 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3137 unlock_res_and_lock(lock);
3141 * If the xid matches, then we know this is a resent request, and allow
3142 * it. (It's probably an OPEN, for which we don't send a lock.
3144 if (req_xid_is_last(req))
3148 * This remote handle isn't enqueued, so we never received or processed
3149 * this request. Clear MSG_RESENT, because it can be handled like any
3150 * normal request now.
3152 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3154 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3158 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3159 struct mdt_thread_info *info,
3160 struct ldlm_lock **lockp,
3163 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3164 struct ldlm_reply *ldlm_rep = NULL;
3168 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3169 * (for the resend case) or a new lock. Below we will use it to
3170 * replace the original lock.
3172 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3173 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3174 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3175 rc = mdt_object_lock(info, info->mti_object, lhc,
3176 MDS_INODELOCK_XATTR,
3182 grc = mdt_getxattr(info);
3184 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3186 if (mdt_info_req(info)->rq_repmsg != NULL)
3187 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3188 if (ldlm_rep == NULL)
3189 RETURN(err_serious(-EFAULT));
3191 ldlm_rep->lock_policy_res2 = grc;
3196 static int mdt_intent_getattr(enum mdt_it_code opcode,
3197 struct mdt_thread_info *info,
3198 struct ldlm_lock **lockp,
3201 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3202 struct ldlm_lock *new_lock = NULL;
3204 struct ldlm_reply *ldlm_rep;
3205 struct mdt_body *reqbody;
3206 struct mdt_body *repbody;
3210 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3213 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3216 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3217 repbody->eadatasize = 0;
3218 repbody->aclsize = 0;
3222 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3224 case MDT_IT_GETATTR:
3225 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3229 CERROR("Unsupported intent (%d)\n", opcode);
3230 GOTO(out_shrink, rc = -EINVAL);
3233 rc = mdt_init_ucred(info, reqbody);
3235 GOTO(out_shrink, rc);
3237 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3238 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3240 /* Get lock from request for possible resent case. */
3241 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3243 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3244 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3246 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3247 ldlm_rep->lock_policy_res2 = 0;
3248 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3249 ldlm_rep->lock_policy_res2) {
3250 lhc->mlh_reg_lh.cookie = 0ull;
3251 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3254 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3257 mdt_exit_ucred(info);
3259 mdt_client_compatibility(info);
3260 rc2 = mdt_fix_reply(info);
3266 static int mdt_intent_layout(enum mdt_it_code opcode,
3267 struct mdt_thread_info *info,
3268 struct ldlm_lock **lockp,
3271 struct layout_intent *layout;
3273 struct mdt_object *obj = NULL;
3277 if (opcode != MDT_IT_LAYOUT) {
3278 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3283 fid = &info->mti_tmp_fid2;
3284 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3286 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3288 RETURN(PTR_ERR(obj));
3290 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3291 /* get the length of lsm */
3292 rc = mdt_attr_get_eabuf_size(info, obj);
3296 if (rc > info->mti_mdt->mdt_max_mdsize)
3297 info->mti_mdt->mdt_max_mdsize = rc;
3300 mdt_object_put(info->mti_env, obj);
3302 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3303 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
3304 rc = req_capsule_server_pack(info->mti_pill);
3308 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3309 LASSERT(layout != NULL);
3310 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3311 /* return to normal ldlm handling */
3314 CERROR("%s: Unsupported layout intent (%d)\n",
3315 mdt_obd_name(info->mti_mdt), layout->li_opc);
3319 static int mdt_intent_reint(enum mdt_it_code opcode,
3320 struct mdt_thread_info *info,
3321 struct ldlm_lock **lockp,
3324 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3325 struct ldlm_reply *rep = NULL;
3329 static const struct req_format *intent_fmts[REINT_MAX] = {
3330 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3331 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3336 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3340 if (mdt_it_flavor[opcode].it_reint != opc) {
3341 CERROR("Reint code %ld doesn't match intent: %d\n",
3343 RETURN(err_serious(-EPROTO));
3346 /* Get lock from request for possible resent case. */
3347 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3349 rc = mdt_reint_internal(info, lhc, opc);
3351 /* Check whether the reply has been packed successfully. */
3352 if (mdt_info_req(info)->rq_repmsg != NULL)
3353 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3355 RETURN(err_serious(-EFAULT));
3357 /* MDC expects this in any case */
3359 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3361 /* the open lock or the lock for cross-ref object should be
3362 * returned to the client */
3363 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3364 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3365 rep->lock_policy_res2 = 0;
3366 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3370 rep->lock_policy_res2 = clear_serious(rc);
3372 if (rep->lock_policy_res2 == -ENOENT &&
3373 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3374 rep->lock_policy_res2 = 0;
3376 if (rc == -ENOTCONN || rc == -ENODEV ||
3377 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3379 * If it is the disconnect error (ENODEV & ENOCONN), the error
3380 * will be returned by rq_status, and client at ptlrpc layer
3381 * will detect this, then disconnect, reconnect the import
3382 * immediately, instead of impacting the following the rpc.
3384 lhc->mlh_reg_lh.cookie = 0ull;
3388 * For other cases, the error will be returned by intent.
3389 * and client will retrieve the result from intent.
3392 * FIXME: when open lock is finished, that should be
3395 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3396 LASSERTF(rc == 0, "Error occurred but lock handle "
3397 "is still in use, rc = %d\n", rc);
3398 rep->lock_policy_res2 = 0;
3399 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3402 lhc->mlh_reg_lh.cookie = 0ull;
3403 RETURN(ELDLM_LOCK_ABORTED);
3408 static int mdt_intent_code(long itcode)
3416 case IT_OPEN|IT_CREAT:
3423 rc = MDT_IT_READDIR;
3426 rc = MDT_IT_GETATTR;
3438 rc = MDT_IT_GETXATTR;
3443 case IT_QUOTA_DQACQ:
3448 CERROR("Unknown intent opcode: %ld\n", itcode);
3455 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3456 struct ldlm_lock **lockp, __u64 flags)
3458 struct req_capsule *pill;
3459 struct mdt_it_flavor *flv;
3464 opc = mdt_intent_code(itopc);
3468 pill = info->mti_pill;
3470 if (opc == MDT_IT_QUOTA) {
3471 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3474 RETURN(-EOPNOTSUPP);
3476 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3477 /* pass the request to quota master */
3478 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3479 mdt_info_req(info), lockp,
3484 flv = &mdt_it_flavor[opc];
3485 if (flv->it_fmt != NULL)
3486 req_capsule_extend(pill, flv->it_fmt);
3488 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3490 struct ptlrpc_request *req = mdt_info_req(info);
3491 if (flv->it_flags & MUTABOR &&
3492 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3495 if (rc == 0 && flv->it_act != NULL) {
3496 struct ldlm_reply *rep;
3498 /* execute policy */
3499 rc = flv->it_act(opc, info, lockp, flags);
3501 /* Check whether the reply has been packed successfully. */
3502 if (mdt_info_req(info)->rq_repmsg != NULL) {
3503 rep = req_capsule_server_get(info->mti_pill,
3505 rep->lock_policy_res2 =
3506 ptlrpc_status_hton(rep->lock_policy_res2);
3514 static int mdt_intent_policy(struct ldlm_namespace *ns,
3515 struct ldlm_lock **lockp, void *req_cookie,
3516 ldlm_mode_t mode, __u64 flags, void *data)
3518 struct tgt_session_info *tsi;
3519 struct mdt_thread_info *info;
3520 struct ptlrpc_request *req = req_cookie;
3521 struct ldlm_intent *it;
3522 struct req_capsule *pill;
3527 LASSERT(req != NULL);
3529 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3531 info = tsi2mdt_info(tsi);
3532 LASSERT(info != NULL);
3533 pill = info->mti_pill;
3534 LASSERT(pill->rc_req == req);
3536 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3537 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3538 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3540 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3544 /* Lock without inodebits makes no sense and will oops
3545 * later in ldlm. Let's check it now to see if we have
3546 * ibits corrupted somewhere in mdt_intent_opc().
3547 * The case for client miss to set ibits has been
3548 * processed by others. */
3549 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3550 lr_type == LDLM_IBITS,
3551 info->mti_dlm_req->lock_desc.\
3552 l_policy_data.l_inodebits.bits != 0));
3554 rc = err_serious(-EFAULT);
3556 /* No intent was provided */
3557 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3558 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3559 rc = req_capsule_server_pack(pill);
3561 rc = err_serious(rc);
3563 mdt_thread_info_fini(info);
3567 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3569 struct seq_server_site *ss = mdt_seq_site(mdt);
3571 if (ss->ss_node_id == 0)
3574 if (ss->ss_client_seq != NULL) {
3575 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3576 ss->ss_client_seq->lcs_exp = NULL;
3579 if (ss->ss_server_fld != NULL) {
3580 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3581 ss->ss_server_fld->lsf_control_exp = NULL;
3585 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3587 struct seq_server_site *ss = mdt_seq_site(mdt);
3592 if (ss->ss_server_seq != NULL)
3593 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3596 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3598 mdt_seq_fini_cli(mdt);
3599 mdt_deregister_seq_exp(mdt);
3601 return seq_site_fini(env, mdt_seq_site(mdt));
3605 * It will retrieve its FLDB entries from MDT0, and it only happens
3606 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3607 * and it needs to refresh FLDB from the MDT0.
3609 static int mdt_register_lwp_callback(void *data)
3612 struct mdt_device *mdt = data;
3613 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3617 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3619 if (!likely(fld->lsf_new))
3622 rc = lu_env_init(&env, LCT_MD_THREAD);
3624 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3628 rc = fld_update_from_controller(&env, fld);
3630 CERROR("%s: cannot update controller: rc = %d\n",
3631 mdt_obd_name(mdt), rc);
3639 static int mdt_register_seq_exp(struct mdt_device *mdt)
3641 struct seq_server_site *ss = mdt_seq_site(mdt);
3642 char *lwp_name = NULL;
3645 if (ss->ss_node_id == 0)
3648 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3649 if (lwp_name == NULL)
3650 GOTO(out_free, rc = -ENOMEM);
3652 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3656 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3661 rc = lustre_register_lwp_item(lwp_name,
3662 &ss->ss_server_fld->lsf_control_exp,
3663 mdt_register_lwp_callback, mdt);
3665 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3666 ss->ss_client_seq->lcs_exp = NULL;
3670 if (lwp_name != NULL)
3671 OBD_FREE(lwp_name, MAX_OBD_NAME);
3677 * Init client sequence manager which is used by local MDS to talk to sequence
3678 * controller on remote node.
3680 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3682 struct seq_server_site *ss = mdt_seq_site(mdt);
3687 /* check if this is adding the first MDC and controller is not yet
3689 OBD_ALLOC_PTR(ss->ss_client_seq);
3690 if (ss->ss_client_seq == NULL)
3693 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3694 if (prefix == NULL) {
3695 OBD_FREE_PTR(ss->ss_client_seq);
3696 ss->ss_client_seq = NULL;
3700 /* Note: seq_client_fini will be called in seq_site_fini */
3701 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3702 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3703 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3705 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3707 OBD_FREE_PTR(ss->ss_client_seq);
3708 ss->ss_client_seq = NULL;
3712 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3717 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3719 struct seq_server_site *ss;
3723 ss = mdt_seq_site(mdt);
3724 /* init sequence controller server(MDT0) */
3725 if (ss->ss_node_id == 0) {
3726 OBD_ALLOC_PTR(ss->ss_control_seq);
3727 if (ss->ss_control_seq == NULL)
3730 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3731 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3734 GOTO(out_seq_fini, rc);
3737 /* Init normal sequence server */
3738 OBD_ALLOC_PTR(ss->ss_server_seq);
3739 if (ss->ss_server_seq == NULL)
3740 GOTO(out_seq_fini, rc = -ENOMEM);
3742 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3743 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3745 GOTO(out_seq_fini, rc);
3747 /* init seq client for seq server to talk to seq controller(MDT0) */
3748 rc = mdt_seq_init_cli(env, mdt);
3750 GOTO(out_seq_fini, rc);
3752 if (ss->ss_node_id != 0)
3753 /* register controler export through lwp */
3754 rc = mdt_register_seq_exp(mdt);
3759 mdt_seq_fini(env, mdt);
3767 static int mdt_fld_fini(const struct lu_env *env,
3768 struct mdt_device *m)
3770 struct seq_server_site *ss = mdt_seq_site(m);
3773 if (ss && ss->ss_server_fld) {
3774 fld_server_fini(env, ss->ss_server_fld);
3775 OBD_FREE_PTR(ss->ss_server_fld);
3776 ss->ss_server_fld = NULL;
3782 static int mdt_fld_init(const struct lu_env *env,
3784 struct mdt_device *m)
3786 struct seq_server_site *ss;
3790 ss = mdt_seq_site(m);
3792 OBD_ALLOC_PTR(ss->ss_server_fld);
3793 if (ss->ss_server_fld == NULL)
3794 RETURN(rc = -ENOMEM);
3796 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3799 OBD_FREE_PTR(ss->ss_server_fld);
3800 ss->ss_server_fld = NULL;
3807 static void mdt_stack_pre_fini(const struct lu_env *env,
3808 struct mdt_device *m, struct lu_device *top)
3810 struct lustre_cfg_bufs *bufs;
3811 struct lustre_cfg *lcfg;
3812 struct mdt_thread_info *info;
3817 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3818 LASSERT(info != NULL);
3820 bufs = &info->mti_u.bufs;
3822 LASSERT(m->mdt_child_exp);
3823 LASSERT(m->mdt_child_exp->exp_obd);
3825 /* process cleanup, pass mdt obd name to get obd umount flags */
3826 /* XXX: this is needed because all layers are referenced by
3827 * objects (some of them are pinned by osd, for example *
3828 * the proper solution should be a model where object used
3829 * by osd only doesn't have mdt/mdd slices -bzzz */
3830 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3831 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3832 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3834 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3837 top->ld_ops->ldo_process_config(env, top, lcfg);
3838 lustre_cfg_free(lcfg);
3842 static void mdt_stack_fini(const struct lu_env *env,
3843 struct mdt_device *m, struct lu_device *top)
3845 struct obd_device *obd = mdt2obd_dev(m);
3846 struct lustre_cfg_bufs *bufs;
3847 struct lustre_cfg *lcfg;
3848 struct mdt_thread_info *info;
3852 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3853 LASSERT(info != NULL);
3855 lu_dev_del_linkage(top->ld_site, top);
3857 lu_site_purge(env, top->ld_site, -1);
3859 bufs = &info->mti_u.bufs;
3860 /* process cleanup, pass mdt obd name to get obd umount flags */
3861 /* another purpose is to let all layers to release their objects */
3862 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3867 lustre_cfg_bufs_set_string(bufs, 1, flags);
3868 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3870 CERROR("Cannot alloc lcfg!\n");
3874 top->ld_ops->ldo_process_config(env, top, lcfg);
3875 lustre_cfg_free(lcfg);
3877 lu_site_purge(env, top->ld_site, -1);
3879 m->mdt_child = NULL;
3880 m->mdt_bottom = NULL;
3882 obd_disconnect(m->mdt_child_exp);
3883 m->mdt_child_exp = NULL;
3885 obd_disconnect(m->mdt_bottom_exp);
3886 m->mdt_child_exp = NULL;
3889 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3890 const char *next, struct obd_export **exp)
3892 struct obd_connect_data *data = NULL;
3893 struct obd_device *obd;
3897 OBD_ALLOC_PTR(data);
3899 GOTO(out, rc = -ENOMEM);
3901 obd = class_name2obd(next);
3903 CERROR("%s: can't locate next device: %s\n",
3904 mdt_obd_name(m), next);
3905 GOTO(out, rc = -ENOTCONN);
3908 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3909 data->ocd_version = LUSTRE_VERSION_CODE;
3911 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3913 CERROR("%s: cannot connect to next dev %s (%d)\n",
3914 mdt_obd_name(m), next, rc);
3924 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3925 struct lustre_cfg *cfg)
3927 char *dev = lustre_cfg_string(cfg, 0);
3928 int rc, name_size, uuid_size;
3929 char *name, *uuid, *p;
3930 struct lustre_cfg_bufs *bufs;
3931 struct lustre_cfg *lcfg;
3932 struct obd_device *obd;
3933 struct lustre_profile *lprof;
3934 struct lu_site *site;
3937 /* in 1.8 we had the only device in the stack - MDS.
3938 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3939 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3940 * to generate names and setup MDT, MDD. MDT will be using
3941 * generated name to connect to MDD. for MDD the next device
3942 * will be LOD with name taken from so called "profile" which
3943 * is generated by mount_option line
3945 * 1.8 MGS generates config. commands like this:
3946 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3947 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3948 * 2.0 MGS generates config. commands like this:
3949 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3950 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3951 * 3:lustre-MDT0000-mdtlov 4:f
3953 * we generate MDD name from MDT one, just replacing T with D
3955 * after all the preparations, the logical equivalent will be
3956 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3957 * 3:lustre-MDT0000-mdtlov 4:f
3958 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3959 * 3:lustre-MDD0000 4:f
3961 * notice we build the stack from down to top: MDD first, then MDT */
3963 name_size = MAX_OBD_NAME;
3964 uuid_size = MAX_OBD_NAME;
3966 OBD_ALLOC(name, name_size);
3967 OBD_ALLOC(uuid, uuid_size);
3968 if (name == NULL || uuid == NULL)
3969 GOTO(cleanup_mem, rc = -ENOMEM);
3971 OBD_ALLOC_PTR(bufs);
3973 GOTO(cleanup_mem, rc = -ENOMEM);
3976 p = strstr(name, "-MDT");
3978 GOTO(free_bufs, rc = -ENOMEM);
3981 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3983 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3984 if (lprof == NULL || lprof->lp_dt == NULL) {
3985 CERROR("can't find the profile: %s\n",
3986 lustre_cfg_string(cfg, 0));
3987 GOTO(free_bufs, rc = -EINVAL);
3990 lustre_cfg_bufs_reset(bufs, name);
3991 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3992 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3993 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3995 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3997 GOTO(free_bufs, rc = -ENOMEM);
3999 rc = class_attach(lcfg);
4001 GOTO(lcfg_cleanup, rc);
4003 obd = class_name2obd(name);
4005 CERROR("Can not find obd %s (%s in config)\n",
4006 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4007 GOTO(class_detach, rc = -EINVAL);
4010 lustre_cfg_free(lcfg);
4012 lustre_cfg_bufs_reset(bufs, name);
4013 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4014 lustre_cfg_bufs_set_string(bufs, 2, dev);
4015 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4017 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4019 rc = class_setup(obd, lcfg);
4021 GOTO(class_detach, rc);
4023 /* connect to MDD we just setup */
4024 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4026 GOTO(class_detach, rc);
4028 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4030 LASSERT(mdt_lu_site(mdt) == NULL);
4031 mdt->mdt_lu_dev.ld_site = site;
4032 site->ls_top_dev = &mdt->mdt_lu_dev;
4033 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4035 /* now connect to bottom OSD */
4036 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4037 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4039 GOTO(class_detach, rc);
4041 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4043 rc = lu_env_refill((struct lu_env *)env);
4045 CERROR("Failure to refill session: '%d'\n", rc);
4047 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4052 class_detach(obd, lcfg);
4054 lustre_cfg_free(lcfg);
4059 OBD_FREE(name, name_size);
4061 OBD_FREE(uuid, uuid_size);
4065 /* setup quota master target on MDT0 */
4066 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4067 struct lustre_cfg *cfg)
4069 struct obd_device *obd;
4070 char *dev = lustre_cfg_string(cfg, 0);
4071 char *qmtname, *uuid, *p;
4072 struct lustre_cfg_bufs *bufs;
4073 struct lustre_cfg *lcfg;
4074 struct lustre_profile *lprof;
4075 struct obd_connect_data *data;
4079 LASSERT(mdt->mdt_qmt_exp == NULL);
4080 LASSERT(mdt->mdt_qmt_dev == NULL);
4082 /* quota master is on MDT0 only for now */
4083 if (mdt->mdt_seq_site.ss_node_id != 0)
4086 /* MGS generates config commands which look as follows:
4087 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4088 * 3:lustre-MDT0000-mdtlov 4:f
4090 * We generate the QMT name from the MDT one, just replacing MD with QM
4091 * after all the preparations, the logical equivalent will be:
4092 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4093 * 3:lustre-MDT0000-osd 4:f */
4094 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4095 OBD_ALLOC(uuid, UUID_MAX);
4096 OBD_ALLOC_PTR(bufs);
4097 OBD_ALLOC_PTR(data);
4098 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4099 GOTO(cleanup_mem, rc = -ENOMEM);
4101 strcpy(qmtname, dev);
4102 p = strstr(qmtname, "-MDT");
4104 GOTO(cleanup_mem, rc = -ENOMEM);
4105 /* replace MD with QM */
4109 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4111 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4112 if (lprof == NULL || lprof->lp_dt == NULL) {
4113 CERROR("can't find profile for %s\n",
4114 lustre_cfg_string(cfg, 0));
4115 GOTO(cleanup_mem, rc = -EINVAL);
4118 lustre_cfg_bufs_reset(bufs, qmtname);
4119 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4120 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4121 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4123 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4125 GOTO(cleanup_mem, rc = -ENOMEM);
4127 rc = class_attach(lcfg);
4129 GOTO(lcfg_cleanup, rc);
4131 obd = class_name2obd(qmtname);
4133 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4134 lustre_cfg_string(cfg, 0));
4135 GOTO(class_detach, rc = -EINVAL);
4138 lustre_cfg_free(lcfg);
4140 lustre_cfg_bufs_reset(bufs, qmtname);
4141 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4142 lustre_cfg_bufs_set_string(bufs, 2, dev);
4144 /* for quota, the next device should be the OSD device */
4145 lustre_cfg_bufs_set_string(bufs, 3,
4146 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4148 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4150 rc = class_setup(obd, lcfg);
4152 GOTO(class_detach, rc);
4154 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4156 /* configure local quota objects */
4157 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4161 GOTO(class_cleanup, rc);
4163 /* connect to quota master target */
4164 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4165 data->ocd_version = LUSTRE_VERSION_CODE;
4166 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4169 CERROR("cannot connect to quota master device %s (%d)\n",
4171 GOTO(class_cleanup, rc);
4177 class_manual_cleanup(obd);
4178 mdt->mdt_qmt_dev = NULL;
4182 class_detach(obd, lcfg);
4184 lustre_cfg_free(lcfg);
4189 OBD_FREE(qmtname, MAX_OBD_NAME);
4191 OBD_FREE(uuid, UUID_MAX);
4197 /* Shutdown quota master target associated with mdt */
4198 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4202 if (mdt->mdt_qmt_exp == NULL)
4204 LASSERT(mdt->mdt_qmt_dev != NULL);
4206 /* the qmt automatically shuts down when the mdt disconnects */
4207 obd_disconnect(mdt->mdt_qmt_exp);
4208 mdt->mdt_qmt_exp = NULL;
4209 mdt->mdt_qmt_dev = NULL;
4213 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4214 * for now. This will be removed along with converting rest of MDT code
4215 * to use tgt_session_info */
4216 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4218 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4221 rc = mdt_getxattr(info);
4223 mdt_thread_info_fini(info);
4227 static struct tgt_handler mdt_tgt_handlers[] = {
4228 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4229 0, MDS_CONNECT, mdt_tgt_connect,
4230 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4231 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4232 0, MDS_DISCONNECT, tgt_disconnect,
4233 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4234 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4235 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4236 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4237 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4238 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4239 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4240 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4242 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4243 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4244 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4245 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4246 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4248 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4249 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4250 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4251 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4252 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4254 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4255 mdt_hsm_ct_register),
4256 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4257 mdt_hsm_ct_unregister),
4258 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4260 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4262 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4263 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4265 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4270 static struct tgt_handler mdt_sec_ctx_ops[] = {
4271 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4272 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4273 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4276 static struct tgt_handler mdt_quota_ops[] = {
4277 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4280 static struct tgt_opc_slice mdt_common_slice[] = {
4282 .tos_opc_start = MDS_FIRST_OPC,
4283 .tos_opc_end = MDS_LAST_OPC,
4284 .tos_hs = mdt_tgt_handlers
4287 .tos_opc_start = OBD_FIRST_OPC,
4288 .tos_opc_end = OBD_LAST_OPC,
4289 .tos_hs = tgt_obd_handlers
4292 .tos_opc_start = LDLM_FIRST_OPC,
4293 .tos_opc_end = LDLM_LAST_OPC,
4294 .tos_hs = tgt_dlm_handlers
4297 .tos_opc_start = SEC_FIRST_OPC,
4298 .tos_opc_end = SEC_LAST_OPC,
4299 .tos_hs = mdt_sec_ctx_ops
4302 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4303 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4304 .tos_hs = tgt_out_handlers
4307 .tos_opc_start = FLD_FIRST_OPC,
4308 .tos_opc_end = FLD_LAST_OPC,
4309 .tos_hs = fld_handlers
4312 .tos_opc_start = SEQ_FIRST_OPC,
4313 .tos_opc_end = SEQ_LAST_OPC,
4314 .tos_hs = seq_handlers
4317 .tos_opc_start = QUOTA_DQACQ,
4318 .tos_opc_end = QUOTA_LAST_OPC,
4319 .tos_hs = mdt_quota_ops
4322 .tos_opc_start = LLOG_FIRST_OPC,
4323 .tos_opc_end = LLOG_LAST_OPC,
4324 .tos_hs = tgt_llog_handlers
4327 .tos_opc_start = LFSCK_FIRST_OPC,
4328 .tos_opc_end = LFSCK_LAST_OPC,
4329 .tos_hs = tgt_lfsck_handlers
4337 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4339 struct md_device *next = m->mdt_child;
4340 struct lu_device *d = &m->mdt_lu_dev;
4341 struct obd_device *obd = mdt2obd_dev(m);
4342 struct lfsck_stop stop;
4345 stop.ls_status = LS_PAUSED;
4347 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4349 target_recovery_fini(obd);
4350 ping_evictor_stop();
4351 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4353 if (m->mdt_opts.mo_coordinator)
4354 mdt_hsm_cdt_stop(m);
4356 mdt_hsm_cdt_fini(m);
4358 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4359 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4360 obd_exports_barrier(obd);
4361 obd_zombie_barrier();
4365 tgt_fini(env, &m->mdt_lut);
4366 mdt_fs_cleanup(env, m);
4367 upcall_cache_cleanup(m->mdt_identity_cache);
4368 m->mdt_identity_cache = NULL;
4370 if (m->mdt_namespace != NULL) {
4371 ldlm_namespace_free(m->mdt_namespace, NULL,
4372 d->ld_obd->obd_force);
4373 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4376 mdt_quota_fini(env, m);
4378 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4380 mdt_seq_fini(env, m);
4381 mdt_fld_fini(env, m);
4383 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4384 cfs_timer_disarm(&m->mdt_ck_timer);
4385 mdt_ck_thread_stop(m);
4390 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4392 LASSERT(atomic_read(&d->ld_ref) == 0);
4394 server_put_mount(mdt_obd_name(m), true);
4399 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4401 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4402 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4404 struct mdt_thread_info *info;
4405 struct obd_device *obd;
4406 const char *dev = lustre_cfg_string(cfg, 0);
4407 const char *num = lustre_cfg_string(cfg, 2);
4408 struct lustre_mount_info *lmi = NULL;
4409 struct lustre_sb_info *lsi;
4411 struct seq_server_site *ss_site;
4412 const char *identity_upcall = "NONE";
4413 struct md_device *next;
4419 lu_device_init(&m->mdt_lu_dev, ldt);
4421 * Environment (env) might be missing mdt_thread_key values at that
4422 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4425 * Usually device allocation path doesn't use module key values, but
4426 * mdt has to do a lot of work here, so allocate key value.
4428 rc = lu_env_refill((struct lu_env *)env);
4432 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4433 LASSERT(info != NULL);
4435 obd = class_name2obd(dev);
4436 LASSERT(obd != NULL);
4438 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4440 m->mdt_som_conf = 0;
4442 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4444 /* default is coordinator off, it is started through conf_param
4446 m->mdt_opts.mo_coordinator = 0;
4448 lmi = server_get_mount(dev);
4450 CERROR("Cannot get mount info for %s!\n", dev);
4453 lsi = s2lsi(lmi->lmi_sb);
4454 /* CMD is supported only in IAM mode */
4456 node_id = simple_strtol(num, NULL, 10);
4457 obd->u.obt.obt_magic = OBT_MAGIC;
4460 spin_lock_init(&m->mdt_ioepoch_lock);
4461 m->mdt_capa_timeout = CAPA_TIMEOUT;
4462 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4463 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4464 m->mdt_squash.rsi_uid = 0;
4465 m->mdt_squash.rsi_gid = 0;
4466 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4467 init_rwsem(&m->mdt_squash.rsi_sem);
4468 spin_lock_init(&m->mdt_osfs_lock);
4469 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4470 m->mdt_enable_remote_dir = 0;
4471 m->mdt_enable_remote_dir_gid = 0;
4473 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4474 m->mdt_lu_dev.ld_obd = obd;
4475 /* Set this lu_device to obd for error handling purposes. */
4476 obd->obd_lu_dev = &m->mdt_lu_dev;
4478 /* init the stack */
4479 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4481 CERROR("%s: Can't init device stack, rc %d\n",
4482 mdt_obd_name(m), rc);
4487 ss_site = mdt_seq_site(m);
4488 s->ld_seq_site = ss_site;
4491 /* set server index */
4492 ss_site->ss_node_id = node_id;
4494 /* failover is the default
4495 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4496 * assumed whose ss_node_id == 0 XXX
4498 obd->obd_replayable = 1;
4499 /* No connection accepted until configurations will finish */
4500 obd->obd_no_conn = 1;
4502 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4503 char *str = lustre_cfg_string(cfg, 4);
4504 if (strchr(str, 'n')) {
4505 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4506 obd->obd_replayable = 0;
4510 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4512 GOTO(err_fini_stack, rc);
4514 rc = mdt_seq_init(env, m);
4516 GOTO(err_fini_fld, rc);
4518 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4519 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4520 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4521 LDLM_NAMESPACE_SERVER,
4522 LDLM_NAMESPACE_GREEDY,
4524 if (m->mdt_namespace == NULL)
4525 GOTO(err_fini_seq, rc = -ENOMEM);
4527 m->mdt_namespace->ns_lvbp = m;
4528 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4530 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4531 /* set obd_namespace for compatibility with old code */
4532 obd->obd_namespace = m->mdt_namespace;
4534 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4536 rc = mdt_hsm_cdt_init(m);
4538 CERROR("%s: error initializing coordinator, rc %d\n",
4539 mdt_obd_name(m), rc);
4540 GOTO(err_free_ns, rc);
4543 rc = mdt_ck_thread_start(m);
4545 GOTO(err_free_hsm, rc);
4547 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4548 OBD_FAIL_MDS_ALL_REQUEST_NET,
4549 OBD_FAIL_MDS_ALL_REPLY_NET);
4553 rc = mdt_fs_setup(env, m, obd, lsi);
4557 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4559 next = m->mdt_child;
4560 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4563 GOTO(err_fs_cleanup, rc);
4565 if (mntopts & MNTOPT_USERXATTR)
4566 m->mdt_opts.mo_user_xattr = 1;
4568 m->mdt_opts.mo_user_xattr = 0;
4570 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4572 GOTO(err_fs_cleanup, rc);
4574 if (mntopts & MNTOPT_ACL)
4575 m->mdt_opts.mo_acl = 1;
4577 m->mdt_opts.mo_acl = 0;
4579 /* XXX: to support suppgid for ACL, we enable identity_upcall
4580 * by default, otherwise, maybe got unexpected -EACCESS. */
4581 if (m->mdt_opts.mo_acl)
4582 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4584 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4586 &mdt_identity_upcall_cache_ops);
4587 if (IS_ERR(m->mdt_identity_cache)) {
4588 rc = PTR_ERR(m->mdt_identity_cache);
4589 m->mdt_identity_cache = NULL;
4590 GOTO(err_fs_cleanup, rc);
4593 rc = mdt_procfs_init(m, dev);
4595 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4596 GOTO(err_recovery, rc);
4599 rc = mdt_quota_init(env, m, cfg);
4601 GOTO(err_procfs, rc);
4603 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4604 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4605 "mdt_ldlm_client", m->mdt_ldlm_client);
4607 ping_evictor_start();
4609 /* recovery will be started upon mdt_prepare()
4610 * when the whole stack is complete and ready
4611 * to serve the requests */
4613 mdt_init_capa_ctxt(env, m);
4615 /* Reduce the initial timeout on an MDS because it doesn't need such
4616 * a long timeout as an OST does. Adaptive timeouts will adjust this
4617 * value appropriately. */
4618 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4619 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4625 target_recovery_fini(obd);
4626 upcall_cache_cleanup(m->mdt_identity_cache);
4627 m->mdt_identity_cache = NULL;
4629 mdt_fs_cleanup(env, m);
4631 tgt_fini(env, &m->mdt_lut);
4633 cfs_timer_disarm(&m->mdt_ck_timer);
4634 mdt_ck_thread_stop(m);
4636 mdt_hsm_cdt_fini(m);
4638 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4639 obd->obd_namespace = m->mdt_namespace = NULL;
4641 mdt_seq_fini(env, m);
4643 mdt_fld_fini(env, m);
4645 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4648 server_put_mount(dev, true);
4652 /* For interoperability, the left element is old parameter, the right one
4653 * is the new version of the parameter, if some parameter is deprecated,
4654 * the new version should be set as NULL. */
4655 static struct cfg_interop_param mdt_interop_param[] = {
4656 { "mdt.group_upcall", NULL },
4657 { "mdt.quota_type", NULL },
4658 { "mdd.quota_type", NULL },
4659 { "mdt.rootsquash", "mdt.root_squash" },
4660 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4664 /* used by MGS to process specific configurations */
4665 static int mdt_process_config(const struct lu_env *env,
4666 struct lu_device *d, struct lustre_cfg *cfg)
4668 struct mdt_device *m = mdt_dev(d);
4669 struct md_device *md_next = m->mdt_child;
4670 struct lu_device *next = md2lu_dev(md_next);
4674 switch (cfg->lcfg_command) {
4676 struct obd_device *obd = d->ld_obd;
4678 /* For interoperability */
4679 struct cfg_interop_param *ptr = NULL;
4680 struct lustre_cfg *old_cfg = NULL;
4683 param = lustre_cfg_string(cfg, 1);
4684 if (param == NULL) {
4685 CERROR("param is empty\n");
4690 ptr = class_find_old_param(param, mdt_interop_param);
4692 if (ptr->new_param == NULL) {
4694 CWARN("For interoperability, skip this %s."
4695 " It is obsolete.\n", ptr->old_param);
4699 CWARN("Found old param %s, changed it to %s.\n",
4700 ptr->old_param, ptr->new_param);
4703 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4710 rc = class_process_proc_seq_param(PARAM_MDT, obd->obd_vars,
4712 if (rc > 0 || rc == -ENOSYS) {
4713 /* is it an HSM var ? */
4714 rc = class_process_proc_seq_param(PARAM_HSM,
4715 hsm_cdt_get_proc_vars(),
4717 if (rc > 0 || rc == -ENOSYS)
4718 /* we don't understand; pass it on */
4719 rc = next->ld_ops->ldo_process_config(env, next,
4723 if (old_cfg != NULL)
4724 lustre_cfg_free(cfg);
4729 /* others are passed further */
4730 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4736 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4737 const struct lu_object_header *hdr,
4738 struct lu_device *d)
4740 struct mdt_object *mo;
4744 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4746 struct lu_object *o;
4747 struct lu_object_header *h;
4750 h = &mo->mot_header;
4751 lu_object_header_init(h);
4752 lu_object_init(o, h, d);
4753 lu_object_add_top(h, o);
4754 o->lo_ops = &mdt_obj_ops;
4755 mutex_init(&mo->mot_ioepoch_mutex);
4756 mutex_init(&mo->mot_lov_mutex);
4757 init_rwsem(&mo->mot_open_sem);
4763 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4764 const struct lu_object_conf *unused)
4766 struct mdt_device *d = mdt_dev(o->lo_dev);
4767 struct lu_device *under;
4768 struct lu_object *below;
4772 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4773 PFID(lu_object_fid(o)));
4775 under = &d->mdt_child->md_lu_dev;
4776 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4777 if (below != NULL) {
4778 lu_object_add(o, below);
4785 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4787 struct mdt_object *mo = mdt_obj(o);
4788 struct lu_object_header *h;
4792 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4793 PFID(lu_object_fid(o)));
4795 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4796 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4799 lu_object_header_fini(h);
4800 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4805 static int mdt_object_print(const struct lu_env *env, void *cookie,
4806 lu_printer_t p, const struct lu_object *o)
4808 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4809 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4810 "flags="LPX64", epochcount=%d, writecount=%d)",
4811 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4812 mdto->mot_ioepoch_count, mdto->mot_writecount);
4815 static int mdt_prepare(const struct lu_env *env,
4816 struct lu_device *pdev,
4817 struct lu_device *cdev)
4819 struct mdt_device *mdt = mdt_dev(cdev);
4820 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4821 struct obd_device *obd = cdev->ld_obd;
4828 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4832 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4836 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4840 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4841 /* The LFSCK instance is registered just now, so it must be there when
4842 * register the namespace to such instance. */
4843 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4845 if (mdt->mdt_seq_site.ss_node_id == 0) {
4846 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4847 &mdt->mdt_md_root_fid);
4852 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4853 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4854 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4855 LASSERT(obd->obd_no_conn);
4856 spin_lock(&obd->obd_dev_lock);
4857 obd->obd_no_conn = 0;
4858 spin_unlock(&obd->obd_dev_lock);
4860 if (obd->obd_recovering == 0)
4861 mdt_postrecov(env, mdt);
4866 const struct lu_device_operations mdt_lu_ops = {
4867 .ldo_object_alloc = mdt_object_alloc,
4868 .ldo_process_config = mdt_process_config,
4869 .ldo_prepare = mdt_prepare,
4872 static const struct lu_object_operations mdt_obj_ops = {
4873 .loo_object_init = mdt_object_init,
4874 .loo_object_free = mdt_object_free,
4875 .loo_object_print = mdt_object_print
4878 static int mdt_obd_set_info_async(const struct lu_env *env,
4879 struct obd_export *exp,
4880 __u32 keylen, void *key,
4881 __u32 vallen, void *val,
4882 struct ptlrpc_request_set *set)
4888 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4889 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4897 * Match client and server connection feature flags.
4899 * Compute the compatibility flags for a connection request based on
4900 * features mutually supported by client and server.
4902 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4903 * must not be updated here, otherwise a partially initialized value may
4904 * be exposed. After the connection request is successfully processed,
4905 * the top-level MDT connect request handler atomically updates the export
4906 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4907 * reply. \see mdt_connect().
4909 * \param exp the obd_export associated with this client/target pair
4910 * \param mdt the target device for the connection
4911 * \param data stores data for this connect request
4914 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4915 * \retval -EBADE client and server feature requirements are incompatible
4917 static int mdt_connect_internal(struct obd_export *exp,
4918 struct mdt_device *mdt,
4919 struct obd_connect_data *data)
4921 LASSERT(data != NULL);
4923 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4924 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4926 /* If no known bits (which should not happen, probably,
4927 as everybody should support LOOKUP and UPDATE bits at least)
4928 revert to compat mode with plain locks. */
4929 if (!data->ocd_ibits_known &&
4930 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4931 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4933 if (!mdt->mdt_opts.mo_acl)
4934 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4936 if (!mdt->mdt_opts.mo_user_xattr)
4937 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4939 if (!mdt->mdt_som_conf)
4940 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4942 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4943 data->ocd_brw_size = min(data->ocd_brw_size,
4944 (__u32)MD_MAX_BRW_SIZE);
4945 if (data->ocd_brw_size == 0) {
4946 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4947 " ocd_version: %x ocd_grant: %d "
4948 "ocd_index: %u ocd_brw_size is "
4949 "unexpectedly zero, network data "
4950 "corruption? Refusing connection of this"
4953 exp->exp_client_uuid.uuid,
4954 exp, data->ocd_connect_flags, data->ocd_version,
4955 data->ocd_grant, data->ocd_index);
4960 /* NB: Disregard the rule against updating
4961 * exp_connect_data.ocd_connect_flags in this case, since
4962 * tgt_client_new() needs to know if this is a lightweight
4963 * connection, and it is safe to expose this flag before
4964 * connection processing completes. */
4965 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4966 spin_lock(&exp->exp_lock);
4967 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4968 spin_unlock(&exp->exp_lock);
4971 data->ocd_version = LUSTRE_VERSION_CODE;
4973 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4974 CWARN("%s: MDS requires FID support, but client not\n",
4979 if (mdt->mdt_som_conf &&
4980 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4981 OBD_CONNECT_MDS_MDS |
4982 OBD_CONNECT_SOM))) {
4983 CWARN("%s: MDS has SOM enabled, but client does not support "
4984 "it\n", mdt_obd_name(mdt));
4988 if (OCD_HAS_FLAG(data, PINGLESS)) {
4989 if (ptlrpc_pinger_suppress_pings()) {
4990 spin_lock(&exp->exp_obd->obd_dev_lock);
4991 list_del_init(&exp->exp_obd_chain_timed);
4992 spin_unlock(&exp->exp_obd->obd_dev_lock);
4994 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4998 data->ocd_max_easize = mdt->mdt_max_ea_size;
5003 /* mds_connect copy */
5004 static int mdt_obd_connect(const struct lu_env *env,
5005 struct obd_export **exp, struct obd_device *obd,
5006 struct obd_uuid *cluuid,
5007 struct obd_connect_data *data,
5010 struct obd_export *lexp;
5011 struct lustre_handle conn = { 0 };
5012 struct mdt_device *mdt;
5016 LASSERT(env != NULL);
5017 if (!exp || !obd || !cluuid)
5020 mdt = mdt_dev(obd->obd_lu_dev);
5023 * first, check whether the stack is ready to handle requests
5024 * XXX: probably not very appropriate method is used now
5025 * at some point we should find a better one
5027 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5028 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5029 rc = obd_get_info(env, mdt->mdt_child_exp,
5030 sizeof(KEY_OSP_CONNECTED),
5031 KEY_OSP_CONNECTED, NULL, NULL, NULL);
5034 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5037 rc = class_connect(&conn, obd, cluuid);
5041 lexp = class_conn2export(&conn);
5042 LASSERT(lexp != NULL);
5044 rc = mdt_connect_internal(lexp, mdt, data);
5046 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5049 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5050 rc = tgt_client_new(env, lexp);
5052 mdt_export_stats_init(obd, lexp, localdata);
5054 /* For phase I, sync for cross-ref operation. */
5055 lexp->exp_keep_sync = 1;
5059 class_disconnect(lexp);
5068 static int mdt_obd_reconnect(const struct lu_env *env,
5069 struct obd_export *exp, struct obd_device *obd,
5070 struct obd_uuid *cluuid,
5071 struct obd_connect_data *data,
5077 if (exp == NULL || obd == NULL || cluuid == NULL)
5080 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5082 mdt_export_stats_init(obd, exp, localdata);
5087 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5088 struct mdt_thread_info *info,
5089 struct mdt_file_data *mfd)
5091 struct lu_context ses;
5095 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5100 lu_context_enter(&ses);
5102 mdt_ucred(info)->uc_valid = UCRED_OLD;
5103 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5105 lu_context_exit(&ses);
5106 lu_context_fini(&ses);
5112 static int mdt_export_cleanup(struct obd_export *exp)
5114 struct mdt_export_data *med = &exp->exp_mdt_data;
5115 struct obd_device *obd = exp->exp_obd;
5116 struct mdt_device *mdt;
5117 struct mdt_thread_info *info;
5119 CFS_LIST_HEAD(closing_list);
5120 struct mdt_file_data *mfd, *n;
5124 spin_lock(&med->med_open_lock);
5125 while (!cfs_list_empty(&med->med_open_head)) {
5126 cfs_list_t *tmp = med->med_open_head.next;
5127 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5129 /* Remove mfd handle so it can't be found again.
5130 * We are consuming the mfd_list reference here. */
5131 class_handle_unhash(&mfd->mfd_handle);
5132 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5134 spin_unlock(&med->med_open_lock);
5135 mdt = mdt_dev(obd->obd_lu_dev);
5136 LASSERT(mdt != NULL);
5138 rc = lu_env_init(&env, LCT_MD_THREAD);
5142 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5143 LASSERT(info != NULL);
5144 memset(info, 0, sizeof *info);
5145 info->mti_env = &env;
5146 info->mti_mdt = mdt;
5147 info->mti_exp = exp;
5149 if (!cfs_list_empty(&closing_list)) {
5150 struct md_attr *ma = &info->mti_attr;
5152 /* Close any open files (which may also cause orphan unlinking). */
5153 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5154 cfs_list_del_init(&mfd->mfd_list);
5155 ma->ma_need = ma->ma_valid = 0;
5157 /* This file is being closed due to an eviction, it
5158 * could have been modified and now dirty regarding to
5159 * HSM archive, check this!
5160 * The logic here is to mark a file dirty if there's a
5161 * chance it was dirtied before the client was evicted,
5162 * so that we don't have to wait for a release attempt
5163 * before finding out the file was actually dirty and
5164 * fail the release. Aggressively marking it dirty here
5165 * will cause the policy engine to attempt to
5166 * re-archive it; when rearchiving, we can compare the
5167 * current version to the HSM data_version and make the
5168 * archive request into a noop if it's not actually
5171 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5172 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5174 /* Don't unlink orphan on failover umount, LU-184 */
5175 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5176 ma->ma_valid = MA_FLAGS;
5177 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5179 mdt_mfd_close(info, mfd);
5182 info->mti_mdt = NULL;
5183 /* cleanup client slot early */
5184 /* Do not erase record for recoverable client. */
5185 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5186 tgt_client_del(&env, exp);
5192 static int mdt_obd_disconnect(struct obd_export *exp)
5198 class_export_get(exp);
5200 rc = server_disconnect_export(exp);
5202 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5204 rc = mdt_export_cleanup(exp);
5205 class_export_put(exp);
5209 /* FIXME: Can we avoid using these two interfaces? */
5210 static int mdt_init_export(struct obd_export *exp)
5212 struct mdt_export_data *med = &exp->exp_mdt_data;
5216 CFS_INIT_LIST_HEAD(&med->med_open_head);
5217 spin_lock_init(&med->med_open_lock);
5218 mutex_init(&med->med_idmap_mutex);
5219 med->med_idmap = NULL;
5220 spin_lock(&exp->exp_lock);
5221 exp->exp_connecting = 1;
5222 spin_unlock(&exp->exp_lock);
5224 /* self-export doesn't need client data and ldlm initialization */
5225 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5226 &exp->exp_client_uuid)))
5229 rc = tgt_client_alloc(exp);
5233 rc = ldlm_init_export(exp);
5240 tgt_client_free(exp);
5242 CERROR("%s: Failed to initialize export: rc = %d\n",
5243 exp->exp_obd->obd_name, rc);
5247 static int mdt_destroy_export(struct obd_export *exp)
5251 if (exp_connect_rmtclient(exp))
5252 mdt_cleanup_idmap(&exp->exp_mdt_data);
5254 target_destroy_export(exp);
5255 /* destroy can be called from failed obd_setup, so
5256 * checking uuid is safer than obd_self_export */
5257 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5258 &exp->exp_client_uuid)))
5261 ldlm_destroy_export(exp);
5262 tgt_client_free(exp);
5264 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5265 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5270 /** The maximum depth that fid2path() will search.
5271 * This is limited only because we want to store the fids for
5272 * historical path lookup purposes.
5274 #define MAX_PATH_DEPTH 100
5276 /** mdt_path() lookup structure. */
5277 struct path_lookup_info {
5278 __u64 pli_recno; /**< history point */
5279 __u64 pli_currec; /**< current record */
5280 struct lu_fid pli_fid;
5281 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5282 struct mdt_object *pli_mdt_obj;
5283 char *pli_path; /**< full path */
5285 int pli_linkno; /**< which hardlink to follow */
5286 int pli_fidcount; /**< number of \a pli_fids */
5289 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5290 struct linkea_data *ldata)
5294 LASSERT(ldata->ld_buf->lb_buf != NULL);
5296 if (!mdt_object_exists(mdt_obj))
5299 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5300 ldata->ld_buf, XATTR_NAME_LINK);
5301 if (rc == -ERANGE) {
5302 /* Buf was too small, figure out what we need. */
5303 lu_buf_free(ldata->ld_buf);
5304 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5305 ldata->ld_buf, XATTR_NAME_LINK);
5308 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5309 if (ldata->ld_buf->lb_buf == NULL)
5311 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5312 ldata->ld_buf, XATTR_NAME_LINK);
5317 return linkea_init(ldata);
5320 static int mdt_path_current(struct mdt_thread_info *info,
5321 struct path_lookup_info *pli)
5323 struct mdt_device *mdt = info->mti_mdt;
5324 struct mdt_object *mdt_obj;
5325 struct link_ea_header *leh;
5326 struct link_ea_entry *lee;
5327 struct lu_name *tmpname = &info->mti_name;
5328 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5329 struct lu_buf *buf = &info->mti_big_buf;
5332 struct linkea_data ldata = { 0 };
5336 /* temp buffer for path element, the buffer will be finally freed
5337 * in mdt_thread_info_fini */
5338 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5339 if (buf->lb_buf == NULL)
5343 ptr = pli->pli_path + pli->pli_pathlen - 1;
5346 pli->pli_fidcount = 0;
5347 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5348 *tmpfid = pli->pli_fids[0];
5349 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5350 * so checking root_fid can only happen on MDT0. */
5351 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5352 &pli->pli_fids[pli->pli_fidcount])) {
5353 struct lu_buf lmv_buf;
5355 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5356 if (IS_ERR(mdt_obj))
5357 GOTO(out, rc = PTR_ERR(mdt_obj));
5359 if (mdt_object_remote(mdt_obj)) {
5360 mdt_object_put(info->mti_env, mdt_obj);
5361 GOTO(remote_out, rc = -EREMOTE);
5364 lmv_buf.lb_buf = info->mti_xattr_buf;
5365 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5367 /* Check if it is slave stripes */
5368 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5369 &lmv_buf, XATTR_NAME_LMV);
5371 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5373 /* For slave stripes, get its master */
5374 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5375 struct lmv_mds_md_v1 *lmm1 = &lmm->lmv_md_v1;
5377 fid_le_to_cpu(tmpfid, &lmm1->lmv_master_fid);
5378 if (!fid_is_sane(tmpfid)) {
5379 mdt_object_put(info->mti_env, mdt_obj);
5380 GOTO(out, rc = -EINVAL);
5382 mdt_object_put(info->mti_env, mdt_obj);
5383 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5388 if (!mdt_object_exists(mdt_obj)) {
5389 mdt_object_put(info->mti_env, mdt_obj);
5390 GOTO(out, rc = -ENOENT);
5393 rc = mdt_links_read(info, mdt_obj, &ldata);
5394 mdt_object_put(info->mti_env, mdt_obj);
5399 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5400 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5401 /* If set, use link #linkno for path lookup, otherwise use
5402 link #0. Only do this for the final path element. */
5403 if (pli->pli_fidcount == 0 &&
5404 pli->pli_linkno < leh->leh_reccount) {
5406 for (count = 0; count < pli->pli_linkno; count++) {
5407 lee = (struct link_ea_entry *)
5408 ((char *)lee + reclen);
5409 linkea_entry_unpack(lee, &reclen, tmpname,
5412 if (pli->pli_linkno < leh->leh_reccount - 1)
5413 /* indicate to user there are more links */
5417 /* Pack the name in the end of the buffer */
5418 ptr -= tmpname->ln_namelen;
5419 if (ptr - 1 <= pli->pli_path)
5420 GOTO(out, rc = -EOVERFLOW);
5421 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5424 /* Store the parent fid for historic lookup */
5425 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5426 GOTO(out, rc = -EOVERFLOW);
5427 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5431 ptr++; /* skip leading / */
5432 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5439 /* Returns the full path to this fid, as of changelog record recno. */
5440 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5441 char *path, int pathlen, __u64 *recno, int *linkno,
5444 struct mdt_device *mdt = info->mti_mdt;
5445 struct path_lookup_info *pli;
5453 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5462 pli->pli_mdt_obj = obj;
5463 pli->pli_recno = *recno;
5464 pli->pli_path = path;
5465 pli->pli_pathlen = pathlen;
5466 pli->pli_linkno = *linkno;
5468 /* Retry multiple times in case file is being moved */
5469 while (tries-- && rc == -EAGAIN)
5470 rc = mdt_path_current(info, pli);
5472 /* return the last resolved fids to the client, so the client will
5473 * build the left path on another MDT for remote object */
5474 *fid = pli->pli_fids[pli->pli_fidcount];
5476 *recno = pli->pli_currec;
5477 /* Return next link index to caller */
5478 *linkno = pli->pli_linkno;
5485 static int mdt_fid2path(struct mdt_thread_info *info,
5486 struct getinfo_fid2path *fp)
5488 struct mdt_device *mdt = info->mti_mdt;
5489 struct mdt_object *obj;
5493 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5494 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5496 if (!fid_is_sane(&fp->gf_fid))
5499 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5500 CWARN("%s: "DFID" is invalid, sequence should be "
5501 ">= "LPX64"\n", mdt_obd_name(mdt),
5502 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5506 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5507 if (obj == NULL || IS_ERR(obj)) {
5508 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5513 if (mdt_object_remote(obj))
5515 else if (!mdt_object_exists(obj))
5521 mdt_object_put(info->mti_env, obj);
5522 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5523 PFID(&fp->gf_fid), rc);
5527 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5528 &fp->gf_linkno, &fp->gf_fid);
5530 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5531 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5533 mdt_object_put(info->mti_env, obj);
5538 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5539 void *val, int vallen)
5541 struct getinfo_fid2path *fpout, *fpin;
5544 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5547 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5548 lustre_swab_fid2path(fpin);
5550 memcpy(fpout, fpin, sizeof(*fpin));
5551 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5554 rc = mdt_fid2path(info, fpout);
5558 int mdt_get_info(struct tgt_session_info *tsi)
5568 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5570 CDEBUG(D_IOCTL, "No GETINFO key");
5571 RETURN(err_serious(-EFAULT));
5573 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5576 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5577 if (vallen == NULL) {
5578 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5579 RETURN(err_serious(-EFAULT));
5582 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5584 rc = req_capsule_server_pack(tsi->tsi_pill);
5586 RETURN(err_serious(rc));
5588 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5589 if (valout == NULL) {
5590 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5591 RETURN(err_serious(-EFAULT));
5594 if (KEY_IS(KEY_FID2PATH)) {
5595 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5597 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5598 mdt_thread_info_fini(info);
5605 /* Pass the ioc down */
5606 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5607 unsigned int cmd, int len, void *data)
5609 struct lu_context ioctl_session;
5610 struct md_device *next = mdt->mdt_child;
5614 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5617 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5618 lu_context_enter(&ioctl_session);
5619 env->le_ses = &ioctl_session;
5621 LASSERT(next->md_ops->mdo_iocontrol);
5622 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5624 lu_context_exit(&ioctl_session);
5625 lu_context_fini(&ioctl_session);
5629 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5631 struct obd_ioctl_data *data = karg;
5634 struct mdt_object *obj;
5635 struct mdt_lock_handle *lh;
5639 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5640 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5643 fid = (struct lu_fid *)data->ioc_inlbuf1;
5645 if (!fid_is_sane(fid))
5648 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5650 lh = &mti->mti_lh[MDT_LH_PARENT];
5651 mdt_lock_reg_init(lh, LCK_CR);
5653 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5655 RETURN(PTR_ERR(obj));
5657 if (mdt_object_remote(obj)) {
5660 * before calling version get the correct MDS should be
5661 * fid, this is error to find remote object here
5663 CERROR("nonlocal object "DFID"\n", PFID(fid));
5664 } else if (!mdt_object_exists(obj)) {
5665 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5668 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5669 *(__u64 *)data->ioc_inlbuf2 = version;
5672 mdt_object_unlock_put(mti, obj, lh, 1);
5676 /* ioctls on obd dev */
5677 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5678 void *karg, void *uarg)
5681 struct obd_device *obd = exp->exp_obd;
5682 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5683 struct dt_device *dt = mdt->mdt_bottom;
5687 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5688 rc = lu_env_init(&env, LCT_MD_THREAD);
5694 rc = mdt_device_sync(&env, mdt);
5696 case OBD_IOC_SET_READONLY:
5697 rc = dt->dd_ops->dt_ro(&env, dt);
5699 case OBD_IOC_ABORT_RECOVERY:
5700 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5701 target_stop_recovery_thread(obd);
5704 case OBD_IOC_CHANGELOG_REG:
5705 case OBD_IOC_CHANGELOG_DEREG:
5706 case OBD_IOC_CHANGELOG_CLEAR:
5707 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5709 case OBD_IOC_START_LFSCK: {
5710 struct md_device *next = mdt->mdt_child;
5711 struct obd_ioctl_data *data = karg;
5712 struct lfsck_start_param lsp;
5714 if (unlikely(data == NULL)) {
5719 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5720 lsp.lsp_index_valid = 0;
5721 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5724 case OBD_IOC_STOP_LFSCK: {
5725 struct md_device *next = mdt->mdt_child;
5726 struct obd_ioctl_data *data = karg;
5727 struct lfsck_stop stop;
5729 stop.ls_status = LS_STOPPED;
5730 /* Old lfsck utils may pass NULL @stop. */
5731 if (data->ioc_inlbuf1 == NULL)
5735 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5737 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5740 case OBD_IOC_GET_OBJ_VERSION: {
5741 struct mdt_thread_info *mti;
5742 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5743 memset(mti, 0, sizeof *mti);
5744 mti->mti_env = &env;
5748 rc = mdt_ioc_version_get(mti, karg);
5751 case OBD_IOC_CATLOGLIST: {
5752 struct mdt_thread_info *mti;
5754 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5755 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5756 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5757 &mti->mti_tmp_fid1);
5762 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5763 mdt_obd_name(mdt), cmd, rc);
5770 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5772 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5773 struct lfsck_start_param lsp;
5777 lsp.lsp_start = NULL;
5778 lsp.lsp_index_valid = 0;
5779 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5780 OBD_IOC_START_LFSCK,
5782 if (rc != 0 && rc != -EALREADY)
5783 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5784 mdt_obd_name(mdt), rc);
5786 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5790 static int mdt_obd_postrecov(struct obd_device *obd)
5795 rc = lu_env_init(&env, LCT_MD_THREAD);
5798 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5803 static struct obd_ops mdt_obd_device_ops = {
5804 .o_owner = THIS_MODULE,
5805 .o_set_info_async = mdt_obd_set_info_async,
5806 .o_connect = mdt_obd_connect,
5807 .o_reconnect = mdt_obd_reconnect,
5808 .o_disconnect = mdt_obd_disconnect,
5809 .o_init_export = mdt_init_export,
5810 .o_destroy_export = mdt_destroy_export,
5811 .o_iocontrol = mdt_iocontrol,
5812 .o_postrecov = mdt_obd_postrecov,
5815 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5816 struct lu_device *d)
5818 struct mdt_device *m = mdt_dev(d);
5825 static struct lu_device *mdt_device_free(const struct lu_env *env,
5826 struct lu_device *d)
5828 struct mdt_device *m = mdt_dev(d);
5831 lu_device_fini(&m->mdt_lu_dev);
5837 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5838 struct lu_device_type *t,
5839 struct lustre_cfg *cfg)
5841 struct lu_device *l;
5842 struct mdt_device *m;
5849 rc = mdt_init0(env, m, t, cfg);
5851 mdt_device_free(env, l);
5856 l = ERR_PTR(-ENOMEM);
5860 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5861 LU_KEY_INIT(mdt, struct mdt_thread_info);
5863 static void mdt_key_fini(const struct lu_context *ctx,
5864 struct lu_context_key *key, void* data)
5866 struct mdt_thread_info *info = data;
5868 if (info->mti_big_lmm) {
5869 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5870 info->mti_big_lmm = NULL;
5871 info->mti_big_lmmsize = 0;
5876 /* context key: mdt_thread_key */
5877 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5879 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5881 return lu_ucred(info->mti_env);
5884 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5886 return lu_ucred_check(info->mti_env);
5890 * Enable/disable COS (Commit On Sharing).
5892 * Set/Clear the COS flag in mdt options.
5894 * \param mdt mdt device
5895 * \param val 0 disables COS, other values enable COS
5897 void mdt_enable_cos(struct mdt_device *mdt, int val)
5902 mdt->mdt_opts.mo_cos = !!val;
5903 rc = lu_env_init(&env, LCT_LOCAL);
5904 if (unlikely(rc != 0)) {
5905 CWARN("lu_env initialization failed with rc = %d,"
5906 "cannot sync\n", rc);
5909 mdt_device_sync(&env, mdt);
5914 * Check COS (Commit On Sharing) status.
5916 * Return COS flag status.
5918 * \param mdt mdt device
5920 int mdt_cos_is_enabled(struct mdt_device *mdt)
5922 return mdt->mdt_opts.mo_cos != 0;
5925 static struct lu_device_type_operations mdt_device_type_ops = {
5926 .ldto_device_alloc = mdt_device_alloc,
5927 .ldto_device_free = mdt_device_free,
5928 .ldto_device_fini = mdt_device_fini
5931 static struct lu_device_type mdt_device_type = {
5932 .ldt_tags = LU_DEVICE_MD,
5933 .ldt_name = LUSTRE_MDT_NAME,
5934 .ldt_ops = &mdt_device_type_ops,
5935 .ldt_ctx_tags = LCT_MD_THREAD
5938 static int __init mdt_mod_init(void)
5942 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5943 FID_NOBRACE_LEN + 1);
5944 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5946 rc = lu_kmem_init(mdt_caches);
5950 rc = mds_mod_init();
5954 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
5955 #ifndef HAVE_ONLY_PROCFS_SEQ
5958 LUSTRE_MDT_NAME, &mdt_device_type);
5963 lu_kmem_fini(mdt_caches);
5970 static void __exit mdt_mod_exit(void)
5972 class_unregister_type(LUSTRE_MDT_NAME);
5974 lu_kmem_fini(mdt_caches);
5977 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5978 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5979 MODULE_LICENSE("GPL");
5981 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);