[SERIAL] serial8250: convert to the new platform device interface
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / select.c
blobf10a10317d5494e0eb4995d9c016113a093f34ee
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/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/smp_lock.h>
21 #include <linux/poll.h>
22 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
23 #include <linux/file.h>
24 #include <linux/fs.h>
25 #include <linux/rcupdate.h>
27 #include <asm/uaccess.h>
29 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
30 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
32 struct poll_table_entry {
33 struct file * filp;
34 wait_queue_t wait;
35 wait_queue_head_t * wait_address;
38 struct poll_table_page {
39 struct poll_table_page * next;
40 struct poll_table_entry * entry;
41 struct poll_table_entry entries[0];
44 #define POLL_TABLE_FULL(table) \
45 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
48 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
49 * I have rewritten this, taking some shortcuts: This code may not be easy to
50 * follow, but it should be free of race-conditions, and it's practical. If you
51 * understand what I'm doing here, then you understand how the linux
52 * sleep/wakeup mechanism works.
54 * Two very simple procedures, poll_wait() and poll_freewait() make all the
55 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
56 * as all select/poll functions have to call it to add an entry to the
57 * poll table.
59 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
60 poll_table *p);
62 void poll_initwait(struct poll_wqueues *pwq)
64 init_poll_funcptr(&pwq->pt, __pollwait);
65 pwq->error = 0;
66 pwq->table = NULL;
69 EXPORT_SYMBOL(poll_initwait);
71 void poll_freewait(struct poll_wqueues *pwq)
73 struct poll_table_page * p = pwq->table;
74 while (p) {
75 struct poll_table_entry * entry;
76 struct poll_table_page *old;
78 entry = p->entry;
79 do {
80 entry--;
81 remove_wait_queue(entry->wait_address,&entry->wait);
82 fput(entry->filp);
83 } while (entry > p->entries);
84 old = p;
85 p = p->next;
86 free_page((unsigned long) old);
90 EXPORT_SYMBOL(poll_freewait);
92 static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
93 poll_table *_p)
95 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
96 struct poll_table_page *table = p->table;
98 if (!table || POLL_TABLE_FULL(table)) {
99 struct poll_table_page *new_table;
101 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
102 if (!new_table) {
103 p->error = -ENOMEM;
104 __set_current_state(TASK_RUNNING);
105 return;
107 new_table->entry = new_table->entries;
108 new_table->next = table;
109 p->table = new_table;
110 table = new_table;
113 /* Add a new entry */
115 struct poll_table_entry * entry = table->entry;
116 table->entry = entry+1;
117 get_file(filp);
118 entry->filp = filp;
119 entry->wait_address = wait_address;
120 init_waitqueue_entry(&entry->wait, current);
121 add_wait_queue(wait_address,&entry->wait);
125 #define FDS_IN(fds, n) (fds->in + n)
126 #define FDS_OUT(fds, n) (fds->out + n)
127 #define FDS_EX(fds, n) (fds->ex + n)
129 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
131 static int max_select_fd(unsigned long n, fd_set_bits *fds)
133 unsigned long *open_fds;
134 unsigned long set;
135 int max;
136 struct fdtable *fdt;
138 /* handle last in-complete long-word first */
139 set = ~(~0UL << (n & (__NFDBITS-1)));
140 n /= __NFDBITS;
141 fdt = files_fdtable(current->files);
142 open_fds = fdt->open_fds->fds_bits+n;
143 max = 0;
144 if (set) {
145 set &= BITS(fds, n);
146 if (set) {
147 if (!(set & ~*open_fds))
148 goto get_max;
149 return -EBADF;
152 while (n) {
153 open_fds--;
154 n--;
155 set = BITS(fds, n);
156 if (!set)
157 continue;
158 if (set & ~*open_fds)
159 return -EBADF;
160 if (max)
161 continue;
162 get_max:
163 do {
164 max++;
165 set >>= 1;
166 } while (set);
167 max += n * __NFDBITS;
170 return max;
173 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
174 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
175 #define ISSET(i,m) (((i)&*(m)) != 0)
176 #define SET(i,m) (*(m) |= (i))
178 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
179 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
180 #define POLLEX_SET (POLLPRI)
182 int do_select(int n, fd_set_bits *fds, long *timeout)
184 struct poll_wqueues table;
185 poll_table *wait;
186 int retval, i;
187 long __timeout = *timeout;
189 rcu_read_lock();
190 retval = max_select_fd(n, fds);
191 rcu_read_unlock();
193 if (retval < 0)
194 return retval;
195 n = retval;
197 poll_initwait(&table);
198 wait = &table.pt;
199 if (!__timeout)
200 wait = NULL;
201 retval = 0;
202 for (;;) {
203 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
205 set_current_state(TASK_INTERRUPTIBLE);
207 inp = fds->in; outp = fds->out; exp = fds->ex;
208 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
210 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
211 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
212 unsigned long res_in = 0, res_out = 0, res_ex = 0;
213 struct file_operations *f_op = NULL;
214 struct file *file = NULL;
216 in = *inp++; out = *outp++; ex = *exp++;
217 all_bits = in | out | ex;
218 if (all_bits == 0) {
219 i += __NFDBITS;
220 continue;
223 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
224 if (i >= n)
225 break;
226 if (!(bit & all_bits))
227 continue;
228 file = fget(i);
229 if (file) {
230 f_op = file->f_op;
231 mask = DEFAULT_POLLMASK;
232 if (f_op && f_op->poll)
233 mask = (*f_op->poll)(file, retval ? NULL : wait);
234 fput(file);
235 if ((mask & POLLIN_SET) && (in & bit)) {
236 res_in |= bit;
237 retval++;
239 if ((mask & POLLOUT_SET) && (out & bit)) {
240 res_out |= bit;
241 retval++;
243 if ((mask & POLLEX_SET) && (ex & bit)) {
244 res_ex |= bit;
245 retval++;
248 cond_resched();
250 if (res_in)
251 *rinp = res_in;
252 if (res_out)
253 *routp = res_out;
254 if (res_ex)
255 *rexp = res_ex;
257 wait = NULL;
258 if (retval || !__timeout || signal_pending(current))
259 break;
260 if(table.error) {
261 retval = table.error;
262 break;
264 __timeout = schedule_timeout(__timeout);
266 __set_current_state(TASK_RUNNING);
268 poll_freewait(&table);
271 * Up-to-date the caller timeout.
273 *timeout = __timeout;
274 return retval;
277 static void *select_bits_alloc(int size)
279 return kmalloc(6 * size, GFP_KERNEL);
282 static void select_bits_free(void *bits, int size)
284 kfree(bits);
288 * We can actually return ERESTARTSYS instead of EINTR, but I'd
289 * like to be certain this leads to no problems. So I return
290 * EINTR just for safety.
292 * Update: ERESTARTSYS breaks at least the xview clock binary, so
293 * I'm trying ERESTARTNOHAND which restart only when you want to.
295 #define MAX_SELECT_SECONDS \
296 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
298 asmlinkage long
299 sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
301 fd_set_bits fds;
302 char *bits;
303 long timeout;
304 int ret, size, max_fdset;
305 struct fdtable *fdt;
307 timeout = MAX_SCHEDULE_TIMEOUT;
308 if (tvp) {
309 time_t sec, usec;
311 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
312 || __get_user(sec, &tvp->tv_sec)
313 || __get_user(usec, &tvp->tv_usec)) {
314 ret = -EFAULT;
315 goto out_nofds;
318 ret = -EINVAL;
319 if (sec < 0 || usec < 0)
320 goto out_nofds;
322 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
323 timeout = ROUND_UP(usec, 1000000/HZ);
324 timeout += sec * (unsigned long) HZ;
328 ret = -EINVAL;
329 if (n < 0)
330 goto out_nofds;
332 /* max_fdset can increase, so grab it once to avoid race */
333 rcu_read_lock();
334 fdt = files_fdtable(current->files);
335 max_fdset = fdt->max_fdset;
336 rcu_read_unlock();
337 if (n > max_fdset)
338 n = max_fdset;
341 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
342 * since we used fdset we need to allocate memory in units of
343 * long-words.
345 ret = -ENOMEM;
346 size = FDS_BYTES(n);
347 bits = select_bits_alloc(size);
348 if (!bits)
349 goto out_nofds;
350 fds.in = (unsigned long *) bits;
351 fds.out = (unsigned long *) (bits + size);
352 fds.ex = (unsigned long *) (bits + 2*size);
353 fds.res_in = (unsigned long *) (bits + 3*size);
354 fds.res_out = (unsigned long *) (bits + 4*size);
355 fds.res_ex = (unsigned long *) (bits + 5*size);
357 if ((ret = get_fd_set(n, inp, fds.in)) ||
358 (ret = get_fd_set(n, outp, fds.out)) ||
359 (ret = get_fd_set(n, exp, fds.ex)))
360 goto out;
361 zero_fd_set(n, fds.res_in);
362 zero_fd_set(n, fds.res_out);
363 zero_fd_set(n, fds.res_ex);
365 ret = do_select(n, &fds, &timeout);
367 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
368 time_t sec = 0, usec = 0;
369 if (timeout) {
370 sec = timeout / HZ;
371 usec = timeout % HZ;
372 usec *= (1000000/HZ);
374 put_user(sec, &tvp->tv_sec);
375 put_user(usec, &tvp->tv_usec);
378 if (ret < 0)
379 goto out;
380 if (!ret) {
381 ret = -ERESTARTNOHAND;
382 if (signal_pending(current))
383 goto out;
384 ret = 0;
387 if (set_fd_set(n, inp, fds.res_in) ||
388 set_fd_set(n, outp, fds.res_out) ||
389 set_fd_set(n, exp, fds.res_ex))
390 ret = -EFAULT;
392 out:
393 select_bits_free(bits, size);
394 out_nofds:
395 return ret;
398 struct poll_list {
399 struct poll_list *next;
400 int len;
401 struct pollfd entries[0];
404 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
406 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
407 poll_table ** pwait, int *count)
409 int i;
411 for (i = 0; i < num; i++) {
412 int fd;
413 unsigned int mask;
414 struct pollfd *fdp;
416 mask = 0;
417 fdp = fdpage+i;
418 fd = fdp->fd;
419 if (fd >= 0) {
420 struct file * file = fget(fd);
421 mask = POLLNVAL;
422 if (file != NULL) {
423 mask = DEFAULT_POLLMASK;
424 if (file->f_op && file->f_op->poll)
425 mask = file->f_op->poll(file, *pwait);
426 mask &= fdp->events | POLLERR | POLLHUP;
427 fput(file);
429 if (mask) {
430 *pwait = NULL;
431 (*count)++;
434 fdp->revents = mask;
438 static int do_poll(unsigned int nfds, struct poll_list *list,
439 struct poll_wqueues *wait, long timeout)
441 int count = 0;
442 poll_table* pt = &wait->pt;
444 if (!timeout)
445 pt = NULL;
447 for (;;) {
448 struct poll_list *walk;
449 set_current_state(TASK_INTERRUPTIBLE);
450 walk = list;
451 while(walk != NULL) {
452 do_pollfd( walk->len, walk->entries, &pt, &count);
453 walk = walk->next;
455 pt = NULL;
456 if (count || !timeout || signal_pending(current))
457 break;
458 count = wait->error;
459 if (count)
460 break;
461 timeout = schedule_timeout(timeout);
463 __set_current_state(TASK_RUNNING);
464 return count;
467 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
469 struct poll_wqueues table;
470 int fdcount, err;
471 unsigned int i;
472 struct poll_list *head;
473 struct poll_list *walk;
474 struct fdtable *fdt;
475 int max_fdset;
477 /* Do a sanity check on nfds ... */
478 rcu_read_lock();
479 fdt = files_fdtable(current->files);
480 max_fdset = fdt->max_fdset;
481 rcu_read_unlock();
482 if (nfds > max_fdset && nfds > OPEN_MAX)
483 return -EINVAL;
485 if (timeout) {
486 /* Careful about overflow in the intermediate values */
487 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
488 timeout = (unsigned long)(timeout*HZ+999)/1000+1;
489 else /* Negative or overflow */
490 timeout = MAX_SCHEDULE_TIMEOUT;
493 poll_initwait(&table);
495 head = NULL;
496 walk = NULL;
497 i = nfds;
498 err = -ENOMEM;
499 while(i!=0) {
500 struct poll_list *pp;
501 pp = kmalloc(sizeof(struct poll_list)+
502 sizeof(struct pollfd)*
503 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
504 GFP_KERNEL);
505 if(pp==NULL)
506 goto out_fds;
507 pp->next=NULL;
508 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
509 if (head == NULL)
510 head = pp;
511 else
512 walk->next = pp;
514 walk = pp;
515 if (copy_from_user(pp->entries, ufds + nfds-i,
516 sizeof(struct pollfd)*pp->len)) {
517 err = -EFAULT;
518 goto out_fds;
520 i -= pp->len;
522 fdcount = do_poll(nfds, head, &table, timeout);
524 /* OK, now copy the revents fields back to user space. */
525 walk = head;
526 err = -EFAULT;
527 while(walk != NULL) {
528 struct pollfd *fds = walk->entries;
529 int j;
531 for (j=0; j < walk->len; j++, ufds++) {
532 if(__put_user(fds[j].revents, &ufds->revents))
533 goto out_fds;
535 walk = walk->next;
537 err = fdcount;
538 if (!fdcount && signal_pending(current))
539 err = -EINTR;
540 out_fds:
541 walk = head;
542 while(walk!=NULL) {
543 struct poll_list *pp = walk->next;
544 kfree(walk);
545 walk = pp;
547 poll_freewait(&table);
548 return err;