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_linkea.h>
71 #include <lustre_lfsck.h>
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 ldlm_mode_t mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
96 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 int mdt_get_disposition(struct ldlm_reply *rep, int flag)
134 return (rep->lock_policy_res1 & flag);
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, int flag)
141 info->mti_opdata &= ~flag;
142 tgt_opdata_clear(info->mti_env, flag);
145 rep->lock_policy_res1 &= ~flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, int flag)
152 info->mti_opdata |= flag;
153 tgt_opdata_set(info->mti_env, flag);
156 rep->lock_policy_res1 |= flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, ldlm_mode_t lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, ldlm_mode_t lm,
168 const char *name, int namelen)
170 lh->mlh_reg_mode = lm;
171 lh->mlh_rreg_mode = lm;
172 lh->mlh_type = MDT_PDO_LOCK;
174 if (name != NULL && (name[0] != '\0')) {
175 LASSERT(namelen > 0);
176 lh->mlh_pdo_hash = full_name_hash(name, namelen);
177 /* XXX Workaround for LU-2856
178 * Zero is a valid return value of full_name_hash, but several
179 * users of mlh_pdo_hash assume a non-zero hash value. We
180 * therefore map zero onto an arbitrary, but consistent
181 * value (1) to avoid problems further down the road. */
182 if (unlikely(!lh->mlh_pdo_hash))
183 lh->mlh_pdo_hash = 1;
185 LASSERT(namelen == 0);
186 lh->mlh_pdo_hash = 0ull;
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 static int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
487 const struct lu_env *env = info->mti_env;
491 LASSERT(info->mti_big_lmm_used == 0);
492 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
496 /* big_lmm may need to be grown */
497 if (info->mti_big_lmmsize < rc) {
498 int size = size_roundup_power2(rc);
500 if (info->mti_big_lmmsize > 0) {
501 /* free old buffer */
502 LASSERT(info->mti_big_lmm);
503 OBD_FREE_LARGE(info->mti_big_lmm,
504 info->mti_big_lmmsize);
505 info->mti_big_lmm = NULL;
506 info->mti_big_lmmsize = 0;
509 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
510 if (info->mti_big_lmm == NULL)
512 info->mti_big_lmmsize = size;
514 LASSERT(info->mti_big_lmmsize >= rc);
516 info->mti_buf.lb_buf = info->mti_big_lmm;
517 info->mti_buf.lb_len = info->mti_big_lmmsize;
518 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
523 int mdt_attr_get_lov(struct mdt_thread_info *info,
524 struct mdt_object *o, struct md_attr *ma)
526 struct md_object *next = mdt_object_child(o);
527 struct lu_buf *buf = &info->mti_buf;
530 buf->lb_buf = ma->ma_lmm;
531 buf->lb_len = ma->ma_lmm_size;
532 rc = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_LOV);
534 ma->ma_lmm_size = rc;
535 ma->ma_valid |= MA_LOV;
537 } else if (rc == -ENODATA) {
540 } else if (rc == -ERANGE) {
541 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LOV);
543 info->mti_big_lmm_used = 1;
544 ma->ma_valid |= MA_LOV;
545 ma->ma_lmm = info->mti_big_lmm;
546 ma->ma_lmm_size = rc;
547 /* update mdt_max_mdsize so all clients
548 * will be aware about that */
549 if (info->mti_mdt->mdt_max_mdsize < rc)
550 info->mti_mdt->mdt_max_mdsize = rc;
558 int mdt_attr_get_pfid(struct mdt_thread_info *info,
559 struct mdt_object *o, struct lu_fid *pfid)
561 struct lu_buf *buf = &info->mti_buf;
562 struct link_ea_header *leh;
563 struct link_ea_entry *lee;
567 buf->lb_buf = info->mti_big_lmm;
568 buf->lb_len = info->mti_big_lmmsize;
569 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
570 buf, XATTR_NAME_LINK);
571 /* ignore errors, MA_PFID won't be set and it is
572 * up to the caller to treat this as an error */
573 if (rc == -ERANGE || buf->lb_len == 0) {
574 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
575 buf->lb_buf = info->mti_big_lmm;
576 buf->lb_len = info->mti_big_lmmsize;
581 if (rc < sizeof(*leh)) {
582 CERROR("short LinkEA on "DFID": rc = %d\n",
583 PFID(mdt_object_fid(o)), rc);
587 leh = (struct link_ea_header *) buf->lb_buf;
588 lee = (struct link_ea_entry *)(leh + 1);
589 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
590 leh->leh_magic = LINK_EA_MAGIC;
591 leh->leh_reccount = __swab32(leh->leh_reccount);
592 leh->leh_len = __swab64(leh->leh_len);
594 if (leh->leh_magic != LINK_EA_MAGIC)
596 if (leh->leh_reccount == 0)
599 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
600 fid_be_to_cpu(pfid, pfid);
605 int mdt_attr_get_complex(struct mdt_thread_info *info,
606 struct mdt_object *o, struct md_attr *ma)
608 const struct lu_env *env = info->mti_env;
609 struct md_object *next = mdt_object_child(o);
610 struct lu_buf *buf = &info->mti_buf;
611 u32 mode = lu_object_attr(&next->mo_lu);
612 int need = ma->ma_need;
618 if (need & MA_INODE) {
619 ma->ma_need = MA_INODE;
620 rc = mo_attr_get(env, next, ma);
623 ma->ma_valid |= MA_INODE;
626 if (need & MA_PFID) {
627 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
629 ma->ma_valid |= MA_PFID;
630 /* ignore this error, parent fid is not mandatory */
634 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
635 rc = mdt_attr_get_lov(info, o, ma);
640 if (need & MA_LMV && S_ISDIR(mode)) {
641 buf->lb_buf = ma->ma_lmv;
642 buf->lb_len = ma->ma_lmv_size;
643 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
645 ma->ma_lmv_size = rc2;
646 ma->ma_valid |= MA_LMV;
647 } else if (rc2 == -ENODATA) {
654 if (need & MA_SOM && S_ISREG(mode)) {
655 buf->lb_buf = info->mti_xattr_buf;
656 buf->lb_len = sizeof(info->mti_xattr_buf);
657 CLASSERT(sizeof(struct som_attrs) <=
658 sizeof(info->mti_xattr_buf));
659 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_SOM);
660 rc2 = lustre_buf2som(info->mti_xattr_buf, rc2, ma->ma_som);
662 ma->ma_valid |= MA_SOM;
663 else if (rc2 < 0 && rc2 != -ENODATA)
667 if (need & MA_HSM && S_ISREG(mode)) {
668 buf->lb_buf = info->mti_xattr_buf;
669 buf->lb_len = sizeof(info->mti_xattr_buf);
670 CLASSERT(sizeof(struct hsm_attrs) <=
671 sizeof(info->mti_xattr_buf));
672 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
673 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
675 ma->ma_valid |= MA_HSM;
676 else if (rc2 < 0 && rc2 != -ENODATA)
680 #ifdef CONFIG_FS_POSIX_ACL
681 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
682 buf->lb_buf = ma->ma_acl;
683 buf->lb_len = ma->ma_acl_size;
684 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
686 ma->ma_acl_size = rc2;
687 ma->ma_valid |= MA_ACL_DEF;
688 } else if (rc2 == -ENODATA) {
697 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
698 rc, ma->ma_valid, ma->ma_lmm);
702 static int mdt_getattr_internal(struct mdt_thread_info *info,
703 struct mdt_object *o, int ma_need)
705 struct md_object *next = mdt_object_child(o);
706 const struct mdt_body *reqbody = info->mti_body;
707 struct ptlrpc_request *req = mdt_info_req(info);
708 struct md_attr *ma = &info->mti_attr;
709 struct lu_attr *la = &ma->ma_attr;
710 struct req_capsule *pill = info->mti_pill;
711 const struct lu_env *env = info->mti_env;
712 struct mdt_body *repbody;
713 struct lu_buf *buffer = &info->mti_buf;
718 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
719 RETURN(err_serious(-ENOMEM));
721 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
725 if (mdt_object_remote(o)) {
726 /* This object is located on remote node.*/
727 /* Return -EIO for old client */
728 if (!mdt_is_dne_client(req->rq_export))
729 GOTO(out, rc = -EIO);
731 repbody->fid1 = *mdt_object_fid(o);
732 repbody->valid = OBD_MD_FLID | OBD_MD_MDS;
736 buffer->lb_len = reqbody->eadatasize;
737 if (buffer->lb_len > 0)
738 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
740 buffer->lb_buf = NULL;
742 /* If it is dir object and client require MEA, then we got MEA */
743 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
744 reqbody->valid & OBD_MD_MEA) {
745 /* Assumption: MDT_MD size is enough for lmv size. */
746 ma->ma_lmv = buffer->lb_buf;
747 ma->ma_lmv_size = buffer->lb_len;
748 ma->ma_need = MA_LMV | MA_INODE;
750 ma->ma_lmm = buffer->lb_buf;
751 ma->ma_lmm_size = buffer->lb_len;
752 ma->ma_need = MA_LOV | MA_INODE | MA_HSM;
755 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
756 reqbody->valid & OBD_MD_FLDIREA &&
757 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
758 /* get default stripe info for this dir. */
759 ma->ma_need |= MA_LOV_DEF;
761 ma->ma_need |= ma_need;
762 if (ma->ma_need & MA_SOM)
763 ma->ma_som = &info->mti_u.som.data;
765 rc = mdt_attr_get_complex(info, o, ma);
767 CERROR("%s: getattr error for "DFID": rc = %d\n",
768 mdt_obd_name(info->mti_mdt),
769 PFID(mdt_object_fid(o)), rc);
773 /* if file is released, check if a restore is running */
774 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_RELEASED) &&
775 mdt_hsm_restore_is_running(info, mdt_object_fid(o))) {
776 repbody->t_state = MS_RESTORE;
777 repbody->valid |= OBD_MD_TSTATE;
780 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
782 /* the Lustre protocol supposes to return default striping
783 * on the user-visible root if explicitly requested */
784 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
785 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
786 struct lu_fid rootfid;
787 struct mdt_object *root;
788 struct mdt_device *mdt = info->mti_mdt;
790 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
793 root = mdt_object_find(env, mdt, &rootfid);
795 RETURN(PTR_ERR(root));
796 rc = mdt_attr_get_lov(info, root, ma);
797 mdt_object_put(info->mti_env, root);
799 CERROR("%s: getattr error for "DFID": rc = %d\n",
800 mdt_obd_name(info->mti_mdt),
801 PFID(mdt_object_fid(o)), rc);
806 if (likely(ma->ma_valid & MA_INODE))
807 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
811 if (mdt_body_has_lov(la, reqbody)) {
812 if (ma->ma_valid & MA_LOV) {
813 LASSERT(ma->ma_lmm_size);
814 mdt_dump_lmm(D_INFO, ma->ma_lmm);
815 repbody->eadatasize = ma->ma_lmm_size;
816 if (S_ISDIR(la->la_mode))
817 repbody->valid |= OBD_MD_FLDIREA;
819 repbody->valid |= OBD_MD_FLEASIZE;
821 if (ma->ma_valid & MA_LMV) {
822 LASSERT(S_ISDIR(la->la_mode));
823 repbody->eadatasize = ma->ma_lmv_size;
824 repbody->valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
826 } else if (S_ISLNK(la->la_mode) &&
827 reqbody->valid & OBD_MD_LINKNAME) {
828 buffer->lb_buf = ma->ma_lmm;
829 /* eadatasize from client includes NULL-terminator, so
830 * there is no need to read it */
831 buffer->lb_len = reqbody->eadatasize - 1;
832 rc = mo_readlink(env, next, buffer);
833 if (unlikely(rc <= 0)) {
834 CERROR("%s: readlink failed for "DFID": rc = %d\n",
835 mdt_obd_name(info->mti_mdt),
836 PFID(mdt_object_fid(o)), rc);
839 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
841 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
843 repbody->valid |= OBD_MD_LINKNAME;
844 /* we need to report back size with NULL-terminator
845 * because client expects that */
846 repbody->eadatasize = rc + 1;
847 if (repbody->eadatasize != reqbody->eadatasize)
848 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
849 "on "DFID ", expected %d\n",
850 mdt_obd_name(info->mti_mdt),
851 rc, PFID(mdt_object_fid(o)),
852 reqbody->eadatasize - 1);
854 ((char *)ma->ma_lmm)[rc] = 0;
856 /* If the total CDEBUG() size is larger than a page, it
857 * will print a warning to the console, avoid this by
858 * printing just the last part of the symlink. */
859 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
860 print_limit < rc ? "..." : "", print_limit,
861 (char *)ma->ma_lmm + rc - print_limit, rc);
866 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
867 repbody->max_cookiesize = 0;
868 repbody->max_mdsize = info->mti_mdt->mdt_max_mdsize;
869 repbody->valid |= OBD_MD_FLMODEASIZE;
870 CDEBUG(D_INODE, "I am going to change the MAX_MD_SIZE & "
871 "MAX_COOKIE to : %d:%d\n", repbody->max_mdsize,
872 repbody->max_cookiesize);
875 if (exp_connect_rmtclient(info->mti_exp) &&
876 reqbody->valid & OBD_MD_FLRMTPERM) {
877 void *buf = req_capsule_server_get(pill, &RMF_ACL);
879 /* mdt_getattr_lock only */
880 rc = mdt_pack_remote_perm(info, o, buf);
882 repbody->valid &= ~OBD_MD_FLRMTPERM;
883 repbody->aclsize = 0;
886 repbody->valid |= OBD_MD_FLRMTPERM;
887 repbody->aclsize = sizeof(struct mdt_remote_perm);
890 #ifdef CONFIG_FS_POSIX_ACL
891 else if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
892 (reqbody->valid & OBD_MD_FLACL)) {
893 buffer->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
894 buffer->lb_len = req_capsule_get_size(pill,
895 &RMF_ACL, RCL_SERVER);
896 if (buffer->lb_len > 0) {
897 rc = mo_xattr_get(env, next, buffer,
898 XATTR_NAME_ACL_ACCESS);
900 if (rc == -ENODATA) {
901 repbody->aclsize = 0;
902 repbody->valid |= OBD_MD_FLACL;
904 } else if (rc == -EOPNOTSUPP) {
907 CERROR("%s: unable to read "DFID
909 mdt_obd_name(info->mti_mdt),
910 PFID(mdt_object_fid(o)), rc);
913 repbody->aclsize = rc;
914 repbody->valid |= OBD_MD_FLACL;
921 if (reqbody->valid & OBD_MD_FLMDSCAPA &&
922 info->mti_mdt->mdt_lut.lut_mds_capa &&
923 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
924 struct lustre_capa *capa;
926 capa = req_capsule_server_get(pill, &RMF_CAPA1);
928 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
929 rc = mo_capa_get(env, next, capa, 0);
932 repbody->valid |= OBD_MD_FLMDSCAPA;
937 mdt_counter_incr(req, LPROC_MDT_GETATTR);
942 static int mdt_renew_capa(struct mdt_thread_info *info)
944 struct mdt_object *obj = info->mti_object;
945 struct mdt_body *body;
946 struct lustre_capa *capa, *c;
950 /* if object doesn't exist, or server has disabled capability,
951 * return directly, client will find body->valid OBD_MD_FLOSSCAPA
954 if (!obj || !info->mti_mdt->mdt_lut.lut_oss_capa ||
955 !(exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA))
958 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
959 LASSERT(body != NULL);
961 c = req_capsule_client_get(info->mti_pill, &RMF_CAPA1);
964 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
968 rc = mo_capa_get(info->mti_env, mdt_object_child(obj), capa, 1);
970 body->valid |= OBD_MD_FLOSSCAPA;
974 int mdt_getattr(struct tgt_session_info *tsi)
976 struct mdt_thread_info *info = tsi2mdt_info(tsi);
977 struct mdt_object *obj = info->mti_object;
978 struct req_capsule *pill = info->mti_pill;
979 struct mdt_body *reqbody;
980 struct mdt_body *repbody;
985 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
988 if (reqbody->valid & OBD_MD_FLOSSCAPA) {
989 rc = req_capsule_server_pack(pill);
991 RETURN(err_serious(rc));
992 rc = mdt_renew_capa(info);
993 GOTO(out_shrink, rc);
996 LASSERT(obj != NULL);
997 LASSERT(lu_object_assert_exists(&obj->mot_obj));
999 mode = lu_object_attr(&obj->mot_obj);
1001 /* old clients may not report needed easize, use max value then */
1002 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1003 reqbody->eadatasize == 0 ?
1004 info->mti_mdt->mdt_max_mdsize :
1005 reqbody->eadatasize);
1007 rc = req_capsule_server_pack(pill);
1008 if (unlikely(rc != 0))
1009 GOTO(out, rc = err_serious(rc));
1011 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1012 LASSERT(repbody != NULL);
1013 repbody->eadatasize = 0;
1014 repbody->aclsize = 0;
1016 if (reqbody->valid & OBD_MD_FLRMTPERM)
1017 rc = mdt_init_ucred(info, reqbody);
1019 rc = mdt_check_ucred(info);
1021 GOTO(out_shrink, rc);
1023 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1026 * Don't check capability at all, because rename might getattr for
1027 * remote obj, and at that time no capability is available.
1029 mdt_set_capainfo(info, 1, &reqbody->fid1, BYPASS_CAPA);
1030 rc = mdt_getattr_internal(info, obj, 0);
1031 if (reqbody->valid & OBD_MD_FLRMTPERM)
1032 mdt_exit_ucred(info);
1035 mdt_client_compatibility(info);
1036 rc2 = mdt_fix_reply(info);
1040 mdt_thread_info_fini(info);
1044 int mdt_is_subdir(struct tgt_session_info *tsi)
1046 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1047 struct mdt_object *o = info->mti_object;
1048 struct req_capsule *pill = info->mti_pill;
1049 const struct mdt_body *body = info->mti_body;
1050 struct mdt_body *repbody;
1056 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1059 * We save last checked parent fid to @repbody->fid1 for remote
1062 LASSERT(fid_is_sane(&body->fid2));
1063 LASSERT(mdt_object_exists(o) && !mdt_object_remote(o));
1064 rc = mdo_is_subdir(info->mti_env, mdt_object_child(o),
1065 &body->fid2, &repbody->fid1);
1066 if (rc == 0 || rc == -EREMOTE)
1067 repbody->valid |= OBD_MD_FLID;
1069 mdt_thread_info_fini(info);
1073 int mdt_swap_layouts(struct tgt_session_info *tsi)
1075 struct mdt_thread_info *info;
1076 struct ptlrpc_request *req = tgt_ses_req(tsi);
1077 struct obd_export *exp = req->rq_export;
1078 struct mdt_object *o1, *o2, *o;
1079 struct mdt_lock_handle *lh1, *lh2;
1080 struct mdc_swap_layouts *msl;
1084 /* client does not support layout lock, so layout swaping
1086 * FIXME: there is a problem for old clients which don't support
1087 * layout lock yet. If those clients have already opened the file
1088 * they won't be notified at all so that old layout may still be
1089 * used to do IO. This can be fixed after file release is landed by
1090 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1091 if (!exp_connect_layout(exp))
1092 RETURN(-EOPNOTSUPP);
1094 info = tsi2mdt_info(tsi);
1095 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
1096 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
1097 req_capsule_client_get(info->mti_pill,
1100 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA2, RCL_CLIENT))
1101 mdt_set_capainfo(info, 1, &info->mti_body->fid2,
1102 req_capsule_client_get(info->mti_pill,
1105 o1 = info->mti_object;
1106 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1107 &info->mti_body->fid2);
1109 GOTO(out, rc = PTR_ERR(o));
1111 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1112 GOTO(put, rc = -ENOENT);
1114 rc = lu_fid_cmp(&info->mti_body->fid1, &info->mti_body->fid2);
1115 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1121 /* permission check. Make sure the calling process having permission
1122 * to write both files. */
1123 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1128 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1133 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1135 GOTO(put, rc = -EPROTO);
1137 lh1 = &info->mti_lh[MDT_LH_NEW];
1138 mdt_lock_reg_init(lh1, LCK_EX);
1139 lh2 = &info->mti_lh[MDT_LH_OLD];
1140 mdt_lock_reg_init(lh2, LCK_EX);
1142 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1143 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1147 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1148 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1152 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1153 mdt_object_child(o2), msl->msl_flags);
1156 mdt_object_unlock(info, o2, lh2, rc);
1158 mdt_object_unlock(info, o1, lh1, rc);
1160 mdt_object_put(info->mti_env, o);
1162 mdt_thread_info_fini(info);
1166 static int mdt_raw_lookup(struct mdt_thread_info *info,
1167 struct mdt_object *parent,
1168 const struct lu_name *lname,
1169 struct ldlm_reply *ldlm_rep)
1171 struct md_object *next = mdt_object_child(info->mti_object);
1172 const struct mdt_body *reqbody = info->mti_body;
1173 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1174 struct mdt_body *repbody;
1178 if (reqbody->valid != OBD_MD_FLID)
1181 LASSERT(!info->mti_cross_ref);
1183 /* Only got the fid of this obj by name */
1184 fid_zero(child_fid);
1185 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1188 /* XXX is raw_lookup possible as intent operation? */
1191 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1194 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1196 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1199 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1200 repbody->fid1 = *child_fid;
1201 repbody->valid = OBD_MD_FLID;
1207 * UPDATE lock should be taken against parent, and be release before exit;
1208 * child_bits lock should be taken against child, and be returned back:
1209 * (1)normal request should release the child lock;
1210 * (2)intent request will grant the lock to client.
1212 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1213 struct mdt_lock_handle *lhc,
1215 struct ldlm_reply *ldlm_rep)
1217 struct ptlrpc_request *req = mdt_info_req(info);
1218 struct mdt_body *reqbody = NULL;
1219 struct mdt_object *parent = info->mti_object;
1220 struct mdt_object *child;
1221 struct md_object *next = mdt_object_child(parent);
1222 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1223 struct lu_name *lname = NULL;
1224 const char *name = NULL;
1226 struct mdt_lock_handle *lhp = NULL;
1227 struct ldlm_lock *lock;
1228 struct ldlm_res_id *res_id;
1235 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1236 LASSERT(ergo(is_resent,
1237 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1239 LASSERT(parent != NULL);
1240 name = req_capsule_client_get(info->mti_pill, &RMF_NAME);
1242 RETURN(err_serious(-EFAULT));
1244 namelen = req_capsule_get_size(info->mti_pill, &RMF_NAME,
1246 if (!info->mti_cross_ref) {
1248 * XXX: Check for "namelen == 0" is for getattr by fid
1249 * (OBD_CONNECT_ATTRFID), otherwise do not allow empty name,
1250 * that is the name must contain at least one character and
1251 * the terminating '\0'
1254 reqbody = req_capsule_client_get(info->mti_pill,
1256 if (unlikely(reqbody == NULL))
1257 RETURN(err_serious(-EFAULT));
1259 if (unlikely(!fid_is_sane(&reqbody->fid2)))
1260 RETURN(err_serious(-EINVAL));
1263 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1265 PFID(mdt_object_fid(parent)),
1266 PFID(&reqbody->fid2), ldlm_rep);
1268 lname = mdt_name(info->mti_env, (char *)name, namelen);
1269 CDEBUG(D_INODE, "getattr with lock for "DFID"/%s, "
1270 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1274 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1276 if (unlikely(!mdt_object_exists(parent)) && lname) {
1277 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1279 "Parent doesn't exist!\n");
1281 } else if (!info->mti_cross_ref) {
1282 LASSERTF(!mdt_object_remote(parent),
1283 "Parent "DFID" is on remote server\n",
1284 PFID(mdt_object_fid(parent)));
1287 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1295 if (info->mti_cross_ref) {
1296 /* Only getattr on the child. Parent is on another node. */
1297 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1299 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1300 "ldlm_rep=%p\n", PFID(mdt_object_fid(child)), ldlm_rep);
1303 /* Do not take lock for resent case. */
1304 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1305 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1306 lhc->mlh_reg_lh.cookie);
1307 LASSERT(fid_res_name_eq(mdt_object_fid(child),
1308 &lock->l_resource->lr_name));
1309 LDLM_LOCK_PUT(lock);
1312 mdt_lock_handle_init(lhc);
1313 mdt_lock_reg_init(lhc, LCK_PR);
1316 * Object's name is on another MDS, no lookup or layout
1317 * lock is needed here but update lock is.
1319 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1320 MDS_INODELOCK_LAYOUT);
1321 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1323 rc = mdt_object_lock(info, child, lhc, child_bits,
1327 /* Finally, we can get attr for child. */
1328 if (!mdt_object_exists(child)) {
1329 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1331 "remote object doesn't exist.\n");
1332 mdt_object_unlock(info, child, lhc, 1);
1336 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1338 rc = mdt_getattr_internal(info, child, 0);
1339 if (unlikely(rc != 0))
1340 mdt_object_unlock(info, child, lhc, 1);
1346 /* step 1: lock parent only if parent is a directory */
1347 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1348 lhp = &info->mti_lh[MDT_LH_PARENT];
1349 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1350 rc = mdt_object_lock(info, parent, lhp,
1351 MDS_INODELOCK_UPDATE,
1353 if (unlikely(rc != 0))
1357 /* step 2: lookup child's fid by name */
1358 fid_zero(child_fid);
1359 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1364 mdt_set_disposition(info, ldlm_rep,
1366 GOTO(out_parent, rc);
1368 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1370 *child_fid = reqbody->fid2;
1371 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1375 *step 3: find the child object by fid & lock it.
1376 * regardless if it is local or remote.
1378 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1379 * set parent dir fid the same as child fid in getattr by fid case
1380 * we should not lu_object_find() the object again, could lead
1381 * to hung if there is a concurrent unlink destroyed the object.
1383 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1384 mdt_object_get(info->mti_env, parent);
1387 child = mdt_object_find(info->mti_env, info->mti_mdt,
1391 if (unlikely(IS_ERR(child)))
1392 GOTO(out_parent, rc = PTR_ERR(child));
1394 /* Do not take lock for resent case. */
1395 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1396 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1397 lhc->mlh_reg_lh.cookie);
1399 res_id = &lock->l_resource->lr_name;
1400 if (!fid_res_name_eq(mdt_object_fid(child),
1401 &lock->l_resource->lr_name)) {
1402 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1403 &lock->l_resource->lr_name),
1404 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1405 PLDLMRES(lock->l_resource),
1406 PFID(mdt_object_fid(parent)));
1407 CWARN("Although resent, but still not get child lock"
1408 "parent:"DFID" child:"DFID"\n",
1409 PFID(mdt_object_fid(parent)),
1410 PFID(mdt_object_fid(child)));
1411 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1412 LDLM_LOCK_PUT(lock);
1415 LDLM_LOCK_PUT(lock);
1418 bool try_layout = false;
1421 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1422 mdt_lock_handle_init(lhc);
1423 mdt_lock_reg_init(lhc, LCK_PR);
1425 if (!mdt_object_exists(child)) {
1426 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1428 "Object doesn't exist!\n");
1429 GOTO(out_child, rc = -ENOENT);
1432 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1433 mdt_object_exists(child) && !mdt_object_remote(child)) {
1434 struct md_attr *ma = &info->mti_attr;
1437 ma->ma_need = MA_INODE;
1438 rc = mdt_attr_get_complex(info, child, ma);
1439 if (unlikely(rc != 0))
1440 GOTO(out_child, rc);
1442 /* If the file has not been changed for some time, we
1443 * return not only a LOOKUP lock, but also an UPDATE
1444 * lock and this might save us RPC on later STAT. For
1445 * directories, it also let negative dentry cache start
1446 * working for this dir. */
1447 if (ma->ma_valid & MA_INODE &&
1448 ma->ma_attr.la_valid & LA_CTIME &&
1449 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1450 ma->ma_attr.la_ctime < cfs_time_current_sec())
1451 child_bits |= MDS_INODELOCK_UPDATE;
1454 /* layout lock must be granted in a best-effort way
1455 * for IT operations */
1456 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1457 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1458 exp_connect_layout(info->mti_exp) &&
1459 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1460 !mdt_object_remote(child) && ldlm_rep != NULL) {
1461 /* try to grant layout lock for regular file. */
1467 child_bits |= MDS_INODELOCK_LAYOUT;
1468 /* try layout lock, it may fail to be granted due to
1469 * contention at LOOKUP or UPDATE */
1470 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1472 child_bits &= ~MDS_INODELOCK_LAYOUT;
1473 LASSERT(child_bits != 0);
1474 rc = mdt_object_lock(info, child, lhc,
1475 child_bits, MDT_CROSS_LOCK);
1480 rc = mdt_object_lock(info, child, lhc, child_bits,
1483 if (unlikely(rc != 0))
1484 GOTO(out_child, rc);
1487 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1488 /* Get MA_SOM attributes if update lock is given. */
1490 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1491 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1494 /* finally, we can get attr for child. */
1495 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1496 rc = mdt_getattr_internal(info, child, ma_need);
1497 if (unlikely(rc != 0)) {
1498 mdt_object_unlock(info, child, lhc, 1);
1500 /* Debugging code. */
1501 res_id = &lock->l_resource->lr_name;
1502 LDLM_DEBUG(lock, "Returning lock to client");
1503 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1504 &lock->l_resource->lr_name),
1505 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1506 PLDLMRES(lock->l_resource),
1507 PFID(mdt_object_fid(child)));
1508 if (mdt_object_exists(child) && !mdt_object_remote(child))
1509 mdt_pack_size2body(info, child);
1512 LDLM_LOCK_PUT(lock);
1516 mdt_object_put(info->mti_env, child);
1519 mdt_object_unlock(info, parent, lhp, 1);
1523 /* normal handler: should release the child lock */
1524 int mdt_getattr_name(struct tgt_session_info *tsi)
1526 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1527 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1528 struct mdt_body *reqbody;
1529 struct mdt_body *repbody;
1533 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1534 LASSERT(reqbody != NULL);
1535 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1536 LASSERT(repbody != NULL);
1538 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1539 repbody->eadatasize = 0;
1540 repbody->aclsize = 0;
1542 rc = mdt_init_ucred(info, reqbody);
1544 GOTO(out_shrink, rc);
1546 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1547 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1548 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1549 lhc->mlh_reg_lh.cookie = 0;
1551 mdt_exit_ucred(info);
1554 mdt_client_compatibility(info);
1555 rc2 = mdt_fix_reply(info);
1558 mdt_thread_info_fini(info);
1562 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1563 void *karg, void *uarg);
1565 int mdt_set_info(struct tgt_session_info *tsi)
1567 struct ptlrpc_request *req = tgt_ses_req(tsi);
1570 int keylen, vallen, rc = 0;
1574 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1576 DEBUG_REQ(D_HA, req, "no set_info key");
1577 RETURN(err_serious(-EFAULT));
1580 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1583 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1585 DEBUG_REQ(D_HA, req, "no set_info val");
1586 RETURN(err_serious(-EFAULT));
1589 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1592 /* Swab any part of val you need to here */
1593 if (KEY_IS(KEY_READ_ONLY)) {
1594 spin_lock(&req->rq_export->exp_lock);
1596 *exp_connect_flags_ptr(req->rq_export) |=
1599 *exp_connect_flags_ptr(req->rq_export) &=
1600 ~OBD_CONNECT_RDONLY;
1601 spin_unlock(&req->rq_export->exp_lock);
1602 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1603 struct changelog_setinfo *cs = val;
1605 if (vallen != sizeof(*cs)) {
1606 CERROR("%s: bad changelog_clear setinfo size %d\n",
1607 tgt_name(tsi->tsi_tgt), vallen);
1610 if (ptlrpc_req_need_swab(req)) {
1611 __swab64s(&cs->cs_recno);
1612 __swab32s(&cs->cs_id);
1615 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1623 int mdt_readpage(struct tgt_session_info *tsi)
1625 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1626 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1627 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1628 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1629 struct mdt_body *repbody;
1635 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1636 RETURN(err_serious(-ENOMEM));
1638 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1639 if (repbody == NULL || reqbody == NULL)
1640 RETURN(err_serious(-EFAULT));
1643 * prepare @rdpg before calling lower layers and transfer itself. Here
1644 * reqbody->size contains offset of where to start to read and
1645 * reqbody->nlink contains number bytes to read.
1647 rdpg->rp_hash = reqbody->size;
1648 if (rdpg->rp_hash != reqbody->size) {
1649 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1650 rdpg->rp_hash, reqbody->size);
1654 rdpg->rp_attrs = reqbody->mode;
1655 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1656 rdpg->rp_attrs |= LUDA_64BITHASH;
1657 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1658 exp_max_brw_size(tsi->tsi_exp));
1659 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1661 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1662 if (rdpg->rp_pages == NULL)
1665 for (i = 0; i < rdpg->rp_npages; ++i) {
1666 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1667 if (rdpg->rp_pages[i] == NULL)
1668 GOTO(free_rdpg, rc = -ENOMEM);
1671 /* call lower layers to fill allocated pages with directory data */
1672 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1674 GOTO(free_rdpg, rc);
1676 /* send pages to client */
1677 rc = tgt_sendpage(tsi, rdpg, rc);
1682 for (i = 0; i < rdpg->rp_npages; i++)
1683 if (rdpg->rp_pages[i] != NULL)
1684 __free_page(rdpg->rp_pages[i]);
1685 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1687 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1693 static int mdt_reint_internal(struct mdt_thread_info *info,
1694 struct mdt_lock_handle *lhc,
1697 struct req_capsule *pill = info->mti_pill;
1698 struct mdt_body *repbody;
1703 rc = mdt_reint_unpack(info, op);
1705 CERROR("Can't unpack reint, rc %d\n", rc);
1706 RETURN(err_serious(rc));
1709 /* for replay (no_create) lmm is not needed, client has it already */
1710 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1711 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1712 info->mti_rr.rr_eadatalen);
1714 /* llog cookies are always 0, the field is kept for compatibility */
1715 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1716 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1718 rc = req_capsule_server_pack(pill);
1720 CERROR("Can't pack response, rc %d\n", rc);
1721 RETURN(err_serious(rc));
1724 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1725 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1727 repbody->eadatasize = 0;
1728 repbody->aclsize = 0;
1731 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1733 /* for replay no cookkie / lmm need, because client have this already */
1734 if (info->mti_spec.no_create)
1735 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1736 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1738 rc = mdt_init_ucred_reint(info);
1740 GOTO(out_shrink, rc);
1742 rc = mdt_fix_attr_ucred(info, op);
1744 GOTO(out_ucred, rc = err_serious(rc));
1746 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1747 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1748 GOTO(out_ucred, rc);
1750 rc = mdt_reint_rec(info, lhc);
1753 mdt_exit_ucred(info);
1755 mdt_client_compatibility(info);
1756 rc2 = mdt_fix_reply(info);
1762 static long mdt_reint_opcode(struct ptlrpc_request *req,
1763 const struct req_format **fmt)
1765 struct mdt_device *mdt;
1766 struct mdt_rec_reint *rec;
1769 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1771 opc = rec->rr_opcode;
1772 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1773 if (opc < REINT_MAX && fmt[opc] != NULL)
1774 req_capsule_extend(&req->rq_pill, fmt[opc]);
1776 mdt = mdt_exp2dev(req->rq_export);
1777 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1778 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1779 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1780 opc = err_serious(-EFAULT);
1783 opc = err_serious(-EFAULT);
1788 int mdt_reint(struct tgt_session_info *tsi)
1792 static const struct req_format *reint_fmts[REINT_MAX] = {
1793 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1794 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1795 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1796 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1797 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1798 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1799 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1800 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1805 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1807 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1809 * No lock possible here from client to pass it to reint code
1812 rc = mdt_reint_internal(info, NULL, opc);
1813 mdt_thread_info_fini(info);
1818 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1822 /* this should sync the whole device */
1823 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1825 struct dt_device *dt = mdt->mdt_bottom;
1829 rc = dt->dd_ops->dt_sync(env, dt);
1833 /* this should sync this object */
1834 static int mdt_object_sync(struct mdt_thread_info *info)
1836 struct md_object *next;
1840 if (!mdt_object_exists(info->mti_object)) {
1841 CWARN("Non existing object "DFID"!\n",
1842 PFID(mdt_object_fid(info->mti_object)));
1845 next = mdt_object_child(info->mti_object);
1846 rc = mo_object_sync(info->mti_env, next);
1851 int mdt_sync(struct tgt_session_info *tsi)
1853 struct ptlrpc_request *req = tgt_ses_req(tsi);
1854 struct req_capsule *pill = tsi->tsi_pill;
1855 struct mdt_body *body;
1860 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1861 RETURN(err_serious(-ENOMEM));
1863 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1864 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1866 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1868 /* sync an object */
1869 rc = mdt_object_sync(info);
1871 const struct lu_fid *fid;
1872 struct lu_attr *la = &info->mti_attr.ma_attr;
1874 info->mti_attr.ma_need = MA_INODE;
1875 info->mti_attr.ma_valid = 0;
1876 rc = mdt_attr_get_complex(info, info->mti_object,
1879 body = req_capsule_server_get(pill,
1881 fid = mdt_object_fid(info->mti_object);
1882 mdt_pack_attr2body(info, body, la, fid);
1885 mdt_thread_info_fini(info);
1888 mdt_counter_incr(req, LPROC_MDT_SYNC);
1894 * Handle quota control requests to consult current usage/limit, but also
1895 * to configure quota enforcement
1897 int mdt_quotactl(struct tgt_session_info *tsi)
1899 struct obd_export *exp = tsi->tsi_exp;
1900 struct req_capsule *pill = tsi->tsi_pill;
1901 struct obd_quotactl *oqctl, *repoqc;
1903 struct mdt_device *mdt = mdt_exp2dev(exp);
1904 struct lu_device *qmt = mdt->mdt_qmt_dev;
1907 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1909 RETURN(err_serious(-EPROTO));
1911 rc = req_capsule_server_pack(pill);
1913 RETURN(err_serious(rc));
1915 switch (oqctl->qc_cmd) {
1917 case LUSTRE_Q_INVALIDATE:
1918 case LUSTRE_Q_FINVALIDATE:
1922 /* deprecated, not used any more */
1923 RETURN(-EOPNOTSUPP);
1924 /* master quotactl */
1930 RETURN(-EOPNOTSUPP);
1931 /* slave quotactl */
1936 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1940 /* map uid/gid for remote client */
1942 if (exp_connect_rmtclient(exp)) {
1943 struct lustre_idmap_table *idmap;
1945 idmap = exp->exp_mdt_data.med_idmap;
1947 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1948 oqctl->qc_cmd != Q_GETINFO))
1951 if (oqctl->qc_type == USRQUOTA)
1952 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1954 else if (oqctl->qc_type == GRPQUOTA)
1955 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1960 if (id == CFS_IDMAP_NOTFOUND) {
1961 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1966 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1968 RETURN(err_serious(-EFAULT));
1970 if (oqctl->qc_id != id)
1971 swap(oqctl->qc_id, id);
1973 switch (oqctl->qc_cmd) {
1979 /* forward quotactl request to QMT */
1980 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
1985 /* slave quotactl */
1986 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
1991 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1995 if (oqctl->qc_id != id)
1996 swap(oqctl->qc_id, id);
2002 /** clone llog ctxt from child (mdd)
2003 * This allows remote llog (replicator) access.
2004 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2005 * context was originally set up, or we can handle them directly.
2006 * I choose the latter, but that means I need any llog
2007 * contexts set up by child to be accessable by the mdt. So we clone the
2008 * context into our context list here.
2010 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2013 struct md_device *next = mdt->mdt_child;
2014 struct llog_ctxt *ctxt;
2017 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2020 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2021 if (rc || ctxt == NULL) {
2025 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2027 CERROR("Can't set mdt ctxt %d\n", rc);
2032 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2033 struct mdt_device *mdt, int idx)
2035 struct llog_ctxt *ctxt;
2037 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2040 /* Put once for the get we just did, and once for the clone */
2041 llog_ctxt_put(ctxt);
2042 llog_ctxt_put(ctxt);
2047 * sec context handlers
2049 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2053 rc = mdt_handle_idmap(tsi);
2055 struct ptlrpc_request *req = tgt_ses_req(tsi);
2058 opc = lustre_msg_get_opc(req->rq_reqmsg);
2059 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2060 sptlrpc_svc_ctx_invalidate(req);
2063 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2069 * quota request handlers
2071 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2073 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2074 struct lu_device *qmt = mdt->mdt_qmt_dev;
2079 RETURN(err_serious(-EOPNOTSUPP));
2081 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2085 struct mdt_object *mdt_object_new(const struct lu_env *env,
2086 struct mdt_device *d,
2087 const struct lu_fid *f)
2089 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2090 struct lu_object *o;
2091 struct mdt_object *m;
2094 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2095 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2096 if (unlikely(IS_ERR(o)))
2097 m = (struct mdt_object *)o;
2103 struct mdt_object *mdt_object_find(const struct lu_env *env,
2104 struct mdt_device *d,
2105 const struct lu_fid *f)
2107 struct lu_object *o;
2108 struct mdt_object *m;
2111 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2112 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2113 if (unlikely(IS_ERR(o)))
2114 m = (struct mdt_object *)o;
2122 * Asyncronous commit for mdt device.
2124 * Pass asynchonous commit call down the MDS stack.
2126 * \param env environment
2127 * \param mdt the mdt device
2129 static void mdt_device_commit_async(const struct lu_env *env,
2130 struct mdt_device *mdt)
2132 struct dt_device *dt = mdt->mdt_bottom;
2135 rc = dt->dd_ops->dt_commit_async(env, dt);
2136 if (unlikely(rc != 0))
2137 CWARN("async commit start failed with rc = %d", rc);
2141 * Mark the lock as "synchonous".
2143 * Mark the lock to deffer transaction commit to the unlock time.
2145 * \param lock the lock to mark as "synchonous"
2147 * \see mdt_is_lock_sync
2148 * \see mdt_save_lock
2150 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2152 lock->l_ast_data = (void*)1;
2156 * Check whehter the lock "synchonous" or not.
2158 * \param lock the lock to check
2159 * \retval 1 the lock is "synchonous"
2160 * \retval 0 the lock isn't "synchronous"
2162 * \see mdt_set_lock_sync
2163 * \see mdt_save_lock
2165 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2167 return lock->l_ast_data != NULL;
2171 * Blocking AST for mdt locks.
2173 * Starts transaction commit if in case of COS lock conflict or
2174 * deffers such a commit to the mdt_save_lock.
2176 * \param lock the lock which blocks a request or cancelling lock
2177 * \param desc unused
2178 * \param data unused
2179 * \param flag indicates whether this cancelling or blocking callback
2181 * \see ldlm_blocking_ast_nocheck
2183 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2184 void *data, int flag)
2186 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2187 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2191 if (flag == LDLM_CB_CANCELING)
2193 lock_res_and_lock(lock);
2194 if (lock->l_blocking_ast != mdt_blocking_ast) {
2195 unlock_res_and_lock(lock);
2198 if (mdt_cos_is_enabled(mdt) &&
2199 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2200 lock->l_blocking_lock != NULL &&
2201 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2202 mdt_set_lock_sync(lock);
2204 rc = ldlm_blocking_ast_nocheck(lock);
2206 /* There is no lock conflict if l_blocking_lock == NULL,
2207 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2208 * when the last reference to a local lock was released */
2209 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2212 rc = lu_env_init(&env, LCT_LOCAL);
2213 if (unlikely(rc != 0))
2214 CWARN("lu_env initialization failed with rc = %d,"
2215 "cannot start asynchronous commit\n", rc);
2217 mdt_device_commit_async(&env, mdt);
2223 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2224 void *data, int flag)
2226 struct lustre_handle lockh;
2230 case LDLM_CB_BLOCKING:
2231 ldlm_lock2handle(lock, &lockh);
2232 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2234 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2238 case LDLM_CB_CANCELING:
2239 LDLM_DEBUG(lock, "Revoke remote lock\n");
2247 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2248 struct mdt_object *o, struct lustre_handle *lh,
2249 ldlm_mode_t mode, __u64 ibits)
2251 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2252 ldlm_policy_data_t *policy = &mti->mti_policy;
2256 LASSERT(mdt_object_remote(o));
2258 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2260 memset(einfo, 0, sizeof(*einfo));
2261 einfo->ei_type = LDLM_IBITS;
2262 einfo->ei_mode = mode;
2263 einfo->ei_cb_bl = mdt_md_blocking_ast;
2264 einfo->ei_cb_cp = ldlm_completion_ast;
2266 memset(policy, 0, sizeof(*policy));
2267 policy->l_inodebits.bits = ibits;
2269 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2274 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2275 struct mdt_lock_handle *lh, __u64 ibits,
2276 bool nonblock, int locality)
2278 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2279 ldlm_policy_data_t *policy = &info->mti_policy;
2280 struct ldlm_res_id *res_id = &info->mti_res_id;
2285 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2286 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2287 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2288 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2290 if (mdt_object_remote(o)) {
2291 if (locality == MDT_CROSS_LOCK) {
2292 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2293 MDS_INODELOCK_LAYOUT);
2294 ibits |= MDS_INODELOCK_LOOKUP;
2297 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2298 MDS_INODELOCK_LAYOUT)),
2299 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2300 mdt_obd_name(info->mti_mdt), ibits,
2301 PFID(mdt_object_fid(o)));
2302 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2304 /* No PDO lock on remote object */
2305 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2308 if (lh->mlh_type == MDT_PDO_LOCK) {
2309 /* check for exists after object is locked */
2310 if (mdt_object_exists(o) == 0) {
2311 /* Non-existent object shouldn't have PDO lock */
2314 /* Non-dir object shouldn't have PDO lock */
2315 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2320 memset(policy, 0, sizeof(*policy));
2321 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2323 dlmflags = LDLM_FL_ATOMIC_CB;
2325 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2328 * Take PDO lock on whole directory and build correct @res_id for lock
2329 * on part of directory.
2331 if (lh->mlh_pdo_hash != 0) {
2332 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2333 mdt_lock_pdo_mode(info, o, lh);
2334 if (lh->mlh_pdo_mode != LCK_NL) {
2336 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2337 * is never going to be sent to client and we do not
2338 * want it slowed down due to possible cancels.
2340 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2341 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2342 policy, res_id, dlmflags,
2343 &info->mti_exp->exp_handle.h_cookie);
2349 * Finish res_id initializing by name hash marking part of
2350 * directory which is taking modification.
2352 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2355 policy->l_inodebits.bits = ibits;
2358 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2359 * going to be sent to client. If it is - mdt_intent_policy() path will
2360 * fix it up and turn FL_LOCAL flag off.
2362 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2363 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2364 &info->mti_exp->exp_handle.h_cookie);
2366 mdt_object_unlock(info, o, lh, 1);
2367 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2368 lh->mlh_pdo_hash != 0 &&
2369 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2370 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2376 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2377 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2379 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2382 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2383 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2385 struct mdt_lock_handle tmp = *lh;
2388 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2396 * Save a lock within request object.
2398 * Keep the lock referenced until whether client ACK or transaction
2399 * commit happens or release the lock immediately depending on input
2400 * parameters. If COS is ON, a write lock is converted to COS lock
2403 * \param info thead info object
2404 * \param h lock handle
2405 * \param mode lock mode
2406 * \param decref force immediate lock releasing
2409 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2410 ldlm_mode_t mode, int decref)
2414 if (lustre_handle_is_used(h)) {
2415 if (decref || !info->mti_has_trans ||
2416 !(mode & (LCK_PW | LCK_EX))){
2417 mdt_fid_unlock(h, mode);
2419 struct mdt_device *mdt = info->mti_mdt;
2420 struct ldlm_lock *lock = ldlm_handle2lock(h);
2421 struct ptlrpc_request *req = mdt_info_req(info);
2424 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2426 /* there is no request if mdt_object_unlock() is called
2427 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2428 if (likely(req != NULL)) {
2429 CDEBUG(D_HA, "request = %p reply state = %p"
2430 " transno = "LPD64"\n", req,
2431 req->rq_reply_state, req->rq_transno);
2432 if (mdt_cos_is_enabled(mdt)) {
2434 ldlm_lock_downgrade(lock, LCK_COS);
2437 ptlrpc_save_lock(req, h, mode, no_ack);
2439 ldlm_lock_decref(h, mode);
2441 if (mdt_is_lock_sync(lock)) {
2442 CDEBUG(D_HA, "found sync-lock,"
2443 " async commit started\n");
2444 mdt_device_commit_async(info->mti_env,
2447 LDLM_LOCK_PUT(lock);
2456 * Unlock mdt object.
2458 * Immeditely release the regular lock and the PDO lock or save the
2459 * lock in reqeuest and keep them referenced until client ACK or
2460 * transaction commit.
2462 * \param info thread info object
2463 * \param o mdt object
2464 * \param lh mdt lock handle referencing regular and PDO locks
2465 * \param decref force immediate lock releasing
2467 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2468 struct mdt_lock_handle *lh, int decref)
2472 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2473 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2475 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2476 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2481 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2482 const struct lu_fid *f,
2483 struct mdt_lock_handle *lh,
2486 struct mdt_object *o;
2488 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2492 rc = mdt_object_lock(info, o, lh, ibits,
2495 mdt_object_put(info->mti_env, o);
2502 void mdt_object_unlock_put(struct mdt_thread_info * info,
2503 struct mdt_object * o,
2504 struct mdt_lock_handle *lh,
2507 mdt_object_unlock(info, o, lh, decref);
2508 mdt_object_put(info->mti_env, o);
2512 * Generic code handling requests that have struct mdt_body passed in:
2514 * - extract mdt_body from request and save it in @info, if present;
2516 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2519 * - if HABEO_CORPUS flag is set for this request type check whether object
2520 * actually exists on storage (lu_object_exists()).
2523 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2525 const struct mdt_body *body;
2526 struct mdt_object *obj;
2527 const struct lu_env *env;
2528 struct req_capsule *pill;
2532 env = info->mti_env;
2533 pill = info->mti_pill;
2535 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2539 if (!(body->valid & OBD_MD_FLID))
2542 if (!fid_is_sane(&body->fid1)) {
2543 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2548 * Do not get size or any capa fields before we check that request
2549 * contains capa actually. There are some requests which do not, for
2550 * instance MDS_IS_SUBDIR.
2552 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2553 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2554 mdt_set_capainfo(info, 0, &body->fid1,
2555 req_capsule_client_get(pill, &RMF_CAPA1));
2557 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2559 if ((flags & HABEO_CORPUS) &&
2560 !mdt_object_exists(obj)) {
2561 mdt_object_put(env, obj);
2562 /* for capability renew ENOENT will be handled in
2564 if (body->valid & OBD_MD_FLOSSCAPA)
2569 info->mti_object = obj;
2578 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2580 struct req_capsule *pill = info->mti_pill;
2584 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2585 rc = mdt_body_unpack(info, flags);
2589 if (rc == 0 && (flags & HABEO_REFERO)) {
2591 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2592 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2593 info->mti_body->eadatasize);
2594 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2595 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2598 rc = req_capsule_server_pack(pill);
2603 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2605 struct md_device *next = m->mdt_child;
2607 return next->md_ops->mdo_init_capa_ctxt(env, next,
2608 m->mdt_lut.lut_mds_capa,
2609 m->mdt_capa_timeout,
2614 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2616 lh->mlh_type = MDT_NUL_LOCK;
2617 lh->mlh_reg_lh.cookie = 0ull;
2618 lh->mlh_reg_mode = LCK_MINMODE;
2619 lh->mlh_pdo_lh.cookie = 0ull;
2620 lh->mlh_pdo_mode = LCK_MINMODE;
2621 lh->mlh_rreg_lh.cookie = 0ull;
2622 lh->mlh_rreg_mode = LCK_MINMODE;
2625 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2627 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2628 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2632 * Initialize fields of struct mdt_thread_info. Other fields are left in
2633 * uninitialized state, because it's too expensive to zero out whole
2634 * mdt_thread_info (> 1K) on each request arrival.
2636 void mdt_thread_info_init(struct ptlrpc_request *req,
2637 struct mdt_thread_info *info)
2641 info->mti_pill = &req->rq_pill;
2644 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2645 mdt_lock_handle_init(&info->mti_lh[i]);
2647 /* mdt device: it can be NULL while CONNECT */
2648 if (req->rq_export) {
2649 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2650 info->mti_exp = req->rq_export;
2652 info->mti_mdt = NULL;
2653 info->mti_env = req->rq_svc_thread->t_env;
2654 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2656 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2657 info->mti_big_buf = LU_BUF_NULL;
2658 info->mti_body = NULL;
2659 info->mti_object = NULL;
2660 info->mti_dlm_req = NULL;
2661 info->mti_has_trans = 0;
2662 info->mti_cross_ref = 0;
2663 info->mti_opdata = 0;
2664 info->mti_big_lmm_used = 0;
2666 /* To not check for split by default. */
2667 info->mti_spec.no_create = 0;
2668 info->mti_spec.sp_rm_entry = 0;
2671 void mdt_thread_info_fini(struct mdt_thread_info *info)
2675 if (info->mti_object != NULL) {
2676 mdt_object_put(info->mti_env, info->mti_object);
2677 info->mti_object = NULL;
2680 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2681 mdt_lock_handle_fini(&info->mti_lh[i]);
2682 info->mti_env = NULL;
2683 info->mti_pill = NULL;
2684 info->mti_exp = NULL;
2686 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2687 lu_buf_free(&info->mti_big_buf);
2690 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2692 struct mdt_thread_info *mti;
2693 struct lustre_capa *lc;
2695 mti = mdt_th_info(tsi->tsi_env);
2696 LASSERT(mti != NULL);
2698 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2699 if (tsi->tsi_corpus != NULL) {
2700 struct req_capsule *pill = tsi->tsi_pill;
2702 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2703 lu_object_get(tsi->tsi_corpus);
2706 * XXX: must be part of tgt_mdt_body_unpack but moved here
2707 * due to mdt_set_capainfo().
2709 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2710 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2711 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2712 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2715 mti->mti_body = tsi->tsi_mdt_body;
2716 mti->mti_dlm_req = tsi->tsi_dlm_req;
2721 int mdt_tgt_connect(struct tgt_session_info *tsi)
2723 struct ptlrpc_request *req = tgt_ses_req(tsi);
2728 rc = tgt_connect(tsi);
2732 rc = mdt_init_idmap(tsi);
2737 obd_disconnect(class_export_get(req->rq_export));
2756 static int mdt_intent_getattr(enum mdt_it_code opcode,
2757 struct mdt_thread_info *info,
2758 struct ldlm_lock **,
2761 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2762 struct mdt_thread_info *info,
2763 struct ldlm_lock **lockp,
2766 static int mdt_intent_layout(enum mdt_it_code opcode,
2767 struct mdt_thread_info *info,
2768 struct ldlm_lock **,
2770 static int mdt_intent_reint(enum mdt_it_code opcode,
2771 struct mdt_thread_info *info,
2772 struct ldlm_lock **,
2775 static struct mdt_it_flavor {
2776 const struct req_format *it_fmt;
2778 int (*it_act)(enum mdt_it_code ,
2779 struct mdt_thread_info *,
2780 struct ldlm_lock **,
2783 } mdt_it_flavor[] = {
2785 .it_fmt = &RQF_LDLM_INTENT,
2786 /*.it_flags = HABEO_REFERO,*/
2788 .it_act = mdt_intent_reint,
2789 .it_reint = REINT_OPEN
2792 .it_fmt = &RQF_LDLM_INTENT,
2794 * OCREAT is not a MUTABOR request as if the file
2796 * We do the extra check of OBD_CONNECT_RDONLY in
2797 * mdt_reint_open() when we really need to create
2801 .it_act = mdt_intent_reint,
2802 .it_reint = REINT_OPEN
2805 .it_fmt = &RQF_LDLM_INTENT,
2806 .it_flags = MUTABOR,
2807 .it_act = mdt_intent_reint,
2808 .it_reint = REINT_CREATE
2810 [MDT_IT_GETATTR] = {
2811 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2812 .it_flags = HABEO_REFERO,
2813 .it_act = mdt_intent_getattr
2815 [MDT_IT_READDIR] = {
2821 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2822 .it_flags = HABEO_REFERO,
2823 .it_act = mdt_intent_getattr
2826 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2827 .it_flags = MUTABOR,
2829 .it_reint = REINT_UNLINK
2833 .it_flags = MUTABOR,
2836 [MDT_IT_GETXATTR] = {
2837 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2838 .it_flags = HABEO_CORPUS,
2839 .it_act = mdt_intent_getxattr
2842 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2844 .it_act = mdt_intent_layout
2848 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2849 struct ldlm_lock **lockp,
2850 struct ldlm_lock *new_lock,
2851 struct mdt_lock_handle *lh,
2854 struct ptlrpc_request *req = mdt_info_req(info);
2855 struct ldlm_lock *lock = *lockp;
2858 * Get new lock only for cases when possible resent did not find any
2861 if (new_lock == NULL)
2862 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2864 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2865 lh->mlh_reg_lh.cookie = 0;
2869 LASSERTF(new_lock != NULL,
2870 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2873 * If we've already given this lock to a client once, then we should
2874 * have no readers or writers. Otherwise, we should have one reader
2875 * _or_ writer ref (which will be zeroed below) before returning the
2878 if (new_lock->l_export == req->rq_export) {
2879 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2881 LASSERT(new_lock->l_export == NULL);
2882 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2887 if (new_lock->l_export == req->rq_export) {
2889 * Already gave this to the client, which means that we
2890 * reconstructed a reply.
2892 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2894 lh->mlh_reg_lh.cookie = 0;
2895 RETURN(ELDLM_LOCK_REPLACED);
2899 * Fixup the lock to be given to the client.
2901 lock_res_and_lock(new_lock);
2902 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2903 * possible blocking AST. */
2904 while (new_lock->l_readers > 0) {
2905 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2906 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2907 new_lock->l_readers--;
2909 while (new_lock->l_writers > 0) {
2910 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2911 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2912 new_lock->l_writers--;
2915 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2916 new_lock->l_blocking_ast = lock->l_blocking_ast;
2917 new_lock->l_completion_ast = lock->l_completion_ast;
2918 new_lock->l_remote_handle = lock->l_remote_handle;
2919 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2921 unlock_res_and_lock(new_lock);
2923 cfs_hash_add(new_lock->l_export->exp_lock_hash,
2924 &new_lock->l_remote_handle,
2925 &new_lock->l_exp_hash);
2927 LDLM_LOCK_RELEASE(new_lock);
2928 lh->mlh_reg_lh.cookie = 0;
2930 RETURN(ELDLM_LOCK_REPLACED);
2933 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2934 struct ldlm_lock *new_lock,
2935 struct ldlm_lock **old_lock,
2936 struct mdt_lock_handle *lh)
2938 struct ptlrpc_request *req = mdt_info_req(info);
2939 struct obd_export *exp = req->rq_export;
2940 struct lustre_handle remote_hdl;
2941 struct ldlm_request *dlmreq;
2942 struct ldlm_lock *lock;
2944 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2947 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2948 remote_hdl = dlmreq->lock_handle[0];
2950 /* In the function below, .hs_keycmp resolves to
2951 * ldlm_export_lock_keycmp() */
2952 /* coverity[overrun-buffer-val] */
2953 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
2955 if (lock != new_lock) {
2956 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2957 lh->mlh_reg_mode = lock->l_granted_mode;
2959 LDLM_DEBUG(lock, "Restoring lock cookie");
2960 DEBUG_REQ(D_DLMTRACE, req,
2961 "restoring lock cookie "LPX64,
2962 lh->mlh_reg_lh.cookie);
2964 *old_lock = LDLM_LOCK_GET(lock);
2965 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2969 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2973 * If the xid matches, then we know this is a resent request, and allow
2974 * it. (It's probably an OPEN, for which we don't send a lock.
2976 if (req_xid_is_last(req))
2980 * This remote handle isn't enqueued, so we never received or processed
2981 * this request. Clear MSG_RESENT, because it can be handled like any
2982 * normal request now.
2984 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2986 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2990 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2991 struct mdt_thread_info *info,
2992 struct ldlm_lock **lockp,
2995 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2996 struct ldlm_reply *ldlm_rep = NULL;
3000 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3001 * (for the resend case) or a new lock. Below we will use it to
3002 * replace the original lock.
3004 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3005 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3006 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3007 rc = mdt_object_lock(info, info->mti_object, lhc,
3008 MDS_INODELOCK_XATTR,
3014 grc = mdt_getxattr(info);
3016 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3018 if (mdt_info_req(info)->rq_repmsg != NULL)
3019 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3020 if (ldlm_rep == NULL)
3021 RETURN(err_serious(-EFAULT));
3023 ldlm_rep->lock_policy_res2 = grc;
3028 static int mdt_intent_getattr(enum mdt_it_code opcode,
3029 struct mdt_thread_info *info,
3030 struct ldlm_lock **lockp,
3033 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3034 struct ldlm_lock *new_lock = NULL;
3036 struct ldlm_reply *ldlm_rep;
3037 struct ptlrpc_request *req;
3038 struct mdt_body *reqbody;
3039 struct mdt_body *repbody;
3043 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3046 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3049 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3050 repbody->eadatasize = 0;
3051 repbody->aclsize = 0;
3055 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3057 case MDT_IT_GETATTR:
3058 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3062 CERROR("Unsupported intent (%d)\n", opcode);
3063 GOTO(out_shrink, rc = -EINVAL);
3066 rc = mdt_init_ucred(info, reqbody);
3068 GOTO(out_shrink, rc);
3070 req = info->mti_pill->rc_req;
3071 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3072 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3074 /* Get lock from request for possible resent case. */
3075 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3077 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3078 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3080 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3081 ldlm_rep->lock_policy_res2 = 0;
3082 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3083 ldlm_rep->lock_policy_res2) {
3084 lhc->mlh_reg_lh.cookie = 0ull;
3085 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3088 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3091 mdt_exit_ucred(info);
3093 mdt_client_compatibility(info);
3094 rc2 = mdt_fix_reply(info);
3100 static int mdt_intent_layout(enum mdt_it_code opcode,
3101 struct mdt_thread_info *info,
3102 struct ldlm_lock **lockp,
3105 struct layout_intent *layout;
3107 struct mdt_object *obj = NULL;
3108 struct md_object *child = NULL;
3112 if (opcode != MDT_IT_LAYOUT) {
3113 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3118 fid = &info->mti_tmp_fid2;
3119 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3121 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3123 RETURN(PTR_ERR(obj));
3125 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3126 child = mdt_object_child(obj);
3128 /* get the length of lsm */
3129 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3132 if (rc > info->mti_mdt->mdt_max_mdsize)
3133 info->mti_mdt->mdt_max_mdsize = rc;
3136 mdt_object_put(info->mti_env, obj);
3138 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3139 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3140 ldlm_lvbo_size(*lockp));
3141 rc = req_capsule_server_pack(info->mti_pill);
3145 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3146 LASSERT(layout != NULL);
3147 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3148 /* return to normal ldlm handling */
3151 CERROR("%s: Unsupported layout intent (%d)\n",
3152 mdt_obd_name(info->mti_mdt), layout->li_opc);
3156 static int mdt_intent_reint(enum mdt_it_code opcode,
3157 struct mdt_thread_info *info,
3158 struct ldlm_lock **lockp,
3161 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3162 struct ldlm_reply *rep = NULL;
3166 static const struct req_format *intent_fmts[REINT_MAX] = {
3167 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3168 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3173 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3177 if (mdt_it_flavor[opcode].it_reint != opc) {
3178 CERROR("Reint code %ld doesn't match intent: %d\n",
3180 RETURN(err_serious(-EPROTO));
3183 /* Get lock from request for possible resent case. */
3184 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3186 rc = mdt_reint_internal(info, lhc, opc);
3188 /* Check whether the reply has been packed successfully. */
3189 if (mdt_info_req(info)->rq_repmsg != NULL)
3190 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3192 RETURN(err_serious(-EFAULT));
3194 /* MDC expects this in any case */
3196 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3198 /* the open lock or the lock for cross-ref object should be
3199 * returned to the client */
3200 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3201 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3202 rep->lock_policy_res2 = 0;
3203 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3207 rep->lock_policy_res2 = clear_serious(rc);
3209 if (rep->lock_policy_res2 == -ENOENT &&
3210 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3211 rep->lock_policy_res2 = 0;
3213 if (rc == -ENOTCONN || rc == -ENODEV ||
3214 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3216 * If it is the disconnect error (ENODEV & ENOCONN), the error
3217 * will be returned by rq_status, and client at ptlrpc layer
3218 * will detect this, then disconnect, reconnect the import
3219 * immediately, instead of impacting the following the rpc.
3221 lhc->mlh_reg_lh.cookie = 0ull;
3225 * For other cases, the error will be returned by intent.
3226 * and client will retrieve the result from intent.
3229 * FIXME: when open lock is finished, that should be
3232 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3233 LASSERTF(rc == 0, "Error occurred but lock handle "
3234 "is still in use, rc = %d\n", rc);
3235 rep->lock_policy_res2 = 0;
3236 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3239 lhc->mlh_reg_lh.cookie = 0ull;
3240 RETURN(ELDLM_LOCK_ABORTED);
3245 static int mdt_intent_code(long itcode)
3253 case IT_OPEN|IT_CREAT:
3260 rc = MDT_IT_READDIR;
3263 rc = MDT_IT_GETATTR;
3275 rc = MDT_IT_GETXATTR;
3280 case IT_QUOTA_DQACQ:
3285 CERROR("Unknown intent opcode: %ld\n", itcode);
3292 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3293 struct ldlm_lock **lockp, __u64 flags)
3295 struct req_capsule *pill;
3296 struct mdt_it_flavor *flv;
3301 opc = mdt_intent_code(itopc);
3305 pill = info->mti_pill;
3307 if (opc == MDT_IT_QUOTA) {
3308 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3311 RETURN(-EOPNOTSUPP);
3313 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3314 /* pass the request to quota master */
3315 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3316 mdt_info_req(info), lockp,
3321 flv = &mdt_it_flavor[opc];
3322 if (flv->it_fmt != NULL)
3323 req_capsule_extend(pill, flv->it_fmt);
3325 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3327 struct ptlrpc_request *req = mdt_info_req(info);
3328 if (flv->it_flags & MUTABOR &&
3329 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3332 if (rc == 0 && flv->it_act != NULL) {
3333 struct ldlm_reply *rep;
3335 /* execute policy */
3336 rc = flv->it_act(opc, info, lockp, flags);
3338 /* Check whether the reply has been packed successfully. */
3339 if (mdt_info_req(info)->rq_repmsg != NULL) {
3340 rep = req_capsule_server_get(info->mti_pill,
3342 rep->lock_policy_res2 =
3343 ptlrpc_status_hton(rep->lock_policy_res2);
3351 static int mdt_intent_policy(struct ldlm_namespace *ns,
3352 struct ldlm_lock **lockp, void *req_cookie,
3353 ldlm_mode_t mode, __u64 flags, void *data)
3355 struct tgt_session_info *tsi;
3356 struct mdt_thread_info *info;
3357 struct ptlrpc_request *req = req_cookie;
3358 struct ldlm_intent *it;
3359 struct req_capsule *pill;
3364 LASSERT(req != NULL);
3366 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3368 info = tsi2mdt_info(tsi);
3369 LASSERT(info != NULL);
3370 pill = info->mti_pill;
3371 LASSERT(pill->rc_req == req);
3373 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3374 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3375 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3377 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3381 /* Lock without inodebits makes no sense and will oops
3382 * later in ldlm. Let's check it now to see if we have
3383 * ibits corrupted somewhere in mdt_intent_opc().
3384 * The case for client miss to set ibits has been
3385 * processed by others. */
3386 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3387 lr_type == LDLM_IBITS,
3388 info->mti_dlm_req->lock_desc.\
3389 l_policy_data.l_inodebits.bits != 0));
3391 rc = err_serious(-EFAULT);
3393 /* No intent was provided */
3394 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3395 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3396 rc = req_capsule_server_pack(pill);
3398 rc = err_serious(rc);
3400 mdt_thread_info_fini(info);
3404 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3406 struct seq_server_site *ss = mdt_seq_site(mdt);
3408 if (ss->ss_node_id == 0)
3411 if (ss->ss_client_seq != NULL) {
3412 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3413 ss->ss_client_seq->lcs_exp = NULL;
3416 if (ss->ss_server_fld != NULL) {
3417 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3418 ss->ss_server_fld->lsf_control_exp = NULL;
3422 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3424 struct seq_server_site *ss = mdt_seq_site(mdt);
3429 if (ss->ss_server_seq == NULL)
3430 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3435 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3437 mdt_seq_fini_cli(mdt);
3438 mdt_deregister_seq_exp(mdt);
3440 return seq_site_fini(env, mdt_seq_site(mdt));
3444 * It will retrieve its FLDB entries from MDT0, and it only happens
3445 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3446 * and it needs to refresh FLDB from the MDT0.
3448 static int mdt_register_lwp_callback(void *data)
3451 struct mdt_device *mdt = data;
3452 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3456 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3458 if (!likely(fld->lsf_new))
3461 rc = lu_env_init(&env, LCT_MD_THREAD);
3463 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3467 rc = fld_update_from_controller(&env, fld);
3469 CERROR("%s: cannot update controller: rc = %d\n",
3470 mdt_obd_name(mdt), rc);
3478 static int mdt_register_seq_exp(struct mdt_device *mdt)
3480 struct seq_server_site *ss = mdt_seq_site(mdt);
3481 char *lwp_name = NULL;
3484 if (ss->ss_node_id == 0)
3487 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3488 if (lwp_name == NULL)
3489 GOTO(out_free, rc = -ENOMEM);
3491 rc = tgt_name2lwpname(mdt_obd_name(mdt), lwp_name);
3495 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3500 rc = lustre_register_lwp_item(lwp_name,
3501 &ss->ss_server_fld->lsf_control_exp,
3502 mdt_register_lwp_callback, mdt);
3504 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3505 ss->ss_client_seq->lcs_exp = NULL;
3509 if (lwp_name != NULL)
3510 OBD_FREE(lwp_name, MAX_OBD_NAME);
3516 * Init client sequence manager which is used by local MDS to talk to sequence
3517 * controller on remote node.
3519 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3521 struct seq_server_site *ss = mdt_seq_site(mdt);
3526 /* check if this is adding the first MDC and controller is not yet
3528 OBD_ALLOC_PTR(ss->ss_client_seq);
3529 if (ss->ss_client_seq == NULL)
3532 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3533 if (prefix == NULL) {
3534 OBD_FREE_PTR(ss->ss_client_seq);
3535 ss->ss_client_seq = NULL;
3539 /* Note: seq_client_fini will be called in seq_site_fini */
3540 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3541 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3542 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3544 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3546 OBD_FREE_PTR(ss->ss_client_seq);
3547 ss->ss_client_seq = NULL;
3551 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3556 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3558 struct seq_server_site *ss;
3562 ss = mdt_seq_site(mdt);
3563 /* init sequence controller server(MDT0) */
3564 if (ss->ss_node_id == 0) {
3565 OBD_ALLOC_PTR(ss->ss_control_seq);
3566 if (ss->ss_control_seq == NULL)
3569 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3570 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3573 GOTO(out_seq_fini, rc);
3576 /* Init normal sequence server */
3577 OBD_ALLOC_PTR(ss->ss_server_seq);
3578 if (ss->ss_server_seq == NULL)
3579 GOTO(out_seq_fini, rc = -ENOMEM);
3581 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3582 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3584 GOTO(out_seq_fini, rc);
3586 /* init seq client for seq server to talk to seq controller(MDT0) */
3587 rc = mdt_seq_init_cli(env, mdt);
3589 GOTO(out_seq_fini, rc);
3591 if (ss->ss_node_id != 0)
3592 /* register controler export through lwp */
3593 rc = mdt_register_seq_exp(mdt);
3598 mdt_seq_fini(env, mdt);
3606 static int mdt_fld_fini(const struct lu_env *env,
3607 struct mdt_device *m)
3609 struct seq_server_site *ss = mdt_seq_site(m);
3612 if (ss && ss->ss_server_fld) {
3613 fld_server_fini(env, ss->ss_server_fld);
3614 OBD_FREE_PTR(ss->ss_server_fld);
3615 ss->ss_server_fld = NULL;
3621 static int mdt_fld_init(const struct lu_env *env,
3623 struct mdt_device *m)
3625 struct seq_server_site *ss;
3629 ss = mdt_seq_site(m);
3631 OBD_ALLOC_PTR(ss->ss_server_fld);
3632 if (ss->ss_server_fld == NULL)
3633 RETURN(rc = -ENOMEM);
3635 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3638 OBD_FREE_PTR(ss->ss_server_fld);
3639 ss->ss_server_fld = NULL;
3646 static void mdt_stack_pre_fini(const struct lu_env *env,
3647 struct mdt_device *m, struct lu_device *top)
3649 struct obd_device *obd;
3650 struct lustre_cfg_bufs *bufs;
3651 struct lustre_cfg *lcfg;
3652 struct mdt_thread_info *info;
3657 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3658 LASSERT(info != NULL);
3660 bufs = &info->mti_u.bufs;
3662 LASSERT(m->mdt_child_exp);
3663 LASSERT(m->mdt_child_exp->exp_obd);
3664 obd = m->mdt_child_exp->exp_obd;
3666 /* process cleanup, pass mdt obd name to get obd umount flags */
3667 /* XXX: this is needed because all layers are referenced by
3668 * objects (some of them are pinned by osd, for example *
3669 * the proper solution should be a model where object used
3670 * by osd only doesn't have mdt/mdd slices -bzzz */
3671 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3672 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3673 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3675 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3678 top->ld_ops->ldo_process_config(env, top, lcfg);
3679 lustre_cfg_free(lcfg);
3683 static void mdt_stack_fini(const struct lu_env *env,
3684 struct mdt_device *m, struct lu_device *top)
3686 struct obd_device *obd = mdt2obd_dev(m);
3687 struct lustre_cfg_bufs *bufs;
3688 struct lustre_cfg *lcfg;
3689 struct mdt_thread_info *info;
3693 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3694 LASSERT(info != NULL);
3696 lu_dev_del_linkage(top->ld_site, top);
3698 lu_site_purge(env, top->ld_site, -1);
3700 bufs = &info->mti_u.bufs;
3701 /* process cleanup, pass mdt obd name to get obd umount flags */
3702 /* another purpose is to let all layers to release their objects */
3703 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3708 lustre_cfg_bufs_set_string(bufs, 1, flags);
3709 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3711 CERROR("Cannot alloc lcfg!\n");
3715 top->ld_ops->ldo_process_config(env, top, lcfg);
3716 lustre_cfg_free(lcfg);
3718 lu_site_purge(env, top->ld_site, -1);
3720 m->mdt_child = NULL;
3721 m->mdt_bottom = NULL;
3723 obd_disconnect(m->mdt_child_exp);
3724 m->mdt_child_exp = NULL;
3726 obd_disconnect(m->mdt_bottom_exp);
3727 m->mdt_child_exp = NULL;
3730 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3731 const char *next, struct obd_export **exp)
3733 struct obd_connect_data *data = NULL;
3734 struct obd_device *obd;
3738 OBD_ALLOC_PTR(data);
3740 GOTO(out, rc = -ENOMEM);
3742 obd = class_name2obd(next);
3744 CERROR("%s: can't locate next device: %s\n",
3745 mdt_obd_name(m), next);
3746 GOTO(out, rc = -ENOTCONN);
3749 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3750 data->ocd_version = LUSTRE_VERSION_CODE;
3752 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3754 CERROR("%s: cannot connect to next dev %s (%d)\n",
3755 mdt_obd_name(m), next, rc);
3765 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3766 struct lustre_cfg *cfg)
3768 char *dev = lustre_cfg_string(cfg, 0);
3769 int rc, name_size, uuid_size;
3770 char *name, *uuid, *p;
3771 struct lustre_cfg_bufs *bufs;
3772 struct lustre_cfg *lcfg;
3773 struct obd_device *obd;
3774 struct lustre_profile *lprof;
3775 struct lu_site *site;
3778 /* in 1.8 we had the only device in the stack - MDS.
3779 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3780 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3781 * to generate names and setup MDT, MDD. MDT will be using
3782 * generated name to connect to MDD. for MDD the next device
3783 * will be LOD with name taken from so called "profile" which
3784 * is generated by mount_option line
3786 * 1.8 MGS generates config. commands like this:
3787 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3788 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3789 * 2.0 MGS generates config. commands like this:
3790 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3791 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3792 * 3:lustre-MDT0000-mdtlov 4:f
3794 * we generate MDD name from MDT one, just replacing T with D
3796 * after all the preparations, the logical equivalent will be
3797 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3798 * 3:lustre-MDT0000-mdtlov 4:f
3799 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3800 * 3:lustre-MDD0000 4:f
3802 * notice we build the stack from down to top: MDD first, then MDT */
3804 name_size = MAX_OBD_NAME;
3805 uuid_size = MAX_OBD_NAME;
3807 OBD_ALLOC(name, name_size);
3808 OBD_ALLOC(uuid, uuid_size);
3809 if (name == NULL || uuid == NULL)
3810 GOTO(cleanup_mem, rc = -ENOMEM);
3812 OBD_ALLOC_PTR(bufs);
3814 GOTO(cleanup_mem, rc = -ENOMEM);
3817 p = strstr(name, "-MDT");
3819 GOTO(free_bufs, rc = -ENOMEM);
3822 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3824 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3825 if (lprof == NULL || lprof->lp_dt == NULL) {
3826 CERROR("can't find the profile: %s\n",
3827 lustre_cfg_string(cfg, 0));
3828 GOTO(free_bufs, rc = -EINVAL);
3831 lustre_cfg_bufs_reset(bufs, name);
3832 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3833 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3834 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3836 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3838 GOTO(free_bufs, rc = -ENOMEM);
3840 rc = class_attach(lcfg);
3842 GOTO(lcfg_cleanup, rc);
3844 obd = class_name2obd(name);
3846 CERROR("Can not find obd %s (%s in config)\n",
3847 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
3848 GOTO(class_detach, rc = -EINVAL);
3851 lustre_cfg_free(lcfg);
3853 lustre_cfg_bufs_reset(bufs, name);
3854 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3855 lustre_cfg_bufs_set_string(bufs, 2, dev);
3856 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3858 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3860 rc = class_setup(obd, lcfg);
3862 GOTO(class_detach, rc);
3864 /* connect to MDD we just setup */
3865 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
3867 GOTO(class_detach, rc);
3869 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
3871 LASSERT(mdt_lu_site(mdt) == NULL);
3872 mdt->mdt_lu_dev.ld_site = site;
3873 site->ls_top_dev = &mdt->mdt_lu_dev;
3874 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
3877 /* now connect to bottom OSD */
3878 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
3879 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
3881 GOTO(class_detach, rc);
3883 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
3886 rc = lu_env_refill((struct lu_env *)env);
3888 CERROR("Failure to refill session: '%d'\n", rc);
3890 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
3895 class_detach(obd, lcfg);
3897 lustre_cfg_free(lcfg);
3902 OBD_FREE(name, name_size);
3904 OBD_FREE(uuid, uuid_size);
3908 /* setup quota master target on MDT0 */
3909 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
3910 struct lustre_cfg *cfg)
3912 struct obd_device *obd;
3913 char *dev = lustre_cfg_string(cfg, 0);
3914 char *qmtname, *uuid, *p;
3915 struct lustre_cfg_bufs *bufs;
3916 struct lustre_cfg *lcfg;
3917 struct lustre_profile *lprof;
3918 struct obd_connect_data *data;
3922 LASSERT(mdt->mdt_qmt_exp == NULL);
3923 LASSERT(mdt->mdt_qmt_dev == NULL);
3925 /* quota master is on MDT0 only for now */
3926 if (mdt->mdt_seq_site.ss_node_id != 0)
3929 /* MGS generates config commands which look as follows:
3930 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3931 * 3:lustre-MDT0000-mdtlov 4:f
3933 * We generate the QMT name from the MDT one, just replacing MD with QM
3934 * after all the preparations, the logical equivalent will be:
3935 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
3936 * 3:lustre-MDT0000-osd 4:f */
3937 OBD_ALLOC(qmtname, MAX_OBD_NAME);
3938 OBD_ALLOC(uuid, UUID_MAX);
3939 OBD_ALLOC_PTR(bufs);
3940 OBD_ALLOC_PTR(data);
3941 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
3942 GOTO(cleanup_mem, rc = -ENOMEM);
3944 strcpy(qmtname, dev);
3945 p = strstr(qmtname, "-MDT");
3947 GOTO(cleanup_mem, rc = -ENOMEM);
3948 /* replace MD with QM */
3952 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
3954 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3955 if (lprof == NULL || lprof->lp_dt == NULL) {
3956 CERROR("can't find profile for %s\n",
3957 lustre_cfg_string(cfg, 0));
3958 GOTO(cleanup_mem, rc = -EINVAL);
3961 lustre_cfg_bufs_reset(bufs, qmtname);
3962 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
3963 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3964 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3966 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3968 GOTO(cleanup_mem, rc = -ENOMEM);
3970 rc = class_attach(lcfg);
3972 GOTO(lcfg_cleanup, rc);
3974 obd = class_name2obd(qmtname);
3976 CERROR("Can not find obd %s (%s in config)\n", qmtname,
3977 lustre_cfg_string(cfg, 0));
3978 GOTO(class_detach, rc = -EINVAL);
3981 lustre_cfg_free(lcfg);
3983 lustre_cfg_bufs_reset(bufs, qmtname);
3984 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3985 lustre_cfg_bufs_set_string(bufs, 2, dev);
3987 /* for quota, the next device should be the OSD device */
3988 lustre_cfg_bufs_set_string(bufs, 3,
3989 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
3991 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3993 rc = class_setup(obd, lcfg);
3995 GOTO(class_detach, rc);
3997 mdt->mdt_qmt_dev = obd->obd_lu_dev;
3999 /* configure local quota objects */
4000 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4004 GOTO(class_cleanup, rc);
4006 /* connect to quota master target */
4007 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4008 data->ocd_version = LUSTRE_VERSION_CODE;
4009 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4012 CERROR("cannot connect to quota master device %s (%d)\n",
4014 GOTO(class_cleanup, rc);
4020 class_manual_cleanup(obd);
4021 mdt->mdt_qmt_dev = NULL;
4025 class_detach(obd, lcfg);
4027 lustre_cfg_free(lcfg);
4032 OBD_FREE(qmtname, MAX_OBD_NAME);
4034 OBD_FREE(uuid, UUID_MAX);
4040 /* Shutdown quota master target associated with mdt */
4041 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4045 if (mdt->mdt_qmt_exp == NULL)
4047 LASSERT(mdt->mdt_qmt_dev != NULL);
4049 /* the qmt automatically shuts down when the mdt disconnects */
4050 obd_disconnect(mdt->mdt_qmt_exp);
4051 mdt->mdt_qmt_exp = NULL;
4052 mdt->mdt_qmt_dev = NULL;
4056 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4057 * for now. This will be removed along with converting rest of MDT code
4058 * to use tgt_session_info */
4059 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4061 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4064 rc = mdt_getxattr(info);
4066 mdt_thread_info_fini(info);
4070 static struct tgt_handler mdt_tgt_handlers[] = {
4071 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4072 0, MDS_CONNECT, mdt_tgt_connect,
4073 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4074 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4075 0, MDS_DISCONNECT, tgt_disconnect,
4076 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4077 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4078 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4079 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4080 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4081 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4082 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4083 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4085 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4086 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4087 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4088 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4089 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4091 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4092 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4093 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4094 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4095 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4097 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4098 mdt_hsm_ct_register),
4099 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4100 mdt_hsm_ct_unregister),
4101 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4103 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4105 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4106 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4108 TGT_MDT_HDL(HABEO_CORPUS|HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
4112 static struct tgt_handler mdt_sec_ctx_ops[] = {
4113 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4114 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4115 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4118 static struct tgt_handler mdt_quota_ops[] = {
4119 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4122 static struct tgt_opc_slice mdt_common_slice[] = {
4124 .tos_opc_start = MDS_FIRST_OPC,
4125 .tos_opc_end = MDS_LAST_OPC,
4126 .tos_hs = mdt_tgt_handlers
4129 .tos_opc_start = OBD_FIRST_OPC,
4130 .tos_opc_end = OBD_LAST_OPC,
4131 .tos_hs = tgt_obd_handlers
4134 .tos_opc_start = LDLM_FIRST_OPC,
4135 .tos_opc_end = LDLM_LAST_OPC,
4136 .tos_hs = tgt_dlm_handlers
4139 .tos_opc_start = SEC_FIRST_OPC,
4140 .tos_opc_end = SEC_LAST_OPC,
4141 .tos_hs = mdt_sec_ctx_ops
4144 .tos_opc_start = UPDATE_OBJ,
4145 .tos_opc_end = UPDATE_LAST_OPC,
4146 .tos_hs = tgt_out_handlers
4149 .tos_opc_start = FLD_FIRST_OPC,
4150 .tos_opc_end = FLD_LAST_OPC,
4151 .tos_hs = fld_handlers
4154 .tos_opc_start = SEQ_FIRST_OPC,
4155 .tos_opc_end = SEQ_LAST_OPC,
4156 .tos_hs = seq_handlers
4159 .tos_opc_start = QUOTA_DQACQ,
4160 .tos_opc_end = QUOTA_LAST_OPC,
4161 .tos_hs = mdt_quota_ops
4164 .tos_opc_start = LLOG_FIRST_OPC,
4165 .tos_opc_end = LLOG_LAST_OPC,
4166 .tos_hs = tgt_llog_handlers
4174 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4176 struct md_device *next = m->mdt_child;
4177 struct lu_device *d = &m->mdt_lu_dev;
4178 struct obd_device *obd = mdt2obd_dev(m);
4181 target_recovery_fini(obd);
4183 ping_evictor_stop();
4185 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4187 if (m->mdt_opts.mo_coordinator)
4188 mdt_hsm_cdt_stop(m);
4190 mdt_hsm_cdt_fini(m);
4192 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4193 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4194 obd_exports_barrier(obd);
4195 obd_zombie_barrier();
4199 tgt_fini(env, &m->mdt_lut);
4200 mdt_fs_cleanup(env, m);
4201 upcall_cache_cleanup(m->mdt_identity_cache);
4202 m->mdt_identity_cache = NULL;
4204 if (m->mdt_namespace != NULL) {
4205 ldlm_namespace_free(m->mdt_namespace, NULL,
4206 d->ld_obd->obd_force);
4207 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4210 mdt_quota_fini(env, m);
4212 cfs_free_nidlist(&m->mdt_nosquash_nids);
4213 if (m->mdt_nosquash_str) {
4214 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4215 m->mdt_nosquash_str = NULL;
4216 m->mdt_nosquash_strlen = 0;
4219 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4221 mdt_seq_fini(env, m);
4222 mdt_fld_fini(env, m);
4224 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4225 cfs_timer_disarm(&m->mdt_ck_timer);
4226 mdt_ck_thread_stop(m);
4231 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4233 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4235 server_put_mount(mdt_obd_name(m));
4240 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4242 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4243 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4245 struct mdt_thread_info *info;
4246 struct obd_device *obd;
4247 const char *dev = lustre_cfg_string(cfg, 0);
4248 const char *num = lustre_cfg_string(cfg, 2);
4249 struct lustre_mount_info *lmi = NULL;
4250 struct lustre_sb_info *lsi;
4252 struct seq_server_site *ss_site;
4253 const char *identity_upcall = "NONE";
4254 struct md_device *next;
4260 lu_device_init(&m->mdt_lu_dev, ldt);
4262 * Environment (env) might be missing mdt_thread_key values at that
4263 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4266 * Usually device allocation path doesn't use module key values, but
4267 * mdt has to do a lot of work here, so allocate key value.
4269 rc = lu_env_refill((struct lu_env *)env);
4273 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4274 LASSERT(info != NULL);
4276 obd = class_name2obd(dev);
4277 LASSERT(obd != NULL);
4279 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4281 m->mdt_som_conf = 0;
4283 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4285 /* default is coordinator off, it is started through conf_param
4287 m->mdt_opts.mo_coordinator = 0;
4289 lmi = server_get_mount(dev);
4291 CERROR("Cannot get mount info for %s!\n", dev);
4294 lsi = s2lsi(lmi->lmi_sb);
4295 /* CMD is supported only in IAM mode */
4297 node_id = simple_strtol(num, NULL, 10);
4298 obd->u.obt.obt_magic = OBT_MAGIC;
4301 spin_lock_init(&m->mdt_ioepoch_lock);
4302 m->mdt_capa_timeout = CAPA_TIMEOUT;
4303 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4304 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4305 m->mdt_squash_uid = 0;
4306 m->mdt_squash_gid = 0;
4307 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4308 m->mdt_nosquash_str = NULL;
4309 m->mdt_nosquash_strlen = 0;
4310 init_rwsem(&m->mdt_squash_sem);
4311 spin_lock_init(&m->mdt_osfs_lock);
4312 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4313 m->mdt_enable_remote_dir = 0;
4314 m->mdt_enable_remote_dir_gid = 0;
4316 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4317 m->mdt_lu_dev.ld_obd = obd;
4318 /* Set this lu_device to obd for error handling purposes. */
4319 obd->obd_lu_dev = &m->mdt_lu_dev;
4321 /* init the stack */
4322 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4324 CERROR("%s: Can't init device stack, rc %d\n",
4325 mdt_obd_name(m), rc);
4330 ss_site = mdt_seq_site(m);
4331 s->ld_seq_site = ss_site;
4334 /* set server index */
4335 ss_site->ss_node_id = node_id;
4337 /* failover is the default
4338 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4339 * assumed whose ss_node_id == 0 XXX
4341 obd->obd_replayable = 1;
4342 /* No connection accepted until configurations will finish */
4343 obd->obd_no_conn = 1;
4345 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4346 char *str = lustre_cfg_string(cfg, 4);
4347 if (strchr(str, 'n')) {
4348 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4349 obd->obd_replayable = 0;
4353 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4355 GOTO(err_fini_stack, rc);
4357 rc = mdt_seq_init(env, m);
4359 GOTO(err_fini_fld, rc);
4361 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4362 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4363 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4364 LDLM_NAMESPACE_SERVER,
4365 LDLM_NAMESPACE_GREEDY,
4367 if (m->mdt_namespace == NULL)
4368 GOTO(err_fini_seq, rc = -ENOMEM);
4370 m->mdt_namespace->ns_lvbp = m;
4371 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4373 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4374 /* set obd_namespace for compatibility with old code */
4375 obd->obd_namespace = m->mdt_namespace;
4377 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4379 rc = mdt_hsm_cdt_init(m);
4381 CERROR("%s: error initializing coordinator, rc %d\n",
4382 mdt_obd_name(m), rc);
4383 GOTO(err_free_ns, rc);
4386 rc = mdt_ck_thread_start(m);
4388 GOTO(err_free_hsm, rc);
4390 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4391 OBD_FAIL_MDS_ALL_REQUEST_NET,
4392 OBD_FAIL_MDS_ALL_REPLY_NET);
4396 rc = mdt_fs_setup(env, m, obd, lsi);
4400 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4402 next = m->mdt_child;
4403 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4406 GOTO(err_fs_cleanup, rc);
4408 if (mntopts & MNTOPT_USERXATTR)
4409 m->mdt_opts.mo_user_xattr = 1;
4411 m->mdt_opts.mo_user_xattr = 0;
4413 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4415 GOTO(err_fs_cleanup, rc);
4417 if (mntopts & MNTOPT_ACL)
4418 m->mdt_opts.mo_acl = 1;
4420 m->mdt_opts.mo_acl = 0;
4422 /* XXX: to support suppgid for ACL, we enable identity_upcall
4423 * by default, otherwise, maybe got unexpected -EACCESS. */
4424 if (m->mdt_opts.mo_acl)
4425 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4427 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4429 &mdt_identity_upcall_cache_ops);
4430 if (IS_ERR(m->mdt_identity_cache)) {
4431 rc = PTR_ERR(m->mdt_identity_cache);
4432 m->mdt_identity_cache = NULL;
4433 GOTO(err_fs_cleanup, rc);
4436 rc = mdt_procfs_init(m, dev);
4438 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4439 GOTO(err_recovery, rc);
4442 rc = mdt_quota_init(env, m, cfg);
4444 GOTO(err_procfs, rc);
4446 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4447 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4448 "mdt_ldlm_client", m->mdt_ldlm_client);
4450 ping_evictor_start();
4452 /* recovery will be started upon mdt_prepare()
4453 * when the whole stack is complete and ready
4454 * to serve the requests */
4456 mdt_init_capa_ctxt(env, m);
4458 /* Reduce the initial timeout on an MDS because it doesn't need such
4459 * a long timeout as an OST does. Adaptive timeouts will adjust this
4460 * value appropriately. */
4461 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4462 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4468 target_recovery_fini(obd);
4469 upcall_cache_cleanup(m->mdt_identity_cache);
4470 m->mdt_identity_cache = NULL;
4472 mdt_fs_cleanup(env, m);
4474 tgt_fini(env, &m->mdt_lut);
4476 cfs_timer_disarm(&m->mdt_ck_timer);
4477 mdt_ck_thread_stop(m);
4479 mdt_hsm_cdt_fini(m);
4481 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4482 obd->obd_namespace = m->mdt_namespace = NULL;
4484 mdt_seq_fini(env, m);
4486 mdt_fld_fini(env, m);
4488 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4491 server_put_mount(dev);
4495 /* For interoperability, the left element is old parameter, the right one
4496 * is the new version of the parameter, if some parameter is deprecated,
4497 * the new version should be set as NULL. */
4498 static struct cfg_interop_param mdt_interop_param[] = {
4499 { "mdt.group_upcall", NULL },
4500 { "mdt.quota_type", NULL },
4501 { "mdd.quota_type", NULL },
4502 { "mdt.rootsquash", "mdt.root_squash" },
4503 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4507 /* used by MGS to process specific configurations */
4508 static int mdt_process_config(const struct lu_env *env,
4509 struct lu_device *d, struct lustre_cfg *cfg)
4511 struct mdt_device *m = mdt_dev(d);
4512 struct md_device *md_next = m->mdt_child;
4513 struct lu_device *next = md2lu_dev(md_next);
4517 switch (cfg->lcfg_command) {
4519 struct lprocfs_static_vars lvars;
4520 struct obd_device *obd = d->ld_obd;
4522 /* For interoperability */
4523 struct cfg_interop_param *ptr = NULL;
4524 struct lustre_cfg *old_cfg = NULL;
4527 param = lustre_cfg_string(cfg, 1);
4528 if (param == NULL) {
4529 CERROR("param is empty\n");
4534 ptr = class_find_old_param(param, mdt_interop_param);
4536 if (ptr->new_param == NULL) {
4538 CWARN("For interoperability, skip this %s."
4539 " It is obsolete.\n", ptr->old_param);
4543 CWARN("Found old param %s, changed it to %s.\n",
4544 ptr->old_param, ptr->new_param);
4547 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4554 lprocfs_mdt_init_vars(&lvars);
4555 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4557 if (rc > 0 || rc == -ENOSYS) {
4558 /* is it an HSM var ? */
4559 rc = class_process_proc_param(PARAM_HSM,
4560 hsm_cdt_get_proc_vars(),
4562 if (rc > 0 || rc == -ENOSYS)
4563 /* we don't understand; pass it on */
4564 rc = next->ld_ops->ldo_process_config(env, next,
4568 if (old_cfg != NULL)
4569 lustre_cfg_free(cfg);
4574 /* others are passed further */
4575 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4581 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4582 const struct lu_object_header *hdr,
4583 struct lu_device *d)
4585 struct mdt_object *mo;
4589 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
4591 struct lu_object *o;
4592 struct lu_object_header *h;
4595 h = &mo->mot_header;
4596 lu_object_header_init(h);
4597 lu_object_init(o, h, d);
4598 lu_object_add_top(h, o);
4599 o->lo_ops = &mdt_obj_ops;
4600 mutex_init(&mo->mot_ioepoch_mutex);
4601 mutex_init(&mo->mot_lov_mutex);
4602 init_rwsem(&mo->mot_open_sem);
4608 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4609 const struct lu_object_conf *unused)
4611 struct mdt_device *d = mdt_dev(o->lo_dev);
4612 struct lu_device *under;
4613 struct lu_object *below;
4617 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4618 PFID(lu_object_fid(o)));
4620 under = &d->mdt_child->md_lu_dev;
4621 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4622 if (below != NULL) {
4623 lu_object_add(o, below);
4630 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4632 struct mdt_object *mo = mdt_obj(o);
4633 struct lu_object_header *h;
4637 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4638 PFID(lu_object_fid(o)));
4640 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4641 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4644 lu_object_header_fini(h);
4645 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4650 static int mdt_object_print(const struct lu_env *env, void *cookie,
4651 lu_printer_t p, const struct lu_object *o)
4653 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4654 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4655 "flags="LPX64", epochcount=%d, writecount=%d)",
4656 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4657 mdto->mot_ioepoch_count, mdto->mot_writecount);
4660 static int mdt_prepare(const struct lu_env *env,
4661 struct lu_device *pdev,
4662 struct lu_device *cdev)
4664 struct mdt_device *mdt = mdt_dev(cdev);
4665 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4666 struct obd_device *obd = cdev->ld_obd;
4667 struct lfsck_start_param lsp;
4674 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4678 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4682 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4686 lsp.lsp_start = NULL;
4687 lsp.lsp_namespace = mdt->mdt_namespace;
4688 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
4689 OBD_IOC_START_LFSCK,
4692 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
4693 mdt_obd_name(mdt), rc);
4697 if (mdt->mdt_seq_site.ss_node_id == 0) {
4698 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4699 &mdt->mdt_md_root_fid);
4704 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4705 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4706 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4707 LASSERT(obd->obd_no_conn);
4708 spin_lock(&obd->obd_dev_lock);
4709 obd->obd_no_conn = 0;
4710 spin_unlock(&obd->obd_dev_lock);
4712 if (obd->obd_recovering == 0)
4713 mdt_postrecov(env, mdt);
4718 const struct lu_device_operations mdt_lu_ops = {
4719 .ldo_object_alloc = mdt_object_alloc,
4720 .ldo_process_config = mdt_process_config,
4721 .ldo_prepare = mdt_prepare,
4724 static const struct lu_object_operations mdt_obj_ops = {
4725 .loo_object_init = mdt_object_init,
4726 .loo_object_free = mdt_object_free,
4727 .loo_object_print = mdt_object_print
4730 static int mdt_obd_set_info_async(const struct lu_env *env,
4731 struct obd_export *exp,
4732 __u32 keylen, void *key,
4733 __u32 vallen, void *val,
4734 struct ptlrpc_request_set *set)
4740 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4741 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4749 * Match client and server connection feature flags.
4751 * Compute the compatibility flags for a connection request based on
4752 * features mutually supported by client and server.
4754 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4755 * must not be updated here, otherwise a partially initialized value may
4756 * be exposed. After the connection request is successfully processed,
4757 * the top-level MDT connect request handler atomically updates the export
4758 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4759 * reply. \see mdt_connect().
4761 * \param exp the obd_export associated with this client/target pair
4762 * \param mdt the target device for the connection
4763 * \param data stores data for this connect request
4766 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4767 * \retval -EBADE client and server feature requirements are incompatible
4769 static int mdt_connect_internal(struct obd_export *exp,
4770 struct mdt_device *mdt,
4771 struct obd_connect_data *data)
4773 LASSERT(data != NULL);
4775 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4776 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4778 /* If no known bits (which should not happen, probably,
4779 as everybody should support LOOKUP and UPDATE bits at least)
4780 revert to compat mode with plain locks. */
4781 if (!data->ocd_ibits_known &&
4782 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4783 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4785 if (!mdt->mdt_opts.mo_acl)
4786 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4788 if (!mdt->mdt_opts.mo_user_xattr)
4789 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4791 if (!mdt->mdt_som_conf)
4792 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4794 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4795 data->ocd_brw_size = min(data->ocd_brw_size,
4796 (__u32)MD_MAX_BRW_SIZE);
4797 if (data->ocd_brw_size == 0) {
4798 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4799 " ocd_version: %x ocd_grant: %d "
4800 "ocd_index: %u ocd_brw_size is "
4801 "unexpectedly zero, network data "
4802 "corruption? Refusing connection of this"
4805 exp->exp_client_uuid.uuid,
4806 exp, data->ocd_connect_flags, data->ocd_version,
4807 data->ocd_grant, data->ocd_index);
4812 /* NB: Disregard the rule against updating
4813 * exp_connect_data.ocd_connect_flags in this case, since
4814 * tgt_client_new() needs to know if this is a lightweight
4815 * connection, and it is safe to expose this flag before
4816 * connection processing completes. */
4817 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4818 spin_lock(&exp->exp_lock);
4819 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4820 spin_unlock(&exp->exp_lock);
4823 data->ocd_version = LUSTRE_VERSION_CODE;
4825 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4826 CWARN("%s: MDS requires FID support, but client not\n",
4831 if (mdt->mdt_som_conf &&
4832 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4833 OBD_CONNECT_MDS_MDS |
4834 OBD_CONNECT_SOM))) {
4835 CWARN("%s: MDS has SOM enabled, but client does not support "
4836 "it\n", mdt_obd_name(mdt));
4840 if (OCD_HAS_FLAG(data, PINGLESS)) {
4841 if (ptlrpc_pinger_suppress_pings()) {
4842 spin_lock(&exp->exp_obd->obd_dev_lock);
4843 list_del_init(&exp->exp_obd_chain_timed);
4844 spin_unlock(&exp->exp_obd->obd_dev_lock);
4846 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4850 data->ocd_max_easize = mdt->mdt_max_ea_size;
4855 /* mds_connect copy */
4856 static int mdt_obd_connect(const struct lu_env *env,
4857 struct obd_export **exp, struct obd_device *obd,
4858 struct obd_uuid *cluuid,
4859 struct obd_connect_data *data,
4862 struct obd_export *lexp;
4863 struct lustre_handle conn = { 0 };
4864 struct mdt_device *mdt;
4868 LASSERT(env != NULL);
4869 if (!exp || !obd || !cluuid)
4872 mdt = mdt_dev(obd->obd_lu_dev);
4875 * first, check whether the stack is ready to handle requests
4876 * XXX: probably not very appropriate method is used now
4877 * at some point we should find a better one
4879 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
4880 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
4881 rc = obd_get_info(env, mdt->mdt_child_exp,
4882 sizeof(KEY_OSP_CONNECTED),
4883 KEY_OSP_CONNECTED, NULL, NULL, NULL);
4886 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
4889 rc = class_connect(&conn, obd, cluuid);
4893 lexp = class_conn2export(&conn);
4894 LASSERT(lexp != NULL);
4896 rc = mdt_connect_internal(lexp, mdt, data);
4898 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
4901 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
4902 rc = tgt_client_new(env, lexp);
4904 mdt_export_stats_init(obd, lexp, localdata);
4908 class_disconnect(lexp);
4917 static int mdt_obd_reconnect(const struct lu_env *env,
4918 struct obd_export *exp, struct obd_device *obd,
4919 struct obd_uuid *cluuid,
4920 struct obd_connect_data *data,
4926 if (exp == NULL || obd == NULL || cluuid == NULL)
4929 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4931 mdt_export_stats_init(obd, exp, localdata);
4936 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
4937 struct mdt_thread_info *info,
4938 struct mdt_file_data *mfd)
4940 struct lu_context ses;
4944 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
4949 lu_context_enter(&ses);
4951 mdt_ucred(info)->uc_valid = UCRED_OLD;
4952 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
4954 lu_context_exit(&ses);
4955 lu_context_fini(&ses);
4961 static int mdt_export_cleanup(struct obd_export *exp)
4963 struct mdt_export_data *med = &exp->exp_mdt_data;
4964 struct obd_device *obd = exp->exp_obd;
4965 struct mdt_device *mdt;
4966 struct mdt_thread_info *info;
4968 CFS_LIST_HEAD(closing_list);
4969 struct mdt_file_data *mfd, *n;
4973 spin_lock(&med->med_open_lock);
4974 while (!cfs_list_empty(&med->med_open_head)) {
4975 cfs_list_t *tmp = med->med_open_head.next;
4976 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
4978 /* Remove mfd handle so it can't be found again.
4979 * We are consuming the mfd_list reference here. */
4980 class_handle_unhash(&mfd->mfd_handle);
4981 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
4983 spin_unlock(&med->med_open_lock);
4984 mdt = mdt_dev(obd->obd_lu_dev);
4985 LASSERT(mdt != NULL);
4987 rc = lu_env_init(&env, LCT_MD_THREAD);
4991 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4992 LASSERT(info != NULL);
4993 memset(info, 0, sizeof *info);
4994 info->mti_env = &env;
4995 info->mti_mdt = mdt;
4996 info->mti_exp = exp;
4998 if (!cfs_list_empty(&closing_list)) {
4999 struct md_attr *ma = &info->mti_attr;
5001 /* Close any open files (which may also cause orphan unlinking). */
5002 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5003 cfs_list_del_init(&mfd->mfd_list);
5004 ma->ma_need = ma->ma_valid = 0;
5006 /* This file is being closed due to an eviction, it
5007 * could have been modified and now dirty regarding to
5008 * HSM archive, check this!
5009 * The logic here is to mark a file dirty if there's a
5010 * chance it was dirtied before the client was evicted,
5011 * so that we don't have to wait for a release attempt
5012 * before finding out the file was actually dirty and
5013 * fail the release. Aggressively marking it dirty here
5014 * will cause the policy engine to attempt to
5015 * re-archive it; when rearchiving, we can compare the
5016 * current version to the HSM data_version and make the
5017 * archive request into a noop if it's not actually
5020 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5021 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5023 /* Don't unlink orphan on failover umount, LU-184 */
5024 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5025 ma->ma_valid = MA_FLAGS;
5026 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5028 mdt_mfd_close(info, mfd);
5031 info->mti_mdt = NULL;
5032 /* cleanup client slot early */
5033 /* Do not erase record for recoverable client. */
5034 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5035 tgt_client_del(&env, exp);
5041 static int mdt_obd_disconnect(struct obd_export *exp)
5047 class_export_get(exp);
5049 rc = server_disconnect_export(exp);
5051 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5053 rc = mdt_export_cleanup(exp);
5054 class_export_put(exp);
5058 /* FIXME: Can we avoid using these two interfaces? */
5059 static int mdt_init_export(struct obd_export *exp)
5061 struct mdt_export_data *med = &exp->exp_mdt_data;
5065 CFS_INIT_LIST_HEAD(&med->med_open_head);
5066 spin_lock_init(&med->med_open_lock);
5067 mutex_init(&med->med_idmap_mutex);
5068 med->med_idmap = NULL;
5069 spin_lock(&exp->exp_lock);
5070 exp->exp_connecting = 1;
5071 spin_unlock(&exp->exp_lock);
5073 /* self-export doesn't need client data and ldlm initialization */
5074 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5075 &exp->exp_client_uuid)))
5078 rc = tgt_client_alloc(exp);
5082 rc = ldlm_init_export(exp);
5089 tgt_client_free(exp);
5091 CERROR("%s: Failed to initialize export: rc = %d\n",
5092 exp->exp_obd->obd_name, rc);
5096 static int mdt_destroy_export(struct obd_export *exp)
5100 if (exp_connect_rmtclient(exp))
5101 mdt_cleanup_idmap(&exp->exp_mdt_data);
5103 target_destroy_export(exp);
5104 /* destroy can be called from failed obd_setup, so
5105 * checking uuid is safer than obd_self_export */
5106 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5107 &exp->exp_client_uuid)))
5110 ldlm_destroy_export(exp);
5111 tgt_client_free(exp);
5113 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5114 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5119 /** The maximum depth that fid2path() will search.
5120 * This is limited only because we want to store the fids for
5121 * historical path lookup purposes.
5123 #define MAX_PATH_DEPTH 100
5125 /** mdt_path() lookup structure. */
5126 struct path_lookup_info {
5127 __u64 pli_recno; /**< history point */
5128 __u64 pli_currec; /**< current record */
5129 struct lu_fid pli_fid;
5130 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5131 struct mdt_object *pli_mdt_obj;
5132 char *pli_path; /**< full path */
5134 int pli_linkno; /**< which hardlink to follow */
5135 int pli_fidcount; /**< number of \a pli_fids */
5138 static int mdt_links_read(struct mdt_thread_info *info,
5139 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5143 LASSERT(ldata->ld_buf->lb_buf != NULL);
5145 if (!mdt_object_exists(mdt_obj))
5148 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5149 ldata->ld_buf, XATTR_NAME_LINK);
5150 if (rc == -ERANGE) {
5151 /* Buf was too small, figure out what we need. */
5152 lu_buf_free(ldata->ld_buf);
5153 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5154 ldata->ld_buf, XATTR_NAME_LINK);
5157 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5158 if (ldata->ld_buf->lb_buf == NULL)
5160 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5161 ldata->ld_buf, XATTR_NAME_LINK);
5166 return linkea_init(ldata);
5169 static int mdt_path_current(struct mdt_thread_info *info,
5170 struct path_lookup_info *pli)
5172 struct mdt_device *mdt = info->mti_mdt;
5173 struct mdt_object *mdt_obj;
5174 struct link_ea_header *leh;
5175 struct link_ea_entry *lee;
5176 struct lu_name *tmpname = &info->mti_name;
5177 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5178 struct lu_buf *buf = &info->mti_big_buf;
5181 struct linkea_data ldata = { 0 };
5185 /* temp buffer for path element, the buffer will be finally freed
5186 * in mdt_thread_info_fini */
5187 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5188 if (buf->lb_buf == NULL)
5192 ptr = pli->pli_path + pli->pli_pathlen - 1;
5195 pli->pli_fidcount = 0;
5196 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5198 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5199 * so checking root_fid can only happen on MDT0. */
5200 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5201 &pli->pli_fids[pli->pli_fidcount])) {
5202 mdt_obj = mdt_object_find(info->mti_env, mdt,
5203 &pli->pli_fids[pli->pli_fidcount]);
5204 if (IS_ERR(mdt_obj))
5205 GOTO(out, rc = PTR_ERR(mdt_obj));
5206 if (mdt_object_remote(mdt_obj)) {
5207 mdt_object_put(info->mti_env, mdt_obj);
5208 GOTO(remote_out, rc = -EREMOTE);
5210 if (!mdt_object_exists(mdt_obj)) {
5211 mdt_object_put(info->mti_env, mdt_obj);
5212 GOTO(out, rc = -ENOENT);
5215 rc = mdt_links_read(info, mdt_obj, &ldata);
5216 mdt_object_put(info->mti_env, mdt_obj);
5221 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5222 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5223 /* If set, use link #linkno for path lookup, otherwise use
5224 link #0. Only do this for the final path element. */
5225 if (pli->pli_fidcount == 0 &&
5226 pli->pli_linkno < leh->leh_reccount) {
5228 for (count = 0; count < pli->pli_linkno; count++) {
5229 lee = (struct link_ea_entry *)
5230 ((char *)lee + reclen);
5231 linkea_entry_unpack(lee, &reclen, tmpname,
5234 if (pli->pli_linkno < leh->leh_reccount - 1)
5235 /* indicate to user there are more links */
5239 /* Pack the name in the end of the buffer */
5240 ptr -= tmpname->ln_namelen;
5241 if (ptr - 1 <= pli->pli_path)
5242 GOTO(out, rc = -EOVERFLOW);
5243 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5246 /* Store the parent fid for historic lookup */
5247 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5248 GOTO(out, rc = -EOVERFLOW);
5249 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5253 ptr++; /* skip leading / */
5254 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5261 /* Returns the full path to this fid, as of changelog record recno. */
5262 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5263 char *path, int pathlen, __u64 *recno, int *linkno,
5266 struct mdt_device *mdt = info->mti_mdt;
5267 struct path_lookup_info *pli;
5275 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5284 pli->pli_mdt_obj = obj;
5285 pli->pli_recno = *recno;
5286 pli->pli_path = path;
5287 pli->pli_pathlen = pathlen;
5288 pli->pli_linkno = *linkno;
5290 /* Retry multiple times in case file is being moved */
5291 while (tries-- && rc == -EAGAIN)
5292 rc = mdt_path_current(info, pli);
5294 /* return the last resolved fids to the client, so the client will
5295 * build the left path on another MDT for remote object */
5296 *fid = pli->pli_fids[pli->pli_fidcount];
5298 *recno = pli->pli_currec;
5299 /* Return next link index to caller */
5300 *linkno = pli->pli_linkno;
5307 static int mdt_fid2path(struct mdt_thread_info *info,
5308 struct getinfo_fid2path *fp)
5310 struct mdt_device *mdt = info->mti_mdt;
5311 struct mdt_object *obj;
5315 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5316 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5318 if (!fid_is_sane(&fp->gf_fid))
5321 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5322 CWARN("%s: "DFID" is invalid, sequence should be "
5323 ">= "LPX64"\n", mdt_obd_name(mdt),
5324 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5328 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5329 if (obj == NULL || IS_ERR(obj)) {
5330 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5335 if (mdt_object_remote(obj))
5337 else if (!mdt_object_exists(obj))
5343 mdt_object_put(info->mti_env, obj);
5344 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5345 PFID(&fp->gf_fid), rc);
5349 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5350 &fp->gf_linkno, &fp->gf_fid);
5352 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5353 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5355 mdt_object_put(info->mti_env, obj);
5360 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5361 void *val, int vallen)
5363 struct getinfo_fid2path *fpout, *fpin;
5366 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5369 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5370 lustre_swab_fid2path(fpin);
5372 memcpy(fpout, fpin, sizeof(*fpin));
5373 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5376 rc = mdt_fid2path(info, fpout);
5380 int mdt_get_info(struct tgt_session_info *tsi)
5390 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5392 CDEBUG(D_IOCTL, "No GETINFO key");
5393 RETURN(err_serious(-EFAULT));
5395 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5398 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5399 if (vallen == NULL) {
5400 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5401 RETURN(err_serious(-EFAULT));
5404 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5406 rc = req_capsule_server_pack(tsi->tsi_pill);
5408 RETURN(err_serious(rc));
5410 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5411 if (valout == NULL) {
5412 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5413 RETURN(err_serious(-EFAULT));
5416 if (KEY_IS(KEY_FID2PATH)) {
5417 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5419 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5420 mdt_thread_info_fini(info);
5427 /* Pass the ioc down */
5428 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5429 unsigned int cmd, int len, void *data)
5431 struct lu_context ioctl_session;
5432 struct md_device *next = mdt->mdt_child;
5436 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5439 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5440 lu_context_enter(&ioctl_session);
5441 env->le_ses = &ioctl_session;
5443 LASSERT(next->md_ops->mdo_iocontrol);
5444 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5446 lu_context_exit(&ioctl_session);
5447 lu_context_fini(&ioctl_session);
5451 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5453 struct obd_ioctl_data *data = karg;
5456 struct mdt_object *obj;
5457 struct mdt_lock_handle *lh;
5461 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5462 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5465 fid = (struct lu_fid *)data->ioc_inlbuf1;
5467 if (!fid_is_sane(fid))
5470 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5472 lh = &mti->mti_lh[MDT_LH_PARENT];
5473 mdt_lock_reg_init(lh, LCK_CR);
5475 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5477 RETURN(PTR_ERR(obj));
5479 if (mdt_object_remote(obj)) {
5482 * before calling version get the correct MDS should be
5483 * fid, this is error to find remote object here
5485 CERROR("nonlocal object "DFID"\n", PFID(fid));
5486 } else if (!mdt_object_exists(obj)) {
5487 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5490 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5491 *(__u64 *)data->ioc_inlbuf2 = version;
5494 mdt_object_unlock_put(mti, obj, lh, 1);
5498 /* ioctls on obd dev */
5499 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5500 void *karg, void *uarg)
5503 struct obd_device *obd = exp->exp_obd;
5504 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5505 struct dt_device *dt = mdt->mdt_bottom;
5509 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5510 rc = lu_env_init(&env, LCT_MD_THREAD);
5516 rc = mdt_device_sync(&env, mdt);
5518 case OBD_IOC_SET_READONLY:
5519 rc = dt->dd_ops->dt_ro(&env, dt);
5521 case OBD_IOC_ABORT_RECOVERY:
5522 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5523 target_stop_recovery_thread(obd);
5526 case OBD_IOC_CHANGELOG_REG:
5527 case OBD_IOC_CHANGELOG_DEREG:
5528 case OBD_IOC_CHANGELOG_CLEAR:
5529 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5531 case OBD_IOC_START_LFSCK: {
5532 struct md_device *next = mdt->mdt_child;
5533 struct obd_ioctl_data *data = karg;
5534 struct lfsck_start_param lsp;
5536 if (unlikely(data == NULL)) {
5541 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5542 lsp.lsp_namespace = mdt->mdt_namespace;
5543 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5546 case OBD_IOC_STOP_LFSCK: {
5547 struct md_device *next = mdt->mdt_child;
5549 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
5552 case OBD_IOC_GET_OBJ_VERSION: {
5553 struct mdt_thread_info *mti;
5554 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5555 memset(mti, 0, sizeof *mti);
5556 mti->mti_env = &env;
5560 rc = mdt_ioc_version_get(mti, karg);
5563 case OBD_IOC_CATLOGLIST:
5564 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg);
5568 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5569 mdt_obd_name(mdt), cmd, rc);
5576 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5578 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5582 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5586 int mdt_obd_postrecov(struct obd_device *obd)
5591 rc = lu_env_init(&env, LCT_MD_THREAD);
5594 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5599 static struct obd_ops mdt_obd_device_ops = {
5600 .o_owner = THIS_MODULE,
5601 .o_set_info_async = mdt_obd_set_info_async,
5602 .o_connect = mdt_obd_connect,
5603 .o_reconnect = mdt_obd_reconnect,
5604 .o_disconnect = mdt_obd_disconnect,
5605 .o_init_export = mdt_init_export,
5606 .o_destroy_export = mdt_destroy_export,
5607 .o_iocontrol = mdt_iocontrol,
5608 .o_postrecov = mdt_obd_postrecov,
5611 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5612 struct lu_device *d)
5614 struct mdt_device *m = mdt_dev(d);
5621 static struct lu_device *mdt_device_free(const struct lu_env *env,
5622 struct lu_device *d)
5624 struct mdt_device *m = mdt_dev(d);
5627 lu_device_fini(&m->mdt_lu_dev);
5633 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5634 struct lu_device_type *t,
5635 struct lustre_cfg *cfg)
5637 struct lu_device *l;
5638 struct mdt_device *m;
5645 rc = mdt_init0(env, m, t, cfg);
5647 mdt_device_free(env, l);
5652 l = ERR_PTR(-ENOMEM);
5656 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5657 LU_KEY_INIT(mdt, struct mdt_thread_info);
5659 static void mdt_key_fini(const struct lu_context *ctx,
5660 struct lu_context_key *key, void* data)
5662 struct mdt_thread_info *info = data;
5664 if (info->mti_big_lmm) {
5665 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5666 info->mti_big_lmm = NULL;
5667 info->mti_big_lmmsize = 0;
5672 /* context key: mdt_thread_key */
5673 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5675 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5677 return lu_ucred(info->mti_env);
5680 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5682 return lu_ucred_check(info->mti_env);
5686 * Enable/disable COS (Commit On Sharing).
5688 * Set/Clear the COS flag in mdt options.
5690 * \param mdt mdt device
5691 * \param val 0 disables COS, other values enable COS
5693 void mdt_enable_cos(struct mdt_device *mdt, int val)
5698 mdt->mdt_opts.mo_cos = !!val;
5699 rc = lu_env_init(&env, LCT_LOCAL);
5700 if (unlikely(rc != 0)) {
5701 CWARN("lu_env initialization failed with rc = %d,"
5702 "cannot sync\n", rc);
5705 mdt_device_sync(&env, mdt);
5710 * Check COS (Commit On Sharing) status.
5712 * Return COS flag status.
5714 * \param mdt mdt device
5716 int mdt_cos_is_enabled(struct mdt_device *mdt)
5718 return mdt->mdt_opts.mo_cos != 0;
5721 static struct lu_device_type_operations mdt_device_type_ops = {
5722 .ldto_device_alloc = mdt_device_alloc,
5723 .ldto_device_free = mdt_device_free,
5724 .ldto_device_fini = mdt_device_fini
5727 static struct lu_device_type mdt_device_type = {
5728 .ldt_tags = LU_DEVICE_MD,
5729 .ldt_name = LUSTRE_MDT_NAME,
5730 .ldt_ops = &mdt_device_type_ops,
5731 .ldt_ctx_tags = LCT_MD_THREAD
5734 static int __init mdt_mod_init(void)
5736 struct lprocfs_static_vars lvars;
5739 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5740 FID_NOBRACE_LEN + 1);
5741 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5743 rc = lu_kmem_init(mdt_caches);
5747 rc = mds_mod_init();
5751 lprocfs_mdt_init_vars(&lvars);
5752 rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
5753 #ifndef HAVE_ONLY_PROCFS_SEQ
5756 LUSTRE_MDT_NAME, &mdt_device_type);
5761 lu_kmem_fini(mdt_caches);
5768 static void __exit mdt_mod_exit(void)
5770 class_unregister_type(LUSTRE_MDT_NAME);
5772 lu_kmem_fini(mdt_caches);
5775 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5776 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5777 MODULE_LICENSE("GPL");
5779 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);