Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hostfs: support rename flags

Support RENAME_NOREPLACE and RENAME_EXCHANGE flags on hostfs if the
underlying filesystem supports it.

Since renameat2(2) is not yet in any libc, use syscall(2) to invoke the
renameat2 syscall.

Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
Cc: Richard Weinberger <richard@nod.at>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

authored by

Miklos Szeredi and committed by
Al Viro
9a423bb6 37456771

+49 -10
+1
fs/hostfs/hostfs.h
··· 89 89 extern int link_file(const char *from, const char *to); 90 90 extern int hostfs_do_readlink(char *file, char *buf, int size); 91 91 extern int rename_file(char *from, char *to); 92 + extern int rename2_file(char *from, char *to, unsigned int flags); 92 93 extern int do_statfs(char *root, long *bsize_out, long long *blocks_out, 93 94 long long *bfree_out, long long *bavail_out, 94 95 long long *files_out, long long *ffree_out,
+20 -10
fs/hostfs/hostfs_kern.c
··· 741 741 return err; 742 742 } 743 743 744 - static int hostfs_rename(struct inode *from_ino, struct dentry *from, 745 - struct inode *to_ino, struct dentry *to) 744 + static int hostfs_rename2(struct inode *old_dir, struct dentry *old_dentry, 745 + struct inode *new_dir, struct dentry *new_dentry, 746 + unsigned int flags) 746 747 { 747 - char *from_name, *to_name; 748 + char *old_name, *new_name; 748 749 int err; 749 750 750 - if ((from_name = dentry_name(from)) == NULL) 751 + if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 752 + return -EINVAL; 753 + 754 + old_name = dentry_name(old_dentry); 755 + if (old_name == NULL) 751 756 return -ENOMEM; 752 - if ((to_name = dentry_name(to)) == NULL) { 753 - __putname(from_name); 757 + new_name = dentry_name(new_dentry); 758 + if (new_name == NULL) { 759 + __putname(old_name); 754 760 return -ENOMEM; 755 761 } 756 - err = rename_file(from_name, to_name); 757 - __putname(from_name); 758 - __putname(to_name); 762 + if (!flags) 763 + err = rename_file(old_name, new_name); 764 + else 765 + err = rename2_file(old_name, new_name, flags); 766 + 767 + __putname(old_name); 768 + __putname(new_name); 759 769 return err; 760 770 } 761 771 ··· 877 867 .mkdir = hostfs_mkdir, 878 868 .rmdir = hostfs_rmdir, 879 869 .mknod = hostfs_mknod, 880 - .rename = hostfs_rename, 870 + .rename2 = hostfs_rename2, 881 871 .permission = hostfs_permission, 882 872 .setattr = hostfs_setattr, 883 873 };
+28
fs/hostfs/hostfs_user.c
··· 14 14 #include <sys/time.h> 15 15 #include <sys/types.h> 16 16 #include <sys/vfs.h> 17 + #include <sys/syscall.h> 17 18 #include "hostfs.h" 18 19 #include <utime.h> 19 20 ··· 359 358 if (err < 0) 360 359 return -errno; 361 360 return 0; 361 + } 362 + 363 + int rename2_file(char *from, char *to, unsigned int flags) 364 + { 365 + int err; 366 + 367 + #ifndef SYS_renameat2 368 + # ifdef __x86_64__ 369 + # define SYS_renameat2 316 370 + # endif 371 + # ifdef __i386__ 372 + # define SYS_renameat2 353 373 + # endif 374 + #endif 375 + 376 + #ifdef SYS_renameat2 377 + err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags); 378 + if (err < 0) { 379 + if (errno != ENOSYS) 380 + return -errno; 381 + else 382 + return -EINVAL; 383 + } 384 + return 0; 385 + #else 386 + return -EINVAL; 387 + #endif 362 388 } 363 389 364 390 int do_statfs(char *root, long *bsize_out, long long *blocks_out,