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>
72 #include <lustre_nodemap.h>
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 ldlm_mode_t mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
104 /* For HSM restore handles */
105 struct kmem_cache *mdt_hsm_cdt_kmem;
107 /* For HSM request handles */
108 struct kmem_cache *mdt_hsm_car_kmem;
110 static struct lu_kmem_descr mdt_caches[] = {
112 .ckd_cache = &mdt_object_kmem,
113 .ckd_name = "mdt_obj",
114 .ckd_size = sizeof(struct mdt_object)
117 .ckd_cache = &mdt_hsm_cdt_kmem,
118 .ckd_name = "mdt_cdt_restore_handle",
119 .ckd_size = sizeof(struct cdt_restore_handle)
122 .ckd_cache = &mdt_hsm_car_kmem,
123 .ckd_name = "mdt_cdt_agent_req",
124 .ckd_size = sizeof(struct cdt_agent_req)
131 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
135 return (rep->lock_policy_res1 & flag);
138 void mdt_clear_disposition(struct mdt_thread_info *info,
139 struct ldlm_reply *rep, int flag)
142 info->mti_opdata &= ~flag;
143 tgt_opdata_clear(info->mti_env, flag);
146 rep->lock_policy_res1 &= ~flag;
149 void mdt_set_disposition(struct mdt_thread_info *info,
150 struct ldlm_reply *rep, int flag)
153 info->mti_opdata |= flag;
154 tgt_opdata_set(info->mti_env, flag);
157 rep->lock_policy_res1 |= flag;
160 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
162 lh->mlh_pdo_hash = 0;
163 lh->mlh_reg_mode = lm;
164 lh->mlh_rreg_mode = lm;
165 lh->mlh_type = MDT_REG_LOCK;
168 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
169 const struct lu_name *lname)
171 lh->mlh_reg_mode = lock_mode;
172 lh->mlh_rreg_mode = lock_mode;
173 lh->mlh_type = MDT_PDO_LOCK;
175 if (lu_name_is_valid(lname)) {
176 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
178 /* XXX Workaround for LU-2856
180 * Zero is a valid return value of full_name_hash, but
181 * several users of mlh_pdo_hash assume a non-zero
182 * hash value. We therefore map zero onto an
183 * arbitrary, but consistent value (1) to avoid
184 * problems further down the road. */
185 if (unlikely(lh->mlh_pdo_hash == 0))
186 lh->mlh_pdo_hash = 1;
188 lh->mlh_pdo_hash = 0;
192 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
193 struct mdt_lock_handle *lh)
199 * Any dir access needs couple of locks:
201 * 1) on part of dir we gonna take lookup/modify;
203 * 2) on whole dir to protect it from concurrent splitting and/or to
204 * flush client's cache for readdir().
206 * so, for a given mode and object this routine decides what lock mode
207 * to use for lock #2:
209 * 1) if caller's gonna lookup in dir then we need to protect dir from
210 * being splitted only - LCK_CR
212 * 2) if caller's gonna modify dir then we need to protect dir from
213 * being splitted and to flush cache - LCK_CW
215 * 3) if caller's gonna modify dir and that dir seems ready for
216 * splitting then we need to protect it from any type of access
217 * (lookup/modify/split) - LCK_EX --bzzz
220 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
221 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
224 * Ask underlaying level its opinion about preferable PDO lock mode
225 * having access type passed as regular lock mode:
227 * - MDL_MINMODE means that lower layer does not want to specify lock
230 * - MDL_NL means that no PDO lock should be taken. This is used in some
231 * cases. Say, for non-splittable directories no need to use PDO locks
234 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
235 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
237 if (mode != MDL_MINMODE) {
238 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
241 * Lower layer does not want to specify locking mode. We do it
242 * our selves. No special protection is needed, just flush
243 * client's cache on modification and allow concurrent
246 switch (lh->mlh_reg_mode) {
248 lh->mlh_pdo_mode = LCK_EX;
251 lh->mlh_pdo_mode = LCK_CR;
254 lh->mlh_pdo_mode = LCK_CW;
257 CERROR("Not expected lock type (0x%x)\n",
258 (int)lh->mlh_reg_mode);
263 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
267 static int mdt_getstatus(struct tgt_session_info *tsi)
269 struct mdt_thread_info *info = tsi2mdt_info(tsi);
270 struct mdt_device *mdt = info->mti_mdt;
271 struct mdt_body *repbody;
276 rc = mdt_check_ucred(info);
278 GOTO(out, rc = err_serious(rc));
280 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
281 GOTO(out, rc = err_serious(-ENOMEM));
283 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
284 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
285 repbody->mbo_valid |= OBD_MD_FLID;
287 if (tsi->tsi_tgt->lut_mds_capa &&
288 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
289 struct mdt_object *root;
290 struct lustre_capa *capa;
292 root = mdt_object_find(info->mti_env, mdt, &repbody->mbo_fid1);
294 GOTO(out, rc = PTR_ERR(root));
296 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
298 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
299 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
301 mdt_object_put(info->mti_env, root);
303 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
307 mdt_thread_info_fini(info);
311 static int mdt_statfs(struct tgt_session_info *tsi)
313 struct ptlrpc_request *req = tgt_ses_req(tsi);
314 struct mdt_thread_info *info = tsi2mdt_info(tsi);
315 struct md_device *next = info->mti_mdt->mdt_child;
316 struct ptlrpc_service_part *svcpt;
317 struct obd_statfs *osfs;
322 svcpt = req->rq_rqbd->rqbd_svcpt;
324 /* This will trigger a watchdog timeout */
325 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
326 (MDT_SERVICE_WATCHDOG_FACTOR *
327 at_get(&svcpt->scp_at_estimate)) + 1);
329 rc = mdt_check_ucred(info);
331 GOTO(out, rc = err_serious(rc));
333 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
334 GOTO(out, rc = err_serious(-ENOMEM));
336 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
338 GOTO(out, rc = -EPROTO);
340 /** statfs information are cached in the mdt_device */
341 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
342 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
343 /** statfs data is too old, get up-to-date one */
344 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
347 spin_lock(&info->mti_mdt->mdt_osfs_lock);
348 info->mti_mdt->mdt_osfs = *osfs;
349 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
350 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
352 /** use cached statfs data */
353 spin_lock(&info->mti_mdt->mdt_osfs_lock);
354 *osfs = info->mti_mdt->mdt_osfs;
355 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
359 mdt_counter_incr(req, LPROC_MDT_STATFS);
361 mdt_thread_info_fini(info);
366 * Pack SOM attributes into the reply.
367 * Call under a DLM UPDATE lock.
369 static void mdt_pack_size2body(struct mdt_thread_info *info,
370 struct mdt_object *mo)
373 struct md_attr *ma = &info->mti_attr;
375 LASSERT(ma->ma_attr.la_valid & LA_MODE);
376 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
378 /* Check if Size-on-MDS is supported, if this is a regular file,
379 * if SOM is enabled on the object and if SOM cache exists and valid.
380 * Otherwise do not pack Size-on-MDS attributes to the reply. */
381 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
382 !S_ISREG(ma->ma_attr.la_mode) ||
383 !mdt_object_is_som_enabled(mo) ||
384 !(ma->ma_valid & MA_SOM))
387 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
388 b->mbo_size = ma->ma_som->msd_size;
389 b->mbo_blocks = ma->ma_som->msd_blocks;
392 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
393 const struct lu_attr *attr, const struct lu_fid *fid)
395 struct md_attr *ma = &info->mti_attr;
396 struct obd_export *exp = info->mti_exp;
397 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
399 LASSERT(ma->ma_valid & MA_INODE);
401 b->mbo_atime = attr->la_atime;
402 b->mbo_mtime = attr->la_mtime;
403 b->mbo_ctime = attr->la_ctime;
404 b->mbo_mode = attr->la_mode;
405 b->mbo_size = attr->la_size;
406 b->mbo_blocks = attr->la_blocks;
407 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
408 NODEMAP_FS_TO_CLIENT,
410 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
411 NODEMAP_FS_TO_CLIENT,
413 b->mbo_flags = attr->la_flags;
414 b->mbo_nlink = attr->la_nlink;
415 b->mbo_rdev = attr->la_rdev;
417 /* XXX should pack the reply body according to lu_valid */
418 b->mbo_valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
419 OBD_MD_FLGID | OBD_MD_FLTYPE |
420 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
421 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
423 if (!S_ISREG(attr->la_mode)) {
424 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
425 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
426 /* means no objects are allocated on osts. */
427 LASSERT(!(ma->ma_valid & MA_LOV));
428 /* just ignore blocks occupied by extend attributes on MDS */
430 /* if no object is allocated on osts, the size on mds is valid.
432 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
433 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
434 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
435 /* A released file stores its size on MDS. */
436 /* But return 1 block for released file, unless tools like tar
437 * will consider it fully sparse. (LU-3864)
439 if (unlikely(b->mbo_size == 0))
443 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
448 b->mbo_valid |= OBD_MD_FLID;
449 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
450 PFID(fid), b->mbo_nlink,
451 b->mbo_mode, b->mbo_size);
455 mdt_body_reverse_idmap(info, b);
457 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
458 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
459 PFID(fid), (unsigned long long)b->mbo_size);
462 static inline int mdt_body_has_lov(const struct lu_attr *la,
463 const struct mdt_body *body)
465 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
466 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
469 void mdt_client_compatibility(struct mdt_thread_info *info)
471 struct mdt_body *body;
472 struct ptlrpc_request *req = mdt_info_req(info);
473 struct obd_export *exp = req->rq_export;
474 struct md_attr *ma = &info->mti_attr;
475 struct lu_attr *la = &ma->ma_attr;
478 if (exp_connect_layout(exp))
479 /* the client can deal with 16-bit lmm_stripe_count */
482 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
484 if (!mdt_body_has_lov(la, body))
487 /* now we have a reply with a lov for a client not compatible with the
488 * layout lock so we have to clean the layout generation number */
489 if (S_ISREG(la->la_mode))
490 ma->ma_lmm->lmm_layout_gen = 0;
494 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
496 const struct lu_env *env = info->mti_env;
499 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
508 /* Is it a directory? Let's check for the LMV as well */
509 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
510 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
514 rc2 = mo_xattr_get(env, mdt_object_child(o),
516 XATTR_NAME_DEFAULT_LMV);
518 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
526 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
529 const struct lu_env *env = info->mti_env;
533 LASSERT(info->mti_big_lmm_used == 0);
534 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
538 /* big_lmm may need to be grown */
539 if (info->mti_big_lmmsize < rc) {
540 int size = size_roundup_power2(rc);
542 if (info->mti_big_lmmsize > 0) {
543 /* free old buffer */
544 LASSERT(info->mti_big_lmm);
545 OBD_FREE_LARGE(info->mti_big_lmm,
546 info->mti_big_lmmsize);
547 info->mti_big_lmm = NULL;
548 info->mti_big_lmmsize = 0;
551 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
552 if (info->mti_big_lmm == NULL)
554 info->mti_big_lmmsize = size;
556 LASSERT(info->mti_big_lmmsize >= rc);
558 info->mti_buf.lb_buf = info->mti_big_lmm;
559 info->mti_buf.lb_len = info->mti_big_lmmsize;
560 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
565 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
566 struct md_attr *ma, const char *name)
568 struct md_object *next = mdt_object_child(o);
569 struct lu_buf *buf = &info->mti_buf;
572 if (strcmp(name, XATTR_NAME_LOV) == 0) {
573 buf->lb_buf = ma->ma_lmm;
574 buf->lb_len = ma->ma_lmm_size;
575 LASSERT(!(ma->ma_valid & MA_LOV));
576 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
577 buf->lb_buf = ma->ma_lmv;
578 buf->lb_len = ma->ma_lmv_size;
579 LASSERT(!(ma->ma_valid & MA_LMV));
580 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
581 buf->lb_buf = ma->ma_lmv;
582 buf->lb_len = ma->ma_lmv_size;
583 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
588 rc = mo_xattr_get(info->mti_env, next, buf, name);
592 if (strcmp(name, XATTR_NAME_LOV) == 0) {
593 if (info->mti_big_lmm_used)
594 ma->ma_lmm = info->mti_big_lmm;
596 /* NOT return LOV EA with hole to old client. */
597 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
598 LOV_PATTERN_F_HOLE) &&
599 !(exp_connect_flags(info->mti_exp) &
600 OBD_CONNECT_LFSCK)) {
603 ma->ma_lmm_size = rc;
604 ma->ma_valid |= MA_LOV;
606 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
607 if (info->mti_big_lmm_used)
608 ma->ma_lmv = info->mti_big_lmm;
610 ma->ma_lmv_size = rc;
611 ma->ma_valid |= MA_LMV;
612 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
613 ma->ma_lmv_size = rc;
614 ma->ma_valid |= MA_LMV_DEF;
617 /* Update mdt_max_mdsize so all clients will be aware that */
618 if (info->mti_mdt->mdt_max_mdsize < rc)
619 info->mti_mdt->mdt_max_mdsize = rc;
622 } else if (rc == -ENODATA) {
625 } else if (rc == -ERANGE) {
626 /* Default LMV has fixed size, so it must be able to fit
627 * in the original buffer */
628 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
630 rc = mdt_big_xattr_get(info, o, name);
632 info->mti_big_lmm_used = 1;
640 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
641 struct mdt_object *o, struct lu_fid *pfid)
643 struct lu_buf *buf = &info->mti_buf;
644 struct link_ea_header *leh;
645 struct link_ea_entry *lee;
649 buf->lb_buf = info->mti_big_lmm;
650 buf->lb_len = info->mti_big_lmmsize;
651 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
652 buf, XATTR_NAME_LINK);
653 /* ignore errors, MA_PFID won't be set and it is
654 * up to the caller to treat this as an error */
655 if (rc == -ERANGE || buf->lb_len == 0) {
656 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
657 buf->lb_buf = info->mti_big_lmm;
658 buf->lb_len = info->mti_big_lmmsize;
663 if (rc < sizeof(*leh)) {
664 CERROR("short LinkEA on "DFID": rc = %d\n",
665 PFID(mdt_object_fid(o)), rc);
669 leh = (struct link_ea_header *) buf->lb_buf;
670 lee = (struct link_ea_entry *)(leh + 1);
671 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
672 leh->leh_magic = LINK_EA_MAGIC;
673 leh->leh_reccount = __swab32(leh->leh_reccount);
674 leh->leh_len = __swab64(leh->leh_len);
676 if (leh->leh_magic != LINK_EA_MAGIC)
678 if (leh->leh_reccount == 0)
681 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
682 fid_be_to_cpu(pfid, pfid);
687 int mdt_attr_get_complex(struct mdt_thread_info *info,
688 struct mdt_object *o, struct md_attr *ma)
690 const struct lu_env *env = info->mti_env;
691 struct md_object *next = mdt_object_child(o);
692 struct lu_buf *buf = &info->mti_buf;
693 int need = ma->ma_need;
700 if (mdt_object_exists(o) == 0)
701 GOTO(out, rc = -ENOENT);
702 mode = lu_object_attr(&next->mo_lu);
704 if (need & MA_INODE) {
705 ma->ma_need = MA_INODE;
706 rc = mo_attr_get(env, next, ma);
709 ma->ma_valid |= MA_INODE;
712 if (need & MA_PFID) {
713 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
715 ma->ma_valid |= MA_PFID;
716 /* ignore this error, parent fid is not mandatory */
720 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
721 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
726 if (need & MA_LMV && S_ISDIR(mode)) {
727 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
732 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
733 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
738 if (need & MA_SOM && S_ISREG(mode)) {
739 buf->lb_buf = info->mti_xattr_buf;
740 buf->lb_len = sizeof(info->mti_xattr_buf);
741 CLASSERT(sizeof(struct som_attrs) <=
742 sizeof(info->mti_xattr_buf));
743 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
744 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
746 ma->ma_valid |= MA_SOM;
747 else if (rc2 < 0 && rc2 != -ENODATA)
751 if (need & MA_HSM && S_ISREG(mode)) {
752 buf->lb_buf = info->mti_xattr_buf;
753 buf->lb_len = sizeof(info->mti_xattr_buf);
754 CLASSERT(sizeof(struct hsm_attrs) <=
755 sizeof(info->mti_xattr_buf));
756 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
757 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
759 ma->ma_valid |= MA_HSM;
760 else if (rc2 < 0 && rc2 != -ENODATA)
764 #ifdef CONFIG_FS_POSIX_ACL
765 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
766 buf->lb_buf = ma->ma_acl;
767 buf->lb_len = ma->ma_acl_size;
768 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
770 ma->ma_acl_size = rc2;
771 ma->ma_valid |= MA_ACL_DEF;
772 } else if (rc2 == -ENODATA) {
781 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
782 rc, ma->ma_valid, ma->ma_lmm);
786 static int mdt_getattr_internal(struct mdt_thread_info *info,
787 struct mdt_object *o, int ma_need)
789 struct md_object *next = mdt_object_child(o);
790 const struct mdt_body *reqbody = info->mti_body;
791 struct ptlrpc_request *req = mdt_info_req(info);
792 struct md_attr *ma = &info->mti_attr;
793 struct lu_attr *la = &ma->ma_attr;
794 struct req_capsule *pill = info->mti_pill;
795 const struct lu_env *env = info->mti_env;
796 struct mdt_body *repbody;
797 struct lu_buf *buffer = &info->mti_buf;
798 struct obd_export *exp = info->mti_exp;
799 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
804 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
805 RETURN(err_serious(-ENOMEM));
807 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
811 if (mdt_object_remote(o)) {
812 /* This object is located on remote node.*/
813 /* Return -ENOTSUPP for old client */
814 if (!mdt_is_dne_client(req->rq_export))
815 GOTO(out, rc = -ENOTSUPP);
817 repbody->mbo_fid1 = *mdt_object_fid(o);
818 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
822 if (reqbody->mbo_eadatasize > 0) {
823 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
824 if (buffer->lb_buf == NULL)
825 GOTO(out, rc = -EPROTO);
826 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
829 buffer->lb_buf = NULL;
831 ma_need &= ~(MA_LOV | MA_LMV);
832 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
833 mdt_obd_name(info->mti_mdt),
834 req->rq_export->exp_client_uuid.uuid);
837 /* If it is dir object and client require MEA, then we got MEA */
838 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
839 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
840 /* Assumption: MDT_MD size is enough for lmv size. */
841 ma->ma_lmv = buffer->lb_buf;
842 ma->ma_lmv_size = buffer->lb_len;
843 ma->ma_need = MA_INODE;
844 if (ma->ma_lmv_size > 0) {
845 if (reqbody->mbo_valid & OBD_MD_MEA)
846 ma->ma_need |= MA_LMV;
847 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
848 ma->ma_need |= MA_LMV_DEF;
851 ma->ma_lmm = buffer->lb_buf;
852 ma->ma_lmm_size = buffer->lb_len;
853 ma->ma_need = MA_INODE | MA_HSM;
854 if (ma->ma_lmm_size > 0)
855 ma->ma_need |= MA_LOV;
858 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
859 reqbody->mbo_valid & OBD_MD_FLDIREA &&
860 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
861 /* get default stripe info for this dir. */
862 ma->ma_need |= MA_LOV_DEF;
864 ma->ma_need |= ma_need;
865 if (ma->ma_need & MA_SOM)
866 ma->ma_som = &info->mti_u.som.data;
868 rc = mdt_attr_get_complex(info, o, ma);
870 CERROR("%s: getattr error for "DFID": rc = %d\n",
871 mdt_obd_name(info->mti_mdt),
872 PFID(mdt_object_fid(o)), rc);
876 /* if file is released, check if a restore is running */
877 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
878 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
879 repbody->mbo_t_state = MS_RESTORE;
880 repbody->mbo_valid |= OBD_MD_TSTATE;
883 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
885 /* the Lustre protocol supposes to return default striping
886 * on the user-visible root if explicitly requested */
887 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
888 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
889 struct lu_fid rootfid;
890 struct mdt_object *root;
891 struct mdt_device *mdt = info->mti_mdt;
893 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
896 root = mdt_object_find(env, mdt, &rootfid);
898 RETURN(PTR_ERR(root));
899 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
900 mdt_object_put(info->mti_env, root);
902 CERROR("%s: getattr error for "DFID": rc = %d\n",
903 mdt_obd_name(info->mti_mdt),
904 PFID(mdt_object_fid(o)), rc);
909 if (likely(ma->ma_valid & MA_INODE))
910 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
914 if (mdt_body_has_lov(la, reqbody)) {
915 if (ma->ma_valid & MA_LOV) {
916 LASSERT(ma->ma_lmm_size);
917 repbody->mbo_eadatasize = ma->ma_lmm_size;
918 if (S_ISDIR(la->la_mode))
919 repbody->mbo_valid |= OBD_MD_FLDIREA;
921 repbody->mbo_valid |= OBD_MD_FLEASIZE;
922 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
924 if (ma->ma_valid & MA_LMV) {
925 /* Return -ENOTSUPP for old client */
926 if (!mdt_is_striped_client(req->rq_export))
929 LASSERT(S_ISDIR(la->la_mode));
930 mdt_dump_lmv(D_INFO, ma->ma_lmv);
931 repbody->mbo_eadatasize = ma->ma_lmv_size;
932 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
934 if (ma->ma_valid & MA_LMV_DEF) {
935 /* Return -ENOTSUPP for old client */
936 if (!mdt_is_striped_client(req->rq_export))
938 LASSERT(S_ISDIR(la->la_mode));
939 repbody->mbo_eadatasize = ma->ma_lmv_size;
940 repbody->mbo_valid |= (OBD_MD_FLDIREA |
943 } else if (S_ISLNK(la->la_mode) &&
944 reqbody->mbo_valid & OBD_MD_LINKNAME) {
945 buffer->lb_buf = ma->ma_lmm;
946 /* eadatasize from client includes NULL-terminator, so
947 * there is no need to read it */
948 buffer->lb_len = reqbody->mbo_eadatasize - 1;
949 rc = mo_readlink(env, next, buffer);
950 if (unlikely(rc <= 0)) {
951 CERROR("%s: readlink failed for "DFID": rc = %d\n",
952 mdt_obd_name(info->mti_mdt),
953 PFID(mdt_object_fid(o)), rc);
956 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
958 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
960 repbody->mbo_valid |= OBD_MD_LINKNAME;
961 /* we need to report back size with NULL-terminator
962 * because client expects that */
963 repbody->mbo_eadatasize = rc + 1;
964 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
965 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
966 "on "DFID ", expected %d\n",
967 mdt_obd_name(info->mti_mdt),
968 rc, PFID(mdt_object_fid(o)),
969 reqbody->mbo_eadatasize - 1);
971 ((char *)ma->ma_lmm)[rc] = 0;
973 /* If the total CDEBUG() size is larger than a page, it
974 * will print a warning to the console, avoid this by
975 * printing just the last part of the symlink. */
976 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
977 print_limit < rc ? "..." : "", print_limit,
978 (char *)ma->ma_lmm + rc - print_limit, rc);
983 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
984 repbody->mbo_max_cookiesize = 0;
985 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
986 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
987 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
988 "MAX_COOKIE to : %d:%d\n", repbody->mbo_max_mdsize,
989 repbody->mbo_max_cookiesize);
992 if (exp_connect_rmtclient(info->mti_exp) &&
993 reqbody->mbo_valid & OBD_MD_FLRMTPERM) {
994 void *buf = req_capsule_server_get(pill, &RMF_ACL);
996 /* mdt_getattr_lock only */
997 rc = mdt_pack_remote_perm(info, o, buf);
999 repbody->mbo_valid &= ~OBD_MD_FLRMTPERM;
1000 repbody->mbo_aclsize = 0;
1003 repbody->mbo_valid |= OBD_MD_FLRMTPERM;
1004 repbody->mbo_aclsize = sizeof(struct mdt_remote_perm);
1007 #ifdef CONFIG_FS_POSIX_ACL
1008 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1009 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1010 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
1011 buffer->lb_len = req_capsule_get_size(pill,
1012 &RMF_ACL, RCL_SERVER);
1013 if (buffer->lb_len > 0) {
1014 rc = mo_xattr_get(env, next, buffer,
1015 XATTR_NAME_ACL_ACCESS);
1017 if (rc == -ENODATA) {
1018 repbody->mbo_aclsize = 0;
1019 repbody->mbo_valid |= OBD_MD_FLACL;
1021 } else if (rc == -EOPNOTSUPP) {
1024 CERROR("%s: unable to read "DFID
1026 mdt_obd_name(info->mti_mdt),
1027 PFID(mdt_object_fid(o)), rc);
1030 rc = nodemap_map_acl(nodemap, buffer->lb_buf,
1031 rc, NODEMAP_FS_TO_CLIENT);
1032 /* if all ACLs mapped out, rc is still >= 0 */
1034 CERROR("%s: nodemap_map_acl unable to"
1035 " parse "DFID" ACL: rc = %d\n",
1036 mdt_obd_name(info->mti_mdt),
1037 PFID(mdt_object_fid(o)), rc);
1039 repbody->mbo_aclsize = rc;
1040 repbody->mbo_valid |= OBD_MD_FLACL;
1048 if (reqbody->mbo_valid & OBD_MD_FLMDSCAPA &&
1049 info->mti_mdt->mdt_lut.lut_mds_capa &&
1050 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1051 struct lustre_capa *capa;
1053 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1055 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1056 rc = mo_capa_get(env, next, capa, 0);
1059 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
1064 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1069 static int mdt_renew_capa(struct mdt_thread_info *info)
1071 struct mdt_object *obj = info->mti_object;
1072 struct mdt_body *body;
1073 struct lustre_capa *capa, *c;
1077 /* if object doesn't exist, or server has disabled capability,
1078 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1081 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1082 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1085 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1086 LASSERT(body != NULL);
1088 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1091 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1095 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1097 body->mbo_valid |= OBD_MD_FLOSSCAPA;
1101 static int mdt_getattr(struct tgt_session_info *tsi)
1103 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1104 struct mdt_object *obj = info->mti_object;
1105 struct req_capsule *pill = info->mti_pill;
1106 struct mdt_body *reqbody;
1107 struct mdt_body *repbody;
1111 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1114 if (reqbody->mbo_valid & OBD_MD_FLOSSCAPA) {
1115 rc = req_capsule_server_pack(pill);
1117 RETURN(err_serious(rc));
1118 rc = mdt_renew_capa(info);
1119 GOTO(out_shrink, rc);
1122 LASSERT(obj != NULL);
1123 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1125 /* Unlike intent case where we need to pre-fill out buffers early on
1126 * in intent policy for ldlm reasons, here we can have a much better
1127 * guess at EA size by just reading it from disk.
1128 * Exceptions are readdir and (missing) directory striping */
1130 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1131 /* No easy way to know how long is the symlink, but it cannot
1132 * be more than PATH_MAX, so we allocate +1 */
1135 /* A special case for fs ROOT: getattr there might fetch
1136 * default EA for entire fs, not just for this dir!
1138 } else if (lu_fid_eq(mdt_object_fid(obj),
1139 &info->mti_mdt->mdt_md_root_fid) &&
1140 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1141 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1143 /* Should the default strping be bigger, mdt_fix_reply
1144 * will reallocate */
1145 rc = DEF_REP_MD_SIZE;
1147 /* Read the actual EA size from disk */
1148 rc = mdt_attr_get_eabuf_size(info, obj);
1152 GOTO(out, rc = err_serious(rc));
1154 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1156 rc = req_capsule_server_pack(pill);
1157 if (unlikely(rc != 0))
1158 GOTO(out, rc = err_serious(rc));
1160 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1161 LASSERT(repbody != NULL);
1162 repbody->mbo_eadatasize = 0;
1163 repbody->mbo_aclsize = 0;
1165 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1166 rc = mdt_init_ucred(info, reqbody);
1168 rc = mdt_check_ucred(info);
1170 GOTO(out_shrink, rc);
1172 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1175 * Don't check capability at all, because rename might getattr for
1176 * remote obj, and at that time no capability is available.
1178 mdt_set_capainfo(info, 1, &reqbody->mbo_fid1, BYPASS_CAPA);
1179 rc = mdt_getattr_internal(info, obj, 0);
1180 if (reqbody->mbo_valid & OBD_MD_FLRMTPERM)
1181 mdt_exit_ucred(info);
1184 mdt_client_compatibility(info);
1185 rc2 = mdt_fix_reply(info);
1189 mdt_thread_info_fini(info);
1193 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1195 struct mdt_thread_info *info;
1196 struct ptlrpc_request *req = tgt_ses_req(tsi);
1197 struct obd_export *exp = req->rq_export;
1198 struct mdt_object *o1, *o2, *o;
1199 struct mdt_lock_handle *lh1, *lh2;
1200 struct mdc_swap_layouts *msl;
1204 /* client does not support layout lock, so layout swaping
1206 * FIXME: there is a problem for old clients which don't support
1207 * layout lock yet. If those clients have already opened the file
1208 * they won't be notified at all so that old layout may still be
1209 * used to do IO. This can be fixed after file release is landed by
1210 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1211 if (!exp_connect_layout(exp))
1212 RETURN(-EOPNOTSUPP);
1214 info = tsi2mdt_info(tsi);
1216 if (info->mti_dlm_req != NULL)
1217 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1219 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1220 mdt_set_capainfo(info, 0, &info->mti_body->mbo_fid1,
1221 req_capsule_client_get(info->mti_pill,
1224 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1225 mdt_set_capainfo(info, 1, &info->mti_body->mbo_fid2,
1226 req_capsule_client_get(info->mti_pill,
1229 o1 = info->mti_object;
1230 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1231 &info->mti_body->mbo_fid2);
1233 GOTO(out, rc = PTR_ERR(o));
1235 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1236 GOTO(put, rc = -ENOENT);
1238 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1239 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1245 /* permission check. Make sure the calling process having permission
1246 * to write both files. */
1247 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1252 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1257 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1259 GOTO(put, rc = -EPROTO);
1261 lh1 = &info->mti_lh[MDT_LH_NEW];
1262 mdt_lock_reg_init(lh1, LCK_EX);
1263 lh2 = &info->mti_lh[MDT_LH_OLD];
1264 mdt_lock_reg_init(lh2, LCK_EX);
1266 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1267 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1271 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1272 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1276 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1277 mdt_object_child(o2), msl->msl_flags);
1280 mdt_object_unlock(info, o2, lh2, rc);
1282 mdt_object_unlock(info, o1, lh1, rc);
1284 mdt_object_put(info->mti_env, o);
1286 mdt_thread_info_fini(info);
1290 static int mdt_raw_lookup(struct mdt_thread_info *info,
1291 struct mdt_object *parent,
1292 const struct lu_name *lname,
1293 struct ldlm_reply *ldlm_rep)
1295 struct md_object *next = mdt_object_child(info->mti_object);
1296 const struct mdt_body *reqbody = info->mti_body;
1297 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1298 struct mdt_body *repbody;
1302 if (reqbody->mbo_valid != OBD_MD_FLID)
1305 LASSERT(!info->mti_cross_ref);
1307 /* Only got the fid of this obj by name */
1308 fid_zero(child_fid);
1309 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1312 /* XXX is raw_lookup possible as intent operation? */
1315 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1318 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1320 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1323 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1324 repbody->mbo_fid1 = *child_fid;
1325 repbody->mbo_valid = OBD_MD_FLID;
1331 * UPDATE lock should be taken against parent, and be release before exit;
1332 * child_bits lock should be taken against child, and be returned back:
1333 * (1)normal request should release the child lock;
1334 * (2)intent request will grant the lock to client.
1336 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1337 struct mdt_lock_handle *lhc,
1339 struct ldlm_reply *ldlm_rep)
1341 struct ptlrpc_request *req = mdt_info_req(info);
1342 struct mdt_body *reqbody = NULL;
1343 struct mdt_object *parent = info->mti_object;
1344 struct mdt_object *child;
1345 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1346 struct lu_name *lname = NULL;
1347 struct mdt_lock_handle *lhp = NULL;
1348 struct ldlm_lock *lock;
1355 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1356 LASSERT(ergo(is_resent,
1357 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1362 if (info->mti_cross_ref) {
1363 /* Only getattr on the child. Parent is on another node. */
1364 mdt_set_disposition(info, ldlm_rep,
1365 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1367 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1369 PFID(mdt_object_fid(child)), ldlm_rep);
1371 rc = mdt_check_resent_lock(info, child, lhc);
1374 } else if (rc > 0) {
1375 mdt_lock_handle_init(lhc);
1376 mdt_lock_reg_init(lhc, LCK_PR);
1379 * Object's name is on another MDS, no lookup or layout
1380 * lock is needed here but update lock is.
1382 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1383 MDS_INODELOCK_LAYOUT);
1384 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1386 rc = mdt_object_lock(info, child, lhc, child_bits,
1392 /* Finally, we can get attr for child. */
1393 if (!mdt_object_exists(child)) {
1394 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1396 "remote object doesn't exist.\n");
1397 mdt_object_unlock(info, child, lhc, 1);
1401 mdt_set_capainfo(info, 0, mdt_object_fid(child), BYPASS_CAPA);
1402 rc = mdt_getattr_internal(info, child, 0);
1403 if (unlikely(rc != 0))
1404 mdt_object_unlock(info, child, lhc, 1);
1409 lname = &info->mti_name;
1410 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1412 if (lu_name_is_valid(lname)) {
1413 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1414 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1415 PNAME(lname), ldlm_rep);
1417 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1418 if (unlikely(reqbody == NULL))
1419 RETURN(err_serious(-EPROTO));
1421 *child_fid = reqbody->mbo_fid2;
1423 if (unlikely(!fid_is_sane(child_fid)))
1424 RETURN(err_serious(-EINVAL));
1426 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1428 PFID(mdt_object_fid(parent)),
1429 PFID(&reqbody->mbo_fid2), ldlm_rep);
1432 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1434 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1435 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1437 "Parent doesn't exist!\n");
1441 if (mdt_object_remote(parent)) {
1442 CERROR("%s: parent "DFID" is on remote target\n",
1443 mdt_obd_name(info->mti_mdt),
1444 PFID(mdt_object_fid(parent)));
1448 if (lu_name_is_valid(lname)) {
1449 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1456 /* step 1: lock parent only if parent is a directory */
1457 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1458 lhp = &info->mti_lh[MDT_LH_PARENT];
1459 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1460 rc = mdt_object_lock(info, parent, lhp,
1461 MDS_INODELOCK_UPDATE,
1463 if (unlikely(rc != 0))
1467 /* step 2: lookup child's fid by name */
1468 fid_zero(child_fid);
1469 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1470 child_fid, &info->mti_spec);
1472 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1475 GOTO(out_parent, rc);
1478 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1481 *step 3: find the child object by fid & lock it.
1482 * regardless if it is local or remote.
1484 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1485 * set parent dir fid the same as child fid in getattr by fid case
1486 * we should not lu_object_find() the object again, could lead
1487 * to hung if there is a concurrent unlink destroyed the object.
1489 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1490 mdt_object_get(info->mti_env, parent);
1493 child = mdt_object_find(info->mti_env, info->mti_mdt,
1497 if (unlikely(IS_ERR(child)))
1498 GOTO(out_parent, rc = PTR_ERR(child));
1500 rc = mdt_check_resent_lock(info, child, lhc);
1502 GOTO(out_child, rc);
1503 } else if (rc > 0) {
1504 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1505 mdt_lock_handle_init(lhc);
1506 mdt_lock_reg_init(lhc, LCK_PR);
1509 if (!mdt_object_exists(child)) {
1510 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1512 "Object doesn't exist!\n");
1513 GOTO(out_child, rc = -ENOENT);
1516 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1517 mdt_object_exists(child) && !mdt_object_remote(child)) {
1518 struct md_attr *ma = &info->mti_attr;
1521 ma->ma_need = MA_INODE;
1522 rc = mdt_attr_get_complex(info, child, ma);
1523 if (unlikely(rc != 0))
1524 GOTO(out_child, rc);
1526 /* If the file has not been changed for some time, we
1527 * return not only a LOOKUP lock, but also an UPDATE
1528 * lock and this might save us RPC on later STAT. For
1529 * directories, it also let negative dentry cache start
1530 * working for this dir. */
1531 if (ma->ma_valid & MA_INODE &&
1532 ma->ma_attr.la_valid & LA_CTIME &&
1533 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1534 ma->ma_attr.la_ctime < cfs_time_current_sec())
1535 child_bits |= MDS_INODELOCK_UPDATE;
1538 /* layout lock must be granted in a best-effort way
1539 * for IT operations */
1540 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1541 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1542 exp_connect_layout(info->mti_exp) &&
1543 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1544 !mdt_object_remote(child) && ldlm_rep != NULL) {
1545 /* try to grant layout lock for regular file. */
1551 child_bits |= MDS_INODELOCK_LAYOUT;
1552 /* try layout lock, it may fail to be granted due to
1553 * contention at LOOKUP or UPDATE */
1554 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1556 child_bits &= ~MDS_INODELOCK_LAYOUT;
1557 LASSERT(child_bits != 0);
1558 rc = mdt_object_lock(info, child, lhc,
1559 child_bits, MDT_CROSS_LOCK);
1564 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1565 * client will enqueue the lock to the remote MDT */
1566 if (mdt_object_remote(child))
1567 child_bits &= ~MDS_INODELOCK_UPDATE;
1568 rc = mdt_object_lock(info, child, lhc, child_bits,
1571 if (unlikely(rc != 0))
1572 GOTO(out_child, rc);
1575 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1576 /* Get MA_SOM attributes if update lock is given. */
1578 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1579 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1582 /* finally, we can get attr for child. */
1583 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1584 rc = mdt_getattr_internal(info, child, ma_need);
1585 if (unlikely(rc != 0)) {
1586 mdt_object_unlock(info, child, lhc, 1);
1588 /* Debugging code. */
1589 LDLM_DEBUG(lock, "Returning lock to client");
1590 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1591 &lock->l_resource->lr_name),
1592 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1593 PLDLMRES(lock->l_resource),
1594 PFID(mdt_object_fid(child)));
1595 if (mdt_object_exists(child) && !mdt_object_remote(child))
1596 mdt_pack_size2body(info, child);
1599 LDLM_LOCK_PUT(lock);
1603 mdt_object_put(info->mti_env, child);
1606 mdt_object_unlock(info, parent, lhp, 1);
1610 /* normal handler: should release the child lock */
1611 static int mdt_getattr_name(struct tgt_session_info *tsi)
1613 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1614 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1615 struct mdt_body *reqbody;
1616 struct mdt_body *repbody;
1620 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1621 LASSERT(reqbody != NULL);
1622 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1623 LASSERT(repbody != NULL);
1625 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1626 repbody->mbo_eadatasize = 0;
1627 repbody->mbo_aclsize = 0;
1629 rc = mdt_init_ucred(info, reqbody);
1631 GOTO(out_shrink, rc);
1633 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1634 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1635 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1636 lhc->mlh_reg_lh.cookie = 0;
1638 mdt_exit_ucred(info);
1641 mdt_client_compatibility(info);
1642 rc2 = mdt_fix_reply(info);
1645 mdt_thread_info_fini(info);
1649 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1650 void *karg, void *uarg);
1652 static int mdt_set_info(struct tgt_session_info *tsi)
1654 struct ptlrpc_request *req = tgt_ses_req(tsi);
1657 int keylen, vallen, rc = 0;
1661 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1663 DEBUG_REQ(D_HA, req, "no set_info key");
1664 RETURN(err_serious(-EFAULT));
1667 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1670 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1672 DEBUG_REQ(D_HA, req, "no set_info val");
1673 RETURN(err_serious(-EFAULT));
1676 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1679 /* Swab any part of val you need to here */
1680 if (KEY_IS(KEY_READ_ONLY)) {
1681 spin_lock(&req->rq_export->exp_lock);
1683 *exp_connect_flags_ptr(req->rq_export) |=
1686 *exp_connect_flags_ptr(req->rq_export) &=
1687 ~OBD_CONNECT_RDONLY;
1688 spin_unlock(&req->rq_export->exp_lock);
1689 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1690 struct changelog_setinfo *cs = val;
1692 if (vallen != sizeof(*cs)) {
1693 CERROR("%s: bad changelog_clear setinfo size %d\n",
1694 tgt_name(tsi->tsi_tgt), vallen);
1697 if (ptlrpc_req_need_swab(req)) {
1698 __swab64s(&cs->cs_recno);
1699 __swab32s(&cs->cs_id);
1702 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1710 static int mdt_readpage(struct tgt_session_info *tsi)
1712 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1713 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1714 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1715 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1716 struct mdt_body *repbody;
1722 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1723 RETURN(err_serious(-ENOMEM));
1725 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1726 if (repbody == NULL || reqbody == NULL)
1727 RETURN(err_serious(-EFAULT));
1730 * prepare @rdpg before calling lower layers and transfer itself. Here
1731 * reqbody->size contains offset of where to start to read and
1732 * reqbody->nlink contains number bytes to read.
1734 rdpg->rp_hash = reqbody->mbo_size;
1735 if (rdpg->rp_hash != reqbody->mbo_size) {
1736 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1737 rdpg->rp_hash, reqbody->mbo_size);
1741 rdpg->rp_attrs = reqbody->mbo_mode;
1742 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1743 rdpg->rp_attrs |= LUDA_64BITHASH;
1744 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1745 exp_max_brw_size(tsi->tsi_exp));
1746 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1748 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1749 if (rdpg->rp_pages == NULL)
1752 for (i = 0; i < rdpg->rp_npages; ++i) {
1753 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1754 if (rdpg->rp_pages[i] == NULL)
1755 GOTO(free_rdpg, rc = -ENOMEM);
1758 /* call lower layers to fill allocated pages with directory data */
1759 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1761 GOTO(free_rdpg, rc);
1763 /* send pages to client */
1764 rc = tgt_sendpage(tsi, rdpg, rc);
1769 for (i = 0; i < rdpg->rp_npages; i++)
1770 if (rdpg->rp_pages[i] != NULL)
1771 __free_page(rdpg->rp_pages[i]);
1772 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1774 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1780 static int mdt_reint_internal(struct mdt_thread_info *info,
1781 struct mdt_lock_handle *lhc,
1784 struct req_capsule *pill = info->mti_pill;
1785 struct mdt_body *repbody;
1790 rc = mdt_reint_unpack(info, op);
1792 CERROR("Can't unpack reint, rc %d\n", rc);
1793 RETURN(err_serious(rc));
1796 /* for replay (no_create) lmm is not needed, client has it already */
1797 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1798 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1801 /* llog cookies are always 0, the field is kept for compatibility */
1802 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1803 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1805 rc = req_capsule_server_pack(pill);
1807 CERROR("Can't pack response, rc %d\n", rc);
1808 RETURN(err_serious(rc));
1811 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1812 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1814 repbody->mbo_eadatasize = 0;
1815 repbody->mbo_aclsize = 0;
1818 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1820 /* for replay no cookkie / lmm need, because client have this already */
1821 if (info->mti_spec.no_create)
1822 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1823 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1825 rc = mdt_init_ucred_reint(info);
1827 GOTO(out_shrink, rc);
1829 rc = mdt_fix_attr_ucred(info, op);
1831 GOTO(out_ucred, rc = err_serious(rc));
1833 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1834 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1835 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1836 GOTO(out_ucred, rc);
1838 rc = mdt_reint_rec(info, lhc);
1841 mdt_exit_ucred(info);
1843 mdt_client_compatibility(info);
1844 rc2 = mdt_fix_reply(info);
1850 static long mdt_reint_opcode(struct ptlrpc_request *req,
1851 const struct req_format **fmt)
1853 struct mdt_device *mdt;
1854 struct mdt_rec_reint *rec;
1857 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1859 opc = rec->rr_opcode;
1860 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1861 if (opc < REINT_MAX && fmt[opc] != NULL)
1862 req_capsule_extend(&req->rq_pill, fmt[opc]);
1864 mdt = mdt_exp2dev(req->rq_export);
1865 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1866 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1867 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1868 opc = err_serious(-EFAULT);
1871 opc = err_serious(-EFAULT);
1876 static int mdt_reint(struct tgt_session_info *tsi)
1880 static const struct req_format *reint_fmts[REINT_MAX] = {
1881 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1882 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1883 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1884 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1885 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1886 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1887 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1888 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1889 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1894 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1896 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1898 * No lock possible here from client to pass it to reint code
1901 rc = mdt_reint_internal(info, NULL, opc);
1902 mdt_thread_info_fini(info);
1907 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1911 /* this should sync the whole device */
1912 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1914 struct dt_device *dt = mdt->mdt_bottom;
1918 rc = dt->dd_ops->dt_sync(env, dt);
1922 /* this should sync this object */
1923 static int mdt_object_sync(struct mdt_thread_info *info)
1925 struct md_object *next;
1929 if (!mdt_object_exists(info->mti_object)) {
1930 CWARN("%s: non existing object "DFID": rc = %d\n",
1931 mdt_obd_name(info->mti_mdt),
1932 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1935 next = mdt_object_child(info->mti_object);
1936 rc = mo_object_sync(info->mti_env, next);
1941 static int mdt_sync(struct tgt_session_info *tsi)
1943 struct ptlrpc_request *req = tgt_ses_req(tsi);
1944 struct req_capsule *pill = tsi->tsi_pill;
1945 struct mdt_body *body;
1950 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1951 RETURN(err_serious(-ENOMEM));
1953 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
1954 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1956 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1958 /* sync an object */
1959 rc = mdt_object_sync(info);
1961 const struct lu_fid *fid;
1962 struct lu_attr *la = &info->mti_attr.ma_attr;
1964 info->mti_attr.ma_need = MA_INODE;
1965 info->mti_attr.ma_valid = 0;
1966 rc = mdt_attr_get_complex(info, info->mti_object,
1969 body = req_capsule_server_get(pill,
1971 fid = mdt_object_fid(info->mti_object);
1972 mdt_pack_attr2body(info, body, la, fid);
1975 mdt_thread_info_fini(info);
1978 mdt_counter_incr(req, LPROC_MDT_SYNC);
1984 * Handle quota control requests to consult current usage/limit, but also
1985 * to configure quota enforcement
1987 static int mdt_quotactl(struct tgt_session_info *tsi)
1989 struct obd_export *exp = tsi->tsi_exp;
1990 struct req_capsule *pill = tsi->tsi_pill;
1991 struct obd_quotactl *oqctl, *repoqc;
1993 struct mdt_device *mdt = mdt_exp2dev(exp);
1994 struct lu_device *qmt = mdt->mdt_qmt_dev;
1995 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
1998 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2000 RETURN(err_serious(-EPROTO));
2002 rc = req_capsule_server_pack(pill);
2004 RETURN(err_serious(rc));
2006 switch (oqctl->qc_cmd) {
2008 case LUSTRE_Q_INVALIDATE:
2009 case LUSTRE_Q_FINVALIDATE:
2013 /* deprecated, not used any more */
2014 RETURN(-EOPNOTSUPP);
2015 /* master quotactl */
2018 if (!nodemap_can_setquota(nodemap))
2023 RETURN(-EOPNOTSUPP);
2024 /* slave quotactl */
2029 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2033 /* map uid/gid for remote client */
2035 if (exp_connect_rmtclient(exp)) {
2036 struct lustre_idmap_table *idmap;
2038 idmap = exp->exp_mdt_data.med_idmap;
2040 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2041 oqctl->qc_cmd != Q_GETINFO))
2044 if (oqctl->qc_type == USRQUOTA)
2045 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2047 else if (oqctl->qc_type == GRPQUOTA)
2048 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2053 if (id == CFS_IDMAP_NOTFOUND) {
2054 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2059 if (oqctl->qc_type == USRQUOTA)
2060 id = nodemap_map_id(nodemap, NODEMAP_UID,
2061 NODEMAP_CLIENT_TO_FS, id);
2062 else if (oqctl->qc_type == GRPQUOTA)
2063 id = nodemap_map_id(nodemap, NODEMAP_UID,
2064 NODEMAP_CLIENT_TO_FS, id);
2066 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2068 RETURN(err_serious(-EFAULT));
2070 if (oqctl->qc_id != id)
2071 swap(oqctl->qc_id, id);
2073 switch (oqctl->qc_cmd) {
2079 /* forward quotactl request to QMT */
2080 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2085 /* slave quotactl */
2086 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2091 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2095 if (oqctl->qc_id != id)
2096 swap(oqctl->qc_id, id);
2102 /** clone llog ctxt from child (mdd)
2103 * This allows remote llog (replicator) access.
2104 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2105 * context was originally set up, or we can handle them directly.
2106 * I choose the latter, but that means I need any llog
2107 * contexts set up by child to be accessable by the mdt. So we clone the
2108 * context into our context list here.
2110 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2113 struct md_device *next = mdt->mdt_child;
2114 struct llog_ctxt *ctxt;
2117 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2120 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2121 if (rc || ctxt == NULL) {
2125 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2127 CERROR("Can't set mdt ctxt %d\n", rc);
2132 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2133 struct mdt_device *mdt, int idx)
2135 struct llog_ctxt *ctxt;
2137 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2140 /* Put once for the get we just did, and once for the clone */
2141 llog_ctxt_put(ctxt);
2142 llog_ctxt_put(ctxt);
2147 * sec context handlers
2149 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2153 rc = mdt_handle_idmap(tsi);
2155 struct ptlrpc_request *req = tgt_ses_req(tsi);
2158 opc = lustre_msg_get_opc(req->rq_reqmsg);
2159 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2160 sptlrpc_svc_ctx_invalidate(req);
2163 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2169 * quota request handlers
2171 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2173 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2174 struct lu_device *qmt = mdt->mdt_qmt_dev;
2179 RETURN(err_serious(-EOPNOTSUPP));
2181 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2185 struct mdt_object *mdt_object_new(const struct lu_env *env,
2186 struct mdt_device *d,
2187 const struct lu_fid *f)
2189 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2190 struct lu_object *o;
2191 struct mdt_object *m;
2194 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2195 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2196 if (unlikely(IS_ERR(o)))
2197 m = (struct mdt_object *)o;
2203 struct mdt_object *mdt_object_find(const struct lu_env *env,
2204 struct mdt_device *d,
2205 const struct lu_fid *f)
2207 struct lu_object *o;
2208 struct mdt_object *m;
2211 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2212 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2213 if (unlikely(IS_ERR(o)))
2214 m = (struct mdt_object *)o;
2222 * Asyncronous commit for mdt device.
2224 * Pass asynchonous commit call down the MDS stack.
2226 * \param env environment
2227 * \param mdt the mdt device
2229 static void mdt_device_commit_async(const struct lu_env *env,
2230 struct mdt_device *mdt)
2232 struct dt_device *dt = mdt->mdt_bottom;
2235 rc = dt->dd_ops->dt_commit_async(env, dt);
2236 if (unlikely(rc != 0))
2237 CWARN("%s: async commit start failed: rc = %d\n",
2238 mdt_obd_name(mdt), rc);
2242 * Mark the lock as "synchonous".
2244 * Mark the lock to deffer transaction commit to the unlock time.
2246 * \param lock the lock to mark as "synchonous"
2248 * \see mdt_is_lock_sync
2249 * \see mdt_save_lock
2251 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2253 lock->l_ast_data = (void*)1;
2257 * Check whehter the lock "synchonous" or not.
2259 * \param lock the lock to check
2260 * \retval 1 the lock is "synchonous"
2261 * \retval 0 the lock isn't "synchronous"
2263 * \see mdt_set_lock_sync
2264 * \see mdt_save_lock
2266 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2268 return lock->l_ast_data != NULL;
2272 * Blocking AST for mdt locks.
2274 * Starts transaction commit if in case of COS lock conflict or
2275 * deffers such a commit to the mdt_save_lock.
2277 * \param lock the lock which blocks a request or cancelling lock
2278 * \param desc unused
2279 * \param data unused
2280 * \param flag indicates whether this cancelling or blocking callback
2282 * \see ldlm_blocking_ast_nocheck
2284 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2285 void *data, int flag)
2287 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2288 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2292 if (flag == LDLM_CB_CANCELING)
2294 lock_res_and_lock(lock);
2295 if (lock->l_blocking_ast != mdt_blocking_ast) {
2296 unlock_res_and_lock(lock);
2299 if (mdt_cos_is_enabled(mdt) &&
2300 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2301 lock->l_blocking_lock != NULL &&
2302 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2303 mdt_set_lock_sync(lock);
2305 rc = ldlm_blocking_ast_nocheck(lock);
2307 /* There is no lock conflict if l_blocking_lock == NULL,
2308 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2309 * when the last reference to a local lock was released */
2310 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2313 rc = lu_env_init(&env, LCT_LOCAL);
2314 if (unlikely(rc != 0))
2315 CWARN("%s: lu_env initialization failed, cannot "
2316 "start asynchronous commit: rc = %d\n",
2319 mdt_device_commit_async(&env, mdt);
2325 /* Used for cross-MDT lock */
2326 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2327 void *data, int flag)
2329 struct lustre_handle lockh;
2333 case LDLM_CB_BLOCKING:
2334 ldlm_lock2handle(lock, &lockh);
2335 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2337 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2341 case LDLM_CB_CANCELING:
2342 LDLM_DEBUG(lock, "Revoke remote lock\n");
2350 int mdt_check_resent_lock(struct mdt_thread_info *info,
2351 struct mdt_object *mo,
2352 struct mdt_lock_handle *lhc)
2354 /* the lock might already be gotten in ldlm_handle_enqueue() */
2355 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2356 struct ptlrpc_request *req = mdt_info_req(info);
2357 struct ldlm_lock *lock;
2359 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2360 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2362 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2363 * a resend case, however, it could be already destroyed
2364 * due to client eviction or a raced cancel RPC. */
2365 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
2366 lhc->mlh_reg_lh.cookie);
2370 if (!fid_res_name_eq(mdt_object_fid(mo),
2371 &lock->l_resource->lr_name)) {
2372 CWARN("%s: Although resent, but still not "
2373 "get child lock:"DFID"\n",
2374 info->mti_exp->exp_obd->obd_name,
2375 PFID(mdt_object_fid(mo)));
2376 LDLM_LOCK_PUT(lock);
2379 LDLM_LOCK_PUT(lock);
2385 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2386 struct mdt_object *o, const struct lu_fid *fid,
2387 struct lustre_handle *lh, ldlm_mode_t mode,
2390 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2391 ldlm_policy_data_t *policy = &mti->mti_policy;
2392 struct ldlm_res_id *res_id = &mti->mti_res_id;
2396 LASSERT(mdt_object_remote(o));
2398 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2400 fid_build_reg_res_name(fid, res_id);
2402 memset(einfo, 0, sizeof(*einfo));
2403 einfo->ei_type = LDLM_IBITS;
2404 einfo->ei_mode = mode;
2405 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2406 einfo->ei_cb_cp = ldlm_completion_ast;
2407 einfo->ei_enq_slave = 0;
2408 einfo->ei_res_id = res_id;
2410 memset(policy, 0, sizeof(*policy));
2411 policy->l_inodebits.bits = ibits;
2413 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2418 static int mdt_object_local_lock(struct mdt_thread_info *info,
2419 struct mdt_object *o,
2420 struct mdt_lock_handle *lh, __u64 ibits,
2421 bool nonblock, int locality)
2423 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2424 ldlm_policy_data_t *policy = &info->mti_policy;
2425 struct ldlm_res_id *res_id = &info->mti_res_id;
2430 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2431 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2432 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2433 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2435 /* Only enqueue LOOKUP lock for remote object */
2436 if (mdt_object_remote(o))
2437 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2439 if (lh->mlh_type == MDT_PDO_LOCK) {
2440 /* check for exists after object is locked */
2441 if (mdt_object_exists(o) == 0) {
2442 /* Non-existent object shouldn't have PDO lock */
2445 /* Non-dir object shouldn't have PDO lock */
2446 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2451 memset(policy, 0, sizeof(*policy));
2452 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2454 dlmflags = LDLM_FL_ATOMIC_CB;
2456 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2459 * Take PDO lock on whole directory and build correct @res_id for lock
2460 * on part of directory.
2462 if (lh->mlh_pdo_hash != 0) {
2463 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2464 mdt_lock_pdo_mode(info, o, lh);
2465 if (lh->mlh_pdo_mode != LCK_NL) {
2467 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2468 * is never going to be sent to client and we do not
2469 * want it slowed down due to possible cancels.
2471 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2472 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2473 policy, res_id, dlmflags,
2474 info->mti_exp == NULL ? NULL :
2475 &info->mti_exp->exp_handle.h_cookie);
2476 if (unlikely(rc != 0))
2477 GOTO(out_unlock, rc);
2481 * Finish res_id initializing by name hash marking part of
2482 * directory which is taking modification.
2484 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2487 policy->l_inodebits.bits = ibits;
2490 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2491 * going to be sent to client. If it is - mdt_intent_policy() path will
2492 * fix it up and turn FL_LOCAL flag off.
2494 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2495 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2496 info->mti_exp == NULL ? NULL :
2497 &info->mti_exp->exp_handle.h_cookie);
2500 mdt_object_unlock(info, o, lh, 1);
2501 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2502 lh->mlh_pdo_hash != 0 &&
2503 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2504 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2510 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2511 struct mdt_lock_handle *lh, __u64 ibits,
2512 bool nonblock, int locality)
2517 if (!mdt_object_remote(o))
2518 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2521 if (locality == MDT_LOCAL_LOCK) {
2522 CERROR("%s: try to get local lock for remote object"
2523 DFID".\n", mdt_obd_name(info->mti_mdt),
2524 PFID(mdt_object_fid(o)));
2528 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2529 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2530 MDS_INODELOCK_XATTR);
2531 if (ibits & MDS_INODELOCK_UPDATE) {
2532 /* Sigh, PDO needs to enqueue 2 locks right now, but
2533 * enqueue RPC can only request 1 lock, to avoid extra
2534 * RPC, so it will instead enqueue EX lock for remote
2535 * object anyway XXX*/
2536 if (lh->mlh_type == MDT_PDO_LOCK &&
2537 lh->mlh_pdo_hash != 0) {
2538 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2539 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2540 PFID(mdt_object_fid(o)));
2541 lh->mlh_pdo_hash = 0;
2542 lh->mlh_rreg_mode = LCK_EX;
2543 lh->mlh_type = MDT_REG_LOCK;
2545 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2548 MDS_INODELOCK_UPDATE);
2553 /* Only enqueue LOOKUP lock for remote object */
2554 if (ibits & MDS_INODELOCK_LOOKUP) {
2555 rc = mdt_object_local_lock(info, o, lh,
2556 MDS_INODELOCK_LOOKUP,
2557 nonblock, locality);
2565 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2566 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2568 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2571 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2572 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2574 struct mdt_lock_handle tmp = *lh;
2577 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2585 * Save a lock within request object.
2587 * Keep the lock referenced until whether client ACK or transaction
2588 * commit happens or release the lock immediately depending on input
2589 * parameters. If COS is ON, a write lock is converted to COS lock
2592 * \param info thead info object
2593 * \param h lock handle
2594 * \param mode lock mode
2595 * \param decref force immediate lock releasing
2598 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2599 ldlm_mode_t mode, int decref)
2603 if (lustre_handle_is_used(h)) {
2604 if (decref || !info->mti_has_trans ||
2605 !(mode & (LCK_PW | LCK_EX))){
2606 mdt_fid_unlock(h, mode);
2608 struct mdt_device *mdt = info->mti_mdt;
2609 struct ldlm_lock *lock = ldlm_handle2lock(h);
2610 struct ptlrpc_request *req = mdt_info_req(info);
2613 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2615 /* there is no request if mdt_object_unlock() is called
2616 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2617 if (likely(req != NULL)) {
2618 CDEBUG(D_HA, "request = %p reply state = %p"
2619 " transno = "LPD64"\n", req,
2620 req->rq_reply_state, req->rq_transno);
2621 if (mdt_cos_is_enabled(mdt)) {
2623 ldlm_lock_downgrade(lock, LCK_COS);
2626 ptlrpc_save_lock(req, h, mode, no_ack);
2628 ldlm_lock_decref(h, mode);
2630 if (mdt_is_lock_sync(lock)) {
2631 CDEBUG(D_HA, "found sync-lock,"
2632 " async commit started\n");
2633 mdt_device_commit_async(info->mti_env,
2636 LDLM_LOCK_PUT(lock);
2645 * Unlock mdt object.
2647 * Immeditely release the regular lock and the PDO lock or save the
2648 * lock in reqeuest and keep them referenced until client ACK or
2649 * transaction commit.
2651 * \param info thread info object
2652 * \param o mdt object
2653 * \param lh mdt lock handle referencing regular and PDO locks
2654 * \param decref force immediate lock releasing
2656 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2657 struct mdt_lock_handle *lh, int decref)
2661 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2662 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2664 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2665 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2670 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2671 const struct lu_fid *f,
2672 struct mdt_lock_handle *lh,
2675 struct mdt_object *o;
2677 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2681 rc = mdt_object_lock(info, o, lh, ibits,
2684 mdt_object_put(info->mti_env, o);
2691 void mdt_object_unlock_put(struct mdt_thread_info * info,
2692 struct mdt_object * o,
2693 struct mdt_lock_handle *lh,
2696 mdt_object_unlock(info, o, lh, decref);
2697 mdt_object_put(info->mti_env, o);
2701 * Generic code handling requests that have struct mdt_body passed in:
2703 * - extract mdt_body from request and save it in @info, if present;
2705 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2708 * - if HABEO_CORPUS flag is set for this request type check whether object
2709 * actually exists on storage (lu_object_exists()).
2712 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2714 const struct mdt_body *body;
2715 struct mdt_object *obj;
2716 const struct lu_env *env;
2717 struct req_capsule *pill;
2721 env = info->mti_env;
2722 pill = info->mti_pill;
2724 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2728 if (!(body->mbo_valid & OBD_MD_FLID))
2731 if (!fid_is_sane(&body->mbo_fid1)) {
2732 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2737 * Do not get size or any capa fields before we check that request
2738 * contains capa actually. There are some requests which do not, for
2739 * instance MDS_IS_SUBDIR.
2741 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2742 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2743 mdt_set_capainfo(info, 0, &body->mbo_fid1,
2744 req_capsule_client_get(pill, &RMF_CAPA1));
2746 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2748 if ((flags & HABEO_CORPUS) &&
2749 !mdt_object_exists(obj)) {
2750 mdt_object_put(env, obj);
2751 /* for capability renew ENOENT will be handled in
2753 if (body->mbo_valid & OBD_MD_FLOSSCAPA)
2758 info->mti_object = obj;
2767 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2769 struct req_capsule *pill = info->mti_pill;
2773 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2774 rc = mdt_body_unpack(info, flags);
2778 if (rc == 0 && (flags & HABEO_REFERO)) {
2780 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2781 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2783 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2784 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2787 rc = req_capsule_server_pack(pill);
2792 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2794 struct md_device *next = m->mdt_child;
2796 return next->md_ops->mdo_init_capa_ctxt(env, next,
2797 m->mdt_lut.lut_mds_capa,
2798 m->mdt_capa_timeout,
2803 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2805 lh->mlh_type = MDT_NUL_LOCK;
2806 lh->mlh_reg_lh.cookie = 0ull;
2807 lh->mlh_reg_mode = LCK_MINMODE;
2808 lh->mlh_pdo_lh.cookie = 0ull;
2809 lh->mlh_pdo_mode = LCK_MINMODE;
2810 lh->mlh_rreg_lh.cookie = 0ull;
2811 lh->mlh_rreg_mode = LCK_MINMODE;
2814 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2816 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2817 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2821 * Initialize fields of struct mdt_thread_info. Other fields are left in
2822 * uninitialized state, because it's too expensive to zero out whole
2823 * mdt_thread_info (> 1K) on each request arrival.
2825 void mdt_thread_info_init(struct ptlrpc_request *req,
2826 struct mdt_thread_info *info)
2830 info->mti_pill = &req->rq_pill;
2833 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2834 mdt_lock_handle_init(&info->mti_lh[i]);
2836 /* mdt device: it can be NULL while CONNECT */
2837 if (req->rq_export) {
2838 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2839 info->mti_exp = req->rq_export;
2841 info->mti_mdt = NULL;
2842 info->mti_env = req->rq_svc_thread->t_env;
2843 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2845 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2846 info->mti_big_buf = LU_BUF_NULL;
2847 info->mti_body = NULL;
2848 info->mti_object = NULL;
2849 info->mti_dlm_req = NULL;
2850 info->mti_has_trans = 0;
2851 info->mti_cross_ref = 0;
2852 info->mti_opdata = 0;
2853 info->mti_big_lmm_used = 0;
2855 info->mti_spec.no_create = 0;
2856 info->mti_spec.sp_rm_entry = 0;
2858 info->mti_spec.u.sp_ea.eadata = NULL;
2859 info->mti_spec.u.sp_ea.eadatalen = 0;
2862 void mdt_thread_info_fini(struct mdt_thread_info *info)
2866 if (info->mti_object != NULL) {
2867 mdt_object_put(info->mti_env, info->mti_object);
2868 info->mti_object = NULL;
2871 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2872 mdt_lock_handle_fini(&info->mti_lh[i]);
2873 info->mti_env = NULL;
2874 info->mti_pill = NULL;
2875 info->mti_exp = NULL;
2877 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2878 lu_buf_free(&info->mti_big_buf);
2881 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2883 struct mdt_thread_info *mti;
2884 struct lustre_capa *lc;
2886 mti = mdt_th_info(tsi->tsi_env);
2887 LASSERT(mti != NULL);
2889 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2890 if (tsi->tsi_corpus != NULL) {
2891 struct req_capsule *pill = tsi->tsi_pill;
2893 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2894 lu_object_get(tsi->tsi_corpus);
2897 * XXX: must be part of tgt_mdt_body_unpack but moved here
2898 * due to mdt_set_capainfo().
2900 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2901 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2902 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2903 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->mbo_fid1,
2907 mti->mti_body = tsi->tsi_mdt_body;
2908 mti->mti_dlm_req = tsi->tsi_dlm_req;
2913 static int mdt_tgt_connect(struct tgt_session_info *tsi)
2915 struct ptlrpc_request *req = tgt_ses_req(tsi);
2920 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
2922 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
2923 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE, HZ * 3);
2925 rc = tgt_connect(tsi);
2929 rc = mdt_init_idmap(tsi);
2934 obd_disconnect(class_export_get(req->rq_export));
2953 static int mdt_intent_getattr(enum mdt_it_code opcode,
2954 struct mdt_thread_info *info,
2955 struct ldlm_lock **,
2958 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2959 struct mdt_thread_info *info,
2960 struct ldlm_lock **lockp,
2963 static int mdt_intent_layout(enum mdt_it_code opcode,
2964 struct mdt_thread_info *info,
2965 struct ldlm_lock **,
2967 static int mdt_intent_reint(enum mdt_it_code opcode,
2968 struct mdt_thread_info *info,
2969 struct ldlm_lock **,
2972 static struct mdt_it_flavor {
2973 const struct req_format *it_fmt;
2975 int (*it_act)(enum mdt_it_code ,
2976 struct mdt_thread_info *,
2977 struct ldlm_lock **,
2980 } mdt_it_flavor[] = {
2982 .it_fmt = &RQF_LDLM_INTENT,
2983 /*.it_flags = HABEO_REFERO,*/
2985 .it_act = mdt_intent_reint,
2986 .it_reint = REINT_OPEN
2989 .it_fmt = &RQF_LDLM_INTENT,
2991 * OCREAT is not a MUTABOR request as if the file
2993 * We do the extra check of OBD_CONNECT_RDONLY in
2994 * mdt_reint_open() when we really need to create
2998 .it_act = mdt_intent_reint,
2999 .it_reint = REINT_OPEN
3002 .it_fmt = &RQF_LDLM_INTENT,
3003 .it_flags = MUTABOR,
3004 .it_act = mdt_intent_reint,
3005 .it_reint = REINT_CREATE
3007 [MDT_IT_GETATTR] = {
3008 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3009 .it_flags = HABEO_REFERO,
3010 .it_act = mdt_intent_getattr
3012 [MDT_IT_READDIR] = {
3018 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3019 .it_flags = HABEO_REFERO,
3020 .it_act = mdt_intent_getattr
3023 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3024 .it_flags = MUTABOR,
3026 .it_reint = REINT_UNLINK
3030 .it_flags = MUTABOR,
3033 [MDT_IT_GETXATTR] = {
3034 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3035 .it_flags = HABEO_CORPUS,
3036 .it_act = mdt_intent_getxattr
3039 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3041 .it_act = mdt_intent_layout
3046 mdt_intent_lock_replace(struct mdt_thread_info *info,
3047 struct ldlm_lock **lockp,
3048 struct mdt_lock_handle *lh,
3051 struct ptlrpc_request *req = mdt_info_req(info);
3052 struct ldlm_lock *lock = *lockp;
3053 struct ldlm_lock *new_lock;
3055 /* If possible resent found a lock, @lh is set to its handle */
3056 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3058 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3059 lh->mlh_reg_lh.cookie = 0;
3063 LASSERTF(new_lock != NULL,
3064 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3067 * If we've already given this lock to a client once, then we should
3068 * have no readers or writers. Otherwise, we should have one reader
3069 * _or_ writer ref (which will be zeroed below) before returning the
3072 if (new_lock->l_export == req->rq_export) {
3073 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3075 LASSERT(new_lock->l_export == NULL);
3076 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3081 if (new_lock->l_export == req->rq_export) {
3083 * Already gave this to the client, which means that we
3084 * reconstructed a reply.
3086 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3089 LDLM_LOCK_RELEASE(new_lock);
3090 lh->mlh_reg_lh.cookie = 0;
3091 RETURN(ELDLM_LOCK_REPLACED);
3095 * Fixup the lock to be given to the client.
3097 lock_res_and_lock(new_lock);
3098 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3099 * possible blocking AST. */
3100 while (new_lock->l_readers > 0) {
3101 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3102 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3103 new_lock->l_readers--;
3105 while (new_lock->l_writers > 0) {
3106 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3107 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3108 new_lock->l_writers--;
3111 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3112 new_lock->l_blocking_ast = lock->l_blocking_ast;
3113 new_lock->l_completion_ast = lock->l_completion_ast;
3114 new_lock->l_remote_handle = lock->l_remote_handle;
3115 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3117 unlock_res_and_lock(new_lock);
3119 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3120 &new_lock->l_remote_handle,
3121 &new_lock->l_exp_hash);
3123 LDLM_LOCK_RELEASE(new_lock);
3124 lh->mlh_reg_lh.cookie = 0;
3126 RETURN(ELDLM_LOCK_REPLACED);
3129 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3130 struct ldlm_lock *new_lock,
3131 struct mdt_lock_handle *lh,
3134 struct ptlrpc_request *req = mdt_info_req(info);
3135 struct ldlm_request *dlmreq;
3137 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3140 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3142 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3143 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3145 if (flags & LDLM_FL_RESENT) {
3146 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3147 lh->mlh_reg_mode = new_lock->l_granted_mode;
3149 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3150 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3151 lh->mlh_reg_lh.cookie);
3156 * If the xid matches, then we know this is a resent request, and allow
3157 * it. (It's probably an OPEN, for which we don't send a lock.
3159 if (req_xid_is_last(req))
3163 * This remote handle isn't enqueued, so we never received or processed
3164 * this request. Clear MSG_RESENT, because it can be handled like any
3165 * normal request now.
3167 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3169 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3170 dlmreq->lock_handle[0].cookie);
3173 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3174 struct mdt_thread_info *info,
3175 struct ldlm_lock **lockp,
3178 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3179 struct ldlm_reply *ldlm_rep = NULL;
3183 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3184 * (for the resend case) or a new lock. Below we will use it to
3185 * replace the original lock.
3187 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3188 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3189 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3190 rc = mdt_object_lock(info, info->mti_object, lhc,
3191 MDS_INODELOCK_XATTR,
3197 grc = mdt_getxattr(info);
3199 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3201 if (mdt_info_req(info)->rq_repmsg != NULL)
3202 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3203 if (ldlm_rep == NULL)
3204 RETURN(err_serious(-EFAULT));
3206 ldlm_rep->lock_policy_res2 = grc;
3211 static int mdt_intent_getattr(enum mdt_it_code opcode,
3212 struct mdt_thread_info *info,
3213 struct ldlm_lock **lockp,
3216 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3218 struct ldlm_reply *ldlm_rep;
3219 struct mdt_body *reqbody;
3220 struct mdt_body *repbody;
3224 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3227 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3230 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3231 repbody->mbo_eadatasize = 0;
3232 repbody->mbo_aclsize = 0;
3236 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3238 case MDT_IT_GETATTR:
3239 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3243 CERROR("Unsupported intent (%d)\n", opcode);
3244 GOTO(out_shrink, rc = -EINVAL);
3247 rc = mdt_init_ucred(info, reqbody);
3249 GOTO(out_shrink, rc);
3251 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3252 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3254 /* Get lock from request for possible resent case. */
3255 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3257 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3258 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3260 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3261 ldlm_rep->lock_policy_res2 = 0;
3262 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3263 ldlm_rep->lock_policy_res2) {
3264 lhc->mlh_reg_lh.cookie = 0ull;
3265 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3268 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3271 mdt_exit_ucred(info);
3273 mdt_client_compatibility(info);
3274 rc2 = mdt_fix_reply(info);
3280 static int mdt_intent_layout(enum mdt_it_code opcode,
3281 struct mdt_thread_info *info,
3282 struct ldlm_lock **lockp,
3285 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3286 struct layout_intent *layout;
3288 struct mdt_object *obj = NULL;
3292 if (opcode != MDT_IT_LAYOUT) {
3293 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3298 fid = &info->mti_tmp_fid2;
3299 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3301 /* Get lock from request for possible resent case. */
3302 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3304 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3306 RETURN(PTR_ERR(obj));
3308 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3309 /* get the length of lsm */
3310 rc = mdt_attr_get_eabuf_size(info, obj);
3312 mdt_object_put(info->mti_env, obj);
3316 if (rc > info->mti_mdt->mdt_max_mdsize)
3317 info->mti_mdt->mdt_max_mdsize = rc;
3320 mdt_object_put(info->mti_env, obj);
3322 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3323 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER, rc);
3324 rc = req_capsule_server_pack(info->mti_pill);
3328 if (lustre_handle_is_used(&lhc->mlh_reg_lh))
3329 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3331 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3332 LASSERT(layout != NULL);
3333 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3334 /* return to normal/resent ldlm handling */
3337 CERROR("%s: Unsupported layout intent (%d)\n",
3338 mdt_obd_name(info->mti_mdt), layout->li_opc);
3342 static int mdt_intent_reint(enum mdt_it_code opcode,
3343 struct mdt_thread_info *info,
3344 struct ldlm_lock **lockp,
3347 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3348 struct ldlm_reply *rep = NULL;
3352 static const struct req_format *intent_fmts[REINT_MAX] = {
3353 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3354 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3359 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3363 if (mdt_it_flavor[opcode].it_reint != opc) {
3364 CERROR("Reint code %ld doesn't match intent: %d\n",
3366 RETURN(err_serious(-EPROTO));
3369 /* Get lock from request for possible resent case. */
3370 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3372 rc = mdt_reint_internal(info, lhc, opc);
3374 /* Check whether the reply has been packed successfully. */
3375 if (mdt_info_req(info)->rq_repmsg != NULL)
3376 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3378 RETURN(err_serious(-EFAULT));
3380 /* MDC expects this in any case */
3382 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3384 /* the open lock or the lock for cross-ref object should be
3385 * returned to the client */
3386 if (rc == -MDT_EREMOTE_OPEN || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3387 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3388 rep->lock_policy_res2 = 0;
3389 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3393 rep->lock_policy_res2 = clear_serious(rc);
3395 if (rep->lock_policy_res2 == -ENOENT &&
3396 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3397 rep->lock_policy_res2 = 0;
3399 if (rc == -ENOTCONN || rc == -ENODEV ||
3400 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3402 * If it is the disconnect error (ENODEV & ENOCONN), the error
3403 * will be returned by rq_status, and client at ptlrpc layer
3404 * will detect this, then disconnect, reconnect the import
3405 * immediately, instead of impacting the following the rpc.
3407 lhc->mlh_reg_lh.cookie = 0ull;
3411 * For other cases, the error will be returned by intent.
3412 * and client will retrieve the result from intent.
3415 * FIXME: when open lock is finished, that should be
3418 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3419 LASSERTF(rc == 0, "Error occurred but lock handle "
3420 "is still in use, rc = %d\n", rc);
3421 rep->lock_policy_res2 = 0;
3422 rc = mdt_intent_lock_replace(info, lockp, lhc, flags);
3425 lhc->mlh_reg_lh.cookie = 0ull;
3426 RETURN(ELDLM_LOCK_ABORTED);
3431 static int mdt_intent_code(long itcode)
3439 case IT_OPEN|IT_CREAT:
3446 rc = MDT_IT_READDIR;
3449 rc = MDT_IT_GETATTR;
3461 rc = MDT_IT_GETXATTR;
3466 case IT_QUOTA_DQACQ:
3471 CERROR("Unknown intent opcode: %ld\n", itcode);
3478 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3479 struct ldlm_lock **lockp, __u64 flags)
3481 struct req_capsule *pill = info->mti_pill;
3482 struct ptlrpc_request *req = mdt_info_req(info);
3483 struct mdt_it_flavor *flv;
3488 opc = mdt_intent_code(itopc);
3492 if (opc == MDT_IT_QUOTA) {
3493 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3496 RETURN(-EOPNOTSUPP);
3498 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3499 /* pass the request to quota master */
3500 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3501 mdt_info_req(info), lockp,
3506 flv = &mdt_it_flavor[opc];
3507 if (flv->it_fmt != NULL)
3508 req_capsule_extend(pill, flv->it_fmt);
3510 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3514 if (flv->it_flags & MUTABOR &&
3515 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3518 if (flv->it_act != NULL) {
3519 struct ldlm_reply *rep;
3521 /* execute policy */
3522 rc = flv->it_act(opc, info, lockp, flags);
3524 /* Check whether the reply has been packed successfully. */
3525 if (req->rq_repmsg != NULL) {
3526 rep = req_capsule_server_get(info->mti_pill,
3528 rep->lock_policy_res2 =
3529 ptlrpc_status_hton(rep->lock_policy_res2);
3536 static int mdt_intent_policy(struct ldlm_namespace *ns,
3537 struct ldlm_lock **lockp, void *req_cookie,
3538 ldlm_mode_t mode, __u64 flags, void *data)
3540 struct tgt_session_info *tsi;
3541 struct mdt_thread_info *info;
3542 struct ptlrpc_request *req = req_cookie;
3543 struct ldlm_intent *it;
3544 struct req_capsule *pill;
3549 LASSERT(req != NULL);
3551 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3553 info = tsi2mdt_info(tsi);
3554 LASSERT(info != NULL);
3555 pill = info->mti_pill;
3556 LASSERT(pill->rc_req == req);
3558 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3559 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3560 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3562 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3566 /* Lock without inodebits makes no sense and will oops
3567 * later in ldlm. Let's check it now to see if we have
3568 * ibits corrupted somewhere in mdt_intent_opc().
3569 * The case for client miss to set ibits has been
3570 * processed by others. */
3571 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3572 lr_type == LDLM_IBITS,
3573 info->mti_dlm_req->lock_desc.\
3574 l_policy_data.l_inodebits.bits != 0));
3576 rc = err_serious(-EFAULT);
3578 /* No intent was provided */
3579 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3580 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3581 rc = req_capsule_server_pack(pill);
3583 rc = err_serious(rc);
3585 mdt_thread_info_fini(info);
3589 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3591 struct seq_server_site *ss = mdt_seq_site(mdt);
3593 if (ss->ss_node_id == 0)
3596 if (ss->ss_client_seq != NULL) {
3597 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3598 ss->ss_client_seq->lcs_exp = NULL;
3601 if (ss->ss_server_fld != NULL) {
3602 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3603 ss->ss_server_fld->lsf_control_exp = NULL;
3607 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3609 struct seq_server_site *ss = mdt_seq_site(mdt);
3614 if (ss->ss_server_seq != NULL)
3615 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3618 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3620 mdt_seq_fini_cli(mdt);
3621 mdt_deregister_seq_exp(mdt);
3623 return seq_site_fini(env, mdt_seq_site(mdt));
3627 * It will retrieve its FLDB entries from MDT0, and it only happens
3628 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3629 * and it needs to refresh FLDB from the MDT0.
3631 static int mdt_register_lwp_callback(void *data)
3634 struct mdt_device *mdt = data;
3635 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3639 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3641 if (!likely(fld->lsf_new))
3644 rc = lu_env_init(&env, LCT_MD_THREAD);
3646 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3650 rc = fld_update_from_controller(&env, fld);
3652 CERROR("%s: cannot update controller: rc = %d\n",
3653 mdt_obd_name(mdt), rc);
3661 static int mdt_register_seq_exp(struct mdt_device *mdt)
3663 struct seq_server_site *ss = mdt_seq_site(mdt);
3664 char *lwp_name = NULL;
3667 if (ss->ss_node_id == 0)
3670 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3671 if (lwp_name == NULL)
3672 GOTO(out_free, rc = -ENOMEM);
3674 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3678 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3683 rc = lustre_register_lwp_item(lwp_name,
3684 &ss->ss_server_fld->lsf_control_exp,
3685 mdt_register_lwp_callback, mdt);
3687 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3688 ss->ss_client_seq->lcs_exp = NULL;
3692 if (lwp_name != NULL)
3693 OBD_FREE(lwp_name, MAX_OBD_NAME);
3699 * Init client sequence manager which is used by local MDS to talk to sequence
3700 * controller on remote node.
3702 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3704 struct seq_server_site *ss = mdt_seq_site(mdt);
3709 /* check if this is adding the first MDC and controller is not yet
3711 OBD_ALLOC_PTR(ss->ss_client_seq);
3712 if (ss->ss_client_seq == NULL)
3715 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3716 if (prefix == NULL) {
3717 OBD_FREE_PTR(ss->ss_client_seq);
3718 ss->ss_client_seq = NULL;
3722 /* Note: seq_client_fini will be called in seq_site_fini */
3723 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3724 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3725 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3727 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3729 OBD_FREE_PTR(ss->ss_client_seq);
3730 ss->ss_client_seq = NULL;
3734 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3739 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3741 struct seq_server_site *ss;
3745 ss = mdt_seq_site(mdt);
3746 /* init sequence controller server(MDT0) */
3747 if (ss->ss_node_id == 0) {
3748 OBD_ALLOC_PTR(ss->ss_control_seq);
3749 if (ss->ss_control_seq == NULL)
3752 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3753 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3756 GOTO(out_seq_fini, rc);
3759 /* Init normal sequence server */
3760 OBD_ALLOC_PTR(ss->ss_server_seq);
3761 if (ss->ss_server_seq == NULL)
3762 GOTO(out_seq_fini, rc = -ENOMEM);
3764 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3765 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3767 GOTO(out_seq_fini, rc);
3769 /* init seq client for seq server to talk to seq controller(MDT0) */
3770 rc = mdt_seq_init_cli(env, mdt);
3772 GOTO(out_seq_fini, rc);
3774 if (ss->ss_node_id != 0)
3775 /* register controler export through lwp */
3776 rc = mdt_register_seq_exp(mdt);
3781 mdt_seq_fini(env, mdt);
3789 static int mdt_fld_fini(const struct lu_env *env,
3790 struct mdt_device *m)
3792 struct seq_server_site *ss = mdt_seq_site(m);
3795 if (ss && ss->ss_server_fld) {
3796 fld_server_fini(env, ss->ss_server_fld);
3797 OBD_FREE_PTR(ss->ss_server_fld);
3798 ss->ss_server_fld = NULL;
3804 static int mdt_fld_init(const struct lu_env *env,
3806 struct mdt_device *m)
3808 struct seq_server_site *ss;
3812 ss = mdt_seq_site(m);
3814 OBD_ALLOC_PTR(ss->ss_server_fld);
3815 if (ss->ss_server_fld == NULL)
3816 RETURN(rc = -ENOMEM);
3818 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3821 OBD_FREE_PTR(ss->ss_server_fld);
3822 ss->ss_server_fld = NULL;
3829 static void mdt_stack_pre_fini(const struct lu_env *env,
3830 struct mdt_device *m, struct lu_device *top)
3832 struct lustre_cfg_bufs *bufs;
3833 struct lustre_cfg *lcfg;
3834 struct mdt_thread_info *info;
3839 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3840 LASSERT(info != NULL);
3842 bufs = &info->mti_u.bufs;
3844 LASSERT(m->mdt_child_exp);
3845 LASSERT(m->mdt_child_exp->exp_obd);
3847 /* process cleanup, pass mdt obd name to get obd umount flags */
3848 /* XXX: this is needed because all layers are referenced by
3849 * objects (some of them are pinned by osd, for example *
3850 * the proper solution should be a model where object used
3851 * by osd only doesn't have mdt/mdd slices -bzzz */
3852 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3853 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3854 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3856 CERROR("%s: cannot alloc lcfg\n", mdt_obd_name(m));
3859 top->ld_ops->ldo_process_config(env, top, lcfg);
3860 lustre_cfg_free(lcfg);
3864 static void mdt_stack_fini(const struct lu_env *env,
3865 struct mdt_device *m, struct lu_device *top)
3867 struct obd_device *obd = mdt2obd_dev(m);
3868 struct lustre_cfg_bufs *bufs;
3869 struct lustre_cfg *lcfg;
3870 struct mdt_thread_info *info;
3874 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3875 LASSERT(info != NULL);
3877 lu_dev_del_linkage(top->ld_site, top);
3879 lu_site_purge(env, top->ld_site, -1);
3881 bufs = &info->mti_u.bufs;
3882 /* process cleanup, pass mdt obd name to get obd umount flags */
3883 /* another purpose is to let all layers to release their objects */
3884 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3889 lustre_cfg_bufs_set_string(bufs, 1, flags);
3890 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3892 CERROR("Cannot alloc lcfg!\n");
3896 top->ld_ops->ldo_process_config(env, top, lcfg);
3897 lustre_cfg_free(lcfg);
3899 lu_site_purge(env, top->ld_site, -1);
3901 m->mdt_child = NULL;
3902 m->mdt_bottom = NULL;
3904 obd_disconnect(m->mdt_child_exp);
3905 m->mdt_child_exp = NULL;
3907 obd_disconnect(m->mdt_bottom_exp);
3908 m->mdt_child_exp = NULL;
3911 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3912 const char *next, struct obd_export **exp)
3914 struct obd_connect_data *data = NULL;
3915 struct obd_device *obd;
3919 OBD_ALLOC_PTR(data);
3921 GOTO(out, rc = -ENOMEM);
3923 obd = class_name2obd(next);
3925 CERROR("%s: can't locate next device: %s\n",
3926 mdt_obd_name(m), next);
3927 GOTO(out, rc = -ENOTCONN);
3930 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3931 data->ocd_version = LUSTRE_VERSION_CODE;
3933 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3935 CERROR("%s: cannot connect to next dev %s (%d)\n",
3936 mdt_obd_name(m), next, rc);
3946 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3947 struct lustre_cfg *cfg)
3949 char *dev = lustre_cfg_string(cfg, 0);
3950 int rc, name_size, uuid_size;
3951 char *name, *uuid, *p;
3952 struct lustre_cfg_bufs *bufs;
3953 struct lustre_cfg *lcfg;
3954 struct obd_device *obd;
3955 struct lustre_profile *lprof;
3956 struct lu_site *site;
3959 /* in 1.8 we had the only device in the stack - MDS.
3960 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3961 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3962 * to generate names and setup MDT, MDD. MDT will be using
3963 * generated name to connect to MDD. for MDD the next device
3964 * will be LOD with name taken from so called "profile" which
3965 * is generated by mount_option line
3967 * 1.8 MGS generates config. commands like this:
3968 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3969 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3970 * 2.0 MGS generates config. commands like this:
3971 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3972 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3973 * 3:lustre-MDT0000-mdtlov 4:f
3975 * we generate MDD name from MDT one, just replacing T with D
3977 * after all the preparations, the logical equivalent will be
3978 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3979 * 3:lustre-MDT0000-mdtlov 4:f
3980 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3981 * 3:lustre-MDD0000 4:f
3983 * notice we build the stack from down to top: MDD first, then MDT */
3985 name_size = MAX_OBD_NAME;
3986 uuid_size = MAX_OBD_NAME;
3988 OBD_ALLOC(name, name_size);
3989 OBD_ALLOC(uuid, uuid_size);
3990 if (name == NULL || uuid == NULL)
3991 GOTO(cleanup_mem, rc = -ENOMEM);
3993 OBD_ALLOC_PTR(bufs);
3995 GOTO(cleanup_mem, rc = -ENOMEM);
3998 p = strstr(name, "-MDT");
4000 GOTO(free_bufs, rc = -ENOMEM);
4003 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4005 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4006 if (lprof == NULL || lprof->lp_dt == NULL) {
4007 CERROR("can't find the profile: %s\n",
4008 lustre_cfg_string(cfg, 0));
4009 GOTO(free_bufs, rc = -EINVAL);
4012 lustre_cfg_bufs_reset(bufs, name);
4013 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4014 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4015 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4017 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4019 GOTO(free_bufs, rc = -ENOMEM);
4021 rc = class_attach(lcfg);
4023 GOTO(lcfg_cleanup, rc);
4025 obd = class_name2obd(name);
4027 CERROR("Can not find obd %s (%s in config)\n",
4028 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4029 GOTO(lcfg_cleanup, rc = -EINVAL);
4032 lustre_cfg_free(lcfg);
4034 lustre_cfg_bufs_reset(bufs, name);
4035 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4036 lustre_cfg_bufs_set_string(bufs, 2, dev);
4037 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4039 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4041 rc = class_setup(obd, lcfg);
4043 GOTO(class_detach, rc);
4045 /* connect to MDD we just setup */
4046 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4048 GOTO(class_detach, rc);
4050 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4052 LASSERT(mdt_lu_site(mdt) == NULL);
4053 mdt->mdt_lu_dev.ld_site = site;
4054 site->ls_top_dev = &mdt->mdt_lu_dev;
4055 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4057 /* now connect to bottom OSD */
4058 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4059 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4061 GOTO(class_detach, rc);
4063 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4065 rc = lu_env_refill((struct lu_env *)env);
4067 CERROR("Failure to refill session: '%d'\n", rc);
4069 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4074 class_detach(obd, lcfg);
4076 lustre_cfg_free(lcfg);
4081 OBD_FREE(name, name_size);
4083 OBD_FREE(uuid, uuid_size);
4087 /* setup quota master target on MDT0 */
4088 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4089 struct lustre_cfg *cfg)
4091 struct obd_device *obd;
4092 char *dev = lustre_cfg_string(cfg, 0);
4093 char *qmtname, *uuid, *p;
4094 struct lustre_cfg_bufs *bufs;
4095 struct lustre_cfg *lcfg;
4096 struct lustre_profile *lprof;
4097 struct obd_connect_data *data;
4101 LASSERT(mdt->mdt_qmt_exp == NULL);
4102 LASSERT(mdt->mdt_qmt_dev == NULL);
4104 /* quota master is on MDT0 only for now */
4105 if (mdt->mdt_seq_site.ss_node_id != 0)
4108 /* MGS generates config commands which look as follows:
4109 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4110 * 3:lustre-MDT0000-mdtlov 4:f
4112 * We generate the QMT name from the MDT one, just replacing MD with QM
4113 * after all the preparations, the logical equivalent will be:
4114 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4115 * 3:lustre-MDT0000-osd 4:f */
4116 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4117 OBD_ALLOC(uuid, UUID_MAX);
4118 OBD_ALLOC_PTR(bufs);
4119 OBD_ALLOC_PTR(data);
4120 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4121 GOTO(cleanup_mem, rc = -ENOMEM);
4123 strcpy(qmtname, dev);
4124 p = strstr(qmtname, "-MDT");
4126 GOTO(cleanup_mem, rc = -ENOMEM);
4127 /* replace MD with QM */
4131 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4133 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4134 if (lprof == NULL || lprof->lp_dt == NULL) {
4135 CERROR("can't find profile for %s\n",
4136 lustre_cfg_string(cfg, 0));
4137 GOTO(cleanup_mem, rc = -EINVAL);
4140 lustre_cfg_bufs_reset(bufs, qmtname);
4141 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4142 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4143 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4145 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4147 GOTO(cleanup_mem, rc = -ENOMEM);
4149 rc = class_attach(lcfg);
4151 GOTO(lcfg_cleanup, rc);
4153 obd = class_name2obd(qmtname);
4155 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4156 lustre_cfg_string(cfg, 0));
4157 GOTO(lcfg_cleanup, rc = -EINVAL);
4160 lustre_cfg_free(lcfg);
4162 lustre_cfg_bufs_reset(bufs, qmtname);
4163 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4164 lustre_cfg_bufs_set_string(bufs, 2, dev);
4166 /* for quota, the next device should be the OSD device */
4167 lustre_cfg_bufs_set_string(bufs, 3,
4168 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4170 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4172 rc = class_setup(obd, lcfg);
4174 GOTO(class_detach, rc);
4176 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4178 /* configure local quota objects */
4179 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4183 GOTO(class_cleanup, rc);
4185 /* connect to quota master target */
4186 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4187 data->ocd_version = LUSTRE_VERSION_CODE;
4188 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4191 CERROR("cannot connect to quota master device %s (%d)\n",
4193 GOTO(class_cleanup, rc);
4199 class_manual_cleanup(obd);
4200 mdt->mdt_qmt_dev = NULL;
4204 class_detach(obd, lcfg);
4206 lustre_cfg_free(lcfg);
4211 OBD_FREE(qmtname, MAX_OBD_NAME);
4213 OBD_FREE(uuid, UUID_MAX);
4219 /* Shutdown quota master target associated with mdt */
4220 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4224 if (mdt->mdt_qmt_exp == NULL)
4226 LASSERT(mdt->mdt_qmt_dev != NULL);
4228 /* the qmt automatically shuts down when the mdt disconnects */
4229 obd_disconnect(mdt->mdt_qmt_exp);
4230 mdt->mdt_qmt_exp = NULL;
4231 mdt->mdt_qmt_dev = NULL;
4235 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4236 * for now. This will be removed along with converting rest of MDT code
4237 * to use tgt_session_info */
4238 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4240 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4243 rc = mdt_getxattr(info);
4245 mdt_thread_info_fini(info);
4249 static struct tgt_handler mdt_tgt_handlers[] = {
4250 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4251 0, MDS_CONNECT, mdt_tgt_connect,
4252 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4253 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4254 0, MDS_DISCONNECT, tgt_disconnect,
4255 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4256 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4257 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4258 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4259 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4260 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4261 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4262 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4264 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4265 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4266 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4267 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4268 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4270 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4271 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4272 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4273 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4275 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4276 mdt_hsm_ct_register),
4277 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4278 mdt_hsm_ct_unregister),
4279 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4281 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4283 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4284 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4286 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4291 static struct tgt_handler mdt_sec_ctx_ops[] = {
4292 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4293 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4294 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4297 static struct tgt_handler mdt_quota_ops[] = {
4298 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4301 static struct tgt_opc_slice mdt_common_slice[] = {
4303 .tos_opc_start = MDS_FIRST_OPC,
4304 .tos_opc_end = MDS_LAST_OPC,
4305 .tos_hs = mdt_tgt_handlers
4308 .tos_opc_start = OBD_FIRST_OPC,
4309 .tos_opc_end = OBD_LAST_OPC,
4310 .tos_hs = tgt_obd_handlers
4313 .tos_opc_start = LDLM_FIRST_OPC,
4314 .tos_opc_end = LDLM_LAST_OPC,
4315 .tos_hs = tgt_dlm_handlers
4318 .tos_opc_start = SEC_FIRST_OPC,
4319 .tos_opc_end = SEC_LAST_OPC,
4320 .tos_hs = mdt_sec_ctx_ops
4323 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4324 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4325 .tos_hs = tgt_out_handlers
4328 .tos_opc_start = FLD_FIRST_OPC,
4329 .tos_opc_end = FLD_LAST_OPC,
4330 .tos_hs = fld_handlers
4333 .tos_opc_start = SEQ_FIRST_OPC,
4334 .tos_opc_end = SEQ_LAST_OPC,
4335 .tos_hs = seq_handlers
4338 .tos_opc_start = QUOTA_DQACQ,
4339 .tos_opc_end = QUOTA_LAST_OPC,
4340 .tos_hs = mdt_quota_ops
4343 .tos_opc_start = LLOG_FIRST_OPC,
4344 .tos_opc_end = LLOG_LAST_OPC,
4345 .tos_hs = tgt_llog_handlers
4348 .tos_opc_start = LFSCK_FIRST_OPC,
4349 .tos_opc_end = LFSCK_LAST_OPC,
4350 .tos_hs = tgt_lfsck_handlers
4358 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4360 struct md_device *next = m->mdt_child;
4361 struct lu_device *d = &m->mdt_lu_dev;
4362 struct obd_device *obd = mdt2obd_dev(m);
4363 struct lfsck_stop stop;
4366 stop.ls_status = LS_PAUSED;
4368 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4370 target_recovery_fini(obd);
4371 ping_evictor_stop();
4372 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4374 if (m->mdt_opts.mo_coordinator)
4375 mdt_hsm_cdt_stop(m);
4377 mdt_hsm_cdt_fini(m);
4379 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4380 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4381 obd_exports_barrier(obd);
4382 obd_zombie_barrier();
4386 tgt_fini(env, &m->mdt_lut);
4387 mdt_fs_cleanup(env, m);
4388 upcall_cache_cleanup(m->mdt_identity_cache);
4389 m->mdt_identity_cache = NULL;
4391 if (m->mdt_namespace != NULL) {
4392 ldlm_namespace_free(m->mdt_namespace, NULL,
4393 d->ld_obd->obd_force);
4394 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4397 mdt_quota_fini(env, m);
4399 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4401 mdt_seq_fini(env, m);
4402 mdt_fld_fini(env, m);
4404 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4405 cfs_timer_disarm(&m->mdt_ck_timer);
4406 mdt_ck_thread_stop(m);
4411 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4413 LASSERT(atomic_read(&d->ld_ref) == 0);
4415 server_put_mount(mdt_obd_name(m), true);
4420 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4422 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4423 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4425 struct mdt_thread_info *info;
4426 struct obd_device *obd;
4427 const char *dev = lustre_cfg_string(cfg, 0);
4428 const char *num = lustre_cfg_string(cfg, 2);
4429 struct lustre_mount_info *lmi = NULL;
4430 struct lustre_sb_info *lsi;
4432 struct seq_server_site *ss_site;
4433 const char *identity_upcall = "NONE";
4434 struct md_device *next;
4440 lu_device_init(&m->mdt_lu_dev, ldt);
4442 * Environment (env) might be missing mdt_thread_key values at that
4443 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4446 * Usually device allocation path doesn't use module key values, but
4447 * mdt has to do a lot of work here, so allocate key value.
4449 rc = lu_env_refill((struct lu_env *)env);
4453 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4454 LASSERT(info != NULL);
4456 obd = class_name2obd(dev);
4457 LASSERT(obd != NULL);
4459 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4461 m->mdt_som_conf = 0;
4463 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4465 /* default is coordinator off, it is started through conf_param
4467 m->mdt_opts.mo_coordinator = 0;
4469 lmi = server_get_mount(dev);
4471 CERROR("Cannot get mount info for %s!\n", dev);
4474 lsi = s2lsi(lmi->lmi_sb);
4475 /* CMD is supported only in IAM mode */
4477 node_id = simple_strtol(num, NULL, 10);
4478 obd->u.obt.obt_magic = OBT_MAGIC;
4481 spin_lock_init(&m->mdt_ioepoch_lock);
4482 m->mdt_capa_timeout = CAPA_TIMEOUT;
4483 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4484 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4485 m->mdt_squash.rsi_uid = 0;
4486 m->mdt_squash.rsi_gid = 0;
4487 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4488 init_rwsem(&m->mdt_squash.rsi_sem);
4489 spin_lock_init(&m->mdt_osfs_lock);
4490 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4491 m->mdt_enable_remote_dir = 0;
4492 m->mdt_enable_remote_dir_gid = 0;
4494 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4495 m->mdt_lu_dev.ld_obd = obd;
4496 /* Set this lu_device to obd for error handling purposes. */
4497 obd->obd_lu_dev = &m->mdt_lu_dev;
4499 /* init the stack */
4500 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4502 CERROR("%s: Can't init device stack, rc %d\n",
4503 mdt_obd_name(m), rc);
4508 ss_site = mdt_seq_site(m);
4509 s->ld_seq_site = ss_site;
4512 /* set server index */
4513 ss_site->ss_node_id = node_id;
4515 /* failover is the default
4516 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4517 * assumed whose ss_node_id == 0 XXX
4519 obd->obd_replayable = 1;
4520 /* No connection accepted until configurations will finish */
4521 obd->obd_no_conn = 1;
4523 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4524 char *str = lustre_cfg_string(cfg, 4);
4525 if (strchr(str, 'n')) {
4526 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4527 obd->obd_replayable = 0;
4531 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4533 GOTO(err_fini_stack, rc);
4535 rc = mdt_seq_init(env, m);
4537 GOTO(err_fini_fld, rc);
4539 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4540 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4541 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4542 LDLM_NAMESPACE_SERVER,
4543 LDLM_NAMESPACE_GREEDY,
4545 if (m->mdt_namespace == NULL)
4546 GOTO(err_fini_seq, rc = -ENOMEM);
4548 m->mdt_namespace->ns_lvbp = m;
4549 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4551 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4552 /* set obd_namespace for compatibility with old code */
4553 obd->obd_namespace = m->mdt_namespace;
4555 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4557 rc = mdt_hsm_cdt_init(m);
4559 CERROR("%s: error initializing coordinator, rc %d\n",
4560 mdt_obd_name(m), rc);
4561 GOTO(err_free_ns, rc);
4564 rc = mdt_ck_thread_start(m);
4566 GOTO(err_free_hsm, rc);
4568 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4569 OBD_FAIL_MDS_ALL_REQUEST_NET,
4570 OBD_FAIL_MDS_ALL_REPLY_NET);
4574 rc = mdt_fs_setup(env, m, obd, lsi);
4578 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4580 next = m->mdt_child;
4581 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4584 GOTO(err_fs_cleanup, rc);
4586 if (mntopts & MNTOPT_USERXATTR)
4587 m->mdt_opts.mo_user_xattr = 1;
4589 m->mdt_opts.mo_user_xattr = 0;
4591 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4593 GOTO(err_fs_cleanup, rc);
4595 if (mntopts & MNTOPT_ACL)
4596 m->mdt_opts.mo_acl = 1;
4598 m->mdt_opts.mo_acl = 0;
4600 /* XXX: to support suppgid for ACL, we enable identity_upcall
4601 * by default, otherwise, maybe got unexpected -EACCESS. */
4602 if (m->mdt_opts.mo_acl)
4603 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4605 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4607 &mdt_identity_upcall_cache_ops);
4608 if (IS_ERR(m->mdt_identity_cache)) {
4609 rc = PTR_ERR(m->mdt_identity_cache);
4610 m->mdt_identity_cache = NULL;
4611 GOTO(err_fs_cleanup, rc);
4614 rc = mdt_procfs_init(m, dev);
4616 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4617 GOTO(err_recovery, rc);
4620 rc = mdt_quota_init(env, m, cfg);
4622 GOTO(err_procfs, rc);
4624 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4625 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4626 "mdt_ldlm_client", m->mdt_ldlm_client);
4628 ping_evictor_start();
4630 /* recovery will be started upon mdt_prepare()
4631 * when the whole stack is complete and ready
4632 * to serve the requests */
4634 mdt_init_capa_ctxt(env, m);
4636 /* Reduce the initial timeout on an MDS because it doesn't need such
4637 * a long timeout as an OST does. Adaptive timeouts will adjust this
4638 * value appropriately. */
4639 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4640 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4646 target_recovery_fini(obd);
4647 upcall_cache_cleanup(m->mdt_identity_cache);
4648 m->mdt_identity_cache = NULL;
4650 mdt_fs_cleanup(env, m);
4652 tgt_fini(env, &m->mdt_lut);
4654 cfs_timer_disarm(&m->mdt_ck_timer);
4655 mdt_ck_thread_stop(m);
4657 mdt_hsm_cdt_fini(m);
4659 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4660 obd->obd_namespace = m->mdt_namespace = NULL;
4662 mdt_seq_fini(env, m);
4664 mdt_fld_fini(env, m);
4666 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4669 server_put_mount(dev, true);
4673 /* For interoperability, the left element is old parameter, the right one
4674 * is the new version of the parameter, if some parameter is deprecated,
4675 * the new version should be set as NULL. */
4676 static struct cfg_interop_param mdt_interop_param[] = {
4677 { "mdt.group_upcall", NULL },
4678 { "mdt.quota_type", NULL },
4679 { "mdd.quota_type", NULL },
4680 { "mdt.rootsquash", "mdt.root_squash" },
4681 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4685 /* used by MGS to process specific configurations */
4686 static int mdt_process_config(const struct lu_env *env,
4687 struct lu_device *d, struct lustre_cfg *cfg)
4689 struct mdt_device *m = mdt_dev(d);
4690 struct md_device *md_next = m->mdt_child;
4691 struct lu_device *next = md2lu_dev(md_next);
4695 switch (cfg->lcfg_command) {
4697 struct obd_device *obd = d->ld_obd;
4699 /* For interoperability */
4700 struct cfg_interop_param *ptr = NULL;
4701 struct lustre_cfg *old_cfg = NULL;
4704 param = lustre_cfg_string(cfg, 1);
4705 if (param == NULL) {
4706 CERROR("param is empty\n");
4711 ptr = class_find_old_param(param, mdt_interop_param);
4713 if (ptr->new_param == NULL) {
4715 CWARN("For interoperability, skip this %s."
4716 " It is obsolete.\n", ptr->old_param);
4720 CWARN("Found old param %s, changed it to %s.\n",
4721 ptr->old_param, ptr->new_param);
4724 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4731 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4733 if (rc > 0 || rc == -ENOSYS) {
4734 /* is it an HSM var ? */
4735 rc = class_process_proc_param(PARAM_HSM,
4736 hsm_cdt_get_proc_vars(),
4738 if (rc > 0 || rc == -ENOSYS)
4739 /* we don't understand; pass it on */
4740 rc = next->ld_ops->ldo_process_config(env, next,
4744 if (old_cfg != NULL)
4745 lustre_cfg_free(cfg);
4750 /* others are passed further */
4751 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4757 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4758 const struct lu_object_header *hdr,
4759 struct lu_device *d)
4761 struct mdt_object *mo;
4765 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4767 struct lu_object *o;
4768 struct lu_object_header *h;
4771 h = &mo->mot_header;
4772 lu_object_header_init(h);
4773 lu_object_init(o, h, d);
4774 lu_object_add_top(h, o);
4775 o->lo_ops = &mdt_obj_ops;
4776 mutex_init(&mo->mot_ioepoch_mutex);
4777 mutex_init(&mo->mot_lov_mutex);
4778 init_rwsem(&mo->mot_open_sem);
4784 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4785 const struct lu_object_conf *unused)
4787 struct mdt_device *d = mdt_dev(o->lo_dev);
4788 struct lu_device *under;
4789 struct lu_object *below;
4793 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4794 PFID(lu_object_fid(o)));
4796 under = &d->mdt_child->md_lu_dev;
4797 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4798 if (below != NULL) {
4799 lu_object_add(o, below);
4806 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4808 struct mdt_object *mo = mdt_obj(o);
4809 struct lu_object_header *h;
4813 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4814 PFID(lu_object_fid(o)));
4816 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4817 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4820 lu_object_header_fini(h);
4821 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4826 static int mdt_object_print(const struct lu_env *env, void *cookie,
4827 lu_printer_t p, const struct lu_object *o)
4829 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4830 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4831 "flags="LPX64", epochcount=%d, writecount=%d)",
4832 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4833 mdto->mot_ioepoch_count, mdto->mot_writecount);
4836 static int mdt_prepare(const struct lu_env *env,
4837 struct lu_device *pdev,
4838 struct lu_device *cdev)
4840 struct mdt_device *mdt = mdt_dev(cdev);
4841 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4842 struct obd_device *obd = cdev->ld_obd;
4849 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4853 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4857 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4861 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4862 /* The LFSCK instance is registered just now, so it must be there when
4863 * register the namespace to such instance. */
4864 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4866 if (mdt->mdt_seq_site.ss_node_id == 0) {
4867 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4868 &mdt->mdt_md_root_fid);
4873 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4874 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4875 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4876 LASSERT(obd->obd_no_conn);
4877 spin_lock(&obd->obd_dev_lock);
4878 obd->obd_no_conn = 0;
4879 spin_unlock(&obd->obd_dev_lock);
4881 if (obd->obd_recovering == 0)
4882 mdt_postrecov(env, mdt);
4887 const struct lu_device_operations mdt_lu_ops = {
4888 .ldo_object_alloc = mdt_object_alloc,
4889 .ldo_process_config = mdt_process_config,
4890 .ldo_prepare = mdt_prepare,
4893 static const struct lu_object_operations mdt_obj_ops = {
4894 .loo_object_init = mdt_object_init,
4895 .loo_object_free = mdt_object_free,
4896 .loo_object_print = mdt_object_print
4899 static int mdt_obd_set_info_async(const struct lu_env *env,
4900 struct obd_export *exp,
4901 __u32 keylen, void *key,
4902 __u32 vallen, void *val,
4903 struct ptlrpc_request_set *set)
4909 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4910 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4918 * Match client and server connection feature flags.
4920 * Compute the compatibility flags for a connection request based on
4921 * features mutually supported by client and server.
4923 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4924 * must not be updated here, otherwise a partially initialized value may
4925 * be exposed. After the connection request is successfully processed,
4926 * the top-level MDT connect request handler atomically updates the export
4927 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4928 * reply. \see mdt_connect().
4930 * \param exp the obd_export associated with this client/target pair
4931 * \param mdt the target device for the connection
4932 * \param data stores data for this connect request
4935 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4936 * \retval -EBADE client and server feature requirements are incompatible
4938 static int mdt_connect_internal(struct obd_export *exp,
4939 struct mdt_device *mdt,
4940 struct obd_connect_data *data)
4942 LASSERT(data != NULL);
4944 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4945 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4947 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
4948 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
4949 CWARN("%s: client %s does not support ibits lock, either "
4950 "very old or an invalid client: flags "LPX64"\n",
4951 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
4952 data->ocd_connect_flags);
4956 if (!mdt->mdt_opts.mo_acl)
4957 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4959 if (!mdt->mdt_opts.mo_user_xattr)
4960 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4962 if (!mdt->mdt_som_conf)
4963 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4965 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4966 data->ocd_brw_size = min(data->ocd_brw_size,
4967 (__u32)MD_MAX_BRW_SIZE);
4968 if (data->ocd_brw_size == 0) {
4969 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4970 " ocd_version: %x ocd_grant: %d "
4971 "ocd_index: %u ocd_brw_size is "
4972 "unexpectedly zero, network data "
4973 "corruption? Refusing connection of this"
4976 exp->exp_client_uuid.uuid,
4977 exp, data->ocd_connect_flags, data->ocd_version,
4978 data->ocd_grant, data->ocd_index);
4983 /* NB: Disregard the rule against updating
4984 * exp_connect_data.ocd_connect_flags in this case, since
4985 * tgt_client_new() needs to know if this is a lightweight
4986 * connection, and it is safe to expose this flag before
4987 * connection processing completes. */
4988 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4989 spin_lock(&exp->exp_lock);
4990 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4991 spin_unlock(&exp->exp_lock);
4994 data->ocd_version = LUSTRE_VERSION_CODE;
4996 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4997 CWARN("%s: MDS requires FID support, but client not\n",
5002 if (mdt->mdt_som_conf &&
5003 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5004 OBD_CONNECT_MDS_MDS |
5005 OBD_CONNECT_SOM))) {
5006 CWARN("%s: MDS has SOM enabled, but client does not support "
5007 "it\n", mdt_obd_name(mdt));
5011 if (OCD_HAS_FLAG(data, PINGLESS)) {
5012 if (ptlrpc_pinger_suppress_pings()) {
5013 spin_lock(&exp->exp_obd->obd_dev_lock);
5014 list_del_init(&exp->exp_obd_chain_timed);
5015 spin_unlock(&exp->exp_obd->obd_dev_lock);
5017 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5021 data->ocd_max_easize = mdt->mdt_max_ea_size;
5026 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5027 struct mdt_thread_info *info,
5028 struct mdt_file_data *mfd)
5030 struct lu_context ses;
5034 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5039 lu_context_enter(&ses);
5041 mdt_ucred(info)->uc_valid = UCRED_OLD;
5042 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5044 lu_context_exit(&ses);
5045 lu_context_fini(&ses);
5051 static int mdt_export_cleanup(struct obd_export *exp)
5053 struct list_head closing_list;
5054 struct mdt_export_data *med = &exp->exp_mdt_data;
5055 struct obd_device *obd = exp->exp_obd;
5056 struct mdt_device *mdt;
5057 struct mdt_thread_info *info;
5059 struct mdt_file_data *mfd, *n;
5063 INIT_LIST_HEAD(&closing_list);
5064 spin_lock(&med->med_open_lock);
5065 while (!list_empty(&med->med_open_head)) {
5066 struct list_head *tmp = med->med_open_head.next;
5067 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5069 /* Remove mfd handle so it can't be found again.
5070 * We are consuming the mfd_list reference here. */
5071 class_handle_unhash(&mfd->mfd_handle);
5072 list_move_tail(&mfd->mfd_list, &closing_list);
5074 spin_unlock(&med->med_open_lock);
5075 mdt = mdt_dev(obd->obd_lu_dev);
5076 LASSERT(mdt != NULL);
5078 rc = lu_env_init(&env, LCT_MD_THREAD);
5082 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5083 LASSERT(info != NULL);
5084 memset(info, 0, sizeof *info);
5085 info->mti_env = &env;
5086 info->mti_mdt = mdt;
5087 info->mti_exp = exp;
5089 if (!list_empty(&closing_list)) {
5090 struct md_attr *ma = &info->mti_attr;
5092 /* Close any open files (which may also cause orphan
5094 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5095 list_del_init(&mfd->mfd_list);
5096 ma->ma_need = ma->ma_valid = 0;
5098 /* This file is being closed due to an eviction, it
5099 * could have been modified and now dirty regarding to
5100 * HSM archive, check this!
5101 * The logic here is to mark a file dirty if there's a
5102 * chance it was dirtied before the client was evicted,
5103 * so that we don't have to wait for a release attempt
5104 * before finding out the file was actually dirty and
5105 * fail the release. Aggressively marking it dirty here
5106 * will cause the policy engine to attempt to
5107 * re-archive it; when rearchiving, we can compare the
5108 * current version to the HSM data_version and make the
5109 * archive request into a noop if it's not actually
5112 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5113 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5115 /* Don't unlink orphan on failover umount, LU-184 */
5116 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5117 ma->ma_valid = MA_FLAGS;
5118 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5120 mdt_mfd_close(info, mfd);
5123 info->mti_mdt = NULL;
5124 /* cleanup client slot early */
5125 /* Do not erase record for recoverable client. */
5126 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5127 tgt_client_del(&env, exp);
5133 static int mdt_obd_disconnect(struct obd_export *exp)
5139 class_export_get(exp);
5141 nodemap_del_member(exp);
5142 rc = server_disconnect_export(exp);
5144 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5146 rc = mdt_export_cleanup(exp);
5147 class_export_put(exp);
5151 /* mds_connect copy */
5152 static int mdt_obd_connect(const struct lu_env *env,
5153 struct obd_export **exp, struct obd_device *obd,
5154 struct obd_uuid *cluuid,
5155 struct obd_connect_data *data,
5158 struct obd_export *lexp;
5159 struct lustre_handle conn = { 0 };
5160 struct mdt_device *mdt;
5162 lnet_nid_t *client_nid = localdata;
5165 LASSERT(env != NULL);
5166 if (!exp || !obd || !cluuid)
5169 mdt = mdt_dev(obd->obd_lu_dev);
5172 * first, check whether the stack is ready to handle requests
5173 * XXX: probably not very appropriate method is used now
5174 * at some point we should find a better one
5176 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5177 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5178 rc = obd_get_info(env, mdt->mdt_child_exp,
5179 sizeof(KEY_OSP_CONNECTED),
5180 KEY_OSP_CONNECTED, NULL, NULL, NULL);
5183 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5186 rc = class_connect(&conn, obd, cluuid);
5190 lexp = class_conn2export(&conn);
5191 LASSERT(lexp != NULL);
5193 rc = mdt_connect_internal(lexp, mdt, data);
5195 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5198 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5199 rc = tgt_client_new(env, lexp);
5201 rc = nodemap_add_member(*client_nid, lexp);
5202 if (rc != 0 && rc != -EEXIST)
5205 mdt_export_stats_init(obd, lexp, localdata);
5208 /* For phase I, sync for cross-ref operation. */
5209 spin_lock(&lexp->exp_lock);
5210 lexp->exp_keep_sync = 1;
5211 spin_unlock(&lexp->exp_lock);
5215 class_disconnect(lexp);
5224 static int mdt_obd_reconnect(const struct lu_env *env,
5225 struct obd_export *exp, struct obd_device *obd,
5226 struct obd_uuid *cluuid,
5227 struct obd_connect_data *data,
5230 lnet_nid_t *client_nid = localdata;
5234 if (exp == NULL || obd == NULL || cluuid == NULL)
5237 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5239 rc = nodemap_add_member(*client_nid, exp);
5240 if (rc == 0 || rc == -EEXIST)
5241 mdt_export_stats_init(obd, exp, localdata);
5247 /* FIXME: Can we avoid using these two interfaces? */
5248 static int mdt_init_export(struct obd_export *exp)
5250 struct mdt_export_data *med = &exp->exp_mdt_data;
5254 INIT_LIST_HEAD(&med->med_open_head);
5255 spin_lock_init(&med->med_open_lock);
5256 mutex_init(&med->med_idmap_mutex);
5257 med->med_idmap = NULL;
5258 spin_lock(&exp->exp_lock);
5259 exp->exp_connecting = 1;
5260 spin_unlock(&exp->exp_lock);
5262 /* self-export doesn't need client data and ldlm initialization */
5263 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5264 &exp->exp_client_uuid)))
5267 rc = tgt_client_alloc(exp);
5271 rc = ldlm_init_export(exp);
5278 tgt_client_free(exp);
5280 CERROR("%s: Failed to initialize export: rc = %d\n",
5281 exp->exp_obd->obd_name, rc);
5285 static int mdt_destroy_export(struct obd_export *exp)
5289 if (exp_connect_rmtclient(exp))
5290 mdt_cleanup_idmap(&exp->exp_mdt_data);
5292 target_destroy_export(exp);
5293 /* destroy can be called from failed obd_setup, so
5294 * checking uuid is safer than obd_self_export */
5295 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5296 &exp->exp_client_uuid)))
5299 ldlm_destroy_export(exp);
5300 tgt_client_free(exp);
5302 LASSERT(list_empty(&exp->exp_outstanding_replies));
5303 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5308 /** The maximum depth that fid2path() will search.
5309 * This is limited only because we want to store the fids for
5310 * historical path lookup purposes.
5312 #define MAX_PATH_DEPTH 100
5314 /** mdt_path() lookup structure. */
5315 struct path_lookup_info {
5316 __u64 pli_recno; /**< history point */
5317 __u64 pli_currec; /**< current record */
5318 struct lu_fid pli_fid;
5319 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5320 struct mdt_object *pli_mdt_obj;
5321 char *pli_path; /**< full path */
5323 int pli_linkno; /**< which hardlink to follow */
5324 int pli_fidcount; /**< number of \a pli_fids */
5327 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5328 struct linkea_data *ldata)
5332 LASSERT(ldata->ld_buf->lb_buf != NULL);
5334 if (!mdt_object_exists(mdt_obj))
5337 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5338 ldata->ld_buf, XATTR_NAME_LINK);
5339 if (rc == -ERANGE) {
5340 /* Buf was too small, figure out what we need. */
5341 lu_buf_free(ldata->ld_buf);
5342 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5343 ldata->ld_buf, XATTR_NAME_LINK);
5346 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5347 if (ldata->ld_buf->lb_buf == NULL)
5349 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5350 ldata->ld_buf, XATTR_NAME_LINK);
5355 return linkea_init(ldata);
5358 static int mdt_path_current(struct mdt_thread_info *info,
5359 struct path_lookup_info *pli)
5361 struct mdt_device *mdt = info->mti_mdt;
5362 struct mdt_object *mdt_obj;
5363 struct link_ea_header *leh;
5364 struct link_ea_entry *lee;
5365 struct lu_name *tmpname = &info->mti_name;
5366 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5367 struct lu_buf *buf = &info->mti_big_buf;
5370 struct linkea_data ldata = { 0 };
5374 /* temp buffer for path element, the buffer will be finally freed
5375 * in mdt_thread_info_fini */
5376 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5377 if (buf->lb_buf == NULL)
5381 ptr = pli->pli_path + pli->pli_pathlen - 1;
5384 pli->pli_fidcount = 0;
5385 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5386 *tmpfid = pli->pli_fids[0];
5387 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5388 * so checking root_fid can only happen on MDT0. */
5389 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5390 &pli->pli_fids[pli->pli_fidcount])) {
5391 struct lu_buf lmv_buf;
5393 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5394 if (IS_ERR(mdt_obj))
5395 GOTO(out, rc = PTR_ERR(mdt_obj));
5397 if (!mdt_object_exists(mdt_obj)) {
5398 mdt_object_put(info->mti_env, mdt_obj);
5399 GOTO(out, rc = -ENOENT);
5402 if (mdt_object_remote(mdt_obj)) {
5403 mdt_object_put(info->mti_env, mdt_obj);
5404 GOTO(remote_out, rc = -EREMOTE);
5407 rc = mdt_links_read(info, mdt_obj, &ldata);
5409 mdt_object_put(info->mti_env, mdt_obj);
5414 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5415 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5416 /* If set, use link #linkno for path lookup, otherwise use
5417 link #0. Only do this for the final path element. */
5418 if (pli->pli_fidcount == 0 &&
5419 pli->pli_linkno < leh->leh_reccount) {
5421 for (count = 0; count < pli->pli_linkno; count++) {
5422 lee = (struct link_ea_entry *)
5423 ((char *)lee + reclen);
5424 linkea_entry_unpack(lee, &reclen, tmpname,
5427 if (pli->pli_linkno < leh->leh_reccount - 1)
5428 /* indicate to user there are more links */
5432 lmv_buf.lb_buf = info->mti_xattr_buf;
5433 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5434 /* Check if it is slave stripes */
5435 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5436 &lmv_buf, XATTR_NAME_LMV);
5437 mdt_object_put(info->mti_env, mdt_obj);
5439 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5441 /* For slave stripes, get its master */
5442 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5443 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5446 } else if (rc < 0 && rc != -ENODATA) {
5452 /* Pack the name in the end of the buffer */
5453 ptr -= tmpname->ln_namelen;
5454 if (ptr - 1 <= pli->pli_path)
5455 GOTO(out, rc = -EOVERFLOW);
5456 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5459 /* Store the parent fid for historic lookup */
5460 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5461 GOTO(out, rc = -EOVERFLOW);
5462 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5466 ptr++; /* skip leading / */
5467 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5474 /* Returns the full path to this fid, as of changelog record recno. */
5475 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5476 char *path, int pathlen, __u64 *recno, int *linkno,
5479 struct mdt_device *mdt = info->mti_mdt;
5480 struct path_lookup_info *pli;
5488 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5497 pli->pli_mdt_obj = obj;
5498 pli->pli_recno = *recno;
5499 pli->pli_path = path;
5500 pli->pli_pathlen = pathlen;
5501 pli->pli_linkno = *linkno;
5503 /* Retry multiple times in case file is being moved */
5504 while (tries-- && rc == -EAGAIN)
5505 rc = mdt_path_current(info, pli);
5507 /* return the last resolved fids to the client, so the client will
5508 * build the left path on another MDT for remote object */
5509 *fid = pli->pli_fids[pli->pli_fidcount];
5511 *recno = pli->pli_currec;
5512 /* Return next link index to caller */
5513 *linkno = pli->pli_linkno;
5520 static int mdt_fid2path(struct mdt_thread_info *info,
5521 struct getinfo_fid2path *fp)
5523 struct mdt_device *mdt = info->mti_mdt;
5524 struct mdt_object *obj;
5528 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5529 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5531 if (!fid_is_sane(&fp->gf_fid))
5534 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5535 CWARN("%s: "DFID" is invalid, sequence should be "
5536 ">= "LPX64"\n", mdt_obd_name(mdt),
5537 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5541 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5542 if (obj == NULL || IS_ERR(obj)) {
5543 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5548 if (mdt_object_remote(obj))
5550 else if (!mdt_object_exists(obj))
5556 mdt_object_put(info->mti_env, obj);
5557 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5558 PFID(&fp->gf_fid), rc);
5562 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5563 &fp->gf_linkno, &fp->gf_fid);
5565 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5566 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5568 mdt_object_put(info->mti_env, obj);
5573 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5574 void *val, int vallen)
5576 struct getinfo_fid2path *fpout, *fpin;
5579 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5582 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5583 lustre_swab_fid2path(fpin);
5585 memcpy(fpout, fpin, sizeof(*fpin));
5586 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5589 rc = mdt_fid2path(info, fpout);
5593 int mdt_get_info(struct tgt_session_info *tsi)
5603 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5605 CDEBUG(D_IOCTL, "No GETINFO key\n");
5606 RETURN(err_serious(-EFAULT));
5608 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5611 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5612 if (vallen == NULL) {
5613 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5614 tgt_name(tsi->tsi_tgt));
5615 RETURN(err_serious(-EFAULT));
5618 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5620 rc = req_capsule_server_pack(tsi->tsi_pill);
5622 RETURN(err_serious(rc));
5624 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5625 if (valout == NULL) {
5626 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5627 tgt_name(tsi->tsi_tgt));
5628 RETURN(err_serious(-EFAULT));
5631 if (KEY_IS(KEY_FID2PATH)) {
5632 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5634 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5635 mdt_thread_info_fini(info);
5642 /* Pass the ioc down */
5643 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5644 unsigned int cmd, int len, void *data)
5646 struct lu_context ioctl_session;
5647 struct md_device *next = mdt->mdt_child;
5651 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5654 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5655 lu_context_enter(&ioctl_session);
5656 env->le_ses = &ioctl_session;
5658 LASSERT(next->md_ops->mdo_iocontrol);
5659 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5661 lu_context_exit(&ioctl_session);
5662 lu_context_fini(&ioctl_session);
5666 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5668 struct obd_ioctl_data *data = karg;
5671 struct mdt_object *obj;
5672 struct mdt_lock_handle *lh;
5676 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5677 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5680 fid = (struct lu_fid *)data->ioc_inlbuf1;
5682 if (!fid_is_sane(fid))
5685 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5687 lh = &mti->mti_lh[MDT_LH_PARENT];
5688 mdt_lock_reg_init(lh, LCK_CR);
5690 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5692 RETURN(PTR_ERR(obj));
5694 if (mdt_object_remote(obj)) {
5697 * before calling version get the correct MDS should be
5698 * fid, this is error to find remote object here
5700 CERROR("nonlocal object "DFID"\n", PFID(fid));
5701 } else if (!mdt_object_exists(obj)) {
5702 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5705 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5706 *(__u64 *)data->ioc_inlbuf2 = version;
5709 mdt_object_unlock_put(mti, obj, lh, 1);
5713 /* ioctls on obd dev */
5714 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5715 void *karg, void *uarg)
5718 struct obd_device *obd = exp->exp_obd;
5719 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5720 struct dt_device *dt = mdt->mdt_bottom;
5724 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5725 rc = lu_env_init(&env, LCT_MD_THREAD);
5731 rc = mdt_device_sync(&env, mdt);
5733 case OBD_IOC_SET_READONLY:
5734 rc = dt->dd_ops->dt_ro(&env, dt);
5736 case OBD_IOC_ABORT_RECOVERY:
5737 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5738 target_stop_recovery_thread(obd);
5741 case OBD_IOC_CHANGELOG_REG:
5742 case OBD_IOC_CHANGELOG_DEREG:
5743 case OBD_IOC_CHANGELOG_CLEAR:
5744 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5746 case OBD_IOC_START_LFSCK: {
5747 struct md_device *next = mdt->mdt_child;
5748 struct obd_ioctl_data *data = karg;
5749 struct lfsck_start_param lsp;
5751 if (unlikely(data == NULL)) {
5756 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5757 lsp.lsp_index_valid = 0;
5758 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5761 case OBD_IOC_STOP_LFSCK: {
5762 struct md_device *next = mdt->mdt_child;
5763 struct obd_ioctl_data *data = karg;
5764 struct lfsck_stop stop;
5766 stop.ls_status = LS_STOPPED;
5767 /* Old lfsck utils may pass NULL @stop. */
5768 if (data->ioc_inlbuf1 == NULL)
5772 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5774 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5777 case OBD_IOC_GET_OBJ_VERSION: {
5778 struct mdt_thread_info *mti;
5779 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5780 memset(mti, 0, sizeof *mti);
5781 mti->mti_env = &env;
5785 rc = mdt_ioc_version_get(mti, karg);
5788 case OBD_IOC_CATLOGLIST: {
5789 struct mdt_thread_info *mti;
5791 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5792 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5793 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5794 &mti->mti_tmp_fid1);
5799 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5800 mdt_obd_name(mdt), cmd, rc);
5807 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5809 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5810 struct lfsck_start_param lsp;
5814 lsp.lsp_start = NULL;
5815 lsp.lsp_index_valid = 0;
5816 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
5817 OBD_IOC_START_LFSCK,
5819 if (rc != 0 && rc != -EALREADY)
5820 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
5821 mdt_obd_name(mdt), rc);
5823 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5827 static int mdt_obd_postrecov(struct obd_device *obd)
5832 rc = lu_env_init(&env, LCT_MD_THREAD);
5835 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5840 static struct obd_ops mdt_obd_device_ops = {
5841 .o_owner = THIS_MODULE,
5842 .o_set_info_async = mdt_obd_set_info_async,
5843 .o_connect = mdt_obd_connect,
5844 .o_reconnect = mdt_obd_reconnect,
5845 .o_disconnect = mdt_obd_disconnect,
5846 .o_init_export = mdt_init_export,
5847 .o_destroy_export = mdt_destroy_export,
5848 .o_iocontrol = mdt_iocontrol,
5849 .o_postrecov = mdt_obd_postrecov,
5852 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5853 struct lu_device *d)
5855 struct mdt_device *m = mdt_dev(d);
5862 static struct lu_device *mdt_device_free(const struct lu_env *env,
5863 struct lu_device *d)
5865 struct mdt_device *m = mdt_dev(d);
5868 lu_device_fini(&m->mdt_lu_dev);
5874 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5875 struct lu_device_type *t,
5876 struct lustre_cfg *cfg)
5878 struct lu_device *l;
5879 struct mdt_device *m;
5886 rc = mdt_init0(env, m, t, cfg);
5888 mdt_device_free(env, l);
5893 l = ERR_PTR(-ENOMEM);
5897 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5898 LU_KEY_INIT(mdt, struct mdt_thread_info);
5900 static void mdt_key_fini(const struct lu_context *ctx,
5901 struct lu_context_key *key, void* data)
5903 struct mdt_thread_info *info = data;
5905 if (info->mti_big_lmm) {
5906 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5907 info->mti_big_lmm = NULL;
5908 info->mti_big_lmmsize = 0;
5913 /* context key: mdt_thread_key */
5914 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5916 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5918 return lu_ucred(info->mti_env);
5921 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5923 return lu_ucred_check(info->mti_env);
5927 * Enable/disable COS (Commit On Sharing).
5929 * Set/Clear the COS flag in mdt options.
5931 * \param mdt mdt device
5932 * \param val 0 disables COS, other values enable COS
5934 void mdt_enable_cos(struct mdt_device *mdt, int val)
5939 mdt->mdt_opts.mo_cos = !!val;
5940 rc = lu_env_init(&env, LCT_LOCAL);
5941 if (unlikely(rc != 0)) {
5942 CWARN("%s: lu_env initialization failed, cannot "
5943 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
5946 mdt_device_sync(&env, mdt);
5951 * Check COS (Commit On Sharing) status.
5953 * Return COS flag status.
5955 * \param mdt mdt device
5957 int mdt_cos_is_enabled(struct mdt_device *mdt)
5959 return mdt->mdt_opts.mo_cos != 0;
5962 static struct lu_device_type_operations mdt_device_type_ops = {
5963 .ldto_device_alloc = mdt_device_alloc,
5964 .ldto_device_free = mdt_device_free,
5965 .ldto_device_fini = mdt_device_fini
5968 static struct lu_device_type mdt_device_type = {
5969 .ldt_tags = LU_DEVICE_MD,
5970 .ldt_name = LUSTRE_MDT_NAME,
5971 .ldt_ops = &mdt_device_type_ops,
5972 .ldt_ctx_tags = LCT_MD_THREAD
5975 static int __init mdt_mod_init(void)
5979 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5980 FID_NOBRACE_LEN + 1);
5981 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5983 rc = lu_kmem_init(mdt_caches);
5987 rc = mds_mod_init();
5991 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
5992 LUSTRE_MDT_NAME, &mdt_device_type);
5997 lu_kmem_fini(mdt_caches);
6004 static void __exit mdt_mod_exit(void)
6006 class_unregister_type(LUSTRE_MDT_NAME);
6008 lu_kmem_fini(mdt_caches);
6011 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
6012 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6013 MODULE_LICENSE("GPL");
6015 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);