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;
790 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
791 struct mdt_lock_handle *s0_lh = NULL;
792 struct mdt_object *s0_obj = NULL;
797 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
798 PNAME(&rr->rr_name));
800 if (info->mti_dlm_req)
801 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
803 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
804 RETURN(err_serious(-ENOENT));
806 if (!fid_is_md_operative(rr->rr_fid1))
810 * step 1: Found the parent.
812 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
818 parent_lh = &info->mti_lh[MDT_LH_PARENT];
819 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
820 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
823 GOTO(put_parent, rc);
825 if (!mdt_object_remote(mp)) {
826 rc = mdt_version_get_check_save(info, mp, 0);
828 GOTO(unlock_parent, rc);
831 /* step 2: find & lock the child */
832 /* lookup child object along with version checking */
834 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
836 /* Name might not be able to find during resend of
837 * remote unlink, considering following case.
838 * dir_A is a remote directory, the name entry of
839 * dir_A is on MDT0, the directory is on MDT1,
841 * 1. client sends unlink req to MDT1.
842 * 2. MDT1 sends name delete update to MDT0.
843 * 3. name entry is being deleted in MDT0 synchronously.
844 * 4. MDT1 is restarted.
845 * 5. client resends unlink req to MDT1. So it can not
846 * find the name entry on MDT0 anymore.
847 * In this case, MDT1 only needs to destory the local
850 if (mdt_object_remote(mp) && rc == -ENOENT &&
851 !fid_is_zero(rr->rr_fid2) &&
852 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
854 *child_fid = *rr->rr_fid2;
856 GOTO(unlock_parent, rc);
860 if (!fid_is_md_operative(child_fid))
861 GOTO(unlock_parent, rc = -EPERM);
863 /* We will lock the child regardless it is local or remote. No harm. */
864 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
866 GOTO(unlock_parent, rc = PTR_ERR(mc));
868 child_lh = &info->mti_lh[MDT_LH_CHILD];
869 mdt_lock_reg_init(child_lh, LCK_EX);
870 if (mdt_object_remote(mc)) {
871 struct mdt_body *repbody;
873 if (!fid_is_zero(rr->rr_fid2)) {
874 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
875 mdt_obd_name(info->mti_mdt),
876 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
877 GOTO(put_child, rc = -ENOENT);
879 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
880 mdt_obd_name(info->mti_mdt),
881 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
883 if (!mdt_is_dne_client(req->rq_export))
884 /* Return -ENOTSUPP for old client */
885 GOTO(put_child, rc = -ENOTSUPP);
887 if (info->mti_spec.sp_rm_entry) {
888 struct lu_ucred *uc = mdt_ucred(info);
890 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
891 CERROR("%s: unlink remote entry is only "
892 "permitted for administrator: rc = %d\n",
893 mdt_obd_name(info->mti_mdt),
895 GOTO(put_child, rc = -EPERM);
898 ma->ma_need = MA_INODE;
900 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
901 NULL, &rr->rr_name, ma, no_name);
904 /* Revoke the LOOKUP lock of the remote object granted by
905 * this MDT. Since the unlink will happen on another MDT,
906 * it will release the LOOKUP lock right away. Then What
907 * would happen if another client try to grab the LOOKUP
908 * lock at the same time with unlink XXX */
909 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
911 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
912 LASSERT(repbody != NULL);
913 repbody->mbo_fid1 = *mdt_object_fid(mc);
914 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
915 GOTO(unlock_child, rc = -EREMOTE);
916 } else if (info->mti_spec.sp_rm_entry) {
918 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
920 mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
924 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
925 * this now because a running HSM restore on the child (unlink
926 * victim) will hold the layout lock. See LU-4002. */
927 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
928 if (mdt_object_remote(mp)) {
929 /* Enqueue lookup lock from parent MDT */
930 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
931 &child_lh->mlh_rreg_lh,
932 child_lh->mlh_rreg_mode,
933 MDS_INODELOCK_LOOKUP);
937 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
940 rc = mdt_object_lock(info, mc, child_lh, lock_ibits, MDT_CROSS_LOCK);
944 * Now we can only make sure we need MA_INODE, in mdd layer, will check
945 * whether need MA_LOV and MA_COOKIE.
947 ma->ma_need = MA_INODE;
950 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
951 mdt_lock_reg_init(s0_lh, LCK_EX);
952 rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
955 GOTO(unlock_child, rc);
957 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
958 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
959 /* save version when object is locked */
960 mdt_version_get_save(info, mc, 1);
962 mutex_lock(&mc->mot_lov_mutex);
964 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
965 mdt_object_child(mc), &rr->rr_name, ma, no_name);
967 mutex_unlock(&mc->mot_lov_mutex);
969 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
970 rc = mdt_attr_get_complex(info, mc, ma);
972 mdt_handle_last_unlink(info, mc, ma);
974 if (ma->ma_valid & MA_INODE) {
975 switch (ma->ma_attr.la_mode & S_IFMT) {
977 mdt_counter_incr(req, LPROC_MDT_RMDIR);
985 mdt_counter_incr(req, LPROC_MDT_UNLINK);
988 LASSERTF(0, "bad file type %o unlinking\n",
989 ma->ma_attr.la_mode);
996 mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
997 mdt_object_unlock(info, mc, child_lh, rc);
999 mdt_object_put(info->mti_env, mc);
1001 mdt_object_unlock(info, mp, parent_lh, rc);
1003 mdt_object_put(info->mti_env, mp);
1009 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1012 static int mdt_reint_link(struct mdt_thread_info *info,
1013 struct mdt_lock_handle *lhc)
1015 struct mdt_reint_record *rr = &info->mti_rr;
1016 struct ptlrpc_request *req = mdt_info_req(info);
1017 struct md_attr *ma = &info->mti_attr;
1018 struct mdt_object *ms;
1019 struct mdt_object *mp;
1020 struct mdt_lock_handle *lhs;
1021 struct mdt_lock_handle *lhp;
1025 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1026 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1028 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1029 RETURN(err_serious(-ENOENT));
1031 if (info->mti_dlm_req)
1032 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1034 /* Invalid case so return error immediately instead of
1036 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1039 if (!fid_is_md_operative(rr->rr_fid1) ||
1040 !fid_is_md_operative(rr->rr_fid2))
1043 /* step 1: find & lock the target parent dir */
1044 lhp = &info->mti_lh[MDT_LH_PARENT];
1045 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1046 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1047 MDS_INODELOCK_UPDATE);
1049 RETURN(PTR_ERR(mp));
1051 rc = mdt_version_get_check_save(info, mp, 0);
1053 GOTO(out_unlock_parent, rc);
1055 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1057 /* step 2: find & lock the source */
1058 lhs = &info->mti_lh[MDT_LH_CHILD];
1059 mdt_lock_reg_init(lhs, LCK_EX);
1061 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1063 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1065 if (!mdt_object_exists(ms)) {
1066 mdt_object_put(info->mti_env, ms);
1067 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1068 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1069 GOTO(out_unlock_parent, rc = -ENOENT);
1072 if (mdt_object_remote(ms)) {
1073 mdt_object_put(info->mti_env, ms);
1074 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1075 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1077 GOTO(out_unlock_parent, rc = -EXDEV);
1080 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1081 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1083 mdt_object_put(info->mti_env, ms);
1084 GOTO(out_unlock_parent, rc);
1087 /* step 3: link it */
1088 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1089 OBD_FAIL_MDS_REINT_LINK_WRITE);
1091 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1092 rc = mdt_version_get_check_save(info, ms, 1);
1094 GOTO(out_unlock_child, rc);
1096 /** check target version by name during replay */
1097 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1098 &info->mti_tmp_fid1, 2);
1099 if (rc != 0 && rc != -ENOENT)
1100 GOTO(out_unlock_child, rc);
1101 /* save version of file name for replay, it must be ENOENT here */
1102 if (!req_is_replay(mdt_info_req(info))) {
1103 if (rc != -ENOENT) {
1104 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1105 PNAME(&rr->rr_name));
1106 GOTO(out_unlock_child, rc = -EEXIST);
1108 info->mti_ver[2] = ENOENT_VERSION;
1109 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1112 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1113 mdt_object_child(ms), &rr->rr_name, ma);
1116 mdt_counter_incr(req, LPROC_MDT_LINK);
1120 mdt_object_unlock_put(info, ms, lhs, rc);
1122 mdt_object_unlock_put(info, mp, lhp, rc);
1126 * lock the part of the directory according to the hash of the name
1127 * (lh->mlh_pdo_hash) in parallel directory lock.
1129 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1130 struct mdt_lock_handle *lh,
1131 struct mdt_object *obj, __u64 ibits)
1133 struct ldlm_res_id *res = &info->mti_res_id;
1134 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1135 ldlm_policy_data_t *policy = &info->mti_policy;
1139 * Finish res_id initializing by name hash marking part of
1140 * directory which is taking modification.
1142 LASSERT(lh->mlh_pdo_hash != 0);
1143 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1144 memset(policy, 0, sizeof(*policy));
1145 policy->l_inodebits.bits = ibits;
1147 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1148 * going to be sent to client. If it is - mdt_intent_policy() path will
1149 * fix it up and turn FL_LOCAL flag off.
1151 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1152 res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1153 &info->mti_exp->exp_handle.h_cookie);
1157 enum mdt_rename_lock {
1163 * Get BFL lock for rename or migrate process, right now, it does not support
1164 * cross-MDT rename, so we only need global rename lock during migration.
1166 static int mdt_rename_lock(struct mdt_thread_info *info,
1167 struct lustre_handle *lh,
1168 enum mdt_rename_lock rename_lock)
1170 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1171 ldlm_policy_data_t *policy = &info->mti_policy;
1172 struct ldlm_res_id *res_id = &info->mti_res_id;
1177 /* XXX only do global rename lock for migration */
1178 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0 &&
1179 rename_lock == MRL_MIGRATE) {
1180 struct lu_fid *fid = &info->mti_tmp_fid1;
1181 struct mdt_object *obj;
1183 /* XXX, right now, it has to use object API to
1184 * enqueue lock cross MDT, so it will enqueue
1185 * rename lock(with LUSTRE_BFL_FID) by root object */
1187 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1189 RETURN(PTR_ERR(obj));
1191 LASSERT(mdt_object_remote(obj));
1192 rc = mdt_remote_object_lock(info, obj,
1193 &LUSTRE_BFL_FID, lh,
1195 MDS_INODELOCK_UPDATE);
1196 mdt_object_put(info->mti_env, obj);
1198 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1199 memset(policy, 0, sizeof *policy);
1200 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1201 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1202 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1203 LCK_EX, &flags, ldlm_blocking_ast,
1204 ldlm_completion_ast, NULL, NULL, 0,
1206 &info->mti_exp->exp_handle.h_cookie,
1213 static void mdt_rename_unlock(struct lustre_handle *lh)
1216 LASSERT(lustre_handle_is_used(lh));
1217 /* Cancel the single rename lock right away */
1218 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1223 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1224 * target. Source should not be ancestor of target dir. May be other rename
1225 * checks can be moved here later.
1227 static int mdt_is_subdir(struct mdt_thread_info *info,
1228 struct mdt_object *dir,
1229 const struct lu_fid *fid)
1231 struct lu_fid dir_fid = dir->mot_header.loh_fid;
1235 /* If the source and target are in the same directory, they can not
1236 * be parent/child relationship, so subdir check is not needed */
1237 if (lu_fid_eq(&dir_fid, fid))
1240 if (!mdt_object_exists(dir))
1243 rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1246 CERROR("%s: failed subdir check in "DFID" for "DFID
1247 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1248 PFID(&dir_fid), PFID(fid), rc);
1249 /* Return EINVAL only if a parent is the @fid */
1253 /* check the found fid */
1254 if (lu_fid_eq(&dir_fid, fid))
1261 /* Update object linkEA */
1262 struct mdt_lock_list {
1263 struct mdt_object *mll_obj;
1264 struct mdt_lock_handle mll_lh;
1265 struct list_head mll_list;
1268 static void mdt_unlock_list(struct mdt_thread_info *info,
1269 struct list_head *list, int rc)
1271 struct mdt_lock_list *mll;
1272 struct mdt_lock_list *mll2;
1274 list_for_each_entry_safe(mll, mll2, list, mll_list) {
1275 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1276 list_del(&mll->mll_list);
1281 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1282 struct mdt_object *obj,
1283 struct mdt_object *pobj,
1284 struct list_head *lock_list)
1286 struct lu_buf *buf = &info->mti_big_buf;
1287 struct linkea_data ldata = { NULL };
1292 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1295 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1296 if (buf->lb_buf == NULL)
1300 rc = mdt_links_read(info, obj, &ldata);
1302 if (rc == -ENOENT || rc == -ENODATA)
1307 LASSERT(ldata.ld_leh != NULL);
1308 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1309 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1310 struct mdt_device *mdt = info->mti_mdt;
1311 struct mdt_object *mdt_pobj;
1312 struct mdt_lock_list *mll;
1313 struct lu_name name;
1316 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1318 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1320 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1321 if (IS_ERR(mdt_pobj)) {
1322 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1323 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1327 if (!mdt_object_exists(mdt_pobj)) {
1328 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1329 mdt_obd_name(mdt), PFID(&fid));
1330 mdt_object_put(info->mti_env, mdt_pobj);
1334 if (mdt_pobj == pobj) {
1335 CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1336 mdt_obd_name(mdt), PFID(&fid));
1337 mdt_object_put(info->mti_env, mdt_pobj);
1343 mdt_object_put(info->mti_env, mdt_pobj);
1344 GOTO(out, rc = -ENOMEM);
1347 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1348 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1349 MDS_INODELOCK_UPDATE,
1352 CERROR("%s: cannot lock "DFID": rc =%d\n",
1353 mdt_obd_name(mdt), PFID(&fid), rc);
1354 mdt_object_put(info->mti_env, mdt_pobj);
1359 INIT_LIST_HEAD(&mll->mll_list);
1360 mll->mll_obj = mdt_pobj;
1361 list_add_tail(&mll->mll_list, lock_list);
1365 mdt_unlock_list(info, lock_list, rc);
1369 /* migrate files from one MDT to another MDT */
1370 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1371 struct mdt_lock_handle *lhc)
1373 struct mdt_reint_record *rr = &info->mti_rr;
1374 struct md_attr *ma = &info->mti_attr;
1375 struct mdt_object *msrcdir;
1376 struct mdt_object *mold;
1377 struct mdt_object *mnew = NULL;
1378 struct mdt_lock_handle *lh_dirp;
1379 struct mdt_lock_handle *lh_childp;
1380 struct mdt_lock_handle *lh_tgtp = NULL;
1381 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1382 struct list_head lock_list;
1387 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1388 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1390 /* 1: lock the source dir. */
1391 msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1392 if (IS_ERR(msrcdir)) {
1393 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1394 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1395 (int)PTR_ERR(msrcdir));
1396 RETURN(PTR_ERR(msrcdir));
1399 lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1400 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1401 rc = mdt_object_lock(info, msrcdir, lh_dirp,
1402 MDS_INODELOCK_UPDATE,
1405 GOTO(out_put_parent, rc);
1407 if (!mdt_object_remote(msrcdir)) {
1408 rc = mdt_version_get_check_save(info, msrcdir, 0);
1410 GOTO(out_unlock_parent, rc);
1413 /* 2: sanity check and find the object to be migrated. */
1415 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1417 GOTO(out_unlock_parent, rc);
1419 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1420 GOTO(out_unlock_parent, rc = -EINVAL);
1422 if (!fid_is_md_operative(old_fid))
1423 GOTO(out_unlock_parent, rc = -EPERM);
1425 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1427 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1429 if (mdt_object_remote(mold)) {
1430 CERROR("%s: source "DFID" is on the remote MDT\n",
1431 mdt_obd_name(info->mti_mdt), PFID(old_fid));
1432 GOTO(out_put_child, rc = -EREMOTE);
1435 if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1436 !mdt_object_remote(msrcdir)) {
1437 CERROR("%s: parent "DFID" is still on the same"
1438 " MDT, which should be migrated first:"
1439 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1440 PFID(mdt_object_fid(msrcdir)), -EPERM);
1441 GOTO(out_put_child, rc = -EPERM);
1444 rc = mdt_remote_permission(info, msrcdir, mold);
1446 GOTO(out_put_child, rc);
1448 /* 3: iterate the linkea of the object and lock all of the objects */
1449 INIT_LIST_HEAD(&lock_list);
1450 rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1452 GOTO(out_put_child, rc);
1454 /* 4: lock of the object migrated object */
1455 lh_childp = &info->mti_lh[MDT_LH_OLD];
1456 mdt_lock_reg_init(lh_childp, LCK_EX);
1457 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1458 MDS_INODELOCK_LAYOUT;
1459 if (mdt_object_remote(msrcdir)) {
1460 /* Enqueue lookup lock from the parent MDT */
1461 rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
1462 &lh_childp->mlh_rreg_lh,
1463 lh_childp->mlh_rreg_mode,
1464 MDS_INODELOCK_LOOKUP);
1466 GOTO(out_unlock_list, rc);
1468 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1471 rc = mdt_object_lock(info, mold, lh_childp, lock_ibits, MDT_CROSS_LOCK);
1473 GOTO(out_unlock_child, rc);
1475 ma->ma_need = MA_LMV;
1477 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1478 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1479 rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1481 GOTO(out_unlock_child, rc);
1483 if ((ma->ma_valid & MA_LMV)) {
1484 struct lmv_mds_md_v1 *lmm1;
1486 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1487 lmm1 = &ma->ma_lmv->lmv_md_v1;
1488 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1489 CERROR("%s: can not migrate striped dir "DFID
1490 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1491 PFID(mdt_object_fid(mold)), -EPERM);
1492 GOTO(out_unlock_child, rc = -EPERM);
1495 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1496 GOTO(out_unlock_child, rc = -EINVAL);
1498 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1499 &lmm1->lmv_stripe_fids[1]);
1501 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1503 if (!mdt_object_remote(mnew)) {
1504 CERROR("%s: "DFID" being migrated is on this MDT:"
1505 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1506 PFID(rr->rr_fid2), -EPERM);
1507 GOTO(out_put_new, rc = -EPERM);
1510 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1511 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1512 rc = mdt_remote_object_lock(info, mnew,
1513 mdt_object_fid(mnew),
1514 &lh_tgtp->mlh_rreg_lh,
1515 lh_tgtp->mlh_rreg_mode,
1516 MDS_INODELOCK_UPDATE);
1519 GOTO(out_put_new, rc);
1522 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1525 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1526 if (!mdt_object_remote(mnew)) {
1527 CERROR("%s: Migration "DFID" is on this MDT:"
1528 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1529 PFID(rr->rr_fid2), -EXDEV);
1530 GOTO(out_put_new, rc = -EXDEV);
1535 mdt_reint_init_ma(info, ma);
1537 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1538 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1540 rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1541 mdt_object_child(mold), &rr->rr_name,
1542 mdt_object_child(mnew), ma);
1544 GOTO(out_unlock_new, rc);
1546 if (lh_tgtp != NULL)
1547 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1550 mdt_object_put(info->mti_env, mnew);
1552 mdt_object_unlock(info, mold, lh_childp, rc);
1554 mdt_unlock_list(info, &lock_list, rc);
1556 mdt_object_put(info->mti_env, mold);
1558 mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1560 mdt_object_put(info->mti_env, msrcdir);
1565 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1566 const struct lu_fid *fid,
1569 struct mdt_object *dir;
1573 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1577 /* check early, the real version will be saved after locking */
1578 rc = mdt_version_get_check(info, dir, idx);
1584 mdt_object_put(info->mti_env, dir);
1588 static int mdt_object_lock_save(struct mdt_thread_info *info,
1589 struct mdt_object *dir,
1590 struct mdt_lock_handle *lh,
1595 /* we lock the target dir if it is local */
1596 rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
1601 /* get and save correct version after locking */
1602 mdt_version_get_save(info, dir, idx);
1607 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1608 struct mdt_object **srcp,
1609 struct mdt_object **tgtp)
1611 struct mdt_reint_record *rr = &info->mti_rr;
1612 const struct lu_fid *fid_src = rr->rr_fid1;
1613 const struct lu_fid *fid_tgt = rr->rr_fid2;
1614 struct mdt_lock_handle *lh_src = &info->mti_lh[MDT_LH_PARENT];
1615 struct mdt_lock_handle *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1616 struct mdt_object *src;
1617 struct mdt_object *tgt;
1622 /* find both parents. */
1623 src = mdt_object_find_check(info, fid_src, 0);
1625 RETURN(PTR_ERR(src));
1627 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1629 if (lu_fid_eq(fid_src, fid_tgt)) {
1631 mdt_object_get(info->mti_env, tgt);
1633 /* Check if the @src is not a child of the @tgt, otherwise a
1634 * reverse locking must take place. */
1635 rc = mdt_is_subdir(info, src, fid_tgt);
1639 GOTO(err_src_put, rc);
1641 tgt = mdt_object_find_check(info, fid_tgt, 1);
1643 GOTO(err_src_put, rc = PTR_ERR(tgt));
1645 if (unlikely(mdt_object_remote(tgt))) {
1646 CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1647 "on different MDTs\n", PFID(fid_src),
1649 GOTO(err_tgt_put, rc = -EXDEV);
1653 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1655 /* lock parents in the proper order. */
1657 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1659 GOTO(err_tgt_put, rc);
1661 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1663 rc = mdt_object_lock_save(info, src, lh_src, 0);
1665 rc = mdt_object_lock_save(info, src, lh_src, 0);
1667 GOTO(err_tgt_put, rc);
1669 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1672 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1673 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1674 rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1675 MDS_INODELOCK_UPDATE);
1676 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1680 GOTO(err_unlock, rc);
1682 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1689 /* The order does not matter as the handle is checked inside,
1690 * as well as not used handle. */
1691 mdt_object_unlock(info, src, lh_src, rc);
1692 mdt_object_unlock(info, tgt, lh_tgt, rc);
1694 mdt_object_put(info->mti_env, tgt);
1696 mdt_object_put(info->mti_env, src);
1701 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1702 * 2 - src child; 3 - tgt child.
1703 * Update on disk version of src child.
1706 * For DNE phase I, only these renames are allowed
1707 * mv src_p/src_c tgt_p/tgt_c
1708 * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1709 * 2. src_p and tgt_p are same directory, and tgt_c does not
1710 * exists. In this case, all of modification will happen
1711 * in the MDT where ithesource parent is, only one remote
1712 * update is needed, i.e. set c_time/m_time on the child.
1713 * And tgt_c will be still in the same MDT as the original
1716 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1717 struct mdt_lock_handle *lhc)
1719 struct mdt_reint_record *rr = &info->mti_rr;
1720 struct md_attr *ma = &info->mti_attr;
1721 struct ptlrpc_request *req = mdt_info_req(info);
1722 struct mdt_object *msrcdir = NULL;
1723 struct mdt_object *mtgtdir = NULL;
1724 struct mdt_object *mold;
1725 struct mdt_object *mnew = NULL;
1726 struct mdt_lock_handle *lh_srcdirp;
1727 struct mdt_lock_handle *lh_tgtdirp;
1728 struct mdt_lock_handle *lh_oldp = NULL;
1729 struct mdt_lock_handle *lh_newp = NULL;
1730 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1731 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1735 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1736 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1737 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1739 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1740 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1741 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1742 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1744 /* step 1&2: lock the source and target dirs. */
1745 rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1749 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1751 /* step 3: find & lock the old object. */
1753 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1755 GOTO(out_unlock_parents, rc);
1757 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1758 GOTO(out_unlock_parents, rc = -EINVAL);
1760 if (!fid_is_md_operative(old_fid))
1761 GOTO(out_unlock_parents, rc = -EPERM);
1763 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1765 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1767 /* Check if @mtgtdir is subdir of @mold, before locking child
1768 * to avoid reverse locking. */
1769 rc = mdt_is_subdir(info, mtgtdir, old_fid);
1771 GOTO(out_put_old, rc);
1773 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1774 /* save version after locking */
1775 mdt_version_get_save(info, mold, 2);
1777 /* step 4: find & lock the new object. */
1778 /* new target object may not exist now */
1779 /* lookup with version checking */
1781 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1784 /* the new_fid should have been filled at this moment */
1785 if (lu_fid_eq(old_fid, new_fid))
1786 GOTO(out_put_old, rc);
1788 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1789 lu_fid_eq(new_fid, rr->rr_fid2))
1790 GOTO(out_put_old, rc = -EINVAL);
1792 if (!fid_is_md_operative(new_fid))
1793 GOTO(out_put_old, rc = -EPERM);
1795 if (mdt_object_remote(mold)) {
1796 CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1798 GOTO(out_put_old, rc = -EXDEV);
1801 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1803 GOTO(out_put_old, rc = PTR_ERR(mnew));
1805 if (mdt_object_remote(mnew)) {
1806 CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1808 GOTO(out_put_new, rc = -EXDEV);
1811 /* Before locking the target dir, check we do not replace
1812 * a dir with a non-dir, otherwise it may deadlock with
1813 * link op which tries to create a link in this dir
1814 * back to this non-dir. */
1815 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1816 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1817 GOTO(out_put_new, rc = -EISDIR);
1819 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1820 mdt_lock_reg_init(lh_oldp, LCK_EX);
1821 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1822 MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1824 GOTO(out_put_new, rc);
1826 /* Check if @msrcdir is subdir of @mnew, before locking child
1827 * to avoid reverse locking. */
1828 rc = mdt_is_subdir(info, msrcdir, new_fid);
1830 GOTO(out_unlock_old, rc);
1832 /* We used to acquire MDS_INODELOCK_FULL here but we
1833 * can't do this now because a running HSM restore on
1834 * the rename onto victim will hold the layout
1835 * lock. See LU-4002. */
1837 lh_newp = &info->mti_lh[MDT_LH_NEW];
1838 mdt_lock_reg_init(lh_newp, LCK_EX);
1839 rc = mdt_object_lock(info, mnew, lh_newp,
1840 MDS_INODELOCK_LOOKUP |
1841 MDS_INODELOCK_UPDATE,
1844 GOTO(out_unlock_old, rc);
1846 /* get and save version after locking */
1847 mdt_version_get_save(info, mnew, 3);
1848 } else if (rc != -EREMOTE && rc != -ENOENT) {
1849 GOTO(out_put_old, rc);
1851 /* If mnew does not exist and mold are remote directory,
1852 * it only allows rename if they are under same directory */
1853 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1854 CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1856 GOTO(out_put_old, rc = -EXDEV);
1859 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1860 mdt_lock_reg_init(lh_oldp, LCK_EX);
1861 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1862 MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1864 GOTO(out_put_old, rc);
1866 mdt_enoent_version_save(info, 3);
1869 /* step 5: rename it */
1870 mdt_reint_init_ma(info, ma);
1872 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1873 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1876 mutex_lock(&mnew->mot_lov_mutex);
1878 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1879 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1880 mnew != NULL ? mdt_object_child(mnew) : NULL,
1881 &rr->rr_tgt_name, ma);
1884 mutex_unlock(&mnew->mot_lov_mutex);
1886 /* handle last link of tgt object */
1888 mdt_counter_incr(req, LPROC_MDT_RENAME);
1890 mdt_handle_last_unlink(info, mnew, ma);
1892 mdt_rename_counter_tally(info, info->mti_mdt, req,
1898 mdt_object_unlock(info, mnew, lh_newp, rc);
1900 mdt_object_unlock(info, mold, lh_oldp, rc);
1903 mdt_object_put(info->mti_env, mnew);
1905 mdt_object_put(info->mti_env, mold);
1907 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1908 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1912 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1913 struct mdt_lock_handle *lhc,
1914 enum mdt_rename_lock rename_lock)
1916 struct mdt_reint_record *rr = &info->mti_rr;
1917 struct ptlrpc_request *req = mdt_info_req(info);
1918 struct lustre_handle rename_lh = { 0 };
1922 if (info->mti_dlm_req)
1923 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1925 if (!fid_is_md_operative(rr->rr_fid1) ||
1926 !fid_is_md_operative(rr->rr_fid2))
1929 rc = mdt_rename_lock(info, &rename_lh, rename_lock);
1931 CERROR("%s: can't lock FS for rename: rc = %d\n",
1932 mdt_obd_name(info->mti_mdt), rc);
1936 if (rename_lock == MRL_RENAME)
1937 rc = mdt_reint_rename_internal(info, lhc);
1939 rc = mdt_reint_migrate_internal(info, lhc);
1941 if (lustre_handle_is_used(&rename_lh))
1942 mdt_rename_unlock(&rename_lh);
1947 static int mdt_reint_rename(struct mdt_thread_info *info,
1948 struct mdt_lock_handle *lhc)
1950 return mdt_reint_rename_or_migrate(info, lhc, MRL_RENAME);
1953 static int mdt_reint_migrate(struct mdt_thread_info *info,
1954 struct mdt_lock_handle *lhc)
1956 return mdt_reint_rename_or_migrate(info, lhc, MRL_MIGRATE);
1959 struct mdt_reinter {
1960 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
1961 enum lprocfs_extra_opc mr_extra_opc;
1964 static const struct mdt_reinter mdt_reinters[] = {
1966 .mr_handler = &mdt_reint_setattr,
1967 .mr_extra_opc = MDS_REINT_SETATTR,
1970 .mr_handler = &mdt_reint_create,
1971 .mr_extra_opc = MDS_REINT_CREATE,
1974 .mr_handler = &mdt_reint_link,
1975 .mr_extra_opc = MDS_REINT_LINK,
1978 .mr_handler = &mdt_reint_unlink,
1979 .mr_extra_opc = MDS_REINT_UNLINK,
1982 .mr_handler = &mdt_reint_rename,
1983 .mr_extra_opc = MDS_REINT_RENAME,
1986 .mr_handler = &mdt_reint_open,
1987 .mr_extra_opc = MDS_REINT_OPEN,
1989 [REINT_SETXATTR] = {
1990 .mr_handler = &mdt_reint_setxattr,
1991 .mr_extra_opc = MDS_REINT_SETXATTR,
1994 .mr_handler = &mdt_reint_unlink,
1995 .mr_extra_opc = MDS_REINT_UNLINK,
1998 .mr_handler = &mdt_reint_migrate,
1999 .mr_extra_opc = MDS_REINT_RENAME,
2003 int mdt_reint_rec(struct mdt_thread_info *info,
2004 struct mdt_lock_handle *lhc)
2006 const struct mdt_reinter *mr;
2010 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2013 mr = &mdt_reinters[info->mti_rr.rr_opcode];
2014 if (mr->mr_handler == NULL)
2017 rc = (*mr->mr_handler)(info, lhc);
2019 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2020 PTLRPC_LAST_CNTR + mr->mr_extra_opc);