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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_reint.c
33 * Lustre Metadata Target (mdt) reintegration routines
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Huang Hua <huanghua@clusterfs.com>
39 * Author: Yury Umanets <umka@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_MDS
44 #include <lprocfs_status.h>
45 #include "mdt_internal.h"
46 #include <lustre_lmv.h>
48 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
51 ma->ma_need = MA_INODE;
56 * Get version of object by fid.
58 * Return real version or ENOENT_VERSION if object doesn't exist
60 static void mdt_obj_version_get(struct mdt_thread_info *info,
61 struct mdt_object *o, __u64 *version)
65 if (mdt_object_exists(o) && !mdt_object_remote(o) &&
66 !fid_is_obf(mdt_object_fid(o)))
67 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
69 *version = ENOENT_VERSION;
70 CDEBUG(D_INODE, "FID "DFID" version is %#llx\n",
71 PFID(mdt_object_fid(o)), *version);
75 * Check version is correct.
77 * Should be called only during replay.
79 static int mdt_version_check(struct ptlrpc_request *req,
80 __u64 version, int idx)
82 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
85 if (!exp_connect_vbr(req->rq_export))
88 LASSERT(req_is_replay(req));
89 /** VBR: version is checked always because costs nothing */
90 LASSERT(idx < PTLRPC_NUM_VERSIONS);
91 /** Sanity check for malformed buffers */
92 if (pre_ver == NULL) {
93 CERROR("No versions in request buffer\n");
94 spin_lock(&req->rq_export->exp_lock);
95 req->rq_export->exp_vbr_failed = 1;
96 spin_unlock(&req->rq_export->exp_lock);
98 } else if (pre_ver[idx] != version) {
99 CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
100 pre_ver[idx], version);
101 spin_lock(&req->rq_export->exp_lock);
102 req->rq_export->exp_vbr_failed = 1;
103 spin_unlock(&req->rq_export->exp_lock);
110 * Save pre-versions in reply.
112 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
117 if (!exp_connect_vbr(req->rq_export))
120 LASSERT(!req_is_replay(req));
121 LASSERT(req->rq_repmsg != NULL);
122 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
124 reply_ver[idx] = version;
128 * Save enoent version, it is needed when it is obvious that object doesn't
129 * exist, e.g. child during create.
131 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
133 /* save version of file name for replay, it must be ENOENT here */
134 if (!req_is_replay(mdt_info_req(info))) {
135 info->mti_ver[idx] = ENOENT_VERSION;
136 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
141 * Get version from disk and save in reply buffer.
143 * Versions are saved in reply only during normal operations not replays.
145 void mdt_version_get_save(struct mdt_thread_info *info,
146 struct mdt_object *mto, int idx)
148 /* don't save versions during replay */
149 if (!req_is_replay(mdt_info_req(info))) {
150 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
151 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
156 * Get version from disk and check it, no save in reply.
158 int mdt_version_get_check(struct mdt_thread_info *info,
159 struct mdt_object *mto, int idx)
161 /* only check versions during replay */
162 if (!req_is_replay(mdt_info_req(info)))
165 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
166 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
170 * Get version from disk and check if recovery or just save.
172 int mdt_version_get_check_save(struct mdt_thread_info *info,
173 struct mdt_object *mto, int idx)
177 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
178 if (req_is_replay(mdt_info_req(info)))
179 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
182 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
187 * Lookup with version checking.
189 * This checks version of 'name'. Many reint functions uses 'name' for child not
190 * FID, therefore we need to get object by name and check its version.
192 int mdt_lookup_version_check(struct mdt_thread_info *info,
193 struct mdt_object *p,
194 const struct lu_name *lname,
195 struct lu_fid *fid, int idx)
199 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
201 /* Check version only during replay */
202 if (!req_is_replay(mdt_info_req(info)))
205 info->mti_ver[idx] = ENOENT_VERSION;
207 struct mdt_object *child;
209 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
210 if (likely(!IS_ERR(child))) {
211 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
212 mdt_object_put(info->mti_env, child);
215 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
216 return vbrc ? vbrc : rc;
220 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
221 struct mdt_object *obj,
222 struct ldlm_enqueue_info *einfo,
225 union ldlm_policy_data *policy = &mti->mti_policy;
226 struct mdt_lock_handle *lh = &mti->mti_lh[MDT_LH_LOCAL];
227 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
230 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
231 LASSERT(slave_locks);
233 memset(policy, 0, sizeof(*policy));
234 policy->l_inodebits.bits = einfo->ei_inodebits;
235 mdt_lock_handle_init(lh);
236 mdt_lock_reg_init(lh, einfo->ei_mode);
237 for (i = 0; i < slave_locks->ha_count; i++) {
238 if (test_bit(i, (void *)slave_locks->ha_map))
239 lh->mlh_rreg_lh = slave_locks->ha_handles[i];
241 lh->mlh_reg_lh = slave_locks->ha_handles[i];
242 mdt_object_unlock(mti, NULL, lh, decref);
243 slave_locks->ha_handles[i].cookie = 0ull;
246 return mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
250 static inline int mdt_object_striped(struct mdt_thread_info *mti,
251 struct mdt_object *obj)
253 struct lu_device *bottom_dev;
254 struct lu_object *bottom_obj;
257 if (!S_ISDIR(obj->mot_header.loh_attr))
260 /* getxattr from bottom obj to avoid reading in shard FIDs */
261 bottom_dev = dt2lu_dev(mti->mti_mdt->mdt_bottom);
262 bottom_obj = lu_object_find_slice(mti->mti_env, bottom_dev,
263 mdt_object_fid(obj), NULL);
264 if (IS_ERR(bottom_obj))
265 return PTR_ERR(bottom_obj);
267 rc = dt_xattr_get(mti->mti_env, lu2dt(bottom_obj), &LU_BUF_NULL,
269 lu_object_put(mti->mti_env, bottom_obj);
271 return (rc > 0) ? 1 : (rc == -ENODATA) ? 0 : rc;
275 * Lock slave stripes if necessary, the lock handles of slave stripes
276 * will be stored in einfo->ei_cbdata.
278 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
279 enum ldlm_mode mode, __u64 ibits,
280 struct ldlm_enqueue_info *einfo)
282 union ldlm_policy_data *policy = &mti->mti_policy;
284 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
286 einfo->ei_type = LDLM_IBITS;
287 einfo->ei_mode = mode;
288 einfo->ei_cb_bl = mdt_remote_blocking_ast;
289 einfo->ei_cb_local_bl = mdt_blocking_ast;
290 einfo->ei_cb_cp = ldlm_completion_ast;
291 einfo->ei_enq_slave = 1;
292 einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
293 einfo->ei_inodebits = ibits;
294 memset(policy, 0, sizeof(*policy));
295 policy->l_inodebits.bits = ibits;
297 return mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
301 int mdt_reint_striped_lock(struct mdt_thread_info *info,
302 struct mdt_object *o,
303 struct mdt_lock_handle *lh,
305 struct ldlm_enqueue_info *einfo,
310 LASSERT(!mdt_object_remote(o));
312 memset(einfo, 0, sizeof(*einfo));
314 rc = mdt_reint_object_lock(info, o, lh, ibits, cos_incompat);
318 rc = mdt_object_striped(info, o);
321 mdt_object_unlock(info, o, lh, rc);
325 rc = mdt_lock_slaves(info, o, lh->mlh_reg_mode, ibits, einfo);
327 mdt_object_unlock(info, o, lh, rc);
328 if (rc == -EIO && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME))
335 void mdt_reint_striped_unlock(struct mdt_thread_info *info,
336 struct mdt_object *o,
337 struct mdt_lock_handle *lh,
338 struct ldlm_enqueue_info *einfo, int decref)
340 if (einfo->ei_cbdata)
341 mdt_unlock_slaves(info, o, einfo, decref);
342 mdt_object_unlock(info, o, lh, decref);
345 static int mdt_restripe(struct mdt_thread_info *info,
346 struct mdt_object *parent,
347 const struct lu_name *lname,
348 const struct lu_fid *tfid,
349 struct md_op_spec *spec,
352 struct mdt_device *mdt = info->mti_mdt;
353 struct lu_fid *fid = &info->mti_tmp_fid2;
354 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
355 struct lmv_mds_md_v1 *lmv;
356 struct mdt_object *child;
357 struct mdt_lock_handle *lhp;
358 struct mdt_lock_handle *lhc;
359 struct mdt_body *repbody;
363 if (!mdt->mdt_enable_dir_restripe)
366 rc = mdt_version_get_check_save(info, parent, 0);
370 lhp = &info->mti_lh[MDT_LH_PARENT];
371 mdt_lock_pdo_init(lhp, LCK_PW, lname);
372 rc = mdt_reint_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
377 rc = mdt_stripe_get(info, parent, ma, XATTR_NAME_LMV);
379 GOTO(unlock_parent, rc);
381 if (ma->ma_valid & MA_LMV) {
382 /* don't allow restripe if parent dir layout is changing */
383 lmv = &ma->ma_lmv->lmv_md_v1;
384 if (!lmv_is_sane2(lmv))
385 GOTO(unlock_parent, rc = -EBADF);
387 if (lmv_is_layout_changing(lmv))
388 GOTO(unlock_parent, rc = -EBUSY);
392 rc = mdt_lookup_version_check(info, parent, lname, fid, 1);
394 GOTO(unlock_parent, rc);
396 child = mdt_object_find(info->mti_env, mdt, fid);
398 GOTO(unlock_parent, rc = PTR_ERR(child));
400 if (!mdt_object_exists(child))
401 GOTO(out_child, rc = -ENOENT);
403 if (mdt_object_remote(child)) {
404 struct mdt_body *repbody;
406 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
408 GOTO(out_child, rc = -EPROTO);
410 repbody->mbo_fid1 = *fid;
411 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
412 GOTO(out_child, rc = -EREMOTE);
415 if (!S_ISDIR(lu_object_attr(&child->mot_obj)))
416 GOTO(out_child, rc = -ENOTDIR);
418 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
422 /* race with migrate? */
423 if ((ma->ma_valid & MA_LMV) &&
424 lmv_is_migrating(&ma->ma_lmv->lmv_md_v1))
425 GOTO(out_child, rc = -EBUSY);
428 lhc = &info->mti_lh[MDT_LH_CHILD];
429 mdt_lock_reg_init(lhc, LCK_EX);
431 /* enqueue object remote LOOKUP lock */
432 if (mdt_object_remote(parent)) {
433 rc = mdt_remote_object_lock(info, parent, fid,
436 MDS_INODELOCK_LOOKUP, false);
441 rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
444 GOTO(unlock_child, rc);
446 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
447 rc = mdt_version_get_check_save(info, child, 1);
449 GOTO(unlock_child, rc);
451 spin_lock(&mdt->mdt_restriper.mdr_lock);
452 if (child->mot_restriping) {
454 spin_unlock(&mdt->mdt_restriper.mdr_lock);
455 GOTO(unlock_child, rc = -EBUSY);
457 child->mot_restriping = 1;
458 spin_unlock(&mdt->mdt_restriper.mdr_lock);
461 rc = mdt_restripe_internal(info, parent, child, lname, fid, spec, ma);
463 GOTO(restriping_clear, rc);
465 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
467 GOTO(restriping_clear, rc = -EPROTO);
469 mdt_pack_attr2body(info, repbody, &ma->ma_attr, fid);
473 child->mot_restriping = 0;
475 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
477 mdt_object_put(info->mti_env, child);
479 mdt_object_unlock(info, parent, lhp, rc);
485 * VBR: we save three versions in reply:
486 * 0 - parent. Check that parent version is the same during replay.
487 * 1 - name. Version of 'name' if file exists with the same name or
488 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
489 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
492 static int mdt_create(struct mdt_thread_info *info)
494 struct mdt_device *mdt = info->mti_mdt;
495 struct mdt_object *parent;
496 struct mdt_object *child;
497 struct mdt_lock_handle *lh;
498 struct mdt_body *repbody;
499 struct md_attr *ma = &info->mti_attr;
500 struct mdt_reint_record *rr = &info->mti_rr;
501 struct md_op_spec *spec = &info->mti_spec;
502 bool restripe = false;
506 DEBUG_REQ(D_INODE, mdt_info_req(info),
507 "Create ("DNAME"->"DFID") in "DFID,
508 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
510 if (!fid_is_md_operative(rr->rr_fid1))
513 if (S_ISDIR(ma->ma_attr.la_mode) &&
514 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
515 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
516 struct lu_ucred *uc = mdt_ucred(info);
517 struct obd_export *exp = mdt_info_req(info)->rq_export;
519 /* Only new clients can create remote dir( >= 2.4) and
520 * striped dir(>= 2.6), old client will return -ENOTSUPP
522 if (!mdt_is_dne_client(exp))
525 if (le32_to_cpu(lum->lum_stripe_count) > 1) {
526 if (!mdt_is_striped_client(exp))
529 if (!mdt->mdt_enable_striped_dir)
531 } else if (!mdt->mdt_enable_remote_dir) {
535 if ((!(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH)) &&
536 (le32_to_cpu(lum->lum_hash_type) & LMV_HASH_TYPE_MASK) ==
540 if (!md_capable(uc, CAP_SYS_ADMIN) &&
541 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
542 mdt->mdt_enable_remote_dir_gid != -1)
545 /* restripe if later found dir exists, MDS_OPEN_CREAT means
546 * this is create only, don't try restripe.
548 if (mdt->mdt_enable_dir_restripe &&
549 le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT &&
550 !(spec->sp_cr_flags & MDS_OPEN_CREAT))
554 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
556 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
558 RETURN(PTR_ERR(parent));
560 if (!mdt_object_exists(parent))
561 GOTO(put_parent, rc = -ENOENT);
564 * LU-10235: check if name exists locklessly first to avoid massive
565 * lock recalls on existing directories.
567 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
568 &info->mti_tmp_fid1, 1);
571 GOTO(put_parent, rc = -EEXIST);
573 rc = mdt_restripe(info, parent, &rr->rr_name, rr->rr_fid2, spec,
577 /* -ENOENT is expected here */
579 GOTO(put_parent, rc);
581 /* save version of file name for replay, it must be ENOENT here */
582 mdt_enoent_version_save(info, 1);
584 OBD_RACE(OBD_FAIL_MDS_CREATE_RACE);
586 lh = &info->mti_lh[MDT_LH_PARENT];
587 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
588 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
590 GOTO(put_parent, rc);
592 if (!mdt_object_remote(parent)) {
593 rc = mdt_version_get_check_save(info, parent, 0);
595 GOTO(unlock_parent, rc);
598 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
599 if (unlikely(IS_ERR(child)))
600 GOTO(unlock_parent, rc = PTR_ERR(child));
602 ma->ma_need = MA_INODE;
605 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
606 OBD_FAIL_MDS_REINT_CREATE_WRITE);
608 /* Version of child will be updated on disk. */
609 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
610 rc = mdt_version_get_check_save(info, child, 2);
615 * Do not perform lookup sanity check. We know that name does
618 info->mti_spec.sp_cr_lookup = 0;
619 info->mti_spec.sp_feat = &dt_directory_features;
621 rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
622 mdt_object_child(child), &info->mti_spec, ma);
624 rc = mdt_attr_get_complex(info, child, ma);
630 * On DNE, we need to eliminate dependey between 'mkdir a' and
631 * 'mkdir a/b' if b is a striped directory, to achieve this, two
632 * things are done below:
633 * 1. save child and slaves lock.
634 * 2. if the child is a striped directory, relock parent so to
635 * compare against with COS locks to ensure parent was
638 if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
639 struct mdt_lock_handle *lhc;
640 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
643 rc = mdt_object_striped(info, child);
649 if (!mdt_object_remote(parent)) {
650 mdt_object_unlock(info, parent, lh, 1);
651 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
652 rc = mdt_reint_object_lock(info, parent, lh,
653 MDS_INODELOCK_UPDATE,
660 lhc = &info->mti_lh[MDT_LH_CHILD];
661 mdt_lock_handle_init(lhc);
662 mdt_lock_reg_init(lhc, LCK_PW);
663 rc = mdt_reint_striped_lock(info, child, lhc,
664 MDS_INODELOCK_UPDATE, einfo,
669 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
672 /* Return fid & attr to client. */
673 if (ma->ma_valid & MA_INODE)
674 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
675 mdt_object_fid(child));
678 mdt_object_put(info->mti_env, child);
680 mdt_object_unlock(info, parent, lh, rc);
682 mdt_object_put(info->mti_env, parent);
686 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
689 struct mdt_lock_handle *lh;
690 int do_vbr = ma->ma_attr.la_valid &
691 (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
692 __u64 lockpart = MDS_INODELOCK_UPDATE;
693 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
698 rc = mdt_object_striped(info, mo);
704 lh = &info->mti_lh[MDT_LH_PARENT];
705 mdt_lock_reg_init(lh, LCK_PW);
707 /* Even though the new MDT will grant PERM lock to the old
708 * client, but the old client will almost ignore that during
709 * So it needs to revoke both LOOKUP and PERM lock here, so
710 * both new and old client can cancel the dcache
712 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
713 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
715 rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
720 /* all attrs are packed into mti_attr in unpack_setattr */
721 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
722 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
724 /* VBR: update version if attr changed are important for recovery */
726 /* update on-disk version of changed object */
727 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
728 rc = mdt_version_get_check_save(info, mo, 0);
730 GOTO(out_unlock, rc);
733 /* Ensure constant striping during chown(). See LU-2789. */
734 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
735 mutex_lock(&mo->mot_lov_mutex);
737 /* all attrs are packed into mti_attr in unpack_setattr */
738 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
740 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
741 mutex_unlock(&mo->mot_lov_mutex);
744 GOTO(out_unlock, rc);
745 mdt_dom_obj_lvb_update(info->mti_env, mo, false);
748 mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
753 * Check HSM flags and add HS_DIRTY flag if relevant.
755 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
756 * and is not RELEASED.
758 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
761 struct lu_ucred *uc = mdt_ucred(info);
766 /* If the file was modified, add the dirty flag */
767 ma->ma_need = MA_HSM;
768 rc = mdt_attr_get_complex(info, mo, ma);
770 CERROR("file attribute read error for "DFID": %d.\n",
771 PFID(mdt_object_fid(mo)), rc);
775 /* If an up2date copy exists in the backend, add dirty flag */
776 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
777 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
778 ma->ma_hsm.mh_flags |= HS_DIRTY;
780 /* Bump cap so that closes from non-owner writers can
781 * set the HSM state to dirty.
783 cap_saved = uc->uc_cap;
784 uc->uc_cap |= MD_CAP_TO_MASK(CAP_FOWNER);
785 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
786 uc->uc_cap = cap_saved;
788 CERROR("file attribute change error for "DFID": %d\n",
789 PFID(mdt_object_fid(mo)), rc);
795 static int mdt_reint_setattr(struct mdt_thread_info *info,
796 struct mdt_lock_handle *lhc)
798 struct mdt_device *mdt = info->mti_mdt;
799 struct md_attr *ma = &info->mti_attr;
800 struct mdt_reint_record *rr = &info->mti_rr;
801 struct ptlrpc_request *req = mdt_info_req(info);
802 struct mdt_object *mo;
803 struct mdt_body *repbody;
804 ktime_t kstart = ktime_get();
808 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
809 (unsigned int)ma->ma_attr.la_valid);
811 if (info->mti_dlm_req)
812 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
814 OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
816 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
817 mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
819 GOTO(out, rc = PTR_ERR(mo));
821 if (!mdt_object_exists(mo))
822 GOTO(out_put, rc = -ENOENT);
824 if (mdt_object_remote(mo))
825 GOTO(out_put, rc = -EREMOTE);
827 ma->ma_enable_chprojid_gid = mdt->mdt_enable_chprojid_gid;
828 /* revoke lease lock if size is going to be changed */
829 if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
830 !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
831 atomic_read(&mo->mot_lease_count) > 0)) {
832 down_read(&mo->mot_open_sem);
834 if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
835 lhc = &info->mti_lh[MDT_LH_LOCAL];
836 mdt_lock_reg_init(lhc, LCK_CW);
838 rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
840 up_read(&mo->mot_open_sem);
844 /* revoke lease lock */
845 mdt_object_unlock(info, mo, lhc, 1);
847 up_read(&mo->mot_open_sem);
850 if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
851 /* Check write access for the O_TRUNC case */
852 if (mdt_write_read(mo) < 0)
853 GOTO(out_put, rc = -ETXTBSY);
855 /* LU-10286: compatibility check for FLR.
856 * Please check the comment in mdt_finish_open() for details
858 if (!exp_connect_flr(info->mti_exp) ||
859 !exp_connect_overstriping(info->mti_exp)) {
860 rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
861 if (rc < 0 && rc != -ENODATA)
864 if (!exp_connect_flr(info->mti_exp)) {
866 mdt_lmm_is_flr(info->mti_big_lmm))
867 GOTO(out_put, rc = -EOPNOTSUPP);
870 if (!exp_connect_overstriping(info->mti_exp)) {
872 mdt_lmm_is_overstriping(info->mti_big_lmm))
873 GOTO(out_put, rc = -EOPNOTSUPP);
877 /* For truncate, the file size sent from client
878 * is believable, but the blocks are incorrect,
879 * which makes the block size in LSOM attribute
880 * inconsisent with the real block size.
882 rc = mdt_lsom_update(info, mo, true);
887 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
888 if (ma->ma_valid & MA_LOV)
889 GOTO(out_put, rc = -EPROTO);
891 /* MDT supports FMD for regular files due to Data-on-MDT */
892 if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
893 ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
894 tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
897 if (ma->ma_attr.la_valid & LA_MTIME) {
898 rc = mdt_attr_get_pfid(info, mo, &ma->ma_pfid);
900 ma->ma_valid |= MA_PFID;
904 rc = mdt_attr_set(info, mo, ma);
907 } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
908 (ma->ma_valid & MA_INODE)) {
909 struct lu_buf *buf = &info->mti_buf;
910 struct lu_ucred *uc = mdt_ucred(info);
911 struct mdt_lock_handle *lh;
913 __u64 lockpart = MDS_INODELOCK_XATTR;
915 /* reject if either remote or striped dir is disabled */
916 if (ma->ma_valid & MA_LMV) {
917 if (!mdt->mdt_enable_remote_dir ||
918 !mdt->mdt_enable_striped_dir)
919 GOTO(out_put, rc = -EPERM);
921 if (!md_capable(uc, CAP_SYS_ADMIN) &&
922 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
923 mdt->mdt_enable_remote_dir_gid != -1)
924 GOTO(out_put, rc = -EPERM);
927 if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
928 GOTO(out_put, rc = -ENOTDIR);
930 if (ma->ma_attr.la_valid != 0)
931 GOTO(out_put, rc = -EPROTO);
933 if (ma->ma_valid & MA_LOV) {
934 buf->lb_buf = ma->ma_lmm;
935 buf->lb_len = ma->ma_lmm_size;
936 name = XATTR_NAME_LOV;
938 struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
941 buf->lb_len = ma->ma_lmv_size;
942 name = XATTR_NAME_DEFAULT_LMV;
943 /* force client to update dir default layout */
944 lockpart |= MDS_INODELOCK_LOOKUP;
947 lh = &info->mti_lh[MDT_LH_PARENT];
948 mdt_lock_reg_init(lh, LCK_PW);
950 rc = mdt_object_lock(info, mo, lh, lockpart);
954 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
957 mdt_object_unlock(info, mo, lh, rc);
961 GOTO(out_put, rc = -EPROTO);
964 /* If file data is modified, add the dirty flag */
965 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
966 rc = mdt_add_dirty_flag(info, mo, ma);
968 ma->ma_need = MA_INODE;
970 rc = mdt_attr_get_complex(info, mo, ma);
974 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
978 mdt_object_put(info->mti_env, mo);
981 mdt_counter_incr(req, LPROC_MDT_SETATTR,
982 ktime_us_delta(ktime_get(), kstart));
984 mdt_client_compatibility(info);
985 rc2 = mdt_fix_reply(info);
991 static int mdt_reint_create(struct mdt_thread_info *info,
992 struct mdt_lock_handle *lhc)
994 struct ptlrpc_request *req = mdt_info_req(info);
995 ktime_t kstart = ktime_get();
999 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
1000 RETURN(err_serious(-ESTALE));
1002 if (info->mti_dlm_req)
1003 ldlm_request_cancel(mdt_info_req(info),
1004 info->mti_dlm_req, 0, LATF_SKIP);
1006 if (!lu_name_is_valid(&info->mti_rr.rr_name))
1009 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
1019 CERROR("%s: Unsupported mode %o\n",
1020 mdt_obd_name(info->mti_mdt),
1021 info->mti_attr.ma_attr.la_mode);
1022 RETURN(err_serious(-EOPNOTSUPP));
1025 rc = mdt_create(info);
1027 if ((info->mti_attr.ma_attr.la_mode & S_IFMT) == S_IFDIR)
1028 mdt_counter_incr(req, LPROC_MDT_MKDIR,
1029 ktime_us_delta(ktime_get(), kstart));
1031 /* Special file should stay on the same node as parent*/
1032 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1033 ktime_us_delta(ktime_get(), kstart));
1040 * VBR: save parent version in reply and child version getting by its name.
1041 * Version of child is getting and checking during its lookup. If
1043 static int mdt_reint_unlink(struct mdt_thread_info *info,
1044 struct mdt_lock_handle *lhc)
1046 struct mdt_reint_record *rr = &info->mti_rr;
1047 struct ptlrpc_request *req = mdt_info_req(info);
1048 struct md_attr *ma = &info->mti_attr;
1049 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1050 struct mdt_object *mp;
1051 struct mdt_object *mc;
1052 struct mdt_lock_handle *parent_lh;
1053 struct mdt_lock_handle *child_lh;
1054 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
1056 bool cos_incompat = false;
1058 ktime_t kstart = ktime_get();
1062 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
1063 PNAME(&rr->rr_name));
1065 if (info->mti_dlm_req)
1066 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1068 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1069 RETURN(err_serious(-ENOENT));
1071 if (!fid_is_md_operative(rr->rr_fid1))
1074 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1076 RETURN(PTR_ERR(mp));
1078 if (mdt_object_remote(mp)) {
1079 cos_incompat = true;
1081 rc = mdt_version_get_check_save(info, mp, 0);
1083 GOTO(put_parent, rc);
1086 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1087 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1089 parent_lh = &info->mti_lh[MDT_LH_PARENT];
1090 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
1091 rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
1094 GOTO(put_parent, rc);
1096 /* lookup child object along with version checking */
1097 fid_zero(child_fid);
1098 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
1100 /* Name might not be able to find during resend of
1101 * remote unlink, considering following case.
1102 * dir_A is a remote directory, the name entry of
1103 * dir_A is on MDT0, the directory is on MDT1,
1105 * 1. client sends unlink req to MDT1.
1106 * 2. MDT1 sends name delete update to MDT0.
1107 * 3. name entry is being deleted in MDT0 synchronously.
1108 * 4. MDT1 is restarted.
1109 * 5. client resends unlink req to MDT1. So it can not
1110 * find the name entry on MDT0 anymore.
1111 * In this case, MDT1 only needs to destory the local
1114 if (mdt_object_remote(mp) && rc == -ENOENT &&
1115 !fid_is_zero(rr->rr_fid2) &&
1116 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1118 *child_fid = *rr->rr_fid2;
1120 GOTO(unlock_parent, rc);
1124 if (!fid_is_md_operative(child_fid))
1125 GOTO(unlock_parent, rc = -EPERM);
1127 /* We will lock the child regardless it is local or remote. No harm. */
1128 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1130 GOTO(unlock_parent, rc = PTR_ERR(mc));
1132 if (!cos_incompat) {
1133 rc = mdt_object_striped(info, mc);
1135 GOTO(put_child, rc);
1139 mdt_object_put(info->mti_env, mc);
1140 mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
1145 child_lh = &info->mti_lh[MDT_LH_CHILD];
1146 mdt_lock_reg_init(child_lh, LCK_EX);
1147 if (info->mti_spec.sp_rm_entry) {
1148 struct lu_ucred *uc = mdt_ucred(info);
1150 if (!mdt_is_dne_client(req->rq_export))
1151 /* Return -ENOTSUPP for old client */
1152 GOTO(put_child, rc = -ENOTSUPP);
1154 if (!md_capable(uc, CAP_SYS_ADMIN))
1155 GOTO(put_child, rc = -EPERM);
1157 ma->ma_need = MA_INODE;
1159 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1160 NULL, &rr->rr_name, ma, no_name);
1161 GOTO(put_child, rc);
1164 if (mdt_object_remote(mc)) {
1165 struct mdt_body *repbody;
1167 if (!fid_is_zero(rr->rr_fid2)) {
1168 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
1169 mdt_obd_name(info->mti_mdt),
1170 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1171 GOTO(put_child, rc = -ENOENT);
1173 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
1174 mdt_obd_name(info->mti_mdt),
1175 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1177 if (!mdt_is_dne_client(req->rq_export))
1178 /* Return -ENOTSUPP for old client */
1179 GOTO(put_child, rc = -ENOTSUPP);
1181 /* Revoke the LOOKUP lock of the remote object granted by
1182 * this MDT. Since the unlink will happen on another MDT,
1183 * it will release the LOOKUP lock right away. Then What
1184 * would happen if another client try to grab the LOOKUP
1185 * lock at the same time with unlink XXX
1187 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1188 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1189 LASSERT(repbody != NULL);
1190 repbody->mbo_fid1 = *mdt_object_fid(mc);
1191 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1192 GOTO(unlock_child, rc = -EREMOTE);
1194 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1195 * this now because a running HSM restore on the child (unlink
1196 * victim) will hold the layout lock. See LU-4002.
1198 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1199 if (mdt_object_remote(mp)) {
1200 /* Enqueue lookup lock from parent MDT */
1201 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1202 &child_lh->mlh_rreg_lh,
1203 child_lh->mlh_rreg_mode,
1204 MDS_INODELOCK_LOOKUP, false);
1206 GOTO(put_child, rc);
1208 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1211 rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
1214 GOTO(put_child, rc);
1217 * Now we can only make sure we need MA_INODE, in mdd layer, will check
1218 * whether need MA_LOV and MA_COOKIE.
1220 ma->ma_need = MA_INODE;
1223 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1224 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1225 /* save version when object is locked */
1226 mdt_version_get_save(info, mc, 1);
1228 mutex_lock(&mc->mot_lov_mutex);
1230 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1231 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1233 mutex_unlock(&mc->mot_lov_mutex);
1235 GOTO(unlock_child, rc);
1237 if (!lu_object_is_dying(&mc->mot_header)) {
1238 rc = mdt_attr_get_complex(info, mc, ma);
1241 } else if (mdt_dom_check_for_discard(info, mc)) {
1242 mdt_dom_discard_data(info, mc);
1244 mdt_handle_last_unlink(info, mc, ma);
1247 if (ma->ma_valid & MA_INODE) {
1248 switch (ma->ma_attr.la_mode & S_IFMT) {
1250 mdt_counter_incr(req, LPROC_MDT_RMDIR,
1251 ktime_us_delta(ktime_get(), kstart));
1259 mdt_counter_incr(req, LPROC_MDT_UNLINK,
1260 ktime_us_delta(ktime_get(), kstart));
1263 LASSERTF(0, "bad file type %o unlinking\n",
1264 ma->ma_attr.la_mode);
1271 mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
1273 mdt_object_put(info->mti_env, mc);
1275 mdt_object_unlock(info, mp, parent_lh, rc);
1277 mdt_object_put(info->mti_env, mp);
1278 CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
1283 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1286 static int mdt_reint_link(struct mdt_thread_info *info,
1287 struct mdt_lock_handle *lhc)
1289 struct mdt_reint_record *rr = &info->mti_rr;
1290 struct ptlrpc_request *req = mdt_info_req(info);
1291 struct md_attr *ma = &info->mti_attr;
1292 struct mdt_object *ms;
1293 struct mdt_object *mp;
1294 struct mdt_lock_handle *lhs;
1295 struct mdt_lock_handle *lhp;
1296 ktime_t kstart = ktime_get();
1301 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1302 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1304 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1305 RETURN(err_serious(-ENOENT));
1307 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE) ||
1308 OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND)) {
1309 req->rq_no_reply = 1;
1310 RETURN(err_serious(-ENOENT));
1313 if (info->mti_dlm_req)
1314 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1316 /* Invalid case so return error immediately instead of
1319 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1322 if (!fid_is_md_operative(rr->rr_fid1) ||
1323 !fid_is_md_operative(rr->rr_fid2))
1326 /* step 1: find target parent dir */
1327 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1329 RETURN(PTR_ERR(mp));
1331 rc = mdt_version_get_check_save(info, mp, 0);
1333 GOTO(put_parent, rc);
1335 /* step 2: find source */
1336 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1338 GOTO(put_parent, rc = PTR_ERR(ms));
1340 if (!mdt_object_exists(ms)) {
1341 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1342 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1343 GOTO(put_source, rc = -ENOENT);
1346 cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1348 lhp = &info->mti_lh[MDT_LH_PARENT];
1349 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1350 rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1353 GOTO(put_source, rc);
1355 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1357 lhs = &info->mti_lh[MDT_LH_CHILD];
1358 mdt_lock_reg_init(lhs, LCK_EX);
1359 rc = mdt_reint_object_lock(info, ms, lhs,
1360 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1363 GOTO(unlock_parent, rc);
1365 /* step 3: link it */
1366 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1367 OBD_FAIL_MDS_REINT_LINK_WRITE);
1369 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1370 rc = mdt_version_get_check_save(info, ms, 1);
1372 GOTO(unlock_source, rc);
1374 /** check target version by name during replay */
1375 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1376 &info->mti_tmp_fid1, 2);
1377 if (rc != 0 && rc != -ENOENT)
1378 GOTO(unlock_source, rc);
1379 /* save version of file name for replay, it must be ENOENT here */
1380 if (!req_is_replay(mdt_info_req(info))) {
1381 if (rc != -ENOENT) {
1382 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1383 PNAME(&rr->rr_name));
1384 GOTO(unlock_source, rc = -EEXIST);
1386 info->mti_ver[2] = ENOENT_VERSION;
1387 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1390 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1391 mdt_object_child(ms), &rr->rr_name, ma);
1394 mdt_counter_incr(req, LPROC_MDT_LINK,
1395 ktime_us_delta(ktime_get(), kstart));
1399 mdt_object_unlock(info, ms, lhs, rc);
1401 mdt_object_unlock(info, mp, lhp, rc);
1403 mdt_object_put(info->mti_env, ms);
1405 mdt_object_put(info->mti_env, mp);
1409 * lock the part of the directory according to the hash of the name
1410 * (lh->mlh_pdo_hash) in parallel directory lock.
1412 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1413 struct mdt_lock_handle *lh,
1414 struct mdt_object *obj, __u64 ibits,
1417 struct ldlm_res_id *res = &info->mti_res_id;
1418 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1419 union ldlm_policy_data *policy = &info->mti_policy;
1420 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1424 * Finish res_id initializing by name hash marking part of
1425 * directory which is taking modification.
1427 LASSERT(lh->mlh_pdo_hash != 0);
1428 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1429 memset(policy, 0, sizeof(*policy));
1430 policy->l_inodebits.bits = ibits;
1432 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1433 dlmflags |= LDLM_FL_COS_INCOMPAT;
1435 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1436 * going to be sent to client. If it is - mdt_intent_policy() path will
1437 * fix it up and turn FL_LOCAL flag off.
1439 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
1440 policy, res, dlmflags,
1441 &info->mti_exp->exp_handle.h_cookie);
1446 * Get BFL lock for rename or migrate process.
1448 static int mdt_rename_lock(struct mdt_thread_info *info,
1449 struct lustre_handle *lh)
1454 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1455 struct lu_fid *fid = &info->mti_tmp_fid1;
1456 struct mdt_object *obj;
1458 /* XXX, right now, it has to use object API to
1459 * enqueue lock cross MDT, so it will enqueue
1460 * rename lock(with LUSTRE_BFL_FID) by root object
1463 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1465 RETURN(PTR_ERR(obj));
1467 rc = mdt_remote_object_lock(info, obj,
1468 &LUSTRE_BFL_FID, lh,
1470 MDS_INODELOCK_UPDATE, false);
1471 mdt_object_put(info->mti_env, obj);
1473 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1474 union ldlm_policy_data *policy = &info->mti_policy;
1475 struct ldlm_res_id *res_id = &info->mti_res_id;
1478 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1479 memset(policy, 0, sizeof(*policy));
1480 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1481 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1482 rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
1483 LDLM_IBITS, policy, LCK_EX, &flags,
1485 ldlm_completion_ast, NULL, NULL, 0,
1487 &info->mti_exp->exp_handle.h_cookie,
1494 static void mdt_rename_unlock(struct lustre_handle *lh)
1497 LASSERT(lustre_handle_is_used(lh));
1498 /* Cancel the single rename lock right away */
1499 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1503 static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
1504 const struct lu_fid *fid,
1507 struct mdt_object *dir;
1511 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1515 /* check early, the real version will be saved after locking */
1516 rc = mdt_version_get_check(info, dir, idx);
1520 if (!mdt_object_exists(dir))
1521 GOTO(out_put, rc = -ENOENT);
1523 if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
1524 GOTO(out_put, rc = -ENOTDIR);
1528 mdt_object_put(info->mti_env, dir);
1533 * in case obj is remote obj on its parent, revoke LOOKUP lock,
1534 * herein we don't really check it, just do revoke.
1536 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
1537 struct mdt_object *pobj,
1538 struct mdt_object *obj)
1540 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1543 mdt_lock_handle_init(lh);
1544 mdt_lock_reg_init(lh, LCK_EX);
1546 if (mdt_object_remote(pobj)) {
1547 /* don't bother to check if pobj and obj are on the same MDT. */
1548 rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
1549 &lh->mlh_rreg_lh, LCK_EX,
1550 MDS_INODELOCK_LOOKUP, false);
1551 } else if (mdt_object_remote(obj)) {
1552 struct ldlm_res_id *res = &info->mti_res_id;
1553 union ldlm_policy_data *policy = &info->mti_policy;
1554 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
1555 LDLM_FL_COS_INCOMPAT;
1557 fid_build_reg_res_name(mdt_object_fid(obj), res);
1558 memset(policy, 0, sizeof(*policy));
1559 policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
1560 rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
1561 &lh->mlh_reg_lh, LCK_EX, policy, res,
1564 /* do nothing if both are local */
1572 * TODO, currently we don't save this lock because there is no place to
1573 * hold this lock handle, but to avoid race we need to save this lock.
1575 mdt_object_unlock(info, NULL, lh, 1);
1581 * operation may takes locks of linkea, or directory stripes, group them in
1584 struct mdt_sub_lock {
1585 struct mdt_object *msl_obj;
1586 struct mdt_lock_handle msl_lh;
1587 struct list_head msl_linkage;
1590 static void mdt_unlock_list(struct mdt_thread_info *info,
1591 struct list_head *list, int decref)
1593 struct mdt_sub_lock *msl;
1594 struct mdt_sub_lock *tmp;
1596 list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
1597 mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
1598 list_del(&msl->msl_linkage);
1603 static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
1604 struct mdt_object *obj,
1605 struct mdt_lock_handle *lh,
1606 struct ldlm_enqueue_info *einfo,
1607 struct list_head *slave_locks,
1610 if (mdt_object_remote(obj)) {
1611 mdt_unlock_list(info, slave_locks, decref);
1612 mdt_object_unlock(info, obj, lh, decref);
1614 mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
1619 * lock parents of links, and also check whether total locks don't exceed
1622 * \retval 0 on success, and locks can be saved in ptlrpc_reply_stat
1623 * \retval 1 on success, but total lock count may exceed RS_MAX_LOCKS
1624 * \retval -ev negative errno upon error
1626 static int mdt_link_parents_lock(struct mdt_thread_info *info,
1627 struct mdt_object *pobj,
1628 const struct md_attr *ma,
1629 struct mdt_object *obj,
1630 struct mdt_lock_handle *lhp,
1631 struct ldlm_enqueue_info *peinfo,
1632 struct list_head *parent_slave_locks,
1633 struct list_head *link_locks)
1635 struct mdt_device *mdt = info->mti_mdt;
1636 struct lu_buf *buf = &info->mti_big_buf;
1637 struct lu_name *lname = &info->mti_name;
1638 struct linkea_data ldata = { NULL };
1639 bool blocked = false;
1640 int local_lnkp_cnt = 0;
1644 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1647 buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
1648 if (buf->lb_buf == NULL)
1652 rc = mdt_links_read(info, obj, &ldata);
1654 if (rc == -ENOENT || rc == -ENODATA)
1659 for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
1660 linkea_next_entry(&ldata)) {
1661 struct mdt_object *lnkp;
1662 struct mdt_sub_lock *msl;
1666 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
1669 /* check if it's also linked to parent */
1670 if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
1671 CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
1672 PFID(&fid), PNAME(lname));
1673 /* in case link is remote object, revoke LOOKUP lock */
1674 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1680 /* check if it's linked to a stripe of parent */
1681 if (ma->ma_valid & MA_LMV) {
1682 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1683 struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
1686 for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
1687 fid_le_to_cpu(stripe_fid,
1688 &lmv->lmv_stripe_fids[j]);
1689 if (lu_fid_eq(stripe_fid, &fid)) {
1690 CDEBUG(D_INFO, "skip stripe "DFID
1691 ", reovke "DNAME"\n",
1692 PFID(&fid), PNAME(lname));
1693 lnkp = mdt_object_find(info->mti_env,
1696 GOTO(out, rc = PTR_ERR(lnkp));
1702 rc = mdt_revoke_remote_lookup_lock(info, lnkp,
1704 mdt_object_put(info->mti_env, lnkp);
1709 /* Check if it's already locked */
1710 list_for_each_entry(msl, link_locks, msl_linkage) {
1711 if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
1713 DFID" was locked, revoke "DNAME"\n",
1714 PFID(&fid), PNAME(lname));
1715 lnkp = msl->msl_obj;
1721 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1725 CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
1726 PFID(&fid), PNAME(lname));
1728 lnkp = mdt_object_find(info->mti_env, mdt, &fid);
1730 CWARN("%s: cannot find obj "DFID": %ld\n",
1731 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
1735 if (!mdt_object_exists(lnkp)) {
1736 CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
1737 PFID(&fid), PNAME(lname));
1738 mdt_object_put(info->mti_env, lnkp);
1742 if (!mdt_object_remote(lnkp))
1747 GOTO(out, rc = -ENOMEM);
1750 * we can't follow parent-child lock order like other MD
1751 * operations, use lock_try here to avoid deadlock, if the lock
1752 * cannot be taken, drop all locks taken, revoke the blocked
1753 * one, and continue processing the remaining entries, and in
1754 * the end of the loop restart from beginning.
1756 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1758 rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
1759 MDS_INODELOCK_UPDATE, true);
1760 if (!(ibits & MDS_INODELOCK_UPDATE)) {
1762 CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
1763 PFID(&fid), PNAME(lname));
1765 mdt_unlock_list(info, link_locks, 1);
1766 /* also unlock parent locks to avoid deadlock */
1768 mdt_migrate_object_unlock(info, pobj, lhp,
1775 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1776 rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
1777 MDS_INODELOCK_UPDATE);
1779 mdt_object_put(info->mti_env, lnkp);
1784 if (mdt_object_remote(lnkp)) {
1785 struct ldlm_lock *lock;
1788 * for remote object, set lock cb_atomic,
1789 * so lock can be released in blocking_ast()
1790 * immediately, then the next lock_try will
1791 * have better chance of success.
1793 lock = ldlm_handle2lock(
1794 &msl->msl_lh.mlh_rreg_lh);
1795 LASSERT(lock != NULL);
1796 lock_res_and_lock(lock);
1797 ldlm_set_atomic_cb(lock);
1798 unlock_res_and_lock(lock);
1799 LDLM_LOCK_PUT(lock);
1802 mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
1807 INIT_LIST_HEAD(&msl->msl_linkage);
1808 msl->msl_obj = lnkp;
1809 list_add_tail(&msl->msl_linkage, link_locks);
1811 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1815 GOTO(out, rc = -EBUSY);
1820 mdt_unlock_list(info, link_locks, rc);
1821 } else if (local_lnkp_cnt > RS_MAX_LOCKS - 5) {
1822 CDEBUG(D_INFO, "Too many links (%d), sync operations\n",
1825 * parent may have 3 local objects: master object and 2 stripes
1826 * (if it's being migrated too); source may have 1 local objects
1827 * as regular file; target has 1 local object.
1828 * Note, source may have 2 local locks if it is directory but it
1829 * can't have hardlinks, so it is not considered here.
1836 static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
1837 struct mdt_object *obj,
1838 const struct md_attr *ma,
1839 struct list_head *slave_locks)
1841 struct mdt_device *mdt = info->mti_mdt;
1842 const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1843 struct lu_fid *fid = &info->mti_tmp_fid1;
1844 struct mdt_object *slave;
1845 struct mdt_sub_lock *msl;
1850 LASSERT(mdt_object_remote(obj));
1851 LASSERT(ma->ma_valid & MA_LMV);
1854 if (!lmv_is_sane(lmv))
1857 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
1858 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
1860 if (!fid_is_sane(fid))
1863 slave = mdt_object_find(info->mti_env, mdt, fid);
1865 GOTO(out, rc = PTR_ERR(slave));
1869 mdt_object_put(info->mti_env, slave);
1870 GOTO(out, rc = -ENOMEM);
1873 mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
1874 rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
1875 MDS_INODELOCK_UPDATE, true);
1878 mdt_object_put(info->mti_env, slave);
1882 INIT_LIST_HEAD(&msl->msl_linkage);
1883 msl->msl_obj = slave;
1884 list_add_tail(&msl->msl_linkage, slave_locks);
1890 mdt_unlock_list(info, slave_locks, rc);
1894 /* lock parent and its stripes */
1895 static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
1896 struct mdt_object *obj,
1897 const struct md_attr *ma,
1898 struct mdt_lock_handle *lh,
1899 struct ldlm_enqueue_info *einfo,
1900 struct list_head *slave_locks)
1904 if (mdt_object_remote(obj)) {
1905 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1906 &lh->mlh_rreg_lh, LCK_PW,
1907 MDS_INODELOCK_UPDATE, false);
1912 * if obj is remote and striped, lock its stripes explicitly
1913 * because it's not striped in LOD layer on this MDT.
1915 if (ma->ma_valid & MA_LMV) {
1916 rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
1918 mdt_object_unlock(info, obj, lh, rc);
1921 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
1929 * in migration, object may be remote, and we need take full lock of it and its
1930 * stripes if it's directory, besides, object may be a remote object on its
1931 * parent, revoke its LOOKUP lock on where its parent is located.
1933 static int mdt_migrate_object_lock(struct mdt_thread_info *info,
1934 struct mdt_object *pobj,
1935 struct mdt_object *obj,
1936 struct mdt_lock_handle *lh,
1937 struct ldlm_enqueue_info *einfo,
1938 struct list_head *slave_locks)
1942 if (mdt_object_remote(obj)) {
1943 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1947 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1948 &lh->mlh_rreg_lh, LCK_EX,
1949 MDS_INODELOCK_FULL, false);
1954 * if obj is remote and striped, lock its stripes explicitly
1955 * because it's not striped in LOD layer on this MDT.
1957 if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
1958 struct md_attr *ma = &info->mti_attr;
1960 rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
1962 mdt_object_unlock(info, obj, lh, rc);
1966 if (ma->ma_valid & MA_LMV) {
1967 rc = mdt_lock_remote_slaves(info, obj, ma,
1970 mdt_object_unlock(info, obj, lh, rc);
1974 if (mdt_object_remote(pobj)) {
1975 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1980 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
1988 * lookup source by name, if parent is striped directory, we need to find the
1989 * corresponding stripe where source is located, and then lookup there.
1991 * besides, if parent is migrating too, and file is already in target stripe,
1992 * this should be a redo of 'lfs migrate' on client side.
1994 static int mdt_migrate_lookup(struct mdt_thread_info *info,
1995 struct mdt_object *pobj,
1996 const struct md_attr *ma,
1997 const struct lu_name *lname,
1998 struct mdt_object **spobj,
1999 struct mdt_object **sobj)
2001 const struct lu_env *env = info->mti_env;
2002 struct lu_fid *fid = &info->mti_tmp_fid1;
2003 struct mdt_object *stripe;
2006 if (ma->ma_valid & MA_LMV) {
2007 /* if parent is striped, lookup on corresponding stripe */
2008 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
2010 if (!lmv_is_sane(lmv))
2013 rc = lmv_name_to_stripe_index_old(lmv, lname->ln_name,
2018 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2020 stripe = mdt_object_find(env, info->mti_mdt, fid);
2022 return PTR_ERR(stripe);
2025 rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
2027 if (rc == -ENOENT && lmv_is_layout_changing(lmv)) {
2029 * if parent layout is changeing, and lookup child
2030 * failed on source stripe, lookup again on target
2031 * stripe, if it exists, it means previous migration
2032 * was interrupted, and current file was migrated
2035 mdt_object_put(env, stripe);
2037 rc = lmv_name_to_stripe_index(lmv, lname->ln_name,
2042 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2044 stripe = mdt_object_find(env, info->mti_mdt, fid);
2046 return PTR_ERR(stripe);
2049 rc = mdo_lookup(env, mdt_object_child(stripe), lname,
2050 fid, &info->mti_spec);
2051 mdt_object_put(env, stripe);
2052 return rc ?: -EALREADY;
2054 mdt_object_put(env, stripe);
2059 rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
2065 mdt_object_get(env, stripe);
2070 *sobj = mdt_object_find(env, info->mti_mdt, fid);
2071 if (IS_ERR(*sobj)) {
2072 mdt_object_put(env, stripe);
2073 rc = PTR_ERR(*sobj);
2081 /* end lease and close file for regular file */
2082 static int mdd_migrate_close(struct mdt_thread_info *info,
2083 struct mdt_object *obj)
2085 struct close_data *data;
2086 struct mdt_body *repbody;
2087 struct ldlm_lock *lease;
2092 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
2094 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
2098 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2103 lease = ldlm_handle2lock(&data->cd_handle);
2107 /* check if the lease was already canceled */
2108 lock_res_and_lock(lease);
2109 rc = ldlm_is_cancel(lease);
2110 unlock_res_and_lock(lease);
2114 LDLM_DEBUG(lease, DFID" lease broken",
2115 PFID(mdt_object_fid(obj)));
2119 * cancel server side lease, client side counterpart should have been
2120 * cancelled, it's okay to cancel it now as we've held mot_open_sem.
2122 ldlm_lock_cancel(lease);
2123 ldlm_reprocess_all(lease->l_resource, lease);
2124 LDLM_LOCK_PUT(lease);
2127 rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
2128 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2129 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2135 * migrate file in below steps:
2136 * 1. lock parent and its stripes
2137 * 2. lookup source by name
2138 * 3. lock parents of source links if source is not directory
2139 * 4. reject if source is in HSM
2140 * 5. take source open_sem and close file if source is regular file
2141 * 6. lock source and its stripes if it's directory
2142 * 7. lock target so subsequent change to it can trigger COS
2144 * 9. unlock above locks
2145 * 10. sync device if source has links
2147 int mdt_reint_migrate(struct mdt_thread_info *info,
2148 struct mdt_lock_handle *unused)
2150 const struct lu_env *env = info->mti_env;
2151 struct mdt_device *mdt = info->mti_mdt;
2152 struct ptlrpc_request *req = mdt_info_req(info);
2153 struct mdt_reint_record *rr = &info->mti_rr;
2154 struct lu_ucred *uc = mdt_ucred(info);
2155 struct md_attr *ma = &info->mti_attr;
2156 struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
2157 struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
2158 struct mdt_object *pobj;
2159 struct mdt_object *spobj = NULL;
2160 struct mdt_object *sobj = NULL;
2161 struct mdt_object *tobj;
2162 struct lustre_handle rename_lh = { 0 };
2163 struct mdt_lock_handle *lhp;
2164 struct mdt_lock_handle *lhs;
2165 struct mdt_lock_handle *lht;
2166 LIST_HEAD(parent_slave_locks);
2167 LIST_HEAD(child_slave_locks);
2168 LIST_HEAD(link_locks);
2169 int lock_retries = 5;
2170 bool open_sem_locked = false;
2171 bool do_sync = false;
2175 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
2176 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
2178 if (info->mti_dlm_req)
2179 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2181 if (!fid_is_md_operative(rr->rr_fid1) ||
2182 !fid_is_md_operative(rr->rr_fid2))
2185 /* don't allow migrate . or .. */
2186 if (lu_name_is_dot_or_dotdot(&rr->rr_name))
2189 if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
2192 if (uc && !md_capable(uc, CAP_SYS_ADMIN) &&
2193 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
2194 mdt->mdt_enable_remote_dir_gid != -1)
2198 * Note: do not enqueue rename lock for replay request, because
2199 * if other MDT holds rename lock, but being blocked to wait for
2200 * this MDT to finish its recovery, and the failover MDT can not
2201 * get rename lock, which will cause deadlock.
2203 * req is NULL if this is called by directory auto-split.
2205 if (req && !req_is_replay(req)) {
2206 rc = mdt_rename_lock(info, &rename_lh);
2208 CERROR("%s: can't lock FS for rename: rc = %d\n",
2209 mdt_obd_name(info->mti_mdt), rc);
2214 /* pobj is master object of parent */
2215 pobj = mdt_object_find(env, mdt, rr->rr_fid1);
2217 GOTO(unlock_rename, rc = PTR_ERR(pobj));
2220 rc = mdt_version_get_check(info, pobj, 0);
2222 GOTO(put_parent, rc);
2225 if (!mdt_object_exists(pobj))
2226 GOTO(put_parent, rc = -ENOENT);
2228 if (!S_ISDIR(lu_object_attr(&pobj->mot_obj)))
2229 GOTO(put_parent, rc = -ENOTDIR);
2231 rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
2233 GOTO(put_parent, rc);
2236 /* lock parent object */
2237 lhp = &info->mti_lh[MDT_LH_PARENT];
2238 mdt_lock_reg_init(lhp, LCK_PW);
2239 rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
2240 &parent_slave_locks);
2242 GOTO(put_parent, rc);
2245 * spobj is the corresponding stripe against name if pobj is striped
2246 * directory, which is the real parent, and no need to lock, because
2247 * we've taken full lock of pobj.
2249 rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
2251 GOTO(unlock_parent, rc);
2253 /* lock parents of source links, and revoke LOOKUP lock of links */
2254 rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
2255 &parent_slave_locks, &link_locks);
2256 if (rc == -EBUSY && lock_retries-- > 0) {
2257 mdt_object_put(env, sobj);
2258 mdt_object_put(env, spobj);
2263 GOTO(put_source, rc);
2266 * RS_MAX_LOCKS is the limit of number of locks that can be saved along
2267 * with one request, if total lock count exceeds this limit, we will
2268 * drop all locks after migration, and synchronous device in the end.
2272 /* TODO: DoM migration is not supported, migrate dirent only */
2273 if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
2274 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
2276 GOTO(unlock_links, rc);
2278 if (ma->ma_valid & MA_LOV && mdt_lmm_dom_stripesize(ma->ma_lmm))
2279 info->mti_spec.sp_migrate_nsonly = 1;
2280 } else if (S_ISDIR(lu_object_attr(&sobj->mot_obj))) {
2281 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2283 GOTO(unlock_links, rc);
2285 /* race with restripe/auto-split? */
2286 if ((ma->ma_valid & MA_LMV) &&
2287 lmv_is_restriping(&ma->ma_lmv->lmv_md_v1))
2288 GOTO(unlock_links, rc = -EBUSY);
2291 /* if migration HSM is allowed */
2292 if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
2293 ma->ma_need = MA_HSM;
2295 rc = mdt_attr_get_complex(info, sobj, ma);
2297 GOTO(unlock_links, rc);
2299 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
2300 GOTO(unlock_links, rc = -EOPNOTSUPP);
2303 /* end lease and close file for regular file */
2304 if (info->mti_spec.sp_migrate_close) {
2305 /* try to hold open_sem so that nobody else can open the file */
2306 if (!down_write_trylock(&sobj->mot_open_sem)) {
2308 mdd_migrate_close(info, sobj);
2309 GOTO(unlock_links, rc = -EBUSY);
2311 open_sem_locked = true;
2312 rc = mdd_migrate_close(info, sobj);
2314 GOTO(unlock_open_sem, rc);
2319 lhs = &info->mti_lh[MDT_LH_OLD];
2320 mdt_lock_reg_init(lhs, LCK_EX);
2321 rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
2322 &child_slave_locks);
2324 GOTO(unlock_open_sem, rc);
2327 tobj = mdt_object_find(env, mdt, rr->rr_fid2);
2329 GOTO(unlock_source, rc = PTR_ERR(tobj));
2331 lht = &info->mti_lh[MDT_LH_NEW];
2332 mdt_lock_reg_init(lht, LCK_EX);
2333 rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
2335 GOTO(put_target, rc);
2337 /* Don't do lookup sanity check. We know name doesn't exist. */
2338 info->mti_spec.sp_cr_lookup = 0;
2339 info->mti_spec.sp_feat = &dt_directory_features;
2341 rc = mdo_migrate(env, mdt_object_child(pobj),
2342 mdt_object_child(sobj), &rr->rr_name,
2343 mdt_object_child(tobj),
2344 &info->mti_spec, ma);
2346 lprocfs_counter_incr(mdt->mdt_lu_dev.ld_obd->obd_md_stats,
2347 LPROC_MDT_MIGRATE + LPROC_MD_LAST_OPC);
2350 mdt_object_unlock(info, tobj, lht, rc);
2352 mdt_object_put(env, tobj);
2354 mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
2355 &child_slave_locks, rc);
2357 if (open_sem_locked)
2358 up_write(&sobj->mot_open_sem);
2360 /* if we've got too many locks to save into RPC,
2361 * then just commit before the locks are released
2364 mdt_device_sync(env, mdt);
2365 mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
2367 mdt_object_put(env, sobj);
2368 mdt_object_put(env, spobj);
2370 mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
2371 &parent_slave_locks, rc);
2373 mdt_object_put(env, pobj);
2375 if (lustre_handle_is_used(&rename_lh))
2376 mdt_rename_unlock(&rename_lh);
2381 static int mdt_object_lock_save(struct mdt_thread_info *info,
2382 struct mdt_object *dir,
2383 struct mdt_lock_handle *lh,
2384 int idx, bool cos_incompat)
2388 /* we lock the target dir if it is local */
2389 rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
2394 /* get and save correct version after locking */
2395 mdt_version_get_save(info, dir, idx);
2400 * determine lock order of sobj and tobj
2402 * there are two situations we need to lock tobj before sobj:
2403 * 1. sobj is child of tobj
2404 * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
2405 * larger than that of tobj
2407 * \retval 1 lock tobj before sobj
2408 * \retval 0 lock sobj before tobj
2409 * \retval -ev negative errno upon error
2411 static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
2412 struct mdt_object *sobj,
2413 struct mdt_object *tobj)
2415 struct md_attr *ma = &info->mti_attr;
2416 struct lu_fid *spfid = &info->mti_tmp_fid1;
2417 struct lu_fid *tpfid = &info->mti_tmp_fid2;
2418 struct lmv_mds_md_v1 *lmv;
2423 /* sobj and tobj are the same */
2427 if (fid_is_root(mdt_object_fid(sobj)))
2430 if (fid_is_root(mdt_object_fid(tobj)))
2433 /* check whether sobj is child of tobj */
2434 rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
2435 mdt_object_fid(tobj));
2442 /* check whether sobj and tobj are children of the same parent */
2443 rc = mdt_attr_get_pfid(info, sobj, spfid);
2447 rc = mdt_attr_get_pfid(info, tobj, tpfid);
2451 if (!lu_fid_eq(spfid, tpfid))
2454 /* check whether sobj and tobj are sibling stripes */
2455 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2459 if (!(ma->ma_valid & MA_LMV))
2462 lmv = &ma->ma_lmv->lmv_md_v1;
2463 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2465 sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2468 rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
2472 if (!(ma->ma_valid & MA_LMV))
2475 lmv = &ma->ma_lmv->lmv_md_v1;
2476 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2478 tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2480 /* check stripe index of sobj and tobj */
2481 if (sindex == tindex)
2484 return sindex < tindex ? 0 : 1;
2488 * lock rename source object.
2490 * Both source and source parent may be remote, and source may be a remote
2491 * object on source parent, to avoid overriding lock handle, store remote
2492 * LOOKUP lock separately in @lhr.
2494 * \retval 0 on success
2495 * \retval -ev negative errno upon error
2497 static int mdt_rename_source_lock(struct mdt_thread_info *info,
2498 struct mdt_object *parent,
2499 struct mdt_object *child,
2500 struct mdt_lock_handle *lhc,
2501 struct mdt_lock_handle *lhr,
2507 rc = mdt_is_remote_object(info, parent, child);
2512 /* enqueue remote LOOKUP lock from the parent MDT */
2513 __u64 rmt_ibits = MDS_INODELOCK_LOOKUP;
2515 if (mdt_object_remote(parent)) {
2516 rc = mdt_remote_object_lock(info, parent,
2517 mdt_object_fid(child),
2524 LASSERT(mdt_object_remote(child));
2525 rc = mdt_object_local_lock(info, child, lhr,
2526 &rmt_ibits, 0, true);
2531 ibits &= ~MDS_INODELOCK_LOOKUP;
2534 if (mdt_object_remote(child)) {
2535 rc = mdt_remote_object_lock(info, child, mdt_object_fid(child),
2542 rc = mdt_reint_object_lock(info, child, lhc, ibits,
2547 mdt_object_unlock(info, child, lhr, rc);
2553 * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
2554 * 2 - srcdir child; 3 - tgtdir child.
2555 * Update on disk version of srcdir child.
2557 static int mdt_reint_rename(struct mdt_thread_info *info,
2558 struct mdt_lock_handle *unused)
2560 struct mdt_device *mdt = info->mti_mdt;
2561 struct mdt_reint_record *rr = &info->mti_rr;
2562 struct md_attr *ma = &info->mti_attr;
2563 struct ptlrpc_request *req = mdt_info_req(info);
2564 struct mdt_object *msrcdir = NULL;
2565 struct mdt_object *mtgtdir = NULL;
2566 struct mdt_object *mold;
2567 struct mdt_object *mnew = NULL;
2568 struct lustre_handle rename_lh = { 0 };
2569 struct mdt_lock_handle *lh_srcdirp;
2570 struct mdt_lock_handle *lh_tgtdirp;
2571 struct mdt_lock_handle *lh_oldp = NULL;
2572 struct mdt_lock_handle *lh_rmt = NULL;
2573 struct mdt_lock_handle *lh_newp = NULL;
2574 struct lu_fid *old_fid = &info->mti_tmp_fid1;
2575 struct lu_fid *new_fid = &info->mti_tmp_fid2;
2577 bool reverse = false, discard = false;
2579 ktime_t kstart = ktime_get();
2583 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
2584 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
2585 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
2587 if (info->mti_dlm_req)
2588 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2590 if (!fid_is_md_operative(rr->rr_fid1) ||
2591 !fid_is_md_operative(rr->rr_fid2))
2594 /* find both parents. */
2595 msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
2596 if (IS_ERR(msrcdir))
2597 RETURN(PTR_ERR(msrcdir));
2599 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
2601 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
2603 mdt_object_get(info->mti_env, mtgtdir);
2605 mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
2606 if (IS_ERR(mtgtdir))
2607 GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
2611 * Note: do not enqueue rename lock for replay request, because
2612 * if other MDT holds rename lock, but being blocked to wait for
2613 * this MDT to finish its recovery, and the failover MDT can not
2614 * get rename lock, which will cause deadlock.
2616 if (!req_is_replay(req)) {
2618 * Normally rename RPC is handled on the MDT with the target
2619 * directory (if target exists, it's on the MDT with the
2620 * target), if the source directory is remote, it's a hint that
2621 * source is remote too (this may not be true, but it won't
2622 * cause any issue), return -EXDEV early to avoid taking
2625 if (!mdt->mdt_enable_remote_rename &&
2626 mdt_object_remote(msrcdir))
2627 GOTO(out_put_tgtdir, rc = -EXDEV);
2629 /* This might be further relaxed in the future for regular file
2630 * renames in different source and target parents. Start with
2631 * only same-directory renames for simplicity and because this
2632 * is by far the most the common use case.
2634 if (msrcdir != mtgtdir) {
2635 rc = mdt_rename_lock(info, &rename_lh);
2637 CERROR("%s: cannot lock for rename: rc = %d\n",
2638 mdt_obd_name(mdt), rc);
2639 GOTO(out_put_tgtdir, rc);
2642 CDEBUG(D_INFO, "%s: samedir rename "DFID"/"DNAME"\n",
2643 mdt_obd_name(mdt), PFID(rr->rr_fid1),
2644 PNAME(&rr->rr_name));
2648 rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
2650 GOTO(out_unlock_rename, rc);
2654 /* source needs to be looked up after locking source parent, otherwise
2655 * this rename may race with unlink source, and cause rename hang, see
2656 * sanityn.sh 55b, so check parents first, if later we found source is
2657 * remote, relock parents.
2659 cos_incompat = (mdt_object_remote(msrcdir) ||
2660 mdt_object_remote(mtgtdir));
2662 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2664 /* lock parents in the proper order. */
2665 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
2666 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
2668 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
2669 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
2671 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
2672 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
2675 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2678 GOTO(out_unlock_rename, rc);
2680 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2682 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2685 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2686 GOTO(out_unlock_rename, rc);
2689 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2692 GOTO(out_unlock_rename, rc);
2694 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2696 if (mtgtdir != msrcdir) {
2697 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2699 } else if (!mdt_object_remote(mtgtdir) &&
2700 lh_srcdirp->mlh_pdo_hash !=
2701 lh_tgtdirp->mlh_pdo_hash) {
2702 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
2703 MDS_INODELOCK_UPDATE,
2705 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
2708 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2709 GOTO(out_unlock_rename, rc);
2713 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2714 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
2716 /* find mold object. */
2718 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
2720 GOTO(out_unlock_parents, rc);
2722 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
2723 GOTO(out_unlock_parents, rc = -EINVAL);
2725 if (!fid_is_md_operative(old_fid))
2726 GOTO(out_unlock_parents, rc = -EPERM);
2728 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
2730 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
2732 if (!mdt_object_exists(mold)) {
2733 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2735 "object does not exist");
2736 GOTO(out_put_old, rc = -ENOENT);
2739 if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
2740 GOTO(out_put_old, rc = -EXDEV);
2742 /* Check if @mtgtdir is subdir of @mold, before locking child
2743 * to avoid reverse locking.
2745 if (mtgtdir != msrcdir) {
2746 rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
2751 GOTO(out_put_old, rc);
2755 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
2756 /* save version after locking */
2757 mdt_version_get_save(info, mold, 2);
2759 if (!cos_incompat && mdt_object_remote(mold)) {
2760 cos_incompat = true;
2761 mdt_object_put(info->mti_env, mold);
2762 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
2763 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
2767 /* find mnew object:
2768 * mnew target object may not exist now
2769 * lookup with version checking
2772 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
2775 /* the new_fid should have been filled at this moment */
2776 if (lu_fid_eq(old_fid, new_fid))
2777 GOTO(out_put_old, rc);
2779 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
2780 lu_fid_eq(new_fid, rr->rr_fid2))
2781 GOTO(out_put_old, rc = -EINVAL);
2783 if (!fid_is_md_operative(new_fid))
2784 GOTO(out_put_old, rc = -EPERM);
2786 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
2788 GOTO(out_put_old, rc = PTR_ERR(mnew));
2790 if (!mdt_object_exists(mnew)) {
2791 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2793 "object does not exist");
2794 GOTO(out_put_new, rc = -ENOENT);
2797 if (mdt_object_remote(mnew)) {
2798 struct mdt_body *repbody;
2800 /* Always send rename req to the target child MDT */
2801 repbody = req_capsule_server_get(info->mti_pill,
2803 LASSERT(repbody != NULL);
2804 repbody->mbo_fid1 = *new_fid;
2805 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2806 GOTO(out_put_new, rc = -EXDEV);
2808 /* Before locking the target dir, check we do not replace
2809 * a dir with a non-dir, otherwise it may deadlock with
2810 * link op which tries to create a link in this dir
2811 * back to this non-dir.
2813 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2814 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2815 GOTO(out_put_new, rc = -EISDIR);
2817 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2818 lh_rmt = &info->mti_lh[MDT_LH_RMT];
2819 mdt_lock_reg_init(lh_oldp, LCK_EX);
2820 mdt_lock_reg_init(lh_rmt, LCK_EX);
2821 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2822 rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
2823 lh_rmt, lock_ibits, cos_incompat);
2825 GOTO(out_put_new, rc);
2827 /* Check if @msrcdir is subdir of @mnew, before locking child
2828 * to avoid reverse locking.
2830 if (mtgtdir != msrcdir) {
2831 rc = mdo_is_subdir(info->mti_env,
2832 mdt_object_child(msrcdir), new_fid);
2836 GOTO(out_unlock_old, rc);
2840 /* We used to acquire MDS_INODELOCK_FULL here but we
2841 * can't do this now because a running HSM restore on
2842 * the rename onto victim will hold the layout
2843 * lock. See LU-4002.
2846 lh_newp = &info->mti_lh[MDT_LH_NEW];
2847 mdt_lock_reg_init(lh_newp, LCK_EX);
2848 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2849 if (mdt_object_remote(mtgtdir)) {
2850 rc = mdt_remote_object_lock(info, mtgtdir,
2851 mdt_object_fid(mnew),
2852 &lh_newp->mlh_rreg_lh,
2853 lh_newp->mlh_rreg_mode,
2854 MDS_INODELOCK_LOOKUP,
2857 GOTO(out_unlock_old, rc);
2859 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2861 rc = mdt_reint_object_lock(info, mnew, lh_newp, lock_ibits,
2864 GOTO(out_unlock_new, rc);
2866 /* get and save version after locking */
2867 mdt_version_get_save(info, mnew, 3);
2868 } else if (rc != -ENOENT) {
2869 GOTO(out_put_old, rc);
2871 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2872 lh_rmt = &info->mti_lh[MDT_LH_RMT];
2873 mdt_lock_reg_init(lh_oldp, LCK_EX);
2874 mdt_lock_reg_init(lh_rmt, LCK_EX);
2875 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2876 rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
2877 lh_rmt, lock_ibits, cos_incompat);
2879 GOTO(out_put_old, rc);
2881 mdt_enoent_version_save(info, 3);
2884 /* step 5: rename it */
2885 mdt_reint_init_ma(info, ma);
2887 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
2888 OBD_FAIL_MDS_REINT_RENAME_WRITE);
2891 mutex_lock(&mnew->mot_lov_mutex);
2893 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
2894 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
2895 mnew != NULL ? mdt_object_child(mnew) : NULL,
2896 &rr->rr_tgt_name, ma);
2899 mutex_unlock(&mnew->mot_lov_mutex);
2901 /* handle last link of tgt object */
2903 mdt_counter_incr(req, LPROC_MDT_RENAME,
2904 ktime_us_delta(ktime_get(), kstart));
2906 mdt_handle_last_unlink(info, mnew, ma);
2907 discard = mdt_dom_check_for_discard(info, mnew);
2909 mdt_rename_counter_tally(info, info->mti_mdt, req,
2911 ktime_us_delta(ktime_get(), kstart));
2917 mdt_object_unlock(info, mnew, lh_newp, rc);
2919 mdt_object_unlock(info, NULL, lh_rmt, rc);
2920 mdt_object_unlock(info, mold, lh_oldp, rc);
2922 if (mnew && !discard)
2923 mdt_object_put(info->mti_env, mnew);
2925 mdt_object_put(info->mti_env, mold);
2927 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2928 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2930 if (lustre_handle_is_used(&rename_lh))
2931 mdt_rename_unlock(&rename_lh);
2933 mdt_object_put(info->mti_env, mtgtdir);
2935 mdt_object_put(info->mti_env, msrcdir);
2937 /* The DoM discard can be done right in the place above where it is
2938 * assigned, meanwhile it is done here after rename unlock due to
2939 * compatibility with old clients, for them the discard blocks
2940 * the main thread until completion. Check LU-11359 for details.
2943 mdt_dom_discard_data(info, mnew);
2944 mdt_object_put(info->mti_env, mnew);
2949 static int mdt_reint_resync(struct mdt_thread_info *info,
2950 struct mdt_lock_handle *lhc)
2952 struct mdt_reint_record *rr = &info->mti_rr;
2953 struct ptlrpc_request *req = mdt_info_req(info);
2954 struct md_attr *ma = &info->mti_attr;
2955 struct mdt_object *mo;
2956 struct ldlm_lock *lease;
2957 struct mdt_body *repbody;
2958 struct md_layout_change layout = { .mlc_mirror_id = rr->rr_mirror_id };
2963 DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
2965 if (info->mti_dlm_req)
2966 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2968 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
2970 GOTO(out, rc = PTR_ERR(mo));
2972 if (!mdt_object_exists(mo))
2973 GOTO(out_obj, rc = -ENOENT);
2975 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
2976 GOTO(out_obj, rc = -EINVAL);
2978 if (mdt_object_remote(mo))
2979 GOTO(out_obj, rc = -EREMOTE);
2981 lease = ldlm_handle2lock(rr->rr_lease_handle);
2983 GOTO(out_obj, rc = -ESTALE);
2985 /* It's really necessary to grab open_sem and check if the lease lock
2986 * has been lost. There would exist a concurrent writer coming in and
2987 * generating some dirty data in memory cache, the writeback would fail
2988 * after the layout version is increased by MDS_REINT_RESYNC RPC.
2990 if (!down_write_trylock(&mo->mot_open_sem))
2991 GOTO(out_put_lease, rc = -EBUSY);
2993 lock_res_and_lock(lease);
2994 lease_broken = ldlm_is_cancel(lease);
2995 unlock_res_and_lock(lease);
2997 GOTO(out_unlock, rc = -EBUSY);
2999 /* the file has yet opened by anyone else after we took the lease. */
3000 layout.mlc_opc = MD_LAYOUT_RESYNC;
3001 lhc = &info->mti_lh[MDT_LH_LOCAL];
3002 rc = mdt_layout_change(info, mo, lhc, &layout);
3004 GOTO(out_unlock, rc);
3006 mdt_object_unlock(info, mo, lhc, 0);
3008 ma->ma_need = MA_INODE;
3010 rc = mdt_attr_get_complex(info, mo, ma);
3012 GOTO(out_unlock, rc);
3014 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3015 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
3019 up_write(&mo->mot_open_sem);
3021 LDLM_LOCK_PUT(lease);
3023 mdt_object_put(info->mti_env, mo);
3025 mdt_client_compatibility(info);
3026 rc2 = mdt_fix_reply(info);
3032 struct mdt_reinter {
3033 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
3034 enum lprocfs_extra_opc mr_extra_opc;
3037 static const struct mdt_reinter mdt_reinters[] = {
3039 .mr_handler = &mdt_reint_setattr,
3040 .mr_extra_opc = MDS_REINT_SETATTR,
3043 .mr_handler = &mdt_reint_create,
3044 .mr_extra_opc = MDS_REINT_CREATE,
3047 .mr_handler = &mdt_reint_link,
3048 .mr_extra_opc = MDS_REINT_LINK,
3051 .mr_handler = &mdt_reint_unlink,
3052 .mr_extra_opc = MDS_REINT_UNLINK,
3055 .mr_handler = &mdt_reint_rename,
3056 .mr_extra_opc = MDS_REINT_RENAME,
3059 .mr_handler = &mdt_reint_open,
3060 .mr_extra_opc = MDS_REINT_OPEN,
3062 [REINT_SETXATTR] = {
3063 .mr_handler = &mdt_reint_setxattr,
3064 .mr_extra_opc = MDS_REINT_SETXATTR,
3067 .mr_handler = &mdt_reint_unlink,
3068 .mr_extra_opc = MDS_REINT_UNLINK,
3071 .mr_handler = &mdt_reint_migrate,
3072 .mr_extra_opc = MDS_REINT_RENAME,
3075 .mr_handler = &mdt_reint_resync,
3076 .mr_extra_opc = MDS_REINT_RESYNC,
3080 int mdt_reint_rec(struct mdt_thread_info *info,
3081 struct mdt_lock_handle *lhc)
3083 const struct mdt_reinter *mr;
3087 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
3090 mr = &mdt_reinters[info->mti_rr.rr_opcode];
3091 if (mr->mr_handler == NULL)
3094 rc = (*mr->mr_handler)(info, lhc);
3096 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
3097 PTLRPC_LAST_CNTR + mr->mr_extra_opc);