crypt32: Move decoding hash messages to a helper function.
[wine] / server / fd.c
index 5c4efd4..cf3a306 100644 (file)
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 
 #ifdef HAVE_SYS_POLL_H
 #include <sys/poll.h>
 #endif
+#ifdef HAVE_LINUX_MAJOR_H
+#include <linux/major.h>
+#endif
+#ifdef HAVE_SYS_STATVFS_H
+#include <sys/statvfs.h>
+#endif
+#ifdef HAVE_SYS_VFS_H
+#include <sys/vfs.h>
+#endif
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#endif
+#ifdef HAVE_SYS_MOUNT_H
+#include <sys/mount.h>
+#endif
+#ifdef HAVE_SYS_STATFS_H
+#include <sys/statfs.h>
+#endif
+#ifdef HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif
+#ifdef HAVE_SYS_EVENT_H
+#include <sys/event.h>
+#undef LIST_INIT
+#undef LIST_ENTRY
+#endif
 #ifdef HAVE_STDINT_H
 #include <stdint.h>
 #endif
@@ -54,6 +80,7 @@
 #include "request.h"
 
 #include "winternl.h"
+#include "winioctl.h"
 
 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
 # include <sys/epoll.h>
@@ -143,13 +170,16 @@ struct fd
     struct object       *user;        /* object using this file descriptor */
     struct list          locks;       /* list of locks on this fd */
     unsigned int         access;      /* file access (FILE_READ_DATA etc.) */
+    unsigned int         options;     /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
     unsigned int         sharing;     /* file sharing mode */
     int                  unix_fd;     /* unix file descriptor */
+    unsigned int         no_fd_status;/* status to return when unix_fd is -1 */
+    int                  signaled :1; /* is the fd signaled? */
     int                  fs_locks :1; /* can we use filesystem locks for this fd? */
-    int                  unmounted :1;/* has the device been unmounted? */
     int                  poll_index;  /* index of fd in poll array */
-    struct list          read_q;      /* async readers of this fd */
-    struct list          write_q;     /* async writers of this fd */
+    struct async_queue  *read_q;      /* async readers of this fd */
+    struct async_queue  *write_q;     /* async writers of this fd */
+    struct async_queue  *wait_q;      /* other async waiters of this fd */
 };
 
 static void fd_dump( struct object *obj, int verbose );
@@ -167,6 +197,7 @@ static const struct object_ops fd_ops =
     no_get_fd,                /* get_fd */
     no_map_access,            /* map_access */
     no_lookup_name,           /* lookup_name */
+    no_open_file,             /* open_file */
     no_close_handle,          /* close_handle */
     fd_destroy                /* destroy */
 };
@@ -200,6 +231,7 @@ static const struct object_ops device_ops =
     no_get_fd,                /* get_fd */
     no_map_access,            /* map_access */
     no_lookup_name,           /* lookup_name */
+    no_open_file,             /* open_file */
     no_close_handle,          /* close_handle */
     device_destroy            /* destroy */
 };
@@ -232,6 +264,7 @@ static const struct object_ops inode_ops =
     no_get_fd,                /* get_fd */
     no_map_access,            /* map_access */
     no_lookup_name,           /* lookup_name */
+    no_open_file,             /* open_file */
     no_close_handle,          /* close_handle */
     inode_destroy             /* destroy */
 };
@@ -266,6 +299,7 @@ static const struct object_ops file_lock_ops =
     no_get_fd,                  /* get_fd */
     no_map_access,              /* map_access */
     no_lookup_name,             /* lookup_name */
+    no_open_file,               /* open_file */
     no_close_handle,            /* close_handle */
     no_destroy                  /* destroy */
 };
@@ -278,7 +312,7 @@ static file_pos_t max_unix_offset = OFF_T_MAX;
 
 #define DUMP_LONG_LONG(val) do { \
     if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
-        fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
+        fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
     else \
         fprintf( stderr, "%lx", (unsigned long)(val) ); \
   } while (0)
@@ -291,22 +325,30 @@ static file_pos_t max_unix_offset = OFF_T_MAX;
 struct timeout_user
 {
     struct list           entry;      /* entry in sorted timeout list */
-    struct timeval        when;       /* timeout expiry (absolute time) */
+    timeout_t             when;       /* timeout expiry (absolute time) */
     timeout_callback      callback;   /* callback function */
     void                 *private;    /* callback private data */
 };
 
 static struct list timeout_list = LIST_INIT(timeout_list);   /* sorted timeouts list */
+timeout_t current_time;
+
+static inline void set_current_time(void)
+{
+    static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
+    struct timeval now;
+    gettimeofday( &now, NULL );
+    current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
+}
 
 /* add a timeout user */
-struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
-                                       void *private )
+struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
 {
     struct timeout_user *user;
     struct list *ptr;
 
     if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
-    user->when     = *when;
+    user->when     = (when > 0) ? when : current_time - when;
     user->callback = func;
     user->private  = private;
 
@@ -315,7 +357,7 @@ struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callb
     LIST_FOR_EACH( ptr, &timeout_list )
     {
         struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
-        if (!time_before( &timeout->when, when )) break;
+        if (timeout->when >= user->when) break;
     }
     list_add_before( ptr, &user->entry );
     return user;
@@ -328,19 +370,39 @@ void remove_timeout_user( struct timeout_user *user )
     free( user );
 }
 
