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 lu_ucred *uc = mdt_ucred(info);
359 struct lmv_mds_md_v1 *lmv;
360 struct mdt_object *child;
361 struct mdt_lock_handle *lhp;
362 struct mdt_lock_handle *lhc;
363 struct mdt_body *repbody;
368 /* we want rbac roles to have precedence over any other
369 * permission or capability checks
371 if (!mdt->mdt_enable_dir_restripe && !uc->uc_rbac_dne_ops)
375 lum->lum_hash_type |= cpu_to_le32(LMV_HASH_FLAG_FIXED);
377 rc = mdt_version_get_check_save(info, parent, 0);
381 lhp = &info->mti_lh[MDT_LH_PARENT];
382 mdt_lock_pdo_init(lhp, LCK_PW, lname);
383 rc = mdt_reint_object_lock(info, parent, lhp, MDS_INODELOCK_UPDATE,
388 rc = mdt_stripe_get(info, parent, ma, XATTR_NAME_LMV);
390 GOTO(unlock_parent, rc);
392 if (ma->ma_valid & MA_LMV) {
393 /* don't allow restripe if parent dir layout is changing */
394 lmv = &ma->ma_lmv->lmv_md_v1;
395 if (!lmv_is_sane2(lmv))
396 GOTO(unlock_parent, rc = -EBADF);
398 if (lmv_is_layout_changing(lmv))
399 GOTO(unlock_parent, rc = -EBUSY);
403 rc = mdt_lookup_version_check(info, parent, lname, fid, 1);
405 GOTO(unlock_parent, rc);
407 child = mdt_object_find(info->mti_env, mdt, fid);
409 GOTO(unlock_parent, rc = PTR_ERR(child));
411 if (!mdt_object_exists(child))
412 GOTO(out_child, rc = -ENOENT);
414 if (mdt_object_remote(child)) {
415 struct mdt_body *repbody;
417 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
419 GOTO(out_child, rc = -EPROTO);
421 repbody->mbo_fid1 = *fid;
422 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
423 GOTO(out_child, rc = -EREMOTE);
426 if (!S_ISDIR(lu_object_attr(&child->mot_obj)))
427 GOTO(out_child, rc = -ENOTDIR);
429 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
433 /* race with migrate? */
434 if ((ma->ma_valid & MA_LMV) &&
435 lmv_is_migrating(&ma->ma_lmv->lmv_md_v1))
436 GOTO(out_child, rc = -EBUSY);
439 lhc = &info->mti_lh[MDT_LH_CHILD];
440 mdt_lock_reg_init(lhc, LCK_EX);
442 /* enqueue object remote LOOKUP lock */
443 if (mdt_object_remote(parent)) {
444 rc = mdt_remote_object_lock(info, parent, fid,
447 MDS_INODELOCK_LOOKUP, false);
452 rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
455 GOTO(unlock_child, rc);
457 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
458 rc = mdt_version_get_check_save(info, child, 1);
460 GOTO(unlock_child, rc);
462 spin_lock(&mdt->mdt_restriper.mdr_lock);
463 if (child->mot_restriping) {
465 spin_unlock(&mdt->mdt_restriper.mdr_lock);
466 GOTO(unlock_child, rc = -EBUSY);
468 child->mot_restriping = 1;
469 spin_unlock(&mdt->mdt_restriper.mdr_lock);
472 rc = mdt_restripe_internal(info, parent, child, lname, fid, spec, ma);
474 GOTO(restriping_clear, rc);
476 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
478 GOTO(restriping_clear, rc = -EPROTO);
480 mdt_pack_attr2body(info, repbody, &ma->ma_attr, fid);
484 child->mot_restriping = 0;
486 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
488 mdt_object_put(info->mti_env, child);
490 mdt_object_unlock(info, parent, lhp, rc);
496 * VBR: we save three versions in reply:
497 * 0 - parent. Check that parent version is the same during replay.
498 * 1 - name. Version of 'name' if file exists with the same name or
499 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
500 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
503 static int mdt_create(struct mdt_thread_info *info)
505 struct mdt_device *mdt = info->mti_mdt;
506 struct mdt_object *parent;
507 struct mdt_object *child;
508 struct mdt_lock_handle *lh;
509 struct mdt_body *repbody;
510 struct md_attr *ma = &info->mti_attr;
511 struct mdt_reint_record *rr = &info->mti_rr;
512 struct md_op_spec *spec = &info->mti_spec;
513 bool restripe = false;
517 DEBUG_REQ(D_INODE, mdt_info_req(info),
518 "Create ("DNAME"->"DFID") in "DFID,
519 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
521 if (!fid_is_md_operative(rr->rr_fid1))
524 if (S_ISDIR(ma->ma_attr.la_mode) &&
525 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
526 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
527 struct lu_ucred *uc = mdt_ucred(info);
528 struct obd_export *exp = mdt_info_req(info)->rq_export;
530 /* Only new clients can create remote dir( >= 2.4) and
531 * striped dir(>= 2.6), old client will return -ENOTSUPP
533 if (!mdt_is_dne_client(exp))
536 if (le32_to_cpu(lum->lum_stripe_count) > 1) {
537 if (!mdt_is_striped_client(exp))
540 if (!mdt->mdt_enable_striped_dir)
542 } else if (!mdt->mdt_enable_remote_dir) {
546 if ((!(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH)) &&
547 (le32_to_cpu(lum->lum_hash_type) & LMV_HASH_TYPE_MASK) >=
551 /* we want rbac roles to have precedence over any other
552 * permission or capability checks
554 if (!uc->uc_rbac_dne_ops ||
555 (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
556 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
557 mdt->mdt_enable_remote_dir_gid != -1))
560 /* restripe if later found dir exists, MDS_OPEN_CREAT means
561 * this is create only, don't try restripe.
563 if (mdt->mdt_enable_dir_restripe &&
564 le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT &&
565 !(spec->sp_cr_flags & MDS_OPEN_CREAT))
569 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
571 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
573 RETURN(PTR_ERR(parent));
575 if (!mdt_object_exists(parent))
576 GOTO(put_parent, rc = -ENOENT);
578 rc = mdt_check_enc(info, parent);
580 GOTO(put_parent, rc);
583 * LU-10235: check if name exists locklessly first to avoid massive
584 * lock recalls on existing directories.
586 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
587 &info->mti_tmp_fid1, 1);
590 GOTO(put_parent, rc = -EEXIST);
592 rc = mdt_restripe(info, parent, &rr->rr_name, rr->rr_fid2, spec,
596 /* -ENOENT is expected here */
598 GOTO(put_parent, rc);
600 /* save version of file name for replay, it must be ENOENT here */
601 mdt_enoent_version_save(info, 1);
603 OBD_RACE(OBD_FAIL_MDS_CREATE_RACE);
605 lh = &info->mti_lh[MDT_LH_PARENT];
606 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
607 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
609 GOTO(put_parent, rc);
611 if (!mdt_object_remote(parent)) {
612 rc = mdt_version_get_check_save(info, parent, 0);
614 GOTO(unlock_parent, rc);
617 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
618 if (unlikely(IS_ERR(child)))
619 GOTO(unlock_parent, rc = PTR_ERR(child));
621 ma->ma_need = MA_INODE;
624 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
625 OBD_FAIL_MDS_REINT_CREATE_WRITE);
627 /* Version of child will be updated on disk. */
628 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
629 rc = mdt_version_get_check_save(info, child, 2);
634 * Do not perform lookup sanity check. We know that name does
637 info->mti_spec.sp_cr_lookup = 0;
638 if (mdt_object_remote(parent))
639 info->mti_spec.sp_cr_lookup = 1;
640 info->mti_spec.sp_feat = &dt_directory_features;
642 rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
643 mdt_object_child(child), &info->mti_spec, ma);
645 rc = mdt_attr_get_complex(info, child, ma);
651 * On DNE, we need to eliminate dependey between 'mkdir a' and
652 * 'mkdir a/b' if b is a striped directory, to achieve this, two
653 * things are done below:
654 * 1. save child and slaves lock.
655 * 2. if the child is a striped directory, relock parent so to
656 * compare against with COS locks to ensure parent was
659 if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
660 struct mdt_lock_handle *lhc;
661 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
664 rc = mdt_object_striped(info, child);
670 if (!mdt_object_remote(parent)) {
671 mdt_object_unlock(info, parent, lh, 1);
672 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
673 rc = mdt_reint_object_lock(info, parent, lh,
674 MDS_INODELOCK_UPDATE,
681 lhc = &info->mti_lh[MDT_LH_CHILD];
682 mdt_lock_handle_init(lhc);
683 mdt_lock_reg_init(lhc, LCK_PW);
684 rc = mdt_reint_striped_lock(info, child, lhc,
685 MDS_INODELOCK_UPDATE, einfo,
690 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
693 /* Return fid & attr to client. */
694 if (ma->ma_valid & MA_INODE)
695 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
696 mdt_object_fid(child));
699 mdt_object_put(info->mti_env, child);
701 mdt_object_unlock(info, parent, lh, rc);
703 mdt_object_put(info->mti_env, parent);
707 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
710 struct mdt_lock_handle *lh;
711 int do_vbr = ma->ma_attr.la_valid &
712 (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
713 __u64 lockpart = MDS_INODELOCK_UPDATE;
714 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
719 rc = mdt_object_striped(info, mo);
725 lh = &info->mti_lh[MDT_LH_PARENT];
726 mdt_lock_reg_init(lh, LCK_PW);
728 /* Even though the new MDT will grant PERM lock to the old
729 * client, but the old client will almost ignore that during
730 * So it needs to revoke both LOOKUP and PERM lock here, so
731 * both new and old client can cancel the dcache
733 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
734 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
735 /* Clear xattr cache on clients, so the virtual project ID xattr
736 * can get the new project ID
738 if (ma->ma_attr.la_valid & LA_PROJID)
739 lockpart |= MDS_INODELOCK_XATTR;
741 rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
746 /* all attrs are packed into mti_attr in unpack_setattr */
747 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
748 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
750 /* VBR: update version if attr changed are important for recovery */
752 /* update on-disk version of changed object */
753 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
754 rc = mdt_version_get_check_save(info, mo, 0);
756 GOTO(out_unlock, rc);
759 /* Ensure constant striping during chown(). See LU-2789. */
760 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
761 mutex_lock(&mo->mot_lov_mutex);
763 /* all attrs are packed into mti_attr in unpack_setattr */
764 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
766 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
767 mutex_unlock(&mo->mot_lov_mutex);
770 GOTO(out_unlock, rc);
771 mdt_dom_obj_lvb_update(info->mti_env, mo, NULL, false);
774 mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
779 * Check HSM flags and add HS_DIRTY flag if relevant.
781 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
782 * and is not RELEASED.
784 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
787 struct lu_ucred *uc = mdt_ucred(info);
788 kernel_cap_t cap_saved;
792 /* If the file was modified, add the dirty flag */
793 ma->ma_need = MA_HSM;
794 rc = mdt_attr_get_complex(info, mo, ma);
796 CERROR("file attribute read error for "DFID": %d.\n",
797 PFID(mdt_object_fid(mo)), rc);
801 /* If an up2date copy exists in the backend, add dirty flag */
802 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
803 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
804 ma->ma_hsm.mh_flags |= HS_DIRTY;
806 /* Bump cap so that closes from non-owner writers can
807 * set the HSM state to dirty.
809 cap_saved = uc->uc_cap;
810 cap_raise(uc->uc_cap, CAP_FOWNER);
811 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
812 uc->uc_cap = cap_saved;
814 CERROR("file attribute change error for "DFID": %d\n",
815 PFID(mdt_object_fid(mo)), rc);
821 static int mdt_reint_setattr(struct mdt_thread_info *info,
822 struct mdt_lock_handle *lhc)
824 struct mdt_device *mdt = info->mti_mdt;
825 struct md_attr *ma = &info->mti_attr;
826 struct mdt_reint_record *rr = &info->mti_rr;
827 struct ptlrpc_request *req = mdt_info_req(info);
828 struct mdt_object *mo;
829 struct mdt_body *repbody;
830 ktime_t kstart = ktime_get();
834 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
835 (unsigned int)ma->ma_attr.la_valid);
837 if (info->mti_dlm_req)
838 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
840 OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
842 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
843 mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
845 GOTO(out, rc = PTR_ERR(mo));
847 if (!mdt_object_exists(mo))
848 GOTO(out_put, rc = -ENOENT);
850 if (mdt_object_remote(mo))
851 GOTO(out_put, rc = -EREMOTE);
853 ma->ma_enable_chprojid_gid = mdt->mdt_enable_chprojid_gid;
854 /* revoke lease lock if size is going to be changed */
855 if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
856 !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
857 atomic_read(&mo->mot_lease_count) > 0)) {
858 down_read(&mo->mot_open_sem);
860 if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
861 lhc = &info->mti_lh[MDT_LH_LOCAL];
862 mdt_lock_reg_init(lhc, LCK_CW);
864 rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
866 up_read(&mo->mot_open_sem);
870 /* revoke lease lock */
871 mdt_object_unlock(info, mo, lhc, 1);
873 up_read(&mo->mot_open_sem);
876 if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
877 /* Check write access for the O_TRUNC case */
878 if (mdt_write_read(mo) < 0)
879 GOTO(out_put, rc = -ETXTBSY);
881 /* LU-10286: compatibility check for FLR.
882 * Please check the comment in mdt_finish_open() for details
884 if (!exp_connect_flr(info->mti_exp) ||
885 !exp_connect_overstriping(info->mti_exp)) {
886 rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
887 if (rc < 0 && rc != -ENODATA)
890 if (!exp_connect_flr(info->mti_exp)) {
892 mdt_lmm_is_flr(info->mti_big_lmm))
893 GOTO(out_put, rc = -EOPNOTSUPP);
896 if (!exp_connect_overstriping(info->mti_exp)) {
898 mdt_lmm_is_overstriping(info->mti_big_lmm))
899 GOTO(out_put, rc = -EOPNOTSUPP);
903 /* For truncate, the file size sent from client
904 * is believable, but the blocks are incorrect,
905 * which makes the block size in LSOM attribute
906 * inconsisent with the real block size.
908 rc = mdt_lsom_update(info, mo, true);
913 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
914 if (ma->ma_valid & MA_LOV)
915 GOTO(out_put, rc = -EPROTO);
917 /* MDT supports FMD for regular files due to Data-on-MDT */
918 if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
919 ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
920 tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
923 if (ma->ma_attr.la_valid & LA_MTIME) {
924 rc = mdt_attr_get_pfid(info, mo, &ma->ma_pfid);
926 ma->ma_valid |= MA_PFID;
930 rc = mdt_attr_set(info, mo, ma);
933 } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
934 (ma->ma_valid & MA_INODE)) {
935 struct lu_buf *buf = &info->mti_buf;
936 struct lu_ucred *uc = mdt_ucred(info);
937 struct mdt_lock_handle *lh;
939 __u64 lockpart = MDS_INODELOCK_XATTR;
941 /* reject if either remote or striped dir is disabled */
942 if (ma->ma_valid & MA_LMV) {
943 if (!mdt->mdt_enable_remote_dir ||
944 !mdt->mdt_enable_striped_dir)
945 GOTO(out_put, rc = -EPERM);
947 /* we want rbac roles to have precedence over any other
948 * permission or capability checks
950 if (!uc->uc_rbac_dne_ops ||
951 (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
952 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
953 mdt->mdt_enable_remote_dir_gid != -1))
954 GOTO(out_put, rc = -EPERM);
957 if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
958 GOTO(out_put, rc = -ENOTDIR);
960 if (ma->ma_attr.la_valid != 0)
961 GOTO(out_put, rc = -EPROTO);
963 lh = &info->mti_lh[MDT_LH_PARENT];
964 mdt_lock_reg_init(lh, LCK_PW);
966 if (ma->ma_valid & MA_LOV) {
967 buf->lb_buf = ma->ma_lmm;
968 buf->lb_len = ma->ma_lmm_size;
969 name = XATTR_NAME_LOV;
971 struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
972 struct lu_fid *pfid = &info->mti_tmp_fid1;
973 struct lu_name *pname = &info->mti_name;
974 const char dotdot[] = "..";
975 struct mdt_object *pobj;
978 buf->lb_len = ma->ma_lmv_size;
979 name = XATTR_NAME_DEFAULT_LMV;
981 if (fid_is_root(rr->rr_fid1)) {
982 lockpart |= MDS_INODELOCK_LOOKUP;
984 /* force client to update dir default layout */
986 pname->ln_name = dotdot;
987 pname->ln_namelen = sizeof(dotdot);
988 rc = mdo_lookup(info->mti_env,
989 mdt_object_child(mo), pname,
994 pobj = mdt_object_find(info->mti_env, mdt,
997 GOTO(out_put, rc = PTR_ERR(pobj));
999 if (mdt_object_remote(pobj))
1000 rc = mdt_remote_object_lock(info, pobj,
1002 &lh->mlh_rreg_lh, LCK_EX,
1003 MDS_INODELOCK_LOOKUP, false);
1005 lockpart |= MDS_INODELOCK_LOOKUP;
1007 mdt_object_put(info->mti_env, pobj);
1014 rc = mdt_object_lock(info, mo, lh, lockpart);
1018 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
1021 mdt_object_unlock(info, mo, lh, rc);
1025 GOTO(out_put, rc = -EPROTO);
1028 /* If file data is modified, add the dirty flag */
1029 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
1030 rc = mdt_add_dirty_flag(info, mo, ma);
1032 ma->ma_need = MA_INODE;
1034 rc = mdt_attr_get_complex(info, mo, ma);
1038 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
1042 mdt_object_put(info->mti_env, mo);
1045 mdt_counter_incr(req, LPROC_MDT_SETATTR,
1046 ktime_us_delta(ktime_get(), kstart));
1048 mdt_client_compatibility(info);
1052 static int mdt_reint_create(struct mdt_thread_info *info,
1053 struct mdt_lock_handle *lhc)
1055 struct ptlrpc_request *req = mdt_info_req(info);
1056 ktime_t kstart = ktime_get();
1060 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
1061 RETURN(err_serious(-ESTALE));
1063 if (info->mti_dlm_req)
1064 ldlm_request_cancel(mdt_info_req(info),
1065 info->mti_dlm_req, 0, LATF_SKIP);
1067 if (!lu_name_is_valid(&info->mti_rr.rr_name))
1070 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
1080 CERROR("%s: Unsupported mode %o\n",
1081 mdt_obd_name(info->mti_mdt),
1082 info->mti_attr.ma_attr.la_mode);
1083 RETURN(err_serious(-EOPNOTSUPP));
1086 rc = mdt_create(info);
1088 if ((info->mti_attr.ma_attr.la_mode & S_IFMT) == S_IFDIR)
1089 mdt_counter_incr(req, LPROC_MDT_MKDIR,
1090 ktime_us_delta(ktime_get(), kstart));
1092 /* Special file should stay on the same node as parent*/
1093 mdt_counter_incr(req, LPROC_MDT_MKNOD,
1094 ktime_us_delta(ktime_get(), kstart));
1101 * VBR: save parent version in reply and child version getting by its name.
1102 * Version of child is getting and checking during its lookup. If
1104 static int mdt_reint_unlink(struct mdt_thread_info *info,
1105 struct mdt_lock_handle *lhc)
1107 struct mdt_reint_record *rr = &info->mti_rr;
1108 struct ptlrpc_request *req = mdt_info_req(info);
1109 struct md_attr *ma = &info->mti_attr;
1110 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1111 struct mdt_object *mp;
1112 struct mdt_object *mc;
1113 struct mdt_lock_handle *parent_lh;
1114 struct mdt_lock_handle *child_lh;
1115 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
1117 bool cos_incompat = false;
1119 ktime_t kstart = ktime_get();
1123 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
1124 PNAME(&rr->rr_name));
1126 if (info->mti_dlm_req)
1127 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1129 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1130 RETURN(err_serious(-ENOENT));
1132 if (!fid_is_md_operative(rr->rr_fid1))
1135 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1137 RETURN(PTR_ERR(mp));
1139 if (mdt_object_remote(mp)) {
1140 cos_incompat = true;
1142 rc = mdt_version_get_check_save(info, mp, 0);
1144 GOTO(put_parent, rc);
1147 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
1148 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
1150 parent_lh = &info->mti_lh[MDT_LH_PARENT];
1151 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
1152 rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
1155 GOTO(put_parent, rc);
1157 if (info->mti_spec.sp_rm_entry) {
1158 struct lu_ucred *uc = mdt_ucred(info);
1160 if (!mdt_is_dne_client(req->rq_export))
1161 /* Return -ENOTSUPP for old client */
1162 GOTO(unlock_parent, rc = -ENOTSUPP);
1164 if (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN))
1165 GOTO(unlock_parent, rc = -EPERM);
1167 ma->ma_need = MA_INODE;
1169 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1170 NULL, &rr->rr_name, ma, no_name);
1171 GOTO(unlock_parent, rc);
1174 if (info->mti_spec.sp_cr_flags & MDS_OP_WITH_FID) {
1175 *child_fid = *rr->rr_fid2;
1177 /* lookup child object along with version checking */
1178 fid_zero(child_fid);
1179 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid,
1182 /* Name might not be able to find during resend of
1183 * remote unlink, considering following case.
1184 * dir_A is a remote directory, the name entry of
1185 * dir_A is on MDT0, the directory is on MDT1,
1187 * 1. client sends unlink req to MDT1.
1188 * 2. MDT1 sends name delete update to MDT0.
1189 * 3. name entry is being deleted in MDT0 synchronously.
1190 * 4. MDT1 is restarted.
1191 * 5. client resends unlink req to MDT1. So it can not
1192 * find the name entry on MDT0 anymore.
1193 * In this case, MDT1 only needs to destory the local
1196 if (mdt_object_remote(mp) && rc == -ENOENT &&
1197 !fid_is_zero(rr->rr_fid2) &&
1198 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1200 *child_fid = *rr->rr_fid2;
1202 GOTO(unlock_parent, rc);
1207 if (!fid_is_md_operative(child_fid))
1208 GOTO(unlock_parent, rc = -EPERM);
1210 /* We will lock the child regardless it is local or remote. No harm. */
1211 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1213 GOTO(unlock_parent, rc = PTR_ERR(mc));
1215 if (info->mti_spec.sp_cr_flags & MDS_OP_WITH_FID) {
1216 /* In this case, child fid is embedded in the request, and we do
1217 * not have a proper name as rr_name contains an encoded
1218 * hash. So find name that matches provided hash.
1220 if (!find_name_matching_hash(info, &rr->rr_name,
1222 GOTO(put_child, rc = -ENOENT);
1225 if (!cos_incompat) {
1226 rc = mdt_object_striped(info, mc);
1228 GOTO(put_child, rc);
1232 mdt_object_put(info->mti_env, mc);
1233 mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
1238 child_lh = &info->mti_lh[MDT_LH_CHILD];
1239 mdt_lock_reg_init(child_lh, LCK_EX);
1240 if (mdt_object_remote(mc)) {
1241 struct mdt_body *repbody;
1243 if (!fid_is_zero(rr->rr_fid2)) {
1244 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
1245 mdt_obd_name(info->mti_mdt),
1246 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1247 GOTO(put_child, rc = -ENOENT);
1249 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
1250 mdt_obd_name(info->mti_mdt),
1251 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1253 if (!mdt_is_dne_client(req->rq_export))
1254 /* Return -ENOTSUPP for old client */
1255 GOTO(put_child, rc = -ENOTSUPP);
1257 /* Revoke the LOOKUP lock of the remote object granted by
1258 * this MDT. Since the unlink will happen on another MDT,
1259 * it will release the LOOKUP lock right away. Then What
1260 * would happen if another client try to grab the LOOKUP
1261 * lock at the same time with unlink XXX
1263 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1264 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1265 LASSERT(repbody != NULL);
1266 repbody->mbo_fid1 = *mdt_object_fid(mc);
1267 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1268 GOTO(unlock_child, rc = -EREMOTE);
1270 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1271 * this now because a running HSM restore on the child (unlink
1272 * victim) will hold the layout lock. See LU-4002.
1274 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1275 if (mdt_object_remote(mp)) {
1276 /* Enqueue lookup lock from parent MDT */
1277 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1278 &child_lh->mlh_rreg_lh,
1279 child_lh->mlh_rreg_mode,
1280 MDS_INODELOCK_LOOKUP, false);
1282 GOTO(put_child, rc);
1284 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1287 rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
1290 GOTO(put_child, rc);
1293 * Now we can only make sure we need MA_INODE, in mdd layer, will check
1294 * whether need MA_LOV and MA_COOKIE.
1296 ma->ma_need = MA_INODE;
1299 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1300 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1301 /* save version when object is locked */
1302 mdt_version_get_save(info, mc, 1);
1304 mutex_lock(&mc->mot_lov_mutex);
1306 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1307 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1309 mutex_unlock(&mc->mot_lov_mutex);
1311 GOTO(unlock_child, rc);
1313 if (!lu_object_is_dying(&mc->mot_header)) {
1314 rc = mdt_attr_get_complex(info, mc, ma);
1317 } else if (mdt_dom_check_for_discard(info, mc)) {
1318 mdt_dom_discard_data(info, mc);
1320 mdt_handle_last_unlink(info, mc, ma);
1323 if (ma->ma_valid & MA_INODE) {
1324 switch (ma->ma_attr.la_mode & S_IFMT) {
1326 mdt_counter_incr(req, LPROC_MDT_RMDIR,
1327 ktime_us_delta(ktime_get(), kstart));
1335 mdt_counter_incr(req, LPROC_MDT_UNLINK,
1336 ktime_us_delta(ktime_get(), kstart));
1339 LASSERTF(0, "bad file type %o unlinking\n",
1340 ma->ma_attr.la_mode);
1347 mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
1349 if (info->mti_spec.sp_cr_flags & MDS_OP_WITH_FID &&
1350 info->mti_big_buf.lb_buf)
1351 lu_buf_free(&info->mti_big_buf);
1352 mdt_object_put(info->mti_env, mc);
1354 mdt_object_unlock(info, mp, parent_lh, rc);
1356 mdt_object_put(info->mti_env, mp);
1357 CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
1362 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1365 static int mdt_reint_link(struct mdt_thread_info *info,
1366 struct mdt_lock_handle *lhc)
1368 struct mdt_reint_record *rr = &info->mti_rr;
1369 struct ptlrpc_request *req = mdt_info_req(info);
1370 struct md_attr *ma = &info->mti_attr;
1371 struct mdt_object *ms;
1372 struct mdt_object *mp;
1373 struct mdt_lock_handle *lhs;
1374 struct mdt_lock_handle *lhp;
1375 ktime_t kstart = ktime_get();
1380 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1381 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1383 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1384 RETURN(err_serious(-ENOENT));
1386 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE) ||
1387 OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND)) {
1388 req->rq_no_reply = 1;
1389 RETURN(err_serious(-ENOENT));
1392 if (info->mti_dlm_req)
1393 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1395 /* Invalid case so return error immediately instead of
1398 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1401 if (!fid_is_md_operative(rr->rr_fid1) ||
1402 !fid_is_md_operative(rr->rr_fid2))
1405 /* step 1: find target parent dir */
1406 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1408 RETURN(PTR_ERR(mp));
1410 rc = mdt_version_get_check_save(info, mp, 0);
1412 GOTO(put_parent, rc);
1414 rc = mdt_check_enc(info, mp);
1416 GOTO(put_parent, rc);
1418 /* step 2: find source */
1419 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1421 GOTO(put_parent, rc = PTR_ERR(ms));
1423 if (!mdt_object_exists(ms)) {
1424 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1425 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1426 GOTO(put_source, rc = -ENOENT);
1429 cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1431 OBD_RACE(OBD_FAIL_MDS_LINK_RENAME_RACE);
1433 lhp = &info->mti_lh[MDT_LH_PARENT];
1434 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1435 rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1438 GOTO(put_source, rc);
1440 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1442 lhs = &info->mti_lh[MDT_LH_CHILD];
1443 mdt_lock_reg_init(lhs, LCK_EX);
1444 rc = mdt_reint_object_lock(info, ms, lhs,
1445 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1448 GOTO(unlock_parent, rc);
1450 /* step 3: link it */
1451 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1452 OBD_FAIL_MDS_REINT_LINK_WRITE);
1454 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1455 rc = mdt_version_get_check_save(info, ms, 1);
1457 GOTO(unlock_source, rc);
1459 /** check target version by name during replay */
1460 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1461 &info->mti_tmp_fid1, 2);
1462 if (rc != 0 && rc != -ENOENT)
1463 GOTO(unlock_source, rc);
1464 /* save version of file name for replay, it must be ENOENT here */
1465 if (!req_is_replay(mdt_info_req(info))) {
1466 if (rc != -ENOENT) {
1467 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1468 PNAME(&rr->rr_name));
1469 GOTO(unlock_source, rc = -EEXIST);
1471 info->mti_ver[2] = ENOENT_VERSION;
1472 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1475 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1476 mdt_object_child(ms), &rr->rr_name, ma);
1479 mdt_counter_incr(req, LPROC_MDT_LINK,
1480 ktime_us_delta(ktime_get(), kstart));
1484 mdt_object_unlock(info, ms, lhs, rc);
1486 mdt_object_unlock(info, mp, lhp, rc);
1488 mdt_object_put(info->mti_env, ms);
1490 mdt_object_put(info->mti_env, mp);
1494 * lock the part of the directory according to the hash of the name
1495 * (lh->mlh_pdo_hash) in parallel directory lock.
1497 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1498 struct mdt_lock_handle *lh,
1499 struct mdt_object *obj, __u64 ibits,
1502 struct ldlm_res_id *res = &info->mti_res_id;
1503 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1504 union ldlm_policy_data *policy = &info->mti_policy;
1505 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1509 * Finish res_id initializing by name hash marking part of
1510 * directory which is taking modification.
1512 LASSERT(lh->mlh_pdo_hash != 0);
1513 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1514 memset(policy, 0, sizeof(*policy));
1515 policy->l_inodebits.bits = ibits;
1517 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1518 dlmflags |= LDLM_FL_COS_INCOMPAT;
1520 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1521 * going to be sent to client. If it is - mdt_intent_policy() path will
1522 * fix it up and turn FL_LOCAL flag off.
1524 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
1525 policy, res, dlmflags,
1526 &info->mti_exp->exp_handle.h_cookie);
1531 * Get BFL lock for rename or migrate process.
1533 static int mdt_rename_lock(struct mdt_thread_info *info,
1534 struct lustre_handle *lh)
1539 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1540 struct lu_fid *fid = &info->mti_tmp_fid1;
1541 struct mdt_object *obj;
1543 /* XXX, right now, it has to use object API to
1544 * enqueue lock cross MDT, so it will enqueue
1545 * rename lock(with LUSTRE_BFL_FID) by root object
1548 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1550 RETURN(PTR_ERR(obj));
1552 rc = mdt_remote_object_lock(info, obj,
1553 &LUSTRE_BFL_FID, lh,
1555 MDS_INODELOCK_UPDATE, false);
1556 mdt_object_put(info->mti_env, obj);
1558 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1559 union ldlm_policy_data *policy = &info->mti_policy;
1560 struct ldlm_res_id *res_id = &info->mti_res_id;
1563 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1564 memset(policy, 0, sizeof(*policy));
1565 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1566 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1567 rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
1568 LDLM_IBITS, policy, LCK_EX, &flags,
1570 ldlm_completion_ast, NULL, NULL, 0,
1572 &info->mti_exp->exp_handle.h_cookie,
1579 static void mdt_rename_unlock(struct lustre_handle *lh)
1582 LASSERT(lustre_handle_is_used(lh));
1583 /* Cancel the single rename lock right away */
1584 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1588 static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
1589 const struct lu_fid *fid,
1592 struct mdt_object *dir;
1596 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1600 /* check early, the real version will be saved after locking */
1601 rc = mdt_version_get_check(info, dir, idx);
1605 if (!mdt_object_exists(dir))
1606 GOTO(out_put, rc = -ENOENT);
1608 if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
1609 GOTO(out_put, rc = -ENOTDIR);
1613 mdt_object_put(info->mti_env, dir);
1618 * in case obj is remote obj on its parent, revoke LOOKUP lock,
1619 * herein we don't really check it, just do revoke.
1621 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
1622 struct mdt_object *pobj,
1623 struct mdt_object *obj)
1625 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1628 mdt_lock_handle_init(lh);
1629 mdt_lock_reg_init(lh, LCK_EX);
1631 if (mdt_object_remote(pobj)) {
1632 /* don't bother to check if pobj and obj are on the same MDT. */
1633 rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
1634 &lh->mlh_rreg_lh, LCK_EX,
1635 MDS_INODELOCK_LOOKUP, false);
1636 } else if (mdt_object_remote(obj)) {
1637 struct ldlm_res_id *res = &info->mti_res_id;
1638 union ldlm_policy_data *policy = &info->mti_policy;
1639 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
1640 LDLM_FL_COS_INCOMPAT;
1642 fid_build_reg_res_name(mdt_object_fid(obj), res);
1643 memset(policy, 0, sizeof(*policy));
1644 policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
1645 rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
1646 &lh->mlh_reg_lh, LCK_EX, policy, res,
1649 /* do nothing if both are local */
1657 * TODO, currently we don't save this lock because there is no place to
1658 * hold this lock handle, but to avoid race we need to save this lock.
1660 mdt_object_unlock(info, NULL, lh, 1);
1666 * operation may takes locks of linkea, or directory stripes, group them in
1669 struct mdt_sub_lock {
1670 struct mdt_object *msl_obj;
1671 struct mdt_lock_handle msl_lh;
1672 struct list_head msl_linkage;
1675 static void mdt_unlock_list(struct mdt_thread_info *info,
1676 struct list_head *list, int decref)
1678 struct mdt_sub_lock *msl;
1679 struct mdt_sub_lock *tmp;
1681 list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
1682 mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
1683 list_del(&msl->msl_linkage);
1688 static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
1689 struct mdt_object *obj,
1690 struct mdt_lock_handle *lh,
1691 struct ldlm_enqueue_info *einfo,
1692 struct list_head *slave_locks,
1695 if (mdt_object_remote(obj)) {
1696 mdt_unlock_list(info, slave_locks, decref);
1697 mdt_object_unlock(info, obj, lh, decref);
1699 mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
1704 * lock parents of links, and also check whether total locks don't exceed
1707 * \retval 0 on success, and locks can be saved in ptlrpc_reply_stat
1708 * \retval 1 on success, but total lock count may exceed RS_MAX_LOCKS
1709 * \retval -ev negative errno upon error
1711 static int mdt_link_parents_lock(struct mdt_thread_info *info,
1712 struct mdt_object *pobj,
1713 const struct md_attr *ma,
1714 struct mdt_object *obj,
1715 struct mdt_lock_handle *lhp,
1716 struct ldlm_enqueue_info *peinfo,
1717 struct list_head *parent_slave_locks,
1718 struct list_head *link_locks)
1720 struct mdt_device *mdt = info->mti_mdt;
1721 struct lu_buf *buf = &info->mti_big_buf;
1722 struct lu_name *lname = &info->mti_name;
1723 struct linkea_data ldata = { NULL };
1724 bool blocked = false;
1725 int local_lnkp_cnt = 0;
1729 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1732 buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
1733 if (buf->lb_buf == NULL)
1737 rc = mdt_links_read(info, obj, &ldata);
1739 if (rc == -ENOENT || rc == -ENODATA)
1744 for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
1745 linkea_next_entry(&ldata)) {
1746 struct mdt_object *lnkp;
1747 struct mdt_sub_lock *msl;
1751 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
1754 /* check if it's also linked to parent */
1755 if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
1756 CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
1757 PFID(&fid), PNAME(lname));
1758 /* in case link is remote object, revoke LOOKUP lock */
1759 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1765 /* check if it's linked to a stripe of parent */
1766 if (ma->ma_valid & MA_LMV) {
1767 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1768 struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
1771 for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
1772 fid_le_to_cpu(stripe_fid,
1773 &lmv->lmv_stripe_fids[j]);
1774 if (lu_fid_eq(stripe_fid, &fid)) {
1775 CDEBUG(D_INFO, "skip stripe "DFID
1776 ", reovke "DNAME"\n",
1777 PFID(&fid), PNAME(lname));
1778 lnkp = mdt_object_find(info->mti_env,
1781 GOTO(out, rc = PTR_ERR(lnkp));
1787 rc = mdt_revoke_remote_lookup_lock(info, lnkp,
1789 mdt_object_put(info->mti_env, lnkp);
1794 /* Check if it's already locked */
1795 list_for_each_entry(msl, link_locks, msl_linkage) {
1796 if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
1798 DFID" was locked, revoke "DNAME"\n",
1799 PFID(&fid), PNAME(lname));
1800 lnkp = msl->msl_obj;
1806 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1810 CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
1811 PFID(&fid), PNAME(lname));
1813 lnkp = mdt_object_find(info->mti_env, mdt, &fid);
1815 CWARN("%s: cannot find obj "DFID": %ld\n",
1816 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
1820 if (!mdt_object_exists(lnkp)) {
1821 CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
1822 PFID(&fid), PNAME(lname));
1823 mdt_object_put(info->mti_env, lnkp);
1827 if (!mdt_object_remote(lnkp))
1832 GOTO(out, rc = -ENOMEM);
1835 * we can't follow parent-child lock order like other MD
1836 * operations, use lock_try here to avoid deadlock, if the lock
1837 * cannot be taken, drop all locks taken, revoke the blocked
1838 * one, and continue processing the remaining entries, and in
1839 * the end of the loop restart from beginning.
1841 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1843 rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
1844 MDS_INODELOCK_UPDATE, true);
1845 if (!(ibits & MDS_INODELOCK_UPDATE)) {
1847 CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
1848 PFID(&fid), PNAME(lname));
1850 mdt_unlock_list(info, link_locks, 1);
1851 /* also unlock parent locks to avoid deadlock */
1853 mdt_migrate_object_unlock(info, pobj, lhp,
1860 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1861 rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
1862 MDS_INODELOCK_UPDATE);
1864 mdt_object_put(info->mti_env, lnkp);
1869 if (mdt_object_remote(lnkp)) {
1870 struct ldlm_lock *lock;
1873 * for remote object, set lock cb_atomic,
1874 * so lock can be released in blocking_ast()
1875 * immediately, then the next lock_try will
1876 * have better chance of success.
1878 lock = ldlm_handle2lock(
1879 &msl->msl_lh.mlh_rreg_lh);
1880 LASSERT(lock != NULL);
1881 lock_res_and_lock(lock);
1882 ldlm_set_atomic_cb(lock);
1883 unlock_res_and_lock(lock);
1884 LDLM_LOCK_PUT(lock);
1887 mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
1892 INIT_LIST_HEAD(&msl->msl_linkage);
1893 msl->msl_obj = lnkp;
1894 list_add_tail(&msl->msl_linkage, link_locks);
1896 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1900 GOTO(out, rc = -EBUSY);
1905 mdt_unlock_list(info, link_locks, rc);
1906 } else if (local_lnkp_cnt > RS_MAX_LOCKS - 5) {
1907 CDEBUG(D_INFO, "Too many links (%d), sync operations\n",
1910 * parent may have 3 local objects: master object and 2 stripes
1911 * (if it's being migrated too); source may have 1 local objects
1912 * as regular file; target has 1 local object.
1913 * Note, source may have 2 local locks if it is directory but it
1914 * can't have hardlinks, so it is not considered here.
1921 static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
1922 struct mdt_object *obj,
1923 const struct md_attr *ma,
1924 struct list_head *slave_locks)
1926 struct mdt_device *mdt = info->mti_mdt;
1927 const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1928 struct lu_fid *fid = &info->mti_tmp_fid1;
1929 struct mdt_object *slave;
1930 struct mdt_sub_lock *msl;
1935 LASSERT(mdt_object_remote(obj));
1936 LASSERT(ma->ma_valid & MA_LMV);
1939 if (!lmv_is_sane(lmv))
1942 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
1943 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
1945 if (!fid_is_sane(fid))
1948 slave = mdt_object_find(info->mti_env, mdt, fid);
1950 GOTO(out, rc = PTR_ERR(slave));
1954 mdt_object_put(info->mti_env, slave);
1955 GOTO(out, rc = -ENOMEM);
1958 mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
1959 rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
1960 MDS_INODELOCK_UPDATE, true);
1963 mdt_object_put(info->mti_env, slave);
1967 INIT_LIST_HEAD(&msl->msl_linkage);
1968 msl->msl_obj = slave;
1969 list_add_tail(&msl->msl_linkage, slave_locks);
1975 mdt_unlock_list(info, slave_locks, rc);
1979 /* lock parent and its stripes */
1980 static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
1981 struct mdt_object *obj,
1982 const struct md_attr *ma,
1983 struct mdt_lock_handle *lh,
1984 struct ldlm_enqueue_info *einfo,
1985 struct list_head *slave_locks)
1989 if (mdt_object_remote(obj)) {
1990 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1991 &lh->mlh_rreg_lh, LCK_PW,
1992 MDS_INODELOCK_UPDATE, false);
1997 * if obj is remote and striped, lock its stripes explicitly
1998 * because it's not striped in LOD layer on this MDT.
2000 if (ma->ma_valid & MA_LMV) {
2001 rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
2003 mdt_object_unlock(info, obj, lh, rc);
2006 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
2014 * in migration, object may be remote, and we need take full lock of it and its
2015 * stripes if it's directory, besides, object may be a remote object on its
2016 * parent, revoke its LOOKUP lock on where its parent is located.
2018 static int mdt_migrate_object_lock(struct mdt_thread_info *info,
2019 struct mdt_object *pobj,
2020 struct mdt_object *obj,
2021 struct mdt_lock_handle *lh,
2022 struct ldlm_enqueue_info *einfo,
2023 struct list_head *slave_locks)
2027 if (mdt_object_remote(obj)) {
2028 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
2032 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
2033 &lh->mlh_rreg_lh, LCK_EX,
2034 MDS_INODELOCK_FULL, false);
2039 * if obj is remote and striped, lock its stripes explicitly
2040 * because it's not striped in LOD layer on this MDT.
2042 if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
2043 struct md_attr *ma = &info->mti_attr;
2045 rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
2047 mdt_object_unlock(info, obj, lh, rc);
2051 if (ma->ma_valid & MA_LMV) {
2052 rc = mdt_lock_remote_slaves(info, obj, ma,
2055 mdt_object_unlock(info, obj, lh, rc);
2059 if (mdt_object_remote(pobj)) {
2060 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
2065 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
2073 * lookup source by name, if parent is striped directory, we need to find the
2074 * corresponding stripe where source is located, and then lookup there.
2076 * besides, if parent is migrating too, and file is already in target stripe,
2077 * this should be a redo of 'lfs migrate' on client side.
2079 static int mdt_migrate_lookup(struct mdt_thread_info *info,
2080 struct mdt_object *pobj,
2081 const struct md_attr *ma,
2082 const struct lu_name *lname,
2083 struct mdt_object **spobj,
2084 struct mdt_object **sobj)
2086 const struct lu_env *env = info->mti_env;
2087 struct lu_fid *fid = &info->mti_tmp_fid1;
2088 struct mdt_object *stripe;
2091 if (ma->ma_valid & MA_LMV) {
2092 /* if parent is striped, lookup on corresponding stripe */
2093 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
2095 if (!lmv_is_sane(lmv))
2098 rc = lmv_name_to_stripe_index_old(lmv, lname->ln_name,
2103 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2105 stripe = mdt_object_find(env, info->mti_mdt, fid);
2107 return PTR_ERR(stripe);
2110 rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
2112 if (rc == -ENOENT && lmv_is_layout_changing(lmv)) {
2114 * if parent layout is changeing, and lookup child
2115 * failed on source stripe, lookup again on target
2116 * stripe, if it exists, it means previous migration
2117 * was interrupted, and current file was migrated
2120 mdt_object_put(env, stripe);
2122 rc = lmv_name_to_stripe_index(lmv, lname->ln_name,
2127 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2129 stripe = mdt_object_find(env, info->mti_mdt, fid);
2131 return PTR_ERR(stripe);
2134 rc = mdo_lookup(env, mdt_object_child(stripe), lname,
2135 fid, &info->mti_spec);
2136 mdt_object_put(env, stripe);
2137 return rc ?: -EALREADY;
2139 mdt_object_put(env, stripe);
2144 rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
2150 mdt_object_get(env, stripe);
2155 *sobj = mdt_object_find(env, info->mti_mdt, fid);
2156 if (IS_ERR(*sobj)) {
2157 mdt_object_put(env, stripe);
2158 rc = PTR_ERR(*sobj);
2166 /* end lease and close file for regular file */
2167 static int mdd_migrate_close(struct mdt_thread_info *info,
2168 struct mdt_object *obj)
2170 struct close_data *data;
2171 struct mdt_body *repbody;
2172 struct ldlm_lock *lease;
2177 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
2179 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
2183 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2188 lease = ldlm_handle2lock(&data->cd_handle);
2192 /* check if the lease was already canceled */
2193 lock_res_and_lock(lease);
2194 rc = ldlm_is_cancel(lease);
2195 unlock_res_and_lock(lease);
2199 LDLM_DEBUG(lease, DFID" lease broken",
2200 PFID(mdt_object_fid(obj)));
2204 * cancel server side lease, client side counterpart should have been
2205 * cancelled, it's okay to cancel it now as we've held mot_open_sem.
2207 ldlm_lock_cancel(lease);
2208 ldlm_reprocess_all(lease->l_resource,
2209 lease->l_policy_data.l_inodebits.bits);
2210 LDLM_LOCK_PUT(lease);
2213 rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
2214 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2215 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2221 * migrate file in below steps:
2222 * 1. lock parent and its stripes
2223 * 2. lookup source by name
2224 * 3. lock parents of source links if source is not directory
2225 * 4. reject if source is in HSM
2226 * 5. take source open_sem and close file if source is regular file
2227 * 6. lock source and its stripes if it's directory
2228 * 7. lock target so subsequent change to it can trigger COS
2230 * 9. unlock above locks
2231 * 10. sync device if source has links
2233 int mdt_reint_migrate(struct mdt_thread_info *info,
2234 struct mdt_lock_handle *unused)
2236 const struct lu_env *env = info->mti_env;
2237 struct mdt_device *mdt = info->mti_mdt;
2238 struct ptlrpc_request *req = mdt_info_req(info);
2239 struct mdt_reint_record *rr = &info->mti_rr;
2240 struct lu_ucred *uc = mdt_ucred(info);
2241 struct md_attr *ma = &info->mti_attr;
2242 struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
2243 struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
2244 struct mdt_object *pobj;
2245 struct mdt_object *spobj = NULL;
2246 struct mdt_object *sobj = NULL;
2247 struct mdt_object *tobj;
2248 struct lustre_handle rename_lh = { 0 };
2249 struct mdt_lock_handle *lhp;
2250 struct mdt_lock_handle *lhs;
2251 struct mdt_lock_handle *lht;
2252 LIST_HEAD(parent_slave_locks);
2253 LIST_HEAD(child_slave_locks);
2254 LIST_HEAD(link_locks);
2255 int lock_retries = 5;
2256 bool open_sem_locked = false;
2257 bool do_sync = false;
2261 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
2262 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
2264 if (info->mti_dlm_req)
2265 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2267 if (!fid_is_md_operative(rr->rr_fid1) ||
2268 !fid_is_md_operative(rr->rr_fid2))
2271 /* don't allow migrate . or .. */
2272 if (lu_name_is_dot_or_dotdot(&rr->rr_name))
2275 if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
2278 /* we want rbac roles to have precedence over any other
2279 * permission or capability checks
2281 if (uc && (!uc->uc_rbac_dne_ops ||
2282 (!cap_raised(uc->uc_cap, CAP_SYS_ADMIN) &&
2283 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
2284 mdt->mdt_enable_remote_dir_gid != -1)))
2288 * Note: do not enqueue rename lock for replay request, because
2289 * if other MDT holds rename lock, but being blocked to wait for
2290 * this MDT to finish its recovery, and the failover MDT can not
2291 * get rename lock, which will cause deadlock.
2293 * req is NULL if this is called by directory auto-split.
2295 if (req && !req_is_replay(req)) {
2296 rc = mdt_rename_lock(info, &rename_lh);
2298 CERROR("%s: can't lock FS for rename: rc = %d\n",
2299 mdt_obd_name(info->mti_mdt), rc);
2304 /* pobj is master object of parent */
2305 pobj = mdt_object_find(env, mdt, rr->rr_fid1);
2307 GOTO(unlock_rename, rc = PTR_ERR(pobj));
2310 rc = mdt_version_get_check(info, pobj, 0);
2312 GOTO(put_parent, rc);
2315 if (!mdt_object_exists(pobj))
2316 GOTO(put_parent, rc = -ENOENT);
2318 if (!S_ISDIR(lu_object_attr(&pobj->mot_obj)))
2319 GOTO(put_parent, rc = -ENOTDIR);
2321 rc = mdt_check_enc(info, pobj);
2323 GOTO(put_parent, rc);
2325 rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
2327 GOTO(put_parent, rc);
2330 /* lock parent object */
2331 lhp = &info->mti_lh[MDT_LH_PARENT];
2332 mdt_lock_reg_init(lhp, LCK_PW);
2333 rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
2334 &parent_slave_locks);
2336 GOTO(put_parent, rc);
2339 * spobj is the corresponding stripe against name if pobj is striped
2340 * directory, which is the real parent, and no need to lock, because
2341 * we've taken full lock of pobj.
2343 rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
2345 GOTO(unlock_parent, rc);
2347 /* lock parents of source links, and revoke LOOKUP lock of links */
2348 rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
2349 &parent_slave_locks, &link_locks);
2350 if (rc == -EBUSY && lock_retries-- > 0) {
2351 mdt_object_put(env, sobj);
2352 mdt_object_put(env, spobj);
2357 GOTO(put_source, rc);
2360 * RS_MAX_LOCKS is the limit of number of locks that can be saved along
2361 * with one request, if total lock count exceeds this limit, we will
2362 * drop all locks after migration, and synchronous device in the end.
2366 /* TODO: DoM migration is not supported, migrate dirent only */
2367 if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
2368 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
2370 GOTO(unlock_links, rc);
2372 if (ma->ma_valid & MA_LOV && mdt_lmm_dom_stripesize(ma->ma_lmm))
2373 info->mti_spec.sp_migrate_nsonly = 1;
2374 } else if (S_ISDIR(lu_object_attr(&sobj->mot_obj))) {
2375 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2377 GOTO(unlock_links, rc);
2379 /* race with restripe/auto-split? */
2380 if ((ma->ma_valid & MA_LMV) &&
2381 lmv_is_restriping(&ma->ma_lmv->lmv_md_v1))
2382 GOTO(unlock_links, rc = -EBUSY);
2385 /* if migration HSM is allowed */
2386 if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
2387 ma->ma_need = MA_HSM;
2389 rc = mdt_attr_get_complex(info, sobj, ma);
2391 GOTO(unlock_links, rc);
2393 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
2394 GOTO(unlock_links, rc = -EOPNOTSUPP);
2397 /* end lease and close file for regular file */
2398 if (info->mti_spec.sp_migrate_close) {
2399 /* try to hold open_sem so that nobody else can open the file */
2400 if (!down_write_trylock(&sobj->mot_open_sem)) {
2402 mdd_migrate_close(info, sobj);
2403 GOTO(unlock_links, rc = -EBUSY);
2405 open_sem_locked = true;
2406 rc = mdd_migrate_close(info, sobj);
2408 GOTO(unlock_open_sem, rc);
2413 lhs = &info->mti_lh[MDT_LH_OLD];
2414 mdt_lock_reg_init(lhs, LCK_EX);
2415 rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
2416 &child_slave_locks);
2418 GOTO(unlock_open_sem, rc);
2421 tobj = mdt_object_find(env, mdt, rr->rr_fid2);
2423 GOTO(unlock_source, rc = PTR_ERR(tobj));
2425 lht = &info->mti_lh[MDT_LH_NEW];
2426 mdt_lock_reg_init(lht, LCK_EX);
2427 rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
2429 GOTO(put_target, rc);
2431 /* Don't do lookup sanity check. We know name doesn't exist. */
2432 info->mti_spec.sp_cr_lookup = 0;
2433 info->mti_spec.sp_feat = &dt_directory_features;
2435 rc = mdo_migrate(env, mdt_object_child(pobj),
2436 mdt_object_child(sobj), &rr->rr_name,
2437 mdt_object_child(tobj),
2438 &info->mti_spec, ma);
2440 lprocfs_counter_incr(mdt->mdt_lu_dev.ld_obd->obd_md_stats,
2441 LPROC_MDT_MIGRATE + LPROC_MD_LAST_OPC);
2444 mdt_object_unlock(info, tobj, lht, rc);
2446 mdt_object_put(env, tobj);
2448 mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
2449 &child_slave_locks, rc);
2451 if (open_sem_locked)
2452 up_write(&sobj->mot_open_sem);
2454 /* if we've got too many locks to save into RPC,
2455 * then just commit before the locks are released
2458 mdt_device_sync(env, mdt);
2459 mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
2461 mdt_object_put(env, sobj);
2462 mdt_object_put(env, spobj);
2464 mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
2465 &parent_slave_locks, rc);
2467 mdt_object_put(env, pobj);
2469 if (lustre_handle_is_used(&rename_lh))
2470 mdt_rename_unlock(&rename_lh);
2475 static int mdt_object_lock_save(struct mdt_thread_info *info,
2476 struct mdt_object *dir,
2477 struct mdt_lock_handle *lh,
2478 int idx, bool cos_incompat)
2482 /* we lock the target dir if it is local */
2483 rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
2488 /* get and save correct version after locking */
2489 mdt_version_get_save(info, dir, idx);
2494 * determine lock order of sobj and tobj
2496 * there are two situations we need to lock tobj before sobj:
2497 * 1. sobj is child of tobj
2498 * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
2499 * larger than that of tobj
2501 * \retval 1 lock tobj before sobj
2502 * \retval 0 lock sobj before tobj
2503 * \retval -ev negative errno upon error
2505 static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
2506 struct mdt_object *sobj,
2507 struct mdt_object *tobj)
2509 struct md_attr *ma = &info->mti_attr;
2510 struct lu_fid *spfid = &info->mti_tmp_fid1;
2511 struct lu_fid *tpfid = &info->mti_tmp_fid2;
2512 struct lmv_mds_md_v1 *lmv;
2517 /* sobj and tobj are the same */
2521 if (fid_is_root(mdt_object_fid(sobj)))
2524 if (fid_is_root(mdt_object_fid(tobj)))
2527 /* check whether sobj is child of tobj */
2528 rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
2529 mdt_object_fid(tobj));
2536 /* check whether sobj and tobj are children of the same parent */
2537 rc = mdt_attr_get_pfid(info, sobj, spfid);
2541 rc = mdt_attr_get_pfid(info, tobj, tpfid);
2545 if (!lu_fid_eq(spfid, tpfid))
2548 /* check whether sobj and tobj are sibling stripes */
2549 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2553 if (!(ma->ma_valid & MA_LMV))
2556 lmv = &ma->ma_lmv->lmv_md_v1;
2557 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2559 sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2562 rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
2566 if (!(ma->ma_valid & MA_LMV))
2569 lmv = &ma->ma_lmv->lmv_md_v1;
2570 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2572 tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2574 /* check stripe index of sobj and tobj */
2575 if (sindex == tindex)
2578 return sindex < tindex ? 0 : 1;
2582 * lock rename source object.
2584 * Both source and source parent may be remote, and source may be a remote
2585 * object on source parent, to avoid overriding lock handle, store remote
2586 * LOOKUP lock separately in @lhr.
2588 * \retval 0 on success
2589 * \retval -ev negative errno upon error
2591 static int mdt_rename_source_lock(struct mdt_thread_info *info,
2592 struct mdt_object *parent,
2593 struct mdt_object *child,
2594 struct mdt_lock_handle *lhc,
2595 struct mdt_lock_handle *lhr,
2601 rc = mdt_is_remote_object(info, parent, child);
2606 /* enqueue remote LOOKUP lock from the parent MDT */
2607 __u64 rmt_ibits = MDS_INODELOCK_LOOKUP;
2609 if (mdt_object_remote(parent)) {
2610 rc = mdt_remote_object_lock(info, parent,
2611 mdt_object_fid(child),
2618 LASSERT(mdt_object_remote(child));
2619 rc = mdt_object_local_lock(info, child, lhr,
2620 &rmt_ibits, 0, true);
2625 ibits &= ~MDS_INODELOCK_LOOKUP;
2628 if (mdt_object_remote(child)) {
2629 rc = mdt_remote_object_lock(info, child, mdt_object_fid(child),
2636 rc = mdt_reint_object_lock(info, child, lhc, ibits,
2641 mdt_object_unlock(info, child, lhr, rc);
2646 /* Helper function for mdt_reint_rename so we don't need to opencode
2647 * two different order lockings
2649 static int mdt_lock_two_dirs(struct mdt_thread_info *info,
2650 struct mdt_object *mfirstdir,
2651 struct mdt_lock_handle *lh_firstdirp,
2652 struct mdt_object *mseconddir,
2653 struct mdt_lock_handle *lh_seconddirp,
2658 rc = mdt_object_lock_save(info, mfirstdir, lh_firstdirp, 0,
2663 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2665 if (mfirstdir != mseconddir) {
2666 rc = mdt_object_lock_save(info, mseconddir, lh_seconddirp, 1,
2668 } else if (!mdt_object_remote(mseconddir) &&
2669 lh_firstdirp->mlh_pdo_hash !=
2670 lh_seconddirp->mlh_pdo_hash) {
2671 rc = mdt_pdir_hash_lock(info, lh_seconddirp, mseconddir,
2672 MDS_INODELOCK_UPDATE,
2674 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
2678 mdt_object_unlock(info, mfirstdir, lh_firstdirp, rc);
2684 * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
2685 * 2 - srcdir child; 3 - tgtdir child.
2686 * Update on disk version of srcdir child.
2688 static int mdt_reint_rename(struct mdt_thread_info *info,
2689 struct mdt_lock_handle *unused)
2691 struct mdt_device *mdt = info->mti_mdt;
2692 struct mdt_reint_record *rr = &info->mti_rr;
2693 struct md_attr *ma = &info->mti_attr;
2694 struct ptlrpc_request *req = mdt_info_req(info);
2695 struct mdt_object *msrcdir = NULL;
2696 struct mdt_object *mtgtdir = NULL;
2697 struct mdt_object *mold;
2698 struct mdt_object *mnew = NULL;
2699 struct lustre_handle rename_lh = { 0 };
2700 struct mdt_lock_handle *lh_srcdirp;
2701 struct mdt_lock_handle *lh_tgtdirp;
2702 struct mdt_lock_handle *lh_oldp = NULL;
2703 struct mdt_lock_handle *lh_rmt = NULL;
2704 struct mdt_lock_handle *lh_newp = NULL;
2705 struct lu_fid *old_fid = &info->mti_tmp_fid1;
2706 struct lu_fid *new_fid = &info->mti_tmp_fid2;
2708 bool reverse = false, discard = false;
2710 ktime_t kstart = ktime_get();
2711 enum mdt_stat_idx msi = 0;
2715 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
2716 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
2717 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
2719 if (info->mti_dlm_req)
2720 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2722 if (!fid_is_md_operative(rr->rr_fid1) ||
2723 !fid_is_md_operative(rr->rr_fid2))
2726 /* find both parents. */
2727 msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
2728 if (IS_ERR(msrcdir))
2729 RETURN(PTR_ERR(msrcdir));
2731 rc = mdt_check_enc(info, msrcdir);
2733 GOTO(out_put_srcdir, rc);
2735 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
2737 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
2739 mdt_object_get(info->mti_env, mtgtdir);
2741 mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
2742 if (IS_ERR(mtgtdir))
2743 GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
2746 rc = mdt_check_enc(info, mtgtdir);
2748 GOTO(out_put_tgtdir, rc);
2751 * Note: do not enqueue rename lock for replay request, because
2752 * if other MDT holds rename lock, but being blocked to wait for
2753 * this MDT to finish its recovery, and the failover MDT can not
2754 * get rename lock, which will cause deadlock.
2756 if (!req_is_replay(req)) {
2757 bool remote = mdt_object_remote(msrcdir);
2760 * Normally rename RPC is handled on the MDT with the target
2761 * directory (if target exists, it's on the MDT with the
2762 * target), if the source directory is remote, it's a hint that
2763 * source is remote too (this may not be true, but it won't
2764 * cause any issue), return -EXDEV early to avoid taking
2767 if (!mdt->mdt_enable_remote_rename && remote)
2768 GOTO(out_put_tgtdir, rc = -EXDEV);
2770 /* This might be further relaxed in the future for regular file
2771 * renames in different source and target parents. Start with
2772 * only same-directory renames for simplicity and because this
2773 * is by far the most the common use case.
2775 * Striped directories should be considered "remote".
2777 if (msrcdir != mtgtdir || remote ||
2778 (S_ISDIR(ma->ma_attr.la_mode) &&
2779 !mdt->mdt_enable_parallel_rename_dir) ||
2780 (!S_ISDIR(ma->ma_attr.la_mode) &&
2781 !mdt->mdt_enable_parallel_rename_file)) {
2782 rc = mdt_rename_lock(info, &rename_lh);
2784 CERROR("%s: cannot lock for rename: rc = %d\n",
2785 mdt_obd_name(mdt), rc);
2786 GOTO(out_put_tgtdir, rc);
2789 if (S_ISDIR(ma->ma_attr.la_mode))
2790 msi = LPROC_MDT_RENAME_PAR_DIR;
2792 msi = LPROC_MDT_RENAME_PAR_FILE;
2795 "%s: samedir parallel rename "DFID"/"DNAME"\n",
2796 mdt_obd_name(mdt), PFID(rr->rr_fid1),
2797 PNAME(&rr->rr_name));
2801 rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
2803 GOTO(out_unlock_rename, rc);
2806 /* source needs to be looked up after locking source parent, otherwise
2807 * this rename may race with unlink source, and cause rename hang, see
2808 * sanityn.sh 55b, so check parents first, if later we found source is
2809 * remote, relock parents.
2811 cos_incompat = (mdt_object_remote(msrcdir) ||
2812 mdt_object_remote(mtgtdir));
2814 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2816 /* lock parents in the proper order. */
2817 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
2818 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
2820 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN);
2821 OBD_RACE(OBD_FAIL_MDS_REINT_OPEN2);
2823 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
2824 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
2826 /* In case of same dir local rename we must sort by the hash,
2827 * otherwise a lock deadlock is possible when renaming
2828 * a to b and b to a at the same time LU-15285
2830 if (!mdt_object_remote(mtgtdir) && mtgtdir == msrcdir)
2831 reverse = lh_srcdirp->mlh_pdo_hash > lh_tgtdirp->mlh_pdo_hash;
2832 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)))
2836 rc = mdt_lock_two_dirs(info, mtgtdir, lh_tgtdirp, msrcdir,
2837 lh_srcdirp, cos_incompat);
2839 rc = mdt_lock_two_dirs(info, msrcdir, lh_srcdirp, mtgtdir,
2840 lh_tgtdirp, cos_incompat);
2843 GOTO(out_unlock_rename, rc);
2845 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2846 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
2848 /* find mold object. */
2850 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
2852 GOTO(out_unlock_parents, rc);
2854 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
2855 GOTO(out_unlock_parents, rc = -EINVAL);
2857 if (!fid_is_md_operative(old_fid))
2858 GOTO(out_unlock_parents, rc = -EPERM);
2860 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
2862 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
2864 if (!mdt_object_exists(mold)) {
2865 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2867 "object does not exist");
2868 GOTO(out_put_old, rc = -ENOENT);
2871 if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
2872 GOTO(out_put_old, rc = -EXDEV);
2874 /* Check if @mtgtdir is subdir of @mold, before locking child
2875 * to avoid reverse locking.
2877 if (mtgtdir != msrcdir) {
2878 rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
2883 GOTO(out_put_old, rc);
2887 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
2888 /* save version after locking */
2889 mdt_version_get_save(info, mold, 2);
2891 if (!cos_incompat && mdt_object_remote(mold)) {
2892 cos_incompat = true;
2893 mdt_object_put(info->mti_env, mold);
2894 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
2895 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
2899 /* find mnew object:
2900 * mnew target object may not exist now
2901 * lookup with version checking
2904 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
2907 /* the new_fid should have been filled at this moment */
2908 if (lu_fid_eq(old_fid, new_fid))
2909 GOTO(out_put_old, rc);
2911 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
2912 lu_fid_eq(new_fid, rr->rr_fid2))
2913 GOTO(out_put_old, rc = -EINVAL);
2915 if (!fid_is_md_operative(new_fid))
2916 GOTO(out_put_old, rc = -EPERM);
2918 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
2920 GOTO(out_put_old, rc = PTR_ERR(mnew));
2922 if (!mdt_object_exists(mnew)) {
2923 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2925 "object does not exist");
2926 GOTO(out_put_new, rc = -ENOENT);
2929 if (mdt_object_remote(mnew)) {
2930 struct mdt_body *repbody;
2932 /* Always send rename req to the target child MDT */
2933 repbody = req_capsule_server_get(info->mti_pill,
2935 LASSERT(repbody != NULL);
2936 repbody->mbo_fid1 = *new_fid;
2937 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2938 GOTO(out_put_new, rc = -EXDEV);
2940 /* Before locking the target dir, check we do not replace
2941 * a dir with a non-dir, otherwise it may deadlock with
2942 * link op which tries to create a link in this dir
2943 * back to this non-dir.
2945 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2946 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2947 GOTO(out_put_new, rc = -EISDIR);
2949 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2950 lh_rmt = &info->mti_lh[MDT_LH_RMT];
2951 mdt_lock_reg_init(lh_oldp, LCK_EX);
2952 mdt_lock_reg_init(lh_rmt, LCK_EX);
2953 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2954 rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
2955 lh_rmt, lock_ibits, cos_incompat);
2957 GOTO(out_put_new, rc);
2959 /* Check if @msrcdir is subdir of @mnew, before locking child
2960 * to avoid reverse locking.
2962 if (mtgtdir != msrcdir) {
2963 rc = mdo_is_subdir(info->mti_env,
2964 mdt_object_child(msrcdir), new_fid);
2968 GOTO(out_unlock_old, rc);
2972 /* We used to acquire MDS_INODELOCK_FULL here but we
2973 * can't do this now because a running HSM restore on
2974 * the rename onto victim will hold the layout
2975 * lock. See LU-4002.
2978 lh_newp = &info->mti_lh[MDT_LH_NEW];
2979 mdt_lock_reg_init(lh_newp, LCK_EX);
2980 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
2981 if (mdt_object_remote(mtgtdir)) {
2982 rc = mdt_remote_object_lock(info, mtgtdir,
2983 mdt_object_fid(mnew),
2984 &lh_newp->mlh_rreg_lh,
2985 lh_newp->mlh_rreg_mode,
2986 MDS_INODELOCK_LOOKUP,
2989 GOTO(out_unlock_old, rc);
2991 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2993 rc = mdt_reint_object_lock(info, mnew, lh_newp, lock_ibits,
2996 GOTO(out_unlock_new, rc);
2998 /* get and save version after locking */
2999 mdt_version_get_save(info, mnew, 3);
3000 } else if (rc != -ENOENT) {
3001 GOTO(out_put_old, rc);
3003 lh_oldp = &info->mti_lh[MDT_LH_OLD];
3004 lh_rmt = &info->mti_lh[MDT_LH_RMT];
3005 mdt_lock_reg_init(lh_oldp, LCK_EX);
3006 mdt_lock_reg_init(lh_rmt, LCK_EX);
3007 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
3008 rc = mdt_rename_source_lock(info, msrcdir, mold, lh_oldp,
3009 lh_rmt, lock_ibits, cos_incompat);
3011 GOTO(out_put_old, rc);
3013 mdt_enoent_version_save(info, 3);
3016 /* step 5: rename it */
3017 mdt_reint_init_ma(info, ma);
3019 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
3020 OBD_FAIL_MDS_REINT_RENAME_WRITE);
3023 mutex_lock(&mnew->mot_lov_mutex);
3025 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
3026 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
3027 mnew != NULL ? mdt_object_child(mnew) : NULL,
3028 &rr->rr_tgt_name, ma);
3031 mutex_unlock(&mnew->mot_lov_mutex);
3033 /* handle last link of tgt object */
3036 mdt_handle_last_unlink(info, mnew, ma);
3037 discard = mdt_dom_check_for_discard(info, mnew);
3039 mdt_rename_counter_tally(info, info->mti_mdt, req,
3040 msrcdir, mtgtdir, msi,
3041 ktime_us_delta(ktime_get(), kstart));
3047 mdt_object_unlock(info, mnew, lh_newp, rc);
3049 mdt_object_unlock(info, NULL, lh_rmt, rc);
3050 mdt_object_unlock(info, mold, lh_oldp, rc);
3052 if (mnew && !discard)
3053 mdt_object_put(info->mti_env, mnew);
3055 mdt_object_put(info->mti_env, mold);
3057 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
3058 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
3060 if (lustre_handle_is_used(&rename_lh))
3061 mdt_rename_unlock(&rename_lh);
3063 mdt_object_put(info->mti_env, mtgtdir);
3065 mdt_object_put(info->mti_env, msrcdir);
3067 /* The DoM discard can be done right in the place above where it is
3068 * assigned, meanwhile it is done here after rename unlock due to
3069 * compatibility with old clients, for them the discard blocks
3070 * the main thread until completion. Check LU-11359 for details.
3073 mdt_dom_discard_data(info, mnew);
3074 mdt_object_put(info->mti_env, mnew);
3076 OBD_RACE(OBD_FAIL_MDS_LINK_RENAME_RACE);
3080 static int mdt_reint_resync(struct mdt_thread_info *info,
3081 struct mdt_lock_handle *lhc)
3083 struct mdt_reint_record *rr = &info->mti_rr;
3084 struct ptlrpc_request *req = mdt_info_req(info);
3085 struct md_attr *ma = &info->mti_attr;
3086 struct mdt_object *mo;
3087 struct ldlm_lock *lease;
3088 struct mdt_body *repbody;
3089 struct md_layout_change layout = { .mlc_mirror_id = rr->rr_mirror_id };
3094 DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
3096 if (info->mti_dlm_req)
3097 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
3099 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
3101 GOTO(out, rc = PTR_ERR(mo));
3103 if (!mdt_object_exists(mo))
3104 GOTO(out_obj, rc = -ENOENT);
3106 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
3107 GOTO(out_obj, rc = -EINVAL);
3109 if (mdt_object_remote(mo))
3110 GOTO(out_obj, rc = -EREMOTE);
3112 lease = ldlm_handle2lock(rr->rr_lease_handle);
3114 GOTO(out_obj, rc = -ESTALE);
3116 /* It's really necessary to grab open_sem and check if the lease lock
3117 * has been lost. There would exist a concurrent writer coming in and
3118 * generating some dirty data in memory cache, the writeback would fail
3119 * after the layout version is increased by MDS_REINT_RESYNC RPC.
3121 if (!down_write_trylock(&mo->mot_open_sem))
3122 GOTO(out_put_lease, rc = -EBUSY);
3124 lock_res_and_lock(lease);
3125 lease_broken = ldlm_is_cancel(lease);
3126 unlock_res_and_lock(lease);
3128 GOTO(out_unlock, rc = -EBUSY);
3130 /* the file has yet opened by anyone else after we took the lease. */
3131 layout.mlc_opc = MD_LAYOUT_RESYNC;
3132 lhc = &info->mti_lh[MDT_LH_LOCAL];
3133 rc = mdt_layout_change(info, mo, lhc, &layout);
3135 GOTO(out_unlock, rc);
3137 mdt_object_unlock(info, mo, lhc, 0);
3139 ma->ma_need = MA_INODE;
3141 rc = mdt_attr_get_complex(info, mo, ma);
3143 GOTO(out_unlock, rc);
3145 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3146 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
3150 up_write(&mo->mot_open_sem);
3152 LDLM_LOCK_PUT(lease);
3154 mdt_object_put(info->mti_env, mo);
3156 mdt_client_compatibility(info);
3160 struct mdt_reinter {
3161 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
3162 enum lprocfs_extra_opc mr_extra_opc;
3165 static const struct mdt_reinter mdt_reinters[] = {
3167 .mr_handler = &mdt_reint_setattr,
3168 .mr_extra_opc = MDS_REINT_SETATTR,
3171 .mr_handler = &mdt_reint_create,
3172 .mr_extra_opc = MDS_REINT_CREATE,
3175 .mr_handler = &mdt_reint_link,
3176 .mr_extra_opc = MDS_REINT_LINK,
3179 .mr_handler = &mdt_reint_unlink,
3180 .mr_extra_opc = MDS_REINT_UNLINK,
3183 .mr_handler = &mdt_reint_rename,
3184 .mr_extra_opc = MDS_REINT_RENAME,
3187 .mr_handler = &mdt_reint_open,
3188 .mr_extra_opc = MDS_REINT_OPEN,
3190 [REINT_SETXATTR] = {
3191 .mr_handler = &mdt_reint_setxattr,
3192 .mr_extra_opc = MDS_REINT_SETXATTR,
3195 .mr_handler = &mdt_reint_unlink,
3196 .mr_extra_opc = MDS_REINT_UNLINK,
3199 .mr_handler = &mdt_reint_migrate,
3200 .mr_extra_opc = MDS_REINT_RENAME,
3203 .mr_handler = &mdt_reint_resync,
3204 .mr_extra_opc = MDS_REINT_RESYNC,
3208 int mdt_reint_rec(struct mdt_thread_info *info,
3209 struct mdt_lock_handle *lhc)
3211 const struct mdt_reinter *mr;
3215 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
3218 mr = &mdt_reinters[info->mti_rr.rr_opcode];
3219 if (mr->mr_handler == NULL)
3222 rc = (*mr->mr_handler)(info, lhc);
3224 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
3225 PTLRPC_LAST_CNTR + mr->mr_extra_opc);