MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / select.c
blobdfff0ad1baf5231e492dc7d1c1f314afaf94adac
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/module.h>
18 #include <linux/slab.h>
19 #include <linux/smp_lock.h>
20 #include <linux/poll.h>
21 #include <linux/personality.h> /* for STICKY_TIMEOUTS */
22 #include <linux/file.h>
23 #include <linux/fs.h>
25 #include <asm/uaccess.h>
27 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
28 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
30 struct poll_table_entry {
31 struct file * filp;
32 wait_queue_t wait;
33 wait_queue_head_t * wait_address;
36 struct poll_table_page {
37 struct poll_table_page * next;
38 struct poll_table_entry * entry;
39 struct poll_table_entry entries[0];
42 #define POLL_TABLE_FULL(table) \
43 ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
46 * Ok, Peter made a complicated, but straightforward multiple_wait() function.
47 * I have rewritten this, taking some shortcuts: This code may not be easy to
48 * follow, but it should be free of race-conditions, and it's practical. If you
49 * understand what I'm doing here, then you understand how the linux
50 * sleep/wakeup mechanism works.
52 * Two very simple procedures, poll_wait() and poll_freewait() make all the
53 * work. poll_wait() is an inline-function defined in <linux/poll.h>,
54 * as all select/poll functions have to call it to add an entry to the
55 * poll table.
57 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p);
59 void poll_initwait(struct poll_wqueues *pwq)
61 init_poll_funcptr(&pwq->pt, __pollwait);
62 pwq->error = 0;
63 pwq->table = NULL;
66 EXPORT_SYMBOL(poll_initwait);
68 void poll_freewait(struct poll_wqueues *pwq)
70 struct poll_table_page * p = pwq->table;
71 while (p) {
72 struct poll_table_entry * entry;
73 struct poll_table_page *old;
75 entry = p->entry;
76 do {
77 entry--;
78 remove_wait_queue(entry->wait_address,&entry->wait);
79 fput(entry->filp);
80 } while (entry > p->entries);
81 old = p;
82 p = p->next;
83 free_page((unsigned long) old);
87 EXPORT_SYMBOL(poll_freewait);
89 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *_p)
91 struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
92 struct poll_table_page *table = p->table;
94 if (!table || POLL_TABLE_FULL(table)) {
95 struct poll_table_page *new_table;
97 new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
98 if (!new_table) {
99 p->error = -ENOMEM;
100 __set_current_state(TASK_RUNNING);
101 return;
103 new_table->entry = new_table->entries;
104 new_table->next = table;
105 p->table = new_table;
106 table = new_table;
109 /* Add a new entry */
111 struct poll_table_entry * entry = table->entry;
112 table->entry = entry+1;
113 get_file(filp);
114 entry->filp = filp;
115 entry->wait_address = wait_address;
116 init_waitqueue_entry(&entry->wait, current);
117 add_wait_queue(wait_address,&entry->wait);
121 #define FDS_IN(fds, n) (fds->in + n)
122 #define FDS_OUT(fds, n) (fds->out + n)
123 #define FDS_EX(fds, n) (fds->ex + n)
125 #define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n))
127 static int max_select_fd(unsigned long n, fd_set_bits *fds)
129 unsigned long *open_fds;
130 unsigned long set;
131 int max;
133 /* handle last in-complete long-word first */
134 set = ~(~0UL << (n & (__NFDBITS-1)));
135 n /= __NFDBITS;
136 open_fds = current->files->open_fds->fds_bits+n;
137 max = 0;
138 if (set) {
139 set &= BITS(fds, n);
140 if (set) {
141 if (!(set & ~*open_fds))
142 goto get_max;
143 return -EBADF;
146 while (n) {
147 open_fds--;
148 n--;
149 set = BITS(fds, n);
150 if (!set)
151 continue;
152 if (set & ~*open_fds)
153 return -EBADF;
154 if (max)
155 continue;
156 get_max:
157 do {
158 max++;
159 set >>= 1;
160 } while (set);
161 max += n * __NFDBITS;
164 return max;
167 #define BIT(i) (1UL << ((i)&(__NFDBITS-1)))
168 #define MEM(i,m) ((m)+(unsigned)(i)/__NFDBITS)
169 #define ISSET(i,m) (((i)&*(m)) != 0)
170 #define SET(i,m) (*(m) |= (i))
172 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
173 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
174 #define POLLEX_SET (POLLPRI)
176 int do_select(int n, fd_set_bits *fds, long *timeout)
178 struct poll_wqueues table;
179 poll_table *wait;
180 int retval, i;
181 long __timeout = *timeout;
183 spin_lock(&current->files->file_lock);
184 retval = max_select_fd(n, fds);
185 spin_unlock(&current->files->file_lock);
187 if (retval < 0)
188 return retval;
189 n = retval;
191 poll_initwait(&table);
192 wait = &table.pt;
193 if (!__timeout)
194 wait = NULL;
195 retval = 0;
196 for (;;) {
197 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
199 set_current_state(TASK_INTERRUPTIBLE);
201 inp = fds->in; outp = fds->out; exp = fds->ex;
202 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
204 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
205 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
206 unsigned long res_in = 0, res_out = 0, res_ex = 0;
207 struct file_operations *f_op = NULL;
208 struct file *file = NULL;
210 in = *inp++; out = *outp++; ex = *exp++;
211 all_bits = in | out | ex;
212 if (all_bits == 0) {
213 i += __NFDBITS;
214 continue;
217 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
218 if (i >= n)
219 break;
220 if (!(bit & all_bits))
221 continue;
222 file = fget(i);
223 if (file) {
224 f_op = file->f_op;
225 mask = DEFAULT_POLLMASK;
226 if (f_op && f_op->poll)
227 mask = (*f_op->poll)(file, retval ? NULL : wait);
228 fput(file);
229 if ((mask & POLLIN_SET) && (in & bit)) {
230 res_in |= bit;
231 retval++;
233 if ((mask & POLLOUT_SET) && (out & bit)) {
234 res_out |= bit;
235 retval++;
237 if ((mask & POLLEX_SET) && (ex & bit)) {
238 res_ex |= bit;
239 retval++;
243 if (res_in)
244 *rinp = res_in;
245 if (res_out)
246 *routp = res_out;
247 if (res_ex)
248 *rexp = res_ex;
250 wait = NULL;
251 if (retval || !__timeout || signal_pending(current))
252 break;
253 if(table.error) {
254 retval = table.error;
255 break;
257 __timeout = schedule_timeout(__timeout);
259 __set_current_state(TASK_RUNNING);
261 poll_freewait(&table);
264 * Up-to-date the caller timeout.
266 *timeout = __timeout;
267 return retval;
270 EXPORT_SYMBOL(do_select);
272 static void *select_bits_alloc(int size)
274 return kmalloc(6 * size, GFP_KERNEL);
277 static void select_bits_free(void *bits, int size)
279 kfree(bits);
283 * We can actually return ERESTARTSYS instead of EINTR, but I'd
284 * like to be certain this leads to no problems. So I return
285 * EINTR just for safety.
287 * Update: ERESTARTSYS breaks at least the xview clock binary, so
288 * I'm trying ERESTARTNOHAND which restart only when you want to.
290 #define MAX_SELECT_SECONDS \
291 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
293 asmlinkage long
294 sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
296 fd_set_bits fds;
297 char *bits;
298 long timeout;
299 int ret, size, max_fdset;
301 timeout = MAX_SCHEDULE_TIMEOUT;
302 if (tvp) {
303 time_t sec, usec;
305 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
306 || (ret = __get_user(sec, &tvp->tv_sec))
307 || (ret = __get_user(usec, &tvp->tv_usec)))
308 goto out_nofds;
310 ret = -EINVAL;
311 if (sec < 0 || usec < 0)
312 goto out_nofds;
314 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
315 timeout = ROUND_UP(usec, 1000000/HZ);
316 timeout += sec * (unsigned long) HZ;
320 ret = -EINVAL;
321 if (n < 0)
322 goto out_nofds;
324 /* max_fdset can increase, so grab it once to avoid race */
325 max_fdset = current->files->max_fdset;
326 if (n > max_fdset)
327 n = max_fdset;
330 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
331 * since we used fdset we need to allocate memory in units of
332 * long-words.
334 ret = -ENOMEM;
335 size = FDS_BYTES(n);
336 bits = select_bits_alloc(size);
337 if (!bits)
338 goto out_nofds;
339 fds.in = (unsigned long *) bits;
340 fds.out = (unsigned long *) (bits + size);
341 fds.ex = (unsigned long *) (bits + 2*size);
342 fds.res_in = (unsigned long *) (bits + 3*size);
343 fds.res_out = (unsigned long *) (bits + 4*size);
344 fds.res_ex = (unsigned long *) (bits + 5*size);
346 if ((ret = get_fd_set(n, inp, fds.in)) ||
347 (ret = get_fd_set(n, outp, fds.out)) ||
348 (ret = get_fd_set(n, exp, fds.ex)))
349 goto out;
350 zero_fd_set(n, fds.res_in);
351 zero_fd_set(n, fds.res_out);
352 zero_fd_set(n, fds.res_ex);
354 ret = do_select(n, &fds, &timeout);
356 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
357 time_t sec = 0, usec = 0;
358 if (timeout) {
359 sec = timeout / HZ;
360 usec = timeout % HZ;
361 usec *= (1000000/HZ);
363 put_user(sec, &tvp->tv_sec);
364 put_user(usec, &tvp->tv_usec);
367 if (ret < 0)
368 goto out;
369 if (!ret) {
370 ret = -ERESTARTNOHAND;
371 if (signal_pending(current))
372 goto out;
373 ret = 0;
376 if (set_fd_set(n, inp, fds.res_in) ||
377 set_fd_set(n, outp, fds.res_out) ||
378 set_fd_set(n, exp, fds.res_ex))
379 ret = -EFAULT;
381 out:
382 select_bits_free(bits, size);
383 out_nofds:
384 return ret;
387 struct poll_list {
388 struct poll_list *next;
389 int len;
390 struct pollfd entries[0];
393 #define POLLFD_PER_PAGE ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
395 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
396 poll_table ** pwait, int *count)
398 int i;
400 for (i = 0; i < num; i++) {
401 int fd;
402 unsigned int mask;
403 struct pollfd *fdp;
405 mask = 0;
406 fdp = fdpage+i;
407 fd = fdp->fd;
408 if (fd >= 0) {
409 struct file * file = fget(fd);
410 mask = POLLNVAL;
411 if (file != NULL) {
412 mask = DEFAULT_POLLMASK;
413 if (file->f_op && file->f_op->poll)
414 mask = file->f_op->poll(file, *pwait);
415 mask &= fdp->events | POLLERR | POLLHUP;
416 fput(file);
418 if (mask) {
419 *pwait = NULL;
420 (*count)++;
423 fdp->revents = mask;
427 static int do_poll(unsigned int nfds, struct poll_list *list,
428 struct poll_wqueues *wait, long timeout)
430 int count = 0;
431 poll_table* pt = &wait->pt;
433 if (!timeout)
434 pt = NULL;
436 for (;;) {
437 struct poll_list *walk;
438 set_current_state(TASK_INTERRUPTIBLE);
439 walk = list;
440 while(walk != NULL) {
441 do_pollfd( walk->len, walk->entries, &pt, &count);
442 walk = walk->next;
444 pt = NULL;
445 if (count || !timeout || signal_pending(current))
446 break;
447 count = wait->error;
448 if (count)
449 break;
450 timeout = schedule_timeout(timeout);
452 __set_current_state(TASK_RUNNING);
453 return count;
456 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
458 struct poll_wqueues table;
459 int fdcount, err;
460 unsigned int i;
461 struct poll_list *head;
462 struct poll_list *walk;
464 /* Do a sanity check on nfds ... */
465 if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
466 return -EINVAL;
468 if (timeout) {
469 /* Careful about overflow in the intermediate values */
470 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
471 timeout = (unsigned long)(timeout*HZ+999)/1000+1;
472 else /* Negative or overflow */
473 timeout = MAX_SCHEDULE_TIMEOUT;
476 poll_initwait(&table);
478 head = NULL;
479 walk = NULL;
480 i = nfds;
481 err = -ENOMEM;
482 while(i!=0) {
483 struct poll_list *pp;
484 pp = kmalloc(sizeof(struct poll_list)+
485 sizeof(struct pollfd)*
486 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
487 GFP_KERNEL);
488 if(pp==NULL)
489 goto out_fds;
490 pp->next=NULL;
491 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
492 if (head == NULL)
493 head = pp;
494 else
495 walk->next = pp;
497 walk = pp;
498 if (copy_from_user(pp->entries, ufds + nfds-i,
499 sizeof(struct pollfd)*pp->len)) {
500 err = -EFAULT;
501 goto out_fds;
503 i -= pp->len;
505 fdcount = do_poll(nfds, head, &table, timeout);
507 /* OK, now copy the revents fields back to user space. */
508 walk = head;
509 err = -EFAULT;
510 while(walk != NULL) {
511 struct pollfd *fds = walk->entries;
512 int j;
514 for (j=0; j < walk->len; j++, ufds++) {
515 if(__put_user(fds[j].revents, &ufds->revents))
516 goto out_fds;
518 walk = walk->next;
520 err = fdcount;
521 if (!fdcount && signal_pending(current))
522 err = -EINTR;
523 out_fds:
524 walk = head;
525 while(walk!=NULL) {
526 struct poll_list *pp = walk->next;
527 kfree(walk);
528 walk = pp;
530 poll_freewait(&table);
531 return err;