2 * Server-side file descriptor management
4 * Copyright (C) 2000, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
37 #ifdef HAVE_SYS_POLL_H
45 #include <sys/types.h>
49 #define WIN32_NO_STATUS
58 #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL_CREATE)
59 # include <sys/epoll.h>
61 #elif defined(linux) && defined(__i386__) && defined(HAVE_STDINT_H)
63 # define EPOLLIN POLLIN
64 # define EPOLLOUT POLLOUT
65 # define EPOLLERR POLLERR
66 # define EPOLLHUP POLLHUP
67 # define EPOLL_CTL_ADD 1
68 # define EPOLL_CTL_DEL 2
69 # define EPOLL_CTL_MOD 3
71 typedef union epoll_data
85 #define SYSCALL_RET(ret) do { \
86 if (ret < 0) { errno = -ret; ret = -1; } \
90 static inline int epoll_create( int size
)
93 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
94 : "=a" (ret
) : "0" (254 /*NR_epoll_create*/), "r" (size
) );
98 static inline int epoll_ctl( int epfd
, int op
, int fd
, const struct epoll_event
*event
)
101 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
103 : "0" (255 /*NR_epoll_ctl*/), "r" (epfd
), "c" (op
), "d" (fd
), "S" (event
), "m" (*event
) );
107 static inline int epoll_wait( int epfd
, struct epoll_event
*events
, int maxevents
, int timeout
)
110 __asm__( "pushl %%ebx; movl %2,%%ebx; int $0x80; popl %%ebx"
112 : "0" (256 /*NR_epoll_wait*/), "r" (epfd
), "c" (events
), "d" (maxevents
), "S" (timeout
)
118 #endif /* linux && __i386__ && HAVE_STDINT_H */
121 /* Because of the stupid Posix locking semantics, we need to keep
122 * track of all file descriptors referencing a given file, and not
123 * close a single one until all the locks are gone (sigh).
126 /* file descriptor object */
128 /* closed_fd is used to keep track of the unix fd belonging to a closed fd object */
131 struct list entry
; /* entry in inode closed list */
132 int unix_fd
; /* the unix file descriptor */
133 char unlink
[1]; /* name to unlink on close (if any) */
138 struct object obj
; /* object header */
139 const struct fd_ops
*fd_ops
; /* file descriptor operations */
140 struct inode
*inode
; /* inode that this fd belongs to */
141 struct list inode_entry
; /* entry in inode fd list */
142 struct closed_fd
*closed
; /* structure to store the unix fd at destroy time */
143 struct object
*user
; /* object using this file descriptor */
144 struct list locks
; /* list of locks on this fd */
145 unsigned int access
; /* file access (FILE_READ_DATA etc.) */
146 unsigned int sharing
; /* file sharing mode */
147 int unix_fd
; /* unix file descriptor */
148 int fs_locks
:1; /* can we use filesystem locks for this fd? */
149 int unmounted
:1;/* has the device been unmounted? */
150 int poll_index
; /* index of fd in poll array */
151 struct list read_q
; /* async readers of this fd */
152 struct list write_q
; /* async writers of this fd */
155 static void fd_dump( struct object
*obj
, int verbose
);
156 static void fd_destroy( struct object
*obj
);
158 static const struct object_ops fd_ops
=
160 sizeof(struct fd
), /* size */
162 no_add_queue
, /* add_queue */
163 NULL
, /* remove_queue */
165 NULL
, /* satisfied */
166 no_signal
, /* signal */
167 no_get_fd
, /* get_fd */
168 no_map_access
, /* map_access */
169 no_lookup_name
, /* lookup_name */
170 no_close_handle
, /* close_handle */
171 fd_destroy
/* destroy */
176 #define DEVICE_HASH_SIZE 7
177 #define INODE_HASH_SIZE 17
181 struct object obj
; /* object header */
182 struct list entry
; /* entry in device hash list */
183 dev_t dev
; /* device number */
184 int removable
; /* removable device? (or -1 if unknown) */
185 struct list inode_hash
[INODE_HASH_SIZE
]; /* inodes hash table */
188 static void device_dump( struct object
*obj
, int verbose
);
189 static void device_destroy( struct object
*obj
);
191 static const struct object_ops device_ops
=
193 sizeof(struct device
), /* size */
194 device_dump
, /* dump */
195 no_add_queue
, /* add_queue */
196 NULL
, /* remove_queue */
198 NULL
, /* satisfied */
199 no_signal
, /* signal */
200 no_get_fd
, /* get_fd */
201 no_map_access
, /* map_access */
202 no_lookup_name
, /* lookup_name */
203 no_close_handle
, /* close_handle */
204 device_destroy
/* destroy */
211 struct object obj
; /* object header */
212 struct list entry
; /* inode hash list entry */
213 struct device
*device
; /* device containing this inode */
214 ino_t ino
; /* inode number */
215 struct list open
; /* list of open file descriptors */
216 struct list locks
; /* list of file locks */
217 struct list closed
; /* list of file descriptors to close at destroy time */
220 static void inode_dump( struct object
*obj
, int verbose
);
221 static void inode_destroy( struct object
*obj
);
223 static const struct object_ops inode_ops
=
225 sizeof(struct inode
), /* size */
226 inode_dump
, /* dump */
227 no_add_queue
, /* add_queue */
228 NULL
, /* remove_queue */
230 NULL
, /* satisfied */
231 no_signal
, /* signal */
232 no_get_fd
, /* get_fd */
233 no_map_access
, /* map_access */
234 no_lookup_name
, /* lookup_name */
235 no_close_handle
, /* close_handle */
236 inode_destroy
/* destroy */
239 /* file lock object */
243 struct object obj
; /* object header */
244 struct fd
*fd
; /* fd owning this lock */
245 struct list fd_entry
; /* entry in list of locks on a given fd */
246 struct list inode_entry
; /* entry in inode list of locks */
247 int shared
; /* shared lock? */
248 file_pos_t start
; /* locked region is interval [start;end) */
250 struct process
*process
; /* process owning this lock */
251 struct list proc_entry
; /* entry in list of locks owned by the process */
254 static void file_lock_dump( struct object
*obj
, int verbose
);
255 static int file_lock_signaled( struct object
*obj
, struct thread
*thread
);
257 static const struct object_ops file_lock_ops
=
259 sizeof(struct file_lock
), /* size */
260 file_lock_dump
, /* dump */
261 add_queue
, /* add_queue */
262 remove_queue
, /* remove_queue */
263 file_lock_signaled
, /* signaled */
264 no_satisfied
, /* satisfied */
265 no_signal
, /* signal */
266 no_get_fd
, /* get_fd */
267 no_map_access
, /* map_access */
268 no_lookup_name
, /* lookup_name */
269 no_close_handle
, /* close_handle */
270 no_destroy
/* destroy */
274 #define OFF_T_MAX (~((file_pos_t)1 << (8*sizeof(off_t)-1)))
275 #define FILE_POS_T_MAX (~(file_pos_t)0)
277 static file_pos_t max_unix_offset
= OFF_T_MAX
;
279 #define DUMP_LONG_LONG(val) do { \
280 if (sizeof(val) > sizeof(unsigned long) && (val) > ~0UL) \
281 fprintf( stderr, "%lx%08lx", (unsigned long)((val) >> 32), (unsigned long)(val) ); \
283 fprintf( stderr, "%lx", (unsigned long)(val) ); \
288 /****************************************************************/
289 /* timeouts support */
293 struct list entry
; /* entry in sorted timeout list */
294 struct timeval when
; /* timeout expiry (absolute time) */
295 timeout_callback callback
; /* callback function */
296 void *private; /* callback private data */
299 static struct list timeout_list
= LIST_INIT(timeout_list
); /* sorted timeouts list */
301 /* add a timeout user */
302 struct timeout_user
*add_timeout_user( const struct timeval
*when
, timeout_callback func
,
305 struct timeout_user
*user
;
308 if (!(user
= mem_alloc( sizeof(*user
) ))) return NULL
;
310 user
->callback
= func
;
311 user
->private = private;
313 /* Now insert it in the linked list */
315 LIST_FOR_EACH( ptr
, &timeout_list
)
317 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
318 if (!time_before( &timeout
->when
, when
)) break;
320 list_add_before( ptr
, &user
->entry
);
324 /* remove a timeout user */
325 void remove_timeout_user( struct timeout_user
*user
)
327 list_remove( &user
->entry
);
331 /* add a timeout in milliseconds to an absolute time */
332 void add_timeout( struct timeval
*when
, int timeout
)
336 long sec
= timeout
/ 1000;
337 if ((when
->tv_usec
+= (timeout
- 1000*sec
) * 1000) >= 1000000)
339 when
->tv_usec
-= 1000000;
347 /****************************************************************/
350 static struct fd
**poll_users
; /* users array */
351 static struct pollfd
*pollfd
; /* poll fd array */
352 static int nb_users
; /* count of array entries actually in use */
353 static int active_users
; /* current number of active users */
354 static int allocated_users
; /* count of allocated entries in the array */
355 static struct fd
**freelist
; /* list of free entries in the array */
360 static struct epoll_event
*epoll_events
;
362 /* set the events that epoll waits for on this fd; helper for set_fd_events */
363 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
365 struct epoll_event ev
;
368 if (epoll_fd
== -1) return;
370 if (events
== -1) /* stop waiting on this fd completely */
372 if (pollfd
[user
].fd
== -1) return; /* already removed */
375 else if (pollfd
[user
].fd
== -1)
377 if (pollfd
[user
].events
) return; /* stopped waiting on it, don't restart */
382 if (pollfd
[user
].events
== events
) return; /* nothing to do */
389 if (epoll_ctl( epoll_fd
, ctl
, fd
->unix_fd
, &ev
) == -1)
391 if (errno
== ENOMEM
) /* not enough memory, give up on epoll */
396 else perror( "epoll_ctl" ); /* should not happen */
400 #else /* USE_EPOLL */
402 static inline void set_fd_epoll_events( struct fd
*fd
, int user
, int events
)
406 #endif /* USE_EPOLL */
409 /* add a user in the poll array and return its index, or -1 on failure */
410 static int add_poll_user( struct fd
*fd
)
415 ret
= freelist
- poll_users
;
416 freelist
= (struct fd
**)poll_users
[ret
];
420 if (nb_users
== allocated_users
)
422 struct fd
**newusers
;
423 struct pollfd
*newpoll
;
424 int new_count
= allocated_users
? (allocated_users
+ allocated_users
/ 2) : 16;
425 if (!(newusers
= realloc( poll_users
, new_count
* sizeof(*poll_users
) ))) return -1;
426 if (!(newpoll
= realloc( pollfd
, new_count
* sizeof(*pollfd
) )))
429 poll_users
= newusers
;
434 poll_users
= newusers
;
437 if (!allocated_users
) epoll_fd
= epoll_create( new_count
);
440 struct epoll_event
*new_events
;
441 if (!(new_events
= realloc( epoll_events
, new_count
* sizeof(*epoll_events
) )))
443 epoll_events
= new_events
;
446 allocated_users
= new_count
;
451 pollfd
[ret
].events
= 0;
452 pollfd
[ret
].revents
= 0;
453 poll_users
[ret
] = fd
;
458 /* remove a user from the poll list */
459 static void remove_poll_user( struct fd
*fd
, int user
)
462 assert( poll_users
[user
] == fd
);
465 if (epoll_fd
!= -1 && pollfd
[user
].fd
!= -1)
467 struct epoll_event dummy
;
468 epoll_ctl( epoll_fd
, EPOLL_CTL_DEL
, fd
->unix_fd
, &dummy
);
471 pollfd
[user
].fd
= -1;
472 pollfd
[user
].events
= 0;
473 pollfd
[user
].revents
= 0;
474 poll_users
[user
] = (struct fd
*)freelist
;
475 freelist
= &poll_users
[user
];
479 /* process pending timeouts and return the time until the next timeout, in milliseconds */
480 static int get_next_timeout(void)
482 if (!list_empty( &timeout_list
))
484 struct list expired_list
, *ptr
;
487 gettimeofday( &now
, NULL
);
489 /* first remove all expired timers from the list */
491 list_init( &expired_list
);
492 while ((ptr
= list_head( &timeout_list
)) != NULL
)
494 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
496 if (!time_before( &now
, &timeout
->when
))
498 list_remove( &timeout
->entry
);
499 list_add_tail( &expired_list
, &timeout
->entry
);
504 /* now call the callback for all the removed timers */
506 while ((ptr
= list_head( &expired_list
)) != NULL
)
508 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
509 list_remove( &timeout
->entry
);
510 timeout
->callback( timeout
->private );
514 if ((ptr
= list_head( &timeout_list
)) != NULL
)
516 struct timeout_user
*timeout
= LIST_ENTRY( ptr
, struct timeout_user
, entry
);
517 int diff
= (timeout
->when
.tv_sec
- now
.tv_sec
) * 1000
518 + (timeout
->when
.tv_usec
- now
.tv_usec
) / 1000;
519 if (diff
< 0) diff
= 0;
523 return -1; /* no pending timeouts */
526 /* server main poll() loop */
532 assert( POLLIN
== EPOLLIN
);
533 assert( POLLOUT
== EPOLLOUT
);
534 assert( POLLERR
== EPOLLERR
);
535 assert( POLLHUP
== EPOLLHUP
);
541 timeout
= get_next_timeout();
543 if (!active_users
) break; /* last user removed by a timeout */
544 if (epoll_fd
== -1) break; /* an error occurred with epoll */
546 ret
= epoll_wait( epoll_fd
, epoll_events
, allocated_users
, timeout
);
548 /* put the events into the pollfd array first, like poll does */
549 for (i
= 0; i
< ret
; i
++)
551 int user
= epoll_events
[i
].data
.u32
;
552 pollfd
[user
].revents
= epoll_events
[i
].events
;
555 /* read events from the pollfd array, as set_fd_events may modify them */
556 for (i
= 0; i
< ret
; i
++)
558 int user
= epoll_events
[i
].data
.u32
;
559 if (pollfd
[user
].revents
) fd_poll_event( poll_users
[user
], pollfd
[user
].revents
);
563 /* fall through to normal poll loop */
564 #endif /* USE_EPOLL */
568 timeout
= get_next_timeout();
570 if (!active_users
) break; /* last user removed by a timeout */
572 ret
= poll( pollfd
, nb_users
, timeout
);
575 for (i
= 0; i
< nb_users
; i
++)
577 if (pollfd
[i
].revents
)
579 fd_poll_event( poll_users
[i
], pollfd
[i
].revents
);
588 /****************************************************************/
589 /* device functions */
591 static struct list device_hash
[DEVICE_HASH_SIZE
];
593 /* retrieve the device object for a given fd, creating it if needed */
594 static struct device
*get_device( dev_t dev
, int create
)
596 struct device
*device
;
597 unsigned int i
, hash
= dev
% DEVICE_HASH_SIZE
;
599 if (device_hash
[hash
].next
)
601 LIST_FOR_EACH_ENTRY( device
, &device_hash
[hash
], struct device
, entry
)
602 if (device
->dev
== dev
) return (struct device
*)grab_object( device
);
604 else list_init( &device_hash
[hash
] );
606 /* not found, create it */
608 if (!create
) return NULL
;
609 if ((device
= alloc_object( &device_ops
)))
612 device
->removable
= -1;
613 for (i
= 0; i
< INODE_HASH_SIZE
; i
++) list_init( &device
->inode_hash
[i
] );
614 list_add_head( &device_hash
[hash
], &device
->entry
);
619 static void device_dump( struct object
*obj
, int verbose
)
621 struct device
*device
= (struct device
*)obj
;
622 fprintf( stderr
, "Device dev=" );
623 DUMP_LONG_LONG( device
->dev
);
624 fprintf( stderr
, "\n" );
627 static void device_destroy( struct object
*obj
)
629 struct device
*device
= (struct device
*)obj
;
632 for (i
= 0; i
< INODE_HASH_SIZE
; i
++)
633 assert( list_empty(&device
->inode_hash
[i
]) );
635 list_remove( &device
->entry
); /* remove it from the hash table */
639 /****************************************************************/
640 /* inode functions */
642 /* close all pending file descriptors in the closed list */
643 static void inode_close_pending( struct inode
*inode
, int keep_unlinks
)
645 struct list
*ptr
= list_head( &inode
->closed
);
649 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
650 struct list
*next
= list_next( &inode
->closed
, ptr
);
652 if (fd
->unix_fd
!= -1)
654 close( fd
->unix_fd
);
657 if (!keep_unlinks
|| !fd
->unlink
[0]) /* get rid of it unless there's an unlink pending on that file */
666 static void inode_dump( struct object
*obj
, int verbose
)
668 struct inode
*inode
= (struct inode
*)obj
;
669 fprintf( stderr
, "Inode device=%p ino=", inode
->device
);
670 DUMP_LONG_LONG( inode
->ino
);
671 fprintf( stderr
, "\n" );
674 static void inode_destroy( struct object
*obj
)
676 struct inode
*inode
= (struct inode
*)obj
;
679 assert( list_empty(&inode
->open
) );
680 assert( list_empty(&inode
->locks
) );
682 list_remove( &inode
->entry
);
684 while ((ptr
= list_head( &inode
->closed
)))
686 struct closed_fd
*fd
= LIST_ENTRY( ptr
, struct closed_fd
, entry
);
688 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
691 /* make sure it is still the same file */
693 if (!stat( fd
->unlink
, &st
) && st
.st_dev
== inode
->device
->dev
&& st
.st_ino
== inode
->ino
)
695 if (S_ISDIR(st
.st_mode
)) rmdir( fd
->unlink
);
696 else unlink( fd
->unlink
);
701 release_object( inode
->device
);
704 /* retrieve the inode object for a given fd, creating it if needed */
705 static struct inode
*get_inode( dev_t dev
, ino_t ino
)
707 struct device
*device
;
709 unsigned int hash
= ino
% INODE_HASH_SIZE
;
711 if (!(device
= get_device( dev
, 1 ))) return NULL
;
713 LIST_FOR_EACH_ENTRY( inode
, &device
->inode_hash
[hash
], struct inode
, entry
)
715 if (inode
->ino
== ino
)
717 release_object( device
);
718 return (struct inode
*)grab_object( inode
);
722 /* not found, create it */
723 if ((inode
= alloc_object( &inode_ops
)))
725 inode
->device
= device
;
727 list_init( &inode
->open
);
728 list_init( &inode
->locks
);
729 list_init( &inode
->closed
);
730 list_add_head( &device
->inode_hash
[hash
], &inode
->entry
);
732 else release_object( device
);
737 /* add fd to the inode list of file descriptors to close */
738 static void inode_add_closed_fd( struct inode
*inode
, struct closed_fd
*fd
)
740 if (!list_empty( &inode
->locks
))
742 list_add_head( &inode
->closed
, &fd
->entry
);
744 else if (fd
->unlink
[0]) /* close the fd but keep the structure around for unlink */
746 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
748 list_add_head( &inode
->closed
, &fd
->entry
);
750 else /* no locks on this inode and no unlink, get rid of the fd */
752 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
758 /****************************************************************/
759 /* file lock functions */
761 static void file_lock_dump( struct object
*obj
, int verbose
)
763 struct file_lock
*lock
= (struct file_lock
*)obj
;
764 fprintf( stderr
, "Lock %s fd=%p proc=%p start=",
765 lock
->shared
? "shared" : "excl", lock
->fd
, lock
->process
);
766 DUMP_LONG_LONG( lock
->start
);
767 fprintf( stderr
, " end=" );
768 DUMP_LONG_LONG( lock
->end
);
769 fprintf( stderr
, "\n" );
772 static int file_lock_signaled( struct object
*obj
, struct thread
*thread
)
774 struct file_lock
*lock
= (struct file_lock
*)obj
;
775 /* lock is signaled if it has lost its owner */
776 return !lock
->process
;
779 /* set (or remove) a Unix lock if possible for the given range */
780 static int set_unix_lock( struct fd
*fd
, file_pos_t start
, file_pos_t end
, int type
)
784 if (!fd
->fs_locks
) return 1; /* no fs locks possible for this fd */
787 if (start
== end
) return 1; /* can't set zero-byte lock */
788 if (start
> max_unix_offset
) return 1; /* ignore it */
790 fl
.l_whence
= SEEK_SET
;
792 if (!end
|| end
> max_unix_offset
) fl
.l_len
= 0;
793 else fl
.l_len
= end
- start
;
794 if (fcntl( fd
->unix_fd
, F_SETLK
, &fl
) != -1) return 1;
799 /* check whether locks work at all on this file system */
800 if (fcntl( fd
->unix_fd
, F_GETLK
, &fl
) != -1)
802 set_error( STATUS_FILE_LOCK_CONFLICT
);
808 /* no locking on this fs, just ignore it */
812 set_error( STATUS_FILE_LOCK_CONFLICT
);
815 /* this can happen if we try to set a write lock on a read-only file */
816 /* we just ignore that error */
817 if (fl
.l_type
== F_WRLCK
) return 1;
818 set_error( STATUS_ACCESS_DENIED
);
824 /* this can happen if off_t is 64-bit but the kernel only supports 32-bit */
825 /* in that case we shrink the limit and retry */
826 if (max_unix_offset
> INT_MAX
)
828 max_unix_offset
= INT_MAX
;
839 /* check if interval [start;end) overlaps the lock */
840 inline static int lock_overlaps( struct file_lock
*lock
, file_pos_t start
, file_pos_t end
)
842 if (lock
->end
&& start
>= lock
->end
) return 0;
843 if (end
&& lock
->start
>= end
) return 0;
847 /* remove Unix locks for all bytes in the specified area that are no longer locked */
848 static void remove_unix_locks( struct fd
*fd
, file_pos_t start
, file_pos_t end
)
856 } *first
, *cur
, *next
, *buffer
;
861 if (!fd
->inode
) return;
862 if (!fd
->fs_locks
) return;
863 if (start
== end
|| start
> max_unix_offset
) return;
864 if (!end
|| end
> max_unix_offset
) end
= max_unix_offset
+ 1;
866 /* count the number of locks overlapping the specified area */
868 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
870 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
871 if (lock
->start
== lock
->end
) continue;
872 if (lock_overlaps( lock
, start
, end
)) count
++;
875 if (!count
) /* no locks at all, we can unlock everything */
877 set_unix_lock( fd
, start
, end
, F_UNLCK
);
881 /* allocate space for the list of holes */
882 /* max. number of holes is number of locks + 1 */
884 if (!(buffer
= malloc( sizeof(*buffer
) * (count
+1) ))) return;
888 first
->start
= start
;
892 /* build a sorted list of unlocked holes in the specified area */
894 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
896 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
897 if (lock
->start
== lock
->end
) continue;
898 if (!lock_overlaps( lock
, start
, end
)) continue;
900 /* go through all the holes touched by this lock */
901 for (cur
= first
; cur
; cur
= cur
->next
)
903 if (cur
->end
<= lock
->start
) continue; /* hole is before start of lock */
904 if (lock
->end
&& cur
->start
>= lock
->end
) break; /* hole is after end of lock */
906 /* now we know that lock is overlapping hole */
908 if (cur
->start
>= lock
->start
) /* lock starts before hole, shrink from start */
910 cur
->start
= lock
->end
;
911 if (cur
->start
&& cur
->start
< cur
->end
) break; /* done with this lock */
912 /* now hole is empty, remove it */
913 if (cur
->next
) cur
->next
->prev
= cur
->prev
;
914 if (cur
->prev
) cur
->prev
->next
= cur
->next
;
915 else if (!(first
= cur
->next
)) goto done
; /* no more holes at all */
917 else if (!lock
->end
|| cur
->end
<= lock
->end
) /* lock larger than hole, shrink from end */
919 cur
->end
= lock
->start
;
920 assert( cur
->start
< cur
->end
);
922 else /* lock is in the middle of hole, split hole in two */
925 next
->next
= cur
->next
;
927 next
->start
= lock
->end
;
928 next
->end
= cur
->end
;
929 cur
->end
= lock
->start
;
930 assert( next
->start
< next
->end
);
931 assert( cur
->end
< next
->start
);
933 break; /* done with this lock */
938 /* clear Unix locks for all the holes */
940 for (cur
= first
; cur
; cur
= cur
->next
)
941 set_unix_lock( fd
, cur
->start
, cur
->end
, F_UNLCK
);
947 /* create a new lock on a fd */
948 static struct file_lock
*add_lock( struct fd
*fd
, int shared
, file_pos_t start
, file_pos_t end
)
950 struct file_lock
*lock
;
952 if (!fd
->inode
) /* not a regular file */
954 set_error( STATUS_INVALID_HANDLE
);
958 if (!(lock
= alloc_object( &file_lock_ops
))) return NULL
;
959 lock
->shared
= shared
;
963 lock
->process
= current
->process
;
965 /* now try to set a Unix lock */
966 if (!set_unix_lock( lock
->fd
, lock
->start
, lock
->end
, lock
->shared
? F_RDLCK
: F_WRLCK
))
968 release_object( lock
);
971 list_add_head( &fd
->locks
, &lock
->fd_entry
);
972 list_add_head( &fd
->inode
->locks
, &lock
->inode_entry
);
973 list_add_head( &lock
->process
->locks
, &lock
->proc_entry
);
977 /* remove an existing lock */
978 static void remove_lock( struct file_lock
*lock
, int remove_unix
)
980 struct inode
*inode
= lock
->fd
->inode
;
982 list_remove( &lock
->fd_entry
);
983 list_remove( &lock
->inode_entry
);
984 list_remove( &lock
->proc_entry
);
985 if (remove_unix
) remove_unix_locks( lock
->fd
, lock
->start
, lock
->end
);
986 if (list_empty( &inode
->locks
)) inode_close_pending( inode
, 1 );
987 lock
->process
= NULL
;
988 wake_up( &lock
->obj
, 0 );
989 release_object( lock
);
992 /* remove all locks owned by a given process */
993 void remove_process_locks( struct process
*process
)
997 while ((ptr
= list_head( &process
->locks
)))
999 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, proc_entry
);
1000 remove_lock( lock
, 1 ); /* this removes it from the list */
1004 /* remove all locks on a given fd */
1005 static void remove_fd_locks( struct fd
*fd
)
1007 file_pos_t start
= FILE_POS_T_MAX
, end
= 0;
1010 while ((ptr
= list_head( &fd
->locks
)))
1012 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
1013 if (lock
->start
< start
) start
= lock
->start
;
1014 if (!lock
->end
|| lock
->end
> end
) end
= lock
->end
- 1;
1015 remove_lock( lock
, 0 );
1017 if (start
< end
) remove_unix_locks( fd
, start
, end
+ 1 );
1020 /* add a lock on an fd */
1021 /* returns handle to wait on */
1022 obj_handle_t
lock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
, int shared
, int wait
)
1025 file_pos_t end
= start
+ count
;
1027 /* don't allow wrapping locks */
1028 if (end
&& end
< start
)
1030 set_error( STATUS_INVALID_PARAMETER
);
1034 /* check if another lock on that file overlaps the area */
1035 LIST_FOR_EACH( ptr
, &fd
->inode
->locks
)
1037 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, inode_entry
);
1038 if (!lock_overlaps( lock
, start
, end
)) continue;
1039 if (lock
->shared
&& shared
) continue;
1043 set_error( STATUS_FILE_LOCK_CONFLICT
);
1046 set_error( STATUS_PENDING
);
1047 return alloc_handle( current
->process
, lock
, SYNCHRONIZE
, 0 );
1050 /* not found, add it */
1051 if (add_lock( fd
, shared
, start
, end
)) return 0;
1052 if (get_error() == STATUS_FILE_LOCK_CONFLICT
)
1054 /* Unix lock conflict -> tell client to wait and retry */
1055 if (wait
) set_error( STATUS_PENDING
);
1060 /* remove a lock on an fd */
1061 void unlock_fd( struct fd
*fd
, file_pos_t start
, file_pos_t count
)
1064 file_pos_t end
= start
+ count
;
1066 /* find an existing lock with the exact same parameters */
1067 LIST_FOR_EACH( ptr
, &fd
->locks
)
1069 struct file_lock
*lock
= LIST_ENTRY( ptr
, struct file_lock
, fd_entry
);
1070 if ((lock
->start
== start
) && (lock
->end
== end
))
1072 remove_lock( lock
, 1 );
1076 set_error( STATUS_FILE_LOCK_CONFLICT
);
1080 /****************************************************************/
1081 /* asynchronous operations support */
1085 struct thread
*thread
;
1089 struct timeout_user
*timeout
;
1093 /* notifies client thread of new status of its async request */
1094 /* destroys the server side of it */
1095 static void async_terminate( struct async
*async
, int status
)
1097 thread_queue_apc( async
->thread
, NULL
, async
->apc
, APC_ASYNC_IO
,
1098 1, async
->user
, async
->sb
, (void *)status
);
1100 if (async
->timeout
) remove_timeout_user( async
->timeout
);
1101 async
->timeout
= NULL
;
1102 list_remove( &async
->entry
);
1103 release_object( async
->thread
);
1107 /* cb for timeout on an async request */
1108 static void async_callback(void *private)
1110 struct async
*async
= (struct async
*)private;
1112 /* fprintf(stderr, "async timeout out %p\n", async); */
1113 async
->timeout
= NULL
;
1114 async_terminate( async
, STATUS_TIMEOUT
);
1117 /* create an async on a given queue of a fd */
1118 struct async
*create_async(struct thread
*thread
, int* timeout
, struct list
*queue
,
1119 void *io_apc
, void *io_user
, void* io_sb
)
1121 struct async
*async
= mem_alloc( sizeof(struct async
) );
1123 if (!async
) return NULL
;
1125 async
->thread
= (struct thread
*)grab_object(thread
);
1126 async
->apc
= io_apc
;
1127 async
->user
= io_user
;
1130 list_add_tail( queue
, &async
->entry
);
1134 struct timeval when
;
1136 gettimeofday( &when
, NULL
);
1137 add_timeout( &when
, *timeout
);
1138 async
->timeout
= add_timeout_user( &when
, async_callback
, async
);
1140 else async
->timeout
= NULL
;
1145 /* terminate the async operation at the head of the queue */
1146 void async_terminate_head( struct list
*queue
, int status
)
1148 struct list
*ptr
= list_head( queue
);
1149 if (ptr
) async_terminate( LIST_ENTRY( ptr
, struct async
, entry
), status
);
1152 /****************************************************************/
1153 /* file descriptor functions */
1155 static void fd_dump( struct object
*obj
, int verbose
)
1157 struct fd
*fd
= (struct fd
*)obj
;
1158 fprintf( stderr
, "Fd unix_fd=%d user=%p", fd
->unix_fd
, fd
->user
);
1159 if (fd
->inode
) fprintf( stderr
, " inode=%p unlink='%s'", fd
->inode
, fd
->closed
->unlink
);
1160 fprintf( stderr
, "\n" );
1163 static void fd_destroy( struct object
*obj
)
1165 struct fd
*fd
= (struct fd
*)obj
;
1167 async_terminate_queue( &fd
->read_q
, STATUS_CANCELLED
);
1168 async_terminate_queue( &fd
->write_q
, STATUS_CANCELLED
);
1170 remove_fd_locks( fd
);
1171 list_remove( &fd
->inode_entry
);
1172 if (fd
->poll_index
!= -1) remove_poll_user( fd
, fd
->poll_index
);
1175 inode_add_closed_fd( fd
->inode
, fd
->closed
);
1176 release_object( fd
->inode
);
1178 else /* no inode, close it right away */
1180 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1184 /* set the events that select waits for on this fd */
1185 void set_fd_events( struct fd
*fd
, int events
)
1187 int user
= fd
->poll_index
;
1188 assert( poll_users
[user
] == fd
);
1190 set_fd_epoll_events( fd
, user
, events
);
1192 if (events
== -1) /* stop waiting on this fd completely */
1194 pollfd
[user
].fd
= -1;
1195 pollfd
[user
].events
= POLLERR
;
1196 pollfd
[user
].revents
= 0;
1198 else if (pollfd
[user
].fd
!= -1 || !pollfd
[user
].events
)
1200 pollfd
[user
].fd
= fd
->unix_fd
;
1201 pollfd
[user
].events
= events
;
1205 /* prepare an fd for unmounting its corresponding device */
1206 static inline void unmount_fd( struct fd
*fd
)
1208 assert( fd
->inode
);
1210 async_terminate_queue( &fd
->read_q
, STATUS_VOLUME_DISMOUNTED
);
1211 async_terminate_queue( &fd
->write_q
, STATUS_VOLUME_DISMOUNTED
);
1213 if (fd
->poll_index
!= -1) set_fd_events( fd
, -1 );
1215 if (fd
->unix_fd
!= -1) close( fd
->unix_fd
);
1219 fd
->closed
->unix_fd
= -1;
1220 fd
->closed
->unlink
[0] = 0;
1222 /* stop using Unix locks on this fd (existing locks have been removed by close) */
1226 /* allocate an fd object, without setting the unix fd yet */
1227 struct fd
*alloc_fd( const struct fd_ops
*fd_user_ops
, struct object
*user
)
1229 struct fd
*fd
= alloc_object( &fd_ops
);
1231 if (!fd
) return NULL
;
1233 fd
->fd_ops
= fd_user_ops
;
1242 fd
->poll_index
= -1;
1243 list_init( &fd
->inode_entry
);
1244 list_init( &fd
->locks
);
1245 list_init( &fd
->read_q
);
1246 list_init( &fd
->write_q
);
1248 if ((fd
->poll_index
= add_poll_user( fd
)) == -1)
1250 release_object( fd
);
1256 /* allocate a pseudo fd object, for objects that need to behave like files but don't have a unix fd */
1257 struct fd
*alloc_pseudo_fd( const struct fd_ops
*fd_user_ops
, struct object
*user
)
1259 struct fd
*fd
= alloc_object( &fd_ops
);
1261 if (!fd
) return NULL
;
1263 fd
->fd_ops
= fd_user_ops
;
1272 fd
->poll_index
= -1;
1273 list_init( &fd
->inode_entry
);
1274 list_init( &fd
->locks
);
1275 list_init( &fd
->read_q
);
1276 list_init( &fd
->write_q
);
1280 /* check if the desired access is possible without violating */
1281 /* the sharing mode of other opens of the same file */
1282 static int check_sharing( struct fd
*fd
, unsigned int access
, unsigned int sharing
)
1284 unsigned int existing_sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
1285 unsigned int existing_access
= 0;
1289 /* if access mode is 0, sharing mode is ignored */
1290 if (!access
) sharing
= existing_sharing
;
1291 fd
->access
= access
;
1292 fd
->sharing
= sharing
;
1294 LIST_FOR_EACH( ptr
, &fd
->inode
->open
)
1296 struct fd
*fd_ptr
= LIST_ENTRY( ptr
, struct fd
, inode_entry
);
1299 existing_sharing
&= fd_ptr
->sharing
;
1300 existing_access
|= fd_ptr
->access
;
1301 if (fd_ptr
->closed
->unlink
[0]) unlink
= 1;
1305 if ((access
& FILE_UNIX_READ_ACCESS
) && !(existing_sharing
& FILE_SHARE_READ
)) return 0;
1306 if ((access
& FILE_UNIX_WRITE_ACCESS
) && !(existing_sharing
& FILE_SHARE_WRITE
)) return 0;
1307 if ((existing_access
& FILE_UNIX_READ_ACCESS
) && !(sharing
& FILE_SHARE_READ
)) return 0;
1308 if ((existing_access
& FILE_UNIX_WRITE_ACCESS
) && !(sharing
& FILE_SHARE_WRITE
)) return 0;
1309 if (fd
->closed
->unlink
[0] && !(existing_sharing
& FILE_SHARE_DELETE
)) return 0;
1310 if (unlink
&& !(sharing
& FILE_SHARE_DELETE
)) return 0;
1314 /* open() wrapper using a struct fd */
1315 /* the fd must have been created with alloc_fd */
1316 /* on error the fd object is released */
1317 struct fd
*open_fd( struct fd
*fd
, const char *name
, int flags
, mode_t
*mode
,
1318 unsigned int access
, unsigned int sharing
, unsigned int options
)
1321 struct closed_fd
*closed_fd
;
1322 const char *unlink_name
= "";
1324 assert( fd
->unix_fd
== -1 );
1326 if (options
& FILE_DELETE_ON_CLOSE
) unlink_name
= name
;
1327 if (!(closed_fd
= mem_alloc( sizeof(*closed_fd
) + strlen(unlink_name
) )))
1329 release_object( fd
);
1332 /* create the directory if needed */
1333 if ((options
& FILE_DIRECTORY_FILE
) && (flags
& O_CREAT
))
1335 if (mkdir( name
, 0777 ) == -1)
1337 if (errno
!= EEXIST
|| (flags
& O_EXCL
))
1340 release_object( fd
);
1345 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
1347 if ((fd
->unix_fd
= open( name
, flags
& ~O_TRUNC
, *mode
)) == -1)
1350 release_object( fd
);
1354 closed_fd
->unix_fd
= fd
->unix_fd
;
1355 closed_fd
->unlink
[0] = 0;
1356 fstat( fd
->unix_fd
, &st
);
1359 /* only bother with an inode for normal files and directories */
1360 if (S_ISREG(st
.st_mode
) || S_ISDIR(st
.st_mode
))
1362 struct inode
*inode
= get_inode( st
.st_dev
, st
.st_ino
);
1366 /* we can close the fd because there are no others open on the same file,
1367 * otherwise we wouldn't have failed to allocate a new inode
1372 fd
->closed
= closed_fd
;
1373 list_add_head( &inode
->open
, &fd
->inode_entry
);
1375 /* check directory options */
1376 if ((options
& FILE_DIRECTORY_FILE
) && !S_ISDIR(st
.st_mode
))
1378 release_object( fd
);
1379 set_error( STATUS_NOT_A_DIRECTORY
);
1382 if ((options
& FILE_NON_DIRECTORY_FILE
) && S_ISDIR(st
.st_mode
))
1384 release_object( fd
);
1385 set_error( STATUS_FILE_IS_A_DIRECTORY
);
1388 if (!check_sharing( fd
, access
, sharing
))
1390 release_object( fd
);
1391 set_error( STATUS_SHARING_VIOLATION
);
1394 strcpy( closed_fd
->unlink
, unlink_name
);
1395 if (flags
& O_TRUNC
) ftruncate( fd
->unix_fd
, 0 );
1397 else /* special file */
1399 if (options
& FILE_DIRECTORY_FILE
)
1401 set_error( STATUS_NOT_A_DIRECTORY
);
1404 if (unlink_name
[0]) /* we can't unlink special files */
1406 set_error( STATUS_INVALID_PARAMETER
);
1414 release_object( fd
);
1419 /* create an fd for an anonymous file */
1420 /* if the function fails the unix fd is closed */
1421 struct fd
*create_anonymous_fd( const struct fd_ops
*fd_user_ops
, int unix_fd
, struct object
*user
)
1423 struct fd
*fd
= alloc_fd( fd_user_ops
, user
);
1427 fd
->unix_fd
= unix_fd
;
1434 /* retrieve the object that is using an fd */
1435 void *get_fd_user( struct fd
*fd
)
1440 /* retrieve the unix fd for an object */
1441 int get_unix_fd( struct fd
*fd
)
1443 if (fd
->unix_fd
== -1)
1445 if (fd
->unmounted
) set_error( STATUS_VOLUME_DISMOUNTED
);
1446 else set_error( STATUS_BAD_DEVICE_TYPE
);
1451 /* check if two file descriptors point to the same file */
1452 int is_same_file_fd( struct fd
*fd1
, struct fd
*fd2
)
1454 return fd1
->inode
== fd2
->inode
;
1457 /* callback for event happening in the main poll() loop */
1458 void fd_poll_event( struct fd
*fd
, int event
)
1460 return fd
->fd_ops
->poll_event( fd
, event
);
1463 /* check if events are pending and if yes return which one(s) */
1464 int check_fd_events( struct fd
*fd
, int events
)
1468 if (fd
->unix_fd
== -1) return POLLERR
;
1470 pfd
.fd
= fd
->unix_fd
;
1471 pfd
.events
= events
;
1472 if (poll( &pfd
, 1, 0 ) <= 0) return 0;
1476 /* default add_queue() routine for objects that poll() on an fd */
1477 int default_fd_add_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1479 struct fd
*fd
= get_obj_fd( obj
);
1482 if (!fd
->inode
&& list_empty( &obj
->wait_queue
)) /* first on the queue */
1483 set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
1484 add_queue( obj
, entry
);
1485 release_object( fd
);
1489 /* default remove_queue() routine for objects that poll() on an fd */
1490 void default_fd_remove_queue( struct object
*obj
, struct wait_queue_entry
*entry
)
1492 struct fd
*fd
= get_obj_fd( obj
);
1495 remove_queue( obj
, entry
);
1496 if (!fd
->inode
&& list_empty( &obj
->wait_queue
)) /* last on the queue is gone */
1497 set_fd_events( fd
, 0 );
1498 release_object( obj
);
1499 release_object( fd
);
1502 /* default signaled() routine for objects that poll() on an fd */
1503 int default_fd_signaled( struct object
*obj
, struct thread
*thread
)
1506 struct fd
*fd
= get_obj_fd( obj
);
1508 if (fd
->inode
) ret
= 1; /* regular files are always signaled */
1511 events
= fd
->fd_ops
->get_poll_events( fd
);
1512 ret
= check_fd_events( fd
, events
) != 0;
1516 /* stop waiting on select() if we are signaled */
1517 set_fd_events( fd
, 0 );
1519 else if (!list_empty( &obj
->wait_queue
))
1521 /* restart waiting on poll() if we are no longer signaled */
1522 set_fd_events( fd
, events
);
1525 release_object( fd
);
1529 int default_fd_get_poll_events( struct fd
*fd
)
1533 if (!list_empty( &fd
->read_q
))
1535 if (!list_empty( &fd
->write_q
))
1541 /* default handler for poll() events */
1542 void default_poll_event( struct fd
*fd
, int event
)
1544 if (!list_empty( &fd
->read_q
) && (POLLIN
& event
) )
1546 async_terminate_head( &fd
->read_q
, STATUS_ALERTED
);
1549 if (!list_empty( &fd
->write_q
) && (POLLOUT
& event
) )
1551 async_terminate_head( &fd
->write_q
, STATUS_ALERTED
);
1555 /* if an error occurred, stop polling this fd to avoid busy-looping */
1556 if (event
& (POLLERR
| POLLHUP
)) set_fd_events( fd
, -1 );
1557 wake_up( fd
->user
, 0 );
1560 void fd_queue_async_timeout( struct fd
*fd
, void *apc
, void *user
, void *io_sb
, int type
, int count
, int *timeout
)
1565 if (!(fd
->fd_ops
->get_file_info( fd
) & (FD_FLAG_OVERLAPPED
|FD_FLAG_TIMEOUT
)))
1567 set_error( STATUS_INVALID_HANDLE
);
1573 case ASYNC_TYPE_READ
:
1574 queue
= &fd
->read_q
;
1576 case ASYNC_TYPE_WRITE
:
1577 queue
= &fd
->write_q
;
1580 set_error( STATUS_INVALID_PARAMETER
);
1584 if (!create_async( current
, timeout
, queue
, apc
, user
, io_sb
))
1587 /* Check if the new pending request can be served immediately */
1588 events
= check_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
1589 if (events
) fd
->fd_ops
->poll_event( fd
, events
);
1591 set_fd_events( fd
, fd
->fd_ops
->get_poll_events( fd
) );
1594 void default_fd_queue_async( struct fd
*fd
, void *apc
, void *user
, void *io_sb
, int type
, int count
)
1596 fd_queue_async_timeout( fd
, apc
, user
, io_sb
, type
, count
, NULL
);
1599 void default_fd_cancel_async( struct fd
*fd
)
1601 async_terminate_queue( &fd
->read_q
, STATUS_CANCELLED
);
1602 async_terminate_queue( &fd
->write_q
, STATUS_CANCELLED
);
1605 /* default flush() routine */
1606 int no_flush( struct fd
*fd
, struct event
**event
)
1608 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1612 /* default get_file_info() routine */
1613 int no_get_file_info( struct fd
*fd
)
1615 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1619 /* default queue_async() routine */
1620 void no_queue_async( struct fd
*fd
, void* apc
, void* user
, void* io_sb
,
1621 int type
, int count
)
1623 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1626 /* default cancel_async() routine */
1627 void no_cancel_async( struct fd
*fd
)
1629 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
1632 /* close all Unix file descriptors on a device to allow unmounting it */
1633 static void unmount_device( struct fd
*device_fd
)
1637 struct device
*device
;
1638 struct inode
*inode
;
1640 int unix_fd
= get_unix_fd( device_fd
);
1642 if (unix_fd
== -1) return;
1644 if (fstat( unix_fd
, &st
) == -1 || !S_ISBLK( st
.st_mode
))
1646 set_error( STATUS_INVALID_PARAMETER
);
1650 if (!(device
= get_device( st
.st_rdev
, 0 ))) return;
1652 for (i
= 0; i
< INODE_HASH_SIZE
; i
++)
1654 LIST_FOR_EACH_ENTRY( inode
, &device
->inode_hash
[i
], struct inode
, entry
)
1656 LIST_FOR_EACH_ENTRY( fd
, &inode
->open
, struct fd
, inode_entry
)
1660 inode_close_pending( inode
, 0 );
1663 /* remove it from the hash table */
1664 list_remove( &device
->entry
);
1665 list_init( &device
->entry
);
1666 release_object( device
);
1669 /* same as get_handle_obj but retrieve the struct fd associated to the object */
1670 static struct fd
*get_handle_fd_obj( struct process
*process
, obj_handle_t handle
,
1671 unsigned int access
)
1673 struct fd
*fd
= NULL
;
1676 if ((obj
= get_handle_obj( process
, handle
, access
, NULL
)))
1678 fd
= get_obj_fd( obj
);
1679 release_object( obj
);
1684 /* flush a file buffers */
1685 DECL_HANDLER(flush_file
)
1687 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1688 struct event
* event
= NULL
;
1692 fd
->fd_ops
->flush( fd
, &event
);
1695 reply
->event
= alloc_handle( current
->process
, event
, SYNCHRONIZE
, 0 );
1697 release_object( fd
);
1701 /* open a file object */
1702 DECL_HANDLER(open_file_object
)
1704 struct unicode_str name
;
1705 struct directory
*root
= NULL
;
1708 get_req_unicode_str( &name
);
1709 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
1712 if ((obj
= open_object_dir( root
, &name
, req
->attributes
, NULL
)))
1714 /* make sure this is a valid file object */
1715 struct fd
*fd
= get_obj_fd( obj
);
1718 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->attributes
);
1719 release_object( fd
);
1721 release_object( obj
);
1724 if (root
) release_object( root
);
1727 /* get a Unix fd to access a file */
1728 DECL_HANDLER(get_handle_fd
)
1734 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, req
->access
)))
1736 int unix_fd
= get_unix_fd( fd
);
1739 int cached_fd
= get_handle_unix_fd( current
->process
, req
->handle
, req
->access
);
1740 if (cached_fd
!= -1) reply
->fd
= cached_fd
;
1741 else if (!get_error()) send_client_fd( current
->process
, unix_fd
, req
->handle
);
1743 if (fd
->inode
) reply
->removable
= fd
->inode
->device
->removable
;
1744 reply
->flags
= fd
->fd_ops
->get_file_info( fd
);
1745 release_object( fd
);
1749 /* set the cached file descriptor of a handle */
1750 DECL_HANDLER(set_handle_fd
)
1755 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
1757 struct device
*device
= fd
->inode
? fd
->inode
->device
: NULL
;
1759 if (device
&& device
->removable
== -1) device
->removable
= req
->removable
;
1761 /* only cache the fd on non-removable devices */
1762 if (!device
|| !device
->removable
)
1763 reply
->cur_fd
= set_handle_unix_fd( current
->process
, req
->handle
, req
->fd
);
1764 release_object( fd
);
1768 /* get ready to unmount a Unix device */
1769 DECL_HANDLER(unmount_device
)
1773 if ((fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 )))
1775 unmount_device( fd
);
1776 release_object( fd
);
1780 /* create / reschedule an async I/O */
1781 DECL_HANDLER(register_async
)
1783 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1786 * The queue_async method must do the following:
1788 * 1. Get the async_queue for the request of given type.
1789 * 2. Create a new asynchronous request for the selected queue
1790 * 3. Carry out any operations necessary to adjust the object's poll events
1791 * Usually: set_elect_events (obj, obj->ops->get_poll_events()).
1792 * 4. When the async request is triggered, then send back (with a proper APC)
1793 * the trigger (STATUS_ALERTED) to the thread that posted the request.
1794 * async_destroy() is to be called: it will both notify the sender about
1795 * the trigger and destroy the request by itself
1796 * See also the implementations in file.c, serial.c, and sock.c.
1801 fd
->fd_ops
->queue_async( fd
, req
->io_apc
, req
->io_user
, req
->io_sb
,
1802 req
->type
, req
->count
);
1803 release_object( fd
);
1807 /* cancels all async I/O */
1808 DECL_HANDLER(cancel_async
)
1810 struct fd
*fd
= get_handle_fd_obj( current
->process
, req
->handle
, 0 );
1813 /* Note: we don't kill the queued APC_ASYNC_IO on this thread because
1814 * NtCancelIoFile() will force the pending APC to be run. Since,
1815 * Windows only guarantees that the current thread will have no async
1816 * operation on the current fd when NtCancelIoFile returns, this shall
1819 fd
->fd_ops
->cancel_async( fd
);
1820 release_object( fd
);