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
23 #include "wine/port.h"
37 #ifdef HAVE_SYS_POLL_H
40 #ifdef HAVE_SYS_EVENT_H
41 #include <sys/event.h>
50 #include <sys/types.h>
54 #define WIN32_NO_STATUS
63 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
64 # include <sys/epoll.h>
66 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
68 # define EPOLLIN POLLIN
69 # define EPOLLOUT POLLOUT
70 # define EPOLLERR POLLERR
71 # define EPOLLHUP POLLHUP
72 # define EPOLL_CTL_ADD 1
73 # define EPOLL_CTL_DEL 2
74 # define EPOLL_CTL_MOD 3
76 typedef union epoll_data
90 #define SYSCALL_RET(ret) do { \
91 if (ret < 0) { errno = -ret; ret = -1; } \
95 static inline int epoll_create( int size )
98 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
99 : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
103 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
106 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
108 : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
112 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
115 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
117 : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
123 #endif /* linux && __i386__ && HAVE_STDINT_H */
126 /* Because of the stupid Posix locking semantics, we need to keep
127 * track of all file descriptors referencing a given file, and not
128 * close a single one until all the locks are gone (sigh).
131 /* file descriptor object */
133 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
136 struct list entry; /* entry in inode closed list */
137 int unix_fd; /* the unix file descriptor */
138 char unlink[1]; /* name to unlink on close (if any) */
143 struct object obj; /* object header */
144 const struct fd_ops *fd_ops; /* file descriptor operations */
145 struct inode *inode; /* inode that this fd belongs to */
146 struct list inode_entry; /* entry in inode fd list */
147 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
148 struct object *user; /* object using this file descriptor */
149 struct list locks; /* list of locks on this fd */
150 unsigned int access; /* file access (FILE_READ_DATA etc.) */
151 unsigned int sharing; /* file sharing mode */
152 int unix_fd; /* unix file descriptor */
153 int fs_locks :1; /* can we use filesystem locks for this fd? */
154 int unmounted :1;/* has the device been unmounted? */
155 int poll_index; /* index of fd in poll array */
156 struct list read_q; /* async readers of this fd */
157 struct list write_q; /* async writers of this fd */
160 static void fd_dump( struct object *obj, int verbose );
161 static void fd_destroy( struct object *obj );
163 static const struct object_ops fd_ops =
165 sizeof(struct fd), /* size */
167 no_add_queue, /* add_queue */
168 NULL, /* remove_queue */
170 NULL, /* satisfied */
171 no_signal, /* signal */
172 no_get_fd, /* get_fd */
173 no_map_access, /* map_access */
174 no_lookup_name, /* lookup_name */
175 no_close_handle, /* close_handle */
176 fd_destroy /* destroy */
181 #define DEVICE_HASH_SIZE 7
182 #define INODE_HASH_SIZE 17
186 struct object obj; /* object header */
187 struct list entry; /* entry in device hash list */
188 dev_t dev; /* device number */
189 int removable; /* removable device? (or -1 if unknown) */
190 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
193 static void device_dump( struct object *obj, int verbose );
194 static void device_destroy( struct object *obj );
196 static const struct object_ops device_ops =
198 sizeof(struct device), /* size */
199 device_dump, /* dump */
200 no_add_queue, /* add_queue */
201 NULL, /* remove_queue */
203 NULL, /* satisfied */
204 no_signal, /* signal */
205 no_get_fd, /* get_fd */
206 no_map_access, /* map_access */
207 no_lookup_name, /* lookup_name */
208 no_close_handle, /* close_handle */
209 device_destroy /* destroy */
216 struct object obj; /* object header */
217 struct list entry; /* inode hash list entry */
218 struct device *device; /* device containing this inode */
219 ino_t ino; /* inode number */
220 struct list open; /* list of open file descriptors */
221 struct list locks; /* list of file locks */
222 struct list closed; /* list of file descriptors to close at destroy time */
225 static void inode_dump( struct object *obj, int verbose );
226 static void inode_destroy( struct object *obj );
228 static const struct object_ops inode_ops =
230 sizeof(struct inode), /* size */
231 inode_dump, /* dump */
232 no_add_queue, /* add_queue */
233 NULL, /* remove_queue */
235 NULL, /* satisfied */
236 no_signal, /* signal */
237 no_get_fd, /* get_fd */
238 no_map_access, /* map_access */
239 no_lookup_name, /* lookup_name */
240 no_close_handle, /* close_handle */
241 inode_destroy /* destroy */
244 /* file lock object */
248 struct object obj; /* object header */
249 struct fd *fd; /* fd owning this lock */
250 struct list fd_entry; /* entry in list of locks on a given fd */
251 struct list inode_entry; /* entry in inode list of locks */
252 int shared; /* shared lock? */
253 file_pos_t start; /* locked region is interval [start;end) */
255 struct process *process; /* process owning this lock */
256 struct list proc_entry; /* entry in list of locks owned by the process */
259 static void file_lock_dump( struct object *obj, int verbose );
260 static int file_lock_signaled( struct object *obj, struct thread *thread );
262 static const struct object_ops file_lock_ops =
264 sizeof(struct file_lock), /* size */
265 file_lock_dump, /* dump */
266 add_queue, /* add_queue */
267 remove_queue, /* remove_queue */
268 file_lock_signaled, /* signaled */
269 no_satisfied, /* satisfied */
270 no_signal, /* signal */
271 no_get_fd, /* get_fd */
272 no_map_access, /* map_access */
273 no_lookup_name, /* lookup_name */
274 no_close_handle, /* close_handle */
275 no_destroy /* destroy */
279 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
280 #define FILE_POS_T_MAX (~(file_pos_t)0)
282 static file_pos_t max_unix_offset = OFF_T_MAX;
284 #define DUMP_LONG_LONG(val) do { \
285 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
286 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
288 fprintf( stderr, "%lx", (unsigned long)(val) ); \
293 /****************************************************************/
294 /* timeouts support */
298 struct list entry; /* entry in sorted timeout list */
299 struct timeval when; /* timeout expiry (absolute time) */
300 timeout_callback callback; /* callback function */
301 void *private; /* callback private data */
304 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
305 struct timeval current_time;
307 /* add a timeout user */
308 struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
311 struct timeout_user *user;
314 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
316 user->callback = func;
317 user->private = private;
319 /* Now insert it in the linked list */
321 LIST_FOR_EACH( ptr, &timeout_list )
323 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
324 if (!time_before( &timeout->when, when )) break;
326 list_add_before( ptr, &user->entry );
330 /* remove a timeout user */
331 void remove_timeout_user( struct timeout_user *user )
333 list_remove( &user->entry );
337 /* add a timeout in milliseconds to an absolute time */
338 void add_timeout( struct timeval *when, int timeout )
342 long sec = timeout / 1000;
343 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
345 when->tv_usec -= 1000000;
353 /****************************************************************/
356 static struct fd **poll_users; /* users array */
357 static struct pollfd *pollfd; /* poll fd array */
358 static int nb_users; /* count of array entries actually in use */
359 static int active_users; /* current number of active users */
360 static int allocated_users; /* count of allocated entries in the array */
361 static struct fd **freelist; /* list of free entries in the array */
363 static int get_next_timeout(void);
367 static int epoll_fd = -1;
369 static inline void init_epoll(void)
371 epoll_fd = epoll_create( 128 );
374 /* set the events that epoll waits for on this fd; helper for set_fd_events */
375 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
377 struct epoll_event ev;
380 if (epoll_fd == -1) return;
382 if (events == -1) /* stop waiting on this fd completely */
384 if (pollfd[user].fd == -1) return; /* already removed */
387 else if (pollfd[user].fd == -1)
389 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
394 if (pollfd[user].events == events) return; /* nothing to do */
399 memset(&ev.data, 0, sizeof(ev.data));
402 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
404 if (errno == ENOMEM) /* not enough memory, give up on epoll */
409 else perror( "epoll_ctl" ); /* should not happen */
413 static inline void remove_epoll_user( struct fd *fd, int user )
415 if (epoll_fd == -1) return;
417 if (pollfd[user].fd != -1)
419 struct epoll_event dummy;
420 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
424 static inline void main_loop_epoll(void)
427 struct epoll_event events[128];
429 assert( POLLIN == EPOLLIN );
430 assert( POLLOUT == EPOLLOUT );
431 assert( POLLERR == EPOLLERR );
432 assert( POLLHUP == EPOLLHUP );
434 if (epoll_fd == -1) return;
438 timeout = get_next_timeout();
440 if (!active_users) break; /* last user removed by a timeout */
441 if (epoll_fd == -1) break; /* an error occurred with epoll */
443 ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
444 gettimeofday( ¤t_time, NULL );
446 /* put the events into the pollfd array first, like poll does */
447 for (i = 0; i < ret; i++)
449 int user = events[i].data.u32;
450 pollfd[user].revents = events[i].events;
453 /* read events from the pollfd array, as set_fd_events may modify them */
454 for (i = 0; i < ret; i++)
456 int user = events[i].data.u32;
457 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
462 #elif defined(HAVE_KQUEUE)
464 static int kqueue_fd = -1;
466 static inline void init_epoll(void)
468 #ifndef __APPLE__ /* kqueue support is broken in the MacOS kernel so we can't use it */
469 kqueue_fd = kqueue();
473 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
477 if (kqueue_fd == -1) return;
479 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)user );
480 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)user );
482 if (events == -1) /* stop waiting on this fd completely */
484 if (pollfd[user].fd == -1) return; /* already removed */
485 ev[0].flags |= EV_DELETE;
486 ev[1].flags |= EV_DELETE;
488 else if (pollfd[user].fd == -1)
490 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
491 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
492 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
496 if (pollfd[user].events == events) return; /* nothing to do */
497 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
498 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
501 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
503 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
508 else perror( "kevent" ); /* should not happen */
512 static inline void remove_epoll_user( struct fd *fd, int user )
514 if (kqueue_fd == -1) return;
516 if (pollfd[user].fd != -1)
520 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
521 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
522 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
526 static inline void main_loop_epoll(void)
529 struct kevent events[128];
531 if (kqueue_fd == -1) return;
535 timeout = get_next_timeout();
537 if (!active_users) break; /* last user removed by a timeout */
538 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
544 ts.tv_sec = timeout / 1000;
545 ts.tv_nsec = (timeout % 1000) * 1000000;
546 ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
548 else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
550 gettimeofday( ¤t_time, NULL );
552 /* put the events into the pollfd array first, like poll does */
553 for (i = 0; i < ret; i++)
555 long user = (long)events[i].udata;
556 pollfd[user].revents = 0;
558 for (i = 0; i < ret; i++)
560 long user = (long)events[i].udata;
561 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
562 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
563 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
564 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
567 /* read events from the pollfd array, as set_fd_events may modify them */
568 for (i = 0; i < ret; i++)
570 long user = (long)events[i].udata;
571 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
572 pollfd[user].revents = 0;
577 #else /* HAVE_KQUEUE */
579 static inline void init_epoll(void) { }
580 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
581 static inline void remove_epoll_user( struct fd *fd, int user ) { }
582 static inline void main_loop_epoll(void) { }
584 #endif /* USE_EPOLL */
587 /* add a user in the poll array and return its index, or -1 on failure */
588 static int add_poll_user( struct fd *fd )
593 ret = freelist - poll_users;
594 freelist = (struct fd **)poll_users[ret];
598 if (nb_users == allocated_users)
600 struct fd **newusers;
601 struct pollfd *newpoll;
602 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
603 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
604 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
607 poll_users = newusers;
612 poll_users = newusers;
614 if (!allocated_users) init_epoll();
615 allocated_users = new_count;
620 pollfd[ret].events = 0;
621 pollfd[ret].revents = 0;
622 poll_users[ret] = fd;
627 /* remove a user from the poll list */
628 static void remove_poll_user( struct fd *fd, int user )
631 assert( poll_users[user] == fd );
633 remove_epoll_user( fd, user );
634 pollfd[user].fd = -1;
635 pollfd[user].events = 0;
636 pollfd[user].revents = 0;
637 poll_users[user] = (struct fd *)freelist;
638 freelist = &poll_users[user];
642 /* process pending timeouts and return the time until the next timeout, in milliseconds */
643 static int get_next_timeout(void)
645 if (!list_empty( &timeout_list ))
647 struct list expired_list, *ptr;
649 /* first remove all expired timers from the list */
651 list_init( &expired_list );
652 while ((ptr = list_head( &timeout_list )) != NULL)
654 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
656 if (!time_before( ¤t_time, &timeout->when ))
658 list_remove( &timeout->entry );
659 list_add_tail( &expired_list, &timeout->entry );
664 /* now call the callback for all the removed timers */
666 while ((ptr = list_head( &expired_list )) != NULL)
668 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
669 list_remove( &timeout->entry );
670 timeout->callback( timeout->private );
674 if ((ptr = list_head( &timeout_list )) != NULL)
676 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
677 int diff = (timeout->when.tv_sec - current_time.tv_sec) * 1000
678 + (timeout->when.tv_usec - current_time.tv_usec + 999) / 1000;
679 if (diff < 0) diff = 0;
683 return -1; /* no pending timeouts */
686 /* server main poll() loop */
691 gettimeofday( ¤t_time, NULL );
694 /* fall through to normal poll loop */
698 timeout = get_next_timeout();
700 if (!active_users) break; /* last user removed by a timeout */
702 ret = poll( pollfd, nb_users, timeout );
703 gettimeofday( ¤t_time, NULL );
707 for (i = 0; i < nb_users; i++)
709 if (pollfd[i].revents)
711 fd_poll_event( poll_users[i], pollfd[i].revents );
720 /****************************************************************/
721 /* device functions */
723 static struct list device_hash[DEVICE_HASH_SIZE];
725 /* retrieve the device object for a given fd, creating it if needed */
726 static struct device *get_device( dev_t dev, int create )
728 struct device *device;
729 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
731 if (device_hash[hash].next)
733 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
734 if (device->dev == dev) return (struct device *)grab_object( device );
736 else list_init( &device_hash[hash] );
738 /* not found, create it */
740 if (!create) return NULL;
741 if ((device = alloc_object( &device_ops )))
744 device->removable = -1;
745 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
746 list_add_head( &device_hash[hash], &device->entry );
751 static void device_dump( struct object *obj, int verbose )
753 struct device *device = (struct device *)obj;
754 fprintf( stderr, "Device dev=" );
755 DUMP_LONG_LONG( device->dev );
756 fprintf( stderr, "\n" );
759 static void device_destroy( struct object *obj )
761 struct device *device = (struct device *)obj;
764 for (i = 0; i < INODE_HASH_SIZE; i++)
765 assert( list_empty(&device->inode_hash[i]) );
767 list_remove( &device->entry ); /* remove it from the hash table */
771 /****************************************************************/
772 /* inode functions */
774 /* close all pending file descriptors in the closed list */
775 static void inode_close_pending( struct inode *inode, int keep_unlinks )
777 struct list *ptr = list_head( &inode->closed );
781 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
782 struct list *next = list_next( &inode->closed, ptr );
784 if (fd->unix_fd != -1)
786 close( fd->unix_fd );
789 if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
798 static void inode_dump( struct object *obj, int verbose )
800 struct inode *inode = (struct inode *)obj;
801 fprintf( stderr, "Inode device=%p ino=", inode->device );
802 DUMP_LONG_LONG( inode->ino );
803 fprintf( stderr, "\n" );
806 static void inode_destroy( struct object *obj )
808 struct inode *inode = (struct inode *)obj;
811 assert( list_empty(&inode->open) );
812 assert( list_empty(&inode->locks) );
814 list_remove( &inode->entry );
816 while ((ptr = list_head( &inode->closed )))
818 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
820 if (fd->unix_fd != -1) close( fd->unix_fd );
823 /* make sure it is still the same file */
825 if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
827 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
828 else unlink( fd->unlink );
833 release_object( inode->device );
836 /* retrieve the inode object for a given fd, creating it if needed */
837 static struct inode *get_inode( dev_t dev, ino_t ino )
839 struct device *device;
841 unsigned int hash = ino % INODE_HASH_SIZE;
843 if (!(device = get_device( dev, 1 ))) return NULL;
845 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
847 if (inode->ino == ino)
849 release_object( device );
850 return (struct inode *)grab_object( inode );
854 /* not found, create it */
855 if ((inode = alloc_object( &inode_ops )))
857 inode->device = device;
859 list_init( &inode->open );
860 list_init( &inode->locks );
861 list_init( &inode->closed );
862 list_add_head( &device->inode_hash[hash], &inode->entry );
864 else release_object( device );
869 /* add fd to the inode list of file descriptors to close */
870 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
872 if (!list_empty( &inode->locks ))
874 list_add_head( &inode->closed, &fd->entry );
876 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
878 if (fd->unix_fd != -1) close( fd->unix_fd );
880 list_add_head( &inode->closed, &fd->entry );
882 else /* no locks on this inode and no unlink, get rid of the fd */
884 if (fd->unix_fd != -1) close( fd->unix_fd );
890 /****************************************************************/
891 /* file lock functions */
893 static void file_lock_dump( struct object *obj, int verbose )
895 struct file_lock *lock = (struct file_lock *)obj;
896 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
897 lock->shared ? "shared" : "excl", lock->fd, lock->process );
898 DUMP_LONG_LONG( lock->start );
899 fprintf( stderr, " end=" );
900 DUMP_LONG_LONG( lock->end );
901 fprintf( stderr, "\n" );
904 static int file_lock_signaled( struct object *obj, struct thread *thread )
906 struct file_lock *lock = (struct file_lock *)obj;
907 /* lock is signaled if it has lost its owner */
908 return !lock->process;
911 /* set (or remove) a Unix lock if possible for the given range */
912 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
916 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
919 if (start == end) return 1; /* can't set zero-byte lock */
920 if (start > max_unix_offset) return 1; /* ignore it */
922 fl.l_whence = SEEK_SET;
924 if (!end || end > max_unix_offset) fl.l_len = 0;
925 else fl.l_len = end - start;
926 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
931 /* check whether locks work at all on this file system */
932 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
934 set_error( STATUS_FILE_LOCK_CONFLICT );
940 /* no locking on this fs, just ignore it */
944 set_error( STATUS_FILE_LOCK_CONFLICT );
947 /* this can happen if we try to set a write lock on a read-only file */
948 /* we just ignore that error */
949 if (fl.l_type == F_WRLCK) return 1;
950 set_error( STATUS_ACCESS_DENIED );
956 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
957 /* in that case we shrink the limit and retry */
958 if (max_unix_offset > INT_MAX)
960 max_unix_offset = INT_MAX;
971 /* check if interval [start;end) overlaps the lock */
972 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
974 if (lock->end && start >= lock->end) return 0;
975 if (end && lock->start >= end) return 0;
979 /* remove Unix locks for all bytes in the specified area that are no longer locked */
980 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
988 } *first, *cur, *next, *buffer;
993 if (!fd->inode) return;
994 if (!fd->fs_locks) return;
995 if (start == end || start > max_unix_offset) return;
996 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
998 /* count the number of locks overlapping the specified area */
1000 LIST_FOR_EACH( ptr, &fd->inode->locks )
1002 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1003 if (lock->start == lock->end) continue;
1004 if (lock_overlaps( lock, start, end )) count++;
1007 if (!count) /* no locks at all, we can unlock everything */
1009 set_unix_lock( fd, start, end, F_UNLCK );
1013 /* allocate space for the list of holes */
1014 /* max. number of holes is number of locks + 1 */
1016 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1020 first->start = start;
1024 /* build a sorted list of unlocked holes in the specified area */
1026 LIST_FOR_EACH( ptr, &fd->inode->locks )
1028 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1029 if (lock->start == lock->end) continue;
1030 if (!lock_overlaps( lock, start, end )) continue;
1032 /* go through all the holes touched by this lock */
1033 for (cur = first; cur; cur = cur->next)
1035 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1036 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1038 /* now we know that lock is overlapping hole */
1040 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1042 cur->start = lock->end;
1043 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1044 /* now hole is empty, remove it */
1045 if (cur->next) cur->next->prev = cur->prev;
1046 if (cur->prev) cur->prev->next = cur->next;
1047 else if (!(first = cur->next)) goto done; /* no more holes at all */
1049 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1051 cur->end = lock->start;
1052 assert( cur->start < cur->end );
1054 else /* lock is in the middle of hole, split hole in two */
1057 next->next = cur->next;
1059 next->start = lock->end;
1060 next->end = cur->end;
1061 cur->end = lock->start;
1062 assert( next->start < next->end );
1063 assert( cur->end < next->start );
1065 break; /* done with this lock */
1070 /* clear Unix locks for all the holes */
1072 for (cur = first; cur; cur = cur->next)
1073 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1079 /* create a new lock on a fd */
1080 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1082 struct file_lock *lock;
1084 if (!fd->inode) /* not a regular file */
1086 set_error( STATUS_INVALID_HANDLE );
1090 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1091 lock->shared = shared;
1092 lock->start = start;
1095 lock->process = current->process;
1097 /* now try to set a Unix lock */
1098 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1100 release_object( lock );
1103 list_add_head( &fd->locks, &lock->fd_entry );
1104 list_add_head( &fd->inode->locks, &lock->inode_entry );
1105 list_add_head( &lock->process->locks, &lock->proc_entry );
1109 /* remove an existing lock */
1110 static void remove_lock( struct file_lock *lock, int remove_unix )
1112 struct inode *inode = lock->fd->inode;
1114 list_remove( &lock->fd_entry );
1115 list_remove( &lock->inode_entry );
1116 list_remove( &lock->proc_entry );
1117 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1118 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1119 lock->process = NULL;
1120 wake_up( &lock->obj, 0 );
1121 release_object( lock );
1124 /* remove all locks owned by a given process */
1125 void remove_process_locks( struct process *process )
1129 while ((ptr = list_head( &process->locks )))
1131 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1132 remove_lock( lock, 1 ); /* this removes it from the list */
1136 /* remove all locks on a given fd */
1137 static void remove_fd_locks( struct fd *fd )
1139 file_pos_t start = FILE_POS_T_MAX, end = 0;
1142 while ((ptr = list_head( &fd->locks )))
1144 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1145 if (lock->start < start) start = lock->start;
1146 if (!lock->end || lock->end > end) end = lock->end - 1;
1147 remove_lock( lock, 0 );
1149 if (start < end) remove_unix_locks( fd, start, end + 1 );
1152 /* add a lock on an fd */
1153 /* returns handle to wait on */
1154 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1157 file_pos_t end = start + count;
1159 /* don't allow wrapping locks */
1160 if (end && end < start)
1162 set_error( STATUS_INVALID_PARAMETER );
1166 /* check if another lock on that file overlaps the area */
1167 LIST_FOR_EACH( ptr, &fd->inode->locks )
1169 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1170 if (!lock_overlaps( lock, start, end )) continue;
1171 if (lock->shared && shared) continue;
1175 set_error( STATUS_FILE_LOCK_CONFLICT );
1178 set_error( STATUS_PENDING );
1179 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1182 /* not found, add it */
1183 if (add_lock( fd, shared, start, end )) return 0;
1184 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1186 /* Unix lock conflict -> tell client to wait and retry */
1187 if (wait) set_error( STATUS_PENDING );
1192 /* remove a lock on an fd */
1193 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1196 file_pos_t end = start + count;
1198 /* find an existing lock with the exact same parameters */
1199 LIST_FOR_EACH( ptr, &fd->locks )
1201 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1202 if ((lock->start == start) && (lock->end == end))
1204 remove_lock( lock, 1 );
1208 set_error( STATUS_FILE_LOCK_CONFLICT );
1212 /****************************************************************/
1213 /* asynchronous operations support */
1217 struct thread *thread;
1221 struct timeout_user *timeout;
1225 /* notifies client thread of new status of its async request */
1226 /* destroys the server side of it */
1227 static void async_terminate( struct async *async, int status )
1229 thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO,
1230 1, async->user, async->sb, (void *)status );
1232 if (async->timeout) remove_timeout_user( async->timeout );
1233 async->timeout = NULL;
1234 list_remove( &async->entry );
1235 release_object( async->thread );
1239 /* cb for timeout on an async request */
1240 static void async_callback(void *private)
1242 struct async *async = (struct async *)private;
1244 /* fprintf(stderr, "async timeout out %p\n", async); */
1245 async->timeout = NULL;
1246 async_terminate( async, STATUS_TIMEOUT );
1249 /* create an async on a given queue of a fd */
1250 struct async *create_async( struct thread *thread, const struct timeval *timeout,
1251 struct list *queue, void *io_apc, void *io_user, void* io_sb )
1253 struct async *async = mem_alloc( sizeof(struct async) );
1255 if (!async) return NULL;
1257 async->thread = (struct thread *)grab_object(thread);
1258 async->apc = io_apc;
1259 async->user = io_user;
1262 list_add_tail( queue, &async->entry );
1264 if (timeout) async->timeout = add_timeout_user( timeout, async_callback, async );
1265 else async->timeout = NULL;
1270 /* terminate the async operation at the head of the queue */
1271 void async_terminate_head( struct list *queue, int status )
1273 struct list *ptr = list_head( queue );
1274 if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status );
1277 /****************************************************************/
1278 /* file descriptor functions */
1280 static void fd_dump( struct object *obj, int verbose )
1282 struct fd *fd = (struct fd *)obj;
1283 fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
1284 if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1285 fprintf( stderr, "\n" );
1288 static void fd_destroy( struct object *obj )
1290 struct fd *fd = (struct fd *)obj;
1292 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1293 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1295 remove_fd_locks( fd );
1296 list_remove( &fd->inode_entry );
1297 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1300 inode_add_closed_fd( fd->inode, fd->closed );
1301 release_object( fd->inode );
1303 else /* no inode, close it right away */
1305 if (fd->unix_fd != -1) close( fd->unix_fd );
1309 /* set the events that select waits for on this fd */
1310 void set_fd_events( struct fd *fd, int events )
1312 int user = fd->poll_index;
1313 assert( poll_users[user] == fd );
1315 set_fd_epoll_events( fd, user, events );
1317 if (events == -1) /* stop waiting on this fd completely */
1319 pollfd[user].fd = -1;
1320 pollfd[user].events = POLLERR;
1321 pollfd[user].revents = 0;
1323 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1325 pollfd[user].fd = fd->unix_fd;
1326 pollfd[user].events = events;
1330 /* prepare an fd for unmounting its corresponding device */
1331 static inline void unmount_fd( struct fd *fd )
1333 assert( fd->inode );
1335 async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
1336 async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
1338 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1340 if (fd->unix_fd != -1) close( fd->unix_fd );
1344 fd->closed->unix_fd = -1;
1345 fd->closed->unlink[0] = 0;
1347 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1351 /* allocate an fd object, without setting the unix fd yet */
1352 static struct fd *alloc_fd_object(void)
1354 struct fd *fd = alloc_object( &fd_ops );
1356 if (!fd) return NULL;
1367 fd->poll_index = -1;
1368 list_init( &fd->inode_entry );
1369 list_init( &fd->locks );
1370 list_init( &fd->read_q );
1371 list_init( &fd->write_q );
1373 if ((fd->poll_index = add_poll_user( fd )) == -1)
1375 release_object( fd );
1381 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1382 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user )
1384 struct fd *fd = alloc_object( &fd_ops );
1386 if (!fd) return NULL;
1388 fd->fd_ops = fd_user_ops;
1397 fd->poll_index = -1;
1398 list_init( &fd->inode_entry );
1399 list_init( &fd->locks );
1400 list_init( &fd->read_q );
1401 list_init( &fd->write_q );
1405 /* check if the desired access is possible without violating */
1406 /* the sharing mode of other opens of the same file */
1407 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
1409 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1410 unsigned int existing_access = 0;
1413 /* if access mode is 0, sharing mode is ignored */
1414 if (!access) sharing = existing_sharing;
1415 fd->access = access;
1416 fd->sharing = sharing;
1418 LIST_FOR_EACH( ptr, &fd->inode->open )
1420 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1423 existing_sharing &= fd_ptr->sharing;
1424 existing_access |= fd_ptr->access;
1428 if ((access & FILE_UNIX_READ_ACCESS) && !(existing_sharing & FILE_SHARE_READ)) return 0;
1429 if ((access & FILE_UNIX_WRITE_ACCESS) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
1430 if ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
1431 if ((existing_access & FILE_UNIX_READ_ACCESS) && !(sharing & FILE_SHARE_READ)) return 0;
1432 if ((existing_access & FILE_UNIX_WRITE_ACCESS) && !(sharing & FILE_SHARE_WRITE)) return 0;
1433 if ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)) return 0;
1437 /* sets the user of an fd that previously had no user */
1438 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1440 assert( fd->fd_ops == NULL );
1441 fd->fd_ops = user_ops;
1445 /* open() wrapper that returns a struct fd with no fd user set */
1446 struct fd *open_fd( const char *name, int flags, mode_t *mode, unsigned int access,
1447 unsigned int sharing, unsigned int options )
1450 struct closed_fd *closed_fd;
1452 const char *unlink_name = "";
1455 if ((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE))
1457 set_error( STATUS_INVALID_PARAMETER );
1461 if (!(fd = alloc_fd_object())) return NULL;
1463 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
1464 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
1466 release_object( fd );
1470 /* create the directory if needed */
1471 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1473 if (mkdir( name, 0777 ) == -1)
1475 if (errno != EEXIST || (flags & O_EXCL))
1481 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1484 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1486 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1487 else rw_mode = O_WRONLY;
1489 else rw_mode = O_RDONLY;
1491 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1493 /* if we tried to open a directory for write access, retry read-only */
1494 if (errno != EISDIR ||
1495 !(access & FILE_UNIX_WRITE_ACCESS) ||
1496 (fd->unix_fd = open( name, O_RDONLY | (flags & ~O_TRUNC), *mode )) == -1)
1503 closed_fd->unix_fd = fd->unix_fd;
1504 closed_fd->unlink[0] = 0;
1505 fstat( fd->unix_fd, &st );
1508 /* only bother with an inode for normal files and directories */
1509 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1511 struct inode *inode = get_inode( st.st_dev, st.st_ino );
1515 /* we can close the fd because there are no others open on the same file,
1516 * otherwise we wouldn't have failed to allocate a new inode
1521 fd->closed = closed_fd;
1522 list_add_head( &inode->open, &fd->inode_entry );
1524 /* check directory options */
1525 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1527 release_object( fd );
1528 set_error( STATUS_NOT_A_DIRECTORY );
1531 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1533 release_object( fd );
1534 set_error( STATUS_FILE_IS_A_DIRECTORY );
1537 if (!check_sharing( fd, access, sharing ))
1539 release_object( fd );
1540 set_error( STATUS_SHARING_VIOLATION );
1543 strcpy( closed_fd->unlink, unlink_name );
1544 if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
1546 else /* special file */
1548 if (options & FILE_DIRECTORY_FILE)
1550 set_error( STATUS_NOT_A_DIRECTORY );
1553 if (unlink_name[0]) /* we can't unlink special files */
1555 set_error( STATUS_INVALID_PARAMETER );
1563 release_object( fd );
1568 /* create an fd for an anonymous file */
1569 /* if the function fails the unix fd is closed */
1570 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
1572 struct fd *fd = alloc_fd_object();
1576 set_fd_user( fd, fd_user_ops, user );
1577 fd->unix_fd = unix_fd;
1584 /* retrieve the object that is using an fd */
1585 void *get_fd_user( struct fd *fd )
1590 /* retrieve the unix fd for an object */
1591 int get_unix_fd( struct fd *fd )
1593 if (fd->unix_fd == -1)
1595 if (fd->unmounted) set_error( STATUS_VOLUME_DISMOUNTED );
1596 else set_error( STATUS_BAD_DEVICE_TYPE );
1601 /* check if two file descriptors point to the same file */
1602 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1604 return fd1->inode == fd2->inode;
1607 /* callback for event happening in the main poll() loop */
1608 void fd_poll_event( struct fd *fd, int event )
1610 return fd->fd_ops->poll_event( fd, event );
1613 /* check if events are pending and if yes return which one(s) */
1614 int check_fd_events( struct fd *fd, int events )
1618 if (fd->unix_fd == -1) return POLLERR;
1620 pfd.fd = fd->unix_fd;
1621 pfd.events = events;
1622 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1626 /* default add_queue() routine for objects that poll() on an fd */
1627 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1629 struct fd *fd = get_obj_fd( obj );
1632 if (!fd->inode && list_empty( &obj->wait_queue )) /* first on the queue */
1633 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1634 add_queue( obj, entry );
1635 release_object( fd );
1639 /* default remove_queue() routine for objects that poll() on an fd */
1640 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1642 struct fd *fd = get_obj_fd( obj );
1645 remove_queue( obj, entry );
1646 if (!fd->inode && list_empty( &obj->wait_queue )) /* last on the queue is gone */
1647 set_fd_events( fd, 0 );
1648 release_object( obj );
1649 release_object( fd );
1652 /* default signaled() routine for objects that poll() on an fd */
1653 int default_fd_signaled( struct object *obj, struct thread *thread )
1656 struct fd *fd = get_obj_fd( obj );
1658 if (fd->inode) ret = 1; /* regular files are always signaled */
1661 events = fd->fd_ops->get_poll_events( fd );
1662 ret = check_fd_events( fd, events ) != 0;
1666 /* stop waiting on select() if we are signaled */
1667 set_fd_events( fd, 0 );
1669 else if (!list_empty( &obj->wait_queue ))
1671 /* restart waiting on poll() if we are no longer signaled */
1672 set_fd_events( fd, events );
1675 release_object( fd );
1679 int default_fd_get_poll_events( struct fd *fd )
1683 if (!list_empty( &fd->read_q ))
1685 if (!list_empty( &fd->write_q ))
1691 /* default handler for poll() events */
1692 void default_poll_event( struct fd *fd, int event )
1694 if (!list_empty( &fd->read_q ) && (POLLIN & event) )
1696 async_terminate_head( &fd->read_q, STATUS_ALERTED );
1699 if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
1701 async_terminate_head( &fd->write_q, STATUS_ALERTED );
1705 /* if an error occurred, stop polling this fd to avoid busy-looping */
1706 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1707 wake_up( fd->user, 0 );
1710 void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count,
1711 const struct timeval *timeout )
1716 if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
1718 set_error( STATUS_INVALID_HANDLE );
1724 case ASYNC_TYPE_READ:
1725 queue = &fd->read_q;
1727 case ASYNC_TYPE_WRITE:
1728 queue = &fd->write_q;
1731 set_error( STATUS_INVALID_PARAMETER );
1735 if (!create_async( current, timeout, queue, apc, user, io_sb ))
1738 /* Check if the new pending request can be served immediately */
1739 events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1740 if (events) fd->fd_ops->poll_event( fd, events );
1742 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1745 void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count )
1747 fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL );
1750 void default_fd_cancel_async( struct fd *fd )
1752 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1753 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1756 /* default flush() routine */
1757 int no_flush( struct fd *fd, struct event **event )
1759 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1763 /* default get_file_info() routine */
1764 int no_get_file_info( struct fd *fd )
1766 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1770 /* default queue_async() routine */
1771 void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb,
1772 int type, int count)
1774 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1777 /* default cancel_async() routine */
1778 void no_cancel_async( struct fd *fd )
1780 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1783 /* close all Unix file descriptors on a device to allow unmounting it */
1784 static void unmount_device( struct fd *device_fd )
1788 struct device *device;
1789 struct inode *inode;
1791 int unix_fd = get_unix_fd( device_fd );
1793 if (unix_fd == -1) return;
1795 if (fstat( unix_fd, &st ) == -1 || !S_ISBLK( st.st_mode ))
1797 set_error( STATUS_INVALID_PARAMETER );
1801 if (!(device = get_device( st.st_rdev, 0 ))) return;
1803 for (i = 0; i < INODE_HASH_SIZE; i++)
1805 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
1807 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
1811 inode_close_pending( inode, 0 );
1814 /* remove it from the hash table */
1815 list_remove( &device->entry );
1816 list_init( &device->entry );
1817 release_object( device );
1820 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1821 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1822 unsigned int access )
1824 struct fd *fd = NULL;
1827 if ((obj = get_handle_obj( process, handle, access, NULL )))
1829 fd = get_obj_fd( obj );
1830 release_object( obj );
1835 /* flush a file buffers */
1836 DECL_HANDLER(flush_file)
1838 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1839 struct event * event = NULL;
1843 fd->fd_ops->flush( fd, &event );
1846 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1848 release_object( fd );
1852 /* open a file object */
1853 DECL_HANDLER(open_file_object)
1855 struct unicode_str name;
1856 struct directory *root = NULL;
1859 get_req_unicode_str( &name );
1860 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
1863 if ((obj = open_object_dir( root, &name, req->attributes, NULL )))
1865 /* make sure this is a valid file object */
1866 struct fd *fd = get_obj_fd( obj );
1869 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
1870 release_object( fd );
1872 release_object( obj );
1875 if (root) release_object( root );
1878 /* get a Unix fd to access a file */
1879 DECL_HANDLER(get_handle_fd)
1885 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1887 int unix_fd = get_unix_fd( fd );
1890 int cached_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1891 if (cached_fd != -1) reply->fd = cached_fd;
1892 else if (!get_error()) send_client_fd( current->process, unix_fd, req->handle );
1894 if (fd->inode) reply->removable = fd->inode->device->removable;
1895 reply->flags = fd->fd_ops->get_file_info( fd );
1896 release_object( fd );
1900 /* set the cached file descriptor of a handle */
1901 DECL_HANDLER(set_handle_fd)
1906 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1908 struct device *device = fd->inode ? fd->inode->device : NULL;
1910 if (device && device->removable == -1) device->removable = req->removable;
1912 /* only cache the fd on non-removable devices */
1913 if (!device || !device->removable)
1914 reply->cur_fd = set_handle_unix_fd( current->process, req->handle, req->fd );
1915 release_object( fd );
1919 /* get ready to unmount a Unix device */
1920 DECL_HANDLER(unmount_device)
1924 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1926 unmount_device( fd );
1927 release_object( fd );
1931 /* create / reschedule an async I/O */
1932 DECL_HANDLER(register_async)
1934 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1937 * The queue_async method must do the following:
1939 * 1. Get the async_queue for the request of given type.
1940 * 2. Create a new asynchronous request for the selected queue
1941 * 3. Carry out any operations necessary to adjust the object's poll events
1942 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1943 * 4. When the async request is triggered, then send back (with a proper APC)
1944 * the trigger (STATUS_ALERTED) to the thread that posted the request.
1945 * async_destroy() is to be called: it will both notify the sender about
1946 * the trigger and destroy the request by itself
1947 * See also the implementations in file.c, serial.c, and sock.c.
1952 fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb,
1953 req->type, req->count );
1954 release_object( fd );
1958 /* cancels all async I/O */
1959 DECL_HANDLER(cancel_async)
1961 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1964 /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1965 * NtCancelIoFile() will force the pending APC to be run. Since,
1966 * Windows only guarantees that the current thread will have no async
1967 * operation on the current fd when NtCancelIoFile returns, this shall
1970 fd->fd_ops->cancel_async( fd );
1971 release_object( fd );