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