Skip to content

Commit f4e0c30

Browse files
author
Al Viro
committed
allow the temp files created by open() to be linked to
O_TMPFILE | O_CREAT => linkat() with AT_SYMLINK_FOLLOW and /proc/self/fd/<n> as oldpath (i.e. flink()) will create a link O_TMPFILE | O_CREAT | O_EXCL => ENOENT on attempt to link those guys Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 60545d0 commit f4e0c30

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

fs/inode.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,10 @@ EXPORT_SYMBOL(set_nlink);
333333
*/
334334
void inc_nlink(struct inode *inode)
335335
{
336-
if (WARN_ON(inode->i_nlink == 0))
336+
if (unlikely(inode->i_nlink == 0)) {
337+
WARN_ON(!(inode->i_state & I_LINKABLE));
337338
atomic_long_dec(&inode->i_sb->s_remove_count);
339+
}
338340

339341
inode->__i_nlink++;
340342
}

fs/namei.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,8 +2948,14 @@ static int do_tmpfile(int dfd, struct filename *pathname,
29482948
if (error)
29492949
goto out2;
29502950
error = open_check_o_direct(file);
2951-
if (error)
2951+
if (error) {
29522952
fput(file);
2953+
} else if (!(op->open_flag & O_EXCL)) {
2954+
struct inode *inode = file_inode(file);
2955+
spin_lock(&inode->i_lock);
2956+
inode->i_state |= I_LINKABLE;
2957+
spin_unlock(&inode->i_lock);
2958+
}
29532959
out2:
29542960
mnt_drop_write(nd->path.mnt);
29552961
out:
@@ -3628,12 +3634,18 @@ int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_de
36283634

36293635
mutex_lock(&inode->i_mutex);
36303636
/* Make sure we don't allow creating hardlink to an unlinked file */
3631-
if (inode->i_nlink == 0)
3637+
if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
36323638
error = -ENOENT;
36333639
else if (max_links && inode->i_nlink >= max_links)
36343640
error = -EMLINK;
36353641
else
36363642
error = dir->i_op->link(old_dentry, dir, new_dentry);
3643+
3644+
if (!error && (inode->i_state & I_LINKABLE)) {
3645+
spin_lock(&inode->i_lock);
3646+
inode->i_state &= ~I_LINKABLE;
3647+
spin_unlock(&inode->i_lock);
3648+
}
36373649
mutex_unlock(&inode->i_mutex);
36383650
if (!error)
36393651
fsnotify_link(dir, inode, new_dentry);

include/linux/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,7 @@ struct super_operations {
17441744
#define I_REFERENCED (1 << 8)
17451745
#define __I_DIO_WAKEUP 9
17461746
#define I_DIO_WAKEUP (1 << I_DIO_WAKEUP)
1747+
#define I_LINKABLE (1 << 10)
17471748

17481749
#define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)
17491750

0 commit comments

Comments
 (0)