tmpfs-idr.patch 6.83 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
SPDX-License-Identifier: GPL-2.0
J. R. Okajima's avatar
J. R. Okajima committed
2

J. R. Okajima's avatar
J. R. Okajima committed
3
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
J. R. Okajima's avatar
J. R. Okajima committed
4
index d82b6f396588..ff9c7acfedc8 100644
J. R. Okajima's avatar
J. R. Okajima committed
5
6
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
J. R. Okajima's avatar
J. R. Okajima committed
7
@@ -27,10 +27,13 @@ struct shmem_inode_info {
J. R. Okajima's avatar
J. R. Okajima committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 };
 
 struct shmem_sb_info {
+	struct mutex idr_lock;
+	bool idr_nouse;
+	struct idr idr;		    /* manages inode-number */
 	unsigned long max_blocks;   /* How many blocks are allowed */
 	struct percpu_counter used_blocks;  /* How many are allocated */
-	unsigned long max_inodes;   /* How many inodes are allowed */
-	unsigned long free_inodes;  /* How many are left for allocation */
+	int max_inodes;		    /* How many inodes are allowed */
+	int free_inodes;	    /* How many are left for allocation */
 	spinlock_t stat_lock;	    /* Serialize shmem_sb_info changes */
 	umode_t mode;		    /* Mount mode for root directory */
 	unsigned char huge;	    /* Whether to try for hugepages */
diff --git a/mm/shmem.c b/mm/shmem.c
J. R. Okajima's avatar
J. R. Okajima committed
24
index 7c6b6d8f6c39..2990e627cf6c 100644
J. R. Okajima's avatar
J. R. Okajima committed
25
26
--- a/mm/shmem.c
+++ b/mm/shmem.c
J. R. Okajima's avatar
J. R. Okajima committed
27
@@ -109,7 +109,7 @@ struct shmem_falloc {
J. R. Okajima's avatar
J. R. Okajima committed
28
29
30
31
32
33
34
35
 
 struct shmem_options {
 	unsigned long long blocks;
-	unsigned long long inodes;
+	int inodes;
 	struct mempolicy *mpol;
 	kuid_t uid;
 	kgid_t gid;
J. R. Okajima's avatar
J. R. Okajima committed
36
@@ -129,11 +129,14 @@ static unsigned long shmem_default_max_blocks(void)
J. R. Okajima's avatar
J. R. Okajima committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 	return totalram_pages() / 2;
 }
 
-static unsigned long shmem_default_max_inodes(void)
+static int shmem_default_max_inodes(void)
 {
 	unsigned long nr_pages = totalram_pages();
+	unsigned long ul;
 
-	return min(nr_pages - totalhigh_pages(), nr_pages / 2);
+	ul = INT_MAX;
+	ul = min3(ul, nr_pages - totalhigh_pages(), nr_pages / 2);
+	return ul;
 }
 #endif
 
J. R. Okajima's avatar
J. R. Okajima committed
53
@@ -1178,6 +1181,11 @@ static void shmem_evict_inode(struct inode *inode)
J. R. Okajima's avatar
J. R. Okajima committed
54
55
56
57
58
59
60
61
62
63
64
 
 	simple_xattrs_free(&info->xattrs);
 	WARN_ON(inode->i_blocks);
+	if (!sbinfo->idr_nouse && inode->i_ino) {
+		mutex_lock(&sbinfo->idr_lock);
+		idr_remove(&sbinfo->idr, inode->i_ino);
+		mutex_unlock(&sbinfo->idr_lock);
+	}
 	shmem_free_inode(inode->i_sb);
 	clear_inode(inode);
 }
J. R. Okajima's avatar
J. R. Okajima committed
65
@@ -2302,7 +2310,6 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
J. R. Okajima's avatar
J. R. Okajima committed
66
67
68
 
 	inode = new_inode(sb);
 	if (inode) {
J. R. Okajima's avatar
J. R. Okajima committed
69
-		inode->i_ino = ino;
J. R. Okajima's avatar
J. R. Okajima committed
70
71
72
 		inode_init_owner(inode, dir, mode);
 		inode->i_blocks = 0;
 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
J. R. Okajima's avatar
J. R. Okajima committed
73
@@ -2346,6 +2353,25 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
J. R. Okajima's avatar
J. R. Okajima committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 			break;
 		}
 
+		if (!sbinfo->idr_nouse) {
+			/* inum 0 and 1 are unused */
+			mutex_lock(&sbinfo->idr_lock);
+			ino = idr_alloc(&sbinfo->idr, inode, 2, INT_MAX,
+					GFP_NOFS);
+			if (ino > 0) {
+				inode->i_ino = ino;
+				mutex_unlock(&sbinfo->idr_lock);
+				__insert_inode_hash(inode, inode->i_ino);
+			} else {
+				inode->i_ino = 0;
+				mutex_unlock(&sbinfo->idr_lock);
+				iput(inode);
+				/* shmem_free_inode() will be called */
+				inode = NULL;
+			}
+		} else
J. R. Okajima's avatar
J. R. Okajima committed
94
+			inode->i_ino = ino;
J. R. Okajima's avatar
J. R. Okajima committed
95
96
97
98
+
 		lockdep_annotate_inode_mutex_key(inode);
 	} else
 		shmem_free_inode(sb);
