1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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>
48 # define EXPORT_SYMTAB
50 #define DEBUG_SUBSYSTEM S_MDS
52 #include "mdt_internal.h"
55 static inline void mdt_reint_init_ma(struct mdt_thread_info *info,
58 ma->ma_lmm = req_capsule_server_get(info->mti_pill, &RMF_MDT_MD);
59 ma->ma_lmm_size = req_capsule_get_size(info->mti_pill,
60 &RMF_MDT_MD, RCL_SERVER);
62 ma->ma_cookie = req_capsule_server_get(info->mti_pill,
64 ma->ma_cookie_size = req_capsule_get_size(info->mti_pill,
68 ma->ma_need = MA_INODE | MA_LOV | MA_COOKIE;
72 static int mdt_create_pack_capa(struct mdt_thread_info *info, int rc,
73 struct mdt_object *object,
74 struct mdt_body *repbody)
78 /* for cross-ref mkdir, mds capa has been fetched from remote obj, then
79 * we won't go to below*/
80 if (repbody->valid & OBD_MD_FLMDSCAPA)
83 if (rc == 0 && info->mti_mdt->mdt_opts.mo_mds_capa &&
84 info->mti_exp->exp_connect_flags & OBD_CONNECT_MDS_CAPA) {
85 struct lustre_capa *capa;
87 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA1);
89 capa->lc_opc = CAPA_OPC_MDS_DEFAULT;
90 rc = mo_capa_get(info->mti_env, mdt_object_child(object), capa,
93 repbody->valid |= OBD_MD_FLMDSCAPA;
100 * Get version of object by fid.
102 * Return real version or ENOENT_VERSION if object doesn't exist
104 static void mdt_obj_version_get(struct mdt_thread_info *info,
105 struct mdt_object *o, __u64 *version)
108 LASSERT(mdt_object_exists(o) >= 0);
109 if (mdt_object_exists(o) > 0)
110 *version = mo_version_get(info->mti_env, mdt_object_child(o));
112 *version = ENOENT_VERSION;
113 CDEBUG(D_INODE, "FID "DFID" version is "LPX64"\n",
114 PFID(mdt_object_fid(o)), *version);
118 * Check version is correct.
120 * Should be called only during replay.
122 static int mdt_version_check(struct ptlrpc_request *req,
123 __u64 version, int idx)
125 __u64 *pre_ver = lustre_msg_get_versions(req->rq_reqmsg);
128 if (!exp_connect_vbr(req->rq_export))
131 LASSERT(req_is_replay(req));
132 /** VBR: version is checked always because costs nothing */
133 LASSERT(idx < PTLRPC_NUM_VERSIONS);
134 /** Sanity check for malformed buffers */
135 if (pre_ver == NULL) {
136 CERROR("No versions in request buffer\n");
137 cfs_spin_lock(&req->rq_export->exp_lock);
138 req->rq_export->exp_vbr_failed = 1;
139 cfs_spin_unlock(&req->rq_export->exp_lock);
141 } else if (pre_ver[idx] != version) {
142 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
143 pre_ver[idx], version);
144 cfs_spin_lock(&req->rq_export->exp_lock);
145 req->rq_export->exp_vbr_failed = 1;
146 cfs_spin_unlock(&req->rq_export->exp_lock);
153 * Save pre-versions in reply.
155 static void mdt_version_save(struct ptlrpc_request *req, __u64 version,
160 if (!exp_connect_vbr(req->rq_export))
163 LASSERT(!req_is_replay(req));
164 LASSERT(req->rq_repmsg != NULL);
165 reply_ver = lustre_msg_get_versions(req->rq_repmsg);
167 reply_ver[idx] = version;
171 * Save enoent version, it is needed when it is obvious that object doesn't
172 * exist, e.g. child during create.
174 static void mdt_enoent_version_save(struct mdt_thread_info *info, int idx)
176 /* save version of file name for replay, it must be ENOENT here */
177 if (!req_is_replay(mdt_info_req(info))) {
178 info->mti_ver[idx] = ENOENT_VERSION;
179 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
184 * Get version from disk and save in reply buffer.
186 * Versions are saved in reply only during normal operations not replays.
188 void mdt_version_get_save(struct mdt_thread_info *info,
189 struct mdt_object *mto, int idx)
191 /* don't save versions during replay */
192 if (!req_is_replay(mdt_info_req(info))) {
193 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
194 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
199 * Get version from disk and check it, no save in reply.
201 int mdt_version_get_check(struct mdt_thread_info *info,
202 struct mdt_object *mto, int idx)
204 /* only check versions during replay */
205 if (!req_is_replay(mdt_info_req(info)))
208 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
209 return mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
213 * Get version from disk and check if recovery or just save.
215 int mdt_version_get_check_save(struct mdt_thread_info *info,
216 struct mdt_object *mto, int idx)
220 mdt_obj_version_get(info, mto, &info->mti_ver[idx]);
221 if (req_is_replay(mdt_info_req(info)))
222 rc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx],
225 mdt_version_save(mdt_info_req(info), info->mti_ver[idx], idx);
230 * Lookup with version checking.
232 * This checks version of 'name'. Many reint functions uses 'name' for child not
233 * FID, therefore we need to get object by name and check its version.
235 int mdt_lookup_version_check(struct mdt_thread_info *info,
236 struct mdt_object *p, struct lu_name *lname,
237 struct lu_fid *fid, int idx)
241 rc = mdo_lookup(info->mti_env, mdt_object_child(p), lname, fid,
243 /* Check version only during replay */
244 if (!req_is_replay(mdt_info_req(info)))
247 info->mti_ver[idx] = ENOENT_VERSION;
249 struct mdt_object *child;
250 child = mdt_object_find(info->mti_env, info->mti_mdt, fid);
251 if (likely(!IS_ERR(child))) {
252 mdt_obj_version_get(info, child, &info->mti_ver[idx]);
253 mdt_object_put(info->mti_env, child);
256 vbrc = mdt_version_check(mdt_info_req(info), info->mti_ver[idx], idx);
257 return vbrc ? vbrc : rc;
262 * VBR: we save three versions in reply:
263 * 0 - parent. Check that parent version is the same during replay.
264 * 1 - name. Version of 'name' if file exists with the same name or
265 * ENOENT_VERSION, it is needed because file may appear due to missed replays.
266 * 2 - child. Version of child by FID. Must be ENOENT. It is mostly sanity
269 static int mdt_md_create(struct mdt_thread_info *info)
271 struct mdt_device *mdt = info->mti_mdt;
272 struct mdt_object *parent;
273 struct mdt_object *child;
274 struct mdt_lock_handle *lh;
275 struct mdt_body *repbody;
276 struct md_attr *ma = &info->mti_attr;
277 struct mdt_reint_record *rr = &info->mti_rr;
278 struct lu_name *lname;
282 DEBUG_REQ(D_INODE, mdt_info_req(info), "Create (%s->"DFID") in "DFID,
283 rr->rr_name, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
285 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
287 lh = &info->mti_lh[MDT_LH_PARENT];
288 mdt_lock_pdo_init(lh, LCK_PW, rr->rr_name, rr->rr_namelen);
290 parent = mdt_object_find_lock(info, rr->rr_fid1, lh,
291 MDS_INODELOCK_UPDATE);
293 RETURN(PTR_ERR(parent));
295 rc = mdt_version_get_check_save(info, parent, 0);
297 GOTO(out_put_parent, rc);
300 * Check child name version during replay.
301 * During create replay a file may exist with same name.
303 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
304 rc = mdt_lookup_version_check(info, parent, lname,
305 &info->mti_tmp_fid1, 1);
306 /* -ENOENT is expected here */
307 if (rc != 0 && rc != -ENOENT)
308 GOTO(out_put_parent, rc);
310 /* save version of file name for replay, it must be ENOENT here */
311 mdt_enoent_version_save(info, 1);
313 child = mdt_object_find(info->mti_env, mdt, rr->rr_fid2);
314 if (likely(!IS_ERR(child))) {
315 struct md_object *next = mdt_object_child(parent);
317 ma->ma_need = MA_INODE;
319 /* capa for cross-ref will be stored here */
320 ma->ma_capa = req_capsule_server_get(info->mti_pill,
322 LASSERT(ma->ma_capa);
324 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
325 OBD_FAIL_MDS_REINT_CREATE_WRITE);
327 /* Version of child will be updated on disk. */
328 info->mti_mos = child;
329 rc = mdt_version_get_check_save(info, child, 2);
331 GOTO(out_put_child, rc);
333 /* Let lower layer know current lock mode. */
334 info->mti_spec.sp_cr_mode =
335 mdt_dlm_mode2mdl_mode(lh->mlh_pdo_mode);
338 * Do perform lookup sanity check. We do not know if name exists
341 info->mti_spec.sp_cr_lookup = 1;
342 info->mti_spec.sp_feat = &dt_directory_features;
344 rc = mdo_create(info->mti_env, next, lname,
345 mdt_object_child(child),
346 &info->mti_spec, ma);
348 /* Return fid & attr to client. */
349 if (ma->ma_valid & MA_INODE)
350 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
351 mdt_object_fid(child));
354 mdt_object_put(info->mti_env, child);
358 mdt_create_pack_capa(info, rc, child, repbody);
360 mdt_object_unlock_put(info, parent, lh, rc);
364 /* Partial request to create object only */
365 static int mdt_md_mkobj(struct mdt_thread_info *info)
367 struct mdt_device *mdt = info->mti_mdt;
368 struct mdt_object *o;
369 struct mdt_body *repbody;
370 struct md_attr *ma = &info->mti_attr;
374 DEBUG_REQ(D_INODE, mdt_info_req(info), "Partial create "DFID"",
375 PFID(info->mti_rr.rr_fid2));
377 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
379 o = mdt_object_find(info->mti_env, mdt, info->mti_rr.rr_fid2);
381 struct md_object *next = mdt_object_child(o);
383 ma->ma_need = MA_INODE;
387 * Cross-ref create can encounter already created obj in case of
388 * recovery, just get attr in that case.
390 if (mdt_object_exists(o) == 1) {
391 rc = mo_attr_get(info->mti_env, next, ma);
394 * Here, NO permission check for object_create,
395 * such check has been done on the original MDS.
397 rc = mo_object_create(info->mti_env, next,
398 &info->mti_spec, ma);
401 /* Return fid & attr to client. */
402 if (ma->ma_valid & MA_INODE)
403 mdt_pack_attr2body(info, repbody, &ma->ma_attr,
406 mdt_object_put(info->mti_env, o);
410 mdt_create_pack_capa(info, rc, o, repbody);
414 /* In the raw-setattr case, we lock the child inode.
415 * In the write-back case or if being called from open,
416 * the client holds a lock already.
417 * We use the ATTR_FROM_OPEN (translated into MRF_SETATTR_LOCKED by
418 * mdt_setattr_unpack()) flag to tell these cases apart. */
419 int mdt_attr_set(struct mdt_thread_info *info, struct mdt_object *mo,
420 struct md_attr *ma, int flags)
422 struct mdt_lock_handle *lh;
423 int do_vbr = ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID|LA_FLAGS);
427 /* attr shouldn't be set on remote object */
428 LASSERT(mdt_object_exists(mo) >= 0);
430 lh = &info->mti_lh[MDT_LH_PARENT];
431 mdt_lock_reg_init(lh, LCK_PW);
433 if (!(flags & MRF_SETATTR_LOCKED)) {
434 __u64 lockpart = MDS_INODELOCK_UPDATE;
435 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
436 lockpart |= MDS_INODELOCK_LOOKUP;
438 rc = mdt_object_lock(info, mo, lh, lockpart, MDT_LOCAL_LOCK);
443 if (mdt_object_exists(mo) == 0)
444 GOTO(out_unlock, rc = -ENOENT);
446 /* all attrs are packed into mti_attr in unpack_setattr */
447 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
448 OBD_FAIL_MDS_REINT_SETATTR_WRITE);
450 /* This is only for set ctime when rename's source is on remote MDS. */
451 if (unlikely(ma->ma_attr.la_valid == LA_CTIME))
452 ma->ma_attr_flags |= MDS_VTX_BYPASS;
454 /* VBR: update version if attr changed are important for recovery */
456 /* update on-disk version of changed object */
458 rc = mdt_version_get_check_save(info, mo, 0);
460 GOTO(out_unlock, rc);
463 /* all attrs are packed into mti_attr in unpack_setattr */
464 rc = mo_attr_set(info->mti_env, mdt_object_child(mo), ma);
466 GOTO(out_unlock, rc);
470 mdt_object_unlock(info, mo, lh, rc);
474 static int mdt_reint_setattr(struct mdt_thread_info *info,
475 struct mdt_lock_handle *lhc)
477 struct md_attr *ma = &info->mti_attr;
478 struct mdt_reint_record *rr = &info->mti_rr;
479 struct ptlrpc_request *req = mdt_info_req(info);
480 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
481 struct mdt_file_data *mfd;
482 struct mdt_object *mo;
483 struct md_object *next;
484 struct mdt_body *repbody;
488 DEBUG_REQ(D_INODE, req, "setattr "DFID" %x", PFID(rr->rr_fid1),
489 (unsigned int)ma->ma_attr.la_valid);
491 if (info->mti_dlm_req)
492 ldlm_request_cancel(req, info->mti_dlm_req, 0);
494 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
495 mo = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
497 GOTO(out, rc = PTR_ERR(mo));
499 /* start a log jounal handle if needed */
500 if (!(mdt_conn_flags(info) & OBD_CONNECT_SOM)) {
501 if ((ma->ma_attr.la_valid & LA_SIZE) ||
502 (rr->rr_flags & MRF_SETATTR_LOCKED)) {
503 /* Check write access for the O_TRUNC case */
504 if (mdt_write_read(mo) < 0)
505 GOTO(out_put, rc = -ETXTBSY);
507 } else if (info->mti_ioepoch &&
508 (info->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
509 /* Truncate case. IOEpoch is opened. */
510 rc = mdt_write_get(mo);
517 GOTO(out_put, rc = -ENOMEM);
520 mdt_ioepoch_open(info, mo, 0);
521 repbody->ioepoch = mo->mot_ioepoch;
523 mdt_object_get(info->mti_env, mo);
524 mdt_mfd_set_mode(mfd, FMODE_TRUNC);
525 mfd->mfd_object = mo;
526 mfd->mfd_xid = req->rq_xid;
528 cfs_spin_lock(&med->med_open_lock);
529 cfs_list_add(&mfd->mfd_list, &med->med_open_head);
530 cfs_spin_unlock(&med->med_open_lock);
531 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
534 som_au = info->mti_ioepoch && info->mti_ioepoch->flags & MF_SOM_CHANGE;
536 /* SOM Attribute update case. Find the proper mfd and update
537 * SOM attributes on the proper object. */
538 LASSERT(mdt_conn_flags(info) & OBD_CONNECT_SOM);
539 LASSERT(info->mti_ioepoch);
541 cfs_spin_lock(&med->med_open_lock);
542 mfd = mdt_handle2mfd(info, &info->mti_ioepoch->handle);
544 cfs_spin_unlock(&med->med_open_lock);
545 CDEBUG(D_INODE, "no handle for file close: "
546 "fid = "DFID": cookie = "LPX64"\n",
547 PFID(info->mti_rr.rr_fid1),
548 info->mti_ioepoch->handle.cookie);
549 GOTO(out_put, rc = -ESTALE);
551 LASSERT(mfd->mfd_mode == FMODE_SOM);
552 LASSERT(!(info->mti_ioepoch->flags & MF_EPOCH_CLOSE));
554 class_handle_unhash(&mfd->mfd_handle);
555 cfs_list_del_init(&mfd->mfd_list);
556 cfs_spin_unlock(&med->med_open_lock);
558 /* Close the found mfd, update attributes. */
559 ma->ma_lmm_size = info->mti_mdt->mdt_max_mdsize;
560 OBD_ALLOC(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
561 if (ma->ma_lmm == NULL)
562 GOTO(out_put, rc = -ENOMEM);
564 mdt_mfd_close(info, mfd);
566 OBD_FREE(ma->ma_lmm, info->mti_mdt->mdt_max_mdsize);
568 rc = mdt_attr_set(info, mo, ma, rr->rr_flags);
573 ma->ma_need = MA_INODE;
575 next = mdt_object_child(mo);
576 rc = mo_attr_get(info->mti_env, next, ma);
580 mdt_pack_attr2body(info, repbody, &ma->ma_attr, mdt_object_fid(mo));
582 if (info->mti_mdt->mdt_opts.mo_oss_capa &&
583 info->mti_exp->exp_connect_flags & OBD_CONNECT_OSS_CAPA &&
584 S_ISREG(lu_object_attr(&mo->mot_obj.mo_lu)) &&
585 (ma->ma_attr.la_valid & LA_SIZE) && !som_au) {
586 struct lustre_capa *capa;
588 capa = req_capsule_server_get(info->mti_pill, &RMF_CAPA2);
590 capa->lc_opc = CAPA_OPC_OSS_DEFAULT | CAPA_OPC_OSS_TRUNC;
591 rc = mo_capa_get(info->mti_env, mdt_object_child(mo), capa, 0);
594 repbody->valid |= OBD_MD_FLOSSCAPA;
599 mdt_object_put(info->mti_env, mo);
601 mdt_shrink_reply(info);
605 static int mdt_reint_create(struct mdt_thread_info *info,
606 struct mdt_lock_handle *lhc)
611 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
612 RETURN(err_serious(-ESTALE));
614 if (info->mti_dlm_req)
615 ldlm_request_cancel(mdt_info_req(info), info->mti_dlm_req, 0);
617 switch (info->mti_attr.ma_attr.la_mode & S_IFMT) {
619 /* Cross-ref case. */
620 if (info->mti_cross_ref) {
621 rc = mdt_md_mkobj(info);
631 /* Special file should stay on the same node as parent. */
632 LASSERT(info->mti_rr.rr_namelen > 0);
633 rc = mdt_md_create(info);
637 rc = err_serious(-EOPNOTSUPP);
643 * VBR: save parent version in reply and child version getting by its name.
644 * Version of child is getting and checking during its lookup. If
646 static int mdt_reint_unlink(struct mdt_thread_info *info,
647 struct mdt_lock_handle *lhc)
649 struct mdt_reint_record *rr = &info->mti_rr;
650 struct ptlrpc_request *req = mdt_info_req(info);
651 struct md_attr *ma = &info->mti_attr;
652 struct lu_fid *child_fid = &info->mti_tmp_fid1;
653 struct mdt_object *mp;
654 struct mdt_object *mc;
655 struct mdt_lock_handle *parent_lh;
656 struct mdt_lock_handle *child_lh;
657 struct lu_name *lname;
661 DEBUG_REQ(D_INODE, req, "unlink "DFID"/%s", PFID(rr->rr_fid1),
664 if (info->mti_dlm_req)
665 ldlm_request_cancel(req, info->mti_dlm_req, 0);
667 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
668 RETURN(err_serious(-ENOENT));
671 * step 1: lock the parent. Note, this may be child in case of
672 * remote operation denoted by ->mti_cross_ref flag.
674 parent_lh = &info->mti_lh[MDT_LH_PARENT];
675 if (info->mti_cross_ref) {
677 * Init reg lock for cross ref case when we need to do only
680 mdt_lock_reg_init(parent_lh, LCK_PW);
682 mdt_lock_pdo_init(parent_lh, LCK_PW, rr->rr_name,
685 mp = mdt_object_find_lock(info, rr->rr_fid1, parent_lh,
686 MDS_INODELOCK_UPDATE);
689 /* errors are possible here in cross-ref cases, see below */
690 if (info->mti_cross_ref)
695 rc = mdt_version_get_check_save(info, mp, 0);
697 GOTO(out_unlock_parent, rc);
699 mdt_reint_init_ma(info, ma);
700 if (!ma->ma_lmm || !ma->ma_cookie)
701 GOTO(out_unlock_parent, rc = -EINVAL);
703 if (info->mti_cross_ref) {
705 * Remote partial operation. It is possible that replay may
706 * happen on parent MDT and this operation will be repeated.
707 * Therefore the object absense is allowed case and nothing
708 * should be done here.
710 if (mdt_object_exists(mp) > 0) {
711 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
712 rc = mo_ref_del(info->mti_env,
713 mdt_object_child(mp), ma);
715 mdt_handle_last_unlink(info, mp, ma);
718 GOTO(out_unlock_parent, rc);
721 /* step 2: find & lock the child */
722 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
723 /* lookup child object along with version checking */
724 rc = mdt_lookup_version_check(info, mp, lname, child_fid, 1);
726 GOTO(out_unlock_parent, rc);
728 /* We will lock the child regardless it is local or remote. No harm. */
729 mc = mdt_object_find(info->mti_env, info->mti_mdt, child_fid);
731 GOTO(out_unlock_parent, rc = PTR_ERR(mc));
732 child_lh = &info->mti_lh[MDT_LH_CHILD];
733 mdt_lock_reg_init(child_lh, LCK_EX);
734 rc = mdt_object_lock(info, mc, child_lh, MDS_INODELOCK_FULL,
737 mdt_object_put(info->mti_env, mc);
738 GOTO(out_unlock_parent, rc);
741 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
742 OBD_FAIL_MDS_REINT_UNLINK_WRITE);
743 /* save version when object is locked */
744 mdt_version_get_save(info, mc, 1);
746 * Now we can only make sure we need MA_INODE, in mdd layer, will check
747 * whether need MA_LOV and MA_COOKIE.
749 ma->ma_need = MA_INODE;
751 mdt_set_capainfo(info, 1, child_fid, BYPASS_CAPA);
752 rc = mdo_unlink(info->mti_env, mdt_object_child(mp),
753 mdt_object_child(mc), lname, ma);
755 mdt_handle_last_unlink(info, mc, ma);
759 mdt_object_unlock_put(info, mc, child_lh, rc);
761 mdt_object_unlock_put(info, mp, parent_lh, rc);
767 * VBR: save versions in reply: 0 - parent; 1 - child by fid; 2 - target by
770 static int mdt_reint_link(struct mdt_thread_info *info,
771 struct mdt_lock_handle *lhc)
773 struct mdt_reint_record *rr = &info->mti_rr;
774 struct ptlrpc_request *req = mdt_info_req(info);
775 struct md_attr *ma = &info->mti_attr;
776 struct mdt_object *ms;
777 struct mdt_object *mp;
778 struct mdt_lock_handle *lhs;
779 struct mdt_lock_handle *lhp;
780 struct lu_name *lname;
784 DEBUG_REQ(D_INODE, req, "link "DFID" to "DFID"/%s",
785 PFID(rr->rr_fid1), PFID(rr->rr_fid2), rr->rr_name);
787 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
788 RETURN(err_serious(-ENOENT));
790 if (info->mti_dlm_req)
791 ldlm_request_cancel(req, info->mti_dlm_req, 0);
793 if (info->mti_cross_ref) {
794 /* MDT holding name ask us to add ref. */
795 lhs = &info->mti_lh[MDT_LH_CHILD];
796 mdt_lock_reg_init(lhs, LCK_EX);
797 ms = mdt_object_find_lock(info, rr->rr_fid1, lhs,
798 MDS_INODELOCK_UPDATE);
802 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
803 rc = mo_ref_add(info->mti_env, mdt_object_child(ms), ma);
804 mdt_object_unlock_put(info, ms, lhs, rc);
808 /* Invalid case so return error immediately instead of
810 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2))
813 /* step 1: find & lock the target parent dir */
814 lhp = &info->mti_lh[MDT_LH_PARENT];
815 mdt_lock_pdo_init(lhp, LCK_PW, rr->rr_name,
817 mp = mdt_object_find_lock(info, rr->rr_fid2, lhp,
818 MDS_INODELOCK_UPDATE);
822 rc = mdt_version_get_check_save(info, mp, 0);
824 GOTO(out_unlock_parent, rc);
826 /* step 2: find & lock the source */
827 lhs = &info->mti_lh[MDT_LH_CHILD];
828 mdt_lock_reg_init(lhs, LCK_EX);
830 ms = mdt_object_find(info->mti_env, info->mti_mdt, rr->rr_fid1);
832 GOTO(out_unlock_parent, rc = PTR_ERR(ms));
834 rc = mdt_object_lock(info, ms, lhs, MDS_INODELOCK_UPDATE,
837 mdt_object_put(info->mti_env, ms);
838 GOTO(out_unlock_parent, rc);
841 /* step 3: link it */
842 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
843 OBD_FAIL_MDS_REINT_LINK_WRITE);
846 rc = mdt_version_get_check_save(info, ms, 1);
848 GOTO(out_unlock_child, rc);
850 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
851 /** check target version by name during replay */
852 rc = mdt_lookup_version_check(info, mp, lname, &info->mti_tmp_fid1, 2);
853 if (rc != 0 && rc != -ENOENT)
854 GOTO(out_unlock_child, rc);
855 /* save version of file name for replay, it must be ENOENT here */
856 if (!req_is_replay(mdt_info_req(info))) {
857 info->mti_ver[2] = ENOENT_VERSION;
858 mdt_version_save(mdt_info_req(info), info->mti_ver[2], 2);
861 rc = mdo_link(info->mti_env, mdt_object_child(mp),
862 mdt_object_child(ms), lname, ma);
866 mdt_object_unlock_put(info, ms, lhs, rc);
868 mdt_object_unlock_put(info, mp, lhp, rc);
872 * lock the part of the directory according to the hash of the name
873 * (lh->mlh_pdo_hash) in parallel directory lock.
875 static int mdt_pdir_hash_lock(struct mdt_thread_info *info,
876 struct mdt_lock_handle *lh,
877 struct mdt_object *obj, __u64 ibits)
879 struct ldlm_res_id *res_id = &info->mti_res_id;
880 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
881 ldlm_policy_data_t *policy = &info->mti_policy;
885 * Finish res_id initializing by name hash marking part of
886 * directory which is taking modification.
888 LASSERT(lh->mlh_pdo_hash != 0);
889 fid_build_pdo_res_name(mdt_object_fid(obj), lh->mlh_pdo_hash, res_id);
890 memset(policy, 0, sizeof(*policy));
891 policy->l_inodebits.bits = ibits;
893 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
894 * going to be sent to client. If it is - mdt_intent_policy() path will
895 * fix it up and turn FL_LOCAL flag off.
897 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
898 res_id, LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB,
899 &info->mti_exp->exp_handle.h_cookie);
903 /* partial operation for rename */
904 static int mdt_reint_rename_tgt(struct mdt_thread_info *info)
906 struct mdt_reint_record *rr = &info->mti_rr;
907 struct ptlrpc_request *req = mdt_info_req(info);
908 struct md_attr *ma = &info->mti_attr;
909 struct mdt_object *mtgtdir;
910 struct mdt_object *mtgt = NULL;
911 struct mdt_lock_handle *lh_tgtdir;
912 struct mdt_lock_handle *lh_tgt = NULL;
913 struct lu_fid *tgt_fid = &info->mti_tmp_fid1;
914 struct lu_name *lname;
918 DEBUG_REQ(D_INODE, req, "rename_tgt: insert (%s->"DFID") in "DFID,
919 rr->rr_tgt, PFID(rr->rr_fid2), PFID(rr->rr_fid1));
921 /* step 1: lookup & lock the tgt dir. */
922 lh_tgtdir = &info->mti_lh[MDT_LH_PARENT];
923 mdt_lock_pdo_init(lh_tgtdir, LCK_PW, rr->rr_tgt,
925 mtgtdir = mdt_object_find_lock(info, rr->rr_fid1, lh_tgtdir,
926 MDS_INODELOCK_UPDATE);
928 RETURN(PTR_ERR(mtgtdir));
930 /* step 2: find & lock the target object if exists. */
931 mdt_set_capainfo(info, 0, rr->rr_fid1, BYPASS_CAPA);
932 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
933 rc = mdo_lookup(info->mti_env, mdt_object_child(mtgtdir),
934 lname, tgt_fid, &info->mti_spec);
935 if (rc != 0 && rc != -ENOENT) {
936 GOTO(out_unlock_tgtdir, rc);
937 } else if (rc == 0) {
939 * In case of replay that name can be already inserted, check
940 * that and do nothing if so.
942 if (lu_fid_eq(tgt_fid, rr->rr_fid2))
943 GOTO(out_unlock_tgtdir, rc);
945 lh_tgt = &info->mti_lh[MDT_LH_CHILD];
946 mdt_lock_reg_init(lh_tgt, LCK_EX);
948 mtgt = mdt_object_find_lock(info, tgt_fid, lh_tgt,
949 MDS_INODELOCK_LOOKUP);
951 GOTO(out_unlock_tgtdir, rc = PTR_ERR(mtgt));
953 mdt_reint_init_ma(info, ma);
954 if (!ma->ma_lmm || !ma->ma_cookie)
955 GOTO(out_unlock_tgt, rc = -EINVAL);
957 rc = mdo_rename_tgt(info->mti_env, mdt_object_child(mtgtdir),
958 mdt_object_child(mtgt), rr->rr_fid2,
960 } else /* -ENOENT */ {
961 rc = mdo_name_insert(info->mti_env, mdt_object_child(mtgtdir),
962 lname, rr->rr_fid2, ma);
965 /* handle last link of tgt object */
967 mdt_handle_last_unlink(info, mtgt, ma);
972 mdt_object_unlock_put(info, mtgt, lh_tgt, rc);
974 mdt_object_unlock_put(info, mtgtdir, lh_tgtdir, rc);
978 static int mdt_rename_lock(struct mdt_thread_info *info,
979 struct lustre_handle *lh)
981 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
982 ldlm_policy_data_t *policy = &info->mti_policy;
983 struct ldlm_res_id *res_id = &info->mti_res_id;
988 ms = mdt_md_site(info->mti_mdt);
989 fid_build_reg_res_name(&LUSTRE_BFL_FID, res_id);
991 memset(policy, 0, sizeof *policy);
992 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
994 if (ms->ms_control_exp == NULL) {
995 int flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
998 * Current node is controller, that is mdt0, where we should
1001 rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS, policy,
1002 LCK_EX, &flags, ldlm_blocking_ast,
1003 ldlm_completion_ast, NULL, NULL, 0,
1004 &info->mti_exp->exp_handle.h_cookie,
1007 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_EX,
1008 ldlm_blocking_ast, ldlm_completion_ast, NULL, NULL, NULL };
1012 * This is the case mdt0 is remote node, issue DLM lock like
1015 rc = ldlm_cli_enqueue(ms->ms_control_exp, NULL, &einfo, res_id,
1016 policy, &flags, NULL, 0, lh, 0);
1022 static void mdt_rename_unlock(struct lustre_handle *lh)
1025 LASSERT(lustre_handle_is_used(lh));
1026 ldlm_lock_decref(lh, LCK_EX);
1031 * This is is_subdir() variant, it is CMD if cmm forwards it to correct
1032 * target. Source should not be ancestor of target dir. May be other rename
1033 * checks can be moved here later.
1035 static int mdt_rename_sanity(struct mdt_thread_info *info, struct lu_fid *fid)
1037 struct mdt_reint_record *rr = &info->mti_rr;
1038 struct lu_fid dst_fid = *rr->rr_fid2;
1039 struct mdt_object *dst;
1044 LASSERT(fid_is_sane(&dst_fid));
1045 dst = mdt_object_find(info->mti_env, info->mti_mdt, &dst_fid);
1047 rc = mdo_is_subdir(info->mti_env,
1048 mdt_object_child(dst), fid,
1050 mdt_object_put(info->mti_env, dst);
1051 if (rc != -EREMOTE && rc < 0) {
1052 CERROR("Failed mdo_is_subdir(), rc %d\n", rc);
1054 /* check the found fid */
1055 if (lu_fid_eq(&dst_fid, fid))
1061 } while (rc == -EREMOTE);
1067 * VBR: rename versions in reply: 0 - src parent; 1 - tgt parent;
1068 * 2 - src child; 3 - tgt child.
1069 * Update on disk version of src child.
1071 static int mdt_reint_rename(struct mdt_thread_info *info,
1072 struct mdt_lock_handle *lhc)
1074 struct mdt_reint_record *rr = &info->mti_rr;
1075 struct md_attr *ma = &info->mti_attr;
1076 struct ptlrpc_request *req = mdt_info_req(info);
1077 struct mdt_object *msrcdir;
1078 struct mdt_object *mtgtdir;
1079 struct mdt_object *mold;
1080 struct mdt_object *mnew = NULL;
1081 struct mdt_lock_handle *lh_srcdirp;
1082 struct mdt_lock_handle *lh_tgtdirp;
1083 struct mdt_lock_handle *lh_oldp;
1084 struct mdt_lock_handle *lh_newp;
1085 struct lu_fid *old_fid = &info->mti_tmp_fid1;
1086 struct lu_fid *new_fid = &info->mti_tmp_fid2;
1087 struct lustre_handle rename_lh = { 0 };
1088 struct lu_name slname = { 0 };
1089 struct lu_name *lname;
1093 if (info->mti_dlm_req)
1094 ldlm_request_cancel(req, info->mti_dlm_req, 0);
1096 if (info->mti_cross_ref) {
1097 rc = mdt_reint_rename_tgt(info);
1101 DEBUG_REQ(D_INODE, req, "rename "DFID"/%s to "DFID"/%s",
1102 PFID(rr->rr_fid1), rr->rr_name,
1103 PFID(rr->rr_fid2), rr->rr_tgt);
1105 rc = mdt_rename_lock(info, &rename_lh);
1107 CERROR("Can't lock FS for rename, rc %d\n", rc);
1111 lh_newp = &info->mti_lh[MDT_LH_NEW];
1113 /* step 1: lock the source dir. */
1114 lh_srcdirp = &info->mti_lh[MDT_LH_PARENT];
1115 mdt_lock_pdo_init(lh_srcdirp, LCK_PW, rr->rr_name,
1117 msrcdir = mdt_object_find_lock(info, rr->rr_fid1, lh_srcdirp,
1118 MDS_INODELOCK_UPDATE);
1119 if (IS_ERR(msrcdir))
1120 GOTO(out_rename_lock, rc = PTR_ERR(msrcdir));
1122 rc = mdt_version_get_check_save(info, msrcdir, 0);
1124 GOTO(out_unlock_source, rc);
1126 /* step 2: find & lock the target dir. */
1127 lh_tgtdirp = &info->mti_lh[MDT_LH_CHILD];
1128 mdt_lock_pdo_init(lh_tgtdirp, LCK_PW, rr->rr_tgt,
1130 if (lu_fid_eq(rr->rr_fid1, rr->rr_fid2)) {
1131 mdt_object_get(info->mti_env, msrcdir);
1133 if (lh_tgtdirp->mlh_pdo_hash != lh_srcdirp->mlh_pdo_hash) {
1134 rc = mdt_pdir_hash_lock(info, lh_tgtdirp, mtgtdir,
1135 MDS_INODELOCK_UPDATE);
1137 GOTO(out_unlock_source, rc);
1138 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK2, 10);
1141 mtgtdir = mdt_object_find(info->mti_env, info->mti_mdt,
1143 if (IS_ERR(mtgtdir))
1144 GOTO(out_unlock_source, rc = PTR_ERR(mtgtdir));
1146 /* check early, the real version will be saved after locking */
1147 rc = mdt_version_get_check(info, mtgtdir, 1);
1149 GOTO(out_put_target, rc);
1151 rc = mdt_object_exists(mtgtdir);
1153 GOTO(out_put_target, rc = -ESTALE);
1154 } else if (rc > 0) {
1155 /* we lock the target dir if it is local */
1156 rc = mdt_object_lock(info, mtgtdir, lh_tgtdirp,
1157 MDS_INODELOCK_UPDATE,
1160 GOTO(out_put_target, rc);
1161 /* get and save correct version after locking */
1162 mdt_version_get_save(info, mtgtdir, 1);
1166 /* step 3: find & lock the old object. */
1167 lname = mdt_name(info->mti_env, (char *)rr->rr_name, rr->rr_namelen);
1168 mdt_name_copy(&slname, lname);
1169 rc = mdt_lookup_version_check(info, msrcdir, &slname, old_fid, 2);
1171 GOTO(out_unlock_target, rc);
1173 if (lu_fid_eq(old_fid, rr->rr_fid1) || lu_fid_eq(old_fid, rr->rr_fid2))
1174 GOTO(out_unlock_target, rc = -EINVAL);
1176 mold = mdt_object_find(info->mti_env, info->mti_mdt, old_fid);
1178 GOTO(out_unlock_target, rc = PTR_ERR(mold));
1180 lh_oldp = &info->mti_lh[MDT_LH_OLD];
1181 mdt_lock_reg_init(lh_oldp, LCK_EX);
1182 rc = mdt_object_lock(info, mold, lh_oldp, MDS_INODELOCK_LOOKUP,
1185 mdt_object_put(info->mti_env, mold);
1186 GOTO(out_unlock_target, rc);
1189 info->mti_mos = mold;
1190 /* save version after locking */
1191 mdt_version_get_save(info, mold, 2);
1192 mdt_set_capainfo(info, 2, old_fid, BYPASS_CAPA);
1194 /* step 4: find & lock the new object. */
1195 /* new target object may not exist now */
1196 lname = mdt_name(info->mti_env, (char *)rr->rr_tgt, rr->rr_tgtlen);
1197 /* lookup with version checking */
1198 rc = mdt_lookup_version_check(info, mtgtdir, lname, new_fid, 3);
1200 /* the new_fid should have been filled at this moment */
1201 if (lu_fid_eq(old_fid, new_fid))
1202 GOTO(out_unlock_old, rc);
1204 if (lu_fid_eq(new_fid, rr->rr_fid1) ||
1205 lu_fid_eq(new_fid, rr->rr_fid2))
1206 GOTO(out_unlock_old, rc = -EINVAL);
1208 mdt_lock_reg_init(lh_newp, LCK_EX);
1209 mnew = mdt_object_find(info->mti_env, info->mti_mdt, new_fid);
1211 GOTO(out_unlock_old, rc = PTR_ERR(mnew));
1213 rc = mdt_object_lock(info, mnew, lh_newp,
1214 MDS_INODELOCK_FULL, MDT_CROSS_LOCK);
1216 mdt_object_put(info->mti_env, mnew);
1217 GOTO(out_unlock_old, rc);
1219 /* get and save version after locking */
1220 mdt_version_get_save(info, mnew, 3);
1221 mdt_set_capainfo(info, 3, new_fid, BYPASS_CAPA);
1222 } else if (rc != -EREMOTE && rc != -ENOENT) {
1223 GOTO(out_unlock_old, rc);
1225 mdt_enoent_version_save(info, 3);
1228 /* step 5: rename it */
1229 mdt_reint_init_ma(info, ma);
1230 if (!ma->ma_lmm || !ma->ma_cookie)
1231 GOTO(out_unlock_new, rc = -EINVAL);
1233 mdt_fail_write(info->mti_env, info->mti_mdt->mdt_bottom,
1234 OBD_FAIL_MDS_REINT_RENAME_WRITE);
1237 /* Check if @dst is subdir of @src. */
1238 rc = mdt_rename_sanity(info, old_fid);
1240 GOTO(out_unlock_new, rc);
1242 rc = mdo_rename(info->mti_env, mdt_object_child(msrcdir),
1243 mdt_object_child(mtgtdir), old_fid, &slname,
1244 (mnew ? mdt_object_child(mnew) : NULL),
1247 /* handle last link of tgt object */
1248 if (rc == 0 && mnew)
1249 mdt_handle_last_unlink(info, mnew, ma);
1254 mdt_object_unlock_put(info, mnew, lh_newp, rc);
1256 mdt_object_unlock_put(info, mold, lh_oldp, rc);
1258 mdt_object_unlock(info, mtgtdir, lh_tgtdirp, rc);
1260 mdt_object_put(info->mti_env, mtgtdir);
1262 mdt_object_unlock_put(info, msrcdir, lh_srcdirp, rc);
1264 mdt_rename_unlock(&rename_lh);
1268 typedef int (*mdt_reinter)(struct mdt_thread_info *info,
1269 struct mdt_lock_handle *lhc);
1271 static mdt_reinter reinters[REINT_MAX] = {
1272 [REINT_SETATTR] = mdt_reint_setattr,
1273 [REINT_CREATE] = mdt_reint_create,
1274 [REINT_LINK] = mdt_reint_link,
1275 [REINT_UNLINK] = mdt_reint_unlink,
1276 [REINT_RENAME] = mdt_reint_rename,
1277 [REINT_OPEN] = mdt_reint_open,
1278 [REINT_SETXATTR] = mdt_reint_setxattr
1281 int mdt_reint_rec(struct mdt_thread_info *info,
1282 struct mdt_lock_handle *lhc)
1287 rc = reinters[info->mti_rr.rr_opcode](info, lhc);