Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 9 | #include <linux/syscalls.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 10 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/fs.h> |
| 12 | #include <linux/mm.h> |
Andrew Morton | 6d4831c | 2011-04-27 15:26:41 -0700 | [diff] [blame] | 13 | #include <linux/mmzone.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/time.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 15 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/vmalloc.h> |
| 18 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 19 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/rcupdate.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | |
Eric Dumazet | 9cfe015 | 2008-02-06 01:37:16 -0800 | [diff] [blame] | 26 | int sysctl_nr_open __read_mostly = 1024*1024; |
Al Viro | eceea0b | 2008-05-10 10:08:32 -0400 | [diff] [blame] | 27 | int sysctl_nr_open_min = BITS_PER_LONG; |
| 28 | int sysctl_nr_open_max = 1024 * 1024; /* raised later */ |
Eric Dumazet | 9cfe015 | 2008-02-06 01:37:16 -0800 | [diff] [blame] | 29 | |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 30 | static void *alloc_fdmem(size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Andrew Morton | 6d4831c | 2011-04-27 15:26:41 -0700 | [diff] [blame] | 32 | /* |
| 33 | * Very large allocations can stress page reclaim, so fall back to |
| 34 | * vmalloc() if the allocation size will be considered "large" by the VM. |
| 35 | */ |
| 36 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) { |
Eric W. Biederman | 96c7a2f | 2014-02-10 14:25:41 -0800 | [diff] [blame] | 37 | void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY); |
Andrew Morton | 6d4831c | 2011-04-27 15:26:41 -0700 | [diff] [blame] | 38 | if (data != NULL) |
| 39 | return data; |
| 40 | } |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 41 | return vmalloc(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 44 | static void free_fdmem(void *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 46 | is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr); |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 49 | static void __free_fdtable(struct fdtable *fdt) |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 50 | { |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 51 | free_fdmem(fdt->fd); |
| 52 | free_fdmem(fdt->open_fds); |
| 53 | kfree(fdt); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 56 | static void free_fdtable_rcu(struct rcu_head *rcu) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 57 | { |
Al Viro | ac3e3c5 | 2013-04-28 21:42:33 -0400 | [diff] [blame] | 58 | __free_fdtable(container_of(rcu, struct fdtable, rcu)); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Expand the fdset in the files_struct. Called with the files spinlock |
| 63 | * held for write. |
| 64 | */ |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 65 | static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 66 | { |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 67 | unsigned int cpy, set; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 68 | |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 69 | BUG_ON(nfdt->max_fds < ofdt->max_fds); |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 70 | |
| 71 | cpy = ofdt->max_fds * sizeof(struct file *); |
| 72 | set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); |
| 73 | memcpy(nfdt->fd, ofdt->fd, cpy); |
| 74 | memset((char *)(nfdt->fd) + cpy, 0, set); |
| 75 | |
| 76 | cpy = ofdt->max_fds / BITS_PER_BYTE; |
| 77 | set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE; |
| 78 | memcpy(nfdt->open_fds, ofdt->open_fds, cpy); |
| 79 | memset((char *)(nfdt->open_fds) + cpy, 0, set); |
| 80 | memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy); |
| 81 | memset((char *)(nfdt->close_on_exec) + cpy, 0, set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 84 | static struct fdtable * alloc_fdtable(unsigned int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 86 | struct fdtable *fdt; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 87 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 89 | /* |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 90 | * Figure out how many fds we actually want to support in this fdtable. |
| 91 | * Allocation steps are keyed to the size of the fdarray, since it |
| 92 | * grows far faster than any of the other dynamic data. We try to fit |
| 93 | * the fdarray into comfortable page-tuned chunks: starting at 1024B |
| 94 | * and growing in powers of two from there on. |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 95 | */ |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 96 | nr /= (1024 / sizeof(struct file *)); |
| 97 | nr = roundup_pow_of_two(nr + 1); |
| 98 | nr *= (1024 / sizeof(struct file *)); |
Al Viro | 5c598b3 | 2008-04-27 20:04:15 -0400 | [diff] [blame] | 99 | /* |
| 100 | * Note that this can drive nr *below* what we had passed if sysctl_nr_open |
| 101 | * had been set lower between the check in expand_files() and here. Deal |
| 102 | * with that in caller, it's cheaper that way. |
| 103 | * |
| 104 | * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise |
| 105 | * bitmaps handling below becomes unpleasant, to put it mildly... |
| 106 | */ |
| 107 | if (unlikely(nr > sysctl_nr_open)) |
| 108 | nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1; |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 109 | |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 110 | fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL); |
| 111 | if (!fdt) |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 112 | goto out; |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 113 | fdt->max_fds = nr; |
| 114 | data = alloc_fdmem(nr * sizeof(struct file *)); |
| 115 | if (!data) |
| 116 | goto out_fdt; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 117 | fdt->fd = data; |
| 118 | |
| 119 | data = alloc_fdmem(max_t(size_t, |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 120 | 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES)); |
| 121 | if (!data) |
| 122 | goto out_arr; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 123 | fdt->open_fds = data; |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 124 | data += nr / BITS_PER_BYTE; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 125 | fdt->close_on_exec = data; |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 126 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 127 | return fdt; |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 128 | |
| 129 | out_arr: |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 130 | free_fdmem(fdt->fd); |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 131 | out_fdt: |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 132 | kfree(fdt); |
Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 133 | out: |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | /* |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 138 | * Expand the file descriptor table. |
| 139 | * This function will allocate a new fdtable and both fd array and fdset, of |
| 140 | * the given size. |
| 141 | * Return <0 error code on error; 1 on successful completion. |
| 142 | * The files->file_lock should be held on entry, and will be held on exit. |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 143 | */ |
| 144 | static int expand_fdtable(struct files_struct *files, int nr) |
| 145 | __releases(files->file_lock) |
| 146 | __acquires(files->file_lock) |
| 147 | { |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 148 | struct fdtable *new_fdt, *cur_fdt; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | spin_unlock(&files->file_lock); |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 151 | new_fdt = alloc_fdtable(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | spin_lock(&files->file_lock); |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 153 | if (!new_fdt) |
| 154 | return -ENOMEM; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 155 | /* |
Al Viro | 5c598b3 | 2008-04-27 20:04:15 -0400 | [diff] [blame] | 156 | * extremely unlikely race - sysctl_nr_open decreased between the check in |
| 157 | * caller and alloc_fdtable(). Cheaper to catch it here... |
| 158 | */ |
| 159 | if (unlikely(new_fdt->max_fds <= nr)) { |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 160 | __free_fdtable(new_fdt); |
Al Viro | 5c598b3 | 2008-04-27 20:04:15 -0400 | [diff] [blame] | 161 | return -EMFILE; |
| 162 | } |
| 163 | /* |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 164 | * Check again since another task may have expanded the fd table while |
| 165 | * we dropped the lock |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 166 | */ |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 167 | cur_fdt = files_fdtable(files); |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 168 | if (nr >= cur_fdt->max_fds) { |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 169 | /* Continue as planned */ |
| 170 | copy_fdtable(new_fdt, cur_fdt); |
| 171 | rcu_assign_pointer(files->fdt, new_fdt); |
Al Viro | ac3e3c5 | 2013-04-28 21:42:33 -0400 | [diff] [blame] | 172 | if (cur_fdt != &files->fdtab) |
Al Viro | 1983e78 | 2012-08-15 20:06:36 -0400 | [diff] [blame] | 173 | call_rcu(&cur_fdt->rcu, free_fdtable_rcu); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 174 | } else { |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 175 | /* Somebody else expanded, so undo our attempt */ |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 176 | __free_fdtable(new_fdt); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 177 | } |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 178 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Expand files. |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 183 | * This function will expand the file structures, if the requested size exceeds |
| 184 | * the current capacity and there is room for expansion. |
| 185 | * Return <0 error code on error; 0 when nothing done; 1 when files were |
| 186 | * expanded and execution may have blocked. |
| 187 | * The files->file_lock should be held on entry, and will be held on exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | */ |
Al Viro | ad47bd7 | 2012-08-21 20:11:34 -0400 | [diff] [blame] | 189 | static int expand_files(struct files_struct *files, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 191 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 193 | fdt = files_fdtable(files); |
Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 194 | |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 195 | /* Do we need to expand? */ |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 196 | if (nr < fdt->max_fds) |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 197 | return 0; |
Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 198 | |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 199 | /* Can we expand? */ |
Eric Dumazet | 9cfe015 | 2008-02-06 01:37:16 -0800 | [diff] [blame] | 200 | if (nr >= sysctl_nr_open) |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 201 | return -EMFILE; |
| 202 | |
| 203 | /* All good, so we try */ |
| 204 | return expand_fdtable(files, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 206 | |
Al Viro | b8318b0 | 2012-08-21 20:09:42 -0400 | [diff] [blame] | 207 | static inline void __set_close_on_exec(int fd, struct fdtable *fdt) |
| 208 | { |
| 209 | __set_bit(fd, fdt->close_on_exec); |
| 210 | } |
| 211 | |
| 212 | static inline void __clear_close_on_exec(int fd, struct fdtable *fdt) |
| 213 | { |
| 214 | __clear_bit(fd, fdt->close_on_exec); |
| 215 | } |
| 216 | |
| 217 | static inline void __set_open_fd(int fd, struct fdtable *fdt) |
| 218 | { |
| 219 | __set_bit(fd, fdt->open_fds); |
| 220 | } |
| 221 | |
| 222 | static inline void __clear_open_fd(int fd, struct fdtable *fdt) |
| 223 | { |
| 224 | __clear_bit(fd, fdt->open_fds); |
| 225 | } |
| 226 | |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 227 | static int count_open_files(struct fdtable *fdt) |
| 228 | { |
| 229 | int size = fdt->max_fds; |
| 230 | int i; |
| 231 | |
| 232 | /* Find the last open fd */ |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 233 | for (i = size / BITS_PER_LONG; i > 0; ) { |
| 234 | if (fdt->open_fds[--i]) |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 235 | break; |
| 236 | } |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 237 | i = (i + 1) * BITS_PER_LONG; |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 238 | return i; |
| 239 | } |
| 240 | |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 241 | /* |
| 242 | * Allocate a new files structure and copy contents from the |
| 243 | * passed in files structure. |
| 244 | * errorp will be valid only when the returned files_struct is NULL. |
| 245 | */ |
| 246 | struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) |
| 247 | { |
| 248 | struct files_struct *newf; |
| 249 | struct file **old_fds, **new_fds; |
| 250 | int open_files, size, i; |
| 251 | struct fdtable *old_fdt, *new_fdt; |
| 252 | |
| 253 | *errorp = -ENOMEM; |
Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 254 | newf = kmem_cache_alloc(files_cachep, GFP_KERNEL); |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 255 | if (!newf) |
| 256 | goto out; |
| 257 | |
Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 258 | atomic_set(&newf->count, 1); |
| 259 | |
| 260 | spin_lock_init(&newf->file_lock); |
| 261 | newf->next_fd = 0; |
| 262 | new_fdt = &newf->fdtab; |
| 263 | new_fdt->max_fds = NR_OPEN_DEFAULT; |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 264 | new_fdt->close_on_exec = newf->close_on_exec_init; |
| 265 | new_fdt->open_fds = newf->open_fds_init; |
Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 266 | new_fdt->fd = &newf->fd_array[0]; |
Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 267 | |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 268 | spin_lock(&oldf->file_lock); |
| 269 | old_fdt = files_fdtable(oldf); |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 270 | open_files = count_open_files(old_fdt); |
| 271 | |
| 272 | /* |
| 273 | * Check whether we need to allocate a larger fd array and fd set. |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 274 | */ |
Al Viro | adbecb1 | 2008-05-08 21:19:42 -0400 | [diff] [blame] | 275 | while (unlikely(open_files > new_fdt->max_fds)) { |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 276 | spin_unlock(&oldf->file_lock); |
Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 277 | |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 278 | if (new_fdt != &newf->fdtab) |
| 279 | __free_fdtable(new_fdt); |
Al Viro | adbecb1 | 2008-05-08 21:19:42 -0400 | [diff] [blame] | 280 | |
Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 281 | new_fdt = alloc_fdtable(open_files - 1); |
| 282 | if (!new_fdt) { |
| 283 | *errorp = -ENOMEM; |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 284 | goto out_release; |
Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* beyond sysctl_nr_open; nothing to do */ |
| 288 | if (unlikely(new_fdt->max_fds < open_files)) { |
Changli Gao | a892e2d | 2010-08-10 18:01:35 -0700 | [diff] [blame] | 289 | __free_fdtable(new_fdt); |
Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 290 | *errorp = -EMFILE; |
| 291 | goto out_release; |
| 292 | } |
Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 293 | |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 294 | /* |
| 295 | * Reacquire the oldf lock and a pointer to its fd table |
| 296 | * who knows it may have a new bigger fd table. We need |
| 297 | * the latest pointer. |
| 298 | */ |
| 299 | spin_lock(&oldf->file_lock); |
| 300 | old_fdt = files_fdtable(oldf); |
Al Viro | adbecb1 | 2008-05-08 21:19:42 -0400 | [diff] [blame] | 301 | open_files = count_open_files(old_fdt); |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | old_fds = old_fdt->fd; |
| 305 | new_fds = new_fdt->fd; |
| 306 | |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 307 | memcpy(new_fdt->open_fds, old_fdt->open_fds, open_files / 8); |
| 308 | memcpy(new_fdt->close_on_exec, old_fdt->close_on_exec, open_files / 8); |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 309 | |
| 310 | for (i = open_files; i != 0; i--) { |
| 311 | struct file *f = *old_fds++; |
| 312 | if (f) { |
| 313 | get_file(f); |
| 314 | } else { |
| 315 | /* |
| 316 | * The fd may be claimed in the fd bitmap but not yet |
| 317 | * instantiated in the files array if a sibling thread |
| 318 | * is partway through open(). So make sure that this |
| 319 | * fd is available to the new process. |
| 320 | */ |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 321 | __clear_open_fd(open_files - i, new_fdt); |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 322 | } |
| 323 | rcu_assign_pointer(*new_fds++, f); |
| 324 | } |
| 325 | spin_unlock(&oldf->file_lock); |
| 326 | |
| 327 | /* compute the remainder to be cleared */ |
| 328 | size = (new_fdt->max_fds - open_files) * sizeof(struct file *); |
| 329 | |
| 330 | /* This is long word aligned thus could use a optimized version */ |
| 331 | memset(new_fds, 0, size); |
| 332 | |
| 333 | if (new_fdt->max_fds > open_files) { |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 334 | int left = (new_fdt->max_fds - open_files) / 8; |
| 335 | int start = open_files / BITS_PER_LONG; |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 336 | |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 337 | memset(&new_fdt->open_fds[start], 0, left); |
| 338 | memset(&new_fdt->close_on_exec[start], 0, left); |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 341 | rcu_assign_pointer(newf->fdt, new_fdt); |
| 342 | |
Al Viro | 02afc626 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 343 | return newf; |
| 344 | |
| 345 | out_release: |
| 346 | kmem_cache_free(files_cachep, newf); |
| 347 | out: |
| 348 | return NULL; |
| 349 | } |
| 350 | |
Oleg Nesterov | ce08b62 | 2014-01-11 19:19:53 +0100 | [diff] [blame] | 351 | static struct fdtable *close_files(struct files_struct * files) |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 352 | { |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 353 | /* |
| 354 | * It is safe to dereference the fd table without RCU or |
| 355 | * ->file_lock because this is the last reference to the |
Oleg Nesterov | ce08b62 | 2014-01-11 19:19:53 +0100 | [diff] [blame] | 356 | * files structure. |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 357 | */ |
Oleg Nesterov | ce08b62 | 2014-01-11 19:19:53 +0100 | [diff] [blame] | 358 | struct fdtable *fdt = rcu_dereference_raw(files->fdt); |
| 359 | int i, j = 0; |
| 360 | |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 361 | for (;;) { |
| 362 | unsigned long set; |
| 363 | i = j * BITS_PER_LONG; |
| 364 | if (i >= fdt->max_fds) |
| 365 | break; |
| 366 | set = fdt->open_fds[j++]; |
| 367 | while (set) { |
| 368 | if (set & 1) { |
| 369 | struct file * file = xchg(&fdt->fd[i], NULL); |
| 370 | if (file) { |
| 371 | filp_close(file, files); |
| 372 | cond_resched(); |
| 373 | } |
| 374 | } |
| 375 | i++; |
| 376 | set >>= 1; |
| 377 | } |
| 378 | } |
Oleg Nesterov | ce08b62 | 2014-01-11 19:19:53 +0100 | [diff] [blame] | 379 | |
| 380 | return fdt; |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | struct files_struct *get_files_struct(struct task_struct *task) |
| 384 | { |
| 385 | struct files_struct *files; |
| 386 | |
| 387 | task_lock(task); |
| 388 | files = task->files; |
| 389 | if (files) |
| 390 | atomic_inc(&files->count); |
| 391 | task_unlock(task); |
| 392 | |
| 393 | return files; |
| 394 | } |
| 395 | |
| 396 | void put_files_struct(struct files_struct *files) |
| 397 | { |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 398 | if (atomic_dec_and_test(&files->count)) { |
Oleg Nesterov | ce08b62 | 2014-01-11 19:19:53 +0100 | [diff] [blame] | 399 | struct fdtable *fdt = close_files(files); |
| 400 | |
Al Viro | b9e02af | 2012-08-15 20:00:58 -0400 | [diff] [blame] | 401 | /* free the arrays if they are not embedded */ |
| 402 | if (fdt != &files->fdtab) |
| 403 | __free_fdtable(fdt); |
| 404 | kmem_cache_free(files_cachep, files); |
Al Viro | 7cf4dc3 | 2012-08-15 19:56:12 -0400 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | void reset_files_struct(struct files_struct *files) |
| 409 | { |
| 410 | struct task_struct *tsk = current; |
| 411 | struct files_struct *old; |
| 412 | |
| 413 | old = tsk->files; |
| 414 | task_lock(tsk); |
| 415 | tsk->files = files; |
| 416 | task_unlock(tsk); |
| 417 | put_files_struct(old); |
| 418 | } |
| 419 | |
| 420 | void exit_files(struct task_struct *tsk) |
| 421 | { |
| 422 | struct files_struct * files = tsk->files; |
| 423 | |
| 424 | if (files) { |
| 425 | task_lock(tsk); |
| 426 | tsk->files = NULL; |
| 427 | task_unlock(tsk); |
| 428 | put_files_struct(files); |
| 429 | } |
| 430 | } |
| 431 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 432 | void __init files_defer_init(void) |
| 433 | { |
Al Viro | eceea0b | 2008-05-10 10:08:32 -0400 | [diff] [blame] | 434 | sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) & |
| 435 | -BITS_PER_LONG; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 436 | } |
Al Viro | f52111b | 2008-05-08 18:19:16 -0400 | [diff] [blame] | 437 | |
| 438 | struct files_struct init_files = { |
| 439 | .count = ATOMIC_INIT(1), |
| 440 | .fdt = &init_files.fdtab, |
| 441 | .fdtab = { |
| 442 | .max_fds = NR_OPEN_DEFAULT, |
| 443 | .fd = &init_files.fd_array[0], |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 444 | .close_on_exec = init_files.close_on_exec_init, |
| 445 | .open_fds = init_files.open_fds_init, |
Al Viro | f52111b | 2008-05-08 18:19:16 -0400 | [diff] [blame] | 446 | }, |
Thomas Gleixner | eece09e | 2011-07-17 21:25:03 +0200 | [diff] [blame] | 447 | .file_lock = __SPIN_LOCK_UNLOCKED(init_files.file_lock), |
Al Viro | f52111b | 2008-05-08 18:19:16 -0400 | [diff] [blame] | 448 | }; |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 449 | |
| 450 | /* |
| 451 | * allocate a file descriptor, mark it busy. |
| 452 | */ |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 453 | int __alloc_fd(struct files_struct *files, |
| 454 | unsigned start, unsigned end, unsigned flags) |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 455 | { |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 456 | unsigned int fd; |
| 457 | int error; |
| 458 | struct fdtable *fdt; |
| 459 | |
| 460 | spin_lock(&files->file_lock); |
| 461 | repeat: |
| 462 | fdt = files_fdtable(files); |
| 463 | fd = start; |
| 464 | if (fd < files->next_fd) |
| 465 | fd = files->next_fd; |
| 466 | |
| 467 | if (fd < fdt->max_fds) |
David Howells | 1fd36ad | 2012-02-16 17:49:54 +0000 | [diff] [blame] | 468 | fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd); |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 469 | |
Al Viro | f33ff99 | 2012-08-12 16:17:59 -0400 | [diff] [blame] | 470 | /* |
| 471 | * N.B. For clone tasks sharing a files structure, this test |
| 472 | * will limit the total number of files that can be opened. |
| 473 | */ |
| 474 | error = -EMFILE; |
| 475 | if (fd >= end) |
| 476 | goto out; |
| 477 | |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 478 | error = expand_files(files, fd); |
| 479 | if (error < 0) |
| 480 | goto out; |
| 481 | |
| 482 | /* |
| 483 | * If we needed to expand the fs array we |
| 484 | * might have blocked - try again. |
| 485 | */ |
| 486 | if (error) |
| 487 | goto repeat; |
| 488 | |
| 489 | if (start <= files->next_fd) |
| 490 | files->next_fd = fd + 1; |
| 491 | |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 492 | __set_open_fd(fd, fdt); |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 493 | if (flags & O_CLOEXEC) |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 494 | __set_close_on_exec(fd, fdt); |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 495 | else |
David Howells | 1dce27c | 2012-02-16 17:49:42 +0000 | [diff] [blame] | 496 | __clear_close_on_exec(fd, fdt); |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 497 | error = fd; |
| 498 | #if 1 |
| 499 | /* Sanity check */ |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 500 | if (rcu_dereference_raw(fdt->fd[fd]) != NULL) { |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 501 | printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd); |
| 502 | rcu_assign_pointer(fdt->fd[fd], NULL); |
| 503 | } |
| 504 | #endif |
| 505 | |
| 506 | out: |
| 507 | spin_unlock(&files->file_lock); |
| 508 | return error; |
| 509 | } |
| 510 | |
Al Viro | ad47bd7 | 2012-08-21 20:11:34 -0400 | [diff] [blame] | 511 | static int alloc_fd(unsigned start, unsigned flags) |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 512 | { |
| 513 | return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags); |
| 514 | } |
| 515 | |
Al Viro | 1a7bd22 | 2012-08-12 17:18:05 -0400 | [diff] [blame] | 516 | int get_unused_fd_flags(unsigned flags) |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 517 | { |
Al Viro | dcfadfa | 2012-08-12 17:27:30 -0400 | [diff] [blame] | 518 | return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags); |
Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 519 | } |
Al Viro | 1a7bd22 | 2012-08-12 17:18:05 -0400 | [diff] [blame] | 520 | EXPORT_SYMBOL(get_unused_fd_flags); |
Al Viro | 56007ca | 2012-08-15 21:03:26 -0400 | [diff] [blame] | 521 | |
| 522 | static void __put_unused_fd(struct files_struct *files, unsigned int fd) |
| 523 | { |
| 524 | struct fdtable *fdt = files_fdtable(files); |
| 525 | __clear_open_fd(fd, fdt); |
| 526 | if (fd < files->next_fd) |
| 527 | files->next_fd = fd; |
| 528 | } |
| 529 | |
| 530 | void put_unused_fd(unsigned int fd) |
| 531 | { |
| 532 | struct files_struct *files = current->files; |
| 533 | spin_lock(&files->file_lock); |
| 534 | __put_unused_fd(files, fd); |
| 535 | spin_unlock(&files->file_lock); |
| 536 | } |
| 537 | |
| 538 | EXPORT_SYMBOL(put_unused_fd); |
| 539 | |
| 540 | /* |
| 541 | * Install a file pointer in the fd array. |
| 542 | * |
| 543 | * The VFS is full of places where we drop the files lock between |
| 544 | * setting the open_fds bitmap and installing the file in the file |
| 545 | * array. At any such point, we are vulnerable to a dup2() race |
| 546 | * installing a file in the array before us. We need to detect this and |
| 547 | * fput() the struct file we are about to overwrite in this case. |
| 548 | * |
| 549 | * It should never happen - if we allow dup2() do it, _really_ bad things |
| 550 | * will follow. |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 551 | * |
| 552 | * NOTE: __fd_install() variant is really, really low-level; don't |
| 553 | * use it unless you are forced to by truly lousy API shoved down |
| 554 | * your throat. 'files' *MUST* be either current->files or obtained |
| 555 | * by get_files_struct(current) done by whoever had given it to you, |
| 556 | * or really bad things will happen. Normally you want to use |
| 557 | * fd_install() instead. |
Al Viro | 56007ca | 2012-08-15 21:03:26 -0400 | [diff] [blame] | 558 | */ |
| 559 | |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 560 | void __fd_install(struct files_struct *files, unsigned int fd, |
| 561 | struct file *file) |
Al Viro | 56007ca | 2012-08-15 21:03:26 -0400 | [diff] [blame] | 562 | { |
Al Viro | 56007ca | 2012-08-15 21:03:26 -0400 | [diff] [blame] | 563 | struct fdtable *fdt; |
| 564 | spin_lock(&files->file_lock); |
| 565 | fdt = files_fdtable(files); |
| 566 | BUG_ON(fdt->fd[fd] != NULL); |
| 567 | rcu_assign_pointer(fdt->fd[fd], file); |
| 568 | spin_unlock(&files->file_lock); |
| 569 | } |
| 570 | |
Al Viro | f869e8a | 2012-08-15 21:06:33 -0400 | [diff] [blame] | 571 | void fd_install(unsigned int fd, struct file *file) |
| 572 | { |
| 573 | __fd_install(current->files, fd, file); |
| 574 | } |
| 575 | |
Al Viro | 56007ca | 2012-08-15 21:03:26 -0400 | [diff] [blame] | 576 | EXPORT_SYMBOL(fd_install); |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 577 | |
Al Viro | 483ce1d | 2012-08-19 12:04:24 -0400 | [diff] [blame] | 578 | /* |
| 579 | * The same warnings as for __alloc_fd()/__fd_install() apply here... |
| 580 | */ |
| 581 | int __close_fd(struct files_struct *files, unsigned fd) |
| 582 | { |
| 583 | struct file *file; |
| 584 | struct fdtable *fdt; |
| 585 | |
| 586 | spin_lock(&files->file_lock); |
| 587 | fdt = files_fdtable(files); |
| 588 | if (fd >= fdt->max_fds) |
| 589 | goto out_unlock; |
| 590 | file = fdt->fd[fd]; |
| 591 | if (!file) |
| 592 | goto out_unlock; |
| 593 | rcu_assign_pointer(fdt->fd[fd], NULL); |
| 594 | __clear_close_on_exec(fd, fdt); |
| 595 | __put_unused_fd(files, fd); |
| 596 | spin_unlock(&files->file_lock); |
| 597 | return filp_close(file, files); |
| 598 | |
| 599 | out_unlock: |
| 600 | spin_unlock(&files->file_lock); |
| 601 | return -EBADF; |
| 602 | } |
| 603 | |
Al Viro | 6a6d27d | 2012-08-21 09:56:33 -0400 | [diff] [blame] | 604 | void do_close_on_exec(struct files_struct *files) |
| 605 | { |
| 606 | unsigned i; |
| 607 | struct fdtable *fdt; |
| 608 | |
| 609 | /* exec unshares first */ |
Al Viro | 6a6d27d | 2012-08-21 09:56:33 -0400 | [diff] [blame] | 610 | spin_lock(&files->file_lock); |
| 611 | for (i = 0; ; i++) { |
| 612 | unsigned long set; |
| 613 | unsigned fd = i * BITS_PER_LONG; |
| 614 | fdt = files_fdtable(files); |
| 615 | if (fd >= fdt->max_fds) |
| 616 | break; |
| 617 | set = fdt->close_on_exec[i]; |
| 618 | if (!set) |
| 619 | continue; |
| 620 | fdt->close_on_exec[i] = 0; |
| 621 | for ( ; set ; fd++, set >>= 1) { |
| 622 | struct file *file; |
| 623 | if (!(set & 1)) |
| 624 | continue; |
| 625 | file = fdt->fd[fd]; |
| 626 | if (!file) |
| 627 | continue; |
| 628 | rcu_assign_pointer(fdt->fd[fd], NULL); |
| 629 | __put_unused_fd(files, fd); |
| 630 | spin_unlock(&files->file_lock); |
| 631 | filp_close(file, files); |
| 632 | cond_resched(); |
| 633 | spin_lock(&files->file_lock); |
| 634 | } |
| 635 | |
| 636 | } |
| 637 | spin_unlock(&files->file_lock); |
| 638 | } |
| 639 | |
Oleg Nesterov | 1deb46e | 2014-01-13 16:48:19 +0100 | [diff] [blame] | 640 | static struct file *__fget(unsigned int fd, fmode_t mask) |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 641 | { |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 642 | struct files_struct *files = current->files; |
Oleg Nesterov | 1deb46e | 2014-01-13 16:48:19 +0100 | [diff] [blame] | 643 | struct file *file; |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 644 | |
| 645 | rcu_read_lock(); |
| 646 | file = fcheck_files(files, fd); |
| 647 | if (file) { |
| 648 | /* File object ref couldn't be taken */ |
Oleg Nesterov | 1deb46e | 2014-01-13 16:48:19 +0100 | [diff] [blame] | 649 | if ((file->f_mode & mask) || |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 650 | !atomic_long_inc_not_zero(&file->f_count)) |
| 651 | file = NULL; |
| 652 | } |
| 653 | rcu_read_unlock(); |
| 654 | |
| 655 | return file; |
| 656 | } |
| 657 | |
Oleg Nesterov | 1deb46e | 2014-01-13 16:48:19 +0100 | [diff] [blame] | 658 | struct file *fget(unsigned int fd) |
| 659 | { |
| 660 | return __fget(fd, FMODE_PATH); |
| 661 | } |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 662 | EXPORT_SYMBOL(fget); |
| 663 | |
| 664 | struct file *fget_raw(unsigned int fd) |
| 665 | { |
Oleg Nesterov | 1deb46e | 2014-01-13 16:48:19 +0100 | [diff] [blame] | 666 | return __fget(fd, 0); |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 667 | } |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 668 | EXPORT_SYMBOL(fget_raw); |
| 669 | |
| 670 | /* |
| 671 | * Lightweight file lookup - no refcnt increment if fd table isn't shared. |
| 672 | * |
| 673 | * You can use this instead of fget if you satisfy all of the following |
| 674 | * conditions: |
| 675 | * 1) You must call fput_light before exiting the syscall and returning control |
| 676 | * to userspace (i.e. you cannot remember the returned struct file * after |
| 677 | * returning to userspace). |
| 678 | * 2) You must not call filp_close on the returned struct file * in between |
| 679 | * calls to fget_light and fput_light. |
| 680 | * 3) You must not clone the current task in between the calls to fget_light |
| 681 | * and fput_light. |
| 682 | * |
| 683 | * The fput_needed flag returned by fget_light should be passed to the |
| 684 | * corresponding fput_light. |
| 685 | */ |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 686 | static unsigned long __fget_light(unsigned int fd, fmode_t mask) |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 687 | { |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 688 | struct files_struct *files = current->files; |
Oleg Nesterov | ad46183 | 2014-01-13 16:48:40 +0100 | [diff] [blame] | 689 | struct file *file; |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 690 | |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 691 | if (atomic_read(&files->count) == 1) { |
Oleg Nesterov | a8d4b83 | 2014-01-11 19:19:32 +0100 | [diff] [blame] | 692 | file = __fcheck_files(files, fd); |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 693 | if (!file || unlikely(file->f_mode & mask)) |
| 694 | return 0; |
| 695 | return (unsigned long)file; |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 696 | } else { |
Oleg Nesterov | e6ff9a9 | 2014-01-13 16:49:06 +0100 | [diff] [blame] | 697 | file = __fget(fd, mask); |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 698 | if (!file) |
| 699 | return 0; |
| 700 | return FDPUT_FPUT | (unsigned long)file; |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 701 | } |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 702 | } |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 703 | unsigned long __fdget(unsigned int fd) |
Oleg Nesterov | ad46183 | 2014-01-13 16:48:40 +0100 | [diff] [blame] | 704 | { |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 705 | return __fget_light(fd, FMODE_PATH); |
Oleg Nesterov | ad46183 | 2014-01-13 16:48:40 +0100 | [diff] [blame] | 706 | } |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 707 | EXPORT_SYMBOL(__fdget); |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 708 | |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 709 | unsigned long __fdget_raw(unsigned int fd) |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 710 | { |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 711 | return __fget_light(fd, 0); |
Al Viro | 0ee8cdf | 2012-08-15 21:12:10 -0400 | [diff] [blame] | 712 | } |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 713 | |
Al Viro | bd2a31d | 2014-03-04 14:54:22 -0500 | [diff] [blame] | 714 | unsigned long __fdget_pos(unsigned int fd) |
| 715 | { |
| 716 | struct files_struct *files = current->files; |
| 717 | struct file *file; |
| 718 | unsigned long v; |
| 719 | |
| 720 | if (atomic_read(&files->count) == 1) { |
| 721 | file = __fcheck_files(files, fd); |
| 722 | v = 0; |
| 723 | } else { |
| 724 | file = __fget(fd, 0); |
| 725 | v = FDPUT_FPUT; |
| 726 | } |
| 727 | if (!file) |
| 728 | return 0; |
| 729 | |
| 730 | if (file->f_mode & FMODE_ATOMIC_POS) { |
| 731 | if (file_count(file) > 1) { |
| 732 | v |= FDPUT_POS_UNLOCK; |
| 733 | mutex_lock(&file->f_pos_lock); |
| 734 | } |
| 735 | } |
| 736 | return v | (unsigned long)file; |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * We only lock f_pos if we have threads or if the file might be |
| 741 | * shared with another process. In both cases we'll have an elevated |
| 742 | * file count (done either by fdget() or by fork()). |
| 743 | */ |
| 744 | |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 745 | void set_close_on_exec(unsigned int fd, int flag) |
| 746 | { |
| 747 | struct files_struct *files = current->files; |
| 748 | struct fdtable *fdt; |
| 749 | spin_lock(&files->file_lock); |
| 750 | fdt = files_fdtable(files); |
| 751 | if (flag) |
| 752 | __set_close_on_exec(fd, fdt); |
| 753 | else |
| 754 | __clear_close_on_exec(fd, fdt); |
| 755 | spin_unlock(&files->file_lock); |
| 756 | } |
| 757 | |
| 758 | bool get_close_on_exec(unsigned int fd) |
| 759 | { |
| 760 | struct files_struct *files = current->files; |
| 761 | struct fdtable *fdt; |
| 762 | bool res; |
| 763 | rcu_read_lock(); |
| 764 | fdt = files_fdtable(files); |
| 765 | res = close_on_exec(fd, fdt); |
| 766 | rcu_read_unlock(); |
| 767 | return res; |
| 768 | } |
| 769 | |
Al Viro | 8280d16 | 2012-08-21 12:11:46 -0400 | [diff] [blame] | 770 | static int do_dup2(struct files_struct *files, |
| 771 | struct file *file, unsigned fd, unsigned flags) |
| 772 | { |
| 773 | struct file *tofree; |
| 774 | struct fdtable *fdt; |
| 775 | |
| 776 | /* |
| 777 | * We need to detect attempts to do dup2() over allocated but still |
| 778 | * not finished descriptor. NB: OpenBSD avoids that at the price of |
| 779 | * extra work in their equivalent of fget() - they insert struct |
| 780 | * file immediately after grabbing descriptor, mark it larval if |
| 781 | * more work (e.g. actual opening) is needed and make sure that |
| 782 | * fget() treats larval files as absent. Potentially interesting, |
| 783 | * but while extra work in fget() is trivial, locking implications |
| 784 | * and amount of surgery on open()-related paths in VFS are not. |
| 785 | * FreeBSD fails with -EBADF in the same situation, NetBSD "solution" |
| 786 | * deadlocks in rather amusing ways, AFAICS. All of that is out of |
| 787 | * scope of POSIX or SUS, since neither considers shared descriptor |
| 788 | * tables and this condition does not arise without those. |
| 789 | */ |
| 790 | fdt = files_fdtable(files); |
| 791 | tofree = fdt->fd[fd]; |
| 792 | if (!tofree && fd_is_open(fd, fdt)) |
| 793 | goto Ebusy; |
| 794 | get_file(file); |
| 795 | rcu_assign_pointer(fdt->fd[fd], file); |
| 796 | __set_open_fd(fd, fdt); |
| 797 | if (flags & O_CLOEXEC) |
| 798 | __set_close_on_exec(fd, fdt); |
| 799 | else |
| 800 | __clear_close_on_exec(fd, fdt); |
| 801 | spin_unlock(&files->file_lock); |
| 802 | |
| 803 | if (tofree) |
| 804 | filp_close(tofree, files); |
| 805 | |
| 806 | return fd; |
| 807 | |
| 808 | Ebusy: |
| 809 | spin_unlock(&files->file_lock); |
| 810 | return -EBUSY; |
| 811 | } |
| 812 | |
| 813 | int replace_fd(unsigned fd, struct file *file, unsigned flags) |
| 814 | { |
| 815 | int err; |
| 816 | struct files_struct *files = current->files; |
| 817 | |
| 818 | if (!file) |
| 819 | return __close_fd(files, fd); |
| 820 | |
| 821 | if (fd >= rlimit(RLIMIT_NOFILE)) |
Al Viro | 08f05c4 | 2012-10-31 03:37:48 +0000 | [diff] [blame] | 822 | return -EBADF; |
Al Viro | 8280d16 | 2012-08-21 12:11:46 -0400 | [diff] [blame] | 823 | |
| 824 | spin_lock(&files->file_lock); |
| 825 | err = expand_files(files, fd); |
| 826 | if (unlikely(err < 0)) |
| 827 | goto out_unlock; |
| 828 | return do_dup2(files, file, fd, flags); |
| 829 | |
| 830 | out_unlock: |
| 831 | spin_unlock(&files->file_lock); |
| 832 | return err; |
| 833 | } |
| 834 | |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 835 | SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags) |
| 836 | { |
| 837 | int err = -EBADF; |
Al Viro | 8280d16 | 2012-08-21 12:11:46 -0400 | [diff] [blame] | 838 | struct file *file; |
| 839 | struct files_struct *files = current->files; |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 840 | |
| 841 | if ((flags & ~O_CLOEXEC) != 0) |
| 842 | return -EINVAL; |
| 843 | |
Richard W.M. Jones | aed9764 | 2012-10-09 15:27:43 +0100 | [diff] [blame] | 844 | if (unlikely(oldfd == newfd)) |
| 845 | return -EINVAL; |
| 846 | |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 847 | if (newfd >= rlimit(RLIMIT_NOFILE)) |
Al Viro | 08f05c4 | 2012-10-31 03:37:48 +0000 | [diff] [blame] | 848 | return -EBADF; |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 849 | |
| 850 | spin_lock(&files->file_lock); |
| 851 | err = expand_files(files, newfd); |
| 852 | file = fcheck(oldfd); |
| 853 | if (unlikely(!file)) |
| 854 | goto Ebadf; |
| 855 | if (unlikely(err < 0)) { |
| 856 | if (err == -EMFILE) |
| 857 | goto Ebadf; |
| 858 | goto out_unlock; |
| 859 | } |
Al Viro | 8280d16 | 2012-08-21 12:11:46 -0400 | [diff] [blame] | 860 | return do_dup2(files, file, newfd, flags); |
Al Viro | fe17f22 | 2012-08-21 11:48:11 -0400 | [diff] [blame] | 861 | |
| 862 | Ebadf: |
| 863 | err = -EBADF; |
| 864 | out_unlock: |
| 865 | spin_unlock(&files->file_lock); |
| 866 | return err; |
| 867 | } |
| 868 | |
| 869 | SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd) |
| 870 | { |
| 871 | if (unlikely(newfd == oldfd)) { /* corner case */ |
| 872 | struct files_struct *files = current->files; |
| 873 | int retval = oldfd; |
| 874 | |
| 875 | rcu_read_lock(); |
| 876 | if (!fcheck_files(files, oldfd)) |
| 877 | retval = -EBADF; |
| 878 | rcu_read_unlock(); |
| 879 | return retval; |
| 880 | } |
| 881 | return sys_dup3(oldfd, newfd, 0); |
| 882 | } |
| 883 | |
| 884 | SYSCALL_DEFINE1(dup, unsigned int, fildes) |
| 885 | { |
| 886 | int ret = -EBADF; |
| 887 | struct file *file = fget_raw(fildes); |
| 888 | |
| 889 | if (file) { |
| 890 | ret = get_unused_fd(); |
| 891 | if (ret >= 0) |
| 892 | fd_install(ret, file); |
| 893 | else |
| 894 | fput(file); |
| 895 | } |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | int f_dupfd(unsigned int from, struct file *file, unsigned flags) |
| 900 | { |
| 901 | int err; |
| 902 | if (from >= rlimit(RLIMIT_NOFILE)) |
| 903 | return -EINVAL; |
| 904 | err = alloc_fd(from, flags); |
| 905 | if (err >= 0) { |
| 906 | get_file(file); |
| 907 | fd_install(err, file); |
| 908 | } |
| 909 | return err; |
| 910 | } |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 911 | |
| 912 | int iterate_fd(struct files_struct *files, unsigned n, |
| 913 | int (*f)(const void *, struct file *, unsigned), |
| 914 | const void *p) |
| 915 | { |
| 916 | struct fdtable *fdt; |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 917 | int res = 0; |
| 918 | if (!files) |
| 919 | return 0; |
| 920 | spin_lock(&files->file_lock); |
Al Viro | a77cfcb | 2012-11-29 22:57:33 -0500 | [diff] [blame] | 921 | for (fdt = files_fdtable(files); n < fdt->max_fds; n++) { |
| 922 | struct file *file; |
| 923 | file = rcu_dereference_check_fdtable(files, fdt->fd[n]); |
| 924 | if (!file) |
| 925 | continue; |
| 926 | res = f(p, file, n); |
| 927 | if (res) |
| 928 | break; |
Al Viro | c3c073f | 2012-08-21 22:32:06 -0400 | [diff] [blame] | 929 | } |
| 930 | spin_unlock(&files->file_lock); |
| 931 | return res; |
| 932 | } |
| 933 | EXPORT_SYMBOL(iterate_fd); |