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