Skip to content

Commit 8a05a8c

Browse files
amir73ilbrauner
authored andcommitted
fs: move kmem_cache_zalloc() into alloc_empty_file*() helpers
Use a common helper init_file() instead of __alloc_file() for alloc_empty_file*() helpers and improrve the documentation. This is needed for a follow up patch that allocates a backing_file container. Suggested-by: Christoph Hellwig <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent cbb0b9d commit 8a05a8c

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

fs/file_table.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,15 @@ static int __init init_fs_stat_sysctls(void)
131131
fs_initcall(init_fs_stat_sysctls);
132132
#endif
133133

134-
static struct file *__alloc_file(int flags, const struct cred *cred)
134+
static int init_file(struct file *f, int flags, const struct cred *cred)
135135
{
136-
struct file *f;
137136
int error;
138137

139-
f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
140-
if (unlikely(!f))
141-
return ERR_PTR(-ENOMEM);
142-
143138
f->f_cred = get_cred(cred);
144139
error = security_file_alloc(f);
145140
if (unlikely(error)) {
146141
file_free_rcu(&f->f_rcuhead);
147-
return ERR_PTR(error);
142+
return error;
148143
}
149144

150145
atomic_long_set(&f->f_count, 1);
@@ -155,7 +150,7 @@ static struct file *__alloc_file(int flags, const struct cred *cred)
155150
f->f_mode = OPEN_FMODE(flags);
156151
/* f->f_version: 0 */
157152

158-
return f;
153+
return 0;
159154
}
160155

161156
/* Find an unused file structure and return a pointer to it.
@@ -172,6 +167,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
172167
{
173168
static long old_max;
174169
struct file *f;
170+
int error;
175171

176172
/*
177173
* Privileged users can go above max_files
@@ -185,9 +181,15 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
185181
goto over;
186182
}
187183

188-
f = __alloc_file(flags, cred);
189-
if (!IS_ERR(f))
190-
percpu_counter_inc(&nr_files);
184+
f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
185+
if (unlikely(!f))
186+
return ERR_PTR(-ENOMEM);
187+
188+
error = init_file(f, flags, cred);
189+
if (unlikely(error))
190+
return ERR_PTR(error);
191+
192+
percpu_counter_inc(&nr_files);
191193

192194
return f;
193195

@@ -203,14 +205,23 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
203205
/*
204206
* Variant of alloc_empty_file() that doesn't check and modify nr_files.
205207
*
206-
* Should not be used unless there's a very good reason to do so.
208+
* This is only for kernel internal use, and the allocate file must not be
209+
* installed into file tables or such.
207210
*/
208211
struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
209212
{
210-
struct file *f = __alloc_file(flags, cred);
213+
struct file *f;
214+
int error;
215+
216+
f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
217+
if (unlikely(!f))
218+
return ERR_PTR(-ENOMEM);
219+
220+
error = init_file(f, flags, cred);
221+
if (unlikely(error))
222+
return ERR_PTR(error);
211223

212-
if (!IS_ERR(f))
213-
f->f_mode |= FMODE_NOACCOUNT;
224+
f->f_mode |= FMODE_NOACCOUNT;
214225

215226
return f;
216227
}

0 commit comments

Comments
 (0)