Remove call to StrCatW by passing in NULL for lpszExtraInformation in
[wine.git] / server / fd.c
blob4ab7cbc9f41793d389e17f11a6bee1153b697246
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 "ntstatus.h"
49 #define WIN32_NO_STATUS
50 #include "object.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "process.h"
54 #include "request.h"
56 #include "winternl.h"
58 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
59 # include <sys/epoll.h>
60 # define USE_EPOLL
61 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
62 # define USE_EPOLL
63 # define EPOLLIN POLLIN
64 # define EPOLLOUT POLLOUT
65 # define EPOLLERR POLLERR
66 # define EPOLLHUP POLLHUP
67 # define EPOLL_CTL_ADD 1
68 # define EPOLL_CTL_DEL 2
69 # define EPOLL_CTL_MOD 3
71 typedef union epoll_data
73 void *ptr;
74 int fd;
75 uint32_t u32;
76 uint64_t u64;
77 } epoll_data_t;
79 struct epoll_event
81 uint32_t events;
82 epoll_data_t data;
85 #define SYSCALL_RET(ret) do { \
86 if (ret < 0) { errno = -ret; ret = -1; } \
87 return ret; \
88 } while(0)
90 static inline int epoll_create( int size )
92 int ret;
93 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
94 : "=a" (ret) : "0" (254 /*NR_epoll_create*/), "r" (size) );
95 SYSCALL_RET(ret);
98 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
100 int ret;
101 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
102 : "=a" (ret)
103 : "0" (255 /*NR_epoll_ctl*/), "r" (epfd), "c" (op), "d" (fd), "S" (event), "m" (*event) );
104 SYSCALL_RET(ret);
107 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
109 int ret;
110 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
111 : "=a" (ret)
112 : "0" (256 /*NR_epoll_wait*/), "r" (epfd), "c" (events), "d" (maxevents), "S" (timeout)
113 : "memory" );
114 SYSCALL_RET(ret);
116 #undef SYSCALL_RET
118 #endif /* linux && __i386__ && HAVE_STDINT_H */
121 /* Because of the stupid Posix locking semantics, we need to keep
122 * track of all file descriptors referencing a given file, and not
123 * close a single one until all the locks are gone (sigh).
126 /* file descriptor object */
128 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
129 struct closed_fd
131 struct list entry; /* entry in inode closed list */
132 int unix_fd; /* the unix file descriptor */
133 char unlink[1]; /* name to unlink on close (if any) */
136 struct fd
138 struct object obj; /* object header */
139 const struct fd_ops *fd_ops; /* file descriptor operations */
140 struct inode *inode; /* inode that this fd belongs to */
141 struct list inode_entry; /* entry in inode fd list */
142 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
143 struct object *user; /* object using this file descriptor */
144 struct list locks; /* list of locks on this fd */
145 unsigned int access; /* file access (GENERIC_READ/WRITE) */
146 unsigned int sharing; /* file sharing mode */
147 int unix_fd; /* unix file descriptor */
148 int fs_locks; /* can we use filesystem locks for this fd? */
149 int poll_index; /* index of fd in poll array */
150 struct list read_q; /* async readers of this fd */
151 struct list write_q; /* async writers of this fd */
154 static void fd_dump( struct object *obj, int verbose );
155 static void fd_destroy( struct object *obj );
157 static const struct object_ops fd_ops =
159 sizeof(struct fd), /* size */
160 fd_dump, /* dump */
161 no_add_queue, /* add_queue */
162 NULL, /* remove_queue */
163 NULL, /* signaled */
164 NULL, /* satisfied */
165 no_signal, /* signal */
166 no_get_fd, /* get_fd */
167 no_lookup_name, /* lookup_name */
168 no_close_handle, /* close_handle */
169 fd_destroy /* destroy */
172 /* device object */
174 #define DEVICE_HASH_SIZE 7
175 #define INODE_HASH_SIZE 17
177 struct device
179 struct object obj; /* object header */
180 struct list entry; /* entry in device hash list */
181 dev_t dev; /* device number */
182 int removable; /* removable device? (or -1 if unknown) */
183 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
186 static void device_dump( struct object *obj, int verbose );
187 static void device_destroy( struct object *obj );
189 static const struct object_ops device_ops =
191 sizeof(struct device), /* size */
192 device_dump, /* dump */
193 no_add_queue, /* add_queue */
194 NULL, /* remove_queue */
195 NULL, /* signaled */
196 NULL, /* satisfied */
197 no_signal, /* signal */
198 no_get_fd, /* get_fd */
199 no_lookup_name, /* lookup_name */
200 no_close_handle, /* close_handle */
201 device_destroy /* destroy */
204 /* inode object */
206 struct inode
208 struct object obj; /* object header */
209 struct list entry; /* inode hash list entry */
210 struct device *device; /* device containing this inode */
211 ino_t ino; /* inode number */
212 struct list open; /* list of open file descriptors */
213 struct list locks; /* list of file locks */
214 struct list closed; /* list of file descriptors to close at destroy time */
217 static void inode_dump( struct object *obj, int verbose );
218 static void inode_destroy( struct object *obj );
220 static const struct object_ops inode_ops =
222 sizeof(struct inode), /* size */
223 inode_dump, /* dump */
224 no_add_queue, /* add_queue */
225 NULL, /* remove_queue */
226 NULL, /* signaled */
227 NULL, /* satisfied */
228 no_signal, /* signal */
229 no_get_fd, /* get_fd */
230 no_lookup_name, /* lookup_name */
231 no_close_handle, /* close_handle */
232 inode_destroy /* destroy */
235 /* file lock object */
237 struct file_lock
239 struct object obj; /* object header */
240 struct fd *fd; /* fd owning this lock */
241 struct list fd_entry; /* entry in list of locks on a given fd */
242 struct list inode_entry; /* entry in inode list of locks */
243 int shared; /* shared lock? */
244 file_pos_t start; /* locked region is interval [start;end) */
245 file_pos_t end;
246 struct process *process; /* process owning this lock */
247 struct list proc_entry; /* entry in list of locks owned by the process */
250 static void file_lock_dump( struct object *obj, int verbose );
251 static int file_lock_signaled( struct object *obj, struct thread *thread );
253 static const struct object_ops file_lock_ops =
255 sizeof(struct file_lock), /* size */
256 file_lock_dump, /* dump */
257 add_queue, /* add_queue */
258 remove_queue, /* remove_queue */
259 file_lock_signaled, /* signaled */
260 no_satisfied, /* satisfied */
261 no_signal, /* signal */
262 no_get_fd, /* get_fd */
263 no_lookup_name, /* lookup_name */
264 no_close_handle, /* close_handle */
265 no_destroy /* destroy */
269 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
270 #define FILE_POS_T_MAX (~(file_pos_t)0)
272 static file_pos_t max_unix_offset = OFF_T_MAX;
274 #define DUMP_LONG_LONG(val) do { \
275 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
276 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
277 else \
278 fprintf( stderr, "%lx", (unsigned long)(val) ); \
279 } while (0)
283 /****************************************************************/
284 /* timeouts support */
286 struct timeout_user
288 struct list entry; /* entry in sorted timeout list */
289 struct timeval when; /* timeout expiry (absolute time) */
290 timeout_callback callback; /* callback function */
291 void *private; /* callback private data */
294 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
296 /* add a timeout user */
297 struct timeout_user *add_timeout_user( const struct timeval *when, timeout_callback func,
298 void *private )
300 struct timeout_user *user;
301 struct list *ptr;
303 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
304 user->when = *when;
305 user->callback = func;
306 user->private = private;
308 /* Now insert it in the linked list */
310 LIST_FOR_EACH( ptr, &timeout_list )
312 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
313 if (!time_before( &timeout->when, when )) break;
315 list_add_before( ptr, &user->entry );
316 return user;
319 /* remove a timeout user */
320 void remove_timeout_user( struct timeout_user *user )
322 list_remove( &user->entry );
323 free( user );
326 /* add a timeout in milliseconds to an absolute time */
327 void add_timeout( struct timeval *when, int timeout )
329 if (timeout)
331 long sec = timeout / 1000;
332 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
334 when->tv_usec -= 1000000;
335 when->tv_sec++;
337 when->tv_sec += sec;
342 /****************************************************************/
343 /* poll support */
345 static struct fd **poll_users; /* users array */
346 static struct pollfd *pollfd; /* poll fd array */
347 static int nb_users; /* count of array entries actually in use */
348 static int active_users; /* current number of active users */
349 static int allocated_users; /* count of allocated entries in the array */
350 static struct fd **freelist; /* list of free entries in the array */
352 #ifdef USE_EPOLL
354 static int epoll_fd;
355 static struct epoll_event *epoll_events;
357 /* set the events that epoll waits for on this fd; helper for set_fd_events */
358 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
360 struct epoll_event ev;
361 int ctl;
363 if (epoll_fd == -1) return;
365 if (events == -1) /* stop waiting on this fd completely */
367 if (pollfd[user].fd == -1) return; /* already removed */
368 ctl = EPOLL_CTL_DEL;
370 else if (pollfd[user].fd == -1)
372 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
373 ctl = EPOLL_CTL_ADD;
375 else
377 if (pollfd[user].events == events) return; /* nothing to do */
378 ctl = EPOLL_CTL_MOD;
381 ev.events = events;
382 ev.data.u32 = user;
384 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
386 if (errno == ENOMEM) /* not enough memory, give up on epoll */
388 close( epoll_fd );
389 epoll_fd = -1;
391 else perror( "epoll_ctl" ); /* should not happen */
395 #else /* USE_EPOLL */
397 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
401 #endif /* USE_EPOLL */
404 /* add a user in the poll array and return its index, or -1 on failure */
405 static int add_poll_user( struct fd *fd )
407 int ret;
408 if (freelist)
410 ret = freelist - poll_users;
411 freelist = (struct fd **)poll_users[ret];
413 else
415 if (nb_users == allocated_users)
417 struct fd **newusers;
418 struct pollfd *newpoll;
419 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
420 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
421 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
423 if (allocated_users)
424 poll_users = newusers;
425 else
426 free( newusers );
427 return -1;
429 poll_users = newusers;
430 pollfd = newpoll;
431 #ifdef USE_EPOLL
432 if (!allocated_users) epoll_fd = epoll_create( new_count );
433 if (epoll_fd != -1)
435 struct epoll_event *new_events;
436 if (!(new_events = realloc( epoll_events, new_count * sizeof(*epoll_events) )))
437 return -1;
438 epoll_events = new_events;
440 #endif
441 allocated_users = new_count;
443 ret = nb_users++;
445 pollfd[ret].fd = -1;
446 pollfd[ret].events = 0;
447 pollfd[ret].revents = 0;
448 poll_users[ret] = fd;
449 active_users++;
450 return ret;
453 /* remove a user from the poll list */
454 static void remove_poll_user( struct fd *fd, int user )
456 assert( user >= 0 );
457 assert( poll_users[user] == fd );
459 #ifdef USE_EPOLL
460 if (epoll_fd != -1 && pollfd[user].fd != -1)
462 struct epoll_event dummy;
463 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
465 #endif
466 pollfd[user].fd = -1;
467 pollfd[user].events = 0;
468 pollfd[user].revents = 0;
469 poll_users[user] = (struct fd *)freelist;
470 freelist = &poll_users[user];
471 active_users--;
474 /* process pending timeouts and return the time until the next timeout, in milliseconds */
475 static int get_next_timeout(void)
477 if (!list_empty( &timeout_list ))
479 struct list expired_list, *ptr;
480 struct timeval now;
482 gettimeofday( &now, NULL );
484 /* first remove all expired timers from the list */
486 list_init( &expired_list );
487 while ((ptr = list_head( &timeout_list )) != NULL)
489 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
491 if (!time_before( &now, &timeout->when ))
493 list_remove( &timeout->entry );
494 list_add_tail( &expired_list, &timeout->entry );
496 else break;
499 /* now call the callback for all the removed timers */
501 while ((ptr = list_head( &expired_list )) != NULL)
503 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
504 list_remove( &timeout->entry );
505 timeout->callback( timeout->private );
506 free( timeout );
509 if ((ptr = list_head( &timeout_list )) != NULL)
511 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
512 int diff = (timeout->when.tv_sec - now.tv_sec) * 1000
513 + (timeout->when.tv_usec - now.tv_usec) / 1000;
514 if (diff < 0) diff = 0;
515 return diff;
518 return -1; /* no pending timeouts */
521 /* server main poll() loop */
522 void main_loop(void)
524 int i, ret, timeout;
526 #ifdef USE_EPOLL
527 assert( POLLIN == EPOLLIN );
528 assert( POLLOUT == EPOLLOUT );
529 assert( POLLERR == EPOLLERR );
530 assert( POLLHUP == EPOLLHUP );
532 if (epoll_fd != -1)
534 while (active_users)
536 timeout = get_next_timeout();
538 if (!active_users) break; /* last user removed by a timeout */
539 if (epoll_fd == -1) break; /* an error occurred with epoll */
541 ret = epoll_wait( epoll_fd, epoll_events, allocated_users, timeout );
543 /* put the events into the pollfd array first, like poll does */
544 for (i = 0; i < ret; i++)
546 int user = epoll_events[i].data.u32;
547 pollfd[user].revents = epoll_events[i].events;
550 /* read events from the pollfd array, as set_fd_events may modify them */
551 for (i = 0; i < ret; i++)
553 int user = epoll_events[i].data.u32;
554 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
558 /* fall through to normal poll loop */
559 #endif /* USE_EPOLL */
561 while (active_users)
563 timeout = get_next_timeout();
565 if (!active_users) break; /* last user removed by a timeout */
567 ret = poll( pollfd, nb_users, timeout );
568 if (ret > 0)
570 for (i = 0; i < nb_users; i++)
572 if (pollfd[i].revents)
574 fd_poll_event( poll_users[i], pollfd[i].revents );
575 if (!--ret) break;
583 /****************************************************************/
584 /* device functions */
586 static struct list device_hash[DEVICE_HASH_SIZE];
588 /* retrieve the device object for a given fd, creating it if needed */
589 static struct device *get_device( dev_t dev, int create )
591 struct device *device;
592 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
594 if (device_hash[hash].next)
596 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
597 if (device->dev == dev) return (struct device *)grab_object( device );
599 else list_init( &device_hash[hash] );
601 /* not found, create it */
603 if (!create) return NULL;
604 if ((device = alloc_object( &device_ops )))
606 device->dev = dev;
607 device->removable = -1;
608 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
609 list_add_head( &device_hash[hash], &device->entry );
611 return device;
614 static void device_dump( struct object *obj, int verbose )
616 struct device *device = (struct device *)obj;
617 fprintf( stderr, "Device dev=" );
618 DUMP_LONG_LONG( device->dev );
619 fprintf( stderr, "\n" );
622 static void device_destroy( struct object *obj )
624 struct device *device = (struct device *)obj;
625 unsigned int i;
627 for (i = 0; i < INODE_HASH_SIZE; i++)
628 assert( list_empty(&device->inode_hash[i]) );
630 list_remove( &device->entry ); /* remove it from the hash table */
634 /****************************************************************/
635 /* inode functions */
637 /* close all pending file descriptors in the closed list */
638 static void inode_close_pending( struct inode *inode, int keep_unlinks )
640 struct list *ptr = list_head( &inode->closed );
642 while (ptr)
644 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
645 struct list *next = list_next( &inode->closed, ptr );
647 if (fd->unix_fd != -1)
649 close( fd->unix_fd );
650 fd->unix_fd = -1;
652 if (!keep_unlinks || !fd->unlink[0]) /* get rid of it unless there's an unlink pending on that file */
654 list_remove( ptr );
655 free( fd );
657 ptr = next;
661 static void inode_dump( struct object *obj, int verbose )
663 struct inode *inode = (struct inode *)obj;
664 fprintf( stderr, "Inode device=%p ino=", inode->device );
665 DUMP_LONG_LONG( inode->ino );
666 fprintf( stderr, "\n" );
669 static void inode_destroy( struct object *obj )
671 struct inode *inode = (struct inode *)obj;
672 struct list *ptr;
674 assert( list_empty(&inode->open) );
675 assert( list_empty(&inode->locks) );
677 list_remove( &inode->entry );
679 while ((ptr = list_head( &inode->closed )))
681 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
682 list_remove( ptr );
683 if (fd->unix_fd != -1) close( fd->unix_fd );
684 if (fd->unlink[0])
686 /* make sure it is still the same file */
687 struct stat st;
688 if (!stat( fd->unlink, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
690 if (S_ISDIR(st.st_mode)) rmdir( fd->unlink );
691 else unlink( fd->unlink );
694 free( fd );
696 release_object( inode->device );
699 /* retrieve the inode object for a given fd, creating it if needed */
700 static struct inode *get_inode( dev_t dev, ino_t ino )
702 struct device *device;
703 struct inode *inode;
704 unsigned int hash = ino % INODE_HASH_SIZE;
706 if (!(device = get_device( dev, 1 ))) return NULL;
708 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
710 if (inode->ino == ino)
712 release_object( device );
713 return (struct inode *)grab_object( inode );
717 /* not found, create it */
718 if ((inode = alloc_object( &inode_ops )))
720 inode->device = device;
721 inode->ino = ino;
722 list_init( &inode->open );
723 list_init( &inode->locks );
724 list_init( &inode->closed );
725 list_add_head( &device->inode_hash[hash], &inode->entry );
727 else release_object( device );
729 return inode;
732 /* add fd to the inode list of file descriptors to close */
733 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
735 if (!list_empty( &inode->locks ))
737 list_add_head( &inode->closed, &fd->entry );
739 else if (fd->unlink[0]) /* close the fd but keep the structure around for unlink */
741 if (fd->unix_fd != -1) close( fd->unix_fd );
742 fd->unix_fd = -1;
743 list_add_head( &inode->closed, &fd->entry );
745 else /* no locks on this inode and no unlink, get rid of the fd */
747 if (fd->unix_fd != -1) close( fd->unix_fd );
748 free( fd );
753 /****************************************************************/
754 /* file lock functions */
756 static void file_lock_dump( struct object *obj, int verbose )
758 struct file_lock *lock = (struct file_lock *)obj;
759 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
760 lock->shared ? "shared" : "excl", lock->fd, lock->process );
761 DUMP_LONG_LONG( lock->start );
762 fprintf( stderr, " end=" );
763 DUMP_LONG_LONG( lock->end );
764 fprintf( stderr, "\n" );
767 static int file_lock_signaled( struct object *obj, struct thread *thread )
769 struct file_lock *lock = (struct file_lock *)obj;
770 /* lock is signaled if it has lost its owner */
771 return !lock->process;
774 /* set (or remove) a Unix lock if possible for the given range */
775 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
777 struct flock fl;
779 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
780 for (;;)
782 if (start == end) return 1; /* can't set zero-byte lock */
783 if (start > max_unix_offset) return 1; /* ignore it */
784 fl.l_type = type;
785 fl.l_whence = SEEK_SET;
786 fl.l_start = start;
787 if (!end || end > max_unix_offset) fl.l_len = 0;
788 else fl.l_len = end - start;
789 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
791 switch(errno)
793 case EACCES:
794 /* check whether locks work at all on this file system */
795 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
797 set_error( STATUS_FILE_LOCK_CONFLICT );
798 return 0;
800 /* fall through */
801 case EIO:
802 case ENOLCK:
803 /* no locking on this fs, just ignore it */
804 fd->fs_locks = 0;
805 return 1;
806 case EAGAIN:
807 set_error( STATUS_FILE_LOCK_CONFLICT );
808 return 0;
809 case EBADF:
810 /* this can happen if we try to set a write lock on a read-only file */
811 /* we just ignore that error */
812 if (fl.l_type == F_WRLCK) return 1;
813 set_error( STATUS_ACCESS_DENIED );
814 return 0;
815 #ifdef EOVERFLOW
816 case EOVERFLOW:
817 #endif
818 case EINVAL:
819 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
820 /* in that case we shrink the limit and retry */
821 if (max_unix_offset > INT_MAX)
823 max_unix_offset = INT_MAX;
824 break; /* retry */
826 /* fall through */
827 default:
828 file_set_error();
829 return 0;
834 /* check if interval [start;end) overlaps the lock */
835 inline static int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
837 if (lock->end && start >= lock->end) return 0;
838 if (end && lock->start >= end) return 0;
839 return 1;
842 /* remove Unix locks for all bytes in the specified area that are no longer locked */
843 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
845 struct hole
847 struct hole *next;
848 struct hole *prev;
849 file_pos_t start;
850 file_pos_t end;
851 } *first, *cur, *next, *buffer;
853 struct list *ptr;
854 int count = 0;
856 if (!fd->inode) return;
857 if (!fd->fs_locks) return;
858 if (start == end || start > max_unix_offset) return;
859 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
861 /* count the number of locks overlapping the specified area */
863 LIST_FOR_EACH( ptr, &fd->inode->locks )
865 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
866 if (lock->start == lock->end) continue;
867 if (lock_overlaps( lock, start, end )) count++;
870 if (!count) /* no locks at all, we can unlock everything */
872 set_unix_lock( fd, start, end, F_UNLCK );
873 return;
876 /* allocate space for the list of holes */
877 /* max. number of holes is number of locks + 1 */
879 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
880 first = buffer;
881 first->next = NULL;
882 first->prev = NULL;
883 first->start = start;
884 first->end = end;
885 next = first + 1;
887 /* build a sorted list of unlocked holes in the specified area */
889 LIST_FOR_EACH( ptr, &fd->inode->locks )
891 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
892 if (lock->start == lock->end) continue;
893 if (!lock_overlaps( lock, start, end )) continue;
895 /* go through all the holes touched by this lock */
896 for (cur = first; cur; cur = cur->next)
898 if (cur->end <= lock->start) continue; /* hole is before start of lock */
899 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
901 /* now we know that lock is overlapping hole */
903 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
905 cur->start = lock->end;
906 if (cur->start && cur->start < cur->end) break; /* done with this lock */
907 /* now hole is empty, remove it */
908 if (cur->next) cur->next->prev = cur->prev;
909 if (cur->prev) cur->prev->next = cur->next;
910 else if (!(first = cur->next)) goto done; /* no more holes at all */
912 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
914 cur->end = lock->start;
915 assert( cur->start < cur->end );
917 else /* lock is in the middle of hole, split hole in two */
919 next->prev = cur;
920 next->next = cur->next;
921 cur->next = next;
922 next->start = lock->end;
923 next->end = cur->end;
924 cur->end = lock->start;
925 assert( next->start < next->end );
926 assert( cur->end < next->start );
927 next++;
928 break; /* done with this lock */
933 /* clear Unix locks for all the holes */
935 for (cur = first; cur; cur = cur->next)
936 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
938 done:
939 free( buffer );
942 /* create a new lock on a fd */
943 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
945 struct file_lock *lock;
947 if (!fd->inode) /* not a regular file */
949 set_error( STATUS_INVALID_HANDLE );
950 return NULL;
953 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
954 lock->shared = shared;
955 lock->start = start;
956 lock->end = end;
957 lock->fd = fd;
958 lock->process = current->process;
960 /* now try to set a Unix lock */
961 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
963 release_object( lock );
964 return NULL;
966 list_add_head( &fd->locks, &lock->fd_entry );
967 list_add_head( &fd->inode->locks, &lock->inode_entry );
968 list_add_head( &lock->process->locks, &lock->proc_entry );
969 return lock;
972 /* remove an existing lock */
973 static void remove_lock( struct file_lock *lock, int remove_unix )
975 struct inode *inode = lock->fd->inode;
977 list_remove( &lock->fd_entry );
978 list_remove( &lock->inode_entry );
979 list_remove( &lock->proc_entry );
980 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
981 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
982 lock->process = NULL;
983 wake_up( &lock->obj, 0 );
984 release_object( lock );
987 /* remove all locks owned by a given process */
988 void remove_process_locks( struct process *process )
990 struct list *ptr;
992 while ((ptr = list_head( &process->locks )))
994 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
995 remove_lock( lock, 1 ); /* this removes it from the list */
999 /* remove all locks on a given fd */
1000 static void remove_fd_locks( struct fd *fd )
1002 file_pos_t start = FILE_POS_T_MAX, end = 0;
1003 struct list *ptr;
1005 while ((ptr = list_head( &fd->locks )))
1007 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1008 if (lock->start < start) start = lock->start;
1009 if (!lock->end || lock->end > end) end = lock->end - 1;
1010 remove_lock( lock, 0 );
1012 if (start < end) remove_unix_locks( fd, start, end + 1 );
1015 /* add a lock on an fd */
1016 /* returns handle to wait on */
1017 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1019 struct list *ptr;
1020 file_pos_t end = start + count;
1022 /* don't allow wrapping locks */
1023 if (end && end < start)
1025 set_error( STATUS_INVALID_PARAMETER );
1026 return 0;
1029 /* check if another lock on that file overlaps the area */
1030 LIST_FOR_EACH( ptr, &fd->inode->locks )
1032 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1033 if (!lock_overlaps( lock, start, end )) continue;
1034 if (lock->shared && shared) continue;
1035 /* found one */
1036 if (!wait)
1038 set_error( STATUS_FILE_LOCK_CONFLICT );
1039 return 0;
1041 set_error( STATUS_PENDING );
1042 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1045 /* not found, add it */
1046 if (add_lock( fd, shared, start, end )) return 0;
1047 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1049 /* Unix lock conflict -> tell client to wait and retry */
1050 if (wait) set_error( STATUS_PENDING );
1052 return 0;
1055 /* remove a lock on an fd */
1056 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1058 struct list *ptr;
1059 file_pos_t end = start + count;
1061 /* find an existing lock with the exact same parameters */
1062 LIST_FOR_EACH( ptr, &fd->locks )
1064 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1065 if ((lock->start == start) && (lock->end == end))
1067 remove_lock( lock, 1 );
1068 return;
1071 set_error( STATUS_FILE_LOCK_CONFLICT );
1075 /****************************************************************/
1076 /* asynchronous operations support */
1078 struct async
1080 struct thread *thread;
1081 void *apc;
1082 void *user;
1083 void *sb;
1084 struct timeout_user *timeout;
1085 struct list entry;
1088 /* notifies client thread of new status of its async request */
1089 /* destroys the server side of it */
1090 static void async_terminate( struct async *async, int status )
1092 thread_queue_apc( async->thread, NULL, async->apc, APC_ASYNC_IO,
1093 1, async->user, async->sb, (void *)status );
1095 if (async->timeout) remove_timeout_user( async->timeout );
1096 async->timeout = NULL;
1097 list_remove( &async->entry );
1098 release_object( async->thread );
1099 free( async );
1102 /* cb for timeout on an async request */
1103 static void async_callback(void *private)
1105 struct async *async = (struct async *)private;
1107 /* fprintf(stderr, "async timeout out %p\n", async); */
1108 async->timeout = NULL;
1109 async_terminate( async, STATUS_TIMEOUT );
1112 /* create an async on a given queue of a fd */
1113 struct async *create_async(struct thread *thread, int* timeout, struct list *queue,
1114 void *io_apc, void *io_user, void* io_sb)
1116 struct async *async = mem_alloc( sizeof(struct async) );
1118 if (!async) return NULL;
1120 async->thread = (struct thread *)grab_object(thread);
1121 async->apc = io_apc;
1122 async->user = io_user;
1123 async->sb = io_sb;
1125 list_add_tail( queue, &async->entry );
1127 if (timeout)
1129 struct timeval when;
1131 gettimeofday( &when, NULL );
1132 add_timeout( &when, *timeout );
1133 async->timeout = add_timeout_user( &when, async_callback, async );
1135 else async->timeout = NULL;
1137 return async;
1140 /* terminate the async operation at the head of the queue */
1141 void async_terminate_head( struct list *queue, int status )
1143 struct list *ptr = list_head( queue );
1144 if (ptr) async_terminate( LIST_ENTRY( ptr, struct async, entry ), status );
1147 /****************************************************************/
1148 /* file descriptor functions */
1150 static void fd_dump( struct object *obj, int verbose )
1152 struct fd *fd = (struct fd *)obj;
1153 fprintf( stderr, "Fd unix_fd=%d user=%p", fd->unix_fd, fd->user );
1154 if (fd->inode) fprintf( stderr, " inode=%p unlink='%s'", fd->inode, fd->closed->unlink );
1155 fprintf( stderr, "\n" );
1158 static void fd_destroy( struct object *obj )
1160 struct fd *fd = (struct fd *)obj;
1162 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1163 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1165 remove_fd_locks( fd );
1166 list_remove( &fd->inode_entry );
1167 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1168 if (fd->inode)
1170 inode_add_closed_fd( fd->inode, fd->closed );
1171 release_object( fd->inode );
1173 else /* no inode, close it right away */
1175 if (fd->unix_fd != -1) close( fd->unix_fd );
1179 /* set the events that select waits for on this fd */
1180 void set_fd_events( struct fd *fd, int events )
1182 int user = fd->poll_index;
1183 assert( poll_users[user] == fd );
1185 set_fd_epoll_events( fd, user, events );
1187 if (events == -1) /* stop waiting on this fd completely */
1189 pollfd[user].fd = -1;
1190 pollfd[user].events = POLLERR;
1191 pollfd[user].revents = 0;
1193 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1195 pollfd[user].fd = fd->unix_fd;
1196 pollfd[user].events = events;
1200 /* prepare an fd for unmounting its corresponding device */
1201 static inline void unmount_fd( struct fd *fd )
1203 assert( fd->inode );
1205 async_terminate_queue( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
1206 async_terminate_queue( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
1208 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1210 if (fd->unix_fd != -1) close( fd->unix_fd );
1212 fd->unix_fd = -1;
1213 fd->closed->unix_fd = -1;
1214 fd->closed->unlink[0] = 0;
1216 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1217 fd->fs_locks = 0;
1220 /* allocate an fd object, without setting the unix fd yet */
1221 struct fd *alloc_fd( const struct fd_ops *fd_user_ops, struct object *user )
1223 struct fd *fd = alloc_object( &fd_ops );
1225 if (!fd) return NULL;
1227 fd->fd_ops = fd_user_ops;
1228 fd->user = user;
1229 fd->inode = NULL;
1230 fd->closed = NULL;
1231 fd->access = 0;
1232 fd->sharing = 0;
1233 fd->unix_fd = -1;
1234 fd->fs_locks = 1;
1235 fd->poll_index = -1;
1236 list_init( &fd->inode_entry );
1237 list_init( &fd->locks );
1238 list_init( &fd->read_q );
1239 list_init( &fd->write_q );
1241 if ((fd->poll_index = add_poll_user( fd )) == -1)
1243 release_object( fd );
1244 return NULL;
1246 return fd;
1249 /* check if the desired access is possible without violating */
1250 /* the sharing mode of other opens of the same file */
1251 static int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing )
1253 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1254 unsigned int existing_access = 0;
1255 int unlink = 0;
1256 struct list *ptr;
1258 /* if access mode is 0, sharing mode is ignored */
1259 if (!access) sharing = existing_sharing;
1260 fd->access = access;
1261 fd->sharing = sharing;
1263 LIST_FOR_EACH( ptr, &fd->inode->open )
1265 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1266 if (fd_ptr != fd)
1268 existing_sharing &= fd_ptr->sharing;
1269 existing_access |= fd_ptr->access;
1270 if (fd_ptr->closed->unlink[0]) unlink = 1;
1274 if ((access & GENERIC_READ) && !(existing_sharing & FILE_SHARE_READ)) return 0;
1275 if ((access & GENERIC_WRITE) && !(existing_sharing & FILE_SHARE_WRITE)) return 0;
1276 if ((existing_access & GENERIC_READ) && !(sharing & FILE_SHARE_READ)) return 0;
1277 if ((existing_access & GENERIC_WRITE) && !(sharing & FILE_SHARE_WRITE)) return 0;
1278 if (fd->closed->unlink[0] && !(existing_sharing & FILE_SHARE_DELETE)) return 0;
1279 if (unlink && !(sharing & FILE_SHARE_DELETE)) return 0;
1280 return 1;
1283 /* open() wrapper using a struct fd */
1284 /* the fd must have been created with alloc_fd */
1285 /* on error the fd object is released */
1286 struct fd *open_fd( struct fd *fd, const char *name, int flags, mode_t *mode,
1287 unsigned int access, unsigned int sharing, unsigned int options )
1289 struct stat st;
1290 struct closed_fd *closed_fd;
1291 const char *unlink_name = "";
1293 assert( fd->unix_fd == -1 );
1295 if (options & FILE_DELETE_ON_CLOSE) unlink_name = name;
1296 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) + strlen(unlink_name) )))
1298 release_object( fd );
1299 return NULL;
1301 /* create the directory if needed */
1302 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1304 if (mkdir( name, 0777 ) == -1)
1306 if (errno != EEXIST || (flags & O_EXCL))
1308 file_set_error();
1309 release_object( fd );
1310 free( closed_fd );
1311 return NULL;
1314 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1316 if ((fd->unix_fd = open( name, flags & ~O_TRUNC, *mode )) == -1)
1318 file_set_error();
1319 release_object( fd );
1320 free( closed_fd );
1321 return NULL;
1323 closed_fd->unix_fd = fd->unix_fd;
1324 closed_fd->unlink[0] = 0;
1325 fstat( fd->unix_fd, &st );
1326 *mode = st.st_mode;
1328 /* only bother with an inode for normal files and directories */
1329 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1331 struct inode *inode = get_inode( st.st_dev, st.st_ino );
1333 if (!inode)
1335 /* we can close the fd because there are no others open on the same file,
1336 * otherwise we wouldn't have failed to allocate a new inode
1338 goto error;
1340 fd->inode = inode;
1341 fd->closed = closed_fd;
1342 list_add_head( &inode->open, &fd->inode_entry );
1344 /* check directory options */
1345 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1347 release_object( fd );
1348 set_error( STATUS_NOT_A_DIRECTORY );
1349 return NULL;
1351 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1353 release_object( fd );
1354 set_error( STATUS_FILE_IS_A_DIRECTORY );
1355 return NULL;
1357 if (!check_sharing( fd, access, sharing ))
1359 release_object( fd );
1360 set_error( STATUS_SHARING_VIOLATION );
1361 return NULL;
1363 strcpy( closed_fd->unlink, unlink_name );
1364 if (flags & O_TRUNC) ftruncate( fd->unix_fd, 0 );
1366 else /* special file */
1368 if (options & FILE_DIRECTORY_FILE)
1370 set_error( STATUS_NOT_A_DIRECTORY );
1371 goto error;
1373 if (unlink_name[0]) /* we can't unlink special files */
1375 set_error( STATUS_INVALID_PARAMETER );
1376 goto error;
1378 free( closed_fd );
1380 return fd;
1382 error:
1383 release_object( fd );
1384 free( closed_fd );
1385 return NULL;
1388 /* create an fd for an anonymous file */
1389 /* if the function fails the unix fd is closed */
1390 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user )
1392 struct fd *fd = alloc_fd( fd_user_ops, user );
1394 if (fd)
1396 fd->unix_fd = unix_fd;
1397 return fd;
1399 close( unix_fd );
1400 return NULL;
1403 /* retrieve the object that is using an fd */
1404 void *get_fd_user( struct fd *fd )
1406 return fd->user;
1409 /* retrieve the unix fd for an object */
1410 int get_unix_fd( struct fd *fd )
1412 if (fd->unix_fd == -1) set_error( STATUS_VOLUME_DISMOUNTED );
1413 return fd->unix_fd;
1416 /* check if two file descriptors point to the same file */
1417 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1419 return fd1->inode == fd2->inode;
1422 /* callback for event happening in the main poll() loop */
1423 void fd_poll_event( struct fd *fd, int event )
1425 return fd->fd_ops->poll_event( fd, event );
1428 /* check if events are pending and if yes return which one(s) */
1429 int check_fd_events( struct fd *fd, int events )
1431 struct pollfd pfd;
1433 if (fd->unix_fd == -1) return POLLERR;
1435 pfd.fd = fd->unix_fd;
1436 pfd.events = events;
1437 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1438 return pfd.revents;
1441 /* default add_queue() routine for objects that poll() on an fd */
1442 int default_fd_add_queue( struct object *obj, struct wait_queue_entry *entry )
1444 struct fd *fd = get_obj_fd( obj );
1446 if (!fd) return 0;
1447 if (list_empty( &obj->wait_queue )) /* first on the queue */
1448 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1449 add_queue( obj, entry );
1450 release_object( fd );
1451 return 1;
1454 /* default remove_queue() routine for objects that poll() on an fd */
1455 void default_fd_remove_queue( struct object *obj, struct wait_queue_entry *entry )
1457 struct fd *fd = get_obj_fd( obj );
1459 grab_object( obj );
1460 remove_queue( obj, entry );
1461 if (list_empty( &obj->wait_queue )) /* last on the queue is gone */
1462 set_fd_events( fd, 0 );
1463 release_object( obj );
1464 release_object( fd );
1467 /* default signaled() routine for objects that poll() on an fd */
1468 int default_fd_signaled( struct object *obj, struct thread *thread )
1470 int events, ret;
1471 struct fd *fd = get_obj_fd( obj );
1473 if (fd->inode) return 1; /* regular files are always signaled */
1475 events = fd->fd_ops->get_poll_events( fd );
1476 ret = check_fd_events( fd, events ) != 0;
1478 if (ret)
1479 set_fd_events( fd, 0 ); /* stop waiting on select() if we are signaled */
1480 else if (!list_empty( &obj->wait_queue ))
1481 set_fd_events( fd, events ); /* restart waiting on poll() if we are no longer signaled */
1483 release_object( fd );
1484 return ret;
1487 int default_fd_get_poll_events( struct fd *fd )
1489 int events = 0;
1491 if (!list_empty( &fd->read_q ))
1492 events |= POLLIN;
1493 if (!list_empty( &fd->write_q ))
1494 events |= POLLOUT;
1496 return events;
1499 /* default handler for poll() events */
1500 void default_poll_event( struct fd *fd, int event )
1502 if (!list_empty( &fd->read_q ) && (POLLIN & event) )
1504 async_terminate_head( &fd->read_q, STATUS_ALERTED );
1505 return;
1507 if (!list_empty( &fd->write_q ) && (POLLOUT & event) )
1509 async_terminate_head( &fd->write_q, STATUS_ALERTED );
1510 return;
1513 /* if an error occurred, stop polling this fd to avoid busy-looping */
1514 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
1515 wake_up( fd->user, 0 );
1518 void fd_queue_async_timeout( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count, int *timeout )
1520 struct list *queue;
1521 int events;
1523 if (!(fd->fd_ops->get_file_info( fd ) & (FD_FLAG_OVERLAPPED|FD_FLAG_TIMEOUT)))
1525 set_error( STATUS_INVALID_HANDLE );
1526 return;
1529 switch (type)
1531 case ASYNC_TYPE_READ:
1532 queue = &fd->read_q;
1533 break;
1534 case ASYNC_TYPE_WRITE:
1535 queue = &fd->write_q;
1536 break;
1537 default:
1538 set_error( STATUS_INVALID_PARAMETER );
1539 return;
1542 if (!create_async( current, timeout, queue, apc, user, io_sb ))
1543 return;
1545 /* Check if the new pending request can be served immediately */
1546 events = check_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1547 if (events) fd->fd_ops->poll_event( fd, events );
1549 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
1552 void default_fd_queue_async( struct fd *fd, void *apc, void *user, void *io_sb, int type, int count )
1554 fd_queue_async_timeout( fd, apc, user, io_sb, type, count, NULL );
1557 void default_fd_cancel_async( struct fd *fd )
1559 async_terminate_queue( &fd->read_q, STATUS_CANCELLED );
1560 async_terminate_queue( &fd->write_q, STATUS_CANCELLED );
1563 /* default flush() routine */
1564 int no_flush( struct fd *fd, struct event **event )
1566 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1567 return 0;
1570 /* default get_file_info() routine */
1571 int no_get_file_info( struct fd *fd )
1573 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1574 return 0;
1577 /* default queue_async() routine */
1578 void no_queue_async( struct fd *fd, void* apc, void* user, void* io_sb,
1579 int type, int count)
1581 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1584 /* default cancel_async() routine */
1585 void no_cancel_async( struct fd *fd )
1587 set_error( STATUS_OBJECT_TYPE_MISMATCH );
1590 /* close all Unix file descriptors on a device to allow unmounting it */
1591 static void unmount_device( struct fd *device_fd )
1593 unsigned int i;
1594 struct stat st;
1595 struct device *device;
1596 struct inode *inode;
1597 struct fd *fd;
1599 if (device_fd->unix_fd == -1 || fstat( device_fd->unix_fd, &st ) == -1 || !S_ISBLK( st.st_mode ))
1601 set_error( STATUS_INVALID_PARAMETER );
1602 return;
1605 if (!(device = get_device( st.st_rdev, 0 ))) return;
1607 for (i = 0; i < INODE_HASH_SIZE; i++)
1609 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
1611 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
1613 unmount_fd( fd );
1615 inode_close_pending( inode, 0 );
1618 /* remove it from the hash table */
1619 list_remove( &device->entry );
1620 list_init( &device->entry );
1621 release_object( device );
1624 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1625 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
1626 unsigned int access )
1628 struct fd *fd = NULL;
1629 struct object *obj;
1631 if ((obj = get_handle_obj( process, handle, access, NULL )))
1633 fd = get_obj_fd( obj );
1634 release_object( obj );
1636 return fd;
1639 /* flush a file buffers */
1640 DECL_HANDLER(flush_file)
1642 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1643 struct event * event = NULL;
1645 if (fd)
1647 fd->fd_ops->flush( fd, &event );
1648 if ( event )
1650 reply->event = alloc_handle( current->process, event, SYNCHRONIZE, 0 );
1652 release_object( fd );
1656 /* get a Unix fd to access a file */
1657 DECL_HANDLER(get_handle_fd)
1659 struct fd *fd;
1661 reply->fd = -1;
1663 if ((fd = get_handle_fd_obj( current->process, req->handle, req->access )))
1665 int unix_fd = get_unix_fd( fd );
1666 if (unix_fd != -1)
1668 int cached_fd = get_handle_unix_fd( current->process, req->handle, req->access );
1669 if (cached_fd != -1) reply->fd = cached_fd;
1670 else if (!get_error()) send_client_fd( current->process, unix_fd, req->handle );
1672 if (fd->inode) reply->removable = fd->inode->device->removable;
1673 reply->flags = fd->fd_ops->get_file_info( fd );
1674 release_object( fd );
1678 /* set the cached file descriptor of a handle */
1679 DECL_HANDLER(set_handle_fd)
1681 struct fd *fd;
1683 reply->cur_fd = -1;
1684 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1686 struct device *device = fd->inode ? fd->inode->device : NULL;
1688 if (device && device->removable == -1) device->removable = req->removable;
1690 /* only cache the fd on non-removable devices */
1691 if (!device || !device->removable)
1692 reply->cur_fd = set_handle_unix_fd( current->process, req->handle, req->fd );
1693 release_object( fd );
1697 /* get ready to unmount a Unix device */
1698 DECL_HANDLER(unmount_device)
1700 struct fd *fd;
1702 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
1704 unmount_device( fd );
1705 release_object( fd );
1709 /* create / reschedule an async I/O */
1710 DECL_HANDLER(register_async)
1712 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1715 * The queue_async method must do the following:
1717 * 1. Get the async_queue for the request of given type.
1718 * 2. Create a new asynchronous request for the selected queue
1719 * 3. Carry out any operations necessary to adjust the object's poll events
1720 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1721 * 4. When the async request is triggered, then send back (with a proper APC)
1722 * the trigger (STATUS_ALERTED) to the thread that posted the request.
1723 * async_destroy() is to be called: it will both notify the sender about
1724 * the trigger and destroy the request by itself
1725 * See also the implementations in file.c, serial.c, and sock.c.
1728 if (fd)
1730 fd->fd_ops->queue_async( fd, req->io_apc, req->io_user, req->io_sb,
1731 req->type, req->count );
1732 release_object( fd );
1736 /* cancels all async I/O */
1737 DECL_HANDLER(cancel_async)
1739 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
1740 if (fd)
1742 /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1743 * NtCancelIoFile() will force the pending APC to be run. Since,
1744 * Windows only guarantees that the current thread will have no async
1745 * operation on the current fd when NtCancelIoFile returns, this shall
1746 * do the work.
1748 fd->fd_ops->cancel_async( fd );
1749 release_object( fd );