xref: /illumos-gate/usr/src/uts/common/sys/file.h (revision edd58064)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a5f69788Scraigm  * Common Development and Distribution License (the "License").
6a5f69788Scraigm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23794f0adbSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
26794f0adbSRoger A. Faulkner /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27b4203d75SMarcel Telka /*	  All Rights Reserved	*/
28794f0adbSRoger A. Faulkner 
29b075ad5bSTheo Schlossnagle /* Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved. */
30*edd58064SJerry Jelinek /* Copyright 2020 Joyent, Inc. */
31b075ad5bSTheo Schlossnagle 
327c478bd9Sstevel@tonic-gate #ifndef _SYS_FILE_H
337c478bd9Sstevel@tonic-gate #define	_SYS_FILE_H
357c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
36086d9687SPatrick Mooney #if defined(_KERNEL) || defined(_FAKE_KERNEL)
377c478bd9Sstevel@tonic-gate #include <sys/model.h>
387c478bd9Sstevel@tonic-gate #include <sys/user.h>
397c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
427c478bd9Sstevel@tonic-gate extern "C" {
437c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * fio locking:
477c478bd9Sstevel@tonic-gate  *   f_rwlock	protects f_vnode and f_cred
487c478bd9Sstevel@tonic-gate  *   f_tlock	protects the rest
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *   The purpose of locking in this layer is to keep the kernel
517c478bd9Sstevel@tonic-gate  *   from panicing if, for example, a thread calls close() while
527c478bd9Sstevel@tonic-gate  *   another thread is doing a read().  It is up to higher levels
537c478bd9Sstevel@tonic-gate  *   to make sure 2 threads doing I/O to the same file don't
547c478bd9Sstevel@tonic-gate  *   screw each other up.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * One file structure is allocated for each open/creat/pipe call.
587a5aac98SJerry Jelinek  * Main use is to hold the read/write pointer (and OFD locks) associated with
597c478bd9Sstevel@tonic-gate  * each open file.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate typedef struct file {
627c478bd9Sstevel@tonic-gate 	kmutex_t	f_tlock;	/* short term lock */
637c478bd9Sstevel@tonic-gate 	ushort_t	f_flag;
64794f0adbSRoger A. Faulkner 	ushort_t	f_flag2;	/* extra flags (FSEARCH, FEXEC) */
657c478bd9Sstevel@tonic-gate 	struct vnode	*f_vnode;	/* pointer to vnode structure */
667c478bd9Sstevel@tonic-gate 	offset_t	f_offset;	/* read/write character pointer */
677c478bd9Sstevel@tonic-gate 	struct cred	*f_cred;	/* credentials of user who opened it */
687c478bd9Sstevel@tonic-gate 	struct f_audit_data	*f_audit_data;	/* file audit data */
697c478bd9Sstevel@tonic-gate 	int		f_count;	/* reference count */
707a5aac98SJerry Jelinek 	struct filock *f_filock;	/* ptr to single lock_descriptor_t */
717c478bd9Sstevel@tonic-gate } file_t;
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * fpollinfo struct - used by poll caching to track who has polled the fd
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate typedef struct fpollinfo {
777c478bd9Sstevel@tonic-gate 	struct _kthread		*fp_thread;	/* thread caching poll info */
787c478bd9Sstevel@tonic-gate 	struct fpollinfo	*fp_next;
797c478bd9Sstevel@tonic-gate } fpollinfo_t;
81794f0adbSRoger A. Faulkner /* f_flag */
837c478bd9Sstevel@tonic-gate #define	FOPEN		0xffffffff
847c478bd9Sstevel@tonic-gate #define	FREAD		0x01	/* <sys/aiocb.h> LIO_READ must be identical */
857c478bd9Sstevel@tonic-gate #define	FWRITE		0x02	/* <sys/aiocb.h> LIO_WRITE must be identical */
867c478bd9Sstevel@tonic-gate #define	FNDELAY		0x04
877c478bd9Sstevel@tonic-gate #define	FAPPEND		0x08
887c478bd9Sstevel@tonic-gate #define	FSYNC		0x10	/* file (data+inode) integrity while writing */
89d3e55dcdSgww #define	FREVOKED	0x20	/* Object reuse Revoked file */
907c478bd9Sstevel@tonic-gate #define	FDSYNC		0x40	/* file data only integrity while writing */
917c478bd9Sstevel@tonic-gate #define	FNONBLOCK	0x80
937c478bd9Sstevel@tonic-gate #define	FMASK		0xa0ff	/* all flags that can be changed by F_SETFL */
957c478bd9Sstevel@tonic-gate /* open-only modes */
977c478bd9Sstevel@tonic-gate #define	FCREAT		0x0100
987c478bd9Sstevel@tonic-gate #define	FTRUNC		0x0200
997c478bd9Sstevel@tonic-gate #define	FEXCL		0x0400
100da6c28aaSamw #define	FASYNC		0x1000	/* asyncio in progress pseudo flag */
101da6c28aaSamw #define	FOFFMAX		0x2000	/* large file */
1027c478bd9Sstevel@tonic-gate #define	FXATTR		0x4000	/* open as extended attribute */
103da6c28aaSamw #define	FNOCTTY		0x0800
104da6c28aaSamw #define	FRSYNC		0x8000	/* sync read operations at same level of */
105da6c28aaSamw 				/* integrity as specified for writes by */
106da6c28aaSamw 				/* FSYNC and FDSYNC flags */
1087c478bd9Sstevel@tonic-gate #define	FNODSYNC	0x10000 /* fsync pseudo flag */
1107c478bd9Sstevel@tonic-gate #define	FNOFOLLOW	0x20000	/* don't follow symlinks */
1117c478bd9Sstevel@tonic-gate #define	FNOLINKS	0x40000	/* don't allow multiple hard links */
112da6c28aaSamw #define	FIGNORECASE	0x80000 /* request case-insensitive lookups */
113da6c28aaSamw #define	FXATTRDIROPEN	0x100000  /* only opening hidden attribute directory */
115794f0adbSRoger A. Faulkner /* f_flag2 (open-only) */
116794f0adbSRoger A. Faulkner 
117794f0adbSRoger A. Faulkner #define	FSEARCH		0x200000	/* O_SEARCH = 0x200000 */
118794f0adbSRoger A. Faulkner #define	FEXEC		0x400000	/* O_EXEC = 0x400000 */
119794f0adbSRoger A. Faulkner 
120b075ad5bSTheo Schlossnagle #define	FCLOEXEC	0x800000	/* O_CLOEXEC = 0x800000 */
12169c811abSRobert Mustacchi #define	FDIRECTORY	0x1000000	/* O_DIRECTORY = 0x1000000 */
122*edd58064SJerry Jelinek #define	FDIRECT		0x2000000	/* O_DIRECT = 0x2000000 */
123b075ad5bSTheo Schlossnagle 
1248329232eSGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Fake flags for driver ioctl calls to inform them of the originating
1287c478bd9Sstevel@tonic-gate  * process' model.  See <sys/model.h>
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * Part of the Solaris 2.6+ DDI/DKI
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate #define	FMODELS	DATAMODEL_MASK	/* Note: 0x0ff00000 */
1337c478bd9Sstevel@tonic-gate #define	FILP32	DATAMODEL_ILP32
1347c478bd9Sstevel@tonic-gate #define	FLP64	DATAMODEL_LP64
1357c478bd9Sstevel@tonic-gate #define	FNATIVE	DATAMODEL_NATIVE
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Large Files: The macro gets the offset maximum (refer to LFS API doc)
1397c478bd9Sstevel@tonic-gate  * corresponding to a file descriptor. We had the choice of storing
1407c478bd9Sstevel@tonic-gate  * this value in file descriptor. Right now we only have two
1417c478bd9Sstevel@tonic-gate  * offset maximums one if MAXOFF_T and other is MAXOFFSET_T. It is
1427c478bd9Sstevel@tonic-gate  * inefficient to store these two values in a separate member in
1437c478bd9Sstevel@tonic-gate  * file descriptor. To avoid wasting spaces we define this macro.
1447c478bd9Sstevel@tonic-gate  * The day there are more than two offset maximum we may want to
1457c478bd9Sstevel@tonic-gate  * rewrite this macro.
1467c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate #define	OFFSET_MAX(fd)	((fd->f_flag & FOFFMAX) ? MAXOFFSET_T : MAXOFF32_T)
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Fake flag => internal ioctl call for layered drivers.
1527c478bd9Sstevel@tonic-gate  * Note that this flag deliberately *won't* fit into
1537c478bd9Sstevel@tonic-gate  * the f_flag field of a file_t.
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * Part of the Solaris 2.x DDI/DKI.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate #define	FKIOCTL		0x80000000	/* ioctl addresses are from kernel */
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Fake flag => this time to specify that the open(9E)
1617c478bd9Sstevel@tonic-gate  * comes from another part of the kernel, not userland.
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * Part of the Solaris 2.x DDI/DKI.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate #define	FKLYR		0x40000000	/* layered driver call */
1677c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1697c478bd9Sstevel@tonic-gate /* miscellaneous defines */
1717c478bd9Sstevel@tonic-gate #ifndef L_SET
1727c478bd9Sstevel@tonic-gate #define	L_SET	0	/* for lseek */
1737c478bd9Sstevel@tonic-gate #endif /* L_SET */
1757a5aac98SJerry Jelinek /*
1767a5aac98SJerry Jelinek  * For flock(3C).  These really don't belong here but for historical reasons
1777a5aac98SJerry Jelinek  * the interface defines them to be here.
1787a5aac98SJerry Jelinek  */
1797a5aac98SJerry Jelinek #define	LOCK_SH	1
1807a5aac98SJerry Jelinek #define	LOCK_EX	2
1817a5aac98SJerry Jelinek #define	LOCK_NB	4
1827a5aac98SJerry Jelinek #define	LOCK_UN	8
1837a5aac98SJerry Jelinek 
1847a5aac98SJerry Jelinek #if !defined(_STRICT_SYMBOLS)
1857a5aac98SJerry Jelinek extern int flock(int, int);
1867a5aac98SJerry Jelinek #endif
1877a5aac98SJerry Jelinek 
1888329232eSGordon Ross #if defined(_KERNEL) || defined(_FAKE_KERNEL)
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Routines dealing with user per-open file flags and
1927c478bd9Sstevel@tonic-gate  * user open files.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate struct proc;	/* forward reference for function prototype */
1957c478bd9Sstevel@tonic-gate struct vnodeops;
196794f0adbSRoger A. Faulkner struct vattr;
1978329232eSGordon Ross struct uf_info;
1997c478bd9Sstevel@tonic-gate extern file_t *getf(int);
200086d9687SPatrick Mooney extern file_t *getf_gen(int, uf_entry_gen_t *);
2017c478bd9Sstevel@tonic-gate extern void releasef(int);
2028329232eSGordon Ross extern void areleasef(int, struct uf_info *);
2037c478bd9Sstevel@tonic-gate #ifndef	_BOOT
2048329232eSGordon Ross extern void closeall(struct uf_info *);
2057c478bd9Sstevel@tonic-gate #endif
2068329232eSGordon Ross extern void flist_fork(struct uf_info *, struct uf_info *);
2077c478bd9Sstevel@tonic-gate extern int closef(file_t *);
2087c478bd9Sstevel@tonic-gate extern int closeandsetf(int, file_t *);
2097c478bd9Sstevel@tonic-gate extern int ufalloc_file(int, file_t *);
2107c478bd9Sstevel@tonic-gate extern int ufalloc(int);
2112cb27123Saguzovsk extern int ufcanalloc(struct proc *, uint_t);
2127c478bd9Sstevel@tonic-gate extern int falloc(struct vnode *, int, file_t **, int *);
2137c478bd9Sstevel@tonic-gate extern void finit(void);
2147c478bd9Sstevel@tonic-gate extern void unfalloc(file_t *);
2157c478bd9Sstevel@tonic-gate extern void setf(int, file_t *);
2167c478bd9Sstevel@tonic-gate extern int f_getfd_error(int, int *);
2177c478bd9Sstevel@tonic-gate extern char f_getfd(int);
2187c478bd9Sstevel@tonic-gate extern int f_setfd_error(int, int);
2197c478bd9Sstevel@tonic-gate extern void f_setfd(int, char);
2207c478bd9Sstevel@tonic-gate extern int f_getfl(int, int *);
221a5f69788Scraigm extern int f_badfd(int, int *, int);
2227c478bd9Sstevel@tonic-gate extern int fassign(struct vnode **, int, int *);
2238329232eSGordon Ross extern void fcnt_add(struct uf_info *, int);
2248329232eSGordon Ross extern void close_exec(struct uf_info *);
2257c478bd9Sstevel@tonic-gate extern void clear_stale_fd(void);
2267c478bd9Sstevel@tonic-gate extern void clear_active_fd(int);
2277c478bd9Sstevel@tonic-gate extern void free_afd(afd_t *afd);
228794f0adbSRoger A. Faulkner extern int fgetstartvp(int, char *, struct vnode **);
229794f0adbSRoger A. Faulkner extern int fsetattrat(int, char *, int, struct vattr *);
2307c478bd9Sstevel@tonic-gate extern int fisopen(struct vnode *);
2317c478bd9Sstevel@tonic-gate extern void delfpollinfo(int);
2327c478bd9Sstevel@tonic-gate extern void addfpollinfo(int);
2337c478bd9Sstevel@tonic-gate extern int sock_getfasync(struct vnode *);
2347c478bd9Sstevel@tonic-gate extern int files_can_change_zones(void);
2357c478bd9Sstevel@tonic-gate #ifdef DEBUG
2367c478bd9Sstevel@tonic-gate /* The following functions are only used in ASSERT()s */
2377c478bd9Sstevel@tonic-gate extern void checkwfdlist(struct vnode *, fpollinfo_t *);
2387c478bd9Sstevel@tonic-gate extern void checkfpollinfo(void);
2397c478bd9Sstevel@tonic-gate extern int infpollinfo(int);
2407c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
2427c478bd9Sstevel@tonic-gate #endif	/* defined(_KERNEL) */
2447c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate #endif
2487c478bd9Sstevel@tonic-gate #endif	/* _SYS_FILE_H */