-/* add a timeout in milliseconds to an absolute time */
-void add_timeout( struct timeval *when, int timeout )
+/* return a text description of a timeout for debugging purposes */
+const char *get_timeout_str( timeout_t timeout )
 {
-    if (timeout)
+    static char buffer[64];
+    long secs, nsecs;
+
+    if (!timeout) return "0";
+    if (timeout == TIMEOUT_INFINITE) return "infinite";
+
+    if (timeout < 0)  /* relative */
     {
-        long sec = timeout / 1000;
-        if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
+        secs = -timeout / TICKS_PER_SEC;
+        nsecs = -timeout % TICKS_PER_SEC;
+        sprintf( buffer, "+%ld.%07ld", secs, nsecs );
+    }
+    else  /* absolute */
+    {
+        secs = (timeout - current_time) / TICKS_PER_SEC;
+        nsecs = (timeout - current_time) % TICKS_PER_SEC;
+        if (nsecs < 0)
         {
-            when->tv_usec -= 1000000;
-            when->tv_sec++;
+            nsecs += TICKS_PER_SEC;
+            secs--;
         }
-        when->tv_sec += sec;
+        if (secs >= 0)
+            sprintf( buffer, "%x%08x (+%ld.%07ld)",
+                     (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
+        else
+            sprintf( buffer, "%x%08x (-%ld.%07ld)",
+                     (unsigned int)(timeout >> 32), (unsigned int)timeout,
+                     -(secs + 1), TICKS_PER_SEC - nsecs );
     }
+    return buffer;
 }
 
 
@@ -354,10 +416,16 @@ static int active_users;                    /* current number of active users */
 static int allocated_users;                 /* count of allocated entries in the array */
 static struct fd **freelist;                /* list of free entries in the array */
 
+static int get_next_timeout(void);
+
 #ifdef USE_EPOLL
 
-static int epoll_fd;
-static struct epoll_event *epoll_events;
+static int epoll_fd = -1;
+
+static inline void init_epoll(void)
+{
+    epoll_fd = epoll_create( 128 );
+}
 
 /* set the events that epoll waits for on this fd; helper for set_fd_events */
 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
@@ -384,6 +452,7 @@ static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
     }
 
     ev.events = events;
+    memset(&ev.data, 0, sizeof(ev.data));
     ev.data.u32 = user;
 
     if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
@@ -397,12 +466,185 @@ static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
     }
 }
 
-#else /* USE_EPOLL */
+static inline void remove_epoll_user( struct fd *fd, int user )
+{
+    if (epoll_fd == -1) return;
+
+    if (pollfd[user].fd != -1)
+    {
+        struct epoll_event dummy;
+        epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
+    }
+}
+
+static inline void main_loop_epoll(void)
+{
+    int i, ret, timeout;
+    struct epoll_event events[128];
+
+    assert( POLLIN == EPOLLIN );
+    assert( POLLOUT == EPOLLOUT );
+    assert( POLLERR == EPOLLERR );
+    assert( POLLHUP == EPOLLHUP );
+
+    if (epoll_fd == -1) return;
+
+    while (active_users)
+    {
+        timeout = get_next_timeout();
+
+        if (!active_users) break;  /* last user removed by a timeout */
+        if (epoll_fd == -1) break;  /* an error occurred with epoll */
+
+        ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
+        set_current_time();
+
+        /* put the events into the pollfd array first, like poll does */
+        for (i = 0; i < ret; i++)
+        {
+            int user = events[i].data.u32;
+            pollfd[user].revents = events[i].events;
+        }
+
+        /* read events from the pollfd array, as set_fd_events may modify them */
+        for (i = 0; i < ret; i++)
+        {
+            int user = events[i].data.u32;
+            if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
+        }
+    }
+}
+
+#elif defined(HAVE_KQUEUE)
+
+static int kqueue_fd = -1;
+
+static inline void init_epoll(void)
+{
+#ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
+    int mib[2];
+    char release[32];
+    size_t len = sizeof(release);
+
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_OSRELEASE;
+    if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
+    if (atoi(release) < 9) return;
+#endif
+    kqueue_fd = kqueue();
+}
 
 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
 {
+    struct kevent ev[2];
+
+    if (kqueue_fd == -1) return;
+
+    EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user );
+    EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user );
+
+    if (events == -1)  /* stop waiting on this fd completely */
+    {
+        if (pollfd[user].fd == -1) return;  /* already removed */
+        ev[0].flags |= EV_DELETE;
+        ev[1].flags |= EV_DELETE;
+    }
+    else if (pollfd[user].fd == -1)
+    {
+        if (pollfd[user].events) return;  /* stopped waiting on it, don't restart */
+        ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
+        ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
+    }
+    else
+    {
+        if (pollfd[user].events == events) return;  /* nothing to do */
+        ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
+        ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
+    }
+
+    if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
+    {
+        if (errno == ENOMEM)  /* not enough memory, give up on kqueue */
+        {
+            close( kqueue_fd );
+            kqueue_fd = -1;
+        }
+        else perror( "kevent" );  /* should not happen */
+    }
 }
 
