[PATCH] mm: use symbolic names instead of indices for zone initialisation
[linux-2.6/openmoko-kernel.git] / fs / eventpoll.c
blob557d5b614fae6ba694703b86d950471cc2eeedc5
1 /*
2 * fs/eventpoll.c ( Efficent event polling implementation )
3 * Copyright (C) 2001,...,2006 Davide Libenzi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Davide Libenzi <davidel@xmailserver.org>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/fs.h>
19 #include <linux/file.h>
20 #include <linux/signal.h>
21 #include <linux/errno.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/poll.h>
25 #include <linux/smp_lock.h>
26 #include <linux/string.h>
27 #include <linux/list.h>
28 #include <linux/hash.h>
29 #include <linux/spinlock.h>
30 #include <linux/syscalls.h>
31 #include <linux/rwsem.h>
32 #include <linux/rbtree.h>
33 #include <linux/wait.h>
34 #include <linux/eventpoll.h>
35 #include <linux/mount.h>
36 #include <linux/bitops.h>
37 #include <linux/mutex.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/mman.h>
42 #include <asm/atomic.h>
43 #include <asm/semaphore.h>
47 * LOCKING:
48 * There are three level of locking required by epoll :
50 * 1) epmutex (mutex)
51 * 2) ep->sem (rw_semaphore)
52 * 3) ep->lock (rw_lock)
54 * The acquire order is the one listed above, from 1 to 3.
55 * We need a spinlock (ep->lock) because we manipulate objects
56 * from inside the poll callback, that might be triggered from
57 * a wake_up() that in turn might be called from IRQ context.
58 * So we can't sleep inside the poll callback and hence we need
59 * a spinlock. During the event transfer loop (from kernel to
60 * user space) we could end up sleeping due a copy_to_user(), so
61 * we need a lock that will allow us to sleep. This lock is a
62 * read-write semaphore (ep->sem). It is acquired on read during
63 * the event transfer loop and in write during epoll_ctl(EPOLL_CTL_DEL)
64 * and during eventpoll_release_file(). Then we also need a global
65 * semaphore to serialize eventpoll_release_file() and ep_free().
66 * This semaphore is acquired by ep_free() during the epoll file
67 * cleanup path and it is also acquired by eventpoll_release_file()
68 * if a file has been pushed inside an epoll set and it is then
69 * close()d without a previous call toepoll_ctl(EPOLL_CTL_DEL).
70 * It is possible to drop the "ep->sem" and to use the global
71 * semaphore "epmutex" (together with "ep->lock") to have it working,
72 * but having "ep->sem" will make the interface more scalable.
73 * Events that require holding "epmutex" are very rare, while for
74 * normal operations the epoll private "ep->sem" will guarantee
75 * a greater scalability.
79 #define EVENTPOLLFS_MAGIC 0x03111965 /* My birthday should work for this :) */
81 #define DEBUG_EPOLL 0
83 #if DEBUG_EPOLL > 0
84 #define DPRINTK(x) printk x
85 #define DNPRINTK(n, x) do { if ((n) <= DEBUG_EPOLL) printk x; } while (0)
86 #else /* #if DEBUG_EPOLL > 0 */
87 #define DPRINTK(x) (void) 0
88 #define DNPRINTK(n, x) (void) 0
89 #endif /* #if DEBUG_EPOLL > 0 */
91 #define DEBUG_EPI 0
93 #if DEBUG_EPI != 0
94 #define EPI_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
95 #else /* #if DEBUG_EPI != 0 */
96 #define EPI_SLAB_DEBUG 0
97 #endif /* #if DEBUG_EPI != 0 */
99 /* Epoll private bits inside the event mask */
100 #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET)
102 /* Maximum number of poll wake up nests we are allowing */
103 #define EP_MAX_POLLWAKE_NESTS 4
105 /* Maximum msec timeout value storeable in a long int */
106 #define EP_MAX_MSTIMEO min(1000ULL * MAX_SCHEDULE_TIMEOUT / HZ, (LONG_MAX - 999ULL) / HZ)
109 struct epoll_filefd {
110 struct file *file;
111 int fd;
115 * Node that is linked into the "wake_task_list" member of the "struct poll_safewake".
116 * It is used to keep track on all tasks that are currently inside the wake_up() code
117 * to 1) short-circuit the one coming from the same task and same wait queue head
118 * ( loop ) 2) allow a maximum number of epoll descriptors inclusion nesting
119 * 3) let go the ones coming from other tasks.
121 struct wake_task_node {
122 struct list_head llink;
123 struct task_struct *task;
124 wait_queue_head_t *wq;
128 * This is used to implement the safe poll wake up avoiding to reenter
129 * the poll callback from inside wake_up().
131 struct poll_safewake {
132 struct list_head wake_task_list;
133 spinlock_t lock;
137 * This structure is stored inside the "private_data" member of the file
138 * structure and rapresent the main data sructure for the eventpoll
139 * interface.
141 struct eventpoll {
142 /* Protect the this structure access */
143 rwlock_t lock;
146 * This semaphore is used to ensure that files are not removed
147 * while epoll is using them. This is read-held during the event
148 * collection loop and it is write-held during the file cleanup
149 * path, the epoll file exit code and the ctl operations.
151 struct rw_semaphore sem;
153 /* Wait queue used by sys_epoll_wait() */
154 wait_queue_head_t wq;
156 /* Wait queue used by file->poll() */
157 wait_queue_head_t poll_wait;
159 /* List of ready file descriptors */
160 struct list_head rdllist;
162 /* RB-Tree root used to store monitored fd structs */
163 struct rb_root rbr;
166 /* Wait structure used by the poll hooks */
167 struct eppoll_entry {
168 /* List header used to link this structure to the "struct epitem" */
169 struct list_head llink;
171 /* The "base" pointer is set to the container "struct epitem" */
172 void *base;
175 * Wait queue item that will be linked to the target file wait
176 * queue head.
178 wait_queue_t wait;
180 /* The wait queue head that linked the "wait" wait queue item */
181 wait_queue_head_t *whead;
185 * Each file descriptor added to the eventpoll interface will
186 * have an entry of this type linked to the hash.
188 struct epitem {
189 /* RB-Tree node used to link this structure to the eventpoll rb-tree */
190 struct rb_node rbn;
192 /* List header used to link this structure to the eventpoll ready list */
193 struct list_head rdllink;
195 /* The file descriptor information this item refers to */
196 struct epoll_filefd ffd;
198 /* Number of active wait queue attached to poll operations */
199 int nwait;
201 /* List containing poll wait queues */
202 struct list_head pwqlist;
204 /* The "container" of this item */
205 struct eventpoll *ep;
207 /* The structure that describe the interested events and the source fd */
208 struct epoll_event event;
211 * Used to keep track of the usage count of the structure. This avoids
212 * that the structure will desappear from underneath our processing.
214 atomic_t usecnt;
216 /* List header used to link this item to the "struct file" items list */
217 struct list_head fllink;
219 /* List header used to link the item to the transfer list */
220 struct list_head txlink;
223 * This is used during the collection/transfer of events to userspace
224 * to pin items empty events set.
226 unsigned int revents;
229 /* Wrapper struct used by poll queueing */
230 struct ep_pqueue {
231 poll_table pt;
232 struct epitem *epi;
237 static void ep_poll_safewake_init(struct poll_safewake *psw);
238 static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq);
239 static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
240 struct eventpoll *ep);
241 static int ep_alloc(struct eventpoll **pep);
242 static void ep_free(struct eventpoll *ep);
243 static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd);
244 static void ep_use_epitem(struct epitem *epi);
245 static void ep_release_epitem(struct epitem *epi);
246 static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
247 poll_table *pt);
248 static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi);
249 static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
250 struct file *tfile, int fd);
251 static int ep_modify(struct eventpoll *ep, struct epitem *epi,
252 struct epoll_event *event);
253 static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi);
254 static int ep_unlink(struct eventpoll *ep, struct epitem *epi);
255 static int ep_remove(struct eventpoll *ep, struct epitem *epi);
256 static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key);
257 static int ep_eventpoll_close(struct inode *inode, struct file *file);
258 static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait);
259 static int ep_collect_ready_items(struct eventpoll *ep,
260 struct list_head *txlist, int maxevents);
261 static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
262 struct epoll_event __user *events);
263 static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist);
264 static int ep_events_transfer(struct eventpoll *ep,
265 struct epoll_event __user *events,
266 int maxevents);
267 static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
268 int maxevents, long timeout);
269 static int eventpollfs_delete_dentry(struct dentry *dentry);
270 static struct inode *ep_eventpoll_inode(void);
271 static int eventpollfs_get_sb(struct file_system_type *fs_type,
272 int flags, const char *dev_name,
273 void *data, struct vfsmount *mnt);
276 * This semaphore is used to serialize ep_free() and eventpoll_release_file().
278 static struct mutex epmutex;
280 /* Safe wake up implementation */
281 static struct poll_safewake psw;
283 /* Slab cache used to allocate "struct epitem" */
284 static kmem_cache_t *epi_cache __read_mostly;
286 /* Slab cache used to allocate "struct eppoll_entry" */
287 static kmem_cache_t *pwq_cache __read_mostly;
289 /* Virtual fs used to allocate inodes for eventpoll files */
290 static struct vfsmount *eventpoll_mnt __read_mostly;
292 /* File callbacks that implement the eventpoll file behaviour */
293 static const struct file_operations eventpoll_fops = {
294 .release = ep_eventpoll_close,
295 .poll = ep_eventpoll_poll
299 * This is used to register the virtual file system from where
300 * eventpoll inodes are allocated.
302 static struct file_system_type eventpoll_fs_type = {
303 .name = "eventpollfs",
304 .get_sb = eventpollfs_get_sb,
305 .kill_sb = kill_anon_super,
308 /* Very basic directory entry operations for the eventpoll virtual file system */
309 static struct dentry_operations eventpollfs_dentry_operations = {
310 .d_delete = eventpollfs_delete_dentry,
315 /* Fast test to see if the file is an evenpoll file */
316 static inline int is_file_epoll(struct file *f)
318 return f->f_op == &eventpoll_fops;
321 /* Setup the structure that is used as key for the rb-tree */
322 static inline void ep_set_ffd(struct epoll_filefd *ffd,
323 struct file *file, int fd)
325 ffd->file = file;
326 ffd->fd = fd;
329 /* Compare rb-tree keys */
330 static inline int ep_cmp_ffd(struct epoll_filefd *p1,
331 struct epoll_filefd *p2)
333 return (p1->file > p2->file ? +1:
334 (p1->file < p2->file ? -1 : p1->fd - p2->fd));
337 /* Special initialization for the rb-tree node to detect linkage */
338 static inline void ep_rb_initnode(struct rb_node *n)
340 rb_set_parent(n, n);
343 /* Removes a node from the rb-tree and marks it for a fast is-linked check */
344 static inline void ep_rb_erase(struct rb_node *n, struct rb_root *r)
346 rb_erase(n, r);
347 rb_set_parent(n, n);
350 /* Fast check to verify that the item is linked to the main rb-tree */
351 static inline int ep_rb_linked(struct rb_node *n)
353 return rb_parent(n) != n;
357 * Remove the item from the list and perform its initialization.
358 * This is useful for us because we can test if the item is linked
359 * using "ep_is_linked(p)".
361 static inline void ep_list_del(struct list_head *p)
363 list_del(p);
364 INIT_LIST_HEAD(p);
367 /* Tells us if the item is currently linked */
368 static inline int ep_is_linked(struct list_head *p)
370 return !list_empty(p);
373 /* Get the "struct epitem" from a wait queue pointer */
374 static inline struct epitem * ep_item_from_wait(wait_queue_t *p)
376 return container_of(p, struct eppoll_entry, wait)->base;
379 /* Get the "struct epitem" from an epoll queue wrapper */
380 static inline struct epitem * ep_item_from_epqueue(poll_table *p)
382 return container_of(p, struct ep_pqueue, pt)->epi;
385 /* Tells if the epoll_ctl(2) operation needs an event copy from userspace */
386 static inline int ep_op_hash_event(int op)
388 return op != EPOLL_CTL_DEL;
391 /* Initialize the poll safe wake up structure */
392 static void ep_poll_safewake_init(struct poll_safewake *psw)
395 INIT_LIST_HEAD(&psw->wake_task_list);
396 spin_lock_init(&psw->lock);
401 * Perform a safe wake up of the poll wait list. The problem is that
402 * with the new callback'd wake up system, it is possible that the
403 * poll callback is reentered from inside the call to wake_up() done
404 * on the poll wait queue head. The rule is that we cannot reenter the
405 * wake up code from the same task more than EP_MAX_POLLWAKE_NESTS times,
406 * and we cannot reenter the same wait queue head at all. This will
407 * enable to have a hierarchy of epoll file descriptor of no more than
408 * EP_MAX_POLLWAKE_NESTS deep. We need the irq version of the spin lock
409 * because this one gets called by the poll callback, that in turn is called
410 * from inside a wake_up(), that might be called from irq context.
412 static void ep_poll_safewake(struct poll_safewake *psw, wait_queue_head_t *wq)
414 int wake_nests = 0;
415 unsigned long flags;
416 struct task_struct *this_task = current;
417 struct list_head *lsthead = &psw->wake_task_list, *lnk;
418 struct wake_task_node *tncur;
419 struct wake_task_node tnode;
421 spin_lock_irqsave(&psw->lock, flags);
423 /* Try to see if the current task is already inside this wakeup call */
424 list_for_each(lnk, lsthead) {
425 tncur = list_entry(lnk, struct wake_task_node, llink);
427 if (tncur->wq == wq ||
428 (tncur->task == this_task && ++wake_nests > EP_MAX_POLLWAKE_NESTS)) {
430 * Ops ... loop detected or maximum nest level reached.
431 * We abort this wake by breaking the cycle itself.
433 spin_unlock_irqrestore(&psw->lock, flags);
434 return;
438 /* Add the current task to the list */
439 tnode.task = this_task;
440 tnode.wq = wq;
441 list_add(&tnode.llink, lsthead);
443 spin_unlock_irqrestore(&psw->lock, flags);
445 /* Do really wake up now */
446 wake_up(wq);
448 /* Remove the current task from the list */
449 spin_lock_irqsave(&psw->lock, flags);
450 list_del(&tnode.llink);
451 spin_unlock_irqrestore(&psw->lock, flags);
456 * This is called from eventpoll_release() to unlink files from the eventpoll
457 * interface. We need to have this facility to cleanup correctly files that are
458 * closed without being removed from the eventpoll interface.
460 void eventpoll_release_file(struct file *file)
462 struct list_head *lsthead = &file->f_ep_links;
463 struct eventpoll *ep;
464 struct epitem *epi;
467 * We don't want to get "file->f_ep_lock" because it is not
468 * necessary. It is not necessary because we're in the "struct file"
469 * cleanup path, and this means that noone is using this file anymore.
470 * The only hit might come from ep_free() but by holding the semaphore
471 * will correctly serialize the operation. We do need to acquire
472 * "ep->sem" after "epmutex" because ep_remove() requires it when called
473 * from anywhere but ep_free().
475 mutex_lock(&epmutex);
477 while (!list_empty(lsthead)) {
478 epi = list_entry(lsthead->next, struct epitem, fllink);
480 ep = epi->ep;
481 ep_list_del(&epi->fllink);
482 down_write(&ep->sem);
483 ep_remove(ep, epi);
484 up_write(&ep->sem);
487 mutex_unlock(&epmutex);
492 * It opens an eventpoll file descriptor by suggesting a storage of "size"
493 * file descriptors. The size parameter is just an hint about how to size
494 * data structures. It won't prevent the user to store more than "size"
495 * file descriptors inside the epoll interface. It is the kernel part of
496 * the userspace epoll_create(2).
498 asmlinkage long sys_epoll_create(int size)
500 int error, fd;
501 struct eventpoll *ep;
502 struct inode *inode;
503 struct file *file;
505 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d)\n",
506 current, size));
509 * Sanity check on the size parameter, and create the internal data
510 * structure ( "struct eventpoll" ).
512 error = -EINVAL;
513 if (size <= 0 || (error = ep_alloc(&ep)) != 0)
514 goto eexit_1;
517 * Creates all the items needed to setup an eventpoll file. That is,
518 * a file structure, and inode and a free file descriptor.
520 error = ep_getfd(&fd, &inode, &file, ep);
521 if (error)
522 goto eexit_2;
524 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
525 current, size, fd));
527 return fd;
529 eexit_2:
530 ep_free(ep);
531 kfree(ep);
532 eexit_1:
533 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_create(%d) = %d\n",
534 current, size, error));
535 return error;
540 * The following function implements the controller interface for
541 * the eventpoll file that enables the insertion/removal/change of
542 * file descriptors inside the interest set. It represents
543 * the kernel part of the user space epoll_ctl(2).
545 asmlinkage long
546 sys_epoll_ctl(int epfd, int op, int fd, struct epoll_event __user *event)
548 int error;
549 struct file *file, *tfile;
550 struct eventpoll *ep;
551 struct epitem *epi;
552 struct epoll_event epds;
554 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p)\n",
555 current, epfd, op, fd, event));
557 error = -EFAULT;
558 if (ep_op_hash_event(op) &&
559 copy_from_user(&epds, event, sizeof(struct epoll_event)))
560 goto eexit_1;
562 /* Get the "struct file *" for the eventpoll file */
563 error = -EBADF;
564 file = fget(epfd);
565 if (!file)
566 goto eexit_1;
568 /* Get the "struct file *" for the target file */
569 tfile = fget(fd);
570 if (!tfile)
571 goto eexit_2;
573 /* The target file descriptor must support poll */
574 error = -EPERM;
575 if (!tfile->f_op || !tfile->f_op->poll)
576 goto eexit_3;
579 * We have to check that the file structure underneath the file descriptor
580 * the user passed to us _is_ an eventpoll file. And also we do not permit
581 * adding an epoll file descriptor inside itself.
583 error = -EINVAL;
584 if (file == tfile || !is_file_epoll(file))
585 goto eexit_3;
588 * At this point it is safe to assume that the "private_data" contains
589 * our own data structure.
591 ep = file->private_data;
593 down_write(&ep->sem);
595 /* Try to lookup the file inside our hash table */
596 epi = ep_find(ep, tfile, fd);
598 error = -EINVAL;
599 switch (op) {
600 case EPOLL_CTL_ADD:
601 if (!epi) {
602 epds.events |= POLLERR | POLLHUP;
604 error = ep_insert(ep, &epds, tfile, fd);
605 } else
606 error = -EEXIST;
607 break;
608 case EPOLL_CTL_DEL:
609 if (epi)
610 error = ep_remove(ep, epi);
611 else
612 error = -ENOENT;
613 break;
614 case EPOLL_CTL_MOD:
615 if (epi) {
616 epds.events |= POLLERR | POLLHUP;
617 error = ep_modify(ep, epi, &epds);
618 } else
619 error = -ENOENT;
620 break;
624 * The function ep_find() increments the usage count of the structure
625 * so, if this is not NULL, we need to release it.
627 if (epi)
628 ep_release_epitem(epi);
630 up_write(&ep->sem);
632 eexit_3:
633 fput(tfile);
634 eexit_2:
635 fput(file);
636 eexit_1:
637 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_ctl(%d, %d, %d, %p) = %d\n",
638 current, epfd, op, fd, event, error));
640 return error;
643 #define MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
646 * Implement the event wait interface for the eventpoll file. It is the kernel
647 * part of the user space epoll_wait(2).
649 asmlinkage long sys_epoll_wait(int epfd, struct epoll_event __user *events,
650 int maxevents, int timeout)
652 int error;
653 struct file *file;
654 struct eventpoll *ep;
656 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d)\n",
657 current, epfd, events, maxevents, timeout));
659 /* The maximum number of event must be greater than zero */
660 if (maxevents <= 0 || maxevents > MAX_EVENTS)
661 return -EINVAL;
663 /* Verify that the area passed by the user is writeable */
664 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event))) {
665 error = -EFAULT;
666 goto eexit_1;
669 /* Get the "struct file *" for the eventpoll file */
670 error = -EBADF;
671 file = fget(epfd);
672 if (!file)
673 goto eexit_1;
676 * We have to check that the file structure underneath the fd
677 * the user passed to us _is_ an eventpoll file.
679 error = -EINVAL;
680 if (!is_file_epoll(file))
681 goto eexit_2;
684 * At this point it is safe to assume that the "private_data" contains
685 * our own data structure.
687 ep = file->private_data;
689 /* Time to fish for events ... */
690 error = ep_poll(ep, events, maxevents, timeout);
692 eexit_2:
693 fput(file);
694 eexit_1:
695 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: sys_epoll_wait(%d, %p, %d, %d) = %d\n",
696 current, epfd, events, maxevents, timeout, error));
698 return error;
703 * Creates the file descriptor to be used by the epoll interface.
705 static int ep_getfd(int *efd, struct inode **einode, struct file **efile,
706 struct eventpoll *ep)
708 struct qstr this;
709 char name[32];
710 struct dentry *dentry;
711 struct inode *inode;
712 struct file *file;
713 int error, fd;
715 /* Get an ready to use file */
716 error = -ENFILE;
717 file = get_empty_filp();
718 if (!file)
719 goto eexit_1;
721 /* Allocates an inode from the eventpoll file system */
722 inode = ep_eventpoll_inode();
723 if (IS_ERR(inode)) {
724 error = PTR_ERR(inode);
725 goto eexit_2;
728 /* Allocates a free descriptor to plug the file onto */
729 error = get_unused_fd();
730 if (error < 0)
731 goto eexit_3;
732 fd = error;
735 * Link the inode to a directory entry by creating a unique name
736 * using the inode number.
738 error = -ENOMEM;
739 sprintf(name, "[%lu]", inode->i_ino);
740 this.name = name;
741 this.len = strlen(name);
742 this.hash = inode->i_ino;
743 dentry = d_alloc(eventpoll_mnt->mnt_sb->s_root, &this);
744 if (!dentry)
745 goto eexit_4;
746 dentry->d_op = &eventpollfs_dentry_operations;
747 d_add(dentry, inode);
748 file->f_vfsmnt = mntget(eventpoll_mnt);
749 file->f_dentry = dentry;
750 file->f_mapping = inode->i_mapping;
752 file->f_pos = 0;
753 file->f_flags = O_RDONLY;
754 file->f_op = &eventpoll_fops;
755 file->f_mode = FMODE_READ;
756 file->f_version = 0;
757 file->private_data = ep;
759 /* Install the new setup file into the allocated fd. */
760 fd_install(fd, file);
762 *efd = fd;
763 *einode = inode;
764 *efile = file;
765 return 0;
767 eexit_4:
768 put_unused_fd(fd);
769 eexit_3:
770 iput(inode);
771 eexit_2:
772 put_filp(file);
773 eexit_1:
774 return error;
778 static int ep_alloc(struct eventpoll **pep)
780 struct eventpoll *ep = kzalloc(sizeof(*ep), GFP_KERNEL);
782 if (!ep)
783 return -ENOMEM;
785 rwlock_init(&ep->lock);
786 init_rwsem(&ep->sem);
787 init_waitqueue_head(&ep->wq);
788 init_waitqueue_head(&ep->poll_wait);
789 INIT_LIST_HEAD(&ep->rdllist);
790 ep->rbr = RB_ROOT;
792 *pep = ep;
794 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_alloc() ep=%p\n",
795 current, ep));
796 return 0;
800 static void ep_free(struct eventpoll *ep)
802 struct rb_node *rbp;
803 struct epitem *epi;
805 /* We need to release all tasks waiting for these file */
806 if (waitqueue_active(&ep->poll_wait))
807 ep_poll_safewake(&psw, &ep->poll_wait);
810 * We need to lock this because we could be hit by
811 * eventpoll_release_file() while we're freeing the "struct eventpoll".
812 * We do not need to hold "ep->sem" here because the epoll file
813 * is on the way to be removed and no one has references to it
814 * anymore. The only hit might come from eventpoll_release_file() but
815 * holding "epmutex" is sufficent here.
817 mutex_lock(&epmutex);
820 * Walks through the whole tree by unregistering poll callbacks.
822 for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
823 epi = rb_entry(rbp, struct epitem, rbn);
825 ep_unregister_pollwait(ep, epi);
829 * Walks through the whole hash by freeing each "struct epitem". At this
830 * point we are sure no poll callbacks will be lingering around, and also by
831 * write-holding "sem" we can be sure that no file cleanup code will hit
832 * us during this operation. So we can avoid the lock on "ep->lock".
834 while ((rbp = rb_first(&ep->rbr)) != 0) {
835 epi = rb_entry(rbp, struct epitem, rbn);
836 ep_remove(ep, epi);
839 mutex_unlock(&epmutex);
844 * Search the file inside the eventpoll hash. It add usage count to
845 * the returned item, so the caller must call ep_release_epitem()
846 * after finished using the "struct epitem".
848 static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
850 int kcmp;
851 unsigned long flags;
852 struct rb_node *rbp;
853 struct epitem *epi, *epir = NULL;
854 struct epoll_filefd ffd;
856 ep_set_ffd(&ffd, file, fd);
857 read_lock_irqsave(&ep->lock, flags);
858 for (rbp = ep->rbr.rb_node; rbp; ) {
859 epi = rb_entry(rbp, struct epitem, rbn);
860 kcmp = ep_cmp_ffd(&ffd, &epi->ffd);
861 if (kcmp > 0)
862 rbp = rbp->rb_right;
863 else if (kcmp < 0)
864 rbp = rbp->rb_left;
865 else {
866 ep_use_epitem(epi);
867 epir = epi;
868 break;
871 read_unlock_irqrestore(&ep->lock, flags);
873 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_find(%p) -> %p\n",
874 current, file, epir));
876 return epir;
881 * Increment the usage count of the "struct epitem" making it sure
882 * that the user will have a valid pointer to reference.
884 static void ep_use_epitem(struct epitem *epi)
887 atomic_inc(&epi->usecnt);
892 * Decrement ( release ) the usage count by signaling that the user
893 * has finished using the structure. It might lead to freeing the
894 * structure itself if the count goes to zero.
896 static void ep_release_epitem(struct epitem *epi)
899 if (atomic_dec_and_test(&epi->usecnt))
900 kmem_cache_free(epi_cache, epi);
905 * This is the callback that is used to add our wait queue to the
906 * target file wakeup lists.
908 static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
909 poll_table *pt)
911 struct epitem *epi = ep_item_from_epqueue(pt);
912 struct eppoll_entry *pwq;
914 if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, SLAB_KERNEL))) {
915 init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
916 pwq->whead = whead;
917 pwq->base = epi;
918 add_wait_queue(whead, &pwq->wait);
919 list_add_tail(&pwq->llink, &epi->pwqlist);
920 epi->nwait++;
921 } else {
922 /* We have to signal that an error occurred */
923 epi->nwait = -1;
928 static void ep_rbtree_insert(struct eventpoll *ep, struct epitem *epi)
930 int kcmp;
931 struct rb_node **p = &ep->rbr.rb_node, *parent = NULL;
932 struct epitem *epic;
934 while (*p) {
935 parent = *p;
936 epic = rb_entry(parent, struct epitem, rbn);
937 kcmp = ep_cmp_ffd(&epi->ffd, &epic->ffd);
938 if (kcmp > 0)
939 p = &parent->rb_right;
940 else
941 p = &parent->rb_left;
943 rb_link_node(&epi->rbn, parent, p);
944 rb_insert_color(&epi->rbn, &ep->rbr);
948 static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
949 struct file *tfile, int fd)
951 int error, revents, pwake = 0;
952 unsigned long flags;
953 struct epitem *epi;
954 struct ep_pqueue epq;
956 error = -ENOMEM;
957 if (!(epi = kmem_cache_alloc(epi_cache, SLAB_KERNEL)))
958 goto eexit_1;
960 /* Item initialization follow here ... */
961 ep_rb_initnode(&epi->rbn);
962 INIT_LIST_HEAD(&epi->rdllink);
963 INIT_LIST_HEAD(&epi->fllink);
964 INIT_LIST_HEAD(&epi->txlink);
965 INIT_LIST_HEAD(&epi->pwqlist);
966 epi->ep = ep;
967 ep_set_ffd(&epi->ffd, tfile, fd);
968 epi->event = *event;
969 atomic_set(&epi->usecnt, 1);
970 epi->nwait = 0;
972 /* Initialize the poll table using the queue callback */
973 epq.epi = epi;
974 init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
977 * Attach the item to the poll hooks and get current event bits.
978 * We can safely use the file* here because its usage count has
979 * been increased by the caller of this function.
981 revents = tfile->f_op->poll(tfile, &epq.pt);
984 * We have to check if something went wrong during the poll wait queue
985 * install process. Namely an allocation for a wait queue failed due
986 * high memory pressure.
988 if (epi->nwait < 0)
989 goto eexit_2;
991 /* Add the current item to the list of active epoll hook for this file */
992 spin_lock(&tfile->f_ep_lock);
993 list_add_tail(&epi->fllink, &tfile->f_ep_links);
994 spin_unlock(&tfile->f_ep_lock);
996 /* We have to drop the new item inside our item list to keep track of it */
997 write_lock_irqsave(&ep->lock, flags);
999 /* Add the current item to the rb-tree */
1000 ep_rbtree_insert(ep, epi);
1002 /* If the file is already "ready" we drop it inside the ready list */
1003 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) {
1004 list_add_tail(&epi->rdllink, &ep->rdllist);
1006 /* Notify waiting tasks that events are available */
1007 if (waitqueue_active(&ep->wq))
1008 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
1009 if (waitqueue_active(&ep->poll_wait))
1010 pwake++;
1013 write_unlock_irqrestore(&ep->lock, flags);
1015 /* We have to call this outside the lock */
1016 if (pwake)
1017 ep_poll_safewake(&psw, &ep->poll_wait);
1019 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_insert(%p, %p, %d)\n",
1020 current, ep, tfile, fd));
1022 return 0;
1024 eexit_2:
1025 ep_unregister_pollwait(ep, epi);
1028 * We need to do this because an event could have been arrived on some
1029 * allocated wait queue.
1031 write_lock_irqsave(&ep->lock, flags);
1032 if (ep_is_linked(&epi->rdllink))
1033 ep_list_del(&epi->rdllink);
1034 write_unlock_irqrestore(&ep->lock, flags);
1036 kmem_cache_free(epi_cache, epi);
1037 eexit_1:
1038 return error;
1043 * Modify the interest event mask by dropping an event if the new mask
1044 * has a match in the current file status.
1046 static int ep_modify(struct eventpoll *ep, struct epitem *epi, struct epoll_event *event)
1048 int pwake = 0;
1049 unsigned int revents;
1050 unsigned long flags;
1053 * Set the new event interest mask before calling f_op->poll(), otherwise
1054 * a potential race might occur. In fact if we do this operation inside
1055 * the lock, an event might happen between the f_op->poll() call and the
1056 * new event set registering.
1058 epi->event.events = event->events;
1061 * Get current event bits. We can safely use the file* here because
1062 * its usage count has been increased by the caller of this function.
1064 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
1066 write_lock_irqsave(&ep->lock, flags);
1068 /* Copy the data member from inside the lock */
1069 epi->event.data = event->data;
1072 * If the item is not linked to the hash it means that it's on its
1073 * way toward the removal. Do nothing in this case.
1075 if (ep_rb_linked(&epi->rbn)) {
1077 * If the item is "hot" and it is not registered inside the ready
1078 * list, push it inside. If the item is not "hot" and it is currently
1079 * registered inside the ready list, unlink it.
1081 if (revents & event->events) {
1082 if (!ep_is_linked(&epi->rdllink)) {
1083 list_add_tail(&epi->rdllink, &ep->rdllist);
1085 /* Notify waiting tasks that events are available */
1086 if (waitqueue_active(&ep->wq))
1087 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1088 TASK_INTERRUPTIBLE);
1089 if (waitqueue_active(&ep->poll_wait))
1090 pwake++;
1095 write_unlock_irqrestore(&ep->lock, flags);
1097 /* We have to call this outside the lock */
1098 if (pwake)
1099 ep_poll_safewake(&psw, &ep->poll_wait);
1101 return 0;
1106 * This function unregister poll callbacks from the associated file descriptor.
1107 * Since this must be called without holding "ep->lock" the atomic exchange trick
1108 * will protect us from multiple unregister.
1110 static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
1112 int nwait;
1113 struct list_head *lsthead = &epi->pwqlist;
1114 struct eppoll_entry *pwq;
1116 /* This is called without locks, so we need the atomic exchange */
1117 nwait = xchg(&epi->nwait, 0);
1119 if (nwait) {
1120 while (!list_empty(lsthead)) {
1121 pwq = list_entry(lsthead->next, struct eppoll_entry, llink);
1123 ep_list_del(&pwq->llink);
1124 remove_wait_queue(pwq->whead, &pwq->wait);
1125 kmem_cache_free(pwq_cache, pwq);
1132 * Unlink the "struct epitem" from all places it might have been hooked up.
1133 * This function must be called with write IRQ lock on "ep->lock".
1135 static int ep_unlink(struct eventpoll *ep, struct epitem *epi)
1137 int error;
1140 * It can happen that this one is called for an item already unlinked.
1141 * The check protect us from doing a double unlink ( crash ).
1143 error = -ENOENT;
1144 if (!ep_rb_linked(&epi->rbn))
1145 goto eexit_1;
1148 * Clear the event mask for the unlinked item. This will avoid item
1149 * notifications to be sent after the unlink operation from inside
1150 * the kernel->userspace event transfer loop.
1152 epi->event.events = 0;
1155 * At this point is safe to do the job, unlink the item from our rb-tree.
1156 * This operation togheter with the above check closes the door to
1157 * double unlinks.
1159 ep_rb_erase(&epi->rbn, &ep->rbr);
1162 * If the item we are going to remove is inside the ready file descriptors
1163 * we want to remove it from this list to avoid stale events.
1165 if (ep_is_linked(&epi->rdllink))
1166 ep_list_del(&epi->rdllink);
1168 error = 0;
1169 eexit_1:
1171 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_unlink(%p, %p) = %d\n",
1172 current, ep, epi->ffd.file, error));
1174 return error;
1179 * Removes a "struct epitem" from the eventpoll hash and deallocates
1180 * all the associated resources.
1182 static int ep_remove(struct eventpoll *ep, struct epitem *epi)
1184 int error;
1185 unsigned long flags;
1186 struct file *file = epi->ffd.file;
1189 * Removes poll wait queue hooks. We _have_ to do this without holding
1190 * the "ep->lock" otherwise a deadlock might occur. This because of the
1191 * sequence of the lock acquisition. Here we do "ep->lock" then the wait
1192 * queue head lock when unregistering the wait queue. The wakeup callback
1193 * will run by holding the wait queue head lock and will call our callback
1194 * that will try to get "ep->lock".
1196 ep_unregister_pollwait(ep, epi);
1198 /* Remove the current item from the list of epoll hooks */
1199 spin_lock(&file->f_ep_lock);
1200 if (ep_is_linked(&epi->fllink))
1201 ep_list_del(&epi->fllink);
1202 spin_unlock(&file->f_ep_lock);
1204 /* We need to acquire the write IRQ lock before calling ep_unlink() */
1205 write_lock_irqsave(&ep->lock, flags);
1207 /* Really unlink the item from the hash */
1208 error = ep_unlink(ep, epi);
1210 write_unlock_irqrestore(&ep->lock, flags);
1212 if (error)
1213 goto eexit_1;
1215 /* At this point it is safe to free the eventpoll item */
1216 ep_release_epitem(epi);
1218 error = 0;
1219 eexit_1:
1220 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: ep_remove(%p, %p) = %d\n",
1221 current, ep, file, error));
1223 return error;
1228 * This is the callback that is passed to the wait queue wakeup
1229 * machanism. It is called by the stored file descriptors when they
1230 * have events to report.
1232 static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *key)
1234 int pwake = 0;
1235 unsigned long flags;
1236 struct epitem *epi = ep_item_from_wait(wait);
1237 struct eventpoll *ep = epi->ep;
1239 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: poll_callback(%p) epi=%p ep=%p\n",
1240 current, epi->ffd.file, epi, ep));
1242 write_lock_irqsave(&ep->lock, flags);
1245 * If the event mask does not contain any poll(2) event, we consider the
1246 * descriptor to be disabled. This condition is likely the effect of the
1247 * EPOLLONESHOT bit that disables the descriptor when an event is received,
1248 * until the next EPOLL_CTL_MOD will be issued.
1250 if (!(epi->event.events & ~EP_PRIVATE_BITS))
1251 goto is_disabled;
1253 /* If this file is already in the ready list we exit soon */
1254 if (ep_is_linked(&epi->rdllink))
1255 goto is_linked;
1257 list_add_tail(&epi->rdllink, &ep->rdllist);
1259 is_linked:
1261 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1262 * wait list.
1264 if (waitqueue_active(&ep->wq))
1265 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1266 TASK_INTERRUPTIBLE);
1267 if (waitqueue_active(&ep->poll_wait))
1268 pwake++;
1270 is_disabled:
1271 write_unlock_irqrestore(&ep->lock, flags);
1273 /* We have to call this outside the lock */
1274 if (pwake)
1275 ep_poll_safewake(&psw, &ep->poll_wait);
1277 return 1;
1281 static int ep_eventpoll_close(struct inode *inode, struct file *file)
1283 struct eventpoll *ep = file->private_data;
1285 if (ep) {
1286 ep_free(ep);
1287 kfree(ep);
1290 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: close() ep=%p\n", current, ep));
1291 return 0;
1295 static unsigned int ep_eventpoll_poll(struct file *file, poll_table *wait)
1297 unsigned int pollflags = 0;
1298 unsigned long flags;
1299 struct eventpoll *ep = file->private_data;
1301 /* Insert inside our poll wait queue */
1302 poll_wait(file, &ep->poll_wait, wait);
1304 /* Check our condition */
1305 read_lock_irqsave(&ep->lock, flags);
1306 if (!list_empty(&ep->rdllist))
1307 pollflags = POLLIN | POLLRDNORM;
1308 read_unlock_irqrestore(&ep->lock, flags);
1310 return pollflags;
1315 * Since we have to release the lock during the __copy_to_user() operation and
1316 * during the f_op->poll() call, we try to collect the maximum number of items
1317 * by reducing the irqlock/irqunlock switching rate.
1319 static int ep_collect_ready_items(struct eventpoll *ep, struct list_head *txlist, int maxevents)
1321 int nepi;
1322 unsigned long flags;
1323 struct list_head *lsthead = &ep->rdllist, *lnk;
1324 struct epitem *epi;
1326 write_lock_irqsave(&ep->lock, flags);
1328 for (nepi = 0, lnk = lsthead->next; lnk != lsthead && nepi < maxevents;) {
1329 epi = list_entry(lnk, struct epitem, rdllink);
1331 lnk = lnk->next;
1333 /* If this file is already in the ready list we exit soon */
1334 if (!ep_is_linked(&epi->txlink)) {
1336 * This is initialized in this way so that the default
1337 * behaviour of the reinjecting code will be to push back
1338 * the item inside the ready list.
1340 epi->revents = epi->event.events;
1342 /* Link the ready item into the transfer list */
1343 list_add(&epi->txlink, txlist);
1344 nepi++;
1347 * Unlink the item from the ready list.
1349 ep_list_del(&epi->rdllink);
1353 write_unlock_irqrestore(&ep->lock, flags);
1355 return nepi;
1360 * This function is called without holding the "ep->lock" since the call to
1361 * __copy_to_user() might sleep, and also f_op->poll() might reenable the IRQ
1362 * because of the way poll() is traditionally implemented in Linux.
1364 static int ep_send_events(struct eventpoll *ep, struct list_head *txlist,
1365 struct epoll_event __user *events)
1367 int eventcnt = 0;
1368 unsigned int revents;
1369 struct list_head *lnk;
1370 struct epitem *epi;
1373 * We can loop without lock because this is a task private list.
1374 * The test done during the collection loop will guarantee us that
1375 * another task will not try to collect this file. Also, items
1376 * cannot vanish during the loop because we are holding "sem".
1378 list_for_each(lnk, txlist) {
1379 epi = list_entry(lnk, struct epitem, txlink);
1382 * Get the ready file event set. We can safely use the file
1383 * because we are holding the "sem" in read and this will
1384 * guarantee that both the file and the item will not vanish.
1386 revents = epi->ffd.file->f_op->poll(epi->ffd.file, NULL);
1389 * Set the return event set for the current file descriptor.
1390 * Note that only the task task was successfully able to link
1391 * the item to its "txlist" will write this field.
1393 epi->revents = revents & epi->event.events;
1395 if (epi->revents) {
1396 if (__put_user(epi->revents,
1397 &events[eventcnt].events) ||
1398 __put_user(epi->event.data,
1399 &events[eventcnt].data))
1400 return -EFAULT;
1401 if (epi->event.events & EPOLLONESHOT)
1402 epi->event.events &= EP_PRIVATE_BITS;
1403 eventcnt++;
1406 return eventcnt;
1411 * Walk through the transfer list we collected with ep_collect_ready_items()
1412 * and, if 1) the item is still "alive" 2) its event set is not empty 3) it's
1413 * not already linked, links it to the ready list. Same as above, we are holding
1414 * "sem" so items cannot vanish underneath our nose.
1416 static void ep_reinject_items(struct eventpoll *ep, struct list_head *txlist)
1418 int ricnt = 0, pwake = 0;
1419 unsigned long flags;
1420 struct epitem *epi;
1422 write_lock_irqsave(&ep->lock, flags);
1424 while (!list_empty(txlist)) {
1425 epi = list_entry(txlist->next, struct epitem, txlink);
1427 /* Unlink the current item from the transfer list */
1428 ep_list_del(&epi->txlink);
1431 * If the item is no more linked to the interest set, we don't
1432 * have to push it inside the ready list because the following
1433 * ep_release_epitem() is going to drop it. Also, if the current
1434 * item is set to have an Edge Triggered behaviour, we don't have
1435 * to push it back either.
1437 if (ep_rb_linked(&epi->rbn) && !(epi->event.events & EPOLLET) &&
1438 (epi->revents & epi->event.events) && !ep_is_linked(&epi->rdllink)) {
1439 list_add_tail(&epi->rdllink, &ep->rdllist);
1440 ricnt++;
1444 if (ricnt) {
1446 * Wake up ( if active ) both the eventpoll wait list and the ->poll()
1447 * wait list.
1449 if (waitqueue_active(&ep->wq))
1450 __wake_up_locked(&ep->wq, TASK_UNINTERRUPTIBLE |
1451 TASK_INTERRUPTIBLE);
1452 if (waitqueue_active(&ep->poll_wait))
1453 pwake++;
1456 write_unlock_irqrestore(&ep->lock, flags);
1458 /* We have to call this outside the lock */
1459 if (pwake)
1460 ep_poll_safewake(&psw, &ep->poll_wait);
1465 * Perform the transfer of events to user space.
1467 static int ep_events_transfer(struct eventpoll *ep,
1468 struct epoll_event __user *events, int maxevents)
1470 int eventcnt = 0;
1471 struct list_head txlist;
1473 INIT_LIST_HEAD(&txlist);
1476 * We need to lock this because we could be hit by
1477 * eventpoll_release_file() and epoll_ctl(EPOLL_CTL_DEL).
1479 down_read(&ep->sem);
1481 /* Collect/extract ready items */
1482 if (ep_collect_ready_items(ep, &txlist, maxevents) > 0) {
1483 /* Build result set in userspace */
1484 eventcnt = ep_send_events(ep, &txlist, events);
1486 /* Reinject ready items into the ready list */
1487 ep_reinject_items(ep, &txlist);
1490 up_read(&ep->sem);
1492 return eventcnt;
1496 static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
1497 int maxevents, long timeout)
1499 int res, eavail;
1500 unsigned long flags;
1501 long jtimeout;
1502 wait_queue_t wait;
1505 * Calculate the timeout by checking for the "infinite" value ( -1 )
1506 * and the overflow condition. The passed timeout is in milliseconds,
1507 * that why (t * HZ) / 1000.
1509 jtimeout = (timeout < 0 || timeout >= EP_MAX_MSTIMEO) ?
1510 MAX_SCHEDULE_TIMEOUT : (timeout * HZ + 999) / 1000;
1512 retry:
1513 write_lock_irqsave(&ep->lock, flags);
1515 res = 0;
1516 if (list_empty(&ep->rdllist)) {
1518 * We don't have any available event to return to the caller.
1519 * We need to sleep here, and we will be wake up by
1520 * ep_poll_callback() when events will become available.
1522 init_waitqueue_entry(&wait, current);
1523 __add_wait_queue(&ep->wq, &wait);
1525 for (;;) {
1527 * We don't want to sleep if the ep_poll_callback() sends us
1528 * a wakeup in between. That's why we set the task state
1529 * to TASK_INTERRUPTIBLE before doing the checks.
1531 set_current_state(TASK_INTERRUPTIBLE);
1532 if (!list_empty(&ep->rdllist) || !jtimeout)
1533 break;
1534 if (signal_pending(current)) {
1535 res = -EINTR;
1536 break;
1539 write_unlock_irqrestore(&ep->lock, flags);
1540 jtimeout = schedule_timeout(jtimeout);
1541 write_lock_irqsave(&ep->lock, flags);
1543 __remove_wait_queue(&ep->wq, &wait);
1545 set_current_state(TASK_RUNNING);
1548 /* Is it worth to try to dig for events ? */
1549 eavail = !list_empty(&ep->rdllist);
1551 write_unlock_irqrestore(&ep->lock, flags);
1554 * Try to transfer events to user space. In case we get 0 events and
1555 * there's still timeout left over, we go trying again in search of
1556 * more luck.
1558 if (!res && eavail &&
1559 !(res = ep_events_transfer(ep, events, maxevents)) && jtimeout)
1560 goto retry;
1562 return res;
1566 static int eventpollfs_delete_dentry(struct dentry *dentry)
1569 return 1;
1573 static struct inode *ep_eventpoll_inode(void)
1575 int error = -ENOMEM;
1576 struct inode *inode = new_inode(eventpoll_mnt->mnt_sb);
1578 if (!inode)
1579 goto eexit_1;
1581 inode->i_fop = &eventpoll_fops;
1584 * Mark the inode dirty from the very beginning,
1585 * that way it will never be moved to the dirty
1586 * list because mark_inode_dirty() will think
1587 * that it already _is_ on the dirty list.
1589 inode->i_state = I_DIRTY;
1590 inode->i_mode = S_IRUSR | S_IWUSR;
1591 inode->i_uid = current->fsuid;
1592 inode->i_gid = current->fsgid;
1593 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1594 return inode;
1596 eexit_1:
1597 return ERR_PTR(error);
1601 static int
1602 eventpollfs_get_sb(struct file_system_type *fs_type, int flags,
1603 const char *dev_name, void *data, struct vfsmount *mnt)
1605 return get_sb_pseudo(fs_type, "eventpoll:", NULL, EVENTPOLLFS_MAGIC,
1606 mnt);
1610 static int __init eventpoll_init(void)
1612 int error;
1614 mutex_init(&epmutex);
1616 /* Initialize the structure used to perform safe poll wait head wake ups */
1617 ep_poll_safewake_init(&psw);
1619 /* Allocates slab cache used to allocate "struct epitem" items */
1620 epi_cache = kmem_cache_create("eventpoll_epi", sizeof(struct epitem),
1621 0, SLAB_HWCACHE_ALIGN|EPI_SLAB_DEBUG|SLAB_PANIC,
1622 NULL, NULL);
1624 /* Allocates slab cache used to allocate "struct eppoll_entry" */
1625 pwq_cache = kmem_cache_create("eventpoll_pwq",
1626 sizeof(struct eppoll_entry), 0,
1627 EPI_SLAB_DEBUG|SLAB_PANIC, NULL, NULL);
1630 * Register the virtual file system that will be the source of inodes
1631 * for the eventpoll files
1633 error = register_filesystem(&eventpoll_fs_type);
1634 if (error)
1635 goto epanic;
1637 /* Mount the above commented virtual file system */
1638 eventpoll_mnt = kern_mount(&eventpoll_fs_type);
1639 error = PTR_ERR(eventpoll_mnt);
1640 if (IS_ERR(eventpoll_mnt))
1641 goto epanic;
1643 DNPRINTK(3, (KERN_INFO "[%p] eventpoll: successfully initialized.\n",
1644 current));
1645 return 0;
1647 epanic:
1648 panic("eventpoll_init() failed\n");
1652 static void __exit eventpoll_exit(void)
1654 /* Undo all operations done inside eventpoll_init() */
1655 unregister_filesystem(&eventpoll_fs_type);
1656 mntput(eventpoll_mnt);
1657 kmem_cache_destroy(pwq_cache);
1658 kmem_cache_destroy(epi_cache);
1661 module_init(eventpoll_init);
1662 module_exit(eventpoll_exit);
1664 MODULE_LICENSE("GPL");