J. R. Okajima's avatar
J. R. Okajima committed
99
@@ -3334,8 +3360,7 @@ static struct dentry *shmem_get_parent(struct dentry *child)
J. R. Okajima's avatar
J. R. Okajima committed
100
101
102
103
104
105
106
107
108
 static int shmem_match(struct inode *ino, void *vfh)
 {
 	__u32 *fh = vfh;
-	__u64 inum = fh[2];
-	inum = (inum << 32) | fh[1];
+	__u64 inum = fh[1];
 	return ino->i_ino == inum && fh[0] == ino->i_generation;
 }
 
J. R. Okajima's avatar
J. R. Okajima committed
109
@@ -3355,14 +3380,11 @@ static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
J. R. Okajima's avatar
J. R. Okajima committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 	struct dentry *dentry = NULL;
 	u64 inum;
 
-	if (fh_len < 3)
+	if (fh_len < 2)
 		return NULL;
 
-	inum = fid->raw[2];
-	inum = (inum << 32) | fid->raw[1];
-
-	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
-			shmem_match, fid->raw);
+	inum = fid->raw[1];
+	inode = ilookup5(sb, inum, shmem_match, fid->raw);
 	if (inode) {
 		dentry = shmem_find_alias(inode);
 		iput(inode);
J. R. Okajima's avatar
J. R. Okajima committed
127
@@ -3374,30 +3396,15 @@ static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
J. R. Okajima's avatar
J. R. Okajima committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
 				struct inode *parent)
 {
-	if (*len < 3) {
-		*len = 3;
+	if (*len < 2) {
+		*len = 2;
 		return FILEID_INVALID;
 	}
 
-	if (inode_unhashed(inode)) {
-		/* Unfortunately insert_inode_hash is not idempotent,
-		 * so as we hash inodes here rather than at creation
-		 * time, we need a lock to ensure we only try
-		 * to do it once
-		 */
-		static DEFINE_SPINLOCK(lock);
-		spin_lock(&lock);
-		if (inode_unhashed(inode))
-			__insert_inode_hash(inode,
-					    inode->i_ino + inode->i_generation);
-		spin_unlock(&lock);
-	}
-
 	fh[0] = inode->i_generation;
 	fh[1] = inode->i_ino;
-	fh[2] = ((__u64)inode->i_ino) >> 32;
 
-	*len = 3;
+	*len = 2;
 	return 1;
 }
 
J. R. Okajima's avatar
J. R. Okajima committed
161
@@ -3476,7 +3483,7 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
J. R. Okajima's avatar
J. R. Okajima committed
162
163
164
165
166
167
168
169
 		break;
 	case Opt_nr_inodes:
 		ctx->inodes = memparse(param->string, &rest);
-		if (*rest)
+		if (*rest || ctx->inodes < 2)
 			goto bad_value;
 		ctx->seen |= SHMEM_SEEN_INODES;
 		break;
J. R. Okajima's avatar
J. R. Okajima committed
170
@@ -3586,7 +3593,7 @@ static int shmem_reconfigure(struct fs_context *fc)
J. R. Okajima's avatar
J. R. Okajima committed
171
 {
J. R. Okajima's avatar
J. R. Okajima committed
172
173
 	struct shmem_options *ctx = fc->fs_private;
 	struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb);
J. R. Okajima's avatar
J. R. Okajima committed
174
175
-	unsigned long inodes;
+	int inodes;
J. R. Okajima's avatar
J. R. Okajima committed
176
 	const char *err;
J. R. Okajima's avatar
J. R. Okajima committed
177
 
J. R. Okajima's avatar
J. R. Okajima committed
178
 	spin_lock(&sbinfo->stat_lock);
J. R. Okajima's avatar
J. R. Okajima committed
179
@@ -3653,7 +3660,7 @@ static int shmem_show_options(struct seq_file *seq, struct dentry *root)
J. R. Okajima's avatar
J. R. Okajima committed
180
181
182
183
184
185
186
187
 		seq_printf(seq, ",size=%luk",
 			sbinfo->max_blocks << (PAGE_SHIFT - 10));
 	if (sbinfo->max_inodes != shmem_default_max_inodes())
-		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
+		seq_printf(seq, ",nr_inodes=%d", sbinfo->max_inodes);
 	if (sbinfo->mode != (0777 | S_ISVTX))
 		seq_printf(seq, ",mode=%03ho", sbinfo->mode);
 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
J. R. Okajima's avatar
J. R. Okajima committed
188
@@ -3700,6 +3707,8 @@ static void shmem_put_super(struct super_block *sb)
J. R. Okajima's avatar
J. R. Okajima committed
189
190
191
192
193
 {
 	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
 
+	if (!sbinfo->idr_nouse)
+		idr_destroy(&sbinfo->idr);
J. R. Okajima's avatar
J. R. Okajima committed
194
 	free_percpu(sbinfo->ino_batch);
J. R. Okajima's avatar
J. R. Okajima committed
195
196
 	percpu_counter_destroy(&sbinfo->used_blocks);
 	mpol_put(sbinfo->mpol);
J. R. Okajima's avatar
J. R. Okajima committed
197
@@ -3743,6 +3752,8 @@ static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
J. R. Okajima's avatar
J. R. Okajima committed
198
199
200
 #else
 	sb->s_flags |= SB_NOUSER;
 #endif
J. R. Okajima's avatar
J. R. Okajima committed
201
202
+	mutex_init(&sbinfo->idr_lock);
+	idr_init(&sbinfo->idr);
J. R. Okajima's avatar
J. R. Okajima committed
203
204
 	sbinfo->max_blocks = ctx->blocks;
 	sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes;
J. R. Okajima's avatar
J. R. Okajima committed
205
 	if (sb->s_flags & SB_KERNMOUNT) {
J. R. Okajima's avatar
J. R. Okajima committed
206
@@ -3860,6 +3871,15 @@ static void shmem_destroy_inodecache(void)
J. R. Okajima's avatar
J. R. Okajima committed
207
208
209
210
211
212
213
214
215
216
217
218
 	kmem_cache_destroy(shmem_inode_cachep);
 }
 
+static __init void shmem_no_idr(struct super_block *sb)
+{
+	struct shmem_sb_info *sbinfo;
+
+	sbinfo = SHMEM_SB(sb);
+	sbinfo->idr_nouse = true;
+	idr_destroy(&sbinfo->idr);
+}
+
J. R. Okajima's avatar
J. R. Okajima committed
219
 const struct address_space_operations shmem_aops = {
J. R. Okajima's avatar
J. R. Okajima committed
220
221
 	.writepage	= shmem_writepage,
 	.set_page_dirty	= __set_page_dirty_no_writeback,
J. R. Okajima's avatar
J. R. Okajima committed
222
@@ -4001,6 +4021,7 @@ int __init shmem_init(void)
J. R. Okajima's avatar
J. R. Okajima committed
223
224
225
226
227
 		pr_err("Could not kern_mount tmpfs\n");
 		goto out1;
 	}
+	shmem_no_idr(shm_mnt->mnt_sb);
 
J. R. Okajima's avatar
J. R. Okajima committed
228
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
J. R. Okajima's avatar
J. R. Okajima committed
229
 	if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)