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) 2011, 2015, 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_reint.c
38 * Lustre Metadata Target (mdt) reintegration routines
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Huang Hua <huanghua@clusterfs.com>
44 * Author: Yury Umanets <umka@clusterfs.com>
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <lprocfs_status.h>
50 #include "mdt_internal.h"
51 #include <lustre_lmv.h>
53 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
56 ma->ma_need = MA_INODE;
61 * Get version of object by fid.
63 * Return real version or ENOENT_VERSION if object doesn't exist
65 static void mdt_obj_version_get(struct mdt_thread_info *info,
66 struct mdt_object *o, __u64 *version)
69 if (mdt_object_exists(o) && !mdt_object_remote(o) &&
70 !fid_is_obf(mdt_object_fid(o)))
71 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
73 *version = ENOENT_VERSION;
74 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
75 PFID(mdt_object_fid(o)), *version);
79 * Check version is correct.
81 * Should be called only during replay.
83 static int mdt_version_check(struct ptlrpc_request *req,
84 __u64 version, int idx)
86 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
89 if (!exp_connect_vbr(req->rq_export))
92 LASSERT(req_is_replay(req));
93 /** VBR: version is checked always because costs nothing */
94 LASSERT(idx < PTLRPC_NUM_VERSIONS);
95 /** Sanity check for malformed buffers */
96 if (pre_ver == NULL) {
97 CERROR("No versions in request buffer\n");
98 spin_lock(&req->rq_export->exp_lock);
99 req->rq_export->exp_vbr_failed = 1;
100 spin_unlock(&req->rq_export->exp_lock);
102 } else if (pre_ver[idx] != version) {
103 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
104 pre_ver[idx], version);
105 spin_lock(&req->rq_export->exp_lock);
106 req->rq_export->exp_vbr_failed = 1;
107 spin_unlock(&req->rq_export->exp_lock);
114 * Save pre-versions in reply.
116 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
121 if (!exp_connect_vbr(req->rq_export))
124 LASSERT(!req_is_replay(req));
125 LASSERT(req->rq_repmsg != NULL);
126 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
128 reply_ver[idx] = version;
132 * Save enoent version, it is needed when it is obvious that object doesn't
133 * exist, e.g. child during create.
135 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
137 /* save version of file name for replay, it must be ENOENT here */
138 if (!req_is_replay(mdt_info_req(info))) {
139 info->mti_ver[idx] = ENOENT_VERSION;
140 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
145 * Get version from disk and save in reply buffer.
147 * Versions are saved in reply only during normal operations not replays.
149 void mdt_version_get_save(struct mdt_thread_info *info,
150 struct mdt_object *mto, int idx)
152 /* don't save versions during replay */
153 if (!req_is_replay(mdt_info_req(info))) {
154 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
155 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
160 * Get version from disk and check it, no save in reply.
162 int mdt_version_get_check(struct mdt_thread_info *info,
163 struct mdt_object *mto, int idx)
165 /* only check versions during replay */
166 if (!req_is_replay(mdt_info_req(info)))
169 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
170 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
174 * Get version from disk and check if recovery or just save.
176 int mdt_version_get_check_save(struct mdt_thread_info *info,
177 struct mdt_object *mto, int idx)
181 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
182 if (req_is_replay(mdt_info_req(info)))
183 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
186 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
191 * Lookup with version checking.
193 * This checks version of 'name'. Many reint functions uses 'name' for child not
194 * FID, therefore we need to get object by name and check its version.
196 static int mdt_lookup_version_check(struct mdt_thread_info *info,
197 struct mdt_object *p,
198 const struct lu_name *lname,
199 struct lu_fid *fid, int idx)
203 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
205 /* Check version only during replay */
206 if (!req_is_replay(mdt_info_req(info)))
209 info->mti_ver[idx] = ENOENT_VERSION;
211 struct mdt_object *child;
212 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
213 if (likely(!IS_ERR(child))) {
214 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
215 mdt_object_put(info->mti_env, child);
218 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
219 return vbrc ? vbrc : rc;
223 static inline int mdt_remote_permission_check(struct mdt_thread_info *info)
225 struct lu_ucred *uc = mdt_ucred(info);
226 struct mdt_device *mdt = info->mti_mdt;
228 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
229 if (uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
230 mdt->mdt_enable_remote_dir_gid != -1)
238 * mdt_remote_permission: Check whether the remote operation is permitted,
240 * Only sysadmin can create remote directory / striped directory,
241 * migrate directory and set default stripedEA on directory, unless
243 * lctl set_param mdt.*.enable_remote_dir_gid=allow_gid.
245 * param[in] info: mdt_thread_info.
247 * retval = 0 remote operation is allowed.
248 * < 0 remote operation is denied.
250 static int mdt_remote_permission(struct mdt_thread_info *info)
252 struct md_op_spec *spec = &info->mti_spec;
253 struct lu_attr *attr = &info->mti_attr.ma_attr;
254 struct obd_export *exp = mdt_info_req(info)->rq_export;
257 if (info->mti_rr.rr_opcode == REINT_MIGRATE) {
258 rc = mdt_remote_permission_check(info);
263 if (info->mti_rr.rr_opcode == REINT_CREATE &&
264 (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
265 spec->u.sp_ea.eadatalen != 0)) {
266 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
268 /* Only new clients can create remote dir( >= 2.4) and
269 * striped dir(>= 2.6), old client will return -ENOTSUPP */
270 if (!mdt_is_dne_client(exp))
273 if (le32_to_cpu(lum->lum_stripe_count) > 1 &&
274 !mdt_is_striped_client(exp))
277 rc = mdt_remote_permission_check(info);
282 if (info->mti_rr.rr_opcode == REINT_SETATTR) {
283 struct md_attr *ma = &info->mti_attr;
285 if ((ma->ma_valid & MA_LMV)) {
286 rc = mdt_remote_permission_check(info);
295 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
296 struct mdt_object *obj, __u64 ibits,
297 struct mdt_lock_handle *s0_lh,
298 struct mdt_object *s0_obj,
299 struct ldlm_enqueue_info *einfo,
302 union ldlm_policy_data *policy = &mti->mti_policy;
303 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
308 if (!S_ISDIR(obj->mot_header.loh_attr))
311 /* Unlock stripe 0 */
312 if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
313 LASSERT(s0_obj != NULL);
314 mdt_object_unlock_put(mti, s0_obj, s0_lh, decref);
317 memset(policy, 0, sizeof(*policy));
318 policy->l_inodebits.bits = ibits;
320 if (slave_locks != NULL) {
321 LASSERT(s0_lh != NULL);
322 for (i = 1; i < slave_locks->count; i++) {
323 /* borrow s0_lh temporarily to do mdt unlock */
324 mdt_lock_reg_init(s0_lh, einfo->ei_mode);
325 s0_lh->mlh_rreg_lh = slave_locks->handles[i];
326 mdt_object_unlock(mti, obj, s0_lh, decref);
327 slave_locks->handles[i].cookie = 0ull;
331 rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
336 static int mdt_init_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
339 struct lu_buf *buf = &mti->mti_buf;
340 struct lmv_mds_md_v1 *lmv;
344 if (!S_ISDIR(obj->mot_header.loh_attr))
347 buf->lb_buf = mti->mti_xattr_buf;
348 buf->lb_len = sizeof(mti->mti_xattr_buf);
349 rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
352 rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
354 buf->lb_buf = mti->mti_big_lmm;
355 buf->lb_len = mti->mti_big_lmmsize;
359 if (rc == -ENODATA || rc == -ENOENT)
366 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
369 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
375 * Lock slave stripes if necessary, the lock handles of slave stripes
376 * will be stored in einfo->ei_cbdata.
378 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
379 enum ldlm_mode mode, __u64 ibits,
380 struct lu_fid *s0_fid,
381 struct mdt_lock_handle *s0_lh,
382 struct mdt_object **s0_objp,
383 struct ldlm_enqueue_info *einfo)
385 union ldlm_policy_data *policy = &mti->mti_policy;
389 rc = mdt_init_slaves(mti, obj, s0_fid);
393 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
395 if (!lu_fid_eq(s0_fid, mdt_object_fid(obj))) {
396 /* Except migrating object, whose 0_stripe and master
397 * object are the same object, 0_stripe and master
398 * object are different, though they are in the same
399 * MDT, to avoid adding osd_object_lock here, so we
400 * will enqueue the stripe0 lock in MDT0 for now */
401 *s0_objp = mdt_object_find(mti->mti_env, mti->mti_mdt, s0_fid);
402 if (IS_ERR(*s0_objp))
403 RETURN(PTR_ERR(*s0_objp));
405 rc = mdt_reint_object_lock(mti, *s0_objp, s0_lh, ibits, true);
407 mdt_object_put(mti->mti_env, *s0_objp);
412 memset(einfo, 0, sizeof(*einfo));
413 einfo->ei_type = LDLM_IBITS;
414 einfo->ei_mode = mode;
415 einfo->ei_cb_bl = mdt_remote_blocking_ast;
416 einfo->ei_cb_local_bl = mdt_blocking_ast;
417 einfo->ei_cb_cp = ldlm_completion_ast;
418 einfo->ei_enq_slave = 1;
419 einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
420 memset(policy, 0, sizeof(*policy));
421 policy->l_inodebits.bits = ibits;
423 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
429 * VBR: we save three versions in reply:
430 * 0 - parent. Check that parent version is the same during replay.
431 * 1 - name. Version of 'name' if file exists with the same name or
432 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
433 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
436 static int mdt_md_create(struct mdt_thread_info *info)
438 struct mdt_device *mdt = info->mti_mdt;
439 struct mdt_object *parent;
440 struct mdt_object *child;
441 struct mdt_lock_handle *lh;
442 struct mdt_body *repbody;
443 struct md_attr *ma = &info->mti_attr;
444 struct mdt_reint_record *rr = &info->mti_rr;
448 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create ("DNAME"->"DFID") "
450 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
452 if (!fid_is_md_operative(rr->rr_fid1))
455 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
457 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
459 RETURN(PTR_ERR(parent));
461 if (!mdt_object_exists(parent))
462 GOTO(put_parent, rc = -ENOENT);
464 lh = &info->mti_lh[MDT_LH_PARENT];
465 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
466 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
468 GOTO(put_parent, rc);
470 if (!mdt_object_remote(parent)) {
471 rc = mdt_version_get_check_save(info, parent, 0);
473 GOTO(unlock_parent, rc);
477 * Check child name version during replay.
478 * During create replay a file may exist with same name.
480 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
481 &info->mti_tmp_fid1, 1);
483 GOTO(unlock_parent, rc = -EEXIST);
485 /* -ENOENT is expected here */
487 GOTO(unlock_parent, rc);
489 /* save version of file name for replay, it must be ENOENT here */
490 mdt_enoent_version_save(info, 1);
492 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
493 if (unlikely(IS_ERR(child)))
494 GOTO(unlock_parent, rc = PTR_ERR(child));
496 rc = mdt_remote_permission(info);
500 ma->ma_need = MA_INODE;
503 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
504 OBD_FAIL_MDS_REINT_CREATE_WRITE);
506 /* Version of child will be updated on disk. */
507 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
508 rc = mdt_version_get_check_save(info, child, 2);
512 /* Let lower layer know current lock mode. */
513 info->mti_spec.sp_cr_mode = mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
516 * Do not perform lookup sanity check. We know that name does
519 info->mti_spec.sp_cr_lookup = 0;
520 info->mti_spec.sp_feat = &dt_directory_features;
522 rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
523 mdt_object_child(child), &info->mti_spec, ma);
525 rc = mdt_attr_get_complex(info, child, ma);
531 * On DNE, we need to eliminate dependey between 'mkdir a' and
532 * 'mkdir a/b' if b is a striped directory, to achieve this, two
533 * things are done below:
534 * 1. save child and slaves lock.
535 * 2. if the child is a striped directory, relock parent so to
536 * compare against with COS locks to ensure parent was
539 if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
540 struct mdt_lock_handle *lhc;
541 struct mdt_lock_handle *s0_lh;
542 struct mdt_object *s0_obj = NULL;
543 struct ldlm_enqueue_info *einfo;
544 struct lu_fid *s0_fid = &info->mti_tmp_fid1;
545 bool cos_incompat = false;
547 rc = mdt_init_slaves(info, child, s0_fid);
550 if (!mdt_object_remote(parent)) {
551 mdt_object_unlock(info, parent, lh, 1);
552 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
553 rc = mdt_reint_object_lock(info, parent, lh,
554 MDS_INODELOCK_UPDATE,
561 einfo = &info->mti_einfo;
562 lhc = &info->mti_lh[MDT_LH_CHILD];
563 mdt_lock_handle_init(lhc);
564 mdt_lock_reg_init(lhc, LCK_PW);
565 rc = mdt_reint_object_lock(info, child, lhc,
566 MDS_INODELOCK_UPDATE,
570 mdt_object_unlock(info, child, lhc, rc);
572 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
573 mdt_lock_handle_init(s0_lh);
574 mdt_lock_reg_init(s0_lh, LCK_PW);
575 rc = mdt_lock_slaves(info, child, LCK_PW, MDS_INODELOCK_UPDATE,
576 s0_fid, s0_lh, &s0_obj, einfo);
577 mdt_unlock_slaves(info, child, MDS_INODELOCK_UPDATE, s0_lh,
579 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) && rc == -EIO)
583 /* Return fid & attr to client. */
584 if (ma->ma_valid & MA_INODE)
585 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
586 mdt_object_fid(child));
588 mdt_object_put(info->mti_env, child);
590 mdt_object_unlock(info, parent, lh, rc);
592 mdt_object_put(info->mti_env, parent);
596 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
599 struct mdt_lock_handle *lh;
600 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
601 __u64 lockpart = MDS_INODELOCK_UPDATE;
602 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
603 struct lu_fid *s0_fid = &info->mti_tmp_fid1;
604 struct mdt_lock_handle *s0_lh = NULL;
605 struct mdt_object *s0_obj = NULL;
606 bool cos_incompat = false;
610 rc = mdt_init_slaves(info, mo, s0_fid);
614 lh = &info->mti_lh[MDT_LH_PARENT];
615 mdt_lock_reg_init(lh, LCK_PW);
617 /* Even though the new MDT will grant PERM lock to the old
618 * client, but the old client will almost ignore that during
619 * So it needs to revoke both LOOKUP and PERM lock here, so
620 * both new and old client can cancel the dcache */
621 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
622 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
624 rc = mdt_reint_object_lock(info, mo, lh, lockpart, cos_incompat);
628 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
629 mdt_lock_reg_init(s0_lh, LCK_PW);
630 rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_fid, s0_lh, &s0_obj,
633 GOTO(out_unlock, rc);
635 /* all attrs are packed into mti_attr in unpack_setattr */
636 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
637 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
639 /* This is only for set ctime when rename's source is on remote MDS. */
640 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
641 ma->ma_attr_flags |= MDS_VTX_BYPASS;
643 /* VBR: update version if attr changed are important for recovery */
645 /* update on-disk version of changed object */
646 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
647 rc = mdt_version_get_check_save(info, mo, 0);
649 GOTO(out_unlock, rc);
652 /* Ensure constant striping during chown(). See LU-2789. */
653 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
654 mutex_lock(&mo->mot_lov_mutex);
656 /* all attrs are packed into mti_attr in unpack_setattr */
657 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
659 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
660 mutex_unlock(&mo->mot_lov_mutex);
663 GOTO(out_unlock, rc);
667 mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo, rc);
668 mdt_object_unlock(info, mo, lh, rc);
673 * Check HSM flags and add HS_DIRTY flag if relevant.
675 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
676 * and is not RELEASED.
678 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
684 /* If the file was modified, add the dirty flag */
685 ma->ma_need = MA_HSM;
686 rc = mdt_attr_get_complex(info, mo, ma);
688 CERROR("file attribute read error for "DFID": %d.\n",
689 PFID(mdt_object_fid(mo)), rc);
693 /* If an up2date copy exists in the backend, add dirty flag */
694 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
695 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
696 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
698 ma->ma_hsm.mh_flags |= HS_DIRTY;
700 mdt_lock_reg_init(lh, LCK_PW);
701 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR);
705 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
707 CERROR("file attribute change error for "DFID": %d\n",
708 PFID(mdt_object_fid(mo)), rc);
709 mdt_object_unlock(info, mo, lh, rc);
715 static int mdt_reint_setattr(struct mdt_thread_info *info,
716 struct mdt_lock_handle *lhc)
718 struct md_attr *ma = &info->mti_attr;
719 struct mdt_reint_record *rr = &info->mti_rr;
720 struct ptlrpc_request *req = mdt_info_req(info);
721 struct mdt_object *mo;
722 struct mdt_body *repbody;
726 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
727 (unsigned int)ma->ma_attr.la_valid);
729 if (info->mti_dlm_req)
730 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
732 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
733 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
735 GOTO(out, rc = PTR_ERR(mo));
737 if (!mdt_object_exists(mo))
738 GOTO(out_put, rc = -ENOENT);
740 if (mdt_object_remote(mo))
741 GOTO(out_put, rc = -EREMOTE);
743 if ((ma->ma_attr.la_valid & LA_SIZE) ||
744 (rr->rr_flags & MRF_OPEN_TRUNC)) {
745 /* Check write access for the O_TRUNC case */
746 if (mdt_write_read(mo) < 0)
747 GOTO(out_put, rc = -ETXTBSY);
750 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
751 if (ma->ma_valid & MA_LOV)
752 GOTO(out_put, rc = -EPROTO);
754 rc = mdt_attr_set(info, mo, ma);
757 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
758 struct lu_buf *buf = &info->mti_buf;
760 if (ma->ma_attr.la_valid != 0)
761 GOTO(out_put, rc = -EPROTO);
763 buf->lb_buf = ma->ma_lmm;
764 buf->lb_len = ma->ma_lmm_size;
765 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
766 buf, XATTR_NAME_LOV, 0);
769 } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
770 struct lu_buf *buf = &info->mti_buf;
771 struct mdt_lock_handle *lh;
773 rc = mdt_remote_permission(info);
777 if (ma->ma_attr.la_valid != 0)
778 GOTO(out_put, rc = -EPROTO);
780 lh = &info->mti_lh[MDT_LH_PARENT];
781 mdt_lock_reg_init(lh, LCK_PW);
783 rc = mdt_object_lock(info, mo, lh,
784 MDS_INODELOCK_XATTR);
788 buf->lb_buf = ma->ma_lmv;
789 buf->lb_len = ma->ma_lmv_size;
790 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
791 buf, XATTR_NAME_DEFAULT_LMV, 0);
793 mdt_object_unlock(info, mo, lh, rc);
797 GOTO(out_put, rc = -EPROTO);
800 /* If file data is modified, add the dirty flag */
801 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
802 rc = mdt_add_dirty_flag(info, mo, ma);
804 ma->ma_need = MA_INODE;
806 rc = mdt_attr_get_complex(info, mo, ma);
810 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
814 mdt_object_put(info->mti_env, mo);
817 mdt_counter_incr(req, LPROC_MDT_SETATTR);
819 mdt_client_compatibility(info);
820 rc2 = mdt_fix_reply(info);
826 static int mdt_reint_create(struct mdt_thread_info *info,
827 struct mdt_lock_handle *lhc)
829 struct ptlrpc_request *req = mdt_info_req(info);
833 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
834 RETURN(err_serious(-ESTALE));
836 if (info->mti_dlm_req)
837 ldlm_request_cancel(mdt_info_req(info),
838 info->mti_dlm_req, 0, LATF_SKIP);
840 if (!lu_name_is_valid(&info->mti_rr.rr_name))
843 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
845 mdt_counter_incr(req, LPROC_MDT_MKDIR);
853 /* Special file should stay on the same node as parent. */
854 mdt_counter_incr(req, LPROC_MDT_MKNOD);
857 CERROR("%s: Unsupported mode %o\n",
858 mdt_obd_name(info->mti_mdt),
859 info->mti_attr.ma_attr.la_mode);
860 RETURN(err_serious(-EOPNOTSUPP));
863 rc = mdt_md_create(info);
868 * VBR: save parent version in reply and child version getting by its name.
869 * Version of child is getting and checking during its lookup. If
871 static int mdt_reint_unlink(struct mdt_thread_info *info,
872 struct mdt_lock_handle *lhc)
874 struct mdt_reint_record *rr = &info->mti_rr;
875 struct ptlrpc_request *req = mdt_info_req(info);
876 struct md_attr *ma = &info->mti_attr;
877 struct lu_fid *child_fid = &info->mti_tmp_fid1;
878 struct mdt_object *mp;
879 struct mdt_object *mc;
880 struct mdt_lock_handle *parent_lh;
881 struct mdt_lock_handle *child_lh;
882 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
883 struct lu_fid *s0_fid = &info->mti_tmp_fid2;
884 struct mdt_lock_handle *s0_lh = NULL;
885 struct mdt_object *s0_obj = NULL;
887 bool cos_incompat = false;
892 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
893 PNAME(&rr->rr_name));
895 if (info->mti_dlm_req)
896 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
898 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
899 RETURN(err_serious(-ENOENT));
901 if (!fid_is_md_operative(rr->rr_fid1))
904 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
908 if (!mdt_object_remote(mp)) {
909 rc = mdt_version_get_check_save(info, mp, 0);
911 GOTO(put_parent, rc);
914 /* lookup child object along with version checking */
916 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
918 /* Name might not be able to find during resend of
919 * remote unlink, considering following case.
920 * dir_A is a remote directory, the name entry of
921 * dir_A is on MDT0, the directory is on MDT1,
923 * 1. client sends unlink req to MDT1.
924 * 2. MDT1 sends name delete update to MDT0.
925 * 3. name entry is being deleted in MDT0 synchronously.
926 * 4. MDT1 is restarted.
927 * 5. client resends unlink req to MDT1. So it can not
928 * find the name entry on MDT0 anymore.
929 * In this case, MDT1 only needs to destory the local
932 if (mdt_object_remote(mp) && rc == -ENOENT &&
933 !fid_is_zero(rr->rr_fid2) &&
934 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
936 *child_fid = *rr->rr_fid2;
938 GOTO(put_parent, rc);
942 if (!fid_is_md_operative(child_fid))
943 GOTO(put_parent, rc = -EPERM);
945 /* We will lock the child regardless it is local or remote. No harm. */
946 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
948 GOTO(put_parent, rc = PTR_ERR(mc));
950 rc = mdt_init_slaves(info, mc, s0_fid);
951 cos_incompat = (mdt_object_remote(mp) || (rc > 0));
953 parent_lh = &info->mti_lh[MDT_LH_PARENT];
954 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
955 rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
960 child_lh = &info->mti_lh[MDT_LH_CHILD];
961 mdt_lock_reg_init(child_lh, LCK_EX);
962 if (info->mti_spec.sp_rm_entry) {
963 struct lu_ucred *uc = mdt_ucred(info);
965 if (!mdt_is_dne_client(req->rq_export))
966 /* Return -ENOTSUPP for old client */
967 GOTO(unlock_parent, rc = -ENOTSUPP);
969 if (!md_capable(uc, CFS_CAP_SYS_ADMIN))
970 GOTO(unlock_parent, rc = -EPERM);
972 ma->ma_need = MA_INODE;
974 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
975 NULL, &rr->rr_name, ma, no_name);
976 GOTO(unlock_parent, rc);
979 if (mdt_object_remote(mc)) {
980 struct mdt_body *repbody;
982 if (!fid_is_zero(rr->rr_fid2)) {
983 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
984 mdt_obd_name(info->mti_mdt),
985 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
986 GOTO(unlock_parent, rc = -ENOENT);
988 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
989 mdt_obd_name(info->mti_mdt),
990 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
992 if (!mdt_is_dne_client(req->rq_export))
993 /* Return -ENOTSUPP for old client */
994 GOTO(unlock_parent, rc = -ENOTSUPP);
996 /* Revoke the LOOKUP lock of the remote object granted by
997 * this MDT. Since the unlink will happen on another MDT,
998 * it will release the LOOKUP lock right away. Then What
999 * would happen if another client try to grab the LOOKUP
1000 * lock at the same time with unlink XXX */
1001 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1002 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1003 LASSERT(repbody != NULL);
1004 repbody->mbo_fid1 = *mdt_object_fid(mc);
1005 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1006 GOTO(unlock_child, rc = -EREMOTE);
1008 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1009 * this now because a running HSM restore on the child (unlink
1010 * victim) will hold the layout lock. See LU-4002. */
1011 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1012 if (mdt_object_remote(mp)) {
1013 /* Enqueue lookup lock from parent MDT */
1014 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1015 &child_lh->mlh_rreg_lh,
1016 child_lh->mlh_rreg_mode,
1017 MDS_INODELOCK_LOOKUP, false);
1019 GOTO(unlock_parent, rc);
1021 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1024 rc = mdt_reint_object_lock(info, mc, child_lh, lock_ibits,
1027 GOTO(unlock_child, rc);
1030 * Now we can only make sure we need MA_INODE, in mdd layer, will check
1031 * whether need MA_LOV and MA_COOKIE.
1033 ma->ma_need = MA_INODE;
1036 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
1037 mdt_lock_reg_init(s0_lh, LCK_EX);
1038 rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_fid,
1039 s0_lh, &s0_obj, einfo);
1041 GOTO(unlock_child, rc);
1043 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1044 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1045 /* save version when object is locked */
1046 mdt_version_get_save(info, mc, 1);
1048 mutex_lock(&mc->mot_lov_mutex);
1050 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1051 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1053 mutex_unlock(&mc->mot_lov_mutex);
1055 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
1056 rc = mdt_attr_get_complex(info, mc, ma);
1058 mdt_handle_last_unlink(info, mc, ma);
1060 if (ma->ma_valid & MA_INODE) {
1061 switch (ma->ma_attr.la_mode & S_IFMT) {
1063 mdt_counter_incr(req, LPROC_MDT_RMDIR);
1071 mdt_counter_incr(req, LPROC_MDT_UNLINK);
1074 LASSERTF(0, "bad file type %o unlinking\n",
1075 ma->ma_attr.la_mode);
1082 mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo,
1084 mdt_object_unlock(info, mc, child_lh, rc);
1086 mdt_object_unlock(info, mp, parent_lh, rc);
1088 mdt_object_put(info->mti_env, mc);
1090 mdt_object_put(info->mti_env, mp);
1095 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1098 static int mdt_reint_link(struct mdt_thread_info *info,
1099 struct mdt_lock_handle *lhc)
1101 struct mdt_reint_record *rr = &info->mti_rr;
1102 struct ptlrpc_request *req = mdt_info_req(info);
1103 struct md_attr *ma = &info->mti_attr;
1104 struct mdt_object *ms;
1105 struct mdt_object *mp;
1106 struct mdt_lock_handle *lhs;
1107 struct mdt_lock_handle *lhp;
1112 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1113 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1115 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1116 RETURN(err_serious(-ENOENT));
1118 if (info->mti_dlm_req)
1119 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1121 /* Invalid case so return error immediately instead of
1123 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1126 if (!fid_is_md_operative(rr->rr_fid1) ||
1127 !fid_is_md_operative(rr->rr_fid2))
1130 /* step 1: find target parent dir */
1131 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1133 RETURN(PTR_ERR(mp));
1135 rc = mdt_version_get_check_save(info, mp, 0);
1137 GOTO(put_parent, rc);
1139 /* step 2: find source */
1140 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1142 GOTO(put_parent, rc = PTR_ERR(ms));
1144 if (!mdt_object_exists(ms)) {
1145 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1146 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1147 GOTO(put_source, rc = -ENOENT);
1150 cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1152 lhp = &info->mti_lh[MDT_LH_PARENT];
1153 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1154 rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1157 GOTO(put_source, rc);
1159 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1161 lhs = &info->mti_lh[MDT_LH_CHILD];
1162 mdt_lock_reg_init(lhs, LCK_EX);
1163 rc = mdt_reint_object_lock(info, ms, lhs,
1164 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1167 GOTO(unlock_parent, rc);
1169 /* step 3: link it */
1170 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1171 OBD_FAIL_MDS_REINT_LINK_WRITE);
1173 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1174 rc = mdt_version_get_check_save(info, ms, 1);
1176 GOTO(unlock_source, rc);
1178 /** check target version by name during replay */
1179 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1180 &info->mti_tmp_fid1, 2);
1181 if (rc != 0 && rc != -ENOENT)
1182 GOTO(unlock_source, rc);
1183 /* save version of file name for replay, it must be ENOENT here */
1184 if (!req_is_replay(mdt_info_req(info))) {
1185 if (rc != -ENOENT) {
1186 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1187 PNAME(&rr->rr_name));
1188 GOTO(unlock_source, rc = -EEXIST);
1190 info->mti_ver[2] = ENOENT_VERSION;
1191 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1194 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1195 mdt_object_child(ms), &rr->rr_name, ma);
1198 mdt_counter_incr(req, LPROC_MDT_LINK);
1202 mdt_object_unlock(info, ms, lhs, rc);
1204 mdt_object_unlock(info, mp, lhp, rc);
1206 mdt_object_put(info->mti_env, ms);
1208 mdt_object_put(info->mti_env, mp);
1212 * lock the part of the directory according to the hash of the name
1213 * (lh->mlh_pdo_hash) in parallel directory lock.
1215 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1216 struct mdt_lock_handle *lh,
1217 struct mdt_object *obj, __u64 ibits,
1220 struct ldlm_res_id *res = &info->mti_res_id;
1221 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1222 union ldlm_policy_data *policy = &info->mti_policy;
1223 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1227 * Finish res_id initializing by name hash marking part of
1228 * directory which is taking modification.
1230 LASSERT(lh->mlh_pdo_hash != 0);
1231 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1232 memset(policy, 0, sizeof(*policy));
1233 policy->l_inodebits.bits = ibits;
1235 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1236 dlmflags |= LDLM_FL_COS_INCOMPAT;
1238 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1239 * going to be sent to client. If it is - mdt_intent_policy() path will
1240 * fix it up and turn FL_LOCAL flag off.
1242 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1243 res, dlmflags, &info->mti_exp->exp_handle.h_cookie);
1248 * Get BFL lock for rename or migrate process.
1250 static int mdt_rename_lock(struct mdt_thread_info *info,
1251 struct lustre_handle *lh)
1256 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1257 struct lu_fid *fid = &info->mti_tmp_fid1;
1258 struct mdt_object *obj;
1260 /* XXX, right now, it has to use object API to
1261 * enqueue lock cross MDT, so it will enqueue
1262 * rename lock(with LUSTRE_BFL_FID) by root object */
1264 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1266 RETURN(PTR_ERR(obj));
1268 rc = mdt_remote_object_lock(info, obj,
1269 &LUSTRE_BFL_FID, lh,
1271 MDS_INODELOCK_UPDATE, false);
1272 mdt_object_put(info->mti_env, obj);
1274 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1275 union ldlm_policy_data *policy = &info->mti_policy;
1276 struct ldlm_res_id *res_id = &info->mti_res_id;
1279 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1280 memset(policy, 0, sizeof *policy);
1281 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1282 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1283 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1284 LCK_EX, &flags, ldlm_blocking_ast,
1285 ldlm_completion_ast, NULL, NULL, 0,
1287 &info->mti_exp->exp_handle.h_cookie,
1294 static void mdt_rename_unlock(struct lustre_handle *lh)
1297 LASSERT(lustre_handle_is_used(lh));
1298 /* Cancel the single rename lock right away */
1299 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1304 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1305 * target. Source should not be ancestor of target dir. May be other rename
1306 * checks can be moved here later.
1308 static int mdt_is_subdir(struct mdt_thread_info *info,
1309 struct mdt_object *dir,
1310 const struct lu_fid *fid)
1312 struct lu_fid dir_fid = dir->mot_header.loh_fid;
1316 /* If the source and target are in the same directory, they can not
1317 * be parent/child relationship, so subdir check is not needed */
1318 if (lu_fid_eq(&dir_fid, fid))
1321 if (!mdt_object_exists(dir))
1324 rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1327 CERROR("%s: failed subdir check in "DFID" for "DFID
1328 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1329 PFID(&dir_fid), PFID(fid), rc);
1330 /* Return EINVAL only if a parent is the @fid */
1334 /* check the found fid */
1335 if (lu_fid_eq(&dir_fid, fid))
1342 /* Update object linkEA */
1343 struct mdt_lock_list {
1344 struct mdt_object *mll_obj;
1345 struct mdt_lock_handle mll_lh;
1346 struct list_head mll_list;
1349 static void mdt_unlock_list(struct mdt_thread_info *info,
1350 struct list_head *list, int rc)
1352 struct mdt_lock_list *mll;
1353 struct mdt_lock_list *mll2;
1355 list_for_each_entry_safe(mll, mll2, list, mll_list) {
1356 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1357 list_del(&mll->mll_list);
1362 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1363 struct mdt_object *obj,
1364 struct mdt_object *pobj,
1365 struct list_head *lock_list)
1367 struct lu_buf *buf = &info->mti_big_buf;
1368 struct linkea_data ldata = { NULL };
1374 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1377 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1378 if (buf->lb_buf == NULL)
1382 rc = mdt_links_read(info, obj, &ldata);
1384 if (rc == -ENOENT || rc == -ENODATA)
1389 /* ignore the migrating parent(@pobj) */
1390 retry_count = ldata.ld_leh->leh_reccount - 1;
1393 LASSERT(ldata.ld_leh != NULL);
1394 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1395 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1396 struct mdt_device *mdt = info->mti_mdt;
1397 struct mdt_object *mdt_pobj;
1398 struct mdt_lock_list *mll;
1399 struct lu_name name;
1402 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1404 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1405 if (IS_ERR(mdt_pobj)) {
1406 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1407 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1411 if (!mdt_object_exists(mdt_pobj)) {
1412 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1413 mdt_obd_name(mdt), PFID(&fid));
1414 mdt_object_put(info->mti_env, mdt_pobj);
1418 /* Check if the object already exists in the list */
1419 list_for_each_entry(mll, lock_list, mll_list) {
1420 if (mll->mll_obj == mdt_pobj) {
1421 mdt_object_put(info->mti_env, mdt_pobj);
1426 if (mdt_pobj == pobj) {
1427 CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1428 mdt_obd_name(mdt), PFID(&fid));
1429 mdt_object_put(info->mti_env, mdt_pobj);
1435 mdt_object_put(info->mti_env, mdt_pobj);
1436 GOTO(out, rc = -ENOMEM);
1439 /* Since this needs to lock all of objects in linkea, to avoid
1440 * deadlocks, because it does not follow parent-child order as
1441 * other MDT operation, let's use try_lock here and if the lock
1442 * cannot be gotten because of conflicting locks, then drop all
1443 * current locks, send an AST to the client, and start again. */
1444 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1445 rc = mdt_reint_object_lock_try(info, mdt_pobj, &mll->mll_lh,
1446 MDS_INODELOCK_UPDATE, true);
1448 mdt_unlock_list(info, lock_list, rc);
1450 CDEBUG(D_INFO, "%s: busy lock on "DFID" %s retry %d\n",
1451 mdt_obd_name(mdt), PFID(&fid), name.ln_name,
1454 if (retry_count == 0) {
1455 mdt_object_put(info->mti_env, mdt_pobj);
1457 GOTO(out, rc = -EBUSY);
1460 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1461 MDS_INODELOCK_UPDATE);
1463 mdt_object_put(info->mti_env, mdt_pobj);
1468 if (mdt_object_remote(mdt_pobj)) {
1469 struct ldlm_lock *lock;
1471 /* For remote object, Set lock to cb_atomic,
1472 * so lock can be released in blocking_ast()
1473 * immediately, then the next try_lock will
1474 * have better chance to succeds */
1476 ldlm_handle2lock(&mll->mll_lh.mlh_rreg_lh);
1477 LASSERT(lock != NULL);
1478 lock_res_and_lock(lock);
1479 ldlm_set_atomic_cb(lock);
1480 unlock_res_and_lock(lock);
1481 LDLM_LOCK_PUT(lock);
1483 mdt_object_unlock_put(info, mdt_pobj, &mll->mll_lh, rc);
1489 INIT_LIST_HEAD(&mll->mll_list);
1490 mll->mll_obj = mdt_pobj;
1491 list_add_tail(&mll->mll_list, lock_list);
1493 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1498 mdt_unlock_list(info, lock_list, rc);
1502 /* migrate files from one MDT to another MDT */
1503 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1504 struct mdt_lock_handle *lhc)
1506 struct mdt_reint_record *rr = &info->mti_rr;
1507 struct md_attr *ma = &info->mti_attr;
1508 struct mdt_object *msrcdir;
1509 struct mdt_object *mold;
1510 struct mdt_object *mnew = NULL;
1511 struct mdt_lock_handle *lh_dirp;
1512 struct mdt_lock_handle *lh_childp;
1513 struct mdt_lock_handle *lh_tgtp = NULL;
1514 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1515 struct list_head lock_list;
1517 struct ldlm_lock *lease = NULL;
1518 bool lock_open_sem = false;
1522 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1523 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1525 /* 1: lock the source dir. */
1526 msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1527 if (IS_ERR(msrcdir)) {
1528 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1529 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1530 (int)PTR_ERR(msrcdir));
1531 RETURN(PTR_ERR(msrcdir));
1534 lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1535 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1536 rc = mdt_reint_object_lock(info, msrcdir, lh_dirp, MDS_INODELOCK_UPDATE,
1539 GOTO(out_put_parent, rc);
1541 if (!mdt_object_remote(msrcdir)) {
1542 rc = mdt_version_get_check_save(info, msrcdir, 0);
1544 GOTO(out_unlock_parent, rc);
1547 /* 2: sanity check and find the object to be migrated. */
1549 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1551 GOTO(out_unlock_parent, rc);
1553 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1554 GOTO(out_unlock_parent, rc = -EINVAL);
1556 if (!fid_is_md_operative(old_fid))
1557 GOTO(out_unlock_parent, rc = -EPERM);
1559 if (lu_fid_eq(old_fid, &info->mti_mdt->mdt_md_root_fid))
1560 GOTO(out_unlock_parent, rc = -EPERM);
1562 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1564 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1566 if (mdt_object_remote(mold)) {
1567 CERROR("%s: source "DFID" is on the remote MDT\n",
1568 mdt_obd_name(info->mti_mdt), PFID(old_fid));
1569 GOTO(out_put_child, rc = -EREMOTE);
1572 if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1573 !mdt_object_remote(msrcdir)) {
1574 CERROR("%s: parent "DFID" is still on the same"
1575 " MDT, which should be migrated first:"
1576 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1577 PFID(mdt_object_fid(msrcdir)), -EPERM);
1578 GOTO(out_put_child, rc = -EPERM);
1581 rc = mdt_remote_permission(info);
1583 GOTO(out_put_child, rc);
1585 /* 3: iterate the linkea of the object and lock all of the objects */
1586 INIT_LIST_HEAD(&lock_list);
1587 rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1589 GOTO(out_put_child, rc);
1591 if (info->mti_spec.sp_migrate_close) {
1592 struct close_data *data;
1593 struct mdt_body *repbody;
1594 bool lease_broken = false;
1596 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
1598 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
1600 GOTO(out_lease, rc = -EPROTO);
1602 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1604 GOTO(out_lease, rc = -EPROTO);
1606 lease = ldlm_handle2lock(&data->cd_handle);
1608 GOTO(out_lease, rc = -ESTALE);
1610 /* try to hold open_sem so that nobody else can open the file */
1611 if (!down_write_trylock(&mold->mot_open_sem)) {
1612 ldlm_lock_cancel(lease);
1613 GOTO(out_lease, rc = -EBUSY);
1616 lock_open_sem = true;
1617 /* Check if the lease open lease has already canceled */
1618 lock_res_and_lock(lease);
1619 lease_broken = ldlm_is_cancel(lease);
1620 unlock_res_and_lock(lease);
1622 LDLM_DEBUG(lease, DFID " lease broken? %d",
1623 PFID(mdt_object_fid(mold)), lease_broken);
1625 /* Cancel server side lease. Client side counterpart should
1626 * have been cancelled. It's okay to cancel it now as we've
1627 * held mot_open_sem. */
1628 ldlm_lock_cancel(lease);
1631 GOTO(out_lease, rc = -EAGAIN);
1633 rc = mdt_close_internal(info, mdt_info_req(info), NULL);
1634 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1635 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1637 GOTO(out_unlock_list, rc);
1640 /* 4: lock of the object migrated object */
1641 lh_childp = &info->mti_lh[MDT_LH_OLD];
1642 mdt_lock_reg_init(lh_childp, LCK_EX);
1643 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1644 MDS_INODELOCK_LAYOUT;
1645 if (mdt_object_remote(msrcdir)) {
1646 /* Enqueue lookup lock from the parent MDT */
1647 rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
1648 &lh_childp->mlh_rreg_lh,
1649 lh_childp->mlh_rreg_mode,
1650 MDS_INODELOCK_LOOKUP, false);
1652 GOTO(out_unlock_list, rc);
1654 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1657 rc = mdt_reint_object_lock(info, mold, lh_childp, lock_ibits, true);
1659 GOTO(out_unlock_child, rc);
1661 /* Migration is incompatible with HSM. */
1662 ma->ma_need = MA_HSM;
1664 rc = mdt_attr_get_complex(info, mold, ma);
1666 GOTO(out_unlock_child, rc);
1668 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0) {
1670 CERROR("%s: cannot migrate HSM archived file "DFID": rc = %d\n",
1671 mdt_obd_name(info->mti_mdt), PFID(old_fid), rc);
1672 GOTO(out_unlock_child, rc);
1675 ma->ma_need = MA_LMV;
1677 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1678 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1679 rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1681 GOTO(out_unlock_child, rc);
1683 if ((ma->ma_valid & MA_LMV)) {
1684 struct lmv_mds_md_v1 *lmm1;
1686 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1687 lmm1 = &ma->ma_lmv->lmv_md_v1;
1688 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1689 CERROR("%s: can not migrate striped dir "DFID
1690 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1691 PFID(mdt_object_fid(mold)), -EPERM);
1692 GOTO(out_unlock_child, rc = -EPERM);
1695 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1696 GOTO(out_unlock_child, rc = -EINVAL);
1698 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1699 &lmm1->lmv_stripe_fids[1]);
1701 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1703 if (!mdt_object_remote(mnew)) {
1704 CERROR("%s: "DFID" being migrated is on this MDT:"
1705 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1706 PFID(rr->rr_fid2), -EPERM);
1707 GOTO(out_put_new, rc = -EPERM);
1710 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1711 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1712 rc = mdt_remote_object_lock(info, mnew,
1713 mdt_object_fid(mnew),
1714 &lh_tgtp->mlh_rreg_lh,
1715 lh_tgtp->mlh_rreg_mode,
1716 MDS_INODELOCK_UPDATE, false);
1719 GOTO(out_put_new, rc);
1722 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1725 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1726 if (!mdt_object_remote(mnew)) {
1727 CERROR("%s: Migration "DFID" is on this MDT:"
1728 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1729 PFID(rr->rr_fid2), -EXDEV);
1730 GOTO(out_put_new, rc = -EXDEV);
1735 mdt_reint_init_ma(info, ma);
1737 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1738 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1740 rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1741 mdt_object_child(mold), &rr->rr_name,
1742 mdt_object_child(mnew), ma);
1744 GOTO(out_unlock_new, rc);
1747 if (lh_tgtp != NULL)
1748 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1751 mdt_object_put(info->mti_env, mnew);
1753 mdt_object_unlock(info, mold, lh_childp, rc);
1755 /* we don't really modify linkea objects, so we can safely decref these
1756 * locks, and this can avoid saving them as COS locks, which may prevent
1757 * subsequent migrate. */
1758 mdt_unlock_list(info, &lock_list, 1);
1759 if (lease != NULL) {
1760 ldlm_reprocess_all(lease->l_resource);
1761 LDLM_LOCK_PUT(lease);
1765 up_write(&mold->mot_open_sem);
1767 mdt_object_put(info->mti_env, mold);
1769 mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1771 mdt_object_put(info->mti_env, msrcdir);
1776 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1777 const struct lu_fid *fid,
1780 struct mdt_object *dir;
1784 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1788 /* check early, the real version will be saved after locking */
1789 rc = mdt_version_get_check(info, dir, idx);
1795 mdt_object_put(info->mti_env, dir);
1799 static int mdt_object_lock_save(struct mdt_thread_info *info,
1800 struct mdt_object *dir,
1801 struct mdt_lock_handle *lh,
1802 int idx, bool cos_incompat)
1806 /* we lock the target dir if it is local */
1807 rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
1812 /* get and save correct version after locking */
1813 mdt_version_get_save(info, dir, idx);
1818 * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
1819 * 2 - srcdir child; 3 - tgtdir child.
1820 * Update on disk version of srcdir child.
1823 * For DNE phase I, only these renames are allowed
1824 * mv src_p/src_c tgt_p/tgt_c
1825 * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1826 * 2. src_p and tgt_p are same directory, and tgt_c does not
1827 * exists. In this case, all of modification will happen
1828 * in the MDT where ithesource parent is, only one remote
1829 * update is needed, i.e. set c_time/m_time on the child.
1830 * And tgt_c will be still in the same MDT as the original
1833 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1834 struct mdt_lock_handle *lhc)
1836 struct mdt_reint_record *rr = &info->mti_rr;
1837 struct md_attr *ma = &info->mti_attr;
1838 struct ptlrpc_request *req = mdt_info_req(info);
1839 struct mdt_object *msrcdir = NULL;
1840 struct mdt_object *mtgtdir = NULL;
1841 struct mdt_object *mold;
1842 struct mdt_object *mnew = NULL;
1843 struct mdt_lock_handle *lh_srcdirp;
1844 struct mdt_lock_handle *lh_tgtdirp;
1845 struct mdt_lock_handle *lh_oldp = NULL;
1846 struct mdt_lock_handle *lh_newp = NULL;
1847 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1848 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1850 bool reverse = false;
1855 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1856 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1857 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1859 /* find both parents. */
1860 msrcdir = mdt_object_find_check(info, rr->rr_fid1, 0);
1861 if (IS_ERR(msrcdir))
1862 RETURN(PTR_ERR(msrcdir));
1864 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1866 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1868 mdt_object_get(info->mti_env, mtgtdir);
1870 /* Check if the @msrcdir is not a child of the @mtgtdir,
1871 * otherwise a reverse locking must take place. */
1872 rc = mdt_is_subdir(info, msrcdir, rr->rr_fid2);
1876 GOTO(out_put_srcdir, rc);
1878 mtgtdir = mdt_object_find_check(info, rr->rr_fid2, 1);
1879 if (IS_ERR(mtgtdir))
1880 GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
1883 /* source needs to be looked up after locking source parent, otherwise
1884 * this rename may race with unlink source, and cause rename hang, see
1885 * sanityn.sh 55b, so check parents first, if later we found source is
1886 * remote, relock parents. */
1887 cos_incompat = (mdt_object_remote(msrcdir) ||
1888 mdt_object_remote(mtgtdir));
1890 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1892 /* lock parents in the proper order. */
1893 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1894 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1897 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1898 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1901 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
1904 GOTO(out_put_tgtdir, rc);
1906 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1908 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
1911 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1912 GOTO(out_put_tgtdir, rc);
1915 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
1918 GOTO(out_put_tgtdir, rc);
1920 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1922 if (mtgtdir != msrcdir) {
1923 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
1925 } else if (lh_srcdirp->mlh_pdo_hash !=
1926 lh_tgtdirp->mlh_pdo_hash) {
1927 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1928 MDS_INODELOCK_UPDATE,
1930 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1933 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
1934 GOTO(out_put_tgtdir, rc);
1938 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1939 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1941 /* find mold object. */
1943 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1945 GOTO(out_unlock_parents, rc);
1947 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1948 GOTO(out_unlock_parents, rc = -EINVAL);
1950 if (!fid_is_md_operative(old_fid))
1951 GOTO(out_unlock_parents, rc = -EPERM);
1953 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1955 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1957 /* Check if @mtgtdir is subdir of @mold, before locking child
1958 * to avoid reverse locking. */
1959 if (mtgtdir != msrcdir) {
1960 rc = mdt_is_subdir(info, mtgtdir, old_fid);
1962 GOTO(out_put_old, rc);
1965 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1966 /* save version after locking */
1967 mdt_version_get_save(info, mold, 2);
1969 if (!cos_incompat && mdt_object_remote(mold)) {
1970 cos_incompat = true;
1971 mdt_object_put(info->mti_env, mold);
1972 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
1973 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
1977 /* find mnew object:
1978 * mnew target object may not exist now
1979 * lookup with version checking */
1981 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1984 /* the new_fid should have been filled at this moment */
1985 if (lu_fid_eq(old_fid, new_fid))
1986 GOTO(out_put_old, rc);
1988 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1989 lu_fid_eq(new_fid, rr->rr_fid2))
1990 GOTO(out_put_old, rc = -EINVAL);
1992 if (!fid_is_md_operative(new_fid))
1993 GOTO(out_put_old, rc = -EPERM);
1995 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1997 GOTO(out_put_old, rc = PTR_ERR(mnew));
1999 if (mdt_object_remote(mnew)) {
2000 struct mdt_body *repbody;
2002 /* Always send rename req to the target child MDT */
2003 repbody = req_capsule_server_get(info->mti_pill,
2005 LASSERT(repbody != NULL);
2006 repbody->mbo_fid1 = *new_fid;
2007 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2008 GOTO(out_put_new, rc = -EXDEV);
2010 /* Before locking the target dir, check we do not replace
2011 * a dir with a non-dir, otherwise it may deadlock with
2012 * link op which tries to create a link in this dir
2013 * back to this non-dir. */
2014 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2015 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2016 GOTO(out_put_new, rc = -EISDIR);
2018 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2019 mdt_lock_reg_init(lh_oldp, LCK_EX);
2020 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2021 if (mdt_object_remote(msrcdir)) {
2022 /* Enqueue lookup lock from the parent MDT */
2023 rc = mdt_remote_object_lock(info, msrcdir,
2024 mdt_object_fid(mold),
2025 &lh_oldp->mlh_rreg_lh,
2026 lh_oldp->mlh_rreg_mode,
2027 MDS_INODELOCK_LOOKUP,
2030 GOTO(out_put_new, rc);
2032 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2035 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2038 GOTO(out_unlock_old, rc);
2040 /* Check if @msrcdir is subdir of @mnew, before locking child
2041 * to avoid reverse locking. */
2042 if (mtgtdir != msrcdir) {
2043 rc = mdt_is_subdir(info, msrcdir, new_fid);
2045 GOTO(out_unlock_old, rc);
2048 /* We used to acquire MDS_INODELOCK_FULL here but we
2049 * can't do this now because a running HSM restore on
2050 * the rename onto victim will hold the layout
2051 * lock. See LU-4002. */
2053 lh_newp = &info->mti_lh[MDT_LH_NEW];
2054 mdt_lock_reg_init(lh_newp, LCK_EX);
2055 rc = mdt_reint_object_lock(info, mnew, lh_newp,
2056 MDS_INODELOCK_LOOKUP |
2057 MDS_INODELOCK_UPDATE,
2060 GOTO(out_unlock_old, rc);
2062 /* get and save version after locking */
2063 mdt_version_get_save(info, mnew, 3);
2064 } else if (rc != -EREMOTE && rc != -ENOENT) {
2065 GOTO(out_put_old, rc);
2067 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2068 mdt_lock_reg_init(lh_oldp, LCK_EX);
2069 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2070 if (mdt_object_remote(msrcdir)) {
2071 /* Enqueue lookup lock from the parent MDT */
2072 rc = mdt_remote_object_lock(info, msrcdir,
2073 mdt_object_fid(mold),
2074 &lh_oldp->mlh_rreg_lh,
2075 lh_oldp->mlh_rreg_mode,
2076 MDS_INODELOCK_LOOKUP,
2079 GOTO(out_put_old, rc);
2081 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2084 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2087 GOTO(out_unlock_old, rc);
2089 mdt_enoent_version_save(info, 3);
2092 /* step 5: rename it */
2093 mdt_reint_init_ma(info, ma);
2095 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
2096 OBD_FAIL_MDS_REINT_RENAME_WRITE);
2099 mutex_lock(&mnew->mot_lov_mutex);
2101 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
2102 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
2103 mnew != NULL ? mdt_object_child(mnew) : NULL,
2104 &rr->rr_tgt_name, ma);
2107 mutex_unlock(&mnew->mot_lov_mutex);
2109 /* handle last link of tgt object */
2111 mdt_counter_incr(req, LPROC_MDT_RENAME);
2113 mdt_handle_last_unlink(info, mnew, ma);
2115 mdt_rename_counter_tally(info, info->mti_mdt, req,
2121 mdt_object_unlock(info, mnew, lh_newp, rc);
2123 mdt_object_unlock(info, mold, lh_oldp, rc);
2126 mdt_object_put(info->mti_env, mnew);
2128 mdt_object_put(info->mti_env, mold);
2130 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2131 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2133 mdt_object_put(info->mti_env, mtgtdir);
2135 mdt_object_put(info->mti_env, msrcdir);
2139 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
2140 struct mdt_lock_handle *lhc, bool rename)
2142 struct mdt_reint_record *rr = &info->mti_rr;
2143 struct ptlrpc_request *req = mdt_info_req(info);
2144 struct lustre_handle rename_lh = { 0 };
2148 if (info->mti_dlm_req)
2149 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2151 if (!fid_is_md_operative(rr->rr_fid1) ||
2152 !fid_is_md_operative(rr->rr_fid2))
2155 /* Note: do not enqueue rename lock for replay request, because
2156 * if other MDT holds rename lock, but being blocked to wait for
2157 * this MDT to finish its recovery, and the failover MDT can not
2158 * get rename lock, which will cause deadlock. */
2159 if (!req_is_replay(req)) {
2160 rc = mdt_rename_lock(info, &rename_lh);
2162 CERROR("%s: can't lock FS for rename: rc = %d\n",
2163 mdt_obd_name(info->mti_mdt), rc);
2169 rc = mdt_reint_rename_internal(info, lhc);
2171 rc = mdt_reint_migrate_internal(info, lhc);
2173 if (lustre_handle_is_used(&rename_lh))
2174 mdt_rename_unlock(&rename_lh);
2179 static int mdt_reint_rename(struct mdt_thread_info *info,
2180 struct mdt_lock_handle *lhc)
2182 return mdt_reint_rename_or_migrate(info, lhc, true);
2185 static int mdt_reint_migrate(struct mdt_thread_info *info,
2186 struct mdt_lock_handle *lhc)
2188 return mdt_reint_rename_or_migrate(info, lhc, false);
2191 struct mdt_reinter {
2192 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
2193 enum lprocfs_extra_opc mr_extra_opc;
2196 static const struct mdt_reinter mdt_reinters[] = {
2198 .mr_handler = &mdt_reint_setattr,
2199 .mr_extra_opc = MDS_REINT_SETATTR,
2202 .mr_handler = &mdt_reint_create,
2203 .mr_extra_opc = MDS_REINT_CREATE,
2206 .mr_handler = &mdt_reint_link,
2207 .mr_extra_opc = MDS_REINT_LINK,
2210 .mr_handler = &mdt_reint_unlink,
2211 .mr_extra_opc = MDS_REINT_UNLINK,
2214 .mr_handler = &mdt_reint_rename,
2215 .mr_extra_opc = MDS_REINT_RENAME,
2218 .mr_handler = &mdt_reint_open,
2219 .mr_extra_opc = MDS_REINT_OPEN,
2221 [REINT_SETXATTR] = {
2222 .mr_handler = &mdt_reint_setxattr,
2223 .mr_extra_opc = MDS_REINT_SETXATTR,
2226 .mr_handler = &mdt_reint_unlink,
2227 .mr_extra_opc = MDS_REINT_UNLINK,
2230 .mr_handler = &mdt_reint_migrate,
2231 .mr_extra_opc = MDS_REINT_RENAME,
2235 int mdt_reint_rec(struct mdt_thread_info *info,
2236 struct mdt_lock_handle *lhc)
2238 const struct mdt_reinter *mr;
2242 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2245 mr = &mdt_reinters[info->mti_rr.rr_opcode];
2246 if (mr->mr_handler == NULL)
2249 rc = (*mr->mr_handler)(info, lhc);
2251 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2252 PTLRPC_LAST_CNTR + mr->mr_extra_opc);