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);
346 * VBR: we save three versions in reply:
347 * 0 - parent. Check that parent version is the same during replay.
348 * 1 - name. Version of 'name' if file exists with the same name or
349 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
350 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
353 static int mdt_create(struct mdt_thread_info *info)
355 struct mdt_device *mdt = info->mti_mdt;
356 struct mdt_object *parent;
357 struct mdt_object *child;
358 struct mdt_lock_handle *lh;
359 struct mdt_body *repbody;
360 struct md_attr *ma = &info->mti_attr;
361 struct mdt_reint_record *rr = &info->mti_rr;
362 struct md_op_spec *spec = &info->mti_spec;
366 DEBUG_REQ(D_INODE, mdt_info_req(info),
367 "Create ("DNAME"->"DFID") in "DFID,
368 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
370 if (!fid_is_md_operative(rr->rr_fid1))
373 if (S_ISDIR(ma->ma_attr.la_mode) &&
374 spec->u.sp_ea.eadata != NULL && spec->u.sp_ea.eadatalen != 0) {
375 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
376 struct lu_ucred *uc = mdt_ucred(info);
377 struct obd_export *exp = mdt_info_req(info)->rq_export;
379 /* Only new clients can create remote dir( >= 2.4) and
380 * striped dir(>= 2.6), old client will return -ENOTSUPP */
381 if (!mdt_is_dne_client(exp))
384 if (le32_to_cpu(lum->lum_stripe_count) > 1) {
385 if (!mdt_is_striped_client(exp))
388 if (!mdt->mdt_enable_striped_dir)
390 } else if (!mdt->mdt_enable_remote_dir) {
394 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
395 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
396 mdt->mdt_enable_remote_dir_gid != -1)
400 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
402 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
404 RETURN(PTR_ERR(parent));
406 if (!mdt_object_exists(parent))
407 GOTO(put_parent, rc = -ENOENT);
409 lh = &info->mti_lh[MDT_LH_PARENT];
410 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
411 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
413 GOTO(put_parent, rc);
415 if (!mdt_object_remote(parent)) {
416 rc = mdt_version_get_check_save(info, parent, 0);
418 GOTO(unlock_parent, rc);
422 * Check child name version during replay.
423 * During create replay a file may exist with same name.
425 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
426 &info->mti_tmp_fid1, 1);
428 GOTO(unlock_parent, rc = -EEXIST);
430 /* -ENOENT is expected here */
432 GOTO(unlock_parent, rc);
434 /* save version of file name for replay, it must be ENOENT here */
435 mdt_enoent_version_save(info, 1);
437 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
438 if (unlikely(IS_ERR(child)))
439 GOTO(unlock_parent, rc = PTR_ERR(child));
441 ma->ma_need = MA_INODE;
444 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
445 OBD_FAIL_MDS_REINT_CREATE_WRITE);
447 /* Version of child will be updated on disk. */
448 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
449 rc = mdt_version_get_check_save(info, child, 2);
453 /* Let lower layer know current lock mode. */
454 info->mti_spec.sp_cr_mode = mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
457 * Do not perform lookup sanity check. We know that name does
460 info->mti_spec.sp_cr_lookup = 0;
461 info->mti_spec.sp_feat = &dt_directory_features;
463 rc = mdo_create(info->mti_env, mdt_object_child(parent), &rr->rr_name,
464 mdt_object_child(child), &info->mti_spec, ma);
466 rc = mdt_attr_get_complex(info, child, ma);
472 * On DNE, we need to eliminate dependey between 'mkdir a' and
473 * 'mkdir a/b' if b is a striped directory, to achieve this, two
474 * things are done below:
475 * 1. save child and slaves lock.
476 * 2. if the child is a striped directory, relock parent so to
477 * compare against with COS locks to ensure parent was
480 if (mdt_slc_is_enabled(mdt) && S_ISDIR(ma->ma_attr.la_mode)) {
481 struct mdt_lock_handle *lhc;
482 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
485 rc = mdt_object_striped(info, child);
491 if (!mdt_object_remote(parent)) {
492 mdt_object_unlock(info, parent, lh, 1);
493 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
494 rc = mdt_reint_object_lock(info, parent, lh,
495 MDS_INODELOCK_UPDATE,
502 lhc = &info->mti_lh[MDT_LH_CHILD];
503 mdt_lock_handle_init(lhc);
504 mdt_lock_reg_init(lhc, LCK_PW);
505 rc = mdt_reint_striped_lock(info, child, lhc,
506 MDS_INODELOCK_UPDATE, einfo,
511 mdt_reint_striped_unlock(info, child, lhc, einfo, rc);
514 /* Return fid & attr to client. */
515 if (ma->ma_valid & MA_INODE)
516 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
517 mdt_object_fid(child));
519 mdt_object_put(info->mti_env, child);
521 mdt_object_unlock(info, parent, lh, rc);
523 mdt_object_put(info->mti_env, parent);
527 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
530 struct mdt_lock_handle *lh;
531 int do_vbr = ma->ma_attr.la_valid &
532 (LA_MODE | LA_UID | LA_GID | LA_PROJID | LA_FLAGS);
533 __u64 lockpart = MDS_INODELOCK_UPDATE;
534 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
539 rc = mdt_object_striped(info, mo);
545 lh = &info->mti_lh[MDT_LH_PARENT];
546 mdt_lock_reg_init(lh, LCK_PW);
548 /* Even though the new MDT will grant PERM lock to the old
549 * client, but the old client will almost ignore that during
550 * So it needs to revoke both LOOKUP and PERM lock here, so
551 * both new and old client can cancel the dcache */
552 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
553 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
555 rc = mdt_reint_striped_lock(info, mo, lh, lockpart, einfo,
560 /* all attrs are packed into mti_attr in unpack_setattr */
561 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
562 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
564 /* VBR: update version if attr changed are important for recovery */
566 /* update on-disk version of changed object */
567 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
568 rc = mdt_version_get_check_save(info, mo, 0);
570 GOTO(out_unlock, rc);
573 /* Ensure constant striping during chown(). See LU-2789. */
574 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
575 mutex_lock(&mo->mot_lov_mutex);
577 /* all attrs are packed into mti_attr in unpack_setattr */
578 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
580 if (ma->ma_attr.la_valid & (LA_UID|LA_GID|LA_PROJID))
581 mutex_unlock(&mo->mot_lov_mutex);
584 GOTO(out_unlock, rc);
585 mdt_dom_obj_lvb_update(info->mti_env, mo, false);
588 mdt_reint_striped_unlock(info, mo, lh, einfo, rc);
593 * Check HSM flags and add HS_DIRTY flag if relevant.
595 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
596 * and is not RELEASED.
598 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
601 struct lu_ucred *uc = mdt_ucred(info);
606 /* If the file was modified, add the dirty flag */
607 ma->ma_need = MA_HSM;
608 rc = mdt_attr_get_complex(info, mo, ma);
610 CERROR("file attribute read error for "DFID": %d.\n",
611 PFID(mdt_object_fid(mo)), rc);
615 /* If an up2date copy exists in the backend, add dirty flag */
616 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
617 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
618 ma->ma_hsm.mh_flags |= HS_DIRTY;
620 /* Bump cap so that closes from non-owner writers can
621 * set the HSM state to dirty. */
622 cap_saved = uc->uc_cap;
623 uc->uc_cap |= MD_CAP_TO_MASK(CFS_CAP_FOWNER);
624 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
625 uc->uc_cap = cap_saved;
627 CERROR("file attribute change error for "DFID": %d\n",
628 PFID(mdt_object_fid(mo)), rc);
634 static int mdt_reint_setattr(struct mdt_thread_info *info,
635 struct mdt_lock_handle *lhc)
637 struct mdt_device *mdt = info->mti_mdt;
638 struct md_attr *ma = &info->mti_attr;
639 struct mdt_reint_record *rr = &info->mti_rr;
640 struct ptlrpc_request *req = mdt_info_req(info);
641 struct mdt_object *mo;
642 struct mdt_body *repbody;
646 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
647 (unsigned int)ma->ma_attr.la_valid);
649 if (info->mti_dlm_req)
650 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
652 OBD_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
654 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
655 mo = mdt_object_find(info->mti_env, mdt, rr->rr_fid1);
657 GOTO(out, rc = PTR_ERR(mo));
659 if (!mdt_object_exists(mo))
660 GOTO(out_put, rc = -ENOENT);
662 if (mdt_object_remote(mo))
663 GOTO(out_put, rc = -EREMOTE);
665 /* revoke lease lock if size is going to be changed */
666 if (unlikely(ma->ma_attr.la_valid & LA_SIZE &&
667 !(ma->ma_attr_flags & MDS_TRUNC_KEEP_LEASE) &&
668 atomic_read(&mo->mot_lease_count) > 0)) {
669 down_read(&mo->mot_open_sem);
671 if (atomic_read(&mo->mot_lease_count) > 0) { /* lease exists */
672 lhc = &info->mti_lh[MDT_LH_LOCAL];
673 mdt_lock_reg_init(lhc, LCK_CW);
675 rc = mdt_object_lock(info, mo, lhc, MDS_INODELOCK_OPEN);
677 up_read(&mo->mot_open_sem);
681 /* revoke lease lock */
682 mdt_object_unlock(info, mo, lhc, 1);
684 up_read(&mo->mot_open_sem);
687 if (ma->ma_attr.la_valid & LA_SIZE || rr->rr_flags & MRF_OPEN_TRUNC) {
688 /* Check write access for the O_TRUNC case */
689 if (mdt_write_read(mo) < 0)
690 GOTO(out_put, rc = -ETXTBSY);
692 /* LU-10286: compatibility check for FLR.
693 * Please check the comment in mdt_finish_open() for details */
694 if (!exp_connect_flr(info->mti_exp) ||
695 !exp_connect_overstriping(info->mti_exp)) {
696 rc = mdt_big_xattr_get(info, mo, XATTR_NAME_LOV);
697 if (rc < 0 && rc != -ENODATA)
700 if (!exp_connect_flr(info->mti_exp)) {
702 mdt_lmm_is_flr(info->mti_big_lmm))
703 GOTO(out_put, rc = -EOPNOTSUPP);
706 if (!exp_connect_overstriping(info->mti_exp)) {
708 mdt_lmm_is_overstriping(info->mti_big_lmm))
709 GOTO(out_put, rc = -EOPNOTSUPP);
713 /* For truncate, the file size sent from client
714 * is believable, but the blocks are incorrect,
715 * which makes the block size in LSOM attribute
716 * inconsisent with the real block size.
718 rc = mdt_lsom_update(info, mo, true);
723 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
724 if (ma->ma_valid & MA_LOV)
725 GOTO(out_put, rc = -EPROTO);
727 /* MDT supports FMD for regular files due to Data-on-MDT */
728 if (S_ISREG(lu_object_attr(&mo->mot_obj)) &&
729 ma->ma_attr.la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
730 tgt_fmd_update(info->mti_exp, mdt_object_fid(mo),
733 rc = mdt_attr_set(info, mo, ma);
736 } else if ((ma->ma_valid & (MA_LOV | MA_LMV)) &&
737 (ma->ma_valid & MA_INODE)) {
738 struct lu_buf *buf = &info->mti_buf;
739 struct lu_ucred *uc = mdt_ucred(info);
740 struct mdt_lock_handle *lh;
742 __u64 lockpart = MDS_INODELOCK_XATTR;
744 /* reject if either remote or striped dir is disabled */
745 if (ma->ma_valid & MA_LMV) {
746 if (!mdt->mdt_enable_remote_dir ||
747 !mdt->mdt_enable_striped_dir)
748 GOTO(out_put, rc = -EPERM);
750 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
751 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
752 mdt->mdt_enable_remote_dir_gid != -1)
753 GOTO(out_put, rc = -EPERM);
756 if (!S_ISDIR(lu_object_attr(&mo->mot_obj)))
757 GOTO(out_put, rc = -ENOTDIR);
759 if (ma->ma_attr.la_valid != 0)
760 GOTO(out_put, rc = -EPROTO);
762 if (ma->ma_valid & MA_LOV) {
763 buf->lb_buf = ma->ma_lmm;
764 buf->lb_len = ma->ma_lmm_size;
765 name = XATTR_NAME_LOV;
767 struct lmv_user_md *lmu = &ma->ma_lmv->lmv_user_md;
770 buf->lb_len = ma->ma_lmv_size;
771 name = XATTR_NAME_DEFAULT_LMV;
772 /* force client to update dir default layout */
773 lockpart |= MDS_INODELOCK_LOOKUP;
776 lh = &info->mti_lh[MDT_LH_PARENT];
777 mdt_lock_reg_init(lh, LCK_PW);
779 rc = mdt_object_lock(info, mo, lh, lockpart);
783 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo), buf,
786 mdt_object_unlock(info, mo, lh, rc);
790 GOTO(out_put, rc = -EPROTO);
793 /* If file data is modified, add the dirty flag */
794 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
795 rc = mdt_add_dirty_flag(info, mo, ma);
797 ma->ma_need = MA_INODE;
799 rc = mdt_attr_get_complex(info, mo, ma);
803 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
807 mdt_object_put(info->mti_env, mo);
810 mdt_counter_incr(req, LPROC_MDT_SETATTR);
812 mdt_client_compatibility(info);
813 rc2 = mdt_fix_reply(info);
819 static int mdt_reint_create(struct mdt_thread_info *info,
820 struct mdt_lock_handle *lhc)
822 struct ptlrpc_request *req = mdt_info_req(info);
826 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
827 RETURN(err_serious(-ESTALE));
829 if (info->mti_dlm_req)
830 ldlm_request_cancel(mdt_info_req(info),
831 info->mti_dlm_req, 0, LATF_SKIP);
833 if (!lu_name_is_valid(&info->mti_rr.rr_name))
836 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
838 mdt_counter_incr(req, LPROC_MDT_MKDIR);
846 /* Special file should stay on the same node as parent. */
847 mdt_counter_incr(req, LPROC_MDT_MKNOD);
850 CERROR("%s: Unsupported mode %o\n",
851 mdt_obd_name(info->mti_mdt),
852 info->mti_attr.ma_attr.la_mode);
853 RETURN(err_serious(-EOPNOTSUPP));
856 rc = mdt_create(info);
861 * VBR: save parent version in reply and child version getting by its name.
862 * Version of child is getting and checking during its lookup. If
864 static int mdt_reint_unlink(struct mdt_thread_info *info,
865 struct mdt_lock_handle *lhc)
867 struct mdt_reint_record *rr = &info->mti_rr;
868 struct ptlrpc_request *req = mdt_info_req(info);
869 struct md_attr *ma = &info->mti_attr;
870 struct lu_fid *child_fid = &info->mti_tmp_fid1;
871 struct mdt_object *mp;
872 struct mdt_object *mc;
873 struct mdt_lock_handle *parent_lh;
874 struct mdt_lock_handle *child_lh;
875 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
877 bool cos_incompat = false;
883 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
884 PNAME(&rr->rr_name));
886 if (info->mti_dlm_req)
887 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
889 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
890 RETURN(err_serious(-ENOENT));
892 if (!fid_is_md_operative(rr->rr_fid1))
895 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
899 if (mdt_object_remote(mp)) {
902 rc = mdt_version_get_check_save(info, mp, 0);
904 GOTO(put_parent, rc);
908 parent_lh = &info->mti_lh[MDT_LH_PARENT];
909 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
910 rc = mdt_reint_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
913 GOTO(put_parent, rc);
915 /* lookup child object along with version checking */
917 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
919 /* Name might not be able to find during resend of
920 * remote unlink, considering following case.
921 * dir_A is a remote directory, the name entry of
922 * dir_A is on MDT0, the directory is on MDT1,
924 * 1. client sends unlink req to MDT1.
925 * 2. MDT1 sends name delete update to MDT0.
926 * 3. name entry is being deleted in MDT0 synchronously.
927 * 4. MDT1 is restarted.
928 * 5. client resends unlink req to MDT1. So it can not
929 * find the name entry on MDT0 anymore.
930 * In this case, MDT1 only needs to destory the local
933 if (mdt_object_remote(mp) && rc == -ENOENT &&
934 !fid_is_zero(rr->rr_fid2) &&
935 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
937 *child_fid = *rr->rr_fid2;
939 GOTO(unlock_parent, rc);
943 if (!fid_is_md_operative(child_fid))
944 GOTO(unlock_parent, rc = -EPERM);
946 /* We will lock the child regardless it is local or remote. No harm. */
947 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
949 GOTO(unlock_parent, rc = PTR_ERR(mc));
952 rc = mdt_object_striped(info, mc);
958 mdt_object_put(info->mti_env, mc);
959 mdt_object_unlock(info, mp, parent_lh, -EAGAIN);
964 child_lh = &info->mti_lh[MDT_LH_CHILD];
965 mdt_lock_reg_init(child_lh, LCK_EX);
966 if (info->mti_spec.sp_rm_entry) {
967 struct lu_ucred *uc = mdt_ucred(info);
969 if (!mdt_is_dne_client(req->rq_export))
970 /* Return -ENOTSUPP for old client */
971 GOTO(put_child, rc = -ENOTSUPP);
973 if (!md_capable(uc, CFS_CAP_SYS_ADMIN))
974 GOTO(put_child, rc = -EPERM);
976 ma->ma_need = MA_INODE;
978 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
979 NULL, &rr->rr_name, ma, no_name);
983 if (mdt_object_remote(mc)) {
984 struct mdt_body *repbody;
986 if (!fid_is_zero(rr->rr_fid2)) {
987 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
988 mdt_obd_name(info->mti_mdt),
989 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
990 GOTO(put_child, rc = -ENOENT);
992 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
993 mdt_obd_name(info->mti_mdt),
994 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
996 if (!mdt_is_dne_client(req->rq_export))
997 /* Return -ENOTSUPP for old client */
998 GOTO(put_child, rc = -ENOTSUPP);
1000 /* Revoke the LOOKUP lock of the remote object granted by
1001 * this MDT. Since the unlink will happen on another MDT,
1002 * it will release the LOOKUP lock right away. Then What
1003 * would happen if another client try to grab the LOOKUP
1004 * lock at the same time with unlink XXX */
1005 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
1006 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1007 LASSERT(repbody != NULL);
1008 repbody->mbo_fid1 = *mdt_object_fid(mc);
1009 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1010 GOTO(unlock_child, rc = -EREMOTE);
1012 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
1013 * this now because a running HSM restore on the child (unlink
1014 * victim) will hold the layout lock. See LU-4002. */
1015 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1016 if (mdt_object_remote(mp)) {
1017 /* Enqueue lookup lock from parent MDT */
1018 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
1019 &child_lh->mlh_rreg_lh,
1020 child_lh->mlh_rreg_mode,
1021 MDS_INODELOCK_LOOKUP, false);
1023 GOTO(put_child, rc);
1025 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1028 rc = mdt_reint_striped_lock(info, mc, child_lh, lock_ibits, einfo,
1031 GOTO(put_child, rc);
1034 * Now we can only make sure we need MA_INODE, in mdd layer, will check
1035 * whether need MA_LOV and MA_COOKIE.
1037 ma->ma_need = MA_INODE;
1040 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1041 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
1042 /* save version when object is locked */
1043 mdt_version_get_save(info, mc, 1);
1045 mutex_lock(&mc->mot_lov_mutex);
1047 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1048 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1050 mutex_unlock(&mc->mot_lov_mutex);
1052 GOTO(unlock_child, rc);
1054 if (!lu_object_is_dying(&mc->mot_header)) {
1055 rc = mdt_attr_get_complex(info, mc, ma);
1058 } else if (mdt_dom_check_for_discard(info, mc)) {
1059 mdt_dom_discard_data(info, mc);
1061 mdt_handle_last_unlink(info, mc, ma);
1064 if (ma->ma_valid & MA_INODE) {
1065 switch (ma->ma_attr.la_mode & S_IFMT) {
1067 mdt_counter_incr(req, LPROC_MDT_RMDIR);
1075 mdt_counter_incr(req, LPROC_MDT_UNLINK);
1078 LASSERTF(0, "bad file type %o unlinking\n",
1079 ma->ma_attr.la_mode);
1086 mdt_reint_striped_unlock(info, mc, child_lh, einfo, rc);
1088 mdt_object_put(info->mti_env, mc);
1090 mdt_object_unlock(info, mp, parent_lh, rc);
1092 mdt_object_put(info->mti_env, mp);
1093 CFS_RACE_WAKEUP(OBD_FAIL_OBD_ZERO_NLINK_RACE);
1098 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1101 static int mdt_reint_link(struct mdt_thread_info *info,
1102 struct mdt_lock_handle *lhc)
1104 struct mdt_reint_record *rr = &info->mti_rr;
1105 struct ptlrpc_request *req = mdt_info_req(info);
1106 struct md_attr *ma = &info->mti_attr;
1107 struct mdt_object *ms;
1108 struct mdt_object *mp;
1109 struct mdt_lock_handle *lhs;
1110 struct mdt_lock_handle *lhp;
1115 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1116 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1118 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1119 RETURN(err_serious(-ENOENT));
1121 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE)) {
1122 req->rq_no_reply = 1;
1123 RETURN(err_serious(-ENOENT));
1126 if (info->mti_dlm_req)
1127 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1129 /* Invalid case so return error immediately instead of
1131 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1134 if (!fid_is_md_operative(rr->rr_fid1) ||
1135 !fid_is_md_operative(rr->rr_fid2))
1138 /* step 1: find target parent dir */
1139 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid2);
1141 RETURN(PTR_ERR(mp));
1143 rc = mdt_version_get_check_save(info, mp, 0);
1145 GOTO(put_parent, rc);
1147 /* step 2: find source */
1148 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1150 GOTO(put_parent, rc = PTR_ERR(ms));
1152 if (!mdt_object_exists(ms)) {
1153 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1154 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1155 GOTO(put_source, rc = -ENOENT);
1158 cos_incompat = (mdt_object_remote(mp) || mdt_object_remote(ms));
1160 lhp = &info->mti_lh[MDT_LH_PARENT];
1161 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1162 rc = mdt_reint_object_lock(info, mp, lhp, MDS_INODELOCK_UPDATE,
1165 GOTO(put_source, rc);
1167 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1169 lhs = &info->mti_lh[MDT_LH_CHILD];
1170 mdt_lock_reg_init(lhs, LCK_EX);
1171 rc = mdt_reint_object_lock(info, ms, lhs,
1172 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1175 GOTO(unlock_parent, rc);
1177 /* step 3: link it */
1178 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1179 OBD_FAIL_MDS_REINT_LINK_WRITE);
1181 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1182 rc = mdt_version_get_check_save(info, ms, 1);
1184 GOTO(unlock_source, rc);
1186 /** check target version by name during replay */
1187 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1188 &info->mti_tmp_fid1, 2);
1189 if (rc != 0 && rc != -ENOENT)
1190 GOTO(unlock_source, rc);
1191 /* save version of file name for replay, it must be ENOENT here */
1192 if (!req_is_replay(mdt_info_req(info))) {
1193 if (rc != -ENOENT) {
1194 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1195 PNAME(&rr->rr_name));
1196 GOTO(unlock_source, rc = -EEXIST);
1198 info->mti_ver[2] = ENOENT_VERSION;
1199 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1202 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1203 mdt_object_child(ms), &rr->rr_name, ma);
1206 mdt_counter_incr(req, LPROC_MDT_LINK);
1210 mdt_object_unlock(info, ms, lhs, rc);
1212 mdt_object_unlock(info, mp, lhp, rc);
1214 mdt_object_put(info->mti_env, ms);
1216 mdt_object_put(info->mti_env, mp);
1220 * lock the part of the directory according to the hash of the name
1221 * (lh->mlh_pdo_hash) in parallel directory lock.
1223 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1224 struct mdt_lock_handle *lh,
1225 struct mdt_object *obj, __u64 ibits,
1228 struct ldlm_res_id *res = &info->mti_res_id;
1229 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1230 union ldlm_policy_data *policy = &info->mti_policy;
1231 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1235 * Finish res_id initializing by name hash marking part of
1236 * directory which is taking modification.
1238 LASSERT(lh->mlh_pdo_hash != 0);
1239 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1240 memset(policy, 0, sizeof(*policy));
1241 policy->l_inodebits.bits = ibits;
1243 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
1244 dlmflags |= LDLM_FL_COS_INCOMPAT;
1246 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1247 * going to be sent to client. If it is - mdt_intent_policy() path will
1248 * fix it up and turn FL_LOCAL flag off.
1250 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
1251 policy, res, dlmflags,
1252 &info->mti_exp->exp_handle.h_cookie);
1257 * Get BFL lock for rename or migrate process.
1259 static int mdt_rename_lock(struct mdt_thread_info *info,
1260 struct lustre_handle *lh)
1265 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1266 struct lu_fid *fid = &info->mti_tmp_fid1;
1267 struct mdt_object *obj;
1269 /* XXX, right now, it has to use object API to
1270 * enqueue lock cross MDT, so it will enqueue
1271 * rename lock(with LUSTRE_BFL_FID) by root object */
1273 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1275 RETURN(PTR_ERR(obj));
1277 rc = mdt_remote_object_lock(info, obj,
1278 &LUSTRE_BFL_FID, lh,
1280 MDS_INODELOCK_UPDATE, false);
1281 mdt_object_put(info->mti_env, obj);
1283 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1284 union ldlm_policy_data *policy = &info->mti_policy;
1285 struct ldlm_res_id *res_id = &info->mti_res_id;
1288 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1289 memset(policy, 0, sizeof *policy);
1290 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1291 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1292 rc = ldlm_cli_enqueue_local(info->mti_env, ns, res_id,
1293 LDLM_IBITS, policy, LCK_EX, &flags,
1295 ldlm_completion_ast, NULL, NULL, 0,
1297 &info->mti_exp->exp_handle.h_cookie,
1304 static void mdt_rename_unlock(struct lustre_handle *lh)
1307 LASSERT(lustre_handle_is_used(lh));
1308 /* Cancel the single rename lock right away */
1309 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1313 static struct mdt_object *mdt_parent_find_check(struct mdt_thread_info *info,
1314 const struct lu_fid *fid,
1317 struct mdt_object *dir;
1322 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1326 /* check early, the real version will be saved after locking */
1327 rc = mdt_version_get_check(info, dir, idx);
1331 if (!mdt_object_exists(dir))
1332 GOTO(out_put, rc = -ENOENT);
1334 if (!S_ISDIR(lu_object_attr(&dir->mot_obj)))
1335 GOTO(out_put, rc = -ENOTDIR);
1339 mdt_object_put(info->mti_env, dir);
1344 * in case obj is remote obj on its parent, revoke LOOKUP lock,
1345 * herein we don't really check it, just do revoke.
1347 int mdt_revoke_remote_lookup_lock(struct mdt_thread_info *info,
1348 struct mdt_object *pobj,
1349 struct mdt_object *obj)
1351 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1354 mdt_lock_handle_init(lh);
1355 mdt_lock_reg_init(lh, LCK_EX);
1357 if (mdt_object_remote(pobj)) {
1358 /* don't bother to check if pobj and obj are on the same MDT. */
1359 rc = mdt_remote_object_lock(info, pobj, mdt_object_fid(obj),
1360 &lh->mlh_rreg_lh, LCK_EX,
1361 MDS_INODELOCK_LOOKUP, false);
1362 } else if (mdt_object_remote(obj)) {
1363 struct ldlm_res_id *res = &info->mti_res_id;
1364 union ldlm_policy_data *policy = &info->mti_policy;
1365 __u64 dlmflags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB |
1366 LDLM_FL_COS_INCOMPAT;
1368 fid_build_reg_res_name(mdt_object_fid(obj), res);
1369 memset(policy, 0, sizeof(*policy));
1370 policy->l_inodebits.bits = MDS_INODELOCK_LOOKUP;
1371 rc = mdt_fid_lock(info->mti_env, info->mti_mdt->mdt_namespace,
1372 &lh->mlh_reg_lh, LCK_EX, policy, res,
1375 /* do nothing if both are local */
1383 * TODO, currently we don't save this lock because there is no place to
1384 * hold this lock handle, but to avoid race we need to save this lock.
1386 mdt_object_unlock(info, NULL, lh, 1);
1392 * operation may takes locks of linkea, or directory stripes, group them in
1395 struct mdt_sub_lock {
1396 struct mdt_object *msl_obj;
1397 struct mdt_lock_handle msl_lh;
1398 struct list_head msl_linkage;
1401 static void mdt_unlock_list(struct mdt_thread_info *info,
1402 struct list_head *list, int decref)
1404 struct mdt_sub_lock *msl;
1405 struct mdt_sub_lock *tmp;
1407 list_for_each_entry_safe(msl, tmp, list, msl_linkage) {
1408 mdt_object_unlock_put(info, msl->msl_obj, &msl->msl_lh, decref);
1409 list_del(&msl->msl_linkage);
1414 static inline void mdt_migrate_object_unlock(struct mdt_thread_info *info,
1415 struct mdt_object *obj,
1416 struct mdt_lock_handle *lh,
1417 struct ldlm_enqueue_info *einfo,
1418 struct list_head *slave_locks,
1421 if (mdt_object_remote(obj)) {
1422 mdt_unlock_list(info, slave_locks, decref);
1423 mdt_object_unlock(info, obj, lh, decref);
1425 mdt_reint_striped_unlock(info, obj, lh, einfo, decref);
1430 * lock parents of links, and also check whether total locks don't exceed
1433 * \retval 0 on success, and locks can be saved in ptlrpc_reply_stat
1434 * \retval 1 on success, but total lock count may exceed RS_MAX_LOCKS
1435 * \retval -ev negative errno upon error
1437 static int mdt_link_parents_lock(struct mdt_thread_info *info,
1438 struct mdt_object *pobj,
1439 const struct md_attr *ma,
1440 struct mdt_object *obj,
1441 struct mdt_lock_handle *lhp,
1442 struct ldlm_enqueue_info *peinfo,
1443 struct list_head *parent_slave_locks,
1444 struct list_head *link_locks)
1446 struct mdt_device *mdt = info->mti_mdt;
1447 struct lu_buf *buf = &info->mti_big_buf;
1448 struct lu_name *lname = &info->mti_name;
1449 struct linkea_data ldata = { NULL };
1450 bool blocked = false;
1451 int local_lnkp_cnt = 0;
1456 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1459 buf = lu_buf_check_and_alloc(buf, MAX_LINKEA_SIZE);
1460 if (buf->lb_buf == NULL)
1464 rc = mdt_links_read(info, obj, &ldata);
1466 if (rc == -ENOENT || rc == -ENODATA)
1471 for (linkea_first_entry(&ldata); ldata.ld_lee && !rc;
1472 linkea_next_entry(&ldata)) {
1473 struct mdt_object *lnkp;
1474 struct mdt_sub_lock *msl;
1478 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, lname,
1481 /* check if it's also linked to parent */
1482 if (lu_fid_eq(mdt_object_fid(pobj), &fid)) {
1483 CDEBUG(D_INFO, "skip parent "DFID", reovke "DNAME"\n",
1484 PFID(&fid), PNAME(lname));
1485 /* in case link is remote object, revoke LOOKUP lock */
1486 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1492 /* check if it's linked to a stripe of parent */
1493 if (ma->ma_valid & MA_LMV) {
1494 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1495 struct lu_fid *stripe_fid = &info->mti_tmp_fid1;
1498 for (; j < le32_to_cpu(lmv->lmv_stripe_count); j++) {
1499 fid_le_to_cpu(stripe_fid,
1500 &lmv->lmv_stripe_fids[j]);
1501 if (lu_fid_eq(stripe_fid, &fid)) {
1502 CDEBUG(D_INFO, "skip stripe "DFID
1503 ", reovke "DNAME"\n",
1504 PFID(&fid), PNAME(lname));
1505 lnkp = mdt_object_find(info->mti_env,
1508 GOTO(out, rc = PTR_ERR(lnkp));
1514 rc = mdt_revoke_remote_lookup_lock(info, lnkp,
1516 mdt_object_put(info->mti_env, lnkp);
1521 /* Check if it's already locked */
1522 list_for_each_entry(msl, link_locks, msl_linkage) {
1523 if (lu_fid_eq(mdt_object_fid(msl->msl_obj), &fid)) {
1525 DFID" was locked, revoke "DNAME"\n",
1526 PFID(&fid), PNAME(lname));
1527 lnkp = msl->msl_obj;
1533 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1537 CDEBUG(D_INFO, "lock "DFID":"DNAME"\n",
1538 PFID(&fid), PNAME(lname));
1540 lnkp = mdt_object_find(info->mti_env, mdt, &fid);
1542 CWARN("%s: cannot find obj "DFID": %ld\n",
1543 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(lnkp));
1547 if (!mdt_object_exists(lnkp)) {
1548 CDEBUG(D_INFO, DFID" doesn't exist, skip "DNAME"\n",
1549 PFID(&fid), PNAME(lname));
1550 mdt_object_put(info->mti_env, lnkp);
1554 if (!mdt_object_remote(lnkp))
1559 GOTO(out, rc = -ENOMEM);
1562 * we can't follow parent-child lock order like other MD
1563 * operations, use lock_try here to avoid deadlock, if the lock
1564 * cannot be taken, drop all locks taken, revoke the blocked
1565 * one, and continue processing the remaining entries, and in
1566 * the end of the loop restart from beginning.
1568 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1570 rc = mdt_object_lock_try(info, lnkp, &msl->msl_lh, &ibits,
1571 MDS_INODELOCK_UPDATE, true);
1572 if (!(ibits & MDS_INODELOCK_UPDATE)) {
1574 CDEBUG(D_INFO, "busy lock on "DFID" "DNAME"\n",
1575 PFID(&fid), PNAME(lname));
1577 mdt_unlock_list(info, link_locks, 1);
1578 /* also unlock parent locks to avoid deadlock */
1580 mdt_migrate_object_unlock(info, pobj, lhp,
1587 mdt_lock_pdo_init(&msl->msl_lh, LCK_PW, lname);
1588 rc = mdt_object_lock(info, lnkp, &msl->msl_lh,
1589 MDS_INODELOCK_UPDATE);
1591 mdt_object_put(info->mti_env, lnkp);
1596 if (mdt_object_remote(lnkp)) {
1597 struct ldlm_lock *lock;
1600 * for remote object, set lock cb_atomic,
1601 * so lock can be released in blocking_ast()
1602 * immediately, then the next lock_try will
1603 * have better chance of success.
1605 lock = ldlm_handle2lock(
1606 &msl->msl_lh.mlh_rreg_lh);
1607 LASSERT(lock != NULL);
1608 lock_res_and_lock(lock);
1609 ldlm_set_atomic_cb(lock);
1610 unlock_res_and_lock(lock);
1611 LDLM_LOCK_PUT(lock);
1614 mdt_object_unlock_put(info, lnkp, &msl->msl_lh, 1);
1619 INIT_LIST_HEAD(&msl->msl_linkage);
1620 msl->msl_obj = lnkp;
1621 list_add_tail(&msl->msl_linkage, link_locks);
1623 rc = mdt_revoke_remote_lookup_lock(info, lnkp, obj);
1627 GOTO(out, rc = -EBUSY);
1632 mdt_unlock_list(info, link_locks, rc);
1633 else if (local_lnkp_cnt > RS_MAX_LOCKS - 6)
1635 * parent may have 3 local objects: master object and 2 stripes
1636 * (if it's being migrated too); source may have 2 local
1637 * objects: master and 1 stripe; target has 1 local object.
1643 static int mdt_lock_remote_slaves(struct mdt_thread_info *info,
1644 struct mdt_object *obj,
1645 const struct md_attr *ma,
1646 struct list_head *slave_locks)
1648 struct mdt_device *mdt = info->mti_mdt;
1649 const struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1650 struct lu_fid *fid = &info->mti_tmp_fid1;
1651 struct mdt_object *slave;
1652 struct mdt_sub_lock *msl;
1658 LASSERT(mdt_object_remote(obj));
1659 LASSERT(ma->ma_valid & MA_LMV);
1662 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
1665 if (le32_to_cpu(lmv->lmv_stripe_count) < 1)
1668 for (i = 0; i < le32_to_cpu(lmv->lmv_stripe_count); i++) {
1669 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[i]);
1671 if (!fid_is_sane(fid))
1674 slave = mdt_object_find(info->mti_env, mdt, fid);
1676 GOTO(out, rc = PTR_ERR(slave));
1680 mdt_object_put(info->mti_env, slave);
1681 GOTO(out, rc = -ENOMEM);
1684 mdt_lock_reg_init(&msl->msl_lh, LCK_EX);
1685 rc = mdt_reint_object_lock(info, slave, &msl->msl_lh,
1686 MDS_INODELOCK_UPDATE, true);
1689 mdt_object_put(info->mti_env, slave);
1693 INIT_LIST_HEAD(&msl->msl_linkage);
1694 msl->msl_obj = slave;
1695 list_add_tail(&msl->msl_linkage, slave_locks);
1701 mdt_unlock_list(info, slave_locks, rc);
1705 /* lock parent and its stripes */
1706 static int mdt_migrate_parent_lock(struct mdt_thread_info *info,
1707 struct mdt_object *obj,
1708 const struct md_attr *ma,
1709 struct mdt_lock_handle *lh,
1710 struct ldlm_enqueue_info *einfo,
1711 struct list_head *slave_locks)
1715 if (mdt_object_remote(obj)) {
1716 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1717 &lh->mlh_rreg_lh, LCK_PW,
1718 MDS_INODELOCK_UPDATE, false);
1723 * if obj is remote and striped, lock its stripes explicitly
1724 * because it's not striped in LOD layer on this MDT.
1726 if (ma->ma_valid & MA_LMV) {
1727 rc = mdt_lock_remote_slaves(info, obj, ma, slave_locks);
1729 mdt_object_unlock(info, obj, lh, rc);
1732 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_UPDATE,
1740 * in migration, object may be remote, and we need take full lock of it and its
1741 * stripes if it's directory, besides, object may be a remote object on its
1742 * parent, revoke its LOOKUP lock on where its parent is located.
1744 static int mdt_migrate_object_lock(struct mdt_thread_info *info,
1745 struct mdt_object *pobj,
1746 struct mdt_object *obj,
1747 struct mdt_lock_handle *lh,
1748 struct ldlm_enqueue_info *einfo,
1749 struct list_head *slave_locks)
1753 if (mdt_object_remote(obj)) {
1754 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1758 rc = mdt_remote_object_lock(info, obj, mdt_object_fid(obj),
1759 &lh->mlh_rreg_lh, LCK_EX,
1760 MDS_INODELOCK_FULL, false);
1765 * if obj is remote and striped, lock its stripes explicitly
1766 * because it's not striped in LOD layer on this MDT.
1768 if (S_ISDIR(lu_object_attr(&obj->mot_obj))) {
1769 struct md_attr *ma = &info->mti_attr;
1771 ma->ma_lmv = info->mti_big_lmm;
1772 ma->ma_lmv_size = info->mti_big_lmmsize;
1774 rc = mdt_stripe_get(info, obj, ma, XATTR_NAME_LMV);
1776 mdt_object_unlock(info, obj, lh, rc);
1780 if (ma->ma_valid & MA_LMV) {
1781 rc = mdt_lock_remote_slaves(info, obj, ma,
1784 mdt_object_unlock(info, obj, lh, rc);
1788 if (mdt_object_remote(pobj)) {
1789 rc = mdt_revoke_remote_lookup_lock(info, pobj, obj);
1794 rc = mdt_reint_striped_lock(info, obj, lh, MDS_INODELOCK_FULL,
1802 * lookup source by name, if parent is striped directory, we need to find the
1803 * corresponding stripe where source is located, and then lookup there.
1805 * besides, if parent is migrating too, and file is already in target stripe,
1806 * this should be a redo of 'lfs migrate' on client side.
1808 static int mdt_migrate_lookup(struct mdt_thread_info *info,
1809 struct mdt_object *pobj,
1810 const struct md_attr *ma,
1811 const struct lu_name *lname,
1812 struct mdt_object **spobj,
1813 struct mdt_object **sobj)
1815 const struct lu_env *env = info->mti_env;
1816 struct lu_fid *fid = &info->mti_tmp_fid1;
1817 struct mdt_object *stripe;
1820 if (ma->ma_valid & MA_LMV) {
1821 /* if parent is striped, lookup on corresponding stripe */
1822 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1823 __u32 hash_type = le32_to_cpu(lmv->lmv_hash_type);
1824 __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1825 bool is_migrating = le32_to_cpu(lmv->lmv_hash_type) &
1826 LMV_HASH_FLAG_MIGRATION;
1829 hash_type = le32_to_cpu(lmv->lmv_migrate_hash);
1830 stripe_count -= le32_to_cpu(lmv->lmv_migrate_offset);
1833 rc = lmv_name_to_stripe_index(hash_type, stripe_count,
1839 if (le32_to_cpu(lmv->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1840 rc += le32_to_cpu(lmv->lmv_migrate_offset);
1842 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
1844 stripe = mdt_object_find(env, info->mti_mdt, fid);
1846 return PTR_ERR(stripe);
1849 rc = mdo_lookup(env, mdt_object_child(stripe), lname, fid,
1851 if (rc == -ENOENT && is_migrating) {
1853 * if parent is migrating, and lookup child failed on
1854 * source stripe, lookup again on target stripe, if it
1855 * exists, it means previous migration was interrupted,
1856 * and current file was migrated already.
1858 mdt_object_put(env, stripe);
1860 hash_type = le32_to_cpu(lmv->lmv_hash_type);
1861 stripe_count = le32_to_cpu(lmv->lmv_migrate_offset);
1863 rc = lmv_name_to_stripe_index(hash_type, stripe_count,
1869 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[rc]);
1871 stripe = mdt_object_find(env, info->mti_mdt, fid);
1873 return PTR_ERR(stripe);
1876 rc = mdo_lookup(env, mdt_object_child(stripe), lname,
1877 fid, &info->mti_spec);
1878 mdt_object_put(env, stripe);
1879 return rc ?: -EALREADY;
1881 mdt_object_put(env, stripe);
1886 rc = mdo_lookup(env, mdt_object_child(pobj), lname, fid,
1892 mdt_object_get(env, stripe);
1897 *sobj = mdt_object_find(env, info->mti_mdt, fid);
1898 if (IS_ERR(*sobj)) {
1899 mdt_object_put(env, stripe);
1900 rc = PTR_ERR(*sobj);
1908 /* end lease and close file for regular file */
1909 static int mdd_migrate_close(struct mdt_thread_info *info,
1910 struct mdt_object *obj)
1912 struct close_data *data;
1913 struct mdt_body *repbody;
1914 struct ldlm_lock *lease;
1919 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
1921 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
1925 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1930 lease = ldlm_handle2lock(&data->cd_handle);
1934 /* check if the lease was already canceled */
1935 lock_res_and_lock(lease);
1936 rc = ldlm_is_cancel(lease);
1937 unlock_res_and_lock(lease);
1941 LDLM_DEBUG(lease, DFID" lease broken",
1942 PFID(mdt_object_fid(obj)));
1946 * cancel server side lease, client side counterpart should have been
1947 * cancelled, it's okay to cancel it now as we've held mot_open_sem.
1949 ldlm_lock_cancel(lease);
1950 ldlm_reprocess_all(lease->l_resource, lease);
1951 LDLM_LOCK_PUT(lease);
1954 rc2 = mdt_close_internal(info, mdt_info_req(info), NULL);
1955 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1956 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1962 * migrate file in below steps:
1963 * 1. lock parent and its stripes
1964 * 2. lookup source by name
1965 * 3. lock parents of source links if source is not directory
1966 * 4. reject if source is in HSM
1967 * 5. take source open_sem and close file if source is regular file
1968 * 6. lock source and its stripes if it's directory
1969 * 7. lock target so subsequent change to it can trigger COS
1971 * 9. unlock above locks
1972 * 10. sync device if source has links
1974 static int mdt_reint_migrate(struct mdt_thread_info *info,
1975 struct mdt_lock_handle *unused)
1977 const struct lu_env *env = info->mti_env;
1978 struct mdt_device *mdt = info->mti_mdt;
1979 struct ptlrpc_request *req = mdt_info_req(info);
1980 struct mdt_reint_record *rr = &info->mti_rr;
1981 struct lu_ucred *uc = mdt_ucred(info);
1982 struct md_attr *ma = &info->mti_attr;
1983 struct ldlm_enqueue_info *peinfo = &info->mti_einfo[0];
1984 struct ldlm_enqueue_info *seinfo = &info->mti_einfo[1];
1985 struct mdt_object *pobj;
1986 struct mdt_object *spobj = NULL;
1987 struct mdt_object *sobj = NULL;
1988 struct mdt_object *tobj;
1989 struct lustre_handle rename_lh = { 0 };
1990 struct mdt_lock_handle *lhp;
1991 struct mdt_lock_handle *lhs;
1992 struct mdt_lock_handle *lht;
1993 LIST_HEAD(parent_slave_locks);
1994 LIST_HEAD(child_slave_locks);
1995 LIST_HEAD(link_locks);
1996 int lock_retries = 5;
1997 bool open_sem_locked = false;
1998 bool do_sync = false;
2002 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
2003 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
2005 if (info->mti_dlm_req)
2006 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2008 if (!fid_is_md_operative(rr->rr_fid1) ||
2009 !fid_is_md_operative(rr->rr_fid2))
2012 /* don't allow migrate . or .. */
2013 if (lu_name_is_dot_or_dotdot(&rr->rr_name))
2016 if (!mdt->mdt_enable_remote_dir || !mdt->mdt_enable_dir_migration)
2019 if (!md_capable(uc, CFS_CAP_SYS_ADMIN) &&
2020 uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
2021 mdt->mdt_enable_remote_dir_gid != -1)
2025 * Note: do not enqueue rename lock for replay request, because
2026 * if other MDT holds rename lock, but being blocked to wait for
2027 * this MDT to finish its recovery, and the failover MDT can not
2028 * get rename lock, which will cause deadlock.
2030 if (!req_is_replay(req)) {
2031 rc = mdt_rename_lock(info, &rename_lh);
2033 CERROR("%s: can't lock FS for rename: rc = %d\n",
2034 mdt_obd_name(info->mti_mdt), rc);
2039 /* pobj is master object of parent */
2040 pobj = mdt_parent_find_check(info, rr->rr_fid1, 0);
2042 GOTO(unlock_rename, rc = PTR_ERR(pobj));
2044 if (unlikely(!info->mti_big_lmm)) {
2045 info->mti_big_lmmsize = lmv_mds_md_size(64, LMV_MAGIC);
2046 OBD_ALLOC(info->mti_big_lmm, info->mti_big_lmmsize);
2047 if (!info->mti_big_lmm)
2048 GOTO(put_parent, rc = -ENOMEM);
2051 ma->ma_lmv = info->mti_big_lmm;
2052 ma->ma_lmv_size = info->mti_big_lmmsize;
2054 rc = mdt_stripe_get(info, pobj, ma, XATTR_NAME_LMV);
2056 GOTO(put_parent, rc);
2059 /* lock parent object */
2060 lhp = &info->mti_lh[MDT_LH_PARENT];
2061 mdt_lock_reg_init(lhp, LCK_PW);
2062 rc = mdt_migrate_parent_lock(info, pobj, ma, lhp, peinfo,
2063 &parent_slave_locks);
2065 GOTO(put_parent, rc);
2068 * spobj is the corresponding stripe against name if pobj is striped
2069 * directory, which is the real parent, and no need to lock, because
2070 * we've taken full lock of pobj.
2072 rc = mdt_migrate_lookup(info, pobj, ma, &rr->rr_name, &spobj, &sobj);
2074 GOTO(unlock_parent, rc);
2076 /* lock parents of source links, and revoke LOOKUP lock of links */
2077 rc = mdt_link_parents_lock(info, pobj, ma, sobj, lhp, peinfo,
2078 &parent_slave_locks, &link_locks);
2079 if (rc == -EBUSY && lock_retries-- > 0) {
2080 mdt_object_put(env, sobj);
2081 mdt_object_put(env, spobj);
2086 GOTO(put_source, rc);
2089 * RS_MAX_LOCKS is the limit of number of locks that can be saved along
2090 * with one request, if total lock count exceeds this limit, we will
2091 * drop all locks after migration, and synchronous device in the end.
2095 /* TODO: DoM migration is not supported yet */
2096 if (S_ISREG(lu_object_attr(&sobj->mot_obj))) {
2097 ma->ma_lmm = info->mti_big_lmm;
2098 ma->ma_lmm_size = info->mti_big_lmmsize;
2100 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LOV);
2102 GOTO(put_source, rc);
2104 if (ma->ma_valid & MA_LOV &&
2105 mdt_lmm_dom_entry(ma->ma_lmm) != LMM_NO_DOM)
2106 GOTO(put_source, rc = -EOPNOTSUPP);
2109 /* if migration HSM is allowed */
2110 if (!mdt->mdt_opts.mo_migrate_hsm_allowed) {
2111 ma->ma_need = MA_HSM;
2113 rc = mdt_attr_get_complex(info, sobj, ma);
2115 GOTO(unlock_links, rc);
2117 if ((ma->ma_valid & MA_HSM) && ma->ma_hsm.mh_flags != 0)
2118 GOTO(unlock_links, rc = -EOPNOTSUPP);
2121 /* end lease and close file for regular file */
2122 if (info->mti_spec.sp_migrate_close) {
2123 /* try to hold open_sem so that nobody else can open the file */
2124 if (!down_write_trylock(&sobj->mot_open_sem)) {
2126 mdd_migrate_close(info, sobj);
2127 GOTO(unlock_links, rc = -EBUSY);
2129 open_sem_locked = true;
2130 rc = mdd_migrate_close(info, sobj);
2132 GOTO(unlock_open_sem, rc);
2137 lhs = &info->mti_lh[MDT_LH_OLD];
2138 mdt_lock_reg_init(lhs, LCK_EX);
2139 rc = mdt_migrate_object_lock(info, spobj, sobj, lhs, seinfo,
2140 &child_slave_locks);
2142 GOTO(unlock_open_sem, rc);
2145 tobj = mdt_object_find(env, mdt, rr->rr_fid2);
2147 GOTO(unlock_source, rc = PTR_ERR(tobj));
2149 lht = &info->mti_lh[MDT_LH_NEW];
2150 mdt_lock_reg_init(lht, LCK_EX);
2151 rc = mdt_reint_object_lock(info, tobj, lht, MDS_INODELOCK_FULL, true);
2153 GOTO(put_target, rc);
2155 /* Don't do lookup sanity check. We know name doesn't exist. */
2156 info->mti_spec.sp_cr_lookup = 0;
2157 info->mti_spec.sp_feat = &dt_directory_features;
2159 rc = mdo_migrate(env, mdt_object_child(pobj),
2160 mdt_object_child(sobj), &rr->rr_name,
2161 mdt_object_child(tobj), &info->mti_spec, ma);
2164 mdt_object_unlock(info, tobj, lht, rc);
2166 mdt_object_put(env, tobj);
2168 mdt_migrate_object_unlock(info, sobj, lhs, seinfo,
2169 &child_slave_locks, rc);
2171 if (open_sem_locked)
2172 up_write(&sobj->mot_open_sem);
2174 /* if we've got too many locks to save into RPC,
2175 * then just commit before the locks are released */
2177 mdt_device_sync(env, mdt);
2178 mdt_unlock_list(info, &link_locks, do_sync ? 1 : rc);
2180 mdt_object_put(env, sobj);
2181 mdt_object_put(env, spobj);
2183 mdt_migrate_object_unlock(info, pobj, lhp, peinfo,
2184 &parent_slave_locks, rc);
2186 mdt_object_put(env, pobj);
2188 if (lustre_handle_is_used(&rename_lh))
2189 mdt_rename_unlock(&rename_lh);
2194 static int mdt_object_lock_save(struct mdt_thread_info *info,
2195 struct mdt_object *dir,
2196 struct mdt_lock_handle *lh,
2197 int idx, bool cos_incompat)
2201 /* we lock the target dir if it is local */
2202 rc = mdt_reint_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
2207 /* get and save correct version after locking */
2208 mdt_version_get_save(info, dir, idx);
2213 * determine lock order of sobj and tobj
2215 * there are two situations we need to lock tobj before sobj:
2216 * 1. sobj is child of tobj
2217 * 2. sobj and tobj are stripes of a directory, and stripe index of sobj is
2218 * larger than that of tobj
2220 * \retval 1 lock tobj before sobj
2221 * \retval 0 lock sobj before tobj
2222 * \retval -ev negative errno upon error
2224 static int mdt_rename_determine_lock_order(struct mdt_thread_info *info,
2225 struct mdt_object *sobj,
2226 struct mdt_object *tobj)
2228 struct md_attr *ma = &info->mti_attr;
2229 struct lu_fid *spfid = &info->mti_tmp_fid1;
2230 struct lu_fid *tpfid = &info->mti_tmp_fid2;
2231 struct lmv_mds_md_v1 *lmv;
2236 /* sobj and tobj are the same */
2240 if (fid_is_root(mdt_object_fid(sobj)))
2243 if (fid_is_root(mdt_object_fid(tobj)))
2246 /* check whether sobj is child of tobj */
2247 rc = mdo_is_subdir(info->mti_env, mdt_object_child(sobj),
2248 mdt_object_fid(tobj));
2255 /* check whether sobj and tobj are children of the same parent */
2256 rc = mdt_attr_get_pfid(info, sobj, spfid);
2260 rc = mdt_attr_get_pfid(info, tobj, tpfid);
2264 if (!lu_fid_eq(spfid, tpfid))
2267 /* check whether sobj and tobj are sibling stripes */
2268 ma->ma_need = MA_LMV;
2270 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
2271 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
2272 rc = mdt_stripe_get(info, sobj, ma, XATTR_NAME_LMV);
2276 if (!(ma->ma_valid & MA_LMV))
2279 lmv = &ma->ma_lmv->lmv_md_v1;
2280 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2282 sindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2285 rc = mdt_stripe_get(info, tobj, ma, XATTR_NAME_LMV);
2289 if (!(ma->ma_valid & MA_LMV))
2292 lmv = &ma->ma_lmv->lmv_md_v1;
2293 if (!(le32_to_cpu(lmv->lmv_magic) & LMV_MAGIC_STRIPE))
2295 tindex = le32_to_cpu(lmv->lmv_master_mdt_index);
2297 /* check stripe index of sobj and tobj */
2298 if (sindex == tindex)
2301 return sindex < tindex ? 0 : 1;
2305 * VBR: rename versions in reply: 0 - srcdir parent; 1 - tgtdir parent;
2306 * 2 - srcdir child; 3 - tgtdir child.
2307 * Update on disk version of srcdir child.
2309 static int mdt_reint_rename(struct mdt_thread_info *info,
2310 struct mdt_lock_handle *unused)
2312 struct mdt_device *mdt = info->mti_mdt;
2313 struct mdt_reint_record *rr = &info->mti_rr;
2314 struct md_attr *ma = &info->mti_attr;
2315 struct ptlrpc_request *req = mdt_info_req(info);
2316 struct mdt_object *msrcdir = NULL;
2317 struct mdt_object *mtgtdir = NULL;
2318 struct mdt_object *mold;
2319 struct mdt_object *mnew = NULL;
2320 struct lustre_handle rename_lh = { 0 };
2321 struct mdt_lock_handle *lh_srcdirp;
2322 struct mdt_lock_handle *lh_tgtdirp;
2323 struct mdt_lock_handle *lh_oldp = NULL;
2324 struct mdt_lock_handle *lh_newp = NULL;
2325 struct lu_fid *old_fid = &info->mti_tmp_fid1;
2326 struct lu_fid *new_fid = &info->mti_tmp_fid2;
2328 bool reverse = false, discard = false;
2333 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
2334 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
2335 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
2337 if (info->mti_dlm_req)
2338 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2340 if (!fid_is_md_operative(rr->rr_fid1) ||
2341 !fid_is_md_operative(rr->rr_fid2))
2344 /* find both parents. */
2345 msrcdir = mdt_parent_find_check(info, rr->rr_fid1, 0);
2346 if (IS_ERR(msrcdir))
2347 RETURN(PTR_ERR(msrcdir));
2349 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
2351 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
2353 mdt_object_get(info->mti_env, mtgtdir);
2355 mtgtdir = mdt_parent_find_check(info, rr->rr_fid2, 1);
2356 if (IS_ERR(mtgtdir))
2357 GOTO(out_put_srcdir, rc = PTR_ERR(mtgtdir));
2361 * Note: do not enqueue rename lock for replay request, because
2362 * if other MDT holds rename lock, but being blocked to wait for
2363 * this MDT to finish its recovery, and the failover MDT can not
2364 * get rename lock, which will cause deadlock.
2366 if (!req_is_replay(req)) {
2368 * Normally rename RPC is handled on the MDT with the target
2369 * directory (if target exists, it's on the MDT with the
2370 * target), if the source directory is remote, it's a hint that
2371 * source is remote too (this may not be true, but it won't
2372 * cause any issue), return -EXDEV early to avoid taking
2375 if (!mdt->mdt_enable_remote_rename &&
2376 mdt_object_remote(msrcdir))
2377 GOTO(out_put_tgtdir, rc = -EXDEV);
2379 rc = mdt_rename_lock(info, &rename_lh);
2381 CERROR("%s: can't lock FS for rename: rc = %d\n",
2382 mdt_obd_name(mdt), rc);
2383 GOTO(out_put_tgtdir, rc);
2387 rc = mdt_rename_determine_lock_order(info, msrcdir, mtgtdir);
2389 GOTO(out_unlock_rename, rc);
2393 /* source needs to be looked up after locking source parent, otherwise
2394 * this rename may race with unlink source, and cause rename hang, see
2395 * sanityn.sh 55b, so check parents first, if later we found source is
2396 * remote, relock parents. */
2397 cos_incompat = (mdt_object_remote(msrcdir) ||
2398 mdt_object_remote(mtgtdir));
2400 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2402 /* lock parents in the proper order. */
2403 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
2404 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
2407 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
2408 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
2411 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2414 GOTO(out_unlock_rename, rc);
2416 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2418 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2421 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2422 GOTO(out_unlock_rename, rc);
2425 rc = mdt_object_lock_save(info, msrcdir, lh_srcdirp, 0,
2428 GOTO(out_unlock_rename, rc);
2430 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
2432 if (mtgtdir != msrcdir) {
2433 rc = mdt_object_lock_save(info, mtgtdir, lh_tgtdirp, 1,
2435 } else if (!mdt_object_remote(mtgtdir) &&
2436 lh_srcdirp->mlh_pdo_hash !=
2437 lh_tgtdirp->mlh_pdo_hash) {
2438 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
2439 MDS_INODELOCK_UPDATE,
2441 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
2444 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2445 GOTO(out_unlock_rename, rc);
2449 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
2450 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
2452 /* find mold object. */
2454 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
2456 GOTO(out_unlock_parents, rc);
2458 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
2459 GOTO(out_unlock_parents, rc = -EINVAL);
2461 if (!fid_is_md_operative(old_fid))
2462 GOTO(out_unlock_parents, rc = -EPERM);
2464 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
2466 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
2468 if (!mdt_object_exists(mold)) {
2469 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2471 "object does not exist");
2472 GOTO(out_put_old, rc = -ENOENT);
2475 if (mdt_object_remote(mold) && !mdt->mdt_enable_remote_rename)
2476 GOTO(out_put_old, rc = -EXDEV);
2478 /* Check if @mtgtdir is subdir of @mold, before locking child
2479 * to avoid reverse locking. */
2480 if (mtgtdir != msrcdir) {
2481 rc = mdo_is_subdir(info->mti_env, mdt_object_child(mtgtdir),
2486 GOTO(out_put_old, rc);
2490 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
2491 /* save version after locking */
2492 mdt_version_get_save(info, mold, 2);
2494 if (!cos_incompat && mdt_object_remote(mold)) {
2495 cos_incompat = true;
2496 mdt_object_put(info->mti_env, mold);
2497 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, -EAGAIN);
2498 mdt_object_unlock(info, msrcdir, lh_srcdirp, -EAGAIN);
2502 /* find mnew object:
2503 * mnew target object may not exist now
2504 * lookup with version checking */
2506 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
2509 /* the new_fid should have been filled at this moment */
2510 if (lu_fid_eq(old_fid, new_fid))
2511 GOTO(out_put_old, rc);
2513 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
2514 lu_fid_eq(new_fid, rr->rr_fid2))
2515 GOTO(out_put_old, rc = -EINVAL);
2517 if (!fid_is_md_operative(new_fid))
2518 GOTO(out_put_old, rc = -EPERM);
2520 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
2522 GOTO(out_put_old, rc = PTR_ERR(mnew));
2524 if (!mdt_object_exists(mnew)) {
2525 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2527 "object does not exist");
2528 GOTO(out_put_new, rc = -ENOENT);
2531 if (mdt_object_remote(mnew)) {
2532 struct mdt_body *repbody;
2534 /* Always send rename req to the target child MDT */
2535 repbody = req_capsule_server_get(info->mti_pill,
2537 LASSERT(repbody != NULL);
2538 repbody->mbo_fid1 = *new_fid;
2539 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
2540 GOTO(out_put_new, rc = -EXDEV);
2542 /* Before locking the target dir, check we do not replace
2543 * a dir with a non-dir, otherwise it may deadlock with
2544 * link op which tries to create a link in this dir
2545 * back to this non-dir. */
2546 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
2547 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
2548 GOTO(out_put_new, rc = -EISDIR);
2550 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2551 mdt_lock_reg_init(lh_oldp, LCK_EX);
2552 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2553 if (mdt_object_remote(msrcdir)) {
2554 /* Enqueue lookup lock from the parent MDT */
2555 rc = mdt_remote_object_lock(info, msrcdir,
2556 mdt_object_fid(mold),
2557 &lh_oldp->mlh_rreg_lh,
2558 lh_oldp->mlh_rreg_mode,
2559 MDS_INODELOCK_LOOKUP,
2562 GOTO(out_put_new, rc);
2564 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2567 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2570 GOTO(out_unlock_old, rc);
2572 /* Check if @msrcdir is subdir of @mnew, before locking child
2573 * to avoid reverse locking. */
2574 if (mtgtdir != msrcdir) {
2575 rc = mdo_is_subdir(info->mti_env,
2576 mdt_object_child(msrcdir), new_fid);
2580 GOTO(out_unlock_old, rc);
2584 /* We used to acquire MDS_INODELOCK_FULL here but we
2585 * can't do this now because a running HSM restore on
2586 * the rename onto victim will hold the layout
2587 * lock. See LU-4002. */
2589 lh_newp = &info->mti_lh[MDT_LH_NEW];
2590 mdt_lock_reg_init(lh_newp, LCK_EX);
2591 rc = mdt_reint_object_lock(info, mnew, lh_newp,
2592 MDS_INODELOCK_LOOKUP |
2593 MDS_INODELOCK_UPDATE,
2596 GOTO(out_unlock_old, rc);
2598 /* get and save version after locking */
2599 mdt_version_get_save(info, mnew, 3);
2600 } else if (rc != -EREMOTE && rc != -ENOENT) {
2601 GOTO(out_put_old, rc);
2603 lh_oldp = &info->mti_lh[MDT_LH_OLD];
2604 mdt_lock_reg_init(lh_oldp, LCK_EX);
2605 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
2606 if (mdt_object_remote(msrcdir)) {
2607 /* Enqueue lookup lock from the parent MDT */
2608 rc = mdt_remote_object_lock(info, msrcdir,
2609 mdt_object_fid(mold),
2610 &lh_oldp->mlh_rreg_lh,
2611 lh_oldp->mlh_rreg_mode,
2612 MDS_INODELOCK_LOOKUP,
2615 GOTO(out_put_old, rc);
2617 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
2620 rc = mdt_reint_object_lock(info, mold, lh_oldp, lock_ibits,
2623 GOTO(out_unlock_old, rc);
2625 mdt_enoent_version_save(info, 3);
2628 /* step 5: rename it */
2629 mdt_reint_init_ma(info, ma);
2631 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
2632 OBD_FAIL_MDS_REINT_RENAME_WRITE);
2635 mutex_lock(&mnew->mot_lov_mutex);
2637 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
2638 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
2639 mnew != NULL ? mdt_object_child(mnew) : NULL,
2640 &rr->rr_tgt_name, ma);
2643 mutex_unlock(&mnew->mot_lov_mutex);
2645 /* handle last link of tgt object */
2647 mdt_counter_incr(req, LPROC_MDT_RENAME);
2649 mdt_handle_last_unlink(info, mnew, ma);
2650 discard = mdt_dom_check_for_discard(info, mnew);
2652 mdt_rename_counter_tally(info, info->mti_mdt, req,
2658 mdt_object_unlock(info, mnew, lh_newp, rc);
2660 mdt_object_unlock(info, mold, lh_oldp, rc);
2662 if (mnew && !discard)
2663 mdt_object_put(info->mti_env, mnew);
2665 mdt_object_put(info->mti_env, mold);
2667 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
2668 mdt_object_unlock(info, msrcdir, lh_srcdirp, rc);
2670 if (lustre_handle_is_used(&rename_lh))
2671 mdt_rename_unlock(&rename_lh);
2673 mdt_object_put(info->mti_env, mtgtdir);
2675 mdt_object_put(info->mti_env, msrcdir);
2677 /* The DoM discard can be done right in the place above where it is
2678 * assigned, meanwhile it is done here after rename unlock due to
2679 * compatibility with old clients, for them the discard blocks
2680 * the main thread until completion. Check LU-11359 for details.
2683 mdt_dom_discard_data(info, mnew);
2684 mdt_object_put(info->mti_env, mnew);
2689 static int mdt_reint_resync(struct mdt_thread_info *info,
2690 struct mdt_lock_handle *lhc)
2692 struct mdt_reint_record *rr = &info->mti_rr;
2693 struct ptlrpc_request *req = mdt_info_req(info);
2694 struct md_attr *ma = &info->mti_attr;
2695 struct mdt_object *mo;
2696 struct ldlm_lock *lease;
2697 struct mdt_body *repbody;
2698 struct md_layout_change layout = { .mlc_mirror_id = rr->rr_mirror_id };
2704 DEBUG_REQ(D_INODE, req, DFID", FLR file resync", PFID(rr->rr_fid1));
2706 if (info->mti_dlm_req)
2707 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
2709 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
2711 GOTO(out, rc = PTR_ERR(mo));
2713 if (!mdt_object_exists(mo))
2714 GOTO(out_obj, rc = -ENOENT);
2716 if (!S_ISREG(lu_object_attr(&mo->mot_obj)))
2717 GOTO(out_obj, rc = -EINVAL);
2719 if (mdt_object_remote(mo))
2720 GOTO(out_obj, rc = -EREMOTE);
2722 lease = ldlm_handle2lock(rr->rr_lease_handle);
2724 GOTO(out_obj, rc = -ESTALE);
2726 /* It's really necessary to grab open_sem and check if the lease lock
2727 * has been lost. There would exist a concurrent writer coming in and
2728 * generating some dirty data in memory cache, the writeback would fail
2729 * after the layout version is increased by MDS_REINT_RESYNC RPC. */
2730 if (!down_write_trylock(&mo->mot_open_sem))
2731 GOTO(out_put_lease, rc = -EBUSY);
2733 lock_res_and_lock(lease);
2734 lease_broken = ldlm_is_cancel(lease);
2735 unlock_res_and_lock(lease);
2737 GOTO(out_unlock, rc = -EBUSY);
2739 /* the file has yet opened by anyone else after we took the lease. */
2740 layout.mlc_opc = MD_LAYOUT_RESYNC;
2741 lhc = &info->mti_lh[MDT_LH_LOCAL];
2742 rc = mdt_layout_change(info, mo, lhc, &layout);
2744 GOTO(out_unlock, rc);
2746 mdt_object_unlock(info, mo, lhc, 0);
2748 ma->ma_need = MA_INODE;
2750 rc = mdt_attr_get_complex(info, mo, ma);
2752 GOTO(out_unlock, rc);
2754 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2755 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
2759 up_write(&mo->mot_open_sem);
2761 LDLM_LOCK_PUT(lease);
2763 mdt_object_put(info->mti_env, mo);
2765 mdt_client_compatibility(info);
2766 rc2 = mdt_fix_reply(info);
2772 struct mdt_reinter {
2773 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
2774 enum lprocfs_extra_opc mr_extra_opc;
2777 static const struct mdt_reinter mdt_reinters[] = {
2779 .mr_handler = &mdt_reint_setattr,
2780 .mr_extra_opc = MDS_REINT_SETATTR,
2783 .mr_handler = &mdt_reint_create,
2784 .mr_extra_opc = MDS_REINT_CREATE,
2787 .mr_handler = &mdt_reint_link,
2788 .mr_extra_opc = MDS_REINT_LINK,
2791 .mr_handler = &mdt_reint_unlink,
2792 .mr_extra_opc = MDS_REINT_UNLINK,
2795 .mr_handler = &mdt_reint_rename,
2796 .mr_extra_opc = MDS_REINT_RENAME,
2799 .mr_handler = &mdt_reint_open,
2800 .mr_extra_opc = MDS_REINT_OPEN,
2802 [REINT_SETXATTR] = {
2803 .mr_handler = &mdt_reint_setxattr,
2804 .mr_extra_opc = MDS_REINT_SETXATTR,
2807 .mr_handler = &mdt_reint_unlink,
2808 .mr_extra_opc = MDS_REINT_UNLINK,
2811 .mr_handler = &mdt_reint_migrate,
2812 .mr_extra_opc = MDS_REINT_RENAME,
2815 .mr_handler = &mdt_reint_resync,
2816 .mr_extra_opc = MDS_REINT_RESYNC,
2820 int mdt_reint_rec(struct mdt_thread_info *info,
2821 struct mdt_lock_handle *lhc)
2823 const struct mdt_reinter *mr;
2827 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2830 mr = &mdt_reinters[info->mti_rr.rr_opcode];
2831 if (mr->mr_handler == NULL)
2834 rc = (*mr->mr_handler)(info, lhc);
2836 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2837 PTLRPC_LAST_CNTR + mr->mr_extra_opc);