+static inline void remove_epoll_user( struct fd *fd, int user )
+{
+    if (kqueue_fd == -1) return;
+
+    if (pollfd[user].fd != -1)
+    {
+        struct kevent ev[2];
+
+        EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
+        EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
+        kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
+    }
+}
+
+static inline void main_loop_epoll(void)
+{
+    int i, ret, timeout;
+    struct kevent events[128];
+
+    if (kqueue_fd == -1) return;
+
+    while (active_users)
+    {
+        timeout = get_next_timeout();
+
+        if (!active_users) break;  /* last user removed by a timeout */
+        if (kqueue_fd == -1) break;  /* an error occurred with kqueue */
+
+        if (timeout != -1)
+        {
+            struct timespec ts;
+
+            ts.tv_sec = timeout / 1000;
+            ts.tv_nsec = (timeout % 1000) * 1000000;
+            ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
+        }
+        else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
+
+        set_current_time();
+
+        /* put the events into the pollfd array first, like poll does */
+        for (i = 0; i < ret; i++)
+        {
+            long user = (long)events[i].udata;
+            pollfd[user].revents = 0;
+        }
+        for (i = 0; i < ret; i++)
+        {
+            long user = (long)events[i].udata;
+            if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
+            else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
+            if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
+            if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
+        }
+
+        /* read events from the pollfd array, as set_fd_events may modify them */
+        for (i = 0; i < ret; i++)
+        {
+            long user = (long)events[i].udata;
+            if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
+            pollfd[user].revents = 0;
+        }
+    }
+}
+
+#else /* HAVE_KQUEUE */
+
+static inline void init_epoll(void) { }
+static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
+static inline void remove_epoll_user( struct fd *fd, int user ) { }
+static inline void main_loop_epoll(void) { }
+
 #endif /* USE_EPOLL */
 
 
@@ -433,16 +675,7 @@ static int add_poll_user( struct fd *fd )
             }
             poll_users = newusers;
             pollfd = newpoll;
-#ifdef USE_EPOLL
-            if (!allocated_users) epoll_fd = epoll_create( new_count );
-            if (epoll_fd != -1)
-            {
-                struct epoll_event *new_events;
-                if (!(new_events = realloc( epoll_events, new_count * sizeof(*epoll_events) )))
-                    return -1;
-                epoll_events = new_events;
-            }
-#endif
+            if (!allocated_users) init_epoll();
             allocated_users = new_count;
         }
         ret = nb_users++;
@@ -461,13 +694,7 @@ static void remove_poll_user( struct fd *fd, int user )
     assert( user >= 0 );
     assert( poll_users[user] == fd );
 
-#ifdef USE_EPOLL
-    if (epoll_fd != -1 && pollfd[user].fd != -1)
-    {
-        struct epoll_event dummy;
-        epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
-    }
-#endif
+    remove_epoll_user( fd, user );
     pollfd[user].fd = -1;
     pollfd[user].events = 0;
     pollfd[user].revents = 0;
