source: rtems-libbsd/freebsd/sys/sys/file.h @ af5333e

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since af5333e was af5333e, checked in by Sebastian Huber <sebastian.huber@…>, on 11/04/13 at 10:33:00

Update to FreeBSD 8.4

  • Property mode set to 100644
File size: 12.3 KB
Line 
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      @(#)file.h      8.3 (Berkeley) 1/9/95
30 * $FreeBSD$
31 */
32
33#ifndef _SYS_FILE_H_
34#define _SYS_FILE_H_
35
36#ifndef _KERNEL
37#include <rtems/bsd/sys/types.h> /* XXX */
38#include <sys/fcntl.h>
39#include <rtems/bsd/sys/unistd.h>
40#else
41#include <sys/queue.h>
42#include <sys/refcount.h>
43#include <sys/_lock.h>
44#include <sys/_mutex.h>
45
46struct stat;
47struct thread;
48struct uio;
49struct knote;
50struct vnode;
51struct socket;
52
53
54#endif /* _KERNEL */
55
56#define DTYPE_VNODE     1       /* file */
57#define DTYPE_SOCKET    2       /* communications endpoint */
58#define DTYPE_PIPE      3       /* pipe */
59#define DTYPE_FIFO      4       /* fifo (named pipe) */
60#define DTYPE_KQUEUE    5       /* event queue */
61#define DTYPE_CRYPTO    6       /* crypto */
62#define DTYPE_MQUEUE    7       /* posix message queue */
63#define DTYPE_SHM       8       /* swap-backed shared memory */
64#define DTYPE_SEM       9       /* posix semaphore */
65#define DTYPE_PTS       10      /* pseudo teletype master device */
66
67#ifdef _KERNEL
68
69struct file;
70struct ucred;
71
72typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
73                    struct ucred *active_cred, int flags,
74                    struct thread *td);
75#define FOF_OFFSET      1       /* Use the offset in uio argument */
76typedef int fo_truncate_t(struct file *fp, off_t length,
77                    struct ucred *active_cred, struct thread *td);
78typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
79                    struct ucred *active_cred, struct thread *td);
80typedef int fo_poll_t(struct file *fp, int events,
81                    struct ucred *active_cred, struct thread *td);
82typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
83typedef int fo_stat_t(struct file *fp, struct stat *sb,
84                    struct ucred *active_cred, struct thread *td);
85typedef int fo_close_t(struct file *fp, struct thread *td);
86typedef int fo_flags_t;
87
88struct fileops {
89        fo_rdwr_t       *fo_read;
90        fo_rdwr_t       *fo_write;
91        fo_truncate_t   *fo_truncate;
92        fo_ioctl_t      *fo_ioctl;
93        fo_poll_t       *fo_poll;
94        fo_kqfilter_t   *fo_kqfilter;
95        fo_stat_t       *fo_stat;
96        fo_close_t      *fo_close;
97        fo_flags_t      fo_flags;       /* DFLAG_* below */
98};
99
100#define DFLAG_PASSABLE  0x01    /* may be passed via unix sockets. */
101#define DFLAG_SEEKABLE  0x02    /* seekable / nonsequential */
102#endif /* _KERNEL */
103
104#if defined(_KERNEL) || defined(_WANT_FILE)
105#ifdef __rtems__
106#include <rtems/libio_.h>
107#include <sys/fcntl.h>
108#endif /* __rtems__ */
109/*
110 * Kernel descriptor table.
111 * One entry for each open kernel vnode and socket.
112 *
113 * Below is the list of locks that protects members in struct file.
114 *
115 * (f) protected with mtx_lock(mtx_pool_find(fp))
116 * (d) cdevpriv_mtx
117 * none not locked
118 */
119
120struct fadvise_info {
121        int             fa_advice;      /* (f) FADV_* type. */
122        off_t           fa_start;       /* (f) Region start. */
123        off_t           fa_end;         /* (f) Region end. */
124        off_t           fa_prevstart;   /* (f) Previous NOREUSE start. */
125        off_t           fa_prevend;     /* (f) Previous NOREUSE end. */
126};
127
128struct file {
129#ifndef __rtems__
130        void            *f_data;        /* file descriptor specific data */
131        struct fileops  *f_ops;         /* File operations */
132        struct ucred    *f_cred;        /* associated credentials. */
133        struct vnode    *f_vnode;       /* NULL or applicable vnode */
134        short           f_type;         /* descriptor type */
135        short           f_vnread_flags; /* (f) Sleep lock for f_offset */
136        volatile u_int  f_flag;         /* see fcntl.h */
137        volatile u_int  f_count;        /* reference count */
138        /*
139         *  DTYPE_VNODE specific fields.
140         */
141        int             f_seqcount;     /* Count of sequential accesses. */
142        off_t           f_nextoff;      /* next expected read/write offset. */
143        union {
144                struct cdev_privdata *fvn_cdevpriv;
145                                        /* (d) Private data for the cdev. */
146                struct fadvise_info *fvn_advice;
147        } f_vnun;
148        /*
149         *  DFLAG_SEEKABLE specific fields
150         */
151        off_t           f_offset;
152        /*
153         * Mandatory Access control information.
154         */
155        void            *f_label;       /* Place-holder for MAC label. */
156#else /* __rtems__ */
157        rtems_libio_t   f_io;
158#endif /* __rtems__ */
159};
160#ifdef __rtems__
161#define f_data f_io.pathinfo.node_access
162
163static inline struct file *
164rtems_bsd_iop_to_fp(rtems_libio_t *iop)
165{
166        return (struct file *) iop;
167}
168
169static inline struct file *
170rtems_bsd_fd_to_fp(int fd)
171{
172        return rtems_bsd_iop_to_fp(&rtems_libio_iops[fd]);
173}
174
175static inline int
176rtems_bsd_fp_to_fd(struct file *fp)
177{
178        return fp - rtems_bsd_iop_to_fp(&rtems_libio_iops[0]);
179}
180
181static inline void *
182rtems_bsd_loc_to_f_data(const rtems_filesystem_location_info_t *loc)
183{
184        return loc->node_access;
185}
186
187static inline uint32_t
188rtems_bsd_fflag_to_libio_flags(u_int fflag)
189{
190        uint32_t libio_flags = 0;
191
192        if ((fflag & FREAD) == FREAD) {
193                libio_flags |= LIBIO_FLAGS_READ;
194        }
195
196        if ((fflag & FWRITE) == FWRITE) {
197                libio_flags |= LIBIO_FLAGS_WRITE;
198        }
199
200        if ((fflag & FNONBLOCK) == FNONBLOCK) {
201                libio_flags |= LIBIO_FLAGS_NO_DELAY;
202        }
203
204        return (libio_flags);
205}
206
207static inline u_int
208rtems_bsd_libio_flags_to_fflag(uint32_t libio_flags)
209{
210        u_int fflag = 0;
211
212        if ((libio_flags & LIBIO_FLAGS_READ) == LIBIO_FLAGS_READ) {
213                fflag |= FREAD;
214        }
215
216        if ((libio_flags & LIBIO_FLAGS_WRITE) == LIBIO_FLAGS_WRITE) {
217                fflag |= FWRITE;
218        }
219
220        if ((libio_flags & LIBIO_FLAGS_NO_DELAY) == LIBIO_FLAGS_NO_DELAY) {
221                fflag |= FNONBLOCK;
222        }
223
224        return (fflag);
225}
226
227static int inline
228rtems_bsd_error_to_status_and_errno(int error)
229{
230        if (error == 0) {
231                return 0;
232        } else {
233                rtems_set_errno_and_return_minus_one(error);
234        }
235}
236#endif /* __rtems__ */
237
238#define f_cdevpriv      f_vnun.fvn_cdevpriv
239#define f_advice        f_vnun.fvn_advice
240
241#define FOFFSET_LOCKED       0x1
242#define FOFFSET_LOCK_WAITING 0x2                 
243
244#endif /* _KERNEL || _WANT_FILE */
245
246/*
247 * Userland version of struct file, for sysctl
248 */
249struct xfile {
250        size_t  xf_size;        /* size of struct xfile */
251        pid_t   xf_pid;         /* owning process */
252        uid_t   xf_uid;         /* effective uid of owning process */
253        int     xf_fd;          /* descriptor number */
254        void    *xf_file;       /* address of struct file */
255        short   xf_type;        /* descriptor type */
256        int     xf_count;       /* reference count */
257        int     xf_msgcount;    /* references from message queue */
258        off_t   xf_offset;      /* file offset */
259        void    *xf_data;       /* file descriptor specific data */
260        void    *xf_vnode;      /* vnode pointer */
261        u_int   xf_flag;        /* flags (see fcntl.h) */
262};
263
264#ifdef _KERNEL
265
266#ifdef MALLOC_DECLARE
267MALLOC_DECLARE(M_FILE);
268#endif
269
270extern struct fileops vnops;
271extern struct fileops badfileops;
272#ifndef __rtems__
273extern struct fileops socketops;
274#else /* __rtems__ */
275extern const rtems_filesystem_file_handlers_r socketops;
276#endif /* __rtems__ */
277extern int maxfiles;            /* kernel limit on number of open files */
278extern int maxfilesperproc;     /* per process limit on number of open files */
279extern volatile int openfiles;  /* actual number of open files */
280
281#ifndef __rtems__
282int fget(struct thread *td, int fd, struct file **fpp);
283#else /* __rtems__ */
284struct file *rtems_bsd_get_file(int fd);
285
286static inline int
287fget(struct thread *td, int fd, struct file **fpp)
288{
289        struct file *fp = rtems_bsd_get_file(fd);
290
291        (void) td;
292
293        *fpp = fp;
294
295        return fp != NULL ? 0 : EBADF;
296}
297#endif /* __rtems__ */
298int fget_read(struct thread *td, int fd, struct file **fpp);
299int fget_write(struct thread *td, int fd, struct file **fpp);
300int _fdrop(struct file *fp, struct thread *td);
301
302#ifndef __rtems__
303/*
304 * The socket operations are used a couple of places.
305 * XXX: This is wrong, they should go through the operations vector for
306 * XXX: sockets instead of going directly for the individual functions. /phk
307 */
308fo_rdwr_t       soo_read;
309fo_rdwr_t       soo_write;
310fo_truncate_t   soo_truncate;
311fo_ioctl_t      soo_ioctl;
312fo_poll_t       soo_poll;
313fo_kqfilter_t   soo_kqfilter;
314fo_stat_t       soo_stat;
315fo_close_t      soo_close;
316#else /* __rtems__ */
317int rtems_bsd_soo_kqfilter(rtems_libio_t *iop, struct knote *kn);
318#endif /* __rtems__ */
319
320#ifndef __rtems__
321void finit(struct file *, u_int, short, void *, struct fileops *);
322#else /* __rtems__ */
323static inline void
324finit(struct file *fp, u_int fflag, short type, void *data,
325    const rtems_filesystem_file_handlers_r *ops)
326{
327        rtems_filesystem_location_info_t *pathinfo = &fp->f_io.pathinfo;
328
329        (void) type;
330
331        fp->f_data = data;
332        fp->f_io.flags |= rtems_bsd_fflag_to_libio_flags(fflag);
333
334        pathinfo->handlers = ops;
335        pathinfo->mt_entry = &rtems_filesystem_null_mt_entry;
336        rtems_filesystem_location_add_to_mt_entry(pathinfo);
337}
338#endif /* __rtems__ */
339int fgetvp(struct thread *td, int fd, struct vnode **vpp);
340int fgetvp_read(struct thread *td, int fd, struct vnode **vpp);
341int fgetvp_write(struct thread *td, int fd, struct vnode **vpp);
342
343int fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp);
344void fputsock(struct socket *sp);
345
346#define fhold(fp)                                                       \
347        (refcount_acquire(&(fp)->f_count))
348#ifndef __rtems__
349#define fdrop(fp, td)                                                   \
350        (refcount_release(&(fp)->f_count) ? _fdrop((fp), (td)) : 0)
351#else /* __rtems__ */
352#define fdrop(fp, td) do { } while (0)
353#endif /* __rtems__ */
354
355#ifndef __rtems__
356static __inline fo_rdwr_t       fo_read;
357static __inline fo_rdwr_t       fo_write;
358static __inline fo_truncate_t   fo_truncate;
359static __inline fo_ioctl_t      fo_ioctl;
360static __inline fo_poll_t       fo_poll;
361static __inline fo_kqfilter_t   fo_kqfilter;
362static __inline fo_stat_t       fo_stat;
363static __inline fo_close_t      fo_close;
364
365static __inline int
366fo_read(fp, uio, active_cred, flags, td)
367        struct file *fp;
368        struct uio *uio;
369        struct ucred *active_cred;
370        int flags;
371        struct thread *td;
372{
373
374        return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
375}
376
377static __inline int
378fo_write(fp, uio, active_cred, flags, td)
379        struct file *fp;
380        struct uio *uio;
381        struct ucred *active_cred;
382        int flags;
383        struct thread *td;
384{
385
386        return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
387}
388
389static __inline int
390fo_truncate(fp, length, active_cred, td)
391        struct file *fp;
392        off_t length;
393        struct ucred *active_cred;
394        struct thread *td;
395{
396
397        return ((*fp->f_ops->fo_truncate)(fp, length, active_cred, td));
398}
399#endif /* __rtems__ */
400
401static __inline int
402fo_ioctl(fp, com, data, active_cred, td)
403        struct file *fp;
404        u_long com;
405        void *data;
406        struct ucred *active_cred;
407        struct thread *td;
408{
409
410#ifndef __rtems__
411        return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
412#else /* __rtems__ */
413        int rv;
414
415        (void) active_cred;
416        (void) td;
417
418        errno = 0;
419        rv = ((*fp->f_io.pathinfo.handlers->ioctl_h)(&fp->f_io, com, data));
420        if (rv == 0) {
421                return (0);
422        } else {
423                return (errno);
424        }
425#endif /* __rtems__ */
426}
427
428static __inline int
429fo_poll(fp, events, active_cred, td)
430        struct file *fp;
431        int events;
432        struct ucred *active_cred;
433        struct thread *td;
434{
435
436#ifndef __rtems__
437        return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
438#else /* __rtems__ */
439        (void) active_cred;
440        (void) td;
441
442        return ((*fp->f_io.pathinfo.handlers->poll_h)(&fp->f_io, events));
443#endif /* __rtems__ */
444}
445
446#ifndef __rtems__
447static __inline int
448fo_stat(fp, sb, active_cred, td)
449        struct file *fp;
450        struct stat *sb;
451        struct ucred *active_cred;
452        struct thread *td;
453{
454
455        return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
456}
457
458static __inline int
459fo_close(fp, td)
460        struct file *fp;
461        struct thread *td;
462{
463
464        return ((*fp->f_ops->fo_close)(fp, td));
465}
466#endif /* __rtems__ */
467
468static __inline int
469fo_kqfilter(fp, kn)
470        struct file *fp;
471        struct knote *kn;
472{
473
474#ifndef __rtems__
475        return ((*fp->f_ops->fo_kqfilter)(fp, kn));
476#else /* __rtems__ */
477        return ((*fp->f_io.pathinfo.handlers->kqfilter_h)(&fp->f_io, kn));
478#endif /* __rtems__ */
479}
480
481#endif /* _KERNEL */
482
483#endif /* !SYS_FILE_H */
Note: See TracBrowser for help on using the repository browser.