Add to mips64 also for symmetry.
[linux-2.6/linux-mips.git] / fs / select.c
blob079155c57a1478274fbe34309e31f8997f7feb8b
1 /*
2 * This file contains the procedures for the handling of select and poll
4 * Created for Linux based loosely upon Mathius Lattner's minix
5 * patches by Peter MacDonald. Heavily edited by Linus.
7 * 4 February 1994
8 * COFF/ELF binary emulation. If the process has the STICKY_TIMEOUTS
9 * flag set in its personality we do *not* modify the given timeout
10 * parameter to reflect time remaining.
12 * 24 January 2000
13 * Changed sys_poll()/do_poll() to use PAGE_SIZE chunk-based allocation
14 * of fds to overcome nfds < 16390 descriptors limit (Tigran Aivazian).
17 #include <linux/slab.h>
18 #include <linux/smp_lock.h>
19 #include <linux/poll.h>
20 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
21 #include <linux/file.h>
22 #include <linux/fs.h>
24 #include <asm/uaccess.h>
26 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
27 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
29 struct poll_table_entry {
30 struct file * filp;
31 wait_queue_t wait;
32 wait_queue_head_t * wait_address;
35 struct poll_table_page {
36 struct poll_table_page * next;
37 struct poll_table_entry * entry;
38 struct poll_table_entry entries[0];
41 #define POLL_TABLE_FULL(table) \
42 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
45 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
46 * I have rewritten this, taking some shortcuts: This code may not be easy to
47 * follow, but it should be free of race-conditions, and it's practical. If you
48 * understand what I'm doing here, then you understand how the linux
49 * sleep/wakeup mechanism works.
51 * Two very simple procedures, poll_wait() and poll_freewait() make all the
52 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
53 * as all select/poll functions have to call it to add an entry to the
54 * poll table.
56 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p);
58 void poll_initwait(struct poll_wqueues *pwq)
60 init_poll_funcptr(&pwq->pt, __pollwait);
61 pwq->error = 0;
62 pwq->table = NULL;
65 void poll_freewait(struct poll_wqueues *pwq)
67 struct poll_table_page * p = pwq->table;
68 while (p) {
69 struct poll_table_entry * entry;
70 struct poll_table_page *old;
72 entry = p->entry;
73 do {
74 entry--;
75 remove_wait_queue(entry->wait_address,&entry->wait);
76 fput(entry->filp);
77 } while (entry > p->entries);
78 old = p;
79 p = p->next;
80 free_page((unsigned long) old);
84 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *_p)
86 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
87 struct poll_table_page *table = p->table;
89 if (!table || POLL_TABLE_FULL(table)) {
90 struct poll_table_page *new_table;
92 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
93 if (!new_table) {
94 p->error = -ENOMEM;
95 __set_current_state(TASK_RUNNING);
96 return;
98 new_table->entry = new_table->entries;
99 new_table->next = table;
100 p->table = new_table;
101 table = new_table;
104 /* Add a new entry */
106 struct poll_table_entry * entry = table->entry;
107 table->entry = entry+1;
108 get_file(filp);
109 entry->filp = filp;
110 entry->wait_address = wait_address;
111 init_waitqueue_entry(&entry->wait, current);
112 add_wait_queue(wait_address,&entry->wait);
117 #define __IN(fds, n) (fds->in + n)
118 #define __OUT(fds, n) (fds->out + n)
119 #define __EX(fds, n) (fds->ex + n)
120 #define __RES_IN(fds, n) (fds->res_in + n)
121 #define __RES_OUT(fds, n) (fds->res_out + n)
122 #define __RES_EX(fds, n) (fds->res_ex + n)
124 #define BITS(fds, n) (*__IN(fds, n)|*__OUT(fds, n)|*__EX(fds, n))
126 static int max_select_fd(unsigned long n, fd_set_bits *fds)
128 unsigned long *open_fds;
129 unsigned long set;
130 int max;
132 /* handle last in-complete long-word first */
133 set = ~(~0UL << (n & (__NFDBITS-1)));
134 n /= __NFDBITS;
135 open_fds = current->files->open_fds->fds_bits+n;
136 max = 0;
137 if (set) {
138 set &= BITS(fds, n);
139 if (set) {
140 if (!(set & ~*open_fds))
141 goto get_max;
142 return -EBADF;
145 while (n) {
146 open_fds--;
147 n--;
148 set = BITS(fds, n);
149 if (!set)
150 continue;
151 if (set & ~*open_fds)
152 return -EBADF;
153 if (max)
154 continue;
155 get_max:
156 do {
157 max++;
158 set >>= 1;
159 } while (set);
160 max += n * __NFDBITS;
163 return max;
166 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
167 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
168 #define ISSET(i,m) (((i)&*(m)) != 0)
169 #define SET(i,m) (*(m) |= (i))
171 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
172 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
173 #define POLLEX_SET (POLLPRI)
175 int do_select(int n, fd_set_bits *fds, long *timeout)
177 struct poll_wqueues table;
178 poll_table *wait;
179 int retval, i;
180 long __timeout = *timeout;
182 spin_lock(&current->files->file_lock);
183 retval = max_select_fd(n, fds);
184 spin_unlock(&current->files->file_lock);
186 if (retval < 0)
187 return retval;
188 n = retval;
190 poll_initwait(&table);
191 wait = &table.pt;
192 if (!__timeout)
193 wait = NULL;
194 retval = 0;
195 for (;;) {
196 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
198 set_current_state(TASK_INTERRUPTIBLE);
200 inp = fds->in; outp = fds->out; exp = fds->ex;
201 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
203 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
204 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
205 unsigned long res_in = 0, res_out = 0, res_ex = 0;
206 struct file_operations *f_op = NULL;
207 struct file *file = NULL;
209 in = *inp++; out = *outp++; ex = *exp++;
210 all_bits = in | out | ex;
211 if (all_bits == 0) {
212 i += __NFDBITS;
213 continue;
216 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
217 if (i >= n)
218 break;
219 if (!(bit & all_bits))
220 continue;
221 file = fget(i);
222 if (file) {
223 f_op = file->f_op;
224 mask = DEFAULT_POLLMASK;
225 if (f_op && f_op->poll)
226 mask = (*f_op->poll)(file, retval ? NULL : wait);
227 fput(file);
228 if ((mask & POLLIN_SET) && (in & bit)) {
229 res_in |= bit;
230 retval++;
232 if ((mask & POLLOUT_SET) && (out & bit)) {
233 res_out |= bit;
234 retval++;
236 if ((mask & POLLEX_SET) && (ex & bit)) {
237 res_ex |= bit;
238 retval++;
242 if (res_in)
243 *rinp = res_in;
244 if (res_out)
245 *routp = res_out;
246 if (res_ex)
247 *rexp = res_ex;
249 wait = NULL;
250 if (retval || !__timeout || signal_pending(current))
251 break;
252 if(table.error) {
253 retval = table.error;
254 break;
256 __timeout = schedule_timeout(__timeout);
258 __set_current_state(TASK_RUNNING);
260 poll_freewait(&table);
263 * Up-to-date the caller timeout.
265 *timeout = __timeout;
266 return retval;
269 static void *select_bits_alloc(int size)
271 return kmalloc(6 * size, GFP_KERNEL);
274 static void select_bits_free(void *bits, int size)
276 kfree(bits);
280 * We can actually return ERESTARTSYS instead of EINTR, but I'd
281 * like to be certain this leads to no problems. So I return
282 * EINTR just for safety.
284 * Update: ERESTARTSYS breaks at least the xview clock binary, so
285 * I'm trying ERESTARTNOHAND which restart only when you want to.
287 #define MAX_SELECT_SECONDS \
288 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
290 asmlinkage long
291 sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
293 fd_set_bits fds;
294 char *bits;
295 long timeout;
296 int ret, size, max_fdset;
298 timeout = MAX_SCHEDULE_TIMEOUT;
299 if (tvp) {
300 time_t sec, usec;
302 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
303 || (ret = __get_user(sec, &tvp->tv_sec))
304 || (ret = __get_user(usec, &tvp->tv_usec)))
305 goto out_nofds;
307 ret = -EINVAL;
308 if (sec < 0 || usec < 0)
309 goto out_nofds;
311 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
312 timeout = ROUND_UP(usec, 1000000/HZ);
313 timeout += sec * (unsigned long) HZ;
317 ret = -EINVAL;
318 if (n < 0)
319 goto out_nofds;
321 /* max_fdset can increase, so grab it once to avoid race */
322 max_fdset = current->files->max_fdset;
323 if (n > max_fdset)
324 n = max_fdset;
327 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
328 * since we used fdset we need to allocate memory in units of
329 * long-words.
331 ret = -ENOMEM;
332 size = FDS_BYTES(n);
333 bits = select_bits_alloc(size);
334 if (!bits)
335 goto out_nofds;
336 fds.in = (unsigned long *) bits;
337 fds.out = (unsigned long *) (bits + size);
338 fds.ex = (unsigned long *) (bits + 2*size);
339 fds.res_in = (unsigned long *) (bits + 3*size);
340 fds.res_out = (unsigned long *) (bits + 4*size);
341 fds.res_ex = (unsigned long *) (bits + 5*size);
343 if ((ret = get_fd_set(n, inp, fds.in)) ||
344 (ret = get_fd_set(n, outp, fds.out)) ||
345 (ret = get_fd_set(n, exp, fds.ex)))
346 goto out;
347 zero_fd_set(n, fds.res_in);
348 zero_fd_set(n, fds.res_out);
349 zero_fd_set(n, fds.res_ex);
351 ret = do_select(n, &fds, &timeout);
353 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
354 time_t sec = 0, usec = 0;
355 if (timeout) {
356 sec = timeout / HZ;
357 usec = timeout % HZ;
358 usec *= (1000000/HZ);
360 put_user(sec, &tvp->tv_sec);
361 put_user(usec, &tvp->tv_usec);
364 if (ret < 0)
365 goto out;
366 if (!ret) {
367 ret = -ERESTARTNOHAND;
368 if (signal_pending(current))
369 goto out;
370 ret = 0;
373 set_fd_set(n, inp, fds.res_in);
374 set_fd_set(n, outp, fds.res_out);
375 set_fd_set(n, exp, fds.res_ex);
377 out:
378 select_bits_free(bits, size);
379 out_nofds:
380 return ret;
383 struct poll_list {
384 struct poll_list *next;
385 int len;
386 struct pollfd entries[0];
389 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
391 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
392 poll_table ** pwait, int *count)
394 int i;
396 for (i = 0; i < num; i++) {
397 int fd;
398 unsigned int mask;
399 struct pollfd *fdp;
401 mask = 0;
402 fdp = fdpage+i;
403 fd = fdp->fd;
404 if (fd >= 0) {
405 struct file * file = fget(fd);
406 mask = POLLNVAL;
407 if (file != NULL) {
408 mask = DEFAULT_POLLMASK;
409 if (file->f_op && file->f_op->poll)
410 mask = file->f_op->poll(file, *pwait);
411 mask &= fdp->events | POLLERR | POLLHUP;
412 fput(file);
414 if (mask) {
415 *pwait = NULL;
416 (*count)++;
419 fdp->revents = mask;
423 static int do_poll(unsigned int nfds, struct poll_list *list,
424 struct poll_wqueues *wait, long timeout)
426 int count = 0;
427 poll_table* pt = &wait->pt;
429 if (!timeout)
430 pt = NULL;
432 for (;;) {
433 struct poll_list *walk;
434 set_current_state(TASK_INTERRUPTIBLE);
435 walk = list;
436 while(walk != NULL) {
437 do_pollfd( walk->len, walk->entries, &pt, &count);
438 walk = walk->next;
440 pt = NULL;
441 if (count || !timeout || signal_pending(current))
442 break;
443 count = wait->error;
444 if (count)
445 break;
446 timeout = schedule_timeout(timeout);
448 __set_current_state(TASK_RUNNING);
449 return count;
452 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
454 struct poll_wqueues table;
455 int fdcount, err;
456 unsigned int i;
457 struct poll_list *head;
458 struct poll_list *walk;
460 /* Do a sanity check on nfds ... */
461 if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
462 return -EINVAL;
464 if (timeout) {
465 /* Careful about overflow in the intermediate values */
466 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
467 timeout = (unsigned long)(timeout*HZ+999)/1000+1;
468 else /* Negative or overflow */
469 timeout = MAX_SCHEDULE_TIMEOUT;
472 poll_initwait(&table);
474 head = NULL;
475 walk = NULL;
476 i = nfds;
477 err = -ENOMEM;
478 while(i!=0) {
479 struct poll_list *pp;
480 pp = kmalloc(sizeof(struct poll_list)+
481 sizeof(struct pollfd)*
482 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
483 GFP_KERNEL);
484 if(pp==NULL)
485 goto out_fds;
486 pp->next=NULL;
487 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
488 if (head == NULL)
489 head = pp;
490 else
491 walk->next = pp;
493 walk = pp;
494 if (copy_from_user(pp->entries, ufds + nfds-i,
495 sizeof(struct pollfd)*pp->len)) {
496 err = -EFAULT;
497 goto out_fds;
499 i -= pp->len;
501 fdcount = do_poll(nfds, head, &table, timeout);
503 /* OK, now copy the revents fields back to user space. */
504 walk = head;
505 err = -EFAULT;
506 while(walk != NULL) {
507 struct pollfd *fds = walk->entries;
508 int j;
510 for (j=0; j < walk->len; j++, ufds++) {
511 if(__put_user(fds[j].revents, &ufds->revents))
512 goto out_fds;
514 walk = walk->next;
516 err = fdcount;
517 if (!fdcount && signal_pending(current))
518 err = -EINTR;
519 out_fds:
520 walk = head;
521 while(walk!=NULL) {
522 struct poll_list *pp = walk->next;
523 kfree(walk);
524 walk = pp;
526 poll_freewait(&table);
527 return err;