4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 * struct OBD_{ALLOC,FREE}*()
55 #include <obd_support.h>
56 /* struct ptlrpc_request */
57 #include <lustre_net.h>
58 /* struct obd_export */
59 #include <lustre_export.h>
60 /* struct obd_device */
63 #include <dt_object.h>
64 #include <lustre_mds.h>
65 #include <lustre_log.h>
66 #include "mdt_internal.h"
67 #include <lustre_acl.h>
68 #include <lustre_param.h>
69 #include <lustre_quota.h>
70 #include <lustre_lfsck.h>
72 mdl_mode_t mdt_mdl_lock_modes[] = {
73 [LCK_MINMODE] = MDL_MINMODE,
80 [LCK_GROUP] = MDL_GROUP
83 ldlm_mode_t mdt_dlm_lock_modes[] = {
84 [MDL_MINMODE] = LCK_MINMODE,
91 [MDL_GROUP] = LCK_GROUP
94 static struct mdt_device *mdt_dev(struct lu_device *d);
95 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
97 static const struct lu_object_operations mdt_obj_ops;
99 /* Slab for MDT object allocation */
100 static struct kmem_cache *mdt_object_kmem;
102 /* For HSM restore handles */
103 struct kmem_cache *mdt_hsm_cdt_kmem;
105 /* For HSM request handles */
106 struct kmem_cache *mdt_hsm_car_kmem;
108 static struct lu_kmem_descr mdt_caches[] = {
110 .ckd_cache = &mdt_object_kmem,
111 .ckd_name = "mdt_obj",
112 .ckd_size = sizeof(struct mdt_object)
115 .ckd_cache = &mdt_hsm_cdt_kmem,
116 .ckd_name = "mdt_cdt_restore_handle",
117 .ckd_size = sizeof(struct cdt_restore_handle)
120 .ckd_cache = &mdt_hsm_car_kmem,
121 .ckd_name = "mdt_cdt_agent_req",
122 .ckd_size = sizeof(struct cdt_agent_req)
129 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
133 return (rep->lock_policy_res1 & flag);
136 void mdt_clear_disposition(struct mdt_thread_info *info,
137 struct ldlm_reply *rep, int flag)
140 info->mti_opdata &= ~flag;
141 tgt_opdata_clear(info->mti_env, flag);
144 rep->lock_policy_res1 &= ~flag;
147 void mdt_set_disposition(struct mdt_thread_info *info,
148 struct ldlm_reply *rep, int flag)
151 info->mti_opdata |= flag;
152 tgt_opdata_set(info->mti_env, flag);
155 rep->lock_policy_res1 |= flag;
158 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
160 lh->mlh_pdo_hash = 0;
161 lh->mlh_reg_mode = lm;
162 lh->mlh_rreg_mode = lm;
163 lh->mlh_type = MDT_REG_LOCK;
166 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lock_mode,
167 const struct lu_name *lname)
169 lh->mlh_reg_mode = lock_mode;
170 lh->mlh_rreg_mode = lock_mode;
171 lh->mlh_type = MDT_PDO_LOCK;
173 if (lu_name_is_valid(lname)) {
174 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
176 /* XXX Workaround for LU-2856
178 * Zero is a valid return value of full_name_hash, but
179 * several users of mlh_pdo_hash assume a non-zero
180 * hash value. We therefore map zero onto an
181 * arbitrary, but consistent value (1) to avoid
182 * problems further down the road. */
183 if (unlikely(lh->mlh_pdo_hash == 0))
184 lh->mlh_pdo_hash = 1;
186 lh->mlh_pdo_hash = 0;
190 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
191 struct mdt_lock_handle *lh)
197 * Any dir access needs couple of locks:
199 * 1) on part of dir we gonna take lookup/modify;
201 * 2) on whole dir to protect it from concurrent splitting and/or to
202 * flush client's cache for readdir().
204 * so, for a given mode and object this routine decides what lock mode
205 * to use for lock #2:
207 * 1) if caller's gonna lookup in dir then we need to protect dir from
208 * being splitted only - LCK_CR
210 * 2) if caller's gonna modify dir then we need to protect dir from
211 * being splitted and to flush cache - LCK_CW
213 * 3) if caller's gonna modify dir and that dir seems ready for
214 * splitting then we need to protect it from any type of access
215 * (lookup/modify/split) - LCK_EX --bzzz
218 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
219 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
222 * Ask underlaying level its opinion about preferable PDO lock mode
223 * having access type passed as regular lock mode:
225 * - MDL_MINMODE means that lower layer does not want to specify lock
228 * - MDL_NL means that no PDO lock should be taken. This is used in some
229 * cases. Say, for non-splittable directories no need to use PDO locks
232 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
233 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
235 if (mode != MDL_MINMODE) {
236 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
239 * Lower layer does not want to specify locking mode. We do it
240 * our selves. No special protection is needed, just flush
241 * client's cache on modification and allow concurrent
244 switch (lh->mlh_reg_mode) {
246 lh->mlh_pdo_mode = LCK_EX;
249 lh->mlh_pdo_mode = LCK_CR;
252 lh->mlh_pdo_mode = LCK_CW;
255 CERROR("Not expected lock type (0x%x)\n",
256 (int)lh->mlh_reg_mode);
261 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
265 int mdt_getstatus(struct tgt_session_info *tsi)
267 struct mdt_thread_info *info = tsi2mdt_info(tsi);
268 struct mdt_device *mdt = info->mti_mdt;
269 struct mdt_body *repbody;
274 rc = mdt_check_ucred(info);
276 GOTO(out, rc = err_serious(rc));
278 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK))
279 GOTO(out, rc = err_serious(-ENOMEM));
281 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
282 repbody->fid1 = mdt->mdt_md_root_fid;
283 repbody->valid |= OBD_MD_FLID;
285 if (tsi->tsi_tgt->lut_mds_capa &&
286 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
287 struct mdt_object *root;
288 struct lustre_capa *capa;
290 root = mdt_object_find(info->mti_env, mdt, &repbody->fid1);
292 GOTO(out, rc = PTR_ERR(root));
294 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
296 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
297 rc = mo_capa_get(info->mti_env, mdt_object_child(root), capa,
299 mdt_object_put(info->mti_env, root);
301 repbody->valid |= OBD_MD_FLMDSCAPA;
305 mdt_thread_info_fini(info);
309 int mdt_statfs(struct tgt_session_info *tsi)
311 struct ptlrpc_request *req = tgt_ses_req(tsi);
312 struct mdt_thread_info *info = tsi2mdt_info(tsi);
313 struct md_device *next = info->mti_mdt->mdt_child;
314 struct ptlrpc_service_part *svcpt;
315 struct obd_statfs *osfs;
320 svcpt = req->rq_rqbd->rqbd_svcpt;
322 /* This will trigger a watchdog timeout */
323 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
324 (MDT_SERVICE_WATCHDOG_FACTOR *
325 at_get(&svcpt->scp_at_estimate)) + 1);
327 rc = mdt_check_ucred(info);
329 GOTO(out, rc = err_serious(rc));
331 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
332 GOTO(out, rc = err_serious(-ENOMEM));
334 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
336 GOTO(out, rc = -EPROTO);
338 /** statfs information are cached in the mdt_device */
339 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
340 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
341 /** statfs data is too old, get up-to-date one */
342 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
345 spin_lock(&info->mti_mdt->mdt_osfs_lock);
346 info->mti_mdt->mdt_osfs = *osfs;
347 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
348 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
350 /** use cached statfs data */
351 spin_lock(&info->mti_mdt->mdt_osfs_lock);
352 *osfs = info->mti_mdt->mdt_osfs;
353 spin_unlock(&info->mti_mdt->mdt_osfs_lock);
357 mdt_counter_incr(req, LPROC_MDT_STATFS);
359 mdt_thread_info_fini(info);
364 * Pack SOM attributes into the reply.
365 * Call under a DLM UPDATE lock.
367 static void mdt_pack_size2body(struct mdt_thread_info *info,
368 struct mdt_object *mo)
371 struct md_attr *ma = &info->mti_attr;
373 LASSERT(ma->ma_attr.la_valid & LA_MODE);
374 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
376 /* Check if Size-on-MDS is supported, if this is a regular file,
377 * if SOM is enabled on the object and if SOM cache exists and valid.
378 * Otherwise do not pack Size-on-MDS attributes to the reply. */
379 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM) ||
380 !S_ISREG(ma->ma_attr.la_mode) ||
381 !mdt_object_is_som_enabled(mo) ||
382 !(ma->ma_valid & MA_SOM))
385 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
386 b->size = ma->ma_som->msd_size;
387 b->blocks = ma->ma_som->msd_blocks;
390 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
391 const struct lu_attr *attr, const struct lu_fid *fid)
393 struct md_attr *ma = &info->mti_attr;
395 LASSERT(ma->ma_valid & MA_INODE);
397 b->atime = attr->la_atime;
398 b->mtime = attr->la_mtime;
399 b->ctime = attr->la_ctime;
400 b->mode = attr->la_mode;
401 b->size = attr->la_size;
402 b->blocks = attr->la_blocks;
403 b->uid = attr->la_uid;
404 b->gid = attr->la_gid;
405 b->flags = attr->la_flags;
406 b->nlink = attr->la_nlink;
407 b->rdev = attr->la_rdev;
409 /*XXX should pack the reply body according to lu_valid*/
410 b->valid |= OBD_MD_FLCTIME | OBD_MD_FLUID |
411 OBD_MD_FLGID | OBD_MD_FLTYPE |
412 OBD_MD_FLMODE | OBD_MD_FLNLINK | OBD_MD_FLFLAGS |
413 OBD_MD_FLATIME | OBD_MD_FLMTIME ;
415 if (!S_ISREG(attr->la_mode)) {
416 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
417 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
418 /* means no objects are allocated on osts. */
419 LASSERT(!(ma->ma_valid & MA_LOV));
420 /* just ignore blocks occupied by extend attributes on MDS */
422 /* if no object is allocated on osts, the size on mds is valid. b=22272 */
423 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
424 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
425 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
426 /* A released file stores its size on MDS. */
427 /* But return 1 block for released file, unless tools like tar
428 * will consider it fully sparse. (LU-3864)
430 if (unlikely(b->size == 0))
434 b->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
439 b->valid |= OBD_MD_FLID;
440 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, size="LPU64"\n",
441 PFID(fid), b->nlink, b->mode, b->size);
445 mdt_body_reverse_idmap(info, b);
447 if (fid != NULL && (b->valid & OBD_MD_FLSIZE))
448 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
449 PFID(fid), (unsigned long long)b->size);
452 static inline int mdt_body_has_lov(const struct lu_attr *la,
453 const struct mdt_body *body)
455 return ((S_ISREG(la->la_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
456 (S_ISDIR(la->la_mode) && (body->valid & OBD_MD_FLDIREA )) );
459 void mdt_client_compatibility(struct mdt_thread_info *info)
461 struct mdt_body *body;
462 struct ptlrpc_request *req = mdt_info_req(info);
463 struct obd_export *exp = req->rq_export;
464 struct md_attr *ma = &info->mti_attr;
465 struct lu_attr *la = &ma->ma_attr;
468 if (exp_connect_layout(exp))
469 /* the client can deal with 16-bit lmm_stripe_count */
472 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
474 if (!mdt_body_has_lov(la, body))
477 /* now we have a reply with a lov for a client not compatible with the
478 * layout lock so we have to clean the layout generation number */
479 if (S_ISREG(la->la_mode))
480 ma->ma_lmm->lmm_layout_gen = 0;
484 int mdt_attr_get_eabuf_size(struct mdt_thread_info *info, struct mdt_object *o)
486 const struct lu_env *env = info->mti_env;
489 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
498 /* Is it a directory? Let's check for the LMV as well */
499 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
500 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
502 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
510 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
513 const struct lu_env *env = info->mti_env;
517 LASSERT(info->mti_big_lmm_used == 0);
518 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
522 /* big_lmm may need to be grown */
523 if (info->mti_big_lmmsize < rc) {
524 int size = size_roundup_power2(rc);
526 if (info->mti_big_lmmsize > 0) {
527 /* free old buffer */
528 LASSERT(info->mti_big_lmm);
529 OBD_FREE_LARGE(info->mti_big_lmm,
530 info->mti_big_lmmsize);
531 info->mti_big_lmm = NULL;
532 info->mti_big_lmmsize = 0;
535 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
536 if (info->mti_big_lmm == NULL)
538 info->mti_big_lmmsize = size;
540 LASSERT(info->mti_big_lmmsize >= rc);
542 info->mti_buf.lb_buf = info->mti_big_lmm;
543 info->mti_buf.lb_len = info->mti_big_lmmsize;
544 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
549 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
550 struct md_attr *ma, const char *name)
552 struct md_object *next = mdt_object_child(o);
553 struct lu_buf *buf = &info->mti_buf;
556 if (strcmp(name, XATTR_NAME_LOV) == 0) {
557 buf->lb_buf = ma->ma_lmm;
558 buf->lb_len = ma->ma_lmm_size;
559 LASSERT(!(ma->ma_valid & MA_LOV));
560 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
561 buf->lb_buf = ma->ma_lmv;
562 buf->lb_len = ma->ma_lmv_size;
563 LASSERT(!(ma->ma_valid & MA_LMV));
564 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
565 buf->lb_buf = ma->ma_lmv;
566 buf->lb_len = ma->ma_lmv_size;
567 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
572 rc = mo_xattr_get(info->mti_env, next, buf, name);
574 if (strcmp(name, XATTR_NAME_LOV) == 0) {
575 ma->ma_lmm_size = rc;
576 ma->ma_valid |= MA_LOV;
577 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
578 ma->ma_lmv_size = rc;
579 ma->ma_valid |= MA_LMV;
580 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
581 ma->ma_lmv_size = rc;
582 ma->ma_valid |= MA_LMV_DEF;
586 } else if (rc == -ENODATA) {
589 } else if (rc == -ERANGE) {
590 /* Default LMV has fixed size, so it must be able to fit
591 * in the original buffer */
592 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
594 rc = mdt_big_xattr_get(info, o, name);
596 info->mti_big_lmm_used = 1;
597 if (!strcmp(name, XATTR_NAME_LOV)) {
598 ma->ma_valid |= MA_LOV;
599 ma->ma_lmm = info->mti_big_lmm;
600 ma->ma_lmm_size = rc;
601 } else if (!strcmp(name, XATTR_NAME_LMV)) {
602 ma->ma_valid |= MA_LMV;
603 ma->ma_lmv = info->mti_big_lmm;
604 ma->ma_lmv_size = rc;
609 /* update mdt_max_mdsize so all clients
610 * will be aware about that */
611 if (info->mti_mdt->mdt_max_mdsize < rc)
612 info->mti_mdt->mdt_max_mdsize = rc;
620 int mdt_attr_get_pfid(struct mdt_thread_info *info,
621 struct mdt_object *o, struct lu_fid *pfid)
623 struct lu_buf *buf = &info->mti_buf;
624 struct link_ea_header *leh;
625 struct link_ea_entry *lee;
629 buf->lb_buf = info->mti_big_lmm;
630 buf->lb_len = info->mti_big_lmmsize;
631 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
632 buf, XATTR_NAME_LINK);
633 /* ignore errors, MA_PFID won't be set and it is
634 * up to the caller to treat this as an error */
635 if (rc == -ERANGE || buf->lb_len == 0) {
636 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
637 buf->lb_buf = info->mti_big_lmm;
638 buf->lb_len = info->mti_big_lmmsize;
643 if (rc < sizeof(*leh)) {
644 CERROR("short LinkEA on "DFID": rc = %d\n",
645 PFID(mdt_object_fid(o)), rc);
649 leh = (struct link_ea_header *) buf->lb_buf;
650 lee = (struct link_ea_entry *)(leh + 1);
651 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
652 leh->leh_magic = LINK_EA_MAGIC;
653 leh->leh_reccount = __swab32(leh->leh_reccount);
654 leh->leh_len = __swab64(leh->leh_len);
656 if (leh->leh_magic != LINK_EA_MAGIC)
658 if (leh->leh_reccount == 0)
661 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
662 fid_be_to_cpu(pfid, pfid);
667 int mdt_attr_get_complex(struct mdt_thread_info *info,
668 struct mdt_object *o, struct md_attr *ma)
670 const struct lu_env *env = info->mti_env;
671 struct md_object *next = mdt_object_child(o);
672 struct lu_buf *buf = &info->mti_buf;
673 u32 mode = lu_object_attr(&next->mo_lu);
674 int need = ma->ma_need;
680 if (need & MA_INODE) {
681 ma->ma_need = MA_INODE;
682 rc = mo_attr_get(env, next, ma);
685 ma->ma_valid |= MA_INODE;
688 if (need & MA_PFID) {
689 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
691 ma->ma_valid |= MA_PFID;
692 /* ignore this error, parent fid is not mandatory */
696 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
697 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
702 if (need & MA_LMV && S_ISDIR(mode)) {
703 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
708 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
709 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
714 if (need & MA_SOM && S_ISREG(mode)) {
715 buf->lb_buf = info->mti_xattr_buf;
716 buf->lb_len = sizeof(info->mti_xattr_buf);
717 CLASSERT(sizeof(struct som_attrs) <=
718 sizeof(info->mti_xattr_buf));
719 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
720 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
722 ma->ma_valid |= MA_SOM;
723 else if (rc2 < 0 && rc2 != -ENODATA)
727 if (need & MA_HSM && S_ISREG(mode)) {
728 buf->lb_buf = info->mti_xattr_buf;
729 buf->lb_len = sizeof(info->mti_xattr_buf);
730 CLASSERT(sizeof(struct hsm_attrs) <=
731 sizeof(info->mti_xattr_buf));
732 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
733 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
735 ma->ma_valid |= MA_HSM;
736 else if (rc2 < 0 && rc2 != -ENODATA)
740 #ifdef CONFIG_FS_POSIX_ACL
741 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
742 buf->lb_buf = ma->ma_acl;
743 buf->lb_len = ma->ma_acl_size;
744 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
746 ma->ma_acl_size = rc2;
747 ma->ma_valid |= MA_ACL_DEF;
748 } else if (rc2 == -ENODATA) {
757 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
758 rc, ma->ma_valid, ma->ma_lmm);
762 static int mdt_getattr_internal(struct mdt_thread_info *info,
763 struct mdt_object *o, int ma_need)
765 struct md_object *next = mdt_object_child(o);
766 const struct mdt_body *reqbody = info->mti_body;
767 struct ptlrpc_request *req = mdt_info_req(info);
768 struct md_attr *ma = &info->mti_attr;
769 struct lu_attr *la = &ma->ma_attr;
770 struct req_capsule *pill = info->mti_pill;
771 const struct lu_env *env = info->mti_env;
772 struct mdt_body *repbody;
773 struct lu_buf *buffer = &info->mti_buf;
778 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
779 RETURN(err_serious(-ENOMEM));
781 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
785 if (mdt_object_remote(o)) {
786 /* This object is located on remote node.*/
787 /* Return -EIO for old client */
788 if (!mdt_is_dne_client(req->rq_export))
789 GOTO(out, rc = -EIO);
791 repbody->fid1 = *mdt_object_fid(o);
792 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
796 if (reqbody->eadatasize > 0) {
797 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
798 if (buffer->lb_buf == NULL)
799 GOTO(out, rc = -EPROTO);
800 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
803 buffer->lb_buf = NULL;
805 ma_need &= ~(MA_LOV | MA_LMV);
806 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
807 mdt_obd_name(info->mti_mdt),
808 req->rq_export->exp_client_uuid.uuid);
811 /* If it is dir object and client require MEA, then we got MEA */
812 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
813 (reqbody->valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
814 /* Assumption: MDT_MD size is enough for lmv size. */
815 ma->ma_lmv = buffer->lb_buf;
816 ma->ma_lmv_size = buffer->lb_len;
817 ma->ma_need = MA_INODE;
818 if (ma->ma_lmv_size > 0) {
819 if (reqbody->valid & OBD_MD_MEA)
820 ma->ma_need |= MA_LMV;
821 else if (reqbody->valid & OBD_MD_DEFAULT_MEA)
822 ma->ma_need |= MA_LMV_DEF;
825 ma->ma_lmm = buffer->lb_buf;
826 ma->ma_lmm_size = buffer->lb_len;
827 ma->ma_need = MA_INODE | MA_HSM;
828 if (ma->ma_lmm_size > 0)
829 ma->ma_need |= MA_LOV;
832 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
833 reqbody->valid & OBD_MD_FLDIREA &&
834 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
835 /* get default stripe info for this dir. */
836 ma->ma_need |= MA_LOV_DEF;
838 ma->ma_need |= ma_need;
839 if (ma->ma_need & MA_SOM)
840 ma->ma_som = &info->mti_u.som.data;
842 rc = mdt_attr_get_complex(info, o, ma);
844 CERROR("%s: getattr error for "DFID": rc = %d\n",
845 mdt_obd_name(info->mti_mdt),
846 PFID(mdt_object_fid(o)), rc);
850 /* if file is released, check if a restore is running */
851 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
852 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
853 repbody->t_state = MS_RESTORE;
854 repbody->valid |= OBD_MD_TSTATE;
857 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
859 /* the Lustre protocol supposes to return default striping
860 * on the user-visible root if explicitly requested */
861 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
862 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
863 struct lu_fid rootfid;
864 struct mdt_object *root;
865 struct mdt_device *mdt = info->mti_mdt;
867 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
870 root = mdt_object_find(env, mdt, &rootfid);
872 RETURN(PTR_ERR(root));
873 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
874 mdt_object_put(info->mti_env, root);
876 CERROR("%s: getattr error for "DFID": rc = %d\n",
877 mdt_obd_name(info->mti_mdt),
878 PFID(mdt_object_fid(o)), rc);
883 if (likely(ma->ma_valid & MA_INODE))
884 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
888 if (mdt_body_has_lov(la, reqbody)) {
889 if (ma->ma_valid & MA_LOV) {
890 LASSERT(ma->ma_lmm_size);
891 repbody->eadatasize = ma->ma_lmm_size;
892 if (S_ISDIR(la->la_mode))
893 repbody->valid |= OBD_MD_FLDIREA;
895 repbody->valid |= OBD_MD_FLEASIZE;
896 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->valid);
898 if (ma->ma_valid & MA_LMV) {
899 LASSERT(S_ISDIR(la->la_mode));
900 mdt_dump_lmv(D_INFO, ma->ma_lmv);
901 repbody->eadatasize = ma->ma_lmv_size;
902 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
904 if (ma->ma_valid & MA_LMV_DEF) {
905 LASSERT(S_ISDIR(la->la_mode));
906 repbody->eadatasize = ma->ma_lmv_size;
907 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_DEFAULT_MEA);
909 } else if (S_ISLNK(la->la_mode) &&
910 reqbody->valid & OBD_MD_LINKNAME) {
911 buffer->lb_buf = ma->ma_lmm;
912 /* eadatasize from client includes NULL-terminator, so
913 * there is no need to read it */
914 buffer->lb_len = reqbody->eadatasize - 1;
915 rc = mo_readlink(env, next, buffer);
916 if (unlikely(rc <= 0)) {
917 CERROR("%s: readlink failed for "DFID": rc = %d\n",
918 mdt_obd_name(info->mti_mdt),
919 PFID(mdt_object_fid(o)), rc);
922 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
924 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
926 repbody->valid |= OBD_MD_LINKNAME;
927 /* we need to report back size with NULL-terminator
928 * because client expects that */
929 repbody->eadatasize = rc + 1;
930 if (repbody->eadatasize != reqbody->eadatasize)
931 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
932 "on "DFID ", expected %d\n",
933 mdt_obd_name(info->mti_mdt),
934 rc, PFID(mdt_object_fid(o)),
935 reqbody->eadatasize - 1);
937 ((char *)ma->ma_lmm)[rc] = 0;
939 /* If the total CDEBUG() size is larger than a page, it
940 * will print a warning to the console, avoid this by
941 * printing just the last part of the symlink. */
942 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
943 print_limit < rc ? "..." : "", print_limit,
944 (char *)ma->ma_lmm + rc - print_limit, rc);
949 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
950 repbody->max_cookiesize = 0;
951 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
952 repbody->valid |= OBD_MD_FLMODEASIZE;
953 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
954 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
955 repbody->max_cookiesize);
958 if (exp_connect_rmtclient(info->mti_exp) &&
959 reqbody->valid & OBD_MD_FLRMTPERM) {
960 void *buf = req_capsule_server_get(pill, &RMF_ACL);
962 /* mdt_getattr_lock only */
963 rc = mdt_pack_remote_perm(info, o, buf);
965 repbody->valid &= ~OBD_MD_FLRMTPERM;
966 repbody->aclsize = 0;
969 repbody->valid |= OBD_MD_FLRMTPERM;
970 repbody->aclsize = sizeof(struct mdt_remote_perm);
973 #ifdef CONFIG_FS_POSIX_ACL
974 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
975 (reqbody->valid & OBD_MD_FLACL)) {
976 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
977 buffer->lb_len = req_capsule_get_size(pill,
978 &RMF_ACL, RCL_SERVER);
979 if (buffer->lb_len > 0) {
980 rc = mo_xattr_get(env, next, buffer,
981 XATTR_NAME_ACL_ACCESS);
983 if (rc == -ENODATA) {
984 repbody->aclsize = 0;
985 repbody->valid |= OBD_MD_FLACL;
987 } else if (rc == -EOPNOTSUPP) {
990 CERROR("%s: unable to read "DFID
992 mdt_obd_name(info->mti_mdt),
993 PFID(mdt_object_fid(o)), rc);
996 repbody->aclsize = rc;
997 repbody->valid |= OBD_MD_FLACL;
1004 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
1005 info->mti_mdt->mdt_lut.lut_mds_capa &&
1006 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
1007 struct lustre_capa *capa;
1009 capa = req_capsule_server_get(pill, &RMF_CAPA1);
1011 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
1012 rc = mo_capa_get(env, next, capa, 0);
1015 repbody->valid |= OBD_MD_FLMDSCAPA;
1020 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1025 static int mdt_renew_capa(struct mdt_thread_info *info)
1027 struct mdt_object *obj = info->mti_object;
1028 struct mdt_body *body;
1029 struct lustre_capa *capa, *c;
1033 /* if object doesn't exist, or server has disabled capability,
1034 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
1037 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
1038 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
1041 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1042 LASSERT(body != NULL);
1044 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
1047 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
1051 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
1053 body->valid |= OBD_MD_FLOSSCAPA;
1057 int mdt_getattr(struct tgt_session_info *tsi)
1059 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1060 struct mdt_object *obj = info->mti_object;
1061 struct req_capsule *pill = info->mti_pill;
1062 struct mdt_body *reqbody;
1063 struct mdt_body *repbody;
1068 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1071 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
1072 rc = req_capsule_server_pack(pill);
1074 RETURN(err_serious(rc));
1075 rc = mdt_renew_capa(info);
1076 GOTO(out_shrink, rc);
1079 LASSERT(obj != NULL);
1080 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1082 mode = lu_object_attr(&obj->mot_obj);
1085 if (reqbody->valid & OBD_MD_LINKNAME) {
1086 /* No easy way to know how long is the symlink, but it cannot
1087 * be more than PATH_MAX, so we allocate +1 */
1090 /* A special case for fs ROOT: getattr there might fetch
1091 * default EA for entire fs, not just for this dir!
1093 } else if (lu_fid_eq(mdt_object_fid(obj),
1094 &info->mti_mdt->mdt_md_root_fid) &&
1095 (reqbody->valid & OBD_MD_FLDIREA) &&
1096 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1098 /* Should the default strping be bigger, mdt_fix_reply
1099 * will reallocate */
1100 rc = DEF_REP_MD_SIZE;
1102 /* Hopefully no race in EA change for either file or directory?
1104 rc = mdt_attr_get_eabuf_size(info, obj);
1108 GOTO(out_shrink, rc);
1110 /* old clients may not report needed easize, use max value then */
1111 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1113 rc = req_capsule_server_pack(pill);
1114 if (unlikely(rc != 0))
1115 GOTO(out, rc = err_serious(rc));
1117 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1118 LASSERT(repbody != NULL);
1119 repbody->eadatasize = 0;
1120 repbody->aclsize = 0;
1122 if (reqbody->valid & OBD_MD_FLRMTPERM)
1123 rc = mdt_init_ucred(info, reqbody);
1125 rc = mdt_check_ucred(info);
1127 GOTO(out_shrink, rc);
1129 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1132 * Don't check capability at all, because rename might getattr for
1133 * remote obj, and at that time no capability is available.
1135 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1136 rc = mdt_getattr_internal(info, obj, 0);
1137 if (reqbody->valid & OBD_MD_FLRMTPERM)
1138 mdt_exit_ucred(info);
1141 mdt_client_compatibility(info);
1142 rc2 = mdt_fix_reply(info);
1146 mdt_thread_info_fini(info);
1150 int mdt_is_subdir(struct tgt_session_info *tsi)
1152 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1153 struct mdt_object *o = info->mti_object;
1154 struct req_capsule *pill = info->mti_pill;
1155 const struct mdt_body *body = info->mti_body;
1156 struct mdt_body *repbody;
1162 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1165 * We save last checked parent fid to @repbody->fid1 for remote
1168 LASSERT(fid_is_sane(&body->fid2));
1169 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1170 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1171 &body->fid2, &repbody->fid1);
1172 if (rc == 0 || rc == -EREMOTE)
1173 repbody->valid |= OBD_MD_FLID;
1175 mdt_thread_info_fini(info);
1179 int mdt_swap_layouts(struct tgt_session_info *tsi)
1181 struct mdt_thread_info *info;
1182 struct ptlrpc_request *req = tgt_ses_req(tsi);
1183 struct obd_export *exp = req->rq_export;
1184 struct mdt_object *o1, *o2, *o;
1185 struct mdt_lock_handle *lh1, *lh2;
1186 struct mdc_swap_layouts *msl;
1190 /* client does not support layout lock, so layout swaping
1192 * FIXME: there is a problem for old clients which don't support
1193 * layout lock yet. If those clients have already opened the file
1194 * they won't be notified at all so that old layout may still be
1195 * used to do IO. This can be fixed after file release is landed by
1196 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1197 if (!exp_connect_layout(exp))
1198 RETURN(-EOPNOTSUPP);
1200 info = tsi2mdt_info(tsi);
1202 if (info->mti_dlm_req != NULL)
1203 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1205 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1206 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1207 req_capsule_client_get(info->mti_pill,
1210 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1211 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1212 req_capsule_client_get(info->mti_pill,
1215 o1 = info->mti_object;
1216 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1217 &info->mti_body->fid2);
1219 GOTO(out, rc = PTR_ERR(o));
1221 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1222 GOTO(put, rc = -ENOENT);
1224 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1225 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1231 /* permission check. Make sure the calling process having permission
1232 * to write both files. */
1233 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1238 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1243 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1245 GOTO(put, rc = -EPROTO);
1247 lh1 = &info->mti_lh[MDT_LH_NEW];
1248 mdt_lock_reg_init(lh1, LCK_EX);
1249 lh2 = &info->mti_lh[MDT_LH_OLD];
1250 mdt_lock_reg_init(lh2, LCK_EX);
1252 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1253 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1257 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1258 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1262 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1263 mdt_object_child(o2), msl->msl_flags);
1266 mdt_object_unlock(info, o2, lh2, rc);
1268 mdt_object_unlock(info, o1, lh1, rc);
1270 mdt_object_put(info->mti_env, o);
1272 mdt_thread_info_fini(info);
1276 static int mdt_raw_lookup(struct mdt_thread_info *info,
1277 struct mdt_object *parent,
1278 const struct lu_name *lname,
1279 struct ldlm_reply *ldlm_rep)
1281 struct md_object *next = mdt_object_child(info->mti_object);
1282 const struct mdt_body *reqbody = info->mti_body;
1283 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1284 struct mdt_body *repbody;
1288 if (reqbody->valid != OBD_MD_FLID)
1291 LASSERT(!info->mti_cross_ref);
1293 /* Only got the fid of this obj by name */
1294 fid_zero(child_fid);
1295 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1298 /* XXX is raw_lookup possible as intent operation? */
1301 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1304 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1306 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1309 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1310 repbody->fid1 = *child_fid;
1311 repbody->valid = OBD_MD_FLID;
1317 * UPDATE lock should be taken against parent, and be release before exit;
1318 * child_bits lock should be taken against child, and be returned back:
1319 * (1)normal request should release the child lock;
1320 * (2)intent request will grant the lock to client.
1322 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1323 struct mdt_lock_handle *lhc,
1325 struct ldlm_reply *ldlm_rep)
1327 struct ptlrpc_request *req = mdt_info_req(info);
1328 struct mdt_body *reqbody = NULL;
1329 struct mdt_object *parent = info->mti_object;
1330 struct mdt_object *child;
1331 struct md_object *next = mdt_object_child(parent);
1332 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1333 struct lu_name *lname = NULL;
1334 struct mdt_lock_handle *lhp = NULL;
1335 struct ldlm_lock *lock;
1336 struct ldlm_res_id *res_id;
1343 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1344 LASSERT(ergo(is_resent,
1345 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1347 LASSERT(parent != NULL);
1349 lname = &info->mti_name;
1350 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1352 if (!info->mti_cross_ref) {
1354 * XXX: Check for anonymous name is for getattr by fid
1355 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1356 * that is the name must contain at least one character and
1357 * the terminating '\0'.
1359 if (!lu_name_is_valid(lname)) {
1360 reqbody = req_capsule_client_get(info->mti_pill,
1362 if (unlikely(reqbody == NULL))
1363 RETURN(err_serious(-EFAULT));
1365 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1366 RETURN(err_serious(-EINVAL));
1368 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1370 PFID(mdt_object_fid(parent)),
1371 PFID(&reqbody->fid2), ldlm_rep);
1373 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1374 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1375 PNAME(lname), ldlm_rep);
1379 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1381 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1382 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1384 "Parent doesn't exist!\n");
1386 } else if (!info->mti_cross_ref) {
1387 LASSERTF(!mdt_object_remote(parent),
1388 "Parent "DFID" is on remote server\n",
1389 PFID(mdt_object_fid(parent)));
1392 if (lu_name_is_valid(lname)) {
1393 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1401 if (info->mti_cross_ref) {
1402 /* Only getattr on the child. Parent is on another node. */
1403 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1405 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1406 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1409 /* Do not take lock for resent case. */
1410 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1411 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1412 lhc->mlh_reg_lh.cookie);
1413 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1414 &lock->l_resource->lr_name));
1415 LDLM_LOCK_PUT(lock);
1418 mdt_lock_handle_init(lhc);
1419 mdt_lock_reg_init(lhc, LCK_PR);
1422 * Object's name is on another MDS, no lookup or layout
1423 * lock is needed here but update lock is.
1425 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1426 MDS_INODELOCK_LAYOUT);
1427 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1429 rc = mdt_object_lock(info, child, lhc, child_bits,
1433 /* Finally, we can get attr for child. */
1434 if (!mdt_object_exists(child)) {
1435 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1437 "remote object doesn't exist.\n");
1438 mdt_object_unlock(info, child, lhc, 1);
1442 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1444 rc = mdt_getattr_internal(info, child, 0);
1445 if (unlikely(rc != 0))
1446 mdt_object_unlock(info, child, lhc, 1);
1451 if (lu_name_is_valid(lname)) {
1452 /* step 1: lock parent only if parent is a directory */
1453 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1454 lhp = &info->mti_lh[MDT_LH_PARENT];
1455 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1456 rc = mdt_object_lock(info, parent, lhp,
1457 MDS_INODELOCK_UPDATE,
1459 if (unlikely(rc != 0))
1463 /* step 2: lookup child's fid by name */
1464 fid_zero(child_fid);
1465 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1470 mdt_set_disposition(info, ldlm_rep,
1472 GOTO(out_parent, rc);
1474 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1476 *child_fid = reqbody->fid2;
1477 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 /* Do not take lock for resent case. */
1501 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1502 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1503 lhc->mlh_reg_lh.cookie);
1505 res_id = &lock->l_resource->lr_name;
1506 if (!fid_res_name_eq(mdt_object_fid(child),
1507 &lock->l_resource->lr_name)) {
1508 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1509 &lock->l_resource->lr_name),
1510 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1511 PLDLMRES(lock->l_resource),
1512 PFID(mdt_object_fid(parent)));
1513 CWARN("Although resent, but still not get child lock"
1514 "parent:"DFID" child:"DFID"\n",
1515 PFID(mdt_object_fid(parent)),
1516 PFID(mdt_object_fid(child)));
1517 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1518 LDLM_LOCK_PUT(lock);
1521 LDLM_LOCK_PUT(lock);
1524 bool try_layout = false;
1527 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1528 mdt_lock_handle_init(lhc);
1529 mdt_lock_reg_init(lhc, LCK_PR);
1531 if (!mdt_object_exists(child)) {
1532 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1534 "Object doesn't exist!\n");
1535 GOTO(out_child, rc = -ENOENT);
1538 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1539 mdt_object_exists(child) && !mdt_object_remote(child)) {
1540 struct md_attr *ma = &info->mti_attr;
1543 ma->ma_need = MA_INODE;
1544 rc = mdt_attr_get_complex(info, child, ma);
1545 if (unlikely(rc != 0))
1546 GOTO(out_child, rc);
1548 /* If the file has not been changed for some time, we
1549 * return not only a LOOKUP lock, but also an UPDATE
1550 * lock and this might save us RPC on later STAT. For
1551 * directories, it also let negative dentry cache start
1552 * working for this dir. */
1553 if (ma->ma_valid & MA_INODE &&
1554 ma->ma_attr.la_valid & LA_CTIME &&
1555 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1556 ma->ma_attr.la_ctime < cfs_time_current_sec())
1557 child_bits |= MDS_INODELOCK_UPDATE;
1560 /* layout lock must be granted in a best-effort way
1561 * for IT operations */
1562 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1563 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1564 exp_connect_layout(info->mti_exp) &&
1565 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1566 !mdt_object_remote(child) && ldlm_rep != NULL) {
1567 /* try to grant layout lock for regular file. */
1573 child_bits |= MDS_INODELOCK_LAYOUT;
1574 /* try layout lock, it may fail to be granted due to
1575 * contention at LOOKUP or UPDATE */
1576 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1578 child_bits &= ~MDS_INODELOCK_LAYOUT;
1579 LASSERT(child_bits != 0);
1580 rc = mdt_object_lock(info, child, lhc,
1581 child_bits, MDT_CROSS_LOCK);
1586 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1587 * client will enqueue the lock to the remote MDT */
1588 if (mdt_object_remote(child))
1589 child_bits &= ~MDS_INODELOCK_UPDATE;
1590 rc = mdt_object_lock(info, child, lhc, child_bits,
1593 if (unlikely(rc != 0))
1594 GOTO(out_child, rc);
1597 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1598 /* Get MA_SOM attributes if update lock is given. */
1600 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1601 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1604 /* finally, we can get attr for child. */
1605 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1606 rc = mdt_getattr_internal(info, child, ma_need);
1607 if (unlikely(rc != 0)) {
1608 mdt_object_unlock(info, child, lhc, 1);
1610 /* Debugging code. */
1611 res_id = &lock->l_resource->lr_name;
1612 LDLM_DEBUG(lock, "Returning lock to client");
1613 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1614 &lock->l_resource->lr_name),
1615 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1616 PLDLMRES(lock->l_resource),
1617 PFID(mdt_object_fid(child)));
1618 if (mdt_object_exists(child) && !mdt_object_remote(child))
1619 mdt_pack_size2body(info, child);
1622 LDLM_LOCK_PUT(lock);
1626 mdt_object_put(info->mti_env, child);
1629 mdt_object_unlock(info, parent, lhp, 1);
1633 /* normal handler: should release the child lock */
1634 int mdt_getattr_name(struct tgt_session_info *tsi)
1636 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1637 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1638 struct mdt_body *reqbody;
1639 struct mdt_body *repbody;
1643 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1644 LASSERT(reqbody != NULL);
1645 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1646 LASSERT(repbody != NULL);
1648 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1649 repbody->eadatasize = 0;
1650 repbody->aclsize = 0;
1652 rc = mdt_init_ucred(info, reqbody);
1654 GOTO(out_shrink, rc);
1656 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1657 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1658 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1659 lhc->mlh_reg_lh.cookie = 0;
1661 mdt_exit_ucred(info);
1664 mdt_client_compatibility(info);
1665 rc2 = mdt_fix_reply(info);
1668 mdt_thread_info_fini(info);
1672 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1673 void *karg, void *uarg);
1675 int mdt_set_info(struct tgt_session_info *tsi)
1677 struct ptlrpc_request *req = tgt_ses_req(tsi);
1680 int keylen, vallen, rc = 0;
1684 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1686 DEBUG_REQ(D_HA, req, "no set_info key");
1687 RETURN(err_serious(-EFAULT));
1690 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1693 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1695 DEBUG_REQ(D_HA, req, "no set_info val");
1696 RETURN(err_serious(-EFAULT));
1699 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1702 /* Swab any part of val you need to here */
1703 if (KEY_IS(KEY_READ_ONLY)) {
1704 spin_lock(&req->rq_export->exp_lock);
1706 *exp_connect_flags_ptr(req->rq_export) |=
1709 *exp_connect_flags_ptr(req->rq_export) &=
1710 ~OBD_CONNECT_RDONLY;
1711 spin_unlock(&req->rq_export->exp_lock);
1712 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1713 struct changelog_setinfo *cs = val;
1715 if (vallen != sizeof(*cs)) {
1716 CERROR("%s: bad changelog_clear setinfo size %d\n",
1717 tgt_name(tsi->tsi_tgt), vallen);
1720 if (ptlrpc_req_need_swab(req)) {
1721 __swab64s(&cs->cs_recno);
1722 __swab32s(&cs->cs_id);
1725 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1733 int mdt_readpage(struct tgt_session_info *tsi)
1735 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1736 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1737 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1738 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1739 struct mdt_body *repbody;
1745 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1746 RETURN(err_serious(-ENOMEM));
1748 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1749 if (repbody == NULL || reqbody == NULL)
1750 RETURN(err_serious(-EFAULT));
1753 * prepare @rdpg before calling lower layers and transfer itself. Here
1754 * reqbody->size contains offset of where to start to read and
1755 * reqbody->nlink contains number bytes to read.
1757 rdpg->rp_hash = reqbody->size;
1758 if (rdpg->rp_hash != reqbody->size) {
1759 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1760 rdpg->rp_hash, reqbody->size);
1764 rdpg->rp_attrs = reqbody->mode;
1765 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1766 rdpg->rp_attrs |= LUDA_64BITHASH;
1767 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1768 exp_max_brw_size(tsi->tsi_exp));
1769 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1771 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1772 if (rdpg->rp_pages == NULL)
1775 for (i = 0; i < rdpg->rp_npages; ++i) {
1776 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1777 if (rdpg->rp_pages[i] == NULL)
1778 GOTO(free_rdpg, rc = -ENOMEM);
1781 /* call lower layers to fill allocated pages with directory data */
1782 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1784 GOTO(free_rdpg, rc);
1786 /* send pages to client */
1787 rc = tgt_sendpage(tsi, rdpg, rc);
1792 for (i = 0; i < rdpg->rp_npages; i++)
1793 if (rdpg->rp_pages[i] != NULL)
1794 __free_page(rdpg->rp_pages[i]);
1795 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1797 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1803 static int mdt_reint_internal(struct mdt_thread_info *info,
1804 struct mdt_lock_handle *lhc,
1807 struct req_capsule *pill = info->mti_pill;
1808 struct mdt_body *repbody;
1813 rc = mdt_reint_unpack(info, op);
1815 CERROR("Can't unpack reint, rc %d\n", rc);
1816 RETURN(err_serious(rc));
1819 /* for replay (no_create) lmm is not needed, client has it already */
1820 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1821 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1824 /* llog cookies are always 0, the field is kept for compatibility */
1825 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1826 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1828 rc = req_capsule_server_pack(pill);
1830 CERROR("Can't pack response, rc %d\n", rc);
1831 RETURN(err_serious(rc));
1834 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1835 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1837 repbody->eadatasize = 0;
1838 repbody->aclsize = 0;
1841 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1843 /* for replay no cookkie / lmm need, because client have this already */
1844 if (info->mti_spec.no_create)
1845 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1846 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1848 rc = mdt_init_ucred_reint(info);
1850 GOTO(out_shrink, rc);
1852 rc = mdt_fix_attr_ucred(info, op);
1854 GOTO(out_ucred, rc = err_serious(rc));
1856 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1857 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1858 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1859 GOTO(out_ucred, rc);
1861 rc = mdt_reint_rec(info, lhc);
1864 mdt_exit_ucred(info);
1866 mdt_client_compatibility(info);
1867 rc2 = mdt_fix_reply(info);
1873 static long mdt_reint_opcode(struct ptlrpc_request *req,
1874 const struct req_format **fmt)
1876 struct mdt_device *mdt;
1877 struct mdt_rec_reint *rec;
1880 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1882 opc = rec->rr_opcode;
1883 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1884 if (opc < REINT_MAX && fmt[opc] != NULL)
1885 req_capsule_extend(&req->rq_pill, fmt[opc]);
1887 mdt = mdt_exp2dev(req->rq_export);
1888 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1889 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1890 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1891 opc = err_serious(-EFAULT);
1894 opc = err_serious(-EFAULT);
1899 int mdt_reint(struct tgt_session_info *tsi)
1903 static const struct req_format *reint_fmts[REINT_MAX] = {
1904 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1905 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1906 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1907 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1908 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1909 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1910 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1911 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1912 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1917 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1919 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1921 * No lock possible here from client to pass it to reint code
1924 rc = mdt_reint_internal(info, NULL, opc);
1925 mdt_thread_info_fini(info);
1930 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1934 /* this should sync the whole device */
1935 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1937 struct dt_device *dt = mdt->mdt_bottom;
1941 rc = dt->dd_ops->dt_sync(env, dt);
1945 /* this should sync this object */
1946 static int mdt_object_sync(struct mdt_thread_info *info)
1948 struct md_object *next;
1952 if (!mdt_object_exists(info->mti_object)) {
1953 CWARN("Non existing object "DFID"!\n",
1954 PFID(mdt_object_fid(info->mti_object)));
1957 next = mdt_object_child(info->mti_object);
1958 rc = mo_object_sync(info->mti_env, next);
1963 int mdt_sync(struct tgt_session_info *tsi)
1965 struct ptlrpc_request *req = tgt_ses_req(tsi);
1966 struct req_capsule *pill = tsi->tsi_pill;
1967 struct mdt_body *body;
1972 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1973 RETURN(err_serious(-ENOMEM));
1975 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1976 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1978 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1980 /* sync an object */
1981 rc = mdt_object_sync(info);
1983 const struct lu_fid *fid;
1984 struct lu_attr *la = &info->mti_attr.ma_attr;
1986 info->mti_attr.ma_need = MA_INODE;
1987 info->mti_attr.ma_valid = 0;
1988 rc = mdt_attr_get_complex(info, info->mti_object,
1991 body = req_capsule_server_get(pill,
1993 fid = mdt_object_fid(info->mti_object);
1994 mdt_pack_attr2body(info, body, la, fid);
1997 mdt_thread_info_fini(info);
2000 mdt_counter_incr(req, LPROC_MDT_SYNC);
2006 * Handle quota control requests to consult current usage/limit, but also
2007 * to configure quota enforcement
2009 int mdt_quotactl(struct tgt_session_info *tsi)
2011 struct obd_export *exp = tsi->tsi_exp;
2012 struct req_capsule *pill = tsi->tsi_pill;
2013 struct obd_quotactl *oqctl, *repoqc;
2015 struct mdt_device *mdt = mdt_exp2dev(exp);
2016 struct lu_device *qmt = mdt->mdt_qmt_dev;
2019 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2021 RETURN(err_serious(-EPROTO));
2023 rc = req_capsule_server_pack(pill);
2025 RETURN(err_serious(rc));
2027 switch (oqctl->qc_cmd) {
2029 case LUSTRE_Q_INVALIDATE:
2030 case LUSTRE_Q_FINVALIDATE:
2034 /* deprecated, not used any more */
2035 RETURN(-EOPNOTSUPP);
2036 /* master quotactl */
2042 RETURN(-EOPNOTSUPP);
2043 /* slave quotactl */
2048 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2052 /* map uid/gid for remote client */
2054 if (exp_connect_rmtclient(exp)) {
2055 struct lustre_idmap_table *idmap;
2057 idmap = exp->exp_mdt_data.med_idmap;
2059 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
2060 oqctl->qc_cmd != Q_GETINFO))
2063 if (oqctl->qc_type == USRQUOTA)
2064 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
2066 else if (oqctl->qc_type == GRPQUOTA)
2067 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
2072 if (id == CFS_IDMAP_NOTFOUND) {
2073 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
2078 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2080 RETURN(err_serious(-EFAULT));
2082 if (oqctl->qc_id != id)
2083 swap(oqctl->qc_id, id);
2085 switch (oqctl->qc_cmd) {
2091 /* forward quotactl request to QMT */
2092 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2097 /* slave quotactl */
2098 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2103 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2107 if (oqctl->qc_id != id)
2108 swap(oqctl->qc_id, id);
2114 /** clone llog ctxt from child (mdd)
2115 * This allows remote llog (replicator) access.
2116 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2117 * context was originally set up, or we can handle them directly.
2118 * I choose the latter, but that means I need any llog
2119 * contexts set up by child to be accessable by the mdt. So we clone the
2120 * context into our context list here.
2122 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2125 struct md_device *next = mdt->mdt_child;
2126 struct llog_ctxt *ctxt;
2129 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2132 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2133 if (rc || ctxt == NULL) {
2137 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2139 CERROR("Can't set mdt ctxt %d\n", rc);
2144 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2145 struct mdt_device *mdt, int idx)
2147 struct llog_ctxt *ctxt;
2149 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2152 /* Put once for the get we just did, and once for the clone */
2153 llog_ctxt_put(ctxt);
2154 llog_ctxt_put(ctxt);
2159 * sec context handlers
2161 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2165 rc = mdt_handle_idmap(tsi);
2167 struct ptlrpc_request *req = tgt_ses_req(tsi);
2170 opc = lustre_msg_get_opc(req->rq_reqmsg);
2171 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2172 sptlrpc_svc_ctx_invalidate(req);
2175 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2181 * quota request handlers
2183 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2185 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2186 struct lu_device *qmt = mdt->mdt_qmt_dev;
2191 RETURN(err_serious(-EOPNOTSUPP));
2193 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2197 struct mdt_object *mdt_object_new(const struct lu_env *env,
2198 struct mdt_device *d,
2199 const struct lu_fid *f)
2201 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2202 struct lu_object *o;
2203 struct mdt_object *m;
2206 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2207 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2208 if (unlikely(IS_ERR(o)))
2209 m = (struct mdt_object *)o;
2215 struct mdt_object *mdt_object_find(const struct lu_env *env,
2216 struct mdt_device *d,
2217 const struct lu_fid *f)
2219 struct lu_object *o;
2220 struct mdt_object *m;
2223 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2224 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2225 if (unlikely(IS_ERR(o)))
2226 m = (struct mdt_object *)o;
2234 * Asyncronous commit for mdt device.
2236 * Pass asynchonous commit call down the MDS stack.
2238 * \param env environment
2239 * \param mdt the mdt device
2241 static void mdt_device_commit_async(const struct lu_env *env,
2242 struct mdt_device *mdt)
2244 struct dt_device *dt = mdt->mdt_bottom;
2247 rc = dt->dd_ops->dt_commit_async(env, dt);
2248 if (unlikely(rc != 0))
2249 CWARN("async commit start failed with rc = %d", rc);
2253 * Mark the lock as "synchonous".
2255 * Mark the lock to deffer transaction commit to the unlock time.
2257 * \param lock the lock to mark as "synchonous"
2259 * \see mdt_is_lock_sync
2260 * \see mdt_save_lock
2262 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2264 lock->l_ast_data = (void*)1;
2268 * Check whehter the lock "synchonous" or not.
2270 * \param lock the lock to check
2271 * \retval 1 the lock is "synchonous"
2272 * \retval 0 the lock isn't "synchronous"
2274 * \see mdt_set_lock_sync
2275 * \see mdt_save_lock
2277 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2279 return lock->l_ast_data != NULL;
2283 * Blocking AST for mdt locks.
2285 * Starts transaction commit if in case of COS lock conflict or
2286 * deffers such a commit to the mdt_save_lock.
2288 * \param lock the lock which blocks a request or cancelling lock
2289 * \param desc unused
2290 * \param data unused
2291 * \param flag indicates whether this cancelling or blocking callback
2293 * \see ldlm_blocking_ast_nocheck
2295 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2296 void *data, int flag)
2298 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2299 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2303 if (flag == LDLM_CB_CANCELING)
2305 lock_res_and_lock(lock);
2306 if (lock->l_blocking_ast != mdt_blocking_ast) {
2307 unlock_res_and_lock(lock);
2310 if (mdt_cos_is_enabled(mdt) &&
2311 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2312 lock->l_blocking_lock != NULL &&
2313 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2314 mdt_set_lock_sync(lock);
2316 rc = ldlm_blocking_ast_nocheck(lock);
2318 /* There is no lock conflict if l_blocking_lock == NULL,
2319 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2320 * when the last reference to a local lock was released */
2321 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2324 rc = lu_env_init(&env, LCT_LOCAL);
2325 if (unlikely(rc != 0))
2326 CWARN("lu_env initialization failed with rc = %d,"
2327 "cannot start asynchronous commit\n", rc);
2329 mdt_device_commit_async(&env, mdt);
2335 /* Used for cross-MDT lock */
2336 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2337 void *data, int flag)
2339 struct lustre_handle lockh;
2343 case LDLM_CB_BLOCKING:
2344 ldlm_lock2handle(lock, &lockh);
2345 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2347 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2351 case LDLM_CB_CANCELING:
2352 LDLM_DEBUG(lock, "Revoke remote lock\n");
2360 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2361 struct mdt_object *o, const struct lu_fid *fid,
2362 struct lustre_handle *lh, ldlm_mode_t mode,
2365 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2366 ldlm_policy_data_t *policy = &mti->mti_policy;
2367 struct ldlm_res_id *res_id = &mti->mti_res_id;
2371 LASSERT(mdt_object_remote(o));
2373 LASSERT(ibits == MDS_INODELOCK_UPDATE);
2375 fid_build_reg_res_name(fid, res_id);
2377 memset(einfo, 0, sizeof(*einfo));
2378 einfo->ei_type = LDLM_IBITS;
2379 einfo->ei_mode = mode;
2380 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2381 einfo->ei_cb_cp = ldlm_completion_ast;
2382 einfo->ei_enq_slave = 0;
2383 einfo->ei_res_id = res_id;
2385 memset(policy, 0, sizeof(*policy));
2386 policy->l_inodebits.bits = ibits;
2388 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2393 static int mdt_object_local_lock(struct mdt_thread_info *info,
2394 struct mdt_object *o,
2395 struct mdt_lock_handle *lh, __u64 ibits,
2396 bool nonblock, int locality)
2398 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2399 ldlm_policy_data_t *policy = &info->mti_policy;
2400 struct ldlm_res_id *res_id = &info->mti_res_id;
2405 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2406 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2407 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2408 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2410 /* Only enqueue LOOKUP lock for remote object */
2411 if (mdt_object_remote(o))
2412 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2414 if (lh->mlh_type == MDT_PDO_LOCK) {
2415 /* check for exists after object is locked */
2416 if (mdt_object_exists(o) == 0) {
2417 /* Non-existent object shouldn't have PDO lock */
2420 /* Non-dir object shouldn't have PDO lock */
2421 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2426 memset(policy, 0, sizeof(*policy));
2427 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2429 dlmflags = LDLM_FL_ATOMIC_CB;
2431 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2434 * Take PDO lock on whole directory and build correct @res_id for lock
2435 * on part of directory.
2437 if (lh->mlh_pdo_hash != 0) {
2438 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2439 mdt_lock_pdo_mode(info, o, lh);
2440 if (lh->mlh_pdo_mode != LCK_NL) {
2442 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2443 * is never going to be sent to client and we do not
2444 * want it slowed down due to possible cancels.
2446 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2447 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2448 policy, res_id, dlmflags,
2449 info->mti_exp == NULL ? NULL :
2450 &info->mti_exp->exp_handle.h_cookie);
2456 * Finish res_id initializing by name hash marking part of
2457 * directory which is taking modification.
2459 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2462 policy->l_inodebits.bits = ibits;
2465 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2466 * going to be sent to client. If it is - mdt_intent_policy() path will
2467 * fix it up and turn FL_LOCAL flag off.
2469 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2470 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2471 info->mti_exp == NULL ? NULL :
2472 &info->mti_exp->exp_handle.h_cookie);
2474 mdt_object_unlock(info, o, lh, 1);
2475 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2476 lh->mlh_pdo_hash != 0 &&
2477 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2478 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2484 int mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2485 struct mdt_lock_handle *lh, __u64 ibits,
2486 bool nonblock, int locality)
2491 if (!mdt_object_remote(o))
2492 return mdt_object_local_lock(info, o, lh, ibits, nonblock,
2495 if (locality == MDT_LOCAL_LOCK) {
2496 CERROR("%s: try to get local lock for remote object"
2497 DFID".\n", mdt_obd_name(info->mti_mdt),
2498 PFID(mdt_object_fid(o)));
2502 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2503 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2504 MDS_INODELOCK_XATTR);
2505 if (ibits & MDS_INODELOCK_UPDATE) {
2506 /* Sigh, PDO needs to enqueue 2 locks right now, but
2507 * enqueue RPC can only request 1 lock, to avoid extra
2508 * RPC, so it will instead enqueue EX lock for remote
2509 * object anyway XXX*/
2510 if (lh->mlh_type == MDT_PDO_LOCK &&
2511 lh->mlh_pdo_hash != 0) {
2512 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2513 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2514 PFID(mdt_object_fid(o)));
2515 lh->mlh_pdo_hash = 0;
2516 lh->mlh_rreg_mode = LCK_EX;
2517 lh->mlh_type = MDT_REG_LOCK;
2519 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2522 MDS_INODELOCK_UPDATE);
2527 /* Only enqueue LOOKUP lock for remote object */
2528 if (ibits & MDS_INODELOCK_LOOKUP) {
2529 rc = mdt_object_local_lock(info, o, lh,
2530 MDS_INODELOCK_LOOKUP,
2531 nonblock, locality);
2539 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2540 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2542 return mdt_object_lock_internal(info, o, lh, ibits, false, locality);
2545 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2546 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2548 struct mdt_lock_handle tmp = *lh;
2551 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, locality);
2559 * Save a lock within request object.
2561 * Keep the lock referenced until whether client ACK or transaction
2562 * commit happens or release the lock immediately depending on input
2563 * parameters. If COS is ON, a write lock is converted to COS lock
2566 * \param info thead info object
2567 * \param h lock handle
2568 * \param mode lock mode
2569 * \param decref force immediate lock releasing
2572 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2573 ldlm_mode_t mode, int decref)
2577 if (lustre_handle_is_used(h)) {
2578 if (decref || !info->mti_has_trans ||
2579 !(mode & (LCK_PW | LCK_EX))){
2580 mdt_fid_unlock(h, mode);
2582 struct mdt_device *mdt = info->mti_mdt;
2583 struct ldlm_lock *lock = ldlm_handle2lock(h);
2584 struct ptlrpc_request *req = mdt_info_req(info);
2587 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2589 /* there is no request if mdt_object_unlock() is called
2590 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2591 if (likely(req != NULL)) {
2592 CDEBUG(D_HA, "request = %p reply state = %p"
2593 " transno = "LPD64"\n", req,
2594 req->rq_reply_state, req->rq_transno);
2595 if (mdt_cos_is_enabled(mdt)) {
2597 ldlm_lock_downgrade(lock, LCK_COS);
2600 ptlrpc_save_lock(req, h, mode, no_ack);
2602 ldlm_lock_decref(h, mode);
2604 if (mdt_is_lock_sync(lock)) {
2605 CDEBUG(D_HA, "found sync-lock,"
2606 " async commit started\n");
2607 mdt_device_commit_async(info->mti_env,
2610 LDLM_LOCK_PUT(lock);
2619 * Unlock mdt object.
2621 * Immeditely release the regular lock and the PDO lock or save the
2622 * lock in reqeuest and keep them referenced until client ACK or
2623 * transaction commit.
2625 * \param info thread info object
2626 * \param o mdt object
2627 * \param lh mdt lock handle referencing regular and PDO locks
2628 * \param decref force immediate lock releasing
2630 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2631 struct mdt_lock_handle *lh, int decref)
2635 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2636 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2638 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2639 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2644 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2645 const struct lu_fid *f,
2646 struct mdt_lock_handle *lh,
2649 struct mdt_object *o;
2651 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2655 rc = mdt_object_lock(info, o, lh, ibits,
2658 mdt_object_put(info->mti_env, o);
2665 void mdt_object_unlock_put(struct mdt_thread_info * info,
2666 struct mdt_object * o,
2667 struct mdt_lock_handle *lh,
2670 mdt_object_unlock(info, o, lh, decref);
2671 mdt_object_put(info->mti_env, o);
2675 * Generic code handling requests that have struct mdt_body passed in:
2677 * - extract mdt_body from request and save it in @info, if present;
2679 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2682 * - if HABEO_CORPUS flag is set for this request type check whether object
2683 * actually exists on storage (lu_object_exists()).
2686 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2688 const struct mdt_body *body;
2689 struct mdt_object *obj;
2690 const struct lu_env *env;
2691 struct req_capsule *pill;
2695 env = info->mti_env;
2696 pill = info->mti_pill;
2698 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2702 if (!(body->valid & OBD_MD_FLID))
2705 if (!fid_is_sane(&body->fid1)) {
2706 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2711 * Do not get size or any capa fields before we check that request
2712 * contains capa actually. There are some requests which do not, for
2713 * instance MDS_IS_SUBDIR.
2715 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2716 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2717 mdt_set_capainfo(info, 0, &body->fid1,
2718 req_capsule_client_get(pill, &RMF_CAPA1));
2720 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2722 if ((flags & HABEO_CORPUS) &&
2723 !mdt_object_exists(obj)) {
2724 mdt_object_put(env, obj);
2725 /* for capability renew ENOENT will be handled in
2727 if (body->valid & OBD_MD_FLOSSCAPA)
2732 info->mti_object = obj;
2741 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2743 struct req_capsule *pill = info->mti_pill;
2747 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2748 rc = mdt_body_unpack(info, flags);
2752 if (rc == 0 && (flags & HABEO_REFERO)) {
2754 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2755 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2757 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2758 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2761 rc = req_capsule_server_pack(pill);
2766 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2768 struct md_device *next = m->mdt_child;
2770 return next->md_ops->mdo_init_capa_ctxt(env, next,
2771 m->mdt_lut.lut_mds_capa,
2772 m->mdt_capa_timeout,
2777 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2779 lh->mlh_type = MDT_NUL_LOCK;
2780 lh->mlh_reg_lh.cookie = 0ull;
2781 lh->mlh_reg_mode = LCK_MINMODE;
2782 lh->mlh_pdo_lh.cookie = 0ull;
2783 lh->mlh_pdo_mode = LCK_MINMODE;
2784 lh->mlh_rreg_lh.cookie = 0ull;
2785 lh->mlh_rreg_mode = LCK_MINMODE;
2788 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2790 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2791 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2795 * Initialize fields of struct mdt_thread_info. Other fields are left in
2796 * uninitialized state, because it's too expensive to zero out whole
2797 * mdt_thread_info (> 1K) on each request arrival.
2799 void mdt_thread_info_init(struct ptlrpc_request *req,
2800 struct mdt_thread_info *info)
2804 info->mti_pill = &req->rq_pill;
2807 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2808 mdt_lock_handle_init(&info->mti_lh[i]);
2810 /* mdt device: it can be NULL while CONNECT */
2811 if (req->rq_export) {
2812 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2813 info->mti_exp = req->rq_export;
2815 info->mti_mdt = NULL;
2816 info->mti_env = req->rq_svc_thread->t_env;
2817 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2819 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2820 info->mti_big_buf = LU_BUF_NULL;
2821 info->mti_body = NULL;
2822 info->mti_object = NULL;
2823 info->mti_dlm_req = NULL;
2824 info->mti_has_trans = 0;
2825 info->mti_cross_ref = 0;
2826 info->mti_opdata = 0;
2827 info->mti_big_lmm_used = 0;
2829 info->mti_spec.no_create = 0;
2830 info->mti_spec.sp_rm_entry = 0;
2832 info->mti_spec.u.sp_ea.eadata = NULL;
2833 info->mti_spec.u.sp_ea.eadatalen = 0;
2836 void mdt_thread_info_fini(struct mdt_thread_info *info)
2840 if (info->mti_object != NULL) {
2841 mdt_object_put(info->mti_env, info->mti_object);
2842 info->mti_object = NULL;
2845 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2846 mdt_lock_handle_fini(&info->mti_lh[i]);
2847 info->mti_env = NULL;
2848 info->mti_pill = NULL;
2849 info->mti_exp = NULL;
2851 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2852 lu_buf_free(&info->mti_big_buf);
2855 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2857 struct mdt_thread_info *mti;
2858 struct lustre_capa *lc;
2860 mti = mdt_th_info(tsi->tsi_env);
2861 LASSERT(mti != NULL);
2863 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2864 if (tsi->tsi_corpus != NULL) {
2865 struct req_capsule *pill = tsi->tsi_pill;
2867 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2868 lu_object_get(tsi->tsi_corpus);
2871 * XXX: must be part of tgt_mdt_body_unpack but moved here
2872 * due to mdt_set_capainfo().
2874 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2875 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2876 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2877 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2880 mti->mti_body = tsi->tsi_mdt_body;
2881 mti->mti_dlm_req = tsi->tsi_dlm_req;
2886 int mdt_tgt_connect(struct tgt_session_info *tsi)
2888 struct ptlrpc_request *req = tgt_ses_req(tsi);
2893 rc = tgt_connect(tsi);
2897 rc = mdt_init_idmap(tsi);
2902 obd_disconnect(class_export_get(req->rq_export));
2921 static int mdt_intent_getattr(enum mdt_it_code opcode,
2922 struct mdt_thread_info *info,
2923 struct ldlm_lock **,
2926 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2927 struct mdt_thread_info *info,
2928 struct ldlm_lock **lockp,
2931 static int mdt_intent_layout(enum mdt_it_code opcode,
2932 struct mdt_thread_info *info,
2933 struct ldlm_lock **,
2935 static int mdt_intent_reint(enum mdt_it_code opcode,
2936 struct mdt_thread_info *info,
2937 struct ldlm_lock **,
2940 static struct mdt_it_flavor {
2941 const struct req_format *it_fmt;
2943 int (*it_act)(enum mdt_it_code ,
2944 struct mdt_thread_info *,
2945 struct ldlm_lock **,
2948 } mdt_it_flavor[] = {
2950 .it_fmt = &RQF_LDLM_INTENT,
2951 /*.it_flags = HABEO_REFERO,*/
2953 .it_act = mdt_intent_reint,
2954 .it_reint = REINT_OPEN
2957 .it_fmt = &RQF_LDLM_INTENT,
2959 * OCREAT is not a MUTABOR request as if the file
2961 * We do the extra check of OBD_CONNECT_RDONLY in
2962 * mdt_reint_open() when we really need to create
2966 .it_act = mdt_intent_reint,
2967 .it_reint = REINT_OPEN
2970 .it_fmt = &RQF_LDLM_INTENT,
2971 .it_flags = MUTABOR,
2972 .it_act = mdt_intent_reint,
2973 .it_reint = REINT_CREATE
2975 [MDT_IT_GETATTR] = {
2976 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2977 .it_flags = HABEO_REFERO,
2978 .it_act = mdt_intent_getattr
2980 [MDT_IT_READDIR] = {
2986 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2987 .it_flags = HABEO_REFERO,
2988 .it_act = mdt_intent_getattr
2991 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2992 .it_flags = MUTABOR,
2994 .it_reint = REINT_UNLINK
2998 .it_flags = MUTABOR,
3001 [MDT_IT_GETXATTR] = {
3002 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3003 .it_flags = HABEO_CORPUS,
3004 .it_act = mdt_intent_getxattr
3007 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3009 .it_act = mdt_intent_layout
3013 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3014 struct ldlm_lock **lockp,
3015 struct ldlm_lock *new_lock,
3016 struct mdt_lock_handle *lh,
3019 struct ptlrpc_request *req = mdt_info_req(info);
3020 struct ldlm_lock *lock = *lockp;
3023 * Get new lock only for cases when possible resent did not find any
3026 if (new_lock == NULL)
3027 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3029 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3030 lh->mlh_reg_lh.cookie = 0;
3034 LASSERTF(new_lock != NULL,
3035 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
3038 * If we've already given this lock to a client once, then we should
3039 * have no readers or writers. Otherwise, we should have one reader
3040 * _or_ writer ref (which will be zeroed below) before returning the
3043 if (new_lock->l_export == req->rq_export) {
3044 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3046 LASSERT(new_lock->l_export == NULL);
3047 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3052 if (new_lock->l_export == req->rq_export) {
3054 * Already gave this to the client, which means that we
3055 * reconstructed a reply.
3057 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3059 lh->mlh_reg_lh.cookie = 0;
3060 RETURN(ELDLM_LOCK_REPLACED);
3064 * Fixup the lock to be given to the client.
3066 lock_res_and_lock(new_lock);
3067 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3068 * possible blocking AST. */
3069 while (new_lock->l_readers > 0) {
3070 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3071 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3072 new_lock->l_readers--;
3074 while (new_lock->l_writers > 0) {
3075 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3076 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3077 new_lock->l_writers--;
3080 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3081 new_lock->l_blocking_ast = lock->l_blocking_ast;
3082 new_lock->l_completion_ast = lock->l_completion_ast;
3083 new_lock->l_remote_handle = lock->l_remote_handle;
3084 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3086 unlock_res_and_lock(new_lock);
3088 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3089 &new_lock->l_remote_handle,
3090 &new_lock->l_exp_hash);
3092 LDLM_LOCK_RELEASE(new_lock);
3093 lh->mlh_reg_lh.cookie = 0;
3095 RETURN(ELDLM_LOCK_REPLACED);
3098 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3099 struct ldlm_lock *new_lock,
3100 struct ldlm_lock **old_lock,
3101 struct mdt_lock_handle *lh,
3102 enum mdt_it_code opcode)
3104 struct ptlrpc_request *req = mdt_info_req(info);
3105 struct obd_export *exp = req->rq_export;
3106 struct lustre_handle remote_hdl;
3107 struct ldlm_request *dlmreq;
3108 struct ldlm_lock *lock;
3110 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3113 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3114 remote_hdl = dlmreq->lock_handle[0];
3115 /* If the client does not require open lock, it does not need to
3116 * search lock in exp_lock_hash, since the server thread will
3117 * make sure the lock will be released, and the resend request
3118 * can always re-enqueue the lock */
3119 if ((opcode != MDT_IT_OPEN) || (opcode == MDT_IT_OPEN &&
3120 info->mti_spec.sp_cr_flags & MDS_OPEN_LOCK)) {
3121 /* In the function below, .hs_keycmp resolves to
3122 * ldlm_export_lock_keycmp() */
3123 /* coverity[overrun-buffer-val] */
3124 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
3126 lock_res_and_lock(lock);
3127 if (lock != new_lock) {
3128 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
3129 lh->mlh_reg_mode = lock->l_granted_mode;
3131 LDLM_DEBUG(lock, "Restoring lock cookie");
3132 DEBUG_REQ(D_DLMTRACE, req,
3133 "restoring lock cookie "LPX64,
3134 lh->mlh_reg_lh.cookie);
3136 *old_lock = LDLM_LOCK_GET(lock);
3137 cfs_hash_put(exp->exp_lock_hash,
3139 unlock_res_and_lock(lock);
3142 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
3143 unlock_res_and_lock(lock);
3147 * If the xid matches, then we know this is a resent request, and allow
3148 * it. (It's probably an OPEN, for which we don't send a lock.
3150 if (req_xid_is_last(req))
3154 * This remote handle isn't enqueued, so we never received or processed
3155 * this request. Clear MSG_RESENT, because it can be handled like any
3156 * normal request now.
3158 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3160 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3164 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3165 struct mdt_thread_info *info,
3166 struct ldlm_lock **lockp,
3169 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3170 struct ldlm_reply *ldlm_rep = NULL;
3174 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3175 * (for the resend case) or a new lock. Below we will use it to
3176 * replace the original lock.
3178 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3179 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3180 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3181 rc = mdt_object_lock(info, info->mti_object, lhc,
3182 MDS_INODELOCK_XATTR,
3188 grc = mdt_getxattr(info);
3190 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3192 if (mdt_info_req(info)->rq_repmsg != NULL)
3193 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3194 if (ldlm_rep == NULL)
3195 RETURN(err_serious(-EFAULT));
3197 ldlm_rep->lock_policy_res2 = grc;
3202 static int mdt_intent_getattr(enum mdt_it_code opcode,
3203 struct mdt_thread_info *info,
3204 struct ldlm_lock **lockp,
3207 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3208 struct ldlm_lock *new_lock = NULL;
3210 struct ldlm_reply *ldlm_rep;
3211 struct ptlrpc_request *req;
3212 struct mdt_body *reqbody;
3213 struct mdt_body *repbody;
3217 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3220 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3223 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3224 repbody->eadatasize = 0;
3225 repbody->aclsize = 0;
3229 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3231 case MDT_IT_GETATTR:
3232 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3236 CERROR("Unsupported intent (%d)\n", opcode);
3237 GOTO(out_shrink, rc = -EINVAL);
3240 rc = mdt_init_ucred(info, reqbody);
3242 GOTO(out_shrink, rc);
3244 req = info->mti_pill->rc_req;
3245 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3246 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3248 /* Get lock from request for possible resent case. */
3249 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc, opcode);
3251 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3252 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3254 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3255 ldlm_rep->lock_policy_res2 = 0;
3256 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3257 ldlm_rep->lock_policy_res2) {
3258 lhc->mlh_reg_lh.cookie = 0ull;
3259 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3262 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3265 mdt_exit_ucred(info);
3267 mdt_client_compatibility(info);
3268 rc2 = mdt_fix_reply(info);
3274 static int mdt_intent_layout(enum mdt_it_code opcode,
3275 struct mdt_thread_info *info,
3276 struct ldlm_lock **lockp,
3279 struct layout_intent *layout;
3281 struct mdt_object *obj = NULL;
3282 struct md_object *child = NULL;
3286 if (opcode != MDT_IT_LAYOUT) {
3287 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3292 fid = &info->mti_tmp_fid2;
3293 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3295 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3297 RETURN(PTR_ERR(obj));
3299 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3300 child = mdt_object_child(obj);
3302 /* get the length of lsm */
3303 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3306 if (rc > info->mti_mdt->mdt_max_mdsize)
3307 info->mti_mdt->mdt_max_mdsize = rc;
3310 mdt_object_put(info->mti_env, obj);
3312 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3313 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3314 ldlm_lvbo_size(*lockp));
3315 rc = req_capsule_server_pack(info->mti_pill);
3319 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3320 LASSERT(layout != NULL);
3321 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3322 /* return to normal ldlm handling */
3325 CERROR("%s: Unsupported layout intent (%d)\n",
3326 mdt_obd_name(info->mti_mdt), layout->li_opc);
3330 static int mdt_intent_reint(enum mdt_it_code opcode,
3331 struct mdt_thread_info *info,
3332 struct ldlm_lock **lockp,
3335 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3336 struct ldlm_reply *rep = NULL;
3340 static const struct req_format *intent_fmts[REINT_MAX] = {
3341 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3342 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3347 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3351 if (mdt_it_flavor[opcode].it_reint != opc) {
3352 CERROR("Reint code %ld doesn't match intent: %d\n",
3354 RETURN(err_serious(-EPROTO));
3357 /* Get lock from request for possible resent case. */
3358 mdt_intent_fixup_resent(info, *lockp, NULL, lhc, opcode);
3360 rc = mdt_reint_internal(info, lhc, opc);
3362 /* Check whether the reply has been packed successfully. */
3363 if (mdt_info_req(info)->rq_repmsg != NULL)
3364 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3366 RETURN(err_serious(-EFAULT));
3368 /* MDC expects this in any case */
3370 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3372 /* the open lock or the lock for cross-ref object should be
3373 * returned to the client */
3374 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3375 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3376 rep->lock_policy_res2 = 0;
3377 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3381 rep->lock_policy_res2 = clear_serious(rc);
3383 if (rep->lock_policy_res2 == -ENOENT &&
3384 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3385 rep->lock_policy_res2 = 0;
3387 if (rc == -ENOTCONN || rc == -ENODEV ||
3388 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3390 * If it is the disconnect error (ENODEV & ENOCONN), the error
3391 * will be returned by rq_status, and client at ptlrpc layer
3392 * will detect this, then disconnect, reconnect the import
3393 * immediately, instead of impacting the following the rpc.
3395 lhc->mlh_reg_lh.cookie = 0ull;
3399 * For other cases, the error will be returned by intent.
3400 * and client will retrieve the result from intent.
3403 * FIXME: when open lock is finished, that should be
3406 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3407 LASSERTF(rc == 0, "Error occurred but lock handle "
3408 "is still in use, rc = %d\n", rc);
3409 rep->lock_policy_res2 = 0;
3410 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3413 lhc->mlh_reg_lh.cookie = 0ull;
3414 RETURN(ELDLM_LOCK_ABORTED);
3419 static int mdt_intent_code(long itcode)
3427 case IT_OPEN|IT_CREAT:
3434 rc = MDT_IT_READDIR;
3437 rc = MDT_IT_GETATTR;
3449 rc = MDT_IT_GETXATTR;
3454 case IT_QUOTA_DQACQ:
3459 CERROR("Unknown intent opcode: %ld\n", itcode);
3466 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3467 struct ldlm_lock **lockp, __u64 flags)
3469 struct req_capsule *pill;
3470 struct mdt_it_flavor *flv;
3475 opc = mdt_intent_code(itopc);
3479 pill = info->mti_pill;
3481 if (opc == MDT_IT_QUOTA) {
3482 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3485 RETURN(-EOPNOTSUPP);
3487 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3488 /* pass the request to quota master */
3489 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3490 mdt_info_req(info), lockp,
3495 flv = &mdt_it_flavor[opc];
3496 if (flv->it_fmt != NULL)
3497 req_capsule_extend(pill, flv->it_fmt);
3499 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3501 struct ptlrpc_request *req = mdt_info_req(info);
3502 if (flv->it_flags & MUTABOR &&
3503 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3506 if (rc == 0 && flv->it_act != NULL) {
3507 struct ldlm_reply *rep;
3509 /* execute policy */
3510 rc = flv->it_act(opc, info, lockp, flags);
3512 /* Check whether the reply has been packed successfully. */
3513 if (mdt_info_req(info)->rq_repmsg != NULL) {
3514 rep = req_capsule_server_get(info->mti_pill,
3516 rep->lock_policy_res2 =
3517 ptlrpc_status_hton(rep->lock_policy_res2);
3525 static int mdt_intent_policy(struct ldlm_namespace *ns,
3526 struct ldlm_lock **lockp, void *req_cookie,
3527 ldlm_mode_t mode, __u64 flags, void *data)
3529 struct tgt_session_info *tsi;
3530 struct mdt_thread_info *info;
3531 struct ptlrpc_request *req = req_cookie;
3532 struct ldlm_intent *it;
3533 struct req_capsule *pill;
3538 LASSERT(req != NULL);
3540 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3542 info = tsi2mdt_info(tsi);
3543 LASSERT(info != NULL);
3544 pill = info->mti_pill;
3545 LASSERT(pill->rc_req == req);
3547 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3548 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3549 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3551 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3555 /* Lock without inodebits makes no sense and will oops
3556 * later in ldlm. Let's check it now to see if we have
3557 * ibits corrupted somewhere in mdt_intent_opc().
3558 * The case for client miss to set ibits has been
3559 * processed by others. */
3560 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3561 lr_type == LDLM_IBITS,
3562 info->mti_dlm_req->lock_desc.\
3563 l_policy_data.l_inodebits.bits != 0));
3565 rc = err_serious(-EFAULT);
3567 /* No intent was provided */
3568 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3569 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3570 rc = req_capsule_server_pack(pill);
3572 rc = err_serious(rc);
3574 mdt_thread_info_fini(info);
3578 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3580 struct seq_server_site *ss = mdt_seq_site(mdt);
3582 if (ss->ss_node_id == 0)
3585 if (ss->ss_client_seq != NULL) {
3586 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3587 ss->ss_client_seq->lcs_exp = NULL;
3590 if (ss->ss_server_fld != NULL) {
3591 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3592 ss->ss_server_fld->lsf_control_exp = NULL;
3596 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3598 struct seq_server_site *ss = mdt_seq_site(mdt);
3603 if (ss->ss_server_seq == NULL)
3604 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3609 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3611 mdt_seq_fini_cli(mdt);
3612 mdt_deregister_seq_exp(mdt);
3614 return seq_site_fini(env, mdt_seq_site(mdt));
3618 * It will retrieve its FLDB entries from MDT0, and it only happens
3619 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3620 * and it needs to refresh FLDB from the MDT0.
3622 static int mdt_register_lwp_callback(void *data)
3625 struct mdt_device *mdt = data;
3626 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3630 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3632 if (!likely(fld->lsf_new))
3635 rc = lu_env_init(&env, LCT_MD_THREAD);
3637 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3641 rc = fld_update_from_controller(&env, fld);
3643 CERROR("%s: cannot update controller: rc = %d\n",
3644 mdt_obd_name(mdt), rc);
3652 static int mdt_register_seq_exp(struct mdt_device *mdt)
3654 struct seq_server_site *ss = mdt_seq_site(mdt);
3655 char *lwp_name = NULL;
3658 if (ss->ss_node_id == 0)
3661 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3662 if (lwp_name == NULL)
3663 GOTO(out_free, rc = -ENOMEM);
3665 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3669 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3674 rc = lustre_register_lwp_item(lwp_name,
3675 &ss->ss_server_fld->lsf_control_exp,
3676 mdt_register_lwp_callback, mdt);
3678 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3679 ss->ss_client_seq->lcs_exp = NULL;
3683 if (lwp_name != NULL)
3684 OBD_FREE(lwp_name, MAX_OBD_NAME);
3690 * Init client sequence manager which is used by local MDS to talk to sequence
3691 * controller on remote node.
3693 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3695 struct seq_server_site *ss = mdt_seq_site(mdt);
3700 /* check if this is adding the first MDC and controller is not yet
3702 OBD_ALLOC_PTR(ss->ss_client_seq);
3703 if (ss->ss_client_seq == NULL)
3706 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3707 if (prefix == NULL) {
3708 OBD_FREE_PTR(ss->ss_client_seq);
3709 ss->ss_client_seq = NULL;
3713 /* Note: seq_client_fini will be called in seq_site_fini */
3714 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3715 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3716 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3718 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3720 OBD_FREE_PTR(ss->ss_client_seq);
3721 ss->ss_client_seq = NULL;
3725 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3730 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3732 struct seq_server_site *ss;
3736 ss = mdt_seq_site(mdt);
3737 /* init sequence controller server(MDT0) */
3738 if (ss->ss_node_id == 0) {
3739 OBD_ALLOC_PTR(ss->ss_control_seq);
3740 if (ss->ss_control_seq == NULL)
3743 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3744 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3747 GOTO(out_seq_fini, rc);
3750 /* Init normal sequence server */
3751 OBD_ALLOC_PTR(ss->ss_server_seq);
3752 if (ss->ss_server_seq == NULL)
3753 GOTO(out_seq_fini, rc = -ENOMEM);
3755 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3756 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3758 GOTO(out_seq_fini, rc);
3760 /* init seq client for seq server to talk to seq controller(MDT0) */
3761 rc = mdt_seq_init_cli(env, mdt);
3763 GOTO(out_seq_fini, rc);
3765 if (ss->ss_node_id != 0)
3766 /* register controler export through lwp */
3767 rc = mdt_register_seq_exp(mdt);
3772 mdt_seq_fini(env, mdt);
3780 static int mdt_fld_fini(const struct lu_env *env,
3781 struct mdt_device *m)
3783 struct seq_server_site *ss = mdt_seq_site(m);
3786 if (ss && ss->ss_server_fld) {
3787 fld_server_fini(env, ss->ss_server_fld);
3788 OBD_FREE_PTR(ss->ss_server_fld);
3789 ss->ss_server_fld = NULL;
3795 static int mdt_fld_init(const struct lu_env *env,
3797 struct mdt_device *m)
3799 struct seq_server_site *ss;
3803 ss = mdt_seq_site(m);
3805 OBD_ALLOC_PTR(ss->ss_server_fld);
3806 if (ss->ss_server_fld == NULL)
3807 RETURN(rc = -ENOMEM);
3809 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3812 OBD_FREE_PTR(ss->ss_server_fld);
3813 ss->ss_server_fld = NULL;
3820 static void mdt_stack_pre_fini(const struct lu_env *env,
3821 struct mdt_device *m, struct lu_device *top)
3823 struct obd_device *obd;
3824 struct lustre_cfg_bufs *bufs;
3825 struct lustre_cfg *lcfg;
3826 struct mdt_thread_info *info;
3831 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3832 LASSERT(info != NULL);
3834 bufs = &info->mti_u.bufs;
3836 LASSERT(m->mdt_child_exp);
3837 LASSERT(m->mdt_child_exp->exp_obd);
3838 obd = m->mdt_child_exp->exp_obd;
3840 /* process cleanup, pass mdt obd name to get obd umount flags */
3841 /* XXX: this is needed because all layers are referenced by
3842 * objects (some of them are pinned by osd, for example *
3843 * the proper solution should be a model where object used
3844 * by osd only doesn't have mdt/mdd slices -bzzz */
3845 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3846 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3847 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3849 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3852 top->ld_ops->ldo_process_config(env, top, lcfg);
3853 lustre_cfg_free(lcfg);
3857 static void mdt_stack_fini(const struct lu_env *env,
3858 struct mdt_device *m, struct lu_device *top)
3860 struct obd_device *obd = mdt2obd_dev(m);
3861 struct lustre_cfg_bufs *bufs;
3862 struct lustre_cfg *lcfg;
3863 struct mdt_thread_info *info;
3867 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3868 LASSERT(info != NULL);
3870 lu_dev_del_linkage(top->ld_site, top);
3872 lu_site_purge(env, top->ld_site, -1);
3874 bufs = &info->mti_u.bufs;
3875 /* process cleanup, pass mdt obd name to get obd umount flags */
3876 /* another purpose is to let all layers to release their objects */
3877 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3882 lustre_cfg_bufs_set_string(bufs, 1, flags);
3883 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3885 CERROR("Cannot alloc lcfg!\n");
3889 top->ld_ops->ldo_process_config(env, top, lcfg);
3890 lustre_cfg_free(lcfg);
3892 lu_site_purge(env, top->ld_site, -1);
3894 m->mdt_child = NULL;
3895 m->mdt_bottom = NULL;
3897 obd_disconnect(m->mdt_child_exp);
3898 m->mdt_child_exp = NULL;
3900 obd_disconnect(m->mdt_bottom_exp);
3901 m->mdt_child_exp = NULL;
3904 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3905 const char *next, struct obd_export **exp)
3907 struct obd_connect_data *data = NULL;
3908 struct obd_device *obd;
3912 OBD_ALLOC_PTR(data);
3914 GOTO(out, rc = -ENOMEM);
3916 obd = class_name2obd(next);
3918 CERROR("%s: can't locate next device: %s\n",
3919 mdt_obd_name(m), next);
3920 GOTO(out, rc = -ENOTCONN);
3923 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3924 data->ocd_version = LUSTRE_VERSION_CODE;
3926 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3928 CERROR("%s: cannot connect to next dev %s (%d)\n",
3929 mdt_obd_name(m), next, rc);
3939 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3940 struct lustre_cfg *cfg)
3942 char *dev = lustre_cfg_string(cfg, 0);
3943 int rc, name_size, uuid_size;
3944 char *name, *uuid, *p;
3945 struct lustre_cfg_bufs *bufs;
3946 struct lustre_cfg *lcfg;
3947 struct obd_device *obd;
3948 struct lustre_profile *lprof;
3949 struct lu_site *site;
3952 /* in 1.8 we had the only device in the stack - MDS.
3953 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3954 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3955 * to generate names and setup MDT, MDD. MDT will be using
3956 * generated name to connect to MDD. for MDD the next device
3957 * will be LOD with name taken from so called "profile" which
3958 * is generated by mount_option line
3960 * 1.8 MGS generates config. commands like this:
3961 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3962 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3963 * 2.0 MGS generates config. commands like this:
3964 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3965 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3966 * 3:lustre-MDT0000-mdtlov 4:f
3968 * we generate MDD name from MDT one, just replacing T with D
3970 * after all the preparations, the logical equivalent will be
3971 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3972 * 3:lustre-MDT0000-mdtlov 4:f
3973 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3974 * 3:lustre-MDD0000 4:f
3976 * notice we build the stack from down to top: MDD first, then MDT */
3978 name_size = MAX_OBD_NAME;
3979 uuid_size = MAX_OBD_NAME;
3981 OBD_ALLOC(name, name_size);
3982 OBD_ALLOC(uuid, uuid_size);
3983 if (name == NULL || uuid == NULL)
3984 GOTO(cleanup_mem, rc = -ENOMEM);
3986 OBD_ALLOC_PTR(bufs);
3988 GOTO(cleanup_mem, rc = -ENOMEM);
3991 p = strstr(name, "-MDT");
3993 GOTO(free_bufs, rc = -ENOMEM);
3996 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3998 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3999 if (lprof == NULL || lprof->lp_dt == NULL) {
4000 CERROR("can't find the profile: %s\n",
4001 lustre_cfg_string(cfg, 0));
4002 GOTO(free_bufs, rc = -EINVAL);
4005 lustre_cfg_bufs_reset(bufs, name);
4006 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4007 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4008 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4010 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4012 GOTO(free_bufs, rc = -ENOMEM);
4014 rc = class_attach(lcfg);
4016 GOTO(lcfg_cleanup, rc);
4018 obd = class_name2obd(name);
4020 CERROR("Can not find obd %s (%s in config)\n",
4021 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4022 GOTO(class_detach, rc = -EINVAL);
4025 lustre_cfg_free(lcfg);
4027 lustre_cfg_bufs_reset(bufs, name);
4028 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4029 lustre_cfg_bufs_set_string(bufs, 2, dev);
4030 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4032 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4034 rc = class_setup(obd, lcfg);
4036 GOTO(class_detach, rc);
4038 /* connect to MDD we just setup */
4039 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4041 GOTO(class_detach, rc);
4043 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4045 LASSERT(mdt_lu_site(mdt) == NULL);
4046 mdt->mdt_lu_dev.ld_site = site;
4047 site->ls_top_dev = &mdt->mdt_lu_dev;
4048 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4050 /* now connect to bottom OSD */
4051 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4052 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4054 GOTO(class_detach, rc);
4056 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4058 rc = lu_env_refill((struct lu_env *)env);
4060 CERROR("Failure to refill session: '%d'\n", rc);
4062 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4067 class_detach(obd, lcfg);
4069 lustre_cfg_free(lcfg);
4074 OBD_FREE(name, name_size);
4076 OBD_FREE(uuid, uuid_size);
4080 /* setup quota master target on MDT0 */
4081 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4082 struct lustre_cfg *cfg)
4084 struct obd_device *obd;
4085 char *dev = lustre_cfg_string(cfg, 0);
4086 char *qmtname, *uuid, *p;
4087 struct lustre_cfg_bufs *bufs;
4088 struct lustre_cfg *lcfg;
4089 struct lustre_profile *lprof;
4090 struct obd_connect_data *data;
4094 LASSERT(mdt->mdt_qmt_exp == NULL);
4095 LASSERT(mdt->mdt_qmt_dev == NULL);
4097 /* quota master is on MDT0 only for now */
4098 if (mdt->mdt_seq_site.ss_node_id != 0)
4101 /* MGS generates config commands which look as follows:
4102 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4103 * 3:lustre-MDT0000-mdtlov 4:f
4105 * We generate the QMT name from the MDT one, just replacing MD with QM
4106 * after all the preparations, the logical equivalent will be:
4107 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4108 * 3:lustre-MDT0000-osd 4:f */
4109 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4110 OBD_ALLOC(uuid, UUID_MAX);
4111 OBD_ALLOC_PTR(bufs);
4112 OBD_ALLOC_PTR(data);
4113 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4114 GOTO(cleanup_mem, rc = -ENOMEM);
4116 strcpy(qmtname, dev);
4117 p = strstr(qmtname, "-MDT");
4119 GOTO(cleanup_mem, rc = -ENOMEM);
4120 /* replace MD with QM */
4124 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4126 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4127 if (lprof == NULL || lprof->lp_dt == NULL) {
4128 CERROR("can't find profile for %s\n",
4129 lustre_cfg_string(cfg, 0));
4130 GOTO(cleanup_mem, rc = -EINVAL);
4133 lustre_cfg_bufs_reset(bufs, qmtname);
4134 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4135 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4136 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4138 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4140 GOTO(cleanup_mem, rc = -ENOMEM);
4142 rc = class_attach(lcfg);
4144 GOTO(lcfg_cleanup, rc);
4146 obd = class_name2obd(qmtname);
4148 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4149 lustre_cfg_string(cfg, 0));
4150 GOTO(class_detach, rc = -EINVAL);
4153 lustre_cfg_free(lcfg);
4155 lustre_cfg_bufs_reset(bufs, qmtname);
4156 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4157 lustre_cfg_bufs_set_string(bufs, 2, dev);
4159 /* for quota, the next device should be the OSD device */
4160 lustre_cfg_bufs_set_string(bufs, 3,
4161 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4163 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4165 rc = class_setup(obd, lcfg);
4167 GOTO(class_detach, rc);
4169 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4171 /* configure local quota objects */
4172 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4176 GOTO(class_cleanup, rc);
4178 /* connect to quota master target */
4179 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4180 data->ocd_version = LUSTRE_VERSION_CODE;
4181 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4184 CERROR("cannot connect to quota master device %s (%d)\n",
4186 GOTO(class_cleanup, rc);
4192 class_manual_cleanup(obd);
4193 mdt->mdt_qmt_dev = NULL;
4197 class_detach(obd, lcfg);
4199 lustre_cfg_free(lcfg);
4204 OBD_FREE(qmtname, MAX_OBD_NAME);
4206 OBD_FREE(uuid, UUID_MAX);
4212 /* Shutdown quota master target associated with mdt */
4213 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4217 if (mdt->mdt_qmt_exp == NULL)
4219 LASSERT(mdt->mdt_qmt_dev != NULL);
4221 /* the qmt automatically shuts down when the mdt disconnects */
4222 obd_disconnect(mdt->mdt_qmt_exp);
4223 mdt->mdt_qmt_exp = NULL;
4224 mdt->mdt_qmt_dev = NULL;
4228 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4229 * for now. This will be removed along with converting rest of MDT code
4230 * to use tgt_session_info */
4231 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4233 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4236 rc = mdt_getxattr(info);
4238 mdt_thread_info_fini(info);
4242 static struct tgt_handler mdt_tgt_handlers[] = {
4243 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4244 0, MDS_CONNECT, mdt_tgt_connect,
4245 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4246 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4247 0, MDS_DISCONNECT, tgt_disconnect,
4248 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4249 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4250 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4251 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4252 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4253 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4254 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4255 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4257 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4258 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4259 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4260 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4261 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4263 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4264 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4265 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4266 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4267 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4269 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4270 mdt_hsm_ct_register),
4271 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4272 mdt_hsm_ct_unregister),
4273 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4275 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4277 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4278 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4280 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4285 static struct tgt_handler mdt_sec_ctx_ops[] = {
4286 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4287 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4288 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4291 static struct tgt_handler mdt_quota_ops[] = {
4292 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4295 static struct tgt_opc_slice mdt_common_slice[] = {
4297 .tos_opc_start = MDS_FIRST_OPC,
4298 .tos_opc_end = MDS_LAST_OPC,
4299 .tos_hs = mdt_tgt_handlers
4302 .tos_opc_start = OBD_FIRST_OPC,
4303 .tos_opc_end = OBD_LAST_OPC,
4304 .tos_hs = tgt_obd_handlers
4307 .tos_opc_start = LDLM_FIRST_OPC,
4308 .tos_opc_end = LDLM_LAST_OPC,
4309 .tos_hs = tgt_dlm_handlers
4312 .tos_opc_start = SEC_FIRST_OPC,
4313 .tos_opc_end = SEC_LAST_OPC,
4314 .tos_hs = mdt_sec_ctx_ops
4317 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4318 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4319 .tos_hs = tgt_out_handlers
4322 .tos_opc_start = FLD_FIRST_OPC,
4323 .tos_opc_end = FLD_LAST_OPC,
4324 .tos_hs = fld_handlers
4327 .tos_opc_start = SEQ_FIRST_OPC,
4328 .tos_opc_end = SEQ_LAST_OPC,
4329 .tos_hs = seq_handlers
4332 .tos_opc_start = QUOTA_DQACQ,
4333 .tos_opc_end = QUOTA_LAST_OPC,
4334 .tos_hs = mdt_quota_ops
4337 .tos_opc_start = LLOG_FIRST_OPC,
4338 .tos_opc_end = LLOG_LAST_OPC,
4339 .tos_hs = tgt_llog_handlers
4342 .tos_opc_start = LFSCK_FIRST_OPC,
4343 .tos_opc_end = LFSCK_LAST_OPC,
4344 .tos_hs = tgt_lfsck_handlers
4352 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4354 struct md_device *next = m->mdt_child;
4355 struct lu_device *d = &m->mdt_lu_dev;
4356 struct obd_device *obd = mdt2obd_dev(m);
4357 struct lfsck_stop stop;
4360 stop.ls_status = LS_PAUSED;
4362 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4364 target_recovery_fini(obd);
4365 ping_evictor_stop();
4366 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4368 if (m->mdt_opts.mo_coordinator)
4369 mdt_hsm_cdt_stop(m);
4371 mdt_hsm_cdt_fini(m);
4373 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4374 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4375 obd_exports_barrier(obd);
4376 obd_zombie_barrier();
4380 tgt_fini(env, &m->mdt_lut);
4381 mdt_fs_cleanup(env, m);
4382 upcall_cache_cleanup(m->mdt_identity_cache);
4383 m->mdt_identity_cache = NULL;
4385 if (m->mdt_namespace != NULL) {
4386 ldlm_namespace_free(m->mdt_namespace, NULL,
4387 d->ld_obd->obd_force);
4388 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4391 mdt_quota_fini(env, m);
4393 cfs_free_nidlist(&m->mdt_nosquash_nids);
4394 if (m->mdt_nosquash_str) {
4395 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4396 m->mdt_nosquash_str = NULL;
4397 m->mdt_nosquash_strlen = 0;
4400 mdt_seq_fini(env, m);
4401 mdt_fld_fini(env, m);
4403 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4404 cfs_timer_disarm(&m->mdt_ck_timer);
4405 mdt_ck_thread_stop(m);
4410 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4412 LASSERT(atomic_read(&d->ld_ref) == 0);
4414 server_put_mount(mdt_obd_name(m));
4419 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4421 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4422 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4424 struct mdt_thread_info *info;
4425 struct obd_device *obd;
4426 const char *dev = lustre_cfg_string(cfg, 0);
4427 const char *num = lustre_cfg_string(cfg, 2);
4428 struct lustre_mount_info *lmi = NULL;
4429 struct lustre_sb_info *lsi;
4431 struct seq_server_site *ss_site;
4432 const char *identity_upcall = "NONE";
4433 struct md_device *next;
4439 lu_device_init(&m->mdt_lu_dev, ldt);
4441 * Environment (env) might be missing mdt_thread_key values at that
4442 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4445 * Usually device allocation path doesn't use module key values, but
4446 * mdt has to do a lot of work here, so allocate key value.
4448 rc = lu_env_refill((struct lu_env *)env);
4452 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4453 LASSERT(info != NULL);
4455 obd = class_name2obd(dev);
4456 LASSERT(obd != NULL);
4458 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4460 m->mdt_som_conf = 0;
4462 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4464 /* default is coordinator off, it is started through conf_param
4466 m->mdt_opts.mo_coordinator = 0;
4468 lmi = server_get_mount(dev);
4470 CERROR("Cannot get mount info for %s!\n", dev);
4473 lsi = s2lsi(lmi->lmi_sb);
4474 /* CMD is supported only in IAM mode */
4476 node_id = simple_strtol(num, NULL, 10);
4477 obd->u.obt.obt_magic = OBT_MAGIC;
4480 spin_lock_init(&m->mdt_ioepoch_lock);
4481 m->mdt_capa_timeout = CAPA_TIMEOUT;
4482 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4483 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4484 m->mdt_squash_uid = 0;
4485 m->mdt_squash_gid = 0;
4486 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4487 m->mdt_nosquash_str = NULL;
4488 m->mdt_nosquash_strlen = 0;
4489 init_rwsem(&m->mdt_squash_sem);
4490 spin_lock_init(&m->mdt_osfs_lock);
4491 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4492 m->mdt_enable_remote_dir = 0;
4493 m->mdt_enable_remote_dir_gid = 0;
4495 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4496 m->mdt_lu_dev.ld_obd = obd;
4497 /* Set this lu_device to obd for error handling purposes. */
4498 obd->obd_lu_dev = &m->mdt_lu_dev;
4500 /* init the stack */
4501 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4503 CERROR("%s: Can't init device stack, rc %d\n",
4504 mdt_obd_name(m), rc);
4509 ss_site = mdt_seq_site(m);
4510 s->ld_seq_site = ss_site;
4513 /* set server index */
4514 ss_site->ss_node_id = node_id;
4516 /* failover is the default
4517 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4518 * assumed whose ss_node_id == 0 XXX
4520 obd->obd_replayable = 1;
4521 /* No connection accepted until configurations will finish */
4522 obd->obd_no_conn = 1;
4524 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4525 char *str = lustre_cfg_string(cfg, 4);
4526 if (strchr(str, 'n')) {
4527 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4528 obd->obd_replayable = 0;
4532 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4534 GOTO(err_fini_stack, rc);
4536 rc = mdt_seq_init(env, m);
4538 GOTO(err_fini_fld, rc);
4540 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4541 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4542 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4543 LDLM_NAMESPACE_SERVER,
4544 LDLM_NAMESPACE_GREEDY,
4546 if (m->mdt_namespace == NULL)
4547 GOTO(err_fini_seq, rc = -ENOMEM);
4549 m->mdt_namespace->ns_lvbp = m;
4550 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4552 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4553 /* set obd_namespace for compatibility with old code */
4554 obd->obd_namespace = m->mdt_namespace;
4556 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4558 rc = mdt_hsm_cdt_init(m);
4560 CERROR("%s: error initializing coordinator, rc %d\n",
4561 mdt_obd_name(m), rc);
4562 GOTO(err_free_ns, rc);
4565 rc = mdt_ck_thread_start(m);
4567 GOTO(err_free_hsm, rc);
4569 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4570 OBD_FAIL_MDS_ALL_REQUEST_NET,
4571 OBD_FAIL_MDS_ALL_REPLY_NET);
4575 rc = mdt_fs_setup(env, m, obd, lsi);
4579 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4581 next = m->mdt_child;
4582 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4585 GOTO(err_fs_cleanup, rc);
4587 if (mntopts & MNTOPT_USERXATTR)
4588 m->mdt_opts.mo_user_xattr = 1;
4590 m->mdt_opts.mo_user_xattr = 0;
4592 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4594 GOTO(err_fs_cleanup, rc);
4596 if (mntopts & MNTOPT_ACL)
4597 m->mdt_opts.mo_acl = 1;
4599 m->mdt_opts.mo_acl = 0;
4601 /* XXX: to support suppgid for ACL, we enable identity_upcall
4602 * by default, otherwise, maybe got unexpected -EACCESS. */
4603 if (m->mdt_opts.mo_acl)
4604 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4606 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4608 &mdt_identity_upcall_cache_ops);
4609 if (IS_ERR(m->mdt_identity_cache)) {
4610 rc = PTR_ERR(m->mdt_identity_cache);
4611 m->mdt_identity_cache = NULL;
4612 GOTO(err_fs_cleanup, rc);
4615 rc = mdt_procfs_init(m, dev);
4617 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4618 GOTO(err_recovery, rc);
4621 rc = mdt_quota_init(env, m, cfg);
4623 GOTO(err_procfs, rc);
4625 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4626 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4627 "mdt_ldlm_client", m->mdt_ldlm_client);
4629 ping_evictor_start();
4631 /* recovery will be started upon mdt_prepare()
4632 * when the whole stack is complete and ready
4633 * to serve the requests */
4635 mdt_init_capa_ctxt(env, m);
4637 /* Reduce the initial timeout on an MDS because it doesn't need such
4638 * a long timeout as an OST does. Adaptive timeouts will adjust this
4639 * value appropriately. */
4640 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4641 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4647 target_recovery_fini(obd);
4648 upcall_cache_cleanup(m->mdt_identity_cache);
4649 m->mdt_identity_cache = NULL;
4651 mdt_fs_cleanup(env, m);
4653 tgt_fini(env, &m->mdt_lut);
4655 cfs_timer_disarm(&m->mdt_ck_timer);
4656 mdt_ck_thread_stop(m);
4658 mdt_hsm_cdt_fini(m);
4660 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4661 obd->obd_namespace = m->mdt_namespace = NULL;
4663 mdt_seq_fini(env, m);
4665 mdt_fld_fini(env, m);
4667 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4670 server_put_mount(dev);
4674 /* For interoperability, the left element is old parameter, the right one
4675 * is the new version of the parameter, if some parameter is deprecated,
4676 * the new version should be set as NULL. */
4677 static struct cfg_interop_param mdt_interop_param[] = {
4678 { "mdt.group_upcall", NULL },
4679 { "mdt.quota_type", NULL },
4680 { "mdd.quota_type", NULL },
4681 { "mdt.rootsquash", "mdt.root_squash" },
4682 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4686 /* used by MGS to process specific configurations */
4687 static int mdt_process_config(const struct lu_env *env,
4688 struct lu_device *d, struct lustre_cfg *cfg)
4690 struct mdt_device *m = mdt_dev(d);
4691 struct md_device *md_next = m->mdt_child;
4692 struct lu_device *next = md2lu_dev(md_next);
4696 switch (cfg->lcfg_command) {
4698 struct lprocfs_static_vars lvars;
4699 struct obd_device *obd = d->ld_obd;
4701 /* For interoperability */
4702 struct cfg_interop_param *ptr = NULL;
4703 struct lustre_cfg *old_cfg = NULL;
4706 param = lustre_cfg_string(cfg, 1);
4707 if (param == NULL) {
4708 CERROR("param is empty\n");
4713 ptr = class_find_old_param(param, mdt_interop_param);
4715 if (ptr->new_param == NULL) {
4717 CWARN("For interoperability, skip this %s."
4718 " It is obsolete.\n", ptr->old_param);
4722 CWARN("Found old param %s, changed it to %s.\n",
4723 ptr->old_param, ptr->new_param);
4726 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4733 lprocfs_mdt_init_vars(&lvars);
4734 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4736 if (rc > 0 || rc == -ENOSYS) {
4737 /* is it an HSM var ? */
4738 rc = class_process_proc_param(PARAM_HSM,
4739 hsm_cdt_get_proc_vars(),
4741 if (rc > 0 || rc == -ENOSYS)
4742 /* we don't understand; pass it on */
4743 rc = next->ld_ops->ldo_process_config(env, next,
4747 if (old_cfg != NULL)
4748 lustre_cfg_free(cfg);
4753 /* others are passed further */
4754 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4760 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4761 const struct lu_object_header *hdr,
4762 struct lu_device *d)
4764 struct mdt_object *mo;
4768 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4770 struct lu_object *o;
4771 struct lu_object_header *h;
4774 h = &mo->mot_header;
4775 lu_object_header_init(h);
4776 lu_object_init(o, h, d);
4777 lu_object_add_top(h, o);
4778 o->lo_ops = &mdt_obj_ops;
4779 mutex_init(&mo->mot_ioepoch_mutex);
4780 mutex_init(&mo->mot_lov_mutex);
4781 init_rwsem(&mo->mot_open_sem);
4787 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4788 const struct lu_object_conf *unused)
4790 struct mdt_device *d = mdt_dev(o->lo_dev);
4791 struct lu_device *under;
4792 struct lu_object *below;
4796 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4797 PFID(lu_object_fid(o)));
4799 under = &d->mdt_child->md_lu_dev;
4800 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4801 if (below != NULL) {
4802 lu_object_add(o, below);
4809 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4811 struct mdt_object *mo = mdt_obj(o);
4812 struct lu_object_header *h;
4816 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4817 PFID(lu_object_fid(o)));
4819 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4820 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4823 lu_object_header_fini(h);
4824 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4829 static int mdt_object_print(const struct lu_env *env, void *cookie,
4830 lu_printer_t p, const struct lu_object *o)
4832 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4833 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4834 "flags="LPX64", epochcount=%d, writecount=%d)",
4835 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4836 mdto->mot_ioepoch_count, mdto->mot_writecount);
4839 static int mdt_prepare(const struct lu_env *env,
4840 struct lu_device *pdev,
4841 struct lu_device *cdev)
4843 struct mdt_device *mdt = mdt_dev(cdev);
4844 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4845 struct obd_device *obd = cdev->ld_obd;
4846 struct lfsck_start_param lsp;
4853 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4857 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4861 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4865 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4866 /* The LFSCK instance is registered just now, so it must be there when
4867 * register the namespace to such instance. */
4868 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4870 lsp.lsp_start = NULL;
4871 lsp.lsp_index_valid = 0;
4872 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
4873 OBD_IOC_START_LFSCK,
4876 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
4877 mdt_obd_name(mdt), rc);
4881 if (mdt->mdt_seq_site.ss_node_id == 0) {
4882 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4883 &mdt->mdt_md_root_fid);
4888 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4889 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4890 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4891 LASSERT(obd->obd_no_conn);
4892 spin_lock(&obd->obd_dev_lock);
4893 obd->obd_no_conn = 0;
4894 spin_unlock(&obd->obd_dev_lock);
4896 if (obd->obd_recovering == 0)
4897 mdt_postrecov(env, mdt);
4902 const struct lu_device_operations mdt_lu_ops = {
4903 .ldo_object_alloc = mdt_object_alloc,
4904 .ldo_process_config = mdt_process_config,
4905 .ldo_prepare = mdt_prepare,
4908 static const struct lu_object_operations mdt_obj_ops = {
4909 .loo_object_init = mdt_object_init,
4910 .loo_object_free = mdt_object_free,
4911 .loo_object_print = mdt_object_print
4914 static int mdt_obd_set_info_async(const struct lu_env *env,
4915 struct obd_export *exp,
4916 __u32 keylen, void *key,
4917 __u32 vallen, void *val,
4918 struct ptlrpc_request_set *set)
4924 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4925 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4933 * Match client and server connection feature flags.
4935 * Compute the compatibility flags for a connection request based on
4936 * features mutually supported by client and server.
4938 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4939 * must not be updated here, otherwise a partially initialized value may
4940 * be exposed. After the connection request is successfully processed,
4941 * the top-level MDT connect request handler atomically updates the export
4942 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4943 * reply. \see mdt_connect().
4945 * \param exp the obd_export associated with this client/target pair
4946 * \param mdt the target device for the connection
4947 * \param data stores data for this connect request
4950 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4951 * \retval -EBADE client and server feature requirements are incompatible
4953 static int mdt_connect_internal(struct obd_export *exp,
4954 struct mdt_device *mdt,
4955 struct obd_connect_data *data)
4957 LASSERT(data != NULL);
4959 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4960 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4962 /* If no known bits (which should not happen, probably,
4963 as everybody should support LOOKUP and UPDATE bits at least)
4964 revert to compat mode with plain locks. */
4965 if (!data->ocd_ibits_known &&
4966 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4967 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4969 if (!mdt->mdt_opts.mo_acl)
4970 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4972 if (!mdt->mdt_opts.mo_user_xattr)
4973 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4975 if (!mdt->mdt_som_conf)
4976 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4978 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4979 data->ocd_brw_size = min(data->ocd_brw_size,
4980 (__u32)MD_MAX_BRW_SIZE);
4981 if (data->ocd_brw_size == 0) {
4982 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4983 " ocd_version: %x ocd_grant: %d "
4984 "ocd_index: %u ocd_brw_size is "
4985 "unexpectedly zero, network data "
4986 "corruption? Refusing connection of this"
4989 exp->exp_client_uuid.uuid,
4990 exp, data->ocd_connect_flags, data->ocd_version,
4991 data->ocd_grant, data->ocd_index);
4996 /* NB: Disregard the rule against updating
4997 * exp_connect_data.ocd_connect_flags in this case, since
4998 * tgt_client_new() needs to know if this is a lightweight
4999 * connection, and it is safe to expose this flag before
5000 * connection processing completes. */
5001 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5002 spin_lock(&exp->exp_lock);
5003 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5004 spin_unlock(&exp->exp_lock);
5007 data->ocd_version = LUSTRE_VERSION_CODE;
5009 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5010 CWARN("%s: MDS requires FID support, but client not\n",
5015 if (mdt->mdt_som_conf &&
5016 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
5017 OBD_CONNECT_MDS_MDS |
5018 OBD_CONNECT_SOM))) {
5019 CWARN("%s: MDS has SOM enabled, but client does not support "
5020 "it\n", mdt_obd_name(mdt));
5024 if (OCD_HAS_FLAG(data, PINGLESS)) {
5025 if (ptlrpc_pinger_suppress_pings()) {
5026 spin_lock(&exp->exp_obd->obd_dev_lock);
5027 list_del_init(&exp->exp_obd_chain_timed);
5028 spin_unlock(&exp->exp_obd->obd_dev_lock);
5030 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5034 data->ocd_max_easize = mdt->mdt_max_ea_size;
5039 /* mds_connect copy */
5040 static int mdt_obd_connect(const struct lu_env *env,
5041 struct obd_export **exp, struct obd_device *obd,
5042 struct obd_uuid *cluuid,
5043 struct obd_connect_data *data,
5046 struct obd_export *lexp;
5047 struct lustre_handle conn = { 0 };
5048 struct mdt_device *mdt;
5052 LASSERT(env != NULL);
5053 if (!exp || !obd || !cluuid)
5056 mdt = mdt_dev(obd->obd_lu_dev);
5059 * first, check whether the stack is ready to handle requests
5060 * XXX: probably not very appropriate method is used now
5061 * at some point we should find a better one
5063 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
5064 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5065 rc = obd_get_info(env, mdt->mdt_child_exp,
5066 sizeof(KEY_OSP_CONNECTED),
5067 KEY_OSP_CONNECTED, NULL, NULL, NULL);
5070 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5073 rc = class_connect(&conn, obd, cluuid);
5077 lexp = class_conn2export(&conn);
5078 LASSERT(lexp != NULL);
5080 rc = mdt_connect_internal(lexp, mdt, data);
5082 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5085 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5086 rc = tgt_client_new(env, lexp);
5088 mdt_export_stats_init(obd, lexp, localdata);
5090 /* For phase I, sync for cross-ref operation. */
5091 lexp->exp_keep_sync = 1;
5095 class_disconnect(lexp);
5104 static int mdt_obd_reconnect(const struct lu_env *env,
5105 struct obd_export *exp, struct obd_device *obd,
5106 struct obd_uuid *cluuid,
5107 struct obd_connect_data *data,
5113 if (exp == NULL || obd == NULL || cluuid == NULL)
5116 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5118 mdt_export_stats_init(obd, exp, localdata);
5123 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5124 struct mdt_thread_info *info,
5125 struct mdt_file_data *mfd)
5127 struct lu_context ses;
5131 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5136 lu_context_enter(&ses);
5138 mdt_ucred(info)->uc_valid = UCRED_OLD;
5139 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5141 lu_context_exit(&ses);
5142 lu_context_fini(&ses);
5148 static int mdt_export_cleanup(struct obd_export *exp)
5150 struct mdt_export_data *med = &exp->exp_mdt_data;
5151 struct obd_device *obd = exp->exp_obd;
5152 struct mdt_device *mdt;
5153 struct mdt_thread_info *info;
5155 CFS_LIST_HEAD(closing_list);
5156 struct mdt_file_data *mfd, *n;
5160 spin_lock(&med->med_open_lock);
5161 while (!cfs_list_empty(&med->med_open_head)) {
5162 cfs_list_t *tmp = med->med_open_head.next;
5163 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
5165 /* Remove mfd handle so it can't be found again.
5166 * We are consuming the mfd_list reference here. */
5167 class_handle_unhash(&mfd->mfd_handle);
5168 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
5170 spin_unlock(&med->med_open_lock);
5171 mdt = mdt_dev(obd->obd_lu_dev);
5172 LASSERT(mdt != NULL);
5174 rc = lu_env_init(&env, LCT_MD_THREAD);
5178 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5179 LASSERT(info != NULL);
5180 memset(info, 0, sizeof *info);
5181 info->mti_env = &env;
5182 info->mti_mdt = mdt;
5183 info->mti_exp = exp;
5185 if (!cfs_list_empty(&closing_list)) {
5186 struct md_attr *ma = &info->mti_attr;
5188 /* Close any open files (which may also cause orphan unlinking). */
5189 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5190 cfs_list_del_init(&mfd->mfd_list);
5191 ma->ma_need = ma->ma_valid = 0;
5193 /* This file is being closed due to an eviction, it
5194 * could have been modified and now dirty regarding to
5195 * HSM archive, check this!
5196 * The logic here is to mark a file dirty if there's a
5197 * chance it was dirtied before the client was evicted,
5198 * so that we don't have to wait for a release attempt
5199 * before finding out the file was actually dirty and
5200 * fail the release. Aggressively marking it dirty here
5201 * will cause the policy engine to attempt to
5202 * re-archive it; when rearchiving, we can compare the
5203 * current version to the HSM data_version and make the
5204 * archive request into a noop if it's not actually
5207 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5208 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5210 /* Don't unlink orphan on failover umount, LU-184 */
5211 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5212 ma->ma_valid = MA_FLAGS;
5213 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5215 mdt_mfd_close(info, mfd);
5218 info->mti_mdt = NULL;
5219 /* cleanup client slot early */
5220 /* Do not erase record for recoverable client. */
5221 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5222 tgt_client_del(&env, exp);
5228 static int mdt_obd_disconnect(struct obd_export *exp)
5234 class_export_get(exp);
5236 rc = server_disconnect_export(exp);
5238 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5240 rc = mdt_export_cleanup(exp);
5241 class_export_put(exp);
5245 /* FIXME: Can we avoid using these two interfaces? */
5246 static int mdt_init_export(struct obd_export *exp)
5248 struct mdt_export_data *med = &exp->exp_mdt_data;
5252 CFS_INIT_LIST_HEAD(&med->med_open_head);
5253 spin_lock_init(&med->med_open_lock);
5254 mutex_init(&med->med_idmap_mutex);
5255 med->med_idmap = NULL;
5256 spin_lock(&exp->exp_lock);
5257 exp->exp_connecting = 1;
5258 spin_unlock(&exp->exp_lock);
5260 /* self-export doesn't need client data and ldlm initialization */
5261 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5262 &exp->exp_client_uuid)))
5265 rc = tgt_client_alloc(exp);
5269 rc = ldlm_init_export(exp);
5276 tgt_client_free(exp);
5278 CERROR("%s: Failed to initialize export: rc = %d\n",
5279 exp->exp_obd->obd_name, rc);
5283 static int mdt_destroy_export(struct obd_export *exp)
5287 if (exp_connect_rmtclient(exp))
5288 mdt_cleanup_idmap(&exp->exp_mdt_data);
5290 target_destroy_export(exp);
5291 /* destroy can be called from failed obd_setup, so
5292 * checking uuid is safer than obd_self_export */
5293 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5294 &exp->exp_client_uuid)))
5297 ldlm_destroy_export(exp);
5298 tgt_client_free(exp);
5300 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5301 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5306 /** The maximum depth that fid2path() will search.
5307 * This is limited only because we want to store the fids for
5308 * historical path lookup purposes.
5310 #define MAX_PATH_DEPTH 100
5312 /** mdt_path() lookup structure. */
5313 struct path_lookup_info {
5314 __u64 pli_recno; /**< history point */
5315 __u64 pli_currec; /**< current record */
5316 struct lu_fid pli_fid;
5317 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5318 struct mdt_object *pli_mdt_obj;
5319 char *pli_path; /**< full path */
5321 int pli_linkno; /**< which hardlink to follow */
5322 int pli_fidcount; /**< number of \a pli_fids */
5325 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5326 struct linkea_data *ldata)
5330 LASSERT(ldata->ld_buf->lb_buf != NULL);
5332 if (!mdt_object_exists(mdt_obj))
5335 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5336 ldata->ld_buf, XATTR_NAME_LINK);
5337 if (rc == -ERANGE) {
5338 /* Buf was too small, figure out what we need. */
5339 lu_buf_free(ldata->ld_buf);
5340 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5341 ldata->ld_buf, XATTR_NAME_LINK);
5344 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5345 if (ldata->ld_buf->lb_buf == NULL)
5347 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5348 ldata->ld_buf, XATTR_NAME_LINK);
5353 return linkea_init(ldata);
5356 static int mdt_path_current(struct mdt_thread_info *info,
5357 struct path_lookup_info *pli)
5359 struct mdt_device *mdt = info->mti_mdt;
5360 struct mdt_object *mdt_obj;
5361 struct link_ea_header *leh;
5362 struct link_ea_entry *lee;
5363 struct lu_name *tmpname = &info->mti_name;
5364 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5365 struct lu_buf *buf = &info->mti_big_buf;
5368 struct linkea_data ldata = { 0 };
5372 /* temp buffer for path element, the buffer will be finally freed
5373 * in mdt_thread_info_fini */
5374 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5375 if (buf->lb_buf == NULL)
5379 ptr = pli->pli_path + pli->pli_pathlen - 1;
5382 pli->pli_fidcount = 0;
5383 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5385 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5386 * so checking root_fid can only happen on MDT0. */
5387 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5388 &pli->pli_fids[pli->pli_fidcount])) {
5389 mdt_obj = mdt_object_find(info->mti_env, mdt,
5390 &pli->pli_fids[pli->pli_fidcount]);
5391 if (IS_ERR(mdt_obj))
5392 GOTO(out, rc = PTR_ERR(mdt_obj));
5393 if (mdt_object_remote(mdt_obj)) {
5394 mdt_object_put(info->mti_env, mdt_obj);
5395 GOTO(remote_out, rc = -EREMOTE);
5397 if (!mdt_object_exists(mdt_obj)) {
5398 mdt_object_put(info->mti_env, mdt_obj);
5399 GOTO(out, rc = -ENOENT);
5402 rc = mdt_links_read(info, mdt_obj, &ldata);
5403 mdt_object_put(info->mti_env, mdt_obj);
5408 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5409 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5410 /* If set, use link #linkno for path lookup, otherwise use
5411 link #0. Only do this for the final path element. */
5412 if (pli->pli_fidcount == 0 &&
5413 pli->pli_linkno < leh->leh_reccount) {
5415 for (count = 0; count < pli->pli_linkno; count++) {
5416 lee = (struct link_ea_entry *)
5417 ((char *)lee + reclen);
5418 linkea_entry_unpack(lee, &reclen, tmpname,
5421 if (pli->pli_linkno < leh->leh_reccount - 1)
5422 /* indicate to user there are more links */
5426 /* Pack the name in the end of the buffer */
5427 ptr -= tmpname->ln_namelen;
5428 if (ptr - 1 <= pli->pli_path)
5429 GOTO(out, rc = -EOVERFLOW);
5430 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5433 /* Store the parent fid for historic lookup */
5434 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5435 GOTO(out, rc = -EOVERFLOW);
5436 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5440 ptr++; /* skip leading / */
5441 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5448 /* Returns the full path to this fid, as of changelog record recno. */
5449 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5450 char *path, int pathlen, __u64 *recno, int *linkno,
5453 struct mdt_device *mdt = info->mti_mdt;
5454 struct path_lookup_info *pli;
5462 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5471 pli->pli_mdt_obj = obj;
5472 pli->pli_recno = *recno;
5473 pli->pli_path = path;
5474 pli->pli_pathlen = pathlen;
5475 pli->pli_linkno = *linkno;
5477 /* Retry multiple times in case file is being moved */
5478 while (tries-- && rc == -EAGAIN)
5479 rc = mdt_path_current(info, pli);
5481 /* return the last resolved fids to the client, so the client will
5482 * build the left path on another MDT for remote object */
5483 *fid = pli->pli_fids[pli->pli_fidcount];
5485 *recno = pli->pli_currec;
5486 /* Return next link index to caller */
5487 *linkno = pli->pli_linkno;
5494 static int mdt_fid2path(struct mdt_thread_info *info,
5495 struct getinfo_fid2path *fp)
5497 struct mdt_device *mdt = info->mti_mdt;
5498 struct mdt_object *obj;
5502 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5503 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5505 if (!fid_is_sane(&fp->gf_fid))
5508 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5509 CWARN("%s: "DFID" is invalid, sequence should be "
5510 ">= "LPX64"\n", mdt_obd_name(mdt),
5511 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5515 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5516 if (obj == NULL || IS_ERR(obj)) {
5517 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5522 if (mdt_object_remote(obj))
5524 else if (!mdt_object_exists(obj))
5530 mdt_object_put(info->mti_env, obj);
5531 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5532 PFID(&fp->gf_fid), rc);
5536 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5537 &fp->gf_linkno, &fp->gf_fid);
5539 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5540 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5542 mdt_object_put(info->mti_env, obj);
5547 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5548 void *val, int vallen)
5550 struct getinfo_fid2path *fpout, *fpin;
5553 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5556 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5557 lustre_swab_fid2path(fpin);
5559 memcpy(fpout, fpin, sizeof(*fpin));
5560 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5563 rc = mdt_fid2path(info, fpout);
5567 int mdt_get_info(struct tgt_session_info *tsi)
5577 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5579 CDEBUG(D_IOCTL, "No GETINFO key");
5580 RETURN(err_serious(-EFAULT));
5582 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5585 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5586 if (vallen == NULL) {
5587 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5588 RETURN(err_serious(-EFAULT));
5591 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5593 rc = req_capsule_server_pack(tsi->tsi_pill);
5595 RETURN(err_serious(rc));
5597 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5598 if (valout == NULL) {
5599 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5600 RETURN(err_serious(-EFAULT));
5603 if (KEY_IS(KEY_FID2PATH)) {
5604 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5606 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5607 mdt_thread_info_fini(info);
5614 /* Pass the ioc down */
5615 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5616 unsigned int cmd, int len, void *data)
5618 struct lu_context ioctl_session;
5619 struct md_device *next = mdt->mdt_child;
5623 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5626 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5627 lu_context_enter(&ioctl_session);
5628 env->le_ses = &ioctl_session;
5630 LASSERT(next->md_ops->mdo_iocontrol);
5631 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5633 lu_context_exit(&ioctl_session);
5634 lu_context_fini(&ioctl_session);
5638 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5640 struct obd_ioctl_data *data = karg;
5643 struct mdt_object *obj;
5644 struct mdt_lock_handle *lh;
5648 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5649 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5652 fid = (struct lu_fid *)data->ioc_inlbuf1;
5654 if (!fid_is_sane(fid))
5657 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5659 lh = &mti->mti_lh[MDT_LH_PARENT];
5660 mdt_lock_reg_init(lh, LCK_CR);
5662 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5664 RETURN(PTR_ERR(obj));
5666 if (mdt_object_remote(obj)) {
5669 * before calling version get the correct MDS should be
5670 * fid, this is error to find remote object here
5672 CERROR("nonlocal object "DFID"\n", PFID(fid));
5673 } else if (!mdt_object_exists(obj)) {
5674 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5677 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5678 *(__u64 *)data->ioc_inlbuf2 = version;
5681 mdt_object_unlock_put(mti, obj, lh, 1);
5685 /* ioctls on obd dev */
5686 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5687 void *karg, void *uarg)
5690 struct obd_device *obd = exp->exp_obd;
5691 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5692 struct dt_device *dt = mdt->mdt_bottom;
5696 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5697 rc = lu_env_init(&env, LCT_MD_THREAD);
5703 rc = mdt_device_sync(&env, mdt);
5705 case OBD_IOC_SET_READONLY:
5706 rc = dt->dd_ops->dt_ro(&env, dt);
5708 case OBD_IOC_ABORT_RECOVERY:
5709 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5710 target_stop_recovery_thread(obd);
5713 case OBD_IOC_CHANGELOG_REG:
5714 case OBD_IOC_CHANGELOG_DEREG:
5715 case OBD_IOC_CHANGELOG_CLEAR:
5716 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5718 case OBD_IOC_START_LFSCK: {
5719 struct md_device *next = mdt->mdt_child;
5720 struct obd_ioctl_data *data = karg;
5721 struct lfsck_start_param lsp;
5723 if (unlikely(data == NULL)) {
5728 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5729 lsp.lsp_index_valid = 0;
5730 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5733 case OBD_IOC_STOP_LFSCK: {
5734 struct md_device *next = mdt->mdt_child;
5735 struct obd_ioctl_data *data = karg;
5736 struct lfsck_stop stop;
5738 stop.ls_status = LS_STOPPED;
5739 /* Old lfsck utils may pass NULL @stop. */
5740 if (data->ioc_inlbuf1 == NULL)
5744 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5746 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5749 case OBD_IOC_GET_OBJ_VERSION: {
5750 struct mdt_thread_info *mti;
5751 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5752 memset(mti, 0, sizeof *mti);
5753 mti->mti_env = &env;
5757 rc = mdt_ioc_version_get(mti, karg);
5760 case OBD_IOC_CATLOGLIST: {
5761 struct mdt_thread_info *mti;
5763 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5764 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5765 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5766 &mti->mti_tmp_fid1);
5771 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5772 mdt_obd_name(mdt), cmd, rc);
5779 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5781 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5785 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5789 int mdt_obd_postrecov(struct obd_device *obd)
5794 rc = lu_env_init(&env, LCT_MD_THREAD);
5797 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5802 static struct obd_ops mdt_obd_device_ops = {
5803 .o_owner = THIS_MODULE,
5804 .o_set_info_async = mdt_obd_set_info_async,
5805 .o_connect = mdt_obd_connect,
5806 .o_reconnect = mdt_obd_reconnect,
5807 .o_disconnect = mdt_obd_disconnect,
5808 .o_init_export = mdt_init_export,
5809 .o_destroy_export = mdt_destroy_export,
5810 .o_iocontrol = mdt_iocontrol,
5811 .o_postrecov = mdt_obd_postrecov,
5814 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5815 struct lu_device *d)
5817 struct mdt_device *m = mdt_dev(d);
5824 static struct lu_device *mdt_device_free(const struct lu_env *env,
5825 struct lu_device *d)
5827 struct mdt_device *m = mdt_dev(d);
5830 lu_device_fini(&m->mdt_lu_dev);
5836 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5837 struct lu_device_type *t,
5838 struct lustre_cfg *cfg)
5840 struct lu_device *l;
5841 struct mdt_device *m;
5848 rc = mdt_init0(env, m, t, cfg);
5850 mdt_device_free(env, l);
5855 l = ERR_PTR(-ENOMEM);
5859 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5860 LU_KEY_INIT(mdt, struct mdt_thread_info);
5862 static void mdt_key_fini(const struct lu_context *ctx,
5863 struct lu_context_key *key, void* data)
5865 struct mdt_thread_info *info = data;
5867 if (info->mti_big_lmm) {
5868 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5869 info->mti_big_lmm = NULL;
5870 info->mti_big_lmmsize = 0;
5875 /* context key: mdt_thread_key */
5876 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5878 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5880 return lu_ucred(info->mti_env);
5883 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5885 return lu_ucred_check(info->mti_env);
5889 * Enable/disable COS (Commit On Sharing).
5891 * Set/Clear the COS flag in mdt options.
5893 * \param mdt mdt device
5894 * \param val 0 disables COS, other values enable COS
5896 void mdt_enable_cos(struct mdt_device *mdt, int val)
5901 mdt->mdt_opts.mo_cos = !!val;
5902 rc = lu_env_init(&env, LCT_LOCAL);
5903 if (unlikely(rc != 0)) {
5904 CWARN("lu_env initialization failed with rc = %d,"
5905 "cannot sync\n", rc);
5908 mdt_device_sync(&env, mdt);
5913 * Check COS (Commit On Sharing) status.
5915 * Return COS flag status.
5917 * \param mdt mdt device
5919 int mdt_cos_is_enabled(struct mdt_device *mdt)
5921 return mdt->mdt_opts.mo_cos != 0;
5924 static struct lu_device_type_operations mdt_device_type_ops = {
5925 .ldto_device_alloc = mdt_device_alloc,
5926 .ldto_device_free = mdt_device_free,
5927 .ldto_device_fini = mdt_device_fini
5930 static struct lu_device_type mdt_device_type = {
5931 .ldt_tags = LU_DEVICE_MD,
5932 .ldt_name = LUSTRE_MDT_NAME,
5933 .ldt_ops = &mdt_device_type_ops,
5934 .ldt_ctx_tags = LCT_MD_THREAD
5937 static int __init mdt_mod_init(void)
5939 struct lprocfs_static_vars lvars;
5942 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5943 FID_NOBRACE_LEN + 1);
5944 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5946 rc = lu_kmem_init(mdt_caches);
5950 rc = mds_mod_init();
5954 lprocfs_mdt_init_vars(&lvars);
5955 rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
5956 #ifndef HAVE_ONLY_PROCFS_SEQ
5959 LUSTRE_MDT_NAME, &mdt_device_type);
5964 lu_kmem_fini(mdt_caches);
5971 static void __exit mdt_mod_exit(void)
5973 class_unregister_type(LUSTRE_MDT_NAME);
5975 lu_kmem_fini(mdt_caches);
5978 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5979 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5980 MODULE_LICENSE("GPL");
5982 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);