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 mdt_set_capainfo(info, 0, mdt_object_fid(child),
1330 rc = mdt_getattr_internal(info, child, 0);
1331 if (unlikely(rc != 0))
1332 mdt_object_unlock(info, child, lhc, 1);
1338 /* step 1: lock parent only if parent is a directory */
1339 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1340 lhp = &info->mti_lh[MDT_LH_PARENT];
1341 mdt_lock_pdo_init(lhp, LCK_PR, name, namelen);
1342 rc = mdt_object_lock(info, parent, lhp,
1343 MDS_INODELOCK_UPDATE,
1345 if (unlikely(rc != 0))
1349 /* step 2: lookup child's fid by name */
1350 fid_zero(child_fid);
1351 rc = mdo_lookup(info->mti_env, next, lname, child_fid,
1356 mdt_set_disposition(info, ldlm_rep,
1358 GOTO(out_parent, rc);
1360 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1362 *child_fid = reqbody->fid2;
1363 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1367 *step 3: find the child object by fid & lock it.
1368 * regardless if it is local or remote.
1370 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1371 * set parent dir fid the same as child fid in getattr by fid case
1372 * we should not lu_object_find() the object again, could lead
1373 * to hung if there is a concurrent unlink destroyed the object.
1375 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1376 mdt_object_get(info->mti_env, parent);
1379 child = mdt_object_find(info->mti_env, info->mti_mdt,
1383 if (unlikely(IS_ERR(child)))
1384 GOTO(out_parent, rc = PTR_ERR(child));
1386 /* Do not take lock for resent case. */
1387 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1388 LASSERTF(lock != NULL, "Invalid lock handle "LPX64"\n",
1389 lhc->mlh_reg_lh.cookie);
1391 res_id = &lock->l_resource->lr_name;
1392 if (!fid_res_name_eq(mdt_object_fid(child),
1393 &lock->l_resource->lr_name)) {
1394 LASSERTF(fid_res_name_eq(mdt_object_fid(parent),
1395 &lock->l_resource->lr_name),
1396 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1397 PLDLMRES(lock->l_resource),
1398 PFID(mdt_object_fid(parent)));
1399 CWARN("Although resent, but still not get child lock"
1400 "parent:"DFID" child:"DFID"\n",
1401 PFID(mdt_object_fid(parent)),
1402 PFID(mdt_object_fid(child)));
1403 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1404 LDLM_LOCK_PUT(lock);
1407 LDLM_LOCK_PUT(lock);
1410 bool try_layout = false;
1413 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
1414 mdt_lock_handle_init(lhc);
1415 mdt_lock_reg_init(lhc, LCK_PR);
1417 if (!mdt_object_exists(child)) {
1418 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1420 "Object doesn't exist!\n");
1421 GOTO(out_child, rc = -ENOENT);
1424 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1425 mdt_object_exists(child) && !mdt_object_remote(child)) {
1426 struct md_attr *ma = &info->mti_attr;
1429 ma->ma_need = MA_INODE;
1430 rc = mdt_attr_get_complex(info, child, ma);
1431 if (unlikely(rc != 0))
1432 GOTO(out_child, rc);
1434 /* If the file has not been changed for some time, we
1435 * return not only a LOOKUP lock, but also an UPDATE
1436 * lock and this might save us RPC on later STAT. For
1437 * directories, it also let negative dentry cache start
1438 * working for this dir. */
1439 if (ma->ma_valid & MA_INODE &&
1440 ma->ma_attr.la_valid & LA_CTIME &&
1441 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1442 ma->ma_attr.la_ctime < cfs_time_current_sec())
1443 child_bits |= MDS_INODELOCK_UPDATE;
1446 /* layout lock must be granted in a best-effort way
1447 * for IT operations */
1448 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1449 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1450 exp_connect_layout(info->mti_exp) &&
1451 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1452 !mdt_object_remote(child) && ldlm_rep != NULL) {
1453 /* try to grant layout lock for regular file. */
1459 child_bits |= MDS_INODELOCK_LAYOUT;
1460 /* try layout lock, it may fail to be granted due to
1461 * contention at LOOKUP or UPDATE */
1462 if (!mdt_object_lock_try(info, child, lhc, child_bits,
1464 child_bits &= ~MDS_INODELOCK_LAYOUT;
1465 LASSERT(child_bits != 0);
1466 rc = mdt_object_lock(info, child, lhc,
1467 child_bits, MDT_CROSS_LOCK);
1472 rc = mdt_object_lock(info, child, lhc, child_bits,
1475 if (unlikely(rc != 0))
1476 GOTO(out_child, rc);
1479 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1480 /* Get MA_SOM attributes if update lock is given. */
1482 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE &&
1483 S_ISREG(lu_object_attr(&mdt_object_child(child)->mo_lu)))
1486 /* finally, we can get attr for child. */
1487 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1488 rc = mdt_getattr_internal(info, child, ma_need);
1489 if (unlikely(rc != 0)) {
1490 mdt_object_unlock(info, child, lhc, 1);
1492 /* Debugging code. */
1493 res_id = &lock->l_resource->lr_name;
1494 LDLM_DEBUG(lock, "Returning lock to client");
1495 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1496 &lock->l_resource->lr_name),
1497 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1498 PLDLMRES(lock->l_resource),
1499 PFID(mdt_object_fid(child)));
1500 if (mdt_object_exists(child) && !mdt_object_remote(child))
1501 mdt_pack_size2body(info, child);
1504 LDLM_LOCK_PUT(lock);
1508 mdt_object_put(info->mti_env, child);
1511 mdt_object_unlock(info, parent, lhp, 1);
1515 /* normal handler: should release the child lock */
1516 int mdt_getattr_name(struct tgt_session_info *tsi)
1518 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1519 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1520 struct mdt_body *reqbody;
1521 struct mdt_body *repbody;
1525 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1526 LASSERT(reqbody != NULL);
1527 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1528 LASSERT(repbody != NULL);
1530 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
1531 repbody->eadatasize = 0;
1532 repbody->aclsize = 0;
1534 rc = mdt_init_ucred(info, reqbody);
1536 GOTO(out_shrink, rc);
1538 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1539 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1540 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1541 lhc->mlh_reg_lh.cookie = 0;
1543 mdt_exit_ucred(info);
1546 mdt_client_compatibility(info);
1547 rc2 = mdt_fix_reply(info);
1550 mdt_thread_info_fini(info);
1554 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1555 void *karg, void *uarg);
1557 int mdt_set_info(struct tgt_session_info *tsi)
1559 struct ptlrpc_request *req = tgt_ses_req(tsi);
1562 int keylen, vallen, rc = 0;
1566 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1568 DEBUG_REQ(D_HA, req, "no set_info key");
1569 RETURN(err_serious(-EFAULT));
1572 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1575 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1577 DEBUG_REQ(D_HA, req, "no set_info val");
1578 RETURN(err_serious(-EFAULT));
1581 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1584 /* Swab any part of val you need to here */
1585 if (KEY_IS(KEY_READ_ONLY)) {
1586 spin_lock(&req->rq_export->exp_lock);
1588 *exp_connect_flags_ptr(req->rq_export) |=
1591 *exp_connect_flags_ptr(req->rq_export) &=
1592 ~OBD_CONNECT_RDONLY;
1593 spin_unlock(&req->rq_export->exp_lock);
1594 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1595 struct changelog_setinfo *cs = val;
1597 if (vallen != sizeof(*cs)) {
1598 CERROR("%s: bad changelog_clear setinfo size %d\n",
1599 tgt_name(tsi->tsi_tgt), vallen);
1602 if (ptlrpc_req_need_swab(req)) {
1603 __swab64s(&cs->cs_recno);
1604 __swab32s(&cs->cs_id);
1607 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1615 int mdt_readpage(struct tgt_session_info *tsi)
1617 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1618 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1619 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1620 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1621 struct mdt_body *repbody;
1627 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1628 RETURN(err_serious(-ENOMEM));
1630 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1631 if (repbody == NULL || reqbody == NULL)
1632 RETURN(err_serious(-EFAULT));
1635 * prepare @rdpg before calling lower layers and transfer itself. Here
1636 * reqbody->size contains offset of where to start to read and
1637 * reqbody->nlink contains number bytes to read.
1639 rdpg->rp_hash = reqbody->size;
1640 if (rdpg->rp_hash != reqbody->size) {
1641 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1642 rdpg->rp_hash, reqbody->size);
1646 rdpg->rp_attrs = reqbody->mode;
1647 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1648 rdpg->rp_attrs |= LUDA_64BITHASH;
1649 rdpg->rp_count = min_t(unsigned int, reqbody->nlink,
1650 exp_max_brw_size(tsi->tsi_exp));
1651 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1653 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1654 if (rdpg->rp_pages == NULL)
1657 for (i = 0; i < rdpg->rp_npages; ++i) {
1658 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1659 if (rdpg->rp_pages[i] == NULL)
1660 GOTO(free_rdpg, rc = -ENOMEM);
1663 /* call lower layers to fill allocated pages with directory data */
1664 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1666 GOTO(free_rdpg, rc);
1668 /* send pages to client */
1669 rc = tgt_sendpage(tsi, rdpg, rc);
1674 for (i = 0; i < rdpg->rp_npages; i++)
1675 if (rdpg->rp_pages[i] != NULL)
1676 __free_page(rdpg->rp_pages[i]);
1677 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1679 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1685 static int mdt_reint_internal(struct mdt_thread_info *info,
1686 struct mdt_lock_handle *lhc,
1689 struct req_capsule *pill = info->mti_pill;
1690 struct mdt_body *repbody;
1695 rc = mdt_reint_unpack(info, op);
1697 CERROR("Can't unpack reint, rc %d\n", rc);
1698 RETURN(err_serious(rc));
1701 /* for replay (no_create) lmm is not needed, client has it already */
1702 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1703 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1704 info->mti_rr.rr_eadatalen);
1706 /* llog cookies are always 0, the field is kept for compatibility */
1707 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1708 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1710 rc = req_capsule_server_pack(pill);
1712 CERROR("Can't pack response, rc %d\n", rc);
1713 RETURN(err_serious(rc));
1716 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1717 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1719 repbody->eadatasize = 0;
1720 repbody->aclsize = 0;
1723 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1725 /* for replay no cookkie / lmm need, because client have this already */
1726 if (info->mti_spec.no_create)
1727 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1728 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1730 rc = mdt_init_ucred_reint(info);
1732 GOTO(out_shrink, rc);
1734 rc = mdt_fix_attr_ucred(info, op);
1736 GOTO(out_ucred, rc = err_serious(rc));
1738 if (mdt_check_resent(info, mdt_reconstruct, lhc)) {
1739 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1740 GOTO(out_ucred, rc);
1742 rc = mdt_reint_rec(info, lhc);
1745 mdt_exit_ucred(info);
1747 mdt_client_compatibility(info);
1748 rc2 = mdt_fix_reply(info);
1754 static long mdt_reint_opcode(struct ptlrpc_request *req,
1755 const struct req_format **fmt)
1757 struct mdt_device *mdt;
1758 struct mdt_rec_reint *rec;
1761 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1763 opc = rec->rr_opcode;
1764 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1765 if (opc < REINT_MAX && fmt[opc] != NULL)
1766 req_capsule_extend(&req->rq_pill, fmt[opc]);
1768 mdt = mdt_exp2dev(req->rq_export);
1769 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1770 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1771 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1772 opc = err_serious(-EFAULT);
1775 opc = err_serious(-EFAULT);
1780 int mdt_reint(struct tgt_session_info *tsi)
1784 static const struct req_format *reint_fmts[REINT_MAX] = {
1785 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1786 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1787 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1788 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1789 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1790 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1791 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1792 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK
1797 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1799 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1801 * No lock possible here from client to pass it to reint code
1804 rc = mdt_reint_internal(info, NULL, opc);
1805 mdt_thread_info_fini(info);
1810 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1814 /* this should sync the whole device */
1815 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1817 struct dt_device *dt = mdt->mdt_bottom;
1821 rc = dt->dd_ops->dt_sync(env, dt);
1825 /* this should sync this object */
1826 static int mdt_object_sync(struct mdt_thread_info *info)
1828 struct md_object *next;
1832 if (!mdt_object_exists(info->mti_object)) {
1833 CWARN("Non existing object "DFID"!\n",
1834 PFID(mdt_object_fid(info->mti_object)));
1837 next = mdt_object_child(info->mti_object);
1838 rc = mo_object_sync(info->mti_env, next);
1843 int mdt_sync(struct tgt_session_info *tsi)
1845 struct ptlrpc_request *req = tgt_ses_req(tsi);
1846 struct req_capsule *pill = tsi->tsi_pill;
1847 struct mdt_body *body;
1852 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1853 RETURN(err_serious(-ENOMEM));
1855 if (fid_seq(&tsi->tsi_mdt_body->fid1) == 0) {
1856 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
1858 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1860 /* sync an object */
1861 rc = mdt_object_sync(info);
1863 const struct lu_fid *fid;
1864 struct lu_attr *la = &info->mti_attr.ma_attr;
1866 info->mti_attr.ma_need = MA_INODE;
1867 info->mti_attr.ma_valid = 0;
1868 rc = mdt_attr_get_complex(info, info->mti_object,
1871 body = req_capsule_server_get(pill,
1873 fid = mdt_object_fid(info->mti_object);
1874 mdt_pack_attr2body(info, body, la, fid);
1877 mdt_thread_info_fini(info);
1880 mdt_counter_incr(req, LPROC_MDT_SYNC);
1886 * Handle quota control requests to consult current usage/limit, but also
1887 * to configure quota enforcement
1889 int mdt_quotactl(struct tgt_session_info *tsi)
1891 struct obd_export *exp = tsi->tsi_exp;
1892 struct req_capsule *pill = tsi->tsi_pill;
1893 struct obd_quotactl *oqctl, *repoqc;
1895 struct mdt_device *mdt = mdt_exp2dev(exp);
1896 struct lu_device *qmt = mdt->mdt_qmt_dev;
1899 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
1901 RETURN(err_serious(-EPROTO));
1903 rc = req_capsule_server_pack(pill);
1905 RETURN(err_serious(rc));
1907 switch (oqctl->qc_cmd) {
1909 case LUSTRE_Q_INVALIDATE:
1910 case LUSTRE_Q_FINVALIDATE:
1914 /* deprecated, not used any more */
1915 RETURN(-EOPNOTSUPP);
1916 /* master quotactl */
1922 RETURN(-EOPNOTSUPP);
1923 /* slave quotactl */
1928 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1932 /* map uid/gid for remote client */
1934 if (exp_connect_rmtclient(exp)) {
1935 struct lustre_idmap_table *idmap;
1937 idmap = exp->exp_mdt_data.med_idmap;
1939 if (unlikely(oqctl->qc_cmd != Q_GETQUOTA &&
1940 oqctl->qc_cmd != Q_GETINFO))
1943 if (oqctl->qc_type == USRQUOTA)
1944 id = lustre_idmap_lookup_uid(NULL, idmap, 0,
1946 else if (oqctl->qc_type == GRPQUOTA)
1947 id = lustre_idmap_lookup_gid(NULL, idmap, 0,
1952 if (id == CFS_IDMAP_NOTFOUND) {
1953 CDEBUG(D_QUOTA, "no mapping for id %u\n", oqctl->qc_id);
1958 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
1960 RETURN(err_serious(-EFAULT));
1962 if (oqctl->qc_id != id)
1963 swap(oqctl->qc_id, id);
1965 switch (oqctl->qc_cmd) {
1971 /* forward quotactl request to QMT */
1972 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
1977 /* slave quotactl */
1978 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
1983 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
1987 if (oqctl->qc_id != id)
1988 swap(oqctl->qc_id, id);
1994 /** clone llog ctxt from child (mdd)
1995 * This allows remote llog (replicator) access.
1996 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
1997 * context was originally set up, or we can handle them directly.
1998 * I choose the latter, but that means I need any llog
1999 * contexts set up by child to be accessable by the mdt. So we clone the
2000 * context into our context list here.
2002 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2005 struct md_device *next = mdt->mdt_child;
2006 struct llog_ctxt *ctxt;
2009 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2012 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2013 if (rc || ctxt == NULL) {
2017 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2019 CERROR("Can't set mdt ctxt %d\n", rc);
2024 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2025 struct mdt_device *mdt, int idx)
2027 struct llog_ctxt *ctxt;
2029 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2032 /* Put once for the get we just did, and once for the clone */
2033 llog_ctxt_put(ctxt);
2034 llog_ctxt_put(ctxt);
2039 * sec context handlers
2041 int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2045 rc = mdt_handle_idmap(tsi);
2047 struct ptlrpc_request *req = tgt_ses_req(tsi);
2050 opc = lustre_msg_get_opc(req->rq_reqmsg);
2051 if (opc == SEC_CTX_INIT || opc == SEC_CTX_INIT_CONT)
2052 sptlrpc_svc_ctx_invalidate(req);
2055 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2061 * quota request handlers
2063 int mdt_quota_dqacq(struct tgt_session_info *tsi)
2065 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2066 struct lu_device *qmt = mdt->mdt_qmt_dev;
2071 RETURN(err_serious(-EOPNOTSUPP));
2073 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2077 struct mdt_object *mdt_object_new(const struct lu_env *env,
2078 struct mdt_device *d,
2079 const struct lu_fid *f)
2081 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2082 struct lu_object *o;
2083 struct mdt_object *m;
2086 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2087 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2088 if (unlikely(IS_ERR(o)))
2089 m = (struct mdt_object *)o;
2095 struct mdt_object *mdt_object_find(const struct lu_env *env,
2096 struct mdt_device *d,
2097 const struct lu_fid *f)
2099 struct lu_object *o;
2100 struct mdt_object *m;
2103 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2104 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2105 if (unlikely(IS_ERR(o)))
2106 m = (struct mdt_object *)o;
2114 * Asyncronous commit for mdt device.
2116 * Pass asynchonous commit call down the MDS stack.
2118 * \param env environment
2119 * \param mdt the mdt device
2121 static void mdt_device_commit_async(const struct lu_env *env,
2122 struct mdt_device *mdt)
2124 struct dt_device *dt = mdt->mdt_bottom;
2127 rc = dt->dd_ops->dt_commit_async(env, dt);
2128 if (unlikely(rc != 0))
2129 CWARN("async commit start failed with rc = %d", rc);
2133 * Mark the lock as "synchonous".
2135 * Mark the lock to deffer transaction commit to the unlock time.
2137 * \param lock the lock to mark as "synchonous"
2139 * \see mdt_is_lock_sync
2140 * \see mdt_save_lock
2142 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2144 lock->l_ast_data = (void*)1;
2148 * Check whehter the lock "synchonous" or not.
2150 * \param lock the lock to check
2151 * \retval 1 the lock is "synchonous"
2152 * \retval 0 the lock isn't "synchronous"
2154 * \see mdt_set_lock_sync
2155 * \see mdt_save_lock
2157 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2159 return lock->l_ast_data != NULL;
2163 * Blocking AST for mdt locks.
2165 * Starts transaction commit if in case of COS lock conflict or
2166 * deffers such a commit to the mdt_save_lock.
2168 * \param lock the lock which blocks a request or cancelling lock
2169 * \param desc unused
2170 * \param data unused
2171 * \param flag indicates whether this cancelling or blocking callback
2173 * \see ldlm_blocking_ast_nocheck
2175 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2176 void *data, int flag)
2178 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2179 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2183 if (flag == LDLM_CB_CANCELING)
2185 lock_res_and_lock(lock);
2186 if (lock->l_blocking_ast != mdt_blocking_ast) {
2187 unlock_res_and_lock(lock);
2190 if (mdt_cos_is_enabled(mdt) &&
2191 lock->l_req_mode & (LCK_PW | LCK_EX) &&
2192 lock->l_blocking_lock != NULL &&
2193 lock->l_client_cookie != lock->l_blocking_lock->l_client_cookie) {
2194 mdt_set_lock_sync(lock);
2196 rc = ldlm_blocking_ast_nocheck(lock);
2198 /* There is no lock conflict if l_blocking_lock == NULL,
2199 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2200 * when the last reference to a local lock was released */
2201 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2204 rc = lu_env_init(&env, LCT_LOCAL);
2205 if (unlikely(rc != 0))
2206 CWARN("lu_env initialization failed with rc = %d,"
2207 "cannot start asynchronous commit\n", rc);
2209 mdt_device_commit_async(&env, mdt);
2215 int mdt_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2216 void *data, int flag)
2218 struct lustre_handle lockh;
2222 case LDLM_CB_BLOCKING:
2223 ldlm_lock2handle(lock, &lockh);
2224 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
2226 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2230 case LDLM_CB_CANCELING:
2231 LDLM_DEBUG(lock, "Revoke remote lock\n");
2239 int mdt_remote_object_lock(struct mdt_thread_info *mti,
2240 struct mdt_object *o, struct lustre_handle *lh,
2241 ldlm_mode_t mode, __u64 ibits)
2243 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2244 ldlm_policy_data_t *policy = &mti->mti_policy;
2248 LASSERT(mdt_object_remote(o));
2250 LASSERT(ibits & MDS_INODELOCK_UPDATE);
2252 memset(einfo, 0, sizeof(*einfo));
2253 einfo->ei_type = LDLM_IBITS;
2254 einfo->ei_mode = mode;
2255 einfo->ei_cb_bl = mdt_md_blocking_ast;
2256 einfo->ei_cb_cp = ldlm_completion_ast;
2258 memset(policy, 0, sizeof(*policy));
2259 policy->l_inodebits.bits = ibits;
2261 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2266 static int mdt_object_lock0(struct mdt_thread_info *info, struct mdt_object *o,
2267 struct mdt_lock_handle *lh, __u64 ibits,
2268 bool nonblock, int locality)
2270 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2271 ldlm_policy_data_t *policy = &info->mti_policy;
2272 struct ldlm_res_id *res_id = &info->mti_res_id;
2277 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2278 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2279 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2280 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2282 if (mdt_object_remote(o)) {
2283 if (locality == MDT_CROSS_LOCK) {
2284 ibits &= ~(MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2285 MDS_INODELOCK_LAYOUT);
2286 ibits |= MDS_INODELOCK_LOOKUP;
2289 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_PERM |
2290 MDS_INODELOCK_LAYOUT)),
2291 "%s: wrong bit "LPX64" for remote obj "DFID"\n",
2292 mdt_obd_name(info->mti_mdt), ibits,
2293 PFID(mdt_object_fid(o)));
2294 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
2296 /* No PDO lock on remote object */
2297 LASSERT(lh->mlh_type != MDT_PDO_LOCK);
2300 if (lh->mlh_type == MDT_PDO_LOCK) {
2301 /* check for exists after object is locked */
2302 if (mdt_object_exists(o) == 0) {
2303 /* Non-existent object shouldn't have PDO lock */
2306 /* Non-dir object shouldn't have PDO lock */
2307 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2312 memset(policy, 0, sizeof(*policy));
2313 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2315 dlmflags = LDLM_FL_ATOMIC_CB;
2317 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2320 * Take PDO lock on whole directory and build correct @res_id for lock
2321 * on part of directory.
2323 if (lh->mlh_pdo_hash != 0) {
2324 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2325 mdt_lock_pdo_mode(info, o, lh);
2326 if (lh->mlh_pdo_mode != LCK_NL) {
2328 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2329 * is never going to be sent to client and we do not
2330 * want it slowed down due to possible cancels.
2332 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2333 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2334 policy, res_id, dlmflags,
2335 &info->mti_exp->exp_handle.h_cookie);
2341 * Finish res_id initializing by name hash marking part of
2342 * directory which is taking modification.
2344 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2347 policy->l_inodebits.bits = ibits;
2350 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2351 * going to be sent to client. If it is - mdt_intent_policy() path will
2352 * fix it up and turn FL_LOCAL flag off.
2354 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2355 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2356 &info->mti_exp->exp_handle.h_cookie);
2358 mdt_object_unlock(info, o, lh, 1);
2359 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2360 lh->mlh_pdo_hash != 0 &&
2361 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX)) {
2362 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2368 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2369 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2371 return mdt_object_lock0(info, o, lh, ibits, false, locality);
2374 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2375 struct mdt_lock_handle *lh, __u64 ibits, int locality)
2377 struct mdt_lock_handle tmp = *lh;
2380 rc = mdt_object_lock0(info, o, &tmp, ibits, true, locality);
2388 * Save a lock within request object.
2390 * Keep the lock referenced until whether client ACK or transaction
2391 * commit happens or release the lock immediately depending on input
2392 * parameters. If COS is ON, a write lock is converted to COS lock
2395 * \param info thead info object
2396 * \param h lock handle
2397 * \param mode lock mode
2398 * \param decref force immediate lock releasing
2401 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2402 ldlm_mode_t mode, int decref)
2406 if (lustre_handle_is_used(h)) {
2407 if (decref || !info->mti_has_trans ||
2408 !(mode & (LCK_PW | LCK_EX))){
2409 mdt_fid_unlock(h, mode);
2411 struct mdt_device *mdt = info->mti_mdt;
2412 struct ldlm_lock *lock = ldlm_handle2lock(h);
2413 struct ptlrpc_request *req = mdt_info_req(info);
2416 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2418 /* there is no request if mdt_object_unlock() is called
2419 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2420 if (likely(req != NULL)) {
2421 CDEBUG(D_HA, "request = %p reply state = %p"
2422 " transno = "LPD64"\n", req,
2423 req->rq_reply_state, req->rq_transno);
2424 if (mdt_cos_is_enabled(mdt)) {
2426 ldlm_lock_downgrade(lock, LCK_COS);
2429 ptlrpc_save_lock(req, h, mode, no_ack);
2431 ldlm_lock_decref(h, mode);
2433 if (mdt_is_lock_sync(lock)) {
2434 CDEBUG(D_HA, "found sync-lock,"
2435 " async commit started\n");
2436 mdt_device_commit_async(info->mti_env,
2439 LDLM_LOCK_PUT(lock);
2448 * Unlock mdt object.
2450 * Immeditely release the regular lock and the PDO lock or save the
2451 * lock in reqeuest and keep them referenced until client ACK or
2452 * transaction commit.
2454 * \param info thread info object
2455 * \param o mdt object
2456 * \param lh mdt lock handle referencing regular and PDO locks
2457 * \param decref force immediate lock releasing
2459 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2460 struct mdt_lock_handle *lh, int decref)
2464 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2465 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2467 if (lustre_handle_is_used(&lh->mlh_rreg_lh))
2468 ldlm_lock_decref(&lh->mlh_rreg_lh, lh->mlh_rreg_mode);
2473 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2474 const struct lu_fid *f,
2475 struct mdt_lock_handle *lh,
2478 struct mdt_object *o;
2480 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2484 rc = mdt_object_lock(info, o, lh, ibits,
2487 mdt_object_put(info->mti_env, o);
2494 void mdt_object_unlock_put(struct mdt_thread_info * info,
2495 struct mdt_object * o,
2496 struct mdt_lock_handle *lh,
2499 mdt_object_unlock(info, o, lh, decref);
2500 mdt_object_put(info->mti_env, o);
2504 * Generic code handling requests that have struct mdt_body passed in:
2506 * - extract mdt_body from request and save it in @info, if present;
2508 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2511 * - if HABEO_CORPUS flag is set for this request type check whether object
2512 * actually exists on storage (lu_object_exists()).
2515 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2517 const struct mdt_body *body;
2518 struct mdt_object *obj;
2519 const struct lu_env *env;
2520 struct req_capsule *pill;
2524 env = info->mti_env;
2525 pill = info->mti_pill;
2527 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2531 if (!(body->valid & OBD_MD_FLID))
2534 if (!fid_is_sane(&body->fid1)) {
2535 CERROR("Invalid fid: "DFID"\n", PFID(&body->fid1));
2540 * Do not get size or any capa fields before we check that request
2541 * contains capa actually. There are some requests which do not, for
2542 * instance MDS_IS_SUBDIR.
2544 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2545 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT))
2546 mdt_set_capainfo(info, 0, &body->fid1,
2547 req_capsule_client_get(pill, &RMF_CAPA1));
2549 obj = mdt_object_find(env, info->mti_mdt, &body->fid1);
2551 if ((flags & HABEO_CORPUS) &&
2552 !mdt_object_exists(obj)) {
2553 mdt_object_put(env, obj);
2554 /* for capability renew ENOENT will be handled in
2556 if (body->valid & OBD_MD_FLOSSCAPA)
2561 info->mti_object = obj;
2570 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2572 struct req_capsule *pill = info->mti_pill;
2576 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2577 rc = mdt_body_unpack(info, flags);
2581 if (rc == 0 && (flags & HABEO_REFERO)) {
2583 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2584 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2585 info->mti_body->eadatasize);
2586 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2587 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2590 rc = req_capsule_server_pack(pill);
2595 static int mdt_init_capa_ctxt(const struct lu_env *env, struct mdt_device *m)
2597 struct md_device *next = m->mdt_child;
2599 return next->md_ops->mdo_init_capa_ctxt(env, next,
2600 m->mdt_lut.lut_mds_capa,
2601 m->mdt_capa_timeout,
2606 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2608 lh->mlh_type = MDT_NUL_LOCK;
2609 lh->mlh_reg_lh.cookie = 0ull;
2610 lh->mlh_reg_mode = LCK_MINMODE;
2611 lh->mlh_pdo_lh.cookie = 0ull;
2612 lh->mlh_pdo_mode = LCK_MINMODE;
2613 lh->mlh_rreg_lh.cookie = 0ull;
2614 lh->mlh_rreg_mode = LCK_MINMODE;
2617 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2619 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2620 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2624 * Initialize fields of struct mdt_thread_info. Other fields are left in
2625 * uninitialized state, because it's too expensive to zero out whole
2626 * mdt_thread_info (> 1K) on each request arrival.
2628 void mdt_thread_info_init(struct ptlrpc_request *req,
2629 struct mdt_thread_info *info)
2633 info->mti_pill = &req->rq_pill;
2636 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2637 mdt_lock_handle_init(&info->mti_lh[i]);
2639 /* mdt device: it can be NULL while CONNECT */
2640 if (req->rq_export) {
2641 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2642 info->mti_exp = req->rq_export;
2644 info->mti_mdt = NULL;
2645 info->mti_env = req->rq_svc_thread->t_env;
2646 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2648 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2649 info->mti_big_buf = LU_BUF_NULL;
2650 info->mti_body = NULL;
2651 info->mti_object = NULL;
2652 info->mti_dlm_req = NULL;
2653 info->mti_has_trans = 0;
2654 info->mti_cross_ref = 0;
2655 info->mti_opdata = 0;
2656 info->mti_big_lmm_used = 0;
2658 /* To not check for split by default. */
2659 info->mti_spec.no_create = 0;
2660 info->mti_spec.sp_rm_entry = 0;
2663 void mdt_thread_info_fini(struct mdt_thread_info *info)
2667 if (info->mti_object != NULL) {
2668 mdt_object_put(info->mti_env, info->mti_object);
2669 info->mti_object = NULL;
2672 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2673 mdt_lock_handle_fini(&info->mti_lh[i]);
2674 info->mti_env = NULL;
2675 info->mti_pill = NULL;
2676 info->mti_exp = NULL;
2678 if (unlikely(info->mti_big_buf.lb_buf != NULL))
2679 lu_buf_free(&info->mti_big_buf);
2682 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
2684 struct mdt_thread_info *mti;
2685 struct lustre_capa *lc;
2687 mti = mdt_th_info(tsi->tsi_env);
2688 LASSERT(mti != NULL);
2690 mdt_thread_info_init(tgt_ses_req(tsi), mti);
2691 if (tsi->tsi_corpus != NULL) {
2692 struct req_capsule *pill = tsi->tsi_pill;
2694 mti->mti_object = mdt_obj(tsi->tsi_corpus);
2695 lu_object_get(tsi->tsi_corpus);
2698 * XXX: must be part of tgt_mdt_body_unpack but moved here
2699 * due to mdt_set_capainfo().
2701 if (req_capsule_has_field(pill, &RMF_CAPA1, RCL_CLIENT) &&
2702 req_capsule_get_size(pill, &RMF_CAPA1, RCL_CLIENT) > 0) {
2703 lc = req_capsule_client_get(pill, &RMF_CAPA1);
2704 mdt_set_capainfo(mti, 0, &tsi->tsi_mdt_body->fid1, lc);
2707 mti->mti_body = tsi->tsi_mdt_body;
2708 mti->mti_dlm_req = tsi->tsi_dlm_req;
2713 int mdt_tgt_connect(struct tgt_session_info *tsi)
2715 struct ptlrpc_request *req = tgt_ses_req(tsi);
2720 rc = tgt_connect(tsi);
2724 rc = mdt_init_idmap(tsi);
2729 obd_disconnect(class_export_get(req->rq_export));
2748 static int mdt_intent_getattr(enum mdt_it_code opcode,
2749 struct mdt_thread_info *info,
2750 struct ldlm_lock **,
2753 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2754 struct mdt_thread_info *info,
2755 struct ldlm_lock **lockp,
2758 static int mdt_intent_layout(enum mdt_it_code opcode,
2759 struct mdt_thread_info *info,
2760 struct ldlm_lock **,
2762 static int mdt_intent_reint(enum mdt_it_code opcode,
2763 struct mdt_thread_info *info,
2764 struct ldlm_lock **,
2767 static struct mdt_it_flavor {
2768 const struct req_format *it_fmt;
2770 int (*it_act)(enum mdt_it_code ,
2771 struct mdt_thread_info *,
2772 struct ldlm_lock **,
2775 } mdt_it_flavor[] = {
2777 .it_fmt = &RQF_LDLM_INTENT,
2778 /*.it_flags = HABEO_REFERO,*/
2780 .it_act = mdt_intent_reint,
2781 .it_reint = REINT_OPEN
2784 .it_fmt = &RQF_LDLM_INTENT,
2786 * OCREAT is not a MUTABOR request as if the file
2788 * We do the extra check of OBD_CONNECT_RDONLY in
2789 * mdt_reint_open() when we really need to create
2793 .it_act = mdt_intent_reint,
2794 .it_reint = REINT_OPEN
2797 .it_fmt = &RQF_LDLM_INTENT,
2798 .it_flags = MUTABOR,
2799 .it_act = mdt_intent_reint,
2800 .it_reint = REINT_CREATE
2802 [MDT_IT_GETATTR] = {
2803 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2804 .it_flags = HABEO_REFERO,
2805 .it_act = mdt_intent_getattr
2807 [MDT_IT_READDIR] = {
2813 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
2814 .it_flags = HABEO_REFERO,
2815 .it_act = mdt_intent_getattr
2818 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
2819 .it_flags = MUTABOR,
2821 .it_reint = REINT_UNLINK
2825 .it_flags = MUTABOR,
2828 [MDT_IT_GETXATTR] = {
2829 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
2830 .it_flags = HABEO_CORPUS,
2831 .it_act = mdt_intent_getxattr
2834 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
2836 .it_act = mdt_intent_layout
2840 int mdt_intent_lock_replace(struct mdt_thread_info *info,
2841 struct ldlm_lock **lockp,
2842 struct ldlm_lock *new_lock,
2843 struct mdt_lock_handle *lh,
2846 struct ptlrpc_request *req = mdt_info_req(info);
2847 struct ldlm_lock *lock = *lockp;
2850 * Get new lock only for cases when possible resent did not find any
2853 if (new_lock == NULL)
2854 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
2856 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
2857 lh->mlh_reg_lh.cookie = 0;
2861 LASSERTF(new_lock != NULL,
2862 "lockh "LPX64"\n", lh->mlh_reg_lh.cookie);
2865 * If we've already given this lock to a client once, then we should
2866 * have no readers or writers. Otherwise, we should have one reader
2867 * _or_ writer ref (which will be zeroed below) before returning the
2870 if (new_lock->l_export == req->rq_export) {
2871 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2873 LASSERT(new_lock->l_export == NULL);
2874 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2879 if (new_lock->l_export == req->rq_export) {
2881 * Already gave this to the client, which means that we
2882 * reconstructed a reply.
2884 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2886 lh->mlh_reg_lh.cookie = 0;
2887 RETURN(ELDLM_LOCK_REPLACED);
2891 * Fixup the lock to be given to the client.
2893 lock_res_and_lock(new_lock);
2894 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
2895 * possible blocking AST. */
2896 while (new_lock->l_readers > 0) {
2897 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
2898 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2899 new_lock->l_readers--;
2901 while (new_lock->l_writers > 0) {
2902 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
2903 lu_ref_del(&new_lock->l_reference, "user", new_lock);
2904 new_lock->l_writers--;
2907 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
2908 new_lock->l_blocking_ast = lock->l_blocking_ast;
2909 new_lock->l_completion_ast = lock->l_completion_ast;
2910 new_lock->l_remote_handle = lock->l_remote_handle;
2911 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2913 unlock_res_and_lock(new_lock);
2915 cfs_hash_add(new_lock->l_export->exp_lock_hash,
2916 &new_lock->l_remote_handle,
2917 &new_lock->l_exp_hash);
2919 LDLM_LOCK_RELEASE(new_lock);
2920 lh->mlh_reg_lh.cookie = 0;
2922 RETURN(ELDLM_LOCK_REPLACED);
2925 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
2926 struct ldlm_lock *new_lock,
2927 struct ldlm_lock **old_lock,
2928 struct mdt_lock_handle *lh)
2930 struct ptlrpc_request *req = mdt_info_req(info);
2931 struct obd_export *exp = req->rq_export;
2932 struct lustre_handle remote_hdl;
2933 struct ldlm_request *dlmreq;
2934 struct ldlm_lock *lock;
2936 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2939 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
2940 remote_hdl = dlmreq->lock_handle[0];
2942 /* In the function below, .hs_keycmp resolves to
2943 * ldlm_export_lock_keycmp() */
2944 /* coverity[overrun-buffer-val] */
2945 lock = cfs_hash_lookup(exp->exp_lock_hash, &remote_hdl);
2947 if (lock != new_lock) {
2948 lh->mlh_reg_lh.cookie = lock->l_handle.h_cookie;
2949 lh->mlh_reg_mode = lock->l_granted_mode;
2951 LDLM_DEBUG(lock, "Restoring lock cookie");
2952 DEBUG_REQ(D_DLMTRACE, req,
2953 "restoring lock cookie "LPX64,
2954 lh->mlh_reg_lh.cookie);
2956 *old_lock = LDLM_LOCK_GET(lock);
2957 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2961 cfs_hash_put(exp->exp_lock_hash, &lock->l_exp_hash);
2965 * If the xid matches, then we know this is a resent request, and allow
2966 * it. (It's probably an OPEN, for which we don't send a lock.
2968 if (req_xid_is_last(req))
2972 * This remote handle isn't enqueued, so we never received or processed
2973 * this request. Clear MSG_RESENT, because it can be handled like any
2974 * normal request now.
2976 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2978 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2982 static int mdt_intent_getxattr(enum mdt_it_code opcode,
2983 struct mdt_thread_info *info,
2984 struct ldlm_lock **lockp,
2987 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
2988 struct ldlm_reply *ldlm_rep = NULL;
2992 * Initialize lhc->mlh_reg_lh either from a previously granted lock
2993 * (for the resend case) or a new lock. Below we will use it to
2994 * replace the original lock.
2996 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
2997 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2998 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
2999 rc = mdt_object_lock(info, info->mti_object, lhc,
3000 MDS_INODELOCK_XATTR,
3006 grc = mdt_getxattr(info);
3008 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3010 if (mdt_info_req(info)->rq_repmsg != NULL)
3011 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3012 if (ldlm_rep == NULL)
3013 RETURN(err_serious(-EFAULT));
3015 ldlm_rep->lock_policy_res2 = grc;
3020 static int mdt_intent_getattr(enum mdt_it_code opcode,
3021 struct mdt_thread_info *info,
3022 struct ldlm_lock **lockp,
3025 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3026 struct ldlm_lock *new_lock = NULL;
3028 struct ldlm_reply *ldlm_rep;
3029 struct ptlrpc_request *req;
3030 struct mdt_body *reqbody;
3031 struct mdt_body *repbody;
3035 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3038 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3041 info->mti_cross_ref = !!(reqbody->valid & OBD_MD_FLCROSSREF);
3042 repbody->eadatasize = 0;
3043 repbody->aclsize = 0;
3047 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3049 case MDT_IT_GETATTR:
3050 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3054 CERROR("Unsupported intent (%d)\n", opcode);
3055 GOTO(out_shrink, rc = -EINVAL);
3058 rc = mdt_init_ucred(info, reqbody);
3060 GOTO(out_shrink, rc);
3062 req = info->mti_pill->rc_req;
3063 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3064 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3066 /* Get lock from request for possible resent case. */
3067 mdt_intent_fixup_resent(info, *lockp, &new_lock, lhc);
3069 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3070 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3072 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3073 ldlm_rep->lock_policy_res2 = 0;
3074 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3075 ldlm_rep->lock_policy_res2) {
3076 lhc->mlh_reg_lh.cookie = 0ull;
3077 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3080 rc = mdt_intent_lock_replace(info, lockp, new_lock, lhc, flags);
3083 mdt_exit_ucred(info);
3085 mdt_client_compatibility(info);
3086 rc2 = mdt_fix_reply(info);
3092 static int mdt_intent_layout(enum mdt_it_code opcode,
3093 struct mdt_thread_info *info,
3094 struct ldlm_lock **lockp,
3097 struct layout_intent *layout;
3099 struct mdt_object *obj = NULL;
3100 struct md_object *child = NULL;
3104 if (opcode != MDT_IT_LAYOUT) {
3105 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3110 fid = &info->mti_tmp_fid2;
3111 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3113 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3115 RETURN(PTR_ERR(obj));
3117 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3118 child = mdt_object_child(obj);
3120 /* get the length of lsm */
3121 rc = mo_xattr_get(info->mti_env, child, &LU_BUF_NULL,
3124 if (rc > info->mti_mdt->mdt_max_mdsize)
3125 info->mti_mdt->mdt_max_mdsize = rc;
3128 mdt_object_put(info->mti_env, obj);
3130 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3131 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3132 ldlm_lvbo_size(*lockp));
3133 rc = req_capsule_server_pack(info->mti_pill);
3137 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3138 LASSERT(layout != NULL);
3139 if (layout->li_opc == LAYOUT_INTENT_ACCESS)
3140 /* return to normal ldlm handling */
3143 CERROR("%s: Unsupported layout intent (%d)\n",
3144 mdt_obd_name(info->mti_mdt), layout->li_opc);
3148 static int mdt_intent_reint(enum mdt_it_code opcode,
3149 struct mdt_thread_info *info,
3150 struct ldlm_lock **lockp,
3153 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3154 struct ldlm_reply *rep = NULL;
3158 static const struct req_format *intent_fmts[REINT_MAX] = {
3159 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3160 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3165 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3169 if (mdt_it_flavor[opcode].it_reint != opc) {
3170 CERROR("Reint code %ld doesn't match intent: %d\n",
3172 RETURN(err_serious(-EPROTO));
3175 /* Get lock from request for possible resent case. */
3176 mdt_intent_fixup_resent(info, *lockp, NULL, lhc);
3178 rc = mdt_reint_internal(info, lhc, opc);
3180 /* Check whether the reply has been packed successfully. */
3181 if (mdt_info_req(info)->rq_repmsg != NULL)
3182 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3184 RETURN(err_serious(-EFAULT));
3186 /* MDC expects this in any case */
3188 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3190 /* the open lock or the lock for cross-ref object should be
3191 * returned to the client */
3192 if (rc == -EREMOTE || mdt_get_disposition(rep, DISP_OPEN_LOCK)) {
3193 LASSERT(lustre_handle_is_used(&lhc->mlh_reg_lh));
3194 rep->lock_policy_res2 = 0;
3195 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3199 rep->lock_policy_res2 = clear_serious(rc);
3201 if (rep->lock_policy_res2 == -ENOENT &&
3202 mdt_get_disposition(rep, DISP_LOOKUP_NEG))
3203 rep->lock_policy_res2 = 0;
3205 if (rc == -ENOTCONN || rc == -ENODEV ||
3206 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3208 * If it is the disconnect error (ENODEV & ENOCONN), the error
3209 * will be returned by rq_status, and client at ptlrpc layer
3210 * will detect this, then disconnect, reconnect the import
3211 * immediately, instead of impacting the following the rpc.
3213 lhc->mlh_reg_lh.cookie = 0ull;
3217 * For other cases, the error will be returned by intent.
3218 * and client will retrieve the result from intent.
3221 * FIXME: when open lock is finished, that should be
3224 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3225 LASSERTF(rc == 0, "Error occurred but lock handle "
3226 "is still in use, rc = %d\n", rc);
3227 rep->lock_policy_res2 = 0;
3228 rc = mdt_intent_lock_replace(info, lockp, NULL, lhc, flags);
3231 lhc->mlh_reg_lh.cookie = 0ull;
3232 RETURN(ELDLM_LOCK_ABORTED);
3237 static int mdt_intent_code(long itcode)
3245 case IT_OPEN|IT_CREAT:
3252 rc = MDT_IT_READDIR;
3255 rc = MDT_IT_GETATTR;
3267 rc = MDT_IT_GETXATTR;
3272 case IT_QUOTA_DQACQ:
3277 CERROR("Unknown intent opcode: %ld\n", itcode);
3284 static int mdt_intent_opc(long itopc, struct mdt_thread_info *info,
3285 struct ldlm_lock **lockp, __u64 flags)
3287 struct req_capsule *pill;
3288 struct mdt_it_flavor *flv;
3293 opc = mdt_intent_code(itopc);
3297 pill = info->mti_pill;
3299 if (opc == MDT_IT_QUOTA) {
3300 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3303 RETURN(-EOPNOTSUPP);
3305 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3306 /* pass the request to quota master */
3307 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3308 mdt_info_req(info), lockp,
3313 flv = &mdt_it_flavor[opc];
3314 if (flv->it_fmt != NULL)
3315 req_capsule_extend(pill, flv->it_fmt);
3317 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3319 struct ptlrpc_request *req = mdt_info_req(info);
3320 if (flv->it_flags & MUTABOR &&
3321 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3324 if (rc == 0 && flv->it_act != NULL) {
3325 struct ldlm_reply *rep;
3327 /* execute policy */
3328 rc = flv->it_act(opc, info, lockp, flags);
3330 /* Check whether the reply has been packed successfully. */
3331 if (mdt_info_req(info)->rq_repmsg != NULL) {
3332 rep = req_capsule_server_get(info->mti_pill,
3334 rep->lock_policy_res2 =
3335 ptlrpc_status_hton(rep->lock_policy_res2);
3343 static int mdt_intent_policy(struct ldlm_namespace *ns,
3344 struct ldlm_lock **lockp, void *req_cookie,
3345 ldlm_mode_t mode, __u64 flags, void *data)
3347 struct tgt_session_info *tsi;
3348 struct mdt_thread_info *info;
3349 struct ptlrpc_request *req = req_cookie;
3350 struct ldlm_intent *it;
3351 struct req_capsule *pill;
3356 LASSERT(req != NULL);
3358 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3360 info = tsi2mdt_info(tsi);
3361 LASSERT(info != NULL);
3362 pill = info->mti_pill;
3363 LASSERT(pill->rc_req == req);
3365 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3366 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3367 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3369 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3373 /* Lock without inodebits makes no sense and will oops
3374 * later in ldlm. Let's check it now to see if we have
3375 * ibits corrupted somewhere in mdt_intent_opc().
3376 * The case for client miss to set ibits has been
3377 * processed by others. */
3378 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3379 lr_type == LDLM_IBITS,
3380 info->mti_dlm_req->lock_desc.\
3381 l_policy_data.l_inodebits.bits != 0));
3383 rc = err_serious(-EFAULT);
3385 /* No intent was provided */
3386 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3387 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3388 rc = req_capsule_server_pack(pill);
3390 rc = err_serious(rc);
3392 mdt_thread_info_fini(info);
3396 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3398 struct seq_server_site *ss = mdt_seq_site(mdt);
3400 if (ss->ss_node_id == 0)
3403 if (ss->ss_client_seq != NULL) {
3404 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3405 ss->ss_client_seq->lcs_exp = NULL;
3408 if (ss->ss_server_fld != NULL) {
3409 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3410 ss->ss_server_fld->lsf_control_exp = NULL;
3414 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3416 struct seq_server_site *ss = mdt_seq_site(mdt);
3421 if (ss->ss_server_seq == NULL)
3422 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3427 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3429 mdt_seq_fini_cli(mdt);
3430 mdt_deregister_seq_exp(mdt);
3432 return seq_site_fini(env, mdt_seq_site(mdt));
3436 * It will retrieve its FLDB entries from MDT0, and it only happens
3437 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3438 * and it needs to refresh FLDB from the MDT0.
3440 static int mdt_register_lwp_callback(void *data)
3443 struct mdt_device *mdt = data;
3444 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3448 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3450 if (!likely(fld->lsf_new))
3453 rc = lu_env_init(&env, LCT_MD_THREAD);
3455 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3459 rc = fld_update_from_controller(&env, fld);
3461 CERROR("%s: cannot update controller: rc = %d\n",
3462 mdt_obd_name(mdt), rc);
3470 static int mdt_register_seq_exp(struct mdt_device *mdt)
3472 struct seq_server_site *ss = mdt_seq_site(mdt);
3473 char *lwp_name = NULL;
3476 if (ss->ss_node_id == 0)
3479 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3480 if (lwp_name == NULL)
3481 GOTO(out_free, rc = -ENOMEM);
3483 rc = tgt_name2lwpname(mdt_obd_name(mdt), lwp_name);
3487 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3492 rc = lustre_register_lwp_item(lwp_name,
3493 &ss->ss_server_fld->lsf_control_exp,
3494 mdt_register_lwp_callback, mdt);
3496 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3497 ss->ss_client_seq->lcs_exp = NULL;
3501 if (lwp_name != NULL)
3502 OBD_FREE(lwp_name, MAX_OBD_NAME);
3508 * Init client sequence manager which is used by local MDS to talk to sequence
3509 * controller on remote node.
3511 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3513 struct seq_server_site *ss = mdt_seq_site(mdt);
3518 /* check if this is adding the first MDC and controller is not yet
3520 OBD_ALLOC_PTR(ss->ss_client_seq);
3521 if (ss->ss_client_seq == NULL)
3524 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3525 if (prefix == NULL) {
3526 OBD_FREE_PTR(ss->ss_client_seq);
3527 ss->ss_client_seq = NULL;
3531 /* Note: seq_client_fini will be called in seq_site_fini */
3532 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3533 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3534 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3536 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3538 OBD_FREE_PTR(ss->ss_client_seq);
3539 ss->ss_client_seq = NULL;
3543 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3548 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3550 struct seq_server_site *ss;
3554 ss = mdt_seq_site(mdt);
3555 /* init sequence controller server(MDT0) */
3556 if (ss->ss_node_id == 0) {
3557 OBD_ALLOC_PTR(ss->ss_control_seq);
3558 if (ss->ss_control_seq == NULL)
3561 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3562 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3565 GOTO(out_seq_fini, rc);
3568 /* Init normal sequence server */
3569 OBD_ALLOC_PTR(ss->ss_server_seq);
3570 if (ss->ss_server_seq == NULL)
3571 GOTO(out_seq_fini, rc = -ENOMEM);
3573 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3574 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3576 GOTO(out_seq_fini, rc);
3578 /* init seq client for seq server to talk to seq controller(MDT0) */
3579 rc = mdt_seq_init_cli(env, mdt);
3581 GOTO(out_seq_fini, rc);
3583 if (ss->ss_node_id != 0)
3584 /* register controler export through lwp */
3585 rc = mdt_register_seq_exp(mdt);
3590 mdt_seq_fini(env, mdt);
3598 static int mdt_fld_fini(const struct lu_env *env,
3599 struct mdt_device *m)
3601 struct seq_server_site *ss = mdt_seq_site(m);
3604 if (ss && ss->ss_server_fld) {
3605 fld_server_fini(env, ss->ss_server_fld);
3606 OBD_FREE_PTR(ss->ss_server_fld);
3607 ss->ss_server_fld = NULL;
3613 static int mdt_fld_init(const struct lu_env *env,
3615 struct mdt_device *m)
3617 struct seq_server_site *ss;
3621 ss = mdt_seq_site(m);
3623 OBD_ALLOC_PTR(ss->ss_server_fld);
3624 if (ss->ss_server_fld == NULL)
3625 RETURN(rc = -ENOMEM);
3627 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3630 OBD_FREE_PTR(ss->ss_server_fld);
3631 ss->ss_server_fld = NULL;
3638 static void mdt_stack_pre_fini(const struct lu_env *env,
3639 struct mdt_device *m, struct lu_device *top)
3641 struct obd_device *obd;
3642 struct lustre_cfg_bufs *bufs;
3643 struct lustre_cfg *lcfg;
3644 struct mdt_thread_info *info;
3649 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3650 LASSERT(info != NULL);
3652 bufs = &info->mti_u.bufs;
3654 LASSERT(m->mdt_child_exp);
3655 LASSERT(m->mdt_child_exp->exp_obd);
3656 obd = m->mdt_child_exp->exp_obd;
3658 /* process cleanup, pass mdt obd name to get obd umount flags */
3659 /* XXX: this is needed because all layers are referenced by
3660 * objects (some of them are pinned by osd, for example *
3661 * the proper solution should be a model where object used
3662 * by osd only doesn't have mdt/mdd slices -bzzz */
3663 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3664 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3665 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3667 CERROR("%s:Cannot alloc lcfg!\n", mdt_obd_name(m));
3670 top->ld_ops->ldo_process_config(env, top, lcfg);
3671 lustre_cfg_free(lcfg);
3675 static void mdt_stack_fini(const struct lu_env *env,
3676 struct mdt_device *m, struct lu_device *top)
3678 struct obd_device *obd = mdt2obd_dev(m);
3679 struct lustre_cfg_bufs *bufs;
3680 struct lustre_cfg *lcfg;
3681 struct mdt_thread_info *info;
3685 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3686 LASSERT(info != NULL);
3688 lu_dev_del_linkage(top->ld_site, top);
3690 lu_site_purge(env, top->ld_site, -1);
3692 bufs = &info->mti_u.bufs;
3693 /* process cleanup, pass mdt obd name to get obd umount flags */
3694 /* another purpose is to let all layers to release their objects */
3695 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3700 lustre_cfg_bufs_set_string(bufs, 1, flags);
3701 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
3703 CERROR("Cannot alloc lcfg!\n");
3707 top->ld_ops->ldo_process_config(env, top, lcfg);
3708 lustre_cfg_free(lcfg);
3710 lu_site_purge(env, top->ld_site, -1);
3712 m->mdt_child = NULL;
3713 m->mdt_bottom = NULL;
3715 obd_disconnect(m->mdt_child_exp);
3716 m->mdt_child_exp = NULL;
3718 obd_disconnect(m->mdt_bottom_exp);
3719 m->mdt_child_exp = NULL;
3722 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
3723 const char *next, struct obd_export **exp)
3725 struct obd_connect_data *data = NULL;
3726 struct obd_device *obd;
3730 OBD_ALLOC_PTR(data);
3732 GOTO(out, rc = -ENOMEM);
3734 obd = class_name2obd(next);
3736 CERROR("%s: can't locate next device: %s\n",
3737 mdt_obd_name(m), next);
3738 GOTO(out, rc = -ENOTCONN);
3741 data->ocd_connect_flags = OBD_CONNECT_VERSION;
3742 data->ocd_version = LUSTRE_VERSION_CODE;
3744 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
3746 CERROR("%s: cannot connect to next dev %s (%d)\n",
3747 mdt_obd_name(m), next, rc);
3757 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
3758 struct lustre_cfg *cfg)
3760 char *dev = lustre_cfg_string(cfg, 0);
3761 int rc, name_size, uuid_size;
3762 char *name, *uuid, *p;
3763 struct lustre_cfg_bufs *bufs;
3764 struct lustre_cfg *lcfg;
3765 struct obd_device *obd;
3766 struct lustre_profile *lprof;
3767 struct lu_site *site;
3770 /* in 1.8 we had the only device in the stack - MDS.
3771 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
3772 * in 2.3 OSD is instantiated by obd_mount.c, so we need
3773 * to generate names and setup MDT, MDD. MDT will be using
3774 * generated name to connect to MDD. for MDD the next device
3775 * will be LOD with name taken from so called "profile" which
3776 * is generated by mount_option line
3778 * 1.8 MGS generates config. commands like this:
3779 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
3780 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
3781 * 2.0 MGS generates config. commands like this:
3782 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
3783 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3784 * 3:lustre-MDT0000-mdtlov 4:f
3786 * we generate MDD name from MDT one, just replacing T with D
3788 * after all the preparations, the logical equivalent will be
3789 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
3790 * 3:lustre-MDT0000-mdtlov 4:f
3791 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3792 * 3:lustre-MDD0000 4:f
3794 * notice we build the stack from down to top: MDD first, then MDT */
3796 name_size = MAX_OBD_NAME;
3797 uuid_size = MAX_OBD_NAME;
3799 OBD_ALLOC(name, name_size);
3800 OBD_ALLOC(uuid, uuid_size);
3801 if (name == NULL || uuid == NULL)
3802 GOTO(cleanup_mem, rc = -ENOMEM);
3804 OBD_ALLOC_PTR(bufs);
3806 GOTO(cleanup_mem, rc = -ENOMEM);
3809 p = strstr(name, "-MDT");
3811 GOTO(free_bufs, rc = -ENOMEM);
3814 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
3816 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3817 if (lprof == NULL || lprof->lp_dt == NULL) {
3818 CERROR("can't find the profile: %s\n",
3819 lustre_cfg_string(cfg, 0));
3820 GOTO(free_bufs, rc = -EINVAL);
3823 lustre_cfg_bufs_reset(bufs, name);
3824 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
3825 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3826 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3828 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3830 GOTO(free_bufs, rc = -ENOMEM);
3832 rc = class_attach(lcfg);
3834 GOTO(lcfg_cleanup, rc);
3836 obd = class_name2obd(name);
3838 CERROR("Can not find obd %s (%s in config)\n",
3839 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
3840 GOTO(class_detach, rc = -EINVAL);
3843 lustre_cfg_free(lcfg);
3845 lustre_cfg_bufs_reset(bufs, name);
3846 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3847 lustre_cfg_bufs_set_string(bufs, 2, dev);
3848 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3850 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3852 rc = class_setup(obd, lcfg);
3854 GOTO(class_detach, rc);
3856 /* connect to MDD we just setup */
3857 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
3859 GOTO(class_detach, rc);
3861 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
3863 LASSERT(mdt_lu_site(mdt) == NULL);
3864 mdt->mdt_lu_dev.ld_site = site;
3865 site->ls_top_dev = &mdt->mdt_lu_dev;
3866 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
3869 /* now connect to bottom OSD */
3870 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
3871 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
3873 GOTO(class_detach, rc);
3875 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
3878 rc = lu_env_refill((struct lu_env *)env);
3880 CERROR("Failure to refill session: '%d'\n", rc);
3882 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
3887 class_detach(obd, lcfg);
3889 lustre_cfg_free(lcfg);
3894 OBD_FREE(name, name_size);
3896 OBD_FREE(uuid, uuid_size);
3900 /* setup quota master target on MDT0 */
3901 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
3902 struct lustre_cfg *cfg)
3904 struct obd_device *obd;
3905 char *dev = lustre_cfg_string(cfg, 0);
3906 char *qmtname, *uuid, *p;
3907 struct lustre_cfg_bufs *bufs;
3908 struct lustre_cfg *lcfg;
3909 struct lustre_profile *lprof;
3910 struct obd_connect_data *data;
3914 LASSERT(mdt->mdt_qmt_exp == NULL);
3915 LASSERT(mdt->mdt_qmt_dev == NULL);
3917 /* quota master is on MDT0 only for now */
3918 if (mdt->mdt_seq_site.ss_node_id != 0)
3921 /* MGS generates config commands which look as follows:
3922 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
3923 * 3:lustre-MDT0000-mdtlov 4:f
3925 * We generate the QMT name from the MDT one, just replacing MD with QM
3926 * after all the preparations, the logical equivalent will be:
3927 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
3928 * 3:lustre-MDT0000-osd 4:f */
3929 OBD_ALLOC(qmtname, MAX_OBD_NAME);
3930 OBD_ALLOC(uuid, UUID_MAX);
3931 OBD_ALLOC_PTR(bufs);
3932 OBD_ALLOC_PTR(data);
3933 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
3934 GOTO(cleanup_mem, rc = -ENOMEM);
3936 strcpy(qmtname, dev);
3937 p = strstr(qmtname, "-MDT");
3939 GOTO(cleanup_mem, rc = -ENOMEM);
3940 /* replace MD with QM */
3944 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
3946 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
3947 if (lprof == NULL || lprof->lp_dt == NULL) {
3948 CERROR("can't find profile for %s\n",
3949 lustre_cfg_string(cfg, 0));
3950 GOTO(cleanup_mem, rc = -EINVAL);
3953 lustre_cfg_bufs_reset(bufs, qmtname);
3954 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
3955 lustre_cfg_bufs_set_string(bufs, 2, uuid);
3956 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
3958 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
3960 GOTO(cleanup_mem, rc = -ENOMEM);
3962 rc = class_attach(lcfg);
3964 GOTO(lcfg_cleanup, rc);
3966 obd = class_name2obd(qmtname);
3968 CERROR("Can not find obd %s (%s in config)\n", qmtname,
3969 lustre_cfg_string(cfg, 0));
3970 GOTO(class_detach, rc = -EINVAL);
3973 lustre_cfg_free(lcfg);
3975 lustre_cfg_bufs_reset(bufs, qmtname);
3976 lustre_cfg_bufs_set_string(bufs, 1, uuid);
3977 lustre_cfg_bufs_set_string(bufs, 2, dev);
3979 /* for quota, the next device should be the OSD device */
3980 lustre_cfg_bufs_set_string(bufs, 3,
3981 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
3983 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
3985 rc = class_setup(obd, lcfg);
3987 GOTO(class_detach, rc);
3989 mdt->mdt_qmt_dev = obd->obd_lu_dev;
3991 /* configure local quota objects */
3992 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
3996 GOTO(class_cleanup, rc);
3998 /* connect to quota master target */
3999 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4000 data->ocd_version = LUSTRE_VERSION_CODE;
4001 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4004 CERROR("cannot connect to quota master device %s (%d)\n",
4006 GOTO(class_cleanup, rc);
4012 class_manual_cleanup(obd);
4013 mdt->mdt_qmt_dev = NULL;
4017 class_detach(obd, lcfg);
4019 lustre_cfg_free(lcfg);
4024 OBD_FREE(qmtname, MAX_OBD_NAME);
4026 OBD_FREE(uuid, UUID_MAX);
4032 /* Shutdown quota master target associated with mdt */
4033 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4037 if (mdt->mdt_qmt_exp == NULL)
4039 LASSERT(mdt->mdt_qmt_dev != NULL);
4041 /* the qmt automatically shuts down when the mdt disconnects */
4042 obd_disconnect(mdt->mdt_qmt_exp);
4043 mdt->mdt_qmt_exp = NULL;
4044 mdt->mdt_qmt_dev = NULL;
4048 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4049 * for now. This will be removed along with converting rest of MDT code
4050 * to use tgt_session_info */
4051 int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4053 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4056 rc = mdt_getxattr(info);
4058 mdt_thread_info_fini(info);
4062 static struct tgt_handler mdt_tgt_handlers[] = {
4063 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4064 0, MDS_CONNECT, mdt_tgt_connect,
4065 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4066 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4067 0, MDS_DISCONNECT, tgt_disconnect,
4068 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4069 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4070 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4071 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4072 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4073 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GETSTATUS, mdt_getstatus),
4074 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4075 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4077 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4078 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4079 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4080 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4081 TGT_MDT_HDL(HABEO_CORPUS, MDS_DONE_WRITING,
4083 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4084 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4085 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_IS_SUBDIR, mdt_is_subdir),
4086 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4087 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4089 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4090 mdt_hsm_ct_register),
4091 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4092 mdt_hsm_ct_unregister),
4093 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4095 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4097 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4098 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4100 TGT_MDT_HDL(HABEO_CORPUS|HABEO_REFERO | MUTABOR, MDS_SWAP_LAYOUTS,
4104 static struct tgt_handler mdt_sec_ctx_ops[] = {
4105 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4106 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4107 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4110 static struct tgt_handler mdt_quota_ops[] = {
4111 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4114 static struct tgt_opc_slice mdt_common_slice[] = {
4116 .tos_opc_start = MDS_FIRST_OPC,
4117 .tos_opc_end = MDS_LAST_OPC,
4118 .tos_hs = mdt_tgt_handlers
4121 .tos_opc_start = OBD_FIRST_OPC,
4122 .tos_opc_end = OBD_LAST_OPC,
4123 .tos_hs = tgt_obd_handlers
4126 .tos_opc_start = LDLM_FIRST_OPC,
4127 .tos_opc_end = LDLM_LAST_OPC,
4128 .tos_hs = tgt_dlm_handlers
4131 .tos_opc_start = SEC_FIRST_OPC,
4132 .tos_opc_end = SEC_LAST_OPC,
4133 .tos_hs = mdt_sec_ctx_ops
4136 .tos_opc_start = UPDATE_OBJ,
4137 .tos_opc_end = UPDATE_LAST_OPC,
4138 .tos_hs = tgt_out_handlers
4141 .tos_opc_start = FLD_FIRST_OPC,
4142 .tos_opc_end = FLD_LAST_OPC,
4143 .tos_hs = fld_handlers
4146 .tos_opc_start = SEQ_FIRST_OPC,
4147 .tos_opc_end = SEQ_LAST_OPC,
4148 .tos_hs = seq_handlers
4151 .tos_opc_start = QUOTA_DQACQ,
4152 .tos_opc_end = QUOTA_LAST_OPC,
4153 .tos_hs = mdt_quota_ops
4156 .tos_opc_start = LLOG_FIRST_OPC,
4157 .tos_opc_end = LLOG_LAST_OPC,
4158 .tos_hs = tgt_llog_handlers
4166 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4168 struct md_device *next = m->mdt_child;
4169 struct lu_device *d = &m->mdt_lu_dev;
4170 struct obd_device *obd = mdt2obd_dev(m);
4173 target_recovery_fini(obd);
4175 ping_evictor_stop();
4177 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4179 if (m->mdt_opts.mo_coordinator)
4180 mdt_hsm_cdt_stop(m);
4182 mdt_hsm_cdt_fini(m);
4184 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4185 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4186 obd_exports_barrier(obd);
4187 obd_zombie_barrier();
4191 tgt_fini(env, &m->mdt_lut);
4192 mdt_fs_cleanup(env, m);
4193 upcall_cache_cleanup(m->mdt_identity_cache);
4194 m->mdt_identity_cache = NULL;
4196 if (m->mdt_namespace != NULL) {
4197 ldlm_namespace_free(m->mdt_namespace, NULL,
4198 d->ld_obd->obd_force);
4199 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4202 mdt_quota_fini(env, m);
4204 cfs_free_nidlist(&m->mdt_nosquash_nids);
4205 if (m->mdt_nosquash_str) {
4206 OBD_FREE(m->mdt_nosquash_str, m->mdt_nosquash_strlen);
4207 m->mdt_nosquash_str = NULL;
4208 m->mdt_nosquash_strlen = 0;
4211 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_PAUSE_LFSCK, 0, NULL);
4213 mdt_seq_fini(env, m);
4214 mdt_fld_fini(env, m);
4216 next->md_ops->mdo_init_capa_ctxt(env, next, 0, 0, 0, NULL);
4217 cfs_timer_disarm(&m->mdt_ck_timer);
4218 mdt_ck_thread_stop(m);
4223 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4225 LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
4227 server_put_mount(mdt_obd_name(m));
4232 int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4234 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4235 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4237 struct mdt_thread_info *info;
4238 struct obd_device *obd;
4239 const char *dev = lustre_cfg_string(cfg, 0);
4240 const char *num = lustre_cfg_string(cfg, 2);
4241 struct lustre_mount_info *lmi = NULL;
4242 struct lustre_sb_info *lsi;
4244 struct seq_server_site *ss_site;
4245 const char *identity_upcall = "NONE";
4246 struct md_device *next;
4252 lu_device_init(&m->mdt_lu_dev, ldt);
4254 * Environment (env) might be missing mdt_thread_key values at that
4255 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4258 * Usually device allocation path doesn't use module key values, but
4259 * mdt has to do a lot of work here, so allocate key value.
4261 rc = lu_env_refill((struct lu_env *)env);
4265 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4266 LASSERT(info != NULL);
4268 obd = class_name2obd(dev);
4269 LASSERT(obd != NULL);
4271 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4273 m->mdt_som_conf = 0;
4275 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4277 /* default is coordinator off, it is started through conf_param
4279 m->mdt_opts.mo_coordinator = 0;
4281 lmi = server_get_mount(dev);
4283 CERROR("Cannot get mount info for %s!\n", dev);
4286 lsi = s2lsi(lmi->lmi_sb);
4287 /* CMD is supported only in IAM mode */
4289 node_id = simple_strtol(num, NULL, 10);
4290 obd->u.obt.obt_magic = OBT_MAGIC;
4293 spin_lock_init(&m->mdt_ioepoch_lock);
4294 m->mdt_capa_timeout = CAPA_TIMEOUT;
4295 m->mdt_capa_alg = CAPA_HMAC_ALG_SHA1;
4296 m->mdt_ck_timeout = CAPA_KEY_TIMEOUT;
4297 m->mdt_squash_uid = 0;
4298 m->mdt_squash_gid = 0;
4299 CFS_INIT_LIST_HEAD(&m->mdt_nosquash_nids);
4300 m->mdt_nosquash_str = NULL;
4301 m->mdt_nosquash_strlen = 0;
4302 init_rwsem(&m->mdt_squash_sem);
4303 spin_lock_init(&m->mdt_osfs_lock);
4304 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4305 m->mdt_enable_remote_dir = 0;
4306 m->mdt_enable_remote_dir_gid = 0;
4308 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4309 m->mdt_lu_dev.ld_obd = obd;
4310 /* Set this lu_device to obd for error handling purposes. */
4311 obd->obd_lu_dev = &m->mdt_lu_dev;
4313 /* init the stack */
4314 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4316 CERROR("%s: Can't init device stack, rc %d\n",
4317 mdt_obd_name(m), rc);
4322 ss_site = mdt_seq_site(m);
4323 s->ld_seq_site = ss_site;
4326 /* set server index */
4327 ss_site->ss_node_id = node_id;
4329 /* failover is the default
4330 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4331 * assumed whose ss_node_id == 0 XXX
4333 obd->obd_replayable = 1;
4334 /* No connection accepted until configurations will finish */
4335 obd->obd_no_conn = 1;
4337 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4338 char *str = lustre_cfg_string(cfg, 4);
4339 if (strchr(str, 'n')) {
4340 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4341 obd->obd_replayable = 0;
4345 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4347 GOTO(err_fini_stack, rc);
4349 rc = mdt_seq_init(env, m);
4351 GOTO(err_fini_fld, rc);
4353 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4354 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4355 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4356 LDLM_NAMESPACE_SERVER,
4357 LDLM_NAMESPACE_GREEDY,
4359 if (m->mdt_namespace == NULL)
4360 GOTO(err_fini_seq, rc = -ENOMEM);
4362 m->mdt_namespace->ns_lvbp = m;
4363 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4365 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4366 /* set obd_namespace for compatibility with old code */
4367 obd->obd_namespace = m->mdt_namespace;
4369 cfs_timer_init(&m->mdt_ck_timer, mdt_ck_timer_callback, m);
4371 rc = mdt_hsm_cdt_init(m);
4373 CERROR("%s: error initializing coordinator, rc %d\n",
4374 mdt_obd_name(m), rc);
4375 GOTO(err_free_ns, rc);
4378 rc = mdt_ck_thread_start(m);
4380 GOTO(err_free_hsm, rc);
4382 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4383 OBD_FAIL_MDS_ALL_REQUEST_NET,
4384 OBD_FAIL_MDS_ALL_REPLY_NET);
4388 rc = mdt_fs_setup(env, m, obd, lsi);
4392 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4394 next = m->mdt_child;
4395 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4398 GOTO(err_fs_cleanup, rc);
4400 if (mntopts & MNTOPT_USERXATTR)
4401 m->mdt_opts.mo_user_xattr = 1;
4403 m->mdt_opts.mo_user_xattr = 0;
4405 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4407 GOTO(err_fs_cleanup, rc);
4409 if (mntopts & MNTOPT_ACL)
4410 m->mdt_opts.mo_acl = 1;
4412 m->mdt_opts.mo_acl = 0;
4414 /* XXX: to support suppgid for ACL, we enable identity_upcall
4415 * by default, otherwise, maybe got unexpected -EACCESS. */
4416 if (m->mdt_opts.mo_acl)
4417 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4419 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4421 &mdt_identity_upcall_cache_ops);
4422 if (IS_ERR(m->mdt_identity_cache)) {
4423 rc = PTR_ERR(m->mdt_identity_cache);
4424 m->mdt_identity_cache = NULL;
4425 GOTO(err_fs_cleanup, rc);
4428 rc = mdt_procfs_init(m, dev);
4430 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4431 GOTO(err_recovery, rc);
4434 rc = mdt_quota_init(env, m, cfg);
4436 GOTO(err_procfs, rc);
4438 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4439 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4440 "mdt_ldlm_client", m->mdt_ldlm_client);
4442 ping_evictor_start();
4444 /* recovery will be started upon mdt_prepare()
4445 * when the whole stack is complete and ready
4446 * to serve the requests */
4448 mdt_init_capa_ctxt(env, m);
4450 /* Reduce the initial timeout on an MDS because it doesn't need such
4451 * a long timeout as an OST does. Adaptive timeouts will adjust this
4452 * value appropriately. */
4453 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4454 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4460 target_recovery_fini(obd);
4461 upcall_cache_cleanup(m->mdt_identity_cache);
4462 m->mdt_identity_cache = NULL;
4464 mdt_fs_cleanup(env, m);
4466 tgt_fini(env, &m->mdt_lut);
4468 cfs_timer_disarm(&m->mdt_ck_timer);
4469 mdt_ck_thread_stop(m);
4471 mdt_hsm_cdt_fini(m);
4473 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4474 obd->obd_namespace = m->mdt_namespace = NULL;
4476 mdt_seq_fini(env, m);
4478 mdt_fld_fini(env, m);
4480 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4483 server_put_mount(dev);
4487 /* For interoperability, the left element is old parameter, the right one
4488 * is the new version of the parameter, if some parameter is deprecated,
4489 * the new version should be set as NULL. */
4490 static struct cfg_interop_param mdt_interop_param[] = {
4491 { "mdt.group_upcall", NULL },
4492 { "mdt.quota_type", NULL },
4493 { "mdd.quota_type", NULL },
4494 { "mdt.rootsquash", "mdt.root_squash" },
4495 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4499 /* used by MGS to process specific configurations */
4500 static int mdt_process_config(const struct lu_env *env,
4501 struct lu_device *d, struct lustre_cfg *cfg)
4503 struct mdt_device *m = mdt_dev(d);
4504 struct md_device *md_next = m->mdt_child;
4505 struct lu_device *next = md2lu_dev(md_next);
4509 switch (cfg->lcfg_command) {
4511 struct lprocfs_static_vars lvars;
4512 struct obd_device *obd = d->ld_obd;
4514 /* For interoperability */
4515 struct cfg_interop_param *ptr = NULL;
4516 struct lustre_cfg *old_cfg = NULL;
4519 param = lustre_cfg_string(cfg, 1);
4520 if (param == NULL) {
4521 CERROR("param is empty\n");
4526 ptr = class_find_old_param(param, mdt_interop_param);
4528 if (ptr->new_param == NULL) {
4530 CWARN("For interoperability, skip this %s."
4531 " It is obsolete.\n", ptr->old_param);
4535 CWARN("Found old param %s, changed it to %s.\n",
4536 ptr->old_param, ptr->new_param);
4539 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4546 lprocfs_mdt_init_vars(&lvars);
4547 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars,
4549 if (rc > 0 || rc == -ENOSYS) {
4550 /* is it an HSM var ? */
4551 rc = class_process_proc_param(PARAM_HSM,
4552 hsm_cdt_get_proc_vars(),
4554 if (rc > 0 || rc == -ENOSYS)
4555 /* we don't understand; pass it on */
4556 rc = next->ld_ops->ldo_process_config(env, next,
4560 if (old_cfg != NULL)
4561 lustre_cfg_free(cfg);
4566 /* others are passed further */
4567 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4573 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4574 const struct lu_object_header *hdr,
4575 struct lu_device *d)
4577 struct mdt_object *mo;
4581 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, __GFP_IO);
4583 struct lu_object *o;
4584 struct lu_object_header *h;
4587 h = &mo->mot_header;
4588 lu_object_header_init(h);
4589 lu_object_init(o, h, d);
4590 lu_object_add_top(h, o);
4591 o->lo_ops = &mdt_obj_ops;
4592 mutex_init(&mo->mot_ioepoch_mutex);
4593 mutex_init(&mo->mot_lov_mutex);
4594 init_rwsem(&mo->mot_open_sem);
4600 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4601 const struct lu_object_conf *unused)
4603 struct mdt_device *d = mdt_dev(o->lo_dev);
4604 struct lu_device *under;
4605 struct lu_object *below;
4609 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4610 PFID(lu_object_fid(o)));
4612 under = &d->mdt_child->md_lu_dev;
4613 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4614 if (below != NULL) {
4615 lu_object_add(o, below);
4622 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4624 struct mdt_object *mo = mdt_obj(o);
4625 struct lu_object_header *h;
4629 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4630 PFID(lu_object_fid(o)));
4632 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4633 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4636 lu_object_header_fini(h);
4637 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4642 static int mdt_object_print(const struct lu_env *env, void *cookie,
4643 lu_printer_t p, const struct lu_object *o)
4645 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4646 return (*p)(env, cookie, LUSTRE_MDT_NAME"-object@%p(ioepoch="LPU64" "
4647 "flags="LPX64", epochcount=%d, writecount=%d)",
4648 mdto, mdto->mot_ioepoch, mdto->mot_flags,
4649 mdto->mot_ioepoch_count, mdto->mot_writecount);
4652 static int mdt_prepare(const struct lu_env *env,
4653 struct lu_device *pdev,
4654 struct lu_device *cdev)
4656 struct mdt_device *mdt = mdt_dev(cdev);
4657 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4658 struct obd_device *obd = cdev->ld_obd;
4659 struct lfsck_start_param lsp;
4666 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4670 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4674 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4678 lsp.lsp_start = NULL;
4679 lsp.lsp_namespace = mdt->mdt_namespace;
4680 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
4681 OBD_IOC_START_LFSCK,
4684 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
4685 mdt_obd_name(mdt), rc);
4689 if (mdt->mdt_seq_site.ss_node_id == 0) {
4690 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
4691 &mdt->mdt_md_root_fid);
4696 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
4697 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
4698 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
4699 LASSERT(obd->obd_no_conn);
4700 spin_lock(&obd->obd_dev_lock);
4701 obd->obd_no_conn = 0;
4702 spin_unlock(&obd->obd_dev_lock);
4704 if (obd->obd_recovering == 0)
4705 mdt_postrecov(env, mdt);
4710 const struct lu_device_operations mdt_lu_ops = {
4711 .ldo_object_alloc = mdt_object_alloc,
4712 .ldo_process_config = mdt_process_config,
4713 .ldo_prepare = mdt_prepare,
4716 static const struct lu_object_operations mdt_obj_ops = {
4717 .loo_object_init = mdt_object_init,
4718 .loo_object_free = mdt_object_free,
4719 .loo_object_print = mdt_object_print
4722 static int mdt_obd_set_info_async(const struct lu_env *env,
4723 struct obd_export *exp,
4724 __u32 keylen, void *key,
4725 __u32 vallen, void *val,
4726 struct ptlrpc_request_set *set)
4732 if (KEY_IS(KEY_SPTLRPC_CONF)) {
4733 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
4741 * Match client and server connection feature flags.
4743 * Compute the compatibility flags for a connection request based on
4744 * features mutually supported by client and server.
4746 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
4747 * must not be updated here, otherwise a partially initialized value may
4748 * be exposed. After the connection request is successfully processed,
4749 * the top-level MDT connect request handler atomically updates the export
4750 * connect flags from the obd_connect_data::ocd_connect_flags field of the
4751 * reply. \see mdt_connect().
4753 * \param exp the obd_export associated with this client/target pair
4754 * \param mdt the target device for the connection
4755 * \param data stores data for this connect request
4758 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
4759 * \retval -EBADE client and server feature requirements are incompatible
4761 static int mdt_connect_internal(struct obd_export *exp,
4762 struct mdt_device *mdt,
4763 struct obd_connect_data *data)
4765 LASSERT(data != NULL);
4767 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
4768 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
4770 /* If no known bits (which should not happen, probably,
4771 as everybody should support LOOKUP and UPDATE bits at least)
4772 revert to compat mode with plain locks. */
4773 if (!data->ocd_ibits_known &&
4774 data->ocd_connect_flags & OBD_CONNECT_IBITS)
4775 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
4777 if (!mdt->mdt_opts.mo_acl)
4778 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
4780 if (!mdt->mdt_opts.mo_user_xattr)
4781 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
4783 if (!mdt->mdt_som_conf)
4784 data->ocd_connect_flags &= ~OBD_CONNECT_SOM;
4786 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
4787 data->ocd_brw_size = min(data->ocd_brw_size,
4788 (__u32)MD_MAX_BRW_SIZE);
4789 if (data->ocd_brw_size == 0) {
4790 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
4791 " ocd_version: %x ocd_grant: %d "
4792 "ocd_index: %u ocd_brw_size is "
4793 "unexpectedly zero, network data "
4794 "corruption? Refusing connection of this"
4797 exp->exp_client_uuid.uuid,
4798 exp, data->ocd_connect_flags, data->ocd_version,
4799 data->ocd_grant, data->ocd_index);
4804 /* NB: Disregard the rule against updating
4805 * exp_connect_data.ocd_connect_flags in this case, since
4806 * tgt_client_new() needs to know if this is a lightweight
4807 * connection, and it is safe to expose this flag before
4808 * connection processing completes. */
4809 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
4810 spin_lock(&exp->exp_lock);
4811 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
4812 spin_unlock(&exp->exp_lock);
4815 data->ocd_version = LUSTRE_VERSION_CODE;
4817 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
4818 CWARN("%s: MDS requires FID support, but client not\n",
4823 if (mdt->mdt_som_conf &&
4824 !(data->ocd_connect_flags & (OBD_CONNECT_LIGHTWEIGHT |
4825 OBD_CONNECT_MDS_MDS |
4826 OBD_CONNECT_SOM))) {
4827 CWARN("%s: MDS has SOM enabled, but client does not support "
4828 "it\n", mdt_obd_name(mdt));
4832 if (OCD_HAS_FLAG(data, PINGLESS)) {
4833 if (ptlrpc_pinger_suppress_pings()) {
4834 spin_lock(&exp->exp_obd->obd_dev_lock);
4835 list_del_init(&exp->exp_obd_chain_timed);
4836 spin_unlock(&exp->exp_obd->obd_dev_lock);
4838 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
4842 data->ocd_max_easize = mdt->mdt_max_ea_size;
4847 /* mds_connect copy */
4848 static int mdt_obd_connect(const struct lu_env *env,
4849 struct obd_export **exp, struct obd_device *obd,
4850 struct obd_uuid *cluuid,
4851 struct obd_connect_data *data,
4854 struct obd_export *lexp;
4855 struct lustre_handle conn = { 0 };
4856 struct mdt_device *mdt;
4860 LASSERT(env != NULL);
4861 if (!exp || !obd || !cluuid)
4864 mdt = mdt_dev(obd->obd_lu_dev);
4867 * first, check whether the stack is ready to handle requests
4868 * XXX: probably not very appropriate method is used now
4869 * at some point we should find a better one
4871 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) && data != NULL &&
4872 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
4873 rc = obd_get_info(env, mdt->mdt_child_exp,
4874 sizeof(KEY_OSP_CONNECTED),
4875 KEY_OSP_CONNECTED, NULL, NULL, NULL);
4878 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
4881 rc = class_connect(&conn, obd, cluuid);
4885 lexp = class_conn2export(&conn);
4886 LASSERT(lexp != NULL);
4888 rc = mdt_connect_internal(lexp, mdt, data);
4890 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
4893 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
4894 rc = tgt_client_new(env, lexp);
4896 mdt_export_stats_init(obd, lexp, localdata);
4900 class_disconnect(lexp);
4909 static int mdt_obd_reconnect(const struct lu_env *env,
4910 struct obd_export *exp, struct obd_device *obd,
4911 struct obd_uuid *cluuid,
4912 struct obd_connect_data *data,
4918 if (exp == NULL || obd == NULL || cluuid == NULL)
4921 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
4923 mdt_export_stats_init(obd, exp, localdata);
4928 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
4929 struct mdt_thread_info *info,
4930 struct mdt_file_data *mfd)
4932 struct lu_context ses;
4936 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
4941 lu_context_enter(&ses);
4943 mdt_ucred(info)->uc_valid = UCRED_OLD;
4944 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
4946 lu_context_exit(&ses);
4947 lu_context_fini(&ses);
4953 static int mdt_export_cleanup(struct obd_export *exp)
4955 struct mdt_export_data *med = &exp->exp_mdt_data;
4956 struct obd_device *obd = exp->exp_obd;
4957 struct mdt_device *mdt;
4958 struct mdt_thread_info *info;
4960 CFS_LIST_HEAD(closing_list);
4961 struct mdt_file_data *mfd, *n;
4965 spin_lock(&med->med_open_lock);
4966 while (!cfs_list_empty(&med->med_open_head)) {
4967 cfs_list_t *tmp = med->med_open_head.next;
4968 mfd = cfs_list_entry(tmp, struct mdt_file_data, mfd_list);
4970 /* Remove mfd handle so it can't be found again.
4971 * We are consuming the mfd_list reference here. */
4972 class_handle_unhash(&mfd->mfd_handle);
4973 cfs_list_move_tail(&mfd->mfd_list, &closing_list);
4975 spin_unlock(&med->med_open_lock);
4976 mdt = mdt_dev(obd->obd_lu_dev);
4977 LASSERT(mdt != NULL);
4979 rc = lu_env_init(&env, LCT_MD_THREAD);
4983 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
4984 LASSERT(info != NULL);
4985 memset(info, 0, sizeof *info);
4986 info->mti_env = &env;
4987 info->mti_mdt = mdt;
4988 info->mti_exp = exp;
4990 if (!cfs_list_empty(&closing_list)) {
4991 struct md_attr *ma = &info->mti_attr;
4993 /* Close any open files (which may also cause orphan unlinking). */
4994 cfs_list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
4995 cfs_list_del_init(&mfd->mfd_list);
4996 ma->ma_need = ma->ma_valid = 0;
4998 /* This file is being closed due to an eviction, it
4999 * could have been modified and now dirty regarding to
5000 * HSM archive, check this!
5001 * The logic here is to mark a file dirty if there's a
5002 * chance it was dirtied before the client was evicted,
5003 * so that we don't have to wait for a release attempt
5004 * before finding out the file was actually dirty and
5005 * fail the release. Aggressively marking it dirty here
5006 * will cause the policy engine to attempt to
5007 * re-archive it; when rearchiving, we can compare the
5008 * current version to the HSM data_version and make the
5009 * archive request into a noop if it's not actually
5012 if (mfd->mfd_mode & (FMODE_WRITE|MDS_FMODE_TRUNC))
5013 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5015 /* Don't unlink orphan on failover umount, LU-184 */
5016 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5017 ma->ma_valid = MA_FLAGS;
5018 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5020 mdt_mfd_close(info, mfd);
5023 info->mti_mdt = NULL;
5024 /* cleanup client slot early */
5025 /* Do not erase record for recoverable client. */
5026 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5027 tgt_client_del(&env, exp);
5033 static int mdt_obd_disconnect(struct obd_export *exp)
5039 class_export_get(exp);
5041 rc = server_disconnect_export(exp);
5043 CDEBUG(D_IOCTL, "server disconnect error: %d\n", rc);
5045 rc = mdt_export_cleanup(exp);
5046 class_export_put(exp);
5050 /* FIXME: Can we avoid using these two interfaces? */
5051 static int mdt_init_export(struct obd_export *exp)
5053 struct mdt_export_data *med = &exp->exp_mdt_data;
5057 CFS_INIT_LIST_HEAD(&med->med_open_head);
5058 spin_lock_init(&med->med_open_lock);
5059 mutex_init(&med->med_idmap_mutex);
5060 med->med_idmap = NULL;
5061 spin_lock(&exp->exp_lock);
5062 exp->exp_connecting = 1;
5063 spin_unlock(&exp->exp_lock);
5065 /* self-export doesn't need client data and ldlm initialization */
5066 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5067 &exp->exp_client_uuid)))
5070 rc = tgt_client_alloc(exp);
5074 rc = ldlm_init_export(exp);
5081 tgt_client_free(exp);
5083 CERROR("%s: Failed to initialize export: rc = %d\n",
5084 exp->exp_obd->obd_name, rc);
5088 static int mdt_destroy_export(struct obd_export *exp)
5092 if (exp_connect_rmtclient(exp))
5093 mdt_cleanup_idmap(&exp->exp_mdt_data);
5095 target_destroy_export(exp);
5096 /* destroy can be called from failed obd_setup, so
5097 * checking uuid is safer than obd_self_export */
5098 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5099 &exp->exp_client_uuid)))
5102 ldlm_destroy_export(exp);
5103 tgt_client_free(exp);
5105 LASSERT(cfs_list_empty(&exp->exp_outstanding_replies));
5106 LASSERT(cfs_list_empty(&exp->exp_mdt_data.med_open_head));
5111 /** The maximum depth that fid2path() will search.
5112 * This is limited only because we want to store the fids for
5113 * historical path lookup purposes.
5115 #define MAX_PATH_DEPTH 100
5117 /** mdt_path() lookup structure. */
5118 struct path_lookup_info {
5119 __u64 pli_recno; /**< history point */
5120 __u64 pli_currec; /**< current record */
5121 struct lu_fid pli_fid;
5122 struct lu_fid pli_fids[MAX_PATH_DEPTH]; /**< path, in fids */
5123 struct mdt_object *pli_mdt_obj;
5124 char *pli_path; /**< full path */
5126 int pli_linkno; /**< which hardlink to follow */
5127 int pli_fidcount; /**< number of \a pli_fids */
5130 static int mdt_links_read(struct mdt_thread_info *info,
5131 struct mdt_object *mdt_obj, struct linkea_data *ldata)
5135 LASSERT(ldata->ld_buf->lb_buf != NULL);
5137 if (!mdt_object_exists(mdt_obj))
5140 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5141 ldata->ld_buf, XATTR_NAME_LINK);
5142 if (rc == -ERANGE) {
5143 /* Buf was too small, figure out what we need. */
5144 lu_buf_free(ldata->ld_buf);
5145 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5146 ldata->ld_buf, XATTR_NAME_LINK);
5149 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5150 if (ldata->ld_buf->lb_buf == NULL)
5152 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5153 ldata->ld_buf, XATTR_NAME_LINK);
5158 return linkea_init(ldata);
5161 static int mdt_path_current(struct mdt_thread_info *info,
5162 struct path_lookup_info *pli)
5164 struct mdt_device *mdt = info->mti_mdt;
5165 struct mdt_object *mdt_obj;
5166 struct link_ea_header *leh;
5167 struct link_ea_entry *lee;
5168 struct lu_name *tmpname = &info->mti_name;
5169 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5170 struct lu_buf *buf = &info->mti_big_buf;
5173 struct linkea_data ldata = { 0 };
5177 /* temp buffer for path element, the buffer will be finally freed
5178 * in mdt_thread_info_fini */
5179 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5180 if (buf->lb_buf == NULL)
5184 ptr = pli->pli_path + pli->pli_pathlen - 1;
5187 pli->pli_fidcount = 0;
5188 pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
5190 /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
5191 * so checking root_fid can only happen on MDT0. */
5192 while (!lu_fid_eq(&mdt->mdt_md_root_fid,
5193 &pli->pli_fids[pli->pli_fidcount])) {
5194 mdt_obj = mdt_object_find(info->mti_env, mdt,
5195 &pli->pli_fids[pli->pli_fidcount]);
5196 if (IS_ERR(mdt_obj))
5197 GOTO(out, rc = PTR_ERR(mdt_obj));
5198 if (mdt_object_remote(mdt_obj)) {
5199 mdt_object_put(info->mti_env, mdt_obj);
5200 GOTO(remote_out, rc = -EREMOTE);
5202 if (!mdt_object_exists(mdt_obj)) {
5203 mdt_object_put(info->mti_env, mdt_obj);
5204 GOTO(out, rc = -ENOENT);
5207 rc = mdt_links_read(info, mdt_obj, &ldata);
5208 mdt_object_put(info->mti_env, mdt_obj);
5213 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5214 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5215 /* If set, use link #linkno for path lookup, otherwise use
5216 link #0. Only do this for the final path element. */
5217 if (pli->pli_fidcount == 0 &&
5218 pli->pli_linkno < leh->leh_reccount) {
5220 for (count = 0; count < pli->pli_linkno; count++) {
5221 lee = (struct link_ea_entry *)
5222 ((char *)lee + reclen);
5223 linkea_entry_unpack(lee, &reclen, tmpname,
5226 if (pli->pli_linkno < leh->leh_reccount - 1)
5227 /* indicate to user there are more links */
5231 /* Pack the name in the end of the buffer */
5232 ptr -= tmpname->ln_namelen;
5233 if (ptr - 1 <= pli->pli_path)
5234 GOTO(out, rc = -EOVERFLOW);
5235 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5238 /* Store the parent fid for historic lookup */
5239 if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
5240 GOTO(out, rc = -EOVERFLOW);
5241 pli->pli_fids[pli->pli_fidcount] = *tmpfid;
5245 ptr++; /* skip leading / */
5246 memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
5253 /* Returns the full path to this fid, as of changelog record recno. */
5254 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5255 char *path, int pathlen, __u64 *recno, int *linkno,
5258 struct mdt_device *mdt = info->mti_mdt;
5259 struct path_lookup_info *pli;
5267 if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
5276 pli->pli_mdt_obj = obj;
5277 pli->pli_recno = *recno;
5278 pli->pli_path = path;
5279 pli->pli_pathlen = pathlen;
5280 pli->pli_linkno = *linkno;
5282 /* Retry multiple times in case file is being moved */
5283 while (tries-- && rc == -EAGAIN)
5284 rc = mdt_path_current(info, pli);
5286 /* return the last resolved fids to the client, so the client will
5287 * build the left path on another MDT for remote object */
5288 *fid = pli->pli_fids[pli->pli_fidcount];
5290 *recno = pli->pli_currec;
5291 /* Return next link index to caller */
5292 *linkno = pli->pli_linkno;
5299 static int mdt_fid2path(struct mdt_thread_info *info,
5300 struct getinfo_fid2path *fp)
5302 struct mdt_device *mdt = info->mti_mdt;
5303 struct mdt_object *obj;
5307 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5308 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5310 if (!fid_is_sane(&fp->gf_fid))
5313 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5314 CWARN("%s: "DFID" is invalid, sequence should be "
5315 ">= "LPX64"\n", mdt_obd_name(mdt),
5316 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5320 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5321 if (obj == NULL || IS_ERR(obj)) {
5322 CDEBUG(D_IOCTL, "no object "DFID": %ld\n", PFID(&fp->gf_fid),
5327 if (mdt_object_remote(obj))
5329 else if (!mdt_object_exists(obj))
5335 mdt_object_put(info->mti_env, obj);
5336 CDEBUG(D_IOCTL, "nonlocal object "DFID": %d\n",
5337 PFID(&fp->gf_fid), rc);
5341 rc = mdt_path(info, obj, fp->gf_path, fp->gf_pathlen, &fp->gf_recno,
5342 &fp->gf_linkno, &fp->gf_fid);
5344 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5345 PFID(&fp->gf_fid), fp->gf_path, fp->gf_recno, fp->gf_linkno);
5347 mdt_object_put(info->mti_env, obj);
5352 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key,
5353 void *val, int vallen)
5355 struct getinfo_fid2path *fpout, *fpin;
5358 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5361 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5362 lustre_swab_fid2path(fpin);
5364 memcpy(fpout, fpin, sizeof(*fpin));
5365 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5368 rc = mdt_fid2path(info, fpout);
5372 int mdt_get_info(struct tgt_session_info *tsi)
5382 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5384 CDEBUG(D_IOCTL, "No GETINFO key");
5385 RETURN(err_serious(-EFAULT));
5387 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5390 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5391 if (vallen == NULL) {
5392 CDEBUG(D_IOCTL, "Unable to get RMF_GETINFO_VALLEN buffer");
5393 RETURN(err_serious(-EFAULT));
5396 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5398 rc = req_capsule_server_pack(tsi->tsi_pill);
5400 RETURN(err_serious(rc));
5402 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5403 if (valout == NULL) {
5404 CDEBUG(D_IOCTL, "Unable to get get-info RPC out buffer");
5405 RETURN(err_serious(-EFAULT));
5408 if (KEY_IS(KEY_FID2PATH)) {
5409 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5411 rc = mdt_rpc_fid2path(info, key, valout, *vallen);
5412 mdt_thread_info_fini(info);
5419 /* Pass the ioc down */
5420 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5421 unsigned int cmd, int len, void *data)
5423 struct lu_context ioctl_session;
5424 struct md_device *next = mdt->mdt_child;
5428 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5431 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5432 lu_context_enter(&ioctl_session);
5433 env->le_ses = &ioctl_session;
5435 LASSERT(next->md_ops->mdo_iocontrol);
5436 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5438 lu_context_exit(&ioctl_session);
5439 lu_context_fini(&ioctl_session);
5443 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5445 struct obd_ioctl_data *data = karg;
5448 struct mdt_object *obj;
5449 struct mdt_lock_handle *lh;
5453 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5454 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5457 fid = (struct lu_fid *)data->ioc_inlbuf1;
5459 if (!fid_is_sane(fid))
5462 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5464 lh = &mti->mti_lh[MDT_LH_PARENT];
5465 mdt_lock_reg_init(lh, LCK_CR);
5467 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5469 RETURN(PTR_ERR(obj));
5471 if (mdt_object_remote(obj)) {
5474 * before calling version get the correct MDS should be
5475 * fid, this is error to find remote object here
5477 CERROR("nonlocal object "DFID"\n", PFID(fid));
5478 } else if (!mdt_object_exists(obj)) {
5479 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5482 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5483 *(__u64 *)data->ioc_inlbuf2 = version;
5486 mdt_object_unlock_put(mti, obj, lh, 1);
5490 /* ioctls on obd dev */
5491 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5492 void *karg, void *uarg)
5495 struct obd_device *obd = exp->exp_obd;
5496 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5497 struct dt_device *dt = mdt->mdt_bottom;
5501 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5502 rc = lu_env_init(&env, LCT_MD_THREAD);
5508 rc = mdt_device_sync(&env, mdt);
5510 case OBD_IOC_SET_READONLY:
5511 rc = dt->dd_ops->dt_ro(&env, dt);
5513 case OBD_IOC_ABORT_RECOVERY:
5514 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5515 target_stop_recovery_thread(obd);
5518 case OBD_IOC_CHANGELOG_REG:
5519 case OBD_IOC_CHANGELOG_DEREG:
5520 case OBD_IOC_CHANGELOG_CLEAR:
5521 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5523 case OBD_IOC_START_LFSCK: {
5524 struct md_device *next = mdt->mdt_child;
5525 struct obd_ioctl_data *data = karg;
5526 struct lfsck_start_param lsp;
5528 if (unlikely(data == NULL)) {
5533 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5534 lsp.lsp_namespace = mdt->mdt_namespace;
5535 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5538 case OBD_IOC_STOP_LFSCK: {
5539 struct md_device *next = mdt->mdt_child;
5541 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, NULL);
5544 case OBD_IOC_GET_OBJ_VERSION: {
5545 struct mdt_thread_info *mti;
5546 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5547 memset(mti, 0, sizeof *mti);
5548 mti->mti_env = &env;
5552 rc = mdt_ioc_version_get(mti, karg);
5555 case OBD_IOC_CATLOGLIST:
5556 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg);
5560 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5561 mdt_obd_name(mdt), cmd, rc);
5568 int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
5570 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
5574 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
5578 int mdt_obd_postrecov(struct obd_device *obd)
5583 rc = lu_env_init(&env, LCT_MD_THREAD);
5586 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
5591 static struct obd_ops mdt_obd_device_ops = {
5592 .o_owner = THIS_MODULE,
5593 .o_set_info_async = mdt_obd_set_info_async,
5594 .o_connect = mdt_obd_connect,
5595 .o_reconnect = mdt_obd_reconnect,
5596 .o_disconnect = mdt_obd_disconnect,
5597 .o_init_export = mdt_init_export,
5598 .o_destroy_export = mdt_destroy_export,
5599 .o_iocontrol = mdt_iocontrol,
5600 .o_postrecov = mdt_obd_postrecov,
5603 static struct lu_device* mdt_device_fini(const struct lu_env *env,
5604 struct lu_device *d)
5606 struct mdt_device *m = mdt_dev(d);
5613 static struct lu_device *mdt_device_free(const struct lu_env *env,
5614 struct lu_device *d)
5616 struct mdt_device *m = mdt_dev(d);
5619 lu_device_fini(&m->mdt_lu_dev);
5625 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
5626 struct lu_device_type *t,
5627 struct lustre_cfg *cfg)
5629 struct lu_device *l;
5630 struct mdt_device *m;
5637 rc = mdt_init0(env, m, t, cfg);
5639 mdt_device_free(env, l);
5644 l = ERR_PTR(-ENOMEM);
5648 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
5649 LU_KEY_INIT(mdt, struct mdt_thread_info);
5651 static void mdt_key_fini(const struct lu_context *ctx,
5652 struct lu_context_key *key, void* data)
5654 struct mdt_thread_info *info = data;
5656 if (info->mti_big_lmm) {
5657 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
5658 info->mti_big_lmm = NULL;
5659 info->mti_big_lmmsize = 0;
5664 /* context key: mdt_thread_key */
5665 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
5667 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
5669 return lu_ucred(info->mti_env);
5672 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
5674 return lu_ucred_check(info->mti_env);
5678 * Enable/disable COS (Commit On Sharing).
5680 * Set/Clear the COS flag in mdt options.
5682 * \param mdt mdt device
5683 * \param val 0 disables COS, other values enable COS
5685 void mdt_enable_cos(struct mdt_device *mdt, int val)
5690 mdt->mdt_opts.mo_cos = !!val;
5691 rc = lu_env_init(&env, LCT_LOCAL);
5692 if (unlikely(rc != 0)) {
5693 CWARN("lu_env initialization failed with rc = %d,"
5694 "cannot sync\n", rc);
5697 mdt_device_sync(&env, mdt);
5702 * Check COS (Commit On Sharing) status.
5704 * Return COS flag status.
5706 * \param mdt mdt device
5708 int mdt_cos_is_enabled(struct mdt_device *mdt)
5710 return mdt->mdt_opts.mo_cos != 0;
5713 static struct lu_device_type_operations mdt_device_type_ops = {
5714 .ldto_device_alloc = mdt_device_alloc,
5715 .ldto_device_free = mdt_device_free,
5716 .ldto_device_fini = mdt_device_fini
5719 static struct lu_device_type mdt_device_type = {
5720 .ldt_tags = LU_DEVICE_MD,
5721 .ldt_name = LUSTRE_MDT_NAME,
5722 .ldt_ops = &mdt_device_type_ops,
5723 .ldt_ctx_tags = LCT_MD_THREAD
5726 static int __init mdt_mod_init(void)
5728 struct lprocfs_static_vars lvars;
5731 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
5732 FID_NOBRACE_LEN + 1);
5733 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
5735 rc = lu_kmem_init(mdt_caches);
5739 rc = mds_mod_init();
5743 lprocfs_mdt_init_vars(&lvars);
5744 rc = class_register_type(&mdt_obd_device_ops, NULL, NULL,
5745 #ifndef HAVE_ONLY_PROCFS_SEQ
5748 LUSTRE_MDT_NAME, &mdt_device_type);
5753 lu_kmem_fini(mdt_caches);
5760 static void __exit mdt_mod_exit(void)
5762 class_unregister_type(LUSTRE_MDT_NAME);
5764 lu_kmem_fini(mdt_caches);
5767 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
5768 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
5769 MODULE_LICENSE("GPL");
5771 cfs_module(mdt, LUSTRE_VERSION_STRING, mdt_mod_init, mdt_mod_exit);