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

55-freebsd-126-freebsd-12
Last change on this file since 567cce1 was de8a76d, checked in by Sebastian Huber <sebastian.huber@…>, on 04/04/17 at 07:36:57

Update to FreeBSD head 2017-04-04

Git mirror commit 642b174daddbd0efd9bb5f242c43f4ab4db6869f.

  • Property mode set to 100644
File size: 15.9 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 * 3. 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 <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#include <vm/vm.h>
46
47struct filedesc;
48struct stat;
49struct thread;
50struct uio;
51struct knote;
52struct vnode;
53
54#endif /* _KERNEL */
55
56#define DTYPE_NONE      0       /* not yet initialized */
57#define DTYPE_VNODE     1       /* file */
58#define DTYPE_SOCKET    2       /* communications endpoint */
59#define DTYPE_PIPE      3       /* pipe */
60#define DTYPE_FIFO      4       /* fifo (named pipe) */
61#define DTYPE_KQUEUE    5       /* event queue */
62#define DTYPE_CRYPTO    6       /* crypto */
63#define DTYPE_MQUEUE    7       /* posix message queue */
64#define DTYPE_SHM       8       /* swap-backed shared memory */
65#define DTYPE_SEM       9       /* posix semaphore */
66#define DTYPE_PTS       10      /* pseudo teletype master device */
67#define DTYPE_DEV       11      /* Device specific fd type */
68#define DTYPE_PROCDESC  12      /* process descriptor */
69#define DTYPE_LINUXEFD  13      /* emulation eventfd type */
70#define DTYPE_LINUXTFD  14      /* emulation timerfd type */
71
72#ifdef _KERNEL
73
74struct file;
75struct filecaps;
76struct kaiocb;
77struct kinfo_file;
78struct ucred;
79
80#define FOF_OFFSET      0x01    /* Use the offset in uio argument */
81#define FOF_NOLOCK      0x02    /* Do not take FOFFSET_LOCK */
82#define FOF_NEXTOFF     0x04    /* Also update f_nextoff */
83#define FOF_NOUPDATE    0x10    /* Do not update f_offset */
84off_t foffset_lock(struct file *fp, int flags);
85void foffset_lock_uio(struct file *fp, struct uio *uio, int flags);
86void foffset_unlock(struct file *fp, off_t val, int flags);
87void foffset_unlock_uio(struct file *fp, struct uio *uio, int flags);
88
89static inline off_t
90foffset_get(struct file *fp)
91{
92
93        return (foffset_lock(fp, FOF_NOLOCK));
94}
95
96typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
97                    struct ucred *active_cred, int flags,
98                    struct thread *td);
99typedef int fo_truncate_t(struct file *fp, off_t length,
100                    struct ucred *active_cred, struct thread *td);
101typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
102                    struct ucred *active_cred, struct thread *td);
103typedef int fo_poll_t(struct file *fp, int events,
104                    struct ucred *active_cred, struct thread *td);
105typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
106typedef int fo_stat_t(struct file *fp, struct stat *sb,
107                    struct ucred *active_cred, struct thread *td);
108typedef int fo_close_t(struct file *fp, struct thread *td);
109typedef int fo_chmod_t(struct file *fp, mode_t mode,
110                    struct ucred *active_cred, struct thread *td);
111typedef int fo_chown_t(struct file *fp, uid_t uid, gid_t gid,
112                    struct ucred *active_cred, struct thread *td);
113typedef int fo_sendfile_t(struct file *fp, int sockfd, struct uio *hdr_uio,
114                    struct uio *trl_uio, off_t offset, size_t nbytes,
115                    off_t *sent, int flags, struct thread *td);
116typedef int fo_seek_t(struct file *fp, off_t offset, int whence,
117                    struct thread *td);
118typedef int fo_fill_kinfo_t(struct file *fp, struct kinfo_file *kif,
119                    struct filedesc *fdp);
120typedef int fo_mmap_t(struct file *fp, vm_map_t map, vm_offset_t *addr,
121                    vm_size_t size, vm_prot_t prot, vm_prot_t cap_maxprot,
122                    int flags, vm_ooffset_t foff, struct thread *td);
123typedef int fo_aio_queue_t(struct file *fp, struct kaiocb *job);
124typedef int fo_flags_t;
125
126struct fileops {
127        fo_rdwr_t       *fo_read;
128        fo_rdwr_t       *fo_write;
129        fo_truncate_t   *fo_truncate;
130        fo_ioctl_t      *fo_ioctl;
131        fo_poll_t       *fo_poll;
132        fo_kqfilter_t   *fo_kqfilter;
133        fo_stat_t       *fo_stat;
134        fo_close_t      *fo_close;
135        fo_chmod_t      *fo_chmod;
136        fo_chown_t      *fo_chown;
137        fo_sendfile_t   *fo_sendfile;
138        fo_seek_t       *fo_seek;
139        fo_fill_kinfo_t *fo_fill_kinfo;
140        fo_mmap_t       *fo_mmap;
141        fo_aio_queue_t  *fo_aio_queue;
142        fo_flags_t      fo_flags;       /* DFLAG_* below */
143};
144
145#define DFLAG_PASSABLE  0x01    /* may be passed via unix sockets. */
146#define DFLAG_SEEKABLE  0x02    /* seekable / nonsequential */
147#endif /* _KERNEL */
148
149#if defined(_KERNEL) || defined(_WANT_FILE)
150#ifdef __rtems__
151#include <rtems/libio_.h>
152#include <sys/fcntl.h>
153#endif /* __rtems__ */
154/*
155 * Kernel descriptor table.
156 * One entry for each open kernel vnode and socket.
157 *
158 * Below is the list of locks that protects members in struct file.
159 *
160 * (a) f_vnode lock required (shared allows both reads and writes)
161 * (f) protected with mtx_lock(mtx_pool_find(fp))
162 * (d) cdevpriv_mtx
163 * none not locked
164 */
165
166struct fadvise_info {
167        int             fa_advice;      /* (f) FADV_* type. */
168        off_t           fa_start;       /* (f) Region start. */
169        off_t           fa_end;         /* (f) Region end. */
170};
171
172struct file {
173#ifndef __rtems__
174        void            *f_data;        /* file descriptor specific data */
175        struct fileops  *f_ops;         /* File operations */
176        struct ucred    *f_cred;        /* associated credentials. */
177        struct vnode    *f_vnode;       /* NULL or applicable vnode */
178        short           f_type;         /* descriptor type */
179        short           f_vnread_flags; /* (f) Sleep lock for f_offset */
180        volatile u_int  f_flag;         /* see fcntl.h */
181        volatile u_int  f_count;        /* reference count */
182        /*
183         *  DTYPE_VNODE specific fields.
184         */
185        int             f_seqcount;     /* (a) Count of sequential accesses. */
186        off_t           f_nextoff;      /* next expected read/write offset. */
187        union {
188                struct cdev_privdata *fvn_cdevpriv;
189                                        /* (d) Private data for the cdev. */
190                struct fadvise_info *fvn_advice;
191        } f_vnun;
192        /*
193         *  DFLAG_SEEKABLE specific fields
194         */
195        off_t           f_offset;
196        /*
197         * Mandatory Access control information.
198         */
199        void            *f_label;       /* Place-holder for MAC label. */
200#else /* __rtems__ */
201        rtems_libio_t   f_io;
202#endif /* __rtems__ */
203};
204#ifdef __rtems__
205#define f_data f_io.pathinfo.node_access_2
206
207static inline struct file *
208rtems_bsd_iop_to_fp(rtems_libio_t *iop)
209{
210        return (struct file *) iop;
211}
212
213static inline struct file *
214rtems_bsd_fd_to_fp(int fd)
215{
216        return rtems_bsd_iop_to_fp(&rtems_libio_iops[fd]);
217}
218
219static inline int
220rtems_bsd_fp_to_fd(struct file *fp)
221{
222        return fp - rtems_bsd_iop_to_fp(&rtems_libio_iops[0]);
223}
224
225static inline void *
226rtems_bsd_loc_to_f_data(const rtems_filesystem_location_info_t *loc)
227{
228        return loc->node_access_2;
229}
230
231static inline uint32_t
232rtems_bsd_fflag_to_libio_flags(u_int fflag)
233{
234        uint32_t libio_flags = 0;
235
236        if ((fflag & FREAD) == FREAD) {
237                libio_flags |= LIBIO_FLAGS_READ;
238        }
239
240        if ((fflag & FWRITE) == FWRITE) {
241                libio_flags |= LIBIO_FLAGS_WRITE;
242        }
243
244        if ((fflag & FNONBLOCK) == FNONBLOCK) {
245                libio_flags |= LIBIO_FLAGS_NO_DELAY;
246        }
247
248        return (libio_flags);
249}
250
251static inline u_int
252rtems_bsd_libio_flags_to_fflag(uint32_t libio_flags)
253{
254        u_int fflag = 0;
255
256        if ((libio_flags & LIBIO_FLAGS_READ) == LIBIO_FLAGS_READ) {
257                fflag |= FREAD;
258        }
259
260        if ((libio_flags & LIBIO_FLAGS_WRITE) == LIBIO_FLAGS_WRITE) {
261                fflag |= FWRITE;
262        }
263
264        if ((libio_flags & LIBIO_FLAGS_NO_DELAY) == LIBIO_FLAGS_NO_DELAY) {
265                fflag |= FNONBLOCK;
266        }
267
268        return (fflag);
269}
270
271static int inline
272rtems_bsd_error_to_status_and_errno(int error)
273{
274        if (error == 0) {
275                return 0;
276        } else {
277                rtems_set_errno_and_return_minus_one(error);
278        }
279}
280#endif /* __rtems__ */
281
282#define f_cdevpriv      f_vnun.fvn_cdevpriv
283#define f_advice        f_vnun.fvn_advice
284
285#define FOFFSET_LOCKED       0x1
286#define FOFFSET_LOCK_WAITING 0x2
287#define FDEVFS_VNODE         0x4
288
289#endif /* _KERNEL || _WANT_FILE */
290
291/*
292 * Userland version of struct file, for sysctl
293 */
294struct xfile {
295        size_t  xf_size;        /* size of struct xfile */
296        pid_t   xf_pid;         /* owning process */
297        uid_t   xf_uid;         /* effective uid of owning process */
298        int     xf_fd;          /* descriptor number */
299        void    *xf_file;       /* address of struct file */
300        short   xf_type;        /* descriptor type */
301        int     xf_count;       /* reference count */
302        int     xf_msgcount;    /* references from message queue */
303        off_t   xf_offset;      /* file offset */
304        void    *xf_data;       /* file descriptor specific data */
305        void    *xf_vnode;      /* vnode pointer */
306        u_int   xf_flag;        /* flags (see fcntl.h) */
307};
308
309#ifdef _KERNEL
310
311extern struct fileops vnops;
312extern struct fileops badfileops;
313#ifndef __rtems__
314extern struct fileops socketops;
315#else /* __rtems__ */
316extern const rtems_filesystem_file_handlers_r socketops;
317#endif /* __rtems__ */
318extern int maxfiles;            /* kernel limit on number of open files */
319extern int maxfilesperproc;     /* per process limit on number of open files */
320extern volatile int openfiles;  /* actual number of open files */
321
322#ifndef __rtems__
323int fget(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp);
324#else /* __rtems__ */
325struct file *rtems_bsd_get_file(int fd);
326
327static inline int
328rtems_bsd_do_fget(int fd, struct file **fpp)
329{
330        struct file *fp = rtems_bsd_get_file(fd);
331
332        *fpp = fp;
333
334        return fp != NULL ? 0 : EBADF;
335}
336
337#define fget(td, fd, rights, fpp)       rtems_bsd_do_fget(fd, fpp)
338#endif /* __rtems__ */
339int fget_mmap(struct thread *td, int fd, cap_rights_t *rightsp,
340    u_char *maxprotp, struct file **fpp);
341int fget_read(struct thread *td, int fd, cap_rights_t *rightsp,
342    struct file **fpp);
343int fget_write(struct thread *td, int fd, cap_rights_t *rightsp,
344    struct file **fpp);
345int fget_fcntl(struct thread *td, int fd, cap_rights_t *rightsp,
346    int needfcntl, struct file **fpp);
347int _fdrop(struct file *fp, struct thread *td);
348
349#ifndef __rtems__
350fo_rdwr_t       invfo_rdwr;
351fo_truncate_t   invfo_truncate;
352fo_ioctl_t      invfo_ioctl;
353fo_poll_t       invfo_poll;
354fo_kqfilter_t   invfo_kqfilter;
355fo_chmod_t      invfo_chmod;
356fo_chown_t      invfo_chown;
357fo_sendfile_t   invfo_sendfile;
358
359fo_sendfile_t   vn_sendfile;
360fo_seek_t       vn_seek;
361fo_fill_kinfo_t vn_fill_kinfo;
362int vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif);
363#else /* __rtems__ */
364int rtems_bsd_soo_kqfilter(rtems_libio_t *iop, struct knote *kn);
365#endif /* __rtems__ */
366
367#ifndef __rtems__
368void finit(struct file *, u_int, short, void *, struct fileops *);
369#else /* __rtems__ */
370static inline void
371finit(struct file *fp, u_int fflag, short type, void *data,
372    const rtems_filesystem_file_handlers_r *ops)
373{
374        rtems_filesystem_location_info_t *pathinfo = &fp->f_io.pathinfo;
375
376        (void) type;
377
378        fp->f_data = data;
379        fp->f_io.flags |= rtems_bsd_fflag_to_libio_flags(fflag);
380
381        pathinfo->handlers = ops;
382}
383#endif /* __rtems__ */
384int fgetvp(struct thread *td, int fd, cap_rights_t *rightsp,
385    struct vnode **vpp);
386int fgetvp_exec(struct thread *td, int fd, cap_rights_t *rightsp,
387    struct vnode **vpp);
388int fgetvp_rights(struct thread *td, int fd, cap_rights_t *needrightsp,
389    struct filecaps *havecaps, struct vnode **vpp);
390int fgetvp_read(struct thread *td, int fd, cap_rights_t *rightsp,
391    struct vnode **vpp);
392int fgetvp_write(struct thread *td, int fd, cap_rights_t *rightsp,
393    struct vnode **vpp);
394
395static __inline int
396_fnoop(void)
397{
398
399        return (0);
400}
401
402#define fhold(fp)                                                       \
403        (refcount_acquire(&(fp)->f_count))
404#ifndef __rtems__
405#define fdrop(fp, td)                                                   \
406        (refcount_release(&(fp)->f_count) ? _fdrop((fp), (td)) : _fnoop())
407#else /* __rtems__ */
408#define fdrop(fp, td) do { } while (0)
409#endif /* __rtems__ */
410
411#ifndef __rtems__
412static __inline fo_rdwr_t       fo_read;
413static __inline fo_rdwr_t       fo_write;
414static __inline fo_truncate_t   fo_truncate;
415static __inline fo_ioctl_t      fo_ioctl;
416static __inline fo_poll_t       fo_poll;
417static __inline fo_kqfilter_t   fo_kqfilter;
418static __inline fo_stat_t       fo_stat;
419static __inline fo_close_t      fo_close;
420static __inline fo_chmod_t      fo_chmod;
421static __inline fo_chown_t      fo_chown;
422static __inline fo_sendfile_t   fo_sendfile;
423
424static __inline int
425fo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
426    int flags, struct thread *td)
427{
428
429        return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
430}
431
432static __inline int
433fo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
434    int flags, struct thread *td)
435{
436
437        return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
438}
439
440static __inline int
441fo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
442    struct thread *td)
443{
444
445        return ((*fp->f_ops->fo_truncate)(fp, length, active_cred, td));
446}
447#endif /* __rtems__ */
448
449static __inline int
450fo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
451    struct thread *td)
452{
453
454#ifndef __rtems__
455        return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
456#else /* __rtems__ */
457        int rv;
458
459        (void) active_cred;
460        (void) td;
461
462        errno = 0;
463        rv = ((*fp->f_io.pathinfo.handlers->ioctl_h)(&fp->f_io, com, data));
464        if (rv == 0) {
465                return (0);
466        } else {
467                return (errno);
468        }
469#endif /* __rtems__ */
470}
471
472static __inline int
473fo_poll(struct file *fp, int events, struct ucred *active_cred,
474    struct thread *td)
475{
476
477#ifndef __rtems__
478        return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
479#else /* __rtems__ */
480        (void) active_cred;
481        (void) td;
482
483        return ((*fp->f_io.pathinfo.handlers->poll_h)(&fp->f_io, events));
484#endif /* __rtems__ */
485}
486
487#ifndef __rtems__
488static __inline int
489fo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
490    struct thread *td)
491{
492
493        return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
494}
495
496static __inline int
497fo_close(struct file *fp, struct thread *td)
498{
499
500        return ((*fp->f_ops->fo_close)(fp, td));
501}
502#endif /* __rtems__ */
503
504static __inline int
505fo_kqfilter(struct file *fp, struct knote *kn)
506{
507
508#ifndef __rtems__
509        return ((*fp->f_ops->fo_kqfilter)(fp, kn));
510#else /* __rtems__ */
511        return ((*fp->f_io.pathinfo.handlers->kqfilter_h)(&fp->f_io, kn));
512#endif /* __rtems__ */
513}
514
515#ifndef __rtems__
516static __inline int
517fo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
518    struct thread *td)
519{
520
521        return ((*fp->f_ops->fo_chmod)(fp, mode, active_cred, td));
522}
523
524static __inline int
525fo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
526    struct thread *td)
527{
528
529        return ((*fp->f_ops->fo_chown)(fp, uid, gid, active_cred, td));
530}
531
532static __inline int
533fo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
534    struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
535    struct thread *td)
536{
537
538        return ((*fp->f_ops->fo_sendfile)(fp, sockfd, hdr_uio, trl_uio, offset,
539            nbytes, sent, flags, td));
540}
541
542static __inline int
543fo_seek(struct file *fp, off_t offset, int whence, struct thread *td)
544{
545
546        return ((*fp->f_ops->fo_seek)(fp, offset, whence, td));
547}
548
549static __inline int
550fo_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
551{
552
553        return ((*fp->f_ops->fo_fill_kinfo)(fp, kif, fdp));
554}
555
556static __inline int
557fo_mmap(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
558    vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
559    struct thread *td)
560{
561
562        if (fp->f_ops->fo_mmap == NULL)
563                return (ENODEV);
564        return ((*fp->f_ops->fo_mmap)(fp, map, addr, size, prot, cap_maxprot,
565            flags, foff, td));
566}
567
568static __inline int
569fo_aio_queue(struct file *fp, struct kaiocb *job)
570{
571
572        return ((*fp->f_ops->fo_aio_queue)(fp, job));
573}
574#endif /* __rtems__ */
575
576#endif /* _KERNEL */
577
578#endif /* !SYS_FILE_H */
Note: See TracBrowser for help on using the repository browser.