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 union ldlm_policy_data *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 enum ldlm_mode mode, __u64 ibits,
437 struct mdt_lock_handle *s0_lh,
438 struct mdt_object **s0_objp,
439 struct ldlm_enqueue_info *einfo)
441 union ldlm_policy_data *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 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
474 if (!lu_fid_eq(fid, mdt_object_fid(obj))) {
475 /* Except migrating object, whose 0_stripe and master
476 * object are the same object, 0_stripe and master
477 * object are different, though they are in the same
478 * MDT, to avoid adding osd_object_lock here, so we
479 * will enqueue the stripe0 lock in MDT0 for now */
480 *s0_objp = mdt_object_find_lock(mti, fid, s0_lh, ibits);
481 if (IS_ERR(*s0_objp))
482 RETURN(PTR_ERR(*s0_objp));
485 memset(einfo, 0, sizeof(*einfo));
486 einfo->ei_type = LDLM_IBITS;
487 einfo->ei_mode = mode;
488 einfo->ei_cb_bl = mdt_remote_blocking_ast;
489 einfo->ei_cb_local_bl = mdt_blocking_ast;
490 einfo->ei_cb_cp = ldlm_completion_ast;
491 einfo->ei_enq_slave = 1;
492 einfo->ei_namespace = mti->mti_mdt->mdt_namespace;
493 memset(policy, 0, sizeof(*policy));
494 policy->l_inodebits.bits = ibits;
496 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
501 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
504 struct mdt_lock_handle *lh;
505 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
506 __u64 lockpart = MDS_INODELOCK_UPDATE;
507 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
508 struct mdt_lock_handle *s0_lh;
509 struct mdt_object *s0_obj = NULL;
513 lh = &info->mti_lh[MDT_LH_PARENT];
514 mdt_lock_reg_init(lh, LCK_PW);
516 /* Even though the new MDT will grant PERM lock to the old
517 * client, but the old client will almost ignore that during
518 * So it needs to revoke both LOOKUP and PERM lock here, so
519 * both new and old client can cancel the dcache */
520 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
521 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
523 rc = mdt_object_lock(info, mo, lh, lockpart);
527 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
528 mdt_lock_reg_init(s0_lh, LCK_PW);
529 rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
531 GOTO(out_unlock, rc);
533 /* all attrs are packed into mti_attr in unpack_setattr */
534 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
535 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
537 /* This is only for set ctime when rename's source is on remote MDS. */
538 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
539 ma->ma_attr_flags |= MDS_VTX_BYPASS;
541 /* VBR: update version if attr changed are important for recovery */
543 /* update on-disk version of changed object */
544 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
545 rc = mdt_version_get_check_save(info, mo, 0);
547 GOTO(out_unlock, rc);
550 /* Ensure constant striping during chown(). See LU-2789. */
551 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
552 mutex_lock(&mo->mot_lov_mutex);
554 /* all attrs are packed into mti_attr in unpack_setattr */
555 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
557 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
558 mutex_unlock(&mo->mot_lov_mutex);
561 GOTO(out_unlock, rc);
565 mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
566 mdt_object_unlock(info, mo, lh, rc);
571 * Check HSM flags and add HS_DIRTY flag if relevant.
573 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
574 * and is not RELEASED.
576 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
582 /* If the file was modified, add the dirty flag */
583 ma->ma_need = MA_HSM;
584 rc = mdt_attr_get_complex(info, mo, ma);
586 CERROR("file attribute read error for "DFID": %d.\n",
587 PFID(mdt_object_fid(mo)), rc);
591 /* If an up2date copy exists in the backend, add dirty flag */
592 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
593 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
594 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
596 ma->ma_hsm.mh_flags |= HS_DIRTY;
598 mdt_lock_reg_init(lh, LCK_PW);
599 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR);
603 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
605 CERROR("file attribute change error for "DFID": %d\n",
606 PFID(mdt_object_fid(mo)), rc);
607 mdt_object_unlock(info, mo, lh, rc);
613 static int mdt_reint_setattr(struct mdt_thread_info *info,
614 struct mdt_lock_handle *lhc)
616 struct md_attr *ma = &info->mti_attr;
617 struct mdt_reint_record *rr = &info->mti_rr;
618 struct ptlrpc_request *req = mdt_info_req(info);
619 struct mdt_object *mo;
620 struct mdt_body *repbody;
624 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
625 (unsigned int)ma->ma_attr.la_valid);
627 if (info->mti_dlm_req)
628 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
630 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
631 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
633 GOTO(out, rc = PTR_ERR(mo));
635 if (!mdt_object_exists(mo))
636 GOTO(out_put, rc = -ENOENT);
638 if (mdt_object_remote(mo))
639 GOTO(out_put, rc = -EREMOTE);
641 if ((ma->ma_attr.la_valid & LA_SIZE) ||
642 (rr->rr_flags & MRF_OPEN_TRUNC)) {
643 /* Check write access for the O_TRUNC case */
644 if (mdt_write_read(mo) < 0)
645 GOTO(out_put, rc = -ETXTBSY);
648 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
649 if (ma->ma_valid & MA_LOV)
650 GOTO(out_put, rc = -EPROTO);
652 rc = mdt_attr_set(info, mo, ma);
655 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
656 struct lu_buf *buf = &info->mti_buf;
658 if (ma->ma_attr.la_valid != 0)
659 GOTO(out_put, rc = -EPROTO);
661 buf->lb_buf = ma->ma_lmm;
662 buf->lb_len = ma->ma_lmm_size;
663 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
664 buf, XATTR_NAME_LOV, 0);
667 } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
668 struct lu_buf *buf = &info->mti_buf;
669 struct mdt_lock_handle *lh;
671 if (ma->ma_attr.la_valid != 0)
672 GOTO(out_put, rc = -EPROTO);
674 lh = &info->mti_lh[MDT_LH_PARENT];
675 mdt_lock_reg_init(lh, LCK_PW);
677 rc = mdt_object_lock(info, mo, lh,
678 MDS_INODELOCK_XATTR);
682 buf->lb_buf = ma->ma_lmv;
683 buf->lb_len = ma->ma_lmv_size;
684 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
685 buf, XATTR_NAME_DEFAULT_LMV, 0);
687 mdt_object_unlock(info, mo, lh, rc);
691 GOTO(out_put, rc = -EPROTO);
694 /* If file data is modified, add the dirty flag */
695 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
696 rc = mdt_add_dirty_flag(info, mo, ma);
698 ma->ma_need = MA_INODE;
700 rc = mdt_attr_get_complex(info, mo, ma);
704 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
708 mdt_object_put(info->mti_env, mo);
711 mdt_counter_incr(req, LPROC_MDT_SETATTR);
713 mdt_client_compatibility(info);
714 rc2 = mdt_fix_reply(info);
720 static int mdt_reint_create(struct mdt_thread_info *info,
721 struct mdt_lock_handle *lhc)
723 struct ptlrpc_request *req = mdt_info_req(info);
727 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
728 RETURN(err_serious(-ESTALE));
730 if (info->mti_dlm_req)
731 ldlm_request_cancel(mdt_info_req(info),
732 info->mti_dlm_req, 0, LATF_SKIP);
734 if (!lu_name_is_valid(&info->mti_rr.rr_name))
737 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
739 mdt_counter_incr(req, LPROC_MDT_MKDIR);
747 /* Special file should stay on the same node as parent. */
748 mdt_counter_incr(req, LPROC_MDT_MKNOD);
751 CERROR("%s: Unsupported mode %o\n",
752 mdt_obd_name(info->mti_mdt),
753 info->mti_attr.ma_attr.la_mode);
754 RETURN(err_serious(-EOPNOTSUPP));
757 rc = mdt_md_create(info);
762 * VBR: save parent version in reply and child version getting by its name.
763 * Version of child is getting and checking during its lookup. If
765 static int mdt_reint_unlink(struct mdt_thread_info *info,
766 struct mdt_lock_handle *lhc)
768 struct mdt_reint_record *rr = &info->mti_rr;
769 struct ptlrpc_request *req = mdt_info_req(info);
770 struct md_attr *ma = &info->mti_attr;
771 struct lu_fid *child_fid = &info->mti_tmp_fid1;
772 struct mdt_object *mp;
773 struct mdt_object *mc;
774 struct mdt_lock_handle *parent_lh;
775 struct mdt_lock_handle *child_lh;
776 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
777 struct mdt_lock_handle *s0_lh = NULL;
778 struct mdt_object *s0_obj = NULL;
784 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
785 PNAME(&rr->rr_name));
787 if (info->mti_dlm_req)
788 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
790 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
791 RETURN(err_serious(-ENOENT));
793 if (!fid_is_md_operative(rr->rr_fid1))
797 * step 1: Found the parent.
799 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
805 parent_lh = &info->mti_lh[MDT_LH_PARENT];
806 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
807 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE);
809 GOTO(put_parent, rc);
811 if (!mdt_object_remote(mp)) {
812 rc = mdt_version_get_check_save(info, mp, 0);
814 GOTO(unlock_parent, rc);
817 /* step 2: find & lock the child */
818 /* lookup child object along with version checking */
820 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
822 /* Name might not be able to find during resend of
823 * remote unlink, considering following case.
824 * dir_A is a remote directory, the name entry of
825 * dir_A is on MDT0, the directory is on MDT1,
827 * 1. client sends unlink req to MDT1.
828 * 2. MDT1 sends name delete update to MDT0.
829 * 3. name entry is being deleted in MDT0 synchronously.
830 * 4. MDT1 is restarted.
831 * 5. client resends unlink req to MDT1. So it can not
832 * find the name entry on MDT0 anymore.
833 * In this case, MDT1 only needs to destory the local
836 if (mdt_object_remote(mp) && rc == -ENOENT &&
837 !fid_is_zero(rr->rr_fid2) &&
838 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
840 *child_fid = *rr->rr_fid2;
842 GOTO(unlock_parent, rc);
846 if (!fid_is_md_operative(child_fid))
847 GOTO(unlock_parent, rc = -EPERM);
849 /* We will lock the child regardless it is local or remote. No harm. */
850 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
852 GOTO(unlock_parent, rc = PTR_ERR(mc));
854 child_lh = &info->mti_lh[MDT_LH_CHILD];
855 mdt_lock_reg_init(child_lh, LCK_EX);
856 if (mdt_object_remote(mc)) {
857 struct mdt_body *repbody;
859 if (!fid_is_zero(rr->rr_fid2)) {
860 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
861 mdt_obd_name(info->mti_mdt),
862 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
863 GOTO(put_child, rc = -ENOENT);
865 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
866 mdt_obd_name(info->mti_mdt),
867 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
869 if (!mdt_is_dne_client(req->rq_export))
870 /* Return -ENOTSUPP for old client */
871 GOTO(put_child, rc = -ENOTSUPP);
873 if (info->mti_spec.sp_rm_entry) {
874 struct lu_ucred *uc = mdt_ucred(info);
876 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
877 CERROR("%s: unlink remote entry is only "
878 "permitted for administrator: rc = %d\n",
879 mdt_obd_name(info->mti_mdt),
881 GOTO(put_child, rc = -EPERM);
884 ma->ma_need = MA_INODE;
886 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
887 NULL, &rr->rr_name, ma, no_name);
890 /* Revoke the LOOKUP lock of the remote object granted by
891 * this MDT. Since the unlink will happen on another MDT,
892 * it will release the LOOKUP lock right away. Then What
893 * would happen if another client try to grab the LOOKUP
894 * lock at the same time with unlink XXX */
895 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP);
896 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
897 LASSERT(repbody != NULL);
898 repbody->mbo_fid1 = *mdt_object_fid(mc);
899 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
900 GOTO(unlock_child, rc = -EREMOTE);
901 } else if (info->mti_spec.sp_rm_entry) {
903 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
905 mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
909 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
910 * this now because a running HSM restore on the child (unlink
911 * victim) will hold the layout lock. See LU-4002. */
912 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
913 if (mdt_object_remote(mp)) {
914 /* Enqueue lookup lock from parent MDT */
915 rc = mdt_remote_object_lock(info, mp, mdt_object_fid(mc),
916 &child_lh->mlh_rreg_lh,
917 child_lh->mlh_rreg_mode,
918 MDS_INODELOCK_LOOKUP, false);
922 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
925 rc = mdt_object_lock(info, mc, child_lh, lock_ibits);
929 * Now we can only make sure we need MA_INODE, in mdd layer, will check
930 * whether need MA_LOV and MA_COOKIE.
932 ma->ma_need = MA_INODE;
935 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
936 mdt_lock_reg_init(s0_lh, LCK_EX);
937 rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
940 GOTO(unlock_child, rc);
942 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
943 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
944 /* save version when object is locked */
945 mdt_version_get_save(info, mc, 1);
947 mutex_lock(&mc->mot_lov_mutex);
949 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
950 mdt_object_child(mc), &rr->rr_name, ma, no_name);
952 mutex_unlock(&mc->mot_lov_mutex);
954 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
955 rc = mdt_attr_get_complex(info, mc, ma);
957 mdt_handle_last_unlink(info, mc, ma);
959 if (ma->ma_valid & MA_INODE) {
960 switch (ma->ma_attr.la_mode & S_IFMT) {
962 mdt_counter_incr(req, LPROC_MDT_RMDIR);
970 mdt_counter_incr(req, LPROC_MDT_UNLINK);
973 LASSERTF(0, "bad file type %o unlinking\n",
974 ma->ma_attr.la_mode);
981 mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
982 mdt_object_unlock(info, mc, child_lh, rc);
984 mdt_object_put(info->mti_env, mc);
986 mdt_object_unlock(info, mp, parent_lh, rc);
988 mdt_object_put(info->mti_env, mp);
994 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
997 static int mdt_reint_link(struct mdt_thread_info *info,
998 struct mdt_lock_handle *lhc)
1000 struct mdt_reint_record *rr = &info->mti_rr;
1001 struct ptlrpc_request *req = mdt_info_req(info);
1002 struct md_attr *ma = &info->mti_attr;
1003 struct mdt_object *ms;
1004 struct mdt_object *mp;
1005 struct mdt_lock_handle *lhs;
1006 struct mdt_lock_handle *lhp;
1010 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1011 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1013 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1014 RETURN(err_serious(-ENOENT));
1016 if (info->mti_dlm_req)
1017 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1019 /* Invalid case so return error immediately instead of
1021 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1024 if (!fid_is_md_operative(rr->rr_fid1) ||
1025 !fid_is_md_operative(rr->rr_fid2))
1028 /* step 1: find & lock the target parent dir */
1029 lhp = &info->mti_lh[MDT_LH_PARENT];
1030 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1031 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1032 MDS_INODELOCK_UPDATE);
1034 RETURN(PTR_ERR(mp));
1036 rc = mdt_version_get_check_save(info, mp, 0);
1038 GOTO(out_unlock_parent, rc);
1040 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1042 /* step 2: find & lock the source */
1043 lhs = &info->mti_lh[MDT_LH_CHILD];
1044 mdt_lock_reg_init(lhs, LCK_EX);
1046 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1048 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1050 if (!mdt_object_exists(ms)) {
1051 mdt_object_put(info->mti_env, ms);
1052 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1053 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1054 GOTO(out_unlock_parent, rc = -ENOENT);
1057 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1058 MDS_INODELOCK_XATTR);
1060 mdt_object_put(info->mti_env, ms);
1061 GOTO(out_unlock_parent, rc);
1064 /* step 3: link it */
1065 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1066 OBD_FAIL_MDS_REINT_LINK_WRITE);
1068 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1069 rc = mdt_version_get_check_save(info, ms, 1);
1071 GOTO(out_unlock_child, rc);
1073 /** check target version by name during replay */
1074 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1075 &info->mti_tmp_fid1, 2);
1076 if (rc != 0 && rc != -ENOENT)
1077 GOTO(out_unlock_child, rc);
1078 /* save version of file name for replay, it must be ENOENT here */
1079 if (!req_is_replay(mdt_info_req(info))) {
1080 if (rc != -ENOENT) {
1081 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1082 PNAME(&rr->rr_name));
1083 GOTO(out_unlock_child, rc = -EEXIST);
1085 info->mti_ver[2] = ENOENT_VERSION;
1086 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1089 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1090 mdt_object_child(ms), &rr->rr_name, ma);
1093 mdt_counter_incr(req, LPROC_MDT_LINK);
1097 mdt_object_unlock_put(info, ms, lhs, rc);
1099 mdt_object_unlock_put(info, mp, lhp, rc);
1103 * lock the part of the directory according to the hash of the name
1104 * (lh->mlh_pdo_hash) in parallel directory lock.
1106 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1107 struct mdt_lock_handle *lh,
1108 struct mdt_object *obj, __u64 ibits)
1110 struct ldlm_res_id *res = &info->mti_res_id;
1111 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1112 union ldlm_policy_data *policy = &info->mti_policy;
1116 * Finish res_id initializing by name hash marking part of
1117 * directory which is taking modification.
1119 LASSERT(lh->mlh_pdo_hash != 0);
1120 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1121 memset(policy, 0, sizeof(*policy));
1122 policy->l_inodebits.bits = ibits;
1124 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1125 * going to be sent to client. If it is - mdt_intent_policy() path will
1126 * fix it up and turn FL_LOCAL flag off.
1128 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1129 res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1130 &info->mti_exp->exp_handle.h_cookie);
1135 * Get BFL lock for rename or migrate process.
1137 static int mdt_rename_lock(struct mdt_thread_info *info,
1138 struct lustre_handle *lh)
1143 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0) {
1144 struct lu_fid *fid = &info->mti_tmp_fid1;
1145 struct mdt_object *obj;
1147 /* XXX, right now, it has to use object API to
1148 * enqueue lock cross MDT, so it will enqueue
1149 * rename lock(with LUSTRE_BFL_FID) by root object */
1151 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1153 RETURN(PTR_ERR(obj));
1155 rc = mdt_remote_object_lock(info, obj,
1156 &LUSTRE_BFL_FID, lh,
1158 MDS_INODELOCK_UPDATE, false);
1159 mdt_object_put(info->mti_env, obj);
1161 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1162 union ldlm_policy_data *policy = &info->mti_policy;
1163 struct ldlm_res_id *res_id = &info->mti_res_id;
1166 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1167 memset(policy, 0, sizeof *policy);
1168 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1169 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1170 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1171 LCK_EX, &flags, ldlm_blocking_ast,
1172 ldlm_completion_ast, NULL, NULL, 0,
1174 &info->mti_exp->exp_handle.h_cookie,
1181 static void mdt_rename_unlock(struct lustre_handle *lh)
1184 LASSERT(lustre_handle_is_used(lh));
1185 /* Cancel the single rename lock right away */
1186 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1191 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1192 * target. Source should not be ancestor of target dir. May be other rename
1193 * checks can be moved here later.
1195 static int mdt_is_subdir(struct mdt_thread_info *info,
1196 struct mdt_object *dir,
1197 const struct lu_fid *fid)
1199 struct lu_fid dir_fid = dir->mot_header.loh_fid;
1203 /* If the source and target are in the same directory, they can not
1204 * be parent/child relationship, so subdir check is not needed */
1205 if (lu_fid_eq(&dir_fid, fid))
1208 if (!mdt_object_exists(dir))
1211 rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1214 CERROR("%s: failed subdir check in "DFID" for "DFID
1215 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1216 PFID(&dir_fid), PFID(fid), rc);
1217 /* Return EINVAL only if a parent is the @fid */
1221 /* check the found fid */
1222 if (lu_fid_eq(&dir_fid, fid))
1229 /* Update object linkEA */
1230 struct mdt_lock_list {
1231 struct mdt_object *mll_obj;
1232 struct mdt_lock_handle mll_lh;
1233 struct list_head mll_list;
1236 static void mdt_unlock_list(struct mdt_thread_info *info,
1237 struct list_head *list, int rc)
1239 struct mdt_lock_list *mll;
1240 struct mdt_lock_list *mll2;
1242 list_for_each_entry_safe(mll, mll2, list, mll_list) {
1243 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1244 list_del(&mll->mll_list);
1249 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1250 struct mdt_object *obj,
1251 struct mdt_object *pobj,
1252 struct list_head *lock_list)
1254 struct lu_buf *buf = &info->mti_big_buf;
1255 struct linkea_data ldata = { NULL };
1260 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1263 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1264 if (buf->lb_buf == NULL)
1268 rc = mdt_links_read(info, obj, &ldata);
1270 if (rc == -ENOENT || rc == -ENODATA)
1275 LASSERT(ldata.ld_leh != NULL);
1276 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1277 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1278 struct mdt_device *mdt = info->mti_mdt;
1279 struct mdt_object *mdt_pobj;
1280 struct mdt_lock_list *mll;
1281 struct lu_name name;
1284 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1286 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1288 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1289 if (IS_ERR(mdt_pobj)) {
1290 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1291 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1295 if (!mdt_object_exists(mdt_pobj)) {
1296 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1297 mdt_obd_name(mdt), PFID(&fid));
1298 mdt_object_put(info->mti_env, mdt_pobj);
1302 if (mdt_pobj == pobj) {
1303 CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1304 mdt_obd_name(mdt), PFID(&fid));
1305 mdt_object_put(info->mti_env, mdt_pobj);
1311 mdt_object_put(info->mti_env, mdt_pobj);
1312 GOTO(out, rc = -ENOMEM);
1315 /* Since this needs to lock all of objects in linkea, to avoid
1316 * deadlocks, because it does not follow parent-child order as
1317 * other MDT operation, let's use try_lock here, i.e. it will
1318 * return immediately once there are conflict locks, and return
1319 * EBUSY to client */
1320 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1321 rc = mdt_object_lock_try(info, mdt_pobj, &mll->mll_lh,
1322 MDS_INODELOCK_UPDATE);
1324 CDEBUG(D_ERROR, "%s: cannot lock "DFID": rc =%d\n",
1325 mdt_obd_name(mdt), PFID(&fid), rc);
1326 mdt_object_put(info->mti_env, mdt_pobj);
1328 GOTO(out, rc = -EBUSY);
1331 INIT_LIST_HEAD(&mll->mll_list);
1332 mll->mll_obj = mdt_pobj;
1333 list_add_tail(&mll->mll_list, lock_list);
1337 mdt_unlock_list(info, lock_list, rc);
1341 /* migrate files from one MDT to another MDT */
1342 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1343 struct mdt_lock_handle *lhc)
1345 struct mdt_reint_record *rr = &info->mti_rr;
1346 struct md_attr *ma = &info->mti_attr;
1347 struct mdt_object *msrcdir;
1348 struct mdt_object *mold;
1349 struct mdt_object *mnew = NULL;
1350 struct mdt_lock_handle *lh_dirp;
1351 struct mdt_lock_handle *lh_childp;
1352 struct mdt_lock_handle *lh_tgtp = NULL;
1353 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1354 struct list_head lock_list;
1356 struct ldlm_lock *lease = NULL;
1357 bool lock_open_sem = false;
1361 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1362 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1364 /* 1: lock the source dir. */
1365 msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1366 if (IS_ERR(msrcdir)) {
1367 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1368 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1369 (int)PTR_ERR(msrcdir));
1370 RETURN(PTR_ERR(msrcdir));
1373 lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1374 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1375 rc = mdt_object_lock(info, msrcdir, lh_dirp,
1376 MDS_INODELOCK_UPDATE);
1378 GOTO(out_put_parent, rc);
1380 if (!mdt_object_remote(msrcdir)) {
1381 rc = mdt_version_get_check_save(info, msrcdir, 0);
1383 GOTO(out_unlock_parent, rc);
1386 /* 2: sanity check and find the object to be migrated. */
1388 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1390 GOTO(out_unlock_parent, rc);
1392 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1393 GOTO(out_unlock_parent, rc = -EINVAL);
1395 if (!fid_is_md_operative(old_fid))
1396 GOTO(out_unlock_parent, rc = -EPERM);
1398 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1400 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1402 if (mdt_object_remote(mold)) {
1403 CERROR("%s: source "DFID" is on the remote MDT\n",
1404 mdt_obd_name(info->mti_mdt), PFID(old_fid));
1405 GOTO(out_put_child, rc = -EREMOTE);
1408 if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1409 !mdt_object_remote(msrcdir)) {
1410 CERROR("%s: parent "DFID" is still on the same"
1411 " MDT, which should be migrated first:"
1412 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1413 PFID(mdt_object_fid(msrcdir)), -EPERM);
1414 GOTO(out_put_child, rc = -EPERM);
1417 rc = mdt_remote_permission(info, msrcdir, mold);
1419 GOTO(out_put_child, rc);
1421 /* 3: iterate the linkea of the object and lock all of the objects */
1422 INIT_LIST_HEAD(&lock_list);
1423 rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1425 GOTO(out_put_child, rc);
1427 if (info->mti_spec.sp_migrate_close) {
1428 struct close_data *data;
1429 struct mdt_body *repbody;
1430 bool lease_broken = false;
1432 if (!req_capsule_field_present(info->mti_pill, &RMF_MDT_EPOCH,
1434 !req_capsule_field_present(info->mti_pill, &RMF_CLOSE_DATA,
1436 GOTO(out_lease, rc = -EPROTO);
1438 data = req_capsule_client_get(info->mti_pill, &RMF_CLOSE_DATA);
1440 GOTO(out_lease, rc = -EPROTO);
1442 lease = ldlm_handle2lock(&data->cd_handle);
1444 GOTO(out_lease, rc = -ESTALE);
1446 /* try to hold open_sem so that nobody else can open the file */
1447 if (!down_write_trylock(&mold->mot_open_sem)) {
1448 ldlm_lock_cancel(lease);
1449 GOTO(out_lease, rc = -EBUSY);
1452 lock_open_sem = true;
1453 /* Check if the lease open lease has already canceled */
1454 lock_res_and_lock(lease);
1455 lease_broken = ldlm_is_cancel(lease);
1456 unlock_res_and_lock(lease);
1458 LDLM_DEBUG(lease, DFID " lease broken? %d\n",
1459 PFID(mdt_object_fid(mold)), lease_broken);
1461 /* Cancel server side lease. Client side counterpart should
1462 * have been cancelled. It's okay to cancel it now as we've
1463 * held mot_open_sem. */
1464 ldlm_lock_cancel(lease);
1467 GOTO(out_lease, rc = -EAGAIN);
1469 rc = mdt_close_internal(info, mdt_info_req(info), NULL);
1470 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1471 repbody->mbo_valid |= OBD_MD_CLOSE_INTENT_EXECED;
1473 GOTO(out_unlock_list, rc);
1476 /* 4: lock of the object migrated object */
1477 lh_childp = &info->mti_lh[MDT_LH_OLD];
1478 mdt_lock_reg_init(lh_childp, LCK_EX);
1479 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1480 MDS_INODELOCK_LAYOUT;
1481 if (mdt_object_remote(msrcdir)) {
1482 /* Enqueue lookup lock from the parent MDT */
1483 rc = mdt_remote_object_lock(info, msrcdir, mdt_object_fid(mold),
1484 &lh_childp->mlh_rreg_lh,
1485 lh_childp->mlh_rreg_mode,
1486 MDS_INODELOCK_LOOKUP, false);
1488 GOTO(out_unlock_list, rc);
1490 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1493 rc = mdt_object_lock(info, mold, lh_childp, lock_ibits);
1495 GOTO(out_unlock_child, rc);
1497 ma->ma_need = MA_LMV;
1499 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1500 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1501 rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1503 GOTO(out_unlock_child, rc);
1505 if ((ma->ma_valid & MA_LMV)) {
1506 struct lmv_mds_md_v1 *lmm1;
1508 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1509 lmm1 = &ma->ma_lmv->lmv_md_v1;
1510 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1511 CERROR("%s: can not migrate striped dir "DFID
1512 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1513 PFID(mdt_object_fid(mold)), -EPERM);
1514 GOTO(out_unlock_child, rc = -EPERM);
1517 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1518 GOTO(out_unlock_child, rc = -EINVAL);
1520 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1521 &lmm1->lmv_stripe_fids[1]);
1523 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1525 if (!mdt_object_remote(mnew)) {
1526 CERROR("%s: "DFID" being migrated is on this MDT:"
1527 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1528 PFID(rr->rr_fid2), -EPERM);
1529 GOTO(out_put_new, rc = -EPERM);
1532 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1533 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1534 rc = mdt_remote_object_lock(info, mnew,
1535 mdt_object_fid(mnew),
1536 &lh_tgtp->mlh_rreg_lh,
1537 lh_tgtp->mlh_rreg_mode,
1538 MDS_INODELOCK_UPDATE, false);
1541 GOTO(out_put_new, rc);
1544 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1547 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1548 if (!mdt_object_remote(mnew)) {
1549 CERROR("%s: Migration "DFID" is on this MDT:"
1550 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1551 PFID(rr->rr_fid2), -EXDEV);
1552 GOTO(out_put_new, rc = -EXDEV);
1557 mdt_reint_init_ma(info, ma);
1559 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1560 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1562 rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1563 mdt_object_child(mold), &rr->rr_name,
1564 mdt_object_child(mnew), ma);
1566 GOTO(out_unlock_new, rc);
1569 if (lh_tgtp != NULL)
1570 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1573 mdt_object_put(info->mti_env, mnew);
1575 mdt_object_unlock(info, mold, lh_childp, rc);
1577 mdt_unlock_list(info, &lock_list, rc);
1578 if (lease != NULL) {
1579 ldlm_reprocess_all(lease->l_resource);
1580 LDLM_LOCK_PUT(lease);
1584 up_write(&mold->mot_open_sem);
1586 mdt_object_put(info->mti_env, mold);
1588 mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1590 mdt_object_put(info->mti_env, msrcdir);
1595 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1596 const struct lu_fid *fid,
1599 struct mdt_object *dir;
1603 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1607 /* check early, the real version will be saved after locking */
1608 rc = mdt_version_get_check(info, dir, idx);
1614 mdt_object_put(info->mti_env, dir);
1618 static int mdt_object_lock_save(struct mdt_thread_info *info,
1619 struct mdt_object *dir,
1620 struct mdt_lock_handle *lh,
1625 /* we lock the target dir if it is local */
1626 rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE);
1630 /* get and save correct version after locking */
1631 mdt_version_get_save(info, dir, idx);
1636 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1637 struct mdt_object **srcp,
1638 struct mdt_object **tgtp)
1640 struct mdt_reint_record *rr = &info->mti_rr;
1641 const struct lu_fid *fid_src = rr->rr_fid1;
1642 const struct lu_fid *fid_tgt = rr->rr_fid2;
1643 struct mdt_lock_handle *lh_src = &info->mti_lh[MDT_LH_PARENT];
1644 struct mdt_lock_handle *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1645 struct mdt_object *src;
1646 struct mdt_object *tgt;
1651 /* find both parents. */
1652 src = mdt_object_find_check(info, fid_src, 0);
1654 RETURN(PTR_ERR(src));
1656 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1658 if (lu_fid_eq(fid_src, fid_tgt)) {
1660 mdt_object_get(info->mti_env, tgt);
1662 /* Check if the @src is not a child of the @tgt, otherwise a
1663 * reverse locking must take place. */
1664 rc = mdt_is_subdir(info, src, fid_tgt);
1668 GOTO(err_src_put, rc);
1670 tgt = mdt_object_find_check(info, fid_tgt, 1);
1672 GOTO(err_src_put, rc = PTR_ERR(tgt));
1675 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1677 /* lock parents in the proper order. */
1679 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1681 GOTO(err_tgt_put, rc);
1683 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1685 rc = mdt_object_lock_save(info, src, lh_src, 0);
1687 rc = mdt_object_lock_save(info, src, lh_src, 0);
1689 GOTO(err_tgt_put, rc);
1691 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1694 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1695 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1696 rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1697 MDS_INODELOCK_UPDATE);
1698 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1702 GOTO(err_unlock, rc);
1704 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1711 /* The order does not matter as the handle is checked inside,
1712 * as well as not used handle. */
1713 mdt_object_unlock(info, src, lh_src, rc);
1714 mdt_object_unlock(info, tgt, lh_tgt, rc);
1716 mdt_object_put(info->mti_env, tgt);
1718 mdt_object_put(info->mti_env, src);
1723 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1724 * 2 - src child; 3 - tgt child.
1725 * Update on disk version of src child.
1728 * For DNE phase I, only these renames are allowed
1729 * mv src_p/src_c tgt_p/tgt_c
1730 * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1731 * 2. src_p and tgt_p are same directory, and tgt_c does not
1732 * exists. In this case, all of modification will happen
1733 * in the MDT where ithesource parent is, only one remote
1734 * update is needed, i.e. set c_time/m_time on the child.
1735 * And tgt_c will be still in the same MDT as the original
1738 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1739 struct mdt_lock_handle *lhc)
1741 struct mdt_reint_record *rr = &info->mti_rr;
1742 struct md_attr *ma = &info->mti_attr;
1743 struct ptlrpc_request *req = mdt_info_req(info);
1744 struct mdt_object *msrcdir = NULL;
1745 struct mdt_object *mtgtdir = NULL;
1746 struct mdt_object *mold;
1747 struct mdt_object *mnew = NULL;
1748 struct mdt_lock_handle *lh_srcdirp;
1749 struct mdt_lock_handle *lh_tgtdirp;
1750 struct mdt_lock_handle *lh_oldp = NULL;
1751 struct mdt_lock_handle *lh_newp = NULL;
1752 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1753 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1758 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1759 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1760 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1762 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1763 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1764 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1765 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1767 /* step 1&2: lock the source and target dirs. */
1768 rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1772 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1774 /* step 3: find & lock the old object. */
1776 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1778 GOTO(out_unlock_parents, rc);
1780 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1781 GOTO(out_unlock_parents, rc = -EINVAL);
1783 if (!fid_is_md_operative(old_fid))
1784 GOTO(out_unlock_parents, rc = -EPERM);
1786 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1788 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1790 /* Check if @mtgtdir is subdir of @mold, before locking child
1791 * to avoid reverse locking. */
1792 rc = mdt_is_subdir(info, mtgtdir, old_fid);
1794 GOTO(out_put_old, rc);
1796 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1797 /* save version after locking */
1798 mdt_version_get_save(info, mold, 2);
1800 /* step 4: find & lock the new object. */
1801 /* new target object may not exist now */
1802 /* lookup with version checking */
1804 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1807 /* the new_fid should have been filled at this moment */
1808 if (lu_fid_eq(old_fid, new_fid))
1809 GOTO(out_put_old, rc);
1811 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1812 lu_fid_eq(new_fid, rr->rr_fid2))
1813 GOTO(out_put_old, rc = -EINVAL);
1815 if (!fid_is_md_operative(new_fid))
1816 GOTO(out_put_old, rc = -EPERM);
1818 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1820 GOTO(out_put_old, rc = PTR_ERR(mnew));
1822 if (mdt_object_remote(mnew)) {
1823 struct mdt_body *repbody;
1825 /* Always send rename req to the target child MDT */
1826 repbody = req_capsule_server_get(info->mti_pill,
1828 LASSERT(repbody != NULL);
1829 repbody->mbo_fid1 = *new_fid;
1830 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
1831 GOTO(out_put_old, rc = -EXDEV);
1833 /* Before locking the target dir, check we do not replace
1834 * a dir with a non-dir, otherwise it may deadlock with
1835 * link op which tries to create a link in this dir
1836 * back to this non-dir. */
1837 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1838 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1839 GOTO(out_put_new, rc = -EISDIR);
1841 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1842 mdt_lock_reg_init(lh_oldp, LCK_EX);
1844 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
1845 if (mdt_object_remote(msrcdir)) {
1846 /* Enqueue lookup lock from the parent MDT */
1847 rc = mdt_remote_object_lock(info, msrcdir,
1848 mdt_object_fid(mold),
1849 &lh_oldp->mlh_rreg_lh,
1850 lh_oldp->mlh_rreg_mode,
1851 MDS_INODELOCK_LOOKUP,
1854 GOTO(out_put_new, rc);
1856 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1859 rc = mdt_object_lock(info, mold, lh_oldp, lock_ibits);
1861 GOTO(out_unlock_old, rc);
1863 /* Check if @msrcdir is subdir of @mnew, before locking child
1864 * to avoid reverse locking. */
1865 rc = mdt_is_subdir(info, msrcdir, new_fid);
1867 GOTO(out_unlock_old, rc);
1869 /* We used to acquire MDS_INODELOCK_FULL here but we
1870 * can't do this now because a running HSM restore on
1871 * the rename onto victim will hold the layout
1872 * lock. See LU-4002. */
1874 lh_newp = &info->mti_lh[MDT_LH_NEW];
1875 mdt_lock_reg_init(lh_newp, LCK_EX);
1876 rc = mdt_object_lock(info, mnew, lh_newp,
1877 MDS_INODELOCK_LOOKUP |
1878 MDS_INODELOCK_UPDATE);
1880 GOTO(out_unlock_old, rc);
1882 /* get and save version after locking */
1883 mdt_version_get_save(info, mnew, 3);
1884 } else if (rc != -EREMOTE && rc != -ENOENT) {
1885 GOTO(out_put_old, rc);
1887 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1888 mdt_lock_reg_init(lh_oldp, LCK_EX);
1890 lock_ibits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_XATTR;
1891 if (mdt_object_remote(msrcdir)) {
1892 /* Enqueue lookup lock from the parent MDT */
1893 rc = mdt_remote_object_lock(info, msrcdir,
1894 mdt_object_fid(mold),
1895 &lh_oldp->mlh_rreg_lh,
1896 lh_oldp->mlh_rreg_mode,
1897 MDS_INODELOCK_LOOKUP,
1900 GOTO(out_put_new, rc);
1902 lock_ibits &= ~MDS_INODELOCK_LOOKUP;
1905 rc = mdt_object_lock(info, mold, lh_oldp, lock_ibits);
1907 GOTO(out_put_old, rc);
1909 mdt_enoent_version_save(info, 3);
1912 /* step 5: rename it */
1913 mdt_reint_init_ma(info, ma);
1915 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1916 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1919 mutex_lock(&mnew->mot_lov_mutex);
1921 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1922 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1923 mnew != NULL ? mdt_object_child(mnew) : NULL,
1924 &rr->rr_tgt_name, ma);
1927 mutex_unlock(&mnew->mot_lov_mutex);
1929 /* handle last link of tgt object */
1931 mdt_counter_incr(req, LPROC_MDT_RENAME);
1933 mdt_handle_last_unlink(info, mnew, ma);
1935 mdt_rename_counter_tally(info, info->mti_mdt, req,
1941 mdt_object_unlock(info, mnew, lh_newp, rc);
1943 mdt_object_unlock(info, mold, lh_oldp, rc);
1946 mdt_object_put(info->mti_env, mnew);
1948 mdt_object_put(info->mti_env, mold);
1950 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1951 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1955 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1956 struct mdt_lock_handle *lhc, bool rename)
1958 struct mdt_reint_record *rr = &info->mti_rr;
1959 struct ptlrpc_request *req = mdt_info_req(info);
1960 struct lustre_handle rename_lh = { 0 };
1964 if (info->mti_dlm_req)
1965 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1967 if (!fid_is_md_operative(rr->rr_fid1) ||
1968 !fid_is_md_operative(rr->rr_fid2))
1971 /* Note: do not enqueue rename lock for replay request, because
1972 * if other MDT holds rename lock, but being blocked to wait for
1973 * this MDT to finish its recovery, and the failover MDT can not
1974 * get rename lock, which will cause deadlock. */
1975 if (!req_is_replay(req)) {
1976 rc = mdt_rename_lock(info, &rename_lh);
1978 CERROR("%s: can't lock FS for rename: rc = %d\n",
1979 mdt_obd_name(info->mti_mdt), rc);
1985 rc = mdt_reint_rename_internal(info, lhc);
1987 rc = mdt_reint_migrate_internal(info, lhc);
1989 if (lustre_handle_is_used(&rename_lh))
1990 mdt_rename_unlock(&rename_lh);
1995 static int mdt_reint_rename(struct mdt_thread_info *info,
1996 struct mdt_lock_handle *lhc)
1998 return mdt_reint_rename_or_migrate(info, lhc, true);
2001 static int mdt_reint_migrate(struct mdt_thread_info *info,
2002 struct mdt_lock_handle *lhc)
2004 return mdt_reint_rename_or_migrate(info, lhc, false);
2007 struct mdt_reinter {
2008 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
2009 enum lprocfs_extra_opc mr_extra_opc;
2012 static const struct mdt_reinter mdt_reinters[] = {
2014 .mr_handler = &mdt_reint_setattr,
2015 .mr_extra_opc = MDS_REINT_SETATTR,
2018 .mr_handler = &mdt_reint_create,
2019 .mr_extra_opc = MDS_REINT_CREATE,
2022 .mr_handler = &mdt_reint_link,
2023 .mr_extra_opc = MDS_REINT_LINK,
2026 .mr_handler = &mdt_reint_unlink,
2027 .mr_extra_opc = MDS_REINT_UNLINK,
2030 .mr_handler = &mdt_reint_rename,
2031 .mr_extra_opc = MDS_REINT_RENAME,
2034 .mr_handler = &mdt_reint_open,
2035 .mr_extra_opc = MDS_REINT_OPEN,
2037 [REINT_SETXATTR] = {
2038 .mr_handler = &mdt_reint_setxattr,
2039 .mr_extra_opc = MDS_REINT_SETXATTR,
2042 .mr_handler = &mdt_reint_unlink,
2043 .mr_extra_opc = MDS_REINT_UNLINK,
2046 .mr_handler = &mdt_reint_migrate,
2047 .mr_extra_opc = MDS_REINT_RENAME,
2051 int mdt_reint_rec(struct mdt_thread_info *info,
2052 struct mdt_lock_handle *lhc)
2054 const struct mdt_reinter *mr;
2058 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2061 mr = &mdt_reinters[info->mti_rr.rr_opcode];
2062 if (mr->mr_handler == NULL)
2065 rc = (*mr->mr_handler)(info, lhc);
2067 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2068 PTLRPC_LAST_CNTR + mr->mr_extra_opc);