@@ -482,9 +709,6 @@ static int get_next_timeout(void)
     if (!list_empty( &timeout_list ))
     {
         struct list expired_list, *ptr;
-        struct timeval now;
-
-        gettimeofday( &now, NULL );
 
         /* first remove all expired timers from the list */
 
@@ -493,7 +717,7 @@ static int get_next_timeout(void)
         {
             struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
 
-            if (!time_before( &now, &timeout->when ))
+            if (timeout->when <= current_time)
             {
                 list_remove( &timeout->entry );
                 list_add_tail( &expired_list, &timeout->entry );
@@ -514,8 +738,7 @@ static int get_next_timeout(void)
         if ((ptr = list_head( &timeout_list )) != NULL)
         {
             struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
-            int diff = (timeout->when.tv_sec - now.tv_sec) * 1000
-                     + (timeout->when.tv_usec - now.tv_usec) / 1000;
+            int diff = (timeout->when - current_time + 9999) / 10000;
             if (diff < 0) diff = 0;
             return diff;
         }
@@ -528,40 +751,11 @@ void main_loop(void)
 {
     int i, ret, timeout;
 
-#ifdef USE_EPOLL
-    assert( POLLIN == EPOLLIN );
-    assert( POLLOUT == EPOLLOUT );
-    assert( POLLERR == EPOLLERR );
-    assert( POLLHUP == EPOLLHUP );
-
-    if (epoll_fd != -1)
-    {
-        while (active_users)
-        {
-            timeout = get_next_timeout();
-
-            if (!active_users) break;  /* last user removed by a timeout */
-            if (epoll_fd == -1) break;  /* an error occurred with epoll */
+    set_current_time();
+    server_start_time = current_time;
 
-            ret = epoll_wait( epoll_fd, epoll_events, allocated_users, timeout );
-
-            /* put the events into the pollfd array first, like poll does */
-            for (i = 0; i < ret; i++)
-            {
-                int user = epoll_events[i].data.u32;
-                pollfd[user].revents = epoll_events[i].events;
-            }
-
-            /* read events from the pollfd array, as set_fd_events may modify them */
-            for (i = 0; i < ret; i++)
-            {
-                int user = epoll_events[i].data.u32;
-                if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
-            }
-        }
-    }
+    main_loop_epoll();
     /* fall through to normal poll loop */
-#endif  /* USE_EPOLL */
 
     while (active_users)
     {
@@ -570,6 +764,8 @@ void main_loop(void)
         if (!active_users) break;  /* last user removed by a timeout */
 
         ret = poll( pollfd, nb_users, timeout );
+        set_current_time();
+
         if (ret > 0)
         {
             for (i = 0; i < nb_users; i++)
@@ -590,8 +786,47 @@ void main_loop(void)
 
 static struct list device_hash[DEVICE_HASH_SIZE];
 
+static int is_device_removable( dev_t dev, int unix_fd )
+{
+#if defined(linux) && defined(HAVE_FSTATFS)
+    struct statfs stfs;
+
+    /* check for floppy disk */
+    if (major(dev) == FLOPPY_MAJOR) return 1;
+
+    if (fstatfs( unix_fd, &stfs ) == -1) return 0;
+    return (stfs.f_type == 0x9660 ||    /* iso9660 */
+            stfs.f_type == 0x9fa1 ||    /* supermount */
+            stfs.f_type == 0x15013346); /* udf */
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
+    struct statfs stfs;
+
+    if (fstatfs( unix_fd, &stfs ) == -1) return 0;
+    return (!strncmp("cd9660", stfs.f_fstypename, sizeof(stfs.f_fstypename)) ||
+            !strncmp("udf", stfs.f_fstypename, sizeof(stfs.f_fstypename)));
+#elif defined(__NetBSD__)
+    struct statvfs stfs;
+
+    if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
+    return (!strncmp("cd9660", stfs.f_fstypename, sizeof(stfs.f_fstypename)) ||
+            !strncmp("udf", stfs.f_fstypename, sizeof(stfs.f_fstypename)));
+#elif defined(sun)
+# include <sys/dkio.h>
+# include <sys/vtoc.h>
+    struct dk_cinfo dkinf;
+    if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
+    return (dkinf.dki_ctype == DKC_CDROM ||
+            dkinf.dki_ctype == DKC_NCRFLOPPY ||
+            dkinf.dki_ctype == DKC_SMSFLOPPY ||
+            dkinf.dki_ctype == DKC_INTEL82072 ||
+            dkinf.dki_ctype == DKC_INTEL82077);
+#else
+    return 0;
+#endif
+}
+
 /* retrieve the device object for a given fd, creating it if needed */
-static struct device *get_device( dev_t dev, int create )
+static struct device *get_device( dev_t dev, int unix_fd )
 {
     struct device *device;
     unsigned int i, hash = dev % DEVICE_HASH_SIZE;
@@ -605,11 +840,11 @@ static struct device *get_device( dev_t dev, int create )
 
     /* not found, create it */
 
-    if (!create) return NULL;
+    if (unix_fd == -1) return NULL;
     if ((device = alloc_object( &device_ops )))
     {
         device->dev = dev;
-        device->removable = -1;
+        device->removable = is_device_removable( dev, unix_fd );
         for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
         list_add_head( &device_hash[hash], &device->entry );
     }
@@ -702,13 +937,13 @@ static void inode_destroy( struct object *obj )
 }
 
 /* retrieve the inode object for a given fd, creating it if needed */
-static struct inode *get_inode( dev_t dev, ino_t ino )
+static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
 {
     struct device *device;
     struct inode *inode;
     unsigned int hash = ino % INODE_HASH_SIZE;
 
-    if (!(device = get_device( dev, 1 ))) return NULL;
+    if (!(device = get_device( dev, unix_fd ))) return NULL;
 
     LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
     {
@@ -837,7 +1072,7 @@ static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int t
 }
 
 /* check if interval [start;end) overlaps the lock */
-inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
+static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
 {
     if (lock->end && start >= lock->end) return 0;
     if (end && lock->start >= end) return 0;
@@ -949,12 +1184,6 @@ static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start,
 {
     struct file_lock *lock;
 
-    if (!fd->inode)  /* not a regular file */
-    {
-        set_error( STATUS_INVALID_HANDLE );
-        return NULL;
-    }
-
     if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
     lock->shared  = shared;
     lock->start   = start;
@@ -1024,6 +1253,12 @@ obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int sha
     struct list *ptr;
     file_pos_t end = start + count;
 
+    if (!fd->inode)  /* not a regular file */
+    {
+        set_error( STATUS_INVALID_DEVICE_REQUEST );
+        return 0;
+    }
+
     /* don't allow wrapping locks */
     if (end && end < start)
     {
@@ -1077,85 +1312,13 @@ void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
 }
 
 
-/****************************************************************/
-/* asynchronous operations support */
-
-struct async
-{
-    struct thread       *thread;
-    void                *apc;
-    void                *user;
-    void                *sb;
-    struct timeout_user *timeout;
-    struct list          entry;
-};
-
-/* notifies client thread of new status of its async request */
-/* destroys the server side of it */
-static void async_terminate( struct async *async, int status )
-{
-    thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO,
-                      1, async->user, async->sb, (void *)status );
-
-    if (async->timeout) remove_timeout_user( async->timeout );
-    async->timeout = NULL;
-    list_remove( &async->entry );
-    release_object( async->thread );
-    free( async );
-}
-
-/* cb for timeout on an async request */
-static void async_callback(void *private)
-{
-    struct async *async = (struct async *)private;
-
-    /* fprintf(stderr, "async timeout out %p\n", async); */
-    async->timeout = NULL;
-    async_terminate( async, STATUS_TIMEOUT );
-}
-
-/* create an async on a given queue of a fd */
-struct async *create_async(struct thread *thread, int* timeout, struct list *queue,
-                           void *io_apc, void *io_user, void* io_sb)
-{
-    struct async *async = mem_alloc( sizeof(struct async) );
-
-    if (!async) return NULL;
-
-    async->thread = (struct thread *)grab_object(thread);
-    async->apc = io_apc;
-    async->user = io_user;
-    async->sb = io_sb;
-
-    list_add_tail( queue, &async->entry );
-
-    if (timeout)
-    {
-        struct timeval when;
-
-        gettimeofday( &when, NULL );
-        add_timeout( &when, *timeout );
-        async->timeout = add_timeout_user( &when, async_callback, async );
-    }
-    else async->timeout = NULL;
-
-    return async;
-}
-
-/* terminate the async operation at the head of the queue */
-void async_terminate_head( struct list *queue, int status )
-{
-    struct list *ptr = list_head( queue );
-    if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status );
-}
-
 /****************************************************************/
 /* file descriptor functions */
 
 static void fd_dump( struct object *obj, int verbose )
 {
     struct fd *fd = (struct fd *)obj;
-    fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
+    fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
     if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
     fprintf( stderr, "\n" );
 }
@@ -1164,8 +1327,9 @@ static void fd_destroy( struct object *obj )
 {
     struct fd *fd = (struct fd *)obj;
 
-    async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
-    async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
+    free_async_queue( fd->read_q );
+    free_async_queue( fd->write_q );
+    free_async_queue( fd->wait_q );
 
     remove_fd_locks( fd );
     list_remove( &fd->inode_entry );
@@ -1207,15 +1371,15 @@ static inline void unmount_fd( struct fd *fd )
 {
     assert( fd->inode );
 
-    async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
-    async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
+    async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
+    async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
 
     if (fd->poll_index != -1) set_fd_events( fd, -1 );
 
     if (fd->unix_fd != -1) close( fd->unix_fd );
 
     fd->unix_fd = -1;
-    fd->unmounted = 1;
+    fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
     fd->closed->unix_fd = -1;
     fd->closed->unlink[0] = 0;
 
@@ -1224,26 +1388,28 @@ static inline void unmount_fd( struct fd *fd )
 }
 
 /* allocate an fd object, without setting the unix fd yet */
-struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
+static struct fd *alloc_fd_object(void)
 {
     struct fd *fd = alloc_object( &fd_ops );
 
     if (!fd) return NULL;
 
-    fd->fd_ops     = fd_user_ops;
-    fd->user       = user;
+    fd->fd_ops     = NULL;
+    fd->user       = NULL;
     fd->inode      = NULL;
     fd->closed     = NULL;
     fd->access     = 0;
+    fd->options    = 0;
     fd->sharing    = 0;
     fd->unix_fd    = -1;
+    fd->signaled   = 1;
     fd->fs_locks   = 1;
-    fd->unmounted  = 0;
     fd->poll_index = -1;
+    fd->read_q     = NULL;
+    fd->write_q    = NULL;
+    fd->wait_q     = NULL;
     list_init( &fd->inode_entry );
     list_init( &fd->locks );
-    list_init( &fd->read_q );
-    list_init( &fd->write_q );
 
     if ((fd->poll_index = add_poll_user( fd )) == -1)
     {
@@ -1254,7 +1420,7 @@ struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
 }
 
 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
-struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user )
+struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
 {
     struct fd *fd = alloc_object( &fd_ops );
 
@@ -1265,25 +1431,33 @@ struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *use
     fd->inode      = NULL;
     fd->closed     = NULL;
     fd->access     = 0;
+    fd->options    = options;
     fd->sharing    = 0;
     fd->unix_fd    = -1;
+    fd->signaled   = 0;
     fd->fs_locks   = 0;
-    fd->unmounted  = 0;
     fd->poll_index = -1;
+    fd->read_q     = NULL;
+    fd->write_q    = NULL;
+    fd->wait_q     = NULL;
+    fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
     list_init( &fd->inode_entry );
     list_init( &fd->locks );
-    list_init( &fd->read_q );
-    list_init( &fd->write_q );
     return fd;
 }
 
+/* set the status to return when the fd has no associated unix fd */
+void set_no_fd_status( struct fd *fd, unsigned int status )
+{
+    fd->no_fd_status = status;
+}
+
 /* check if the desired access is possible without violating */
 /* the sharing mode of other opens of the same file */
 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
 {
     unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
     unsigned int existing_access = 0;
-    int unlink = 0;
     struct list *ptr;
 
     /* if access mode is 0, sharing mode is ignored */
@@ -1298,37 +1472,52 @@ static int check_sharing( struct fd *fd, unsigned int access, unsigned int shari
         {
             existing_sharing &= fd_ptr->sharing;
             existing_access  |= fd_ptr->access;
-            if (fd_ptr->closed->unlink[0]) unlink = 1;
         }
     }
 
     if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0;
     if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
+    if ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
     if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0;
     if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0;
-    if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
-    if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
+    if ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)) return 0;
     return 1;
 }
 
-/* open() wrapper using a struct fd */
-/* the fd must have been created with alloc_fd */
-/* on error the fd object is released */
-struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
-                    unsigned int access, unsigned int sharing, unsigned int options )
+/* sets the user of an fd that previously had no user */
+void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
+{
+    assert( fd->fd_ops == NULL );
+    fd->fd_ops = user_ops;
+    fd->user   = user;
+}
+
+/* open() wrapper that returns a struct fd with no fd user set */
+struct fd *open_fd( const char *name, int flags, mode_t *mode, unsigned int access,
+                    unsigned int sharing, unsigned int options )
 {
     struct stat st;
     struct closed_fd *closed_fd;
+    struct fd *fd;
     const char *unlink_name = "";
+    int rw_mode;
+
+    if ((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE))
+    {
+        set_error( STATUS_INVALID_PARAMETER );
+        return NULL;
+    }
 
-    assert( fd->unix_fd == -1 );
+    if (!(fd = alloc_fd_object())) return NULL;
 
+    fd->options = options;
     if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
     if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
     {
         release_object( fd );
         return NULL;
     }
+
     /* create the directory if needed */
     if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
     {
@@ -1337,20 +1526,31 @@ struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
             if (errno != EEXIST || (flags & O_EXCL))
             {
                 file_set_error();
-                release_object( fd );
-                free( closed_fd );
-                return NULL;
+                goto error;
             }
         }
         flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
     }
-    if ((fd->unix_fd = open( name, flags & ~O_TRUNC, *mode )) == -1)
+
+    if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
     {
-        file_set_error();
-        release_object( fd );
-        free( closed_fd );
-        return NULL;
+        if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
+        else rw_mode = O_WRONLY;
+    }
+    else rw_mode = O_RDONLY;
+
+    if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
+    {
+        /* if we tried to open a directory for write access, retry read-only */
+        if (errno != EISDIR ||
+            !(access & FILE_UNIX_WRITE_ACCESS) ||
+            (fd->unix_fd = open( name, O_RDONLY | (flags & ~O_TRUNC), *mode )) == -1)
+        {
+            file_set_error();
+            goto error;
+        }
     }
+
     closed_fd->unix_fd = fd->unix_fd;
     closed_fd->unlink[0] = 0;
     fstat( fd->unix_fd, &st );
@@ -1359,7 +1559,7 @@ struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
     /* only bother with an inode for normal files and directories */
     if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
     {
-        struct inode *inode = get_inode( st.st_dev, st.st_ino );
+        struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
 
         if (!inode)
         {
@@ -1418,13 +1618,16 @@ error:
 
 /* create an fd for an anonymous file */
 /* if the function fails the unix fd is closed */
-struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
+struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
+                                unsigned int options )
 {
-    struct fd *fd = alloc_fd( fd_user_ops, user );
+    struct fd *fd = alloc_fd_object();
 
     if (fd)
     {
+        set_fd_user( fd, fd_user_ops, user );
         fd->unix_fd = unix_fd;
+        fd->options = options;
         return fd;
     }
     close( unix_fd );
@@ -1437,14 +1640,16 @@ void *get_fd_user( struct fd *fd )
     return fd->user;
 }
 
+/* retrieve the opening options for the fd */
+unsigned int get_fd_options( struct fd *fd )
+{
+    return fd->options;
+}
+
 /* retrieve the unix fd for an object */
 int get_unix_fd( struct fd *fd )
 {
-    if (fd->unix_fd == -1)
-    {
-        if (fd->unmounted) set_error( STATUS_VOLUME_DISMOUNTED );
-        else set_error( STATUS_BAD_DEVICE_TYPE );
-    }
+    if (fd->unix_fd == -1) set_error( fd->no_fd_status );
     return fd->unix_fd;
 }
 
@@ -1454,6 +1659,25 @@ int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
     return fd1->inode == fd2->inode;
 }
 
+/* check if fd is on a removable device */
+int is_fd_removable( struct fd *fd )
+{
+    return (fd->inode && fd->inode->device->removable);
+}
+
+/* set or clear the fd signaled state */
+void set_fd_signaled( struct fd *fd, int signaled )
+{
+    fd->signaled = signaled;
+    if (signaled) wake_up( fd->user, 0 );
+}
+
+/* handler for close_handle that refuses to close fd-associated handles in other processes */
+int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
+{
+    return (!current || current->process == process);
+}
+
 /* callback for event happening in the main poll() loop */
 void fd_poll_event( struct fd *fd, int event )
 {
@@ -1466,6 +1690,7 @@ int check_fd_events( struct fd *fd, int events )
     struct pollfd pfd;
 
     if (fd->unix_fd == -1) return POLLERR;
+    if (fd->inode) return events;  /* regular files are always signaled */
 
     pfd.fd     = fd->unix_fd;
     pfd.events = events;
@@ -1473,48 +1698,11 @@ int check_fd_events( struct fd *fd, int events )
     return pfd.revents;
 }
 
-/* default add_queue() routine for objects that poll() on an fd */
-int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
-{
-    struct fd *fd = get_obj_fd( obj );
-
-    if (!fd) return 0;
-    if (list_empty( &obj->wait_queue ))  /* first on the queue */
-        set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
-    add_queue( obj, entry );
-    release_object( fd );
-    return 1;
-}
-
-/* default remove_queue() routine for objects that poll() on an fd */
-void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
-{
-    struct fd *fd = get_obj_fd( obj );
-
-    grab_object( obj );
-    remove_queue( obj, entry );
-    if (list_empty( &obj->wait_queue ))  /* last on the queue is gone */
-        set_fd_events( fd, 0 );
-    release_object( obj );
-    release_object( fd );
-}
-
 /* default signaled() routine for objects that poll() on an fd */
 int default_fd_signaled( struct object *obj, struct thread *thread )
 {
-    int events, ret;
     struct fd *fd = get_obj_fd( obj );
-
-    if (fd->inode) return 1;  /* regular files are always signaled */
-
-    events = fd->fd_ops->get_poll_events( fd );
-    ret = check_fd_events( fd, events ) != 0;
-
-    if (ret)
-        set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
-    else if (!list_empty( &obj->wait_queue ))
-        set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
-
+    int ret = fd->signaled;
     release_object( fd );
     return ret;
 }
@@ -1523,103 +1711,124 @@ int default_fd_get_poll_events( struct fd *fd )
 {
     int events = 0;
 
-    if (!list_empty( &fd->read_q ))
-        events |= POLLIN;
-    if (!list_empty( &fd->write_q ))
-        events |= POLLOUT;
-
+    if (async_waiting( fd->read_q )) events |= POLLIN;
+    if (async_waiting( fd->write_q )) events |= POLLOUT;
     return events;
 }
 
 /* default handler for poll() events */
 void default_poll_event( struct fd *fd, int event )
 {
-    if (!list_empty( &fd->read_q ) && (POLLIN & event) )
-    {
-        async_terminate_head( &fd->read_q, STATUS_ALERTED );
-        return;
-    }
-    if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
-    {
-        async_terminate_head( &fd->write_q, STATUS_ALERTED );
-        return;
-    }
+    if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED );
+    if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED );
 
     /* if an error occurred, stop polling this fd to avoid busy-looping */
     if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
-    wake_up( fd->user, 0 );
+    else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
 }
 
-void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count, int *timeout )
+struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
 {
-    struct list *queue;
-    int events;
+    struct async_queue *queue;
+    struct async *async;
 
-    if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
+    switch (type)
     {
-        set_error( STATUS_INVALID_HANDLE );
-        return;
+    case ASYNC_TYPE_READ:
+        if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL;
+        queue = fd->read_q;
+        break;
+    case ASYNC_TYPE_WRITE:
+        if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL;
+        queue = fd->write_q;
+        break;
+    case ASYNC_TYPE_WAIT:
+        if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL;
+        queue = fd->wait_q;
+        break;
+    default:
+        queue = NULL;
+        assert(0);
     }
 
+    if ((async = create_async( current, queue, data )) && type != ASYNC_TYPE_WAIT)
+    {
+        if (!fd->inode)
+            set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
+        else  /* regular files are always ready for read and write */
+            async_wake_up( queue, STATUS_ALERTED );
+    }
+    return async;
+}
+
+void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
+{
     switch (type)
     {
     case ASYNC_TYPE_READ:
-        queue = &fd->read_q;
+        async_wake_up( fd->read_q, status );
         break;
     case ASYNC_TYPE_WRITE:
-        queue = &fd->write_q;
+        async_wake_up( fd->write_q, status );
+        break;
+    case ASYNC_TYPE_WAIT:
+        async_wake_up( fd->wait_q, status );
         break;
     default:
-        set_error( STATUS_INVALID_PARAMETER );
-        return;
+        assert(0);
     }
-
-    if (!create_async( current, timeout, queue, apc, user, io_sb ))
-        return;
-
-    /* Check if the new pending request can be served immediately */
-    events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
-    if (events) fd->fd_ops->poll_event( fd, events );
-
-    set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
 }
 
-void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count )
+void fd_reselect_async( struct fd *fd, struct async_queue *queue )
 {
-    fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL );
+    fd->fd_ops->reselect_async( fd, queue );
 }
 
