epoc32/include/stdapis/sys/file.h
branchSymbian2
changeset 2 2fe1408b6811
parent 0 061f57f2323e
child 4 837f303aceeb
     1.1 --- a/epoc32/include/stdapis/sys/file.h	Tue Nov 24 13:55:44 2009 +0000
     1.2 +++ b/epoc32/include/stdapis/sys/file.h	Tue Mar 16 16:12:26 2010 +0000
     1.3 @@ -1,1 +1,284 @@
     1.4 -file.h
     1.5 +/*
     1.6 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
     1.7 +
     1.8 +* Redistribution and use in source and binary forms, with or without 
     1.9 +* modification, are permitted provided that the following conditions are met:
    1.10 +
    1.11 +* Redistributions of source code must retain the above copyright notice, this 
    1.12 +* list of conditions and the following disclaimer.
    1.13 +* Redistributions in binary form must reproduce the above copyright notice, 
    1.14 +* this list of conditions and the following disclaimer in the documentation 
    1.15 +* and/or other materials provided with the distribution.
    1.16 +* Neither the name of Nokia Corporation nor the names of its contributors 
    1.17 +* may be used to endorse or promote products derived from this software 
    1.18 +* without specific prior written permission.
    1.19 +
    1.20 +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    1.21 +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    1.22 +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
    1.23 +* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
    1.24 +* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
    1.25 +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    1.26 +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
    1.27 +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
    1.28 +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.29 +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +*
    1.31 +* Description:
    1.32 +*
    1.33 +*/
    1.34 +
    1.35 +
    1.36 +#ifndef _SYS_FILE_H_
    1.37 +#define	_SYS_FILE_H_
    1.38 +
    1.39 +#ifndef _KERNEL
    1.40 +#include <sys/types.h> /* XXX */
    1.41 +#include <sys/fcntl.h>
    1.42 +#include <sys/unistd.h>
    1.43 +#else
    1.44 +#include <sys/queue.h>
    1.45 +#include <sys/_lock.h>
    1.46 +#include <sys/_mutex.h>
    1.47 +
    1.48 +struct stat;
    1.49 +struct thread;
    1.50 +struct uio;
    1.51 +struct knote;
    1.52 +struct vnode;
    1.53 +struct socket;
    1.54 +
    1.55 +
    1.56 +#endif /* _KERNEL */
    1.57 +
    1.58 +#define	DTYPE_VNODE	1	/* file */
    1.59 +#define	DTYPE_SOCKET	2	/* communications endpoint */
    1.60 +#define	DTYPE_PIPE	3	/* pipe */
    1.61 +#define	DTYPE_FIFO	4	/* fifo (named pipe) */
    1.62 +#define	DTYPE_KQUEUE	5	/* event queue */
    1.63 +#define	DTYPE_CRYPTO	6	/* crypto */
    1.64 +
    1.65 +#ifdef _KERNEL
    1.66 +
    1.67 +struct file;
    1.68 +struct ucred;
    1.69 +
    1.70 +typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
    1.71 +		    struct ucred *active_cred, int flags,
    1.72 +		    struct thread *td);
    1.73 +#define	FOF_OFFSET	1	/* Use the offset in uio argument */
    1.74 +typedef	int fo_ioctl_t(struct file *fp, u_long com, void *data,
    1.75 +		    struct ucred *active_cred, struct thread *td);
    1.76 +typedef	int fo_poll_t(struct file *fp, int events,
    1.77 +		    struct ucred *active_cred, struct thread *td);
    1.78 +typedef	int fo_kqfilter_t(struct file *fp, struct knote *kn);
    1.79 +typedef	int fo_stat_t(struct file *fp, struct stat *sb,
    1.80 +		    struct ucred *active_cred, struct thread *td);
    1.81 +typedef	int fo_close_t(struct file *fp, struct thread *td);
    1.82 +typedef	int fo_flags_t;
    1.83 +
    1.84 +struct fileops {
    1.85 +	fo_rdwr_t	*fo_read;
    1.86 +	fo_rdwr_t	*fo_write;
    1.87 +	fo_ioctl_t	*fo_ioctl;
    1.88 +	fo_poll_t	*fo_poll;
    1.89 +	fo_kqfilter_t	*fo_kqfilter;
    1.90 +	fo_stat_t	*fo_stat;
    1.91 +	fo_close_t	*fo_close;
    1.92 +	fo_flags_t	fo_flags;	/* DFLAG_* below */
    1.93 +};
    1.94 +
    1.95 +#define DFLAG_PASSABLE	0x01	/* may be passed via unix sockets. */
    1.96 +#define DFLAG_SEEKABLE	0x02	/* seekable / nonsequential */
    1.97 +
    1.98 +/*
    1.99 + * Kernel descriptor table.
   1.100 + * One entry for each open kernel vnode and socket.
   1.101 + *
   1.102 + * Below is the list of locks that protects members in struct file.
   1.103 + *
   1.104 + * (fl)	filelist_lock
   1.105 + * (f)	f_mtx in struct file
   1.106 + * none	not locked
   1.107 + */
   1.108 +
   1.109 +struct file {
   1.110 +	LIST_ENTRY(file) f_list;/* (fl) list of active files */
   1.111 +	short	f_type;		/* descriptor type */
   1.112 +	void	*f_data;	/* file descriptor specific data */
   1.113 +	u_int	f_flag;		/* see fcntl.h */
   1.114 +	struct mtx	*f_mtxp;	/* mutex to protect data */
   1.115 +	struct fileops *f_ops;	/* File operations */
   1.116 +	struct	ucred *f_cred;	/* credentials associated with descriptor */
   1.117 +	int	f_count;	/* (f) reference count */
   1.118 +	struct vnode *f_vnode;	/* NULL or applicable vnode */
   1.119 +
   1.120 +	/* DFLAG_SEEKABLE specific fields */
   1.121 +	off_t	f_offset;
   1.122 +
   1.123 +	/* DTYPE_SOCKET specific fields */
   1.124 +	short	f_gcflag;	/* used by thread doing fd garbage collection */
   1.125 +#define	FMARK		0x1	/* mark during gc() */
   1.126 +#define	FDEFER		0x2	/* defer for next gc pass */
   1.127 +	int	f_msgcount;	/* (f) references from message queue */
   1.128 +
   1.129 +	/* DTYPE_VNODE specific fields */
   1.130 +	int	f_seqcount;	/*
   1.131 +				 * count of sequential accesses -- cleared
   1.132 +				 * by most seek operations.
   1.133 +				 */
   1.134 +	off_t	f_nextoff;	/*
   1.135 +				 * offset of next expected read or write
   1.136 +				 */
   1.137 +	void	*f_label;	/* Place-holder for struct label pointer. */
   1.138 +};
   1.139 +
   1.140 +#endif /* _KERNEL */
   1.141 +
   1.142 +/*
   1.143 + * Userland version of struct file, for sysctl
   1.144 + */
   1.145 +struct xfile {
   1.146 +	size_t	xf_size;	/* size of struct xfile */
   1.147 +	pid_t	xf_pid;		/* owning process */
   1.148 +	uid_t	xf_uid;		/* effective uid of owning process */
   1.149 +	int	xf_fd;		/* descriptor number */
   1.150 +	void	*xf_file;	/* address of struct file */
   1.151 +	short	xf_type;	/* descriptor type */
   1.152 +	int	xf_count;	/* reference count */
   1.153 +	int	xf_msgcount;	/* references from message queue */
   1.154 +	off_t	xf_offset;	/* file offset */
   1.155 +	void	*xf_data;	/* file descriptor specific data */
   1.156 +	void	*xf_vnode;	/* vnode pointer */
   1.157 +	u_int	xf_flag;	/* flags (see fcntl.h) */
   1.158 +};
   1.159 +
   1.160 +#ifdef _KERNEL
   1.161 +extern struct filelist filehead; /* (fl) head of list of open files */
   1.162 +extern struct fileops vnops;
   1.163 +extern struct fileops badfileops;
   1.164 +extern struct fileops socketops;
   1.165 +extern int maxfiles;		/* kernel limit on number of open files */
   1.166 +extern int maxfilesperproc;	/* per process limit on number of open files */
   1.167 +extern int openfiles;		/* (fl) actual number of open files */
   1.168 +extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
   1.169 +
   1.170 +/*
   1.171 + * The socket operations are used a couple of places.
   1.172 + * XXX: This is wrong, they should go through the operations vector for
   1.173 + * XXX: sockets instead of going directly for the individual functions. /phk
   1.174 + */
   1.175 +fo_rdwr_t	soo_read;
   1.176 +fo_rdwr_t	soo_write;
   1.177 +fo_ioctl_t	soo_ioctl;
   1.178 +fo_poll_t	soo_poll;
   1.179 +fo_kqfilter_t	soo_kqfilter;
   1.180 +fo_stat_t	soo_stat;
   1.181 +fo_close_t	soo_close;
   1.182 +
   1.183 +/* Lock a file. */
   1.184 +#define	FILE_LOCK(f)	mtx_lock((f)->f_mtxp)
   1.185 +#define	FILE_UNLOCK(f)	mtx_unlock((f)->f_mtxp)
   1.186 +#define	FILE_LOCKED(f)	mtx_owned((f)->f_mtxp)
   1.187 +#define	FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
   1.188 +
   1.189 +#define	fhold_locked(fp)						\
   1.190 +	do {								\
   1.191 +		FILE_LOCK_ASSERT(fp, MA_OWNED);				\
   1.192 +		(fp)->f_count++;					\
   1.193 +	} while (0)
   1.194 +
   1.195 +#define	fhold(fp)							\
   1.196 +	do {								\
   1.197 +		FILE_LOCK(fp);						\
   1.198 +		(fp)->f_count++;					\
   1.199 +		FILE_UNLOCK(fp);					\
   1.200 +	} while (0)
   1.201 +
   1.202 +static __inline fo_rdwr_t	fo_read;
   1.203 +static __inline fo_rdwr_t	fo_write;
   1.204 +static __inline fo_ioctl_t	fo_ioctl;
   1.205 +static __inline fo_poll_t	fo_poll;
   1.206 +static __inline fo_kqfilter_t	fo_kqfilter;
   1.207 +static __inline fo_stat_t	fo_stat;
   1.208 +static __inline fo_close_t	fo_close;
   1.209 +
   1.210 +static __inline int
   1.211 +fo_read(fp, uio, active_cred, flags, td)
   1.212 +	struct file *fp;
   1.213 +	struct uio *uio;
   1.214 +	struct ucred *active_cred;
   1.215 +	int flags;
   1.216 +	struct thread *td;
   1.217 +{
   1.218 +
   1.219 +	return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
   1.220 +}
   1.221 +
   1.222 +static __inline int
   1.223 +fo_write(fp, uio, active_cred, flags, td)
   1.224 +	struct file *fp;
   1.225 +	struct uio *uio;
   1.226 +	struct ucred *active_cred;
   1.227 +	int flags;
   1.228 +	struct thread *td;
   1.229 +{
   1.230 +
   1.231 +	return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
   1.232 +}
   1.233 +
   1.234 +static __inline int
   1.235 +fo_ioctl(fp, com, data, active_cred, td)
   1.236 +	struct file *fp;
   1.237 +	u_long com;
   1.238 +	void *data;
   1.239 +	struct ucred *active_cred;
   1.240 +	struct thread *td;
   1.241 +{
   1.242 +
   1.243 +	return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
   1.244 +}
   1.245 +
   1.246 +static __inline int
   1.247 +fo_poll(fp, events, active_cred, td)
   1.248 +	struct file *fp;
   1.249 +	int events;
   1.250 +	struct ucred *active_cred;
   1.251 +	struct thread *td;
   1.252 +{
   1.253 +
   1.254 +	return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
   1.255 +}
   1.256 +
   1.257 +static __inline int
   1.258 +fo_stat(fp, sb, active_cred, td)
   1.259 +	struct file *fp;
   1.260 +	struct stat *sb;
   1.261 +	struct ucred *active_cred;
   1.262 +	struct thread *td;
   1.263 +{
   1.264 +
   1.265 +	return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
   1.266 +}
   1.267 +
   1.268 +static __inline int
   1.269 +fo_close(fp, td)
   1.270 +	struct file *fp;
   1.271 +	struct thread *td;
   1.272 +{
   1.273 +
   1.274 +	return ((*fp->f_ops->fo_close)(fp, td));
   1.275 +}
   1.276 +
   1.277 +static __inline int
   1.278 +fo_kqfilter(fp, kn)
   1.279 +	struct file *fp;
   1.280 +	struct knote *kn;
   1.281 +{
   1.282 +
   1.283 +	return ((*fp->f_ops->fo_kqfilter)(fp, kn));
   1.284 +}
   1.285 +
   1.286 +#endif /* _KERNEL */
   1.287 +
   1.288 +#endif /* !SYS_FILE_H */