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;
60 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
61 struct mdt_object *object,
62 struct mdt_body *repbody)
66 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
67 * we won't go to below*/
68 if (repbody->mbo_valid & OBD_MD_FLMDSCAPA)
71 if (rc == 0 && info->mti_mdt->mdt_lut.lut_mds_capa &&
72 exp_connect_flags(info->mti_exp) & OBD_CONNECT_MDS_CAPA) {
73 struct lustre_capa *capa;
75 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
77 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
78 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
81 repbody->mbo_valid |= OBD_MD_FLMDSCAPA;
88 * Get version of object by fid.
90 * Return real version or ENOENT_VERSION if object doesn't exist
92 static void mdt_obj_version_get(struct mdt_thread_info *info,
93 struct mdt_object *o, __u64 *version)
96 if (mdt_object_exists(o) && !mdt_object_remote(o) &&
97 !fid_is_obf(mdt_object_fid(o)))
98 *version = dt_version_get(info->mti_env, mdt_obj2dt(o));
100 *version = ENOENT_VERSION;
101 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
102 PFID(mdt_object_fid(o)), *version);
106 * Check version is correct.
108 * Should be called only during replay.
110 static int mdt_version_check(struct ptlrpc_request *req,
111 __u64 version, int idx)
113 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
116 if (!exp_connect_vbr(req->rq_export))
119 LASSERT(req_is_replay(req));
120 /** VBR: version is checked always because costs nothing */
121 LASSERT(idx < PTLRPC_NUM_VERSIONS);
122 /** Sanity check for malformed buffers */
123 if (pre_ver == NULL) {
124 CERROR("No versions in request buffer\n");
125 spin_lock(&req->rq_export->exp_lock);
126 req->rq_export->exp_vbr_failed = 1;
127 spin_unlock(&req->rq_export->exp_lock);
129 } else if (pre_ver[idx] != version) {
130 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
131 pre_ver[idx], version);
132 spin_lock(&req->rq_export->exp_lock);
133 req->rq_export->exp_vbr_failed = 1;
134 spin_unlock(&req->rq_export->exp_lock);
141 * Save pre-versions in reply.
143 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
148 if (!exp_connect_vbr(req->rq_export))
151 LASSERT(!req_is_replay(req));
152 LASSERT(req->rq_repmsg != NULL);
153 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
155 reply_ver[idx] = version;
159 * Save enoent version, it is needed when it is obvious that object doesn't
160 * exist, e.g. child during create.
162 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
164 /* save version of file name for replay, it must be ENOENT here */
165 if (!req_is_replay(mdt_info_req(info))) {
166 info->mti_ver[idx] = ENOENT_VERSION;
167 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
172 * Get version from disk and save in reply buffer.
174 * Versions are saved in reply only during normal operations not replays.
176 void mdt_version_get_save(struct mdt_thread_info *info,
177 struct mdt_object *mto, int idx)
179 /* don't save versions during replay */
180 if (!req_is_replay(mdt_info_req(info))) {
181 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
182 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
187 * Get version from disk and check it, no save in reply.
189 int mdt_version_get_check(struct mdt_thread_info *info,
190 struct mdt_object *mto, int idx)
192 /* only check versions during replay */
193 if (!req_is_replay(mdt_info_req(info)))
196 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
197 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
201 * Get version from disk and check if recovery or just save.
203 int mdt_version_get_check_save(struct mdt_thread_info *info,
204 struct mdt_object *mto, int idx)
208 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
209 if (req_is_replay(mdt_info_req(info)))
210 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
213 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
218 * Lookup with version checking.
220 * This checks version of 'name'. Many reint functions uses 'name' for child not
221 * FID, therefore we need to get object by name and check its version.
223 static int mdt_lookup_version_check(struct mdt_thread_info *info,
224 struct mdt_object *p,
225 const struct lu_name *lname,
226 struct lu_fid *fid, int idx)
230 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
232 /* Check version only during replay */
233 if (!req_is_replay(mdt_info_req(info)))
236 info->mti_ver[idx] = ENOENT_VERSION;
238 struct mdt_object *child;
239 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
240 if (likely(!IS_ERR(child))) {
241 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
242 mdt_object_put(info->mti_env, child);
245 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
246 return vbrc ? vbrc : rc;
251 * mdt_remote_permission: Check whether the remote operation is permitted,
253 * Before we implement async cross-MDT updates (DNE phase 2). There are a few
256 * 1.Only sysadmin can create remote directory and striped directory and
257 * migrate directory now, unless
258 * lctl set_param mdt.*.enable_remote_dir_gid=allow_gid.
259 * 2.Remote directory can only be created on MDT0, unless
260 * lctl set_param mdt.*.enable_remote_dir = 1
261 * 3.Only new clients can access remote dir( >= 2.4) and striped dir(>= 2.6),
262 * old client will return -ENOTSUPP.
264 * XXX these check are only needed for remote synchronization, once async
265 * update is supported, these check will be removed.
267 * param[in]info: execution environment.
268 * param[in]parent: the directory of this operation.
269 * param[in]child: the child of this operation.
271 * retval = 0 remote operation is allowed.
272 * < 0 remote operation is denied.
274 static int mdt_remote_permission(struct mdt_thread_info *info,
275 struct mdt_object *parent,
276 struct mdt_object *child)
278 struct mdt_device *mdt = info->mti_mdt;
279 struct lu_ucred *uc = mdt_ucred(info);
280 struct md_op_spec *spec = &info->mti_spec;
281 struct lu_attr *attr = &info->mti_attr.ma_attr;
282 struct obd_export *exp = mdt_info_req(info)->rq_export;
284 /* Only check create remote directory, striped directory and
286 if (mdt_object_remote(parent) == 0 && mdt_object_remote(child) == 0 &&
287 !(S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
288 spec->u.sp_ea.eadatalen != 0) &&
289 info->mti_rr.rr_opcode != REINT_MIGRATE)
292 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
293 if (uc->uc_gid != mdt->mdt_enable_remote_dir_gid &&
294 mdt->mdt_enable_remote_dir_gid != -1)
298 if (mdt->mdt_enable_remote_dir == 0) {
299 struct seq_server_site *ss = mdt_seq_site(mdt);
300 struct lu_seq_range range = { 0 };
303 fld_range_set_type(&range, LU_SEQ_RANGE_MDT);
304 rc = fld_server_lookup(info->mti_env, ss->ss_server_fld,
305 fid_seq(mdt_object_fid(parent)), &range);
309 if (range.lsr_index != 0)
313 if (!mdt_is_dne_client(exp))
316 if (S_ISDIR(attr->la_mode) && spec->u.sp_ea.eadata != NULL &&
317 spec->u.sp_ea.eadatalen != 0) {
318 const struct lmv_user_md *lum = spec->u.sp_ea.eadata;
320 if (le32_to_cpu(lum->lum_stripe_count) > 1 &&
321 !mdt_is_striped_client(exp))
329 * VBR: we save three versions in reply:
330 * 0 - parent. Check that parent version is the same during replay.
331 * 1 - name. Version of 'name' if file exists with the same name or
332 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
333 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
336 static int mdt_md_create(struct mdt_thread_info *info)
338 struct mdt_device *mdt = info->mti_mdt;
339 struct mdt_object *parent;
340 struct mdt_object *child;
341 struct mdt_lock_handle *lh;
342 struct mdt_body *repbody;
343 struct md_attr *ma = &info->mti_attr;
344 struct mdt_reint_record *rr = &info->mti_rr;
348 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create ("DNAME"->"DFID") "
350 PNAME(&rr->rr_name), PFID(rr->rr_fid2), PFID(rr->rr_fid1));
352 if (!fid_is_md_operative(rr->rr_fid1))
355 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
357 parent = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
359 RETURN(PTR_ERR(parent));
361 if (!mdt_object_exists(parent))
362 GOTO(put_parent, rc = -ENOENT);
364 lh = &info->mti_lh[MDT_LH_PARENT];
365 mdt_lock_pdo_init(lh, LCK_PW, &rr->rr_name);
366 rc = mdt_object_lock(info, parent, lh, MDS_INODELOCK_UPDATE,
369 GOTO(put_parent, rc);
371 if (!mdt_object_remote(parent)) {
372 rc = mdt_version_get_check_save(info, parent, 0);
374 GOTO(unlock_parent, rc);
378 * Check child name version during replay.
379 * During create replay a file may exist with same name.
381 rc = mdt_lookup_version_check(info, parent, &rr->rr_name,
382 &info->mti_tmp_fid1, 1);
384 GOTO(unlock_parent, rc = -EEXIST);
386 /* -ENOENT is expected here */
388 GOTO(unlock_parent, rc);
390 /* save version of file name for replay, it must be ENOENT here */
391 mdt_enoent_version_save(info, 1);
393 child = mdt_object_new(info->mti_env, mdt, rr->rr_fid2);
394 if (likely(!IS_ERR(child))) {
395 struct md_object *next = mdt_object_child(parent);
397 rc = mdt_remote_permission(info, parent, child);
399 GOTO(out_put_child, rc);
401 ma->ma_need = MA_INODE;
403 /* capa for cross-ref will be stored here */
404 ma->ma_capa = req_capsule_server_get(info->mti_pill,
406 LASSERT(ma->ma_capa);
408 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
409 OBD_FAIL_MDS_REINT_CREATE_WRITE);
411 /* Version of child will be updated on disk. */
412 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(child));
413 rc = mdt_version_get_check_save(info, child, 2);
415 GOTO(out_put_child, rc);
417 /* Let lower layer know current lock mode. */
418 info->mti_spec.sp_cr_mode =
419 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
422 * Do not perform lookup sanity check. We know that name does
425 info->mti_spec.sp_cr_lookup = 0;
426 info->mti_spec.sp_feat = &dt_directory_features;
428 rc = mdo_create(info->mti_env, next, &rr->rr_name,
429 mdt_object_child(child), &info->mti_spec, ma);
431 rc = mdt_attr_get_complex(info, child, ma);
434 /* Return fid & attr to client. */
435 if (ma->ma_valid & MA_INODE)
436 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
437 mdt_object_fid(child));
440 mdt_create_pack_capa(info, rc, child, repbody);
441 mdt_object_put(info->mti_env, child);
444 mdt_create_pack_capa(info, rc, NULL, repbody);
447 mdt_object_unlock(info, parent, lh, rc);
449 mdt_object_put(info->mti_env, parent);
453 static int mdt_unlock_slaves(struct mdt_thread_info *mti,
454 struct mdt_object *obj, __u64 ibits,
455 struct mdt_lock_handle *s0_lh,
456 struct mdt_object *s0_obj,
457 struct ldlm_enqueue_info *einfo)
459 ldlm_policy_data_t *policy = &mti->mti_policy;
463 if (!S_ISDIR(obj->mot_header.loh_attr))
466 /* Unlock stripe 0 */
467 if (s0_lh != NULL && lustre_handle_is_used(&s0_lh->mlh_reg_lh)) {
468 LASSERT(s0_obj != NULL);
469 mdt_object_unlock_put(mti, s0_obj, s0_lh, 1);
472 memset(policy, 0, sizeof(*policy));
473 policy->l_inodebits.bits = ibits;
475 rc = mo_object_unlock(mti->mti_env, mdt_object_child(obj), einfo,
481 * Lock slave stripes if necessary, the lock handles of slave stripes
482 * will be stored in einfo->ei_cbdata.
484 static int mdt_lock_slaves(struct mdt_thread_info *mti, struct mdt_object *obj,
485 ldlm_mode_t mode, __u64 ibits,
486 struct mdt_lock_handle *s0_lh,
487 struct mdt_object **s0_objp,
488 struct ldlm_enqueue_info *einfo)
490 ldlm_policy_data_t *policy = &mti->mti_policy;
491 struct lu_buf *buf = &mti->mti_buf;
492 struct lmv_mds_md_v1 *lmv;
493 struct lu_fid *fid = &mti->mti_tmp_fid1;
497 if (!S_ISDIR(obj->mot_header.loh_attr))
500 buf->lb_buf = mti->mti_xattr_buf;
501 buf->lb_len = sizeof(mti->mti_xattr_buf);
502 rc = mo_xattr_get(mti->mti_env, mdt_object_child(obj), buf,
505 rc = mdt_big_xattr_get(mti, obj, XATTR_NAME_LMV);
507 buf->lb_buf = mti->mti_big_lmm;
508 buf->lb_len = mti->mti_big_lmmsize;
512 if (rc == -ENODATA || rc == -ENOENT)
519 if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
522 /* Sigh, 0_stripe and master object are different
523 * object, though they are in the same MDT, to avoid
524 * adding osd_object_lock here, so we will enqueue the
525 * stripe0 lock in MDT0 for now */
526 fid_le_to_cpu(fid, &lmv->lmv_stripe_fids[0]);
527 *s0_objp = mdt_object_find_lock(mti, fid, s0_lh, ibits);
528 if (IS_ERR(*s0_objp))
529 RETURN(PTR_ERR(*s0_objp));
531 memset(einfo, 0, sizeof(*einfo));
532 einfo->ei_type = LDLM_IBITS;
533 einfo->ei_mode = mode;
534 einfo->ei_cb_bl = mdt_remote_blocking_ast;
535 einfo->ei_cb_cp = ldlm_completion_ast;
536 einfo->ei_enq_slave = 1;
537 memset(policy, 0, sizeof(*policy));
538 policy->l_inodebits.bits = ibits;
540 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), NULL, einfo,
545 static int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
548 struct mdt_lock_handle *lh;
549 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
550 __u64 lockpart = MDS_INODELOCK_UPDATE;
551 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
552 struct mdt_lock_handle *s0_lh;
553 struct mdt_object *s0_obj = NULL;
557 lh = &info->mti_lh[MDT_LH_PARENT];
558 mdt_lock_reg_init(lh, LCK_PW);
560 /* Even though the new MDT will grant PERM lock to the old
561 * client, but the old client will almost ignore that during
562 * So it needs to revoke both LOOKUP and PERM lock here, so
563 * both new and old client can cancel the dcache */
564 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
565 lockpart |= MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
567 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
571 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
572 mdt_lock_reg_init(s0_lh, LCK_PW);
573 rc = mdt_lock_slaves(info, mo, LCK_PW, lockpart, s0_lh, &s0_obj, einfo);
575 GOTO(out_unlock, rc);
577 /* all attrs are packed into mti_attr in unpack_setattr */
578 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
579 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
581 /* This is only for set ctime when rename's source is on remote MDS. */
582 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
583 ma->ma_attr_flags |= MDS_VTX_BYPASS;
585 /* VBR: update version if attr changed are important for recovery */
587 /* update on-disk version of changed object */
588 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mo));
589 rc = mdt_version_get_check_save(info, mo, 0);
591 GOTO(out_unlock, rc);
594 /* Ensure constant striping during chown(). See LU-2789. */
595 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
596 mutex_lock(&mo->mot_lov_mutex);
598 /* all attrs are packed into mti_attr in unpack_setattr */
599 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
601 if (ma->ma_attr.la_valid & (LA_UID|LA_GID))
602 mutex_unlock(&mo->mot_lov_mutex);
605 GOTO(out_unlock, rc);
609 mdt_unlock_slaves(info, mo, lockpart, s0_lh, s0_obj, einfo);
610 mdt_object_unlock(info, mo, lh, rc);
615 * Check HSM flags and add HS_DIRTY flag if relevant.
617 * A file could be set dirty only if it has a copy in the backend (HS_EXISTS)
618 * and is not RELEASED.
620 int mdt_add_dirty_flag(struct mdt_thread_info *info, struct mdt_object *mo,
626 /* If the file was modified, add the dirty flag */
627 ma->ma_need = MA_HSM;
628 rc = mdt_attr_get_complex(info, mo, ma);
630 CERROR("file attribute read error for "DFID": %d.\n",
631 PFID(mdt_object_fid(mo)), rc);
635 /* If an up2date copy exists in the backend, add dirty flag */
636 if ((ma->ma_valid & MA_HSM) && (ma->ma_hsm.mh_flags & HS_EXISTS)
637 && !(ma->ma_hsm.mh_flags & (HS_DIRTY|HS_RELEASED))) {
638 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_CHILD];
640 ma->ma_hsm.mh_flags |= HS_DIRTY;
642 mdt_lock_reg_init(lh, LCK_PW);
643 rc = mdt_object_lock(info, mo, lh, MDS_INODELOCK_XATTR,
648 rc = mdt_hsm_attr_set(info, mo, &ma->ma_hsm);
650 CERROR("file attribute change error for "DFID": %d\n",
651 PFID(mdt_object_fid(mo)), rc);
652 mdt_object_unlock(info, mo, lh, rc);
658 static int mdt_reint_setattr(struct mdt_thread_info *info,
659 struct mdt_lock_handle *lhc)
661 struct md_attr *ma = &info->mti_attr;
662 struct mdt_reint_record *rr = &info->mti_rr;
663 struct ptlrpc_request *req = mdt_info_req(info);
664 struct mdt_object *mo;
665 struct mdt_body *repbody;
669 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
670 (unsigned int)ma->ma_attr.la_valid);
672 if (info->mti_dlm_req)
673 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
675 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
676 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
678 GOTO(out, rc = PTR_ERR(mo));
680 if (!mdt_object_exists(mo))
681 GOTO(out_put, rc = -ENOENT);
683 if (mdt_object_remote(mo))
684 GOTO(out_put, rc = -EREMOTE);
686 if ((ma->ma_attr.la_valid & LA_SIZE) ||
687 (rr->rr_flags & MRF_OPEN_TRUNC)) {
688 /* Check write access for the O_TRUNC case */
689 if (mdt_write_read(mo) < 0)
690 GOTO(out_put, rc = -ETXTBSY);
693 if ((ma->ma_valid & MA_INODE) && ma->ma_attr.la_valid) {
694 if (ma->ma_valid & MA_LOV)
695 GOTO(out_put, rc = -EPROTO);
697 rc = mdt_attr_set(info, mo, ma);
700 } else if ((ma->ma_valid & MA_LOV) && (ma->ma_valid & MA_INODE)) {
701 struct lu_buf *buf = &info->mti_buf;
703 if (ma->ma_attr.la_valid != 0)
704 GOTO(out_put, rc = -EPROTO);
706 buf->lb_buf = ma->ma_lmm;
707 buf->lb_len = ma->ma_lmm_size;
708 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
709 buf, XATTR_NAME_LOV, 0);
712 } else if ((ma->ma_valid & MA_LMV) && (ma->ma_valid & MA_INODE)) {
713 struct lu_buf *buf = &info->mti_buf;
714 struct mdt_lock_handle *lh;
716 if (ma->ma_attr.la_valid != 0)
717 GOTO(out_put, rc = -EPROTO);
719 lh = &info->mti_lh[MDT_LH_PARENT];
720 mdt_lock_reg_init(lh, LCK_PW);
722 rc = mdt_object_lock(info, mo, lh,
728 buf->lb_buf = ma->ma_lmv;
729 buf->lb_len = ma->ma_lmv_size;
730 rc = mo_xattr_set(info->mti_env, mdt_object_child(mo),
731 buf, XATTR_NAME_DEFAULT_LMV, 0);
733 mdt_object_unlock(info, mo, lh, rc);
737 GOTO(out_put, rc = -EPROTO);
740 /* If file data is modified, add the dirty flag */
741 if (ma->ma_attr_flags & MDS_DATA_MODIFIED)
742 rc = mdt_add_dirty_flag(info, mo, ma);
744 ma->ma_need = MA_INODE;
746 rc = mdt_attr_get_complex(info, mo, ma);
750 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
752 if (info->mti_mdt->mdt_lut.lut_oss_capa &&
753 exp_connect_flags(info->mti_exp) & OBD_CONNECT_OSS_CAPA &&
754 S_ISREG(lu_object_attr(&mo->mot_obj)) &&
755 (ma->ma_attr.la_valid & LA_SIZE)) {
756 struct lustre_capa *capa;
758 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
760 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
761 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
764 repbody->mbo_valid |= OBD_MD_FLOSSCAPA;
769 mdt_object_put(info->mti_env, mo);
772 mdt_counter_incr(req, LPROC_MDT_SETATTR);
774 mdt_client_compatibility(info);
775 rc2 = mdt_fix_reply(info);
781 static int mdt_reint_create(struct mdt_thread_info *info,
782 struct mdt_lock_handle *lhc)
784 struct ptlrpc_request *req = mdt_info_req(info);
788 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
789 RETURN(err_serious(-ESTALE));
791 if (info->mti_dlm_req)
792 ldlm_request_cancel(mdt_info_req(info),
793 info->mti_dlm_req, 0, LATF_SKIP);
795 if (!lu_name_is_valid(&info->mti_rr.rr_name))
798 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
800 mdt_counter_incr(req, LPROC_MDT_MKDIR);
808 /* Special file should stay on the same node as parent. */
809 mdt_counter_incr(req, LPROC_MDT_MKNOD);
812 CERROR("%s: Unsupported mode %o\n",
813 mdt_obd_name(info->mti_mdt),
814 info->mti_attr.ma_attr.la_mode);
815 RETURN(err_serious(-EOPNOTSUPP));
818 rc = mdt_md_create(info);
823 * VBR: save parent version in reply and child version getting by its name.
824 * Version of child is getting and checking during its lookup. If
826 static int mdt_reint_unlink(struct mdt_thread_info *info,
827 struct mdt_lock_handle *lhc)
829 struct mdt_reint_record *rr = &info->mti_rr;
830 struct ptlrpc_request *req = mdt_info_req(info);
831 struct md_attr *ma = &info->mti_attr;
832 struct lu_fid *child_fid = &info->mti_tmp_fid1;
833 struct mdt_object *mp;
834 struct mdt_object *mc;
835 struct mdt_lock_handle *parent_lh;
836 struct mdt_lock_handle *child_lh;
837 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
838 struct mdt_lock_handle *s0_lh = NULL;
839 struct mdt_object *s0_obj = NULL;
844 DEBUG_REQ(D_INODE, req, "unlink "DFID"/"DNAME"", PFID(rr->rr_fid1),
845 PNAME(&rr->rr_name));
847 if (info->mti_dlm_req)
848 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
850 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
851 RETURN(err_serious(-ENOENT));
853 if (!fid_is_md_operative(rr->rr_fid1))
857 * step 1: Found the parent.
859 mp = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
865 parent_lh = &info->mti_lh[MDT_LH_PARENT];
866 mdt_lock_pdo_init(parent_lh, LCK_PW, &rr->rr_name);
867 rc = mdt_object_lock(info, mp, parent_lh, MDS_INODELOCK_UPDATE,
870 GOTO(put_parent, rc);
872 if (!mdt_object_remote(mp)) {
873 rc = mdt_version_get_check_save(info, mp, 0);
875 GOTO(unlock_parent, rc);
878 /* step 2: find & lock the child */
879 /* lookup child object along with version checking */
881 rc = mdt_lookup_version_check(info, mp, &rr->rr_name, child_fid, 1);
883 /* Name might not be able to find during resend of
884 * remote unlink, considering following case.
885 * dir_A is a remote directory, the name entry of
886 * dir_A is on MDT0, the directory is on MDT1,
888 * 1. client sends unlink req to MDT1.
889 * 2. MDT1 sends name delete update to MDT0.
890 * 3. name entry is being deleted in MDT0 synchronously.
891 * 4. MDT1 is restarted.
892 * 5. client resends unlink req to MDT1. So it can not
893 * find the name entry on MDT0 anymore.
894 * In this case, MDT1 only needs to destory the local
897 if (mdt_object_remote(mp) && rc == -ENOENT &&
898 !fid_is_zero(rr->rr_fid2) &&
899 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
901 *child_fid = *rr->rr_fid2;
903 GOTO(unlock_parent, rc);
907 if (!fid_is_md_operative(child_fid))
908 GOTO(unlock_parent, rc = -EPERM);
910 /* We will lock the child regardless it is local or remote. No harm. */
911 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
913 GOTO(unlock_parent, rc = PTR_ERR(mc));
915 child_lh = &info->mti_lh[MDT_LH_CHILD];
916 mdt_lock_reg_init(child_lh, LCK_EX);
917 if (mdt_object_remote(mc)) {
918 struct mdt_body *repbody;
920 if (!fid_is_zero(rr->rr_fid2)) {
921 CDEBUG(D_INFO, "%s: name "DNAME" cannot find "DFID"\n",
922 mdt_obd_name(info->mti_mdt),
923 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
924 GOTO(put_child, rc = -ENOENT);
926 CDEBUG(D_INFO, "%s: name "DNAME": "DFID" is on another MDT\n",
927 mdt_obd_name(info->mti_mdt),
928 PNAME(&rr->rr_name), PFID(mdt_object_fid(mc)));
930 if (!mdt_is_dne_client(req->rq_export))
931 /* Return -ENOTSUPP for old client */
932 GOTO(put_child, rc = -ENOTSUPP);
934 if (info->mti_spec.sp_rm_entry) {
935 struct lu_ucred *uc = mdt_ucred(info);
937 if (!md_capable(uc, CFS_CAP_SYS_ADMIN)) {
938 CERROR("%s: unlink remote entry is only "
939 "permitted for administrator: rc = %d\n",
940 mdt_obd_name(info->mti_mdt),
942 GOTO(put_child, rc = -EPERM);
945 ma->ma_need = MA_INODE;
947 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
948 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
949 NULL, &rr->rr_name, ma, no_name);
952 /* Revoke the LOOKUP lock of the remote object granted by
953 * this MDT. Since the unlink will happen on another MDT,
954 * it will release the LOOKUP lock right away. Then What
955 * would happen if another client try to grab the LOOKUP
956 * lock at the same time with unlink XXX */
957 mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_LOOKUP,
959 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
960 LASSERT(repbody != NULL);
961 repbody->mbo_fid1 = *mdt_object_fid(mc);
962 repbody->mbo_valid |= (OBD_MD_FLID | OBD_MD_MDS);
963 GOTO(unlock_child, rc = -EREMOTE);
964 } else if (info->mti_spec.sp_rm_entry) {
966 CDEBUG(D_INFO, "%s: no rm_entry on local dir '"DNAME"': "
968 mdt_obd_name(info->mti_mdt), PNAME(&rr->rr_name), rc);
972 /* We used to acquire MDS_INODELOCK_FULL here but we can't do
973 * this now because a running HSM restore on the child (unlink
974 * victim) will hold the layout lock. See LU-4002. */
975 rc = mdt_object_lock(info, mc, child_lh,
976 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
981 * Now we can only make sure we need MA_INODE, in mdd layer, will check
982 * whether need MA_LOV and MA_COOKIE.
984 ma->ma_need = MA_INODE;
987 s0_lh = &info->mti_lh[MDT_LH_LOCAL];
988 mdt_lock_reg_init(s0_lh, LCK_EX);
989 rc = mdt_lock_slaves(info, mc, LCK_EX, MDS_INODELOCK_UPDATE, s0_lh,
992 GOTO(unlock_child, rc);
994 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
995 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
996 /* save version when object is locked */
997 mdt_version_get_save(info, mc, 1);
999 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
1001 mutex_lock(&mc->mot_lov_mutex);
1003 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
1004 mdt_object_child(mc), &rr->rr_name, ma, no_name);
1006 mutex_unlock(&mc->mot_lov_mutex);
1008 if (rc == 0 && !lu_object_is_dying(&mc->mot_header))
1009 rc = mdt_attr_get_complex(info, mc, ma);
1011 mdt_handle_last_unlink(info, mc, ma);
1013 if (ma->ma_valid & MA_INODE) {
1014 switch (ma->ma_attr.la_mode & S_IFMT) {
1016 mdt_counter_incr(req, LPROC_MDT_RMDIR);
1024 mdt_counter_incr(req, LPROC_MDT_UNLINK);
1027 LASSERTF(0, "bad file type %o unlinking\n",
1028 ma->ma_attr.la_mode);
1035 mdt_unlock_slaves(info, mc, MDS_INODELOCK_UPDATE, s0_lh, s0_obj, einfo);
1036 mdt_object_unlock(info, mc, child_lh, rc);
1038 mdt_object_put(info->mti_env, mc);
1040 mdt_object_unlock(info, mp, parent_lh, rc);
1042 mdt_object_put(info->mti_env, mp);
1048 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
1051 static int mdt_reint_link(struct mdt_thread_info *info,
1052 struct mdt_lock_handle *lhc)
1054 struct mdt_reint_record *rr = &info->mti_rr;
1055 struct ptlrpc_request *req = mdt_info_req(info);
1056 struct md_attr *ma = &info->mti_attr;
1057 struct mdt_object *ms;
1058 struct mdt_object *mp;
1059 struct mdt_lock_handle *lhs;
1060 struct mdt_lock_handle *lhp;
1064 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/"DNAME,
1065 PFID(rr->rr_fid1), PFID(rr->rr_fid2), PNAME(&rr->rr_name));
1067 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1068 RETURN(err_serious(-ENOENT));
1070 if (info->mti_dlm_req)
1071 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1073 /* Invalid case so return error immediately instead of
1075 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
1078 if (!fid_is_md_operative(rr->rr_fid1) ||
1079 !fid_is_md_operative(rr->rr_fid2))
1082 /* step 1: find & lock the target parent dir */
1083 lhp = &info->mti_lh[MDT_LH_PARENT];
1084 mdt_lock_pdo_init(lhp, LCK_PW, &rr->rr_name);
1085 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
1086 MDS_INODELOCK_UPDATE);
1088 RETURN(PTR_ERR(mp));
1090 rc = mdt_version_get_check_save(info, mp, 0);
1092 GOTO(out_unlock_parent, rc);
1094 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1096 /* step 2: find & lock the source */
1097 lhs = &info->mti_lh[MDT_LH_CHILD];
1098 mdt_lock_reg_init(lhs, LCK_EX);
1100 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1102 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
1104 if (!mdt_object_exists(ms)) {
1105 mdt_object_put(info->mti_env, ms);
1106 CDEBUG(D_INFO, "%s: "DFID" does not exist.\n",
1107 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1));
1108 GOTO(out_unlock_parent, rc = -ENOENT);
1111 if (mdt_object_remote(ms)) {
1112 mdt_object_put(info->mti_env, ms);
1113 CERROR("%s: source inode "DFID" on remote MDT from "DFID"\n",
1114 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1116 GOTO(out_unlock_parent, rc = -EXDEV);
1119 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE |
1120 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
1122 mdt_object_put(info->mti_env, ms);
1123 GOTO(out_unlock_parent, rc);
1126 /* step 3: link it */
1127 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1128 OBD_FAIL_MDS_REINT_LINK_WRITE);
1130 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(ms));
1131 rc = mdt_version_get_check_save(info, ms, 1);
1133 GOTO(out_unlock_child, rc);
1135 /** check target version by name during replay */
1136 rc = mdt_lookup_version_check(info, mp, &rr->rr_name,
1137 &info->mti_tmp_fid1, 2);
1138 if (rc != 0 && rc != -ENOENT)
1139 GOTO(out_unlock_child, rc);
1140 /* save version of file name for replay, it must be ENOENT here */
1141 if (!req_is_replay(mdt_info_req(info))) {
1142 if (rc != -ENOENT) {
1143 CDEBUG(D_INFO, "link target "DNAME" existed!\n",
1144 PNAME(&rr->rr_name));
1145 GOTO(out_unlock_child, rc = -EEXIST);
1147 info->mti_ver[2] = ENOENT_VERSION;
1148 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
1151 rc = mdo_link(info->mti_env, mdt_object_child(mp),
1152 mdt_object_child(ms), &rr->rr_name, ma);
1155 mdt_counter_incr(req, LPROC_MDT_LINK);
1159 mdt_object_unlock_put(info, ms, lhs, rc);
1161 mdt_object_unlock_put(info, mp, lhp, rc);
1165 * lock the part of the directory according to the hash of the name
1166 * (lh->mlh_pdo_hash) in parallel directory lock.
1168 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
1169 struct mdt_lock_handle *lh,
1170 struct mdt_object *obj, __u64 ibits)
1172 struct ldlm_res_id *res = &info->mti_res_id;
1173 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1174 ldlm_policy_data_t *policy = &info->mti_policy;
1178 * Finish res_id initializing by name hash marking part of
1179 * directory which is taking modification.
1181 LASSERT(lh->mlh_pdo_hash != 0);
1182 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res);
1183 memset(policy, 0, sizeof(*policy));
1184 policy->l_inodebits.bits = ibits;
1186 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
1187 * going to be sent to client. If it is - mdt_intent_policy() path will
1188 * fix it up and turn FL_LOCAL flag off.
1190 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
1191 res, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
1192 &info->mti_exp->exp_handle.h_cookie);
1196 enum mdt_rename_lock {
1202 * Get BFL lock for rename or migrate process, right now, it does not support
1203 * cross-MDT rename, so we only need global rename lock during migration.
1205 static int mdt_rename_lock(struct mdt_thread_info *info,
1206 struct lustre_handle *lh,
1207 enum mdt_rename_lock rename_lock)
1209 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
1210 ldlm_policy_data_t *policy = &info->mti_policy;
1211 struct ldlm_res_id *res_id = &info->mti_res_id;
1216 /* XXX only do global rename lock for migration */
1217 if (mdt_seq_site(info->mti_mdt)->ss_node_id != 0 &&
1218 rename_lock == MRL_MIGRATE) {
1219 struct lu_fid *fid = &info->mti_tmp_fid1;
1220 struct mdt_object *obj;
1222 /* XXX, right now, it has to use object API to
1223 * enqueue lock cross MDT, so it will enqueue
1224 * rename lock(with LUSTRE_BFL_FID) by root object */
1226 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1228 RETURN(PTR_ERR(obj));
1230 LASSERT(mdt_object_remote(obj));
1231 rc = mdt_remote_object_lock(info, obj,
1232 &LUSTRE_BFL_FID, lh,
1234 MDS_INODELOCK_UPDATE);
1235 mdt_object_put(info->mti_env, obj);
1237 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
1238 memset(policy, 0, sizeof *policy);
1239 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
1240 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1241 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1242 LCK_EX, &flags, ldlm_blocking_ast,
1243 ldlm_completion_ast, NULL, NULL, 0,
1245 &info->mti_exp->exp_handle.h_cookie,
1252 static void mdt_rename_unlock(struct lustre_handle *lh)
1255 LASSERT(lustre_handle_is_used(lh));
1256 /* Cancel the single rename lock right away */
1257 ldlm_lock_decref_and_cancel(lh, LCK_EX);
1262 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1263 * target. Source should not be ancestor of target dir. May be other rename
1264 * checks can be moved here later.
1266 static int mdt_is_subdir(struct mdt_thread_info *info,
1267 struct mdt_object *dir,
1268 const struct lu_fid *fid)
1270 struct lu_fid dir_fid = dir->mot_header.loh_fid;
1274 /* If the source and target are in the same directory, they can not
1275 * be parent/child relationship, so subdir check is not needed */
1276 if (lu_fid_eq(&dir_fid, fid))
1279 if (!mdt_object_exists(dir))
1282 rc = mdo_is_subdir(info->mti_env, mdt_object_child(dir),
1285 CERROR("%s: failed subdir check in "DFID" for "DFID
1286 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1287 PFID(&dir_fid), PFID(fid), rc);
1288 /* Return EINVAL only if a parent is the @fid */
1292 /* check the found fid */
1293 if (lu_fid_eq(&dir_fid, fid))
1300 /* Update object linkEA */
1301 struct mdt_lock_list {
1302 struct mdt_object *mll_obj;
1303 struct mdt_lock_handle mll_lh;
1304 struct list_head mll_list;
1307 static void mdt_unlock_list(struct mdt_thread_info *info,
1308 struct list_head *list, int rc)
1310 struct mdt_lock_list *mll;
1311 struct mdt_lock_list *mll2;
1313 list_for_each_entry_safe(mll, mll2, list, mll_list) {
1314 mdt_object_unlock_put(info, mll->mll_obj, &mll->mll_lh, rc);
1315 list_del(&mll->mll_list);
1320 static int mdt_lock_objects_in_linkea(struct mdt_thread_info *info,
1321 struct mdt_object *obj,
1322 struct mdt_object *pobj,
1323 struct list_head *lock_list)
1325 struct lu_buf *buf = &info->mti_big_buf;
1326 struct linkea_data ldata = { NULL };
1331 if (S_ISDIR(lu_object_attr(&obj->mot_obj)))
1334 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1335 if (buf->lb_buf == NULL)
1339 rc = mdt_links_read(info, obj, &ldata);
1341 if (rc == -ENOENT || rc == -ENODATA)
1346 LASSERT(ldata.ld_leh != NULL);
1347 ldata.ld_lee = (struct link_ea_entry *)(ldata.ld_leh + 1);
1348 for (count = 0; count < ldata.ld_leh->leh_reccount; count++) {
1349 struct mdt_device *mdt = info->mti_mdt;
1350 struct mdt_object *mdt_pobj;
1351 struct mdt_lock_list *mll;
1352 struct lu_name name;
1355 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
1357 ldata.ld_lee = (struct link_ea_entry *)((char *)ldata.ld_lee +
1359 mdt_pobj = mdt_object_find(info->mti_env, mdt, &fid);
1360 if (IS_ERR(mdt_pobj)) {
1361 CWARN("%s: cannot find obj "DFID": rc = %ld\n",
1362 mdt_obd_name(mdt), PFID(&fid), PTR_ERR(mdt_pobj));
1366 if (!mdt_object_exists(mdt_pobj)) {
1367 CDEBUG(D_INFO, "%s: obj "DFID" does not exist\n",
1368 mdt_obd_name(mdt), PFID(&fid));
1369 mdt_object_put(info->mti_env, mdt_pobj);
1373 if (mdt_pobj == pobj) {
1374 CDEBUG(D_INFO, "%s: skipping parent obj "DFID"\n",
1375 mdt_obd_name(mdt), PFID(&fid));
1376 mdt_object_put(info->mti_env, mdt_pobj);
1382 mdt_object_put(info->mti_env, mdt_pobj);
1383 GOTO(out, rc = -ENOMEM);
1386 mdt_lock_pdo_init(&mll->mll_lh, LCK_PW, &name);
1387 rc = mdt_object_lock(info, mdt_pobj, &mll->mll_lh,
1388 MDS_INODELOCK_UPDATE,
1391 CERROR("%s: cannot lock "DFID": rc =%d\n",
1392 mdt_obd_name(mdt), PFID(&fid), rc);
1393 mdt_object_put(info->mti_env, mdt_pobj);
1398 INIT_LIST_HEAD(&mll->mll_list);
1399 mll->mll_obj = mdt_pobj;
1400 list_add_tail(&mll->mll_list, lock_list);
1404 mdt_unlock_list(info, lock_list, rc);
1408 /* migrate files from one MDT to another MDT */
1409 static int mdt_reint_migrate_internal(struct mdt_thread_info *info,
1410 struct mdt_lock_handle *lhc)
1412 struct mdt_reint_record *rr = &info->mti_rr;
1413 struct md_attr *ma = &info->mti_attr;
1414 struct mdt_object *msrcdir;
1415 struct mdt_object *mold;
1416 struct mdt_object *mnew = NULL;
1417 struct mdt_lock_handle *lh_dirp;
1418 struct mdt_lock_handle *lh_childp;
1419 struct mdt_lock_handle *lh_tgtp = NULL;
1420 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1421 struct list_head lock_list;
1425 CDEBUG(D_INODE, "migrate "DFID"/"DNAME" to "DFID"\n", PFID(rr->rr_fid1),
1426 PNAME(&rr->rr_name), PFID(rr->rr_fid2));
1428 /* 1: lock the source dir. */
1429 msrcdir = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
1430 if (IS_ERR(msrcdir)) {
1431 CERROR("%s: cannot find source dir "DFID" : rc = %d\n",
1432 mdt_obd_name(info->mti_mdt), PFID(rr->rr_fid1),
1433 (int)PTR_ERR(msrcdir));
1434 RETURN(PTR_ERR(msrcdir));
1437 lh_dirp = &info->mti_lh[MDT_LH_PARENT];
1438 mdt_lock_pdo_init(lh_dirp, LCK_PW, &rr->rr_name);
1439 rc = mdt_object_lock(info, msrcdir, lh_dirp,
1440 MDS_INODELOCK_UPDATE,
1443 GOTO(out_put_parent, rc);
1445 if (!mdt_object_remote(msrcdir)) {
1446 rc = mdt_version_get_check_save(info, msrcdir, 0);
1448 GOTO(out_unlock_parent, rc);
1451 /* 2: sanity check and find the object to be migrated. */
1453 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1455 GOTO(out_unlock_parent, rc);
1457 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1458 GOTO(out_unlock_parent, rc = -EINVAL);
1460 if (!fid_is_md_operative(old_fid))
1461 GOTO(out_unlock_parent, rc = -EPERM);
1463 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1465 GOTO(out_unlock_parent, rc = PTR_ERR(mold));
1467 if (mdt_object_remote(mold)) {
1468 CERROR("%s: source "DFID" is on the remote MDT\n",
1469 mdt_obd_name(info->mti_mdt), PFID(old_fid));
1470 GOTO(out_put_child, rc = -EREMOTE);
1473 if (S_ISREG(lu_object_attr(&mold->mot_obj)) &&
1474 !mdt_object_remote(msrcdir)) {
1475 CERROR("%s: parent "DFID" is still on the same"
1476 " MDT, which should be migrated first:"
1477 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1478 PFID(mdt_object_fid(msrcdir)), -EPERM);
1479 GOTO(out_put_child, rc = -EPERM);
1482 rc = mdt_remote_permission(info, msrcdir, mold);
1484 GOTO(out_put_child, rc);
1486 /* 3: iterate the linkea of the object and lock all of the objects */
1487 INIT_LIST_HEAD(&lock_list);
1488 rc = mdt_lock_objects_in_linkea(info, mold, msrcdir, &lock_list);
1490 GOTO(out_put_child, rc);
1492 /* 4: lock of the object migrated object */
1493 lh_childp = &info->mti_lh[MDT_LH_OLD];
1494 mdt_lock_reg_init(lh_childp, LCK_EX);
1495 rc = mdt_object_lock(info, mold, lh_childp,
1496 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1497 MDS_INODELOCK_LAYOUT, MDT_CROSS_LOCK);
1499 GOTO(out_unlock_list, rc);
1501 ma->ma_need = MA_LMV;
1503 ma->ma_lmv = (union lmv_mds_md *)info->mti_xattr_buf;
1504 ma->ma_lmv_size = sizeof(info->mti_xattr_buf);
1505 rc = mdt_stripe_get(info, mold, ma, XATTR_NAME_LMV);
1507 GOTO(out_unlock_list, rc);
1509 if ((ma->ma_valid & MA_LMV)) {
1510 struct lmv_mds_md_v1 *lmm1;
1512 lmv_le_to_cpu(ma->ma_lmv, ma->ma_lmv);
1513 lmm1 = &ma->ma_lmv->lmv_md_v1;
1514 if (!(lmm1->lmv_hash_type & LMV_HASH_FLAG_MIGRATION)) {
1515 CERROR("%s: can not migrate striped dir "DFID
1516 ": rc = %d\n", mdt_obd_name(info->mti_mdt),
1517 PFID(mdt_object_fid(mold)), -EPERM);
1518 GOTO(out_unlock_child, rc = -EPERM);
1521 if (!fid_is_sane(&lmm1->lmv_stripe_fids[1]))
1522 GOTO(out_unlock_child, rc = -EINVAL);
1524 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1525 &lmm1->lmv_stripe_fids[1]);
1527 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1529 if (!mdt_object_remote(mnew)) {
1530 CERROR("%s: "DFID" being migrated is on this MDT:"
1531 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1532 PFID(rr->rr_fid2), -EPERM);
1533 GOTO(out_put_new, rc = -EPERM);
1536 lh_tgtp = &info->mti_lh[MDT_LH_CHILD];
1537 mdt_lock_reg_init(lh_tgtp, LCK_EX);
1538 rc = mdt_remote_object_lock(info, mnew,
1539 mdt_object_fid(mnew),
1540 &lh_tgtp->mlh_rreg_lh,
1541 lh_tgtp->mlh_rreg_mode,
1542 MDS_INODELOCK_UPDATE);
1545 GOTO(out_put_new, rc);
1548 mnew = mdt_object_find(info->mti_env, info->mti_mdt,
1551 GOTO(out_unlock_child, rc = PTR_ERR(mnew));
1552 if (!mdt_object_remote(mnew)) {
1553 CERROR("%s: Migration "DFID" is on this MDT:"
1554 " rc = %d\n", mdt_obd_name(info->mti_mdt),
1555 PFID(rr->rr_fid2), -EXDEV);
1556 GOTO(out_put_new, rc = -EXDEV);
1561 mdt_reint_init_ma(info, ma);
1563 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1564 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1566 rc = mdo_migrate(info->mti_env, mdt_object_child(msrcdir),
1567 mdt_object_child(mold), &rr->rr_name,
1568 mdt_object_child(mnew), ma);
1570 GOTO(out_unlock_new, rc);
1572 if (lh_tgtp != NULL)
1573 mdt_object_unlock(info, mnew, lh_tgtp, rc);
1576 mdt_object_put(info->mti_env, mnew);
1578 mdt_object_unlock(info, mold, lh_childp, rc);
1580 mdt_unlock_list(info, &lock_list, rc);
1582 mdt_object_put(info->mti_env, mold);
1584 mdt_object_unlock(info, msrcdir, lh_dirp, rc);
1586 mdt_object_put(info->mti_env, msrcdir);
1591 static struct mdt_object *mdt_object_find_check(struct mdt_thread_info *info,
1592 const struct lu_fid *fid,
1595 struct mdt_object *dir;
1599 dir = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1603 /* check early, the real version will be saved after locking */
1604 rc = mdt_version_get_check(info, dir, idx);
1610 mdt_object_put(info->mti_env, dir);
1614 static int mdt_object_lock_save(struct mdt_thread_info *info,
1615 struct mdt_object *dir,
1616 struct mdt_lock_handle *lh,
1621 /* we lock the target dir if it is local */
1622 rc = mdt_object_lock(info, dir, lh, MDS_INODELOCK_UPDATE,
1627 /* get and save correct version after locking */
1628 mdt_version_get_save(info, dir, idx);
1633 static int mdt_rename_parents_lock(struct mdt_thread_info *info,
1634 struct mdt_object **srcp,
1635 struct mdt_object **tgtp)
1637 struct mdt_reint_record *rr = &info->mti_rr;
1638 const struct lu_fid *fid_src = rr->rr_fid1;
1639 const struct lu_fid *fid_tgt = rr->rr_fid2;
1640 struct mdt_lock_handle *lh_src = &info->mti_lh[MDT_LH_PARENT];
1641 struct mdt_lock_handle *lh_tgt = &info->mti_lh[MDT_LH_CHILD];
1642 struct mdt_object *src;
1643 struct mdt_object *tgt;
1648 /* find both parents. */
1649 src = mdt_object_find_check(info, fid_src, 0);
1651 RETURN(PTR_ERR(src));
1653 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME3, 5);
1655 if (lu_fid_eq(fid_src, fid_tgt)) {
1657 mdt_object_get(info->mti_env, tgt);
1659 /* Check if the @src is not a child of the @tgt, otherwise a
1660 * reverse locking must take place. */
1661 rc = mdt_is_subdir(info, src, fid_tgt);
1665 GOTO(err_src_put, rc);
1667 tgt = mdt_object_find_check(info, fid_tgt, 1);
1669 GOTO(err_src_put, rc = PTR_ERR(tgt));
1671 if (unlikely(mdt_object_remote(tgt))) {
1672 CDEBUG(D_INFO, "Source dir "DFID" target dir "DFID
1673 "on different MDTs\n", PFID(fid_src),
1675 GOTO(err_tgt_put, rc = -EXDEV);
1679 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1681 /* lock parents in the proper order. */
1683 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1685 GOTO(err_tgt_put, rc);
1687 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1689 rc = mdt_object_lock_save(info, src, lh_src, 0);
1691 rc = mdt_object_lock_save(info, src, lh_src, 0);
1693 GOTO(err_tgt_put, rc);
1695 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME, 5);
1698 rc = mdt_object_lock_save(info, tgt, lh_tgt, 1);
1699 else if (lh_src->mlh_pdo_hash != lh_tgt->mlh_pdo_hash) {
1700 rc = mdt_pdir_hash_lock(info, lh_tgt, tgt,
1701 MDS_INODELOCK_UPDATE);
1702 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1706 GOTO(err_unlock, rc);
1708 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME4, 5);
1715 /* The order does not matter as the handle is checked inside,
1716 * as well as not used handle. */
1717 mdt_object_unlock(info, src, lh_src, rc);
1718 mdt_object_unlock(info, tgt, lh_tgt, rc);
1720 mdt_object_put(info->mti_env, tgt);
1722 mdt_object_put(info->mti_env, src);
1727 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1728 * 2 - src child; 3 - tgt child.
1729 * Update on disk version of src child.
1732 * For DNE phase I, only these renames are allowed
1733 * mv src_p/src_c tgt_p/tgt_c
1734 * 1. src_p/src_c/tgt_p/tgt_c are in the same MDT.
1735 * 2. src_p and tgt_p are same directory, and tgt_c does not
1736 * exists. In this case, all of modification will happen
1737 * in the MDT where ithesource parent is, only one remote
1738 * update is needed, i.e. set c_time/m_time on the child.
1739 * And tgt_c will be still in the same MDT as the original
1742 static int mdt_reint_rename_internal(struct mdt_thread_info *info,
1743 struct mdt_lock_handle *lhc)
1745 struct mdt_reint_record *rr = &info->mti_rr;
1746 struct md_attr *ma = &info->mti_attr;
1747 struct ptlrpc_request *req = mdt_info_req(info);
1748 struct mdt_object *msrcdir = NULL;
1749 struct mdt_object *mtgtdir = NULL;
1750 struct mdt_object *mold;
1751 struct mdt_object *mnew = NULL;
1752 struct mdt_lock_handle *lh_srcdirp;
1753 struct mdt_lock_handle *lh_tgtdirp;
1754 struct mdt_lock_handle *lh_oldp = NULL;
1755 struct mdt_lock_handle *lh_newp = NULL;
1756 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1757 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1761 DEBUG_REQ(D_INODE, req, "rename "DFID"/"DNAME" to "DFID"/"DNAME,
1762 PFID(rr->rr_fid1), PNAME(&rr->rr_name),
1763 PFID(rr->rr_fid2), PNAME(&rr->rr_tgt_name));
1765 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1766 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, &rr->rr_name);
1767 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1768 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, &rr->rr_tgt_name);
1770 /* step 1&2: lock the source and target dirs. */
1771 rc = mdt_rename_parents_lock(info, &msrcdir, &mtgtdir);
1775 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RENAME2, 5);
1777 /* step 3: find & lock the old object. */
1779 rc = mdt_lookup_version_check(info, msrcdir, &rr->rr_name, old_fid, 2);
1781 GOTO(out_unlock_parents, rc);
1783 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1784 GOTO(out_unlock_parents, rc = -EINVAL);
1786 if (!fid_is_md_operative(old_fid))
1787 GOTO(out_unlock_parents, rc = -EPERM);
1789 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1791 GOTO(out_unlock_parents, rc = PTR_ERR(mold));
1793 /* Check if @mtgtdir is subdir of @mold, before locking child
1794 * to avoid reverse locking. */
1795 rc = mdt_is_subdir(info, mtgtdir, old_fid);
1797 GOTO(out_put_old, rc);
1799 tgt_vbr_obj_set(info->mti_env, mdt_obj2dt(mold));
1800 /* save version after locking */
1801 mdt_version_get_save(info, mold, 2);
1802 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1804 /* step 4: find & lock the new object. */
1805 /* new target object may not exist now */
1806 /* lookup with version checking */
1808 rc = mdt_lookup_version_check(info, mtgtdir, &rr->rr_tgt_name, new_fid,
1811 /* the new_fid should have been filled at this moment */
1812 if (lu_fid_eq(old_fid, new_fid))
1813 GOTO(out_put_old, rc);
1815 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1816 lu_fid_eq(new_fid, rr->rr_fid2))
1817 GOTO(out_put_old, rc = -EINVAL);
1819 if (!fid_is_md_operative(new_fid))
1820 GOTO(out_put_old, rc = -EPERM);
1822 if (mdt_object_remote(mold)) {
1823 CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1825 GOTO(out_put_old, rc = -EXDEV);
1828 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1830 GOTO(out_put_old, rc = PTR_ERR(mnew));
1832 if (mdt_object_remote(mnew)) {
1833 CDEBUG(D_INFO, "src child "DFID" is on another MDT\n",
1835 GOTO(out_put_new, rc = -EXDEV);
1838 /* Before locking the target dir, check we do not replace
1839 * a dir with a non-dir, otherwise it may deadlock with
1840 * link op which tries to create a link in this dir
1841 * back to this non-dir. */
1842 if (S_ISDIR(lu_object_attr(&mnew->mot_obj)) &&
1843 !S_ISDIR(lu_object_attr(&mold->mot_obj)))
1844 GOTO(out_put_new, rc = -EISDIR);
1846 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1847 mdt_lock_reg_init(lh_oldp, LCK_EX);
1848 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1849 MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1851 GOTO(out_put_new, rc);
1853 /* Check if @msrcdir is subdir of @mnew, before locking child
1854 * to avoid reverse locking. */
1855 rc = mdt_is_subdir(info, msrcdir, new_fid);
1857 GOTO(out_unlock_old, rc);
1859 /* We used to acquire MDS_INODELOCK_FULL here but we
1860 * can't do this now because a running HSM restore on
1861 * the rename onto victim will hold the layout
1862 * lock. See LU-4002. */
1864 lh_newp = &info->mti_lh[MDT_LH_NEW];
1865 mdt_lock_reg_init(lh_newp, LCK_EX);
1866 rc = mdt_object_lock(info, mnew, lh_newp,
1867 MDS_INODELOCK_LOOKUP |
1868 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 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1876 } else if (rc != -EREMOTE && rc != -ENOENT) {
1877 GOTO(out_put_old, rc);
1879 /* If mnew does not exist and mold are remote directory,
1880 * it only allows rename if they are under same directory */
1881 if (mtgtdir != msrcdir && mdt_object_remote(mold)) {
1882 CDEBUG(D_INFO, "Src child "DFID" is on another MDT\n",
1884 GOTO(out_put_old, rc = -EXDEV);
1887 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1888 mdt_lock_reg_init(lh_oldp, LCK_EX);
1889 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP |
1890 MDS_INODELOCK_XATTR, MDT_CROSS_LOCK);
1892 GOTO(out_put_old, rc);
1894 mdt_enoent_version_save(info, 3);
1897 /* step 5: rename it */
1898 mdt_reint_init_ma(info, ma);
1900 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1901 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1904 mutex_lock(&mnew->mot_lov_mutex);
1906 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1907 mdt_object_child(mtgtdir), old_fid, &rr->rr_name,
1908 mnew != NULL ? mdt_object_child(mnew) : NULL,
1909 &rr->rr_tgt_name, ma);
1912 mutex_unlock(&mnew->mot_lov_mutex);
1914 /* handle last link of tgt object */
1916 mdt_counter_incr(req, LPROC_MDT_RENAME);
1918 mdt_handle_last_unlink(info, mnew, ma);
1920 mdt_rename_counter_tally(info, info->mti_mdt, req,
1926 mdt_object_unlock(info, mnew, lh_newp, rc);
1928 mdt_object_unlock(info, mold, lh_oldp, rc);
1931 mdt_object_put(info->mti_env, mnew);
1933 mdt_object_put(info->mti_env, mold);
1935 mdt_object_unlock_put(info, mtgtdir, lh_tgtdirp, rc);
1936 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1940 static int mdt_reint_rename_or_migrate(struct mdt_thread_info *info,
1941 struct mdt_lock_handle *lhc,
1942 enum mdt_rename_lock rename_lock)
1944 struct mdt_reint_record *rr = &info->mti_rr;
1945 struct ptlrpc_request *req = mdt_info_req(info);
1946 struct lustre_handle rename_lh = { 0 };
1950 if (info->mti_dlm_req)
1951 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1953 if (!fid_is_md_operative(rr->rr_fid1) ||
1954 !fid_is_md_operative(rr->rr_fid2))
1957 rc = mdt_rename_lock(info, &rename_lh, rename_lock);
1959 CERROR("%s: can't lock FS for rename: rc = %d\n",
1960 mdt_obd_name(info->mti_mdt), rc);
1964 if (rename_lock == MRL_RENAME)
1965 rc = mdt_reint_rename_internal(info, lhc);
1967 rc = mdt_reint_migrate_internal(info, lhc);
1969 if (lustre_handle_is_used(&rename_lh))
1970 mdt_rename_unlock(&rename_lh);
1975 static int mdt_reint_rename(struct mdt_thread_info *info,
1976 struct mdt_lock_handle *lhc)
1978 return mdt_reint_rename_or_migrate(info, lhc, MRL_RENAME);
1981 static int mdt_reint_migrate(struct mdt_thread_info *info,
1982 struct mdt_lock_handle *lhc)
1984 return mdt_reint_rename_or_migrate(info, lhc, MRL_MIGRATE);
1987 struct mdt_reinter {
1988 int (*mr_handler)(struct mdt_thread_info *, struct mdt_lock_handle *);
1989 enum lprocfs_extra_opc mr_extra_opc;
1992 static const struct mdt_reinter mdt_reinters[] = {
1994 .mr_handler = &mdt_reint_setattr,
1995 .mr_extra_opc = MDS_REINT_SETATTR,
1998 .mr_handler = &mdt_reint_create,
1999 .mr_extra_opc = MDS_REINT_CREATE,
2002 .mr_handler = &mdt_reint_link,
2003 .mr_extra_opc = MDS_REINT_LINK,
2006 .mr_handler = &mdt_reint_unlink,
2007 .mr_extra_opc = MDS_REINT_UNLINK,
2010 .mr_handler = &mdt_reint_rename,
2011 .mr_extra_opc = MDS_REINT_RENAME,
2014 .mr_handler = &mdt_reint_open,
2015 .mr_extra_opc = MDS_REINT_OPEN,
2017 [REINT_SETXATTR] = {
2018 .mr_handler = &mdt_reint_setxattr,
2019 .mr_extra_opc = MDS_REINT_SETXATTR,
2022 .mr_handler = &mdt_reint_unlink,
2023 .mr_extra_opc = MDS_REINT_UNLINK,
2026 .mr_handler = &mdt_reint_migrate,
2027 .mr_extra_opc = MDS_REINT_RENAME,
2031 int mdt_reint_rec(struct mdt_thread_info *info,
2032 struct mdt_lock_handle *lhc)
2034 const struct mdt_reinter *mr;
2038 if (!(info->mti_rr.rr_opcode < ARRAY_SIZE(mdt_reinters)))
2041 mr = &mdt_reinters[info->mti_rr.rr_opcode];
2042 if (mr->mr_handler == NULL)
2045 rc = (*mr->mr_handler)(info, lhc);
2047 lprocfs_counter_incr(ptlrpc_req2svc(mdt_info_req(info))->srv_stats,
2048 PTLRPC_LAST_CNTR + mr->mr_extra_opc);