os/ossrv/genericopenlibs/openenvcore/include/sys/file.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*-
sl@0
     2
 * Copyright (c) 1982, 1986, 1989, 1993
sl@0
     3
 *	The Regents of the University of California.  All rights reserved.
sl@0
     4
 *
sl@0
     5
 * Redistribution and use in source and binary forms, with or without
sl@0
     6
 * modification, are permitted provided that the following conditions
sl@0
     7
 * are met:
sl@0
     8
 * 1. Redistributions of source code must retain the above copyright
sl@0
     9
 *    notice, this list of conditions and the following disclaimer.
sl@0
    10
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    11
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    12
 *    documentation and/or other materials provided with the distribution.
sl@0
    13
 * 4. Neither the name of the University nor the names of its contributors
sl@0
    14
 *    may be used to endorse or promote products derived from this software
sl@0
    15
 *    without specific prior written permission.
sl@0
    16
 *
sl@0
    17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
sl@0
    18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
sl@0
    21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    27
 * SUCH DAMAGE.
sl@0
    28
 * ©  Portions Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
    29
 *	@(#)file.h	8.3 (Berkeley) 1/9/95
sl@0
    30
 * $FreeBSD: src/sys/sys/file.h,v 1.70 2005/02/10 12:27:58 phk Exp $
sl@0
    31
 */
sl@0
    32
sl@0
    33
#ifndef _SYS_FILE_H_
sl@0
    34
#define	_SYS_FILE_H_
sl@0
    35
sl@0
    36
#ifndef _KERNEL
sl@0
    37
#include <sys/types.h> /* XXX */
sl@0
    38
#include <sys/fcntl.h>
sl@0
    39
#include <sys/unistd.h>
sl@0
    40
#else
sl@0
    41
#include <sys/queue.h>
sl@0
    42
#include <sys/_lock.h>
sl@0
    43
#include <sys/_mutex.h>
sl@0
    44
sl@0
    45
struct stat;
sl@0
    46
struct thread;
sl@0
    47
struct uio;
sl@0
    48
struct knote;
sl@0
    49
struct vnode;
sl@0
    50
struct socket;
sl@0
    51
sl@0
    52
sl@0
    53
#endif /* _KERNEL */
sl@0
    54
sl@0
    55
#define	DTYPE_VNODE	1	/* file */
sl@0
    56
#define	DTYPE_SOCKET	2	/* communications endpoint */
sl@0
    57
#define	DTYPE_PIPE	3	/* pipe */
sl@0
    58
#define	DTYPE_FIFO	4	/* fifo (named pipe) */
sl@0
    59
#define	DTYPE_KQUEUE	5	/* event queue */
sl@0
    60
#define	DTYPE_CRYPTO	6	/* crypto */
sl@0
    61
sl@0
    62
#ifdef _KERNEL
sl@0
    63
sl@0
    64
struct file;
sl@0
    65
struct ucred;
sl@0
    66
sl@0
    67
typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
sl@0
    68
		    struct ucred *active_cred, int flags,
sl@0
    69
		    struct thread *td);
sl@0
    70
#define	FOF_OFFSET	1	/* Use the offset in uio argument */
sl@0
    71
typedef	int fo_ioctl_t(struct file *fp, u_long com, void *data,
sl@0
    72
		    struct ucred *active_cred, struct thread *td);
sl@0
    73
typedef	int fo_poll_t(struct file *fp, int events,
sl@0
    74
		    struct ucred *active_cred, struct thread *td);
sl@0
    75
typedef	int fo_kqfilter_t(struct file *fp, struct knote *kn);
sl@0
    76
typedef	int fo_stat_t(struct file *fp, struct stat *sb,
sl@0
    77
		    struct ucred *active_cred, struct thread *td);
sl@0
    78
typedef	int fo_close_t(struct file *fp, struct thread *td);
sl@0
    79
typedef	int fo_flags_t;
sl@0
    80
sl@0
    81
struct fileops {
sl@0
    82
	fo_rdwr_t	*fo_read;
sl@0
    83
	fo_rdwr_t	*fo_write;
sl@0
    84
	fo_ioctl_t	*fo_ioctl;
sl@0
    85
	fo_poll_t	*fo_poll;
sl@0
    86
	fo_kqfilter_t	*fo_kqfilter;
sl@0
    87
	fo_stat_t	*fo_stat;
sl@0
    88
	fo_close_t	*fo_close;
sl@0
    89
	fo_flags_t	fo_flags;	/* DFLAG_* below */
sl@0
    90
};
sl@0
    91
sl@0
    92
#define DFLAG_PASSABLE	0x01	/* may be passed via unix sockets. */
sl@0
    93
