mpr: Don't stop enumeration on the first failing network provider.
[wine.git] / server / fd.c
blobf05556a1332f39d666328475e63db6616911a2cf
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 <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <signal.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #ifdef HAVE_POLL_H
35 #include <poll.h>
36 #endif
37 #ifdef HAVE_SYS_POLL_H
38 #include <sys/poll.h>
39 #endif
40 #ifdef HAVE_LINUX_MAJOR_H
41 #include <linux/major.h>
42 #endif
43 #ifdef HAVE_SYS_STATVFS_H
44 #include <sys/statvfs.h>
45 #endif
46 #ifdef HAVE_SYS_VFS_H
47 /* Work around a conflict with Solaris' system list defined in sys/list.h. */
48 #define list SYSLIST
49 #define list_next SYSLIST_NEXT
50 #define list_prev SYSLIST_PREV
51 #define list_head SYSLIST_HEAD
52 #define list_tail SYSLIST_TAIL
53 #define list_move_tail SYSLIST_MOVE_TAIL
54 #define list_remove SYSLIST_REMOVE
55 #include <sys/vfs.h>
56 #undef list
57 #undef list_next
58 #undef list_prev
59 #undef list_head
60 #undef list_tail
61 #undef list_move_tail
62 #undef list_remove
63 #endif
64 #ifdef HAVE_SYS_PARAM_H
65 #include <sys/param.h>
66 #endif
67 #ifdef HAVE_SYS_MOUNT_H
68 #include <sys/mount.h>
69 #endif
70 #ifdef HAVE_SYS_STATFS_H
71 #include <sys/statfs.h>
72 #endif
73 #ifdef HAVE_SYS_SYSCTL_H
74 #include <sys/sysctl.h>
75 #endif
76 #ifdef HAVE_SYS_EVENT_H
77 #include <sys/event.h>
78 #undef LIST_INIT
79 #undef LIST_ENTRY
80 #endif
81 #ifdef HAVE_STDINT_H
82 #include <stdint.h>
83 #endif
84 #include <sys/stat.h>
85 #include <sys/time.h>
86 #ifdef MAJOR_IN_MKDEV
87 #include <sys/mkdev.h>
88 #elif defined(MAJOR_IN_SYSMACROS)
89 #include <sys/sysmacros.h>
90 #endif
91 #include <sys/types.h>
92 #include <unistd.h>
93 #ifdef HAVE_SYS_SYSCALL_H
94 #include <sys/syscall.h>
95 #endif
97 #include "ntstatus.h"
98 #define WIN32_NO_STATUS
99 #include "object.h"
100 #include "file.h"
101 #include "handle.h"
102 #include "process.h"
103 #include "request.h"
105 #include "winternl.h"
106 #include "winioctl.h"
108 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
109 # include <sys/epoll.h>
110 # define USE_EPOLL
111 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
112 # define USE_EPOLL
113 # define EPOLLIN POLLIN
114 # define EPOLLOUT POLLOUT
115 # define EPOLLERR POLLERR
116 # define EPOLLHUP POLLHUP
117 # define EPOLL_CTL_ADD 1
118 # define EPOLL_CTL_DEL 2
119 # define EPOLL_CTL_MOD 3
121 typedef union epoll_data
123 void *ptr;
124 int fd;
125 uint32_t u32;
126 uint64_t u64;
127 } epoll_data_t;
129 struct epoll_event
131 uint32_t events;
132 epoll_data_t data;
135 static inline int epoll_create( int size )
137 return syscall( 254 /*NR_epoll_create*/, size );
140 static inline int epoll_ctl( int epfd, int op, int fd, const struct epoll_event *event )
142 return syscall( 255 /*NR_epoll_ctl*/, epfd, op, fd, event );
145 static inline int epoll_wait( int epfd, struct epoll_event *events, int maxevents, int timeout )
147 return syscall( 256 /*NR_epoll_wait*/, epfd, events, maxevents, timeout );
150 #endif /* linux && __i386__ && HAVE_STDINT_H */
152 #if defined(HAVE_PORT_H) && defined(HAVE_PORT_CREATE)
153 # include <port.h>
154 # define USE_EVENT_PORTS
155 #endif /* HAVE_PORT_H && HAVE_PORT_CREATE */
157 /* Because of the stupid Posix locking semantics, we need to keep
158 * track of all file descriptors referencing a given file, and not
159 * close a single one until all the locks are gone (sigh).
162 /* file descriptor object */
164 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
165 struct closed_fd
167 struct list entry; /* entry in inode closed list */
168 int unix_fd; /* the unix file descriptor */
169 int unlink; /* whether to unlink on close */
170 char *unix_name; /* name to unlink on close, points to parent fd unix_name */
173 struct fd
175 struct object obj; /* object header */
176 const struct fd_ops *fd_ops; /* file descriptor operations */
177 struct inode *inode; /* inode that this fd belongs to */
178 struct list inode_entry; /* entry in inode fd list */
179 struct closed_fd *closed; /* structure to store the unix fd at destroy time */
180 struct object *user; /* object using this file descriptor */
181 struct list locks; /* list of locks on this fd */
182 unsigned int access; /* file access (FILE_READ_DATA etc.) */
183 unsigned int options; /* file options (FILE_DELETE_ON_CLOSE, FILE_SYNCHRONOUS...) */
184 unsigned int sharing; /* file sharing mode */
185 char *unix_name; /* unix file name */
186 int unix_fd; /* unix file descriptor */
187 unsigned int no_fd_status;/* status to return when unix_fd is -1 */
188 unsigned int cacheable :1;/* can the fd be cached on the client side? */
189 unsigned int signaled :1; /* is the fd signaled? */
190 unsigned int fs_locks :1; /* can we use filesystem locks for this fd? */
191 int poll_index; /* index of fd in poll array */
192 struct async_queue *read_q; /* async readers of this fd */
193 struct async_queue *write_q; /* async writers of this fd */
194 struct async_queue *wait_q; /* other async waiters of this fd */
195 struct completion *completion; /* completion object attached to this fd */
196 apc_param_t comp_key; /* completion key to set in completion events */
199 static void fd_dump( struct object *obj, int verbose );
200 static void fd_destroy( struct object *obj );
202 static const struct object_ops fd_ops =
204 sizeof(struct fd), /* size */
205 fd_dump, /* dump */
206 no_get_type, /* get_type */
207 no_add_queue, /* add_queue */
208 NULL, /* remove_queue */
209 NULL, /* signaled */
210 NULL, /* satisfied */
211 no_signal, /* signal */
212 no_get_fd, /* get_fd */
213 no_map_access, /* map_access */
214 default_get_sd, /* get_sd */
215 default_set_sd, /* set_sd */
216 no_lookup_name, /* lookup_name */
217 no_link_name, /* link_name */
218 NULL, /* unlink_name */
219 no_open_file, /* open_file */
220 no_close_handle, /* close_handle */
221 fd_destroy /* destroy */
224 /* device object */
226 #define DEVICE_HASH_SIZE 7
227 #define INODE_HASH_SIZE 17
229 struct device
231 struct object obj; /* object header */
232 struct list entry; /* entry in device hash list */
233 dev_t dev; /* device number */
234 int removable; /* removable device? (or -1 if unknown) */
235 struct list inode_hash[INODE_HASH_SIZE]; /* inodes hash table */
238 static void device_dump( struct object *obj, int verbose );
239 static void device_destroy( struct object *obj );
241 static const struct object_ops device_ops =
243 sizeof(struct device), /* size */
244 device_dump, /* dump */
245 no_get_type, /* get_type */
246 no_add_queue, /* add_queue */
247 NULL, /* remove_queue */
248 NULL, /* signaled */
249 NULL, /* satisfied */
250 no_signal, /* signal */
251 no_get_fd, /* get_fd */
252 no_map_access, /* map_access */
253 default_get_sd, /* get_sd */
254 default_set_sd, /* set_sd */
255 no_lookup_name, /* lookup_name */
256 no_link_name, /* link_name */
257 NULL, /* unlink_name */
258 no_open_file, /* open_file */
259 no_close_handle, /* close_handle */
260 device_destroy /* destroy */
263 /* inode object */
265 struct inode
267 struct object obj; /* object header */
268 struct list entry; /* inode hash list entry */
269 struct device *device; /* device containing this inode */
270 ino_t ino; /* inode number */
271 struct list open; /* list of open file descriptors */
272 struct list locks; /* list of file locks */
273 struct list closed; /* list of file descriptors to close at destroy time */
276 static void inode_dump( struct object *obj, int verbose );
277 static void inode_destroy( struct object *obj );
279 static const struct object_ops inode_ops =
281 sizeof(struct inode), /* size */
282 inode_dump, /* dump */
283 no_get_type, /* get_type */
284 no_add_queue, /* add_queue */
285 NULL, /* remove_queue */
286 NULL, /* signaled */
287 NULL, /* satisfied */
288 no_signal, /* signal */
289 no_get_fd, /* get_fd */
290 no_map_access, /* map_access */
291 default_get_sd, /* get_sd */
292 default_set_sd, /* set_sd */
293 no_lookup_name, /* lookup_name */
294 no_link_name, /* link_name */
295 NULL, /* unlink_name */
296 no_open_file, /* open_file */
297 no_close_handle, /* close_handle */
298 inode_destroy /* destroy */
301 /* file lock object */
303 struct file_lock
305 struct object obj; /* object header */
306 struct fd *fd; /* fd owning this lock */
307 struct list fd_entry; /* entry in list of locks on a given fd */
308 struct list inode_entry; /* entry in inode list of locks */
309 int shared; /* shared lock? */
310 file_pos_t start; /* locked region is interval [start;end) */
311 file_pos_t end;
312 struct process *process; /* process owning this lock */
313 struct list proc_entry; /* entry in list of locks owned by the process */
316 static void file_lock_dump( struct object *obj, int verbose );
317 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry );
319 static const struct object_ops file_lock_ops =
321 sizeof(struct file_lock), /* size */
322 file_lock_dump, /* dump */
323 no_get_type, /* get_type */
324 add_queue, /* add_queue */
325 remove_queue, /* remove_queue */
326 file_lock_signaled, /* signaled */
327 no_satisfied, /* satisfied */
328 no_signal, /* signal */
329 no_get_fd, /* get_fd */
330 no_map_access, /* map_access */
331 default_get_sd, /* get_sd */
332 default_set_sd, /* set_sd */
333 no_lookup_name, /* lookup_name */
334 no_link_name, /* link_name */
335 NULL, /* unlink_name */
336 no_open_file, /* open_file */
337 no_close_handle, /* close_handle */
338 no_destroy /* destroy */
342 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
343 #define FILE_POS_T_MAX (~(file_pos_t)0)
345 static file_pos_t max_unix_offset = OFF_T_MAX;
347 #define DUMP_LONG_LONG(val) do { \
348 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
349 fprintf( stderr, "%lx%08lx", (unsigned long)((unsigned long long)(val) >> 32), (unsigned long)(val) ); \
350 else \
351 fprintf( stderr, "%lx", (unsigned long)(val) ); \
352 } while (0)
356 /****************************************************************/
357 /* timeouts support */
359 struct timeout_user
361 struct list entry; /* entry in sorted timeout list */
362 timeout_t when; /* timeout expiry (absolute time) */
363 timeout_callback callback; /* callback function */
364 void *private; /* callback private data */
367 static struct list timeout_list = LIST_INIT(timeout_list); /* sorted timeouts list */
368 timeout_t current_time;
370 static inline void set_current_time(void)
372 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
373 struct timeval now;
374 gettimeofday( &now, NULL );
375 current_time = (timeout_t)now.tv_sec * TICKS_PER_SEC + now.tv_usec * 10 + ticks_1601_to_1970;
378 /* add a timeout user */
379 struct timeout_user *add_timeout_user( timeout_t when, timeout_callback func, void *private )
381 struct timeout_user *user;
382 struct list *ptr;
384 if (!(user = mem_alloc( sizeof(*user) ))) return NULL;
385 user->when = (when > 0) ? when : current_time - when;
386 user->callback = func;
387 user->private = private;
389 /* Now insert it in the linked list */
391 LIST_FOR_EACH( ptr, &timeout_list )
393 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
394 if (timeout->when >= user->when) break;
396 list_add_before( ptr, &user->entry );
397 return user;
400 /* remove a timeout user */
401 void remove_timeout_user( struct timeout_user *user )
403 list_remove( &user->entry );
404 free( user );
407 /* return a text description of a timeout for debugging purposes */
408 const char *get_timeout_str( timeout_t timeout )
410 static char buffer[64];
411 long secs, nsecs;
413 if (!timeout) return "0";
414 if (timeout == TIMEOUT_INFINITE) return "infinite";
416 if (timeout < 0) /* relative */
418 secs = -timeout / TICKS_PER_SEC;
419 nsecs = -timeout % TICKS_PER_SEC;
420 sprintf( buffer, "+%ld.%07ld", secs, nsecs );
422 else /* absolute */
424 secs = (timeout - current_time) / TICKS_PER_SEC;
425 nsecs = (timeout - current_time) % TICKS_PER_SEC;
426 if (nsecs < 0)
428 nsecs += TICKS_PER_SEC;
429 secs--;
431 if (secs >= 0)
432 sprintf( buffer, "%x%08x (+%ld.%07ld)",
433 (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs );
434 else
435 sprintf( buffer, "%x%08x (-%ld.%07ld)",
436 (unsigned int)(timeout >> 32), (unsigned int)timeout,
437 -(secs + 1), TICKS_PER_SEC - nsecs );
439 return buffer;
443 /****************************************************************/
444 /* poll support */
446 static struct fd **poll_users; /* users array */
447 static struct pollfd *pollfd; /* poll fd array */
448 static int nb_users; /* count of array entries actually in use */
449 static int active_users; /* current number of active users */
450 static int allocated_users; /* count of allocated entries in the array */
451 static struct fd **freelist; /* list of free entries in the array */
453 static int get_next_timeout(void);
455 static inline void fd_poll_event( struct fd *fd, int event )
457 fd->fd_ops->poll_event( fd, event );
460 #ifdef USE_EPOLL
462 static int epoll_fd = -1;
464 static inline void init_epoll(void)
466 epoll_fd = epoll_create( 128 );
469 /* set the events that epoll waits for on this fd; helper for set_fd_events */
470 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
472 struct epoll_event ev;
473 int ctl;
475 if (epoll_fd == -1) return;
477 if (events == -1) /* stop waiting on this fd completely */
479 if (pollfd[user].fd == -1) return; /* already removed */
480 ctl = EPOLL_CTL_DEL;
482 else if (pollfd[user].fd == -1)
484 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
485 ctl = EPOLL_CTL_ADD;
487 else
489 if (pollfd[user].events == events) return; /* nothing to do */
490 ctl = EPOLL_CTL_MOD;
493 ev.events = events;
494 memset(&ev.data, 0, sizeof(ev.data));
495 ev.data.u32 = user;
497 if (epoll_ctl( epoll_fd, ctl, fd->unix_fd, &ev ) == -1)
499 if (errno == ENOMEM) /* not enough memory, give up on epoll */
501 close( epoll_fd );
502 epoll_fd = -1;
504 else perror( "epoll_ctl" ); /* should not happen */
508 static inline void remove_epoll_user( struct fd *fd, int user )
510 if (epoll_fd == -1) return;
512 if (pollfd[user].fd != -1)
514 struct epoll_event dummy;
515 epoll_ctl( epoll_fd, EPOLL_CTL_DEL, fd->unix_fd, &dummy );
519 static inline void main_loop_epoll(void)
521 int i, ret, timeout;
522 struct epoll_event events[128];
524 assert( POLLIN == EPOLLIN );
525 assert( POLLOUT == EPOLLOUT );
526 assert( POLLERR == EPOLLERR );
527 assert( POLLHUP == EPOLLHUP );
529 if (epoll_fd == -1) return;
531 while (active_users)
533 timeout = get_next_timeout();
535 if (!active_users) break; /* last user removed by a timeout */
536 if (epoll_fd == -1) break; /* an error occurred with epoll */
538 ret = epoll_wait( epoll_fd, events, sizeof(events)/sizeof(events[0]), timeout );
539 set_current_time();
541 /* put the events into the pollfd array first, like poll does */
542 for (i = 0; i < ret; i++)
544 int user = events[i].data.u32;
545 pollfd[user].revents = events[i].events;
548 /* read events from the pollfd array, as set_fd_events may modify them */
549 for (i = 0; i < ret; i++)
551 int user = events[i].data.u32;
552 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
557 #elif defined(HAVE_KQUEUE)
559 static int kqueue_fd = -1;
561 static inline void init_epoll(void)
563 #ifdef __APPLE__ /* kqueue support is broken in Mac OS < 10.5 */
564 int mib[2];
565 char release[32];
566 size_t len = sizeof(release);
568 mib[0] = CTL_KERN;
569 mib[1] = KERN_OSRELEASE;
570 if (sysctl( mib, 2, release, &len, NULL, 0 ) == -1) return;
571 if (atoi(release) < 9) return;
572 #endif
573 kqueue_fd = kqueue();
576 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
578 struct kevent ev[2];
580 if (kqueue_fd == -1) return;
582 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, 0, NOTE_LOWAT, 1, (void *)(long)user );
583 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, 0, NOTE_LOWAT, 1, (void *)(long)user );
585 if (events == -1) /* stop waiting on this fd completely */
587 if (pollfd[user].fd == -1) return; /* already removed */
588 ev[0].flags |= EV_DELETE;
589 ev[1].flags |= EV_DELETE;
591 else if (pollfd[user].fd == -1)
593 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
594 ev[0].flags |= EV_ADD | ((events & POLLIN) ? EV_ENABLE : EV_DISABLE);
595 ev[1].flags |= EV_ADD | ((events & POLLOUT) ? EV_ENABLE : EV_DISABLE);
597 else
599 if (pollfd[user].events == events) return; /* nothing to do */
600 ev[0].flags |= (events & POLLIN) ? EV_ENABLE : EV_DISABLE;
601 ev[1].flags |= (events & POLLOUT) ? EV_ENABLE : EV_DISABLE;
604 if (kevent( kqueue_fd, ev, 2, NULL, 0, NULL ) == -1)
606 if (errno == ENOMEM) /* not enough memory, give up on kqueue */
608 close( kqueue_fd );
609 kqueue_fd = -1;
611 else perror( "kevent" ); /* should not happen */
615 static inline void remove_epoll_user( struct fd *fd, int user )
617 if (kqueue_fd == -1) return;
619 if (pollfd[user].fd != -1)
621 struct kevent ev[2];
623 EV_SET( &ev[0], fd->unix_fd, EVFILT_READ, EV_DELETE, 0, 0, 0 );
624 EV_SET( &ev[1], fd->unix_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0 );
625 kevent( kqueue_fd, ev, 2, NULL, 0, NULL );
629 static inline void main_loop_epoll(void)
631 int i, ret, timeout;
632 struct kevent events[128];
634 if (kqueue_fd == -1) return;
636 while (active_users)
638 timeout = get_next_timeout();
640 if (!active_users) break; /* last user removed by a timeout */
641 if (kqueue_fd == -1) break; /* an error occurred with kqueue */
643 if (timeout != -1)
645 struct timespec ts;
647 ts.tv_sec = timeout / 1000;
648 ts.tv_nsec = (timeout % 1000) * 1000000;
649 ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), &ts );
651 else ret = kevent( kqueue_fd, NULL, 0, events, sizeof(events)/sizeof(events[0]), NULL );
653 set_current_time();
655 /* put the events into the pollfd array first, like poll does */
656 for (i = 0; i < ret; i++)
658 long user = (long)events[i].udata;
659 pollfd[user].revents = 0;
661 for (i = 0; i < ret; i++)
663 long user = (long)events[i].udata;
664 if (events[i].filter == EVFILT_READ) pollfd[user].revents |= POLLIN;
665 else if (events[i].filter == EVFILT_WRITE) pollfd[user].revents |= POLLOUT;
666 if (events[i].flags & EV_EOF) pollfd[user].revents |= POLLHUP;
667 if (events[i].flags & EV_ERROR) pollfd[user].revents |= POLLERR;
670 /* read events from the pollfd array, as set_fd_events may modify them */
671 for (i = 0; i < ret; i++)
673 long user = (long)events[i].udata;
674 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
675 pollfd[user].revents = 0;
680 #elif defined(USE_EVENT_PORTS)
682 static int port_fd = -1;
684 static inline void init_epoll(void)
686 port_fd = port_create();
689 static inline void set_fd_epoll_events( struct fd *fd, int user, int events )
691 int ret;
693 if (port_fd == -1) return;
695 if (events == -1) /* stop waiting on this fd completely */
697 if (pollfd[user].fd == -1) return; /* already removed */
698 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
700 else if (pollfd[user].fd == -1)
702 if (pollfd[user].events) return; /* stopped waiting on it, don't restart */
703 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
705 else
707 if (pollfd[user].events == events) return; /* nothing to do */
708 ret = port_associate( port_fd, PORT_SOURCE_FD, fd->unix_fd, events, (void *)user );
711 if (ret == -1)
713 if (errno == ENOMEM) /* not enough memory, give up on port_associate */
715 close( port_fd );
716 port_fd = -1;
718 else perror( "port_associate" ); /* should not happen */
722 static inline void remove_epoll_user( struct fd *fd, int user )
724 if (port_fd == -1) return;
726 if (pollfd[user].fd != -1)
728 port_dissociate( port_fd, PORT_SOURCE_FD, fd->unix_fd );
732 static inline void main_loop_epoll(void)
734 int i, nget, ret, timeout;
735 port_event_t events[128];
737 if (port_fd == -1) return;
739 while (active_users)
741 timeout = get_next_timeout();
742 nget = 1;
744 if (!active_users) break; /* last user removed by a timeout */
745 if (port_fd == -1) break; /* an error occurred with event completion */
747 if (timeout != -1)
749 struct timespec ts;
751 ts.tv_sec = timeout / 1000;
752 ts.tv_nsec = (timeout % 1000) * 1000000;
753 ret = port_getn( port_fd, events, sizeof(events)/sizeof(events[0]), &nget, &ts );
755 else ret = port_getn( port_fd, events, sizeof(events)/sizeof(events[0]), &nget, NULL );
757 if (ret == -1) break; /* an error occurred with event completion */
759 set_current_time();
761 /* put the events into the pollfd array first, like poll does */
762 for (i = 0; i < nget; i++)
764 long user = (long)events[i].portev_user;
765 pollfd[user].revents = events[i].portev_events;
768 /* read events from the pollfd array, as set_fd_events may modify them */
769 for (i = 0; i < nget; i++)
771 long user = (long)events[i].portev_user;
772 if (pollfd[user].revents) fd_poll_event( poll_users[user], pollfd[user].revents );
773 /* if we are still interested, reassociate the fd */
774 if (pollfd[user].fd != -1) {
775 port_associate( port_fd, PORT_SOURCE_FD, pollfd[user].fd, pollfd[user].events, (void *)user );
781 #else /* HAVE_KQUEUE */
783 static inline void init_epoll(void) { }
784 static inline void set_fd_epoll_events( struct fd *fd, int user, int events ) { }
785 static inline void remove_epoll_user( struct fd *fd, int user ) { }
786 static inline void main_loop_epoll(void) { }
788 #endif /* USE_EPOLL */
791 /* add a user in the poll array and return its index, or -1 on failure */
792 static int add_poll_user( struct fd *fd )
794 int ret;
795 if (freelist)
797 ret = freelist - poll_users;
798 freelist = (struct fd **)poll_users[ret];
800 else
802 if (nb_users == allocated_users)
804 struct fd **newusers;
805 struct pollfd *newpoll;
806 int new_count = allocated_users ? (allocated_users + allocated_users / 2) : 16;
807 if (!(newusers = realloc( poll_users, new_count * sizeof(*poll_users) ))) return -1;
808 if (!(newpoll = realloc( pollfd, new_count * sizeof(*pollfd) )))
810 if (allocated_users)
811 poll_users = newusers;
812 else
813 free( newusers );
814 return -1;
816 poll_users = newusers;
817 pollfd = newpoll;
818 if (!allocated_users) init_epoll();
819 allocated_users = new_count;
821 ret = nb_users++;
823 pollfd[ret].fd = -1;
824 pollfd[ret].events = 0;
825 pollfd[ret].revents = 0;
826 poll_users[ret] = fd;
827 active_users++;
828 return ret;
831 /* remove a user from the poll list */
832 static void remove_poll_user( struct fd *fd, int user )
834 assert( user >= 0 );
835 assert( poll_users[user] == fd );
837 remove_epoll_user( fd, user );
838 pollfd[user].fd = -1;
839 pollfd[user].events = 0;
840 pollfd[user].revents = 0;
841 poll_users[user] = (struct fd *)freelist;
842 freelist = &poll_users[user];
843 active_users--;
846 /* process pending timeouts and return the time until the next timeout, in milliseconds */
847 static int get_next_timeout(void)
849 if (!list_empty( &timeout_list ))
851 struct list expired_list, *ptr;
853 /* first remove all expired timers from the list */
855 list_init( &expired_list );
856 while ((ptr = list_head( &timeout_list )) != NULL)
858 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
860 if (timeout->when <= current_time)
862 list_remove( &timeout->entry );
863 list_add_tail( &expired_list, &timeout->entry );
865 else break;
868 /* now call the callback for all the removed timers */
870 while ((ptr = list_head( &expired_list )) != NULL)
872 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
873 list_remove( &timeout->entry );
874 timeout->callback( timeout->private );
875 free( timeout );
878 if ((ptr = list_head( &timeout_list )) != NULL)
880 struct timeout_user *timeout = LIST_ENTRY( ptr, struct timeout_user, entry );
881 int diff = (timeout->when - current_time + 9999) / 10000;
882 if (diff < 0) diff = 0;
883 return diff;
886 return -1; /* no pending timeouts */
889 /* server main poll() loop */
890 void main_loop(void)
892 int i, ret, timeout;
894 set_current_time();
895 server_start_time = current_time;
897 main_loop_epoll();
898 /* fall through to normal poll loop */
900 while (active_users)
902 timeout = get_next_timeout();
904 if (!active_users) break; /* last user removed by a timeout */
906 ret = poll( pollfd, nb_users, timeout );
907 set_current_time();
909 if (ret > 0)
911 for (i = 0; i < nb_users; i++)
913 if (pollfd[i].revents)
915 fd_poll_event( poll_users[i], pollfd[i].revents );
916 if (!--ret) break;
924 /****************************************************************/
925 /* device functions */
927 static struct list device_hash[DEVICE_HASH_SIZE];
929 static int is_device_removable( dev_t dev, int unix_fd )
931 #if defined(linux) && defined(HAVE_FSTATFS)
932 struct statfs stfs;
934 /* check for floppy disk */
935 if (major(dev) == FLOPPY_MAJOR) return 1;
937 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
938 return (stfs.f_type == 0x9660 || /* iso9660 */
939 stfs.f_type == 0x9fa1 || /* supermount */
940 stfs.f_type == 0x15013346); /* udf */
941 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
942 struct statfs stfs;
944 if (fstatfs( unix_fd, &stfs ) == -1) return 0;
945 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
946 #elif defined(__NetBSD__)
947 struct statvfs stfs;
949 if (fstatvfs( unix_fd, &stfs ) == -1) return 0;
950 return (!strcmp("cd9660", stfs.f_fstypename) || !strcmp("udf", stfs.f_fstypename));
951 #elif defined(sun)
952 # include <sys/dkio.h>
953 # include <sys/vtoc.h>
954 struct dk_cinfo dkinf;
955 if (ioctl( unix_fd, DKIOCINFO, &dkinf ) == -1) return 0;
956 return (dkinf.dki_ctype == DKC_CDROM ||
957 dkinf.dki_ctype == DKC_NCRFLOPPY ||
958 dkinf.dki_ctype == DKC_SMSFLOPPY ||
959 dkinf.dki_ctype == DKC_INTEL82072 ||
960 dkinf.dki_ctype == DKC_INTEL82077);
961 #else
962 return 0;
963 #endif
966 /* retrieve the device object for a given fd, creating it if needed */
967 static struct device *get_device( dev_t dev, int unix_fd )
969 struct device *device;
970 unsigned int i, hash = dev % DEVICE_HASH_SIZE;
972 if (device_hash[hash].next)
974 LIST_FOR_EACH_ENTRY( device, &device_hash[hash], struct device, entry )
975 if (device->dev == dev) return (struct device *)grab_object( device );
977 else list_init( &device_hash[hash] );
979 /* not found, create it */
981 if (unix_fd == -1) return NULL;
982 if ((device = alloc_object( &device_ops )))
984 device->dev = dev;
985 device->removable = is_device_removable( dev, unix_fd );
986 for (i = 0; i < INODE_HASH_SIZE; i++) list_init( &device->inode_hash[i] );
987 list_add_head( &device_hash[hash], &device->entry );
989 return device;
992 static void device_dump( struct object *obj, int verbose )
994 struct device *device = (struct device *)obj;
995 fprintf( stderr, "Device dev=" );
996 DUMP_LONG_LONG( device->dev );
997 fprintf( stderr, "\n" );
1000 static void device_destroy( struct object *obj )
1002 struct device *device = (struct device *)obj;
1003 unsigned int i;
1005 for (i = 0; i < INODE_HASH_SIZE; i++)
1006 assert( list_empty(&device->inode_hash[i]) );
1008 list_remove( &device->entry ); /* remove it from the hash table */
1012 /****************************************************************/
1013 /* inode functions */
1015 /* close all pending file descriptors in the closed list */
1016 static void inode_close_pending( struct inode *inode, int keep_unlinks )
1018 struct list *ptr = list_head( &inode->closed );
1020 while (ptr)
1022 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1023 struct list *next = list_next( &inode->closed, ptr );
1025 if (fd->unix_fd != -1)
1027 close( fd->unix_fd );
1028 fd->unix_fd = -1;
1030 if (!keep_unlinks || !fd->unlink) /* get rid of it unless there's an unlink pending on that file */
1032 list_remove( ptr );
1033 free( fd->unix_name );
1034 free( fd );
1036 ptr = next;
1040 static void inode_dump( struct object *obj, int verbose )
1042 struct inode *inode = (struct inode *)obj;
1043 fprintf( stderr, "Inode device=%p ino=", inode->device );
1044 DUMP_LONG_LONG( inode->ino );
1045 fprintf( stderr, "\n" );
1048 static void inode_destroy( struct object *obj )
1050 struct inode *inode = (struct inode *)obj;
1051 struct list *ptr;
1053 assert( list_empty(&inode->open) );
1054 assert( list_empty(&inode->locks) );
1056 list_remove( &inode->entry );
1058 while ((ptr = list_head( &inode->closed )))
1060 struct closed_fd *fd = LIST_ENTRY( ptr, struct closed_fd, entry );
1061 list_remove( ptr );
1062 if (fd->unix_fd != -1) close( fd->unix_fd );
1063 if (fd->unlink)
1065 /* make sure it is still the same file */
1066 struct stat st;
1067 if (!stat( fd->unix_name, &st ) && st.st_dev == inode->device->dev && st.st_ino == inode->ino)
1069 if (S_ISDIR(st.st_mode)) rmdir( fd->unix_name );
1070 else unlink( fd->unix_name );
1073 free( fd->unix_name );
1074 free( fd );
1076 release_object( inode->device );
1079 /* retrieve the inode object for a given fd, creating it if needed */
1080 static struct inode *get_inode( dev_t dev, ino_t ino, int unix_fd )
1082 struct device *device;
1083 struct inode *inode;
1084 unsigned int hash = ino % INODE_HASH_SIZE;
1086 if (!(device = get_device( dev, unix_fd ))) return NULL;
1088 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[hash], struct inode, entry )
1090 if (inode->ino == ino)
1092 release_object( device );
1093 return (struct inode *)grab_object( inode );
1097 /* not found, create it */
1098 if ((inode = alloc_object( &inode_ops )))
1100 inode->device = device;
1101 inode->ino = ino;
1102 list_init( &inode->open );
1103 list_init( &inode->locks );
1104 list_init( &inode->closed );
1105 list_add_head( &device->inode_hash[hash], &inode->entry );
1107 else release_object( device );
1109 return inode;
1112 /* add fd to the inode list of file descriptors to close */
1113 static void inode_add_closed_fd( struct inode *inode, struct closed_fd *fd )
1115 if (!list_empty( &inode->locks ))
1117 list_add_head( &inode->closed, &fd->entry );
1119 else if (fd->unlink) /* close the fd but keep the structure around for unlink */
1121 if (fd->unix_fd != -1) close( fd->unix_fd );
1122 fd->unix_fd = -1;
1123 list_add_head( &inode->closed, &fd->entry );
1125 else /* no locks on this inode and no unlink, get rid of the fd */
1127 if (fd->unix_fd != -1) close( fd->unix_fd );
1128 free( fd->unix_name );
1129 free( fd );
1134 /****************************************************************/
1135 /* file lock functions */
1137 static void file_lock_dump( struct object *obj, int verbose )
1139 struct file_lock *lock = (struct file_lock *)obj;
1140 fprintf( stderr, "Lock %s fd=%p proc=%p start=",
1141 lock->shared ? "shared" : "excl", lock->fd, lock->process );
1142 DUMP_LONG_LONG( lock->start );
1143 fprintf( stderr, " end=" );
1144 DUMP_LONG_LONG( lock->end );
1145 fprintf( stderr, "\n" );
1148 static int file_lock_signaled( struct object *obj, struct wait_queue_entry *entry )
1150 struct file_lock *lock = (struct file_lock *)obj;
1151 /* lock is signaled if it has lost its owner */
1152 return !lock->process;
1155 /* set (or remove) a Unix lock if possible for the given range */
1156 static int set_unix_lock( struct fd *fd, file_pos_t start, file_pos_t end, int type )
1158 struct flock fl;
1160 if (!fd->fs_locks) return 1; /* no fs locks possible for this fd */
1161 for (;;)
1163 if (start == end) return 1; /* can't set zero-byte lock */
1164 if (start > max_unix_offset) return 1; /* ignore it */
1165 fl.l_type = type;
1166 fl.l_whence = SEEK_SET;
1167 fl.l_start = start;
1168 if (!end || end > max_unix_offset) fl.l_len = 0;
1169 else fl.l_len = end - start;
1170 if (fcntl( fd->unix_fd, F_SETLK, &fl ) != -1) return 1;
1172 switch(errno)
1174 case EACCES:
1175 /* check whether locks work at all on this file system */
1176 if (fcntl( fd->unix_fd, F_GETLK, &fl ) != -1)
1178 set_error( STATUS_FILE_LOCK_CONFLICT );
1179 return 0;
1181 /* fall through */
1182 case EIO:
1183 case ENOLCK:
1184 case ENOTSUP:
1185 /* no locking on this fs, just ignore it */
1186 fd->fs_locks = 0;
1187 return 1;
1188 case EAGAIN:
1189 set_error( STATUS_FILE_LOCK_CONFLICT );
1190 return 0;
1191 case EBADF:
1192 /* this can happen if we try to set a write lock on a read-only file */
1193 /* try to at least grab a read lock */
1194 if (fl.l_type == F_WRLCK)
1196 type = F_RDLCK;
1197 break; /* retry */
1199 set_error( STATUS_ACCESS_DENIED );
1200 return 0;
1201 #ifdef EOVERFLOW
1202 case EOVERFLOW:
1203 #endif
1204 case EINVAL:
1205 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
1206 /* in that case we shrink the limit and retry */
1207 if (max_unix_offset > INT_MAX)
1209 max_unix_offset = INT_MAX;
1210 break; /* retry */
1212 /* fall through */
1213 default:
1214 file_set_error();
1215 return 0;
1220 /* check if interval [start;end) overlaps the lock */
1221 static inline int lock_overlaps( struct file_lock *lock, file_pos_t start, file_pos_t end )
1223 if (lock->end && start >= lock->end) return 0;
1224 if (end && lock->start >= end) return 0;
1225 return 1;
1228 /* remove Unix locks for all bytes in the specified area that are no longer locked */
1229 static void remove_unix_locks( struct fd *fd, file_pos_t start, file_pos_t end )
1231 struct hole
1233 struct hole *next;
1234 struct hole *prev;
1235 file_pos_t start;
1236 file_pos_t end;
1237 } *first, *cur, *next, *buffer;
1239 struct list *ptr;
1240 int count = 0;
1242 if (!fd->inode) return;
1243 if (!fd->fs_locks) return;
1244 if (start == end || start > max_unix_offset) return;
1245 if (!end || end > max_unix_offset) end = max_unix_offset + 1;
1247 /* count the number of locks overlapping the specified area */
1249 LIST_FOR_EACH( ptr, &fd->inode->locks )
1251 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1252 if (lock->start == lock->end) continue;
1253 if (lock_overlaps( lock, start, end )) count++;
1256 if (!count) /* no locks at all, we can unlock everything */
1258 set_unix_lock( fd, start, end, F_UNLCK );
1259 return;
1262 /* allocate space for the list of holes */
1263 /* max. number of holes is number of locks + 1 */
1265 if (!(buffer = malloc( sizeof(*buffer) * (count+1) ))) return;
1266 first = buffer;
1267 first->next = NULL;
1268 first->prev = NULL;
1269 first->start = start;
1270 first->end = end;
1271 next = first + 1;
1273 /* build a sorted list of unlocked holes in the specified area */
1275 LIST_FOR_EACH( ptr, &fd->inode->locks )
1277 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1278 if (lock->start == lock->end) continue;
1279 if (!lock_overlaps( lock, start, end )) continue;
1281 /* go through all the holes touched by this lock */
1282 for (cur = first; cur; cur = cur->next)
1284 if (cur->end <= lock->start) continue; /* hole is before start of lock */
1285 if (lock->end && cur->start >= lock->end) break; /* hole is after end of lock */
1287 /* now we know that lock is overlapping hole */
1289 if (cur->start >= lock->start) /* lock starts before hole, shrink from start */
1291 cur->start = lock->end;
1292 if (cur->start && cur->start < cur->end) break; /* done with this lock */
1293 /* now hole is empty, remove it */
1294 if (cur->next) cur->next->prev = cur->prev;
1295 if (cur->prev) cur->prev->next = cur->next;
1296 else if (!(first = cur->next)) goto done; /* no more holes at all */
1298 else if (!lock->end || cur->end <= lock->end) /* lock larger than hole, shrink from end */
1300 cur->end = lock->start;
1301 assert( cur->start < cur->end );
1303 else /* lock is in the middle of hole, split hole in two */
1305 next->prev = cur;
1306 next->next = cur->next;
1307 cur->next = next;
1308 next->start = lock->end;
1309 next->end = cur->end;
1310 cur->end = lock->start;
1311 assert( next->start < next->end );
1312 assert( cur->end < next->start );
1313 next++;
1314 break; /* done with this lock */
1319 /* clear Unix locks for all the holes */
1321 for (cur = first; cur; cur = cur->next)
1322 set_unix_lock( fd, cur->start, cur->end, F_UNLCK );
1324 done:
1325 free( buffer );
1328 /* create a new lock on a fd */
1329 static struct file_lock *add_lock( struct fd *fd, int shared, file_pos_t start, file_pos_t end )
1331 struct file_lock *lock;
1333 if (!(lock = alloc_object( &file_lock_ops ))) return NULL;
1334 lock->shared = shared;
1335 lock->start = start;
1336 lock->end = end;
1337 lock->fd = fd;
1338 lock->process = current->process;
1340 /* now try to set a Unix lock */
1341 if (!set_unix_lock( lock->fd, lock->start, lock->end, lock->shared ? F_RDLCK : F_WRLCK ))
1343 release_object( lock );
1344 return NULL;
1346 list_add_tail( &fd->locks, &lock->fd_entry );
1347 list_add_tail( &fd->inode->locks, &lock->inode_entry );
1348 list_add_tail( &lock->process->locks, &lock->proc_entry );
1349 return lock;
1352 /* remove an existing lock */
1353 static void remove_lock( struct file_lock *lock, int remove_unix )
1355 struct inode *inode = lock->fd->inode;
1357 list_remove( &lock->fd_entry );
1358 list_remove( &lock->inode_entry );
1359 list_remove( &lock->proc_entry );
1360 if (remove_unix) remove_unix_locks( lock->fd, lock->start, lock->end );
1361 if (list_empty( &inode->locks )) inode_close_pending( inode, 1 );
1362 lock->process = NULL;
1363 wake_up( &lock->obj, 0 );
1364 release_object( lock );
1367 /* remove all locks owned by a given process */
1368 void remove_process_locks( struct process *process )
1370 struct list *ptr;
1372 while ((ptr = list_head( &process->locks )))
1374 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, proc_entry );
1375 remove_lock( lock, 1 ); /* this removes it from the list */
1379 /* remove all locks on a given fd */
1380 static void remove_fd_locks( struct fd *fd )
1382 file_pos_t start = FILE_POS_T_MAX, end = 0;
1383 struct list *ptr;
1385 while ((ptr = list_head( &fd->locks )))
1387 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1388 if (lock->start < start) start = lock->start;
1389 if (!lock->end || lock->end > end) end = lock->end - 1;
1390 remove_lock( lock, 0 );
1392 if (start < end) remove_unix_locks( fd, start, end + 1 );
1395 /* add a lock on an fd */
1396 /* returns handle to wait on */
1397 obj_handle_t lock_fd( struct fd *fd, file_pos_t start, file_pos_t count, int shared, int wait )
1399 struct list *ptr;
1400 file_pos_t end = start + count;
1402 if (!fd->inode) /* not a regular file */
1404 set_error( STATUS_INVALID_DEVICE_REQUEST );
1405 return 0;
1408 /* don't allow wrapping locks */
1409 if (end && end < start)
1411 set_error( STATUS_INVALID_PARAMETER );
1412 return 0;
1415 /* check if another lock on that file overlaps the area */
1416 LIST_FOR_EACH( ptr, &fd->inode->locks )
1418 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, inode_entry );
1419 if (!lock_overlaps( lock, start, end )) continue;
1420 if (shared && (lock->shared || lock->fd == fd)) continue;
1421 /* found one */
1422 if (!wait)
1424 set_error( STATUS_FILE_LOCK_CONFLICT );
1425 return 0;
1427 set_error( STATUS_PENDING );
1428 return alloc_handle( current->process, lock, SYNCHRONIZE, 0 );
1431 /* not found, add it */
1432 if (add_lock( fd, shared, start, end )) return 0;
1433 if (get_error() == STATUS_FILE_LOCK_CONFLICT)
1435 /* Unix lock conflict -> tell client to wait and retry */
1436 if (wait) set_error( STATUS_PENDING );
1438 return 0;
1441 /* remove a lock on an fd */
1442 void unlock_fd( struct fd *fd, file_pos_t start, file_pos_t count )
1444 struct list *ptr;
1445 file_pos_t end = start + count;
1447 /* find an existing lock with the exact same parameters */
1448 LIST_FOR_EACH( ptr, &fd->locks )
1450 struct file_lock *lock = LIST_ENTRY( ptr, struct file_lock, fd_entry );
1451 if ((lock->start == start) && (lock->end == end))
1453 remove_lock( lock, 1 );
1454 return;
1457 set_error( STATUS_FILE_LOCK_CONFLICT );
1461 /****************************************************************/
1462 /* file descriptor functions */
1464 static void fd_dump( struct object *obj, int verbose )
1466 struct fd *fd = (struct fd *)obj;
1467 fprintf( stderr, "Fd unix_fd=%d user=%p options=%08x", fd->unix_fd, fd->user, fd->options );
1468 if (fd->inode) fprintf( stderr, " inode=%p unlink=%d", fd->inode, fd->closed->unlink );
1469 fprintf( stderr, "\n" );
1472 static void fd_destroy( struct object *obj )
1474 struct fd *fd = (struct fd *)obj;
1476 free_async_queue( fd->read_q );
1477 free_async_queue( fd->write_q );
1478 free_async_queue( fd->wait_q );
1480 if (fd->completion) release_object( fd->completion );
1481 remove_fd_locks( fd );
1482 list_remove( &fd->inode_entry );
1483 if (fd->poll_index != -1) remove_poll_user( fd, fd->poll_index );
1484 if (fd->inode)
1486 inode_add_closed_fd( fd->inode, fd->closed );
1487 release_object( fd->inode );
1489 else /* no inode, close it right away */
1491 if (fd->unix_fd != -1) close( fd->unix_fd );
1492 free( fd->unix_name );
1496 /* check if the desired access is possible without violating */
1497 /* the sharing mode of other opens of the same file */
1498 static unsigned int check_sharing( struct fd *fd, unsigned int access, unsigned int sharing,
1499 unsigned int open_flags, unsigned int options )
1501 /* only a few access bits are meaningful wrt sharing */
1502 const unsigned int read_access = FILE_READ_DATA | FILE_EXECUTE;
1503 const unsigned int write_access = FILE_WRITE_DATA | FILE_APPEND_DATA;
1504 const unsigned int all_access = read_access | write_access | DELETE;
1506 unsigned int existing_sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
1507 unsigned int existing_access = 0;
1508 struct list *ptr;
1510 fd->access = access;
1511 fd->sharing = sharing;
1513 LIST_FOR_EACH( ptr, &fd->inode->open )
1515 struct fd *fd_ptr = LIST_ENTRY( ptr, struct fd, inode_entry );
1516 if (fd_ptr != fd)
1518 /* if access mode is 0, sharing mode is ignored */
1519 if (fd_ptr->access & all_access) existing_sharing &= fd_ptr->sharing;
1520 existing_access |= fd_ptr->access;
1524 if (((access & read_access) && !(existing_sharing & FILE_SHARE_READ)) ||
1525 ((access & write_access) && !(existing_sharing & FILE_SHARE_WRITE)) ||
1526 ((access & DELETE) && !(existing_sharing & FILE_SHARE_DELETE)))
1527 return STATUS_SHARING_VIOLATION;
1528 if (((existing_access & FILE_MAPPING_WRITE) && !(sharing & FILE_SHARE_WRITE)) ||
1529 ((existing_access & FILE_MAPPING_IMAGE) && (access & FILE_WRITE_DATA)))
1530 return STATUS_SHARING_VIOLATION;
1531 if ((existing_access & FILE_MAPPING_IMAGE) && (options & FILE_DELETE_ON_CLOSE))
1532 return STATUS_CANNOT_DELETE;
1533 if ((existing_access & FILE_MAPPING_ACCESS) && (open_flags & O_TRUNC))
1534 return STATUS_USER_MAPPED_FILE;
1535 if (!(access & all_access))
1536 return 0; /* if access mode is 0, sharing mode is ignored (except for mappings) */
1537 if (((existing_access & read_access) && !(sharing & FILE_SHARE_READ)) ||
1538 ((existing_access & write_access) && !(sharing & FILE_SHARE_WRITE)) ||
1539 ((existing_access & DELETE) && !(sharing & FILE_SHARE_DELETE)))
1540 return STATUS_SHARING_VIOLATION;
1541 return 0;
1544 /* set the events that select waits for on this fd */
1545 void set_fd_events( struct fd *fd, int events )
1547 int user = fd->poll_index;
1548 assert( poll_users[user] == fd );
1550 set_fd_epoll_events( fd, user, events );
1552 if (events == -1) /* stop waiting on this fd completely */
1554 pollfd[user].fd = -1;
1555 pollfd[user].events = POLLERR;
1556 pollfd[user].revents = 0;
1558 else if (pollfd[user].fd != -1 || !pollfd[user].events)
1560 pollfd[user].fd = fd->unix_fd;
1561 pollfd[user].events = events;
1565 /* prepare an fd for unmounting its corresponding device */
1566 static inline void unmount_fd( struct fd *fd )
1568 assert( fd->inode );
1570 async_wake_up( fd->read_q, STATUS_VOLUME_DISMOUNTED );
1571 async_wake_up( fd->write_q, STATUS_VOLUME_DISMOUNTED );
1573 if (fd->poll_index != -1) set_fd_events( fd, -1 );
1575 if (fd->unix_fd != -1) close( fd->unix_fd );
1577 fd->unix_fd = -1;
1578 fd->no_fd_status = STATUS_VOLUME_DISMOUNTED;
1579 fd->closed->unix_fd = -1;
1580 fd->closed->unlink = 0;
1582 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1583 fd->fs_locks = 0;
1586 /* allocate an fd object, without setting the unix fd yet */
1587 static struct fd *alloc_fd_object(void)
1589 struct fd *fd = alloc_object( &fd_ops );
1591 if (!fd) return NULL;
1593 fd->fd_ops = NULL;
1594 fd->user = NULL;
1595 fd->inode = NULL;
1596 fd->closed = NULL;
1597 fd->access = 0;
1598 fd->options = 0;
1599 fd->sharing = 0;
1600 fd->unix_fd = -1;
1601 fd->unix_name = NULL;
1602 fd->cacheable = 0;
1603 fd->signaled = 1;
1604 fd->fs_locks = 1;
1605 fd->poll_index = -1;
1606 fd->read_q = NULL;
1607 fd->write_q = NULL;
1608 fd->wait_q = NULL;
1609 fd->completion = NULL;
1610 list_init( &fd->inode_entry );
1611 list_init( &fd->locks );
1613 if ((fd->poll_index = add_poll_user( fd )) == -1)
1615 release_object( fd );
1616 return NULL;
1618 return fd;
1621 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1622 struct fd *alloc_pseudo_fd( const struct fd_ops *fd_user_ops, struct object *user, unsigned int options )
1624 struct fd *fd = alloc_object( &fd_ops );
1626 if (!fd) return NULL;
1628 fd->fd_ops = fd_user_ops;
1629 fd->user = user;
1630 fd->inode = NULL;
1631 fd->closed = NULL;
1632 fd->access = 0;
1633 fd->options = options;
1634 fd->sharing = 0;
1635 fd->unix_name = NULL;
1636 fd->unix_fd = -1;
1637 fd->cacheable = 0;
1638 fd->signaled = 0;
1639 fd->fs_locks = 0;
1640 fd->poll_index = -1;
1641 fd->read_q = NULL;
1642 fd->write_q = NULL;
1643 fd->wait_q = NULL;
1644 fd->completion = NULL;
1645 fd->no_fd_status = STATUS_BAD_DEVICE_TYPE;
1646 list_init( &fd->inode_entry );
1647 list_init( &fd->locks );
1648 return fd;
1651 /* duplicate an fd object for a different user */
1652 struct fd *dup_fd_object( struct fd *orig, unsigned int access, unsigned int sharing, unsigned int options )
1654 unsigned int err;
1655 struct fd *fd = alloc_fd_object();
1657 if (!fd) return NULL;
1659 fd->options = options;
1660 fd->cacheable = orig->cacheable;
1662 if (orig->unix_name)
1664 if (!(fd->unix_name = mem_alloc( strlen(orig->unix_name) + 1 ))) goto failed;
1665 strcpy( fd->unix_name, orig->unix_name );
1668 if (orig->inode)
1670 struct closed_fd *closed = mem_alloc( sizeof(*closed) );
1671 if (!closed) goto failed;
1672 if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1674 file_set_error();
1675 free( closed );
1676 goto failed;
1678 closed->unix_fd = fd->unix_fd;
1679 closed->unlink = 0;
1680 closed->unix_name = fd->unix_name;
1681 fd->closed = closed;
1682 fd->inode = (struct inode *)grab_object( orig->inode );
1683 list_add_head( &fd->inode->open, &fd->inode_entry );
1684 if ((err = check_sharing( fd, access, sharing, 0, options )))
1686 set_error( err );
1687 goto failed;
1690 else if ((fd->unix_fd = dup( orig->unix_fd )) == -1)
1692 file_set_error();
1693 goto failed;
1695 return fd;
1697 failed:
1698 release_object( fd );
1699 return NULL;
1702 /* find an existing fd object that can be reused for a mapping */
1703 struct fd *get_fd_object_for_mapping( struct fd *fd, unsigned int access, unsigned int sharing )
1705 struct fd *fd_ptr;
1707 if (!fd->inode) return NULL;
1709 LIST_FOR_EACH_ENTRY( fd_ptr, &fd->inode->open, struct fd, inode_entry )
1710 if (fd_ptr->access == access && fd_ptr->sharing == sharing)
1711 return (struct fd *)grab_object( fd_ptr );
1713 return NULL;
1716 /* set the status to return when the fd has no associated unix fd */
1717 void set_no_fd_status( struct fd *fd, unsigned int status )
1719 fd->no_fd_status = status;
1722 /* sets the user of an fd that previously had no user */
1723 void set_fd_user( struct fd *fd, const struct fd_ops *user_ops, struct object *user )
1725 assert( fd->fd_ops == NULL );
1726 fd->fd_ops = user_ops;
1727 fd->user = user;
1730 char *dup_fd_name( struct fd *root, const char *name )
1732 char *ret;
1734 if (!root) return strdup( name );
1735 if (!root->unix_name) return NULL;
1737 /* skip . prefix */
1738 if (name[0] == '.' && (!name[1] || name[1] == '/')) name++;
1740 if ((ret = malloc( strlen(root->unix_name) + strlen(name) + 2 )))
1742 strcpy( ret, root->unix_name );
1743 if (name[0] && name[0] != '/') strcat( ret, "/" );
1744 strcat( ret, name );
1746 return ret;
1749 /* open() wrapper that returns a struct fd with no fd user set */
1750 struct fd *open_fd( struct fd *root, const char *name, int flags, mode_t *mode, unsigned int access,
1751 unsigned int sharing, unsigned int options )
1753 struct stat st;
1754 struct closed_fd *closed_fd;
1755 struct fd *fd;
1756 int root_fd = -1;
1757 int rw_mode;
1759 if (((options & FILE_DELETE_ON_CLOSE) && !(access & DELETE)) ||
1760 ((options & FILE_DIRECTORY_FILE) && (flags & O_TRUNC)))
1762 set_error( STATUS_INVALID_PARAMETER );
1763 return NULL;
1766 if (!(fd = alloc_fd_object())) return NULL;
1768 fd->options = options;
1769 if (!(closed_fd = mem_alloc( sizeof(*closed_fd) )))
1771 release_object( fd );
1772 return NULL;
1775 if (root)
1777 if ((root_fd = get_unix_fd( root )) == -1) goto error;
1778 if (fchdir( root_fd ) == -1)
1780 file_set_error();
1781 root_fd = -1;
1782 goto error;
1786 /* create the directory if needed */
1787 if ((options & FILE_DIRECTORY_FILE) && (flags & O_CREAT))
1789 if (mkdir( name, *mode ) == -1)
1791 if (errno != EEXIST || (flags & O_EXCL))
1793 file_set_error();
1794 goto error;
1797 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
1800 if ((access & FILE_UNIX_WRITE_ACCESS) && !(options & FILE_DIRECTORY_FILE))
1802 if (access & FILE_UNIX_READ_ACCESS) rw_mode = O_RDWR;
1803 else rw_mode = O_WRONLY;
1805 else rw_mode = O_RDONLY;
1807 fd->unix_name = dup_fd_name( root, name );
1809 if ((fd->unix_fd = open( name, rw_mode | (flags & ~O_TRUNC), *mode )) == -1)
1811 /* if we tried to open a directory for write access, retry read-only */
1812 if (errno == EISDIR)
1814 if ((access & FILE_UNIX_WRITE_ACCESS) || (flags & O_CREAT))
1815 fd->unix_fd = open( name, O_RDONLY | (flags & ~(O_TRUNC | O_CREAT | O_EXCL)), *mode );
1818 if (fd->unix_fd == -1)
1820 file_set_error();
1821 goto error;
1825 closed_fd->unix_fd = fd->unix_fd;
1826 closed_fd->unlink = 0;
1827 closed_fd->unix_name = fd->unix_name;
1828 fstat( fd->unix_fd, &st );
1829 *mode = st.st_mode;
1831 /* only bother with an inode for normal files and directories */
1832 if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
1834 unsigned int err;
1835 struct inode *inode = get_inode( st.st_dev, st.st_ino, fd->unix_fd );
1837 if (!inode)
1839 /* we can close the fd because there are no others open on the same file,
1840 * otherwise we wouldn't have failed to allocate a new inode
1842 goto error;
1844 fd->inode = inode;
1845 fd->closed = closed_fd;
1846 fd->cacheable = !inode->device->removable;
1847 list_add_head( &inode->open, &fd->inode_entry );
1848 closed_fd = NULL;
1850 /* check directory options */
1851 if ((options & FILE_DIRECTORY_FILE) && !S_ISDIR(st.st_mode))
1853 set_error( STATUS_NOT_A_DIRECTORY );
1854 goto error;
1856 if ((options & FILE_NON_DIRECTORY_FILE) && S_ISDIR(st.st_mode))
1858 set_error( STATUS_FILE_IS_A_DIRECTORY );
1859 goto error;
1861 if ((err = check_sharing( fd, access, sharing, flags, options )))
1863 set_error( err );
1864 goto error;
1867 /* can't unlink files if we don't have permission to access */
1868 if ((options & FILE_DELETE_ON_CLOSE) && !(flags & O_CREAT) &&
1869 !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1871 set_error( STATUS_CANNOT_DELETE );
1872 goto error;
1875 fd->closed->unlink = (options & FILE_DELETE_ON_CLOSE) != 0;
1876 if (flags & O_TRUNC)
1878 if (S_ISDIR(st.st_mode))
1880 set_error( STATUS_OBJECT_NAME_COLLISION );
1881 goto error;
1883 ftruncate( fd->unix_fd, 0 );
1886 else /* special file */
1888 if (options & FILE_DELETE_ON_CLOSE) /* we can't unlink special files */
1890 set_error( STATUS_INVALID_PARAMETER );
1891 goto error;
1893 free( closed_fd );
1894 fd->cacheable = 1;
1896 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1897 return fd;
1899 error:
1900 release_object( fd );
1901 free( closed_fd );
1902 if (root_fd != -1) fchdir( server_dir_fd ); /* go back to the server dir */
1903 return NULL;
1906 /* create an fd for an anonymous file */
1907 /* if the function fails the unix fd is closed */
1908 struct fd *create_anonymous_fd( const struct fd_ops *fd_user_ops, int unix_fd, struct object *user,
1909 unsigned int options )
1911 struct fd *fd = alloc_fd_object();
1913 if (fd)
1915 set_fd_user( fd, fd_user_ops, user );
1916 fd->unix_fd = unix_fd;
1917 fd->options = options;
1918 return fd;
1920 close( unix_fd );
1921 return NULL;
1924 /* retrieve the object that is using an fd */
1925 void *get_fd_user( struct fd *fd )
1927 return fd->user;
1930 /* retrieve the opening options for the fd */
1931 unsigned int get_fd_options( struct fd *fd )
1933 return fd->options;
1936 /* retrieve the unix fd for an object */
1937 int get_unix_fd( struct fd *fd )
1939 if (fd->unix_fd == -1) set_error( fd->no_fd_status );
1940 return fd->unix_fd;
1943 /* check if two file descriptors point to the same file */
1944 int is_same_file_fd( struct fd *fd1, struct fd *fd2 )
1946 return fd1->inode == fd2->inode;
1949 /* allow the fd to be cached (can't be reset once set) */
1950 void allow_fd_caching( struct fd *fd )
1952 fd->cacheable = 1;
1955 /* check if fd is on a removable device */
1956 int is_fd_removable( struct fd *fd )
1958 return (fd->inode && fd->inode->device->removable);
1961 /* set or clear the fd signaled state */
1962 void set_fd_signaled( struct fd *fd, int signaled )
1964 fd->signaled = signaled;
1965 if (signaled) wake_up( fd->user, 0 );
1968 /* check if fd is signaled */
1969 int is_fd_signaled( struct fd *fd )
1971 return fd->signaled;
1974 /* handler for close_handle that refuses to close fd-associated handles in other processes */
1975 int fd_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
1977 return (!current || current->process == process);
1980 /* check if events are pending and if yes return which one(s) */
1981 int check_fd_events( struct fd *fd, int events )
1983 struct pollfd pfd;
1985 if (fd->unix_fd == -1) return POLLERR;
1986 if (fd->inode) return events; /* regular files are always signaled */
1988 pfd.fd = fd->unix_fd;
1989 pfd.events = events;
1990 if (poll( &pfd, 1, 0 ) <= 0) return 0;
1991 return pfd.revents;
1994 /* default signaled() routine for objects that poll() on an fd */
1995 int default_fd_signaled( struct object *obj, struct wait_queue_entry *entry )
1997 struct fd *fd = get_obj_fd( obj );
1998 int ret = fd->signaled;
1999 release_object( fd );
2000 return ret;
2003 /* default map_access() routine for objects that behave like an fd */
2004 unsigned int default_fd_map_access( struct object *obj, unsigned int access )
2006 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
2007 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
2008 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
2009 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
2010 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
2013 int default_fd_get_poll_events( struct fd *fd )
2015 int events = 0;
2017 if (async_waiting( fd->read_q )) events |= POLLIN;
2018 if (async_waiting( fd->write_q )) events |= POLLOUT;
2019 return events;
2022 /* default handler for poll() events */
2023 void default_poll_event( struct fd *fd, int event )
2025 if (event & (POLLIN | POLLERR | POLLHUP)) async_wake_up( fd->read_q, STATUS_ALERTED );
2026 if (event & (POLLOUT | POLLERR | POLLHUP)) async_wake_up( fd->write_q, STATUS_ALERTED );
2028 /* if an error occurred, stop polling this fd to avoid busy-looping */
2029 if (event & (POLLERR | POLLHUP)) set_fd_events( fd, -1 );
2030 else if (!fd->inode) set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2033 int fd_queue_async( struct fd *fd, struct async *async, int type )
2035 struct async_queue *queue;
2037 switch (type)
2039 case ASYNC_TYPE_READ:
2040 if (!fd->read_q && !(fd->read_q = create_async_queue( fd ))) return 0;
2041 queue = fd->read_q;
2042 break;
2043 case ASYNC_TYPE_WRITE:
2044 if (!fd->write_q && !(fd->write_q = create_async_queue( fd ))) return 0;
2045 queue = fd->write_q;
2046 break;
2047 case ASYNC_TYPE_WAIT:
2048 if (!fd->wait_q && !(fd->wait_q = create_async_queue( fd ))) return 0;
2049 queue = fd->wait_q;
2050 break;
2051 default:
2052 queue = NULL;
2053 assert(0);
2056 queue_async( queue, async );
2058 if (type != ASYNC_TYPE_WAIT)
2060 if (!fd->inode)
2061 set_fd_events( fd, fd->fd_ops->get_poll_events( fd ) );
2062 else /* regular files are always ready for read and write */
2063 async_wake_up( queue, STATUS_ALERTED );
2065 return 1;
2068 void fd_async_wake_up( struct fd *fd, int type, unsigned int status )
2070 switch (type)
2072 case ASYNC_TYPE_READ:
2073 async_wake_up( fd->read_q, status );
2074 break;
2075 case ASYNC_TYPE_WRITE:
2076 async_wake_up( fd->write_q, status );
2077 break;
2078 case ASYNC_TYPE_WAIT:
2079 async_wake_up( fd->wait_q, status );
2080 break;
2081 default:
2082 assert(0);
2086 void fd_reselect_async( struct fd *fd, struct async_queue *queue )
2088 fd->fd_ops->reselect_async( fd, queue );
2091 void no_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2093 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2096 void default_fd_queue_async( struct fd *fd, struct async *async, int type, int count )
2098 if (fd_queue_async( fd, async, type )) set_error( STATUS_PENDING );
2101 /* default reselect_async() fd routine */
2102 void default_fd_reselect_async( struct fd *fd, struct async_queue *queue )
2104 if (queue != fd->wait_q)
2106 int poll_events = fd->fd_ops->get_poll_events( fd );
2107 int events = check_fd_events( fd, poll_events );
2108 if (events) fd->fd_ops->poll_event( fd, events );
2109 else set_fd_events( fd, poll_events );
2113 static inline int is_valid_mounted_device( struct stat *st )
2115 #if defined(linux) || defined(__sun__)
2116 return S_ISBLK( st->st_mode );
2117 #else
2118 /* disks are char devices on *BSD */
2119 return S_ISCHR( st->st_mode );
2120 #endif
2123 /* close all Unix file descriptors on a device to allow unmounting it */
2124 static void unmount_device( struct fd *device_fd )
2126 unsigned int i;
2127 struct stat st;
2128 struct device *device;
2129 struct inode *inode;
2130 struct fd *fd;
2131 int unix_fd = get_unix_fd( device_fd );
2133 if (unix_fd == -1) return;
2135 if (fstat( unix_fd, &st ) == -1 || !is_valid_mounted_device( &st ))
2137 set_error( STATUS_INVALID_PARAMETER );
2138 return;
2141 if (!(device = get_device( st.st_rdev, -1 ))) return;
2143 for (i = 0; i < INODE_HASH_SIZE; i++)
2145 LIST_FOR_EACH_ENTRY( inode, &device->inode_hash[i], struct inode, entry )
2147 LIST_FOR_EACH_ENTRY( fd, &inode->open, struct fd, inode_entry )
2149 unmount_fd( fd );
2151 inode_close_pending( inode, 0 );
2154 /* remove it from the hash table */
2155 list_remove( &device->entry );
2156 list_init( &device->entry );
2157 release_object( device );
2160 /* default read() routine */
2161 obj_handle_t no_fd_read( struct fd *fd, struct async *async, file_pos_t pos )
2163 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2164 return 0;
2167 /* default write() routine */
2168 obj_handle_t no_fd_write( struct fd *fd, struct async *async, file_pos_t pos )
2170 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2171 return 0;
2174 /* default flush() routine */
2175 obj_handle_t no_fd_flush( struct fd *fd, struct async *async )
2177 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2178 return 0;
2181 /* default ioctl() routine */
2182 obj_handle_t no_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2184 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2185 return 0;
2188 /* default ioctl() routine */
2189 obj_handle_t default_fd_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
2191 switch(code)
2193 case FSCTL_DISMOUNT_VOLUME:
2194 unmount_device( fd );
2195 return 0;
2196 default:
2197 set_error( STATUS_NOT_SUPPORTED );
2198 return 0;
2202 /* same as get_handle_obj but retrieve the struct fd associated to the object */
2203 static struct fd *get_handle_fd_obj( struct process *process, obj_handle_t handle,
2204 unsigned int access )
2206 struct fd *fd = NULL;
2207 struct object *obj;
2209 if ((obj = get_handle_obj( process, handle, access, NULL )))
2211 fd = get_obj_fd( obj );
2212 release_object( obj );
2214 return fd;
2217 /* set disposition for the fd */
2218 static void set_fd_disposition( struct fd *fd, int unlink )
2220 struct stat st;
2222 if (!fd->inode)
2224 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2225 return;
2228 if (fd->unix_fd == -1)
2230 set_error( fd->no_fd_status );
2231 return;
2234 if (fstat( fd->unix_fd, &st ) == -1)
2236 file_set_error();
2237 return;
2240 /* can't unlink special files */
2241 if (unlink && !S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
2243 set_error( STATUS_INVALID_PARAMETER );
2244 return;
2247 /* can't unlink files we don't have permission to access */
2248 if (unlink && !(st.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
2250 set_error( STATUS_CANNOT_DELETE );
2251 return;
2254 fd->closed->unlink = unlink || (fd->options & FILE_DELETE_ON_CLOSE);
2257 /* set new name for the fd */
2258 static void set_fd_name( struct fd *fd, struct fd *root, const char *nameptr,
2259 data_size_t len, int create_link )
2261 struct inode *inode;
2262 struct stat st;
2263 char *name;
2265 if (!fd->inode || !fd->unix_name)
2267 set_error( STATUS_OBJECT_TYPE_MISMATCH );
2268 return;
2270 if (!len || ((nameptr[0] == '/') ^ !root))
2272 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
2273 return;
2275 if (!(name = mem_alloc( len + 1 ))) return;
2276 memcpy( name, nameptr, len );
2277 name[len] = 0;
2279 if (root)
2281 char *combined_name = dup_fd_name( root, name );
2282 if (!combined_name)
2284 set_error( STATUS_NO_MEMORY );
2285 goto failed;
2287 free( name );
2288 name = combined_name;
2291 /* when creating a hard link, source cannot be a dir */
2292 if (create_link && fd->unix_fd != -1 &&
2293 !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode ))
2295 set_error( STATUS_FILE_IS_A_DIRECTORY );
2296 goto failed;
2299 if (!stat( name, &st ))
2301 /* can't replace directories or special files */
2302 if (!S_ISREG( st.st_mode ))
2304 set_error( STATUS_ACCESS_DENIED );
2305 goto failed;
2308 /* can't replace an opened file */
2309 if ((inode = get_inode( st.st_dev, st.st_ino, -1 )))
2311 int is_empty = list_empty( &inode->open );
2312 release_object( inode );
2313 if (!is_empty)
2315 set_error( STATUS_ACCESS_DENIED );
2316 goto failed;
2320 /* link() expects that the target doesn't exist */
2321 /* rename() cannot replace files with directories */
2322 if (create_link || (fd->unix_fd != -1 &&
2323 !fstat( fd->unix_fd, &st ) && S_ISDIR( st.st_mode )))
2325 if (unlink( name ))
2327 file_set_error();
2328 goto failed;
2333 if (create_link)
2335 if (link( fd->unix_name, name ))
2336 file_set_error();
2337 free( name );
2338 return;
2341 if (rename( fd->unix_name, name ))
2343 file_set_error();
2344 goto failed;
2347 free( fd->unix_name );
2348 fd->unix_name = name;
2349 fd->closed->unix_name = name;
2350 return;
2352 failed:
2353 free( name );
2356 struct completion *fd_get_completion( struct fd *fd, apc_param_t *p_key )
2358 *p_key = fd->comp_key;
2359 return fd->completion ? (struct completion *)grab_object( fd->completion ) : NULL;
2362 void fd_copy_completion( struct fd *src, struct fd *dst )
2364 assert( !dst->completion );
2365 dst->completion = fd_get_completion( src, &dst->comp_key );
2368 /* flush a file buffers */
2369 DECL_HANDLER(flush)
2371 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, 0 );
2372 struct async *async;
2374 if (!fd) return;
2376 async = create_async( current, &req->async, NULL );
2377 if (async)
2379 reply->event = fd->fd_ops->flush( fd, async );
2380 release_object( async );
2382 release_object( fd );
2385 /* open a file object */
2386 DECL_HANDLER(open_file_object)
2388 struct unicode_str name = get_req_unicode_str();
2389 struct object *obj, *result, *root = NULL;
2391 if (req->rootdir && !(root = get_handle_obj( current->process, req->rootdir, 0, NULL ))) return;
2393 obj = open_named_object( root, NULL, &name, req->attributes );
2394 if (root) release_object( root );
2395 if (!obj) return;
2397 if ((result = obj->ops->open_file( obj, req->access, req->sharing, req->options )))
2399 reply->handle = alloc_handle( current->process, result, req->access, req->attributes );
2400 release_object( result );
2402 release_object( obj );
2405 /* get the Unix name from a file handle */
2406 DECL_HANDLER(get_handle_unix_name)
2408 struct fd *fd;
2410 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2412 if (fd->unix_name)
2414 data_size_t name_len = strlen( fd->unix_name );
2415 reply->name_len = name_len;
2416 if (name_len <= get_reply_max_size()) set_reply_data( fd->unix_name, name_len );
2417 else set_error( STATUS_BUFFER_OVERFLOW );
2419 else set_error( STATUS_OBJECT_TYPE_MISMATCH );
2420 release_object( fd );
2424 /* get a Unix fd to access a file */
2425 DECL_HANDLER(get_handle_fd)
2427 struct fd *fd;
2429 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2431 int unix_fd = get_unix_fd( fd );
2432 reply->cacheable = fd->cacheable;
2433 if (unix_fd != -1)
2435 reply->type = fd->fd_ops->get_fd_type( fd );
2436 reply->options = fd->options;
2437 reply->access = get_handle_access( current->process, req->handle );
2438 send_client_fd( current->process, unix_fd, req->handle );
2440 release_object( fd );
2444 /* perform a read on a file object */
2445 DECL_HANDLER(read)
2447 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_READ_DATA );
2448 struct async *async;
2450 if (!fd) return;
2452 if ((async = create_request_async( current, &req->async )))
2454 reply->wait = fd->fd_ops->read( fd, async, req->pos );
2455 reply->options = fd->options;
2456 release_object( async );
2458 release_object( fd );
2461 /* perform a write on a file object */
2462 DECL_HANDLER(write)
2464 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, FILE_WRITE_DATA );
2465 struct async *async;
2466 struct iosb *iosb;
2468 if (!fd) return;
2470 if ((iosb = create_iosb( get_req_data(), get_req_data_size(), 0 )))
2472 async = create_async( current, &req->async, iosb );
2473 if (async)
2475 reply->wait = fd->fd_ops->write( fd, async, req->pos );
2476 reply->options = fd->options;
2477 release_object( async );
2479 release_object( iosb );
2481 release_object( fd );
2484 /* perform an ioctl on a file */
2485 DECL_HANDLER(ioctl)
2487 unsigned int access = (req->code >> 14) & (FILE_READ_DATA|FILE_WRITE_DATA);
2488 struct fd *fd = get_handle_fd_obj( current->process, req->async.handle, access );
2489 struct async *async;
2490 struct iosb *iosb;
2492 if (!fd) return;
2494 if ((iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() )))
2496 if ((async = create_async( current, &req->async, iosb )))
2498 reply->wait = fd->fd_ops->ioctl( fd, req->code, async );
2499 reply->options = fd->options;
2500 release_object( async );
2502 release_object( iosb );
2504 release_object( fd );
2507 /* create / reschedule an async I/O */
2508 DECL_HANDLER(register_async)
2510 unsigned int access;
2511 struct async *async;
2512 struct fd *fd;
2514 switch(req->type)
2516 case ASYNC_TYPE_READ:
2517 access = FILE_READ_DATA;
2518 break;
2519 case ASYNC_TYPE_WRITE:
2520 access = FILE_WRITE_DATA;
2521 break;
2522 default:
2523 set_error( STATUS_INVALID_PARAMETER );
2524 return;
2527 if ((fd = get_handle_fd_obj( current->process, req->async.handle, access )))
2529 if (get_unix_fd( fd ) != -1 && (async = create_async( current, &req->async, NULL )))
2531 fd->fd_ops->queue_async( fd, async, req->type, req->count );
2532 release_object( async );
2534 release_object( fd );
2538 /* attach completion object to a fd */
2539 DECL_HANDLER(set_completion_info)
2541 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2543 if (fd)
2545 if (!(fd->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) && !fd->completion)
2547 fd->completion = get_completion_obj( current->process, req->chandle, IO_COMPLETION_MODIFY_STATE );
2548 fd->comp_key = req->ckey;
2550 else set_error( STATUS_INVALID_PARAMETER );
2551 release_object( fd );
2555 /* push new completion msg into a completion queue attached to the fd */
2556 DECL_HANDLER(add_fd_completion)
2558 struct fd *fd = get_handle_fd_obj( current->process, req->handle, 0 );
2559 if (fd)
2561 if (fd->completion)
2562 add_completion( fd->completion, fd->comp_key, req->cvalue, req->status, req->information );
2563 release_object( fd );
2567 /* set fd disposition information */
2568 DECL_HANDLER(set_fd_disp_info)
2570 struct fd *fd = get_handle_fd_obj( current->process, req->handle, DELETE );
2571 if (fd)
2573 set_fd_disposition( fd, req->unlink );
2574 release_object( fd );
2578 /* set fd name information */
2579 DECL_HANDLER(set_fd_name_info)
2581 struct fd *fd, *root_fd = NULL;
2583 if (req->rootdir)
2585 struct dir *root;
2587 if (!(root = get_dir_obj( current->process, req->rootdir, 0 ))) return;
2588 root_fd = get_obj_fd( (struct object *)root );
2589 release_object( root );
2590 if (!root_fd) return;
2593 if ((fd = get_handle_fd_obj( current->process, req->handle, 0 )))
2595 set_fd_name( fd, root_fd, get_req_data(), get_req_data_size(), req->link );
2596 release_object( fd );
2598 if (root_fd) release_object( root_fd );