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_is_dne_client(exp))
274 if (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
275 spec->u.sp_ea.eadatalen != 0) {
276 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
278 if (le32_to_cpu(lum->lum_stripe_count) > 1 &&
279 !mdt_is_striped_client(exp))
287 * VBR: we save three versions in reply:
288 * 0 - parent. Check that parent version is the same during replay.
289 * 1 - name. Version of 'name' if file exists with the same name or
290 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
291 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
294 static int mdt_md_create(struct mdt_thread_info *info)
296 struct mdt_device *mdt = info->mti_mdt;
297 struct mdt_object *parent;
298 struct mdt_object *child;
299 struct mdt_lock_handle *lh;
300 struct mdt_body *repbody;
301 struct md_attr *ma = &info->mti_attr;
302 struct mdt_reint_record *rr = &info->mti_rr;
306 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create ("DNAME"->"DFID") "
308 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
310 if (!fid_is_md_operative(rr->rr_fid1))
313 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
315 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
317 RETURN(PTR_ERR(parent));
319 if (!mdt_object_exists(parent))
320 GOTO(put_parent, rc = -ENOENT);
322 lh = &info->mti_lh[MDT_LH_PARENT];
323 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
324 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE);
326 GOTO(put_parent, rc);
328 if (!mdt_object_remote(parent)) {
329 rc = mdt_version_get_check_save(info, parent, 0);
331 GOTO(unlock_parent, rc);
335 * Check child name version during replay.
336 * During create replay a file may exist with same name.
338 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
339 &info->mti_tmp_fid1, 1);
341 GOTO(unlock_parent, rc = -EEXIST);
343 /* -ENOENT is expected here */
345 GOTO(unlock_parent, rc);
347 /* save version of file name for replay, it must be ENOENT here */
348 mdt_enoent_version_save(info, 1);
350 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
351 if (likely(!IS_ERR(child))) {
352 struct md_object *next = mdt_object_child(parent);
354 rc = mdt_remote_permission(info, parent, child);
356 GOTO(out_put_child, rc);
358 ma->ma_need = MA_INODE;
361 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
362 OBD_FAIL_MDS_REINT_CREATE_WRITE);
364 /* Version of child will be updated on disk. */
365 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
366 rc = mdt_version_get_check_save(info, child, 2);
368 GOTO(out_put_child, rc);
370 /* Let lower layer know current lock mode. */
371 info->mti_spec.sp_cr_mode =
372 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
375 * Do not perform lookup sanity check. We know that name does
378 info->mti_spec.sp_cr_lookup = 0;
379 info->mti_spec.sp_feat = &dt_directory_features;
381 rc = mdo_create(info->mti_env, next, &rr->rr_name,
382 mdt_object_child(child), &info->mti_spec, ma);
384 rc = mdt_attr_get_complex(info, child, ma);
387 /* Return fid & attr to client. */
388 if (ma->ma_valid & MA_INODE)
389 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
390 mdt_object_fid(child));
393 mdt_object_put(info->mti_env, child);
398 mdt_object_unlock(info, parent, lh, rc);
400 mdt_object_put(info->mti_env, parent);
404 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
405 struct mdt_object *obj, __u64 ibits,
406 struct mdt_lock_handle *s0_lh,
407 struct mdt_object *s0_obj,
408 struct ldlm_enqueue_info *einfo)
410 ldlm_policy_data_t *policy = &mti->mti_policy;
414 if (!S_ISDIR(obj->mot_header.loh_attr))
417 /* Unlock stripe 0 */
418 if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
419 LASSERT(s0_obj != NULL);
420 mdt_object_unlock_put(mti, s0_obj, s0_lh, 1);
423 memset(policy, 0, sizeof(*policy));
424 policy->l_inodebits.bits = ibits;
426 rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
432 * Lock slave stripes if necessary, the lock handles of slave stripes
433 * will be stored in einfo->ei_cbdata.
435 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
436 ldlm_mode_t mode, __u64 ibits,
437 struct mdt_lock_handle *s0_lh,
438 struct mdt_object **s0_objp,
439 struct ldlm_enqueue_info *einfo)
441 ldlm_policy_data_t *policy = &mti->mti_policy;
442 struct lu_buf *buf = &mti->mti_buf;
443 struct lmv_mds_md_v1 *lmv;
444 struct lu_fid *fid = &mti->mti_tmp_fid1;
448 if (!S_ISDIR(obj->mot_header.loh_attr))
451 buf->lb_buf = mti->mti_xattr_buf;
452 buf->lb_len = sizeof(mti->mti_xattr_buf);
453 rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
456 rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
458 buf->lb_buf = mti->mti_big_lmm;
459 buf->lb_len = mti->mti_big_lmmsize;
463 if (rc == -ENODATA || rc == -ENOENT)
470 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
473 /* Sigh, 0_stripe and master object are different
474 * object, though they are in the same MDT, to avoid
475 * adding osd_object_lock here, so we will enqueue the
476 * stripe0 lock in MDT0 for now */
477 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
478 *s0_objp = mdt_object_find_lock(mti, fid, s0_lh, ibits);
479 if (IS_ERR(*s0_objp))
480 RETURN(PTR_ERR(*s0_objp));
482 memset(einfo, 0, sizeof(*einfo));
483 einfo->ei_type = LDLM_IBITS;
484 einfo->ei_mode = mode;
485 einfo->ei_cb_bl = mdt_remote_blocking_ast;
486 einfo->ei_cb_local_bl = mdt_blocking_ast;
487 einfo->ei_cb_cp = ldlm_completion_ast;
488 einfo->ei_enq_slave = 1;
489 einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
490 memset(policy, 0, sizeof(*policy));
491 policy->l_inodebits.bits = ibits;
493 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
498 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
501 struct mdt_lock_handle *lh;
502 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
503 __u64 lockpart = MDS_INODELOCK_UPDATE;
504 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
505 struct mdt_lock_handle *s0_lh;
506 struct mdt_object *s0_obj = NULL;
510 lh = &info->mti_lh[MDT_LH_PARENT];
511 mdt_lock_reg_init(lh, LCK_PW);
513 /* Even though the new MDT will grant PERM lock to the old
514 * client, but the old client will almost ignore that during
515 * So it needs to revoke both LOOKUP and PERM lock here, so
516 * both new and old client can cancel the dcache */
517 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
518 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
520 rc = mdt_object_lock(info, mo, lh, lockpart);
524 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
525 mdt_lock_reg_init(s0_lh, LCK_PW);
526 rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
528 GOTO(out_unlock, rc);
530 /* all attrs are packed into mti_attr in unpack_setattr */
531 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
532 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
534 /* This is only for set ctime when rename's source is on remote MDS. */
535 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
536 ma->ma_attr_flags |= MDS_VTX_BYPASS;
538 /* VBR: update version if attr changed are important for recovery */
540 /* update on-disk version of changed object */
541 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
542 rc = mdt_version_get_check_save(info, mo, 0);
544 GOTO(out_unlock, rc);
547 /* Ensure constant striping during chown(). See LU-2789. */
548 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
549 mutex_lock(&mo->mot_lov_mutex);
551 /* all attrs are packed into mti_attr in unpack_setattr */
552 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
554 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
555 mutex_unlock(&mo->mot_lov_mutex);
558 GOTO(out_unlock, rc);
562 mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
563 mdt_object_unlock(info, mo, lh, rc);
568 * Check HSM flags and add HS_DIRTY flag if relevant.
570 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
571 * and is not RELEASED.
573 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
579 /* If the file was modified, add the dirty flag */
580 ma->ma_need = MA_HSM;
581 rc = mdt_attr_get_complex(info, mo, ma);
583 CERROR("file attribute read error for "DFID": %d.\n",
584 PFID(mdt_object_fid(mo)), rc);
588 /* If an up2date copy exists in the backend, add dirty flag */
589 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
590 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
591 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
593 ma->ma_hsm.mh_flags |= HS_DIRTY;
595 mdt_lock_reg_init(lh, LCK_PW);
596 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR);
600 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
602 CERROR("file attribute change error for "DFID": %d\n",
603 PFID(mdt_object_fid(mo)), rc);
604 mdt_object_unlock(info, mo, lh, rc);
610 static int mdt_reint_setattr(struct mdt_thread_info *info,
611 struct mdt_lock_handle *lhc)
613 struct md_attr *ma = &info->mti_attr;
614 struct mdt_reint_record *rr = &info->mti_rr;
615 struct ptlrpc_request *req = mdt_info_req(info);
616 struct mdt_object *mo;
617 struct mdt_body *repbody;
621 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
622 (unsigned int)ma->ma_attr.la_valid);
624 if (info->mti_dlm_req)
625 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
627 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
628 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
630 GOTO(out, rc = PTR_ERR(mo));
632 if (!mdt_object_exists(mo))
633 GOTO(out_put, rc = -ENOENT);
635 if (mdt_object_remote(mo))
636 GOTO(out_put, rc = -EREMOTE);
638 if ((ma->ma_attr.la_valid & LA_SIZE) ||
639 (rr->rr_flags & MRF_OPEN_TRUNC)) {
640 /* Check write access for the O_TRUNC case */
641 if (mdt_write_read(mo) < 0)
642 GOTO(out_put, rc = -ETXTBSY);
645 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
646 if (ma->ma_valid & MA_LOV)
647 GOTO(out_put, rc = -EPROTO);
649 rc = mdt_attr_set(info, mo, ma);
652 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
653 struct lu_buf *buf = &info->mti_buf;
655 if (ma->ma_attr.la_valid != 0)
656 GOTO(out_put, rc = -EPROTO);
658 buf->lb_buf = ma->ma_lmm;
659 buf->lb_len = ma->ma_lmm_size;
660 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
661 buf, XATTR_NAME_LOV, 0);
664 } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
665 struct lu_buf *buf = &info->mti_buf;
666 struct mdt_lock_handle *lh;
668 if (ma->ma_attr.la_valid != 0)
669 GOTO(out_put, rc = -EPROTO);
671 lh = &info->mti_lh[MDT_LH_PARENT];
672 mdt_lock_reg_init(lh, LCK_PW);
674 rc = mdt_object_lock(info, mo, lh,
675 MDS_INODELOCK_XATTR);
679 buf->lb_buf = ma->ma_lmv;
680 buf->lb_len = ma->ma_lmv_size;
681 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
682 buf, XATTR_NAME_DEFAULT_LMV, 0);
684 mdt_object_unlock(info, mo, lh, rc);
688 GOTO(out_put, rc = -EPROTO);
691 /* If file data is modified, add the dirty flag */
692 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
693 rc = mdt_add_dirty_flag(info, mo, ma);
695 ma->ma_need = MA_INODE;
697 rc = mdt_attr_get_complex(info, mo, ma);
701 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
705 mdt_object_put(info->mti_env, mo);
708 mdt_counter_incr(req, LPROC_MDT_SETATTR);
710 mdt_client_compatibility(info);
711 rc2 = mdt_fix_reply(info);
717 static int mdt_reint_create(struct mdt_thread_info *info,
718 struct mdt_lock_handle *lhc)
720 struct ptlrpc_request *req = mdt_info_req(info);
724 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
725 RETURN(err_serious(-ESTALE));
727 if (info->mti_dlm_req)
728 ldlm_request_cancel(mdt_info_req(info),
729 info->mti_dlm_req, 0, LATF_SKIP);
731 if (!lu_name_is_valid(&info->mti_rr.rr_name))
734 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
736 mdt_counter_incr(req, LPROC_MDT_MKDIR);
744 /* Special file should stay on the same node as parent. */
745 mdt_counter_incr(req, LPROC_MDT_MKNOD);
748 CERROR("%s: Unsupported mode %o\n",
749 mdt_obd_name(info->mti_mdt),
750 info->mti_attr.ma_attr.la_mode);
751 RETURN(err_serious(-EOPNOTSUPP));
754 rc = mdt_md_create(info);
759 * VBR: save parent version in reply and child version getting by its name.
760 * Version of child is getting and checking during its lookup. If
762 static int mdt_reint_unlink(struct mdt_thread_info *info,
763 struct mdt_lock_handle *lhc)
765 struct mdt_reint_record *rr = &info->mti_rr;
766 struct ptlrpc_request *req = mdt_info_req(info);
767 struct md_attr *ma = &info->mti_attr;
768 struct lu_fid *child_fid = &info->mti_tmp_fid1;
769 struct mdt_object *mp;
770 struct mdt_object *mc;
771 struct mdt_lock_handle *parent_lh;
772 struct mdt_lock_handle *child_lh;
773 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
774 struct mdt_lock_handle *s0_lh = NULL;
775 struct mdt_object *s0_obj = NULL;
781 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
782 PNAME(&rr->rr_name));
784 if (info->mti_dlm_req)
785 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
787 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
788 RETURN(err_serious(-ENOENT));
790 if (!fid_is_md_operative(rr->rr_fid1))
794 * step 1: Found the parent.
796 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
802 parent_lh = &info->mti_lh[MDT_LH_PARENT];
803 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
804 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE);
806 GOTO(put_parent, rc);
808 if (!mdt_object_remote(mp)) {
809 rc = mdt_version_get_check_save(info, mp, 0);
811 GOTO(unlock_parent, rc);
814 /* step 2: find & lock the child */
815 /* lookup child object along with version checking */
817 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
819 /* Name might not be able to find during resend of
820 * remote unlink, considering following case.
821 * dir_A is a remote directory, the name entry of
822 * dir_A is on MDT0, the directory is on MDT1,
824 * 1. client sends unlink req to MDT1.
825 * 2. MDT1 sends name delete update to MDT0.
826 * 3. name entry is being deleted in MDT0 synchronously.
827 * 4. MDT1 is restarted.
828 * 5. client resends unlink req to MDT1. So it can not
829 * find the name entry on MDT0 anymore.
830 * In this case, MDT1 only needs to destory the local
833 if (mdt_object_remote(mp) && rc == -ENOENT &&
834 !fid_is_zero(rr->rr_fid2) &&
835 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
837 *child_fid = *rr->rr_fid2;
839 GOTO(unlock_parent, rc);
843 if (!fid_is_md_operative(child_fid))
844 GOTO(unlock_parent, rc = -EPERM);
846 /* We will lock the child regardless it is local or remote. No harm. */
847 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
849 GOTO(unlock_parent, rc = PTR_ERR(mc));
851 child_lh = &info->mti_lh[MDT_LH_CHILD];
852 mdt_lock_reg_init(child_lh, LCK_EX);
853 if (mdt_object_remote(mc)) {
854 struct mdt_body *repbody;
856 if (!fid_is_zero(rr->rr_fid2)) {
857 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
858 mdt_obd_name(info->mti_mdt),
859 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
860 GOTO(put_child, rc = -ENOENT);
862 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
863 mdt_obd_name(info->mti_mdt),
864 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
866 if (!mdt_is_dne_client(req->rq_export))
867 /* Return -ENOTSUPP for old client */
868 GOTO(put_child, rc = -ENOTSUPP);
870 if (info->mti_spec.sp_rm_entry) {
871 struct lu_ucred *uc = mdt_ucred(info);
873 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
874 CERROR("%s: unlink remote entry is only "
875 "permitted for administrator: rc = %d\n",
876 mdt_obd_name(info->mti_mdt),
878 GOTO(put_child, rc = -EPERM);
881 ma->ma_need = MA_INODE;
883 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
884 NULL, &rr->rr_name, ma, no_name);
887 /* Revoke the LOOKUP lock of the remote object granted by
888 * this MDT. Since the unlink will happen on another MDT,
889 * it will release the LOOKUP lock right away. Then What
890 * would happen if another client try to grab the LOOKUP
891 * lock at the same time with unlink XXX */
892 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
893 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
894 LASSERT(repbody != NULL);
895 repbody->mbo_fid1 = *mdt_object_fid(mc);
896 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
897 GOTO(unlock_child, rc = -EREMOTE);
898 } else if (info->mti_spec.sp_rm_entry) {
900 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
902 mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
906 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
907 * this now because a running HSM restore on the child (unlink
908 * victim) will hold the layout lock. See LU-4002. */
909 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
910 if (mdt_object_remote(mp)) {
911 /* Enqueue lookup lock from parent MDT */
912 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
913 &child_lh->mlh_rreg_lh,
914 child_lh->mlh_rreg_mode,
915 MDS_INODELOCK_LOOKUP);
919 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
922 rc = mdt_object_lock(info, mc, child_lh, lock_ibits);
926 * Now we can only make sure we need MA_INODE, in mdd layer, will check
927 * whether need MA_LOV and MA_COOKIE.
929 ma->ma_need = MA_INODE;
932 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
933 mdt_lock_reg_init(s0_lh, LCK_EX);
934 rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
937 GOTO(unlock_child, rc);
939 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
940 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
941 /* save version when object is locked */
942 mdt_version_get_save(info, mc, 1);
944 mutex_lock(&mc->mot_lov_mutex);
946 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
947 mdt_object_child(mc), &rr->rr_name, ma, no_name);
949 mutex_unlock(&mc->mot_lov_mutex);
951 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
952 rc = mdt_attr_get_complex(info, mc, ma);
954 mdt_handle_last_unlink(info, mc, ma);
956 if (ma->ma_valid & MA_INODE) {
957 switch (ma->ma_attr.la_mode & S_IFMT) {
959 mdt_counter_incr(req, LPROC_MDT_RMDIR);
967 mdt_counter_incr(req, LPROC_MDT_UNLINK);
970 LASSERTF(0, "bad file type %o unlinking\n",
971 ma->ma_attr.la_mode);
978 mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
979 mdt_object_unlock(info, mc, child_lh, rc);
981 mdt_object_put(info->mti_env, mc);
983 mdt_object_unlock(info, mp, parent_lh, rc);
985 mdt_object_put(info->mti_env, mp);
991 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
994 static int mdt_reint_link(struct mdt_thread_info *info,
995 struct mdt_lock_handle *lhc)
997 struct mdt_reint_record *rr = &info->mti_rr;
998 struct ptlrpc_request *req = mdt_info_req(info);
999 struct md_attr *ma = &info->mti_attr;
1000 struct mdt_object *ms;
1001 struct mdt_object *mp;
1002 struct mdt_lock_handle *lhs;
1003 struct mdt_lock_handle *lhp;
1007 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1008 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1010 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1011 RETURN(err_serious(-ENOENT));
1013 if (info->mti_dlm_req)
1014 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1016 /* Invalid case so return error immediately instead of
1018 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1021 if (!fid_is_md_operative(rr->rr_fid1) ||
1022 !fid_is_md_operative(rr->rr_fid2))
1025 /* step 1: find & lock the target parent dir */
1026 lhp = &info->mti_lh[MDT_LH_PARENT];
1027 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1028 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1029 MDS_INODELOCK_UPDATE);
1031 RETURN(PTR_ERR(mp));
1033 rc = mdt_version_get_check_save(info, mp, 0);
1035 GOTO(out_unlock_parent, rc);
1037 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1039 /* step 2: find & lock the source */
1040 lhs = &info->mti_lh[MDT_LH_CHILD];
1041 mdt_lock_reg_init(lhs, LCK_EX);
1043 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1045 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1047 if (!mdt_object_exists(ms)) {
1048 mdt_object_put(info->mti_env, ms);
1049 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1050 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1051 GOTO(out_unlock_parent, rc = -ENOENT);
1054 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1055 MDS_INODELOCK_XATTR);
1057 mdt_object_put(info->mti_env, ms);
1058 GOTO(out_unlock_parent, rc);
1061 /* step 3: link it */
1062 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1063 OBD_FAIL_MDS_REINT_LINK_WRITE);
1065 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1066 rc = mdt_version_get_check_save(info, ms, 1);
1068 GOTO(out_unlock_child, rc);
1070 /** check target version by name during replay */
1071 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1072 &info->mti_tmp_fid1, 2);
1073 if (rc != 0 && rc != -ENOENT)
1074 GOTO(out_unlock_child, rc);
1075 /* save version of file name for replay, it must be ENOENT here */
1076 if (!req_is_replay(mdt_info_req(info))) {
1077 if (rc != -ENOENT) {
1078 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1079 PNAME(&rr->rr_name));
1080 GOTO(out_unlock_child, rc = -EEXIST);
1082 info->mti_ver[2] = ENOENT_VERSION;
1083 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1086 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1087 mdt_object_child(ms), &rr->rr_name, ma);
1090 mdt_counter_incr(req, LPROC_MDT_LINK);
1094 mdt_object_unlock_put(info, ms, lhs, rc);
1096 mdt_object_unlock_put(info, mp, lhp, rc);
1100 * lock the part of the directory according to the hash of the name
1101 * (lh->mlh_pdo_hash) in parallel directory lock.
1103 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1104 struct mdt_lock_handle *lh,
1105 struct mdt_object *obj, __u64 ibits)
1107 struct ldlm_res_id *res = &info->mti_res_id;
1108 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1109 ldlm_policy_data_t *policy = &info->mti_policy;
1113 * Finish res_id initializing by name hash marking part of
1114 * directory which is taking modification.
1116 LASSERT(lh->mlh_pdo_hash != 0);
1117 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1118 memset(policy, 0, sizeof(*policy));
1119 policy->l_inodebits.bits = ibits;
1121 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1122 * going to be sent to client. If it is - mdt_intent_policy() path will
1123 * fix it up and turn FL_LOCAL flag off.
1125 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1126 res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1127 &info->mti_exp->exp_handle.h_cookie);
1132 * Get BFL lock for rename or migrate process.
1134 static int mdt_rename_lock(struct mdt_thread_info *info,
1135 struct lustre_handle *lh)
1140 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1141 struct lu_fid *fid = &info->mti_tmp_fid1;
1142 struct mdt_object *obj;
1144 /* XXX, right now, it has to use object API to
1145 * enqueue lock cross MDT, so it will enqueue
1146 * rename lock(with LUSTRE_BFL_FID) by root object */
1148 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1150 RETURN(PTR_ERR(obj));
1152 rc = mdt_remote_object_lock(info, obj,
1153 &LUSTRE_BFL_FID, lh,
1155 MDS_INODELOCK_UPDATE);
1156 mdt_object_put(info->mti_env, obj);
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;
1163 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1164 memset(policy, 0, sizeof *policy);
1165 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1166 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1167 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1168 LCK_EX, &flags, ldlm_blocking_ast,
1169 ldlm_completion_ast, NULL, NULL, 0,
1171 &info->mti_exp->exp_handle.h_cookie,
1178 static void mdt_rename_unlock(struct lustre_handle *lh)
1181 LASSERT(lustre_handle_is_used(lh));
1182 /* Cancel the single rename lock right away */
1183 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1188 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1189 * target. Source should not be ancestor of target dir. May be other rename
1190 * checks can be moved here later.
1192 static int mdt_is_subdir(struct mdt_thread_info *info,
1193 struct mdt_object *dir,
1194 const struct lu_fid *fid)
1196 struct lu_fid dir_fid = dir->mot_header.loh_fid;
1200 /* If the source and target are in the same directory, they can not
1201 * be parent/child relationship, so subdir check is not needed */
1202 if (lu_fid_eq(&dir_fid, fid))
1205 if (!mdt_object_exists(dir))
1208 rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1211 CERROR("%s: failed subdir check in "DFID" for "DFID
1212 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1213 PFID(&dir_fid), PFID(fid), rc);
1214 /* Return EINVAL only if a parent is the @fid */
1218 /* check the found fid */
1219 if (lu_fid_eq(&dir_fid, fid))
1226 /* Update object linkEA */
1227 struct mdt_lock_list {
1228 struct mdt_object *mll_obj;
1229 struct mdt_lock_handle mll_lh;
1230 struct list_head mll_list;
1233 static void mdt_unlock_list(struct mdt_thread_info *info,
1234 struct list_head *list, int rc)
1236 struct mdt_lock_list *mll;
1237 struct mdt_lock_list *mll2;
1239 list_for_each_entry_safe(mll, mll2, list, mll_list) {
1240 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1241 list_del(&mll->mll_list);
1246 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1247 struct mdt_object *obj,
1248 struct mdt_object *pobj,
1249 struct list_head *lock_list)
1251 struct lu_buf *buf = &info->mti_big_buf;
1252 struct linkea_data ldata = { NULL };
1257 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1260 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1261 if (buf->lb_buf == NULL)
1265 rc = mdt_links_read(info, obj, &ldata);
1267 if (rc == -ENOENT || rc == -ENODATA)
1272 LASSERT(ldata.ld_leh != NULL);
1273 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1274 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1275 struct mdt_device *mdt = info->mti_mdt;
1276 struct mdt_object *mdt_pobj;
1277 struct mdt_lock_list *mll;
1278 struct lu_name name;
1281 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1283 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1285 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1286 if (IS_ERR(mdt_pobj)) {
1287 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1288 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1292 if (!mdt_object_exists(mdt_pobj)) {
1293 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1294 mdt_obd_name(mdt), PFID(&fid));
1295 mdt_object_put(info->mti_env, mdt_pobj);
1299 if (mdt_pobj == pobj) {
1300 CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1301 mdt_obd_name(mdt), PFID(&fid));
1302 mdt_object_put(info->mti_env, mdt_pobj);
1308 mdt_object_put(info->mti_env, mdt_pobj);
1309 GOTO(out, rc = -ENOMEM);
1312 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1313 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1314 MDS_INODELOCK_UPDATE);
1316 CERROR("%s: cannot lock "DFID": rc =%d\n",
1317 mdt_obd_name(mdt), PFID(&fid), rc);
1318 mdt_object_put(info->mti_env, mdt_pobj);
1323 INIT_LIST_HEAD(&mll->mll_list);
1324 mll->mll_obj = mdt_pobj;
1325 list_add_tail(&mll->mll_list, lock_list);
1329 mdt_unlock_list(info, lock_list, rc);
1333 /* migrate files from one MDT to another MDT */
1334 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1335 struct mdt_lock_handle *lhc)
1337 struct mdt_reint_record *rr = &info->mti_rr;
1338 struct md_attr *ma = &info->mti_attr;
1339 struct mdt_object *msrcdir;
1340 struct mdt_object *mold;
1341 struct mdt_object *mnew = NULL;
1342 struct mdt_lock_handle *lh_dirp;
1343 struct mdt_lock_handle *lh_childp;
1344 struct mdt_lock_handle *lh_tgtp = NULL;
1345 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1346 struct list_head lock_list;
1348 struct ldlm_lock *lease = NULL;
1349 bool lock_open_sem = false;
1353 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1354 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1356 /* 1: lock the source dir. */
1357 msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1358 if (IS_ERR(msrcdir)) {
1359 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1360 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1361 (int)PTR_ERR(msrcdir));
1362 RETURN(PTR_ERR(msrcdir));
1365 lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1366 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1367 rc = mdt_object_lock(info, msrcdir, lh_dirp,
1368 MDS_INODELOCK_UPDATE);
1370 GOTO(out_put_parent, rc);
1372 if (!mdt_object_remote(msrcdir)) {
1373 rc = mdt_version_get_check_save(info, msrcdir, 0);
1375 GOTO(out_unlock_parent, rc);
1378 /* 2: sanity check and find the object to be migrated. */
1380 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1382 GOTO(out_unlock_parent, rc);
1384 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1385 GOTO(out_unlock_parent, rc = -EINVAL);
1387 if (!fid_is_md_operative(old_fid))
1388 GOTO(out_unlock_parent, rc = -EPERM);
1390 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1392 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1394 if (mdt_object_remote(mold)) {
1395 CERROR("%s: source "DFID" is on the remote MDT\n",
1396 mdt_obd_name(info->mti_mdt), PFID(old_fid));
1397 GOTO(out_put_child, rc = -EREMOTE);
1400 if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1401 !mdt_object_remote(msrcdir)) {
1402 CERROR("%s: parent "DFID" is still on the same"
1403 " MDT, which should be migrated first:"
1404 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1405 PFID(mdt_object_fid(msrcdir)), -EPERM);
1406 GOTO(out_put_child, rc = -EPERM);
1409 rc = mdt_remote_permission(info, msrcdir, mold);
1411 GOTO(out_put_child, rc);
1413 /* 3: iterate the linkea of the object and lock all of the objects */
1414 INIT_LIST_HEAD(&lock_list);
1415 rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1417 GOTO(out_put_child, rc);
1419 if (info->mti_spec.sp_migrate_close) {
1420 struct close_data *data;
1421 struct mdt_body *repbody;
1422 bool lease_broken = false;
1424 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
1426 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
1428 GOTO(out_lease, rc = -EPROTO);
1430 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1432 GOTO(out_lease, rc = -EPROTO);
1434 lease = ldlm_handle2lock(&data->cd_handle);
1436 GOTO(out_lease, rc = -ESTALE);
1438 /* try to hold open_sem so that nobody else can open the file */
1439 if (!down_write_trylock(&mold->mot_open_sem)) {
1440 ldlm_lock_cancel(lease);
1441 GOTO(out_lease, rc = -EBUSY);
1444 lock_open_sem = true;
1445 /* Check if the lease open lease has already canceled */
1446 lock_res_and_lock(lease);
1447 lease_broken = ldlm_is_cancel(lease);
1448 unlock_res_and_lock(lease);
1450 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
1451 PFID(mdt_object_fid(mold)), lease_broken);
1453 /* Cancel server side lease. Client side counterpart should
1454 * have been cancelled. It's okay to cancel it now as we've
1455 * held mot_open_sem. */
1456 ldlm_lock_cancel(lease);
1459 GOTO(out_lease, rc = -EAGAIN);
1461 rc = mdt_close_internal(info, mdt_info_req(info), NULL);
1462 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1463 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1465 GOTO(out_unlock_list, rc);
1468 /* 4: lock of the object migrated object */
1469 lh_childp = &info->mti_lh[MDT_LH_OLD];
1470 mdt_lock_reg_init(lh_childp, LCK_EX);
1471 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1472 MDS_INODELOCK_LAYOUT;
1473 if (mdt_object_remote(msrcdir)) {
1474 /* Enqueue lookup lock from the parent MDT */
1475 rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
1476 &lh_childp->mlh_rreg_lh,
1477 lh_childp->mlh_rreg_mode,
1478 MDS_INODELOCK_LOOKUP);
1480 GOTO(out_unlock_list, rc);
1482 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1485 rc = mdt_object_lock(info, mold, lh_childp, lock_ibits);
1487 GOTO(out_unlock_child, rc);
1489 ma->ma_need = MA_LMV;
1491 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1492 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1493 rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1495 GOTO(out_unlock_child, rc);
1497 if ((ma->ma_valid & MA_LMV)) {
1498 struct lmv_mds_md_v1 *lmm1;
1500 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1501 lmm1 = &ma->ma_lmv->lmv_md_v1;
1502 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1503 CERROR("%s: can not migrate striped dir "DFID
1504 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1505 PFID(mdt_object_fid(mold)), -EPERM);
1506 GOTO(out_unlock_child, rc = -EPERM);
1509 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1510 GOTO(out_unlock_child, rc = -EINVAL);
1512 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1513 &lmm1->lmv_stripe_fids[1]);
1515 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1517 if (!mdt_object_remote(mnew)) {
1518 CERROR("%s: "DFID" being migrated is on this MDT:"
1519 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1520 PFID(rr->rr_fid2), -EPERM);
1521 GOTO(out_put_new, rc = -EPERM);
1524 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1525 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1526 rc = mdt_remote_object_lock(info, mnew,
1527 mdt_object_fid(mnew),
1528 &lh_tgtp->mlh_rreg_lh,
1529 lh_tgtp->mlh_rreg_mode,
1530 MDS_INODELOCK_UPDATE);
1533 GOTO(out_put_new, rc);
1536 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1539 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1540 if (!mdt_object_remote(mnew)) {
1541 CERROR("%s: Migration "DFID" is on this MDT:"
1542 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1543 PFID(rr->rr_fid2), -EXDEV);
1544 GOTO(out_put_new, rc = -EXDEV);
1549 mdt_reint_init_ma(info, ma);
1551 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1552 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1554 rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1555 mdt_object_child(mold), &rr->rr_name,
1556 mdt_object_child(mnew), ma);
1558 GOTO(out_unlock_new, rc);
1561 if (lh_tgtp != NULL)
1562 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1565 mdt_object_put(info->mti_env, mnew);
1567 mdt_object_unlock(info, mold, lh_childp, rc);
1569 mdt_unlock_list(info, &lock_list, rc);
1570 if (lease != NULL) {
1571 ldlm_reprocess_all(lease->l_resource);
1572 LDLM_LOCK_PUT(lease);
1576 up_write(&mold->mot_open_sem);
1578 mdt_object_put(info->mti_env, mold);
1580 mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1582 mdt_object_put(info->mti_env, msrcdir);
1587 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1588 const struct lu_fid *fid,
1591 struct mdt_object *dir;
1595 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1599 /* check early, the real version will be saved after locking */
1600 rc = mdt_version_get_check(info, dir, idx);
1606 mdt_object_put(info->mti_env, dir);
1610 static int mdt_object_lock_save(struct mdt_thread_info *info,
1611 struct mdt_object *dir,
1612 struct mdt_lock_handle *lh,
1617 /* we lock the target dir if it is local */
1618 rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE);
1622 /* get and save correct version after locking */
1623 mdt_version_get_save(info, dir, idx);
1628 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1629 struct mdt_object **srcp,
1630 struct mdt_object **tgtp)
1632 struct mdt_reint_record *rr = &info->mti_rr;
1633 const struct lu_fid *fid_src = rr->rr_fid1;
1634 const struct lu_fid *fid_tgt = rr->rr_fid2;
1635 struct mdt_lock_handle *lh_src = &info->mti_lh[MDT_LH_PARENT];
1636 struct mdt_lock_handle *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1637 struct mdt_object *src;
1638 struct mdt_object *tgt;
1643 /* find both parents. */
1644 src = mdt_object_find_check(info, fid_src, 0);
1646 RETURN(PTR_ERR(src));
1648 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1650 if (lu_fid_eq(fid_src, fid_tgt)) {
1652 mdt_object_get(info->mti_env, tgt);
1654 /* Check if the @src is not a child of the @tgt, otherwise a
1655 * reverse locking must take place. */
1656 rc = mdt_is_subdir(info, src, fid_tgt);
1660 GOTO(err_src_put, rc);
1662 tgt = mdt_object_find_check(info, fid_tgt, 1);
1664 GOTO(err_src_put, rc = PTR_ERR(tgt));
1667 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1669 /* lock parents in the proper order. */
1671 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1673 GOTO(err_tgt_put, rc);
1675 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1677 rc = mdt_object_lock_save(info, src, lh_src, 0);
1679 rc = mdt_object_lock_save(info, src, lh_src, 0);
1681 GOTO(err_tgt_put, rc);
1683 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1686 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1687 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1688 rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1689 MDS_INODELOCK_UPDATE);
1690 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1694 GOTO(err_unlock, rc);
1696 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1703 /* The order does not matter as the handle is checked inside,
1704 * as well as not used handle. */
1705 mdt_object_unlock(info, src, lh_src, rc);
1706 mdt_object_unlock(info, tgt, lh_tgt, rc);
1708 mdt_object_put(info->mti_env, tgt);
1710 mdt_object_put(info->mti_env, src);
1715 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1716 * 2 - src child; 3 - tgt child.
1717 * Update on disk version of src child.
1720 * For DNE phase I, only these renames are allowed
1721 * mv src_p/src_c tgt_p/tgt_c
1722 * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1723 * 2. src_p and tgt_p are same directory, and tgt_c does not
1724 * exists. In this case, all of modification will happen
1725 * in the MDT where ithesource parent is, only one remote
1726 * update is needed, i.e. set c_time/m_time on the child.
1727 * And tgt_c will be still in the same MDT as the original
1730 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1731 struct mdt_lock_handle *lhc)
1733 struct mdt_reint_record *rr = &info->mti_rr;
1734 struct md_attr *ma = &info->mti_attr;
1735 struct ptlrpc_request *req = mdt_info_req(info);
1736 struct mdt_object *msrcdir = NULL;
1737 struct mdt_object *mtgtdir = NULL;
1738 struct mdt_object *mold;
1739 struct mdt_object *mnew = NULL;
1740 struct mdt_lock_handle *lh_srcdirp;
1741 struct mdt_lock_handle *lh_tgtdirp;
1742 struct mdt_lock_handle *lh_oldp = NULL;
1743 struct mdt_lock_handle *lh_newp = NULL;
1744 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1745 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1750 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1751 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1752 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1754 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1755 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1756 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1757 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1759 /* step 1&2: lock the source and target dirs. */
1760 rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1764 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1766 /* step 3: find & lock the old object. */
1768 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1770 GOTO(out_unlock_parents, rc);
1772 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1773 GOTO(out_unlock_parents, rc = -EINVAL);
1775 if (!fid_is_md_operative(old_fid))
1776 GOTO(out_unlock_parents, rc = -EPERM);
1778 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1780 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1782 /* Check if @mtgtdir is subdir of @mold, before locking child
1783 * to avoid reverse locking. */
1784 rc = mdt_is_subdir(info, mtgtdir, old_fid);
1786 GOTO(out_put_old, rc);
1788 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1789 /* save version after locking */
1790 mdt_version_get_save(info, mold, 2);
1792 /* step 4: find & lock the new object. */
1793 /* new target object may not exist now */
1794 /* lookup with version checking */
1796 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1799 /* the new_fid should have been filled at this moment */
1800 if (lu_fid_eq(old_fid, new_fid))
1801 GOTO(out_put_old, rc);
1803 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1804 lu_fid_eq(new_fid, rr->rr_fid2))
1805 GOTO(out_put_old, rc = -EINVAL);
1807 if (!fid_is_md_operative(new_fid))
1808 GOTO(out_put_old, rc = -EPERM);
1810 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1812 GOTO(out_put_old, rc = PTR_ERR(mnew));
1814 if (mdt_object_remote(mnew)) {
1815 struct mdt_body *repbody;
1817 /* Always send rename req to the target child MDT */
1818 repbody = req_capsule_server_get(info->mti_pill,
1820 LASSERT(repbody != NULL);
1821 repbody->mbo_fid1 = *new_fid;
1822 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1823 GOTO(out_put_old, rc = -EXDEV);
1825 /* Before locking the target dir, check we do not replace
1826 * a dir with a non-dir, otherwise it may deadlock with
1827 * link op which tries to create a link in this dir
1828 * back to this non-dir. */
1829 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1830 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1831 GOTO(out_put_new, rc = -EISDIR);
1833 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1834 mdt_lock_reg_init(lh_oldp, LCK_EX);
1836 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
1837 if (mdt_object_remote(msrcdir)) {
1838 /* Enqueue lookup lock from the parent MDT */
1839 rc = mdt_remote_object_lock(info, msrcdir,
1840 mdt_object_fid(mold),
1841 &lh_oldp->mlh_rreg_lh,
1842 lh_oldp->mlh_rreg_mode,
1843 MDS_INODELOCK_LOOKUP);
1845 GOTO(out_put_new, rc);
1847 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1850 rc = mdt_object_lock(info, mold, lh_oldp, lock_ibits);
1852 GOTO(out_unlock_old, rc);
1854 /* Check if @msrcdir is subdir of @mnew, before locking child
1855 * to avoid reverse locking. */
1856 rc = mdt_is_subdir(info, msrcdir, new_fid);
1858 GOTO(out_unlock_old, rc);
1860 /* We used to acquire MDS_INODELOCK_FULL here but we
1861 * can't do this now because a running HSM restore on
1862 * the rename onto victim will hold the layout
1863 * lock. See LU-4002. */
1865 lh_newp = &info->mti_lh[MDT_LH_NEW];
1866 mdt_lock_reg_init(lh_newp, LCK_EX);
1867 rc = mdt_object_lock(info, mnew, lh_newp,
1868 MDS_INODELOCK_LOOKUP |
1869 MDS_INODELOCK_UPDATE);
1871 GOTO(out_unlock_old, rc);
1873 /* get and save version after locking */
1874 mdt_version_get_save(info, mnew, 3);
1875 } else if (rc != -EREMOTE && rc != -ENOENT) {
1876 GOTO(out_put_old, rc);
1878 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1879 mdt_lock_reg_init(lh_oldp, LCK_EX);
1881 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
1882 if (mdt_object_remote(msrcdir)) {
1883 /* Enqueue lookup lock from the parent MDT */
1884 rc = mdt_remote_object_lock(info, msrcdir,
1885 mdt_object_fid(mold),
1886 &lh_oldp->mlh_rreg_lh,
1887 lh_oldp->mlh_rreg_mode,
1888 MDS_INODELOCK_LOOKUP);
1890 GOTO(out_put_new, rc);
1892 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1895 rc = mdt_object_lock(info, mold, lh_oldp, lock_ibits);
1897 GOTO(out_put_old, rc);
1899 mdt_enoent_version_save(info, 3);
1902 /* step 5: rename it */
1903 mdt_reint_init_ma(info, ma);
1905 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1906 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1909 mutex_lock(&mnew->mot_lov_mutex);
1911 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1912 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1913 mnew != NULL ? mdt_object_child(mnew) : NULL,
1914 &rr->rr_tgt_name, ma);
1917 mutex_unlock(&mnew->mot_lov_mutex);
1919 /* handle last link of tgt object */
1921 mdt_counter_incr(req, LPROC_MDT_RENAME);
1923 mdt_handle_last_unlink(info, mnew, ma);
1925 mdt_rename_counter_tally(info, info->mti_mdt, req,
1931 mdt_object_unlock(info, mnew, lh_newp, rc);
1933 mdt_object_unlock(info, mold, lh_oldp, rc);
1936 mdt_object_put(info->mti_env, mnew);
1938 mdt_object_put(info->mti_env, mold);
1940 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1941 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1945 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1946 struct mdt_lock_handle *lhc, bool rename)
1948 struct mdt_reint_record *rr = &info->mti_rr;
1949 struct ptlrpc_request *req = mdt_info_req(info);
1950 struct lustre_handle rename_lh = { 0 };
1954 if (info->mti_dlm_req)
1955 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1957 if (!fid_is_md_operative(rr->rr_fid1) ||
1958 !fid_is_md_operative(rr->rr_fid2))
1961 /* Note: do not enqueue rename lock for replay request, because
1962 * if other MDT holds rename lock, but being blocked to wait for
1963 * this MDT to finish its recovery, and the failover MDT can not
1964 * get rename lock, which will cause deadlock. */
1965 if (!req_is_replay(req)) {
1966 rc = mdt_rename_lock(info, &rename_lh);
1968 CERROR("%s: can't lock FS for rename: rc = %d\n",
1969 mdt_obd_name(info->mti_mdt), rc);
1975 rc = mdt_reint_rename_internal(info, lhc);
1977 rc = mdt_reint_migrate_internal(info, lhc);
1979 if (lustre_handle_is_used(&rename_lh))
1980 mdt_rename_unlock(&rename_lh);
1985 static int mdt_reint_rename(struct mdt_thread_info *info,
1986 struct mdt_lock_handle *lhc)
1988 return mdt_reint_rename_or_migrate(info, lhc, true);
1991 static int mdt_reint_migrate(struct mdt_thread_info *info,
1992 struct mdt_lock_handle *lhc)
1994 return mdt_reint_rename_or_migrate(info, lhc, false);
1997 struct mdt_reinter {
1998 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
1999 enum lprocfs_extra_opc mr_extra_opc;
2002 static const struct mdt_reinter mdt_reinters[] = {
2004 .mr_handler = &mdt_reint_setattr,
2005 .mr_extra_opc = MDS_REINT_SETATTR,
2008 .mr_handler = &mdt_reint_create,
2009 .mr_extra_opc = MDS_REINT_CREATE,
2012 .mr_handler = &mdt_reint_link,
2013 .mr_extra_opc = MDS_REINT_LINK,
2016 .mr_handler = &mdt_reint_unlink,
2017 .mr_extra_opc = MDS_REINT_UNLINK,
2020 .mr_handler = &mdt_reint_rename,
2021 .mr_extra_opc = MDS_REINT_RENAME,
2024 .mr_handler = &mdt_reint_open,
2025 .mr_extra_opc = MDS_REINT_OPEN,
2027 [REINT_SETXATTR] = {
2028 .mr_handler = &mdt_reint_setxattr,
2029 .mr_extra_opc = MDS_REINT_SETXATTR,
2032 .mr_handler = &mdt_reint_unlink,
2033 .mr_extra_opc = MDS_REINT_UNLINK,
2036 .mr_handler = &mdt_reint_migrate,
2037 .mr_extra_opc = MDS_REINT_RENAME,
2041 int mdt_reint_rec(struct mdt_thread_info *info,
2042 struct mdt_lock_handle *lhc)
2044 const struct mdt_reinter *mr;
2048 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2051 mr = &mdt_reinters[info->mti_rr.rr_opcode];
2052 if (mr->mr_handler == NULL)
2055 rc = (*mr->mr_handler)(info, lhc);
2057 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2058 PTLRPC_LAST_CNTR + mr->mr_extra_opc);