#define DFLAG_SEEKABLE	0x02	/* seekable / nonsequential */
sl@0
    94
sl@0
    95
/*
sl@0
    96
 * Kernel descriptor table.
sl@0
    97
 * One entry for each open kernel vnode and socket.
sl@0
    98
 *
sl@0
    99
 * Below is the list of locks that protects members in struct file.
sl@0
   100
 *
sl@0
   101
 * (fl)	filelist_lock
sl@0
   102
 * (f)	f_mtx in struct file
sl@0
   103
 * none	not locked
sl@0
   104
 */
sl@0
   105
sl@0
   106
struct file {
sl@0
   107
	LIST_ENTRY(file) f_list;/* (fl) list of active files */
sl@0
   108
	short	f_type;		/* descriptor type */
sl@0
   109
	void	*f_data;	/* file descriptor specific data */
sl@0
   110
	u_int	f_flag;		/* see fcntl.h */
sl@0
   111
	struct mtx	*f_mtxp;	/* mutex to protect data */
sl@0
   112
	struct fileops *f_ops;	/* File operations */
sl@0
   113
	struct	ucred *f_cred;	/* credentials associated with descriptor */
sl@0
   114
	int	f_count;	/* (f) reference count */
sl@0
   115
	struct vnode *f_vnode;	/* NULL or applicable vnode */
sl@0
   116
sl@0
   117
	/* DFLAG_SEEKABLE specific fields */
sl@0
   118
	off_t	f_offset;
sl@0
   119
sl@0
   120
	/* DTYPE_SOCKET specific fields */
sl@0
   121
	short	f_gcflag;	/* used by thread doing fd garbage collection */
sl@0
   122
#define	FMARK		0x1	/* mark during gc() */
sl@0
   123
#define	FDEFER		0x2	/* defer for next gc pass */
sl@0
   124
	int	f_msgcount;	/* (f) references from message queue */
sl@0
   125
sl@0
   126
	/* DTYPE_VNODE specific fields */
sl@0
   127
	int	f_seqcount;	/*
sl@0
   128
				 * count of sequential accesses -- cleared
sl@0
   129
				 * by most seek operations.
sl@0
   130
				 */
sl@0
   131
	off_t	f_nextoff;	/*
sl@0
   132
				 * offset of next expected read or write
sl@0
   133
				 */
sl@0
   134
	void	*f_label;	/* Place-holder for struct label pointer. */
sl@0
   135
};
sl@0
   136
sl@0
   137
#endif /* _KERNEL */
sl@0
   138
sl@0
   139
/*
sl@0
   140
 * Userland version of struct file, for sysctl
sl@0
   141
 */
sl@0
   142
struct xfile {
sl@0
   143
	size_t	xf_size;	/* size of struct xfile */
sl@0
   144
	pid_t	xf_pid;		/* owning process */
sl@0
   145
	uid_t	xf_uid;		/* effective uid of owning process */
sl@0
   146
	int	xf_fd;		/* descriptor number */
sl@0
   147
	void	*xf_file;	/* address of struct file */
sl@0
   148
	short	xf_type;	/* descriptor type */
sl@0
   149
	int	xf_count;	/* reference count */
sl@0
   150
	int	xf_msgcount;	/* references from message queue */
sl@0
   151
	off_t	xf_offset;	/* file offset */
sl@0
   152
	void	*xf_data;	/* file descriptor specific data */
sl@0
   153
	void	*xf_vnode;	/* vnode pointer */
sl@0
   154
	u_int	xf_flag;	/* flags (see fcntl.h) */
sl@0
   155
};
sl@0
   156
sl@0
   157
#ifdef _KERNEL
sl@0
   158
extern struct filelist filehead; /* (fl) head of list of open files */
sl@0
   159
extern struct fileops vnops;
sl@0
   160
extern struct fileops badfileops;
sl@0
   161
extern struct fileops socketops;
sl@0
   162
extern int maxfiles;		/* kernel limit on number of open files */
sl@0
   163
extern int maxfilesperproc;	/* per process limit on number of open files */
sl@0
   164
extern int openfiles;		/* (fl) actual number of open files */
sl@0
   165
extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
sl@0
   166
sl@0
   167
/*
sl@0
   168
 * The socket operations are used a couple of places.
sl@0
   169
 * XXX: This is wrong, they should go through the operations vector for
sl@0
   170
 * XXX: sockets instead of going directly for the individual functions. /phk
sl@0
   171
 */
sl@0
   172
fo_rdwr_t	soo_read;
sl@0
   173
fo_rdwr_t	soo_write;
sl@0
   174
fo_ioctl_t	soo_ioctl;
sl@0
   175
fo_poll_t	soo_poll;
sl@0
   176
fo_kqfilter_t	soo_kqfilter;
sl@0
   177
