drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / file.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/file.c
3 *
4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
5 *
6 * Manage the dynamic fd arrays in the process files_struct.
7 */
8
fe17f22d 9#include <linux/syscalls.h>
630d9c47 10#include <linux/export.h>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/mm.h>
6d4831c2 13#include <linux/mmzone.h>
1da177e4 14#include <linux/time.h>
d43c36dc 15#include <linux/sched.h>
1da177e4
LT
16#include <linux/slab.h>
17#include <linux/vmalloc.h>
18#include <linux/file.h>
9f3acc31 19#include <linux/fdtable.h>
1da177e4 20#include <linux/bitops.h>
ab2af1f5
DS
21#include <linux/interrupt.h>
22#include <linux/spinlock.h>
23#include <linux/rcupdate.h>
24#include <linux/workqueue.h>
25
6fa3eb70
S
26#define FD_OVER_CHECK
27
9cfe015a 28int sysctl_nr_open __read_mostly = 1024*1024;
eceea0b3
AV
29int sysctl_nr_open_min = BITS_PER_LONG;
30int sysctl_nr_open_max = 1024 * 1024; /* raised later */
9cfe015a 31
1fd36adc 32static void *alloc_fdmem(size_t size)
1da177e4 33{
6d4831c2
AM
34 /*
35 * Very large allocations can stress page reclaim, so fall back to
36 * vmalloc() if the allocation size will be considered "large" by the VM.
37 */
38 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
434672c0 39 void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY);
6d4831c2
AM
40 if (data != NULL)
41 return data;
42 }
a892e2d7 43 return vmalloc(size);
1da177e4
LT
44}
45
a892e2d7 46static void free_fdmem(void *ptr)
1da177e4 47{
a892e2d7 48 is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
1da177e4
LT
49}
50
a892e2d7 51static void __free_fdtable(struct fdtable *fdt)
1da177e4 52{
a892e2d7
CG
53 free_fdmem(fdt->fd);
54 free_fdmem(fdt->open_fds);
55 kfree(fdt);
ab2af1f5 56}
1da177e4 57
7cf4dc3c 58static void free_fdtable_rcu(struct rcu_head *rcu)
ab2af1f5 59{
ac3e3c5b 60 __free_fdtable(container_of(rcu, struct fdtable, rcu));
ab2af1f5
DS
61}
62
ab2af1f5
DS
63/*
64 * Expand the fdset in the files_struct. Called with the files spinlock
65 * held for write.
66 */
5466b456 67static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
ab2af1f5 68{
5466b456 69 unsigned int cpy, set;
ab2af1f5 70
5466b456 71 BUG_ON(nfdt->max_fds < ofdt->max_fds);
5466b456
VL
72
73 cpy = ofdt->max_fds * sizeof(struct file *);
74 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
75 memcpy(nfdt->fd, ofdt->fd, cpy);
76 memset((char *)(nfdt->fd) + cpy, 0, set);
77
78 cpy = ofdt->max_fds / BITS_PER_BYTE;
79 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
80 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
81 memset((char *)(nfdt->open_fds) + cpy, 0, set);
82 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
83 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
1da177e4
LT
84}
85
5466b456 86static struct fdtable * alloc_fdtable(unsigned int nr)
1da177e4 87{
5466b456 88 struct fdtable *fdt;
1fd36adc 89 void *data;
1da177e4 90
ab2af1f5 91 /*
5466b456
VL
92 * Figure out how many fds we actually want to support in this fdtable.
93 * Allocation steps are keyed to the size of the fdarray, since it
94 * grows far faster than any of the other dynamic data. We try to fit
95 * the fdarray into comfortable page-tuned chunks: starting at 1024B
96 * and growing in powers of two from there on.
ab2af1f5 97 */
5466b456
VL
98 nr /= (1024 / sizeof(struct file *));
99 nr = roundup_pow_of_two(nr + 1);
100 nr *= (1024 / sizeof(struct file *));
5c598b34
AV
101 /*
102 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
103 * had been set lower between the check in expand_files() and here. Deal
104 * with that in caller, it's cheaper that way.
105 *
106 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
107 * bitmaps handling below becomes unpleasant, to put it mildly...
108 */
109 if (unlikely(nr > sysctl_nr_open))
110 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
bbea9f69 111
5466b456
VL
112 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
113 if (!fdt)
bbea9f69 114 goto out;
5466b456
VL
115 fdt->max_fds = nr;
116 data = alloc_fdmem(nr * sizeof(struct file *));
117 if (!data)
118 goto out_fdt;
1fd36adc
DH
119 fdt->fd = data;
120
121 data = alloc_fdmem(max_t(size_t,
5466b456
VL
122 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
123 if (!data)
124 goto out_arr;
1fd36adc 125 fdt->open_fds = data;
5466b456 126 data += nr / BITS_PER_BYTE;
1fd36adc 127 fdt->close_on_exec = data;
5466b456 128
ab2af1f5 129 return fdt;
5466b456
VL
130
131out_arr:
a892e2d7 132 free_fdmem(fdt->fd);
5466b456 133out_fdt:
ab2af1f5 134 kfree(fdt);
5466b456 135out:
ab2af1f5
DS
136 return NULL;
137}
1da177e4 138
ab2af1f5 139/*
74d392aa
VL
140 * Expand the file descriptor table.
141 * This function will allocate a new fdtable and both fd array and fdset, of
142 * the given size.
143 * Return <0 error code on error; 1 on successful completion.
144 * The files->file_lock should be held on entry, and will be held on exit.
ab2af1f5
DS
145 */
146static int expand_fdtable(struct files_struct *files, int nr)
147 __releases(files->file_lock)
148 __acquires(files->file_lock)
149{
74d392aa 150 struct fdtable *new_fdt, *cur_fdt;
ab2af1f5
DS
151
152 spin_unlock(&files->file_lock);
74d392aa 153 new_fdt = alloc_fdtable(nr);
ab2af1f5 154 spin_lock(&files->file_lock);
74d392aa
VL
155 if (!new_fdt)
156 return -ENOMEM;
5c598b34
AV
157 /*
158 * extremely unlikely race - sysctl_nr_open decreased between the check in
159 * caller and alloc_fdtable(). Cheaper to catch it here...
160 */
161 if (unlikely(new_fdt->max_fds <= nr)) {
a892e2d7 162 __free_fdtable(new_fdt);
5c598b34
AV
163 return -EMFILE;
164 }
ab2af1f5 165 /*
74d392aa
VL
166 * Check again since another task may have expanded the fd table while
167 * we dropped the lock
ab2af1f5 168 */
74d392aa 169 cur_fdt = files_fdtable(files);
bbea9f69 170 if (nr >= cur_fdt->max_fds) {
74d392aa
VL
171 /* Continue as planned */
172 copy_fdtable(new_fdt, cur_fdt);
173 rcu_assign_pointer(files->fdt, new_fdt);
ac3e3c5b 174 if (cur_fdt != &files->fdtab)
1983e781 175 call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
ab2af1f5 176 } else {
74d392aa 177 /* Somebody else expanded, so undo our attempt */
a892e2d7 178 __free_fdtable(new_fdt);
ab2af1f5 179 }
74d392aa 180 return 1;
1da177e4
LT
181}
182
183/*
184 * Expand files.
74d392aa
VL
185 * This function will expand the file structures, if the requested size exceeds
186 * the current capacity and there is room for expansion.
187 * Return <0 error code on error; 0 when nothing done; 1 when files were
188 * expanded and execution may have blocked.
189 * The files->file_lock should be held on entry, and will be held on exit.
1da177e4 190 */
ad47bd72 191static int expand_files(struct files_struct *files, int nr)
1da177e4 192{
badf1662 193 struct fdtable *fdt;
1da177e4 194
badf1662 195 fdt = files_fdtable(files);
4e1e018e 196
74d392aa 197 /* Do we need to expand? */
bbea9f69 198 if (nr < fdt->max_fds)
74d392aa 199 return 0;
4e1e018e 200
74d392aa 201 /* Can we expand? */
9cfe015a 202 if (nr >= sysctl_nr_open)
74d392aa
VL
203 return -EMFILE;
204
205 /* All good, so we try */
206 return expand_fdtable(files, nr);
1da177e4 207}
ab2af1f5 208
b8318b01
AV
209static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
210{
211 __set_bit(fd, fdt->close_on_exec);
212}
213
214static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
215{
216 __clear_bit(fd, fdt->close_on_exec);
217}
218
219static inline void __set_open_fd(int fd, struct fdtable *fdt)
220{
221 __set_bit(fd, fdt->open_fds);
222}
223
224static inline void __clear_open_fd(int fd, struct fdtable *fdt)
225{
226 __clear_bit(fd, fdt->open_fds);
227}
228
02afc626
AV
229static int count_open_files(struct fdtable *fdt)
230{
231 int size = fdt->max_fds;
232 int i;
233
234 /* Find the last open fd */
1fd36adc
DH
235 for (i = size / BITS_PER_LONG; i > 0; ) {
236 if (fdt->open_fds[--i])
02afc626
AV
237 break;
238 }
1fd36adc 239 i = (i + 1) * BITS_PER_LONG;
02afc626
AV
240 return i;
241}
242
02afc626
AV
243/*
244 * Allocate a new files structure and copy contents from the
245 * passed in files structure.
246 * errorp will be valid only when the returned files_struct is NULL.
247 */
248struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
249{
250 struct files_struct *newf;
251 struct file **old_fds, **new_fds;
252 int open_files, size, i;
253 struct fdtable *old_fdt, *new_fdt;
254
255 *errorp = -ENOMEM;
afbec7ff 256 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
02afc626
AV
257 if (!newf)
258 goto out;
259
afbec7ff
AV
260 atomic_set(&newf->count, 1);
261
262 spin_lock_init(&newf->file_lock);
263 newf->next_fd = 0;
264 new_fdt = &newf->fdtab;
265 new_fdt->max_fds = NR_OPEN_DEFAULT;
1fd36adc
DH
266 new_fdt->close_on_exec = newf->close_on_exec_init;
267 new_fdt->open_fds = newf->open_fds_init;
afbec7ff 268 new_fdt->fd = &newf->fd_array[0];
afbec7ff 269
02afc626
AV
270 spin_lock(&oldf->file_lock);
271 old_fdt = files_fdtable(oldf);
02afc626
AV
272 open_files = count_open_files(old_fdt);
273
274 /*
275 * Check whether we need to allocate a larger fd array and fd set.
02afc626 276 */
adbecb12 277 while (unlikely(open_files > new_fdt->max_fds)) {
02afc626 278 spin_unlock(&oldf->file_lock);
9dec3c4d 279
a892e2d7
CG
280 if (new_fdt != &newf->fdtab)
281 __free_fdtable(new_fdt);
adbecb12 282
9dec3c4d
AV
283 new_fdt = alloc_fdtable(open_files - 1);
284 if (!new_fdt) {
285 *errorp = -ENOMEM;
286 goto out_release;
287 }
288
289 /* beyond sysctl_nr_open; nothing to do */
290 if (unlikely(new_fdt->max_fds < open_files)) {
a892e2d7 291 __free_fdtable(new_fdt);
9dec3c4d 292 *errorp = -EMFILE;
02afc626 293 goto out_release;
9dec3c4d 294 }
9dec3c4d 295
02afc626
AV
296 /*
297 * Reacquire the oldf lock and a pointer to its fd table
298 * who knows it may have a new bigger fd table. We need
299 * the latest pointer.
300 */
301 spin_lock(&oldf->file_lock);
302 old_fdt = files_fdtable(oldf);
adbecb12 303 open_files = count_open_files(old_fdt);
02afc626
AV
304 }
305
306 old_fds = old_fdt->fd;
307 new_fds = new_fdt->fd;
308
1fd36adc
DH
309 memcpy(new_fdt->open_fds, old_fdt->open_fds, open_files / 8);
310 memcpy(new_fdt->close_on_exec, old_fdt->close_on_exec, open_files / 8);
02afc626
AV
311
312 for (i = open_files; i != 0; i--) {
313 struct file *f = *old_fds++;
314 if (f) {
315 get_file(f);
316 } else {
317 /*
318 * The fd may be claimed in the fd bitmap but not yet
319 * instantiated in the files array if a sibling thread
320 * is partway through open(). So make sure that this
321 * fd is available to the new process.
322 */
1dce27c5 323 __clear_open_fd(open_files - i, new_fdt);
02afc626
AV
324 }
325 rcu_assign_pointer(*new_fds++, f);
326 }
327 spin_unlock(&oldf->file_lock);
328
329 /* compute the remainder to be cleared */
330 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
331
332 /* This is long word aligned thus could use a optimized version */
333 memset(new_fds, 0, size);
334
335 if (new_fdt->max_fds > open_files) {
1fd36adc
DH
336 int left = (new_fdt->max_fds - open_files) / 8;
337 int start = open_files / BITS_PER_LONG;
02afc626 338
1fd36adc
DH
339 memset(&new_fdt->open_fds[start], 0, left);
340 memset(&new_fdt->close_on_exec[start], 0, left);
02afc626
AV
341 }
342
afbec7ff
AV
343 rcu_assign_pointer(newf->fdt, new_fdt);
344
02afc626
AV
345 return newf;
346
347out_release:
348 kmem_cache_free(files_cachep, newf);
349out:
350 return NULL;
351}
352
7cf4dc3c
AV
353static void close_files(struct files_struct * files)
354{
355 int i, j;
356 struct fdtable *fdt;
357
358 j = 0;
359
360 /*
361 * It is safe to dereference the fd table without RCU or
362 * ->file_lock because this is the last reference to the
363 * files structure. But use RCU to shut RCU-lockdep up.
364 */
365 rcu_read_lock();
366 fdt = files_fdtable(files);
367 rcu_read_unlock();
368 for (;;) {
369 unsigned long set;
370 i = j * BITS_PER_LONG;
371 if (i >= fdt->max_fds)
372 break;
373 set = fdt->open_fds[j++];
374 while (set) {
375 if (set & 1) {
376 struct file * file = xchg(&fdt->fd[i], NULL);
377 if (file) {
378 filp_close(file, files);
379 cond_resched();
380 }
381 }
382 i++;
383 set >>= 1;
384 }
385 }
386}
387
388struct files_struct *get_files_struct(struct task_struct *task)
389{
390 struct files_struct *files;
391
392 task_lock(task);
393 files = task->files;
394 if (files)
395 atomic_inc(&files->count);
396 task_unlock(task);
397
398 return files;
399}
400
401void put_files_struct(struct files_struct *files)
402{
403 struct fdtable *fdt;
404
405 if (atomic_dec_and_test(&files->count)) {
406 close_files(files);
b9e02af0 407 /* not really needed, since nobody can see us */
7cf4dc3c
AV
408 rcu_read_lock();
409 fdt = files_fdtable(files);
7cf4dc3c 410 rcu_read_unlock();
b9e02af0
AV
411 /* free the arrays if they are not embedded */
412 if (fdt != &files->fdtab)
413 __free_fdtable(fdt);
414 kmem_cache_free(files_cachep, files);
7cf4dc3c
AV
415 }
416}
417
418void reset_files_struct(struct files_struct *files)
419{
420 struct task_struct *tsk = current;
421 struct files_struct *old;
422
423 old = tsk->files;
424 task_lock(tsk);
425 tsk->files = files;
426 task_unlock(tsk);
427 put_files_struct(old);
428}
429
430void exit_files(struct task_struct *tsk)
431{
432 struct files_struct * files = tsk->files;
433
434 if (files) {
435 task_lock(tsk);
436 tsk->files = NULL;
437 task_unlock(tsk);
438 put_files_struct(files);
439 }
440}
441
ab2af1f5
DS
442void __init files_defer_init(void)
443{
eceea0b3
AV
444 sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
445 -BITS_PER_LONG;
ab2af1f5 446}
f52111b1
AV
447
448struct files_struct init_files = {
449 .count = ATOMIC_INIT(1),
450 .fdt = &init_files.fdtab,
451 .fdtab = {
452 .max_fds = NR_OPEN_DEFAULT,
453 .fd = &init_files.fd_array[0],
1fd36adc
DH
454 .close_on_exec = init_files.close_on_exec_init,
455 .open_fds = init_files.open_fds_init,
f52111b1 456 },
eece09ec 457 .file_lock = __SPIN_LOCK_UNLOCKED(init_files.file_lock),
f52111b1 458};
1027abe8 459
6fa3eb70
S
460#ifdef FD_OVER_CHECK
461#define FD_CHECK_NAME_SIZE 256
462// Declare a radix tree to construct fd set tree
463static RADIX_TREE(over_fd_tree, GFP_KERNEL);
464static LIST_HEAD(fd_listhead);
465static DEFINE_MUTEX(over_fd_mutex);
466struct over_fd_entry
467{
468 int num_of_fd;
469 char name[FD_CHECK_NAME_SIZE];
470 int hash;
471 struct list_head fd_link;
472};
473
474/*
475* Get File Name from FD value
476*/
477long get_file_name_from_fd(struct files_struct *files, int fd, int procid, struct over_fd_entry *res_name)
478{
479 char *tmp;
480 char *pathname;
481 struct file *file;
482 struct path path;
483 spin_lock(&files->file_lock);
484 file = fget(fd);
485 if (!file) {
486 spin_unlock(&files->file_lock);
487 return (long)NULL;
488 }
489 path_get(&file->f_path);
490 path = file->f_path;
491 fput(file);
492 spin_unlock(&files->file_lock);
493 tmp = (char *)__get_free_page(GFP_TEMPORARY);
494 if (!tmp) {
495 return (long)NULL;
496 }
497 pathname = d_path(&path, tmp, PAGE_SIZE);
498
499 path_put(&path);
500 if (IS_ERR(pathname))
501 {
502 free_page((unsigned long)tmp);
503 return PTR_ERR(pathname);
504 } /* do something here with pathname */
505 if(pathname!=NULL)
506 {
507 strncpy(res_name->name, pathname, FD_CHECK_NAME_SIZE - 1);
508 }
509 free_page((unsigned long)tmp);
510 return 1;
511}
512
513unsigned int get_hash(char *name)
514{
515 return full_name_hash(name, strlen(name));
516}
517
518static struct over_fd_entry* fd_lookup(unsigned int hash)
519{
520 return radix_tree_lookup(&over_fd_tree, hash);
521}
522
523static void fd_insert(struct over_fd_entry *entry)
524{
525 unsigned int hash = get_hash(entry->name);
526 struct over_fd_entry *find_entry = fd_lookup(hash);
527
528 if(!find_entry) // Can't find the element, just add the element
529 {
530 entry->num_of_fd = 1;
531 entry->hash = hash;
532 list_add_tail(&entry->fd_link, &fd_listhead);
533 radix_tree_insert(&over_fd_tree, hash, (void *)entry);
534 }
535 else // Cover the original element
536 {
537 find_entry->num_of_fd = find_entry->num_of_fd+1;
538 kfree(entry);
539 }
540}
541
542static void fd_delete(unsigned int hash)
543{
544 radix_tree_delete(&over_fd_tree, hash);
545}
546
547void fd_show_open_files(pid_t pid, struct files_struct *files, struct fdtable *fdt)
548{
549 int i=0;
550 struct over_fd_entry *lentry;
551 long result;
552 int num_of_entry;
553 int sum_fds_of_pid = 0;
554
555 mutex_lock(&over_fd_mutex);
556 //printk(KERN_ERR "(PID:%d)Max FD Number:%d", current->pid, fdt->max_fds);
557 for(i=0; i<fdt->max_fds; i++) {
558 struct over_fd_entry *entry = (struct over_fd_entry*)kzalloc(sizeof(struct over_fd_entry), GFP_KERNEL);
559 if(!entry) {
560 pr_debug("[FD_LEAK](PID:%d)Empty FD:%d", pid, i);
561 }
562 else {
563 memset(entry->name, 0, sizeof entry->name);
564 result = get_file_name_from_fd(files, i, pid, entry);
565 if(result==1) {
566 fd_insert(entry);
567 sum_fds_of_pid++;
568 }
569 }
570 }
571 for(;;) {
572 if(list_empty(&fd_listhead)) {
573 break;
574 }
575 lentry = list_entry((&fd_listhead)->next, struct over_fd_entry, fd_link);
576 num_of_entry = lentry->num_of_fd;
577 if(lentry != NULL && lentry->name!=NULL)
578 pr_debug("[FD_LEAK]OverAllocFDError(PID:%d fileName:%s Num:%d)\n", pid, lentry->name, num_of_entry);
579 else
580 pr_debug("[FD_LEAK]OverAllocFDError(PID:%d fileName:%s Num:%d)\n", pid, "NULL", num_of_entry);
581 list_del((&fd_listhead)->next);
582 fd_delete(lentry->hash);
583 kfree(lentry);
584 }
585 if(sum_fds_of_pid) {
586 pr_debug("[FD_LEAK]OverAllocFDError(PID:%d totalFDs:%d)\n", pid, sum_fds_of_pid);
587 }
588 mutex_unlock(&over_fd_mutex);
589}
590#endif
591
1027abe8
AV
592/*
593 * allocate a file descriptor, mark it busy.
594 */
dcfadfa4
AV
595int __alloc_fd(struct files_struct *files,
596 unsigned start, unsigned end, unsigned flags)
1027abe8 597{
1027abe8
AV
598 unsigned int fd;
599 int error;
600 struct fdtable *fdt;
601
602 spin_lock(&files->file_lock);
603repeat:
604 fdt = files_fdtable(files);
605 fd = start;
606 if (fd < files->next_fd)
607 fd = files->next_fd;
608
609 if (fd < fdt->max_fds)
1fd36adc 610 fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);
1027abe8 611
f33ff992
AV
612 /*
613 * N.B. For clone tasks sharing a files structure, this test
614 * will limit the total number of files that can be opened.
615 */
616 error = -EMFILE;
617 if (fd >= end)
618 goto out;
619
1027abe8
AV
620 error = expand_files(files, fd);
621 if (error < 0)
622 goto out;
623
624 /*
625 * If we needed to expand the fs array we
626 * might have blocked - try again.
627 */
628 if (error)
629 goto repeat;
630
631 if (start <= files->next_fd)
632 files->next_fd = fd + 1;
633
1dce27c5 634 __set_open_fd(fd, fdt);
1027abe8 635 if (flags & O_CLOEXEC)
1dce27c5 636 __set_close_on_exec(fd, fdt);
1027abe8 637 else
1dce27c5 638 __clear_close_on_exec(fd, fdt);
1027abe8
AV
639 error = fd;
640#if 1
641 /* Sanity check */
7dc52157 642 if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
1027abe8
AV
643 printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
644 rcu_assign_pointer(fdt->fd[fd], NULL);
645 }
646#endif
647
648out:
649 spin_unlock(&files->file_lock);
6fa3eb70
S
650#ifdef FD_OVER_CHECK
651 if(error == -EMFILE) {
652 static int dump_current_open_files = 0;
653 if(!dump_current_open_files &&
654 strcmp(current->comm, "Backbone")) { /*add Backbone into FD white list for skype*/
655 dump_current_open_files = 0x1;
656 pr_debug("[FD_LEAK](PID:%d)fd over RLIMIT_NOFILE:%ld", current->pid, rlimit(RLIMIT_NOFILE));
657 fd_show_open_files(current->pid, files, fdt);
658 }
659 }
660#endif
1027abe8
AV
661 return error;
662}
663
ad47bd72 664static int alloc_fd(unsigned start, unsigned flags)
dcfadfa4
AV
665{
666 return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
667}
668
1a7bd226 669int get_unused_fd_flags(unsigned flags)
1027abe8 670{
dcfadfa4 671 return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
1027abe8 672}
1a7bd226 673EXPORT_SYMBOL(get_unused_fd_flags);
56007cae
AV
674
675static void __put_unused_fd(struct files_struct *files, unsigned int fd)
676{
677 struct fdtable *fdt = files_fdtable(files);
678 __clear_open_fd(fd, fdt);
679 if (fd < files->next_fd)
680 files->next_fd = fd;
681}
682
683void put_unused_fd(unsigned int fd)
684{
685 struct files_struct *files = current->files;
686 spin_lock(&files->file_lock);
687 __put_unused_fd(files, fd);
688 spin_unlock(&files->file_lock);
689}
690
691EXPORT_SYMBOL(put_unused_fd);
692
693/*
694 * Install a file pointer in the fd array.
695 *
696 * The VFS is full of places where we drop the files lock between
697 * setting the open_fds bitmap and installing the file in the file
698 * array. At any such point, we are vulnerable to a dup2() race
699 * installing a file in the array before us. We need to detect this and
700 * fput() the struct file we are about to overwrite in this case.
701 *
702 * It should never happen - if we allow dup2() do it, _really_ bad things
703 * will follow.
f869e8a7
AV
704 *
705 * NOTE: __fd_install() variant is really, really low-level; don't
706 * use it unless you are forced to by truly lousy API shoved down
707 * your throat. 'files' *MUST* be either current->files or obtained
708 * by get_files_struct(current) done by whoever had given it to you,
709 * or really bad things will happen. Normally you want to use
710 * fd_install() instead.
56007cae
AV
711 */
712
f869e8a7
AV
713void __fd_install(struct files_struct *files, unsigned int fd,
714 struct file *file)
56007cae 715{
56007cae
AV
716 struct fdtable *fdt;
717 spin_lock(&files->file_lock);
718 fdt = files_fdtable(files);
719 BUG_ON(fdt->fd[fd] != NULL);
720 rcu_assign_pointer(fdt->fd[fd], file);
721 spin_unlock(&files->file_lock);
722}
723
f869e8a7
AV
724void fd_install(unsigned int fd, struct file *file)
725{
726 __fd_install(current->files, fd, file);
727}
728
56007cae 729EXPORT_SYMBOL(fd_install);
0ee8cdfe 730
483ce1d4
AV
731/*
732 * The same warnings as for __alloc_fd()/__fd_install() apply here...
733 */
734int __close_fd(struct files_struct *files, unsigned fd)
735{
736 struct file *file;
737 struct fdtable *fdt;
738
739 spin_lock(&files->file_lock);
740 fdt = files_fdtable(files);
741 if (fd >= fdt->max_fds)
742 goto out_unlock;
743 file = fdt->fd[fd];
744 if (!file)
745 goto out_unlock;
746 rcu_assign_pointer(fdt->fd[fd], NULL);
747 __clear_close_on_exec(fd, fdt);
748 __put_unused_fd(files, fd);
749 spin_unlock(&files->file_lock);
750 return filp_close(file, files);
751
752out_unlock:
753 spin_unlock(&files->file_lock);
754 return -EBADF;
755}
756
6a6d27de
AV
757void do_close_on_exec(struct files_struct *files)
758{
759 unsigned i;
760 struct fdtable *fdt;
761
762 /* exec unshares first */
6a6d27de
AV
763 spin_lock(&files->file_lock);
764 for (i = 0; ; i++) {
765 unsigned long set;
766 unsigned fd = i * BITS_PER_LONG;
767 fdt = files_fdtable(files);
768 if (fd >= fdt->max_fds)
769 break;
770 set = fdt->close_on_exec[i];
771 if (!set)
772 continue;
773 fdt->close_on_exec[i] = 0;
774 for ( ; set ; fd++, set >>= 1) {
775 struct file *file;
776 if (!(set & 1))
777 continue;
778 file = fdt->fd[fd];
779 if (!file)
780 continue;
781 rcu_assign_pointer(fdt->fd[fd], NULL);
782 __put_unused_fd(files, fd);
783 spin_unlock(&files->file_lock);
784 filp_close(file, files);
785 cond_resched();
786 spin_lock(&files->file_lock);
787 }
788
789 }
790 spin_unlock(&files->file_lock);
791}
792
0ee8cdfe
AV
793struct file *fget(unsigned int fd)
794{
795 struct file *file;
796 struct files_struct *files = current->files;
797
798 rcu_read_lock();
799 file = fcheck_files(files, fd);
800 if (file) {
801 /* File object ref couldn't be taken */
802 if (file->f_mode & FMODE_PATH ||
803 !atomic_long_inc_not_zero(&file->f_count))
804 file = NULL;
805 }
806 rcu_read_unlock();
807
808 return file;
809}
810
811EXPORT_SYMBOL(fget);
812
813struct file *fget_raw(unsigned int fd)
814{
815 struct file *file;
816 struct files_struct *files = current->files;
817
818 rcu_read_lock();
819 file = fcheck_files(files, fd);
820 if (file) {
821 /* File object ref couldn't be taken */
822 if (!atomic_long_inc_not_zero(&file->f_count))
823 file = NULL;
824 }
825 rcu_read_unlock();
826
827 return file;
828}
829
830EXPORT_SYMBOL(fget_raw);
831
832/*
833 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
834 *
835 * You can use this instead of fget if you satisfy all of the following
836 * conditions:
837 * 1) You must call fput_light before exiting the syscall and returning control
838 * to userspace (i.e. you cannot remember the returned struct file * after
839 * returning to userspace).
840 * 2) You must not call filp_close on the returned struct file * in between
841 * calls to fget_light and fput_light.
842 * 3) You must not clone the current task in between the calls to fget_light
843 * and fput_light.
844 *
845 * The fput_needed flag returned by fget_light should be passed to the
846 * corresponding fput_light.
847 */
848struct file *fget_light(unsigned int fd, int *fput_needed)
849{
850 struct file *file;
851 struct files_struct *files = current->files;
852
853 *fput_needed = 0;
854 if (atomic_read(&files->count) == 1) {
855 file = fcheck_files(files, fd);
856 if (file && (file->f_mode & FMODE_PATH))
857 file = NULL;
858 } else {
859 rcu_read_lock();
860 file = fcheck_files(files, fd);
861 if (file) {
862 if (!(file->f_mode & FMODE_PATH) &&
863 atomic_long_inc_not_zero(&file->f_count))
864 *fput_needed = 1;
865 else
866 /* Didn't get the reference, someone's freed */
867 file = NULL;
868 }
869 rcu_read_unlock();
870 }
871
872 return file;
873}
4557c669 874EXPORT_SYMBOL(fget_light);
0ee8cdfe
AV
875
876struct file *fget_raw_light(unsigned int fd, int *fput_needed)
877{
878 struct file *file;
879 struct files_struct *files = current->files;
880
881 *fput_needed = 0;
882 if (atomic_read(&files->count) == 1) {
883 file = fcheck_files(files, fd);
884 } else {
885 rcu_read_lock();
886 file = fcheck_files(files, fd);
887 if (file) {
888 if (atomic_long_inc_not_zero(&file->f_count))
889 *fput_needed = 1;
890 else
891 /* Didn't get the reference, someone's freed */
892 file = NULL;
893 }
894 rcu_read_unlock();
895 }
896
897 return file;
898}
fe17f22d
AV
899
900void set_close_on_exec(unsigned int fd, int flag)
901{
902 struct files_struct *files = current->files;
903 struct fdtable *fdt;
904 spin_lock(&files->file_lock);
905 fdt = files_fdtable(files);
906 if (flag)
907 __set_close_on_exec(fd, fdt);
908 else
909 __clear_close_on_exec(fd, fdt);
910 spin_unlock(&files->file_lock);
911}
912
913bool get_close_on_exec(unsigned int fd)
914{
915 struct files_struct *files = current->files;
916 struct fdtable *fdt;
917 bool res;
918 rcu_read_lock();
919 fdt = files_fdtable(files);
920 res = close_on_exec(fd, fdt);
921 rcu_read_unlock();
922 return res;
923}
924
8280d161
AV
925static int do_dup2(struct files_struct *files,
926 struct file *file, unsigned fd, unsigned flags)
fe17f22d 927{
8280d161 928 struct file *tofree;
fe17f22d
AV
929 struct fdtable *fdt;
930
fe17f22d
AV
931 /*
932 * We need to detect attempts to do dup2() over allocated but still
933 * not finished descriptor. NB: OpenBSD avoids that at the price of
934 * extra work in their equivalent of fget() - they insert struct
935 * file immediately after grabbing descriptor, mark it larval if
936 * more work (e.g. actual opening) is needed and make sure that
937 * fget() treats larval files as absent. Potentially interesting,
938 * but while extra work in fget() is trivial, locking implications
939 * and amount of surgery on open()-related paths in VFS are not.
940 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
941 * deadlocks in rather amusing ways, AFAICS. All of that is out of
942 * scope of POSIX or SUS, since neither considers shared descriptor
943 * tables and this condition does not arise without those.
944 */
fe17f22d 945 fdt = files_fdtable(files);
8280d161
AV
946 tofree = fdt->fd[fd];
947 if (!tofree && fd_is_open(fd, fdt))
948 goto Ebusy;
fe17f22d 949 get_file(file);
8280d161
AV
950 rcu_assign_pointer(fdt->fd[fd], file);
951 __set_open_fd(fd, fdt);
fe17f22d 952 if (flags & O_CLOEXEC)
8280d161 953 __set_close_on_exec(fd, fdt);
fe17f22d 954 else
8280d161 955 __clear_close_on_exec(fd, fdt);
fe17f22d
AV
956 spin_unlock(&files->file_lock);
957
958 if (tofree)
959 filp_close(tofree, files);
960
8280d161
AV
961 return fd;
962
963Ebusy:
964 spin_unlock(&files->file_lock);
965 return -EBUSY;
966}
967
968int replace_fd(unsigned fd, struct file *file, unsigned flags)
969{
970 int err;
971 struct files_struct *files = current->files;
972
973 if (!file)
974 return __close_fd(files, fd);
975
976 if (fd >= rlimit(RLIMIT_NOFILE))
08f05c49 977 return -EBADF;
8280d161
AV
978
979 spin_lock(&files->file_lock);
980 err = expand_files(files, fd);
981 if (unlikely(err < 0))
982 goto out_unlock;
983 return do_dup2(files, file, fd, flags);
984
985out_unlock:
986 spin_unlock(&files->file_lock);
987 return err;
988}
989
990SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
991{
992 int err = -EBADF;
993 struct file *file;
994 struct files_struct *files = current->files;
995
996 if ((flags & ~O_CLOEXEC) != 0)
997 return -EINVAL;
998
aed97647
RJ
999 if (unlikely(oldfd == newfd))
1000 return -EINVAL;
1001
8280d161 1002 if (newfd >= rlimit(RLIMIT_NOFILE))
08f05c49 1003 return -EBADF;
8280d161
AV
1004
1005 spin_lock(&files->file_lock);
1006 err = expand_files(files, newfd);
1007 file = fcheck(oldfd);
1008 if (unlikely(!file))
1009 goto Ebadf;
1010 if (unlikely(err < 0)) {
1011 if (err == -EMFILE)
1012 goto Ebadf;
1013 goto out_unlock;
1014 }
1015 return do_dup2(files, file, newfd, flags);
fe17f22d
AV
1016
1017Ebadf:
1018 err = -EBADF;
1019out_unlock:
1020 spin_unlock(&files->file_lock);
1021 return err;
1022}
1023
1024SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
1025{
1026 if (unlikely(newfd == oldfd)) { /* corner case */
1027 struct files_struct *files = current->files;
1028 int retval = oldfd;
1029
1030 rcu_read_lock();
1031 if (!fcheck_files(files, oldfd))
1032 retval = -EBADF;
1033 rcu_read_unlock();
1034 return retval;
1035 }
1036 return sys_dup3(oldfd, newfd, 0);
1037}
1038
1039SYSCALL_DEFINE1(dup, unsigned int, fildes)
1040{
1041 int ret = -EBADF;
1042 struct file *file = fget_raw(fildes);
1043
1044 if (file) {
1045 ret = get_unused_fd();
1046 if (ret >= 0)
1047 fd_install(ret, file);
1048 else
1049 fput(file);
1050 }
1051 return ret;
1052}
1053
1054int f_dupfd(unsigned int from, struct file *file, unsigned flags)
1055{
1056 int err;
1057 if (from >= rlimit(RLIMIT_NOFILE))
1058 return -EINVAL;
1059 err = alloc_fd(from, flags);
1060 if (err >= 0) {
1061 get_file(file);
1062 fd_install(err, file);
1063 }
1064 return err;
1065}
c3c073f8
AV
1066
1067int iterate_fd(struct files_struct *files, unsigned n,
1068 int (*f)(const void *, struct file *, unsigned),
1069 const void *p)
1070{
1071 struct fdtable *fdt;
c3c073f8
AV
1072 int res = 0;
1073 if (!files)
1074 return 0;
1075 spin_lock(&files->file_lock);
a77cfcb4
AV
1076 for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
1077 struct file *file;
1078 file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
1079 if (!file)
1080 continue;
1081 res = f(p, file, n);
1082 if (res)
1083 break;
c3c073f8
AV
1084 }
1085 spin_unlock(&files->file_lock);
1086 return res;
1087}
1088EXPORT_SYMBOL(iterate_fd);