Make mailslots use as much of the default async fd implementation as
[wine] / server / fd.c
1 /*
2  * Server-side file descriptor management
3  *
4  * Copyright (C) 2000, 2003 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <signal.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #ifdef HAVE_POLL_H
34 #include <poll.h>
35 #endif
36 #ifdef HAVE_SYS_POLL_H
37 #include <sys/poll.h>
38 #endif
39 #ifdef HAVE_STDINT_H
40 #include <stdint.h>
41 #endif
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46
47 #include "object.h"
48 #include "file.h"
49 #include "handle.h"
50 #include "process.h"
51 #include "request.h"
52
53 #include "winternl.h"
54
55 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
56 # include <sys/epoll.h>
57 # define USE_EPOLL
58 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
59 # define USE_EPOLL
60 # define EPOLLIN POLLIN
61 # define EPOLLOUT POLLOUT
62 # define EPOLLERR POLLERR
63 # define EPOLLHUP POLLHUP
64 # define EPOLL_CTL_ADD 1
65 # define EPOLL_CTL_DEL 2
66 # define EPOLL_CTL_MOD 3
67
68 typedef union epoll_data
69 {
70   void *ptr;
71   int fd;
72   uint32_t u32;
73   uint64_t u64;
74 } epoll_data_t;
75
76 struct epoll_event
77 {
78   uint32_t events;
79   epoll_data_t data;
80 };
81
82 #define SYSCALL_RET(ret) do { \
83         if (ret < 0) { errno = -ret; ret = -1; } \
84         return ret; \
85     } while(0)
86
87 static inline int epoll_create( int size )
88 {
89     int ret;
90     __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
91              : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
92     SYSCALL_RET(ret);
93 }
94
95 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
96 {
97     int ret;
98     __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
99              : "=a" (ret)
100              : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
101     SYSCALL_RET(ret);
102 }
103
104 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
105 {
106     int ret;
107     __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
108              : "=a" (ret)
109              : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
110              : "memory" );
111     SYSCALL_RET(ret);
112 }
113 #undef SYSCALL_RET
114
115 #endif /* linux && __i386__ && HAVE_STDINT_H */
116
117
118 /* Because of the stupid Posix locking semantics, we need to keep
119  * track of all file descriptors referencing a given file, and not
120  * close a single one until all the locks are gone (sigh).
121  */
122
123 /* file descriptor object */
124
125 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
126 struct closed_fd
127 {
128     struct list entry;       /* entry in inode closed list */
129     int         fd;          /* the unix file descriptor */
130     char        unlink[1];   /* name to unlink on close (if any) */
131 };
132
133 struct fd
134 {
135     struct object        obj;         /* object header */
136     const struct fd_ops *fd_ops;      /* file descriptor operations */
137     struct inode        *inode;       /* inode that this fd belongs to */
138     struct list          inode_entry; /* entry in inode fd list */
139     struct closed_fd    *closed;      /* structure to store the unix fd at destroy time */
140     struct object       *user;        /* object using this file descriptor */
141     struct list          locks;       /* list of locks on this fd */
142     unsigned int         access;      /* file access (GENERIC_READ/WRITE) */
143     unsigned int         sharing;     /* file sharing mode */
144     int                  unix_fd;     /* unix file descriptor */
145     int                  fs_locks;    /* can we use filesystem locks for this fd? */
146     int                  poll_index;  /* index of fd in poll array */
147     struct list          read_q;      /* async readers of this fd */
148     struct list          write_q;     /* async writers of this fd */
149 };
150
151 static void fd_dump( struct object *obj, int verbose );
152 static void fd_destroy( struct object *obj );
153
154 static const struct object_ops fd_ops =
155 {
156     sizeof(struct fd),        /* size */
157     fd_dump,                  /* dump */
158     no_add_queue,             /* add_queue */
159     NULL,                     /* remove_queue */
160     NULL,                     /* signaled */
161     NULL,                     /* satisfied */
162     no_signal,                /* signal */
163     no_get_fd,                /* get_fd */
164     no_close_handle,          /* close_handle */
165     fd_destroy                /* destroy */
166 };
167
168 /* inode object */
169
170 struct inode
171 {
172     struct object       obj;        /* object header */
173     struct list         entry;      /* inode hash list entry */
174     unsigned int        hash;       /* hashing code */
175     dev_t               dev;        /* device number */
176     ino_t               ino;        /* inode number */
177     struct list         open;       /* list of open file descriptors */
178     struct list         locks;      /* list of file locks */
179     struct list         closed;     /* list of file descriptors to close at destroy time */
180 };
181
182 static void inode_dump( struct object *obj, int verbose );
183 static void inode_destroy( struct object *obj );
184
185 static const struct object_ops inode_ops =
186 {
187     sizeof(struct inode),     /* size */
188     inode_dump,               /* dump */
189     no_add_queue,             /* add_queue */
190     NULL,                     /* remove_queue */
191     NULL,                     /* signaled */
192     NULL,                     /* satisfied */
193     no_signal,                /* signal */
194     no_get_fd,                /* get_fd */
195     no_close_handle,          /* close_handle */
196     inode_destroy             /* destroy */
197 };
198
199 /* file lock object */
200
201 struct file_lock
202 {
203     struct object       obj;         /* object header */
204     struct fd          *fd;          /* fd owning this lock */
205     struct list         fd_entry;    /* entry in list of locks on a given fd */
206     struct list         inode_entry; /* entry in inode list of locks */
207     int                 shared;      /* shared lock? */
208     file_pos_t          start;       /* locked region is interval [start;end) */
209     file_pos_t          end;
210     struct process     *process;     /* process owning this lock */
211     struct list         proc_entry;  /* entry in list of locks owned by the process */
212 };
213
214 static void file_lock_dump( struct object *obj, int verbose );
215 static int file_lock_signaled( struct object *obj, struct thread *thread );
216
217 static const struct object_ops file_lock_ops =
218 {
219     sizeof(struct file_lock),   /* size */
220     file_lock_dump,             /* dump */
221     add_queue,                  /* add_queue */
222     remove_queue,               /* remove_queue */
223     file_lock_signaled,         /* signaled */
224     no_satisfied,               /* satisfied */
225     no_signal,                  /* signal */
226     no_get_fd,                  /* get_fd */
227     no_close_handle,            /* close_handle */
228     no_destroy                  /* destroy */
229 };
230
231
232 #define OFF_T_MAX       (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
233 #define FILE_POS_T_MAX  (~(file_pos_t)0)
234
235 static file_pos_t max_unix_offset = OFF_T_MAX;
236
237 #define DUMP_LONG_LONG(val) do { \
238     if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
239         fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
240     else \
241         fprintf( stderr, "%lx", (unsigned long)(val) ); \
242   } while (0)
243
244
245
246 /****************************************************************/
247 /* timeouts support */
248
249 struct timeout_user
250 {
251     struct list           entry;      /* entry in sorted timeout list */
252     struct timeval        when;       /* timeout expiry (absolute time) */
253     timeout_callback      callback;   /* callback function */
254     void                 *private;    /* callback private data */
255 };
256
257 static struct list timeout_list = LIST_INIT(timeout_list);   /* sorted timeouts list */
258
259 /* add a timeout user */
260 struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
261                                        void *private )
262 {
263     struct timeout_user *user;
264     struct list *ptr;
265
266     if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
267     user->when     = *when;
268     user->callback = func;
269     user->private  = private;
270
271     /* Now insert it in the linked list */
272
273     LIST_FOR_EACH( ptr, &timeout_list )
274     {
275         struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
276         if (!time_before( &timeout->when, when )) break;
277     }
278     list_add_before( ptr, &user->entry );
279     return user;
280 }
281
282 /* remove a timeout user */
283 void remove_timeout_user( struct timeout_user *user )
284 {
285     list_remove( &user->entry );
286     free( user );
287 }
288
289 /* add a timeout in milliseconds to an absolute time */
290 void add_timeout( struct timeval *when, int timeout )
291 {
292     if (timeout)
293     {
294         long sec = timeout / 1000;
295         if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
296         {
297             when->tv_usec -= 1000000;
298             when->tv_sec++;
299         }
300         when->tv_sec += sec;
301     }
302 }
303
304
305 /****************************************************************/
306 /* poll support */
307
308 static struct fd **poll_users;              /* users array */
309 static struct pollfd *pollfd;               /* poll fd array */
310 static int nb_users;                        /* count of array entries actually in use */
311 static int active_users;                    /* current number of active users */
312 static int allocated_users;                 /* count of allocated entries in the array */
313 static struct fd **freelist;                /* list of free entries in the array */
314
315 #ifdef USE_EPOLL
316
317 static int epoll_fd;
318 static struct epoll_event *epoll_events;
319
320 /* set the events that epoll waits for on this fd; helper for set_fd_events */
321 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
322 {
323     struct epoll_event ev;
324     int ctl;
325
326     if (epoll_fd == -1) return;
327
328     if (events == -1)  /* stop waiting on this fd completely */
329     {
330         if (pollfd[user].fd == -1) return;  /* already removed */
331         ctl = EPOLL_CTL_DEL;
332     }
333     else if (pollfd[user].fd == -1)
334     {
335         if (pollfd[user].events) return;  /* stopped waiting on it, don't restart */
336         ctl = EPOLL_CTL_ADD;
337     }
338     else
339     {
340         if (pollfd[user].events == events) return;  /* nothing to do */
341         ctl = EPOLL_CTL_MOD;
342     }
343
344     ev.events = events;
345     ev.data.u32 = user;
346
347     if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
348     {
349         if (errno == ENOMEM)  /* not enough memory, give up on epoll */
350         {
351             close( epoll_fd );
352             epoll_fd = -1;
353         }
354         else perror( "epoll_ctl" );  /* should not happen */
355     }
356 }
357
358 #else /* USE_EPOLL */
359
360 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
361 {
362 }
363
364 #endif /* USE_EPOLL */
365
366
367 /* add a user in the poll array and return its index, or -1 on failure */
368 static int add_poll_user( struct fd *fd )
369 {
370     int ret;
371     if (freelist)
372     {
373         ret = freelist - poll_users;
374         freelist = (struct fd **)poll_users[ret];
375     }
376     else
377     {
378         if (nb_users == allocated_users)
379         {
380             struct fd **newusers;
381             struct pollfd *newpoll;
382             int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
383             if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
384             if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
385             {
386                 if (allocated_users)
387                     poll_users = newusers;
388                 else
389                     free( newusers );
390                 return -1;
391             }
392             poll_users = newusers;
393             pollfd = newpoll;
394 #ifdef USE_EPOLL
395             if (!allocated_users) epoll_fd = epoll_create( new_count );
396             if (epoll_fd != -1)
397             {
398                 struct epoll_event *new_events;
399                 if (!(new_events = realloc( epoll_events, new_count * sizeof(*epoll_events) )))
400                     return -1;
401                 epoll_events = new_events;
402             }
403 #endif
404             allocated_users = new_count;
405         }
406         ret = nb_users++;
407     }
408     pollfd[ret].fd = -1;
409     pollfd[ret].events = 0;
410     pollfd[ret].revents = 0;
411     poll_users[ret] = fd;
412     active_users++;
413     return ret;
414 }
415
416 /* remove a user from the poll list */
417 static void remove_poll_user( struct fd *fd, int user )
418 {
419     assert( user >= 0 );
420     assert( poll_users[user] == fd );
421
422 #ifdef USE_EPOLL
423     if (epoll_fd != -1 && pollfd[user].fd != -1)
424     {
425         struct epoll_event dummy;
426         epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
427     }
428 #endif
429     pollfd[user].fd = -1;
430     pollfd[user].events = 0;
431     pollfd[user].revents = 0;
432     poll_users[user] = (struct fd *)freelist;
433     freelist = &poll_users[user];
434     active_users--;
435 }
436
437 /* process pending timeouts and return the time until the next timeout, in milliseconds */
438 static int get_next_timeout(void)
439 {
440     if (!list_empty( &timeout_list ))
441     {
442         struct list expired_list, *ptr;
443         struct timeval now;
444
445         gettimeofday( &now, NULL );
446
447         /* first remove all expired timers from the list */
448
449         list_init( &expired_list );
450         while ((ptr = list_head( &timeout_list )) != NULL)
451         {
452             struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
453
454             if (!time_before( &now, &timeout->when ))
455             {
456                 list_remove( &timeout->entry );
457                 list_add_tail( &expired_list, &timeout->entry );
458             }
459             else break;
460         }
461
462         /* now call the callback for all the removed timers */
463
464         while ((ptr = list_head( &expired_list )) != NULL)
465         {
466             struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
467             list_remove( &timeout->entry );
468             timeout->callback( timeout->private );
469             free( timeout );
470         }
471
472         if ((ptr = list_head( &timeout_list )) != NULL)
473         {
474             struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
475             int diff = (timeout->when.tv_sec - now.tv_sec) * 1000
476                      + (timeout->when.tv_usec - now.tv_usec) / 1000;
477             if (diff < 0) diff = 0;
478             return diff;
479         }
480     }
481     return -1;  /* no pending timeouts */
482 }
483
484 /* server main poll() loop */
485 void main_loop(void)
486 {
487     int i, ret, timeout;
488
489 #ifdef USE_EPOLL
490     assert( POLLIN == EPOLLIN );
491     assert( POLLOUT == EPOLLOUT );
492     assert( POLLERR == EPOLLERR );
493     assert( POLLHUP == EPOLLHUP );
494
495     if (epoll_fd != -1)
496     {
497         while (active_users)
498         {
499             timeout = get_next_timeout();
500
501             if (!active_users) break;  /* last user removed by a timeout */
502             if (epoll_fd == -1) break;  /* an error occurred with epoll */
503
504             ret = epoll_wait( epoll_fd, epoll_events, allocated_users, timeout );
505
506             /* put the events into the pollfd array first, like poll does */
507             for (i = 0; i < ret; i++)
508             {
509                 int user = epoll_events[i].data.u32;
510                 pollfd[user].revents = epoll_events[i].events;
511             }
512
513             /* read events from the pollfd array, as set_fd_events may modify them */
514             for (i = 0; i < ret; i++)
515             {
516                 int user = epoll_events[i].data.u32;
517                 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
518             }
519         }
520     }
521     /* fall through to normal poll loop */
522 #endif  /* USE_EPOLL */
523
524     while (active_users)
525     {
526         timeout = get_next_timeout();
527
528         if (!active_users) break;  /* last user removed by a timeout */
529
530         ret = poll( pollfd, nb_users, timeout );
531         if (ret > 0)
532         {
533             for (i = 0; i < nb_users; i++)
534             {
535                 if (pollfd[i].revents)
536                 {
537                     fd_poll_event( poll_users[i], pollfd[i].revents );
538                     if (!--ret) break;
539                 }
540             }
541         }
542     }
543 }
544
545
546 /****************************************************************/
547 /* inode functions */
548
549 #define HASH_SIZE 37
550
551 static struct list inode_hash[HASH_SIZE];
552
553 /* close all pending file descriptors in the closed list */
554 static void inode_close_pending( struct inode *inode )
555 {
556     struct list *ptr = list_head( &inode->closed );
557
558     while (ptr)
559     {
560         struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
561         struct list *next = list_next( &inode->closed, ptr );
562
563         if (fd->fd != -1)
564         {
565             close( fd->fd );
566             fd->fd = -1;
567         }
568         if (!fd->unlink)  /* get rid of it unless there's an unlink pending on that file */
569         {
570             list_remove( ptr );
571             free( fd );
572         }
573         ptr = next;
574     }
575 }
576
577
578 static void inode_dump( struct object *obj, int verbose )
579 {
580     struct inode *inode = (struct inode *)obj;
581     fprintf( stderr, "Inode dev=" );
582     DUMP_LONG_LONG( inode->dev );
583     fprintf( stderr, " ino=" );
584     DUMP_LONG_LONG( inode->ino );
585     fprintf( stderr, "\n" );
586 }
587
588 static void inode_destroy( struct object *obj )
589 {
590     struct inode *inode = (struct inode *)obj;
591     struct list *ptr;
592
593     assert( list_empty(&inode->open) );
594     assert( list_empty(&inode->locks) );
595
596     list_remove( &inode->entry );
597
598     while ((ptr = list_head( &inode->closed )))
599     {
600         struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
601         list_remove( ptr );
602         if (fd->fd != -1) close( fd->fd );
603         if (fd->unlink[0])
604         {
605             /* make sure it is still the same file */
606             struct stat st;
607             if (!stat( fd->unlink, &st ) && st.st_dev == inode->dev && st.st_ino == inode->ino)
608             {
609                 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
610                 else unlink( fd->unlink );
611             }
612         }
613         free( fd );
614     }
615 }
616
617 /* retrieve the inode object for a given fd, creating it if needed */
618 static struct inode *get_inode( dev_t dev, ino_t ino )
619 {
620     struct list *ptr;
621     struct inode *inode;
622     unsigned int hash = (dev ^ ino) % HASH_SIZE;
623
624     if (inode_hash[hash].next)
625     {
626         LIST_FOR_EACH( ptr, &inode_hash[hash] )
627         {
628             inode = LIST_ENTRY( ptr, struct inode, entry );
629             if (inode->dev == dev && inode->ino == ino)
630                 return (struct inode *)grab_object( inode );
631         }
632     }
633     else list_init( &inode_hash[hash] );
634
635     /* not found, create it */
636     if ((inode = alloc_object( &inode_ops )))
637     {
638         inode->hash   = hash;
639         inode->dev    = dev;
640         inode->ino    = ino;
641         list_init( &inode->open );
642         list_init( &inode->locks );
643         list_init( &inode->closed );
644         list_add_head( &inode_hash[hash], &inode->entry );
645     }
646     return inode;
647 }
648
649 /* add fd to the indoe list of file descriptors to close */
650 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
651 {
652     if (!list_empty( &inode->locks ))
653     {
654         list_add_head( &inode->closed, &fd->entry );
655     }
656     else if (fd->unlink[0])  /* close the fd but keep the structure around for unlink */
657     {
658         close( fd->fd );
659         fd->fd = -1;
660         list_add_head( &inode->closed, &fd->entry );
661     }
662     else  /* no locks on this inode and no unlink, get rid of the fd */
663     {
664         close( fd->fd );
665         free( fd );
666     }
667 }
668
669
670 /****************************************************************/
671 /* file lock functions */
672
673 static void file_lock_dump( struct object *obj, int verbose )
674 {
675     struct file_lock *lock = (struct file_lock *)obj;
676     fprintf( stderr, "Lock %s fd=%p proc=%p start=",
677              lock->shared ? "shared" : "excl", lock->fd, lock->process );
678     DUMP_LONG_LONG( lock->start );
679     fprintf( stderr, " end=" );
680     DUMP_LONG_LONG( lock->end );
681     fprintf( stderr, "\n" );
682 }
683
684 static int file_lock_signaled( struct object *obj, struct thread *thread )
685 {
686     struct file_lock *lock = (struct file_lock *)obj;
687     /* lock is signaled if it has lost its owner */
688     return !lock->process;
689 }
690
691 /* set (or remove) a Unix lock if possible for the given range */
692 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
693 {
694     struct flock fl;
695
696     if (!fd->fs_locks) return 1;  /* no fs locks possible for this fd */
697     for (;;)
698     {
699         if (start == end) return 1;  /* can't set zero-byte lock */
700         if (start > max_unix_offset) return 1;  /* ignore it */
701         fl.l_type   = type;
702         fl.l_whence = SEEK_SET;
703         fl.l_start  = start;
704         if (!end || end > max_unix_offset) fl.l_len = 0;
705         else fl.l_len = end - start;
706         if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
707
708         switch(errno)
709         {
710         case EACCES:
711             /* check whether locks work at all on this file system */
712             if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
713             {
714                 set_error( STATUS_FILE_LOCK_CONFLICT );
715                 return 0;
716             }
717             /* fall through */
718         case EIO:
719         case ENOLCK:
720             /* no locking on this fs, just ignore it */
721             fd->fs_locks = 0;
722             return 1;
723         case EAGAIN:
724             set_error( STATUS_FILE_LOCK_CONFLICT );
725             return 0;
726         case EBADF:
727             /* this can happen if we try to set a write lock on a read-only file */
728             /* we just ignore that error */
729             if (fl.l_type == F_WRLCK) return 1;
730             set_error( STATUS_ACCESS_DENIED );
731             return 0;
732 #ifdef EOVERFLOW
733         case EOVERFLOW:
734 #endif
735         case EINVAL:
736             /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
737             /* in that case we shrink the limit and retry */
738             if (max_unix_offset > INT_MAX)
739             {
740                 max_unix_offset = INT_MAX;
741                 break;  /* retry */
742             }
743             /* fall through */
744         default:
745             file_set_error();
746             return 0;
747         }
748     }
749 }
750
751 /* check if interval [start;end) overlaps the lock */
752 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
753 {
754     if (lock->end && start >= lock->end) return 0;
755     if (end && lock->start >= end) return 0;
756     return 1;
757 }
758
759 /* remove Unix locks for all bytes in the specified area that are no longer locked */
760 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
761 {
762     struct hole
763     {
764         struct hole *next;
765         struct hole *prev;
766         file_pos_t   start;
767         file_pos_t   end;
768     } *first, *cur, *next, *buffer;
769
770     struct list *ptr;
771     int count = 0;
772
773     if (!fd->inode) return;
774     if (!fd->fs_locks) return;
775     if (start == end || start > max_unix_offset) return;
776     if (!end || end > max_unix_offset) end = max_unix_offset + 1;
777
778     /* count the number of locks overlapping the specified area */
779
780     LIST_FOR_EACH( ptr, &fd->inode->locks )
781     {
782         struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
783         if (lock->start == lock->end) continue;
784         if (lock_overlaps( lock, start, end )) count++;
785     }
786
787     if (!count)  /* no locks at all, we can unlock everything */
788     {
789         set_unix_lock( fd, start, end, F_UNLCK );
790         return;
791     }
792
793     /* allocate space for the list of holes */
794     /* max. number of holes is number of locks + 1 */
795
796     if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
797     first = buffer;
798     first->next  = NULL;
799     first->prev  = NULL;
800     first->start = start;
801     first->end   = end;
802     next = first + 1;
803
804     /* build a sorted list of unlocked holes in the specified area */
805
806     LIST_FOR_EACH( ptr, &fd->inode->locks )
807     {
808         struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
809         if (lock->start == lock->end) continue;
810         if (!lock_overlaps( lock, start, end )) continue;
811
812         /* go through all the holes touched by this lock */
813         for (cur = first; cur; cur = cur->next)
814         {
815             if (cur->end <= lock->start) continue; /* hole is before start of lock */
816             if (lock->end && cur->start >= lock->end) break;  /* hole is after end of lock */
817
818             /* now we know that lock is overlapping hole */
819
820             if (cur->start >= lock->start)  /* lock starts before hole, shrink from start */
821             {
822                 cur->start = lock->end;
823                 if (cur->start && cur->start < cur->end) break;  /* done with this lock */
824                 /* now hole is empty, remove it */
825                 if (cur->next) cur->next->prev = cur->prev;
826                 if (cur->prev) cur->prev->next = cur->next;
827                 else if (!(first = cur->next)) goto done;  /* no more holes at all */
828             }
829             else if (!lock->end || cur->end <= lock->end)  /* lock larger than hole, shrink from end */
830             {
831                 cur->end = lock->start;
832                 assert( cur->start < cur->end );
833             }
834             else  /* lock is in the middle of hole, split hole in two */
835             {
836                 next->prev = cur;
837                 next->next = cur->next;
838                 cur->next = next;
839                 next->start = lock->end;
840                 next->end = cur->end;
841                 cur->end = lock->start;
842                 assert( next->start < next->end );
843                 assert( cur->end < next->start );
844                 next++;
845                 break;  /* done with this lock */
846             }
847         }
848     }
849
850     /* clear Unix locks for all the holes */
851
852     for (cur = first; cur; cur = cur->next)
853         set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
854
855  done:
856     free( buffer );
857 }
858
859 /* create a new lock on a fd */
860 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
861 {
862     struct file_lock *lock;
863
864     if (!fd->inode)  /* not a regular file */
865     {
866         set_error( STATUS_INVALID_HANDLE );
867         return NULL;
868     }
869
870     if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
871     lock->shared  = shared;
872     lock->start   = start;
873     lock->end     = end;
874     lock->fd      = fd;
875     lock->process = current->process;
876
877     /* now try to set a Unix lock */
878     if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
879     {
880         release_object( lock );
881         return NULL;
882     }
883     list_add_head( &fd->locks, &lock->fd_entry );
884     list_add_head( &fd->inode->locks, &lock->inode_entry );
885     list_add_head( &lock->process->locks, &lock->proc_entry );
886     return lock;
887 }
888
889 /* remove an existing lock */
890 static void remove_lock( struct file_lock *lock, int remove_unix )
891 {
892     struct inode *inode = lock->fd->inode;
893
894     list_remove( &lock->fd_entry );
895     list_remove( &lock->inode_entry );
896     list_remove( &lock->proc_entry );
897     if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
898     if (list_empty( &inode->locks )) inode_close_pending( inode );
899     lock->process = NULL;
900     wake_up( &lock->obj, 0 );
901     release_object( lock );
902 }
903
904 /* remove all locks owned by a given process */
905 void remove_process_locks( struct process *process )
906 {
907     struct list *ptr;
908
909     while ((ptr = list_head( &process->locks )))
910     {
911         struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
912         remove_lock( lock, 1 );  /* this removes it from the list */
913     }
914 }
915
916 /* remove all locks on a given fd */
917 static void remove_fd_locks( struct fd *fd )
918 {
919     file_pos_t start = FILE_POS_T_MAX, end = 0;
920     struct list *ptr;
921
922     while ((ptr = list_head( &fd->locks )))
923     {
924         struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
925         if (lock->start < start) start = lock->start;
926         if (!lock->end || lock->end > end) end = lock->end - 1;
927         remove_lock( lock, 0 );
928     }
929     if (start < end) remove_unix_locks( fd, start, end + 1 );
930 }
931
932 /* add a lock on an fd */
933 /* returns handle to wait on */
934 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
935 {
936     struct list *ptr;
937     file_pos_t end = start + count;
938
939     /* don't allow wrapping locks */
940     if (end && end < start)
941     {
942         set_error( STATUS_INVALID_PARAMETER );
943         return 0;
944     }
945
946     /* check if another lock on that file overlaps the area */
947     LIST_FOR_EACH( ptr, &fd->inode->locks )
948     {
949         struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
950         if (!lock_overlaps( lock, start, end )) continue;
951         if (lock->shared && shared) continue;
952         /* found one */
953         if (!wait)
954         {
955             set_error( STATUS_FILE_LOCK_CONFLICT );
956             return 0;
957         }
958         set_error( STATUS_PENDING );
959         return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
960     }
961
962     /* not found, add it */
963     if (add_lock( fd, shared, start, end )) return 0;
964     if (get_error() == STATUS_FILE_LOCK_CONFLICT)
965     {
966         /* Unix lock conflict -> tell client to wait and retry */
967         if (wait) set_error( STATUS_PENDING );
968     }
969     return 0;
970 }
971
972 /* remove a lock on an fd */
973 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
974 {
975     struct list *ptr;
976     file_pos_t end = start + count;
977
978     /* find an existing lock with the exact same parameters */
979     LIST_FOR_EACH( ptr, &fd->locks )
980     {
981         struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
982         if ((lock->start == start) && (lock->end == end))
983         {
984             remove_lock( lock, 1 );
985             return;
986         }
987     }
988     set_error( STATUS_FILE_LOCK_CONFLICT );
989 }
990
991
992 /****************************************************************/
993 /* asynchronous operations support */
994
995 struct async
996 {
997     struct thread       *thread;
998     void                *apc;
999     void                *user;
1000     void                *sb;
1001     struct timeout_user *timeout;
1002     struct list          entry;
1003 };
1004
1005 /* notifies client thread of new status of its async request */
1006 /* destroys the server side of it */
1007 static void async_terminate( struct async *async, int status )
1008 {
1009     thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO,
1010                       1, async->user, async->sb, (void *)status );
1011
1012     if (async->timeout) remove_timeout_user( async->timeout );
1013     async->timeout = NULL;
1014     list_remove( &async->entry );
1015     release_object( async->thread );
1016     free( async );
1017 }
1018
1019 /* cb for timeout on an async request */
1020 static void async_callback(void *private)
1021 {
1022     struct async *async = (struct async *)private;
1023
1024     /* fprintf(stderr, "async timeout out %p\n", async); */
1025     async->timeout = NULL;
1026     async_terminate( async, STATUS_TIMEOUT );
1027 }
1028
1029 /* create an async on a given queue of a fd */
1030 struct async *create_async(struct thread *thread, int* timeout, struct list *queue,
1031                            void *io_apc, void *io_user, void* io_sb)
1032 {
1033     struct async *async = mem_alloc( sizeof(struct async) );
1034
1035     if (!async) return NULL;
1036
1037     async->thread = (struct thread *)grab_object(thread);
1038     async->apc = io_apc;
1039     async->user = io_user;
1040     async->sb = io_sb;
1041
1042     list_add_tail( queue, &async->entry );
1043
1044     if (timeout)
1045     {
1046         struct timeval when;
1047
1048         gettimeofday( &when, NULL );
1049         add_timeout( &when, *timeout );
1050         async->timeout = add_timeout_user( &when, async_callback, async );
1051     }
1052     else async->timeout = NULL;
1053
1054     return async;
1055 }
1056
1057 /* terminate the async operation at the head of the queue */
1058 void async_terminate_head( struct list *queue, int status )
1059 {
1060     struct list *ptr = list_head( queue );
1061     if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status );
1062 }
1063
1064 /****************************************************************/
1065 /* file descriptor functions */
1066
1067 static void fd_dump( struct object *obj, int verbose )
1068 {
1069     struct fd *fd = (struct fd *)obj;
1070     fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
1071     if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1072     fprintf( stderr, "\n" );
1073 }
1074
1075 static void fd_destroy( struct object *obj )
1076 {
1077     struct fd *fd = (struct fd *)obj;
1078
1079     async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1080     async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1081
1082     remove_fd_locks( fd );
1083     list_remove( &fd->inode_entry );
1084     if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1085     if (fd->inode)
1086     {
1087         inode_add_closed_fd( fd->inode, fd->closed );
1088         release_object( fd->inode );
1089     }
1090     else  /* no inode, close it right away */
1091     {
1092         if (fd->unix_fd != -1) close( fd->unix_fd );
1093     }
1094 }
1095
1096 /* set the events that select waits for on this fd */
1097 void set_fd_events( struct fd *fd, int events )
1098 {
1099     int user = fd->poll_index;
1100     assert( poll_users[user] == fd );
1101
1102     set_fd_epoll_events( fd, user, events );
1103
1104     if (events == -1)  /* stop waiting on this fd completely */
1105     {
1106         pollfd[user].fd = -1;
1107         pollfd[user].events = POLLERR;
1108         pollfd[user].revents = 0;
1109     }
1110     else if (pollfd[user].fd != -1 || !pollfd[user].events)
1111     {
1112         pollfd[user].fd = fd->unix_fd;
1113         pollfd[user].events = events;
1114     }
1115 }
1116
1117 /* allocate an fd object, without setting the unix fd yet */
1118 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
1119 {
1120     struct fd *fd = alloc_object( &fd_ops );
1121
1122     if (!fd) return NULL;
1123
1124     fd->fd_ops     = fd_user_ops;
1125     fd->user       = user;
1126     fd->inode      = NULL;
1127     fd->closed     = NULL;
1128     fd->access     = 0;
1129     fd->sharing    = 0;
1130     fd->unix_fd    = -1;
1131     fd->fs_locks   = 1;
1132     fd->poll_index = -1;
1133     list_init( &fd->inode_entry );
1134     list_init( &fd->locks );
1135     list_init( &fd->read_q );
1136     list_init( &fd->write_q );
1137
1138     if ((fd->poll_index = add_poll_user( fd )) == -1)
1139     {
1140         release_object( fd );
1141         return NULL;
1142     }
1143     return fd;
1144 }
1145
1146 /* check if the desired access is possible without violating */
1147 /* the sharing mode of other opens of the same file */
1148 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
1149 {
1150     unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1151     unsigned int existing_access = 0;
1152     int unlink = 0;
1153     struct list *ptr;
1154
1155     /* if access mode is 0, sharing mode is ignored */
1156     if (!access) sharing = existing_sharing;
1157     fd->access = access;
1158     fd->sharing = sharing;
1159
1160     LIST_FOR_EACH( ptr, &fd->inode->open )
1161     {
1162         struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1163         if (fd_ptr != fd)
1164         {
1165             existing_sharing &= fd_ptr->sharing;
1166             existing_access  |= fd_ptr->access;
1167             if (fd_ptr->closed->unlink[0]) unlink = 1;
1168         }
1169     }
1170
1171     if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) return 0;
1172     if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
1173     if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) return 0;
1174     if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) return 0;
1175     if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
1176     if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
1177     return 1;
1178 }
1179
1180 /* open() wrapper using a struct fd */
1181 /* the fd must have been created with alloc_fd */
1182 /* on error the fd object is released */
1183 struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
1184                     unsigned int access, unsigned int sharing, unsigned int options )
1185 {
1186     struct stat st;
1187     struct closed_fd *closed_fd;
1188     const char *unlink_name = "";
1189
1190     assert( fd->unix_fd == -1 );
1191
1192     if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
1193     if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
1194     {
1195         release_object( fd );
1196         return NULL;
1197     }
1198     /* create the directory if needed */
1199     if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1200     {
1201         if (mkdir( name, 0777 ) == -1)
1202         {
1203             if (errno != EEXIST || (flags & O_EXCL))
1204             {
1205                 file_set_error();
1206                 release_object( fd );
1207                 free( closed_fd );
1208                 return NULL;
1209             }
1210         }
1211         flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1212     }
1213     if ((fd->unix_fd = open( name, flags & ~O_TRUNC, *mode )) == -1)
1214     {
1215         file_set_error();
1216         release_object( fd );
1217         free( closed_fd );
1218         return NULL;
1219     }
1220     closed_fd->fd = fd->unix_fd;
1221     closed_fd->unlink[0] = 0;
1222     fstat( fd->unix_fd, &st );
1223     *mode = st.st_mode;
1224
1225     /* only bother with an inode for normal files and directories */
1226     if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1227     {
1228         struct inode *inode = get_inode( st.st_dev, st.st_ino );
1229
1230         if (!inode)
1231         {
1232             /* we can close the fd because there are no others open on the same file,
1233              * otherwise we wouldn't have failed to allocate a new inode
1234              */
1235             goto error;
1236         }
1237         fd->inode = inode;
1238         fd->closed = closed_fd;
1239         list_add_head( &inode->open, &fd->inode_entry );
1240
1241         /* check directory options */
1242         if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1243         {
1244             release_object( fd );
1245             set_error( STATUS_NOT_A_DIRECTORY );
1246             return NULL;
1247         }
1248         if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1249         {
1250             release_object( fd );
1251             set_error( STATUS_FILE_IS_A_DIRECTORY );
1252             return NULL;
1253         }
1254         if (!check_sharing( fd, access, sharing ))
1255         {
1256             release_object( fd );
1257             set_error( STATUS_SHARING_VIOLATION );
1258             return NULL;
1259         }
1260         strcpy( closed_fd->unlink, unlink_name );
1261         if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
1262     }
1263     else  /* special file */
1264     {
1265         if (options & FILE_DIRECTORY_FILE)
1266         {
1267             set_error( STATUS_NOT_A_DIRECTORY );
1268             goto error;
1269         }
1270         if (unlink_name[0])  /* we can't unlink special files */
1271         {
1272             set_error( STATUS_INVALID_PARAMETER );
1273             goto error;
1274         }
1275         free( closed_fd );
1276     }
1277     return fd;
1278
1279 error:
1280     release_object( fd );
1281     free( closed_fd );
1282     return NULL;
1283 }
1284
1285 /* create an fd for an anonymous file */
1286 /* if the function fails the unix fd is closed */
1287 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
1288 {
1289     struct fd *fd = alloc_fd( fd_user_ops, user );
1290
1291     if (fd)
1292     {
1293         fd->unix_fd = unix_fd;
1294         return fd;
1295     }
1296     close( unix_fd );
1297     return NULL;
1298 }
1299
1300 /* retrieve the object that is using an fd */
1301 void *get_fd_user( struct fd *fd )
1302 {
1303     return fd->user;
1304 }
1305
1306 /* retrieve the unix fd for an object */
1307 int get_unix_fd( struct fd *fd )
1308 {
1309     return fd->unix_fd;
1310 }
1311
1312 /* check if two file descriptors point to the same file */
1313 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1314 {
1315     return fd1->inode == fd2->inode;
1316 }
1317
1318 /* callback for event happening in the main poll() loop */
1319 void fd_poll_event( struct fd *fd, int event )
1320 {
1321     return fd->fd_ops->poll_event( fd, event );
1322 }
1323
1324 /* check if events are pending and if yes return which one(s) */
1325 int check_fd_events( struct fd *fd, int events )
1326 {
1327     struct pollfd pfd;
1328
1329     pfd.fd     = fd->unix_fd;
1330     pfd.events = events;
1331     if (poll( &pfd, 1, 0 ) <= 0) return 0;
1332     return pfd.revents;
1333 }
1334
1335 /* default add_queue() routine for objects that poll() on an fd */
1336 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1337 {
1338     struct fd *fd = get_obj_fd( obj );
1339
1340     if (!fd) return 0;
1341     if (list_empty( &obj->wait_queue ))  /* first on the queue */
1342         set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1343     add_queue( obj, entry );
1344     release_object( fd );
1345     return 1;
1346 }
1347
1348 /* default remove_queue() routine for objects that poll() on an fd */
1349 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1350 {
1351     struct fd *fd = get_obj_fd( obj );
1352
1353     grab_object( obj );
1354     remove_queue( obj, entry );
1355     if (list_empty( &obj->wait_queue ))  /* last on the queue is gone */
1356         set_fd_events( fd, 0 );
1357     release_object( obj );
1358     release_object( fd );
1359 }
1360
1361 /* default signaled() routine for objects that poll() on an fd */
1362 int default_fd_signaled( struct object *obj, struct thread *thread )
1363 {
1364     int events, ret;
1365     struct fd *fd = get_obj_fd( obj );
1366
1367     if (fd->inode) return 1;  /* regular files are always signaled */
1368
1369     events = fd->fd_ops->get_poll_events( fd );
1370     ret = check_fd_events( fd, events ) != 0;
1371
1372     if (ret)
1373         set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
1374     else if (!list_empty( &obj->wait_queue ))
1375         set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
1376
1377     release_object( fd );
1378     return ret;
1379 }
1380
1381 int default_fd_get_poll_events( struct fd *fd )
1382 {
1383     int events = 0;
1384
1385     if (!list_empty( &fd->read_q ))
1386         events |= POLLIN;
1387     if (!list_empty( &fd->write_q ))
1388         events |= POLLOUT;
1389
1390     return events;
1391 }
1392
1393 /* default handler for poll() events */
1394 void default_poll_event( struct fd *fd, int event )
1395 {
1396     if (!list_empty( &fd->read_q ) && (POLLIN & event) )
1397     {
1398         async_terminate_head( &fd->read_q, STATUS_ALERTED );
1399         return;
1400     }
1401     if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
1402     {
1403         async_terminate_head( &fd->write_q, STATUS_ALERTED );
1404         return;
1405     }
1406
1407     /* if an error occurred, stop polling this fd to avoid busy-looping */
1408     if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1409     wake_up( fd->user, 0 );
1410 }
1411
1412 void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count, int *timeout )
1413 {
1414     struct list *queue;
1415     int events;
1416
1417     if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
1418     {
1419         set_error( STATUS_INVALID_HANDLE );
1420         return;
1421     }
1422
1423     switch (type)
1424     {
1425     case ASYNC_TYPE_READ:
1426         queue = &fd->read_q;
1427         break;
1428     case ASYNC_TYPE_WRITE:
1429         queue = &fd->write_q;
1430         break;
1431     default:
1432         set_error( STATUS_INVALID_PARAMETER );
1433         return;
1434     }
1435
1436     if (!create_async( current, timeout, queue, apc, user, io_sb ))
1437         return;
1438
1439     /* Check if the new pending request can be served immediately */
1440     events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1441     if (events) fd->fd_ops->poll_event( fd, events );
1442
1443     set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1444 }
1445
1446 void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count )
1447 {
1448     fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL );
1449 }
1450
1451 void default_fd_cancel_async( struct fd *fd )
1452 {
1453     async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1454     async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1455 }
1456
1457 /* default flush() routine */
1458 int no_flush( struct fd *fd, struct event **event )
1459 {
1460     set_error( STATUS_OBJECT_TYPE_MISMATCH );
1461     return 0;
1462 }
1463
1464 /* default get_file_info() routine */
1465 int no_get_file_info( struct fd *fd )
1466 {
1467     set_error( STATUS_OBJECT_TYPE_MISMATCH );
1468     return 0;
1469 }
1470
1471 /* default queue_async() routine */
1472 void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb, 
1473                      int type, int count)
1474 {
1475     set_error( STATUS_OBJECT_TYPE_MISMATCH );
1476 }
1477
1478 /* default cancel_async() routine */
1479 void no_cancel_async( struct fd *fd )
1480 {
1481     set_error( STATUS_OBJECT_TYPE_MISMATCH );
1482 }
1483
1484 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1485 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1486                                      unsigned int access )
1487 {
1488     struct fd *fd = NULL;
1489     struct object *obj;
1490
1491     if ((obj = get_handle_obj( process, handle, access, NULL )))
1492     {
1493         fd = get_obj_fd( obj );
1494         release_object( obj );
1495     }
1496     return fd;
1497 }
1498
1499 /* flush a file buffers */
1500 DECL_HANDLER(flush_file)
1501 {
1502     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1503     struct event * event = NULL;
1504
1505     if (fd)
1506     {
1507         fd->fd_ops->flush( fd, &event );
1508         if ( event )
1509         {
1510             reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1511         }
1512         release_object( fd );
1513     }
1514 }
1515
1516 /* get a Unix fd to access a file */
1517 DECL_HANDLER(get_handle_fd)
1518 {
1519     struct fd *fd;
1520
1521     reply->fd = -1;
1522
1523     if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1524     {
1525         int unix_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1526         if (unix_fd != -1) reply->fd = unix_fd;
1527         else if (!get_error())
1528         {
1529             assert( fd->unix_fd != -1 );
1530             send_client_fd( current->process, fd->unix_fd, req->handle );
1531         }
1532         reply->flags = fd->fd_ops->get_file_info( fd );
1533         release_object( fd );
1534     }
1535 }
1536
1537 /* create / reschedule an async I/O */
1538 DECL_HANDLER(register_async)
1539 {
1540     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1541
1542     /*
1543      * The queue_async method must do the following:
1544      *
1545      * 1. Get the async_queue for the request of given type.
1546      * 2. Create a new asynchronous request for the selected queue
1547      * 3. Carry out any operations necessary to adjust the object's poll events
1548      *    Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1549      * 4. When the async request is triggered, then send back (with a proper APC)
1550      *    the trigger (STATUS_ALERTED) to the thread that posted the request. 
1551      *    async_destroy() is to be called: it will both notify the sender about
1552      *    the trigger and destroy the request by itself
1553      * See also the implementations in file.c, serial.c, and sock.c.
1554      */
1555
1556     if (fd)
1557     {
1558         fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb, 
1559                                  req->type, req->count );
1560         release_object( fd );
1561     }
1562 }
1563
1564 /* cancels all async I/O */
1565 DECL_HANDLER(cancel_async)
1566 {
1567     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1568     if (fd)
1569     {
1570         /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1571          * NtCancelIoFile() will force the pending APC to be run. Since, 
1572          * Windows only guarantees that the current thread will have no async 
1573          * operation on the current fd when NtCancelIoFile returns, this shall
1574          * do the work.
1575          */
1576         fd->fd_ops->cancel_async( fd );
1577         release_object( fd );
1578     }        
1579 }