-void default_fd_cancel_async( struct fd *fd )
+void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
 {
-    async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
-    async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
+    struct async *async;
+
+    if ((async = fd_queue_async( fd, data, type, count )))
+    {
+        release_object( async );
+        set_error( STATUS_PENDING );
+    }
 }
 
-/* default flush() routine */
-int no_flush( struct fd *fd, struct event **event )
+/* default reselect_async() fd routine */
+void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
 {
-    set_error( STATUS_OBJECT_TYPE_MISMATCH );
-    return 0;
+    if (queue != fd->wait_q)
+    {
+        int poll_events = fd->fd_ops->get_poll_events( fd );
+        int events = check_fd_events( fd, poll_events );
+        if (events) fd->fd_ops->poll_event( fd, events );
+        else set_fd_events( fd, poll_events );
+    }
 }
 
-/* default get_file_info() routine */
-int no_get_file_info( struct fd *fd )
+/* default cancel_async() fd routine */
+void default_fd_cancel_async( struct fd *fd )
 {
-    set_error( STATUS_OBJECT_TYPE_MISMATCH );
-    return 0;
+    async_wake_up( fd->read_q, STATUS_CANCELLED );
+    async_wake_up( fd->write_q, STATUS_CANCELLED );
+    async_wake_up( fd->wait_q, STATUS_CANCELLED );
 }
 
