epoc32/include/stdapis/sys/file.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 
    20 #ifndef _SYS_FILE_H_
    21 #define	_SYS_FILE_H_
    22 
    23 #ifndef _KERNEL
    24 #include <sys/types.h> /* XXX */
    25 #include <sys/fcntl.h>
    26 #include <sys/unistd.h>
    27 #else
    28 #include <sys/queue.h>
    29 #include <sys/_lock.h>
    30 #include <sys/_mutex.h>
    31 
    32 struct stat;
    33 struct thread;
    34 struct uio;
    35 struct knote;
    36 struct vnode;
    37 struct socket;
    38 
    39 
    40 #endif /* _KERNEL */
    41 
    42 #define	DTYPE_VNODE	1	/* file */
    43 #define	DTYPE_SOCKET	2	/* communications endpoint */
    44 #define	DTYPE_PIPE	3	/* pipe */
    45 #define	DTYPE_FIFO	4	/* fifo (named pipe) */
    46 #define	DTYPE_KQUEUE	5	/* event queue */
    47 #define	DTYPE_CRYPTO	6	/* crypto */
    48 
    49 #ifdef _KERNEL
    50 
    51 struct file;
    52 struct ucred;
    53 
    54 typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
    55 		    struct ucred *active_cred, int flags,
    56 		    struct thread *td);
    57 #define	FOF_OFFSET	1	/* Use the offset in uio argument */
    58 typedef	int fo_ioctl_t(struct file *fp, u_long com, void *data,
    59 		    struct ucred *active_cred, struct thread *td);
    60 typedef	int fo_poll_t(struct file *fp, int events,
    61 		    struct ucred *active_cred, struct thread *td);
    62 typedef	int fo_kqfilter_t(struct file *fp, struct knote *kn);
    63 typedef	int fo_stat_t(struct file *fp, struct stat *sb,
    64 		    struct ucred *active_cred, struct thread *td);
    65 typedef	int fo_close_t(struct file *fp, struct thread *td);
    66 typedef	int fo_flags_t;
    67 
    68 struct fileops {
    69 	fo_rdwr_t	*fo_read;
    70 	fo_rdwr_t	*fo_write;
    71 	fo_ioctl_t	*fo_ioctl;
    72 	fo_poll_t	*fo_poll;
    73 	fo_kqfilter_t	*fo_kqfilter;
    74 	fo_stat_t	*fo_stat;
    75 	fo_close_t	*fo_close;
    76 	fo_flags_t	fo_flags;	/* DFLAG_* below */
    77 };
    78 
    79 #define DFLAG_PASSABLE	0x01	/* may be passed via unix sockets. */
    80 #define DFLAG_SEEKABLE	0x02	/* seekable / nonsequential */
    81 
    82 /*
    83  * Kernel descriptor table.
    84  * One entry for each open kernel vnode and socket.
    85  *
    86  * Below is the list of locks that protects members in struct file.
    87  *
    88  * (fl)	filelist_lock
    89  * (f)	f_mtx in struct file
    90  * none	not locked
    91  */
    92 
    93 struct file {
    94 	LIST_ENTRY(file) f_list;/* (fl) list of active files */
    95 	short	f_type;		/* descriptor type */
    96 	void	*f_data;	/* file descriptor specific data */
    97 	u_int	f_flag;		/* see fcntl.h */
    98 	struct mtx	*f_mtxp;	/* mutex to protect data */
    99 	struct fileops *f_ops;	/* File operations */
   100 	struct	ucred *f_cred;	/* credentials associated with descriptor */
   101 	int	f_count;	/* (f) reference count */
   102 	struct vnode *f_vnode;	/* NULL or applicable vnode */
   103 
   104 	/* DFLAG_SEEKABLE specific fields */
   105 	off_t	f_offset;
   106 
   107 	/* DTYPE_SOCKET specific fields */
   108 	short	f_gcflag;	/* used by thread doing fd garbage collection */
   109 #define	FMARK		0x1	/* mark during gc() */
   110 #define	FDEFER		0x2	/* defer for next gc pass */
   111 	int	f_msgcount;	/* (f) references from message queue */
   112 
   113 	/* DTYPE_VNODE specific fields */
   114 	int	f_seqcount;	/*
   115 				 * count of sequential accesses -- cleared
   116 				 * by most seek operations.
   117 				 */
   118 	off_t	f_nextoff;	/*
   119 				 * offset of next expected read or write
   120 				 */
   121 	void	*f_label;	/* Place-holder for struct label pointer. */
   122 };
   123 
   124 #endif /* _KERNEL */
   125 
   126 /*
   127  * Userland version of struct file, for sysctl
   128  */
   129 struct xfile {
   130 	size_t	xf_size;	/* size of struct xfile */
   131 	pid_t	xf_pid;		/* owning process */
   132 	uid_t	xf_uid;		/* effective uid of owning process */
   133 	int	xf_fd;		/* descriptor number */
   134 	void	*xf_file;	/* address of struct file */
   135 	short	xf_type;	/* descriptor type */
   136 	int	xf_count;	/* reference count */
   137 	int	xf_msgcount;	/* references from message queue */
   138 	off_t	xf_offset;	/* file offset */
   139 	void	*xf_data;	/* file descriptor specific data */
   140 	void	*xf_vnode;	/* vnode pointer */
   141 	u_int	xf_flag;	/* flags (see fcntl.h) */
   142 };
   143 
   144 #ifdef _KERNEL
   145 extern struct filelist filehead; /* (fl) head of list of open files */
   146 extern struct fileops vnops;
   147 extern struct fileops badfileops;
   148 extern struct fileops socketops;
   149 extern int maxfiles;		/* kernel limit on number of open files */
   150 extern int maxfilesperproc;	/* per process limit on number of open files */
   151 extern int openfiles;		/* (fl) actual number of open files */
   152 extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
   153 
   154 /*
   155  * The socket operations are used a couple of places.
   156  * XXX: This is wrong, they should go through the operations vector for
   157  * XXX: sockets instead of going directly for the individual functions. /phk
   158  */
   159 fo_rdwr_t	soo_read;
   160 fo_rdwr_t	soo_write;
   161 fo_ioctl_t	soo_ioctl;
   162 fo_poll_t	soo_poll;
   163 fo_kqfilter_t	soo_kqfilter;
   164 fo_stat_t	soo_stat;
   165 fo_close_t	soo_close;
   166 
   167 /* Lock a file. */
   168 #define	FILE_LOCK(f)	mtx_lock((f)->f_mtxp)
   169 #define	FILE_UNLOCK(f)	mtx_unlock((f)->f_mtxp)
   170 #define	FILE_LOCKED(f)	mtx_owned((f)->f_mtxp)
   171 #define	FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
   172 
   173 #define	fhold_locked(fp)						\
   174 	do {								\
   175 		FILE_LOCK_ASSERT(fp, MA_OWNED);				\
   176 		(fp)->f_count++;					\
   177 	} while (0)
   178 
   179 #define	fhold(fp)							\
   180 	do {								\
   181 		FILE_LOCK(fp);						\
   182 		(fp)->f_count++;					\
   183 		FILE_UNLOCK(fp);					\
   184 	} while (0)
   185 
   186 static __inline fo_rdwr_t	fo_read;
   187 static __inline fo_rdwr_t	fo_write;
   188 static __inline fo_ioctl_t	fo_ioctl;
   189 static __inline fo_poll_t	fo_poll;
   190 static __inline fo_kqfilter_t	fo_kqfilter;
   191 static __inline fo_stat_t	fo_stat;
   192 static __inline fo_close_t	fo_close;
   193 
   194 static __inline int
   195 fo_read(fp, uio, active_cred, flags, td)
   196 	struct file *fp;
   197 	struct uio *uio;
   198 	struct ucred *active_cred;
   199 	int flags;
   200 	struct thread *td;
   201 {
   202 
   203 	return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
   204 }
   205 
   206 static __inline int
   207 fo_write(fp, uio, active_cred, flags, td)
   208 	struct file *fp;
   209 	struct uio *uio;
   210 	struct ucred *active_cred;
   211 	int flags;
   212 	struct thread *td;
   213 {
   214 
   215 	return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
   216 }
   217 
   218 static __inline int
   219 fo_ioctl(fp, com, data, active_cred, td)
   220 	struct file *fp;
   221 	u_long com;
   222 	void *data;
   223 	struct ucred *active_cred;
   224 	struct thread *td;
   225 {
   226 
   227 	return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
   228 }
   229 
   230 static __inline int
   231 fo_poll(fp, events, active_cred, td)
   232 	struct file *fp;
   233 	int events;
   234 	struct ucred *active_cred;
   235 	struct thread *td;
   236 {
   237 
   238 	return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
   239 }
   240 
   241 static __inline int
   242 fo_stat(fp, sb, active_cred, td)
   243 	struct file *fp;
   244 	struct stat *sb;
   245 	struct ucred *active_cred;
   246 	struct thread *td;
   247 {
   248 
   249 	return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
   250 }
   251 
   252 static __inline int
   253 fo_close(fp, td)
   254 	struct file *fp;
   255 	struct thread *td;
   256 {
   257 
   258 	return ((*fp->f_ops->fo_close)(fp, td));
   259 }
   260 
   261 static __inline int
   262 fo_kqfilter(fp, kn)
   263 	struct file *fp;
   264 	struct knote *kn;
   265 {
   266 
   267 	return ((*fp->f_ops->fo_kqfilter)(fp, kn));
   268 }
   269 
   270 #endif /* _KERNEL */
   271 
   272 #endif /* !SYS_FILE_H */