]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - queue-4.14/tmpfs-fix-link-accounting-when-a-tmpfile-is-linked-i.patch
drop some duplicated patches that somehow got merged.
[thirdparty/kernel/stable-queue.git] / queue-4.14 / tmpfs-fix-link-accounting-when-a-tmpfile-is-linked-i.patch
CommitLineData
ecf3b270
SL
1From 10c6e2ab16c88538323b01a9d367e22c24d3471b Mon Sep 17 00:00:00 2001
2From: "Darrick J. Wong" <darrick.wong@oracle.com>
3Date: Thu, 21 Feb 2019 08:48:09 -0800
4Subject: tmpfs: fix link accounting when a tmpfile is linked in
5
6[ Upstream commit 1062af920c07f5b54cf5060fde3339da6df0cf6b ]
7
8tmpfs has a peculiarity of accounting hard links as if they were
9separate inodes: so that when the number of inodes is limited, as it is
10by default, a user cannot soak up an unlimited amount of unreclaimable
11dcache memory just by repeatedly linking a file.
12
13But when v3.11 added O_TMPFILE, and the ability to use linkat() on the
14fd, we missed accommodating this new case in tmpfs: "df -i" shows that
15an extra "inode" remains accounted after the file is unlinked and the fd
16closed and the actual inode evicted. If a user repeatedly links
17tmpfiles into a tmpfs, the limit will be hit (ENOSPC) even after they
18are deleted.
19
20Just skip the extra reservation from shmem_link() in this case: there's
21a sense in which this first link of a tmpfile is then cheaper than a
22hard link of another file, but the accounting works out, and there's
23still good limiting, so no need to do anything more complicated.
24
25Link: http://lkml.kernel.org/r/alpine.LSU.2.11.1902182134370.7035@eggly.anvils
26Fixes: f4e0c30c191 ("allow the temp files created by open() to be linked to")
27Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
28Signed-off-by: Hugh Dickins <hughd@google.com>
29Reported-by: Matej Kupljen <matej.kupljen@gmail.com>
30Acked-by: Al Viro <viro@zeniv.linux.org.uk>
31Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
32Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
33Signed-off-by: Sasha Levin <sashal@kernel.org>
34---
35 mm/shmem.c | 10 +++++++---
36 1 file changed, 7 insertions(+), 3 deletions(-)
37
38diff --git a/mm/shmem.c b/mm/shmem.c
39index 6c10f1d92251..9b78c04f532b 100644
40--- a/mm/shmem.c
41+++ b/mm/shmem.c
42@@ -3102,10 +3102,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(inode);
60--
612.19.1
62