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