10 #include <linux/export.h>
14 #include <linux/time.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
20 #include <linux/bitops.h>
44 static void *alloc_fdmem(
size_t size)
58 static void free_fdmem(
void *
ptr)
60 is_vmalloc_addr(ptr) ?
vfree(ptr) :
kfree(ptr);
63 static void __free_fdtable(
struct fdtable *fdt)
76 spin_lock_bh(&f->
lock);
79 spin_unlock_bh(&f->
lock);
96 if (!is_vmalloc_addr(fdt->
fd) && !is_vmalloc_addr(fdt->
open_fds)) {
102 spin_lock(&fddef->
lock);
107 spin_unlock(&fddef->
lock);
116 static void copy_fdtable(
struct fdtable *nfdt,
struct fdtable *ofdt)
118 unsigned int cpy,
set;
125 memset((
char *)(nfdt->
fd) + cpy, 0,
set);
135 static struct fdtable * alloc_fdtable(
unsigned int nr)
147 nr /= (1024 /
sizeof(
struct file *));
149 nr *= (1024 /
sizeof(
struct file *));
165 data = alloc_fdmem(nr *
sizeof(
struct file *));
170 data = alloc_fdmem(
max_t(
size_t,
200 struct fdtable *new_fdt, *cur_fdt;
202 spin_unlock(&
files->file_lock);
203 new_fdt = alloc_fdtable(nr);
204 spin_lock(&
files->file_lock);
212 __free_fdtable(new_fdt);
222 copy_fdtable(new_fdt, cur_fdt);
228 __free_fdtable(new_fdt);
252 if (nr >= sysctl_nr_open)
256 return expand_fdtable(files, nr);
259 static inline void __set_close_on_exec(
int fd,
struct fdtable *fdt)
264 static inline void __clear_close_on_exec(
int fd,
struct fdtable *fdt)
269 static inline void __set_open_fd(
int fd,
struct fdtable *fdt)
274 static inline void __clear_open_fd(
int fd,
struct fdtable *fdt)
279 static int count_open_files(
struct fdtable *fdt)
301 struct file **old_fds, **new_fds;
302 int open_files,
size,
i;
303 struct fdtable *old_fdt, *new_fdt;
314 new_fdt = &newf->
fdtab;
321 spin_lock(&oldf->file_lock);
323 open_files = count_open_files(old_fdt);
329 spin_unlock(&oldf->file_lock);
331 if (new_fdt != &newf->
fdtab)
332 __free_fdtable(new_fdt);
334 new_fdt = alloc_fdtable(open_files - 1);
342 __free_fdtable(new_fdt);
352 spin_lock(&oldf->file_lock);
354 open_files = count_open_files(old_fdt);
357 old_fds = old_fdt->
fd;
358 new_fds = new_fdt->
fd;
363 for (i = open_files; i != 0; i--) {
364 struct file *f = *old_fds++;
374 __clear_open_fd(open_files - i, new_fdt);
378 spin_unlock(&oldf->file_lock);
381 size = (new_fdt->
max_fds - open_files) *
sizeof(
struct file *);
386 if (new_fdt->
max_fds > open_files) {
463 if (fdt != &files->
fdtab)
505 fdtable_defer_list_init(i);
512 .fdt = &init_files.
fdtab,
538 spin_lock(&files->file_lock);
556 error = expand_files(files, fd);
567 if (start <= files->next_fd)
570 __set_open_fd(fd, fdt);
572 __set_close_on_exec(fd, fdt);
574 __clear_close_on_exec(fd, fdt);
585 spin_unlock(&files->file_lock);
589 static int alloc_fd(
unsigned start,
unsigned flags)
600 static void __put_unused_fd(
struct files_struct *files,
unsigned int fd)
603 __clear_open_fd(fd, fdt);
611 spin_lock(&files->file_lock);
612 __put_unused_fd(files, fd);
613 spin_unlock(&files->file_lock);
642 spin_lock(&files->file_lock);
646 spin_unlock(&files->file_lock);
664 spin_lock(&files->file_lock);
672 __clear_close_on_exec(fd, fdt);
673 __put_unused_fd(files, fd);
674 spin_unlock(&files->file_lock);
678 spin_unlock(&files->file_lock);
688 spin_lock(&files->file_lock);
699 for ( ;
set ; fd++,
set >>= 1) {
707 __put_unused_fd(files, fd);
708 spin_unlock(&files->file_lock);
711 spin_lock(&files->file_lock);
715 spin_unlock(&files->file_lock);
724 file = fcheck_files(files, fd);
744 file = fcheck_files(files, fd);
780 file = fcheck_files(files, fd);
785 file = fcheck_files(files, fd);
808 file = fcheck_files(files, fd);
811 file = fcheck_files(files, fd);
829 spin_lock(&files->file_lock);
832 __set_close_on_exec(fd, fdt);
834 __clear_close_on_exec(fd, fdt);
835 spin_unlock(&files->file_lock);
851 struct file *
file,
unsigned fd,
unsigned flags)
871 tofree = fdt->
fd[
fd];
872 if (!tofree && fd_is_open(fd, fdt))
876 __set_open_fd(fd, fdt);
878 __set_close_on_exec(fd, fdt);
880 __clear_close_on_exec(fd, fdt);
881 spin_unlock(&files->file_lock);
889 spin_unlock(&files->file_lock);
893 int replace_fd(
unsigned fd,
struct file *file,
unsigned flags)
904 spin_lock(&files->file_lock);
905 err = expand_files(files, fd);
908 return do_dup2(files, file, fd, flags);
911 spin_unlock(&files->file_lock);
921 if ((flags & ~O_CLOEXEC) != 0)
930 spin_lock(&files->file_lock);
931 err = expand_files(files, newfd);
940 return do_dup2(files, file, newfd, flags);
945 spin_unlock(&files->file_lock);
956 if (!fcheck_files(files, oldfd))
967 struct file *file =
fget_raw(fildes);
984 err = alloc_fd(from, flags);
993 int (*f)(
const void *,
struct file *,
unsigned),
1000 spin_lock(&files->file_lock);
1006 res =
f(p, file, n);
1010 spin_unlock(&files->file_lock);