3 .\" Copyright (c) 1996 Doug Rabson
5 .\" All rights reserved.
7 .\" This program is free software.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
19 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 .Fn VOP_RENAME "struct vnode *fdvp" "struct vnode *fvp" "struct componentname *fcnp" "struct vnode *tdvp" "struct vnode *tvp" "struct componentname *tcnp"
43 This renames a file and possibly changes its parent directory.
44 If the destination object exists, it will be removed first.
49 The vnode of the old parent directory.
51 The vnode of the file to be renamed.
53 Pathname information about the file's current name.
55 The vnode of the new parent directory.
57 The vnode of the target file (if it exists).
59 Pathname information about the file's new name.
62 The source directory and file are unlocked but are expected to have their
63 ref count bumped on entry.
64 The VOP routine is expected to
69 The destination directory and file are locked as well as having their ref
71 The VOP routine is expected to
78 vop_rename(struct vnode *fdvp, struct vnode *fvp, struct componentname *fcnp,
79 struct vnode *tdvp, struct vnode *tvp, struct componentname *tcnp)
81 int doingdirectory = 0;
85 * Check for cross-device rename.
87 if (fvp->v_mount != tdvp->v_mount) {
101 if (tvp exists and is immutable) {
107 * POSIX: "If the old argument and the new argument
108 * both refer to links to the same existing file,
109 * the rename() function shall return successfully
110 * and perform no other action."
111 * The upper layers already handle this case.
113 KASSERT(fvp != tvp, ("vop_rename: source and destination are the same"));
115 if (fvp is immutable) {
120 error = VOP_LOCK(fvp);
124 if (vp is a directory) {
126 * Avoid ".", "..", and aliases of "." for obvious reasons.
128 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')
130 || ((fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT)) {
140 * Bump link count on fvp while we are moving stuff around. If we
141 * crash before completing the work, the link count may be wrong
147 * If ".." must be changed (ie the directory gets a new
148 * parent) then the source directory must not be in the
149 * directory hierarchy above the target, as this would
150 * orphan everything below the source directory. Also
151 * the user must have write permission in the source so
152 * as to be able to change "..".
154 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
156 if (doingdirectory && fdvp != tdvp) {
158 * Check for pathname conflict.
164 * If the target doesn't exist, link the target to the source and
165 * unlink the source. Otherwise, rewrite the target directory to
166 * reference the source and remove the original entry.
170 * Account for ".." in new directory.
172 if (doingdirectory && fdvp != tdvp) {
174 * Increase link count of tdvp.
180 * Add name in new directory.
185 if (doingdirectory && fdvp != tdvp) {
187 * Decrease link count if tdvp.
196 * Target must be empty if a directory and have no links
197 * to it. Also, ensure source and target are compatible
198 * (both directories, or both not directories).
200 if (tvp is a directory) {
201 if (tvp is not empty) {
205 if (!doingdirectory) {
210 * Update name cache since directory is going away.
213 } else if (doingdirectory) {
219 * Change name tcnp in tdvp to point at fvp.
224 * If the target directory is in same directory as the source
225 * directory, decrement the link count on the parent of the
226 * target directory. This accounts for the fact that a
227 * directory links back to its parent with "..".
229 if (doingdirectory && fdvp == tdvp) {
231 * Decrement link count of tdvp.
238 * Decrement the link count of tvp since the directory no
239 * longer points at it.
242 if (doingdirectory) {
244 * Clean up the old directory tvp.
252 * Unlink the source. If a directory was moved to a new parent,
253 * update its ".." entry. Gobs of ugly UFS code omitted here.
262 if (VOP_LOCK(fvp) == 0) {
264 * Decrement link count of fvp.
277 The file is immutable.
279 It is not possible to rename a file between different file systems.
281 An attempt was made to rename
285 or to perform an operation which would break the directory tree structure.
287 An attempt was made to rename a directory to a file or vice versa.
289 An attempt was made to remove a directory which is not empty.
294 This manual page was written by