wined3d: Pass a wined3d_device_context to wined3d_cs_emit_set_light_enable().
[wine.git] / server / fd.c
blob481e9a88f0ff52c945251a84a69239559f041823
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 /* check if events are pending and if yes return which one(s) */
2122 int check_fd_events( struct fd *fd, int events )
2124 struct pollfd pfd;
2126 if (fd->unix_fd == -1) return POLLERR;
2127 if (fd->inode) return events; /* regular files are always signaled */
2129 pfd.fd = fd->unix_fd;
2130 pfd.events = events;
2131 if (poll( &pfd, 1, 0 ) <= 0) return 0;
2132 return pfd.revents;
2135 /* default signaled() routine for objects that poll() on an fd */
2136 int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
2138 struct fd *fd = get_obj_fd( obj );
2139 int ret = fd->signaled;
2140 release_object( fd );
2141 return ret;
2144 int default_fd_get_poll_events( struct fd *fd )
2146 int events = 0;
2148 if (async_waiting( &fd->read_q )) events |= POLLIN;
2149 if (async_waiting( &fd->write_q )) events |= POLLOUT;
2150 return events;
2153 /* default handler for poll() events */
2154 void default_poll_event( struct fd *fd, int event )
2156 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( &fd->read_q, STATUS_ALERTED );
2157 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( &fd->write_q, STATUS_ALERTED );
2159 /* if an error occurred, stop polling this fd to avoid busy-looping */
2160 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
2161 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2164 void fd_queue_async( struct fd *fd, struct async *async, int type )
2166 struct async_queue *queue;
2168 switch (type)
2170 case ASYNC_TYPE_READ:
2171 queue = &fd->read_q;
2172 break;
2173 case ASYNC_TYPE_WRITE:
2174 queue = &fd->write_q;
2175 break;
2176 case ASYNC_TYPE_WAIT:
2177 queue = &fd->wait_q;
2178 break;
2179 default:
2180 queue = NULL;
2181 assert(0);
2184 queue_async( queue, async );
2186 if (type != ASYNC_TYPE_WAIT)
2188 if (!fd->inode)
2189 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2190 else /* regular files are always ready for read and write */
2191 async_wake_up( queue, STATUS_ALERTED );
2195 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
2197 switch (type)
2199 case ASYNC_TYPE_READ:
2200 async_wake_up( &fd->read_q, status );
2201 break;
2202 case ASYNC_TYPE_WRITE:
2203 async_wake_up( &fd->write_q, status );
2204 break;
2205 case ASYNC_TYPE_WAIT:
2206 async_wake_up( &fd->wait_q, status );
2207 break;
2208 default:
2209 assert(0);
2213 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
2215 fd->fd_ops->reselect_async( fd, queue );
2218 void no_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2220 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2223 void default_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2225 fd_queue_async( fd, async, type );
2226 set_error( STATUS_PENDING );
2229 /* default reselect_async() fd routine */
2230 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
2232 if (queue == &fd->read_q || queue == &fd->write_q)
2234 int poll_events = fd->fd_ops->get_poll_events( fd );
2235 int events = check_fd_events( fd, poll_events );
2236 if (events) fd->fd_ops->poll_event( fd, events );
2237 else set_fd_events( fd, poll_events );
2241 static inline int is_valid_mounted_device( struct stat *st )
2243 #if defined(linux) || defined(__sun__)
2244 return S_ISBLK( st->st_mode );
2245 #else
2246 /* disks are char devices on *BSD */
2247 return S_ISCHR( st->st_mode );
2248 #endif
2251 /* close all Unix file descriptors on a device to allow unmounting it */
2252 static void unmount_device( struct fd *device_fd )
2254 unsigned int i;
2255 struct stat st;
2256 struct device *device;
2257 struct inode *inode;
2258 struct fd *fd;
2259 int unix_fd = get_unix_fd( device_fd );
2261 if (unix_fd == -1) return;
2263 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2265 set_error( STATUS_INVALID_PARAMETER );
2266 return;
2269 if (!(device = get_device( st.st_rdev, -1 ))) return;
2271 for (i = 0; i < INODE_HASH_SIZE; i++)
2273 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
2275 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
2277 unmount_fd( fd );
2279 inode_close_pending( inode, 0 );
2282 /* remove it from the hash table */
2283 list_remove( &device->entry );
2284 list_init( &device->entry );
2285 release_object( device );
2288 /* default read() routine */
2289 int no_fd_read( struct fd *fd, struct async *async, file_pos_t pos )
2291 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2292 return 0;
2295 /* default write() routine */
2296 int no_fd_write( struct fd *fd, struct async *async, file_pos_t pos )
2298 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2299 return 0;
2302 /* default flush() routine */
2303 int no_fd_flush( struct fd *fd, struct async *async )
2305 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2306 return 0;
2309 /* default get_file_info() routine */
2310 void no_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
2312 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2315 /* default get_file_info() routine */
2316 void default_fd_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
2318 switch (info_class)
2320 case FileAccessInformation:
2322 FILE_ACCESS_INFORMATION info;
2323 if (get_reply_max_size() < sizeof(info))
2325 set_error( STATUS_INFO_LENGTH_MISMATCH );
2326 return;
2328 info.AccessFlags = get_handle_access( current->process, handle );
2329 set_reply_data( &info, sizeof(info) );
2330 break;
2332 case FileModeInformation:
2334 FILE_MODE_INFORMATION info;
2335 if (get_reply_max_size() < sizeof(info))
2337 set_error( STATUS_INFO_LENGTH_MISMATCH );
2338 return;
2340 info.Mode = fd->options & ( FILE_WRITE_THROUGH
2341 | FILE_SEQUENTIAL_ONLY
2342 | FILE_NO_INTERMEDIATE_BUFFERING
2343 | FILE_SYNCHRONOUS_IO_ALERT
2344 | FILE_SYNCHRONOUS_IO_NONALERT );
2345 set_reply_data( &info, sizeof(info) );
2346 break;
2348 case FileIoCompletionNotificationInformation:
2350 FILE_IO_COMPLETION_NOTIFICATION_INFORMATION info;
2351 if (get_reply_max_size() < sizeof(info))
2353 set_error( STATUS_INFO_LENGTH_MISMATCH );
2354 return;
2356 info.Flags = fd->comp_flags;
2357 set_reply_data( &info, sizeof(info) );
2358 break;
2360 default:
2361 set_error( STATUS_NOT_IMPLEMENTED );
2365 /* default get_volume_info() routine */
2366 int no_fd_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
2368 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2369 return 0;
2372 /* default ioctl() routine */
2373 int no_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2375 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2376 return 0;
2379 /* default ioctl() routine */
2380 int default_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2382 switch(code)
2384 case FSCTL_DISMOUNT_VOLUME:
2385 unmount_device( fd );
2386 return 1;
2387 default:
2388 set_error( STATUS_NOT_SUPPORTED );
2389 return 0;
2393 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2394 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
2395 unsigned int access )
2397 struct fd *fd = NULL;
2398 struct object *obj;
2400 if ((obj = get_handle_obj( process, handle, access, NULL )))
2402 fd = get_obj_fd( obj );
2403 release_object( obj );
2405 return fd;
2408 static int is_dir_empty( int fd )
2410 DIR *dir;
2411 int empty;
2412 struct dirent *de;
2414 if ((fd = dup( fd )) == -1)
2415 return -1;
2417 if (!(dir = fdopendir( fd )))
2419 close( fd );
2420 return -1;
2423 empty = 1;
2424 while (empty && (de = readdir( dir )))
2426 if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." )) continue;
2427 empty = 0;
2429 closedir( dir );
2430 return empty;
2433 /* set disposition for the fd */
2434 static void set_fd_disposition( struct fd *fd, int unlink )
2436 struct stat st;
2438 if (!fd->inode)
2440 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2441 return;
2444 if (fd->unix_fd == -1)
2446 set_error( fd->no_fd_status );
2447 return;
2450 if (unlink)
2452 if (fstat( fd->unix_fd, &st ) == -1)
2454 file_set_error();
2455 return;
2457 if (S_ISREG( st.st_mode )) /* can't unlink files we don't have permission to write */
2459 if (!(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2461 set_error( STATUS_CANNOT_DELETE );
2462 return;
2465 else if (S_ISDIR( st.st_mode )) /* can't remove non-empty directories */
2467 switch (is_dir_empty( fd->unix_fd ))
2469 case -1:
2470 file_set_error();
2471 return;
2472 case 0:
2473 set_error( STATUS_DIRECTORY_NOT_EMPTY );
2474 return;
2477 else /* can't unlink special files */
2479 set_error( STATUS_INVALID_PARAMETER );
2480 return;
2484 fd->closed->unlink = unlink ? 1 : 0;
2485 if (fd->options & FILE_DELETE_ON_CLOSE)
2486 fd->closed->unlink = -1;
2489 /* set new name for the fd */
2490 static void set_fd_name( struct fd *fd, struct fd *root, const char *nameptr, data_size_t len,
2491 struct unicode_str nt_name, int create_link, int replace )
2493 struct inode *inode;
2494 struct stat st, st2;
2495 char *name;
2497 if (!fd->inode || !fd->unix_name)
2499 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2500 return;
2502 if (fd->unix_fd == -1)
2504 set_error( fd->no_fd_status );
2505 return;
2508 if (!len || ((nameptr[0] == '/') ^ !root))
2510 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
2511 return;
2513 if (!(name = mem_alloc( len + 1 ))) return;
2514 memcpy( name, nameptr, len );
2515 name[len] = 0;
2517 if (root)
2519 char *combined_name = dup_fd_name( root, name );
2520 if (!combined_name)
2522 set_error( STATUS_NO_MEMORY );
2523 goto failed;
2525 free( name );
2526 name = combined_name;
2529 /* when creating a hard link, source cannot be a dir */
2530 if (create_link && !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode ))
2532 set_error( STATUS_FILE_IS_A_DIRECTORY );
2533 goto failed;
2536 if (!stat( name, &st ))
2538 if (!fstat( fd->unix_fd, &st2 ) && st.st_ino == st2.st_ino && st.st_dev == st2.st_dev)
2540 if (create_link && !replace) set_error( STATUS_OBJECT_NAME_COLLISION );
2541 free( name );
2542 return;
2545 if (!replace)
2547 set_error( STATUS_OBJECT_NAME_COLLISION );
2548 goto failed;
2551 /* can't replace directories or special files */
2552 if (!S_ISREG( st.st_mode ))
2554 set_error( STATUS_ACCESS_DENIED );
2555 goto failed;
2558 /* can't replace an opened file */
2559 if ((inode = get_inode( st.st_dev, st.st_ino, -1 )))
2561 int is_empty = list_empty( &inode->open );
2562 release_object( inode );
2563 if (!is_empty)
2565 set_error( STATUS_ACCESS_DENIED );
2566 goto failed;
2570 /* link() expects that the target doesn't exist */
2571 /* rename() cannot replace files with directories */
2572 if (create_link || S_ISDIR( st2.st_mode ))
2574 if (unlink( name ))
2576 file_set_error();
2577 goto failed;
2582 if (create_link)
2584 if (link( fd->unix_name, name ))
2585 file_set_error();
2586 free( name );
2587 return;
2590 if (rename( fd->unix_name, name ))
2592 file_set_error();
2593 goto failed;
2596 if (is_file_executable( fd->unix_name ) != is_file_executable( name ) && !fstat( fd->unix_fd, &st ))
2598 if (is_file_executable( name ))
2599 /* set executable bit where read bit is set */
2600 st.st_mode |= (st.st_mode & 0444) >> 2;
2601 else
2602 st.st_mode &= ~0111;
2603 fchmod( fd->unix_fd, st.st_mode );
2606 free( fd->nt_name );
2607 fd->nt_name = dup_nt_name( root, nt_name, &fd->nt_namelen );
2608 free( fd->unix_name );
2609 fd->closed->unix_name = fd->unix_name = realpath( name, NULL );
2610 free( name );
2611 if (!fd->unix_name)
2612 set_error( STATUS_NO_MEMORY );
2613 return;
2615 failed:
2616 free( name );
2619 struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
2621 *p_key = fd->comp_key;
2622 return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
2625 void fd_copy_completion( struct fd *src, struct fd *dst )
2627 assert( !dst->completion );
2628 dst->completion = fd_get_completion( src, &dst->comp_key );
2629 dst->comp_flags = src->comp_flags;
2632 /* flush a file buffers */
2633 DECL_HANDLER(flush)
2635 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, 0 );
2636 struct async *async;
2638 if (!fd) return;
2640 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2642 reply->event = async_handoff( async, fd->fd_ops->flush( fd, async ), NULL, 1 );
2643 release_object( async );
2645 release_object( fd );
2648 /* query file info */
2649 DECL_HANDLER(get_file_info)
2651 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2653 if (fd)
2655 fd->fd_ops->get_file_info( fd, req->handle, req->info_class );
2656 release_object( fd );
2660 /* query volume info */
2661 DECL_HANDLER(get_volume_info)
2663 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2664 struct async *async;
2666 if (!fd) return;
2668 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2670 reply->wait = async_handoff( async, fd->fd_ops->get_volume_info( fd, async, req->info_class ), NULL, 1 );
2671 release_object( async );
2673 release_object( fd );
2676 /* open a file object */
2677 DECL_HANDLER(open_file_object)
2679 struct unicode_str name = get_req_unicode_str();
2680 struct object *obj, *result, *root = NULL;
2682 if (req->rootdir && !(root = get_handle_obj( current->process, req->rootdir, 0, NULL ))) return;
2684 obj = open_named_object( root, NULL, &name, req->attributes );
2685 if (root) release_object( root );
2686 if (!obj) return;
2688 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
2690 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
2691 release_object( result );
2693 release_object( obj );
2696 /* get the Unix name from a file handle */
2697 DECL_HANDLER(get_handle_unix_name)
2699 struct fd *fd;
2701 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2703 if (fd->unix_name)
2705 data_size_t name_len = strlen( fd->unix_name );
2706 reply->name_len = name_len;
2707 if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
2708 else set_error( STATUS_BUFFER_OVERFLOW );
2710 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
2711 release_object( fd );
2715 /* get a Unix fd to access a file */
2716 DECL_HANDLER(get_handle_fd)
2718 struct fd *fd;
2720 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2722 int unix_fd = get_unix_fd( fd );
2723 reply->cacheable = fd->cacheable;
2724 if (unix_fd != -1)
2726 reply->type = fd->fd_ops->get_fd_type( fd );
2727 reply->options = fd->options;
2728 reply->access = get_handle_access( current->process, req->handle );
2729 send_client_fd( current->process, unix_fd, req->handle );
2731 release_object( fd );
2735 /* perform a read on a file object */
2736 DECL_HANDLER(read)
2738 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_READ_DATA );
2739 struct async *async;
2741 if (!fd) return;
2743 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2745 reply->wait = async_handoff( async, fd->fd_ops->read( fd, async, req->pos ), NULL, 0 );
2746 reply->options = fd->options;
2747 release_object( async );
2749 release_object( fd );
2752 /* perform a write on a file object */
2753 DECL_HANDLER(write)
2755 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_WRITE_DATA );
2756 struct async *async;
2758 if (!fd) return;
2760 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2762 reply->wait = async_handoff( async, fd->fd_ops->write( fd, async, req->pos ), &reply->size, 0 );
2763 reply->options = fd->options;
2764 release_object( async );
2766 release_object( fd );
2769 /* perform an ioctl on a file */
2770 DECL_HANDLER(ioctl)
2772 unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
2773 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
2774 struct async *async;
2776 if (!fd) return;
2778 if ((async = create_request_async( fd, fd->comp_flags, &req->async )))
2780 reply->wait = async_handoff( async, fd->fd_ops->ioctl( fd, req->code, async ), NULL, 0 );
2781 reply->options = fd->options;
2782 release_object( async );
2784 release_object( fd );
2787 /* create / reschedule an async I/O */
2788 DECL_HANDLER(register_async)
2790 unsigned int access;
2791 struct async *async;
2792 struct fd *fd;
2794 switch(req->type)
2796 case ASYNC_TYPE_READ:
2797 access = FILE_READ_DATA;
2798 break;
2799 case ASYNC_TYPE_WRITE:
2800 access = FILE_WRITE_DATA;
2801 break;
2802 default:
2803 set_error( STATUS_INVALID_PARAMETER );
2804 return;
2807 if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
2809 if (get_unix_fd( fd ) != -1 && (async = create_async( fd, current, &req->async, NULL )))
2811 fd->fd_ops->queue_async( fd, async, req->type, req->count );
2812 release_object( async );
2814 release_object( fd );
2818 /* attach completion object to a fd */
2819 DECL_HANDLER(set_completion_info)
2821 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2823 if (fd)
2825 if (is_fd_overlapped( fd ) && !fd->completion)
2827 fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
2828 fd->comp_key = req->ckey;
2830 else set_error( STATUS_INVALID_PARAMETER );
2831 release_object( fd );
2835 /* push new completion msg into a completion queue attached to the fd */
2836 DECL_HANDLER(add_fd_completion)
2838 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2839 if (fd)
2841 if (fd->completion && (req->async || !(fd->comp_flags & FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)))
2842 add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
2843 release_object( fd );
2847 /* set fd completion information */
2848 DECL_HANDLER(set_fd_completion_mode)
2850 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2851 if (fd)
2853 if (is_fd_overlapped( fd ))
2855 if (req->flags & FILE_SKIP_SET_EVENT_ON_HANDLE)
2856 set_fd_signaled( fd, 0 );
2857 /* removing flags is not allowed */
2858 fd->comp_flags |= req->flags & ( FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
2859 | FILE_SKIP_SET_EVENT_ON_HANDLE
2860 | FILE_SKIP_SET_USER_EVENT_ON_FAST_IO );
2862 else
2863 set_error( STATUS_INVALID_PARAMETER );
2864 release_object( fd );
2868 /* set fd disposition information */
2869 DECL_HANDLER(set_fd_disp_info)
2871 struct fd *fd = get_handle_fd_obj( current->process, req->handle, DELETE );
2872 if (fd)
2874 set_fd_disposition( fd, req->unlink );
2875 release_object( fd );
2879 /* set fd name information */
2880 DECL_HANDLER(set_fd_name_info)
2882 struct fd *fd, *root_fd = NULL;
2883 struct unicode_str nt_name;
2885 if (req->namelen > get_req_data_size())
2887 set_error( STATUS_INVALID_PARAMETER );
2888 return;
2890 nt_name.str = get_req_data();
2891 nt_name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2893 if (req->rootdir)
2895 struct dir *root;
2897 if (!(root = get_dir_obj( current->process, req->rootdir, 0 ))) return;
2898 root_fd = get_obj_fd( (struct object *)root );
2899 release_object( root );
2900 if (!root_fd) return;
2903 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2905 set_fd_name( fd, root_fd, (const char *)get_req_data() + req->namelen,
2906 get_req_data_size() - req->namelen, nt_name, req->link, req->replace );
2907 release_object( fd );
2909 if (root_fd) release_object( root_fd );