]>
Commit | Line | Data |
---|---|---|
d6083431 SL |
1 | From 4a2ff7d4d3d4f25440c78faa6eb13bb77d225957 Mon Sep 17 00:00:00 2001 |
2 | From: "Darrick J. Wong" <darrick.wong@oracle.com> | |
3 | Date: Thu, 21 Feb 2019 08:48:09 -0800 | |
4 | Subject: tmpfs: fix link accounting when a tmpfile is linked in | |
5 | ||
6 | [ Upstream commit 1062af920c07f5b54cf5060fde3339da6df0cf6b ] | |
7 | ||
8 | tmpfs has a peculiarity of accounting hard links as if they were | |
9 | separate inodes: so that when the number of inodes is limited, as it is | |
10 | by default, a user cannot soak up an unlimited amount of unreclaimable | |
11 | dcache memory just by repeatedly linking a file. | |
12 | ||
13 | But when v3.11 added O_TMPFILE, and the ability to use linkat() on the | |
14 | fd, we missed accommodating this new case in tmpfs: "df -i" shows that | |
15 | an extra "inode" remains accounted after the file is unlinked and the fd | |
16 | closed and the actual inode evicted. If a user repeatedly links | |
17 | tmpfiles into a tmpfs, the limit will be hit (ENOSPC) even after they | |
18 | are deleted. | |
19 | ||
20 | Just skip the extra reservation from shmem_link() in this case: there's | |
21 | a sense in which this first link of a tmpfile is then cheaper than a | |
22 | hard link of another file, but the accounting works out, and there's | |
23 | still good limiting, so no need to do anything more complicated. | |
24 | ||
25 | Link: http://lkml.kernel.org/r/alpine.LSU.2.11.1902182134370.7035@eggly.anvils | |
26 | Fixes: f4e0c30c191 ("allow the temp files created by open() to be linked to") | |
27 | Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> | |
28 | Signed-off-by: Hugh Dickins <hughd@google.com> | |
29 | Reported-by: Matej Kupljen <matej.kupljen@gmail.com> | |
30 | Acked-by: Al Viro <viro@zeniv.linux.org.uk> | |
31 | Signed-off-by: Andrew Morton <akpm@linux-foundation.org> | |
32 | Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> | |
33 | Signed-off-by: Sasha Levin <sashal@kernel.org> | |
34 | --- | |
35 | mm/shmem.c | 10 +++++++--- | |
36 | 1 file changed, 7 insertions(+), 3 deletions(-) | |
37 | ||
38 | diff --git a/mm/shmem.c b/mm/shmem.c | |
39 | index d902b413941a..183ed4dae219 100644 | |
40 | --- a/mm/shmem.c | |
41 | +++ b/mm/shmem.c | |
42 | @@ -2299,10 +2299,14 @@ static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentr | |
43 | * No ordinary (disk based) filesystem counts links as inodes; | |
44 | * but each new link needs a new dentry, pinning lowmem, and | |
45 | * tmpfs dentries cannot be pruned until they are unlinked. | |
46 | + * But if an O_TMPFILE file is linked into the tmpfs, the | |
47 | + * first link must skip that, to get the accounting right. | |
48 | */ | |
49 | - ret = shmem_reserve_inode(inode->i_sb); | |
50 | - if (ret) | |
51 | - goto out; | |
52 | + if (inode->i_nlink) { | |
53 | + ret = shmem_reserve_inode(inode->i_sb); | |
54 | + if (ret) | |
55 | + goto out; | |
56 | + } | |
57 | ||
58 | dir->i_size += BOGO_DIRENT_SIZE; | |
59 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
60 | -- | |
61 | 2.19.1 | |
62 |