| 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 | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 9 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/fs.h> | 
|  | 11 | #include <linux/mm.h> | 
|  | 12 | #include <linux/time.h> | 
|  | 13 | #include <linux/slab.h> | 
|  | 14 | #include <linux/vmalloc.h> | 
|  | 15 | #include <linux/file.h> | 
| Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 16 | #include <linux/fdtable.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/bitops.h> | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 18 | #include <linux/interrupt.h> | 
|  | 19 | #include <linux/spinlock.h> | 
|  | 20 | #include <linux/rcupdate.h> | 
|  | 21 | #include <linux/workqueue.h> | 
|  | 22 |  | 
|  | 23 | struct fdtable_defer { | 
|  | 24 | spinlock_t lock; | 
|  | 25 | struct work_struct wq; | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 26 | struct fdtable *next; | 
|  | 27 | }; | 
|  | 28 |  | 
| Eric Dumazet | 9cfe015 | 2008-02-06 01:37:16 -0800 | [diff] [blame] | 29 | int sysctl_nr_open __read_mostly = 1024*1024; | 
| Al Viro | eceea0b | 2008-05-10 10:08:32 -0400 | [diff] [blame] | 30 | int sysctl_nr_open_min = BITS_PER_LONG; | 
|  | 31 | int sysctl_nr_open_max = 1024 * 1024; /* raised later */ | 
| Eric Dumazet | 9cfe015 | 2008-02-06 01:37:16 -0800 | [diff] [blame] | 32 |  | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 33 | /* | 
|  | 34 | * We use this list to defer free fdtables that have vmalloced | 
|  | 35 | * sets/arrays. By keeping a per-cpu list, we avoid having to embed | 
|  | 36 | * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in | 
|  | 37 | * this per-task structure. | 
|  | 38 | */ | 
|  | 39 | static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 41 | static inline void * alloc_fdmem(unsigned int size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | if (size <= PAGE_SIZE) | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 44 | return kmalloc(size, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | else | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 46 | return vmalloc(size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 49 | static inline void free_fdarr(struct fdtable *fdt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 51 | if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) | 
|  | 52 | kfree(fdt->fd); | 
|  | 53 | else | 
|  | 54 | vfree(fdt->fd); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | static inline void free_fdset(struct fdtable *fdt) | 
|  | 58 | { | 
|  | 59 | if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2)) | 
|  | 60 | kfree(fdt->open_fds); | 
|  | 61 | else | 
|  | 62 | vfree(fdt->open_fds); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 65 | static void free_fdtable_work(struct work_struct *work) | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 66 | { | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 67 | struct fdtable_defer *f = | 
|  | 68 | container_of(work, struct fdtable_defer, wq); | 
| Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 69 | struct fdtable *fdt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 71 | spin_lock_bh(&f->lock); | 
|  | 72 | fdt = f->next; | 
|  | 73 | f->next = NULL; | 
|  | 74 | spin_unlock_bh(&f->lock); | 
|  | 75 | while(fdt) { | 
|  | 76 | struct fdtable *next = fdt->next; | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 77 | vfree(fdt->fd); | 
|  | 78 | free_fdset(fdt); | 
|  | 79 | kfree(fdt); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 80 | fdt = next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Vadim Lobanov | 4fd4581 | 2006-12-10 02:21:17 -0800 | [diff] [blame] | 84 | void free_fdtable_rcu(struct rcu_head *rcu) | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 85 | { | 
|  | 86 | struct fdtable *fdt = container_of(rcu, struct fdtable, rcu); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 87 | struct fdtable_defer *fddef; | 
|  | 88 |  | 
|  | 89 | BUG_ON(!fdt); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 90 |  | 
| Vadim Lobanov | 4fd4581 | 2006-12-10 02:21:17 -0800 | [diff] [blame] | 91 | if (fdt->max_fds <= NR_OPEN_DEFAULT) { | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 92 | /* | 
| Vadim Lobanov | 4fd4581 | 2006-12-10 02:21:17 -0800 | [diff] [blame] | 93 | * This fdtable is embedded in the files structure and that | 
|  | 94 | * structure itself is getting destroyed. | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 95 | */ | 
| Vadim Lobanov | 4fd4581 | 2006-12-10 02:21:17 -0800 | [diff] [blame] | 96 | kmem_cache_free(files_cachep, | 
|  | 97 | container_of(fdt, struct files_struct, fdtab)); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 98 | return; | 
|  | 99 | } | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 100 | if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) { | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 101 | kfree(fdt->fd); | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 102 | kfree(fdt->open_fds); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 103 | kfree(fdt); | 
|  | 104 | } else { | 
|  | 105 | fddef = &get_cpu_var(fdtable_defer_list); | 
|  | 106 | spin_lock(&fddef->lock); | 
|  | 107 | fdt->next = fddef->next; | 
|  | 108 | fddef->next = fdt; | 
| Tejun Heo | 593be07 | 2006-12-06 20:36:01 -0800 | [diff] [blame] | 109 | /* vmallocs are handled from the workqueue context */ | 
|  | 110 | schedule_work(&fddef->wq); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 111 | spin_unlock(&fddef->lock); | 
|  | 112 | put_cpu_var(fdtable_defer_list); | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 |  | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 116 | /* | 
|  | 117 | * Expand the fdset in the files_struct.  Called with the files spinlock | 
|  | 118 | * held for write. | 
|  | 119 | */ | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 120 | static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt) | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 121 | { | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 122 | unsigned int cpy, set; | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 123 |  | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 124 | BUG_ON(nfdt->max_fds < ofdt->max_fds); | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 125 |  | 
|  | 126 | cpy = ofdt->max_fds * sizeof(struct file *); | 
|  | 127 | set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); | 
|  | 128 | memcpy(nfdt->fd, ofdt->fd, cpy); | 
|  | 129 | memset((char *)(nfdt->fd) + cpy, 0, set); | 
|  | 130 |  | 
|  | 131 | cpy = ofdt->max_fds / BITS_PER_BYTE; | 
|  | 132 | set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE; | 
|  | 133 | memcpy(nfdt->open_fds, ofdt->open_fds, cpy); | 
|  | 134 | memset((char *)(nfdt->open_fds) + cpy, 0, set); | 
|  | 135 | memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy); | 
|  | 136 | memset((char *)(nfdt->close_on_exec) + cpy, 0, set); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 139 | static struct fdtable * alloc_fdtable(unsigned int nr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 141 | struct fdtable *fdt; | 
|  | 142 | char *data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 |  | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 144 | /* | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 145 | * Figure out how many fds we actually want to support in this fdtable. | 
|  | 146 | * Allocation steps are keyed to the size of the fdarray, since it | 
|  | 147 | * grows far faster than any of the other dynamic data. We try to fit | 
|  | 148 | * the fdarray into comfortable page-tuned chunks: starting at 1024B | 
|  | 149 | * and growing in powers of two from there on. | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 150 | */ | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 151 | nr /= (1024 / sizeof(struct file *)); | 
|  | 152 | nr = roundup_pow_of_two(nr + 1); | 
|  | 153 | nr *= (1024 / sizeof(struct file *)); | 
| Al Viro | 5c598b3 | 2008-04-27 20:04:15 -0400 | [diff] [blame] | 154 | /* | 
|  | 155 | * Note that this can drive nr *below* what we had passed if sysctl_nr_open | 
|  | 156 | * had been set lower between the check in expand_files() and here.  Deal | 
|  | 157 | * with that in caller, it's cheaper that way. | 
|  | 158 | * | 
|  | 159 | * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise | 
|  | 160 | * bitmaps handling below becomes unpleasant, to put it mildly... | 
|  | 161 | */ | 
|  | 162 | if (unlikely(nr > sysctl_nr_open)) | 
|  | 163 | nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1; | 
| Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 164 |  | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 165 | fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL); | 
|  | 166 | if (!fdt) | 
| Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 167 | goto out; | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 168 | fdt->max_fds = nr; | 
|  | 169 | data = alloc_fdmem(nr * sizeof(struct file *)); | 
|  | 170 | if (!data) | 
|  | 171 | goto out_fdt; | 
|  | 172 | fdt->fd = (struct file **)data; | 
|  | 173 | data = alloc_fdmem(max_t(unsigned int, | 
|  | 174 | 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES)); | 
|  | 175 | if (!data) | 
|  | 176 | goto out_arr; | 
|  | 177 | fdt->open_fds = (fd_set *)data; | 
|  | 178 | data += nr / BITS_PER_BYTE; | 
|  | 179 | fdt->close_on_exec = (fd_set *)data; | 
|  | 180 | INIT_RCU_HEAD(&fdt->rcu); | 
|  | 181 | fdt->next = NULL; | 
|  | 182 |  | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 183 | return fdt; | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 184 |  | 
|  | 185 | out_arr: | 
|  | 186 | free_fdarr(fdt); | 
|  | 187 | out_fdt: | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 188 | kfree(fdt); | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 189 | out: | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 190 | return NULL; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | /* | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 194 | * Expand the file descriptor table. | 
|  | 195 | * This function will allocate a new fdtable and both fd array and fdset, of | 
|  | 196 | * the given size. | 
|  | 197 | * Return <0 error code on error; 1 on successful completion. | 
|  | 198 | * 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] | 199 | */ | 
|  | 200 | static int expand_fdtable(struct files_struct *files, int nr) | 
|  | 201 | __releases(files->file_lock) | 
|  | 202 | __acquires(files->file_lock) | 
|  | 203 | { | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 204 | struct fdtable *new_fdt, *cur_fdt; | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 205 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | spin_unlock(&files->file_lock); | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 207 | new_fdt = alloc_fdtable(nr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | spin_lock(&files->file_lock); | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 209 | if (!new_fdt) | 
|  | 210 | return -ENOMEM; | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 211 | /* | 
| Al Viro | 5c598b3 | 2008-04-27 20:04:15 -0400 | [diff] [blame] | 212 | * extremely unlikely race - sysctl_nr_open decreased between the check in | 
|  | 213 | * caller and alloc_fdtable().  Cheaper to catch it here... | 
|  | 214 | */ | 
|  | 215 | if (unlikely(new_fdt->max_fds <= nr)) { | 
|  | 216 | free_fdarr(new_fdt); | 
|  | 217 | free_fdset(new_fdt); | 
|  | 218 | kfree(new_fdt); | 
|  | 219 | return -EMFILE; | 
|  | 220 | } | 
|  | 221 | /* | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 222 | * Check again since another task may have expanded the fd table while | 
|  | 223 | * we dropped the lock | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 224 | */ | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 225 | cur_fdt = files_fdtable(files); | 
| Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 226 | if (nr >= cur_fdt->max_fds) { | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 227 | /* Continue as planned */ | 
|  | 228 | copy_fdtable(new_fdt, cur_fdt); | 
|  | 229 | rcu_assign_pointer(files->fdt, new_fdt); | 
| Vadim Lobanov | 4fd4581 | 2006-12-10 02:21:17 -0800 | [diff] [blame] | 230 | if (cur_fdt->max_fds > NR_OPEN_DEFAULT) | 
| Vadim Lobanov | 01b2d93 | 2006-12-22 01:10:43 -0800 | [diff] [blame] | 231 | free_fdtable(cur_fdt); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 232 | } else { | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 233 | /* Somebody else expanded, so undo our attempt */ | 
| Vadim Lobanov | 5466b45 | 2006-12-10 02:21:22 -0800 | [diff] [blame] | 234 | free_fdarr(new_fdt); | 
|  | 235 | free_fdset(new_fdt); | 
|  | 236 | kfree(new_fdt); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 237 | } | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 238 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
|  | 241 | /* | 
|  | 242 | * Expand files. | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 243 | * This function will expand the file structures, if the requested size exceeds | 
|  | 244 | * the current capacity and there is room for expansion. | 
|  | 245 | * Return <0 error code on error; 0 when nothing done; 1 when files were | 
|  | 246 | * expanded and execution may have blocked. | 
|  | 247 | * 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] | 248 | */ | 
|  | 249 | int expand_files(struct files_struct *files, int nr) | 
|  | 250 | { | 
| Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 251 | struct fdtable *fdt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 |  | 
| Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 253 | fdt = files_fdtable(files); | 
| Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 254 |  | 
|  | 255 | /* | 
|  | 256 | * N.B. For clone tasks sharing a files structure, this test | 
|  | 257 | * will limit the total number of files that can be opened. | 
|  | 258 | */ | 
|  | 259 | if (nr >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) | 
|  | 260 | return -EMFILE; | 
|  | 261 |  | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 262 | /* Do we need to expand? */ | 
| Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 263 | if (nr < fdt->max_fds) | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 264 | return 0; | 
| Al Viro | 4e1e018 | 2008-07-26 16:01:20 -0400 | [diff] [blame] | 265 |  | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 266 | /* Can we expand? */ | 
| Eric Dumazet | 9cfe015 | 2008-02-06 01:37:16 -0800 | [diff] [blame] | 267 | if (nr >= sysctl_nr_open) | 
| Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 268 | return -EMFILE; | 
|  | 269 |  | 
|  | 270 | /* All good, so we try */ | 
|  | 271 | return expand_fdtable(files, nr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 273 |  | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 274 | static int count_open_files(struct fdtable *fdt) | 
|  | 275 | { | 
|  | 276 | int size = fdt->max_fds; | 
|  | 277 | int i; | 
|  | 278 |  | 
|  | 279 | /* Find the last open fd */ | 
|  | 280 | for (i = size/(8*sizeof(long)); i > 0; ) { | 
|  | 281 | if (fdt->open_fds->fds_bits[--i]) | 
|  | 282 | break; | 
|  | 283 | } | 
|  | 284 | i = (i+1) * 8 * sizeof(long); | 
|  | 285 | return i; | 
|  | 286 | } | 
|  | 287 |  | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 288 | /* | 
|  | 289 | * Allocate a new files structure and copy contents from the | 
|  | 290 | * passed in files structure. | 
|  | 291 | * errorp will be valid only when the returned files_struct is NULL. | 
|  | 292 | */ | 
|  | 293 | struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) | 
|  | 294 | { | 
|  | 295 | struct files_struct *newf; | 
|  | 296 | struct file **old_fds, **new_fds; | 
|  | 297 | int open_files, size, i; | 
|  | 298 | struct fdtable *old_fdt, *new_fdt; | 
|  | 299 |  | 
|  | 300 | *errorp = -ENOMEM; | 
| Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 301 | newf = kmem_cache_alloc(files_cachep, GFP_KERNEL); | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 302 | if (!newf) | 
|  | 303 | goto out; | 
|  | 304 |  | 
| Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 305 | atomic_set(&newf->count, 1); | 
|  | 306 |  | 
|  | 307 | spin_lock_init(&newf->file_lock); | 
|  | 308 | newf->next_fd = 0; | 
|  | 309 | new_fdt = &newf->fdtab; | 
|  | 310 | new_fdt->max_fds = NR_OPEN_DEFAULT; | 
|  | 311 | new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init; | 
|  | 312 | new_fdt->open_fds = (fd_set *)&newf->open_fds_init; | 
|  | 313 | new_fdt->fd = &newf->fd_array[0]; | 
|  | 314 | INIT_RCU_HEAD(&new_fdt->rcu); | 
|  | 315 | new_fdt->next = NULL; | 
|  | 316 |  | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 317 | spin_lock(&oldf->file_lock); | 
|  | 318 | old_fdt = files_fdtable(oldf); | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 319 | open_files = count_open_files(old_fdt); | 
|  | 320 |  | 
|  | 321 | /* | 
|  | 322 | * Check whether we need to allocate a larger fd array and fd set. | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 323 | */ | 
| Al Viro | adbecb1 | 2008-05-08 21:19:42 -0400 | [diff] [blame] | 324 | while (unlikely(open_files > new_fdt->max_fds)) { | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 325 | spin_unlock(&oldf->file_lock); | 
| Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 326 |  | 
| Al Viro | adbecb1 | 2008-05-08 21:19:42 -0400 | [diff] [blame] | 327 | if (new_fdt != &newf->fdtab) { | 
|  | 328 | free_fdarr(new_fdt); | 
|  | 329 | free_fdset(new_fdt); | 
|  | 330 | kfree(new_fdt); | 
|  | 331 | } | 
|  | 332 |  | 
| Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 333 | new_fdt = alloc_fdtable(open_files - 1); | 
|  | 334 | if (!new_fdt) { | 
|  | 335 | *errorp = -ENOMEM; | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 336 | goto out_release; | 
| Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
|  | 339 | /* beyond sysctl_nr_open; nothing to do */ | 
|  | 340 | if (unlikely(new_fdt->max_fds < open_files)) { | 
|  | 341 | free_fdarr(new_fdt); | 
|  | 342 | free_fdset(new_fdt); | 
|  | 343 | kfree(new_fdt); | 
|  | 344 | *errorp = -EMFILE; | 
|  | 345 | goto out_release; | 
|  | 346 | } | 
| Al Viro | 9dec3c4 | 2008-05-08 21:02:45 -0400 | [diff] [blame] | 347 |  | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 348 | /* | 
|  | 349 | * Reacquire the oldf lock and a pointer to its fd table | 
|  | 350 | * who knows it may have a new bigger fd table. We need | 
|  | 351 | * the latest pointer. | 
|  | 352 | */ | 
|  | 353 | spin_lock(&oldf->file_lock); | 
|  | 354 | old_fdt = files_fdtable(oldf); | 
| Al Viro | adbecb1 | 2008-05-08 21:19:42 -0400 | [diff] [blame] | 355 | open_files = count_open_files(old_fdt); | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
|  | 358 | old_fds = old_fdt->fd; | 
|  | 359 | new_fds = new_fdt->fd; | 
|  | 360 |  | 
|  | 361 | memcpy(new_fdt->open_fds->fds_bits, | 
|  | 362 | old_fdt->open_fds->fds_bits, open_files/8); | 
|  | 363 | memcpy(new_fdt->close_on_exec->fds_bits, | 
|  | 364 | old_fdt->close_on_exec->fds_bits, open_files/8); | 
|  | 365 |  | 
|  | 366 | for (i = open_files; i != 0; i--) { | 
|  | 367 | struct file *f = *old_fds++; | 
|  | 368 | if (f) { | 
|  | 369 | get_file(f); | 
|  | 370 | } else { | 
|  | 371 | /* | 
|  | 372 | * The fd may be claimed in the fd bitmap but not yet | 
|  | 373 | * instantiated in the files array if a sibling thread | 
|  | 374 | * is partway through open().  So make sure that this | 
|  | 375 | * fd is available to the new process. | 
|  | 376 | */ | 
|  | 377 | FD_CLR(open_files - i, new_fdt->open_fds); | 
|  | 378 | } | 
|  | 379 | rcu_assign_pointer(*new_fds++, f); | 
|  | 380 | } | 
|  | 381 | spin_unlock(&oldf->file_lock); | 
|  | 382 |  | 
|  | 383 | /* compute the remainder to be cleared */ | 
|  | 384 | size = (new_fdt->max_fds - open_files) * sizeof(struct file *); | 
|  | 385 |  | 
|  | 386 | /* This is long word aligned thus could use a optimized version */ | 
|  | 387 | memset(new_fds, 0, size); | 
|  | 388 |  | 
|  | 389 | if (new_fdt->max_fds > open_files) { | 
|  | 390 | int left = (new_fdt->max_fds-open_files)/8; | 
|  | 391 | int start = open_files / (8 * sizeof(unsigned long)); | 
|  | 392 |  | 
|  | 393 | memset(&new_fdt->open_fds->fds_bits[start], 0, left); | 
|  | 394 | memset(&new_fdt->close_on_exec->fds_bits[start], 0, left); | 
|  | 395 | } | 
|  | 396 |  | 
| Al Viro | afbec7f | 2008-05-08 21:11:17 -0400 | [diff] [blame] | 397 | rcu_assign_pointer(newf->fdt, new_fdt); | 
|  | 398 |  | 
| Al Viro | 02afc62 | 2008-05-08 19:42:56 -0400 | [diff] [blame] | 399 | return newf; | 
|  | 400 |  | 
|  | 401 | out_release: | 
|  | 402 | kmem_cache_free(files_cachep, newf); | 
|  | 403 | out: | 
|  | 404 | return NULL; | 
|  | 405 | } | 
|  | 406 |  | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 407 | static void __devinit fdtable_defer_list_init(int cpu) | 
|  | 408 | { | 
|  | 409 | struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu); | 
|  | 410 | spin_lock_init(&fddef->lock); | 
| David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 411 | INIT_WORK(&fddef->wq, free_fdtable_work); | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 412 | fddef->next = NULL; | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | void __init files_defer_init(void) | 
|  | 416 | { | 
|  | 417 | int i; | 
| KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 418 | for_each_possible_cpu(i) | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 419 | fdtable_defer_list_init(i); | 
| Al Viro | eceea0b | 2008-05-10 10:08:32 -0400 | [diff] [blame] | 420 | sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) & | 
|  | 421 | -BITS_PER_LONG; | 
| Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 422 | } | 
| Al Viro | f52111b | 2008-05-08 18:19:16 -0400 | [diff] [blame] | 423 |  | 
|  | 424 | struct files_struct init_files = { | 
|  | 425 | .count		= ATOMIC_INIT(1), | 
|  | 426 | .fdt		= &init_files.fdtab, | 
|  | 427 | .fdtab		= { | 
|  | 428 | .max_fds	= NR_OPEN_DEFAULT, | 
|  | 429 | .fd		= &init_files.fd_array[0], | 
|  | 430 | .close_on_exec	= (fd_set *)&init_files.close_on_exec_init, | 
|  | 431 | .open_fds	= (fd_set *)&init_files.open_fds_init, | 
|  | 432 | .rcu		= RCU_HEAD_INIT, | 
|  | 433 | }, | 
|  | 434 | .file_lock	= __SPIN_LOCK_UNLOCKED(init_task.file_lock), | 
|  | 435 | }; | 
| Al Viro | 1027abe | 2008-07-30 04:13:04 -0400 | [diff] [blame] | 436 |  | 
|  | 437 | /* | 
|  | 438 | * allocate a file descriptor, mark it busy. | 
|  | 439 | */ | 
|  | 440 | int alloc_fd(unsigned start, unsigned flags) | 
|  | 441 | { | 
|  | 442 | struct files_struct *files = current->files; | 
|  | 443 | unsigned int fd; | 
|  | 444 | int error; | 
|  | 445 | struct fdtable *fdt; | 
|  | 446 |  | 
|  | 447 | spin_lock(&files->file_lock); | 
|  | 448 | repeat: | 
|  | 449 | fdt = files_fdtable(files); | 
|  | 450 | fd = start; | 
|  | 451 | if (fd < files->next_fd) | 
|  | 452 | fd = files->next_fd; | 
|  | 453 |  | 
|  | 454 | if (fd < fdt->max_fds) | 
|  | 455 | fd = find_next_zero_bit(fdt->open_fds->fds_bits, | 
|  | 456 | fdt->max_fds, fd); | 
|  | 457 |  | 
|  | 458 | error = expand_files(files, fd); | 
|  | 459 | if (error < 0) | 
|  | 460 | goto out; | 
|  | 461 |  | 
|  | 462 | /* | 
|  | 463 | * If we needed to expand the fs array we | 
|  | 464 | * might have blocked - try again. | 
|  | 465 | */ | 
|  | 466 | if (error) | 
|  | 467 | goto repeat; | 
|  | 468 |  | 
|  | 469 | if (start <= files->next_fd) | 
|  | 470 | files->next_fd = fd + 1; | 
|  | 471 |  | 
|  | 472 | FD_SET(fd, fdt->open_fds); | 
|  | 473 | if (flags & O_CLOEXEC) | 
|  | 474 | FD_SET(fd, fdt->close_on_exec); | 
|  | 475 | else | 
|  | 476 | FD_CLR(fd, fdt->close_on_exec); | 
|  | 477 | error = fd; | 
|  | 478 | #if 1 | 
|  | 479 | /* Sanity check */ | 
|  | 480 | if (rcu_dereference(fdt->fd[fd]) != NULL) { | 
|  | 481 | printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd); | 
|  | 482 | rcu_assign_pointer(fdt->fd[fd], NULL); | 
|  | 483 | } | 
|  | 484 | #endif | 
|  | 485 |  | 
|  | 486 | out: | 
|  | 487 | spin_unlock(&files->file_lock); | 
|  | 488 | return error; | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | int get_unused_fd(void) | 
|  | 492 | { | 
|  | 493 | return alloc_fd(0, 0); | 
|  | 494 | } | 
|  | 495 | EXPORT_SYMBOL(get_unused_fd); |