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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_reint.c
34 * Lustre Metadata Target (mdt) reintegration routines
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Huang Hua <huanghua@clusterfs.com>
40 * Author: Yury Umanets <umka@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <lprocfs_status.h>
46 #include "mdt_internal.h"
47 #include <lustre_lmv.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 static 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;
209 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
210 if (likely(!IS_ERR(child))) {
211 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
212 mdt_object_put(info->mti_env, child);
215 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
216 return vbrc ? vbrc : rc;
220 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
221 struct mdt_object *obj,
222 struct ldlm_enqueue_info *einfo,
225 union ldlm_policy_data *policy = &mti->mti_policy;
226 struct mdt_lock_handle *lh = &mti->mti_lh[MDT_LH_LOCAL];
227 struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
230 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
231 LASSERT(slave_locks);
233 memset(policy, 0, sizeof(*policy));
234 policy->l_inodebits.bits = einfo->ei_inodebits;
235 mdt_lock_handle_init(lh);
236 mdt_lock_reg_init(lh, einfo->ei_mode);
237 for (i = 0; i < slave_locks->ha_count; i++) {
238 if (test_bit(i, (void *)slave_locks->ha_map))
239 lh->mlh_rreg_lh = slave_locks->ha_handles[i];
241 lh->mlh_reg_lh = slave_locks->ha_handles[i];
242 mdt_object_unlock(mti, NULL, lh, decref);
243 slave_locks->ha_handles[i].cookie = 0ull;
246 return mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
250 static inline int mdt_object_striped(struct mdt_thread_info *mti,
251 struct mdt_object *obj)
253 struct lu_device *bottom_dev;
254 struct lu_object *bottom_obj;
257 if (!S_ISDIR(obj->mot_header.loh_attr))
260 /* getxattr from bottom obj to avoid reading in shard FIDs */
261 bottom_dev = dt2lu_dev(mti->mti_mdt->mdt_bottom);
262 bottom_obj = lu_object_find_slice(mti->mti_env, bottom_dev,
263 mdt_object_fid(obj), NULL);
264 if (IS_ERR(bottom_obj))
265 return PTR_ERR(bottom_obj);
267 rc = dt_xattr_get(mti->mti_env, lu2dt(bottom_obj), &LU_BUF_NULL,
269 lu_object_put(mti->mti_env, bottom_obj);
271 return (rc > 0) ? 1 : (rc == -ENODATA) ? 0 : rc;
275 * Lock slave stripes if necessary, the lock handles of slave stripes
276 * will be stored in einfo->ei_cbdata.
278 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
279 enum ldlm_mode mode, __u64 ibits,
280 struct ldlm_enqueue_info *einfo)
282 union ldlm_policy_data *policy = &mti->mti_policy;
284 LASSERT(S_ISDIR(obj->mot_header.loh_attr));
286 einfo->ei_type = LDLM_IBITS;
287 einfo->ei_mode = mode;
288 einfo->ei_cb_bl = mdt_remote_blocking_ast;
289 einfo->ei_cb_local_bl = mdt_blocking_ast;
290 einfo->ei_cb_cp = ldlm_completion_ast;
291 einfo->ei_enq_slave = 1;
292 einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
293 einfo->ei_inodebits = ibits;
294 memset(policy, 0, sizeof(*policy));
295 policy->l_inodebits.bits = ibits;
297 return mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
301 int mdt_reint_striped_lock(struct mdt_thread_info *info,
302 struct mdt_object *o,
303 struct mdt_lock_handle *lh,
305 struct ldlm_enqueue_info *einfo,
310 LASSERT(!mdt_object_remote(o));
312 memset(einfo, 0, sizeof(*einfo));
314 rc = mdt_reint_object_lock(info, o, lh, ibits, cos_incompat);
318 rc = mdt_object_striped(info, o);
321 mdt_object_unlock(info, o, lh, rc);
325 rc = mdt_lock_slaves(info, o, lh->mlh_reg_mode, ibits, einfo);
327 mdt_object_unlock(info, o, lh, rc);
328 if (rc == -EIO && OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME))
335 void mdt_reint_striped_unlock(struct mdt_thread_info *info,
336 struct mdt_object *o,
337 struct mdt_lock_handle *lh,
338 struct ldlm_enqueue_info *einfo, int decref)
340 if (einfo->ei_cbdata)
341 mdt_unlock_slaves(info, o, einfo, decref);
342 mdt_object_unlock(info, o, lh, decref);
345 static int mdt_restripe(struct mdt_thread_info *info,
346 struct mdt_object *pobj,
347 const struct lu_name *lname,
348 const struct lu_fid *tfid,
349 struct md_op_spec *spec,
352 const struct lu_env *env = info->mti_env;
353 struct mdt_device *mdt = info->mti_mdt;
354 struct lu_fid *cfid = &info->mti_tmp_fid2;
355 struct lmv_user_md *lum = spec->u.sp_ea.eadata;
356 struct md_layout_change *mlc = &info->mti_mlc;
357 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
358 struct lmv_mds_md_v1 *lmv;
359 struct mdt_object *child;
360 struct mdt_object *tobj = NULL;
361 struct mdt_lock_handle *lhp = NULL;
362 struct mdt_lock_handle *lhc;
363 struct mdt_body *repbody;
364 u32 lmv_stripe_count = 0;
369 if (!mdt->mdt_enable_dir_restripe)
372 /* mti_big_lmm is used to save LMV, but it may be uninitialized. */
373 if (unlikely(!info->mti_big_lmm)) {
374 info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
375 OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
376 if (!info->mti_big_lmm)
380 rc = mdt_version_get_check_save(info, pobj, 0);
384 ma->ma_lmv = info->mti_big_lmm;
385 ma->ma_lmv_size = info->mti_big_lmmsize;
387 rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
391 if (ma->ma_valid & MA_LMV) {
392 /* don't allow restripe if parent dir layout is changing */
393 lmv = &ma->ma_lmv->lmv_md_v1;
394 if (!lmv_is_sane(lmv))
397 if (lmv_is_layout_changing(lmv))
401 lhp = &info->mti_lh[MDT_LH_PARENT];
402 mdt_lock_pdo_init(lhp, LCK_PW, lname);
403 rc = mdt_reint_object_lock(info, pobj, lhp, MDS_INODELOCK_UPDATE, true);
408 rc = mdt_lookup_version_check(info, pobj, lname, cfid, 1);
410 GOTO(unlock_parent, rc);
412 child = mdt_object_find(info->mti_env, mdt, cfid);
414 GOTO(unlock_parent, rc = PTR_ERR(child));
416 if (!mdt_object_exists(child))
417 GOTO(out_child, rc = -ENOENT);
419 if (mdt_object_remote(child)) {
420 struct mdt_body *repbody;
422 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
424 GOTO(out_child, rc = -EPROTO);
426 repbody->mbo_fid1 = *cfid;
427 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
428 GOTO(out_child, rc = -EREMOTE);
432 lhc = &info->mti_lh[MDT_LH_CHILD];
433 mdt_lock_reg_init(lhc, LCK_EX);
435 /* enqueue object remote LOOKUP lock */
436 if (mdt_object_remote(pobj)) {
437 rc = mdt_remote_object_lock(info, pobj, cfid, &lhc->mlh_rreg_lh,
439 MDS_INODELOCK_LOOKUP, false);
444 rc = mdt_reint_striped_lock(info, child, lhc, MDS_INODELOCK_FULL, einfo,
447 GOTO(unlock_child, rc);
449 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
450 rc = mdt_version_get_check_save(info, child, 1);
452 GOTO(unlock_child, rc);
455 rc = mdt_stripe_get(info, child, ma, XATTR_NAME_LMV);
457 GOTO(unlock_child, rc);
459 if (ma->ma_valid & MA_LMV) {
460 lmv = &ma->ma_lmv->lmv_md_v1;
461 if (!lmv_is_sane(lmv))
462 GOTO(unlock_child, rc = -EBADF);
464 /* don't allow restripe if dir layout is changing */
465 if (lmv_is_layout_changing(lmv))
466 GOTO(unlock_child, rc = -EBUSY);
468 /* check whether stripe count and hash unchanged */
469 if (lum->lum_stripe_count == lmv->lmv_stripe_count &&
470 lum->lum_hash_type == lmv->lmv_hash_type)
471 GOTO(unlock_child, rc = -EALREADY);
473 lmv_stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
474 } else if (le32_to_cpu(lum->lum_stripe_count) < 2) {
475 /* stripe count unchanged for plain directory */
476 GOTO(unlock_child, rc = -EALREADY);
479 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
481 GOTO(unlock_child, rc = -EPROTO);
483 if (le32_to_cpu(lum->lum_stripe_count) > lmv_stripe_count) {
485 ma->ma_need = MA_INODE;
487 rc = mdt_attr_get_complex(info, child, ma);
489 GOTO(unlock_child, rc);
491 if (!(ma->ma_valid & MA_INODE))
492 GOTO(unlock_child, rc = -EBADF);
494 if (!lmv_stripe_count) {
495 /* if child is plain directory, allocate @tobj as the
496 * master object, and make child the first stripe of
499 tobj = mdt_object_new(info->mti_env, mdt, tfid);
500 if (unlikely(IS_ERR(tobj)))
501 GOTO(unlock_child, rc = PTR_ERR(tobj));
504 mlc->mlc_opc = MD_LAYOUT_SPLIT;
505 mlc->mlc_parent = mdt_object_child(pobj);
506 mlc->mlc_target = tobj ? mdt_object_child(tobj) : NULL;
507 mlc->mlc_attr = &ma->ma_attr;
508 mlc->mlc_name = lname;
509 mlc->mlc_spec = spec;
510 rc = mo_layout_change(env, mdt_object_child(child), mlc);
514 /* merge only needs to override LMV */
515 struct lu_buf *buf = &info->mti_buf;
518 LASSERT(ma->ma_valid & MA_LMV);
519 lmv = &ma->ma_lmv->lmv_md_v1;
520 version = cpu_to_le32(lmv->lmv_layout_version);
523 if (lum->lum_stripe_count == 0)
524 lum->lum_stripe_count = cpu_to_le32(1);
526 lmv->lmv_hash_type |= cpu_to_le32(LMV_HASH_FLAG_MERGE |
527 LMV_HASH_FLAG_MIGRATION);
528 lmv->lmv_merge_offset = lum->lum_stripe_count;
529 lmv->lmv_merge_hash = lum->lum_hash_type;
530 lmv->lmv_layout_version = cpu_to_le32(++version);
533 buf->lb_len = sizeof(*lmv);
534 rc = mo_xattr_set(env, mdt_object_child(child), buf,
535 XATTR_NAME_LMV, LU_XATTR_REPLACE);
537 GOTO(unlock_child, rc);
540 ma->ma_need = MA_INODE;
542 rc = mdt_attr_get_complex(info, tobj ? tobj : child, ma);
546 if (!(ma->ma_valid & MA_INODE))
547 GOTO(out_tobj, rc = -EBADF);
549 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
550 mdt_object_fid(tobj ? tobj : child));
555 mdt_object_put(env, tobj);
557 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
559 mdt_object_put(env, child);
561 mdt_object_unlock(info, pobj, lhp, rc);
567 * VBR: we save three versions in reply:
568 * 0 - parent. Check that parent version is the same during replay.
569 * 1 - name. Version of 'name' if file exists with the same name or
570 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
571 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
574 static int mdt_create(struct mdt_thread_info *info)
576 struct mdt_device *mdt = info->mti_mdt;
577 struct mdt_object *parent;
578 struct mdt_object *child;
579 struct mdt_lock_handle *lh;
580 struct mdt_body *repbody;
581 struct md_attr *ma = &info->mti_attr;
582 struct mdt_reint_record *rr = &info->mti_rr;
583 struct md_op_spec *spec = &info->mti_spec;
584 bool restripe = false;
588 DEBUG_REQ(D_INODE, mdt_info_req(info),
589 "Create ("DNAME"->"DFID") in "DFID,
590 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
592 if (!fid_is_md_operative(rr->rr_fid1))
595 if (S_ISDIR(ma->ma_attr.la_mode) &&
596 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
597 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
598 struct lu_ucred *uc = mdt_ucred(info);
599 struct obd_export *exp = mdt_info_req(info)->rq_export;
601 /* Only new clients can create remote dir( >= 2.4) and
602 * striped dir(>= 2.6), old client will return -ENOTSUPP */
603 if (!mdt_is_dne_client(exp))
606 if (le32_to_cpu(lum->lum_stripe_count) > 1) {
607 if (!mdt_is_striped_client(exp))
610 if (!mdt->mdt_enable_striped_dir)
612 } else if (!mdt->mdt_enable_remote_dir) {
616 if ((!(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH)) &&
617 (le32_to_cpu(lum->lum_hash_type) & LMV_HASH_TYPE_MASK) ==
621 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
622 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
623 mdt->mdt_enable_remote_dir_gid != -1)
626 /* restripe if later found dir exists */
627 if (le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT)
631 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
633 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
635 RETURN(PTR_ERR(parent));
637 if (!mdt_object_exists(parent))
638 GOTO(put_parent, rc = -ENOENT);
641 * LU-10235: check if name exists locklessly first to avoid massive
642 * lock recalls on existing directories.
644 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
645 &info->mti_tmp_fid1, 1);
648 GOTO(put_parent, rc = -EEXIST);
650 rc = mdt_restripe(info, parent, &rr->rr_name, rr->rr_fid2, spec,
654 /* -ENOENT is expected here */
656 GOTO(put_parent, rc);
658 /* save version of file name for replay, it must be ENOENT here */
659 mdt_enoent_version_save(info, 1);
661 OBD_RACE(OBD_FAIL_MDS_CREATE_RACE);
663 lh = &info->mti_lh[MDT_LH_PARENT];
664 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
665 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
667 GOTO(put_parent, rc);
669 if (!mdt_object_remote(parent)) {
670 rc = mdt_version_get_check_save(info, parent, 0);
672 GOTO(unlock_parent, rc);
675 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
676 if (unlikely(IS_ERR(child)))
677 GOTO(unlock_parent, rc = PTR_ERR(child));
679 ma->ma_need = MA_INODE;
682 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
683 OBD_FAIL_MDS_REINT_CREATE_WRITE);
685 /* Version of child will be updated on disk. */
686 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
687 rc = mdt_version_get_check_save(info, child, 2);
691 /* Let lower layer know current lock mode. */
692 info->mti_spec.sp_cr_mode = mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
695 * Do not perform lookup sanity check. We know that name does
698 info->mti_spec.sp_cr_lookup = 0;
699 info->mti_spec.sp_feat = &dt_directory_features;
701 rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
702 mdt_object_child(child), &info->mti_spec, ma);
704 rc = mdt_attr_get_complex(info, child, ma);
710 * On DNE, we need to eliminate dependey between 'mkdir a' and
711 * 'mkdir a/b' if b is a striped directory, to achieve this, two
712 * things are done below:
713 * 1. save child and slaves lock.
714 * 2. if the child is a striped directory, relock parent so to
715 * compare against with COS locks to ensure parent was
718 if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
719 struct mdt_lock_handle *lhc;
720 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
723 rc = mdt_object_striped(info, child);
729 if (!mdt_object_remote(parent)) {
730 mdt_object_unlock(info, parent, lh, 1);
731 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
732 rc = mdt_reint_object_lock(info, parent, lh,
733 MDS_INODELOCK_UPDATE,
740 lhc = &info->mti_lh[MDT_LH_CHILD];
741 mdt_lock_handle_init(lhc);
742 mdt_lock_reg_init(lhc, LCK_PW);
743 rc = mdt_reint_striped_lock(info, child, lhc,
744 MDS_INODELOCK_UPDATE, einfo,
749 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
752 /* Return fid & attr to client. */
753 if (ma->ma_valid & MA_INODE)
754 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
755 mdt_object_fid(child));
757 mdt_object_put(info->mti_env, child);
759 mdt_object_unlock(info, parent, lh, rc);
761 mdt_object_put(info->mti_env, parent);
765 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
768 struct mdt_lock_handle *lh;
769 int do_vbr = ma->ma_attr.la_valid &
770 (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
771 __u64 lockpart = MDS_INODELOCK_UPDATE;
772 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
777 rc = mdt_object_striped(info, mo);
783 lh = &info->mti_lh[MDT_LH_PARENT];
784 mdt_lock_reg_init(lh, LCK_PW);
786 /* Even though the new MDT will grant PERM lock to the old
787 * client, but the old client will almost ignore that during
788 * So it needs to revoke both LOOKUP and PERM lock here, so
789 * both new and old client can cancel the dcache */
790 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
791 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
793 rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
798 /* all attrs are packed into mti_attr in unpack_setattr */
799 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
800 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
802 /* VBR: update version if attr changed are important for recovery */
804 /* update on-disk version of changed object */
805 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
806 rc = mdt_version_get_check_save(info, mo, 0);
808 GOTO(out_unlock, rc);
811 /* Ensure constant striping during chown(). See LU-2789. */
812 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
813 mutex_lock(&mo->mot_lov_mutex);
815 /* all attrs are packed into mti_attr in unpack_setattr */
816 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
818 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
819 mutex_unlock(&mo->mot_lov_mutex);
822 GOTO(out_unlock, rc);
823 mdt_dom_obj_lvb_update(info->mti_env, mo, false);
826 mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
831 * Check HSM flags and add HS_DIRTY flag if relevant.
833 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
834 * and is not RELEASED.
836 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
839 struct lu_ucred *uc = mdt_ucred(info);
844 /* If the file was modified, add the dirty flag */
845 ma->ma_need = MA_HSM;
846 rc = mdt_attr_get_complex(info, mo, ma);
848 CERROR("file attribute read error for "DFID": %d.\n",
849 PFID(mdt_object_fid(mo)), rc);
853 /* If an up2date copy exists in the backend, add dirty flag */
854 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
855 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
856 ma->ma_hsm.mh_flags |= HS_DIRTY;
858 /* Bump cap so that closes from non-owner writers can
859 * set the HSM state to dirty. */
860 cap_saved = uc->uc_cap;
861 uc->uc_cap |= MD_CAP_TO_MASK(CFS_CAP_FOWNER);
862 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
863 uc->uc_cap = cap_saved;
865 CERROR("file attribute change error for "DFID": %d\n",
866 PFID(mdt_object_fid(mo)), rc);
872 static int mdt_reint_setattr(struct mdt_thread_info *info,
873 struct mdt_lock_handle *lhc)
875 struct mdt_device *mdt = info->mti_mdt;
876 struct md_attr *ma = &info->mti_attr;
877 struct mdt_reint_record *rr = &info->mti_rr;
878 struct ptlrpc_request *req = mdt_info_req(info);
879 struct mdt_object *mo;
880 struct mdt_body *repbody;
884 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
885 (unsigned int)ma->ma_attr.la_valid);
887 if (info->mti_dlm_req)
888 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
890 OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
892 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
893 mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
895 GOTO(out, rc = PTR_ERR(mo));
897 if (!mdt_object_exists(mo))
898 GOTO(out_put, rc = -ENOENT);
900 if (mdt_object_remote(mo))
901 GOTO(out_put, rc = -EREMOTE);
903 ma->ma_enable_chprojid_gid = mdt->mdt_enable_chprojid_gid;
904 /* revoke lease lock if size is going to be changed */
905 if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
906 !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
907 atomic_read(&mo->mot_lease_count) > 0)) {
908 down_read(&mo->mot_open_sem);
910 if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
911 lhc = &info->mti_lh[MDT_LH_LOCAL];
912 mdt_lock_reg_init(lhc, LCK_CW);
914 rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
916 up_read(&mo->mot_open_sem);
920 /* revoke lease lock */
921 mdt_object_unlock(info, mo, lhc, 1);
923 up_read(&mo->mot_open_sem);
926 if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
927 /* Check write access for the O_TRUNC case */
928 if (mdt_write_read(mo) < 0)
929 GOTO(out_put, rc = -ETXTBSY);
931 /* LU-10286: compatibility check for FLR.
932 * Please check the comment in mdt_finish_open() for details */
933 if (!exp_connect_flr(info->mti_exp) ||
934 !exp_connect_overstriping(info->mti_exp)) {
935 rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
936 if (rc < 0 && rc != -ENODATA)
939 if (!exp_connect_flr(info->mti_exp)) {
941 mdt_lmm_is_flr(info->mti_big_lmm))
942 GOTO(out_put, rc = -EOPNOTSUPP);
945 if (!exp_connect_overstriping(info->mti_exp)) {
947 mdt_lmm_is_overstriping(info->mti_big_lmm))
948 GOTO(out_put, rc = -EOPNOTSUPP);
952 /* For truncate, the file size sent from client
953 * is believable, but the blocks are incorrect,
954 * which makes the block size in LSOM attribute
955 * inconsisent with the real block size.
957 rc = mdt_lsom_update(info, mo, true);
962 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
963 if (ma->ma_valid & MA_LOV)
964 GOTO(out_put, rc = -EPROTO);
966 /* MDT supports FMD for regular files due to Data-on-MDT */
967 if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
968 ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
969 tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
972 rc = mdt_attr_set(info, mo, ma);
975 } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
976 (ma->ma_valid & MA_INODE)) {
977 struct lu_buf *buf = &info->mti_buf;
978 struct lu_ucred *uc = mdt_ucred(info);
979 struct mdt_lock_handle *lh;
981 __u64 lockpart = MDS_INODELOCK_XATTR;
983 /* reject if either remote or striped dir is disabled */
984 if (ma->ma_valid & MA_LMV) {
985 if (!mdt->mdt_enable_remote_dir ||
986 !mdt->mdt_enable_striped_dir)
987 GOTO(out_put, rc = -EPERM);
989 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
990 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
991 mdt->mdt_enable_remote_dir_gid != -1)
992 GOTO(out_put, rc = -EPERM);
995 if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
996 GOTO(out_put, rc = -ENOTDIR);
998 if (ma->ma_attr.la_valid != 0)
999 GOTO(out_put, rc = -EPROTO);
1001 if (ma->ma_valid & MA_LOV) {
1002 buf->lb_buf = ma->ma_lmm;
1003 buf->lb_len = ma->ma_lmm_size;
1004 name = XATTR_NAME_LOV;
1006 struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
1009 buf->lb_len = ma->ma_lmv_size;
1010 name = XATTR_NAME_DEFAULT_LMV;
1011 /* force client to update dir default layout */
1012 lockpart |= MDS_INODELOCK_LOOKUP;
1015 lh = &info->mti_lh[MDT_LH_PARENT];
1016 mdt_lock_reg_init(lh, LCK_PW);
1018 rc = mdt_object_lock(info, mo, lh, lockpart);
1022 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
1025 mdt_object_unlock(info, mo, lh, rc);
1029 GOTO(out_put, rc = -EPROTO);
1032 /* If file data is modified, add the dirty flag */
1033 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
1034 rc = mdt_add_dirty_flag(info, mo, ma);
1036 ma->ma_need = MA_INODE;
1038 rc = mdt_attr_get_complex(info, mo, ma);
1042 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
1046 mdt_object_put(info->mti_env, mo);
1049 mdt_counter_incr(req, LPROC_MDT_SETATTR);
1051 mdt_client_compatibility(info);
1052 rc2 = mdt_fix_reply(info);
1058 static int mdt_reint_create(struct mdt_thread_info *info,
1059 struct mdt_lock_handle *lhc)
1061 struct ptlrpc_request *req = mdt_info_req(info);
1065 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
1066 RETURN(err_serious(-ESTALE));
1068 if (info->mti_dlm_req)
1069 ldlm_request_cancel(mdt_info_req(info),
1070 info->mti_dlm_req, 0, LATF_SKIP);
1072 if (!lu_name_is_valid(&info->mti_rr.rr_name))
1075 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
1077 mdt_counter_incr(req, LPROC_MDT_MKDIR);
1085 /* Special file should stay on the same node as parent. */
1086 mdt_counter_incr(req, LPROC_MDT_MKNOD);
1089 CERROR("%s: Unsupported mode %o\n",
1090 mdt_obd_name(info->mti_mdt),
1091 info->mti_attr.ma_attr.la_mode);
1092 RETURN(err_serious(-EOPNOTSUPP));
1095 rc = mdt_create(info);
1100 * VBR: save parent version in reply and child version getting by its name.
1101 * Version of child is getting and checking during its lookup. If
1103 static int mdt_reint_unlink(struct mdt_thread_info *info,
1104 struct mdt_lock_handle *lhc)
1106 struct mdt_reint_record *rr = &info->mti_rr;
1107 struct ptlrpc_request *req = mdt_info_req(info);
1108 struct md_attr *ma = &info->mti_attr;
1109 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1110 struct mdt_object *mp;
1111 struct mdt_object *mc;
1112 struct mdt_lock_handle *parent_lh;
1113 struct mdt_lock_handle *child_lh;
1114 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
1116 bool cos_incompat = false;
1122 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
1123 PNAME(&rr->rr_name));
1125 if (info->mti_dlm_req)
1126 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1128 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1129 RETURN(err_serious(-ENOENT));
1131 if (!fid_is_md_operative(rr->rr_fid1))
1134 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1136 RETURN(PTR_ERR(mp));
1138 if (mdt_object_remote(mp)) {
1139 cos_incompat = true;
1141 rc = mdt_version_get_check_save(info, mp, 0);
1143 GOTO(put_parent, rc);
1147 parent_lh = &info->mti_lh[MDT_LH_PARENT];
1148 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
1149 rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
1152 GOTO(put_parent, rc);
1154 /* lookup child object along with version checking */
1155 fid_zero(child_fid);
1156 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
1158 /* Name might not be able to find during resend of
1159 * remote unlink, considering following case.
1160 * dir_A is a remote directory, the name entry of
1161 * dir_A is on MDT0, the directory is on MDT1,
1163 * 1. client sends unlink req to MDT1.
1164 * 2. MDT1 sends name delete update to MDT0.
1165 * 3. name entry is being deleted in MDT0 synchronously.
1166 * 4. MDT1 is restarted.
1167 * 5. client resends unlink req to MDT1. So it can not
1168 * find the name entry on MDT0 anymore.
1169 * In this case, MDT1 only needs to destory the local
1172 if (mdt_object_remote(mp) && rc == -ENOENT &&
1173 !fid_is_zero(rr->rr_fid2) &&
1174 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
1176 *child_fid = *rr->rr_fid2;
1178 GOTO(unlock_parent, rc);
1182 if (!fid_is_md_operative(child_fid))
1183 GOTO(unlock_parent, rc = -EPERM);
1185 /* We will lock the child regardless it is local or remote. No harm. */
1186 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
1188 GOTO(unlock_parent, rc = PTR_ERR(mc));
1190 if (!cos_incompat) {
1191 rc = mdt_object_striped(info, mc);
1193 GOTO(put_child, rc);
1197 mdt_object_put(info->mti_env, mc);
1198 mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
1203 child_lh = &info->mti_lh[MDT_LH_CHILD];
1204 mdt_lock_reg_init(child_lh, LCK_EX);
1205 if (info->mti_spec.sp_rm_entry) {
1206 struct lu_ucred *uc = mdt_ucred(info);
1208 if (!mdt_is_dne_client(req->rq_export))
1209 /* Return -ENOTSUPP for old client */
1210 GOTO(put_child, rc = -ENOTSUPP);
1212 if (!md_capable(uc, CFS_CAP_SYS_ADMIN))
1213 GOTO(put_child, rc = -EPERM);
1215 ma->ma_need = MA_INODE;
1217 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1218 NULL, &rr->rr_name, ma, no_name);
1219 GOTO(put_child, rc);
1222 if (mdt_object_remote(mc)) {
1223 struct mdt_body *repbody;
1225 if (!fid_is_zero(rr->rr_fid2)) {
1226 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
1227 mdt_obd_name(info->mti_mdt),
1228 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1229 GOTO(put_child, rc = -ENOENT);
1231 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
1232 mdt_obd_name(info->mti_mdt),
1233 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
1235 if (!mdt_is_dne_client(req->rq_export))
1236 /* Return -ENOTSUPP for old client */
1237 GOTO(put_child, rc = -ENOTSUPP);
1239 /* Revoke the LOOKUP lock of the remote object granted by
1240 * this MDT. Since the unlink will happen on another MDT,
1241 * it will release the LOOKUP lock right away. Then What
1242 * would happen if another client try to grab the LOOKUP
1243 * lock at the same time with unlink XXX */
1244 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1245 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1246 LASSERT(repbody != NULL);
1247 repbody->mbo_fid1 = *mdt_object_fid(mc);
1248 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1249 GOTO(unlock_child, rc = -EREMOTE);
1251 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1252 * this now because a running HSM restore on the child (unlink
1253 * victim) will hold the layout lock. See LU-4002. */
1254 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1255 if (mdt_object_remote(mp)) {
1256 /* Enqueue lookup lock from parent MDT */
1257 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1258 &child_lh->mlh_rreg_lh,
1259 child_lh->mlh_rreg_mode,
1260 MDS_INODELOCK_LOOKUP, false);
1262 GOTO(put_child, rc);
1264 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1267 rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
1270 GOTO(put_child, rc);
1273 * Now we can only make sure we need MA_INODE, in mdd layer, will check
1274 * whether need MA_LOV and MA_COOKIE.
1276 ma->ma_need = MA_INODE;
1279 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1280 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1281 /* save version when object is locked */
1282 mdt_version_get_save(info, mc, 1);
1284 mutex_lock(&mc->mot_lov_mutex);
1286 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1287 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1289 mutex_unlock(&mc->mot_lov_mutex);
1291 GOTO(unlock_child, rc);
1293 if (!lu_object_is_dying(&mc->mot_header)) {
1294 rc = mdt_attr_get_complex(info, mc, ma);
1297 } else if (mdt_dom_check_for_discard(info, mc)) {
1298 mdt_dom_discard_data(info, mc);
1300 mdt_handle_last_unlink(info, mc, ma);
1303 if (ma->ma_valid & MA_INODE) {
1304 switch (ma->ma_attr.la_mode & S_IFMT) {
1306 mdt_counter_incr(req, LPROC_MDT_RMDIR);
1314 mdt_counter_incr(req, LPROC_MDT_UNLINK);
1317 LASSERTF(0, "bad file type %o unlinking\n",
1318 ma->ma_attr.la_mode);
1325 mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
1327 mdt_object_put(info->mti_env, mc);
1329 mdt_object_unlock(info, mp, parent_lh, rc);
1331 mdt_object_put(info->mti_env, mp);
1332 CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
1337 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1340 static int mdt_reint_link(struct mdt_thread_info *info,
1341 struct mdt_lock_handle *lhc)
1343 struct mdt_reint_record *rr = &info->mti_rr;
1344 struct ptlrpc_request *req = mdt_info_req(info);
1345 struct md_attr *ma = &info->mti_attr;
1346 struct mdt_object *ms;
1347 struct mdt_object *mp;
1348 struct mdt_lock_handle *lhs;
1349 struct mdt_lock_handle *lhp;
1354 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1355 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1357 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1358 RETURN(err_serious(-ENOENT));
1360 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE)) {
1361 req->rq_no_reply = 1;
1362 RETURN(err_serious(-ENOENT));
1365 if (info->mti_dlm_req)
1366 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1368 /* Invalid case so return error immediately instead of
1370 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1373 if (!fid_is_md_operative(rr->rr_fid1) ||
1374 !fid_is_md_operative(rr->rr_fid2))
1377 /* step 1: find target parent dir */
1378 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1380 RETURN(PTR_ERR(mp));
1382 rc = mdt_version_get_check_save(info, mp, 0);
1384 GOTO(put_parent, rc);
1386 /* step 2: find source */
1387 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1389 GOTO(put_parent, rc = PTR_ERR(ms));
1391 if (!mdt_object_exists(ms)) {
1392 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1393 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1394 GOTO(put_source, rc = -ENOENT);
1397 cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1399 lhp = &info->mti_lh[MDT_LH_PARENT];
1400 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1401 rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1404 GOTO(put_source, rc);
1406 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1408 lhs = &info->mti_lh[MDT_LH_CHILD];
1409 mdt_lock_reg_init(lhs, LCK_EX);
1410 rc = mdt_reint_object_lock(info, ms, lhs,
1411 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1414 GOTO(unlock_parent, rc);
1416 /* step 3: link it */
1417 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1418 OBD_FAIL_MDS_REINT_LINK_WRITE);
1420 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1421 rc = mdt_version_get_check_save(info, ms, 1);
1423 GOTO(unlock_source, rc);
1425 /** check target version by name during replay */
1426 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1427 &info->mti_tmp_fid1, 2);
1428 if (rc != 0 && rc != -ENOENT)
1429 GOTO(unlock_source, rc);
1430 /* save version of file name for replay, it must be ENOENT here */
1431 if (!req_is_replay(mdt_info_req(info))) {
1432 if (rc != -ENOENT) {
1433 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1434 PNAME(&rr->rr_name));
1435 GOTO(unlock_source, rc = -EEXIST);
1437 info->mti_ver[2] = ENOENT_VERSION;
1438 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1441 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1442 mdt_object_child(ms), &rr->rr_name, ma);
1445 mdt_counter_incr(req, LPROC_MDT_LINK);
1449 mdt_object_unlock(info, ms, lhs, rc);
1451 mdt_object_unlock(info, mp, lhp, rc);
1453 mdt_object_put(info->mti_env, ms);
1455 mdt_object_put(info->mti_env, mp);
1459 * lock the part of the directory according to the hash of the name
1460 * (lh->mlh_pdo_hash) in parallel directory lock.
1462 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1463 struct mdt_lock_handle *lh,
1464 struct mdt_object *obj, __u64 ibits,
1467 struct ldlm_res_id *res = &info->mti_res_id;
1468 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1469 union ldlm_policy_data *policy = &info->mti_policy;
1470 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1474 * Finish res_id initializing by name hash marking part of
1475 * directory which is taking modification.
1477 LASSERT(lh->mlh_pdo_hash != 0);
1478 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1479 memset(policy, 0, sizeof(*policy));
1480 policy->l_inodebits.bits = ibits;
1482 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1483 dlmflags |= LDLM_FL_COS_INCOMPAT;
1485 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1486 * going to be sent to client. If it is - mdt_intent_policy() path will
1487 * fix it up and turn FL_LOCAL flag off.
1489 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
1490 policy, res, dlmflags,
1491 &info->mti_exp->exp_handle.h_cookie);
1496 * Get BFL lock for rename or migrate process.
1498 static int mdt_rename_lock(struct mdt_thread_info *info,
1499 struct lustre_handle *lh)
1504 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1505 struct lu_fid *fid = &info->mti_tmp_fid1;
1506 struct mdt_object *obj;
1508 /* XXX, right now, it has to use object API to
1509 * enqueue lock cross MDT, so it will enqueue
1510 * rename lock(with LUSTRE_BFL_FID) by root object */
1512 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1514 RETURN(PTR_ERR(obj));
1516 rc = mdt_remote_object_lock(info, obj,
1517 &LUSTRE_BFL_FID, lh,
1519 MDS_INODELOCK_UPDATE, false);
1520 mdt_object_put(info->mti_env, obj);
1522 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1523 union ldlm_policy_data *policy = &info->mti_policy;
1524 struct ldlm_res_id *res_id = &info->mti_res_id;
1527 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1528 memset(policy, 0, sizeof *policy);
1529 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1530 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1531 rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
1532 LDLM_IBITS, policy, LCK_EX, &flags,
1534 ldlm_completion_ast, NULL, NULL, 0,
1536 &info->mti_exp->exp_handle.h_cookie,
1543 static void mdt_rename_unlock(struct lustre_handle *lh)
1546 LASSERT(lustre_handle_is_used(lh));
1547 /* Cancel the single rename lock right away */
1548 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1552 static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
1553 const struct lu_fid *fid,
1556 struct mdt_object *dir;
1561 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1565 /* check early, the real version will be saved after locking */
1566 rc = mdt_version_get_check(info, dir, idx);
1570 if (!mdt_object_exists(dir))
1571 GOTO(out_put, rc = -ENOENT);
1573 if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
1574 GOTO(out_put, rc = -ENOTDIR);
1578 mdt_object_put(info->mti_env, dir);
1583 * in case obj is remote obj on its parent, revoke LOOKUP lock,
1584 * herein we don't really check it, just do revoke.
1586 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
1587 struct mdt_object *pobj,
1588 struct mdt_object *obj)
1590 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1593 mdt_lock_handle_init(lh);
1594 mdt_lock_reg_init(lh, LCK_EX);
1596 if (mdt_object_remote(pobj)) {
1597 /* don't bother to check if pobj and obj are on the same MDT. */
1598 rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
1599 &lh->mlh_rreg_lh, LCK_EX,
1600 MDS_INODELOCK_LOOKUP, false);
1601 } else if (mdt_object_remote(obj)) {
1602 struct ldlm_res_id *res = &info->mti_res_id;
1603 union ldlm_policy_data *policy = &info->mti_policy;
1604 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
1605 LDLM_FL_COS_INCOMPAT;
1607 fid_build_reg_res_name(mdt_object_fid(obj), res);
1608 memset(policy, 0, sizeof(*policy));
1609 policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
1610 rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
1611 &lh->mlh_reg_lh, LCK_EX, policy, res,
1614 /* do nothing if both are local */
1622 * TODO, currently we don't save this lock because there is no place to
1623 * hold this lock handle, but to avoid race we need to save this lock.
1625 mdt_object_unlock(info, NULL, lh, 1);
1631 * operation may takes locks of linkea, or directory stripes, group them in
1634 struct mdt_sub_lock {
1635 struct mdt_object *msl_obj;
1636 struct mdt_lock_handle msl_lh;
1637 struct list_head msl_linkage;
1640 static void mdt_unlock_list(struct mdt_thread_info *info,
1641 struct list_head *list, int decref)
1643 struct mdt_sub_lock *msl;
1644 struct mdt_sub_lock *tmp;
1646 list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
1647 mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
1648 list_del(&msl->msl_linkage);
1653 static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
1654 struct mdt_object *obj,
1655 struct mdt_lock_handle *lh,
1656 struct ldlm_enqueue_info *einfo,
1657 struct list_head *slave_locks,
1660 if (mdt_object_remote(obj)) {
1661 mdt_unlock_list(info, slave_locks, decref);
1662 mdt_object_unlock(info, obj, lh, decref);
1664 mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
1669 * lock parents of links, and also check whether total locks don't exceed
1672 * \retval 0 on success, and locks can be saved in ptlrpc_reply_stat
1673 * \retval 1 on success, but total lock count may exceed RS_MAX_LOCKS
1674 * \retval -ev negative errno upon error
1676 static int mdt_link_parents_lock(struct mdt_thread_info *info,
1677 struct mdt_object *pobj,
1678 const struct md_attr *ma,
1679 struct mdt_object *obj,
1680 struct mdt_lock_handle *lhp,
1681 struct ldlm_enqueue_info *peinfo,
1682 struct list_head *parent_slave_locks,
1683 struct list_head *link_locks)
1685 struct mdt_device *mdt = info->mti_mdt;
1686 struct lu_buf *buf = &info->mti_big_buf;
1687 struct lu_name *lname = &info->mti_name;
1688 struct linkea_data ldata = { NULL };
1689 bool blocked = false;
1690 int local_lnkp_cnt = 0;
1695 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1698 buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
1699 if (buf->lb_buf == NULL)
1703 rc = mdt_links_read(info, obj, &ldata);
1705 if (rc == -ENOENT || rc == -ENODATA)
1710 for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
1711 linkea_next_entry(&ldata)) {
1712 struct mdt_object *lnkp;
1713 struct mdt_sub_lock *msl;
1717 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
1720 /* check if it's also linked to parent */
1721 if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
1722 CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
1723 PFID(&fid), PNAME(lname));
1724 /* in case link is remote object, revoke LOOKUP lock */
1725 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1731 /* check if it's linked to a stripe of parent */
1732 if (ma->ma_valid & MA_LMV) {
1733 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1734 struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
1737 for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
1738 fid_le_to_cpu(stripe_fid,
1739 &lmv->lmv_stripe_fids[j]);
1740 if (lu_fid_eq(stripe_fid, &fid)) {
1741 CDEBUG(D_INFO, "skip stripe "DFID
1742 ", reovke "DNAME"\n",
1743 PFID(&fid), PNAME(lname));
1744 lnkp = mdt_object_find(info->mti_env,
1747 GOTO(out, rc = PTR_ERR(lnkp));
1753 rc = mdt_revoke_remote_lookup_lock(info, lnkp,
1755 mdt_object_put(info->mti_env, lnkp);
1760 /* Check if it's already locked */
1761 list_for_each_entry(msl, link_locks, msl_linkage) {
1762 if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
1764 DFID" was locked, revoke "DNAME"\n",
1765 PFID(&fid), PNAME(lname));
1766 lnkp = msl->msl_obj;
1772 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1776 CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
1777 PFID(&fid), PNAME(lname));
1779 lnkp = mdt_object_find(info->mti_env, mdt, &fid);
1781 CWARN("%s: cannot find obj "DFID": %ld\n",
1782 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
1786 if (!mdt_object_exists(lnkp)) {
1787 CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
1788 PFID(&fid), PNAME(lname));
1789 mdt_object_put(info->mti_env, lnkp);
1793 if (!mdt_object_remote(lnkp))
1798 GOTO(out, rc = -ENOMEM);
1801 * we can't follow parent-child lock order like other MD
1802 * operations, use lock_try here to avoid deadlock, if the lock
1803 * cannot be taken, drop all locks taken, revoke the blocked
1804 * one, and continue processing the remaining entries, and in
1805 * the end of the loop restart from beginning.
1807 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1809 rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
1810 MDS_INODELOCK_UPDATE, true);
1811 if (!(ibits & MDS_INODELOCK_UPDATE)) {
1813 CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
1814 PFID(&fid), PNAME(lname));
1816 mdt_unlock_list(info, link_locks, 1);
1817 /* also unlock parent locks to avoid deadlock */
1819 mdt_migrate_object_unlock(info, pobj, lhp,
1826 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1827 rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
1828 MDS_INODELOCK_UPDATE);
1830 mdt_object_put(info->mti_env, lnkp);
1835 if (mdt_object_remote(lnkp)) {
1836 struct ldlm_lock *lock;
1839 * for remote object, set lock cb_atomic,
1840 * so lock can be released in blocking_ast()
1841 * immediately, then the next lock_try will
1842 * have better chance of success.
1844 lock = ldlm_handle2lock(
1845 &msl->msl_lh.mlh_rreg_lh);
1846 LASSERT(lock != NULL);
1847 lock_res_and_lock(lock);
1848 ldlm_set_atomic_cb(lock);
1849 unlock_res_and_lock(lock);
1850 LDLM_LOCK_PUT(lock);
1853 mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
1858 INIT_LIST_HEAD(&msl->msl_linkage);
1859 msl->msl_obj = lnkp;
1860 list_add_tail(&msl->msl_linkage, link_locks);
1862 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1866 GOTO(out, rc = -EBUSY);
1871 mdt_unlock_list(info, link_locks, rc);
1872 else if (local_lnkp_cnt > RS_MAX_LOCKS - 6)
1874 * parent may have 3 local objects: master object and 2 stripes
1875 * (if it's being migrated too); source may have 2 local
1876 * objects: master and 1 stripe; target has 1 local object.
1882 static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
1883 struct mdt_object *obj,
1884 const struct md_attr *ma,
1885 struct list_head *slave_locks)
1887 struct mdt_device *mdt = info->mti_mdt;
1888 const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1889 struct lu_fid *fid = &info->mti_tmp_fid1;
1890 struct mdt_object *slave;
1891 struct mdt_sub_lock *msl;
1897 LASSERT(mdt_object_remote(obj));
1898 LASSERT(ma->ma_valid & MA_LMV);
1901 if (!lmv_is_sane(lmv))
1904 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
1905 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
1907 if (!fid_is_sane(fid))
1910 slave = mdt_object_find(info->mti_env, mdt, fid);
1912 GOTO(out, rc = PTR_ERR(slave));
1916 mdt_object_put(info->mti_env, slave);
1917 GOTO(out, rc = -ENOMEM);
1920 mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
1921 rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
1922 MDS_INODELOCK_UPDATE, true);
1925 mdt_object_put(info->mti_env, slave);
1929 INIT_LIST_HEAD(&msl->msl_linkage);
1930 msl->msl_obj = slave;
1931 list_add_tail(&msl->msl_linkage, slave_locks);
1937 mdt_unlock_list(info, slave_locks, rc);
1941 /* lock parent and its stripes */
1942 static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
1943 struct mdt_object *obj,
1944 const struct md_attr *ma,
1945 struct mdt_lock_handle *lh,
1946 struct ldlm_enqueue_info *einfo,
1947 struct list_head *slave_locks)
1951 if (mdt_object_remote(obj)) {
1952 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1953 &lh->mlh_rreg_lh, LCK_PW,
1954 MDS_INODELOCK_UPDATE, false);
1959 * if obj is remote and striped, lock its stripes explicitly
1960 * because it's not striped in LOD layer on this MDT.
1962 if (ma->ma_valid & MA_LMV) {
1963 rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
1965 mdt_object_unlock(info, obj, lh, rc);
1968 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
1976 * in migration, object may be remote, and we need take full lock of it and its
1977 * stripes if it's directory, besides, object may be a remote object on its
1978 * parent, revoke its LOOKUP lock on where its parent is located.
1980 static int mdt_migrate_object_lock(struct mdt_thread_info *info,
1981 struct mdt_object *pobj,
1982 struct mdt_object *obj,
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_revoke_remote_lookup_lock(info, pobj, obj);
1994 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1995 &lh->mlh_rreg_lh, LCK_EX,
1996 MDS_INODELOCK_FULL, false);
2001 * if obj is remote and striped, lock its stripes explicitly
2002 * because it's not striped in LOD layer on this MDT.
2004 if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
2005 struct md_attr *ma = &info->mti_attr;
2007 ma->ma_lmv = info->mti_big_lmm;
2008 ma->ma_lmv_size = info->mti_big_lmmsize;
2010 rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
2012 mdt_object_unlock(info, obj, lh, rc);
2016 if (ma->ma_valid & MA_LMV) {
2017 rc = mdt_lock_remote_slaves(info, obj, ma,
2020 mdt_object_unlock(info, obj, lh, rc);
2024 if (mdt_object_remote(pobj)) {
2025 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
2030 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
2038 * lookup source by name, if parent is striped directory, we need to find the
2039 * corresponding stripe where source is located, and then lookup there.
2041 * besides, if parent is migrating too, and file is already in target stripe,
2042 * this should be a redo of 'lfs migrate' on client side.
2044 static int mdt_migrate_lookup(struct mdt_thread_info *info,
2045 struct mdt_object *pobj,
2046 const struct md_attr *ma,
2047 const struct lu_name *lname,
2048 struct mdt_object **spobj,
2049 struct mdt_object **sobj)
2051 const struct lu_env *env = info->mti_env;
2052 struct lu_fid *fid = &info->mti_tmp_fid1;
2053 struct mdt_object *stripe;
2056 if (ma->ma_valid & MA_LMV) {
2057 /* if parent is striped, lookup on corresponding stripe */
2058 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
2060 if (!lmv_is_sane(lmv))
2063 rc = lmv_name_to_stripe_index_old(lmv, lname->ln_name,
2068 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2070 stripe = mdt_object_find(env, info->mti_mdt, fid);
2072 return PTR_ERR(stripe);
2075 rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
2077 if (rc == -ENOENT && lmv_is_layout_changing(lmv)) {
2079 * if parent layout is changeing, and lookup child
2080 * failed on source stripe, lookup again on target
2081 * stripe, if it exists, it means previous migration
2082 * was interrupted, and current file was migrated
2085 mdt_object_put(env, stripe);
2087 rc = lmv_name_to_stripe_index(lmv, lname->ln_name,
2092 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
2094 stripe = mdt_object_find(env, info->mti_mdt, fid);
2096 return PTR_ERR(stripe);
2099 rc = mdo_lookup(env, mdt_object_child(stripe), lname,
2100 fid, &info->mti_spec);
2101 mdt_object_put(env, stripe);
2102 return rc ?: -EALREADY;
2104 mdt_object_put(env, stripe);
2109 rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
2115 mdt_object_get(env, stripe);
2120 *sobj = mdt_object_find(env, info->mti_mdt, fid);
2121 if (IS_ERR(*sobj)) {
2122 mdt_object_put(env, stripe);
2123 rc = PTR_ERR(*sobj);
2131 /* end lease and close file for regular file */
2132 static int mdd_migrate_close(struct mdt_thread_info *info,
2133 struct mdt_object *obj)
2135 struct close_data *data;
2136 struct mdt_body *repbody;
2137 struct ldlm_lock *lease;
2142 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
2144 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
2148 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
2153 lease = ldlm_handle2lock(&data->cd_handle);
2157 /* check if the lease was already canceled */
2158 lock_res_and_lock(lease);
2159 rc = ldlm_is_cancel(lease);
2160 unlock_res_and_lock(lease);
2164 LDLM_DEBUG(lease, DFID" lease broken",
2165 PFID(mdt_object_fid(obj)));
2169 * cancel server side lease, client side counterpart should have been
2170 * cancelled, it's okay to cancel it now as we've held mot_open_sem.
2172 ldlm_lock_cancel(lease);
2173 ldlm_reprocess_all(lease->l_resource, lease);
2174 LDLM_LOCK_PUT(lease);
2177 rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
2178 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2179 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
2185 * migrate file in below steps:
2186 * 1. lock parent and its stripes
2187 * 2. lookup source by name
2188 * 3. lock parents of source links if source is not directory
2189 * 4. reject if source is in HSM
2190 * 5. take source open_sem and close file if source is regular file
2191 * 6. lock source and its stripes if it's directory
2192 * 7. lock target so subsequent change to it can trigger COS
2194 * 9. unlock above locks
2195 * 10. sync device if source has links
2197 static int mdt_reint_migrate(struct mdt_thread_info *info,
2198 struct mdt_lock_handle *unused)
2200 const struct lu_env *env = info->mti_env;
2201 struct mdt_device *mdt = info->mti_mdt;
2202 struct ptlrpc_request *req = mdt_info_req(info);
2203 struct mdt_reint_record *rr = &info->mti_rr;
2204 struct lu_ucred *uc = mdt_ucred(info);
2205 struct md_attr *ma = &info->mti_attr;
2206 struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
2207 struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
2208 struct mdt_object *pobj;
2209 struct mdt_object *spobj = NULL;
2210 struct mdt_object *sobj = NULL;
2211 struct mdt_object *tobj;
2212 struct lustre_handle rename_lh = { 0 };
2213 struct mdt_lock_handle *lhp;
2214 struct mdt_lock_handle *lhs;
2215 struct mdt_lock_handle *lht;
2216 LIST_HEAD(parent_slave_locks);
2217 LIST_HEAD(child_slave_locks);
2218 LIST_HEAD(link_locks);
2219 int lock_retries = 5;
2220 bool open_sem_locked = false;
2221 bool do_sync = false;
2225 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
2226 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
2228 if (info->mti_dlm_req)
2229 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2231 if (!fid_is_md_operative(rr->rr_fid1) ||
2232 !fid_is_md_operative(rr->rr_fid2))
2235 /* don't allow migrate . or .. */
2236 if (lu_name_is_dot_or_dotdot(&rr->rr_name))
2239 if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
2242 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
2243 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
2244 mdt->mdt_enable_remote_dir_gid != -1)
2248 * Note: do not enqueue rename lock for replay request, because
2249 * if other MDT holds rename lock, but being blocked to wait for
2250 * this MDT to finish its recovery, and the failover MDT can not
2251 * get rename lock, which will cause deadlock.
2253 if (!req_is_replay(req)) {
2254 rc = mdt_rename_lock(info, &rename_lh);
2256 CERROR("%s: can't lock FS for rename: rc = %d\n",
2257 mdt_obd_name(info->mti_mdt), rc);
2262 /* pobj is master object of parent */
2263 pobj = mdt_parent_find_check(info, rr->rr_fid1, 0);
2265 GOTO(unlock_rename, rc = PTR_ERR(pobj));
2267 if (unlikely(!info->mti_big_lmm)) {
2268 info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
2269 OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
2270 if (!info->mti_big_lmm)
2271 GOTO(put_parent, rc = -ENOMEM);
2274 ma->ma_lmv = info->mti_big_lmm;
2275 ma->ma_lmv_size = info->mti_big_lmmsize;
2277 rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
2279 GOTO(put_parent, rc);
2282 /* lock parent object */
2283 lhp = &info->mti_lh[MDT_LH_PARENT];
2284 mdt_lock_reg_init(lhp, LCK_PW);
2285 rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
2286 &parent_slave_locks);
2288 GOTO(put_parent, rc);
2291 * spobj is the corresponding stripe against name if pobj is striped
2292 * directory, which is the real parent, and no need to lock, because
2293 * we've taken full lock of pobj.
2295 rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
2297 GOTO(unlock_parent, rc);
2299 /* lock parents of source links, and revoke LOOKUP lock of links */
2300 rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
2301 &parent_slave_locks, &link_locks);
2302 if (rc == -EBUSY && lock_retries-- > 0) {
2303 mdt_object_put(env, sobj);
2304 mdt_object_put(env, spobj);
2309 GOTO(put_source, rc);
2312 * RS_MAX_LOCKS is the limit of number of locks that can be saved along
2313 * with one request, if total lock count exceeds this limit, we will
2314 * drop all locks after migration, and synchronous device in the end.
2318 /* TODO: DoM migration is not supported yet */
2319 if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
2320 ma->ma_lmm = info->mti_big_lmm;
2321 ma->ma_lmm_size = info->mti_big_lmmsize;
2323 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
2325 GOTO(put_source, rc);
2327 if (ma->ma_valid & MA_LOV && mdt_lmm_dom_stripesize(ma->ma_lmm))
2328 GOTO(put_source, rc = -EOPNOTSUPP);
2331 /* if migration HSM is allowed */
2332 if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
2333 ma->ma_need = MA_HSM;
2335 rc = mdt_attr_get_complex(info, sobj, ma);
2337 GOTO(unlock_links, rc);
2339 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
2340 GOTO(unlock_links, rc = -EOPNOTSUPP);
2343 /* end lease and close file for regular file */
2344 if (info->mti_spec.sp_migrate_close) {
2345 /* try to hold open_sem so that nobody else can open the file */
2346 if (!down_write_trylock(&sobj->mot_open_sem)) {
2348 mdd_migrate_close(info, sobj);
2349 GOTO(unlock_links, rc = -EBUSY);
2351 open_sem_locked = true;
2352 rc = mdd_migrate_close(info, sobj);
2354 GOTO(unlock_open_sem, rc);
2359 lhs = &info->mti_lh[MDT_LH_OLD];
2360 mdt_lock_reg_init(lhs, LCK_EX);
2361 rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
2362 &child_slave_locks);
2364 GOTO(unlock_open_sem, rc);
2367 tobj = mdt_object_find(env, mdt, rr->rr_fid2);
2369 GOTO(unlock_source, rc = PTR_ERR(tobj));
2371 lht = &info->mti_lh[MDT_LH_NEW];
2372 mdt_lock_reg_init(lht, LCK_EX);
2373 rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
2375 GOTO(put_target, rc);
2377 /* Don't do lookup sanity check. We know name doesn't exist. */
2378 info->mti_spec.sp_cr_lookup = 0;
2379 info->mti_spec.sp_feat = &dt_directory_features;
2381 rc = mdo_migrate(env, mdt_object_child(pobj),
2382 mdt_object_child(sobj), &rr->rr_name,
2383 mdt_object_child(tobj),
2384 &info->mti_spec, ma);
2386 mdt_counter_incr(req, LPROC_MDT_MIGRATE);
2389 mdt_object_unlock(info, tobj, lht, rc);
2391 mdt_object_put(env, tobj);
2393 mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
2394 &child_slave_locks, rc);
2396 if (open_sem_locked)
2397 up_write(&sobj->mot_open_sem);
2399 /* if we've got too many locks to save into RPC,
2400 * then just commit before the locks are released */
2402 mdt_device_sync(env, mdt);
2403 mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
2405 mdt_object_put(env, sobj);
2406 mdt_object_put(env, spobj);
2408 mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
2409 &parent_slave_locks, rc);
2411 mdt_object_put(env, pobj);
2413 if (lustre_handle_is_used(&rename_lh))
2414 mdt_rename_unlock(&rename_lh);
2419 static int mdt_object_lock_save(struct mdt_thread_info *info,
2420 struct mdt_object *dir,
2421 struct mdt_lock_handle *lh,
2422 int idx, bool cos_incompat)
2426 /* we lock the target dir if it is local */
2427 rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
2432 /* get and save correct version after locking */
2433 mdt_version_get_save(info, dir, idx);
2438 * determine lock order of sobj and tobj
2440 * there are two situations we need to lock tobj before sobj:
2441 * 1. sobj is child of tobj
2442 * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
2443 * larger than that of tobj
2445 * \retval 1 lock tobj before sobj
2446 * \retval 0 lock sobj before tobj
2447 * \retval -ev negative errno upon error
2449 static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
2450 struct mdt_object *sobj,
2451 struct mdt_object *tobj)
2453 struct md_attr *ma = &info->mti_attr;
2454 struct lu_fid *spfid = &info->mti_tmp_fid1;
2455 struct lu_fid *tpfid = &info->mti_tmp_fid2;
2456 struct lmv_mds_md_v1 *lmv;
2461 /* sobj and tobj are the same */
2465 if (fid_is_root(mdt_object_fid(sobj)))
2468 if (fid_is_root(mdt_object_fid(tobj)))
2471 /* check whether sobj is child of tobj */
2472 rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
2473 mdt_object_fid(tobj));
2480 /* check whether sobj and tobj are children of the same parent */
2481 rc = mdt_attr_get_pfid(info, sobj, spfid);
2485 rc = mdt_attr_get_pfid(info, tobj, tpfid);
2489 if (!lu_fid_eq(spfid, tpfid))
2492 /* check whether sobj and tobj are sibling stripes */
2493 ma->ma_need = MA_LMV;
2495 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
2496 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
2497 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2501 if (!(ma->ma_valid & MA_LMV))
2504 lmv = &ma->ma_lmv->lmv_md_v1;
2505 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2507 sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2510 rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
2514 if (!(ma->ma_valid & MA_LMV))
2517 lmv = &ma->ma_lmv->lmv_md_v1;
2518 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2520 tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2522 /* check stripe index of sobj and tobj */
2523 if (sindex == tindex)
2526 return sindex < tindex ? 0 : 1;
2530 * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
2531 * 2 - srcdir child; 3 - tgtdir child.
2532 * Update on disk version of srcdir child.
2534 static int mdt_reint_rename(struct mdt_thread_info *info,
2535 struct mdt_lock_handle *unused)
2537 struct mdt_device *mdt = info->mti_mdt;
2538 struct mdt_reint_record *rr = &info->mti_rr;
2539 struct md_attr *ma = &info->mti_attr;
2540 struct ptlrpc_request *req = mdt_info_req(info);
2541 struct mdt_object *msrcdir = NULL;
2542 struct mdt_object *mtgtdir = NULL;
2543 struct mdt_object *mold;
2544 struct mdt_object *mnew = NULL;
2545 struct lustre_handle rename_lh = { 0 };
2546 struct mdt_lock_handle *lh_srcdirp;
2547 struct mdt_lock_handle *lh_tgtdirp;
2548 struct mdt_lock_handle *lh_oldp = NULL;
2549 struct mdt_lock_handle *lh_newp = NULL;
2550 struct lu_fid *old_fid = &info->mti_tmp_fid1;
2551 struct lu_fid *new_fid = &info->mti_tmp_fid2;
2553 bool reverse = false, discard = false;
2558 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
2559 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
2560 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
2562 if (info->mti_dlm_req)
2563 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2565 if (!fid_is_md_operative(rr->rr_fid1) ||
2566 !fid_is_md_operative(rr->rr_fid2))
2569 /* find both parents. */
2570 msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
2571 if (IS_ERR(msrcdir))
2572 RETURN(PTR_ERR(msrcdir));
2574 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
2576 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
2578 mdt_object_get(info->mti_env, mtgtdir);
2580 mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
2581 if (IS_ERR(mtgtdir))
2582 GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
2586 * Note: do not enqueue rename lock for replay request, because
2587 * if other MDT holds rename lock, but being blocked to wait for
2588 * this MDT to finish its recovery, and the failover MDT can not
2589 * get rename lock, which will cause deadlock.
2591 if (!req_is_replay(req)) {
2593 * Normally rename RPC is handled on the MDT with the target
2594 * directory (if target exists, it's on the MDT with the
2595 * target), if the source directory is remote, it's a hint that
2596 * source is remote too (this may not be true, but it won't
2597 * cause any issue), return -EXDEV early to avoid taking
2600 if (!mdt->mdt_enable_remote_rename &&
2601 mdt_object_remote(msrcdir))
2602 GOTO(out_put_tgtdir, rc = -EXDEV);
2604 rc = mdt_rename_lock(info, &rename_lh);
2606 CERROR("%s: can't lock FS for rename: rc = %d\n",
2607 mdt_obd_name(mdt), rc);
2608 GOTO(out_put_tgtdir, rc);
2612 rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
2614 GOTO(out_unlock_rename, rc);
2618 /* source needs to be looked up after locking source parent, otherwise
2619 * this rename may race with unlink source, and cause rename hang, see
2620 * sanityn.sh 55b, so check parents first, if later we found source is
2621 * remote, relock parents. */
2622 cos_incompat = (mdt_object_remote(msrcdir) ||
2623 mdt_object_remote(mtgtdir));
2625 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2627 /* lock parents in the proper order. */
2628 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
2629 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
2632 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
2633 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
2636 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2639 GOTO(out_unlock_rename, rc);
2641 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2643 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2646 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2647 GOTO(out_unlock_rename, rc);
2650 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2653 GOTO(out_unlock_rename, rc);
2655 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2657 if (mtgtdir != msrcdir) {
2658 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2660 } else if (!mdt_object_remote(mtgtdir) &&
2661 lh_srcdirp->mlh_pdo_hash !=
2662 lh_tgtdirp->mlh_pdo_hash) {
2663 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
2664 MDS_INODELOCK_UPDATE,
2666 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
2669 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2670 GOTO(out_unlock_rename, rc);
2674 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2675 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
2677 /* find mold object. */
2679 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
2681 GOTO(out_unlock_parents, rc);
2683 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
2684 GOTO(out_unlock_parents, rc = -EINVAL);
2686 if (!fid_is_md_operative(old_fid))
2687 GOTO(out_unlock_parents, rc = -EPERM);
2689 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
2691 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
2693 if (!mdt_object_exists(mold)) {
2694 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2696 "object does not exist");
2697 GOTO(out_put_old, rc = -ENOENT);
2700 if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
2701 GOTO(out_put_old, rc = -EXDEV);
2703 /* Check if @mtgtdir is subdir of @mold, before locking child
2704 * to avoid reverse locking. */
2705 if (mtgtdir != msrcdir) {
2706 rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
2711 GOTO(out_put_old, rc);
2715 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
2716 /* save version after locking */
2717 mdt_version_get_save(info, mold, 2);
2719 if (!cos_incompat && mdt_object_remote(mold)) {
2720 cos_incompat = true;
2721 mdt_object_put(info->mti_env, mold);
2722 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
2723 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
2727 /* find mnew object:
2728 * mnew target object may not exist now
2729 * lookup with version checking */
2731 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
2734 /* the new_fid should have been filled at this moment */
2735 if (lu_fid_eq(old_fid, new_fid))
2736 GOTO(out_put_old, rc);
2738 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
2739 lu_fid_eq(new_fid, rr->rr_fid2))
2740 GOTO(out_put_old, rc = -EINVAL);
2742 if (!fid_is_md_operative(new_fid))
2743 GOTO(out_put_old, rc = -EPERM);
2745 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
2747 GOTO(out_put_old, rc = PTR_ERR(mnew));
2749 if (!mdt_object_exists(mnew)) {
2750 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2752 "object does not exist");
2753 GOTO(out_put_new, rc = -ENOENT);
2756 if (mdt_object_remote(mnew)) {
2757 struct mdt_body *repbody;
2759 /* Always send rename req to the target child MDT */
2760 repbody = req_capsule_server_get(info->mti_pill,
2762 LASSERT(repbody != NULL);
2763 repbody->mbo_fid1 = *new_fid;
2764 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2765 GOTO(out_put_new, rc = -EXDEV);
2767 /* Before locking the target dir, check we do not replace
2768 * a dir with a non-dir, otherwise it may deadlock with
2769 * link op which tries to create a link in this dir
2770 * back to this non-dir. */
2771 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2772 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2773 GOTO(out_put_new, rc = -EISDIR);
2775 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2776 mdt_lock_reg_init(lh_oldp, LCK_EX);
2777 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2778 if (mdt_object_remote(msrcdir)) {
2779 /* Enqueue lookup lock from the parent MDT */
2780 rc = mdt_remote_object_lock(info, msrcdir,
2781 mdt_object_fid(mold),
2782 &lh_oldp->mlh_rreg_lh,
2783 lh_oldp->mlh_rreg_mode,
2784 MDS_INODELOCK_LOOKUP,
2787 GOTO(out_put_new, rc);
2789 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2792 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2795 GOTO(out_unlock_old, rc);
2797 /* Check if @msrcdir is subdir of @mnew, before locking child
2798 * to avoid reverse locking. */
2799 if (mtgtdir != msrcdir) {
2800 rc = mdo_is_subdir(info->mti_env,
2801 mdt_object_child(msrcdir), new_fid);
2805 GOTO(out_unlock_old, rc);
2809 /* We used to acquire MDS_INODELOCK_FULL here but we
2810 * can't do this now because a running HSM restore on
2811 * the rename onto victim will hold the layout
2812 * lock. See LU-4002. */
2814 lh_newp = &info->mti_lh[MDT_LH_NEW];
2815 mdt_lock_reg_init(lh_newp, LCK_EX);
2816 rc = mdt_reint_object_lock(info, mnew, lh_newp,
2817 MDS_INODELOCK_LOOKUP |
2818 MDS_INODELOCK_UPDATE,
2821 GOTO(out_unlock_old, rc);
2823 /* get and save version after locking */
2824 mdt_version_get_save(info, mnew, 3);
2825 } else if (rc != -EREMOTE && rc != -ENOENT) {
2826 GOTO(out_put_old, rc);
2828 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2829 mdt_lock_reg_init(lh_oldp, LCK_EX);
2830 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2831 if (mdt_object_remote(msrcdir)) {
2832 /* Enqueue lookup lock from the parent MDT */
2833 rc = mdt_remote_object_lock(info, msrcdir,
2834 mdt_object_fid(mold),
2835 &lh_oldp->mlh_rreg_lh,
2836 lh_oldp->mlh_rreg_mode,
2837 MDS_INODELOCK_LOOKUP,
2840 GOTO(out_put_old, rc);
2842 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2845 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2848 GOTO(out_unlock_old, rc);
2850 mdt_enoent_version_save(info, 3);
2853 /* step 5: rename it */
2854 mdt_reint_init_ma(info, ma);
2856 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
2857 OBD_FAIL_MDS_REINT_RENAME_WRITE);
2860 mutex_lock(&mnew->mot_lov_mutex);
2862 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
2863 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
2864 mnew != NULL ? mdt_object_child(mnew) : NULL,
2865 &rr->rr_tgt_name, ma);
2868 mutex_unlock(&mnew->mot_lov_mutex);
2870 /* handle last link of tgt object */
2872 mdt_counter_incr(req, LPROC_MDT_RENAME);
2874 mdt_handle_last_unlink(info, mnew, ma);
2875 discard = mdt_dom_check_for_discard(info, mnew);
2877 mdt_rename_counter_tally(info, info->mti_mdt, req,
2883 mdt_object_unlock(info, mnew, lh_newp, rc);
2885 mdt_object_unlock(info, mold, lh_oldp, rc);
2887 if (mnew && !discard)
2888 mdt_object_put(info->mti_env, mnew);
2890 mdt_object_put(info->mti_env, mold);
2892 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2893 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2895 if (lustre_handle_is_used(&rename_lh))
2896 mdt_rename_unlock(&rename_lh);
2898 mdt_object_put(info->mti_env, mtgtdir);
2900 mdt_object_put(info->mti_env, msrcdir);
2902 /* The DoM discard can be done right in the place above where it is
2903 * assigned, meanwhile it is done here after rename unlock due to
2904 * compatibility with old clients, for them the discard blocks
2905 * the main thread until completion. Check LU-11359 for details.
2908 mdt_dom_discard_data(info, mnew);
2909 mdt_object_put(info->mti_env, mnew);
2914 static int mdt_reint_resync(struct mdt_thread_info *info,
2915 struct mdt_lock_handle *lhc)
2917 struct mdt_reint_record *rr = &info->mti_rr;
2918 struct ptlrpc_request *req = mdt_info_req(info);
2919 struct md_attr *ma = &info->mti_attr;
2920 struct mdt_object *mo;
2921 struct ldlm_lock *lease;
2922 struct mdt_body *repbody;
2923 struct md_layout_change layout = { .mlc_mirror_id = rr->rr_mirror_id };
2929 DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
2931 if (info->mti_dlm_req)
2932 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2934 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
2936 GOTO(out, rc = PTR_ERR(mo));
2938 if (!mdt_object_exists(mo))
2939 GOTO(out_obj, rc = -ENOENT);
2941 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
2942 GOTO(out_obj, rc = -EINVAL);
2944 if (mdt_object_remote(mo))
2945 GOTO(out_obj, rc = -EREMOTE);
2947 lease = ldlm_handle2lock(rr->rr_lease_handle);
2949 GOTO(out_obj, rc = -ESTALE);
2951 /* It's really necessary to grab open_sem and check if the lease lock
2952 * has been lost. There would exist a concurrent writer coming in and
2953 * generating some dirty data in memory cache, the writeback would fail
2954 * after the layout version is increased by MDS_REINT_RESYNC RPC. */
2955 if (!down_write_trylock(&mo->mot_open_sem))
2956 GOTO(out_put_lease, rc = -EBUSY);
2958 lock_res_and_lock(lease);
2959 lease_broken = ldlm_is_cancel(lease);
2960 unlock_res_and_lock(lease);
2962 GOTO(out_unlock, rc = -EBUSY);
2964 /* the file has yet opened by anyone else after we took the lease. */
2965 layout.mlc_opc = MD_LAYOUT_RESYNC;
2966 lhc = &info->mti_lh[MDT_LH_LOCAL];
2967 rc = mdt_layout_change(info, mo, lhc, &layout);
2969 GOTO(out_unlock, rc);
2971 mdt_object_unlock(info, mo, lhc, 0);
2973 ma->ma_need = MA_INODE;
2975 rc = mdt_attr_get_complex(info, mo, ma);
2977 GOTO(out_unlock, rc);
2979 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2980 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
2984 up_write(&mo->mot_open_sem);
2986 LDLM_LOCK_PUT(lease);
2988 mdt_object_put(info->mti_env, mo);
2990 mdt_client_compatibility(info);
2991 rc2 = mdt_fix_reply(info);
2997 struct mdt_reinter {
2998 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
2999 enum lprocfs_extra_opc mr_extra_opc;
3002 static const struct mdt_reinter mdt_reinters[] = {
3004 .mr_handler = &mdt_reint_setattr,
3005 .mr_extra_opc = MDS_REINT_SETATTR,
3008 .mr_handler = &mdt_reint_create,
3009 .mr_extra_opc = MDS_REINT_CREATE,
3012 .mr_handler = &mdt_reint_link,
3013 .mr_extra_opc = MDS_REINT_LINK,
3016 .mr_handler = &mdt_reint_unlink,
3017 .mr_extra_opc = MDS_REINT_UNLINK,
3020 .mr_handler = &mdt_reint_rename,
3021 .mr_extra_opc = MDS_REINT_RENAME,
3024 .mr_handler = &mdt_reint_open,
3025 .mr_extra_opc = MDS_REINT_OPEN,
3027 [REINT_SETXATTR] = {
3028 .mr_handler = &mdt_reint_setxattr,
3029 .mr_extra_opc = MDS_REINT_SETXATTR,
3032 .mr_handler = &mdt_reint_unlink,
3033 .mr_extra_opc = MDS_REINT_UNLINK,
3036 .mr_handler = &mdt_reint_migrate,
3037 .mr_extra_opc = MDS_REINT_RENAME,
3040 .mr_handler = &mdt_reint_resync,
3041 .mr_extra_opc = MDS_REINT_RESYNC,
3045 int mdt_reint_rec(struct mdt_thread_info *info,
3046 struct mdt_lock_handle *lhc)
3048 const struct mdt_reinter *mr;
3052 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
3055 mr = &mdt_reinters[info->mti_rr.rr_opcode];
3056 if (mr->mr_handler == NULL)
3059 rc = (*mr->mr_handler)(info, lhc);
3061 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
3062 PTLRPC_LAST_CNTR + mr->mr_extra_opc);