msado15: Implement Dispatch functions in Field.
[wine.git] / server / fd.c
blob65a6f876e5cf80c1e174b5638f0d5a4359545c44
1 /*
2 * Server-side file descriptor management
4 * Copyright (C) 2000, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <signal.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #ifdef HAVE_POLL_H
36 #include <poll.h>
37 #endif
38 #ifdef HAVE_SYS_POLL_H
39 #include <sys/poll.h>
40 #endif
41 #ifdef HAVE_LINUX_MAJOR_H
42 #include <linux/major.h>
43 #endif
44 #ifdef HAVE_SYS_STATVFS_H
45 #include <sys/statvfs.h>
46 #endif
47 #ifdef HAVE_SYS_VFS_H
48 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
49 #define list SYSLIST
50 #define list_next SYSLIST_NEXT
51 #define list_prev SYSLIST_PREV
52 #define list_head SYSLIST_HEAD
53 #define list_tail SYSLIST_TAIL
54 #define list_move_tail SYSLIST_MOVE_TAIL
55 #define list_remove SYSLIST_REMOVE
56 #include <sys/vfs.h>
57 #undef list
58 #undef list_next
59 #undef list_prev
60 #undef list_head
61 #undef list_tail
62 #undef list_move_tail
63 #undef list_remove
64 #endif
65 #ifdef HAVE_SYS_PARAM_H
66 #include <sys/param.h>
67 #endif
68 #ifdef HAVE_SYS_MOUNT_H
69 #include <sys/mount.h>
70 #endif
71 #ifdef HAVE_SYS_STATFS_H
72 #include <sys/statfs.h>
73 #endif
74 #ifdef HAVE_SYS_SYSCTL_H
75 #include <sys/sysctl.h>
76 #endif
77 #ifdef HAVE_SYS_EVENT_H
78 #include <sys/event.h>
79 #undef LIST_INIT
80 #undef LIST_ENTRY
81 #endif
82 #ifdef HAVE_STDINT_H
83 #include <stdint.h>
84 #endif
85 #include <sys/stat.h>
86 #include <sys/time.h>
87 #ifdef MAJOR_IN_MKDEV
88 #include <sys/mkdev.h>
89 #elif defined(MAJOR_IN_SYSMACROS)
90 #include <sys/sysmacros.h>
91 #endif
92 #include <sys/types.h>
93 #include <unistd.h>
94 #ifdef HAVE_SYS_SYSCALL_H
95 #include <sys/syscall.h>
96 #endif
98 #include "ntstatus.h"
99 #define WIN32_NO_STATUS
100 #include "object.h"
101 #include "file.h"
102 #include "handle.h"
103 #include "process.h"
104 #include "request.h"
106 #include "winternl.h"
107 #include "winioctl.h"
108 #include "ddk/wdm.h"
110 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
111 # include <sys/epoll.h>
112 # define USE_EPOLL
113 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
114 # define USE_EPOLL
115 # define EPOLLIN POLLIN
116 # define EPOLLOUT POLLOUT
117 # define EPOLLERR POLLERR
118 # define EPOLLHUP POLLHUP
119 # define EPOLL_CTL_ADD 1
120 # define EPOLL_CTL_DEL 2
121 # define EPOLL_CTL_MOD 3
123 typedef union epoll_data
125 void *ptr;
126 int fd;
127 uint32_t u32;
128 uint64_t u64;
129 } epoll_data_t;
131 struct epoll_event
133 uint32_t events;
134 epoll_data_t data;
137 static inline int epoll_create( int size )
139 return syscall( 254 /*NR_epoll_create*/, size );
142 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
144 return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
147 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
149 return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
152 #endif /* linux && __i386__ && HAVE_STDINT_H */
154 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
155 # include <port.h>
156 # define USE_EVENT_PORTS
157 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
159 /* Because of the stupid Posix locking semantics, we need to keep
160 * track of all file descriptors referencing a given file, and not
161 * close a single one until all the locks are gone (sigh).
164 /* file descriptor object */
166 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
167 struct closed_fd
169 struct list entry; /* entry in inode closed list */
170 int unix_fd; /* the unix file descriptor */
171 int unlink; /* whether to unlink on close: -1 - implicit FILE_DELETE_ON_CLOSE, 1 - explicit disposition */
172 char *unix_name; /* name to unlink on close, points to parent fd unix_name */
175 struct fd
177 struct object obj; /* object header */
178 const struct fd_ops *fd_ops; /* file descriptor operations */
179 struct inode *inode; /* inode that this fd belongs to */
180 struct list inode_entry; /* entry in inode fd list */
181 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
182 struct object *user; /* object using this file descriptor */
183 struct list locks; /* list of locks on this fd */
184 unsigned int access; /* file access (FILE_READ_DATA etc.) */
185 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
186 unsigned int sharing; /* file sharing mode */
187 char *unix_name; /* unix file name */
188 WCHAR *nt_name; /* NT file name */
189 data_size_t nt_namelen; /* length of NT file name */
190 int unix_fd; /* unix file descriptor */
191 unsigned int no_fd_status;/* status to return when unix_fd is -1 */
192 unsigned int cacheable :1;/* can the fd be cached on the client side? */
193 unsigned int signaled :1; /* is the fd signaled? */
194 unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
195 int poll_index; /* index of fd in poll array */
196 struct async_queue read_q; /* async readers of this fd */
197 struct async_queue write_q; /* async writers of this fd */
198 struct async_queue wait_q; /* other async waiters of this fd */
199 struct completion *completion; /* completion object attached to this fd */
200 apc_param_t comp_key; /* completion key to set in completion events */
201 unsigned int comp_flags; /* completion flags */
204 static void fd_dump( struct object *obj, int verbose );
205 static void fd_destroy( struct object *obj );
207 static const struct object_ops fd_ops =
209 sizeof(struct fd), /* size */
210 &no_type, /* type */
211 fd_dump, /* dump */
212 no_add_queue, /* add_queue */
213 NULL, /* remove_queue */
214 NULL, /* signaled */
215 NULL, /* satisfied */
216 no_signal, /* signal */
217 no_get_fd, /* get_fd */
218 default_map_access, /* map_access */
219 default_get_sd, /* get_sd */
220 default_set_sd, /* set_sd */
221 no_get_full_name, /* get_full_name */
222 no_lookup_name, /* lookup_name */
223 no_link_name, /* link_name */
224 NULL, /* unlink_name */
225 no_open_file, /* open_file */
226 no_kernel_obj_list, /* get_kernel_obj_list */
227 no_close_handle, /* close_handle */
228 fd_destroy /* destroy */
231 /* device object */
233 #define DEVICE_HASH_SIZE 7
234 #define INODE_HASH_SIZE 17
236 struct device
238 struct object obj; /* object header */
239 struct list entry; /* entry in device hash list */
240 dev_t dev; /* device number */
241 int removable; /* removable device? (or -1 if unknown) */
242 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
245 static void device_dump( struct object *obj, int verbose );
246 static void device_destroy( struct object *obj );
248 static const struct object_ops device_ops =
250 sizeof(struct device), /* size */
251 &no_type, /* type */
252 device_dump, /* dump */
253 no_add_queue, /* add_queue */
254 NULL, /* remove_queue */
255 NULL, /* signaled */
256 NULL, /* satisfied */
257 no_signal, /* signal */
258 no_get_fd, /* get_fd */
259 default_map_access, /* map_access */
260 default_get_sd, /* get_sd */
261 default_set_sd, /* set_sd */
262 no_get_full_name, /* get_full_name */
263 no_lookup_name, /* lookup_name */
264 no_link_name, /* link_name */
265 NULL, /* unlink_name */
266 no_open_file, /* open_file */
267 no_kernel_obj_list, /* get_kernel_obj_list */
268 no_close_handle, /* close_handle */
269 device_destroy /* destroy */
272 /* inode object */
274 struct inode
276 struct object obj; /* object header */
277 struct list entry; /* inode hash list entry */
278 struct device *device; /* device containing this inode */
279 ino_t ino; /* inode number */
280 struct list open; /* list of open file descriptors */
281 struct list locks; /* list of file locks */
282 struct list closed; /* list of file descriptors to close at destroy time */
285 static void inode_dump( struct object *obj, int verbose );
286 static void inode_destroy( struct object *obj );
288 static const struct object_ops inode_ops =
290 sizeof(struct inode), /* size */
291 &no_type, /* type */
292 inode_dump, /* dump */
293 no_add_queue, /* add_queue */
294 NULL, /* remove_queue */
295 NULL, /* signaled */
296 NULL, /* satisfied */
297 no_signal, /* signal */
298 no_get_fd, /* get_fd */
299 default_map_access, /* map_access */
300 default_get_sd, /* get_sd */
301 default_set_sd, /* set_sd */
302 no_get_full_name, /* get_full_name */
303 no_lookup_name, /* lookup_name */
304 no_link_name, /* link_name */
305 NULL, /* unlink_name */
306 no_open_file, /* open_file */
307 no_kernel_obj_list, /* get_kernel_obj_list */
308 no_close_handle, /* close_handle */
309 inode_destroy /* destroy */
312 /* file lock object */
314 struct file_lock
316 struct object obj; /* object header */
317 struct fd *fd; /* fd owning this lock */
318 struct list fd_entry; /* entry in list of locks on a given fd */
319 struct list inode_entry; /* entry in inode list of locks */
320 int shared; /* shared lock? */
321 file_pos_t start; /* locked region is interval [start;end) */
322 file_pos_t end;
323 struct process *process; /* process owning this lock */
324 struct list proc_entry; /* entry in list of locks owned by the process */
327 static void file_lock_dump( struct object *obj, int verbose );
328 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
330 static const struct object_ops file_lock_ops =
332 sizeof(struct file_lock), /* size */
333 &no_type, /* type */
334 file_lock_dump, /* dump */
335 add_queue, /* add_queue */
336 remove_queue, /* remove_queue */
337 file_lock_signaled, /* signaled */
338 no_satisfied, /* satisfied */
339 no_signal, /* signal */
340 no_get_fd, /* get_fd */
341 default_map_access, /* map_access */
342 default_get_sd, /* get_sd */
343 default_set_sd, /* set_sd */
344 no_get_full_name, /* get_full_name */
345 no_lookup_name, /* lookup_name */
346 no_link_name, /* link_name */
347 NULL, /* unlink_name */
348 no_open_file, /* open_file */
349 no_kernel_obj_list, /* get_kernel_obj_list */
350 no_close_handle, /* close_handle */
351 no_destroy /* destroy */
355 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
356 #define FILE_POS_T_MAX (~(file_pos_t)0)
358 static file_pos_t max_unix_offset = OFF_T_MAX;
360 #define DUMP_LONG_LONG(val) do { \
361 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
362 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
363 else \
364 fprintf( stderr, "%lx", (unsigned long)(val) ); \
365 } while (0)
369 /****************************************************************/
370 /* timeouts support */
372 struct timeout_user
374 struct list entry; /* entry in sorted timeout list */
375 abstime_t when; /* timeout expiry */
376 timeout_callback callback; /* callback function */
377 void *private; /* callback private data */
380 static struct list abs_timeout_list = LIST_INIT(abs_timeout_list); /* sorted absolute timeouts list */
381 static struct list rel_timeout_list = LIST_INIT(rel_timeout_list); /* sorted relative timeouts list */
382 timeout_t current_time;
383 timeout_t monotonic_time;
385 struct _KUSER_SHARED_DATA *user_shared_data = NULL;
386 static const int user_shared_data_timeout = 16;
388 static void set_user_shared_data_time(void)
390 timeout_t tick_count = monotonic_time / 10000;
392 /* on X86 there should be total store order guarantees, so volatile is enough
393 * to ensure the stores aren't reordered by the compiler, and then they will
394 * always be seen in-order from other CPUs. On other archs, we need atomic
395 * intrinsics to guarantee that. */
396 #if defined(__i386__) || defined(__x86_64__)
397 user_shared_data->SystemTime.High2Time = current_time >> 32;
398 user_shared_data->SystemTime.LowPart = current_time;
399 user_shared_data->SystemTime.High1Time = current_time >> 32;
401 user_shared_data->InterruptTime.High2Time = monotonic_time >> 32;
402 user_shared_data->InterruptTime.LowPart = monotonic_time;
403 user_shared_data->InterruptTime.High1Time = monotonic_time >> 32;
405 user_shared_data->TickCount.High2Time = tick_count >> 32;
406 user_shared_data->TickCount.LowPart = tick_count;
407 user_shared_data->TickCount.High1Time = tick_count >> 32;
408 *(volatile ULONG *)&user_shared_data->TickCountLowDeprecated = tick_count;
409 #else
410 __atomic_store_n(&user_shared_data->SystemTime.High2Time, current_time >> 32, __ATOMIC_SEQ_CST);
411 __atomic_store_n(&user_shared_data->SystemTime.LowPart, current_time, __ATOMIC_SEQ_CST);
412 __atomic_store_n(&user_shared_data->SystemTime.High1Time, current_time >> 32, __ATOMIC_SEQ_CST);
414 __atomic_store_n(&user_shared_data->InterruptTime.High2Time, monotonic_time >> 32, __ATOMIC_SEQ_CST);
415 __atomic_store_n(&user_shared_data->InterruptTime.LowPart, monotonic_time, __ATOMIC_SEQ_CST);
416 __atomic_store_n(&user_shared_data->InterruptTime.High1Time, monotonic_time >> 32, __ATOMIC_SEQ_CST);
418 __atomic_store_n(&user_shared_data->TickCount.High2Time, tick_count >> 32, __ATOMIC_SEQ_CST);
419 __atomic_store_n(&user_shared_data->TickCount.LowPart, tick_count, __ATOMIC_SEQ_CST);
420 __atomic_store_n(&user_shared_data->TickCount.High1Time, tick_count >> 32, __ATOMIC_SEQ_CST);
421 __atomic_store_n(&user_shared_data->TickCountLowDeprecated, tick_count, __ATOMIC_SEQ_CST);
422 #endif
425 void set_current_time(void)
427 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
428 struct timeval now;
429 gettimeofday( &now, NULL );
430 current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
431 monotonic_time = monotonic_counter();
432 if (user_shared_data) set_user_shared_data_time();
435 /* add a timeout user */
436 struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
438 struct timeout_user *user;
439 struct list *ptr;
441 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
442 user->when = timeout_to_abstime( when );
443 user->callback = func;
444 user->private = private;
446 /* Now insert it in the linked list */
448 if (user->when > 0)
450 LIST_FOR_EACH( ptr, &abs_timeout_list )
452 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
453 if (timeout->when >= user->when) break;
456 else
458 LIST_FOR_EACH( ptr, &rel_timeout_list )
460 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
461 if (timeout->when <= user->when) break;
464 list_add_before( ptr, &user->entry );
465 return user;
468 /* remove a timeout user */
469 void remove_timeout_user( struct timeout_user *user )
471 list_remove( &user->entry );
472 free( user );
475 /* return a text description of a timeout for debugging purposes */
476 const char *get_timeout_str( timeout_t timeout )
478 static char buffer[64];
479 long secs, nsecs;
481 if (!timeout) return "0";
482 if (timeout == TIMEOUT_INFINITE) return "infinite";
484 if (timeout < 0) /* relative */
486 secs = -timeout / TICKS_PER_SEC;
487 nsecs = -timeout % TICKS_PER_SEC;
488 sprintf( buffer, "+%ld.%07ld", secs, nsecs );
490 else /* absolute */
492 secs = (timeout - current_time) / TICKS_PER_SEC;
493 nsecs = (timeout - current_time) % TICKS_PER_SEC;
494 if (nsecs < 0)
496 nsecs += TICKS_PER_SEC;
497 secs--;
499 if (secs >= 0)
500 sprintf( buffer, "%x%08x (+%ld.%07ld)",
501 (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
502 else
503 sprintf( buffer, "%x%08x (-%ld.%07ld)",
504 (unsigned int)(timeout >> 32), (unsigned int)timeout,
505 -(secs + 1), TICKS_PER_SEC - nsecs );
507 return buffer;
511 /****************************************************************/
512 /* poll support */
514 static struct fd **poll_users; /* users array */
515 static struct pollfd *pollfd; /* poll fd array */
516 static int nb_users; /* count of array entries actually in use */
517 static int active_users; /* current number of active users */
518 static int allocated_users; /* count of allocated entries in the array */
519 static struct fd **freelist; /* list of free entries in the array */
521 static int get_next_timeout(void);
523 static inline void fd_poll_event( struct fd *fd, int event )
525 fd->fd_ops->poll_event( fd, event );
528 #ifdef USE_EPOLL
530 static int epoll_fd = -1;
532 static inline void init_epoll(void)
534 epoll_fd = epoll_create( 128 );
537 /* set the events that epoll waits for on this fd; helper for set_fd_events */
538 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
540 struct epoll_event ev;
541 int ctl;
543 if (epoll_fd == -1) return;
545 if (events == -1) /* stop waiting on this fd completely */
547 if (pollfd[user].fd == -1) return; /* already removed */
548 ctl = EPOLL_CTL_DEL;
550 else if (pollfd[user].fd == -1)
552 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
553 ctl = EPOLL_CTL_ADD;
555 else
557 if (pollfd[user].events == events) return; /* nothing to do */
558 ctl = EPOLL_CTL_MOD;
561 ev.events = events;
562 memset(&ev.data, 0, sizeof(ev.data));
563 ev.data.u32 = user;
565 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
567 if (errno == ENOMEM) /* not enough memory, give up on epoll */
569 close( epoll_fd );
570 epoll_fd = -1;
572 else perror( "epoll_ctl" ); /* should not happen */
576 static inline void remove_epoll_user( struct fd *fd, int user )
578 if (epoll_fd == -1) return;
580 if (pollfd[user].fd != -1)
582 struct epoll_event dummy;
583 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
587 static inline void main_loop_epoll(void)
589 int i, ret, timeout;
590 struct epoll_event events[128];
592 assert( POLLIN == EPOLLIN );
593 assert( POLLOUT == EPOLLOUT );
594 assert( POLLERR == EPOLLERR );
595 assert( POLLHUP == EPOLLHUP );
597 if (epoll_fd == -1) return;
599 while (active_users)
601 timeout = get_next_timeout();
603 if (!active_users) break; /* last user removed by a timeout */
604 if (epoll_fd == -1) break; /* an error occurred with epoll */
606 ret = epoll_wait( epoll_fd, events, ARRAY_SIZE( events ), timeout );
607 set_current_time();
609 /* put the events into the pollfd array first, like poll does */
610 for (i = 0; i < ret; i++)
612 int user = events[i].data.u32;
613 pollfd[user].revents = events[i].events;
616 /* read events from the pollfd array, as set_fd_events may modify them */
617 for (i = 0; i < ret; i++)
619 int user = events[i].data.u32;
620 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
625 #elif defined(HAVE_KQUEUE)
627 static int kqueue_fd = -1;
629 static inline void init_epoll(void)
631 #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
632 int mib[2];
633 char release[32];
634 size_t len = sizeof(release);
636 mib[0] = CTL_KERN;
637 mib[1] = KERN_OSRELEASE;
638 if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
639 if (atoi(release) < 9) return;
640 #endif
641 kqueue_fd = kqueue();
644 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
646 struct kevent ev[2];
648 if (kqueue_fd == -1) return;
650 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)(long)user );
651 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)(long)user );
653 if (events == -1) /* stop waiting on this fd completely */
655 if (pollfd[user].fd == -1) return; /* already removed */
656 ev[0].flags |= EV_DELETE;
657 ev[1].flags |= EV_DELETE;
659 else if (pollfd[user].fd == -1)
661 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
662 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
663 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
665 else
667 if (pollfd[user].events == events) return; /* nothing to do */
668 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
669 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
672 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
674 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
676 close( kqueue_fd );
677 kqueue_fd = -1;
679 else perror( "kevent" ); /* should not happen */
683 static inline void remove_epoll_user( struct fd *fd, int user )
685 if (kqueue_fd == -1) return;
687 if (pollfd[user].fd != -1)
689 struct kevent ev[2];
691 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
692 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
693 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
697 static inline void main_loop_epoll(void)
699 int i, ret, timeout;
700 struct kevent events[128];
702 if (kqueue_fd == -1) return;
704 while (active_users)
706 timeout = get_next_timeout();
708 if (!active_users) break; /* last user removed by a timeout */
709 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
711 if (timeout != -1)
713 struct timespec ts;
715 ts.tv_sec = timeout / 1000;
716 ts.tv_nsec = (timeout % 1000) * 1000000;
717 ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), &ts );
719 else ret = kevent( kqueue_fd, NULL, 0, events, ARRAY_SIZE( events ), NULL );
721 set_current_time();
723 /* put the events into the pollfd array first, like poll does */
724 for (i = 0; i < ret; i++)
726 long user = (long)events[i].udata;
727 pollfd[user].revents = 0;
729 for (i = 0; i < ret; i++)
731 long user = (long)events[i].udata;
732 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
733 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
734 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
735 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
738 /* read events from the pollfd array, as set_fd_events may modify them */
739 for (i = 0; i < ret; i++)
741 long user = (long)events[i].udata;
742 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
743 pollfd[user].revents = 0;
748 #elif defined(USE_EVENT_PORTS)
750 static int port_fd = -1;
752 static inline void init_epoll(void)
754 port_fd = port_create();
757 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
759 int ret;
761 if (port_fd == -1) return;
763 if (events == -1) /* stop waiting on this fd completely */
765 if (pollfd[user].fd == -1) return; /* already removed */
766 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
768 else if (pollfd[user].fd == -1)
770 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
771 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
773 else
775 if (pollfd[user].events == events) return; /* nothing to do */
776 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
779 if (ret == -1)
781 if (errno == ENOMEM) /* not enough memory, give up on port_associate */
783 close( port_fd );
784 port_fd = -1;
786 else perror( "port_associate" ); /* should not happen */
790 static inline void remove_epoll_user( struct fd *fd, int user )
792 if (port_fd == -1) return;
794 if (pollfd[user].fd != -1)
796 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
800 static inline void main_loop_epoll(void)
802 int i, nget, ret, timeout;
803 port_event_t events[128];
805 if (port_fd == -1) return;
807 while (active_users)
809 timeout = get_next_timeout();
810 nget = 1;
812 if (!active_users) break; /* last user removed by a timeout */
813 if (port_fd == -1) break; /* an error occurred with event completion */
815 if (timeout != -1)
817 struct timespec ts;
819 ts.tv_sec = timeout / 1000;
820 ts.tv_nsec = (timeout % 1000) * 1000000;
821 ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, &ts );
823 else ret = port_getn( port_fd, events, ARRAY_SIZE( events ), &nget, NULL );
825 if (ret == -1) break; /* an error occurred with event completion */
827 set_current_time();
829 /* put the events into the pollfd array first, like poll does */
830 for (i = 0; i < nget; i++)
832 long user = (long)events[i].portev_user;
833 pollfd[user].revents = events[i].portev_events;
836 /* read events from the pollfd array, as set_fd_events may modify them */
837 for (i = 0; i < nget; i++)
839 long user = (long)events[i].portev_user;
840 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
841 /* if we are still interested, reassociate the fd */
842 if (pollfd[user].fd != -1) {
843 port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
849 #else /* HAVE_KQUEUE */
851 static inline void init_epoll(void) { }
852 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
853 static inline void remove_epoll_user( struct fd *fd, int user ) { }
854 static inline void main_loop_epoll(void) { }
856 #endif /* USE_EPOLL */
859 /* add a user in the poll array and return its index, or -1 on failure */
860 static int add_poll_user( struct fd *fd )
862 int ret;
863 if (freelist)
865 ret = freelist - poll_users;
866 freelist = (struct fd **)poll_users[ret];
868 else
870 if (nb_users == allocated_users)
872 struct fd **newusers;
873 struct pollfd *newpoll;
874 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
875 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
876 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
878 if (allocated_users)
879 poll_users = newusers;
880 else
881 free( newusers );
882 return -1;
884 poll_users = newusers;
885 pollfd = newpoll;
886 if (!allocated_users) init_epoll();
887 allocated_users = new_count;
889 ret = nb_users++;
891 pollfd[ret].fd = -1;
892 pollfd[ret].events = 0;
893 pollfd[ret].revents = 0;
894 poll_users[ret] = fd;
895 active_users++;
896 return ret;
899 /* remove a user from the poll list */
900 static void remove_poll_user( struct fd *fd, int user )
902 assert( user >= 0 );
903 assert( poll_users[user] == fd );
905 remove_epoll_user( fd, user );
906 pollfd[user].fd = -1;
907 pollfd[user].events = 0;
908 pollfd[user].revents = 0;
909 poll_users[user] = (struct fd *)freelist;
910 freelist = &poll_users[user];
911 active_users--;
914 /* process pending timeouts and return the time until the next timeout, in milliseconds */
915 static int get_next_timeout(void)
917 int ret = user_shared_data ? user_shared_data_timeout : -1;
919 if (!list_empty( &abs_timeout_list ) || !list_empty( &rel_timeout_list ))
921 struct list expired_list, *ptr;
923 /* first remove all expired timers from the list */
925 list_init( &expired_list );
926 while ((ptr = list_head( &abs_timeout_list )) != NULL)
928 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
930 if (timeout->when <= current_time)
932 list_remove( &timeout->entry );
933 list_add_tail( &expired_list, &timeout->entry );
935 else break;
937 while ((ptr = list_head( &rel_timeout_list )) != NULL)
939 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
941 if (-timeout->when <= monotonic_time)
943 list_remove( &timeout->entry );
944 list_add_tail( &expired_list, &timeout->entry );
946 else break;
949 /* now call the callback for all the removed timers */
951 while ((ptr = list_head( &expired_list )) != NULL)
953 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
954 list_remove( &timeout->entry );
955 timeout->callback( timeout->private );
956 free( timeout );
959 if ((ptr = list_head( &abs_timeout_list )) != NULL)
961 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
962 timeout_t diff = (timeout->when - current_time + 9999) / 10000;
963 if (diff > INT_MAX) diff = INT_MAX;
964 else if (diff < 0) diff = 0;
965 if (ret == -1 || diff < ret) ret = diff;
968 if ((ptr = list_head( &rel_timeout_list )) != NULL)
970 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
971 timeout_t diff = (-timeout->when - monotonic_time + 9999) / 10000;
972 if (diff > INT_MAX) diff = INT_MAX;
973 else if (diff < 0) diff = 0;
974 if (ret == -1 || diff < ret) ret = diff;
977 return ret;
980 /* server main poll() loop */
981 void main_loop(void)
983 int i, ret, timeout;
985 set_current_time();
986 server_start_time = current_time;
988 main_loop_epoll();
989 /* fall through to normal poll loop */
991 while (active_users)
993 timeout = get_next_timeout();
995 if (!active_users) break; /* last user removed by a timeout */
997 ret = poll( pollfd, nb_users, timeout );
998 set_current_time();
1000 if (ret > 0)
1002 for (i = 0; i < nb_users; i++)
1004 if (pollfd[i].revents)
1006 fd_poll_event( poll_users[i], pollfd[i].revents );
1007 if (!--ret) break;
1015 /****************************************************************/
1016 /* device functions */
1018 static struct list device_hash[DEVICE_HASH_SIZE];
1020 static int is_device_removable( dev_t dev, int unix_fd )
1022 #if defined(linux) && defined(HAVE_FSTATFS)
1023 struct statfs stfs;
1025 /* check for floppy disk */
1026 if (major(dev) == FLOPPY_MAJOR) return 1;
1028 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
1029 return (stfs.f_type == 0x9660 || /* iso9660 */
1030 stfs.f_type == 0x9fa1 || /* supermount */
1031 stfs.f_type == 0x15013346); /* udf */
1032 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
1033 struct statfs stfs;
1035 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
1036 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
1037 #elif defined(__NetBSD__)
1038 struct statvfs stfs;
1040 if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
1041 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
1042 #elif defined(sun)
1043 # include <sys/dkio.h>
1044 # include <sys/vtoc.h>
1045 struct dk_cinfo dkinf;
1046 if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
1047 return (dkinf.dki_ctype == DKC_CDROM ||
1048 dkinf.dki_ctype == DKC_NCRFLOPPY ||
1049 dkinf.dki_ctype == DKC_SMSFLOPPY ||
1050 dkinf.dki_ctype == DKC_INTEL82072 ||
1051 dkinf.dki_ctype == DKC_INTEL82077);
1052 #else
1053 return 0;
1054 #endif
1057 /* retrieve the device object for a given fd, creating it if needed */
1058 static struct device *get_device( dev_t dev, int unix_fd )
1060 struct device *device;
1061 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
1063 if (device_hash[hash].next)
1065 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
1066 if (device->dev == dev) return (struct device *)grab_object( device );
1068 else list_init( &device_hash[hash] );
1070 /* not found, create it */
1072 if (unix_fd == -1) return NULL;
1073 if ((device = alloc_object( &device_ops )))
1075 device->dev = dev;
1076 device->removable = is_device_removable( dev, unix_fd );
1077 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
1078 list_add_head( &device_hash[hash], &device->entry );
1080 return device;
1083 static void device_dump( struct object *obj, int verbose )
1085 struct device *device = (struct device *)obj;
1086 fprintf( stderr, "Device dev=" );
1087 DUMP_LONG_LONG( device->dev );
1088 fprintf( stderr, "\n" );
1091 static void device_destroy( struct object *obj )
1093 struct device *device = (struct device *)obj;
1094 unsigned int i;
1096 for (i = 0; i < INODE_HASH_SIZE; i++)
1097 assert( list_empty(&device->inode_hash[i]) );
1099 list_remove( &device->entry ); /* remove it from the hash table */
1103 /****************************************************************/
1104 /* inode functions */
1106 /* close all pending file descriptors in the closed list */
1107 static void inode_close_pending( struct inode *inode, int keep_unlinks )
1109 struct list *ptr = list_head( &inode->closed );
1111 while (ptr)
1113 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1114 struct list *next = list_next( &inode->closed, ptr );
1116 if (fd->unix_fd != -1)
1118 close( fd->unix_fd );
1119 fd->unix_fd = -1;
1121 if (!keep_unlinks || !fd->unlink) /* get rid of it unless there's an unlink pending on that file */
1123 list_remove( ptr );
1124 free( fd->unix_name );
1125 free( fd );
1127 ptr = next;
1131 static void inode_dump( struct object *obj, int verbose )
1133 struct inode *inode = (struct inode *)obj;
1134 fprintf( stderr, "Inode device=%p ino=", inode->device );
1135 DUMP_LONG_LONG( inode->ino );
1136 fprintf( stderr, "\n" );
1139 static void inode_destroy( struct object *obj )
1141 struct inode *inode = (struct inode *)obj;
1142 struct list *ptr;
1144 assert( list_empty(&inode->open) );
1145 assert( list_empty(&inode->locks) );
1147 list_remove( &inode->entry );
1149 while ((ptr = list_head( &inode->closed )))
1151 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1152 list_remove( ptr );
1153 if (fd->unix_fd != -1) close( fd->unix_fd );
1154 if (fd->unlink)
1156 /* make sure it is still the same file */
1157 struct stat st;
1158 if (!stat( fd->unix_name, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
1160 if (S_ISDIR(st.st_mode)) rmdir( fd->unix_name );
1161 else unlink( fd->unix_name );
1164 free( fd->unix_name );
1165 free( fd );
1167 release_object( inode->device );
1170 /* retrieve the inode object for a given fd, creating it if needed */
1171 static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
1173 struct device *device;
1174 struct inode *inode;
1175 unsigned int hash = ino % INODE_HASH_SIZE;
1177 if (!(device = get_device( dev, unix_fd ))) return NULL;
1179 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
1181 if (inode->ino == ino)
1183 release_object( device );
1184 return (struct inode *)grab_object( inode );
1188 /* not found, create it */
1189 if ((inode = alloc_object( &inode_ops )))
1191 inode->device = device;
1192 inode->ino = ino;
1193 list_init( &inode->open );
1194 list_init( &inode->locks );
1195 list_init( &inode->closed );
1196 list_add_head( &device->inode_hash[hash], &inode->entry );
1198 else release_object( device );
1200 return inode;
1203 /* add fd to the inode list of file descriptors to close */
1204 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
1206 if (!list_empty( &inode->locks ))
1208 list_add_head( &inode->closed, &fd->entry );
1210 else if (fd->unlink) /* close the fd but keep the structure around for unlink */
1212 if (fd->unix_fd != -1) close( fd->unix_fd );
1213 fd->unix_fd = -1;
1214 list_add_head( &inode->closed, &fd->entry );
1216 else /* no locks on this inode and no unlink, get rid of the fd */
1218 if (fd->unix_fd != -1) close( fd->unix_fd );
1219 free( fd->unix_name );
1220 free( fd );
1225 /****************************************************************/
1226 /* file lock functions */
1228 static void file_lock_dump( struct object *obj, int verbose )
1230 struct file_lock *lock = (struct file_lock *)obj;
1231 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
1232 lock->shared ? "shared" : "excl", lock->fd, lock->process );
1233 DUMP_LONG_LONG( lock->start );
1234 fprintf( stderr, " end=" );
1235 DUMP_LONG_LONG( lock->end );
1236 fprintf( stderr, "\n" );
1239 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry )
1241 struct file_lock *lock = (struct file_lock *)obj;
1242 /* lock is signaled if it has lost its owner */
1243 return !lock->process;
1246 /* set (or remove) a Unix lock if possible for the given range */
1247 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
1249 struct flock fl;
1251 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
1252 for (;;)
1254 if (start == end) return 1; /* can't set zero-byte lock */
1255 if (start > max_unix_offset) return 1; /* ignore it */
1256 fl.l_type = type;
1257 fl.l_whence = SEEK_SET;
1258 fl.l_start = start;
1259 if (!end || end > max_unix_offset) fl.l_len = 0;
1260 else fl.l_len = end - start;
1261 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
1263 switch(errno)
1265 case EACCES:
1266 /* check whether locks work at all on this file system */
1267 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
1269 set_error( STATUS_FILE_LOCK_CONFLICT );
1270 return 0;
1272 /* fall through */
1273 case EIO:
1274 case ENOLCK:
1275 case ENOTSUP:
1276 /* no locking on this fs, just ignore it */
1277 fd->fs_locks = 0;
1278 return 1;
1279 case EAGAIN:
1280 set_error( STATUS_FILE_LOCK_CONFLICT );
1281 return 0;
1282 case EBADF:
1283 /* this can happen if we try to set a write lock on a read-only file */
1284 /* try to at least grab a read lock */
1285 if (fl.l_type == F_WRLCK)
1287 type = F_RDLCK;
1288 break; /* retry */
1290 set_error( STATUS_ACCESS_DENIED );
1291 return 0;
1292 #ifdef EOVERFLOW
1293 case EOVERFLOW:
1294 #endif
1295 case EINVAL:
1296 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1297 /* in that case we shrink the limit and retry */
1298 if (max_unix_offset > INT_MAX)
1300 max_unix_offset = INT_MAX;
1301 break; /* retry */
1303 /* fall through */
1304 default:
1305 file_set_error();
1306 return 0;
1311 /* check if interval [start;end) overlaps the lock */
1312 static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
1314 if (lock->end && start >= lock->end) return 0;
1315 if (end && lock->start >= end) return 0;
1316 return 1;
1319 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1320 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
1322 struct hole
1324 struct hole *next;
1325 struct hole *prev;
1326 file_pos_t start;
1327 file_pos_t end;
1328 } *first, *cur, *next, *buffer;
1330 struct list *ptr;
1331 int count = 0;
1333 if (!fd->inode) return;
1334 if (!fd->fs_locks) return;
1335 if (start == end || start > max_unix_offset) return;
1336 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
1338 /* count the number of locks overlapping the specified area */
1340 LIST_FOR_EACH( ptr, &fd->inode->locks )
1342 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1343 if (lock->start == lock->end) continue;
1344 if (lock_overlaps( lock, start, end )) count++;
1347 if (!count) /* no locks at all, we can unlock everything */
1349 set_unix_lock( fd, start, end, F_UNLCK );
1350 return;
1353 /* allocate space for the list of holes */
1354 /* max. number of holes is number of locks + 1 */
1356 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1357 first = buffer;
1358 first->next = NULL;
1359 first->prev = NULL;
1360 first->start = start;
1361 first->end = end;
1362 next = first + 1;
1364 /* build a sorted list of unlocked holes in the specified area */
1366 LIST_FOR_EACH( ptr, &fd->inode->locks )
1368 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1369 if (lock->start == lock->end) continue;
1370 if (!lock_overlaps( lock, start, end )) continue;
1372 /* go through all the holes touched by this lock */
1373 for (cur = first; cur; cur = cur->next)
1375 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1376 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1378 /* now we know that lock is overlapping hole */
1380 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1382 cur->start = lock->end;
1383 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1384 /* now hole is empty, remove it */
1385 if (cur->next) cur->next->prev = cur->prev;
1386 if (cur->prev) cur->prev->next = cur->next;
1387 else if (!(first = cur->next)) goto done; /* no more holes at all */
1389 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1391 cur->end = lock->start;
1392 assert( cur->start < cur->end );
1394 else /* lock is in the middle of hole, split hole in two */
1396 next->prev = cur;
1397 next->next = cur->next;
1398 cur->next = next;
1399 next->start = lock->end;
1400 next->end = cur->end;
1401 cur->end = lock->start;
1402 assert( next->start < next->end );
1403 assert( cur->end < next->start );
1404 next++;
1405 break; /* done with this lock */
1410 /* clear Unix locks for all the holes */
1412 for (cur = first; cur; cur = cur->next)
1413 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1415 done:
1416 free( buffer );
1419 /* create a new lock on a fd */
1420 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1422 struct file_lock *lock;
1424 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1425 lock->shared = shared;
1426 lock->start = start;
1427 lock->end = end;
1428 lock->fd = fd;
1429 lock->process = current->process;
1431 /* now try to set a Unix lock */
1432 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1434 release_object( lock );
1435 return NULL;
1437 list_add_tail( &fd->locks, &lock->fd_entry );
1438 list_add_tail( &fd->inode->locks, &lock->inode_entry );
1439 list_add_tail( &lock->process->locks, &lock->proc_entry );
1440 return lock;
1443 /* remove an existing lock */
1444 static void remove_lock( struct file_lock *lock, int remove_unix )
1446 struct inode *inode = lock->fd->inode;
1448 list_remove( &lock->fd_entry );
1449 list_remove( &lock->inode_entry );
1450 list_remove( &lock->proc_entry );
1451 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1452 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1453 lock->process = NULL;
1454 wake_up( &lock->obj, 0 );
1455 release_object( lock );
1458 /* remove all locks owned by a given process */
1459 void remove_process_locks( struct process *process )
1461 struct list *ptr;
1463 while ((ptr = list_head( &process->locks )))
1465 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1466 remove_lock( lock, 1 ); /* this removes it from the list */
1470 /* remove all locks on a given fd */
1471 static void remove_fd_locks( struct fd *fd )
1473 file_pos_t start = FILE_POS_T_MAX, end = 0;
1474 struct list *ptr;
1476 while ((ptr = list_head( &fd->locks )))
1478 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1479 if (lock->start < start) start = lock->start;
1480 if (!lock->end || lock->end > end) end = lock->end - 1;
1481 remove_lock( lock, 0 );
1483 if (start < end) remove_unix_locks( fd, start, end + 1 );
1486 /* add a lock on an fd */
1487 /* returns handle to wait on */
1488 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1490 struct list *ptr;
1491 file_pos_t end = start + count;
1493 if (!fd->inode) /* not a regular file */
1495 set_error( STATUS_INVALID_DEVICE_REQUEST );
1496 return 0;
1499 /* don't allow wrapping locks */
1500 if (end && end < start)
1502 set_error( STATUS_INVALID_PARAMETER );
1503 return 0;
1506 /* check if another lock on that file overlaps the area */
1507 LIST_FOR_EACH( ptr, &fd->inode->locks )
1509 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1510 if (!lock_overlaps( lock, start, end )) continue;
1511 if (shared && (lock->shared || lock->fd == fd)) continue;
1512 /* found one */
1513 if (!wait)
1515 set_error( STATUS_FILE_LOCK_CONFLICT );
1516 return 0;
1518 set_error( STATUS_PENDING );
1519 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1522 /* not found, add it */
1523 if (add_lock( fd, shared, start, end )) return 0;
1524 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1526 /* Unix lock conflict -> tell client to wait and retry */
1527 if (wait) set_error( STATUS_PENDING );
1529 return 0;
1532 /* remove a lock on an fd */
1533 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1535 struct list *ptr;
1536 file_pos_t end = start + count;
1538 /* find an existing lock with the exact same parameters */
1539 LIST_FOR_EACH( ptr, &fd->locks )
1541 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1542 if ((lock->start == start) && (lock->end == end))
1544 remove_lock( lock, 1 );
1545 return;
1548 set_error( STATUS_FILE_LOCK_CONFLICT );
1552 /****************************************************************/
1553 /* file descriptor functions */
1555 static void fd_dump( struct object *obj, int verbose )
1557 struct fd *fd = (struct fd *)obj;
1558 fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
1559 if (fd->inode) fprintf( stderr, " inode=%p unlink=%d", fd->inode, fd->closed->unlink );
1560 fprintf( stderr, "\n" );
1563 static void fd_destroy( struct object *obj )
1565 struct fd *fd = (struct fd *)obj;
1567 free_async_queue( &fd->read_q );
1568 free_async_queue( &fd->write_q );
1569 free_async_queue( &fd->wait_q );
1571 if (fd->completion) release_object( fd->completion );
1572 remove_fd_locks( fd );
1573 list_remove( &fd->inode_entry );
1574 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1575 free( fd->nt_name );
1576 if (fd->inode)
1578 inode_add_closed_fd( fd->inode, fd->closed );
1579 release_object( fd->inode );
1581 else /* no inode, close it right away */
1583 if (fd->unix_fd != -1) close( fd->unix_fd );
1584 free( fd->unix_name );
1588 /* check if the desired access is possible without violating */
1589 /* the sharing mode of other opens of the same file */
1590 static unsigned int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing,
1591 unsigned int open_flags, unsigned int options )
1593 /* only a few access bits are meaningful wrt sharing */
1594 const unsigned int read_access = FILE_READ_DATA | FILE_EXECUTE;
1595 const unsigned int write_access = FILE_WRITE_DATA | FILE_APPEND_DATA;
1596 const unsigned int all_access = read_access | write_access | DELETE;
1598 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1599 unsigned int existing_access = 0;
1600 struct list *ptr;
1602 fd->access = access;
1603 fd->sharing = sharing;
1605 LIST_FOR_EACH( ptr, &fd->inode->open )
1607 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1608 if (fd_ptr != fd)
1610 /* if access mode is 0, sharing mode is ignored */
1611 if (fd_ptr->access & all_access) existing_sharing &= fd_ptr->sharing;
1612 existing_access |= fd_ptr->access;
1616 if (((access & read_access) && !(existing_sharing & FILE_SHARE_READ)) ||
1617 ((access & write_access) && !(existing_sharing & FILE_SHARE_WRITE)) ||
1618 ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)))
1619 return STATUS_SHARING_VIOLATION;
1620 if (((existing_access & FILE_MAPPING_WRITE) && !(sharing & FILE_SHARE_WRITE)) ||
1621 ((existing_access & FILE_MAPPING_IMAGE) && (access & FILE_WRITE_DATA)))
1622 return STATUS_SHARING_VIOLATION;
1623 if ((existing_access & FILE_MAPPING_IMAGE) && (options & FILE_DELETE_ON_CLOSE))
1624 return STATUS_CANNOT_DELETE;
1625 if ((existing_access & FILE_MAPPING_ACCESS) && (open_flags & O_TRUNC))
1626 return STATUS_USER_MAPPED_FILE;
1627 if (!(access & all_access))
1628 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1629 if (((existing_access & read_access) && !(sharing & FILE_SHARE_READ)) ||
1630 ((existing_access & write_access) && !(sharing & FILE_SHARE_WRITE)) ||
1631 ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)))
1632 return STATUS_SHARING_VIOLATION;
1633 return 0;
1636 /* set the events that select waits for on this fd */
1637 void set_fd_events( struct fd *fd, int events )
1639 int user = fd->poll_index;
1640 assert( poll_users[user] == fd );
1642 set_fd_epoll_events( fd, user, events );
1644 if (events == -1) /* stop waiting on this fd completely */
1646 pollfd[user].fd = -1;
1647 pollfd[user].events = POLLERR;
1648 pollfd[user].revents = 0;
1650 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1652 pollfd[user].fd = fd->unix_fd;
1653 pollfd[user].events = events;
1657 /* prepare an fd for unmounting its corresponding device */
1658 static inline void unmount_fd( struct fd *fd )
1660 assert( fd->inode );
1662 async_wake_up( &fd->read_q, STATUS_VOLUME_DISMOUNTED );
1663 async_wake_up( &fd->write_q, STATUS_VOLUME_DISMOUNTED );
1665 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1667 if (fd->unix_fd != -1) close( fd->unix_fd );
1669 fd->unix_fd = -1;
1670 fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
1671 fd->closed->unix_fd = -1;
1672 fd->closed->unlink = 0;
1674 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1675 fd->fs_locks = 0;
1678 /* allocate an fd object, without setting the unix fd yet */
1679 static struct fd *alloc_fd_object(void)
1681 struct fd *fd = alloc_object( &fd_ops );
1683 if (!fd) return NULL;
1685 fd->fd_ops = NULL;
1686 fd->user = NULL;
1687 fd->inode = NULL;
1688 fd->closed = NULL;
1689 fd->access = 0;
1690 fd->options = 0;
1691 fd->sharing = 0;
1692 fd->unix_fd = -1;
1693 fd->unix_name = NULL;
1694 fd->nt_name = NULL;
1695 fd->nt_namelen = 0;
1696 fd->cacheable = 0;
1697 fd->signaled = 1;
1698 fd->fs_locks = 1;
1699 fd->poll_index = -1;
1700 fd->completion = NULL;
1701 fd->comp_flags = 0;
1702 init_async_queue( &fd->read_q );
1703 init_async_queue( &fd->write_q );
1704 init_async_queue( &fd->wait_q );
1705 list_init( &fd->inode_entry );
1706 list_init( &fd->locks );
1708 if ((fd->poll_index = add_poll_user( fd )) == -1)
1710 release_object( fd );
1711 return NULL;
1713 return fd;
1716 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1717 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
1719 struct fd *fd = alloc_object( &fd_ops );
1721 if (!fd) return NULL;
1723 fd->fd_ops = fd_user_ops;
1724 fd->user = user;
1725 fd->inode = NULL;
1726 fd->closed = NULL;
1727 fd->access = 0;
1728 fd->options = options;
1729 fd->sharing = 0;
1730 fd->unix_name = NULL;
1731 fd->nt_name = NULL;
1732 fd->nt_namelen = 0;
1733 fd->unix_fd = -1;
1734 fd->cacheable = 0;
1735 fd->signaled = 0;
1736 fd->fs_locks = 0;
1737 fd->poll_index = -1;
1738 fd->completion = NULL;
1739 fd->comp_flags = 0;
1740 fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
1741 init_async_queue( &fd->read_q );
1742 init_async_queue( &fd->write_q );
1743 init_async_queue( &fd->wait_q );
1744 list_init( &fd->inode_entry );
1745 list_init( &fd->locks );
1746 return fd;
1749 /* duplicate an fd object for a different user */
1750 struct fd *dup_fd_object( struct fd *orig, unsigned int access, unsigned int sharing, unsigned int options )
1752 unsigned int err;
1753 struct fd *fd = alloc_fd_object();
1755 if (!fd) return NULL;
1757 fd->options = options;
1758 fd->cacheable = orig->cacheable;
1760 if (orig->unix_name)
1762 if (!(fd->unix_name = mem_alloc( strlen(orig->unix_name) + 1 ))) goto failed;
1763 strcpy( fd->unix_name, orig->unix_name );
1765 if (orig->nt_namelen)
1767 if (!(fd->nt_name = memdup( orig->nt_name, orig->nt_namelen ))) goto failed;
1768 fd->nt_namelen = orig->nt_namelen;
1771 if (orig->inode)
1773 struct closed_fd *closed = mem_alloc( sizeof(*closed) );
1774 if (!closed) goto failed;
1775 if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1777 file_set_error();
1778 free( closed );
1779 goto failed;
1781 closed->unix_fd = fd->unix_fd;
1782 closed->unlink = 0;
1783 closed->unix_name = fd->unix_name;
1784 fd->closed = closed;
1785 fd->inode = (struct inode *)grab_object( orig->inode );
1786 list_add_head( &fd->inode->open, &fd->inode_entry );
1787 if ((err = check_sharing( fd, access, sharing, 0, options )))
1789 set_error( err );
1790 goto failed;
1793 else if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1795 file_set_error();
1796 goto failed;
1798 return fd;
1800 failed:
1801 release_object( fd );
1802 return NULL;
1805 /* find an existing fd object that can be reused for a mapping */
1806 struct fd *get_fd_object_for_mapping( struct fd *fd, unsigned int access, unsigned int sharing )
1808 struct fd *fd_ptr;
1810 if (!fd->inode) return NULL;
1812 LIST_FOR_EACH_ENTRY( fd_ptr, &fd->inode->open, struct fd, inode_entry )
1813 if (fd_ptr->access == access && fd_ptr->sharing == sharing)
1814 return (struct fd *)grab_object( fd_ptr );
1816 return NULL;
1819 /* sets the user of an fd that previously had no user */
1820 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1822 assert( fd->fd_ops == NULL );
1823 fd->fd_ops = user_ops;
1824 fd->user = user;
1827 char *dup_fd_name( struct fd *root, const char *name )
1829 char *ret;
1831 if (!root) return strdup( name );
1832 if (!root->unix_name) return NULL;
1834 /* skip . prefix */
1835 if (name[0] == '.' && (!name[1] || name[1] == '/')) name++;
1837 if ((ret = malloc( strlen(root->unix_name) + strlen(name) + 2 )))
1839 strcpy( ret, root->unix_name );
1840 if (name[0] && name[0] != '/') strcat( ret, "/" );
1841 strcat( ret, name );
1843 return ret;
1846 static WCHAR *dup_nt_name( struct fd *root, struct unicode_str name, data_size_t *len )
1848 WCHAR *ret;
1849 data_size_t retlen;
1851 if (!root)
1853 *len = name.len;
1854 if (!name.len) return NULL;
1855 return memdup( name.str, name.len );
1857 if (!root->nt_namelen) return NULL;
1858 retlen = root->nt_namelen;
1860 /* skip . prefix */
1861 if (name.len && name.str[0] == '.' && (name.len == sizeof(WCHAR) || name.str[1] == '\\'))
1863 name.str++;
1864 name.len -= sizeof(WCHAR);
1866 if ((ret = malloc( retlen + name.len + 1 )))
1868 memcpy( ret, root->nt_name, root->nt_namelen );
1869 if (name.len && name.str[0] != '\\' &&
1870 root->nt_namelen && root->nt_name[root->nt_namelen / sizeof(WCHAR) - 1] != '\\')
1872 ret[retlen / sizeof(WCHAR)] = '\\';
1873 retlen += sizeof(WCHAR);
1875 memcpy( ret + retlen / sizeof(WCHAR), name.str, name.len );
1876 *len = retlen + name.len;
1878 return ret;
1881 void get_nt_name( struct fd *fd, struct unicode_str *name )
1883 name->str = fd->nt_name;
1884 name->len = fd->nt_namelen;
1887 /* open() wrapper that returns a struct fd with no fd user set */
1888 struct fd *open_fd( struct fd *root, const char *name, struct unicode_str nt_name,
1889 int flags, mode_t *mode, unsigned int access,
1890 unsigned int sharing, unsigned int options )
1892 struct stat st;
1893 struct closed_fd *closed_fd;
1894 struct fd *fd;
1895 int root_fd = -1;
1896 int rw_mode;
1897 char *path;
1899 if (((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) ||
1900 ((options & FILE_DIRECTORY_FILE) && (flags & O_TRUNC)))
1902 set_error( STATUS_INVALID_PARAMETER );
1903 return NULL;
1906 if (!(fd = alloc_fd_object())) return NULL;
1908 fd->options = options;
1909 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) )))
1911 release_object( fd );
1912 return NULL;
1915 if (root)
1917 if ((root_fd = get_unix_fd( root )) == -1) goto error;
1918 if (fchdir( root_fd ) == -1)
1920 file_set_error();
1921 root_fd = -1;
1922 goto error;
1926 /* create the directory if needed */
1927 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1929 if (mkdir( name, *mode ) == -1)
1931 if (errno != EEXIST || (flags & O_EXCL))
1933 file_set_error();
1934 goto error;
1937 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1940 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1942 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1943 else rw_mode = O_WRONLY;
1945 else rw_mode = O_RDONLY;
1947 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1949 /* if we tried to open a directory for write access, retry read-only */
1950 if (errno == EISDIR)
1952 if ((access & FILE_UNIX_WRITE_ACCESS) || (flags & O_CREAT))
1953 fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
1956 if (fd->unix_fd == -1)
1958 file_set_error();
1959 goto error;
1963 fd->nt_name = dup_nt_name( root, nt_name, &fd->nt_namelen );
1964 fd->unix_name = NULL;
1965 if ((path = dup_fd_name( root, name )))
1967 fd->unix_name = realpath( path, NULL );
1968 free( path );
1971 closed_fd->unix_fd = fd->unix_fd;
1972 closed_fd->unlink = 0;
1973 closed_fd->unix_name = fd->unix_name;
1974 fstat( fd->unix_fd, &st );
1975 *mode = st.st_mode;
1977 /* only bother with an inode for normal files and directories */
1978 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1980 unsigned int err;
1981 struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
1983 if (!inode)
1985 /* we can close the fd because there are no others open on the same file,
1986 * otherwise we wouldn't have failed to allocate a new inode
1988 goto error;
1990 fd->inode = inode;
1991 fd->closed = closed_fd;
1992 fd->cacheable = !inode->device->removable;
1993 list_add_head( &inode->open, &fd->inode_entry );
1994 closed_fd = NULL;
1996 /* check directory options */
1997 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1999 set_error( STATUS_NOT_A_DIRECTORY );
2000 goto error;
2002 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
2004 set_error( STATUS_FILE_IS_A_DIRECTORY );
2005 goto error;
2007 if ((err = check_sharing( fd, access, sharing, flags, options )))
2009 set_error( err );
2010 goto error;
2013 /* can't unlink files if we don't have permission to access */
2014 if ((options & FILE_DELETE_ON_CLOSE) && !(flags & O_CREAT) &&
2015 !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2017 set_error( STATUS_CANNOT_DELETE );
2018 goto error;
2021 fd->closed->unlink = (options & FILE_DELETE_ON_CLOSE) ? -1 : 0;
2022 if (flags & O_TRUNC)
2024 if (S_ISDIR(st.st_mode))
2026 set_error( STATUS_OBJECT_NAME_COLLISION );
2027 goto error;
2029 ftruncate( fd->unix_fd, 0 );
2032 else /* special file */
2034 if (options & FILE_DELETE_ON_CLOSE) /* we can't unlink special files */
2036 set_error( STATUS_INVALID_PARAMETER );
2037 goto error;
2039 free( closed_fd );
2040 fd->cacheable = 1;
2042 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
2043 return fd;
2045 error:
2046 release_object( fd );
2047 free( closed_fd );
2048 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
2049 return NULL;
2052 /* create an fd for an anonymous file */
2053 /* if the function fails the unix fd is closed */
2054 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
2055 unsigned int options )
2057 struct fd *fd = alloc_fd_object();
2059 if (fd)
2061 set_fd_user( fd, fd_user_ops, user );
2062 fd->unix_fd = unix_fd;
2063 fd->options = options;
2064 return fd;
2066 close( unix_fd );
2067 return NULL;
2070 /* retrieve the object that is using an fd */
2071 void *get_fd_user( struct fd *fd )
2073 return fd->user;
2076 /* retrieve the opening options for the fd */
2077 unsigned int get_fd_options( struct fd *fd )
2079 return fd->options;
2082 /* check if fd is in overlapped mode */
2083 int is_fd_overlapped( struct fd *fd )
2085 return !(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
2088 /* retrieve the unix fd for an object */
2089 int get_unix_fd( struct fd *fd )
2091 if (fd->unix_fd == -1) set_error( fd->no_fd_status );
2092 return fd->unix_fd;
2095 /* check if two file descriptors point to the same file */
2096 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
2098 return fd1->inode == fd2->inode;
2101 /* allow the fd to be cached (can't be reset once set) */
2102 void allow_fd_caching( struct fd *fd )
2104 fd->cacheable = 1;
2107 /* check if fd is on a removable device */
2108 int is_fd_removable( struct fd *fd )
2110 return (fd->inode && fd->inode->device->removable);
2113 /* set or clear the fd signaled state */
2114 void set_fd_signaled( struct fd *fd, int signaled )
2116 if (fd->comp_flags & FILE_SKIP_SET_EVENT_ON_HANDLE) return;
2117 fd->signaled = signaled;
2118 if (signaled) wake_up( fd->user, 0 );
2121 /* handler for close_handle that refuses to close fd-associated handles in other processes */
2122 int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
2124 return (!current || current->process == process);
2127 /* check if events are pending and if yes return which one(s) */
2128 int check_fd_events( struct fd *fd, int events )
2130 struct pollfd pfd;
2132 if (fd->unix_fd == -1) return POLLERR;
2133 if (fd->inode) return events; /* regular files are always signaled */
2135 pfd.fd = fd->unix_fd;
2136 pfd.events = events;
2137 if (poll( &pfd, 1, 0 ) <= 0) return 0;
2138 return pfd.revents;
2141 /* default signaled() routine for objects that poll() on an fd */
2142 int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
2144 struct fd *fd = get_obj_fd( obj );
2145 int ret = fd->signaled;
2146 release_object( fd );
2147 return ret;
2150 int default_fd_get_poll_events( struct fd *fd )
2152 int events = 0;
2154 if (async_waiting( &fd->read_q )) events |= POLLIN;
2155 if (async_waiting( &fd->write_q )) events |= POLLOUT;
2156 return events;
2159 /* default handler for poll() events */
2160 void default_poll_event( struct fd *fd, int event )
2162 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( &fd->read_q, STATUS_ALERTED );
2163 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( &fd->write_q, STATUS_ALERTED );
2165 /* if an error occurred, stop polling this fd to avoid busy-looping */
2166 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
2167 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2170 void fd_queue_async( struct fd *fd, struct async *async, int type )
2172 struct async_queue *queue;
2174 switch (type)
2176 case ASYNC_TYPE_READ:
2177 queue = &fd->read_q;
2178 break;
2179 case ASYNC_TYPE_WRITE:
2180 queue = &fd->write_q;
2181 break;
2182 case ASYNC_TYPE_WAIT:
2183 queue = &fd->wait_q;
2184 break;
2185 default:
2186 queue = NULL;
2187 assert(0);
2190 queue_async( queue, async );
2192 if (type != ASYNC_TYPE_WAIT)
2194 if (!fd->inode)
2195 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2196 else /* regular files are always ready for read and write */
2197 async_wake_up( queue, STATUS_ALERTED );
2201 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
2203 switch (type)
2205 case ASYNC_TYPE_READ:
2206 async_wake_up( &fd->read_q, status );
2207 break;
2208 case ASYNC_TYPE_WRITE:
2209 async_wake_up( &fd->write_q, status );
2210 break;
2211 case ASYNC_TYPE_WAIT:
2212 async_wake_up( &fd->wait_q, status );
2213 break;
2214 default:
2215 assert(0);
2219 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
2221 fd->fd_ops->reselect_async( fd, queue );
2224 void no_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2226 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2229 void default_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2231 fd_queue_async( fd, async, type );
2232 set_error( STATUS_PENDING );
2235 /* default reselect_async() fd routine */
2236 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
2238 if (queue == &fd->read_q || queue == &fd->write_q)
2240 int poll_events = fd->fd_ops->get_poll_events( fd );
2241 int events = check_fd_events( fd, poll_events );
2242 if (events) fd->fd_ops->poll_event( fd, events );
2243 else set_fd_events( fd, poll_events );
2247 static inline int is_valid_mounted_device( struct stat *st )
2249 #if defined(linux) || defined(__sun__)
2250 return S_ISBLK( st->st_mode );
2251 #else
2252 /* disks are char devices on *BSD */
2253 return S_ISCHR( st->st_mode );
2254 #endif
2257 /* close all Unix file descriptors on a device to allow unmounting it */
2258 static void unmount_device( struct fd *device_fd )
2260 unsigned int i;
2261 struct stat st;
2262 struct device *device;
2263 struct inode *inode;
2264 struct fd *fd;
2265 int unix_fd = get_unix_fd( device_fd );
2267 if (unix_fd == -1) return;
2269 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2271 set_error( STATUS_INVALID_PARAMETER );
2272 return;
2275 if (!(device = get_device( st.st_rdev, -1 ))) return;
2277 for (i = 0; i < INODE_HASH_SIZE; i++)
2279 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
2281 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
2283 unmount_fd( fd );
2285 inode_close_pending( inode, 0 );
2288 /* remove it from the hash table */
2289 list_remove( &device->entry );
2290 list_init( &device->entry );
2291 release_object( device );
2294 /* default read() routine */
2295 int no_fd_read( struct fd *fd, struct async *async, file_pos_t pos )
2297 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2298 return 0;
2301 /* default write() routine */
2302 int no_fd_write( struct fd *fd, struct async *async, file_pos_t pos )
2304 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2305 return 0;
2308 /* default flush() routine */
2309 int no_fd_flush( struct fd *fd, struct async *async )
2311 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2312 return 0;
2315 /* default get_file_info() routine */
2316 void no_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
2318 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2321 /* default get_file_info() routine */
2322 void default_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
2324 switch (info_class)
2326 case FileAccessInformation:
2328 FILE_ACCESS_INFORMATION info;
2329 if (get_reply_max_size() < sizeof(info))
2331 set_error( STATUS_INFO_LENGTH_MISMATCH );
2332 return;
2334 info.AccessFlags = get_handle_access( current->process, handle );
2335 set_reply_data( &info, sizeof(info) );
2336 break;
2338 case FileModeInformation:
2340 FILE_MODE_INFORMATION info;
2341 if (get_reply_max_size() < sizeof(info))
2343 set_error( STATUS_INFO_LENGTH_MISMATCH );
2344 return;
2346 info.Mode = fd->options & ( FILE_WRITE_THROUGH
2347 | FILE_SEQUENTIAL_ONLY
2348 | FILE_NO_INTERMEDIATE_BUFFERING
2349 | FILE_SYNCHRONOUS_IO_ALERT
2350 | FILE_SYNCHRONOUS_IO_NONALERT );
2351 set_reply_data( &info, sizeof(info) );
2352 break;
2354 case FileIoCompletionNotificationInformation:
2356 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION info;
2357 if (get_reply_max_size() < sizeof(info))
2359 set_error( STATUS_INFO_LENGTH_MISMATCH );
2360 return;
2362 info.Flags = fd->comp_flags;
2363 set_reply_data( &info, sizeof(info) );
2364 break;
2366 default:
2367 set_error( STATUS_NOT_IMPLEMENTED );
2371 /* default get_volume_info() routine */
2372 int no_fd_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
2374 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2375 return 0;
2378 /* default ioctl() routine */
2379 int no_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2381 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2382 return 0;
2385 /* default ioctl() routine */
2386 int default_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2388 switch(code)
2390 case FSCTL_DISMOUNT_VOLUME:
2391 unmount_device( fd );
2392 return 1;
2393 default:
2394 set_error( STATUS_NOT_SUPPORTED );
2395 return 0;
2399 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2400 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
2401 unsigned int access )
2403 struct fd *fd = NULL;
2404 struct object *obj;
2406 if ((obj = get_handle_obj( process, handle, access, NULL )))
2408 fd = get_obj_fd( obj );
2409 release_object( obj );
2411 return fd;
2414 static int is_dir_empty( int fd )
2416 DIR *dir;
2417 int empty;
2418 struct dirent *de;
2420 if ((fd = dup( fd )) == -1)
2421 return -1;
2423 if (!(dir = fdopendir( fd )))
2425 close( fd );
2426 return -1;
2429 empty = 1;
2430 while (empty && (de = readdir( dir )))
2432 if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." )) continue;
2433 empty = 0;
2435 closedir( dir );
2436 return empty;
2439 /* set disposition for the fd */
2440 static void set_fd_disposition( struct fd *fd, int unlink )
2442 struct stat st;
2444 if (!fd->inode)
2446 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2447 return;
2450 if (fd->unix_fd == -1)
2452 set_error( fd->no_fd_status );
2453 return;
2456 if (unlink)
2458 if (fstat( fd->unix_fd, &st ) == -1)
2460 file_set_error();
2461 return;
2463 if (S_ISREG( st.st_mode )) /* can't unlink files we don't have permission to write */
2465 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2467 set_error( STATUS_CANNOT_DELETE );
2468 return;
2471 else if (S_ISDIR( st.st_mode )) /* can't remove non-empty directories */
2473 switch (is_dir_empty( fd->unix_fd ))
2475 case -1:
2476 file_set_error();
2477 return;
2478 case 0:
2479 set_error( STATUS_DIRECTORY_NOT_EMPTY );
2480 return;
2483 else /* can't unlink special files */
2485 set_error( STATUS_INVALID_PARAMETER );
2486 return;
2490 fd->closed->unlink = unlink ? 1 : 0;
2491 if (fd->options & FILE_DELETE_ON_CLOSE)
2492 fd->closed->unlink = -1;
2495 /* set new name for the fd */
2496 static void set_fd_name( struct fd *fd, struct fd *root, const char *nameptr, data_size_t len,
2497 struct unicode_str nt_name, int create_link, int replace )
2499 struct inode *inode;
2500 struct stat st, st2;
2501 char *name;
2503 if (!fd->inode || !fd->unix_name)
2505 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2506 return;
2508 if (fd->unix_fd == -1)
2510 set_error( fd->no_fd_status );
2511 return;
2514 if (!len || ((nameptr[0] == '/') ^ !root))
2516 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
2517 return;
2519 if (!(name = mem_alloc( len + 1 ))) return;
2520 memcpy( name, nameptr, len );
2521 name[len] = 0;
2523 if (root)
2525 char *combined_name = dup_fd_name( root, name );
2526 if (!combined_name)
2528 set_error( STATUS_NO_MEMORY );
2529 goto failed;
2531 free( name );
2532 name = combined_name;
2535 /* when creating a hard link, source cannot be a dir */
2536 if (create_link && !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode ))
2538 set_error( STATUS_FILE_IS_A_DIRECTORY );
2539 goto failed;
2542 if (!stat( name, &st ))
2544 if (!fstat( fd->unix_fd, &st2 ) && st.st_ino == st2.st_ino && st.st_dev == st2.st_dev)
2546 if (create_link && !replace) set_error( STATUS_OBJECT_NAME_COLLISION );
2547 free( name );
2548 return;
2551 if (!replace)
2553 set_error( STATUS_OBJECT_NAME_COLLISION );
2554 goto failed;
2557 /* can't replace directories or special files */
2558 if (!S_ISREG( st.st_mode ))
2560 set_error( STATUS_ACCESS_DENIED );
2561 goto failed;
2564 /* can't replace an opened file */
2565 if ((inode = get_inode( st.st_dev, st.st_ino, -1 )))
2567 int is_empty = list_empty( &inode->open );
2568 release_object( inode );
2569 if (!is_empty)
2571 set_error( STATUS_ACCESS_DENIED );
2572 goto failed;
2576 /* link() expects that the target doesn't exist */
2577 /* rename() cannot replace files with directories */
2578 if (create_link || S_ISDIR( st2.st_mode ))
2580 if (unlink( name ))
2582 file_set_error();
2583 goto failed;
2588 if (create_link)
2590 if (link( fd->unix_name, name ))
2591 file_set_error();
2592 free( name );
2593 return;
2596 if (rename( fd->unix_name, name ))
2598 file_set_error();
2599 goto failed;
2602 if (is_file_executable( fd->unix_name ) != is_file_executable( name ) && !fstat( fd->unix_fd, &st ))
2604 if (is_file_executable( name ))
2605 /* set executable bit where read bit is set */
2606 st.st_mode |= (st.st_mode & 0444) >> 2;
2607 else
2608 st.st_mode &= ~0111;
2609 fchmod( fd->unix_fd, st.st_mode );
2612 free( fd->nt_name );
2613 fd->nt_name = dup_nt_name( root, nt_name, &fd->nt_namelen );
2614 free( fd->unix_name );
2615 fd->closed->unix_name = fd->unix_name = realpath( name, NULL );
2616 free( name );
2617 if (!fd->unix_name)
2618 set_error( STATUS_NO_MEMORY );
2619 return;
2621 failed:
2622 free( name );
2625 struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
2627 *p_key = fd->comp_key;
2628 return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
2631 void fd_copy_completion( struct fd *src, struct fd *dst )
2633 assert( !dst->completion );
2634 dst->completion = fd_get_completion( src, &dst->comp_key );
2635 dst->comp_flags = src->comp_flags;
2638 /* flush a file buffers */
2639 DECL_HANDLER(flush)
2641 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, 0 );
2642 struct async *async;
2644 if (!fd) return;
2646 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2648 reply->event = async_handoff( async, fd->fd_ops->flush( fd, async ), NULL, 1 );
2649 release_object( async );
2651 release_object( fd );
2654 /* query file info */
2655 DECL_HANDLER(get_file_info)
2657 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2659 if (fd)
2661 fd->fd_ops->get_file_info( fd, req->handle, req->info_class );
2662 release_object( fd );
2666 /* query volume info */
2667 DECL_HANDLER(get_volume_info)
2669 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2670 struct async *async;
2672 if (!fd) return;
2674 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2676 reply->wait = async_handoff( async, fd->fd_ops->get_volume_info( fd, async, req->info_class ), NULL, 1 );
2677 release_object( async );
2679 release_object( fd );
2682 /* open a file object */
2683 DECL_HANDLER(open_file_object)
2685 struct unicode_str name = get_req_unicode_str();
2686 struct object *obj, *result, *root = NULL;
2688 if (req->rootdir && !(root = get_handle_obj( current->process, req->rootdir, 0, NULL ))) return;
2690 obj = open_named_object( root, NULL, &name, req->attributes );
2691 if (root) release_object( root );
2692 if (!obj) return;
2694 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
2696 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
2697 release_object( result );
2699 release_object( obj );
2702 /* get the Unix name from a file handle */
2703 DECL_HANDLER(get_handle_unix_name)
2705 struct fd *fd;
2707 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2709 if (fd->unix_name)
2711 data_size_t name_len = strlen( fd->unix_name );
2712 reply->name_len = name_len;
2713 if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
2714 else set_error( STATUS_BUFFER_OVERFLOW );
2716 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
2717 release_object( fd );
2721 /* get a Unix fd to access a file */
2722 DECL_HANDLER(get_handle_fd)
2724 struct fd *fd;
2726 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2728 int unix_fd = get_unix_fd( fd );
2729 reply->cacheable = fd->cacheable;
2730 if (unix_fd != -1)
2732 reply->type = fd->fd_ops->get_fd_type( fd );
2733 reply->options = fd->options;
2734 reply->access = get_handle_access( current->process, req->handle );
2735 send_client_fd( current->process, unix_fd, req->handle );
2737 release_object( fd );
2741 /* perform a read on a file object */
2742 DECL_HANDLER(read)
2744 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_READ_DATA );
2745 struct async *async;
2747 if (!fd) return;
2749 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2751 reply->wait = async_handoff( async, fd->fd_ops->read( fd, async, req->pos ), NULL, 0 );
2752 reply->options = fd->options;
2753 release_object( async );
2755 release_object( fd );
2758 /* perform a write on a file object */
2759 DECL_HANDLER(write)
2761 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_WRITE_DATA );
2762 struct async *async;
2764 if (!fd) return;
2766 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2768 reply->wait = async_handoff( async, fd->fd_ops->write( fd, async, req->pos ), &reply->size, 0 );
2769 reply->options = fd->options;
2770 release_object( async );
2772 release_object( fd );
2775 /* perform an ioctl on a file */
2776 DECL_HANDLER(ioctl)
2778 unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
2779 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
2780 struct async *async;
2782 if (!fd) return;
2784 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2786 reply->wait = async_handoff( async, fd->fd_ops->ioctl( fd, req->code, async ), NULL, 0 );
2787 reply->options = fd->options;
2788 release_object( async );
2790 release_object( fd );
2793 /* create / reschedule an async I/O */
2794 DECL_HANDLER(register_async)
2796 unsigned int access;
2797 struct async *async;
2798 struct fd *fd;
2800 switch(req->type)
2802 case ASYNC_TYPE_READ:
2803 access = FILE_READ_DATA;
2804 break;
2805 case ASYNC_TYPE_WRITE:
2806 access = FILE_WRITE_DATA;
2807 break;
2808 default:
2809 set_error( STATUS_INVALID_PARAMETER );
2810 return;
2813 if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
2815 if (get_unix_fd( fd ) != -1 && (async = create_async( fd, current, &req->async, NULL )))
2817 fd->fd_ops->queue_async( fd, async, req->type, req->count );
2818 release_object( async );
2820 release_object( fd );
2824 /* attach completion object to a fd */
2825 DECL_HANDLER(set_completion_info)
2827 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2829 if (fd)
2831 if (is_fd_overlapped( fd ) && !fd->completion)
2833 fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
2834 fd->comp_key = req->ckey;
2836 else set_error( STATUS_INVALID_PARAMETER );
2837 release_object( fd );
2841 /* push new completion msg into a completion queue attached to the fd */
2842 DECL_HANDLER(add_fd_completion)
2844 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2845 if (fd)
2847 if (fd->completion && (req->async || !(fd->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
2848 add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
2849 release_object( fd );
2853 /* set fd completion information */
2854 DECL_HANDLER(set_fd_completion_mode)
2856 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2857 if (fd)
2859 if (is_fd_overlapped( fd ))
2861 if (req->flags & FILE_SKIP_SET_EVENT_ON_HANDLE)
2862 set_fd_signaled( fd, 0 );
2863 /* removing flags is not allowed */
2864 fd->comp_flags |= req->flags & ( FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
2865 | FILE_SKIP_SET_EVENT_ON_HANDLE
2866 | FILE_SKIP_SET_USER_EVENT_ON_FAST_IO );
2868 else
2869 set_error( STATUS_INVALID_PARAMETER );
2870 release_object( fd );
2874 /* set fd disposition information */
2875 DECL_HANDLER(set_fd_disp_info)
2877 struct fd *fd = get_handle_fd_obj( current->process, req->handle, DELETE );
2878 if (fd)
2880 set_fd_disposition( fd, req->unlink );
2881 release_object( fd );
2885 /* set fd name information */
2886 DECL_HANDLER(set_fd_name_info)
2888 struct fd *fd, *root_fd = NULL;
2889 struct unicode_str nt_name;
2891 if (req->namelen > get_req_data_size())
2893 set_error( STATUS_INVALID_PARAMETER );
2894 return;
2896 nt_name.str = get_req_data();
2897 nt_name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2899 if (req->rootdir)
2901 struct dir *root;
2903 if (!(root = get_dir_obj( current->process, req->rootdir, 0 ))) return;
2904 root_fd = get_obj_fd( (struct object *)root );
2905 release_object( root );
2906 if (!root_fd) return;
2909 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2911 set_fd_name( fd, root_fd, (const char *)get_req_data() + req->namelen,
2912 get_req_data_size() - req->namelen, nt_name, req->link, req->replace );
2913 release_object( fd );
2915 if (root_fd) release_object( root_fd );