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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 66659ff was 66659ff, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 15:20:21

Update to FreeBSD 9.2

  • Property mode set to 100644
File size: 14.2 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#define DTYPE_DEV       11      /* Device specific fd type */
67#define DTYPE_CAPABILITY        12      /* capability */
68#define DTYPE_PROCDESC  13      /* process descriptor */
69
70#ifdef _KERNEL
71
72struct file;
73struct ucred;
74
75#define FOF_OFFSET      0x01    /* Use the offset in uio argument */
76#define FOF_NOLOCK      0x02    /* Do not take FOFFSET_LOCK */
77#define FOF_NEXTOFF     0x04    /* Also update f_nextoff */
78#define FOF_NOUPDATE    0x10    /* Do not update f_offset */
79off_t foffset_lock(struct file *fp, int flags);
80void foffset_lock_uio(struct file *fp, struct uio *uio, int flags);
81void foffset_unlock(struct file *fp, off_t val, int flags);
82void foffset_unlock_uio(struct file *fp, struct uio *uio, int flags);
83
84static inline off_t
85foffset_get(struct file *fp)
86{
87
88        return (foffset_lock(fp, FOF_NOLOCK));
89}
90
91typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
92                    struct ucred *active_cred, int flags,
93                    struct thread *td);
94typedef int fo_truncate_t(struct file *fp, off_t length,
95                    struct ucred *active_cred, struct thread *td);
96typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
97                    struct ucred *active_cred, struct thread *td);
98typedef int fo_poll_t(struct file *fp, int events,
99                    struct ucred *active_cred, struct thread *td);
100typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
101typedef int fo_stat_t(struct file *fp, struct stat *sb,
102                    struct ucred *active_cred, struct thread *td);
103typedef int fo_close_t(struct file *fp, struct thread *td);
104typedef int fo_chmod_t(struct file *fp, mode_t mode,
105                    struct ucred *active_cred, struct thread *td);
106typedef int fo_chown_t(struct file *fp, uid_t uid, gid_t gid,
107                    struct ucred *active_cred, struct thread *td);
108typedef int fo_flags_t;
109
110struct fileops {
111        fo_rdwr_t       *fo_read;
112        fo_rdwr_t       *fo_write;
113        fo_truncate_t   *fo_truncate;
114        fo_ioctl_t      *fo_ioctl;
115        fo_poll_t       *fo_poll;
116        fo_kqfilter_t   *fo_kqfilter;
117        fo_stat_t       *fo_stat;
118        fo_close_t      *fo_close;
119        fo_chmod_t      *fo_chmod;
120        fo_chown_t      *fo_chown;
121        fo_flags_t      fo_flags;       /* DFLAG_* below */
122};
123
124#define DFLAG_PASSABLE  0x01    /* may be passed via unix sockets. */
125#define DFLAG_SEEKABLE  0x02    /* seekable / nonsequential */
126#endif /* _KERNEL */
127
128#if defined(_KERNEL) || defined(_WANT_FILE)
129#ifdef __rtems__
130#include <rtems/libio_.h>
131#include <sys/fcntl.h>
132#endif /* __rtems__ */
133/*
134 * Kernel descriptor table.
135 * One entry for each open kernel vnode and socket.
136 *
137 * Below is the list of locks that protects members in struct file.
138 *
139 * (f) protected with mtx_lock(mtx_pool_find(fp))
140 * (d) cdevpriv_mtx
141 * none not locked
142 */
143
144struct fadvise_info {
145        int             fa_advice;      /* (f) FADV_* type. */
146        off_t           fa_start;       /* (f) Region start. */
147        off_t           fa_end;         /* (f) Region end. */
148        off_t           fa_prevstart;   /* (f) Previous NOREUSE start. */
149        off_t           fa_prevend;     /* (f) Previous NOREUSE end. */
150};
151
152struct file {
153#ifndef __rtems__
154        void            *f_data;        /* file descriptor specific data */
155        struct fileops  *f_ops;         /* File operations */
156        struct ucred    *f_cred;        /* associated credentials. */
157        struct vnode    *f_vnode;       /* NULL or applicable vnode */
158        short           f_type;         /* descriptor type */
159        short           f_vnread_flags; /* (f) Sleep lock for f_offset */
160        volatile u_int  f_flag;         /* see fcntl.h */
161        volatile u_int  f_count;        /* reference count */
162        /*
163         *  DTYPE_VNODE specific fields.
164         */
165        int             f_seqcount;     /* Count of sequential accesses. */
166        off_t           f_nextoff;      /* next expected read/write offset. */
167        union {
168                struct cdev_privdata *fvn_cdevpriv;
169                                        /* (d) Private data for the cdev. */
170                struct fadvise_info *fvn_advice;
171        } f_vnun;
172        /*
173         *  DFLAG_SEEKABLE specific fields
174         */
175        off_t           f_offset;
176        /*
177         * Mandatory Access control information.
178         */
179        void            *f_label;       /* Place-holder for MAC label. */
180#else /* __rtems__ */
181        rtems_libio_t   f_io;
182#endif /* __rtems__ */
183};
184#ifdef __rtems__
185#define f_data f_io.pathinfo.node_access
186
187static inline struct file *
188rtems_bsd_iop_to_fp(rtems_libio_t *iop)
189{
190        return (struct file *) iop;
191}
192
193static inline struct file *
194rtems_bsd_fd_to_fp(int fd)
195{
196        return rtems_bsd_iop_to_fp(&rtems_libio_iops[fd]);
197}
198
199static inline int
200rtems_bsd_fp_to_fd(struct file *fp)
201{
202        return fp - rtems_bsd_iop_to_fp(&rtems_libio_iops[0]);
203}
204
205static inline void *
206rtems_bsd_loc_to_f_data(const rtems_filesystem_location_info_t *loc)
207{
208        return loc->node_access;
209}
210
211static inline uint32_t
212rtems_bsd_fflag_to_libio_flags(u_int fflag)
213{
214        uint32_t libio_flags = 0;
215
216        if ((fflag & FREAD) == FREAD) {
217                libio_flags |= LIBIO_FLAGS_READ;
218        }
219
220        if ((fflag & FWRITE) == FWRITE) {
221                libio_flags |= LIBIO_FLAGS_WRITE;
222        }
223
224        if ((fflag & FNONBLOCK) == FNONBLOCK) {
225                libio_flags |= LIBIO_FLAGS_NO_DELAY;
226        }
227
228        return (libio_flags);
229}
230
231static inline u_int
232rtems_bsd_libio_flags_to_fflag(uint32_t libio_flags)
233{
234        u_int fflag = 0;
235
236        if ((libio_flags & LIBIO_FLAGS_READ) == LIBIO_FLAGS_READ) {
237                fflag |= FREAD;
238        }
239
240        if ((libio_flags & LIBIO_FLAGS_WRITE) == LIBIO_FLAGS_WRITE) {
241                fflag |= FWRITE;
242        }
243
244        if ((libio_flags & LIBIO_FLAGS_NO_DELAY) == LIBIO_FLAGS_NO_DELAY) {
245                fflag |= FNONBLOCK;
246        }
247
248        return (fflag);
249}
250
251static int inline
252rtems_bsd_error_to_status_and_errno(int error)
253{
254        if (error == 0) {
255                return 0;
256        } else {
257                rtems_set_errno_and_return_minus_one(error);
258        }
259}
260#endif /* __rtems__ */
261
262#define f_cdevpriv      f_vnun.fvn_cdevpriv
263#define f_advice        f_vnun.fvn_advice
264
265#define FOFFSET_LOCKED       0x1
266#define FOFFSET_LOCK_WAITING 0x2
267#define FDEVFS_VNODE         0x4
268
269#endif /* _KERNEL || _WANT_FILE */
270
271/*
272 * Userland version of struct file, for sysctl
273 */
274struct xfile {
275        size_t  xf_size;        /* size of struct xfile */
276        pid_t   xf_pid;         /* owning process */
277        uid_t   xf_uid;         /* effective uid of owning process */
278        int     xf_fd;          /* descriptor number */
279        void    *xf_file;       /* address of struct file */
280        short   xf_type;        /* descriptor type */
281        int     xf_count;       /* reference count */
282        int     xf_msgcount;    /* references from message queue */
283        off_t   xf_offset;      /* file offset */
284        void    *xf_data;       /* file descriptor specific data */
285        void    *xf_vnode;      /* vnode pointer */
286        u_int   xf_flag;        /* flags (see fcntl.h) */
287};
288
289#ifdef _KERNEL
290
291#ifdef MALLOC_DECLARE
292MALLOC_DECLARE(M_FILE);
293#endif
294
295extern struct fileops vnops;
296extern struct fileops badfileops;
297#ifndef __rtems__
298extern struct fileops socketops;
299#else /* __rtems__ */
300extern const rtems_filesystem_file_handlers_r socketops;
301#endif /* __rtems__ */
302extern int maxfiles;            /* kernel limit on number of open files */
303extern int maxfilesperproc;     /* per process limit on number of open files */
304extern volatile int openfiles;  /* actual number of open files */
305
306#ifndef __rtems__
307int fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp);
308#else /* __rtems__ */
309struct file *rtems_bsd_get_file(int fd);
310
311static inline int
312fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
313{
314        struct file *fp = rtems_bsd_get_file(fd);
315
316        (void) td;
317        (void) rights;
318
319        *fpp = fp;
320
321        return fp != NULL ? 0 : EBADF;
322}
323#endif /* __rtems__ */
324int fget_mmap(struct thread *td, int fd, cap_rights_t rights,
325    u_char *maxprotp, struct file **fpp);
326int fget_read(struct thread *td, int fd, cap_rights_t rights,
327    struct file **fpp);
328int fget_write(struct thread *td, int fd, cap_rights_t rights,
329    struct file **fpp);
330int fgetcap(struct thread *td, int fd, struct file **fpp);
331int _fdrop(struct file *fp, struct thread *td);
332
333#ifndef __rtems__
334/*
335 * The socket operations are used a couple of places.
336 * XXX: This is wrong, they should go through the operations vector for
337 * XXX: sockets instead of going directly for the individual functions. /phk
338 */
339fo_rdwr_t       soo_read;
340fo_rdwr_t       soo_write;
341fo_truncate_t   soo_truncate;
342fo_ioctl_t      soo_ioctl;
343fo_poll_t       soo_poll;
344fo_kqfilter_t   soo_kqfilter;
345fo_stat_t       soo_stat;
346fo_close_t      soo_close;
347
348fo_chmod_t      invfo_chmod;
349fo_chown_t      invfo_chown;
350#else /* __rtems__ */
351int rtems_bsd_soo_kqfilter(rtems_libio_t *iop, struct knote *kn);
352#endif /* __rtems__ */
353
354#ifndef __rtems__
355void finit(struct file *, u_int, short, void *, struct fileops *);
356#else /* __rtems__ */
357static inline void
358finit(struct file *fp, u_int fflag, short type, void *data,
359    const rtems_filesystem_file_handlers_r *ops)
360{
361        rtems_filesystem_location_info_t *pathinfo = &fp->f_io.pathinfo;
362
363        (void) type;
364
365        fp->f_data = data;
366        fp->f_io.flags |= rtems_bsd_fflag_to_libio_flags(fflag);
367
368        pathinfo->handlers = ops;
369        pathinfo->mt_entry = &rtems_filesystem_null_mt_entry;
370        rtems_filesystem_location_add_to_mt_entry(pathinfo);
371}
372#endif /* __rtems__ */
373int fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp);
374int fgetvp_exec(struct thread *td, int fd, cap_rights_t rights,
375    struct vnode **vpp);
376int fgetvp_rights(struct thread *td, int fd, cap_rights_t need,
377    cap_rights_t *have, struct vnode **vpp);
378int fgetvp_read(struct thread *td, int fd, cap_rights_t rights,
379    struct vnode **vpp);
380int fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
381    struct vnode **vpp);
382
383int fgetsock(struct thread *td, int fd, cap_rights_t rights,
384    struct socket **spp, u_int *fflagp);
385void fputsock(struct socket *sp);
386
387static __inline int
388_fnoop(void)
389{
390
391        return (0);
392}
393
394#define fhold(fp)                                                       \
395        (refcount_acquire(&(fp)->f_count))
396#ifndef __rtems__
397#define fdrop(fp, td)                                                   \
398        (refcount_release(&(fp)->f_count) ? _fdrop((fp), (td)) : _fnoop())
399#else /* __rtems__ */
400#define fdrop(fp, td) do { } while (0)
401#endif /* __rtems__ */
402
403#ifndef __rtems__
404static __inline fo_rdwr_t       fo_read;
405static __inline fo_rdwr_t       fo_write;
406static __inline fo_truncate_t   fo_truncate;
407static __inline fo_ioctl_t      fo_ioctl;
408static __inline fo_poll_t       fo_poll;
409static __inline fo_kqfilter_t   fo_kqfilter;
410static __inline fo_stat_t       fo_stat;
411static __inline fo_close_t      fo_close;
412static __inline fo_chmod_t      fo_chmod;
413static __inline fo_chown_t      fo_chown;
414
415static __inline int
416fo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
417    int flags, struct thread *td)
418{
419
420        return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
421}
422
423static __inline int
424fo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
425    int flags, struct thread *td)
426{
427
428        return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
429}
430
431static __inline int
432fo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
433    struct thread *td)
434{
435
436        return ((*fp->f_ops->fo_truncate)(fp, length, active_cred, td));
437}
438#endif /* __rtems__ */
439
440static __inline int
441fo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
442    struct thread *td)
443{
444
445#ifndef __rtems__
446        return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
447#else /* __rtems__ */
448        int rv;
449
450        (void) active_cred;
451        (void) td;
452
453        errno = 0;
454        rv = ((*fp->f_io.pathinfo.handlers->ioctl_h)(&fp->f_io, com, data));
455        if (rv == 0) {
456                return (0);
457        } else {
458                return (errno);
459        }
460#endif /* __rtems__ */
461}
462
463static __inline int
464fo_poll(struct file *fp, int events, struct ucred *active_cred,
465    struct thread *td)
466{
467
468#ifndef __rtems__
469        return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
470#else /* __rtems__ */
471        (void) active_cred;
472        (void) td;
473
474        return ((*fp->f_io.pathinfo.handlers->poll_h)(&fp->f_io, events));
475#endif /* __rtems__ */
476}
477
478#ifndef __rtems__
479static __inline int
480fo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
481    struct thread *td)
482{
483
484        return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
485}
486
487static __inline int
488fo_close(struct file *fp, struct thread *td)
489{
490
491        return ((*fp->f_ops->fo_close)(fp, td));
492}
493#endif /* __rtems__ */
494
495static __inline int
496fo_kqfilter(struct file *fp, struct knote *kn)
497{
498
499#ifndef __rtems__
500        return ((*fp->f_ops->fo_kqfilter)(fp, kn));
501#else /* __rtems__ */
502        return ((*fp->f_io.pathinfo.handlers->kqfilter_h)(&fp->f_io, kn));
503#endif /* __rtems__ */
504}
505
506#ifndef __rtems__
507static __inline int
508fo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
509    struct thread *td)
510{
511
512        return ((*fp->f_ops->fo_chmod)(fp, mode, active_cred, td));
513}
514
515static __inline int
516fo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
517    struct thread *td)
518{
519
520        return ((*fp->f_ops->fo_chown)(fp, uid, gid, active_cred, td));
521}
522#endif /* __rtems__ */
523
524#endif /* _KERNEL */
525
526#endif /* !SYS_FILE_H */
Note: See TracBrowser for help on using the repository browser.