Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / fs / select.c
1 /*
2  * This file contains the procedures for the handling of select and poll
3  *
4  * Created for Linux based loosely upon Mathius Lattner's minix
5  * patches by Peter MacDonald. Heavily edited by Linus.
6  *
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.
11  *
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).
15  */
16
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>
24
25 #include <asm/uaccess.h>
26
27 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
28 #define DEFAULT_POLLMASK (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
29
30 struct poll_table_entry {
31         struct file * filp;
32         wait_queue_t wait;
33         wait_queue_head_t * wait_address;
34 };
35
36 struct poll_table_page {
37         struct poll_table_page * next;
38         struct poll_table_entry * entry;
39         struct poll_table_entry entries[0];
40 };
41
42 #define POLL_TABLE_FULL(table) \
43         ((unsigned long)((table)->entry+1) > PAGE_SIZE + (unsigned long)(table))
44
45 /*
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.
51  *
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.
56  */
57 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p);
58
59 void poll_initwait(struct poll_wqueues *pwq)
60 {
61         init_poll_funcptr(&pwq->pt, __pollwait);
62         pwq->error = 0;
63         pwq->table = NULL;
64 }
65
66 EXPORT_SYMBOL(poll_initwait);
67
68 void poll_freewait(struct poll_wqueues *pwq)
69 {
70         struct poll_table_page * p = pwq->table;
71         while (p) {
72                 struct poll_table_entry * entry;
73                 struct poll_table_page *old;
74
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);
84         }
85 }
86
87 EXPORT_SYMBOL(poll_freewait);
88
89 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *_p)
90 {
91         struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
92         struct poll_table_page *table = p->table;
93
94         if (!table || POLL_TABLE_FULL(table)) {
95                 struct poll_table_page *new_table;
96
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;
102                 }
103                 new_table->entry = new_table->entries;
104                 new_table->next = table;
105                 p->table = new_table;
106                 table = new_table;
107         }
108
109         /* Add a new entry */
110         {
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);
118         }
119 }
120
121
122 #define __IN(fds, n)            (fds->in + n)
123 #define __OUT(fds, n)           (fds->out + n)
124 #define __EX(fds, n)            (fds->ex + n)
125 #define __RES_IN(fds, n)        (fds->res_in + n)
126 #define __RES_OUT(fds, n)       (fds->res_out + n)
127 #define __RES_EX(fds, n)        (fds->res_ex + n)
128
129 #define BITS(fds, n)            (*__IN(fds, n)|*__OUT(fds, n)|*__EX(fds, n))
130
131 static int max_select_fd(unsigned long n, fd_set_bits *fds)
132 {
133         unsigned long *open_fds;
134         unsigned long set;
135         int max;
136
137         /* handle last in-complete long-word first */
138         set = ~(~0UL << (n & (__NFDBITS-1)));
139         n /= __NFDBITS;
140         open_fds = current->files->open_fds->fds_bits+n;
141         max = 0;
142         if (set) {
143                 set &= BITS(fds, n);
144                 if (set) {
145                         if (!(set & ~*open_fds))
146                                 goto get_max;
147                         return -EBADF;
148                 }
149         }
150         while (n) {
151                 open_fds--;
152                 n--;
153                 set = BITS(fds, n);
154                 if (!set)
155                         continue;
156                 if (set & ~*open_fds)
157                         return -EBADF;
158                 if (max)
159                         continue;
160 get_max:
161                 do {
162                         max++;
163                         set >>= 1;
164                 } while (set);
165                 max += n * __NFDBITS;
166         }
167
168         return max;
169 }
170
171 #define BIT(i)          (1UL << ((i)&(__NFDBITS-1)))
172 #define MEM(i,m)        ((m)+(unsigned)(i)/__NFDBITS)
173 #define ISSET(i,m)      (((i)&*(m)) != 0)
174 #define SET(i,m)        (*(m) |= (i))
175
176 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP | POLLERR)
177 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
178 #define POLLEX_SET (POLLPRI)
179
180 int do_select(int n, fd_set_bits *fds, long *timeout)
181 {
182         struct poll_wqueues table;
183         poll_table *wait;
184         int retval, i;
185         long __timeout = *timeout;
186
187         spin_lock(&current->files->file_lock);
188         retval = max_select_fd(n, fds);
189         spin_unlock(&current->files->file_lock);
190
191         if (retval < 0)
192                 return retval;
193         n = retval;
194
195         poll_initwait(&table);
196         wait = &table.pt;
197         if (!__timeout)
198                 wait = NULL;
199         retval = 0;
200         for (;;) {
201                 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
202
203                 set_current_state(TASK_INTERRUPTIBLE);
204
205                 inp = fds->in; outp = fds->out; exp = fds->ex;
206                 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
207
208                 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
209                         unsigned long in, out, ex, all_bits, bit = 1, mask, j;
210                         unsigned long res_in = 0, res_out = 0, res_ex = 0;
211                         struct file_operations *f_op = NULL;
212                         struct file *file = NULL;
213
214                         in = *inp++; out = *outp++; ex = *exp++;
215                         all_bits = in | out | ex;
216                         if (all_bits == 0) {
217                                 i += __NFDBITS;
218                                 continue;
219                         }
220
221                         for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
222                                 if (i >= n)
223                                         break;
224                                 if (!(bit & all_bits))
225                                         continue;
226                                 file = fget(i);
227                                 if (file) {
228                                         f_op = file->f_op;
229                                         mask = DEFAULT_POLLMASK;
230                                         if (f_op && f_op->poll)
231                                                 mask = (*f_op->poll)(file, retval ? NULL : wait);
232                                         fput(file);
233                                         if ((mask & POLLIN_SET) && (in & bit)) {
234                                                 res_in |= bit;
235                                                 retval++;
236                                         }
237                                         if ((mask & POLLOUT_SET) && (out & bit)) {
238                                                 res_out |= bit;
239                                                 retval++;
240                                         }
241                                         if ((mask & POLLEX_SET) && (ex & bit)) {
242                                                 res_ex |= bit;
243                                                 retval++;
244                                         }
245                                 }
246                                 cond_resched();
247                         }
248                         if (res_in)
249                                 *rinp = res_in;
250                         if (res_out)
251                                 *routp = res_out;
252                         if (res_ex)
253                                 *rexp = res_ex;
254                 }
255                 wait = NULL;
256                 if (retval || !__timeout || signal_pending(current))
257                         break;
258                 if(table.error) {
259                         retval = table.error;
260                         break;
261                 }
262                 __timeout = schedule_timeout(__timeout);
263         }
264         __set_current_state(TASK_RUNNING);
265
266         poll_freewait(&table);
267
268         /*
269          * Up-to-date the caller timeout.
270          */
271         *timeout = __timeout;
272         return retval;
273 }
274
275 EXPORT_SYMBOL(do_select);
276
277 static void *select_bits_alloc(int size)
278 {
279         return kmalloc(6 * size, GFP_KERNEL);
280 }
281
282 static void select_bits_free(void *bits, int size)
283 {
284         kfree(bits);
285 }
286
287 /*
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.
291  *
292  * Update: ERESTARTSYS breaks at least the xview clock binary, so
293  * I'm trying ERESTARTNOHAND which restart only when you want to.
294  */
295 #define MAX_SELECT_SECONDS \
296         ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
297
298 asmlinkage long
299 sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct timeval __user *tvp)
300 {
301         fd_set_bits fds;
302         char *bits;
303         long timeout;
304         int ret, size, max_fdset;
305
306         timeout = MAX_SCHEDULE_TIMEOUT;
307         if (tvp) {
308                 time_t sec, usec;
309
310                 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
311                     || (ret = __get_user(sec, &tvp->tv_sec))
312                     || (ret = __get_user(usec, &tvp->tv_usec)))
313                         goto out_nofds;
314
315                 ret = -EINVAL;
316                 if (sec < 0 || usec < 0)
317                         goto out_nofds;
318
319                 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
320                         timeout = ROUND_UP(usec, 1000000/HZ);
321                         timeout += sec * (unsigned long) HZ;
322                 }
323         }
324
325         ret = -EINVAL;
326         if (n < 0)
327                 goto out_nofds;
328
329         /* max_fdset can increase, so grab it once to avoid race */
330         max_fdset = current->files->max_fdset;
331         if (n > max_fdset)
332                 n = max_fdset;
333
334         /*
335          * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
336          * since we used fdset we need to allocate memory in units of
337          * long-words. 
338          */
339         ret = -ENOMEM;
340         size = FDS_BYTES(n);
341         bits = select_bits_alloc(size);
342         if (!bits)
343                 goto out_nofds;
344         fds.in      = (unsigned long *)  bits;
345         fds.out     = (unsigned long *) (bits +   size);
346         fds.ex      = (unsigned long *) (bits + 2*size);
347         fds.res_in  = (unsigned long *) (bits + 3*size);
348         fds.res_out = (unsigned long *) (bits + 4*size);
349         fds.res_ex  = (unsigned long *) (bits + 5*size);
350
351         if ((ret = get_fd_set(n, inp, fds.in)) ||
352             (ret = get_fd_set(n, outp, fds.out)) ||
353             (ret = get_fd_set(n, exp, fds.ex)))
354                 goto out;
355         zero_fd_set(n, fds.res_in);
356         zero_fd_set(n, fds.res_out);
357         zero_fd_set(n, fds.res_ex);
358
359         ret = do_select(n, &fds, &timeout);
360
361         if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
362                 time_t sec = 0, usec = 0;
363                 if (timeout) {
364                         sec = timeout / HZ;
365                         usec = timeout % HZ;
366                         usec *= (1000000/HZ);
367                 }
368                 put_user(sec, &tvp->tv_sec);
369                 put_user(usec, &tvp->tv_usec);
370         }
371
372         if (ret < 0)
373                 goto out;
374         if (!ret) {
375                 ret = -ERESTARTNOHAND;
376                 if (signal_pending(current))
377                         goto out;
378                 ret = 0;
379         }
380
381         set_fd_set(n, inp, fds.res_in);
382         set_fd_set(n, outp, fds.res_out);
383         set_fd_set(n, exp, fds.res_ex);
384
385 out:
386         select_bits_free(bits, size);
387 out_nofds:
388         return ret;
389 }
390
391 struct poll_list {
392         struct poll_list *next;
393         int len;
394         struct pollfd entries[0];
395 };
396
397 #define POLLFD_PER_PAGE  ((PAGE_SIZE-sizeof(struct poll_list)) / sizeof(struct pollfd))
398
399 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
400         poll_table ** pwait, int *count)
401 {
402         int i;
403
404         for (i = 0; i < num; i++) {
405                 int fd;
406                 unsigned int mask;
407                 struct pollfd *fdp;
408
409                 mask = 0;
410                 fdp = fdpage+i;
411                 fd = fdp->fd;
412                 if (fd >= 0) {
413                         struct file * file = fget(fd);
414                         mask = POLLNVAL;
415                         if (file != NULL) {
416                                 mask = DEFAULT_POLLMASK;
417                                 if (file->f_op && file->f_op->poll)
418                                         mask = file->f_op->poll(file, *pwait);
419                                 mask &= fdp->events | POLLERR | POLLHUP;
420                                 fput(file);
421                         }
422                         if (mask) {
423                                 *pwait = NULL;
424                                 (*count)++;
425                         }
426                 }
427                 fdp->revents = mask;
428         }
429 }
430
431 static int do_poll(unsigned int nfds,  struct poll_list *list,
432                         struct poll_wqueues *wait, long timeout)
433 {
434         int count = 0;
435         poll_table* pt = &wait->pt;
436
437         if (!timeout)
438                 pt = NULL;
439  
440         for (;;) {
441                 struct poll_list *walk;
442                 set_current_state(TASK_INTERRUPTIBLE);
443                 walk = list;
444                 while(walk != NULL) {
445                         do_pollfd( walk->len, walk->entries, &pt, &count);
446                         walk = walk->next;
447                 }
448                 pt = NULL;
449                 if (count || !timeout || signal_pending(current))
450                         break;
451                 count = wait->error;
452                 if (count)
453                         break;
454                 timeout = schedule_timeout(timeout);
455         }
456         __set_current_state(TASK_RUNNING);
457         return count;
458 }
459
460 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
461 {
462         struct poll_wqueues table;
463         int fdcount, err;
464         unsigned int i;
465         struct poll_list *head;
466         struct poll_list *walk;
467
468         /* Do a sanity check on nfds ... */
469         if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
470                 return -EINVAL;
471
472         if (timeout) {
473                 /* Careful about overflow in the intermediate values */
474                 if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
475                         timeout = (unsigned long)(timeout*HZ+999)/1000+1;
476                 else /* Negative or overflow */
477                         timeout = MAX_SCHEDULE_TIMEOUT;
478         }
479
480         poll_initwait(&table);
481
482         head = NULL;
483         walk = NULL;
484         i = nfds;
485         err = -ENOMEM;
486         while(i!=0) {
487                 struct poll_list *pp;
488                 pp = kmalloc(sizeof(struct poll_list)+
489                                 sizeof(struct pollfd)*
490                                 (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
491                                         GFP_KERNEL);
492                 if(pp==NULL)
493                         goto out_fds;
494                 pp->next=NULL;
495                 pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
496                 if (head == NULL)
497                         head = pp;
498                 else
499                         walk->next = pp;
500
501                 walk = pp;
502                 if (copy_from_user(pp->entries, ufds + nfds-i, 
503                                 sizeof(struct pollfd)*pp->len)) {
504                         err = -EFAULT;
505                         goto out_fds;
506                 }
507                 i -= pp->len;
508         }
509         fdcount = do_poll(nfds, head, &table, timeout);
510
511         /* OK, now copy the revents fields back to user space. */
512         walk = head;
513         err = -EFAULT;
514         while(walk != NULL) {
515                 struct pollfd *fds = walk->entries;
516                 int j;
517
518                 for (j=0; j < walk->len; j++, ufds++) {
519                         if(__put_user(fds[j].revents, &ufds->revents))
520                                 goto out_fds;
521                 }
522                 walk = walk->next;
523         }
524         err = fdcount;
525         if (!fdcount && signal_pending(current))
526                 err = -EINTR;
527 out_fds:
528         walk = head;
529         while(walk!=NULL) {
530                 struct poll_list *pp = walk->next;
531                 kfree(walk);
532                 walk = pp;
533         }
534         poll_freewait(&table);
535         return err;
536 }