-/* default queue_async() routine */
-void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb, 
-                     int type, int count)
+/* default flush() routine */
+void no_flush( struct fd *fd, struct event **event )
 {
     set_error( STATUS_OBJECT_TYPE_MISMATCH );
 }
 
-/* default cancel_async() routine */
-void no_cancel_async( struct fd *fd )
+static inline int is_valid_mounted_device( struct stat *st )
 {
-    set_error( STATUS_OBJECT_TYPE_MISMATCH );
+#if defined(linux) || defined(__sun__)
+    return S_ISBLK( st->st_mode );
+#else
+    /* disks are char devices on *BSD */
+    return S_ISCHR( st->st_mode );
+#endif
 }
 
 /* close all Unix file descriptors on a device to allow unmounting it */
@@ -1634,13 +1843,13 @@ static void unmount_device( struct fd *device_fd )
 
     if (unix_fd == -1) return;
 
-    if (fstat( unix_fd, &st ) == -1 || !S_ISBLK( st.st_mode ))
+    if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
     {
         set_error( STATUS_INVALID_PARAMETER );
         return;
     }
 
-    if (!(device = get_device( st.st_rdev, 0 ))) return;
+    if (!(device = get_device( st.st_rdev, -1 ))) return;
 
     for (i = 0; i < INODE_HASH_SIZE; i++)
     {
@@ -1659,6 +1868,21 @@ static void unmount_device( struct fd *device_fd )
     release_object( device );
 }
 