fo_stat_t	soo_stat;
sl@0
   178
fo_close_t	soo_close;
sl@0
   179
sl@0
   180
/* Lock a file. */
sl@0
   181
#define	FILE_LOCK(f)	mtx_lock((f)->f_mtxp)
sl@0
   182
#define	FILE_UNLOCK(f)	mtx_unlock((f)->f_mtxp)
sl@0
   183
#define	FILE_LOCKED(f)	mtx_owned((f)->f_mtxp)
sl@0
   184
#define	FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
sl@0
   185
sl@0
   186
#define	fhold_locked(fp)						\
sl@0
   187
	do {								\
sl@0
   188
		FILE_LOCK_ASSERT(fp, MA_OWNED);				\
sl@0
   189
		(fp)->f_count++;					\
sl@0
   190
	} while (0)
sl@0
   191
sl@0
   192
#define	fhold(fp)							\
sl@0
   193
	do {								\
sl@0
   194
		FILE_LOCK(fp);						\
sl@0
   195
		(fp)->f_count++;					\
sl@0
   196
		FILE_UNLOCK(fp);					\
sl@0
   197
	} while (0)
sl@0
   198
sl@0
   199
static __inline fo_rdwr_t	fo_read;
sl@0
   200
static __inline fo_rdwr_t	fo_write;
sl@0
   201
static __inline fo_ioctl_t	fo_ioctl;
sl@0
   202
static __inline fo_poll_t	fo_poll;
sl@0
   203
static __inline fo_kqfilter_t	fo_kqfilter;
sl@0
   204
static __inline fo_stat_t	fo_stat;
sl@0
   205
static __inline fo_close_t	fo_close;
sl@0
   206
sl@0
   207
static __inline int
sl@0
   208
fo_read(fp, uio, active_cred, flags, td)
sl@0
   209
	struct file *fp;
sl@0
   210
	struct uio *uio;
sl@0
   211
	struct ucred *active_cred;
sl@0
   212
	int flags;
sl@0
   213
	struct thread *td;
sl@0
   214
{
sl@0
   215
sl@0
   216
	return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
sl@0
   217
}
sl@0
   218
sl@0
   219
static __inline int
sl@0
   220
fo_write(fp, uio, active_cred, flags, td)
sl@0
   221
	struct file *fp;
sl@0
   222
	struct uio *uio;
sl@0
   223
	struct ucred *active_cred;
sl@0
   224
	int flags;
sl@0
   225
	struct thread *td;
sl@0
   226
{
sl@0
   227
sl@0
   228
	return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
sl@0
   229
}
sl@0
   230
sl@0
   231
static __inline int
sl@0
   232
fo_ioctl(fp, com, data, active_cred, td)
sl@0
   233
	struct file *fp;
sl@0
   234
	u_long com;
sl@0
   235
	void *data;
sl@0
   236
	struct ucred *active_cred;
sl@0
   237
	struct thread *td;
sl@0
   238
{
sl@0
   239
sl@0
   240
	return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
sl@0
   241
}
sl@0
   242
sl@0
   243
static __inline int
sl@0
   244
fo_poll(fp, events, active_cred, td)
sl@0
   245
	struct file *fp;
sl@0
   246
	int events;
sl@0
   247
	struct ucred *active_cred;
sl@0
   248
	struct thread *td;
sl@0
   249
{
sl@0
   250
sl@0
   251
	return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
sl@0
   252
}
sl@0
   253
sl@0
   254
static __inline int
sl@0
   255
fo_stat(fp, sb, active_cred, td)
sl@0
   256
	struct file *fp;
sl@0
   257
	struct stat *sb;
sl@0
   258
	struct ucred *active_cred;
sl@0
   259
	struct thread *td;
sl@0
   260
{
sl@0
   261
sl@0
   262
	return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
sl@0
   263
}
sl@0
   264
sl@0
   265
static __inline int
sl@0
   266
fo_close(fp, td)
sl@0
   267
	struct file *fp;
sl@0
   268
	struct thread *td;
sl@0
   269
{
sl@0
   270
sl@0
   271
	return ((*fp->f_ops->fo_close)(fp, td));
sl@0
   272
}
sl@0
   273
sl@0
   274
static __inline int
sl@0
   275
fo_kqfilter(fp, kn)
sl@0
   276
	struct file *fp;
sl@0
   277
	struct knote *kn;
sl@0
   278
{
sl@0
   279
sl@0
   280
	return ((*fp->f_ops->fo_kqfilter)(fp, kn));
sl@0
   281
}
sl@0
   282
sl@0
   283
#endif /* _KERNEL */
sl@0
   284
sl@0
   285
#endif /* !SYS_FILE_H */