Implement ldap_add* functions.
[wine.git] / server / fd.c
blob79148867cbf3da6981c0a1ae092988c0c32b6dd5
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_STDINT_H
41 #include <stdint.h>
42 #endif
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <sys/types.h>
46 #include <unistd.h>
48 #include "object.h"
49 #include "file.h"
50 #include "handle.h"
51 #include "process.h"
52 #include "request.h"
54 #include "winternl.h"
56 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
57 # include <sys/epoll.h>
58 # define USE_EPOLL
59 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
60 # define USE_EPOLL
61 # define EPOLLIN POLLIN
62 # define EPOLLOUT POLLOUT
63 # define EPOLLERR POLLERR
64 # define EPOLLHUP POLLHUP
65 # define EPOLL_CTL_ADD 1
66 # define EPOLL_CTL_DEL 2
67 # define EPOLL_CTL_MOD 3
69 typedef union epoll_data
71 void *ptr;
72 int fd;
73 uint32_t u32;
74 uint64_t u64;
75 } epoll_data_t;
77 struct epoll_event
79 uint32_t events;
80 epoll_data_t data;
83 #define SYSCALL_RET(ret) do { \
84 if (ret < 0) { errno = -ret; ret = -1; } \
85 return ret; \
86 } while(0)
88 static inline int epoll_create( int size )
90 int ret;
91 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
92 : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
93 SYSCALL_RET(ret);
96 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
98 int ret;
99 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
100 : "=a" (ret)
101 : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
102 SYSCALL_RET(ret);
105 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
107 int ret;
108 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
109 : "=a" (ret)
110 : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
111 : "memory" );
112 SYSCALL_RET(ret);
114 #undef SYSCALL_RET
116 #endif /* linux && __i386__ && HAVE_STDINT_H */
119 /* Because of the stupid Posix locking semantics, we need to keep
120 * track of all file descriptors referencing a given file, and not
121 * close a single one until all the locks are gone (sigh).
124 /* file descriptor object */
126 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
127 struct closed_fd
129 struct list entry; /* entry in inode closed list */
130 int unix_fd; /* the unix file descriptor */
131 char unlink[1]; /* name to unlink on close (if any) */
134 struct fd
136 struct object obj; /* object header */
137 const struct fd_ops *fd_ops; /* file descriptor operations */
138 struct inode *inode; /* inode that this fd belongs to */
139 struct list inode_entry; /* entry in inode fd list */
140 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
141 struct object *user; /* object using this file descriptor */
142 struct list locks; /* list of locks on this fd */
143 unsigned int access; /* file access (GENERIC_READ/WRITE) */
144 unsigned int sharing; /* file sharing mode */
145 int unix_fd; /* unix file descriptor */
146 int fs_locks; /* can we use filesystem locks for this fd? */
147 int poll_index; /* index of fd in poll array */
148 struct list read_q; /* async readers of this fd */
149 struct list write_q; /* async writers of this fd */
152 static void fd_dump( struct object *obj, int verbose );
153 static void fd_destroy( struct object *obj );
155 static const struct object_ops fd_ops =
157 sizeof(struct fd), /* size */
158 fd_dump, /* dump */
159 no_add_queue, /* add_queue */
160 NULL, /* remove_queue */
161 NULL, /* signaled */
162 NULL, /* satisfied */
163 no_signal, /* signal */
164 no_get_fd, /* get_fd */
165 no_close_handle, /* close_handle */
166 fd_destroy /* destroy */
169 /* device object */
171 #define DEVICE_HASH_SIZE 7
172 #define INODE_HASH_SIZE 17
174 struct device
176 struct object obj; /* object header */
177 struct list entry; /* entry in device hash list */
178 dev_t dev; /* device number */
179 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
182 static void device_dump( struct object *obj, int verbose );
183 static void device_destroy( struct object *obj );
185 static const struct object_ops device_ops =
187 sizeof(struct device), /* size */
188 device_dump, /* dump */
189 no_add_queue, /* add_queue */
190 NULL, /* remove_queue */
191 NULL, /* signaled */
192 NULL, /* satisfied */
193 no_signal, /* signal */
194 no_get_fd, /* get_fd */
195 no_close_handle, /* close_handle */
196 device_destroy /* destroy */
199 /* inode object */
201 struct inode
203 struct object obj; /* object header */
204 struct list entry; /* inode hash list entry */
205 struct device *device; /* device containing this inode */
206 ino_t ino; /* inode number */
207 struct list open; /* list of open file descriptors */
208 struct list locks; /* list of file locks */
209 struct list closed; /* list of file descriptors to close at destroy time */
212 static void inode_dump( struct object *obj, int verbose );
213 static void inode_destroy( struct object *obj );
215 static const struct object_ops inode_ops =
217 sizeof(struct inode), /* size */
218 inode_dump, /* dump */
219 no_add_queue, /* add_queue */
220 NULL, /* remove_queue */
221 NULL, /* signaled */
222 NULL, /* satisfied */
223 no_signal, /* signal */
224 no_get_fd, /* get_fd */
225 no_close_handle, /* close_handle */
226 inode_destroy /* destroy */
229 /* file lock object */
231 struct file_lock
233 struct object obj; /* object header */
234 struct fd *fd; /* fd owning this lock */
235 struct list fd_entry; /* entry in list of locks on a given fd */
236 struct list inode_entry; /* entry in inode list of locks */
237 int shared; /* shared lock? */
238 file_pos_t start; /* locked region is interval [start;end) */
239 file_pos_t end;
240 struct process *process; /* process owning this lock */
241 struct list proc_entry; /* entry in list of locks owned by the process */
244 static void file_lock_dump( struct object *obj, int verbose );
245 static int file_lock_signaled( struct object *obj, struct thread *thread );
247 static const struct object_ops file_lock_ops =
249 sizeof(struct file_lock), /* size */
250 file_lock_dump, /* dump */
251 add_queue, /* add_queue */
252 remove_queue, /* remove_queue */
253 file_lock_signaled, /* signaled */
254 no_satisfied, /* satisfied */
255 no_signal, /* signal */
256 no_get_fd, /* get_fd */
257 no_close_handle, /* close_handle */
258 no_destroy /* destroy */
262 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
263 #define FILE_POS_T_MAX (~(file_pos_t)0)
265 static file_pos_t max_unix_offset = OFF_T_MAX;
267 #define DUMP_LONG_LONG(val) do { \
268 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
269 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
270 else \
271 fprintf( stderr, "%lx", (unsigned long)(val) ); \
272 } while (0)
276 /****************************************************************/
277 /* timeouts support */
279 struct timeout_user
281 struct list entry; /* entry in sorted timeout list */
282 struct timeval when; /* timeout expiry (absolute time) */
283 timeout_callback callback; /* callback function */
284 void *private; /* callback private data */
287 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
289 /* add a timeout user */
290 struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
291 void *private )
293 struct timeout_user *user;
294 struct list *ptr;
296 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
297 user->when = *when;
298 user->callback = func;
299 user->private = private;
301 /* Now insert it in the linked list */
303 LIST_FOR_EACH( ptr, &timeout_list )
305 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
306 if (!time_before( &timeout->when, when )) break;
308 list_add_before( ptr, &user->entry );
309 return user;
312 /* remove a timeout user */
313 void remove_timeout_user( struct timeout_user *user )
315 list_remove( &user->entry );
316 free( user );
319 /* add a timeout in milliseconds to an absolute time */
320 void add_timeout( struct timeval *when, int timeout )
322 if (timeout)
324 long sec = timeout / 1000;
325 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
327 when->tv_usec -= 1000000;
328 when->tv_sec++;
330 when->tv_sec += sec;
335 /****************************************************************/
336 /* poll support */
338 static struct fd **poll_users; /* users array */
339 static struct pollfd *pollfd; /* poll fd array */
340 static int nb_users; /* count of array entries actually in use */
341 static int active_users; /* current number of active users */
342 static int allocated_users; /* count of allocated entries in the array */
343 static struct fd **freelist; /* list of free entries in the array */
345 #ifdef USE_EPOLL
347 static int epoll_fd;
348 static struct epoll_event *epoll_events;
350 /* set the events that epoll waits for on this fd; helper for set_fd_events */
351 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
353 struct epoll_event ev;
354 int ctl;
356 if (epoll_fd == -1) return;
358 if (events == -1) /* stop waiting on this fd completely */
360 if (pollfd[user].fd == -1) return; /* already removed */
361 ctl = EPOLL_CTL_DEL;
363 else if (pollfd[user].fd == -1)
365 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
366 ctl = EPOLL_CTL_ADD;
368 else
370 if (pollfd[user].events == events) return; /* nothing to do */
371 ctl = EPOLL_CTL_MOD;
374 ev.events = events;
375 ev.data.u32 = user;
377 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
379 if (errno == ENOMEM) /* not enough memory, give up on epoll */
381 close( epoll_fd );
382 epoll_fd = -1;
384 else perror( "epoll_ctl" ); /* should not happen */
388 #else /* USE_EPOLL */
390 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
394 #endif /* USE_EPOLL */
397 /* add a user in the poll array and return its index, or -1 on failure */
398 static int add_poll_user( struct fd *fd )
400 int ret;
401 if (freelist)
403 ret = freelist - poll_users;
404 freelist = (struct fd **)poll_users[ret];
406 else
408 if (nb_users == allocated_users)
410 struct fd **newusers;
411 struct pollfd *newpoll;
412 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
413 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
414 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
416 if (allocated_users)
417 poll_users = newusers;
418 else
419 free( newusers );
420 return -1;
422 poll_users = newusers;
423 pollfd = newpoll;
424 #ifdef USE_EPOLL
425 if (!allocated_users) epoll_fd = epoll_create( new_count );
426 if (epoll_fd != -1)
428 struct epoll_event *new_events;
429 if (!(new_events = realloc( epoll_events, new_count * sizeof(*epoll_events) )))
430 return -1;
431 epoll_events = new_events;
433 #endif
434 allocated_users = new_count;
436 ret = nb_users++;
438 pollfd[ret].fd = -1;
439 pollfd[ret].events = 0;
440 pollfd[ret].revents = 0;
441 poll_users[ret] = fd;
442 active_users++;
443 return ret;
446 /* remove a user from the poll list */
447 static void remove_poll_user( struct fd *fd, int user )
449 assert( user >= 0 );
450 assert( poll_users[user] == fd );
452 #ifdef USE_EPOLL
453 if (epoll_fd != -1 && pollfd[user].fd != -1)
455 struct epoll_event dummy;
456 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
458 #endif
459 pollfd[user].fd = -1;
460 pollfd[user].events = 0;
461 pollfd[user].revents = 0;
462 poll_users[user] = (struct fd *)freelist;
463 freelist = &poll_users[user];
464 active_users--;
467 /* process pending timeouts and return the time until the next timeout, in milliseconds */
468 static int get_next_timeout(void)
470 if (!list_empty( &timeout_list ))
472 struct list expired_list, *ptr;
473 struct timeval now;
475 gettimeofday( &now, NULL );
477 /* first remove all expired timers from the list */
479 list_init( &expired_list );
480 while ((ptr = list_head( &timeout_list )) != NULL)
482 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
484 if (!time_before( &now, &timeout->when ))
486 list_remove( &timeout->entry );
487 list_add_tail( &expired_list, &timeout->entry );
489 else break;
492 /* now call the callback for all the removed timers */
494 while ((ptr = list_head( &expired_list )) != NULL)
496 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
497 list_remove( &timeout->entry );
498 timeout->callback( timeout->private );
499 free( timeout );
502 if ((ptr = list_head( &timeout_list )) != NULL)
504 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
505 int diff = (timeout->when.tv_sec - now.tv_sec) * 1000
506 + (timeout->when.tv_usec - now.tv_usec) / 1000;
507 if (diff < 0) diff = 0;
508 return diff;
511 return -1; /* no pending timeouts */
514 /* server main poll() loop */
515 void main_loop(void)
517 int i, ret, timeout;
519 #ifdef USE_EPOLL
520 assert( POLLIN == EPOLLIN );
521 assert( POLLOUT == EPOLLOUT );
522 assert( POLLERR == EPOLLERR );
523 assert( POLLHUP == EPOLLHUP );
525 if (epoll_fd != -1)
527 while (active_users)
529 timeout = get_next_timeout();
531 if (!active_users) break; /* last user removed by a timeout */
532 if (epoll_fd == -1) break; /* an error occurred with epoll */
534 ret = epoll_wait( epoll_fd, epoll_events, allocated_users, timeout );
536 /* put the events into the pollfd array first, like poll does */
537 for (i = 0; i < ret; i++)
539 int user = epoll_events[i].data.u32;
540 pollfd[user].revents = epoll_events[i].events;
543 /* read events from the pollfd array, as set_fd_events may modify them */
544 for (i = 0; i < ret; i++)
546 int user = epoll_events[i].data.u32;
547 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
551 /* fall through to normal poll loop */
552 #endif /* USE_EPOLL */
554 while (active_users)
556 timeout = get_next_timeout();
558 if (!active_users) break; /* last user removed by a timeout */
560 ret = poll( pollfd, nb_users, timeout );
561 if (ret > 0)
563 for (i = 0; i < nb_users; i++)
565 if (pollfd[i].revents)
567 fd_poll_event( poll_users[i], pollfd[i].revents );
568 if (!--ret) break;
576 /****************************************************************/
577 /* device functions */
579 static struct list device_hash[DEVICE_HASH_SIZE];
581 /* retrieve the device object for a given fd, creating it if needed */
582 static struct device *get_device( dev_t dev )
584 struct device *device;
585 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
587 if (device_hash[hash].next)
589 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
590 if (device->dev == dev) return (struct device *)grab_object( device );
592 else list_init( &device_hash[hash] );
594 /* not found, create it */
595 if ((device = alloc_object( &device_ops )))
597 device->dev = dev;
598 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
599 list_add_head( &device_hash[hash], &device->entry );
601 return device;
604 static void device_dump( struct object *obj, int verbose )
606 struct device *device = (struct device *)obj;
607 fprintf( stderr, "Device dev=" );
608 DUMP_LONG_LONG( device->dev );
609 fprintf( stderr, "\n" );
612 static void device_destroy( struct object *obj )
614 struct device *device = (struct device *)obj;
615 unsigned int i;
617 for (i = 0; i < INODE_HASH_SIZE; i++)
618 assert( list_empty(&device->inode_hash[i]) );
620 list_remove( &device->entry ); /* remove it from the hash table */
624 /****************************************************************/
625 /* inode functions */
627 /* close all pending file descriptors in the closed list */
628 static void inode_close_pending( struct inode *inode, int keep_unlinks )
630 struct list *ptr = list_head( &inode->closed );
632 while (ptr)
634 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
635 struct list *next = list_next( &inode->closed, ptr );
637 if (fd->unix_fd != -1)
639 close( fd->unix_fd );
640 fd->unix_fd = -1;
642 if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
644 list_remove( ptr );
645 free( fd );
647 ptr = next;
651 static void inode_dump( struct object *obj, int verbose )
653 struct inode *inode = (struct inode *)obj;
654 fprintf( stderr, "Inode device=%p ino=", inode->device );
655 DUMP_LONG_LONG( inode->ino );
656 fprintf( stderr, "\n" );
659 static void inode_destroy( struct object *obj )
661 struct inode *inode = (struct inode *)obj;
662 struct list *ptr;
664 assert( list_empty(&inode->open) );
665 assert( list_empty(&inode->locks) );
667 list_remove( &inode->entry );
669 while ((ptr = list_head( &inode->closed )))
671 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
672 list_remove( ptr );
673 if (fd->unix_fd != -1) close( fd->unix_fd );
674 if (fd->unlink[0])
676 /* make sure it is still the same file */
677 struct stat st;
678 if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
680 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
681 else unlink( fd->unlink );
684 free( fd );
686 release_object( inode->device );
689 /* retrieve the inode object for a given fd, creating it if needed */
690 static struct inode *get_inode( dev_t dev, ino_t ino )
692 struct device *device;
693 struct inode *inode;
694 unsigned int hash = ino % INODE_HASH_SIZE;
696 if (!(device = get_device( dev ))) return NULL;
698 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
700 if (inode->ino == ino)
702 release_object( device );
703 return (struct inode *)grab_object( inode );
707 /* not found, create it */
708 if ((inode = alloc_object( &inode_ops )))
710 inode->device = device;
711 inode->ino = ino;
712 list_init( &inode->open );
713 list_init( &inode->locks );
714 list_init( &inode->closed );
715 list_add_head( &device->inode_hash[hash], &inode->entry );
717 else release_object( device );
719 return inode;
722 /* add fd to the inode list of file descriptors to close */
723 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
725 if (!list_empty( &inode->locks ))
727 list_add_head( &inode->closed, &fd->entry );
729 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
731 if (fd->unix_fd != -1) close( fd->unix_fd );
732 fd->unix_fd = -1;
733 list_add_head( &inode->closed, &fd->entry );
735 else /* no locks on this inode and no unlink, get rid of the fd */
737 if (fd->unix_fd != -1) close( fd->unix_fd );
738 free( fd );
743 /****************************************************************/
744 /* file lock functions */
746 static void file_lock_dump( struct object *obj, int verbose )
748 struct file_lock *lock = (struct file_lock *)obj;
749 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
750 lock->shared ? "shared" : "excl", lock->fd, lock->process );
751 DUMP_LONG_LONG( lock->start );
752 fprintf( stderr, " end=" );
753 DUMP_LONG_LONG( lock->end );
754 fprintf( stderr, "\n" );
757 static int file_lock_signaled( struct object *obj, struct thread *thread )
759 struct file_lock *lock = (struct file_lock *)obj;
760 /* lock is signaled if it has lost its owner */
761 return !lock->process;
764 /* set (or remove) a Unix lock if possible for the given range */
765 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
767 struct flock fl;
769 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
770 for (;;)
772 if (start == end) return 1; /* can't set zero-byte lock */
773 if (start > max_unix_offset) return 1; /* ignore it */
774 fl.l_type = type;
775 fl.l_whence = SEEK_SET;
776 fl.l_start = start;
777 if (!end || end > max_unix_offset) fl.l_len = 0;
778 else fl.l_len = end - start;
779 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
781 switch(errno)
783 case EACCES:
784 /* check whether locks work at all on this file system */
785 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
787 set_error( STATUS_FILE_LOCK_CONFLICT );
788 return 0;
790 /* fall through */
791 case EIO:
792 case ENOLCK:
793 /* no locking on this fs, just ignore it */
794 fd->fs_locks = 0;
795 return 1;
796 case EAGAIN:
797 set_error( STATUS_FILE_LOCK_CONFLICT );
798 return 0;
799 case EBADF:
800 /* this can happen if we try to set a write lock on a read-only file */
801 /* we just ignore that error */
802 if (fl.l_type == F_WRLCK) return 1;
803 set_error( STATUS_ACCESS_DENIED );
804 return 0;
805 #ifdef EOVERFLOW
806 case EOVERFLOW:
807 #endif
808 case EINVAL:
809 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
810 /* in that case we shrink the limit and retry */
811 if (max_unix_offset > INT_MAX)
813 max_unix_offset = INT_MAX;
814 break; /* retry */
816 /* fall through */
817 default:
818 file_set_error();
819 return 0;
824 /* check if interval [start;end) overlaps the lock */
825 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
827 if (lock->end && start >= lock->end) return 0;
828 if (end && lock->start >= end) return 0;
829 return 1;
832 /* remove Unix locks for all bytes in the specified area that are no longer locked */
833 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
835 struct hole
837 struct hole *next;
838 struct hole *prev;
839 file_pos_t start;
840 file_pos_t end;
841 } *first, *cur, *next, *buffer;
843 struct list *ptr;
844 int count = 0;
846 if (!fd->inode) return;
847 if (!fd->fs_locks) return;
848 if (start == end || start > max_unix_offset) return;
849 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
851 /* count the number of locks overlapping the specified area */
853 LIST_FOR_EACH( ptr, &fd->inode->locks )
855 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
856 if (lock->start == lock->end) continue;
857 if (lock_overlaps( lock, start, end )) count++;
860 if (!count) /* no locks at all, we can unlock everything */
862 set_unix_lock( fd, start, end, F_UNLCK );
863 return;
866 /* allocate space for the list of holes */
867 /* max. number of holes is number of locks + 1 */
869 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
870 first = buffer;
871 first->next = NULL;
872 first->prev = NULL;
873 first->start = start;
874 first->end = end;
875 next = first + 1;
877 /* build a sorted list of unlocked holes in the specified area */
879 LIST_FOR_EACH( ptr, &fd->inode->locks )
881 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
882 if (lock->start == lock->end) continue;
883 if (!lock_overlaps( lock, start, end )) continue;
885 /* go through all the holes touched by this lock */
886 for (cur = first; cur; cur = cur->next)
888 if (cur->end <= lock->start) continue; /* hole is before start of lock */
889 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
891 /* now we know that lock is overlapping hole */
893 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
895 cur->start = lock->end;
896 if (cur->start && cur->start < cur->end) break; /* done with this lock */
897 /* now hole is empty, remove it */
898 if (cur->next) cur->next->prev = cur->prev;
899 if (cur->prev) cur->prev->next = cur->next;
900 else if (!(first = cur->next)) goto done; /* no more holes at all */
902 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
904 cur->end = lock->start;
905 assert( cur->start < cur->end );
907 else /* lock is in the middle of hole, split hole in two */
909 next->prev = cur;
910 next->next = cur->next;
911 cur->next = next;
912 next->start = lock->end;
913 next->end = cur->end;
914 cur->end = lock->start;
915 assert( next->start < next->end );
916 assert( cur->end < next->start );
917 next++;
918 break; /* done with this lock */
923 /* clear Unix locks for all the holes */
925 for (cur = first; cur; cur = cur->next)
926 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
928 done:
929 free( buffer );
932 /* create a new lock on a fd */
933 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
935 struct file_lock *lock;
937 if (!fd->inode) /* not a regular file */
939 set_error( STATUS_INVALID_HANDLE );
940 return NULL;
943 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
944 lock->shared = shared;
945 lock->start = start;
946 lock->end = end;
947 lock->fd = fd;
948 lock->process = current->process;
950 /* now try to set a Unix lock */
951 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
953 release_object( lock );
954 return NULL;
956 list_add_head( &fd->locks, &lock->fd_entry );
957 list_add_head( &fd->inode->locks, &lock->inode_entry );
958 list_add_head( &lock->process->locks, &lock->proc_entry );
959 return lock;
962 /* remove an existing lock */
963 static void remove_lock( struct file_lock *lock, int remove_unix )
965 struct inode *inode = lock->fd->inode;
967 list_remove( &lock->fd_entry );
968 list_remove( &lock->inode_entry );
969 list_remove( &lock->proc_entry );
970 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
971 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
972 lock->process = NULL;
973 wake_up( &lock->obj, 0 );
974 release_object( lock );
977 /* remove all locks owned by a given process */
978 void remove_process_locks( struct process *process )
980 struct list *ptr;
982 while ((ptr = list_head( &process->locks )))
984 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
985 remove_lock( lock, 1 ); /* this removes it from the list */
989 /* remove all locks on a given fd */
990 static void remove_fd_locks( struct fd *fd )
992 file_pos_t start = FILE_POS_T_MAX, end = 0;
993 struct list *ptr;
995 while ((ptr = list_head( &fd->locks )))
997 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
998 if (lock->start < start) start = lock->start;
999 if (!lock->end || lock->end > end) end = lock->end - 1;
1000 remove_lock( lock, 0 );
1002 if (start < end) remove_unix_locks( fd, start, end + 1 );
1005 /* add a lock on an fd */
1006 /* returns handle to wait on */
1007 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1009 struct list *ptr;
1010 file_pos_t end = start + count;
1012 /* don't allow wrapping locks */
1013 if (end && end < start)
1015 set_error( STATUS_INVALID_PARAMETER );
1016 return 0;
1019 /* check if another lock on that file overlaps the area */
1020 LIST_FOR_EACH( ptr, &fd->inode->locks )
1022 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1023 if (!lock_overlaps( lock, start, end )) continue;
1024 if (lock->shared && shared) continue;
1025 /* found one */
1026 if (!wait)
1028 set_error( STATUS_FILE_LOCK_CONFLICT );
1029 return 0;
1031 set_error( STATUS_PENDING );
1032 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1035 /* not found, add it */
1036 if (add_lock( fd, shared, start, end )) return 0;
1037 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1039 /* Unix lock conflict -> tell client to wait and retry */
1040 if (wait) set_error( STATUS_PENDING );
1042 return 0;
1045 /* remove a lock on an fd */
1046 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1048 struct list *ptr;
1049 file_pos_t end = start + count;
1051 /* find an existing lock with the exact same parameters */
1052 LIST_FOR_EACH( ptr, &fd->locks )
1054 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1055 if ((lock->start == start) && (lock->end == end))
1057 remove_lock( lock, 1 );
1058 return;
1061 set_error( STATUS_FILE_LOCK_CONFLICT );
1065 /****************************************************************/
1066 /* asynchronous operations support */
1068 struct async
1070 struct thread *thread;
1071 void *apc;
1072 void *user;
1073 void *sb;
1074 struct timeout_user *timeout;
1075 struct list entry;
1078 /* notifies client thread of new status of its async request */
1079 /* destroys the server side of it */
1080 static void async_terminate( struct async *async, int status )
1082 thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO,
1083 1, async->user, async->sb, (void *)status );
1085 if (async->timeout) remove_timeout_user( async->timeout );
1086 async->timeout = NULL;
1087 list_remove( &async->entry );
1088 release_object( async->thread );
1089 free( async );
1092 /* cb for timeout on an async request */
1093 static void async_callback(void *private)
1095 struct async *async = (struct async *)private;
1097 /* fprintf(stderr, "async timeout out %p\n", async); */
1098 async->timeout = NULL;
1099 async_terminate( async, STATUS_TIMEOUT );
1102 /* create an async on a given queue of a fd */
1103 struct async *create_async(struct thread *thread, int* timeout, struct list *queue,
1104 void *io_apc, void *io_user, void* io_sb)
1106 struct async *async = mem_alloc( sizeof(struct async) );
1108 if (!async) return NULL;
1110 async->thread = (struct thread *)grab_object(thread);
1111 async->apc = io_apc;
1112 async->user = io_user;
1113 async->sb = io_sb;
1115 list_add_tail( queue, &async->entry );
1117 if (timeout)
1119 struct timeval when;
1121 gettimeofday( &when, NULL );
1122 add_timeout( &when, *timeout );
1123 async->timeout = add_timeout_user( &when, async_callback, async );
1125 else async->timeout = NULL;
1127 return async;
1130 /* terminate the async operation at the head of the queue */
1131 void async_terminate_head( struct list *queue, int status )
1133 struct list *ptr = list_head( queue );
1134 if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status );
1137 /****************************************************************/
1138 /* file descriptor functions */
1140 static void fd_dump( struct object *obj, int verbose )
1142 struct fd *fd = (struct fd *)obj;
1143 fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
1144 if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1145 fprintf( stderr, "\n" );
1148 static void fd_destroy( struct object *obj )
1150 struct fd *fd = (struct fd *)obj;
1152 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1153 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1155 remove_fd_locks( fd );
1156 list_remove( &fd->inode_entry );
1157 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1158 if (fd->inode)
1160 inode_add_closed_fd( fd->inode, fd->closed );
1161 release_object( fd->inode );
1163 else /* no inode, close it right away */
1165 if (fd->unix_fd != -1) close( fd->unix_fd );
1169 /* set the events that select waits for on this fd */
1170 void set_fd_events( struct fd *fd, int events )
1172 int user = fd->poll_index;
1173 assert( poll_users[user] == fd );
1175 set_fd_epoll_events( fd, user, events );
1177 if (events == -1) /* stop waiting on this fd completely */
1179 pollfd[user].fd = -1;
1180 pollfd[user].events = POLLERR;
1181 pollfd[user].revents = 0;
1183 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1185 pollfd[user].fd = fd->unix_fd;
1186 pollfd[user].events = events;
1190 /* prepare an fd for unmounting its corresponding device */
1191 static inline void unmount_fd( struct fd *fd )
1193 assert( fd->inode );
1195 async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
1196 async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
1198 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1200 if (fd->unix_fd != -1) close( fd->unix_fd );
1202 fd->unix_fd = -1;
1203 fd->closed->unix_fd = -1;
1204 fd->closed->unlink[0] = 0;
1206 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1207 fd->fs_locks = 0;
1210 /* allocate an fd object, without setting the unix fd yet */
1211 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
1213 struct fd *fd = alloc_object( &fd_ops );
1215 if (!fd) return NULL;
1217 fd->fd_ops = fd_user_ops;
1218 fd->user = user;
1219 fd->inode = NULL;
1220 fd->closed = NULL;
1221 fd->access = 0;
1222 fd->sharing = 0;
1223 fd->unix_fd = -1;
1224 fd->fs_locks = 1;
1225 fd->poll_index = -1;
1226 list_init( &fd->inode_entry );
1227 list_init( &fd->locks );
1228 list_init( &fd->read_q );
1229 list_init( &fd->write_q );
1231 if ((fd->poll_index = add_poll_user( fd )) == -1)
1233 release_object( fd );
1234 return NULL;
1236 return fd;
1239 /* check if the desired access is possible without violating */
1240 /* the sharing mode of other opens of the same file */
1241 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
1243 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1244 unsigned int existing_access = 0;
1245 int unlink = 0;
1246 struct list *ptr;
1248 /* if access mode is 0, sharing mode is ignored */
1249 if (!access) sharing = existing_sharing;
1250 fd->access = access;
1251 fd->sharing = sharing;
1253 LIST_FOR_EACH( ptr, &fd->inode->open )
1255 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1256 if (fd_ptr != fd)
1258 existing_sharing &= fd_ptr->sharing;
1259 existing_access |= fd_ptr->access;
1260 if (fd_ptr->closed->unlink[0]) unlink = 1;
1264 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) return 0;
1265 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
1266 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) return 0;
1267 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) return 0;
1268 if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
1269 if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
1270 return 1;
1273 /* open() wrapper using a struct fd */
1274 /* the fd must have been created with alloc_fd */
1275 /* on error the fd object is released */
1276 struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
1277 unsigned int access, unsigned int sharing, unsigned int options )
1279 struct stat st;
1280 struct closed_fd *closed_fd;
1281 const char *unlink_name = "";
1283 assert( fd->unix_fd == -1 );
1285 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
1286 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
1288 release_object( fd );
1289 return NULL;
1291 /* create the directory if needed */
1292 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1294 if (mkdir( name, 0777 ) == -1)
1296 if (errno != EEXIST || (flags & O_EXCL))
1298 file_set_error();
1299 release_object( fd );
1300 free( closed_fd );
1301 return NULL;
1304 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1306 if ((fd->unix_fd = open( name, flags & ~O_TRUNC, *mode )) == -1)
1308 file_set_error();
1309 release_object( fd );
1310 free( closed_fd );
1311 return NULL;
1313 closed_fd->unix_fd = fd->unix_fd;
1314 closed_fd->unlink[0] = 0;
1315 fstat( fd->unix_fd, &st );
1316 *mode = st.st_mode;
1318 /* only bother with an inode for normal files and directories */
1319 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1321 struct inode *inode = get_inode( st.st_dev, st.st_ino );
1323 if (!inode)
1325 /* we can close the fd because there are no others open on the same file,
1326 * otherwise we wouldn't have failed to allocate a new inode
1328 goto error;
1330 fd->inode = inode;
1331 fd->closed = closed_fd;
1332 list_add_head( &inode->open, &fd->inode_entry );
1334 /* check directory options */
1335 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1337 release_object( fd );
1338 set_error( STATUS_NOT_A_DIRECTORY );
1339 return NULL;
1341 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1343 release_object( fd );
1344 set_error( STATUS_FILE_IS_A_DIRECTORY );
1345 return NULL;
1347 if (!check_sharing( fd, access, sharing ))
1349 release_object( fd );
1350 set_error( STATUS_SHARING_VIOLATION );
1351 return NULL;
1353 strcpy( closed_fd->unlink, unlink_name );
1354 if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
1356 else /* special file */
1358 if (options & FILE_DIRECTORY_FILE)
1360 set_error( STATUS_NOT_A_DIRECTORY );
1361 goto error;
1363 if (unlink_name[0]) /* we can't unlink special files */
1365 set_error( STATUS_INVALID_PARAMETER );
1366 goto error;
1368 free( closed_fd );
1370 return fd;
1372 error:
1373 release_object( fd );
1374 free( closed_fd );
1375 return NULL;
1378 /* create an fd for an anonymous file */
1379 /* if the function fails the unix fd is closed */
1380 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
1382 struct fd *fd = alloc_fd( fd_user_ops, user );
1384 if (fd)
1386 fd->unix_fd = unix_fd;
1387 return fd;
1389 close( unix_fd );
1390 return NULL;
1393 /* retrieve the object that is using an fd */
1394 void *get_fd_user( struct fd *fd )
1396 return fd->user;
1399 /* retrieve the unix fd for an object */
1400 int get_unix_fd( struct fd *fd )
1402 if (fd->unix_fd == -1) set_error( STATUS_VOLUME_DISMOUNTED );
1403 return fd->unix_fd;
1406 /* check if two file descriptors point to the same file */
1407 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1409 return fd1->inode == fd2->inode;
1412 /* callback for event happening in the main poll() loop */
1413 void fd_poll_event( struct fd *fd, int event )
1415 return fd->fd_ops->poll_event( fd, event );
1418 /* check if events are pending and if yes return which one(s) */
1419 int check_fd_events( struct fd *fd, int events )
1421 struct pollfd pfd;
1423 if (fd->unix_fd == -1) return POLLERR;
1425 pfd.fd = fd->unix_fd;
1426 pfd.events = events;
1427 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1428 return pfd.revents;
1431 /* default add_queue() routine for objects that poll() on an fd */
1432 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1434 struct fd *fd = get_obj_fd( obj );
1436 if (!fd) return 0;
1437 if (list_empty( &obj->wait_queue )) /* first on the queue */
1438 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1439 add_queue( obj, entry );
1440 release_object( fd );
1441 return 1;
1444 /* default remove_queue() routine for objects that poll() on an fd */
1445 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1447 struct fd *fd = get_obj_fd( obj );
1449 grab_object( obj );
1450 remove_queue( obj, entry );
1451 if (list_empty( &obj->wait_queue )) /* last on the queue is gone */
1452 set_fd_events( fd, 0 );
1453 release_object( obj );
1454 release_object( fd );
1457 /* default signaled() routine for objects that poll() on an fd */
1458 int default_fd_signaled( struct object *obj, struct thread *thread )
1460 int events, ret;
1461 struct fd *fd = get_obj_fd( obj );
1463 if (fd->inode) return 1; /* regular files are always signaled */
1465 events = fd->fd_ops->get_poll_events( fd );
1466 ret = check_fd_events( fd, events ) != 0;
1468 if (ret)
1469 set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
1470 else if (!list_empty( &obj->wait_queue ))
1471 set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
1473 release_object( fd );
1474 return ret;
1477 int default_fd_get_poll_events( struct fd *fd )
1479 int events = 0;
1481 if (!list_empty( &fd->read_q ))
1482 events |= POLLIN;
1483 if (!list_empty( &fd->write_q ))
1484 events |= POLLOUT;
1486 return events;
1489 /* default handler for poll() events */
1490 void default_poll_event( struct fd *fd, int event )
1492 if (!list_empty( &fd->read_q ) && (POLLIN & event) )
1494 async_terminate_head( &fd->read_q, STATUS_ALERTED );
1495 return;
1497 if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
1499 async_terminate_head( &fd->write_q, STATUS_ALERTED );
1500 return;
1503 /* if an error occurred, stop polling this fd to avoid busy-looping */
1504 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1505 wake_up( fd->user, 0 );
1508 void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count, int *timeout )
1510 struct list *queue;
1511 int events;
1513 if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
1515 set_error( STATUS_INVALID_HANDLE );
1516 return;
1519 switch (type)
1521 case ASYNC_TYPE_READ:
1522 queue = &fd->read_q;
1523 break;
1524 case ASYNC_TYPE_WRITE:
1525 queue = &fd->write_q;
1526 break;
1527 default:
1528 set_error( STATUS_INVALID_PARAMETER );
1529 return;
1532 if (!create_async( current, timeout, queue, apc, user, io_sb ))
1533 return;
1535 /* Check if the new pending request can be served immediately */
1536 events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1537 if (events) fd->fd_ops->poll_event( fd, events );
1539 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1542 void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count )
1544 fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL );
1547 void default_fd_cancel_async( struct fd *fd )
1549 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1550 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1553 /* default flush() routine */
1554 int no_flush( struct fd *fd, struct event **event )
1556 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1557 return 0;
1560 /* default get_file_info() routine */
1561 int no_get_file_info( struct fd *fd )
1563 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1564 return 0;
1567 /* default queue_async() routine */
1568 void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb,
1569 int type, int count)
1571 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1574 /* default cancel_async() routine */
1575 void no_cancel_async( struct fd *fd )
1577 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1580 /* close all Unix file descriptors on a device to allow unmounting it */
1581 static void unmount_device( struct device *device )
1583 unsigned int i;
1584 struct inode *inode;
1585 struct fd *fd;
1587 for (i = 0; i < INODE_HASH_SIZE; i++)
1589 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
1591 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
1593 unmount_fd( fd );
1595 inode_close_pending( inode, 0 );
1598 /* remove it from the hash table */
1599 list_remove( &device->entry );
1600 list_init( &device->entry );
1603 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1604 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1605 unsigned int access )
1607 struct fd *fd = NULL;
1608 struct object *obj;
1610 if ((obj = get_handle_obj( process, handle, access, NULL )))
1612 fd = get_obj_fd( obj );
1613 release_object( obj );
1615 return fd;
1618 /* flush a file buffers */
1619 DECL_HANDLER(flush_file)
1621 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1622 struct event * event = NULL;
1624 if (fd)
1626 fd->fd_ops->flush( fd, &event );
1627 if ( event )
1629 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1631 release_object( fd );
1635 /* get a Unix fd to access a file */
1636 DECL_HANDLER(get_handle_fd)
1638 struct fd *fd;
1640 reply->fd = -1;
1642 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1644 int unix_fd = get_unix_fd( fd );
1645 if (unix_fd != -1)
1647 int cached_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1648 if (cached_fd != -1) reply->fd = cached_fd;
1649 else if (!get_error()) send_client_fd( current->process, unix_fd, req->handle );
1651 reply->flags = fd->fd_ops->get_file_info( fd );
1652 release_object( fd );
1656 /* get ready to unmount a Unix device */
1657 DECL_HANDLER(unmount_device)
1659 struct fd *fd;
1661 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1663 if (fd->inode) unmount_device( fd->inode->device );
1664 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
1665 release_object( fd );
1669 /* create / reschedule an async I/O */
1670 DECL_HANDLER(register_async)
1672 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1675 * The queue_async method must do the following:
1677 * 1. Get the async_queue for the request of given type.
1678 * 2. Create a new asynchronous request for the selected queue
1679 * 3. Carry out any operations necessary to adjust the object's poll events
1680 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1681 * 4. When the async request is triggered, then send back (with a proper APC)
1682 * the trigger (STATUS_ALERTED) to the thread that posted the request.
1683 * async_destroy() is to be called: it will both notify the sender about
1684 * the trigger and destroy the request by itself
1685 * See also the implementations in file.c, serial.c, and sock.c.
1688 if (fd)
1690 fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb,
1691 req->type, req->count );
1692 release_object( fd );
1696 /* cancels all async I/O */
1697 DECL_HANDLER(cancel_async)
1699 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1700 if (fd)
1702 /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1703 * NtCancelIoFile() will force the pending APC to be run. Since,
1704 * Windows only guarantees that the current thread will have no async
1705 * operation on the current fd when NtCancelIoFile returns, this shall
1706 * do the work.
1708 fd->fd_ops->cancel_async( fd );
1709 release_object( fd );