Skip to content

Commit 520c8b1

Browse files
author
Miklos Szeredi
committed
vfs: add renameat2 syscall
Add new renameat2 syscall, which is the same as renameat with an added flags argument. Pass flags to vfs_rename() and to i_op->rename() as well. Signed-off-by: Miklos Szeredi <mszeredi@suse.cz> Reviewed-by: J. Bruce Fields <bfields@redhat.com>
1 parent bc27027 commit 520c8b1

10 files changed

Lines changed: 58 additions & 15 deletions

File tree

‎Documentation/filesystems/Locking‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ prototypes:
4747
int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
4848
int (*rename) (struct inode *, struct dentry *,
4949
struct inode *, struct dentry *);
50+
int (*rename2) (struct inode *, struct dentry *,
51+
struct inode *, struct dentry *, unsigned int);
5052
int (*readlink) (struct dentry *, char __user *,int);
5153
void * (*follow_link) (struct dentry *, struct nameidata *);
5254
void (*put_link) (struct dentry *, struct nameidata *, void *);
@@ -78,6 +80,7 @@ mkdir: yes
7880
unlink: yes (both)
7981
rmdir: yes (both) (see below)
8082
rename: yes (all) (see below)
83+
rename2: yes (all) (see below)
8184
readlink: no
8285
follow_link: no
8386
put_link: no
@@ -96,7 +99,8 @@ tmpfile: no
9699

97100
Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
98101
victim.
99-
cross-directory ->rename() has (per-superblock) ->s_vfs_rename_sem.
102+
cross-directory ->rename() and rename2() has (per-superblock)
103+
->s_vfs_rename_sem.
100104

101105
See Documentation/filesystems/directory-locking for more detailed discussion
102106
of the locking scheme for directory operations.

‎Documentation/filesystems/vfs.txt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ struct inode_operations {
347347
int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
348348
int (*rename) (struct inode *, struct dentry *,
349349
struct inode *, struct dentry *);
350+
int (*rename2) (struct inode *, struct dentry *,
351+
struct inode *, struct dentry *, unsigned int);
350352
int (*readlink) (struct dentry *, char __user *,int);
351353
void * (*follow_link) (struct dentry *, struct nameidata *);
352354
void (*put_link) (struct dentry *, struct nameidata *, void *);
@@ -414,6 +416,20 @@ otherwise noted.
414416
rename: called by the rename(2) system call to rename the object to
415417
have the parent and name given by the second inode and dentry.
416418

419+
rename2: this has an additional flags argument compared to rename.
420+
If no flags are supported by the filesystem then this method
421+
need not be implemented. If some flags are supported then the
422+
filesystem must return -EINVAL for any unsupported or unknown
423+
flags. Currently the following flags are implemented:
424+
(1) RENAME_NOREPLACE: this flag indicates that if the target
425+
of the rename exists the rename should fail with -EEXIST
426+
instead of replacing the target. The VFS already checks for
427+
existence, so for local filesystems the RENAME_NOREPLACE
428+
implementation is equivalent to plain rename.
429+
(2) RENAME_EXCHANGE: exchange source and target. Both must
430+
exist; this is checked by the VFS. Unlike plain rename,
431+
source and target may be of different type.
432+
417433
readlink: called by the readlink(2) system call. Only required if
418434
you want to support reading symbolic links
419435

‎arch/x86/syscalls/syscall_64.tbl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@
322322
313 common finit_module sys_finit_module
323323
314 common sched_setattr sys_sched_setattr
324324
315 common sched_getattr sys_sched_getattr
325+
316 common renameat2 sys_renameat2
325326

326327
#
327328
# x32-specific system call numbers start at 512 to avoid cache impact

‎drivers/staging/lustre/lustre/include/linux/lustre_compat25.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ static inline void ll_set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
105105
#define ll_vfs_unlink(inode,entry,mnt) vfs_unlink(inode,entry)
106106
#define ll_vfs_mknod(dir,entry,mnt,mode,dev) vfs_mknod(dir,entry,mode,dev)
107107
#define ll_security_inode_unlink(dir,entry,mnt) security_inode_unlink(dir,entry)
108-
#define ll_vfs_rename(old,old_dir,mnt,new,new_dir,mnt1,delegated_inode) \
109-
vfs_rename(old,old_dir,new,new_dir,delegated_inode)
108+
#define ll_vfs_rename(old, old_dir, mnt, new, new_dir, mnt1) \
109+
vfs_rename(old, old_dir, new, new_dir, NULL, 0)
110110

111111
#define cfs_bio_io_error(a,b) bio_io_error((a))
112112
#define cfs_bio_endio(a,b,c) bio_endio((a),(c))

‎drivers/staging/lustre/lustre/lvfs/lvfs_linux.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ int lustre_rename(struct dentry *dir, struct vfsmount *mnt,
223223
GOTO(put_old, err = PTR_ERR(dchild_new));
224224

225225
err = ll_vfs_rename(dir->d_inode, dchild_old, mnt,
226-
dir->d_inode, dchild_new, mnt, NULL);
226+
dir->d_inode, dchild_new, mnt);
227227

228228
dput(dchild_new);
229229
put_old:

‎fs/cachefiles/namei.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ static int cachefiles_bury_object(struct cachefiles_cache *cache,
396396
cachefiles_io_error(cache, "Rename security error %d", ret);
397397
} else {
398398
ret = vfs_rename(dir->d_inode, rep,
399-
cache->graveyard->d_inode, grave, NULL);
399+
cache->graveyard->d_inode, grave, NULL, 0);
400400
if (ret != 0 && ret != -ENOMEM)
401401
cachefiles_io_error(cache,
402402
"Rename failed with error %d", ret);

‎fs/ecryptfs/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
641641
}
642642
rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
643643
lower_new_dir_dentry->d_inode, lower_new_dentry,
644-
NULL);
644+
NULL, 0);
645645
if (rc)
646646
goto out_lock;
647647
if (target_inode)

