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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_reint.c
38 * Lustre Metadata Target (mdt) reintegration routines
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Huang Hua <huanghua@clusterfs.com>
44 * Author: Yury Umanets <umka@clusterfs.com>
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <lprocfs_status.h>
50 #include "mdt_internal.h"
51 #include <lustre_lmv.h>
53 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
56 ma->ma_need = MA_INODE;
61 * Get version of object by fid.
63 * Return real version or ENOENT_VERSION if object doesn't exist
65 static void mdt_obj_version_get(struct mdt_thread_info *info,
66 struct mdt_object *o, __u64 *version)
69 if (mdt_object_exists(o) && !mdt_object_remote(o) &&
70 !fid_is_obf(mdt_object_fid(o)))
71 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
73 *version = ENOENT_VERSION;
74 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
75 PFID(mdt_object_fid(o)), *version);
79 * Check version is correct.
81 * Should be called only during replay.
83 static int mdt_version_check(struct ptlrpc_request *req,
84 __u64 version, int idx)
86 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
89 if (!exp_connect_vbr(req->rq_export))
92 LASSERT(req_is_replay(req));
93 /** VBR: version is checked always because costs nothing */
94 LASSERT(idx < PTLRPC_NUM_VERSIONS);
95 /** Sanity check for malformed buffers */
96 if (pre_ver == NULL) {
97 CERROR("No versions in request buffer\n");
98 spin_lock(&req->rq_export->exp_lock);
99 req->rq_export->exp_vbr_failed = 1;
100 spin_unlock(&req->rq_export->exp_lock);
102 } else if (pre_ver[idx] != version) {
103 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
104 pre_ver[idx], version);
105 spin_lock(&req->rq_export->exp_lock);
106 req->rq_export->exp_vbr_failed = 1;
107 spin_unlock(&req->rq_export->exp_lock);
114 * Save pre-versions in reply.
116 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
121 if (!exp_connect_vbr(req->rq_export))
124 LASSERT(!req_is_replay(req));
125 LASSERT(req->rq_repmsg != NULL);
126 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
128 reply_ver[idx] = version;
132 * Save enoent version, it is needed when it is obvious that object doesn't
133 * exist, e.g. child during create.
135 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
137 /* save version of file name for replay, it must be ENOENT here */
138 if (!req_is_replay(mdt_info_req(info))) {
139 info->mti_ver[idx] = ENOENT_VERSION;
140 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
145 * Get version from disk and save in reply buffer.
147 * Versions are saved in reply only during normal operations not replays.
149 void mdt_version_get_save(struct mdt_thread_info *info,
150 struct mdt_object *mto, int idx)
152 /* don't save versions during replay */
153 if (!req_is_replay(mdt_info_req(info))) {
154 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
155 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
160 * Get version from disk and check it, no save in reply.
162 int mdt_version_get_check(struct mdt_thread_info *info,
163 struct mdt_object *mto, int idx)
165 /* only check versions during replay */
166 if (!req_is_replay(mdt_info_req(info)))
169 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
170 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
174 * Get version from disk and check if recovery or just save.
176 int mdt_version_get_check_save(struct mdt_thread_info *info,
177 struct mdt_object *mto, int idx)
181 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
182 if (req_is_replay(mdt_info_req(info)))
183 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
186 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
191 * Lookup with version checking.
193 * This checks version of 'name'. Many reint functions uses 'name' for child not
194 * FID, therefore we need to get object by name and check its version.
196 static int mdt_lookup_version_check(struct mdt_thread_info *info,
197 struct mdt_object *p,
198 const struct lu_name *lname,
199 struct lu_fid *fid, int idx)
203 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
205 /* Check version only during replay */
206 if (!req_is_replay(mdt_info_req(info)))
209 info->mti_ver[idx] = ENOENT_VERSION;
211 struct mdt_object *child;
212 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
213 if (likely(!IS_ERR(child))) {
214 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
215 mdt_object_put(info->mti_env, child);
218 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
219 return vbrc ? vbrc : rc;
224 * mdt_remote_permission: Check whether the remote operation is permitted,
226 * Before we implement async cross-MDT updates (DNE phase 2). There are a few
229 * 1.Only sysadmin can create remote directory and striped directory and
230 * migrate directory now, unless
231 * lctl set_param mdt.*.enable_remote_dir_gid=allow_gid.
232 * 2.Remote directory can only be created on MDT0, unless
233 * lctl set_param mdt.*.enable_remote_dir = 1
234 * 3.Only new clients can access remote dir( >= 2.4) and striped dir(>= 2.6),
235 * old client will return -ENOTSUPP.
237 * XXX these check are only needed for remote synchronization, once async
238 * update is supported, these check will be removed.
240 * param[in]info: execution environment.
241 * param[in]parent: the directory of this operation.
242 * param[in]child: the child of this operation.
244 * retval = 0 remote operation is allowed.
245 * < 0 remote operation is denied.
247 static int mdt_remote_permission(struct mdt_thread_info *info,
248 struct mdt_object *parent,
249 struct mdt_object *child)
251 struct mdt_device *mdt = info->mti_mdt;
252 struct lu_ucred *uc = mdt_ucred(info);
253 struct md_op_spec *spec = &info->mti_spec;
254 struct lu_attr *attr = &info->mti_attr.ma_attr;
255 struct obd_export *exp = mdt_info_req(info)->rq_export;
257 /* Only check create remote directory, striped directory and
259 if (mdt_object_remote(parent) == 0 && mdt_object_remote(child) == 0 &&
260 !(S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
261 spec->u.sp_ea.eadatalen != 0) &&
262 info->mti_rr.rr_opcode != REINT_MIGRATE)
265 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
266 if (uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
267 mdt->mdt_enable_remote_dir_gid != -1)
271 if (mdt->mdt_enable_remote_dir == 0) {
272 struct seq_server_site *ss = mdt_seq_site(mdt);
273 struct lu_seq_range range = { 0 };
276 fld_range_set_type(&range, LU_SEQ_RANGE_MDT);
277 rc = fld_server_lookup(info->mti_env, ss->ss_server_fld,
278 fid_seq(mdt_object_fid(parent)), &range);
282 if (range.lsr_index != 0)
286 if (!mdt_is_dne_client(exp))
289 if (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
290 spec->u.sp_ea.eadatalen != 0) {
291 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
293 if (le32_to_cpu(lum->lum_stripe_count) > 1 &&
294 !mdt_is_striped_client(exp))
302 * VBR: we save three versions in reply:
303 * 0 - parent. Check that parent version is the same during replay.
304 * 1 - name. Version of 'name' if file exists with the same name or
305 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
306 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
309 static int mdt_md_create(struct mdt_thread_info *info)
311 struct mdt_device *mdt = info->mti_mdt;
312 struct mdt_object *parent;
313 struct mdt_object *child;
314 struct mdt_lock_handle *lh;
315 struct mdt_body *repbody;
316 struct md_attr *ma = &info->mti_attr;
317 struct mdt_reint_record *rr = &info->mti_rr;
321 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create ("DNAME"->"DFID") "
323 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
325 if (!fid_is_md_operative(rr->rr_fid1))
328 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
330 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
332 RETURN(PTR_ERR(parent));
334 if (!mdt_object_exists(parent))
335 GOTO(put_parent, rc = -ENOENT);
337 lh = &info->mti_lh[MDT_LH_PARENT];
338 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
339 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE,
342 GOTO(put_parent, rc);
344 if (!mdt_object_remote(parent)) {
345 rc = mdt_version_get_check_save(info, parent, 0);
347 GOTO(unlock_parent, rc);
351 * Check child name version during replay.
352 * During create replay a file may exist with same name.
354 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
355 &info->mti_tmp_fid1, 1);
357 GOTO(unlock_parent, rc = -EEXIST);
359 /* -ENOENT is expected here */
361 GOTO(unlock_parent, rc);
363 /* save version of file name for replay, it must be ENOENT here */
364 mdt_enoent_version_save(info, 1);
366 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
367 if (likely(!IS_ERR(child))) {
368 struct md_object *next = mdt_object_child(parent);
370 rc = mdt_remote_permission(info, parent, child);
372 GOTO(out_put_child, rc);
374 ma->ma_need = MA_INODE;
377 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
378 OBD_FAIL_MDS_REINT_CREATE_WRITE);
380 /* Version of child will be updated on disk. */
381 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
382 rc = mdt_version_get_check_save(info, child, 2);
384 GOTO(out_put_child, rc);
386 /* Let lower layer know current lock mode. */
387 info->mti_spec.sp_cr_mode =
388 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
391 * Do not perform lookup sanity check. We know that name does
394 info->mti_spec.sp_cr_lookup = 0;
395 info->mti_spec.sp_feat = &dt_directory_features;
397 rc = mdo_create(info->mti_env, next, &rr->rr_name,
398 mdt_object_child(child), &info->mti_spec, ma);
400 rc = mdt_attr_get_complex(info, child, ma);
403 /* Return fid & attr to client. */
404 if (ma->ma_valid & MA_INODE)
405 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
406 mdt_object_fid(child));
409 mdt_object_put(info->mti_env, child);
414 mdt_object_unlock(info, parent, lh, rc);
416 mdt_object_put(info->mti_env, parent);
420 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
421 struct mdt_object *obj, __u64 ibits,
422 struct mdt_lock_handle *s0_lh,
423 struct mdt_object *s0_obj,
424 struct ldlm_enqueue_info *einfo)
426 ldlm_policy_data_t *policy = &mti->mti_policy;
430 if (!S_ISDIR(obj->mot_header.loh_attr))
433 /* Unlock stripe 0 */
434 if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
435 LASSERT(s0_obj != NULL);
436 mdt_object_unlock_put(mti, s0_obj, s0_lh, 1);
439 memset(policy, 0, sizeof(*policy));
440 policy->l_inodebits.bits = ibits;
442 rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
448 * Lock slave stripes if necessary, the lock handles of slave stripes
449 * will be stored in einfo->ei_cbdata.
451 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
452 ldlm_mode_t mode, __u64 ibits,
453 struct mdt_lock_handle *s0_lh,
454 struct mdt_object **s0_objp,
455 struct ldlm_enqueue_info *einfo)
457 ldlm_policy_data_t *policy = &mti->mti_policy;
458 struct lu_buf *buf = &mti->mti_buf;
459 struct lmv_mds_md_v1 *lmv;
460 struct lu_fid *fid = &mti->mti_tmp_fid1;
464 if (!S_ISDIR(obj->mot_header.loh_attr))
467 buf->lb_buf = mti->mti_xattr_buf;
468 buf->lb_len = sizeof(mti->mti_xattr_buf);
469 rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
472 rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
474 buf->lb_buf = mti->mti_big_lmm;
475 buf->lb_len = mti->mti_big_lmmsize;
479 if (rc == -ENODATA || rc == -ENOENT)
486 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
489 /* Sigh, 0_stripe and master object are different
490 * object, though they are in the same MDT, to avoid
491 * adding osd_object_lock here, so we will enqueue the
492 * stripe0 lock in MDT0 for now */
493 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
494 *s0_objp = mdt_object_find_lock(mti, fid, s0_lh, ibits);
495 if (IS_ERR(*s0_objp))
496 RETURN(PTR_ERR(*s0_objp));
498 memset(einfo, 0, sizeof(*einfo));
499 einfo->ei_type = LDLM_IBITS;
500 einfo->ei_mode = mode;
501 einfo->ei_cb_bl = mdt_remote_blocking_ast;
502 einfo->ei_cb_cp = ldlm_completion_ast;
503 einfo->ei_enq_slave = 1;
504 memset(policy, 0, sizeof(*policy));
505 policy->l_inodebits.bits = ibits;
507 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
512 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
515 struct mdt_lock_handle *lh;
516 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
517 __u64 lockpart = MDS_INODELOCK_UPDATE;
518 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
519 struct mdt_lock_handle *s0_lh;
520 struct mdt_object *s0_obj = NULL;
524 lh = &info->mti_lh[MDT_LH_PARENT];
525 mdt_lock_reg_init(lh, LCK_PW);
527 /* Even though the new MDT will grant PERM lock to the old
528 * client, but the old client will almost ignore that during
529 * So it needs to revoke both LOOKUP and PERM lock here, so
530 * both new and old client can cancel the dcache */
531 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
532 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
534 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
538 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
539 mdt_lock_reg_init(s0_lh, LCK_PW);
540 rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
542 GOTO(out_unlock, rc);
544 /* all attrs are packed into mti_attr in unpack_setattr */
545 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
546 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
548 /* This is only for set ctime when rename's source is on remote MDS. */
549 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
550 ma->ma_attr_flags |= MDS_VTX_BYPASS;
552 /* VBR: update version if attr changed are important for recovery */
554 /* update on-disk version of changed object */
555 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
556 rc = mdt_version_get_check_save(info, mo, 0);
558 GOTO(out_unlock, rc);
561 /* Ensure constant striping during chown(). See LU-2789. */
562 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
563 mutex_lock(&mo->mot_lov_mutex);
565 /* all attrs are packed into mti_attr in unpack_setattr */
566 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
568 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
569 mutex_unlock(&mo->mot_lov_mutex);
572 GOTO(out_unlock, rc);
576 mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
577 mdt_object_unlock(info, mo, lh, rc);
582 * Check HSM flags and add HS_DIRTY flag if relevant.
584 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
585 * and is not RELEASED.
587 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
593 /* If the file was modified, add the dirty flag */
594 ma->ma_need = MA_HSM;
595 rc = mdt_attr_get_complex(info, mo, ma);
597 CERROR("file attribute read error for "DFID": %d.\n",
598 PFID(mdt_object_fid(mo)), rc);
602 /* If an up2date copy exists in the backend, add dirty flag */
603 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
604 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
605 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
607 ma->ma_hsm.mh_flags |= HS_DIRTY;
609 mdt_lock_reg_init(lh, LCK_PW);
610 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
615 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
617 CERROR("file attribute change error for "DFID": %d\n",
618 PFID(mdt_object_fid(mo)), rc);
619 mdt_object_unlock(info, mo, lh, rc);
625 static int mdt_reint_setattr(struct mdt_thread_info *info,
626 struct mdt_lock_handle *lhc)
628 struct md_attr *ma = &info->mti_attr;
629 struct mdt_reint_record *rr = &info->mti_rr;
630 struct ptlrpc_request *req = mdt_info_req(info);
631 struct mdt_object *mo;
632 struct mdt_body *repbody;
636 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
637 (unsigned int)ma->ma_attr.la_valid);
639 if (info->mti_dlm_req)
640 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
642 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
643 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
645 GOTO(out, rc = PTR_ERR(mo));
647 if (!mdt_object_exists(mo))
648 GOTO(out_put, rc = -ENOENT);
650 if (mdt_object_remote(mo))
651 GOTO(out_put, rc = -EREMOTE);
653 if ((ma->ma_attr.la_valid & LA_SIZE) ||
654 (rr->rr_flags & MRF_OPEN_TRUNC)) {
655 /* Check write access for the O_TRUNC case */
656 if (mdt_write_read(mo) < 0)
657 GOTO(out_put, rc = -ETXTBSY);
660 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
661 if (ma->ma_valid & MA_LOV)
662 GOTO(out_put, rc = -EPROTO);
664 rc = mdt_attr_set(info, mo, ma);
667 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
668 struct lu_buf *buf = &info->mti_buf;
670 if (ma->ma_attr.la_valid != 0)
671 GOTO(out_put, rc = -EPROTO);
673 buf->lb_buf = ma->ma_lmm;
674 buf->lb_len = ma->ma_lmm_size;
675 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
676 buf, XATTR_NAME_LOV, 0);
679 } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
680 struct lu_buf *buf = &info->mti_buf;
681 struct mdt_lock_handle *lh;
683 if (ma->ma_attr.la_valid != 0)
684 GOTO(out_put, rc = -EPROTO);
686 lh = &info->mti_lh[MDT_LH_PARENT];
687 mdt_lock_reg_init(lh, LCK_PW);
689 rc = mdt_object_lock(info, mo, lh,
695 buf->lb_buf = ma->ma_lmv;
696 buf->lb_len = ma->ma_lmv_size;
697 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
698 buf, XATTR_NAME_DEFAULT_LMV, 0);
700 mdt_object_unlock(info, mo, lh, rc);
704 GOTO(out_put, rc = -EPROTO);
707 /* If file data is modified, add the dirty flag */
708 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
709 rc = mdt_add_dirty_flag(info, mo, ma);
711 ma->ma_need = MA_INODE;
713 rc = mdt_attr_get_complex(info, mo, ma);
717 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
721 mdt_object_put(info->mti_env, mo);
724 mdt_counter_incr(req, LPROC_MDT_SETATTR);
726 mdt_client_compatibility(info);
727 rc2 = mdt_fix_reply(info);
733 static int mdt_reint_create(struct mdt_thread_info *info,
734 struct mdt_lock_handle *lhc)
736 struct ptlrpc_request *req = mdt_info_req(info);
740 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
741 RETURN(err_serious(-ESTALE));
743 if (info->mti_dlm_req)
744 ldlm_request_cancel(mdt_info_req(info),
745 info->mti_dlm_req, 0, LATF_SKIP);
747 if (!lu_name_is_valid(&info->mti_rr.rr_name))
750 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
752 mdt_counter_incr(req, LPROC_MDT_MKDIR);
760 /* Special file should stay on the same node as parent. */
761 mdt_counter_incr(req, LPROC_MDT_MKNOD);
764 CERROR("%s: Unsupported mode %o\n",
765 mdt_obd_name(info->mti_mdt),
766 info->mti_attr.ma_attr.la_mode);
767 RETURN(err_serious(-EOPNOTSUPP));
770 rc = mdt_md_create(info);
775 * VBR: save parent version in reply and child version getting by its name.
776 * Version of child is getting and checking during its lookup. If
778 static int mdt_reint_unlink(struct mdt_thread_info *info,
779 struct mdt_lock_handle *lhc)
781 struct mdt_reint_record *rr = &info->mti_rr;
782 struct ptlrpc_request *req = mdt_info_req(info);
783 struct md_attr *ma = &info->mti_attr;
784 struct lu_fid *child_fid = &info->mti_tmp_fid1;
785 struct mdt_object *mp;
786 struct mdt_object *mc;
787 struct mdt_lock_handle *parent_lh;
788 struct mdt_lock_handle *child_lh;
789 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
790 struct mdt_lock_handle *s0_lh = NULL;
791 struct mdt_object *s0_obj = NULL;
796 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
797 PNAME(&rr->rr_name));
799 if (info->mti_dlm_req)
800 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
802 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
803 RETURN(err_serious(-ENOENT));
805 if (!fid_is_md_operative(rr->rr_fid1))
809 * step 1: Found the parent.
811 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
817 parent_lh = &info->mti_lh[MDT_LH_PARENT];
818 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
819 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
822 GOTO(put_parent, rc);
824 if (!mdt_object_remote(mp)) {
825 rc = mdt_version_get_check_save(info, mp, 0);
827 GOTO(unlock_parent, rc);
830 /* step 2: find & lock the child */
831 /* lookup child object along with version checking */
833 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
835 /* Name might not be able to find during resend of
836 * remote unlink, considering following case.
837 * dir_A is a remote directory, the name entry of
838 * dir_A is on MDT0, the directory is on MDT1,
840 * 1. client sends unlink req to MDT1.
841 * 2. MDT1 sends name delete update to MDT0.
842 * 3. name entry is being deleted in MDT0 synchronously.
843 * 4. MDT1 is restarted.
844 * 5. client resends unlink req to MDT1. So it can not
845 * find the name entry on MDT0 anymore.
846 * In this case, MDT1 only needs to destory the local
849 if (mdt_object_remote(mp) && rc == -ENOENT &&
850 !fid_is_zero(rr->rr_fid2) &&
851 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
853 *child_fid = *rr->rr_fid2;
855 GOTO(unlock_parent, rc);
859 if (!fid_is_md_operative(child_fid))
860 GOTO(unlock_parent, rc = -EPERM);
862 /* We will lock the child regardless it is local or remote. No harm. */
863 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
865 GOTO(unlock_parent, rc = PTR_ERR(mc));
867 child_lh = &info->mti_lh[MDT_LH_CHILD];
868 mdt_lock_reg_init(child_lh, LCK_EX);
869 if (mdt_object_remote(mc)) {
870 struct mdt_body *repbody;
872 if (!fid_is_zero(rr->rr_fid2)) {
873 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
874 mdt_obd_name(info->mti_mdt),
875 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
876 GOTO(put_child, rc = -ENOENT);
878 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
879 mdt_obd_name(info->mti_mdt),
880 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
882 if (!mdt_is_dne_client(req->rq_export))
883 /* Return -ENOTSUPP for old client */
884 GOTO(put_child, rc = -ENOTSUPP);
886 if (info->mti_spec.sp_rm_entry) {
887 struct lu_ucred *uc = mdt_ucred(info);
889 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
890 CERROR("%s: unlink remote entry is only "
891 "permitted for administrator: rc = %d\n",
892 mdt_obd_name(info->mti_mdt),
894 GOTO(put_child, rc = -EPERM);
897 ma->ma_need = MA_INODE;
899 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
900 NULL, &rr->rr_name, ma, no_name);
903 /* Revoke the LOOKUP lock of the remote object granted by
904 * this MDT. Since the unlink will happen on another MDT,
905 * it will release the LOOKUP lock right away. Then What
906 * would happen if another client try to grab the LOOKUP
907 * lock at the same time with unlink XXX */
908 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
910 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
911 LASSERT(repbody != NULL);
912 repbody->mbo_fid1 = *mdt_object_fid(mc);
913 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
914 GOTO(unlock_child, rc = -EREMOTE);
915 } else if (info->mti_spec.sp_rm_entry) {
917 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
919 mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
923 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
924 * this now because a running HSM restore on the child (unlink
925 * victim) will hold the layout lock. See LU-4002. */
926 rc = mdt_object_lock(info, mc, child_lh,
927 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
932 * Now we can only make sure we need MA_INODE, in mdd layer, will check
933 * whether need MA_LOV and MA_COOKIE.
935 ma->ma_need = MA_INODE;
938 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
939 mdt_lock_reg_init(s0_lh, LCK_EX);
940 rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
943 GOTO(unlock_child, rc);
945 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
946 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
947 /* save version when object is locked */
948 mdt_version_get_save(info, mc, 1);
950 mutex_lock(&mc->mot_lov_mutex);
952 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
953 mdt_object_child(mc), &rr->rr_name, ma, no_name);
955 mutex_unlock(&mc->mot_lov_mutex);
957 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
958 rc = mdt_attr_get_complex(info, mc, ma);
960 mdt_handle_last_unlink(info, mc, ma);
962 if (ma->ma_valid & MA_INODE) {
963 switch (ma->ma_attr.la_mode & S_IFMT) {
965 mdt_counter_incr(req, LPROC_MDT_RMDIR);
973 mdt_counter_incr(req, LPROC_MDT_UNLINK);
976 LASSERTF(0, "bad file type %o unlinking\n",
977 ma->ma_attr.la_mode);
984 mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
985 mdt_object_unlock(info, mc, child_lh, rc);
987 mdt_object_put(info->mti_env, mc);
989 mdt_object_unlock(info, mp, parent_lh, rc);
991 mdt_object_put(info->mti_env, mp);
997 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1000 static int mdt_reint_link(struct mdt_thread_info *info,
1001 struct mdt_lock_handle *lhc)
1003 struct mdt_reint_record *rr = &info->mti_rr;
1004 struct ptlrpc_request *req = mdt_info_req(info);
1005 struct md_attr *ma = &info->mti_attr;
1006 struct mdt_object *ms;
1007 struct mdt_object *mp;
1008 struct mdt_lock_handle *lhs;
1009 struct mdt_lock_handle *lhp;
1013 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1014 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1016 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1017 RETURN(err_serious(-ENOENT));
1019 if (info->mti_dlm_req)
1020 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1022 /* Invalid case so return error immediately instead of
1024 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1027 if (!fid_is_md_operative(rr->rr_fid1) ||
1028 !fid_is_md_operative(rr->rr_fid2))
1031 /* step 1: find & lock the target parent dir */
1032 lhp = &info->mti_lh[MDT_LH_PARENT];
1033 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1034 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1035 MDS_INODELOCK_UPDATE);
1037 RETURN(PTR_ERR(mp));
1039 rc = mdt_version_get_check_save(info, mp, 0);
1041 GOTO(out_unlock_parent, rc);
1043 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1045 /* step 2: find & lock the source */
1046 lhs = &info->mti_lh[MDT_LH_CHILD];
1047 mdt_lock_reg_init(lhs, LCK_EX);
1049 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1051 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1053 if (!mdt_object_exists(ms)) {
1054 mdt_object_put(info->mti_env, ms);
1055 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1056 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1057 GOTO(out_unlock_parent, rc = -ENOENT);
1060 if (mdt_object_remote(ms)) {
1061 mdt_object_put(info->mti_env, ms);
1062 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1063 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1065 GOTO(out_unlock_parent, rc = -EXDEV);
1068 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1069 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1071 mdt_object_put(info->mti_env, ms);
1072 GOTO(out_unlock_parent, rc);
1075 /* step 3: link it */
1076 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1077 OBD_FAIL_MDS_REINT_LINK_WRITE);
1079 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1080 rc = mdt_version_get_check_save(info, ms, 1);
1082 GOTO(out_unlock_child, rc);
1084 /** check target version by name during replay */
1085 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1086 &info->mti_tmp_fid1, 2);
1087 if (rc != 0 && rc != -ENOENT)
1088 GOTO(out_unlock_child, rc);
1089 /* save version of file name for replay, it must be ENOENT here */
1090 if (!req_is_replay(mdt_info_req(info))) {
1091 if (rc != -ENOENT) {
1092 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1093 PNAME(&rr->rr_name));
1094 GOTO(out_unlock_child, rc = -EEXIST);
1096 info->mti_ver[2] = ENOENT_VERSION;
1097 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1100 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1101 mdt_object_child(ms), &rr->rr_name, ma);
1104 mdt_counter_incr(req, LPROC_MDT_LINK);
1108 mdt_object_unlock_put(info, ms, lhs, rc);
1110 mdt_object_unlock_put(info, mp, lhp, rc);
1114 * lock the part of the directory according to the hash of the name
1115 * (lh->mlh_pdo_hash) in parallel directory lock.
1117 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1118 struct mdt_lock_handle *lh,
1119 struct mdt_object *obj, __u64 ibits)
1121 struct ldlm_res_id *res = &info->mti_res_id;
1122 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1123 ldlm_policy_data_t *policy = &info->mti_policy;
1127 * Finish res_id initializing by name hash marking part of
1128 * directory which is taking modification.
1130 LASSERT(lh->mlh_pdo_hash != 0);
1131 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1132 memset(policy, 0, sizeof(*policy));
1133 policy->l_inodebits.bits = ibits;
1135 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1136 * going to be sent to client. If it is - mdt_intent_policy() path will
1137 * fix it up and turn FL_LOCAL flag off.
1139 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1140 res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1141 &info->mti_exp->exp_handle.h_cookie);
1145 enum mdt_rename_lock {
1151 * Get BFL lock for rename or migrate process, right now, it does not support
1152 * cross-MDT rename, so we only need global rename lock during migration.
1154 static int mdt_rename_lock(struct mdt_thread_info *info,
1155 struct lustre_handle *lh,
1156 enum mdt_rename_lock rename_lock)
1158 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1159 ldlm_policy_data_t *policy = &info->mti_policy;
1160 struct ldlm_res_id *res_id = &info->mti_res_id;
1165 /* XXX only do global rename lock for migration */
1166 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0 &&
1167 rename_lock == MRL_MIGRATE) {
1168 struct lu_fid *fid = &info->mti_tmp_fid1;
1169 struct mdt_object *obj;
1171 /* XXX, right now, it has to use object API to
1172 * enqueue lock cross MDT, so it will enqueue
1173 * rename lock(with LUSTRE_BFL_FID) by root object */
1175 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1177 RETURN(PTR_ERR(obj));
1179 LASSERT(mdt_object_remote(obj));
1180 rc = mdt_remote_object_lock(info, obj,
1181 &LUSTRE_BFL_FID, lh,
1183 MDS_INODELOCK_UPDATE);
1184 mdt_object_put(info->mti_env, obj);
1186 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1187 memset(policy, 0, sizeof *policy);
1188 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1189 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1190 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1191 LCK_EX, &flags, ldlm_blocking_ast,
1192 ldlm_completion_ast, NULL, NULL, 0,
1194 &info->mti_exp->exp_handle.h_cookie,
1201 static void mdt_rename_unlock(struct lustre_handle *lh)
1204 LASSERT(lustre_handle_is_used(lh));
1205 /* Cancel the single rename lock right away */
1206 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1211 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1212 * target. Source should not be ancestor of target dir. May be other rename
1213 * checks can be moved here later.
1215 static int mdt_is_subdir(struct mdt_thread_info *info,
1216 struct mdt_object *dir,
1217 const struct lu_fid *fid)
1219 struct lu_fid dir_fid = dir->mot_header.loh_fid;
1223 /* If the source and target are in the same directory, they can not
1224 * be parent/child relationship, so subdir check is not needed */
1225 if (lu_fid_eq(&dir_fid, fid))
1228 if (!mdt_object_exists(dir))
1231 rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1234 CERROR("%s: failed subdir check in "DFID" for "DFID
1235 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1236 PFID(&dir_fid), PFID(fid), rc);
1237 /* Return EINVAL only if a parent is the @fid */
1241 /* check the found fid */
1242 if (lu_fid_eq(&dir_fid, fid))
1249 /* Update object linkEA */
1250 struct mdt_lock_list {
1251 struct mdt_object *mll_obj;
1252 struct mdt_lock_handle mll_lh;
1253 struct list_head mll_list;
1256 static void mdt_unlock_list(struct mdt_thread_info *info,
1257 struct list_head *list, int rc)
1259 struct mdt_lock_list *mll;
1260 struct mdt_lock_list *mll2;
1262 list_for_each_entry_safe(mll, mll2, list, mll_list) {
1263 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1264 list_del(&mll->mll_list);
1269 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1270 struct mdt_object *obj,
1271 struct mdt_object *pobj,
1272 struct list_head *lock_list)
1274 struct lu_buf *buf = &info->mti_big_buf;
1275 struct linkea_data ldata = { NULL };
1280 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1283 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1284 if (buf->lb_buf == NULL)
1288 rc = mdt_links_read(info, obj, &ldata);
1290 if (rc == -ENOENT || rc == -ENODATA)
1295 LASSERT(ldata.ld_leh != NULL);
1296 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1297 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1298 struct mdt_device *mdt = info->mti_mdt;
1299 struct mdt_object *mdt_pobj;
1300 struct mdt_lock_list *mll;
1301 struct lu_name name;
1304 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1306 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1308 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1309 if (IS_ERR(mdt_pobj)) {
1310 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1311 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1315 if (!mdt_object_exists(mdt_pobj)) {
1316 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1317 mdt_obd_name(mdt), PFID(&fid));
1318 mdt_object_put(info->mti_env, mdt_pobj);
1322 if (mdt_pobj == pobj) {
1323 CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1324 mdt_obd_name(mdt), PFID(&fid));
1325 mdt_object_put(info->mti_env, mdt_pobj);
1331 mdt_object_put(info->mti_env, mdt_pobj);
1332 GOTO(out, rc = -ENOMEM);
1335 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1336 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1337 MDS_INODELOCK_UPDATE,
1340 CERROR("%s: cannot lock "DFID": rc =%d\n",
1341 mdt_obd_name(mdt), PFID(&fid), rc);
1342 mdt_object_put(info->mti_env, mdt_pobj);
1347 INIT_LIST_HEAD(&mll->mll_list);
1348 mll->mll_obj = mdt_pobj;
1349 list_add_tail(&mll->mll_list, lock_list);
1353 mdt_unlock_list(info, lock_list, rc);
1357 /* migrate files from one MDT to another MDT */
1358 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1359 struct mdt_lock_handle *lhc)
1361 struct mdt_reint_record *rr = &info->mti_rr;
1362 struct md_attr *ma = &info->mti_attr;
1363 struct mdt_object *msrcdir;
1364 struct mdt_object *mold;
1365 struct mdt_object *mnew = NULL;
1366 struct mdt_lock_handle *lh_dirp;
1367 struct mdt_lock_handle *lh_childp;
1368 struct mdt_lock_handle *lh_tgtp = NULL;
1369 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1370 struct list_head lock_list;
1374 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1375 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1377 /* 1: lock the source dir. */
1378 msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1379 if (IS_ERR(msrcdir)) {
1380 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1381 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1382 (int)PTR_ERR(msrcdir));
1383 RETURN(PTR_ERR(msrcdir));
1386 lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1387 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1388 rc = mdt_object_lock(info, msrcdir, lh_dirp,
1389 MDS_INODELOCK_UPDATE,
1392 GOTO(out_put_parent, rc);
1394 if (!mdt_object_remote(msrcdir)) {
1395 rc = mdt_version_get_check_save(info, msrcdir, 0);
1397 GOTO(out_unlock_parent, rc);
1400 /* 2: sanity check and find the object to be migrated. */
1402 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1404 GOTO(out_unlock_parent, rc);
1406 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1407 GOTO(out_unlock_parent, rc = -EINVAL);
1409 if (!fid_is_md_operative(old_fid))
1410 GOTO(out_unlock_parent, rc = -EPERM);
1412 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1414 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1416 if (mdt_object_remote(mold)) {
1417 CERROR("%s: source "DFID" is on the remote MDT\n",
1418 mdt_obd_name(info->mti_mdt), PFID(old_fid));
1419 GOTO(out_put_child, rc = -EREMOTE);
1422 if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1423 !mdt_object_remote(msrcdir)) {
1424 CERROR("%s: parent "DFID" is still on the same"
1425 " MDT, which should be migrated first:"
1426 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1427 PFID(mdt_object_fid(msrcdir)), -EPERM);
1428 GOTO(out_put_child, rc = -EPERM);
1431 rc = mdt_remote_permission(info, msrcdir, mold);
1433 GOTO(out_put_child, rc);
1435 /* 3: iterate the linkea of the object and lock all of the objects */
1436 INIT_LIST_HEAD(&lock_list);
1437 rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1439 GOTO(out_put_child, rc);
1441 /* 4: lock of the object migrated object */
1442 lh_childp = &info->mti_lh[MDT_LH_OLD];
1443 mdt_lock_reg_init(lh_childp, LCK_EX);
1444 rc = mdt_object_lock(info, mold, lh_childp,
1445 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1446 MDS_INODELOCK_LAYOUT, MDT_CROSS_LOCK);
1448 GOTO(out_unlock_list, rc);
1450 ma->ma_need = MA_LMV;
1452 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1453 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1454 rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1456 GOTO(out_unlock_list, rc);
1458 if ((ma->ma_valid & MA_LMV)) {
1459 struct lmv_mds_md_v1 *lmm1;
1461 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1462 lmm1 = &ma->ma_lmv->lmv_md_v1;
1463 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1464 CERROR("%s: can not migrate striped dir "DFID
1465 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1466 PFID(mdt_object_fid(mold)), -EPERM);
1467 GOTO(out_unlock_child, rc = -EPERM);
1470 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1471 GOTO(out_unlock_child, rc = -EINVAL);
1473 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1474 &lmm1->lmv_stripe_fids[1]);
1476 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1478 if (!mdt_object_remote(mnew)) {
1479 CERROR("%s: "DFID" being migrated is on this MDT:"
1480 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1481 PFID(rr->rr_fid2), -EPERM);
1482 GOTO(out_put_new, rc = -EPERM);
1485 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1486 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1487 rc = mdt_remote_object_lock(info, mnew,
1488 mdt_object_fid(mnew),
1489 &lh_tgtp->mlh_rreg_lh,
1490 lh_tgtp->mlh_rreg_mode,
1491 MDS_INODELOCK_UPDATE);
1494 GOTO(out_put_new, rc);
1497 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1500 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1501 if (!mdt_object_remote(mnew)) {
1502 CERROR("%s: Migration "DFID" is on this MDT:"
1503 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1504 PFID(rr->rr_fid2), -EXDEV);
1505 GOTO(out_put_new, rc = -EXDEV);
1510 mdt_reint_init_ma(info, ma);
1512 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1513 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1515 rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1516 mdt_object_child(mold), &rr->rr_name,
1517 mdt_object_child(mnew), ma);
1519 GOTO(out_unlock_new, rc);
1521 if (lh_tgtp != NULL)
1522 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1525 mdt_object_put(info->mti_env, mnew);
1527 mdt_object_unlock(info, mold, lh_childp, rc);
1529 mdt_unlock_list(info, &lock_list, rc);
1531 mdt_object_put(info->mti_env, mold);
1533 mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1535 mdt_object_put(info->mti_env, msrcdir);
1540 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1541 const struct lu_fid *fid,
1544 struct mdt_object *dir;
1548 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1552 /* check early, the real version will be saved after locking */
1553 rc = mdt_version_get_check(info, dir, idx);
1559 mdt_object_put(info->mti_env, dir);
1563 static int mdt_object_lock_save(struct mdt_thread_info *info,
1564 struct mdt_object *dir,
1565 struct mdt_lock_handle *lh,
1570 /* we lock the target dir if it is local */
1571 rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
1576 /* get and save correct version after locking */
1577 mdt_version_get_save(info, dir, idx);
1582 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1583 struct mdt_object **srcp,
1584 struct mdt_object **tgtp)
1586 struct mdt_reint_record *rr = &info->mti_rr;
1587 const struct lu_fid *fid_src = rr->rr_fid1;
1588 const struct lu_fid *fid_tgt = rr->rr_fid2;
1589 struct mdt_lock_handle *lh_src = &info->mti_lh[MDT_LH_PARENT];
1590 struct mdt_lock_handle *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1591 struct mdt_object *src;
1592 struct mdt_object *tgt;
1597 /* find both parents. */
1598 src = mdt_object_find_check(info, fid_src, 0);
1600 RETURN(PTR_ERR(src));
1602 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1604 if (lu_fid_eq(fid_src, fid_tgt)) {
1606 mdt_object_get(info->mti_env, tgt);
1608 /* Check if the @src is not a child of the @tgt, otherwise a
1609 * reverse locking must take place. */
1610 rc = mdt_is_subdir(info, src, fid_tgt);
1614 GOTO(err_src_put, rc);
1616 tgt = mdt_object_find_check(info, fid_tgt, 1);
1618 GOTO(err_src_put, rc = PTR_ERR(tgt));
1620 if (unlikely(mdt_object_remote(tgt))) {
1621 CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1622 "on different MDTs\n", PFID(fid_src),
1624 GOTO(err_tgt_put, rc = -EXDEV);
1628 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1630 /* lock parents in the proper order. */
1632 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1634 GOTO(err_tgt_put, rc);
1636 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1638 rc = mdt_object_lock_save(info, src, lh_src, 0);
1640 rc = mdt_object_lock_save(info, src, lh_src, 0);
1642 GOTO(err_tgt_put, rc);
1644 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1647 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1648 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1649 rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1650 MDS_INODELOCK_UPDATE);
1651 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1655 GOTO(err_unlock, rc);
1657 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1664 /* The order does not matter as the handle is checked inside,
1665 * as well as not used handle. */
1666 mdt_object_unlock(info, src, lh_src, rc);
1667 mdt_object_unlock(info, tgt, lh_tgt, rc);
1669 mdt_object_put(info->mti_env, tgt);
1671 mdt_object_put(info->mti_env, src);
1676 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1677 * 2 - src child; 3 - tgt child.
1678 * Update on disk version of src child.
1681 * For DNE phase I, only these renames are allowed
1682 * mv src_p/src_c tgt_p/tgt_c
1683 * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1684 * 2. src_p and tgt_p are same directory, and tgt_c does not
1685 * exists. In this case, all of modification will happen
1686 * in the MDT where ithesource parent is, only one remote
1687 * update is needed, i.e. set c_time/m_time on the child.
1688 * And tgt_c will be still in the same MDT as the original
1691 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1692 struct mdt_lock_handle *lhc)
1694 struct mdt_reint_record *rr = &info->mti_rr;
1695 struct md_attr *ma = &info->mti_attr;
1696 struct ptlrpc_request *req = mdt_info_req(info);
1697 struct mdt_object *msrcdir = NULL;
1698 struct mdt_object *mtgtdir = NULL;
1699 struct mdt_object *mold;
1700 struct mdt_object *mnew = NULL;
1701 struct mdt_lock_handle *lh_srcdirp;
1702 struct mdt_lock_handle *lh_tgtdirp;
1703 struct mdt_lock_handle *lh_oldp = NULL;
1704 struct mdt_lock_handle *lh_newp = NULL;
1705 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1706 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1710 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1711 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1712 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1714 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1715 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1716 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1717 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1719 /* step 1&2: lock the source and target dirs. */
1720 rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1724 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1726 /* step 3: find & lock the old object. */
1728 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1730 GOTO(out_unlock_parents, rc);
1732 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1733 GOTO(out_unlock_parents, rc = -EINVAL);
1735 if (!fid_is_md_operative(old_fid))
1736 GOTO(out_unlock_parents, rc = -EPERM);
1738 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1740 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1742 /* Check if @mtgtdir is subdir of @mold, before locking child
1743 * to avoid reverse locking. */
1744 rc = mdt_is_subdir(info, mtgtdir, old_fid);
1746 GOTO(out_put_old, rc);
1748 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1749 /* save version after locking */
1750 mdt_version_get_save(info, mold, 2);
1752 /* step 4: find & lock the new object. */
1753 /* new target object may not exist now */
1754 /* lookup with version checking */
1756 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1759 /* the new_fid should have been filled at this moment */
1760 if (lu_fid_eq(old_fid, new_fid))
1761 GOTO(out_put_old, rc);
1763 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1764 lu_fid_eq(new_fid, rr->rr_fid2))
1765 GOTO(out_put_old, rc = -EINVAL);
1767 if (!fid_is_md_operative(new_fid))
1768 GOTO(out_put_old, rc = -EPERM);
1770 if (mdt_object_remote(mold)) {
1771 CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1773 GOTO(out_put_old, rc = -EXDEV);
1776 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1778 GOTO(out_put_old, rc = PTR_ERR(mnew));
1780 if (mdt_object_remote(mnew)) {
1781 CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1783 GOTO(out_put_new, rc = -EXDEV);
1786 /* Before locking the target dir, check we do not replace
1787 * a dir with a non-dir, otherwise it may deadlock with
1788 * link op which tries to create a link in this dir
1789 * back to this non-dir. */
1790 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1791 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1792 GOTO(out_put_new, rc = -EISDIR);
1794 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1795 mdt_lock_reg_init(lh_oldp, LCK_EX);
1796 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1797 MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1799 GOTO(out_put_new, rc);
1801 /* Check if @msrcdir is subdir of @mnew, before locking child
1802 * to avoid reverse locking. */
1803 rc = mdt_is_subdir(info, msrcdir, new_fid);
1805 GOTO(out_unlock_old, rc);
1807 /* We used to acquire MDS_INODELOCK_FULL here but we
1808 * can't do this now because a running HSM restore on
1809 * the rename onto victim will hold the layout
1810 * lock. See LU-4002. */
1812 lh_newp = &info->mti_lh[MDT_LH_NEW];
1813 mdt_lock_reg_init(lh_newp, LCK_EX);
1814 rc = mdt_object_lock(info, mnew, lh_newp,
1815 MDS_INODELOCK_LOOKUP |
1816 MDS_INODELOCK_UPDATE,
1819 GOTO(out_unlock_old, rc);
1821 /* get and save version after locking */
1822 mdt_version_get_save(info, mnew, 3);
1823 } else if (rc != -EREMOTE && rc != -ENOENT) {
1824 GOTO(out_put_old, rc);
1826 /* If mnew does not exist and mold are remote directory,
1827 * it only allows rename if they are under same directory */
1828 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1829 CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1831 GOTO(out_put_old, rc = -EXDEV);
1834 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1835 mdt_lock_reg_init(lh_oldp, LCK_EX);
1836 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1837 MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1839 GOTO(out_put_old, rc);
1841 mdt_enoent_version_save(info, 3);
1844 /* step 5: rename it */
1845 mdt_reint_init_ma(info, ma);
1847 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1848 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1851 mutex_lock(&mnew->mot_lov_mutex);
1853 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1854 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1855 mnew != NULL ? mdt_object_child(mnew) : NULL,
1856 &rr->rr_tgt_name, ma);
1859 mutex_unlock(&mnew->mot_lov_mutex);
1861 /* handle last link of tgt object */
1863 mdt_counter_incr(req, LPROC_MDT_RENAME);
1865 mdt_handle_last_unlink(info, mnew, ma);
1867 mdt_rename_counter_tally(info, info->mti_mdt, req,
1873 mdt_object_unlock(info, mnew, lh_newp, rc);
1875 mdt_object_unlock(info, mold, lh_oldp, rc);
1878 mdt_object_put(info->mti_env, mnew);
1880 mdt_object_put(info->mti_env, mold);
1882 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1883 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1887 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1888 struct mdt_lock_handle *lhc,
1889 enum mdt_rename_lock rename_lock)
1891 struct mdt_reint_record *rr = &info->mti_rr;
1892 struct ptlrpc_request *req = mdt_info_req(info);
1893 struct lustre_handle rename_lh = { 0 };
1897 if (info->mti_dlm_req)
1898 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1900 if (!fid_is_md_operative(rr->rr_fid1) ||
1901 !fid_is_md_operative(rr->rr_fid2))
1904 rc = mdt_rename_lock(info, &rename_lh, rename_lock);
1906 CERROR("%s: can't lock FS for rename: rc = %d\n",
1907 mdt_obd_name(info->mti_mdt), rc);
1911 if (rename_lock == MRL_RENAME)
1912 rc = mdt_reint_rename_internal(info, lhc);
1914 rc = mdt_reint_migrate_internal(info, lhc);
1916 if (lustre_handle_is_used(&rename_lh))
1917 mdt_rename_unlock(&rename_lh);
1922 static int mdt_reint_rename(struct mdt_thread_info *info,
1923 struct mdt_lock_handle *lhc)
1925 return mdt_reint_rename_or_migrate(info, lhc, MRL_RENAME);
1928 static int mdt_reint_migrate(struct mdt_thread_info *info,
1929 struct mdt_lock_handle *lhc)
1931 return mdt_reint_rename_or_migrate(info, lhc, MRL_MIGRATE);
1934 struct mdt_reinter {
1935 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
1936 enum lprocfs_extra_opc mr_extra_opc;
1939 static const struct mdt_reinter mdt_reinters[] = {
1941 .mr_handler = &mdt_reint_setattr,
1942 .mr_extra_opc = MDS_REINT_SETATTR,
1945 .mr_handler = &mdt_reint_create,
1946 .mr_extra_opc = MDS_REINT_CREATE,
1949 .mr_handler = &mdt_reint_link,
1950 .mr_extra_opc = MDS_REINT_LINK,
1953 .mr_handler = &mdt_reint_unlink,
1954 .mr_extra_opc = MDS_REINT_UNLINK,
1957 .mr_handler = &mdt_reint_rename,
1958 .mr_extra_opc = MDS_REINT_RENAME,
1961 .mr_handler = &mdt_reint_open,
1962 .mr_extra_opc = MDS_REINT_OPEN,
1964 [REINT_SETXATTR] = {
1965 .mr_handler = &mdt_reint_setxattr,
1966 .mr_extra_opc = MDS_REINT_SETXATTR,
1969 .mr_handler = &mdt_reint_unlink,
1970 .mr_extra_opc = MDS_REINT_UNLINK,
1973 .mr_handler = &mdt_reint_migrate,
1974 .mr_extra_opc = MDS_REINT_RENAME,
1978 int mdt_reint_rec(struct mdt_thread_info *info,
1979 struct mdt_lock_handle *lhc)
1981 const struct mdt_reinter *mr;
1985 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
1988 mr = &mdt_reinters[info->mti_rr.rr_opcode];
1989 if (mr->mr_handler == NULL)
1992 rc = (*mr->mr_handler)(info, lhc);
1994 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
1995 PTLRPC_LAST_CNTR + mr->mr_extra_opc);