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