‎fs/namei.c‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,7 @@ SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname
39803980
* @new_dir: parent of destination
39813981
* @new_dentry: destination
39823982
* @delegated_inode: returns an inode needing a delegation break
3983+
* @flags: rename flags
39833984
*
39843985
* The caller must hold multiple mutexes--see lock_rename()).
39853986
*
@@ -4023,7 +4024,7 @@ SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname
40234024
*/
40244025
int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40254026
struct inode *new_dir, struct dentry *new_dentry,
4026-
struct inode **delegated_inode)
4027+
struct inode **delegated_inode, unsigned int flags)
40274028
{
40284029
int error;
40294030
bool is_dir = d_is_dir(old_dentry);
@@ -4048,6 +4049,9 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40484049
if (!old_dir->i_op->rename)
40494050
return -EPERM;
40504051

4052+
if (flags && !old_dir->i_op->rename2)
4053+
return -EINVAL;
4054+
40514055
/*
40524056
* If we are going to change the parent - check write permissions,
40534057
* we'll need to flip '..'.
@@ -4093,7 +4097,13 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40934097
goto out;
40944098
}
40954099
}
4096-
error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
4100+
if (!flags) {
4101+
error = old_dir->i_op->rename(old_dir, old_dentry,
4102+
new_dir, new_dentry);
4103+
} else {
4104+
error = old_dir->i_op->rename2(old_dir, old_dentry,
4105+
new_dir, new_dentry, flags);
4106+
}
40974107
if (error)
40984108
goto out;
40994109

@@ -4118,8 +4128,8 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
41184128
return error;
41194129
}
41204130

4121-
SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4122-
int, newdfd, const char __user *, newname)
4131+
SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4132+
int, newdfd, const char __user *, newname, unsigned int, flags)
41234133
{
41244134
struct dentry *old_dir, *new_dir;
41254135
struct dentry *old_dentry, *new_dentry;
@@ -4131,6 +4141,10 @@ SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
41314141
unsigned int lookup_flags = 0;
41324142
bool should_retry = false;
41334143
int error;
4144+
4145+
if (flags)
4146+
return -EINVAL;
4147+
41344148
retry:
41354149
from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
41364150
if (IS_ERR(from)) {
@@ -4202,8 +4216,8 @@ SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
42024216
if (error)
42034217
goto exit5;
42044218
error = vfs_rename(old_dir->d_inode, old_dentry,
4205-
new_dir->d_inode, new_dentry,
4206-
&delegated_inode);
4219+
new_dir->d_inode, new_dentry,
4220+
&delegated_inode, flags);
42074221
exit5:
42084222
dput(new_dentry);
42094223
exit4:
@@ -4233,9 +4247,15 @@ SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
42334247
return error;
42344248
}
42354249

4250+
SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4251+
int, newdfd, const char __user *, newname)
4252+
{
4253+
return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4254+
}
4255+
42364256
SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
42374257
{
4238-
return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
4258+
return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
42394259
}
42404260

42414261
int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)

‎fs/nfsd/vfs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
16941694
if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
16951695
goto out_dput_new;
16961696

1697-
host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL);
1697+
host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
16981698
if (!host_err) {
16991699
host_err = commit_metadata(tfhp);
17001700
if (!host_err)

‎include/linux/fs.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ extern int vfs_symlink(struct inode *, struct dentry *, const char *);
14601460
extern int vfs_link(struct dentry *, struct inode *, struct dentry *, struct inode **);
14611461
extern int vfs_rmdir(struct inode *, struct dentry *);
14621462
extern int vfs_unlink(struct inode *, struct dentry *, struct inode **);
1463-
extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *, struct inode **);
1463+
extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *, struct inode **, unsigned int);
14641464

14651465
/*
14661466
* VFS dentry helper functions.
@@ -1571,6 +1571,8 @@ struct inode_operations {
15711571
int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
15721572
int (*rename) (struct inode *, struct dentry *,
15731573
struct inode *, struct dentry *);
1574+
int (*rename2) (struct inode *, struct dentry *,
1575+
struct inode *, struct dentry *, unsigned int);
15741576
int (*setattr) (struct dentry *, struct iattr *);
15751577
int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
15761578
int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);

0 commit comments

Comments
 (0)