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>
47 #include <lustre_crypto.h>
49 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
52 ma->ma_need = MA_INODE;
57 * Get version of object by fid.
59 * Return real version or ENOENT_VERSION if object doesn't exist
61 static void mdt_obj_version_get(struct mdt_thread_info *info,
62 struct mdt_object *o, __u64 *version)
66 if (mdt_object_exists(o) && !mdt_object_remote(o) &&
67 !fid_is_obf(mdt_object_fid(o)))
68 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
70 *version = ENOENT_VERSION;
71 CDEBUG(D_INODE, "FID "DFID" version is %#llx\n",
72 PFID(mdt_object_fid(o)), *version);
76 * Check version is correct.
78 * Should be called only during replay.
80 static int mdt_version_check(struct ptlrpc_request *req,
81 __u64 version, int idx)
83 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
86 if (!exp_connect_vbr(req->rq_export))
89 LASSERT(req_is_replay(req));
90 /** VBR: version is checked always because costs nothing */
91 LASSERT(idx < PTLRPC_NUM_VERSIONS);
92 /** Sanity check for malformed buffers */
93 if (pre_ver == NULL) {
94 CERROR("No versions in request buffer\n");
95 spin_lock(&req->rq_export->exp_lock);
96 req->rq_export->exp_vbr_failed = 1;
97 spin_unlock(&req->rq_export->exp_lock);
99 } else if (pre_ver[idx] != version) {
100 CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
101 pre_ver[idx], version);
102 spin_lock(&req->rq_export->exp_lock);
103 req->rq_export->exp_vbr_failed = 1;
104 spin_unlock(&req->rq_export->exp_lock);
111 * Save pre-versions in reply.
113 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
118 if (!exp_connect_vbr(req->rq_export))
121 LASSERT(!req_is_replay(req));
122 LASSERT(req->rq_repmsg != NULL);
123 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
125 reply_ver[idx] = version;
129 * Save enoent version, it is needed when it is obvious that object doesn't
130 * exist, e.g. child during create.
132 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
134 /* save version of file name for replay, it must be ENOENT here */
135 if (!req_is_replay(mdt_info_req(info))) {
136 info->mti_ver[idx] = ENOENT_VERSION;
137 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
142 * Get version from disk and save in reply buffer.
144 * Versions are saved in reply only during normal operations not replays.
146 void mdt_version_get_save(struct mdt_thread_info *info,
147 struct mdt_object *mto, int idx)
149 /* don't save versions during replay */
150 if (!req_is_replay(mdt_info_req(info))) {
151 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
152 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
157 * Get version from disk and check it, no save in reply.
159 int mdt_version_get_check(struct mdt_thread_info *info,
160 struct mdt_object *mto, int idx)
162 /* only check versions during replay */
163 if (!req_is_replay(mdt_info_req(info)))
166 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
167 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
171 * Get version from disk and check if recovery or just save.
173 int mdt_version_get_check_save(struct mdt_thread_info *info,
174 struct mdt_object *mto, int idx)
178 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
179 if (req_is_replay(mdt_info_req(info)))
180 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
183 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
188 * Lookup with version checking.
190 * This checks version of 'name'. Many reint functions uses 'name' for child not
191 * FID, therefore we need to get object by name and check its version.
193 int mdt_lookup_version_check(struct mdt_thread_info *info,
194 struct mdt_object *p,
195 const struct lu_name *lname,
196 struct lu_fid *fid, int idx)
200 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
202 /* Check version only during replay */
203 if (!req_is_replay(mdt_info_req(info)))
206 info->mti_ver[idx] = ENOENT_VERSION;
208 struct mdt_object *child;
210 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
211 if (likely(!IS_ERR(child))) {
212 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
213 mdt_object_put(info->mti_env, child);
216 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
217 return vbrc ? vbrc : rc;
221 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
222 struct mdt_object *obj,
223 struct ldlm_enqueue_info *einfo,
226 union ldlm_policy_data *policy = &mti->mti_policy;
227 struct mdt_lock_handle *lh = &mti->mti_lh[MDT_LH_LOCAL];
228 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
231 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
232 LASSERT(slave_locks);
234 memset(policy, 0, sizeof(*policy));
235 policy->l_inodebits.bits = einfo->ei_inodebits;
236 mdt_lock_handle_init(lh);
237 mdt_lock_reg_init(lh, einfo->ei_mode);
238 for (i = 0; i < slave_locks->ha_count; i++) {
239 if (test_bit(i, (void *)slave_locks->ha_map))
240 lh->mlh_rreg_lh = slave_locks->ha_handles[i];
242 lh->mlh_reg_lh = slave_locks->ha_handles[i];
243 mdt_object_unlock(mti, NULL, lh, decref);
244 slave_locks->ha_handles[i].cookie = 0ull;
247 return mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
251 static inline int mdt_object_striped(struct mdt_thread_info *mti,
252 struct mdt_object *obj)
254 struct lu_device *bottom_dev;
255 struct lu_object *bottom_obj;
258 if (!S_ISDIR(obj->mot_header.loh_attr))
261 /* getxattr from bottom obj to avoid reading in shard FIDs */
262 bottom_dev = dt2lu_dev(mti->mti_mdt->mdt_bottom);
263 bottom_obj = lu_object_find_slice(mti->mti_env, bottom_dev,
264 mdt_object_fid(obj), NULL);
265 if (IS_ERR(bottom_obj))
266 return PTR_ERR(bottom_obj);
268 rc = dt_xattr_get(mti->mti_env, lu2dt(bottom_obj), &LU_BUF_NULL,
270 lu_object_put(mti->mti_env, bottom_obj);
272 return (rc > 0) ? 1 : (rc == -ENODATA) ? 0 : rc;
276 * Lock slave stripes if necessary, the lock handles of slave stripes
277 * will be stored in einfo->ei_cbdata.
279 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
280 enum ldlm_mode mode, __u64 ibits,
281 struct ldlm_enqueue_info *einfo)
283 union ldlm_policy_data *policy = &mti->mti_policy;
285 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
287 einfo->ei_type = LDLM_IBITS;
288 einfo->ei_mode = mode;
289 einfo->ei_cb_bl = mdt_remote_blocking_ast;
290 einfo->ei_cb_local_bl = mdt_blocking_ast;
291 einfo->ei_cb_cp = ldlm_completion_ast;
292 einfo->ei_enq_slave = 1;
293 einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
294 einfo->ei_inodebits = ibits;
295 einfo->ei_req_slot = 1;
296 memset(policy, 0, sizeof(*policy));
297 policy->l_inodebits.bits = ibits;
299 return mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
303 int mdt_reint_striped_lock(struct mdt_thread_info *info,
304 struct mdt_object *o,
305 struct mdt_lock_handle *lh,
307 struct ldlm_enqueue_info *einfo,
312 LASSERT(!mdt_object_remote(o));
314 memset(einfo, 0, sizeof(*einfo));
316 rc = mdt_reint_object_lock(info, o, lh, ibits, cos_incompat);
320 rc = mdt_object_striped(info, o);
323 mdt_object_unlock(info, o, lh, rc);
327 rc = mdt_lock_slaves(info, o, lh->mlh_reg_mode, ibits, einfo);
329 mdt_object_unlock(info, o, lh, rc);
330 if (rc == -EIO && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME))
337 void mdt_reint_striped_unlock(struct mdt_thread_info *info,
338 struct mdt_object *o,
339 struct mdt_lock_handle *lh,
340 struct ldlm_enqueue_info *einfo, int decref)
342 if (einfo->ei_cbdata)
343 mdt_unlock_slaves(info, o, einfo, decref);
344 mdt_object_unlock(info, o, lh, decref);
347 static int mdt_restripe(struct mdt_thread_info *info,
348 struct mdt_object *parent,
349 const struct lu_name *lname,
350 const struct lu_fid *tfid,
351 struct md_op_spec *spec,
354 struct mdt_device *mdt = info->mti_mdt;
355 struct lu_fid *fid = &info->mti_tmp_fid2;
356 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
357 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
358 struct lmv_mds_md_v1 *lmv;
359 struct mdt_object *child;
360 struct mdt_lock_handle *lhp;
361 struct mdt_lock_handle *lhc;
362 struct mdt_body *repbody;
366 if (!mdt->mdt_enable_dir_restripe)
370 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_FIXED);
372 rc = mdt_version_get_check_save(info, parent, 0);
376 lhp = &info->mti_lh[MDT_LH_PARENT];
377 mdt_lock_pdo_init(lhp, LCK_PW, lname);
378 rc = mdt_reint_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
383 rc = mdt_stripe_get(info, parent, ma, XATTR_NAME_LMV);
385 GOTO(unlock_parent, rc);
387 if (ma->ma_valid & MA_LMV) {
388 /* don't allow restripe if parent dir layout is changing */
389 lmv = &ma->ma_lmv->lmv_md_v1;
390 if (!lmv_is_sane2(lmv))
391 GOTO(unlock_parent, rc = -EBADF);
393 if (lmv_is_layout_changing(lmv))
394 GOTO(unlock_parent, rc = -EBUSY);
398 rc = mdt_lookup_version_check(info, parent, lname, fid, 1);
400 GOTO(unlock_parent, rc);
402 child = mdt_object_find(info->mti_env, mdt, fid);
404 GOTO(unlock_parent, rc = PTR_ERR(child));
406 if (!mdt_object_exists(child))
407 GOTO(out_child, rc = -ENOENT);
409 if (mdt_object_remote(child)) {
410 struct mdt_body *repbody;
412 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
414 GOTO(out_child, rc = -EPROTO);
416 repbody->mbo_fid1 = *fid;
417 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
418 GOTO(out_child, rc = -EREMOTE);
421 if (!S_ISDIR(lu_object_attr(&child->mot_obj)))
422 GOTO(out_child, rc = -ENOTDIR);
424 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
428 /* race with migrate? */
429 if ((ma->ma_valid & MA_LMV) &&
430 lmv_is_migrating(&ma->ma_lmv->lmv_md_v1))
431 GOTO(out_child, rc = -EBUSY);
434 lhc = &info->mti_lh[MDT_LH_CHILD];
435 mdt_lock_reg_init(lhc, LCK_EX);
437 /* enqueue object remote LOOKUP lock */
438 if (mdt_object_remote(parent)) {
439 rc = mdt_remote_object_lock(info, parent, fid,
442 MDS_INODELOCK_LOOKUP, false);
447 rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
450 GOTO(unlock_child, rc);
452 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
453 rc = mdt_version_get_check_save(info, child, 1);
455 GOTO(unlock_child, rc);
457 spin_lock(&mdt->mdt_restriper.mdr_lock);
458 if (child->mot_restriping) {
460 spin_unlock(&mdt->mdt_restriper.mdr_lock);
461 GOTO(unlock_child, rc = -EBUSY);
463 child->mot_restriping = 1;
464 spin_unlock(&mdt->mdt_restriper.mdr_lock);
467 rc = mdt_restripe_internal(info, parent, child, lname, fid, spec, ma);
469 GOTO(restriping_clear, rc);
471 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
473 GOTO(restriping_clear, rc = -EPROTO);
475 mdt_pack_attr2body(info, repbody, &ma->ma_attr, fid);
479 child->mot_restriping = 0;
481 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
483 mdt_object_put(info->mti_env, child);
485 mdt_object_unlock(info, parent, lhp, rc);
491 * VBR: we save three versions in reply:
492 * 0 - parent. Check that parent version is the same during replay.
493 * 1 - name. Version of 'name' if file exists with the same name or
494 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
495 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
498 static int mdt_create(struct mdt_thread_info *info)
500 struct mdt_device *mdt = info->mti_mdt;
501 struct mdt_object *parent;
502 struct mdt_object *child;
503 struct mdt_lock_handle *lh;
504 struct mdt_body *repbody;
505 struct md_attr *ma = &info->mti_attr;
506 struct mdt_reint_record *rr = &info->mti_rr;
507 struct md_op_spec *spec = &info->mti_spec;
508 bool restripe = false;
512 DEBUG_REQ(D_INODE, mdt_info_req(info),
513 "Create ("DNAME"->"DFID") in "DFID,
514 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
516 if (!fid_is_md_operative(rr->rr_fid1))
519 if (S_ISDIR(ma->ma_attr.la_mode) &&
520 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
521 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
522 struct lu_ucred *uc = mdt_ucred(info);
523 struct obd_export *exp = mdt_info_req(info)->rq_export;
525 /* Only new clients can create remote dir( >= 2.4) and
526 * striped dir(>= 2.6), old client will return -ENOTSUPP
528 if (!mdt_is_dne_client(exp))
531 if (le32_to_cpu(lum->lum_stripe_count) > 1) {
532 if (!mdt_is_striped_client(exp))
535 if (!mdt->mdt_enable_striped_dir)
537 } else if (!mdt->mdt_enable_remote_dir) {
541 if ((!(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH)) &&
542 (le32_to_cpu(lum->lum_hash_type) & LMV_HASH_TYPE_MASK) ==
546 if (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
547 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
548 mdt->mdt_enable_remote_dir_gid != -1)
551 /* restripe if later found dir exists, MDS_OPEN_CREAT means
552 * this is create only, don't try restripe.
554 if (mdt->mdt_enable_dir_restripe &&
555 le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT &&
556 !(spec->sp_cr_flags & MDS_OPEN_CREAT))
560 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
562 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
564 RETURN(PTR_ERR(parent));
566 if (!mdt_object_exists(parent))
567 GOTO(put_parent, rc = -ENOENT);
570 * LU-10235: check if name exists locklessly first to avoid massive
571 * lock recalls on existing directories.
573 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
574 &info->mti_tmp_fid1, 1);
577 GOTO(put_parent, rc = -EEXIST);
579 rc = mdt_restripe(info, parent, &rr->rr_name, rr->rr_fid2, spec,
583 /* -ENOENT is expected here */
585 GOTO(put_parent, rc);
587 /* save version of file name for replay, it must be ENOENT here */
588 mdt_enoent_version_save(info, 1);
590 OBD_RACE(OBD_FAIL_MDS_CREATE_RACE);
592 lh = &info->mti_lh[MDT_LH_PARENT];
593 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
594 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
596 GOTO(put_parent, rc);
598 if (!mdt_object_remote(parent)) {
599 rc = mdt_version_get_check_save(info, parent, 0);
601 GOTO(unlock_parent, rc);
604 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
605 if (unlikely(IS_ERR(child)))
606 GOTO(unlock_parent, rc = PTR_ERR(child));
608 ma->ma_need = MA_INODE;
611 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
612 OBD_FAIL_MDS_REINT_CREATE_WRITE);
614 /* Version of child will be updated on disk. */
615 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
616 rc = mdt_version_get_check_save(info, child, 2);
621 * Do not perform lookup sanity check. We know that name does
624 info->mti_spec.sp_cr_lookup = 0;
625 info->mti_spec.sp_feat = &dt_directory_features;
627 rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
628 mdt_object_child(child), &info->mti_spec, ma);
630 rc = mdt_attr_get_complex(info, child, ma);
636 * On DNE, we need to eliminate dependey between 'mkdir a' and
637 * 'mkdir a/b' if b is a striped directory, to achieve this, two
638 * things are done below:
639 * 1. save child and slaves lock.
640 * 2. if the child is a striped directory, relock parent so to
641 * compare against with COS locks to ensure parent was
644 if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
645 struct mdt_lock_handle *lhc;
646 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
649 rc = mdt_object_striped(info, child);
655 if (!mdt_object_remote(parent)) {
656 mdt_object_unlock(info, parent, lh, 1);
657 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
658 rc = mdt_reint_object_lock(info, parent, lh,
659 MDS_INODELOCK_UPDATE,
666 lhc = &info->mti_lh[MDT_LH_CHILD];
667 mdt_lock_handle_init(lhc);
668 mdt_lock_reg_init(lhc, LCK_PW);
669 rc = mdt_reint_striped_lock(info, child, lhc,
670 MDS_INODELOCK_UPDATE, einfo,
675 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
678 /* Return fid & attr to client. */
679 if (ma->ma_valid & MA_INODE)
680 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
681 mdt_object_fid(child));
684 mdt_object_put(info->mti_env, child);
686 mdt_object_unlock(info, parent, lh, rc);
688 mdt_object_put(info->mti_env, parent);
692 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
695 struct mdt_lock_handle *lh;
696 int do_vbr = ma->ma_attr.la_valid &
697 (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
698 __u64 lockpart = MDS_INODELOCK_UPDATE;
699 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
704 rc = mdt_object_striped(info, mo);
710 lh = &info->mti_lh[MDT_LH_PARENT];
711 mdt_lock_reg_init(lh, LCK_PW);
713 /* Even though the new MDT will grant PERM lock to the old
714 * client, but the old client will almost ignore that during
715 * So it needs to revoke both LOOKUP and PERM lock here, so
716 * both new and old client can cancel the dcache
718 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
719 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
720 /* Clear xattr cache on clients, so the virtual project ID xattr
721 * can get the new project ID
723 if (ma->ma_attr.la_valid & LA_PROJID)
724 lockpart |= MDS_INODELOCK_XATTR;
726 rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
731 /* all attrs are packed into mti_attr in unpack_setattr */
732 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
733 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
735 /* VBR: update version if attr changed are important for recovery */
737 /* update on-disk version of changed object */
738 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
739 rc = mdt_version_get_check_save(info, mo, 0);
741 GOTO(out_unlock, rc);
744 /* Ensure constant striping during chown(). See LU-2789. */
745 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
746 mutex_lock(&mo->mot_lov_mutex);
748 /* all attrs are packed into mti_attr in unpack_setattr */
749 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
751 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
752 mutex_unlock(&mo->mot_lov_mutex);
755 GOTO(out_unlock, rc);
756 mdt_dom_obj_lvb_update(info->mti_env, mo, NULL, false);
759 mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
764 * Check HSM flags and add HS_DIRTY flag if relevant.
766 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
767 * and is not RELEASED.
769 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
772 struct lu_ucred *uc = mdt_ucred(info);
773 kernel_cap_t cap_saved;
777 /* If the file was modified, add the dirty flag */
778 ma->ma_need = MA_HSM;
779 rc = mdt_attr_get_complex(info, mo, ma);
781 CERROR("file attribute read error for "DFID": %d.\n",
782 PFID(mdt_object_fid(mo)), rc);
786 /* If an up2date copy exists in the backend, add dirty flag */
787 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
788 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
789 ma->ma_hsm.mh_flags |= HS_DIRTY;
791 /* Bump cap so that closes from non-owner writers can
792 * set the HSM state to dirty.
794 cap_saved = uc->uc_cap;
795 cap_raise(uc->uc_cap, CAP_FOWNER);
796 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
797 uc->uc_cap = cap_saved;
799 CERROR("file attribute change error for "DFID": %d\n",
800 PFID(mdt_object_fid(mo)), rc);
806 static int mdt_reint_setattr(struct mdt_thread_info *info,
807 struct mdt_lock_handle *lhc)
809 struct mdt_device *mdt = info->mti_mdt;
810 struct md_attr *ma = &info->mti_attr;
811 struct mdt_reint_record *rr = &info->mti_rr;
812 struct ptlrpc_request *req = mdt_info_req(info);
813 struct mdt_object *mo;
814 struct mdt_body *repbody;
815 ktime_t kstart = ktime_get();
819 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
820 (unsigned int)ma->ma_attr.la_valid);
822 if (info->mti_dlm_req)
823 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
825 OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
827 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
828 mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
830 GOTO(out, rc = PTR_ERR(mo));
832 if (!mdt_object_exists(mo))
833 GOTO(out_put, rc = -ENOENT);
835 if (mdt_object_remote(mo))
836 GOTO(out_put, rc = -EREMOTE);
838 ma->ma_enable_chprojid_gid = mdt->mdt_enable_chprojid_gid;
839 /* revoke lease lock if size is going to be changed */
840 if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
841 !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
842 atomic_read(&mo->mot_lease_count) > 0)) {
843 down_read(&mo->mot_open_sem);
845 if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
846 lhc = &info->mti_lh[MDT_LH_LOCAL];
847 mdt_lock_reg_init(lhc, LCK_CW);
849 rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
851 up_read(&mo->mot_open_sem);
855 /* revoke lease lock */
856 mdt_object_unlock(info, mo, lhc, 1);
858 up_read(&mo->mot_open_sem);
861 if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
862 /* Check write access for the O_TRUNC case */
863 if (mdt_write_read(mo) < 0)
864 GOTO(out_put, rc = -ETXTBSY);
866 /* LU-10286: compatibility check for FLR.
867 * Please check the comment in mdt_finish_open() for details
869 if (!exp_connect_flr(info->mti_exp) ||
870 !exp_connect_overstriping(info->mti_exp)) {
871 rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
872 if (rc < 0 && rc != -ENODATA)
875 if (!exp_connect_flr(info->mti_exp)) {
877 mdt_lmm_is_flr(info->mti_big_lmm))
878 GOTO(out_put, rc = -EOPNOTSUPP);
881 if (!exp_connect_overstriping(info->mti_exp)) {
883 mdt_lmm_is_overstriping(info->mti_big_lmm))
884 GOTO(out_put, rc = -EOPNOTSUPP);
888 /* For truncate, the file size sent from client
889 * is believable, but the blocks are incorrect,
890 * which makes the block size in LSOM attribute
891 * inconsisent with the real block size.
893 rc = mdt_lsom_update(info, mo, true);
898 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
899 if (ma->ma_valid & MA_LOV)
900 GOTO(out_put, rc = -EPROTO);
902 /* MDT supports FMD for regular files due to Data-on-MDT */
903 if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
904 ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
905 tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
908 if (ma->ma_attr.la_valid & LA_MTIME) {
909 rc = mdt_attr_get_pfid(info, mo, &ma->ma_pfid);
911 ma->ma_valid |= MA_PFID;
915 rc = mdt_attr_set(info, mo, ma);
918 } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
919 (ma->ma_valid & MA_INODE)) {
920 struct lu_buf *buf = &info->mti_buf;
921 struct lu_ucred *uc = mdt_ucred(info);
922 struct mdt_lock_handle *lh;
924 __u64 lockpart = MDS_INODELOCK_XATTR;
926 /* reject if either remote or striped dir is disabled */
927 if (ma->ma_valid & MA_LMV) {
928 if (!mdt->mdt_enable_remote_dir ||
929 !mdt->mdt_enable_striped_dir)
930 GOTO(out_put, rc = -EPERM);
932 if (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
933 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
934 mdt->mdt_enable_remote_dir_gid != -1)
935 GOTO(out_put, rc = -EPERM);
938 if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
939 GOTO(out_put, rc = -ENOTDIR);
941 if (ma->ma_attr.la_valid != 0)
942 GOTO(out_put, rc = -EPROTO);
944 lh = &info->mti_lh[MDT_LH_PARENT];
945 mdt_lock_reg_init(lh, LCK_PW);
947 if (ma->ma_valid & MA_LOV) {
948 buf->lb_buf = ma->ma_lmm;
949 buf->lb_len = ma->ma_lmm_size;
950 name = XATTR_NAME_LOV;
952 struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
953 struct lu_fid *pfid = &info->mti_tmp_fid1;
954 struct lu_name *pname = &info->mti_name;
955 const char dotdot[] = "..";
956 struct mdt_object *pobj;
959 buf->lb_len = ma->ma_lmv_size;
960 name = XATTR_NAME_DEFAULT_LMV;
962 if (fid_is_root(rr->rr_fid1)) {
963 lockpart |= MDS_INODELOCK_LOOKUP;
965 /* force client to update dir default layout */
967 pname->ln_name = dotdot;
968 pname->ln_namelen = sizeof(dotdot);
969 rc = mdo_lookup(info->mti_env,
970 mdt_object_child(mo), pname,
975 pobj = mdt_object_find(info->mti_env, mdt,
978 GOTO(out_put, rc = PTR_ERR(pobj));
980 if (mdt_object_remote(pobj))
981 rc = mdt_remote_object_lock(info, pobj,
983 &lh->mlh_rreg_lh, LCK_EX,
984 MDS_INODELOCK_LOOKUP, false);
986 lockpart |= MDS_INODELOCK_LOOKUP;
988 mdt_object_put(info->mti_env, pobj);
995 rc = mdt_object_lock(info, mo, lh, lockpart);
999 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
1002 mdt_object_unlock(info, mo, lh, rc);
1006 GOTO(out_put, rc = -EPROTO);
1009 /* If file data is modified, add the dirty flag */
1010 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
1011 rc = mdt_add_dirty_flag(info, mo, ma);
1013 ma->ma_need = MA_INODE;
1015 rc = mdt_attr_get_complex(info, mo, ma);
1019 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
1023 mdt_object_put(info->mti_env, mo);
1026 mdt_counter_incr(req, LPROC_MDT_SETATTR,
1027 ktime_us_delta(ktime_get(), kstart));
1029 mdt_client_compatibility(info);
1030 rc2 = mdt_fix_reply(info);
1036 static int mdt_reint_create(struct mdt_thread_info *info,
1037 struct mdt_lock_handle *lhc)
1039 struct ptlrpc_request *req = mdt_info_req(info);
1040 ktime_t kstart = ktime_get();
1044 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
1045 RETURN(err_serious(-ESTALE));
1047 if (info->mti_dlm_req)
1048 ldlm_request_cancel(mdt_info_req(info),
1049 info->mti_dlm_req, 0, LATF_SKIP);
1051 if (!lu_name_is_valid(&info->mti_rr.rr_name))
1054 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
1064 CERROR("%s: Unsupported mode %o\n",
1065 mdt_obd_name(info->mti_mdt),
1066 info->mti_attr.ma_attr.la_mode);
1067 RETURN(err_serious(-EOPNOTSUPP));
1070 rc = mdt_create(info);
1072 if ((info->mti_attr.ma_attr.la_mode & S_IFMT) == S_IFDIR)
1073 mdt_counter_incr(req, LPROC_MDT_MKDIR,
1074 ktime_us_delta(ktime_get(), kstart));
1076 /* Special file should stay on the same node as parent*/
1077 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1078 ktime_us_delta(ktime_get(), kstart));
1085 * VBR: save parent version in reply and child version getting by its name.
1086 * Version of child is getting and checking during its lookup. If
1088 static int mdt_reint_unlink(struct mdt_thread_info *info,
1089 struct mdt_lock_handle *lhc)
1091 struct mdt_reint_record *rr = &info->mti_rr;
1092 struct ptlrpc_request *req = mdt_info_req(info);
1093 struct md_attr *ma = &info->mti_attr;
1094 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1095 struct mdt_object *mp;
1096 struct mdt_object *mc;
1097 struct mdt_lock_handle *parent_lh;
1098 struct mdt_lock_handle *child_lh;
1099 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
1101 bool cos_incompat = false;
1103 ktime_t kstart = ktime_get();
1107 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
1108 PNAME(&rr->rr_name));
1110 if (info->mti_dlm_req)
1111 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1113 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1114 RETURN(err_serious(-ENOENT));
1116 if (!fid_is_md_operative(rr->rr_fid1))
1119 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1121 RETURN(PTR_ERR(mp));
1123 if (mdt_object_remote(mp)) {
1124 cos_incompat = true;
1126 rc = mdt_version_get_check_save(info, mp, 0);
1128 GOTO(put_parent, rc);
1131 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1132 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1134 parent_lh = &info->mti_lh[MDT_LH_PARENT];
1135 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
1136 rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
1139 GOTO(put_parent, rc);
1141 if (info->mti_spec.sp_cr_flags & MDS_OP_WITH_FID) {
1142 *child_fid = *rr->rr_fid2;
1144 /* lookup child object along with version checking */
1145 fid_zero(child_fid);
1146 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid,
1149 /* Name might not be able to find during resend of
1150 * remote unlink, considering following case.
1151 * dir_A is a remote directory, the name entry of
1152 * dir_A is on MDT0, the directory is on MDT1,
1154 * 1. client sends unlink req to MDT1.
1155 * 2. MDT1 sends name delete update to MDT0.
1156 * 3. name entry is being deleted in MDT0 synchronously.
1157 * 4. MDT1 is restarted.
1158 * 5. client resends unlink req to MDT1. So it can not
1159 * find the name entry on MDT0 anymore.
1160 * In this case, MDT1 only needs to destory the local
1163 if (mdt_object_remote(mp) && rc == -ENOENT &&
1164 !fid_is_zero(rr->rr_fid2) &&
1165 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1167 *child_fid = *rr->rr_fid2;
1169 GOTO(unlock_parent, rc);
1174 if (!fid_is_md_operative(child_fid))
1175 GOTO(unlock_parent, rc = -EPERM);
1177 /* We will lock the child regardless it is local or remote. No harm. */
1178 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1180 GOTO(unlock_parent, rc = PTR_ERR(mc));
1182 if (info->mti_spec.sp_cr_flags & MDS_OP_WITH_FID) {
1183 /* In this case, child fid is embedded in the request, and we do
1184 * not have a proper name as rr_name contains an encoded
1185 * hash. So find name that matches provided hash.
1187 if (!find_name_matching_hash(info, &rr->rr_name,
1189 GOTO(put_child, rc = -ENOENT);
1192 if (!cos_incompat) {
1193 rc = mdt_object_striped(info, mc);
1195 GOTO(put_child, rc);
1199 mdt_object_put(info->mti_env, mc);
1200 mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
1205 child_lh = &info->mti_lh[MDT_LH_CHILD];
1206 mdt_lock_reg_init(child_lh, LCK_EX);
1207 if (info->mti_spec.sp_rm_entry) {
1208 struct lu_ucred *uc = mdt_ucred(info);
1210 if (!mdt_is_dne_client(req->rq_export))
1211 /* Return -ENOTSUPP for old client */
1212 GOTO(put_child, rc = -ENOTSUPP);
1214 if (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN))
1215 GOTO(put_child, rc = -EPERM);
1217 ma->ma_need = MA_INODE;
1219 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1220 NULL, &rr->rr_name, ma, no_name);
1221 GOTO(put_child, rc);
1224 if (mdt_object_remote(mc)) {
1225 struct mdt_body *repbody;
1227 if (!fid_is_zero(rr->rr_fid2)) {
1228 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
1229 mdt_obd_name(info->mti_mdt),
1230 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1231 GOTO(put_child, rc = -ENOENT);
1233 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
1234 mdt_obd_name(info->mti_mdt),
1235 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1237 if (!mdt_is_dne_client(req->rq_export))
1238 /* Return -ENOTSUPP for old client */
1239 GOTO(put_child, rc = -ENOTSUPP);
1241 /* Revoke the LOOKUP lock of the remote object granted by
1242 * this MDT. Since the unlink will happen on another MDT,
1243 * it will release the LOOKUP lock right away. Then What
1244 * would happen if another client try to grab the LOOKUP
1245 * lock at the same time with unlink XXX
1247 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1248 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1249 LASSERT(repbody != NULL);
1250 repbody->mbo_fid1 = *mdt_object_fid(mc);
1251 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1252 GOTO(unlock_child, rc = -EREMOTE);
1254 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1255 * this now because a running HSM restore on the child (unlink
1256 * victim) will hold the layout lock. See LU-4002.
1258 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1259 if (mdt_object_remote(mp)) {
1260 /* Enqueue lookup lock from parent MDT */
1261 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1262 &child_lh->mlh_rreg_lh,
1263 child_lh->mlh_rreg_mode,
1264 MDS_INODELOCK_LOOKUP, false);
1266 GOTO(put_child, rc);
1268 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1271 rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
1274 GOTO(put_child, rc);
1277 * Now we can only make sure we need MA_INODE, in mdd layer, will check
1278 * whether need MA_LOV and MA_COOKIE.
1280 ma->ma_need = MA_INODE;
1283 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1284 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1285 /* save version when object is locked */
1286 mdt_version_get_save(info, mc, 1);
1288 mutex_lock(&mc->mot_lov_mutex);
1290 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1291 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1293 mutex_unlock(&mc->mot_lov_mutex);
1295 GOTO(unlock_child, rc);
1297 if (!lu_object_is_dying(&mc->mot_header)) {
1298 rc = mdt_attr_get_complex(info, mc, ma);
1301 } else if (mdt_dom_check_for_discard(info, mc)) {
1302 mdt_dom_discard_data(info, mc);
1304 mdt_handle_last_unlink(info, mc, ma);
1307 if (ma->ma_valid & MA_INODE) {
1308 switch (ma->ma_attr.la_mode & S_IFMT) {
1310 mdt_counter_incr(req, LPROC_MDT_RMDIR,
1311 ktime_us_delta(ktime_get(), kstart));
1319 mdt_counter_incr(req, LPROC_MDT_UNLINK,
1320 ktime_us_delta(ktime_get(), kstart));
1323 LASSERTF(0, "bad file type %o unlinking\n",
1324 ma->ma_attr.la_mode);
1331 mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
1333 if (info->mti_spec.sp_cr_flags & MDS_OP_WITH_FID &&
1334 info->mti_big_buf.lb_buf)
1335 lu_buf_free(&info->mti_big_buf);
1336 mdt_object_put(info->mti_env, mc);
1338 mdt_object_unlock(info, mp, parent_lh, rc);
1340 mdt_object_put(info->mti_env, mp);
1341 CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
1346 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1349 static int mdt_reint_link(struct mdt_thread_info *info,
1350 struct mdt_lock_handle *lhc)
1352 struct mdt_reint_record *rr = &info->mti_rr;
1353 struct ptlrpc_request *req = mdt_info_req(info);
1354 struct md_attr *ma = &info->mti_attr;
1355 struct mdt_object *ms;
1356 struct mdt_object *mp;
1357 struct mdt_lock_handle *lhs;
1358 struct mdt_lock_handle *lhp;
1359 ktime_t kstart = ktime_get();
1364 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1365 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1367 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1368 RETURN(err_serious(-ENOENT));
1370 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE) ||
1371 OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND)) {
1372 req->rq_no_reply = 1;
1373 RETURN(err_serious(-ENOENT));
1376 if (info->mti_dlm_req)
1377 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1379 /* Invalid case so return error immediately instead of
1382 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1385 if (!fid_is_md_operative(rr->rr_fid1) ||
1386 !fid_is_md_operative(rr->rr_fid2))
1389 /* step 1: find target parent dir */
1390 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1392 RETURN(PTR_ERR(mp));
1394 rc = mdt_version_get_check_save(info, mp, 0);
1396 GOTO(put_parent, rc);
1398 /* step 2: find source */
1399 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1401 GOTO(put_parent, rc = PTR_ERR(ms));
1403 if (!mdt_object_exists(ms)) {
1404 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1405 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1406 GOTO(put_source, rc = -ENOENT);
1409 cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1411 OBD_RACE(OBD_FAIL_MDS_LINK_RENAME_RACE);
1413 lhp = &info->mti_lh[MDT_LH_PARENT];
1414 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1415 rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1418 GOTO(put_source, rc);
1420 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1422 lhs = &info->mti_lh[MDT_LH_CHILD];
1423 mdt_lock_reg_init(lhs, LCK_EX);
1424 rc = mdt_reint_object_lock(info, ms, lhs,
1425 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1428 GOTO(unlock_parent, rc);
1430 /* step 3: link it */
1431 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1432 OBD_FAIL_MDS_REINT_LINK_WRITE);
1434 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1435 rc = mdt_version_get_check_save(info, ms, 1);
1437 GOTO(unlock_source, rc);
1439 /** check target version by name during replay */
1440 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1441 &info->mti_tmp_fid1, 2);
1442 if (rc != 0 && rc != -ENOENT)
1443 GOTO(unlock_source, rc);
1444 /* save version of file name for replay, it must be ENOENT here */
1445 if (!req_is_replay(mdt_info_req(info))) {
1446 if (rc != -ENOENT) {
1447 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1448 PNAME(&rr->rr_name));
1449 GOTO(unlock_source, rc = -EEXIST);
1451 info->mti_ver[2] = ENOENT_VERSION;
1452 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1455 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1456 mdt_object_child(ms), &rr->rr_name, ma);
1459 mdt_counter_incr(req, LPROC_MDT_LINK,
1460 ktime_us_delta(ktime_get(), kstart));
1464 mdt_object_unlock(info, ms, lhs, rc);
1466 mdt_object_unlock(info, mp, lhp, rc);
1468 mdt_object_put(info->mti_env, ms);
1470 mdt_object_put(info->mti_env, mp);
1474 * lock the part of the directory according to the hash of the name
1475 * (lh->mlh_pdo_hash) in parallel directory lock.
1477 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1478 struct mdt_lock_handle *lh,
1479 struct mdt_object *obj, __u64 ibits,
1482 struct ldlm_res_id *res = &info->mti_res_id;
1483 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1484 union ldlm_policy_data *policy = &info->mti_policy;
1485 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1489 * Finish res_id initializing by name hash marking part of
1490 * directory which is taking modification.
1492 LASSERT(lh->mlh_pdo_hash != 0);
1493 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1494 memset(policy, 0, sizeof(*policy));
1495 policy->l_inodebits.bits = ibits;
1497 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1498 dlmflags |= LDLM_FL_COS_INCOMPAT;
1500 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1501 * going to be sent to client. If it is - mdt_intent_policy() path will
1502 * fix it up and turn FL_LOCAL flag off.
1504 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
1505 policy, res, dlmflags,
1506 &info->mti_exp->exp_handle.h_cookie);
1511 * Get BFL lock for rename or migrate process.
1513 static int mdt_rename_lock(struct mdt_thread_info *info,
1514 struct lustre_handle *lh)
1519 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1520 struct lu_fid *fid = &info->mti_tmp_fid1;
1521 struct mdt_object *obj;
1523 /* XXX, right now, it has to use object API to
1524 * enqueue lock cross MDT, so it will enqueue
1525 * rename lock(with LUSTRE_BFL_FID) by root object
1528 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1530 RETURN(PTR_ERR(obj));
1532 rc = mdt_remote_object_lock(info, obj,
1533 &LUSTRE_BFL_FID, lh,
1535 MDS_INODELOCK_UPDATE, false);
1536 mdt_object_put(info->mti_env, obj);
1538 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1539 union ldlm_policy_data *policy = &info->mti_policy;
1540 struct ldlm_res_id *res_id = &info->mti_res_id;
1543 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1544 memset(policy, 0, sizeof(*policy));
1545 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1546 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1547 rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
1548 LDLM_IBITS, policy, LCK_EX, &flags,
1550 ldlm_completion_ast, NULL, NULL, 0,
1552 &info->mti_exp->exp_handle.h_cookie,
1559 static void mdt_rename_unlock(struct lustre_handle *lh)
1562 LASSERT(lustre_handle_is_used(lh));
1563 /* Cancel the single rename lock right away */
1564 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1568 static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
1569 const struct lu_fid *fid,
1572 struct mdt_object *dir;
1576 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1580 /* check early, the real version will be saved after locking */
1581 rc = mdt_version_get_check(info, dir, idx);
1585 if (!mdt_object_exists(dir))
1586 GOTO(out_put, rc = -ENOENT);
1588 if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
1589 GOTO(out_put, rc = -ENOTDIR);
1593 mdt_object_put(info->mti_env, dir);
1598 * in case obj is remote obj on its parent, revoke LOOKUP lock,
1599 * herein we don't really check it, just do revoke.
1601 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
1602 struct mdt_object *pobj,
1603 struct mdt_object *obj)
1605 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1608 mdt_lock_handle_init(lh);
1609 mdt_lock_reg_init(lh, LCK_EX);
1611 if (mdt_object_remote(pobj)) {
1612 /* don't bother to check if pobj and obj are on the same MDT. */
1613 rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
1614 &lh->mlh_rreg_lh, LCK_EX,
1615 MDS_INODELOCK_LOOKUP, false);
1616 } else if (mdt_object_remote(obj)) {
1617 struct ldlm_res_id *res = &info->mti_res_id;
1618 union ldlm_policy_data *policy = &info->mti_policy;
1619 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
1620 LDLM_FL_COS_INCOMPAT;
1622 fid_build_reg_res_name(mdt_object_fid(obj), res);
1623 memset(policy, 0, sizeof(*policy));
1624 policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
1625 rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
1626 &lh->mlh_reg_lh, LCK_EX, policy, res,
1629 /* do nothing if both are local */
1637 * TODO, currently we don't save this lock because there is no place to
1638 * hold this lock handle, but to avoid race we need to save this lock.
1640 mdt_object_unlock(info, NULL, lh, 1);
1646 * operation may takes locks of linkea, or directory stripes, group them in
1649 struct mdt_sub_lock {
1650 struct mdt_object *msl_obj;
1651 struct mdt_lock_handle msl_lh;
1652 struct list_head msl_linkage;
1655 static void mdt_unlock_list(struct mdt_thread_info *info,
1656 struct list_head *list, int decref)
1658 struct mdt_sub_lock *msl;
1659 struct mdt_sub_lock *tmp;
1661 list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
1662 mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
1663 list_del(&msl->msl_linkage);
1668 static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
1669 struct mdt_object *obj,
1670 struct mdt_lock_handle *lh,
1671 struct ldlm_enqueue_info *einfo,
1672 struct list_head *slave_locks,
1675 if (mdt_object_remote(obj)) {
1676 mdt_unlock_list(info, slave_locks, decref);
1677 mdt_object_unlock(info, obj, lh, decref);
1679 mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
1684 * lock parents of links, and also check whether total locks don't exceed
1687 * \retval 0 on success, and locks can be saved in ptlrpc_reply_stat
1688 * \retval 1 on success, but total lock count may exceed RS_MAX_LOCKS
1689 * \retval -ev negative errno upon error
1691 static int mdt_link_parents_lock(struct mdt_thread_info *info,
1692 struct mdt_object *pobj,
1693 const struct md_attr *ma,
1694 struct mdt_object *obj,
1695 struct mdt_lock_handle *lhp,
1696 struct ldlm_enqueue_info *peinfo,
1697 struct list_head *parent_slave_locks,
1698 struct list_head *link_locks)
1700 struct mdt_device *mdt = info->mti_mdt;
1701 struct lu_buf *buf = &info->mti_big_buf;
1702 struct lu_name *lname = &info->mti_name;
1703 struct linkea_data ldata = { NULL };
1704 bool blocked = false;
1705 int local_lnkp_cnt = 0;
1709 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1712 buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
1713 if (buf->lb_buf == NULL)
1717 rc = mdt_links_read(info, obj, &ldata);
1719 if (rc == -ENOENT || rc == -ENODATA)
1724 for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
1725 linkea_next_entry(&ldata)) {
1726 struct mdt_object *lnkp;
1727 struct mdt_sub_lock *msl;
1731 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
1734 /* check if it's also linked to parent */
1735 if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
1736 CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
1737 PFID(&fid), PNAME(lname));
1738 /* in case link is remote object, revoke LOOKUP lock */
1739 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1745 /* check if it's linked to a stripe of parent */
1746 if (ma->ma_valid & MA_LMV) {
1747 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1748 struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
1751 for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
1752 fid_le_to_cpu(stripe_fid,
1753 &lmv->lmv_stripe_fids[j]);
1754 if (lu_fid_eq(stripe_fid, &fid)) {
1755 CDEBUG(D_INFO, "skip stripe "DFID
1756 ", reovke "DNAME"\n",
1757 PFID(&fid), PNAME(lname));
1758 lnkp = mdt_object_find(info->mti_env,
1761 GOTO(out, rc = PTR_ERR(lnkp));
1767 rc = mdt_revoke_remote_lookup_lock(info, lnkp,
1769 mdt_object_put(info->mti_env, lnkp);
1774 /* Check if it's already locked */
1775 list_for_each_entry(msl, link_locks, msl_linkage) {
1776 if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
1778 DFID" was locked, revoke "DNAME"\n",
1779 PFID(&fid), PNAME(lname));
1780 lnkp = msl->msl_obj;
1786 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1790 CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
1791 PFID(&fid), PNAME(lname));
1793 lnkp = mdt_object_find(info->mti_env, mdt, &fid);
1795 CWARN("%s: cannot find obj "DFID": %ld\n",
1796 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
1800 if (!mdt_object_exists(lnkp)) {
1801 CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
1802 PFID(&fid), PNAME(lname));
1803 mdt_object_put(info->mti_env, lnkp);
1807 if (!mdt_object_remote(lnkp))
1812 GOTO(out, rc = -ENOMEM);
1815 * we can't follow parent-child lock order like other MD
1816 * operations, use lock_try here to avoid deadlock, if the lock
1817 * cannot be taken, drop all locks taken, revoke the blocked
1818 * one, and continue processing the remaining entries, and in
1819 * the end of the loop restart from beginning.
1821 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1823 rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
1824 MDS_INODELOCK_UPDATE, true);
1825 if (!(ibits & MDS_INODELOCK_UPDATE)) {
1827 CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
1828 PFID(&fid), PNAME(lname));
1830 mdt_unlock_list(info, link_locks, 1);
1831 /* also unlock parent locks to avoid deadlock */
1833 mdt_migrate_object_unlock(info, pobj, lhp,
1840 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1841 rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
1842 MDS_INODELOCK_UPDATE);
1844 mdt_object_put(info->mti_env, lnkp);
1849 if (mdt_object_remote(lnkp)) {
1850 struct ldlm_lock *lock;
1853 * for remote object, set lock cb_atomic,
1854 * so lock can be released in blocking_ast()
1855 * immediately, then the next lock_try will
1856 * have better chance of success.
1858 lock = ldlm_handle2lock(
1859 &msl->msl_lh.mlh_rreg_lh);
1860 LASSERT(lock != NULL);
1861 lock_res_and_lock(lock);
1862 ldlm_set_atomic_cb(lock);
1863 unlock_res_and_lock(lock);
1864 LDLM_LOCK_PUT(lock);
1867 mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
1872 INIT_LIST_HEAD(&msl->msl_linkage);
1873 msl->msl_obj = lnkp;
1874 list_add_tail(&msl->msl_linkage, link_locks);
1876 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1880 GOTO(out, rc = -EBUSY);
1885 mdt_unlock_list(info, link_locks, rc);
1886 } else if (local_lnkp_cnt > RS_MAX_LOCKS - 5) {
1887 CDEBUG(D_INFO, "Too many links (%d), sync operations\n",
1890 * parent may have 3 local objects: master object and 2 stripes
1891 * (if it's being migrated too); source may have 1 local objects
1892 * as regular file; target has 1 local object.
1893 * Note, source may have 2 local locks if it is directory but it
1894 * can't have hardlinks, so it is not considered here.
1901 static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
1902 struct mdt_object *obj,
1903 const struct md_attr *ma,
1904 struct list_head *slave_locks)
1906 struct mdt_device *mdt = info->mti_mdt;
1907 const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1908 struct lu_fid *fid = &info->mti_tmp_fid1;
1909 struct mdt_object *slave;
1910 struct mdt_sub_lock *msl;
1915 LASSERT(mdt_object_remote(obj));
1916 LASSERT(ma->ma_valid & MA_LMV);
1919 if (!lmv_is_sane(lmv))
1922 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
1923 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
1925 if (!fid_is_sane(fid))
1928 slave = mdt_object_find(info->mti_env, mdt, fid);
1930 GOTO(out, rc = PTR_ERR(slave));
1934 mdt_object_put(info->mti_env, slave);
1935 GOTO(out, rc = -ENOMEM);
1938 mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
1939 rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
1940 MDS_INODELOCK_UPDATE, true);
1943 mdt_object_put(info->mti_env, slave);
1947 INIT_LIST_HEAD(&msl->msl_linkage);
1948 msl->msl_obj = slave;
1949 list_add_tail(&msl->msl_linkage, slave_locks);
1955 mdt_unlock_list(info, slave_locks, rc);
1959 /* lock parent and its stripes */
1960 static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
1961 struct mdt_object *obj,
1962 const struct md_attr *ma,
1963 struct mdt_lock_handle *lh,
1964 struct ldlm_enqueue_info *einfo,
1965 struct list_head *slave_locks)
1969 if (mdt_object_remote(obj)) {
1970 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1971 &lh->mlh_rreg_lh, LCK_PW,
1972 MDS_INODELOCK_UPDATE, false);
1977 * if obj is remote and striped, lock its stripes explicitly
1978 * because it's not striped in LOD layer on this MDT.
1980 if (ma->ma_valid & MA_LMV) {
1981 rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
1983 mdt_object_unlock(info, obj, lh, rc);
1986 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
1994 * in migration, object may be remote, and we need take full lock of it and its
1995 * stripes if it's directory, besides, object may be a remote object on its
1996 * parent, revoke its LOOKUP lock on where its parent is located.
1998 static int mdt_migrate_object_lock(struct mdt_thread_info *info,
1999 struct mdt_object *pobj,
2000 struct mdt_object *obj,
2001 struct mdt_lock_handle *lh,
2002 struct ldlm_enqueue_info *einfo,
2003 struct list_head *slave_locks)
2007 if (mdt_object_remote(obj)) {
2008 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
2012 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
2013 &lh->mlh_rreg_lh, LCK_EX,
2014 MDS_INODELOCK_FULL, false);
2019 * if obj is remote and striped, lock its stripes explicitly
2020 * because it's not striped in LOD layer on this MDT.
2022 if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
2023 struct md_attr *ma = &info->mti_attr;
2025 rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
2027 mdt_object_unlock(info, obj, lh, rc);
2031 if (ma->ma_valid & MA_LMV) {
2032 rc = mdt_lock_remote_slaves(info, obj, ma,
2035 mdt_object_unlock(info, obj, lh, rc);
2039 if (mdt_object_remote(pobj)) {
2040 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
2045 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
2053 * lookup source by name, if parent is striped directory, we need to find the
2054 * corresponding stripe where source is located, and then lookup there.
2056 * besides, if parent is migrating too, and file is already in target stripe,
2057 * this should be a redo of 'lfs migrate' on client side.
2059 static int mdt_migrate_lookup(struct mdt_thread_info *info,
2060 struct mdt_object *pobj,
2061 const struct md_attr *ma,
2062 const struct lu_name *lname,
2063 struct mdt_object **spobj,
2064 struct mdt_object **sobj)
2066 const struct lu_env *env = info->mti_env;
2067 struct lu_fid *fid = &info->mti_tmp_fid1;
2068 struct mdt_object *stripe;
2071 if (ma->ma_valid & MA_LMV) {
2072 /* if parent is striped, lookup on corresponding stripe */
2073 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
2075 if (!lmv_is_sane(lmv))
2078 rc = lmv_name_to_stripe_index_old(lmv, lname->ln_name,
2083 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2085 stripe = mdt_object_find(env, info->mti_mdt, fid);
2087 return PTR_ERR(stripe);
2090 rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
2092 if (rc == -ENOENT && lmv_is_layout_changing(lmv)) {
2094 * if parent layout is changeing, and lookup child
2095 * failed on source stripe, lookup again on target
2096 * stripe, if it exists, it means previous migration
2097 * was interrupted, and current file was migrated
2100 mdt_object_put(env, stripe);
2102 rc = lmv_name_to_stripe_index(lmv, lname->ln_name,
2107 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2109 stripe = mdt_object_find(env, info->mti_mdt, fid);
2111 return PTR_ERR(stripe);
2114 rc = mdo_lookup(env, mdt_object_child(stripe), lname,
2115 fid, &info->mti_spec);
2116 mdt_object_put(env, stripe);
2117 return rc ?: -EALREADY;
2119 mdt_object_put(env, stripe);
2124 rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
2130 mdt_object_get(env, stripe);
2135 *sobj = mdt_object_find(env, info->mti_mdt, fid);
2136 if (IS_ERR(*sobj)) {
2137 mdt_object_put(env, stripe);
2138 rc = PTR_ERR(*sobj);
2146 /* end lease and close file for regular file */
2147 static int mdd_migrate_close(struct mdt_thread_info *info,
2148 struct mdt_object *obj)
2150 struct close_data *data;
2151 struct mdt_body *repbody;
2152 struct ldlm_lock *lease;
2157 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
2159 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
2163 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2168 lease = ldlm_handle2lock(&data->cd_handle);
2172 /* check if the lease was already canceled */
2173 lock_res_and_lock(lease);
2174 rc = ldlm_is_cancel(lease);
2175 unlock_res_and_lock(lease);
2179 LDLM_DEBUG(lease, DFID" lease broken",
2180 PFID(mdt_object_fid(obj)));
2184 * cancel server side lease, client side counterpart should have been
2185 * cancelled, it's okay to cancel it now as we've held mot_open_sem.
2187 ldlm_lock_cancel(lease);
2188 ldlm_reprocess_all(lease->l_resource,
2189 lease->l_policy_data.l_inodebits.bits);
2190 LDLM_LOCK_PUT(lease);
2193 rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
2194 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2195 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2201 * migrate file in below steps:
2202 * 1. lock parent and its stripes
2203 * 2. lookup source by name
2204 * 3. lock parents of source links if source is not directory
2205 * 4. reject if source is in HSM
2206 * 5. take source open_sem and close file if source is regular file
2207 * 6. lock source and its stripes if it's directory
2208 * 7. lock target so subsequent change to it can trigger COS
2210 * 9. unlock above locks
2211 * 10. sync device if source has links
2213 int mdt_reint_migrate(struct mdt_thread_info *info,
2214 struct mdt_lock_handle *unused)
2216 const struct lu_env *env = info->mti_env;
2217 struct mdt_device *mdt = info->mti_mdt;
2218 struct ptlrpc_request *req = mdt_info_req(info);
2219 struct mdt_reint_record *rr = &info->mti_rr;
2220 struct lu_ucred *uc = mdt_ucred(info);
2221 struct md_attr *ma = &info->mti_attr;
2222 struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
2223 struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
2224 struct mdt_object *pobj;
2225 struct mdt_object *spobj = NULL;
2226 struct mdt_object *sobj = NULL;
2227 struct mdt_object *tobj;
2228 struct lustre_handle rename_lh = { 0 };
2229 struct mdt_lock_handle *lhp;
2230 struct mdt_lock_handle *lhs;
2231 struct mdt_lock_handle *lht;
2232 LIST_HEAD(parent_slave_locks);
2233 LIST_HEAD(child_slave_locks);
2234 LIST_HEAD(link_locks);
2235 int lock_retries = 5;
2236 bool open_sem_locked = false;
2237 bool do_sync = false;
2241 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
2242 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
2244 if (info->mti_dlm_req)
2245 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2247 if (!fid_is_md_operative(rr->rr_fid1) ||
2248 !fid_is_md_operative(rr->rr_fid2))
2251 /* don't allow migrate . or .. */
2252 if (lu_name_is_dot_or_dotdot(&rr->rr_name))
2255 if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
2258 if (uc && !cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
2259 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
2260 mdt->mdt_enable_remote_dir_gid != -1)
2264 * Note: do not enqueue rename lock for replay request, because
2265 * if other MDT holds rename lock, but being blocked to wait for
2266 * this MDT to finish its recovery, and the failover MDT can not
2267 * get rename lock, which will cause deadlock.
2269 * req is NULL if this is called by directory auto-split.
2271 if (req && !req_is_replay(req)) {
2272 rc = mdt_rename_lock(info, &rename_lh);
2274 CERROR("%s: can't lock FS for rename: rc = %d\n",
2275 mdt_obd_name(info->mti_mdt), rc);
2280 /* pobj is master object of parent */
2281 pobj = mdt_object_find(env, mdt, rr->rr_fid1);
2283 GOTO(unlock_rename, rc = PTR_ERR(pobj));
2286 rc = mdt_version_get_check(info, pobj, 0);
2288 GOTO(put_parent, rc);
2291 if (!mdt_object_exists(pobj))
2292 GOTO(put_parent, rc = -ENOENT);
2294 if (!S_ISDIR(lu_object_attr(&pobj->mot_obj)))
2295 GOTO(put_parent, rc = -ENOTDIR);
2297 rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
2299 GOTO(put_parent, rc);
2302 /* lock parent object */
2303 lhp = &info->mti_lh[MDT_LH_PARENT];
2304 mdt_lock_reg_init(lhp, LCK_PW);
2305 rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
2306 &parent_slave_locks);
2308 GOTO(put_parent, rc);
2311 * spobj is the corresponding stripe against name if pobj is striped
2312 * directory, which is the real parent, and no need to lock, because
2313 * we've taken full lock of pobj.
2315 rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
2317 GOTO(unlock_parent, rc);
2319 /* lock parents of source links, and revoke LOOKUP lock of links */
2320 rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
2321 &parent_slave_locks, &link_locks);
2322 if (rc == -EBUSY && lock_retries-- > 0) {
2323 mdt_object_put(env, sobj);
2324 mdt_object_put(env, spobj);
2329 GOTO(put_source, rc);
2332 * RS_MAX_LOCKS is the limit of number of locks that can be saved along
2333 * with one request, if total lock count exceeds this limit, we will
2334 * drop all locks after migration, and synchronous device in the end.
2338 /* TODO: DoM migration is not supported, migrate dirent only */
2339 if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
2340 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
2342 GOTO(unlock_links, rc);
2344 if (ma->ma_valid & MA_LOV && mdt_lmm_dom_stripesize(ma->ma_lmm))
2345 info->mti_spec.sp_migrate_nsonly = 1;
2346 } else if (S_ISDIR(lu_object_attr(&sobj->mot_obj))) {
2347 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2349 GOTO(unlock_links, rc);
2351 /* race with restripe/auto-split? */
2352 if ((ma->ma_valid & MA_LMV) &&
2353 lmv_is_restriping(&ma->ma_lmv->lmv_md_v1))
2354 GOTO(unlock_links, rc = -EBUSY);
2357 /* if migration HSM is allowed */
2358 if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
2359 ma->ma_need = MA_HSM;
2361 rc = mdt_attr_get_complex(info, sobj, ma);
2363 GOTO(unlock_links, rc);
2365 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
2366 GOTO(unlock_links, rc = -EOPNOTSUPP);
2369 /* end lease and close file for regular file */
2370 if (info->mti_spec.sp_migrate_close) {
2371 /* try to hold open_sem so that nobody else can open the file */
2372 if (!down_write_trylock(&sobj->mot_open_sem)) {
2374 mdd_migrate_close(info, sobj);
2375 GOTO(unlock_links, rc = -EBUSY);
2377 open_sem_locked = true;
2378 rc = mdd_migrate_close(info, sobj);
2380 GOTO(unlock_open_sem, rc);
2385 lhs = &info->mti_lh[MDT_LH_OLD];
2386 mdt_lock_reg_init(lhs, LCK_EX);
2387 rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
2388 &child_slave_locks);
2390 GOTO(unlock_open_sem, rc);
2393 tobj = mdt_object_find(env, mdt, rr->rr_fid2);
2395 GOTO(unlock_source, rc = PTR_ERR(tobj));
2397 lht = &info->mti_lh[MDT_LH_NEW];
2398 mdt_lock_reg_init(lht, LCK_EX);
2399 rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
2401 GOTO(put_target, rc);
2403 /* Don't do lookup sanity check. We know name doesn't exist. */
2404 info->mti_spec.sp_cr_lookup = 0;
2405 info->mti_spec.sp_feat = &dt_directory_features;
2407 rc = mdo_migrate(env, mdt_object_child(pobj),
2408 mdt_object_child(sobj), &rr->rr_name,
2409 mdt_object_child(tobj),
2410 &info->mti_spec, ma);
2412 lprocfs_counter_incr(mdt->mdt_lu_dev.ld_obd->obd_md_stats,
2413 LPROC_MDT_MIGRATE + LPROC_MD_LAST_OPC);
2416 mdt_object_unlock(info, tobj, lht, rc);
2418 mdt_object_put(env, tobj);
2420 mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
2421 &child_slave_locks, rc);
2423 if (open_sem_locked)
2424 up_write(&sobj->mot_open_sem);
2426 /* if we've got too many locks to save into RPC,
2427 * then just commit before the locks are released
2430 mdt_device_sync(env, mdt);
2431 mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
2433 mdt_object_put(env, sobj);
2434 mdt_object_put(env, spobj);
2436 mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
2437 &parent_slave_locks, rc);
2439 mdt_object_put(env, pobj);
2441 if (lustre_handle_is_used(&rename_lh))
2442 mdt_rename_unlock(&rename_lh);
2447 static int mdt_object_lock_save(struct mdt_thread_info *info,
2448 struct mdt_object *dir,
2449 struct mdt_lock_handle *lh,
2450 int idx, bool cos_incompat)
2454 /* we lock the target dir if it is local */
2455 rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
2460 /* get and save correct version after locking */
2461 mdt_version_get_save(info, dir, idx);
2466 * determine lock order of sobj and tobj
2468 * there are two situations we need to lock tobj before sobj:
2469 * 1. sobj is child of tobj
2470 * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
2471 * larger than that of tobj
2473 * \retval 1 lock tobj before sobj
2474 * \retval 0 lock sobj before tobj
2475 * \retval -ev negative errno upon error
2477 static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
2478 struct mdt_object *sobj,
2479 struct mdt_object *tobj)
2481 struct md_attr *ma = &info->mti_attr;
2482 struct lu_fid *spfid = &info->mti_tmp_fid1;
2483 struct lu_fid *tpfid = &info->mti_tmp_fid2;
2484 struct lmv_mds_md_v1 *lmv;
2489 /* sobj and tobj are the same */
2493 if (fid_is_root(mdt_object_fid(sobj)))
2496 if (fid_is_root(mdt_object_fid(tobj)))
2499 /* check whether sobj is child of tobj */
2500 rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
2501 mdt_object_fid(tobj));
2508 /* check whether sobj and tobj are children of the same parent */
2509 rc = mdt_attr_get_pfid(info, sobj, spfid);
2513 rc = mdt_attr_get_pfid(info, tobj, tpfid);
2517 if (!lu_fid_eq(spfid, tpfid))
2520 /* check whether sobj and tobj are sibling stripes */
2521 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2525 if (!(ma->ma_valid & MA_LMV))
2528 lmv = &ma->ma_lmv->lmv_md_v1;
2529 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2531 sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2534 rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
2538 if (!(ma->ma_valid & MA_LMV))
2541 lmv = &ma->ma_lmv->lmv_md_v1;
2542 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2544 tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2546 /* check stripe index of sobj and tobj */
2547 if (sindex == tindex)
2550 return sindex < tindex ? 0 : 1;
2554 * lock rename source object.
2556 * Both source and source parent may be remote, and source may be a remote
2557 * object on source parent, to avoid overriding lock handle, store remote
2558 * LOOKUP lock separately in @lhr.
2560 * \retval 0 on success
2561 * \retval -ev negative errno upon error
2563 static int mdt_rename_source_lock(struct mdt_thread_info *info,
2564 struct mdt_object *parent,
2565 struct mdt_object *child,
2566 struct mdt_lock_handle *lhc,
2567 struct mdt_lock_handle *lhr,
2573 rc = mdt_is_remote_object(info, parent, child);
2578 /* enqueue remote LOOKUP lock from the parent MDT */
2579 __u64 rmt_ibits = MDS_INODELOCK_LOOKUP;
2581 if (mdt_object_remote(parent)) {
2582 rc = mdt_remote_object_lock(info, parent,
2583 mdt_object_fid(child),
2590 LASSERT(mdt_object_remote(child));
2591 rc = mdt_object_local_lock(info, child, lhr,
2592 &rmt_ibits, 0, true);
2597 ibits &= ~MDS_INODELOCK_LOOKUP;
2600 if (mdt_object_remote(child)) {
2601 rc = mdt_remote_object_lock(info, child, mdt_object_fid(child),
2608 rc = mdt_reint_object_lock(info, child, lhc, ibits,
2613 mdt_object_unlock(info, child, lhr, rc);
2619 * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
2620 * 2 - srcdir child; 3 - tgtdir child.
2621 * Update on disk version of srcdir child.
2623 static int mdt_reint_rename(struct mdt_thread_info *info,
2624 struct mdt_lock_handle *unused)
2626 struct mdt_device *mdt = info->mti_mdt;
2627 struct mdt_reint_record *rr = &info->mti_rr;
2628 struct md_attr *ma = &info->mti_attr;
2629 struct ptlrpc_request *req = mdt_info_req(info);
2630 struct mdt_object *msrcdir = NULL;
2631 struct mdt_object *mtgtdir = NULL;
2632 struct mdt_object *mold;
2633 struct mdt_object *mnew = NULL;
2634 struct lustre_handle rename_lh = { 0 };
2635 struct mdt_lock_handle *lh_srcdirp;
2636 struct mdt_lock_handle *lh_tgtdirp;
2637 struct mdt_lock_handle *lh_oldp = NULL;
2638 struct mdt_lock_handle *lh_rmt = NULL;
2639 struct mdt_lock_handle *lh_newp = NULL;
2640 struct lu_fid *old_fid = &info->mti_tmp_fid1;
2641 struct lu_fid *new_fid = &info->mti_tmp_fid2;
2643 bool reverse = false, discard = false;
2645 ktime_t kstart = ktime_get();
2649 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
2650 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
2651 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
2653 if (info->mti_dlm_req)
2654 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2656 if (!fid_is_md_operative(rr->rr_fid1) ||
2657 !fid_is_md_operative(rr->rr_fid2))
2660 /* find both parents. */
2661 msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
2662 if (IS_ERR(msrcdir))
2663 RETURN(PTR_ERR(msrcdir));
2665 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
2667 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
2669 mdt_object_get(info->mti_env, mtgtdir);
2671 mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
2672 if (IS_ERR(mtgtdir))
2673 GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
2677 * Note: do not enqueue rename lock for replay request, because
2678 * if other MDT holds rename lock, but being blocked to wait for
2679 * this MDT to finish its recovery, and the failover MDT can not
2680 * get rename lock, which will cause deadlock.
2682 if (!req_is_replay(req)) {
2684 * Normally rename RPC is handled on the MDT with the target
2685 * directory (if target exists, it's on the MDT with the
2686 * target), if the source directory is remote, it's a hint that
2687 * source is remote too (this may not be true, but it won't
2688 * cause any issue), return -EXDEV early to avoid taking
2691 if (!mdt->mdt_enable_remote_rename &&
2692 mdt_object_remote(msrcdir))
2693 GOTO(out_put_tgtdir, rc = -EXDEV);
2695 /* This might be further relaxed in the future for regular file
2696 * renames in different source and target parents. Start with
2697 * only same-directory renames for simplicity and because this
2698 * is by far the most the common use case.
2700 if (msrcdir != mtgtdir) {
2701 rc = mdt_rename_lock(info, &rename_lh);
2703 CERROR("%s: cannot lock for rename: rc = %d\n",
2704 mdt_obd_name(mdt), rc);
2705 GOTO(out_put_tgtdir, rc);
2708 CDEBUG(D_INFO, "%s: samedir rename "DFID"/"DNAME"\n",
2709 mdt_obd_name(mdt), PFID(rr->rr_fid1),
2710 PNAME(&rr->rr_name));
2714 rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
2716 GOTO(out_unlock_rename, rc);
2720 /* source needs to be looked up after locking source parent, otherwise
2721 * this rename may race with unlink source, and cause rename hang, see
2722 * sanityn.sh 55b, so check parents first, if later we found source is
2723 * remote, relock parents.
2725 cos_incompat = (mdt_object_remote(msrcdir) ||
2726 mdt_object_remote(mtgtdir));
2728 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2730 /* lock parents in the proper order. */
2731 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
2732 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
2734 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
2735 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
2737 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
2738 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
2741 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2744 GOTO(out_unlock_rename, rc);
2746 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2748 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2751 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2752 GOTO(out_unlock_rename, rc);
2755 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2758 GOTO(out_unlock_rename, rc);
2760 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2762 if (mtgtdir != msrcdir) {
2763 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2765 } else if (!mdt_object_remote(mtgtdir) &&
2766 lh_srcdirp->mlh_pdo_hash !=
2767 lh_tgtdirp->mlh_pdo_hash) {
2768 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
2769 MDS_INODELOCK_UPDATE,
2771 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
2774 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2775 GOTO(out_unlock_rename, rc);
2779 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2780 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
2782 /* find mold object. */
2784 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
2786 GOTO(out_unlock_parents, rc);
2788 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
2789 GOTO(out_unlock_parents, rc = -EINVAL);
2791 if (!fid_is_md_operative(old_fid))
2792 GOTO(out_unlock_parents, rc = -EPERM);
2794 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
2796 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
2798 if (!mdt_object_exists(mold)) {
2799 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2801 "object does not exist");
2802 GOTO(out_put_old, rc = -ENOENT);
2805 if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
2806 GOTO(out_put_old, rc = -EXDEV);
2808 /* Check if @mtgtdir is subdir of @mold, before locking child
2809 * to avoid reverse locking.
2811 if (mtgtdir != msrcdir) {
2812 rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
2817 GOTO(out_put_old, rc);
2821 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
2822 /* save version after locking */
2823 mdt_version_get_save(info, mold, 2);
2825 if (!cos_incompat && mdt_object_remote(mold)) {
2826 cos_incompat = true;
2827 mdt_object_put(info->mti_env, mold);
2828 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
2829 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
2833 /* find mnew object:
2834 * mnew target object may not exist now
2835 * lookup with version checking
2838 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
2841 /* the new_fid should have been filled at this moment */
2842 if (lu_fid_eq(old_fid, new_fid))
2843 GOTO(out_put_old, rc);
2845 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
2846 lu_fid_eq(new_fid, rr->rr_fid2))
2847 GOTO(out_put_old, rc = -EINVAL);
2849 if (!fid_is_md_operative(new_fid))
2850 GOTO(out_put_old, rc = -EPERM);
2852 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
2854 GOTO(out_put_old, rc = PTR_ERR(mnew));
2856 if (!mdt_object_exists(mnew)) {
2857 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2859 "object does not exist");
2860 GOTO(out_put_new, rc = -ENOENT);
2863 if (mdt_object_remote(mnew)) {
2864 struct mdt_body *repbody;
2866 /* Always send rename req to the target child MDT */
2867 repbody = req_capsule_server_get(info->mti_pill,
2869 LASSERT(repbody != NULL);
2870 repbody->mbo_fid1 = *new_fid;
2871 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2872 GOTO(out_put_new, rc = -EXDEV);
2874 /* Before locking the target dir, check we do not replace
2875 * a dir with a non-dir, otherwise it may deadlock with
2876 * link op which tries to create a link in this dir
2877 * back to this non-dir.
2879 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2880 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2881 GOTO(out_put_new, rc = -EISDIR);
2883 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2884 lh_rmt = &info->mti_lh[MDT_LH_RMT];
2885 mdt_lock_reg_init(lh_oldp, LCK_EX);
2886 mdt_lock_reg_init(lh_rmt, LCK_EX);
2887 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2888 rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
2889 lh_rmt, lock_ibits, cos_incompat);
2891 GOTO(out_put_new, rc);
2893 /* Check if @msrcdir is subdir of @mnew, before locking child
2894 * to avoid reverse locking.
2896 if (mtgtdir != msrcdir) {
2897 rc = mdo_is_subdir(info->mti_env,
2898 mdt_object_child(msrcdir), new_fid);
2902 GOTO(out_unlock_old, rc);
2906 /* We used to acquire MDS_INODELOCK_FULL here but we
2907 * can't do this now because a running HSM restore on
2908 * the rename onto victim will hold the layout
2909 * lock. See LU-4002.
2912 lh_newp = &info->mti_lh[MDT_LH_NEW];
2913 mdt_lock_reg_init(lh_newp, LCK_EX);
2914 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2915 if (mdt_object_remote(mtgtdir)) {
2916 rc = mdt_remote_object_lock(info, mtgtdir,
2917 mdt_object_fid(mnew),
2918 &lh_newp->mlh_rreg_lh,
2919 lh_newp->mlh_rreg_mode,
2920 MDS_INODELOCK_LOOKUP,
2923 GOTO(out_unlock_old, rc);
2925 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2927 rc = mdt_reint_object_lock(info, mnew, lh_newp, lock_ibits,
2930 GOTO(out_unlock_new, rc);
2932 /* get and save version after locking */
2933 mdt_version_get_save(info, mnew, 3);
2934 } else if (rc != -ENOENT) {
2935 GOTO(out_put_old, rc);
2937 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2938 lh_rmt = &info->mti_lh[MDT_LH_RMT];
2939 mdt_lock_reg_init(lh_oldp, LCK_EX);
2940 mdt_lock_reg_init(lh_rmt, LCK_EX);
2941 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2942 rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
2943 lh_rmt, lock_ibits, cos_incompat);
2945 GOTO(out_put_old, rc);
2947 mdt_enoent_version_save(info, 3);
2950 /* step 5: rename it */
2951 mdt_reint_init_ma(info, ma);
2953 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
2954 OBD_FAIL_MDS_REINT_RENAME_WRITE);
2957 mutex_lock(&mnew->mot_lov_mutex);
2959 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
2960 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
2961 mnew != NULL ? mdt_object_child(mnew) : NULL,
2962 &rr->rr_tgt_name, ma);
2965 mutex_unlock(&mnew->mot_lov_mutex);
2967 /* handle last link of tgt object */
2969 mdt_counter_incr(req, LPROC_MDT_RENAME,
2970 ktime_us_delta(ktime_get(), kstart));
2972 mdt_handle_last_unlink(info, mnew, ma);
2973 discard = mdt_dom_check_for_discard(info, mnew);
2975 mdt_rename_counter_tally(info, info->mti_mdt, req,
2977 ktime_us_delta(ktime_get(), kstart));
2983 mdt_object_unlock(info, mnew, lh_newp, rc);
2985 mdt_object_unlock(info, NULL, lh_rmt, rc);
2986 mdt_object_unlock(info, mold, lh_oldp, rc);
2988 if (mnew && !discard)
2989 mdt_object_put(info->mti_env, mnew);
2991 mdt_object_put(info->mti_env, mold);
2993 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2994 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2996 if (lustre_handle_is_used(&rename_lh))
2997 mdt_rename_unlock(&rename_lh);
2999 mdt_object_put(info->mti_env, mtgtdir);
3001 mdt_object_put(info->mti_env, msrcdir);
3003 /* The DoM discard can be done right in the place above where it is
3004 * assigned, meanwhile it is done here after rename unlock due to
3005 * compatibility with old clients, for them the discard blocks
3006 * the main thread until completion. Check LU-11359 for details.
3009 mdt_dom_discard_data(info, mnew);
3010 mdt_object_put(info->mti_env, mnew);
3012 OBD_RACE(OBD_FAIL_MDS_LINK_RENAME_RACE);
3016 static int mdt_reint_resync(struct mdt_thread_info *info,
3017 struct mdt_lock_handle *lhc)
3019 struct mdt_reint_record *rr = &info->mti_rr;
3020 struct ptlrpc_request *req = mdt_info_req(info);
3021 struct md_attr *ma = &info->mti_attr;
3022 struct mdt_object *mo;
3023 struct ldlm_lock *lease;
3024 struct mdt_body *repbody;
3025 struct md_layout_change layout = { .mlc_mirror_id = rr->rr_mirror_id };
3030 DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
3032 if (info->mti_dlm_req)
3033 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
3035 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
3037 GOTO(out, rc = PTR_ERR(mo));
3039 if (!mdt_object_exists(mo))
3040 GOTO(out_obj, rc = -ENOENT);
3042 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
3043 GOTO(out_obj, rc = -EINVAL);
3045 if (mdt_object_remote(mo))
3046 GOTO(out_obj, rc = -EREMOTE);
3048 lease = ldlm_handle2lock(rr->rr_lease_handle);
3050 GOTO(out_obj, rc = -ESTALE);
3052 /* It's really necessary to grab open_sem and check if the lease lock
3053 * has been lost. There would exist a concurrent writer coming in and
3054 * generating some dirty data in memory cache, the writeback would fail
3055 * after the layout version is increased by MDS_REINT_RESYNC RPC.
3057 if (!down_write_trylock(&mo->mot_open_sem))
3058 GOTO(out_put_lease, rc = -EBUSY);
3060 lock_res_and_lock(lease);
3061 lease_broken = ldlm_is_cancel(lease);
3062 unlock_res_and_lock(lease);
3064 GOTO(out_unlock, rc = -EBUSY);
3066 /* the file has yet opened by anyone else after we took the lease. */
3067 layout.mlc_opc = MD_LAYOUT_RESYNC;
3068 lhc = &info->mti_lh[MDT_LH_LOCAL];
3069 rc = mdt_layout_change(info, mo, lhc, &layout);
3071 GOTO(out_unlock, rc);
3073 mdt_object_unlock(info, mo, lhc, 0);
3075 ma->ma_need = MA_INODE;
3077 rc = mdt_attr_get_complex(info, mo, ma);
3079 GOTO(out_unlock, rc);
3081 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3082 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
3086 up_write(&mo->mot_open_sem);
3088 LDLM_LOCK_PUT(lease);
3090 mdt_object_put(info->mti_env, mo);
3092 mdt_client_compatibility(info);
3093 rc2 = mdt_fix_reply(info);
3099 struct mdt_reinter {
3100 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
3101 enum lprocfs_extra_opc mr_extra_opc;
3104 static const struct mdt_reinter mdt_reinters[] = {
3106 .mr_handler = &mdt_reint_setattr,
3107 .mr_extra_opc = MDS_REINT_SETATTR,
3110 .mr_handler = &mdt_reint_create,
3111 .mr_extra_opc = MDS_REINT_CREATE,
3114 .mr_handler = &mdt_reint_link,
3115 .mr_extra_opc = MDS_REINT_LINK,
3118 .mr_handler = &mdt_reint_unlink,
3119 .mr_extra_opc = MDS_REINT_UNLINK,
3122 .mr_handler = &mdt_reint_rename,
3123 .mr_extra_opc = MDS_REINT_RENAME,
3126 .mr_handler = &mdt_reint_open,
3127 .mr_extra_opc = MDS_REINT_OPEN,
3129 [REINT_SETXATTR] = {
3130 .mr_handler = &mdt_reint_setxattr,
3131 .mr_extra_opc = MDS_REINT_SETXATTR,
3134 .mr_handler = &mdt_reint_unlink,
3135 .mr_extra_opc = MDS_REINT_UNLINK,
3138 .mr_handler = &mdt_reint_migrate,
3139 .mr_extra_opc = MDS_REINT_RENAME,
3142 .mr_handler = &mdt_reint_resync,
3143 .mr_extra_opc = MDS_REINT_RESYNC,
3147 int mdt_reint_rec(struct mdt_thread_info *info,
3148 struct mdt_lock_handle *lhc)
3150 const struct mdt_reinter *mr;
3154 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
3157 mr = &mdt_reinters[info->mti_rr.rr_opcode];
3158 if (mr->mr_handler == NULL)
3161 rc = (*mr->mr_handler)(info, lhc);
3163 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
3164 PTLRPC_LAST_CNTR + mr->mr_extra_opc);