xref: /illumos-gate/usr/src/uts/common/sys/fs/snode.h (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 /*	  All Rights Reserved	*/
23 
24 
25 /*
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #ifndef	_SYS_FS_SNODE_H
31 #define	_SYS_FS_SNODE_H
32 
33 #include <sys/types.h>
34 #include <sys/t_lock.h>
35 #include <sys/cred.h>
36 #include <sys/vnode.h>
37 
38 /*
39  * The snode represents a special file in any filesystem.  There is
40  * one snode for each active special file.  Filesystems that support
41  * special files use specvp(vp, dev, type, cr) to convert a normal
42  * vnode to a special vnode in the ops lookup() and create().
43  *
44  * To handle having multiple snodes that represent the same
45  * underlying device vnode without cache aliasing problems,
46  * the s_commonvp is used to point to the "common" vnode used for
47  * caching data.  If an snode is created internally by the kernel,
48  * then the s_realvp field is NULL and s_commonvp points to s_vnode.
49  * The other snodes which are created as a result of a lookup of a
50  * device in a file system have s_realvp pointing to the vp which
51  * represents the device in the file system while the s_commonvp points
52  * into the "common" vnode for the device in another snode.
53  */
54 
55 /*
56  * Include SUNDDI type definitions so that the s_dip tag doesn't urk.
57  */
58 #include <sys/dditypes.h>
59 
60 #ifdef	__cplusplus
61 extern "C" {
62 #endif
63 
64 struct snode {
65 	/* These fields are protected by stable_lock */
66 	struct	snode *s_next;		/* must be first */
67 	struct	vnode *s_vnode;		/* vnode associated with this snode */
68 	/*
69 	 * These fields are initialized once.
70 	 */
71 	struct	vnode *s_realvp;	/* vnode for the fs entry (if any) */
72 	struct	vnode *s_commonvp;	/* common device vnode */
73 	dev_t	s_dev;			/* device the snode represents */
74 	dev_info_t *s_dip;		/* dev_info (common snode only) */
75 	/*
76 	 * Doesn't always need to be updated atomically because it is a hint.
77 	 * No lock required.
78 	 */
79 	u_offset_t s_nextr;		/* next byte read offset (read-ahead) */
80 
81 	/* These fields are protected by spec_syncbusy */
82 	struct	snode *s_list;		/* used for syncing */
83 	/* These fields are protected by s_lock */
84 	struct devplcy *s_plcy;		/* device node open policy (cs only) */
85 	u_offset_t s_size;		/* block device size in bytes */
86 	uint_t	s_flag;			/* flags, see below */
87 	dev_t	s_fsid;			/* file system identifier */
88 	time_t  s_atime;		/* time of last access */
89 	time_t  s_mtime;		/* time of last modification */
90 	time_t  s_ctime;		/* time of last attributes change */
91 	int	s_count;		/* count of opened references */
92 	long	s_mapcnt;		/* count of mappings of pages */
93 	/* The locks themselves */
94 	kmutex_t	s_lock;		/* protects snode fields */
95 	kcondvar_t	s_cv;		/* synchronize open/closes */
96 };
97 
98 /* flags */
99 #define	SUPD		0x01		/* update device access time */
100 #define	SACC		0x02		/* update device modification time */
101 #define	SCHG		0x04		/* update device change time */
102 #define	SPRIV		0x08		/* file open for private access */
103 #define	SLOFFSET	0x10		/* device takes 64-bit uio offsets */
104 #define	SLOCKED		0x20		/* use to serialize open/closes */
105 #define	SWANT		0x40		/* some process waiting on lock */
106 #define	SANYOFFSET	0x80		/* device takes any uio offset */
107 #define	SCLONE		0x100		/* represents a cloned device */
108 #define	SNEEDCLOSE	0x200		/* needs driver close call */
109 #define	SDIPSET		0x400		/* the vnode has an association with */
110 					/* the driver, even though it may */
111 					/* not currently have an association */
112 					/* with a specific hardware instance */
113 					/* if s_dip is NULL */
114 #define	SSIZEVALID	0x800		/* s_size field is valid */
115 #define	SMUXED		0x1000		/* this snode is a stream that has */
116 					/* been multiplexed */
117 #define	SSELFCLONE	0x2000		/* represents a self cloning device */
118 #define	SNOFLUSH	0x4000		/* do not flush device on fsync */
119 #define	SCLOSING	0x8000		/* in last close(9E) */
120 #define	SFENCED		0x10000		/* snode fenced off for I/O retire */
121 
122 #ifdef _KERNEL
123 /*
124  * Convert between vnode and snode
125  */
126 #define	VTOS(vp)	((struct snode *)((vp)->v_data))
127 #define	VTOCS(vp)	(VTOS(VTOS(vp)->s_commonvp))
128 #define	STOV(sp)	((sp)->s_vnode)
129 
130 extern int spec_debug;
131 
132 #define	SPEC_FENCE_DEBUG	0x0001	/* emit fence related debug messages */
133 
134 #define	FENDBG(args)	if (spec_debug & SPEC_FENCE_DEBUG) cmn_err args
135 
136 
137 /*
138  * Forward declarations
139  */
140 struct vfssw;
141 struct cred;
142 
143 extern struct vfs	spec_vfs;
144 extern struct vfsops	spec_vfsops;
145 extern struct kmem_cache *snode_cache;
146 
147 /*
148  * specfs functions
149  */
150 offset_t	spec_maxoffset(struct vnode *);
151 struct vnodeops	*spec_getvnodeops(void);
152 struct vnode *specvp(struct vnode *, dev_t, vtype_t, struct cred *);
153 struct vnode *makespecvp(dev_t, vtype_t);
154 struct vnode *other_specvp(struct vnode *);
155 struct vnode *common_specvp(struct vnode *);
156 struct vnode *specfind(dev_t, vtype_t);
157 struct vnode *commonvp(dev_t, vtype_t);
158 struct vnode *makectty(vnode_t *);
159 void	sdelete(struct snode *);
160 void 	smark(struct snode *, int);
161 int	specinit(int, char *);
162 int	device_close(struct vnode *, int, struct cred *);
163 int	spec_putpage(struct vnode *, offset_t, size_t, int, struct cred *,
164 		caller_context_t *);
165 int	spec_segmap(dev_t, off_t, struct as *, caddr_t *, off_t,
166 		    uint_t, uint_t, uint_t, cred_t *);
167 struct vnode *specvp_devfs(struct vnode *, dev_t, vtype_t,
168 		    struct cred *, dev_info_t *);
169 void	spec_assoc_vp_with_devi(struct vnode *, dev_info_t *);
170 dev_info_t *spec_hold_devi_by_vp(struct vnode *);
171 int	spec_sync(struct vfs *, short, struct cred *);
172 void	spec_snode_walk(int (*callback)(struct snode *, void *), void *);
173 int	spec_devi_open_count(struct snode *, dev_info_t **);
174 int	spec_is_clone(struct vnode *);
175 int	spec_is_selfclone(struct vnode *);
176 int	spec_fence_snode(dev_info_t *dip, struct vnode *vp);
177 int	spec_unfence_snode(dev_info_t *dip);
178 void	spec_size_invalidate(dev_t, vtype_t);
179 
180 
181 /*
182  * UNKNOWN_SIZE: If driver does not support the [Ss]ize or [Nn]blocks property
183  * then the size is assumed to be "infinite".  Note that this "infinite" value
184  * may need to be converted to a smaller "infinite" value to avoid EOVERFLOW at
185  * field width conversion locations like the stat(2) and NFS code running
186  * against a special file.  Special file code outside specfs may check the
187  * type of the vnode (VCHR|VBLK) and use MAXOFFSET_T directly to detect
188  * UNKNOWN_SIZE.
189  */
190 #define	UNKNOWN_SIZE		MAXOFFSET_T
191 
192 /*
193  * SPEC_MAXOFFSET_T: Solaris does not fully support 64-bit offsets for D_64BIT
194  * (SLOFFSET) block drivers on a 32-bit kernels: daddr_t is still a signed
195  * 32-bit quantity - which limits the byte offset to 1TB. This issue goes
196  * beyond a driver needing to convert from daddr_t to diskaddr_t if it sets
197  * D_64BIT. Many of the DDI interfaces which take daddr_t arguments have no
198  * 64-bit counterpart (bioclone, blkflush, bread, bread_common, breada, getblk,
199  * getblk_common). SPEC_MAXOFFSET_T is used by 32-bit kernel code to enforce
200  * this restriction.
201  */
202 #ifdef	_ILP32
203 #ifdef	_LONGLONG_TYPE
204 #define	SPEC_MAXOFFSET_T	((1LL << ((NBBY * sizeof (daddr32_t)) +	\
205 				DEV_BSHIFT - 1)) - 1)
206 #else	/* !defined(_LONGLONG_TYPE) */
207 #define	SPEC_MAXOFFSET_T	MAXOFF_T
208 #endif	/* _LONGLONG_TYPE */
209 #endif	/* _ILP32 */
210 
211 /*
212  * Snode lookup stuff.
213  * These routines maintain a table of snodes hashed by dev so
214  * that the snode for an dev can be found if it already exists.
215  * NOTE: STABLESIZE must be a power of 2 for STABLEHASH to work!
216  */
217 
218 #define	STABLESIZE	256
219 #define	STABLEHASH(dev)	((getmajor(dev) + getminor(dev)) & (STABLESIZE - 1))
220 extern struct snode *stable[];
221 extern kmutex_t	stable_lock;
222 extern kmutex_t	spec_syncbusy;
223 
224 /*
225  * Variables used by during asynchronous VOP_PUTPAGE operations.
226  */
227 extern struct async_reqs *spec_async_reqs;	/* async request list */
228 extern kmutex_t spec_async_lock;		/* lock to protect async list */
229 
230 #endif	/* _KERNEL */
231 
232 #ifdef	__cplusplus
233 }
234 #endif
235 
236 #endif	/* _SYS_FS_SNODE_H */
237