xref: /illumos-gate/usr/src/uts/common/sys/file.h (revision a5eb7107)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
30 
31 #ifndef _SYS_FILE_H
32 #define	_SYS_FILE_H
33 
34 #include <sys/t_lock.h>
35 #ifdef _KERNEL
36 #include <sys/model.h>
37 #include <sys/user.h>
38 #endif
39 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 /*
45  * fio locking:
46  *   f_rwlock	protects f_vnode and f_cred
47  *   f_tlock	protects the rest
48  *
49  *   The purpose of locking in this layer is to keep the kernel
50  *   from panicing if, for example, a thread calls close() while
51  *   another thread is doing a read().  It is up to higher levels
52  *   to make sure 2 threads doing I/O to the same file don't
53  *   screw each other up.
54  */
55 /*
56  * One file structure is allocated for each open/creat/pipe call.
57  * Main use is to hold the read/write pointer associated with
58  * each open file.
59  */
60 typedef struct file {
61 	kmutex_t	f_tlock;	/* short term lock */
62 	ushort_t	f_flag;
63 	ushort_t	f_flag2;	/* extra flags (FSEARCH, FEXEC) */
64 	struct vnode	*f_vnode;	/* pointer to vnode structure */
65 	offset_t	f_offset;	/* read/write character pointer */
66 	struct cred	*f_cred;	/* credentials of user who opened it */
67 	struct f_audit_data	*f_audit_data;	/* file audit data */
68 	int		f_count;	/* reference count */
69 } file_t;
70 
71 /*
72  * fpollinfo struct - used by poll caching to track who has polled the fd
73  */
74 typedef struct fpollinfo {
75 	struct _kthread		*fp_thread;	/* thread caching poll info */
76 	struct fpollinfo	*fp_next;
77 } fpollinfo_t;
78 
79 /* f_flag */
80 
81 #define	FOPEN		0xffffffff
82 #define	FREAD		0x01	/* <sys/aiocb.h> LIO_READ must be identical */
83 #define	FWRITE		0x02	/* <sys/aiocb.h> LIO_WRITE must be identical */
84 #define	FNDELAY		0x04
85 #define	FAPPEND		0x08
86 #define	FSYNC		0x10	/* file (data+inode) integrity while writing */
87 #define	FREVOKED	0x20	/* Object reuse Revoked file */
88 #define	FDSYNC		0x40	/* file data only integrity while writing */
89 #define	FNONBLOCK	0x80
90 
91 #define	FMASK		0xa0ff	/* all flags that can be changed by F_SETFL */
92 
93 /* open-only modes */
94 
95 #define	FCREAT		0x0100
96 #define	FTRUNC		0x0200
97 #define	FEXCL		0x0400
98 #define	FASYNC		0x1000	/* asyncio in progress pseudo flag */
99 #define	FOFFMAX		0x2000	/* large file */
100 #define	FXATTR		0x4000	/* open as extended attribute */
101 #define	FNOCTTY		0x0800
102 #define	FRSYNC		0x8000	/* sync read operations at same level of */
103 				/* integrity as specified for writes by */
104 				/* FSYNC and FDSYNC flags */
105 
106 #define	FNODSYNC	0x10000 /* fsync pseudo flag */
107 
108 #define	FNOFOLLOW	0x20000	/* don't follow symlinks */
109 #define	FNOLINKS	0x40000	/* don't allow multiple hard links */
110 #define	FIGNORECASE	0x80000 /* request case-insensitive lookups */
111 #define	FXATTRDIROPEN	0x100000  /* only opening hidden attribute directory */
112 
113 /* f_flag2 (open-only) */
114 
115 #define	FSEARCH		0x200000	/* O_SEARCH = 0x200000 */
116 #define	FEXEC		0x400000	/* O_EXEC = 0x400000 */
117 
118 #define	FCLOEXEC	0x800000	/* O_CLOEXEC = 0x800000 */
119 
120 #ifdef _KERNEL
121 
122 /*
123  * This is a flag that is set on f_flag2, but is never user-visible
124  */
125 #define	FEPOLLED	0x8000
126 
127 /*
128  * Fake flags for driver ioctl calls to inform them of the originating
129  * process' model.  See <sys/model.h>
130  *
131  * Part of the Solaris 2.6+ DDI/DKI
132  */
133 #define	FMODELS	DATAMODEL_MASK	/* Note: 0x0ff00000 */
134 #define	FILP32	DATAMODEL_ILP32
135 #define	FLP64	DATAMODEL_LP64
136 #define	FNATIVE	DATAMODEL_NATIVE
137 
138 /*
139  * Large Files: The macro gets the offset maximum (refer to LFS API doc)
140  * corresponding to a file descriptor. We had the choice of storing
141  * this value in file descriptor. Right now we only have two
142  * offset maximums one if MAXOFF_T and other is MAXOFFSET_T. It is
143  * inefficient to store these two values in a separate member in
144  * file descriptor. To avoid wasting spaces we define this macro.
145  * The day there are more than two offset maximum we may want to
146  * rewrite this macro.
147  */
148 
149 #define	OFFSET_MAX(fd)	((fd->f_flag & FOFFMAX) ? MAXOFFSET_T : MAXOFF32_T)
150 
151 /*
152  * Fake flag => internal ioctl call for layered drivers.
153  * Note that this flag deliberately *won't* fit into
154  * the f_flag field of a file_t.
155  *
156  * Part of the Solaris 2.x DDI/DKI.
157  */
158 #define	FKIOCTL		0x80000000	/* ioctl addresses are from kernel */
159 
160 /*
161  * Fake flag => this time to specify that the open(9E)
162  * comes from another part of the kernel, not userland.
163  *
164  * Part of the Solaris 2.x DDI/DKI.
165  */
166 #define	FKLYR		0x40000000	/* layered driver call */
167 
168 #endif	/* _KERNEL */
169 
170 /* miscellaneous defines */
171 
172 #ifndef L_SET
173 #define	L_SET	0	/* for lseek */
174 #endif /* L_SET */
175 
176 #if defined(_KERNEL)
177 
178 /*
179  * Routines dealing with user per-open file flags and
180  * user open files.
181  */
182 struct proc;	/* forward reference for function prototype */
183 struct vnodeops;
184 struct vattr;
185 
186 extern file_t *getf(int);
187 extern void releasef(int);
188 extern void areleasef(int, uf_info_t *);
189 #ifndef	_BOOT
190 extern void closeall(uf_info_t *);
191 #endif
192 extern void flist_fork(uf_info_t *, uf_info_t *);
193 extern int closef(file_t *);
194 extern int closeandsetf(int, file_t *);
195 extern int ufalloc_file(int, file_t *);
196 extern int ufalloc(int);
197 extern int ufcanalloc(struct proc *, uint_t);
198 extern int falloc(struct vnode *, int, file_t **, int *);
199 extern void finit(void);
200 extern void unfalloc(file_t *);
201 extern void setf(int, file_t *);
202 extern int f_getfd_error(int, int *);
203 extern char f_getfd(int);
204 extern int f_setfd_error(int, int);
205 extern void f_setfd(int, char);
206 extern int f_getfl(int, int *);
207 extern int f_badfd(int, int *, int);
208 extern int fassign(struct vnode **, int, int *);
209 extern void fcnt_add(uf_info_t *, int);
210 extern void close_exec(uf_info_t *);
211 extern void clear_stale_fd(void);
212 extern void clear_active_fd(int);
213 extern void free_afd(afd_t *afd);
214 extern int fgetstartvp(int, char *, struct vnode **);
215 extern int fsetattrat(int, char *, int, struct vattr *);
216 extern int fisopen(struct vnode *);
217 extern void delfpollinfo(int);
218 extern void addfpollinfo(int);
219 extern int sock_getfasync(struct vnode *);
220 extern int files_can_change_zones(void);
221 #ifdef DEBUG
222 /* The following functions are only used in ASSERT()s */
223 extern void checkwfdlist(struct vnode *, fpollinfo_t *);
224 extern void checkfpollinfo(void);
225 extern int infpollinfo(int);
226 #endif	/* DEBUG */
227 
228 #endif	/* defined(_KERNEL) */
229 
230 #ifdef	__cplusplus
231 }
232 #endif
233 
234 #endif	/* _SYS_FILE_H */
235