aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4/namei.c
diff options
context:
space:
mode:
authorMiklos Szeredi <mszeredi@suse.cz>2014-04-01 11:08:43 -0400
committerMiklos Szeredi <mszeredi@suse.cz>2014-04-01 11:08:43 -0400
commit0a7c3937a1f23f8cb5fc77ae01661e9968a51d0c (patch)
tree8924086bfd279409f9eb83aab9a6177e0748b257 /fs/ext4/namei.c
parent520c8b16505236fc82daa352e6c5e73cd9870cff (diff)
vfs: add RENAME_NOREPLACE flag
If this flag is specified and the target of the rename exists then the rename syscall fails with EEXIST. The VFS does the existence checking, so it is trivial to enable for most local filesystems. This patch only enables it in ext4. For network filesystems the VFS check is not enough as there may be a race between a remote create and the rename, so these filesystems need to handle this flag in their ->rename() implementations to ensure atomicity. Andy writes about why this is useful: "The trivial answer: to eliminate the race condition from 'mv -i'. Another answer: there's a common pattern to atomically create a file with contents: open a temporary file, write to it, optionally fsync it, close it, then link(2) it to the final name, then unlink the temporary file. The reason to use link(2) is because it won't silently clobber the destination. This is annoying: - It requires an extra system call that shouldn't be necessary. - It doesn't work on (IMO sensible) filesystems that don't support hard links (e.g. vfat). - It's not atomic -- there's an intermediate state where both files exist. - It's ugly. The new rename flag will make this totally sensible. To be fair, on new enough kernels, you can also use O_TMPFILE and linkat to achieve the same thing even more cleanly." Suggested-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz> Reviewed-by: J. Bruce Fields <bfields@redhat.com>
Diffstat (limited to 'fs/ext4/namei.c')
-rw-r--r--fs/ext4/namei.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index d050e043e884..5f19171b3e1f 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -3204,6 +3204,16 @@ end_rename:
3204 return retval; 3204 return retval;
3205} 3205}
3206 3206
3207static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry,
3208 struct inode *new_dir, struct dentry *new_dentry,
3209 unsigned int flags)
3210{
3211 if (flags & ~RENAME_NOREPLACE)
3212 return -EINVAL;
3213
3214 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry);
3215}
3216
3207/* 3217/*
3208 * directories can handle most operations... 3218 * directories can handle most operations...
3209 */ 3219 */
@@ -3218,6 +3228,7 @@ const struct inode_operations ext4_dir_inode_operations = {
3218 .mknod = ext4_mknod, 3228 .mknod = ext4_mknod,
3219 .tmpfile = ext4_tmpfile, 3229 .tmpfile = ext4_tmpfile,
3220 .rename = ext4_rename, 3230 .rename = ext4_rename,
3231 .rename2 = ext4_rename2,
3221 .setattr = ext4_setattr, 3232 .setattr = ext4_setattr,
3222 .setxattr = generic_setxattr, 3233 .setxattr = generic_setxattr,
3223 .getxattr = generic_getxattr, 3234 .getxattr = generic_getxattr,