widl: Handle aggregate returns in a MSVC compatible way.
[wine/multimedia.git] / server / fd.c
blobe3b722cdf662f34975d80047b98e13a6b8485406
1 /*
2 * Server-side file descriptor management
4 * Copyright (C) 2000, 2003 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #endif
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40 #ifdef HAVE_LINUX_MAJOR_H
41 #include <linux/major.h>
42 #endif
43 #ifdef HAVE_SYS_STATVFS_H
44 #include <sys/statvfs.h>
45 #endif
46 #ifdef HAVE_SYS_VFS_H
48 * Solaris defines its system list in sys/list.h.
49 * This need to be workaround it here.
51 #define list SYSLIST
52 #define list_next SYSLIST_NEXT
53 #define list_prev SYSLIST_PREV
54 #define list_head SYSLIST_HEAD
55 #define list_tail SYSLIST_TAIL
56 #define list_move_tail SYSLIST_MOVE_TAIL
57 #define list_remove SYSLIST_REMOVE
58 #include <sys/vfs.h>
59 #undef list
60 #undef list_next
61 #undef list_prev
62 #undef list_head
63 #undef list_tail
64 #undef list_move_tail
65 #undef list_remove
66 #endif
67 #ifdef HAVE_SYS_PARAM_H
68 #include <sys/param.h>
69 #endif
70 #ifdef HAVE_SYS_MOUNT_H
71 #include <sys/mount.h>
72 #endif
73 #ifdef HAVE_SYS_STATFS_H
74 #include <sys/statfs.h>
75 #endif
76 #ifdef HAVE_SYS_SYSCTL_H
77 #include <sys/sysctl.h>
78 #endif
79 #ifdef HAVE_SYS_EVENT_H
80 #include <sys/event.h>
81 #undef LIST_INIT
82 #undef LIST_ENTRY
83 #endif
84 #ifdef HAVE_STDINT_H
85 #include <stdint.h>
86 #endif
87 #include <sys/stat.h>
88 #include <sys/time.h>
89 #include <sys/types.h>
90 #include <unistd.h>
91 #ifdef HAVE_SYS_SYSCALL_H
92 #include <sys/syscall.h>
93 #endif
95 #include "ntstatus.h"
96 #define WIN32_NO_STATUS
97 #include "object.h"
98 #include "file.h"
99 #include "handle.h"
100 #include "process.h"
101 #include "request.h"
103 #include "winternl.h"
104 #include "winioctl.h"
106 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
107 # include <sys/epoll.h>
108 # define USE_EPOLL
109 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
110 # define USE_EPOLL
111 # define EPOLLIN POLLIN
112 # define EPOLLOUT POLLOUT
113 # define EPOLLERR POLLERR
114 # define EPOLLHUP POLLHUP
115 # define EPOLL_CTL_ADD 1
116 # define EPOLL_CTL_DEL 2
117 # define EPOLL_CTL_MOD 3
119 typedef union epoll_data
121 void *ptr;
122 int fd;
123 uint32_t u32;
124 uint64_t u64;
125 } epoll_data_t;
127 struct epoll_event
129 uint32_t events;
130 epoll_data_t data;
133 static inline int epoll_create( int size )
135 return syscall( 254 /*NR_epoll_create*/, size );
138 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
140 return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
143 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
145 return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
148 #endif /* linux && __i386__ && HAVE_STDINT_H */
150 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
151 # include <port.h>
152 # define USE_EVENT_PORTS
153 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
155 /* Because of the stupid Posix locking semantics, we need to keep
156 * track of all file descriptors referencing a given file, and not
157 * close a single one until all the locks are gone (sigh).
160 /* file descriptor object */
162 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
163 struct closed_fd
165 struct list entry; /* entry in inode closed list */
166 int unix_fd; /* the unix file descriptor */
167 char unlink[1]; /* name to unlink on close (if any) */
170 struct fd
172 struct object obj; /* object header */
173 const struct fd_ops *fd_ops; /* file descriptor operations */
174 struct inode *inode; /* inode that this fd belongs to */
175 struct list inode_entry; /* entry in inode fd list */
176 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
177 struct object *user; /* object using this file descriptor */
178 struct list locks; /* list of locks on this fd */
179 unsigned int access; /* file access (FILE_READ_DATA etc.) */
180 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
181 unsigned int sharing; /* file sharing mode */
182 char *unix_name; /* unix file name */
183 int unix_fd; /* unix file descriptor */
184 unsigned int no_fd_status;/* status to return when unix_fd is -1 */
185 unsigned int cacheable :1;/* can the fd be cached on the client side? */
186 unsigned int signaled :1; /* is the fd signaled? */
187 unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
188 int poll_index; /* index of fd in poll array */
189 struct async_queue *read_q; /* async readers of this fd */
190 struct async_queue *write_q; /* async writers of this fd */
191 struct async_queue *wait_q; /* other async waiters of this fd */
192 struct completion *completion; /* completion object attached to this fd */
193 apc_param_t comp_key; /* completion key to set in completion events */
196 static void fd_dump( struct object *obj, int verbose );
197 static void fd_destroy( struct object *obj );
199 static const struct object_ops fd_ops =
201 sizeof(struct fd), /* size */
202 fd_dump, /* dump */
203 no_get_type, /* get_type */
204 no_add_queue, /* add_queue */
205 NULL, /* remove_queue */
206 NULL, /* signaled */
207 NULL, /* satisfied */
208 no_signal, /* signal */
209 no_get_fd, /* get_fd */
210 no_map_access, /* map_access */
211 default_get_sd, /* get_sd */
212 default_set_sd, /* set_sd */
213 no_lookup_name, /* lookup_name */
214 no_open_file, /* open_file */
215 no_close_handle, /* close_handle */
216 fd_destroy /* destroy */
219 /* device object */
221 #define DEVICE_HASH_SIZE 7
222 #define INODE_HASH_SIZE 17
224 struct device
226 struct object obj; /* object header */
227 struct list entry; /* entry in device hash list */
228 dev_t dev; /* device number */
229 int removable; /* removable device? (or -1 if unknown) */
230 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
233 static void device_dump( struct object *obj, int verbose );
234 static void device_destroy( struct object *obj );
236 static const struct object_ops device_ops =
238 sizeof(struct device), /* size */
239 device_dump, /* dump */
240 no_get_type, /* get_type */
241 no_add_queue, /* add_queue */
242 NULL, /* remove_queue */
243 NULL, /* signaled */
244 NULL, /* satisfied */
245 no_signal, /* signal */
246 no_get_fd, /* get_fd */
247 no_map_access, /* map_access */
248 default_get_sd, /* get_sd */
249 default_set_sd, /* set_sd */
250 no_lookup_name, /* lookup_name */
251 no_open_file, /* open_file */
252 no_close_handle, /* close_handle */
253 device_destroy /* destroy */
256 /* inode object */
258 struct inode
260 struct object obj; /* object header */
261 struct list entry; /* inode hash list entry */
262 struct device *device; /* device containing this inode */
263 ino_t ino; /* inode number */
264 struct list open; /* list of open file descriptors */
265 struct list locks; /* list of file locks */
266 struct list closed; /* list of file descriptors to close at destroy time */
269 static void inode_dump( struct object *obj, int verbose );
270 static void inode_destroy( struct object *obj );
272 static const struct object_ops inode_ops =
274 sizeof(struct inode), /* size */
275 inode_dump, /* dump */
276 no_get_type, /* get_type */
277 no_add_queue, /* add_queue */
278 NULL, /* remove_queue */
279 NULL, /* signaled */
280 NULL, /* satisfied */
281 no_signal, /* signal */
282 no_get_fd, /* get_fd */
283 no_map_access, /* map_access */
284 default_get_sd, /* get_sd */
285 default_set_sd, /* set_sd */
286 no_lookup_name, /* lookup_name */
287 no_open_file, /* open_file */
288 no_close_handle, /* close_handle */
289 inode_destroy /* destroy */
292 /* file lock object */
294 struct file_lock
296 struct object obj; /* object header */
297 struct fd *fd; /* fd owning this lock */
298 struct list fd_entry; /* entry in list of locks on a given fd */
299 struct list inode_entry; /* entry in inode list of locks */
300 int shared; /* shared lock? */
301 file_pos_t start; /* locked region is interval [start;end) */
302 file_pos_t end;
303 struct process *process; /* process owning this lock */
304 struct list proc_entry; /* entry in list of locks owned by the process */
307 static void file_lock_dump( struct object *obj, int verbose );
308 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
310 static const struct object_ops file_lock_ops =
312 sizeof(struct file_lock), /* size */
313 file_lock_dump, /* dump */
314 no_get_type, /* get_type */
315 add_queue, /* add_queue */
316 remove_queue, /* remove_queue */
317 file_lock_signaled, /* signaled */
318 no_satisfied, /* satisfied */
319 no_signal, /* signal */
320 no_get_fd, /* get_fd */
321 no_map_access, /* map_access */
322 default_get_sd, /* get_sd */
323 default_set_sd, /* set_sd */
324 no_lookup_name, /* lookup_name */
325 no_open_file, /* open_file */
326 no_close_handle, /* close_handle */
327 no_destroy /* destroy */
331 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
332 #define FILE_POS_T_MAX (~(file_pos_t)0)
334 static file_pos_t max_unix_offset = OFF_T_MAX;
336 #define DUMP_LONG_LONG(val) do { \
337 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
338 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
339 else \
340 fprintf( stderr, "%lx", (unsigned long)(val) ); \
341 } while (0)
345 /****************************************************************/
346 /* timeouts support */
348 struct timeout_user
350 struct list entry; /* entry in sorted timeout list */
351 timeout_t when; /* timeout expiry (absolute time) */
352 timeout_callback callback; /* callback function */
353 void *private; /* callback private data */
356 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
357 timeout_t current_time;
359 static inline void set_current_time(void)
361 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
362 struct timeval now;
363 gettimeofday( &now, NULL );
364 current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
367 /* add a timeout user */
368 struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
370 struct timeout_user *user;
371 struct list *ptr;
373 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
374 user->when = (when > 0) ? when : current_time - when;
375 user->callback = func;
376 user->private = private;
378 /* Now insert it in the linked list */
380 LIST_FOR_EACH( ptr, &timeout_list )
382 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
383 if (timeout->when >= user->when) break;
385 list_add_before( ptr, &user->entry );
386 return user;
389 /* remove a timeout user */
390 void remove_timeout_user( struct timeout_user *user )
392 list_remove( &user->entry );
393 free( user );
396 /* return a text description of a timeout for debugging purposes */
397 const char *get_timeout_str( timeout_t timeout )
399 static char buffer[64];
400 long secs, nsecs;
402 if (!timeout) return "0";
403 if (timeout == TIMEOUT_INFINITE) return "infinite";
405 if (timeout < 0) /* relative */
407 secs = -timeout / TICKS_PER_SEC;
408 nsecs = -timeout % TICKS_PER_SEC;
409 sprintf( buffer, "+%ld.%07ld", secs, nsecs );
411 else /* absolute */
413 secs = (timeout - current_time) / TICKS_PER_SEC;
414 nsecs = (timeout - current_time) % TICKS_PER_SEC;
415 if (nsecs < 0)
417 nsecs += TICKS_PER_SEC;
418 secs--;
420 if (secs >= 0)
421 sprintf( buffer, "%x%08x (+%ld.%07ld)",
422 (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
423 else
424 sprintf( buffer, "%x%08x (-%ld.%07ld)",
425 (unsigned int)(timeout >> 32), (unsigned int)timeout,
426 -(secs + 1), TICKS_PER_SEC - nsecs );
428 return buffer;
432 /****************************************************************/
433 /* poll support */
435 static struct fd **poll_users; /* users array */
436 static struct pollfd *pollfd; /* poll fd array */
437 static int nb_users; /* count of array entries actually in use */
438 static int active_users; /* current number of active users */
439 static int allocated_users; /* count of allocated entries in the array */
440 static struct fd **freelist; /* list of free entries in the array */
442 static int get_next_timeout(void);
444 static inline void fd_poll_event( struct fd *fd, int event )
446 fd->fd_ops->poll_event( fd, event );
449 #ifdef USE_EPOLL
451 static int epoll_fd = -1;
453 static inline void init_epoll(void)
455 epoll_fd = epoll_create( 128 );
458 /* set the events that epoll waits for on this fd; helper for set_fd_events */
459 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
461 struct epoll_event ev;
462 int ctl;
464 if (epoll_fd == -1) return;
466 if (events == -1) /* stop waiting on this fd completely */
468 if (pollfd[user].fd == -1) return; /* already removed */
469 ctl = EPOLL_CTL_DEL;
471 else if (pollfd[user].fd == -1)
473 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
474 ctl = EPOLL_CTL_ADD;
476 else
478 if (pollfd[user].events == events) return; /* nothing to do */
479 ctl = EPOLL_CTL_MOD;
482 ev.events = events;
483 memset(&ev.data, 0, sizeof(ev.data));
484 ev.data.u32 = user;
486 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
488 if (errno == ENOMEM) /* not enough memory, give up on epoll */
490 close( epoll_fd );
491 epoll_fd = -1;
493 else perror( "epoll_ctl" ); /* should not happen */
497 static inline void remove_epoll_user( struct fd *fd, int user )
499 if (epoll_fd == -1) return;
501 if (pollfd[user].fd != -1)
503 struct epoll_event dummy;
504 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
508 static inline void main_loop_epoll(void)
510 int i, ret, timeout;
511 struct epoll_event events[128];
513 assert( POLLIN == EPOLLIN );
514 assert( POLLOUT == EPOLLOUT );
515 assert( POLLERR == EPOLLERR );
516 assert( POLLHUP == EPOLLHUP );
518 if (epoll_fd == -1) return;
520 while (active_users)
522 timeout = get_next_timeout();
524 if (!active_users) break; /* last user removed by a timeout */
525 if (epoll_fd == -1) break; /* an error occurred with epoll */
527 ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
528 set_current_time();
530 /* put the events into the pollfd array first, like poll does */
531 for (i = 0; i < ret; i++)
533 int user = events[i].data.u32;
534 pollfd[user].revents = events[i].events;
537 /* read events from the pollfd array, as set_fd_events may modify them */
538 for (i = 0; i < ret; i++)
540 int user = events[i].data.u32;
541 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
546 #elif defined(HAVE_KQUEUE)
548 static int kqueue_fd = -1;
550 static inline void init_epoll(void)
552 #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
553 int mib[2];
554 char release[32];
555 size_t len = sizeof(release);
557 mib[0] = CTL_KERN;
558 mib[1] = KERN_OSRELEASE;
559 if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
560 if (atoi(release) < 9) return;
561 #endif
562 kqueue_fd = kqueue();
565 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
567 struct kevent ev[2];
569 if (kqueue_fd == -1) return;
571 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user );
572 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user );
574 if (events == -1) /* stop waiting on this fd completely */
576 if (pollfd[user].fd == -1) return; /* already removed */
577 ev[0].flags |= EV_DELETE;
578 ev[1].flags |= EV_DELETE;
580 else if (pollfd[user].fd == -1)
582 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
583 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
584 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
586 else
588 if (pollfd[user].events == events) return; /* nothing to do */
589 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
590 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
593 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
595 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
597 close( kqueue_fd );
598 kqueue_fd = -1;
600 else perror( "kevent" ); /* should not happen */
604 static inline void remove_epoll_user( struct fd *fd, int user )
606 if (kqueue_fd == -1) return;
608 if (pollfd[user].fd != -1)
610 struct kevent ev[2];
612 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
613 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
614 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
618 static inline void main_loop_epoll(void)
620 int i, ret, timeout;
621 struct kevent events[128];
623 if (kqueue_fd == -1) return;
625 while (active_users)
627 timeout = get_next_timeout();
629 if (!active_users) break; /* last user removed by a timeout */
630 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
632 if (timeout != -1)
634 struct timespec ts;
636 ts.tv_sec = timeout / 1000;
637 ts.tv_nsec = (timeout % 1000) * 1000000;
638 ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
640 else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
642 set_current_time();
644 /* put the events into the pollfd array first, like poll does */
645 for (i = 0; i < ret; i++)
647 long user = (long)events[i].udata;
648 pollfd[user].revents = 0;
650 for (i = 0; i < ret; i++)
652 long user = (long)events[i].udata;
653 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
654 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
655 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
656 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
659 /* read events from the pollfd array, as set_fd_events may modify them */
660 for (i = 0; i < ret; i++)
662 long user = (long)events[i].udata;
663 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
664 pollfd[user].revents = 0;
669 #elif defined(USE_EVENT_PORTS)
671 static int port_fd = -1;
673 static inline void init_epoll(void)
675 port_fd = port_create();
678 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
680 int ret;
682 if (port_fd == -1) return;
684 if (events == -1) /* stop waiting on this fd completely */
686 if (pollfd[user].fd == -1) return; /* already removed */
687 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
689 else if (pollfd[user].fd == -1)
691 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
692 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
694 else
696 if (pollfd[user].events == events) return; /* nothing to do */
697 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
700 if (ret == -1)
702 if (errno == ENOMEM) /* not enough memory, give up on port_associate */
704 close( port_fd );
705 port_fd = -1;
707 else perror( "port_associate" ); /* should not happen */
711 static inline void remove_epoll_user( struct fd *fd, int user )
713 if (port_fd == -1) return;
715 if (pollfd[user].fd != -1)
717 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
721 static inline void main_loop_epoll(void)
723 int i, nget, ret, timeout;
724 port_event_t events[128];
726 if (port_fd == -1) return;
728 while (active_users)
730 timeout = get_next_timeout();
731 nget = 1;
733 if (!active_users) break; /* last user removed by a timeout */
734 if (port_fd == -1) break; /* an error occurred with event completion */
736 if (timeout != -1)
738 struct timespec ts;
740 ts.tv_sec = timeout / 1000;
741 ts.tv_nsec = (timeout % 1000) * 1000000;
742 ret = port_getn( port_fd, events, sizeof(events)/sizeof(events[0]), &nget, &ts );
744 else ret = port_getn( port_fd, events, sizeof(events)/sizeof(events[0]), &nget, NULL );
746 if (ret == -1) break; /* an error occurred with event completion */
748 set_current_time();
750 /* put the events into the pollfd array first, like poll does */
751 for (i = 0; i < nget; i++)
753 long user = (long)events[i].portev_user;
754 pollfd[user].revents = events[i].portev_events;
757 /* read events from the pollfd array, as set_fd_events may modify them */
758 for (i = 0; i < nget; i++)
760 long user = (long)events[i].portev_user;
761 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
762 /* if we are still interested, reassociate the fd */
763 if (pollfd[user].fd != -1) {
764 port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
770 #else /* HAVE_KQUEUE */
772 static inline void init_epoll(void) { }
773 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
774 static inline void remove_epoll_user( struct fd *fd, int user ) { }
775 static inline void main_loop_epoll(void) { }
777 #endif /* USE_EPOLL */
780 /* add a user in the poll array and return its index, or -1 on failure */
781 static int add_poll_user( struct fd *fd )
783 int ret;
784 if (freelist)
786 ret = freelist - poll_users;
787 freelist = (struct fd **)poll_users[ret];
789 else
791 if (nb_users == allocated_users)
793 struct fd **newusers;
794 struct pollfd *newpoll;
795 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
796 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
797 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
799 if (allocated_users)
800 poll_users = newusers;
801 else
802 free( newusers );
803 return -1;
805 poll_users = newusers;
806 pollfd = newpoll;
807 if (!allocated_users) init_epoll();
808 allocated_users = new_count;
810 ret = nb_users++;
812 pollfd[ret].fd = -1;
813 pollfd[ret].events = 0;
814 pollfd[ret].revents = 0;
815 poll_users[ret] = fd;
816 active_users++;
817 return ret;
820 /* remove a user from the poll list */
821 static void remove_poll_user( struct fd *fd, int user )
823 assert( user >= 0 );
824 assert( poll_users[user] == fd );
826 remove_epoll_user( fd, user );
827 pollfd[user].fd = -1;
828 pollfd[user].events = 0;
829 pollfd[user].revents = 0;
830 poll_users[user] = (struct fd *)freelist;
831 freelist = &poll_users[user];
832 active_users--;
835 /* process pending timeouts and return the time until the next timeout, in milliseconds */
836 static int get_next_timeout(void)
838 if (!list_empty( &timeout_list ))
840 struct list expired_list, *ptr;
842 /* first remove all expired timers from the list */
844 list_init( &expired_list );
845 while ((ptr = list_head( &timeout_list )) != NULL)
847 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
849 if (timeout->when <= current_time)
851 list_remove( &timeout->entry );
852 list_add_tail( &expired_list, &timeout->entry );
854 else break;
857 /* now call the callback for all the removed timers */
859 while ((ptr = list_head( &expired_list )) != NULL)
861 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
862 list_remove( &timeout->entry );
863 timeout->callback( timeout->private );
864 free( timeout );
867 if ((ptr = list_head( &timeout_list )) != NULL)
869 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
870 int diff = (timeout->when - current_time + 9999) / 10000;
871 if (diff < 0) diff = 0;
872 return diff;
875 return -1; /* no pending timeouts */
878 /* server main poll() loop */
879 void main_loop(void)
881 int i, ret, timeout;
883 set_current_time();
884 server_start_time = current_time;
886 main_loop_epoll();
887 /* fall through to normal poll loop */
889 while (active_users)
891 timeout = get_next_timeout();
893 if (!active_users) break; /* last user removed by a timeout */
895 ret = poll( pollfd, nb_users, timeout );
896 set_current_time();
898 if (ret > 0)
900 for (i = 0; i < nb_users; i++)
902 if (pollfd[i].revents)
904 fd_poll_event( poll_users[i], pollfd[i].revents );
905 if (!--ret) break;
913 /****************************************************************/
914 /* device functions */
916 static struct list device_hash[DEVICE_HASH_SIZE];
918 static int is_device_removable( dev_t dev, int unix_fd )
920 #if defined(linux) && defined(HAVE_FSTATFS)
921 struct statfs stfs;
923 /* check for floppy disk */
924 if (major(dev) == FLOPPY_MAJOR) return 1;
926 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
927 return (stfs.f_type == 0x9660 || /* iso9660 */
928 stfs.f_type == 0x9fa1 || /* supermount */
929 stfs.f_type == 0x15013346); /* udf */
930 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
931 struct statfs stfs;
933 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
934 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
935 #elif defined(__NetBSD__)
936 struct statvfs stfs;
938 if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
939 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
940 #elif defined(sun)
941 # include <sys/dkio.h>
942 # include <sys/vtoc.h>
943 struct dk_cinfo dkinf;
944 if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
945 return (dkinf.dki_ctype == DKC_CDROM ||
946 dkinf.dki_ctype == DKC_NCRFLOPPY ||
947 dkinf.dki_ctype == DKC_SMSFLOPPY ||
948 dkinf.dki_ctype == DKC_INTEL82072 ||
949 dkinf.dki_ctype == DKC_INTEL82077);
950 #else
951 return 0;
952 #endif
955 /* retrieve the device object for a given fd, creating it if needed */
956 static struct device *get_device( dev_t dev, int unix_fd )
958 struct device *device;
959 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
961 if (device_hash[hash].next)
963 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
964 if (device->dev == dev) return (struct device *)grab_object( device );
966 else list_init( &device_hash[hash] );
968 /* not found, create it */
970 if (unix_fd == -1) return NULL;
971 if ((device = alloc_object( &device_ops )))
973 device->dev = dev;
974 device->removable = is_device_removable( dev, unix_fd );
975 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
976 list_add_head( &device_hash[hash], &device->entry );
978 return device;
981 static void device_dump( struct object *obj, int verbose )
983 struct device *device = (struct device *)obj;
984 fprintf( stderr, "Device dev=" );
985 DUMP_LONG_LONG( device->dev );
986 fprintf( stderr, "\n" );
989 static void device_destroy( struct object *obj )
991 struct device *device = (struct device *)obj;
992 unsigned int i;
994 for (i = 0; i < INODE_HASH_SIZE; i++)
995 assert( list_empty(&device->inode_hash[i]) );
997 list_remove( &device->entry ); /* remove it from the hash table */
1001 /****************************************************************/
1002 /* inode functions */
1004 /* close all pending file descriptors in the closed list */
1005 static void inode_close_pending( struct inode *inode, int keep_unlinks )
1007 struct list *ptr = list_head( &inode->closed );
1009 while (ptr)
1011 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1012 struct list *next = list_next( &inode->closed, ptr );
1014 if (fd->unix_fd != -1)
1016 close( fd->unix_fd );
1017 fd->unix_fd = -1;
1019 if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
1021 list_remove( ptr );
1022 free( fd );
1024 ptr = next;
1028 static void inode_dump( struct object *obj, int verbose )
1030 struct inode *inode = (struct inode *)obj;
1031 fprintf( stderr, "Inode device=%p ino=", inode->device );
1032 DUMP_LONG_LONG( inode->ino );
1033 fprintf( stderr, "\n" );
1036 static void inode_destroy( struct object *obj )
1038 struct inode *inode = (struct inode *)obj;
1039 struct list *ptr;
1041 assert( list_empty(&inode->open) );
1042 assert( list_empty(&inode->locks) );
1044 list_remove( &inode->entry );
1046 while ((ptr = list_head( &inode->closed )))
1048 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1049 list_remove( ptr );
1050 if (fd->unix_fd != -1) close( fd->unix_fd );
1051 if (fd->unlink[0])
1053 /* make sure it is still the same file */
1054 struct stat st;
1055 if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
1057 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
1058 else unlink( fd->unlink );
1061 free( fd );
1063 release_object( inode->device );
1066 /* retrieve the inode object for a given fd, creating it if needed */
1067 static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
1069 struct device *device;
1070 struct inode *inode;
1071 unsigned int hash = ino % INODE_HASH_SIZE;
1073 if (!(device = get_device( dev, unix_fd ))) return NULL;
1075 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
1077 if (inode->ino == ino)
1079 release_object( device );
1080 return (struct inode *)grab_object( inode );
1084 /* not found, create it */
1085 if ((inode = alloc_object( &inode_ops )))
1087 inode->device = device;
1088 inode->ino = ino;
1089 list_init( &inode->open );
1090 list_init( &inode->locks );
1091 list_init( &inode->closed );
1092 list_add_head( &device->inode_hash[hash], &inode->entry );
1094 else release_object( device );
1096 return inode;
1099 /* add fd to the inode list of file descriptors to close */
1100 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
1102 if (!list_empty( &inode->locks ))
1104 list_add_head( &inode->closed, &fd->entry );
1106 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
1108 if (fd->unix_fd != -1) close( fd->unix_fd );
1109 fd->unix_fd = -1;
1110 list_add_head( &inode->closed, &fd->entry );
1112 else /* no locks on this inode and no unlink, get rid of the fd */
1114 if (fd->unix_fd != -1) close( fd->unix_fd );
1115 free( fd );
1120 /****************************************************************/
1121 /* file lock functions */
1123 static void file_lock_dump( struct object *obj, int verbose )
1125 struct file_lock *lock = (struct file_lock *)obj;
1126 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
1127 lock->shared ? "shared" : "excl", lock->fd, lock->process );
1128 DUMP_LONG_LONG( lock->start );
1129 fprintf( stderr, " end=" );
1130 DUMP_LONG_LONG( lock->end );
1131 fprintf( stderr, "\n" );
1134 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry )
1136 struct file_lock *lock = (struct file_lock *)obj;
1137 /* lock is signaled if it has lost its owner */
1138 return !lock->process;
1141 /* set (or remove) a Unix lock if possible for the given range */
1142 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
1144 struct flock fl;
1146 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
1147 for (;;)
1149 if (start == end) return 1; /* can't set zero-byte lock */
1150 if (start > max_unix_offset) return 1; /* ignore it */
1151 fl.l_type = type;
1152 fl.l_whence = SEEK_SET;
1153 fl.l_start = start;
1154 if (!end || end > max_unix_offset) fl.l_len = 0;
1155 else fl.l_len = end - start;
1156 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
1158 switch(errno)
1160 case EACCES:
1161 /* check whether locks work at all on this file system */
1162 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
1164 set_error( STATUS_FILE_LOCK_CONFLICT );
1165 return 0;
1167 /* fall through */
1168 case EIO:
1169 case ENOLCK:
1170 case ENOTSUP:
1171 /* no locking on this fs, just ignore it */
1172 fd->fs_locks = 0;
1173 return 1;
1174 case EAGAIN:
1175 set_error( STATUS_FILE_LOCK_CONFLICT );
1176 return 0;
1177 case EBADF:
1178 /* this can happen if we try to set a write lock on a read-only file */
1179 /* try to at least grab a read lock */
1180 if (fl.l_type == F_WRLCK)
1182 type = F_RDLCK;
1183 break; /* retry */
1185 set_error( STATUS_ACCESS_DENIED );
1186 return 0;
1187 #ifdef EOVERFLOW
1188 case EOVERFLOW:
1189 #endif
1190 case EINVAL:
1191 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1192 /* in that case we shrink the limit and retry */
1193 if (max_unix_offset > INT_MAX)
1195 max_unix_offset = INT_MAX;
1196 break; /* retry */
1198 /* fall through */
1199 default:
1200 file_set_error();
1201 return 0;
1206 /* check if interval [start;end) overlaps the lock */
1207 static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
1209 if (lock->end && start >= lock->end) return 0;
1210 if (end && lock->start >= end) return 0;
1211 return 1;
1214 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1215 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
1217 struct hole
1219 struct hole *next;
1220 struct hole *prev;
1221 file_pos_t start;
1222 file_pos_t end;
1223 } *first, *cur, *next, *buffer;
1225 struct list *ptr;
1226 int count = 0;
1228 if (!fd->inode) return;
1229 if (!fd->fs_locks) return;
1230 if (start == end || start > max_unix_offset) return;
1231 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
1233 /* count the number of locks overlapping the specified area */
1235 LIST_FOR_EACH( ptr, &fd->inode->locks )
1237 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1238 if (lock->start == lock->end) continue;
1239 if (lock_overlaps( lock, start, end )) count++;
1242 if (!count) /* no locks at all, we can unlock everything */
1244 set_unix_lock( fd, start, end, F_UNLCK );
1245 return;
1248 /* allocate space for the list of holes */
1249 /* max. number of holes is number of locks + 1 */
1251 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1252 first = buffer;
1253 first->next = NULL;
1254 first->prev = NULL;
1255 first->start = start;
1256 first->end = end;
1257 next = first + 1;
1259 /* build a sorted list of unlocked holes in the specified area */
1261 LIST_FOR_EACH( ptr, &fd->inode->locks )
1263 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1264 if (lock->start == lock->end) continue;
1265 if (!lock_overlaps( lock, start, end )) continue;
1267 /* go through all the holes touched by this lock */
1268 for (cur = first; cur; cur = cur->next)
1270 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1271 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1273 /* now we know that lock is overlapping hole */
1275 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1277 cur->start = lock->end;
1278 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1279 /* now hole is empty, remove it */
1280 if (cur->next) cur->next->prev = cur->prev;
1281 if (cur->prev) cur->prev->next = cur->next;
1282 else if (!(first = cur->next)) goto done; /* no more holes at all */
1284 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1286 cur->end = lock->start;
1287 assert( cur->start < cur->end );
1289 else /* lock is in the middle of hole, split hole in two */
1291 next->prev = cur;
1292 next->next = cur->next;
1293 cur->next = next;
1294 next->start = lock->end;
1295 next->end = cur->end;
1296 cur->end = lock->start;
1297 assert( next->start < next->end );
1298 assert( cur->end < next->start );
1299 next++;
1300 break; /* done with this lock */
1305 /* clear Unix locks for all the holes */
1307 for (cur = first; cur; cur = cur->next)
1308 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1310 done:
1311 free( buffer );
1314 /* create a new lock on a fd */
1315 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1317 struct file_lock *lock;
1319 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1320 lock->shared = shared;
1321 lock->start = start;
1322 lock->end = end;
1323 lock->fd = fd;
1324 lock->process = current->process;
1326 /* now try to set a Unix lock */
1327 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1329 release_object( lock );
1330 return NULL;
1332 list_add_tail( &fd->locks, &lock->fd_entry );
1333 list_add_tail( &fd->inode->locks, &lock->inode_entry );
1334 list_add_tail( &lock->process->locks, &lock->proc_entry );
1335 return lock;
1338 /* remove an existing lock */
1339 static void remove_lock( struct file_lock *lock, int remove_unix )
1341 struct inode *inode = lock->fd->inode;
1343 list_remove( &lock->fd_entry );
1344 list_remove( &lock->inode_entry );
1345 list_remove( &lock->proc_entry );
1346 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1347 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1348 lock->process = NULL;
1349 wake_up( &lock->obj, 0 );
1350 release_object( lock );
1353 /* remove all locks owned by a given process */
1354 void remove_process_locks( struct process *process )
1356 struct list *ptr;
1358 while ((ptr = list_head( &process->locks )))
1360 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1361 remove_lock( lock, 1 ); /* this removes it from the list */
1365 /* remove all locks on a given fd */
1366 static void remove_fd_locks( struct fd *fd )
1368 file_pos_t start = FILE_POS_T_MAX, end = 0;
1369 struct list *ptr;
1371 while ((ptr = list_head( &fd->locks )))
1373 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1374 if (lock->start < start) start = lock->start;
1375 if (!lock->end || lock->end > end) end = lock->end - 1;
1376 remove_lock( lock, 0 );
1378 if (start < end) remove_unix_locks( fd, start, end + 1 );
1381 /* add a lock on an fd */
1382 /* returns handle to wait on */
1383 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1385 struct list *ptr;
1386 file_pos_t end = start + count;
1388 if (!fd->inode) /* not a regular file */
1390 set_error( STATUS_INVALID_DEVICE_REQUEST );
1391 return 0;
1394 /* don't allow wrapping locks */
1395 if (end && end < start)
1397 set_error( STATUS_INVALID_PARAMETER );
1398 return 0;
1401 /* check if another lock on that file overlaps the area */
1402 LIST_FOR_EACH( ptr, &fd->inode->locks )
1404 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1405 if (!lock_overlaps( lock, start, end )) continue;
1406 if (shared && (lock->shared || lock->fd == fd)) continue;
1407 /* found one */
1408 if (!wait)
1410 set_error( STATUS_FILE_LOCK_CONFLICT );
1411 return 0;
1413 set_error( STATUS_PENDING );
1414 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1417 /* not found, add it */
1418 if (add_lock( fd, shared, start, end )) return 0;
1419 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1421 /* Unix lock conflict -> tell client to wait and retry */
1422 if (wait) set_error( STATUS_PENDING );
1424 return 0;
1427 /* remove a lock on an fd */
1428 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1430 struct list *ptr;
1431 file_pos_t end = start + count;
1433 /* find an existing lock with the exact same parameters */
1434 LIST_FOR_EACH( ptr, &fd->locks )
1436 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1437 if ((lock->start == start) && (lock->end == end))
1439 remove_lock( lock, 1 );
1440 return;
1443 set_error( STATUS_FILE_LOCK_CONFLICT );
1447 /****************************************************************/
1448 /* file descriptor functions */
1450 static void fd_dump( struct object *obj, int verbose )
1452 struct fd *fd = (struct fd *)obj;
1453 fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
1454 if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1455 fprintf( stderr, "\n" );
1458 static void fd_destroy( struct object *obj )
1460 struct fd *fd = (struct fd *)obj;
1462 free_async_queue( fd->read_q );
1463 free_async_queue( fd->write_q );
1464 free_async_queue( fd->wait_q );
1466 if (fd->completion) release_object( fd->completion );
1467 remove_fd_locks( fd );
1468 free( fd->unix_name );
1469 list_remove( &fd->inode_entry );
1470 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1471 if (fd->inode)
1473 inode_add_closed_fd( fd->inode, fd->closed );
1474 release_object( fd->inode );
1476 else /* no inode, close it right away */
1478 if (fd->unix_fd != -1) close( fd->unix_fd );
1482 /* check if the desired access is possible without violating */
1483 /* the sharing mode of other opens of the same file */
1484 static unsigned int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing,
1485 unsigned int open_flags, unsigned int options )
1487 /* only a few access bits are meaningful wrt sharing */
1488 const unsigned int read_access = FILE_READ_DATA | FILE_EXECUTE;
1489 const unsigned int write_access = FILE_WRITE_DATA | FILE_APPEND_DATA;
1490 const unsigned int all_access = read_access | write_access | DELETE;
1492 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1493 unsigned int existing_access = 0;
1494 struct list *ptr;
1496 fd->access = access;
1497 fd->sharing = sharing;
1499 LIST_FOR_EACH( ptr, &fd->inode->open )
1501 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1502 if (fd_ptr != fd)
1504 /* if access mode is 0, sharing mode is ignored */
1505 if (fd_ptr->access & all_access) existing_sharing &= fd_ptr->sharing;
1506 existing_access |= fd_ptr->access;
1510 if (((access & read_access) && !(existing_sharing & FILE_SHARE_READ)) ||
1511 ((access & write_access) && !(existing_sharing & FILE_SHARE_WRITE)) ||
1512 ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)))
1513 return STATUS_SHARING_VIOLATION;
1514 if (((existing_access & FILE_MAPPING_WRITE) && !(sharing & FILE_SHARE_WRITE)) ||
1515 ((existing_access & FILE_MAPPING_IMAGE) && (access & FILE_WRITE_DATA)))
1516 return STATUS_SHARING_VIOLATION;
1517 if ((existing_access & FILE_MAPPING_IMAGE) && (options & FILE_DELETE_ON_CLOSE))
1518 return STATUS_CANNOT_DELETE;
1519 if ((existing_access & FILE_MAPPING_ACCESS) && (open_flags & O_TRUNC))
1520 return STATUS_USER_MAPPED_FILE;
1521 if (!(access & all_access))
1522 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1523 if (((existing_access & read_access) && !(sharing & FILE_SHARE_READ)) ||
1524 ((existing_access & write_access) && !(sharing & FILE_SHARE_WRITE)) ||
1525 ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)))
1526 return STATUS_SHARING_VIOLATION;
1527 return 0;
1530 /* set the events that select waits for on this fd */
1531 void set_fd_events( struct fd *fd, int events )
1533 int user = fd->poll_index;
1534 assert( poll_users[user] == fd );
1536 set_fd_epoll_events( fd, user, events );
1538 if (events == -1) /* stop waiting on this fd completely */
1540 pollfd[user].fd = -1;
1541 pollfd[user].events = POLLERR;
1542 pollfd[user].revents = 0;
1544 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1546 pollfd[user].fd = fd->unix_fd;
1547 pollfd[user].events = events;
1551 /* prepare an fd for unmounting its corresponding device */
1552 static inline void unmount_fd( struct fd *fd )
1554 assert( fd->inode );
1556 async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
1557 async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
1559 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1561 if (fd->unix_fd != -1) close( fd->unix_fd );
1563 fd->unix_fd = -1;
1564 fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
1565 fd->closed->unix_fd = -1;
1566 fd->closed->unlink[0] = 0;
1568 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1569 fd->fs_locks = 0;
1572 /* allocate an fd object, without setting the unix fd yet */
1573 static struct fd *alloc_fd_object(void)
1575 struct fd *fd = alloc_object( &fd_ops );
1577 if (!fd) return NULL;
1579 fd->fd_ops = NULL;
1580 fd->user = NULL;
1581 fd->inode = NULL;
1582 fd->closed = NULL;
1583 fd->access = 0;
1584 fd->options = 0;
1585 fd->sharing = 0;
1586 fd->unix_fd = -1;
1587 fd->unix_name = NULL;
1588 fd->cacheable = 0;
1589 fd->signaled = 1;
1590 fd->fs_locks = 1;
1591 fd->poll_index = -1;
1592 fd->read_q = NULL;
1593 fd->write_q = NULL;
1594 fd->wait_q = NULL;
1595 fd->completion = NULL;
1596 list_init( &fd->inode_entry );
1597 list_init( &fd->locks );
1599 if ((fd->poll_index = add_poll_user( fd )) == -1)
1601 release_object( fd );
1602 return NULL;
1604 return fd;
1607 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1608 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
1610 struct fd *fd = alloc_object( &fd_ops );
1612 if (!fd) return NULL;
1614 fd->fd_ops = fd_user_ops;
1615 fd->user = user;
1616 fd->inode = NULL;
1617 fd->closed = NULL;
1618 fd->access = 0;
1619 fd->options = options;
1620 fd->sharing = 0;
1621 fd->unix_name = NULL;
1622 fd->unix_fd = -1;
1623 fd->cacheable = 0;
1624 fd->signaled = 0;
1625 fd->fs_locks = 0;
1626 fd->poll_index = -1;
1627 fd->read_q = NULL;
1628 fd->write_q = NULL;
1629 fd->wait_q = NULL;
1630 fd->completion = NULL;
1631 fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
1632 list_init( &fd->inode_entry );
1633 list_init( &fd->locks );
1634 return fd;
1637 /* duplicate an fd object for a different user */
1638 struct fd *dup_fd_object( struct fd *orig, unsigned int access, unsigned int sharing, unsigned int options )
1640 unsigned int err;
1641 struct fd *fd = alloc_fd_object();
1643 if (!fd) return NULL;
1645 fd->options = options;
1646 fd->cacheable = orig->cacheable;
1648 if (orig->unix_name)
1650 if (!(fd->unix_name = mem_alloc( strlen(orig->unix_name) + 1 ))) goto failed;
1651 strcpy( fd->unix_name, orig->unix_name );
1654 if (orig->inode)
1656 struct closed_fd *closed = mem_alloc( sizeof(*closed) );
1657 if (!closed) goto failed;
1658 if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1660 file_set_error();
1661 free( closed );
1662 goto failed;
1664 closed->unix_fd = fd->unix_fd;
1665 closed->unlink[0] = 0;
1666 fd->closed = closed;
1667 fd->inode = (struct inode *)grab_object( orig->inode );
1668 list_add_head( &fd->inode->open, &fd->inode_entry );
1669 if ((err = check_sharing( fd, access, sharing, 0, options )))
1671 set_error( err );
1672 goto failed;
1675 else if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1677 file_set_error();
1678 goto failed;
1680 return fd;
1682 failed:
1683 release_object( fd );
1684 return NULL;
1687 /* find an existing fd object that can be reused for a mapping */
1688 struct fd *get_fd_object_for_mapping( struct fd *fd, unsigned int access, unsigned int sharing )
1690 struct fd *fd_ptr;
1692 if (!fd->inode) return NULL;
1694 LIST_FOR_EACH_ENTRY( fd_ptr, &fd->inode->open, struct fd, inode_entry )
1695 if (fd_ptr->access == access && fd_ptr->sharing == sharing)
1696 return (struct fd *)grab_object( fd_ptr );
1698 return NULL;
1701 /* set the status to return when the fd has no associated unix fd */
1702 void set_no_fd_status( struct fd *fd, unsigned int status )
1704 fd->no_fd_status = status;
1707 /* sets the user of an fd that previously had no user */
1708 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1710 assert( fd->fd_ops == NULL );
1711 fd->fd_ops = user_ops;
1712 fd->user = user;
1715 static char *dup_fd_name( struct fd *root, const char *name )
1717 char *ret;
1719 if (!root) return strdup( name );
1720 if (!root->unix_name) return NULL;
1722 /* skip . prefix */
1723 if (name[0] == '.' && (!name[1] || name[1] == '/')) name++;
1725 if ((ret = malloc( strlen(root->unix_name) + strlen(name) + 2 )))
1727 strcpy( ret, root->unix_name );
1728 if (name[0] && name[0] != '/') strcat( ret, "/" );
1729 strcat( ret, name );
1731 return ret;
1734 /* open() wrapper that returns a struct fd with no fd user set */
1735 struct fd *open_fd( struct fd *root, const char *name, int flags, mode_t *mode, unsigned int access,
1736 unsigned int sharing, unsigned int options )
1738 struct stat st;
1739 struct closed_fd *closed_fd;
1740 struct fd *fd;
1741 const char *unlink_name = "";
1742 int root_fd = -1;
1743 int rw_mode;
1745 if (((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) ||
1746 ((options & FILE_DIRECTORY_FILE) && (flags & O_TRUNC)))
1748 set_error( STATUS_INVALID_PARAMETER );
1749 return NULL;
1752 if (!(fd = alloc_fd_object())) return NULL;
1754 fd->options = options;
1755 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
1756 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
1758 release_object( fd );
1759 return NULL;
1762 if (root)
1764 if ((root_fd = get_unix_fd( root )) == -1) goto error;
1765 if (fchdir( root_fd ) == -1)
1767 file_set_error();
1768 root_fd = -1;
1769 goto error;
1773 /* create the directory if needed */
1774 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1776 if (mkdir( name, *mode ) == -1)
1778 if (errno != EEXIST || (flags & O_EXCL))
1780 file_set_error();
1781 goto error;
1784 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1787 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1789 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1790 else rw_mode = O_WRONLY;
1792 else rw_mode = O_RDONLY;
1794 fd->unix_name = dup_fd_name( root, name );
1796 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1798 /* if we tried to open a directory for write access, retry read-only */
1799 if (errno == EISDIR)
1801 if ((access & FILE_UNIX_WRITE_ACCESS) || (flags & O_CREAT))
1802 fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
1805 if (fd->unix_fd == -1)
1807 file_set_error();
1808 goto error;
1812 closed_fd->unix_fd = fd->unix_fd;
1813 closed_fd->unlink[0] = 0;
1814 fstat( fd->unix_fd, &st );
1815 *mode = st.st_mode;
1817 /* only bother with an inode for normal files and directories */
1818 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1820 unsigned int err;
1821 struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
1823 if (!inode)
1825 /* we can close the fd because there are no others open on the same file,
1826 * otherwise we wouldn't have failed to allocate a new inode
1828 goto error;
1830 fd->inode = inode;
1831 fd->closed = closed_fd;
1832 fd->cacheable = !inode->device->removable;
1833 list_add_head( &inode->open, &fd->inode_entry );
1835 /* check directory options */
1836 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1838 release_object( fd );
1839 set_error( STATUS_NOT_A_DIRECTORY );
1840 return NULL;
1842 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1844 release_object( fd );
1845 set_error( STATUS_FILE_IS_A_DIRECTORY );
1846 return NULL;
1848 if ((err = check_sharing( fd, access, sharing, flags, options )))
1850 release_object( fd );
1851 set_error( err );
1852 return NULL;
1854 strcpy( closed_fd->unlink, unlink_name );
1855 if (flags & O_TRUNC)
1857 if (S_ISDIR(st.st_mode))
1859 release_object( fd );
1860 set_error( STATUS_OBJECT_NAME_COLLISION );
1861 return NULL;
1863 ftruncate( fd->unix_fd, 0 );
1866 else /* special file */
1868 if (options & FILE_DIRECTORY_FILE)
1870 set_error( STATUS_NOT_A_DIRECTORY );
1871 goto error;
1873 if (unlink_name[0]) /* we can't unlink special files */
1875 set_error( STATUS_INVALID_PARAMETER );
1876 goto error;
1878 free( closed_fd );
1879 fd->cacheable = 1;
1881 return fd;
1883 error:
1884 release_object( fd );
1885 free( closed_fd );
1886 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1887 return NULL;
1890 /* create an fd for an anonymous file */
1891 /* if the function fails the unix fd is closed */
1892 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
1893 unsigned int options )
1895 struct fd *fd = alloc_fd_object();
1897 if (fd)
1899 set_fd_user( fd, fd_user_ops, user );
1900 fd->unix_fd = unix_fd;
1901 fd->options = options;
1902 return fd;
1904 close( unix_fd );
1905 return NULL;
1908 /* retrieve the object that is using an fd */
1909 void *get_fd_user( struct fd *fd )
1911 return fd->user;
1914 /* retrieve the opening options for the fd */
1915 unsigned int get_fd_options( struct fd *fd )
1917 return fd->options;
1920 /* retrieve the unix fd for an object */
1921 int get_unix_fd( struct fd *fd )
1923 if (fd->unix_fd == -1) set_error( fd->no_fd_status );
1924 return fd->unix_fd;
1927 /* check if two file descriptors point to the same file */
1928 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1930 return fd1->inode == fd2->inode;
1933 /* allow the fd to be cached (can't be reset once set) */
1934 void allow_fd_caching( struct fd *fd )
1936 fd->cacheable = 1;
1939 /* check if fd is on a removable device */
1940 int is_fd_removable( struct fd *fd )
1942 return (fd->inode && fd->inode->device->removable);
1945 /* set or clear the fd signaled state */
1946 void set_fd_signaled( struct fd *fd, int signaled )
1948 fd->signaled = signaled;
1949 if (signaled) wake_up( fd->user, 0 );
1952 /* set or clear the fd signaled state */
1953 int is_fd_signaled( struct fd *fd )
1955 return fd->signaled;
1958 /* handler for close_handle that refuses to close fd-associated handles in other processes */
1959 int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1961 return (!current || current->process == process);
1964 /* check if events are pending and if yes return which one(s) */
1965 int check_fd_events( struct fd *fd, int events )
1967 struct pollfd pfd;
1969 if (fd->unix_fd == -1) return POLLERR;
1970 if (fd->inode) return events; /* regular files are always signaled */
1972 pfd.fd = fd->unix_fd;
1973 pfd.events = events;
1974 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1975 return pfd.revents;
1978 /* default signaled() routine for objects that poll() on an fd */
1979 int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
1981 struct fd *fd = get_obj_fd( obj );
1982 int ret = fd->signaled;
1983 release_object( fd );
1984 return ret;
1987 /* default map_access() routine for objects that behave like an fd */
1988 unsigned int default_fd_map_access( struct object *obj, unsigned int access )
1990 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
1991 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
1992 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
1993 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
1994 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
1997 int default_fd_get_poll_events( struct fd *fd )
1999 int events = 0;
2001 if (async_waiting( fd->read_q )) events |= POLLIN;
2002 if (async_waiting( fd->write_q )) events |= POLLOUT;
2003 return events;
2006 /* default handler for poll() events */
2007 void default_poll_event( struct fd *fd, int event )
2009 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED );
2010 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED );
2012 /* if an error occurred, stop polling this fd to avoid busy-looping */
2013 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
2014 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2017 struct async *fd_queue_async( struct fd *fd, const async_data_t *data, int type )
2019 struct async_queue *queue;
2020 struct async *async;
2022 switch (type)
2024 case ASYNC_TYPE_READ:
2025 if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return NULL;
2026 queue = fd->read_q;
2027 break;
2028 case ASYNC_TYPE_WRITE:
2029 if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return NULL;
2030 queue = fd->write_q;
2031 break;
2032 case ASYNC_TYPE_WAIT:
2033 if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return NULL;
2034 queue = fd->wait_q;
2035 break;
2036 default:
2037 queue = NULL;
2038 assert(0);
2041 if ((async = create_async( current, queue, data )) && type != ASYNC_TYPE_WAIT)
2043 if (!fd->inode)
2044 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2045 else /* regular files are always ready for read and write */
2046 async_wake_up( queue, STATUS_ALERTED );
2048 return async;
2051 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
2053 switch (type)
2055 case ASYNC_TYPE_READ:
2056 async_wake_up( fd->read_q, status );
2057 break;
2058 case ASYNC_TYPE_WRITE:
2059 async_wake_up( fd->write_q, status );
2060 break;
2061 case ASYNC_TYPE_WAIT:
2062 async_wake_up( fd->wait_q, status );
2063 break;
2064 default:
2065 assert(0);
2069 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
2071 fd->fd_ops->reselect_async( fd, queue );
2074 void no_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
2076 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2079 void default_fd_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
2081 struct async *async;
2083 if ((async = fd_queue_async( fd, data, type )))
2085 release_object( async );
2086 set_error( STATUS_PENDING );
2090 /* default reselect_async() fd routine */
2091 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
2093 if (queue != fd->wait_q)
2095 int poll_events = fd->fd_ops->get_poll_events( fd );
2096 int events = check_fd_events( fd, poll_events );
2097 if (events) fd->fd_ops->poll_event( fd, events );
2098 else set_fd_events( fd, poll_events );
2102 /* default cancel_async() fd routine */
2103 void default_fd_cancel_async( struct fd *fd, struct process *process, struct thread *thread, client_ptr_t iosb )
2105 int n = 0;
2107 n += async_wake_up_by( fd->read_q, process, thread, iosb, STATUS_CANCELLED );
2108 n += async_wake_up_by( fd->write_q, process, thread, iosb, STATUS_CANCELLED );
2109 n += async_wake_up_by( fd->wait_q, process, thread, iosb, STATUS_CANCELLED );
2110 if (!n && iosb)
2111 set_error( STATUS_NOT_FOUND );
2114 /* default flush() routine */
2115 void no_flush( struct fd *fd, struct event **event )
2117 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2120 static inline int is_valid_mounted_device( struct stat *st )
2122 #if defined(linux) || defined(__sun__)
2123 return S_ISBLK( st->st_mode );
2124 #else
2125 /* disks are char devices on *BSD */
2126 return S_ISCHR( st->st_mode );
2127 #endif
2130 /* close all Unix file descriptors on a device to allow unmounting it */
2131 static void unmount_device( struct fd *device_fd )
2133 unsigned int i;
2134 struct stat st;
2135 struct device *device;
2136 struct inode *inode;
2137 struct fd *fd;
2138 int unix_fd = get_unix_fd( device_fd );
2140 if (unix_fd == -1) return;
2142 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2144 set_error( STATUS_INVALID_PARAMETER );
2145 return;
2148 if (!(device = get_device( st.st_rdev, -1 ))) return;
2150 for (i = 0; i < INODE_HASH_SIZE; i++)
2152 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
2154 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
2156 unmount_fd( fd );
2158 inode_close_pending( inode, 0 );
2161 /* remove it from the hash table */
2162 list_remove( &device->entry );
2163 list_init( &device->entry );
2164 release_object( device );
2167 obj_handle_t no_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
2168 int blocking, const void *data, data_size_t size )
2170 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2171 return 0;
2174 /* default ioctl() routine */
2175 obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
2176 int blocking, const void *data, data_size_t size )
2178 switch(code)
2180 case FSCTL_DISMOUNT_VOLUME:
2181 unmount_device( fd );
2182 return 0;
2183 default:
2184 set_error( STATUS_NOT_SUPPORTED );
2185 return 0;
2189 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2190 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
2191 unsigned int access )
2193 struct fd *fd = NULL;
2194 struct object *obj;
2196 if ((obj = get_handle_obj( process, handle, access, NULL )))
2198 fd = get_obj_fd( obj );
2199 release_object( obj );
2201 return fd;
2204 struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
2206 *p_key = fd->comp_key;
2207 return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
2210 void fd_copy_completion( struct fd *src, struct fd *dst )
2212 assert( !dst->completion );
2213 dst->completion = fd_get_completion( src, &dst->comp_key );
2216 /* flush a file buffers */
2217 DECL_HANDLER(flush_file)
2219 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2220 struct event * event = NULL;
2222 if (fd)
2224 fd->fd_ops->flush( fd, &event );
2225 if ( event )
2227 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
2229 release_object( fd );
2233 /* open a file object */
2234 DECL_HANDLER(open_file_object)
2236 struct unicode_str name;
2237 struct directory *root = NULL;
2238 struct object *obj, *result;
2240 get_req_unicode_str( &name );
2241 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
2242 return;
2244 if ((obj = open_object_dir( root, &name, req->attributes, NULL )))
2246 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
2248 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
2249 release_object( result );
2251 release_object( obj );
2254 if (root) release_object( root );
2257 /* get the Unix name from a file handle */
2258 DECL_HANDLER(get_handle_unix_name)
2260 struct fd *fd;
2262 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2264 if (fd->unix_name)
2266 data_size_t name_len = strlen( fd->unix_name );
2267 reply->name_len = name_len;
2268 if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
2269 else set_error( STATUS_BUFFER_OVERFLOW );
2271 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
2272 release_object( fd );
2276 /* get a Unix fd to access a file */
2277 DECL_HANDLER(get_handle_fd)
2279 struct fd *fd;
2281 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2283 int unix_fd = get_unix_fd( fd );
2284 if (unix_fd != -1)
2286 reply->type = fd->fd_ops->get_fd_type( fd );
2287 reply->cacheable = fd->cacheable;
2288 reply->options = fd->options;
2289 reply->access = get_handle_access( current->process, req->handle );
2290 send_client_fd( current->process, unix_fd, req->handle );
2292 release_object( fd );
2296 /* perform an ioctl on a file */
2297 DECL_HANDLER(ioctl)
2299 unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
2300 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
2302 if (fd)
2304 reply->wait = fd->fd_ops->ioctl( fd, req->code, &req->async, req->blocking,
2305 get_req_data(), get_req_data_size() );
2306 reply->options = fd->options;
2307 release_object( fd );
2311 /* create / reschedule an async I/O */
2312 DECL_HANDLER(register_async)
2314 unsigned int access;
2315 struct fd *fd;
2317 switch(req->type)
2319 case ASYNC_TYPE_READ:
2320 access = FILE_READ_DATA;
2321 break;
2322 case ASYNC_TYPE_WRITE:
2323 access = FILE_WRITE_DATA;
2324 break;
2325 default:
2326 set_error( STATUS_INVALID_PARAMETER );
2327 return;
2330 if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
2332 if (get_unix_fd( fd ) != -1) fd->fd_ops->queue_async( fd, &req->async, req->type, req->count );
2333 release_object( fd );
2337 /* cancels all async I/O */
2338 DECL_HANDLER(cancel_async)
2340 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2341 struct thread *thread = req->only_thread ? current : NULL;
2343 if (fd)
2345 if (get_unix_fd( fd ) != -1) fd->fd_ops->cancel_async( fd, current->process, thread, req->iosb );
2346 release_object( fd );
2350 /* attach completion object to a fd */
2351 DECL_HANDLER(set_completion_info)
2353 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2355 if (fd)
2357 if (!(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) && !fd->completion)
2359 fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
2360 fd->comp_key = req->ckey;
2362 else set_error( STATUS_INVALID_PARAMETER );
2363 release_object( fd );
2367 /* push new completion msg into a completion queue attached to the fd */
2368 DECL_HANDLER(add_fd_completion)
2370 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2371 if (fd)
2373 if (fd->completion)
2374 add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
2375 release_object( fd );