drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / file_table.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/file_table.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
6 */
7
8#include <linux/string.h>
9#include <linux/slab.h>
10#include <linux/file.h>
9f3acc31 11#include <linux/fdtable.h>
1da177e4
LT
12#include <linux/init.h>
13#include <linux/module.h>
1da177e4
LT
14#include <linux/fs.h>
15#include <linux/security.h>
16#include <linux/eventpoll.h>
ab2af1f5 17#include <linux/rcupdate.h>
1da177e4 18#include <linux/mount.h>
16f7e0fe 19#include <linux/capability.h>
1da177e4 20#include <linux/cdev.h>
0eeca283 21#include <linux/fsnotify.h>
529bf6be 22#include <linux/sysctl.h>
6416ccb7 23#include <linux/lglock.h>
529bf6be 24#include <linux/percpu_counter.h>
6416ccb7 25#include <linux/percpu.h>
4a9d4b02
AV
26#include <linux/hardirq.h>
27#include <linux/task_work.h>
0552f879 28#include <linux/ima.h>
529bf6be 29
60063497 30#include <linux/atomic.h>
1da177e4 31
e81e3f4d
EP
32#include "internal.h"
33
6fa3eb70
S
34#define FILE_OVER_MAX
35#ifdef FILE_OVER_MAX
36extern void fd_show_open_files(pid_t pid, struct files_struct *files, struct fdtable *fdt);
37#endif
38
1da177e4
LT
39/* sysctl tunables... */
40struct files_stat_struct files_stat = {
41 .max_files = NR_FILE
42};
43
b6b3fdea
ED
44/* SLAB cache for file structures */
45static struct kmem_cache *filp_cachep __read_mostly;
46
529bf6be 47static struct percpu_counter nr_files __cacheline_aligned_in_smp;
1da177e4 48
5c33b183 49static void file_free_rcu(struct rcu_head *head)
1da177e4 50{
d76b0d9b
DH
51 struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
52
53 put_cred(f->f_cred);
529bf6be 54 kmem_cache_free(filp_cachep, f);
1da177e4
LT
55}
56
529bf6be 57static inline void file_free(struct file *f)
1da177e4 58{
529bf6be 59 percpu_counter_dec(&nr_files);
ad775f5a 60 file_check_state(f);
529bf6be 61 call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
1da177e4
LT
62}
63
529bf6be
DS
64/*
65 * Return the total number of open files in the system
66 */
518de9b3 67static long get_nr_files(void)
1da177e4 68{
529bf6be 69 return percpu_counter_read_positive(&nr_files);
1da177e4
LT
70}
71
529bf6be
DS
72/*
73 * Return the maximum number of open files in the system
74 */
518de9b3 75unsigned long get_max_files(void)
ab2af1f5 76{
529bf6be 77 return files_stat.max_files;
ab2af1f5 78}
529bf6be
DS
79EXPORT_SYMBOL_GPL(get_max_files);
80
81/*
82 * Handle nr_files sysctl
83 */
84#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
8d65af78 85int proc_nr_files(ctl_table *table, int write,
529bf6be
DS
86 void __user *buffer, size_t *lenp, loff_t *ppos)
87{
88 files_stat.nr_files = get_nr_files();
518de9b3 89 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
529bf6be
DS
90}
91#else
8d65af78 92int proc_nr_files(ctl_table *table, int write,
529bf6be
DS
93 void __user *buffer, size_t *lenp, loff_t *ppos)
94{
95 return -ENOSYS;
96}
97#endif
ab2af1f5 98
1da177e4 99/* Find an unused file structure and return a pointer to it.
1afc99be
AV
100 * Returns an error pointer if some error happend e.g. we over file
101 * structures limit, run out of memory or operation is not permitted.
430e285e
DH
102 *
103 * Be very careful using this. You are responsible for
104 * getting write access to any mount that you might assign
105 * to this filp, if it is opened for write. If this is not
106 * done, you will imbalance int the mount's writer count
107 * and a warning at __fput() time.
1da177e4
LT
108 */
109struct file *get_empty_filp(void)
110{
86a264ab 111 const struct cred *cred = current_cred();
518de9b3 112 static long old_max;
1afc99be
AV
113 struct file *f;
114 int error;
1da177e4
LT
115
116 /*
117 * Privileged users can go above max_files
118 */
529bf6be
DS
119 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
120 /*
121 * percpu_counters are inaccurate. Do an expensive check before
122 * we go and fail.
123 */
52d9f3b4 124 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
529bf6be
DS
125 goto over;
126 }
af4d2ecb 127
4975e45f 128 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
1afc99be
AV
129 if (unlikely(!f))
130 return ERR_PTR(-ENOMEM);
af4d2ecb 131
529bf6be 132 percpu_counter_inc(&nr_files);
78d29788 133 f->f_cred = get_cred(cred);
1afc99be
AV
134 error = security_file_alloc(f);
135 if (unlikely(error)) {
136 file_free(f);
137 return ERR_PTR(error);
138 }
1da177e4 139
516e0cc5 140 atomic_long_set(&f->f_count, 1);
af4d2ecb 141 rwlock_init(&f->f_owner.lock);
68499914 142 spin_lock_init(&f->f_lock);
5a6b7951 143 eventpoll_init_file(f);
af4d2ecb 144 /* f->f_version: 0 */
af4d2ecb
KK
145 return f;
146
147over:
1da177e4 148 /* Ran out of filps - report that */
529bf6be 149 if (get_nr_files() > old_max) {
6fa3eb70
S
150#ifdef FILE_OVER_MAX
151 static int fd_dump_all_files = 0;
152 if(!fd_dump_all_files) {
153 struct task_struct *p;
154 pr_debug("[FD_LEAK](PID:%d)files %ld over old_max:%ld", current->pid, get_nr_files(), old_max);
155 for_each_process(p) {
156 pid_t pid = p->pid;
157 struct files_struct *files = p->files;
158 struct fdtable *fdt = files_fdtable(files);
159 if(files && fdt) {
160 fd_show_open_files(pid, files, fdt);
161 }
162 }
163 fd_dump_all_files = 0x1;
164 }
165#endif
518de9b3 166 pr_info("VFS: file-max limit %lu reached\n", get_max_files());
529bf6be 167 old_max = get_nr_files();
1da177e4 168 }
1afc99be 169 return ERR_PTR(-ENFILE);
1da177e4
LT
170}
171
ce8d2cdf
DH
172/**
173 * alloc_file - allocate and initialize a 'struct file'
174 * @mnt: the vfsmount on which the file will reside
175 * @dentry: the dentry representing the new file
176 * @mode: the mode with which the new file will be opened
177 * @fop: the 'struct file_operations' for the new file
178 *
179 * Use this instead of get_empty_filp() to get a new
180 * 'struct file'. Do so because of the same initialization
181 * pitfalls reasons listed for init_file(). This is a
182 * preferred interface to using init_file().
183 *
184 * If all the callers of init_file() are eliminated, its
185 * code should be moved into this function.
186 */
2c48b9c4
AV
187struct file *alloc_file(struct path *path, fmode_t mode,
188 const struct file_operations *fop)
ce8d2cdf
DH
189{
190 struct file *file;
ce8d2cdf
DH
191
192 file = get_empty_filp();
1afc99be 193 if (IS_ERR(file))
39b65252 194 return file;
ce8d2cdf 195
2c48b9c4 196 file->f_path = *path;
dd37978c 197 file->f_inode = path->dentry->d_inode;
2c48b9c4 198 file->f_mapping = path->dentry->d_inode->i_mapping;
ce8d2cdf
DH
199 file->f_mode = mode;
200 file->f_op = fop;
4a3fd211
DH
201
202 /*
203 * These mounts don't really matter in practice
204 * for r/o bind mounts. They aren't userspace-
205 * visible. We do this for consistency, and so
206 * that we can do debugging checks at __fput()
207 */
2c48b9c4 208 if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
ad775f5a 209 file_take_write(file);
385e3ed4 210 WARN_ON(mnt_clone_write(path->mnt));
4a3fd211 211 }
890275b5
MZ
212 if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
213 i_readcount_inc(path->dentry->d_inode);
3d1e4631 214 return file;
ce8d2cdf 215}
73efc468 216EXPORT_SYMBOL(alloc_file);
ce8d2cdf 217
aceaf78d
DH
218/**
219 * drop_file_write_access - give up ability to write to a file
220 * @file: the file to which we will stop writing
221 *
222 * This is a central place which will give up the ability
223 * to write to @file, along with access to write through
224 * its vfsmount.
225 */
b57ce969 226static void drop_file_write_access(struct file *file)
aceaf78d 227{
4a3fd211 228 struct vfsmount *mnt = file->f_path.mnt;
aceaf78d
DH
229 struct dentry *dentry = file->f_path.dentry;
230 struct inode *inode = dentry->d_inode;
231
ad775f5a
DH
232 if (special_file(inode->i_mode))
233 return;
68ad89c7
AV
234
235 put_write_access(inode);
ad775f5a
DH
236 if (file_check_writeable(file) != 0)
237 return;
eb04c282 238 __mnt_drop_write(mnt);
ad775f5a 239 file_release_write(file);
aceaf78d 240}
aceaf78d 241
d7065da0 242/* the real guts of fput() - releasing the last reference to file
1da177e4 243 */
d7065da0 244static void __fput(struct file *file)
1da177e4 245{
0f7fc9e4
JJS
246 struct dentry *dentry = file->f_path.dentry;
247 struct vfsmount *mnt = file->f_path.mnt;
1da177e4
LT
248 struct inode *inode = dentry->d_inode;
249
250 might_sleep();
0eeca283
RL
251
252 fsnotify_close(file);
1da177e4
LT
253 /*
254 * The function eventpoll_release() should be the first called
255 * in the file cleanup chain.
256 */
257 eventpoll_release(file);
258 locks_remove_flock(file);
259
233e70f4
AV
260 if (unlikely(file->f_flags & FASYNC)) {
261 if (file->f_op && file->f_op->fasync)
262 file->f_op->fasync(-1, file, 0);
263 }
4199d35c 264 ima_file_free(file);
1da177e4
LT
265 if (file->f_op && file->f_op->release)
266 file->f_op->release(inode, file);
267 security_file_free(file);
60ed8cf7
MS
268 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
269 !(file->f_mode & FMODE_PATH))) {
1da177e4 270 cdev_put(inode->i_cdev);
60ed8cf7 271 }
1da177e4 272 fops_put(file->f_op);
609d7fa9 273 put_pid(file->f_owner.pid);
890275b5
MZ
274 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
275 i_readcount_dec(inode);
aceaf78d
DH
276 if (file->f_mode & FMODE_WRITE)
277 drop_file_write_access(file);
0f7fc9e4
JJS
278 file->f_path.dentry = NULL;
279 file->f_path.mnt = NULL;
dd37978c 280 file->f_inode = NULL;
1da177e4
LT
281 file_free(file);
282 dput(dentry);
283 mntput(mnt);
284}
285
2a7abb5a 286static LLIST_HEAD(delayed_fput_list);
4a9d4b02
AV
287static void delayed_fput(struct work_struct *unused)
288{
2a7abb5a
ON
289 struct llist_node *node = llist_del_all(&delayed_fput_list);
290 struct llist_node *next;
291
292 for (; node; node = next) {
293 next = llist_next(node);
294 __fput(llist_entry(node, struct file, f_u.fu_llist));
4a9d4b02
AV
295 }
296}
297
298static void ____fput(struct callback_head *work)
299{
300 __fput(container_of(work, struct file, f_u.fu_rcuhead));
301}
302
303/*
304 * If kernel thread really needs to have the final fput() it has done
305 * to complete, call this. The only user right now is the boot - we
306 * *do* need to make sure our writes to binaries on initramfs has
307 * not left us with opened struct file waiting for __fput() - execve()
308 * won't work without that. Please, don't add more callers without
309 * very good reasons; in particular, never call that with locks
310 * held and never call that from a thread that might need to do
311 * some work on any kind of umount.
312 */
313void flush_delayed_fput(void)
314{
315 delayed_fput(NULL);
316}
317
318static DECLARE_WORK(delayed_fput_work, delayed_fput);
319
d7065da0
AV
320void fput(struct file *file)
321{
4a9d4b02
AV
322 if (atomic_long_dec_and_test(&file->f_count)) {
323 struct task_struct *task = current;
e7b2c406 324
e7b2c406
ON
325 if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
326 init_task_work(&file->f_u.fu_rcuhead, ____fput);
327 if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
328 return;
4a9d4b02 329 }
2a7abb5a
ON
330
331 if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
332 schedule_work(&delayed_fput_work);
4a9d4b02
AV
333 }
334}
335
336/*
337 * synchronous analog of fput(); for kernel threads that might be needed
338 * in some umount() (and thus can't use flush_delayed_fput() without
339 * risking deadlocks), need to wait for completion of __fput() and know
340 * for this specific struct file it won't involve anything that would
341 * need them. Use only if you really need it - at the very least,
342 * don't blindly convert fput() by kernel thread to that.
343 */
344void __fput_sync(struct file *file)
345{
346 if (atomic_long_dec_and_test(&file->f_count)) {
347 struct task_struct *task = current;
4a9d4b02 348 BUG_ON(!(task->flags & PF_KTHREAD));
d7065da0 349 __fput(file);
4a9d4b02 350 }
d7065da0
AV
351}
352
353EXPORT_SYMBOL(fput);
354
1da177e4
LT
355void put_filp(struct file *file)
356{
516e0cc5 357 if (atomic_long_dec_and_test(&file->f_count)) {
1da177e4 358 security_file_free(file);
1da177e4
LT
359 file_free(file);
360 }
361}
362
1da177e4
LT
363void __init files_init(unsigned long mempages)
364{
518de9b3 365 unsigned long n;
b6b3fdea
ED
366
367 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
368 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
369
370 /*
371 * One file with associated inode and dcache is very roughly 1K.
1da177e4
LT
372 * Per default don't use more than 10% of our memory for files.
373 */
374
375 n = (mempages * (PAGE_SIZE / 1024)) / 10;
518de9b3 376 files_stat.max_files = max_t(unsigned long, n, NR_FILE);
ab2af1f5 377 files_defer_init();
0216bfcf 378 percpu_counter_init(&nr_files, 0);
1da177e4 379}