+/* default ioctl() routine */
+obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
+                               const void *data, data_size_t size )
+{
+    switch(code)
+    {
+    case FSCTL_DISMOUNT_VOLUME:
+        unmount_device( fd );
+        return 0;
+    default:
+        set_error( STATUS_NOT_SUPPORTED );
+        return 0;
+    }
+}
+
 /* same as get_handle_obj but retrieve the struct fd associated to the object */
 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
                                      unsigned int access )
@@ -1696,7 +1920,7 @@ DECL_HANDLER(open_file_object)
 {
     struct unicode_str name;
     struct directory *root = NULL;
-    struct object *obj;
+    struct object *obj, *result;
 
     get_req_unicode_str( &name );
     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
@@ -1704,12 +1928,10 @@ DECL_HANDLER(open_file_object)
 
     if ((obj = open_object_dir( root, &name, req->attributes, NULL )))
     {
-        /* make sure this is a valid file object */
-        struct fd *fd = get_obj_fd( obj );
-        if (fd)
+        if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
         {
-            reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
-            release_object( fd );
+            reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
+            release_object( result );
         }
         release_object( obj );
     }
@@ -1722,77 +1944,58 @@ DECL_HANDLER(get_handle_fd)
 {
     struct fd *fd;
 
-    reply->fd = -1;
-
-    if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
+    if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
     {
         int unix_fd = get_unix_fd( fd );
         if (unix_fd != -1)
         {
-            int cached_fd = get_handle_unix_fd( current->process, req->handle, req->access );
-            if (cached_fd != -1) reply->fd = cached_fd;
-            else if (!get_error()) send_client_fd( current->process, unix_fd, req->handle );
+            send_client_fd( current->process, unix_fd, req->handle );
+            reply->type = fd->fd_ops->get_fd_type( fd );
+            reply->removable = is_fd_removable(fd);
+            reply->options = fd->options;
+            reply->access = get_handle_access( current->process, req->handle );
         }
-        if (fd->inode) reply->removable = fd->inode->device->removable;
-        reply->flags = fd->fd_ops->get_file_info( fd );
         release_object( fd );
     }
 }
 
-/* set the cached file descriptor of a handle */
-DECL_HANDLER(set_handle_fd)
+/* perform an ioctl on a file */
+DECL_HANDLER(ioctl)
 {
-    struct fd *fd;
+    unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
+    struct fd *fd = get_handle_fd_obj( current->process, req->handle, access );
 
-    reply->cur_fd = -1;
-    if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
+    if (fd)
     {
-        struct device *device = fd->inode ? fd->inode->device : NULL;
-
-        if (device && device->removable == -1) device->removable = req->removable;
-
-        /* only cache the fd on non-removable devices */
-        if (!device || !device->removable)
-            reply->cur_fd = set_handle_unix_fd( current->process, req->handle, req->fd );
+        reply->wait = fd->fd_ops->ioctl( fd, req->code, &req->async,
+                                         get_req_data(), get_req_data_size() );
+        reply->options = fd->options;
         release_object( fd );
     }
 }
 
-/* get ready to unmount a Unix device */
-DECL_HANDLER(unmount_device)
+/* create / reschedule an async I/O */
+DECL_HANDLER(register_async)
 {
+    unsigned int access;
     struct fd *fd;
 
-    if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
+    switch(req->type)
     {
-        unmount_device( fd );
-        release_object( fd );
+    case ASYNC_TYPE_READ:
+        access = FILE_READ_DATA;
+        break;
+    case ASYNC_TYPE_WRITE:
+        access = FILE_WRITE_DATA;
+        break;
+    default:
+        set_error( STATUS_INVALID_PARAMETER );
+        return;
     }
-}
-
-/* create / reschedule an async I/O */
-DECL_HANDLER(register_async)
-{
-    struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
-
-    /*
-     * The queue_async method must do the following:
-     *
-     * 1. Get the async_queue for the request of given type.
-     * 2. Create a new asynchronous request for the selected queue
-     * 3. Carry out any operations necessary to adjust the object's poll events
-     *    Usually: set_elect_events (obj, obj->ops->get_poll_events()).
-     * 4. When the async request is triggered, then send back (with a proper APC)
-     *    the trigger (STATUS_ALERTED) to the thread that posted the request. 
-     *    async_destroy() is to be called: it will both notify the sender about
-     *    the trigger and destroy the request by itself
-     * See also the implementations in file.c, serial.c, and sock.c.
-     */
 
-    if (fd)
+    if ((fd = get_handle_fd_obj( current->process, req->handle, access )))
     {
-        fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb, 
-                                 req->type, req->count );
+        if (get_unix_fd( fd ) != -1) fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
         release_object( fd );
     }
 }
@@ -1801,15 +2004,10 @@ DECL_HANDLER(register_async)
 DECL_HANDLER(cancel_async)
 {
     struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
+
     if (fd)
     {
-        /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
-         * NtCancelIoFile() will force the pending APC to be run. Since, 
-         * Windows only guarantees that the current thread will have no async 
-         * operation on the current fd when NtCancelIoFile returns, this shall
-         * do the work.
-         */
-        fd->fd_ops->cancel_async( fd );
+        if (get_unix_fd( fd ) != -1) fd->fd_ops->cancel_async( fd );
         release_object( fd );
-    }        
+    }
 }