xref: /titanic_44/usr/src/uts/common/nfs/rnode.h (revision 1cb6af97c6f66f456d4f726ef056e1ebc0f73305)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #ifndef	_NFS_RNODE_H
31 #define	_NFS_RNODE_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include <sys/avl.h>
36 #include <sys/list.h>
37 #include <nfs/nfs.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 typedef enum nfs_access_type {
44 	NFS_ACCESS_UNKNOWN,
45 	NFS_ACCESS_ALLOWED,
46 	NFS_ACCESS_DENIED
47 } nfs_access_type_t;
48 
49 typedef struct acache_hash {
50 	struct acache *next;	/* next and prev must be first */
51 	struct acache *prev;
52 	krwlock_t lock;
53 } acache_hash_t;
54 
55 typedef struct acache {
56 	struct acache *next;	/* next and prev must be first */
57 	struct acache *prev;
58 	uint32_t known;
59 	uint32_t allowed;
60 	struct rnode *rnode;
61 	cred_t *cred;
62 	struct acache *list;
63 	struct acache_hash *hashq;
64 } acache_t;
65 
66 #define	NFS_FHANDLE_LEN	64
67 
68 typedef struct nfs_fhandle {
69 	int fh_len;
70 	char fh_buf[NFS_FHANDLE_LEN];
71 } nfs_fhandle;
72 
73 typedef struct rddir_cache {
74 	lloff_t _cookie;	/* cookie used to find this cache entry */
75 	lloff_t _ncookie;	/* cookie used to find the next cache entry */
76 	char *entries;		/* buffer containing dirent entries */
77 	int eof;		/* EOF reached after this request */
78 	int entlen;		/* size of dirent entries in buf */
79 	int buflen;		/* size of the buffer used to store entries */
80 	int flags;		/* control flags, see below */
81 	kcondvar_t cv;		/* cv for blocking */
82 	int error;		/* error from RPC operation */
83 	kmutex_t lock;
84 	uint_t count;		/* reference count */
85 	avl_node_t tree;	/* AVL tree links */
86 } rddir_cache;
87 
88 #define	nfs_cookie	_cookie._p._l
89 #define	nfs_ncookie	_ncookie._p._l
90 #define	nfs3_cookie	_cookie._f
91 #define	nfs3_ncookie	_ncookie._f
92 
93 #define	RDDIR		0x1	/* readdir operation in progress */
94 #define	RDDIRWAIT	0x2	/* waiting on readdir in progress */
95 #define	RDDIRREQ	0x4	/* a new readdir is required */
96 #define	RDDIRCACHED	0x8	/* entry is in the cache */
97 
98 #define	HAVE_RDDIR_CACHE(rp)	(avl_numnodes(&(rp)->r_dir) > 0)
99 
100 typedef struct symlink_cache {
101 	char *contents;		/* contents of the symbolic link */
102 	int len;		/* length of the contents */
103 	int size;		/* size of the allocated buffer */
104 } symlink_cache;
105 
106 typedef struct commit {
107 	page_t *c_pages;	/* list of pages to commit */
108 	offset3 c_commbase;	/* base offset to do commit from */
109 	count3 c_commlen;	/* len to commit */
110 	kcondvar_t c_cv;	/* condvar for waiting for commit */
111 } commit_t;
112 
113 /*
114  * The various values for the commit states.  These are stored in
115  * the p_fsdata byte in the page struct.
116  */
117 #define	C_NOCOMMIT	0	/* no commit is required */
118 #define	C_COMMIT	1	/* a commit is required so do it now */
119 #define	C_DELAYCOMMIT	2	/* a commit is required, but can be delayed */
120 
121 /*
122  * The lock manager holds state making it possible for the client
123  * and server to be out of sync.  For example, if the response from
124  * the server granting a lock request is lost, the server will think
125  * the lock is granted and the client will think the lock is lost.
126  * To deal with this, a list of processes for which the client is
127  * not sure if the server holds a lock is attached to the rnode.
128  * When such a process closes the rnode, an unlock request is sent
129  * to the server to unlock the entire file.
130  *
131  * The list is kept as a singularly linked NULL terminated list.
132  * Because it is  only added to under extreme error conditions, the
133  * list shouldn't get very big.  DEBUG kernels print a console warning
134  * when the number of entries on a list go beyond nfs_lmpl_high_water
135  * an  arbitrary number defined in nfs_add_locking_id()
136  */
137 #define	RLMPL_PID	1
138 #define	RLMPL_OWNER	2
139 typedef struct lock_manager_pid_list {
140 	int lmpl_type;
141 	pid_t lmpl_pid;
142 	union {
143 		pid_t _pid;
144 		struct {
145 			int len;
146 			char *owner;
147 		} _own;
148 	} un;
149 	struct lock_manager_pid_list *lmpl_next;
150 } lmpl_t;
151 
152 #define	lmpl_opid un._pid
153 #define	lmpl_own_len un._own.len
154 #define	lmpl_owner un._own.owner
155 
156 /*
157  * A homegrown reader/writer lock implementation.  It addresses
158  * two requirements not addressed by the system primitives.  They
159  * are that the `enter" operation is optionally interruptible and
160  * that that they can be re`enter'ed by writers without deadlock.
161  */
162 typedef struct nfs_rwlock {
163 	int count;
164 	int waiters;
165 	kthread_t *owner;
166 	kmutex_t lock;
167 	kcondvar_t cv;
168 } nfs_rwlock_t;
169 
170 /*
171  * The format of the hash bucket used to lookup rnodes from a file handle.
172  */
173 typedef struct rhashq {
174 	struct rnode *r_hashf;
175 	struct rnode *r_hashb;
176 	krwlock_t r_lock;
177 } rhashq_t;
178 
179 /*
180  * Remote file information structure.
181  *
182  * The rnode is the "inode" for remote files.  It contains all the
183  * information necessary to handle remote file on the client side.
184  *
185  * Note on file sizes:  we keep two file sizes in the rnode: the size
186  * according to the client (r_size) and the size according to the server
187  * (r_attr.va_size).  They can differ because we modify r_size during a
188  * write system call (nfs_rdwr), before the write request goes over the
189  * wire (before the file is actually modified on the server).  If an OTW
190  * request occurs before the cached data is written to the server the file
191  * size returned from the server (r_attr.va_size) may not match r_size.
192  * r_size is the one we use, in general.  r_attr.va_size is only used to
193  * determine whether or not our cached data is valid.
194  *
195  * Each rnode has 3 locks associated with it (not including the rnode
196  * hash table and free list locks):
197  *
198  *	r_rwlock:	Serializes nfs_write and nfs_setattr requests
199  *			and allows nfs_read requests to proceed in parallel.
200  *			Serializes reads/updates to directories.
201  *
202  *	r_lkserlock:	Serializes lock requests with map, write, and
203  *			readahead operations.
204  *
205  *	r_statelock:	Protects all fields in the rnode except for
206  *			those listed below.  This lock is intented
207  *			to be held for relatively short periods of
208  *			time (not accross entire putpage operations,
209  *			for example).
210  *
211  * The following members are protected by the mutex rpfreelist_lock:
212  *	r_freef
213  *	r_freeb
214  *
215  * The following members are protected by the hash bucket rwlock:
216  *	r_hashf
217  *	r_hashb
218  *
219  * Note: r_modaddr is only accessed when the r_statelock mutex is held.
220  *	Its value is also controlled via r_rwlock.  It is assumed that
221  *	there will be only 1 writer active at a time, so it safe to
222  *	set r_modaddr and release r_statelock as long as the r_rwlock
223  *	writer lock is held.
224  *
225  * 64-bit offsets: the code formerly assumed that atomic reads of
226  * r_size were safe and reliable; on 32-bit architectures, this is
227  * not true since an intervening bus cycle from another processor
228  * could update half of the size field.  The r_statelock must now
229  * be held whenever any kind of access of r_size is made.
230  *
231  * Lock ordering:
232  * 	r_rwlock > r_lkserlock > r_statelock
233  */
234 struct exportinfo;	/* defined in nfs/export.h */
235 struct servinfo;	/* defined in nfs/nfs_clnt.h */
236 struct failinfo;	/* defined in nfs/nfs_clnt.h */
237 struct mntinfo;		/* defined in nfs/nfs_clnt.h */
238 
239 #ifdef _KERNEL
240 
241 typedef struct rnode {
242 	/* the hash fields must be first to match the rhashq_t */
243 	struct rnode	*r_hashf;	/* hash queue forward pointer */
244 	struct rnode	*r_hashb;	/* hash queue back pointer */
245 	struct rnode	*r_freef;	/* free list forward pointer */
246 	struct rnode	*r_freeb;	/* free list back pointer */
247 	rhashq_t	*r_hashq;	/* pointer to the hash bucket */
248 	vnode_t		*r_vnode;	/* vnode for remote file */
249 	nfs_rwlock_t	r_rwlock;	/* serializes write/setattr requests */
250 	nfs_rwlock_t	r_lkserlock;	/* serialize lock with other ops */
251 	kmutex_t	r_statelock;	/* protects (most of) rnode contents */
252 	nfs_fhandle	r_fh;		/* file handle */
253 	struct servinfo	*r_server;	/* current server */
254 	char		*r_path;	/* path to this rnode */
255 	u_offset_t	r_nextr;	/* next byte read offset (read-ahead) */
256 	cred_t		*r_cred;	/* current credentials */
257 	cred_t		*r_unlcred;	/* unlinked credentials */
258 	char		*r_unlname;	/* unlinked file name */
259 	vnode_t		*r_unldvp;	/* parent dir of unlinked file */
260 	len_t		r_size;		/* client's view of file size */
261 	struct vattr	r_attr;		/* cached vnode attributes */
262 	hrtime_t	r_attrtime;	/* time attributes become invalid */
263 	hrtime_t	r_mtime;	/* client time file last modified */
264 	long		r_mapcnt;	/* count of mmapped pages */
265 	uint_t		r_count;	/* # of refs not reflect in v_count */
266 	uint_t		r_awcount;	/* # of outstanding async write */
267 	uint_t		r_gcount;	/* getattrs waiting to flush pages */
268 	ushort_t	r_flags;	/* flags, see below */
269 	short		r_error;	/* async write error */
270 	kcondvar_t	r_cv;		/* condvar for blocked threads */
271 	int		(*r_putapage)	/* address of putapage routine */
272 		(vnode_t *, page_t *, u_offset_t *, size_t *, int, cred_t *);
273 	avl_tree_t	r_dir;		/* cache of readdir responses */
274 	rddir_cache	*r_direof;	/* pointer to the EOF entry */
275 	symlink_cache	r_symlink;	/* cached readlink response */
276 	writeverf3	r_verf;		/* version 3 write verifier */
277 	u_offset_t	r_modaddr;	/* address for page in writerp */
278 	commit_t	r_commit;	/* commit information */
279 	u_offset_t	r_truncaddr;	/* base for truncate operation */
280 	vsecattr_t	*r_secattr;	/* cached security attributes (acls) */
281 	cookieverf3	r_cookieverf;	/* version 3 readdir cookie verifier */
282 	lmpl_t		*r_lmpl;	/* pids that may be holding locks */
283 	nfs3_pathconf_info *r_pathconf;	/* cached pathconf information */
284 	acache_t	*r_acache;	/* list of access cache entries */
285 	kthread_t	*r_serial;	/* id of purging thread */
286 	list_t		r_indelmap;	/* list of delmap callers */
287 } rnode_t;
288 #endif /* _KERNEL */
289 
290 /*
291  * Flags
292  */
293 #define	RREADDIRPLUS	0x1	/* issue a READDIRPLUS instead of READDIR */
294 #define	RDIRTY		0x2	/* dirty pages from write operation */
295 #define	RSTALE		0x4	/* file handle is stale */
296 #define	RMODINPROGRESS	0x8	/* page modification happening */
297 #define	RTRUNCATE	0x10	/* truncating, don't commit */
298 #define	RHAVEVERF	0x20	/* have a write verifier to compare against */
299 #define	RCOMMIT		0x40	/* commit in progress */
300 #define	RCOMMITWAIT	0x80	/* someone is waiting to do a commit */
301 #define	RHASHED		0x100	/* rnode is in hash queues */
302 #define	ROUTOFSPACE	0x200	/* an out of space error has happened */
303 #define	RDIRECTIO	0x400	/* bypass the buffer cache */
304 #define	RLOOKUP		0x800	/* a lookup has been performed */
305 #define	RWRITEATTR	0x1000	/* attributes came from WRITE */
306 #define	RINDNLCPURGE	0x2000	/* in the process of purging DNLC references */
307 #define	RDELMAPLIST	0x4000	/* delmap callers tracking for as callback */
308 
309 /*
310  * Convert between vnode and rnode
311  */
312 #define	RTOV(rp)	((rp)->r_vnode)
313 #define	VTOR(vp)	((rnode_t *)((vp)->v_data))
314 
315 #define	VTOFH(vp)	(RTOFH(VTOR(vp)))
316 #define	RTOFH(rp)	((fhandle_t *)(&(rp)->r_fh.fh_buf))
317 #define	VTOFH3(vp)	(RTOFH3(VTOR(vp)))
318 #define	RTOFH3(rp)	((nfs_fh3 *)(&(rp)->r_fh))
319 
320 #ifdef _KERNEL
321 extern int	nfs_async_readahead(vnode_t *, u_offset_t, caddr_t,
322 				struct seg *, cred_t *,
323 				void (*)(vnode_t *, u_offset_t,
324 				caddr_t, struct seg *, cred_t *));
325 extern int	nfs_async_putapage(vnode_t *, page_t *, u_offset_t, size_t,
326 				int, cred_t *, int (*)(vnode_t *, page_t *,
327 				u_offset_t, size_t, int, cred_t *));
328 extern int	nfs_async_pageio(vnode_t *, page_t *, u_offset_t, size_t,
329 				int, cred_t *, int (*)(vnode_t *, page_t *,
330 				u_offset_t, size_t, int, cred_t *));
331 extern void	nfs_async_readdir(vnode_t *, rddir_cache *,
332 				cred_t *, int (*)(vnode_t *,
333 				rddir_cache *, cred_t *));
334 extern void	nfs_async_commit(vnode_t *, page_t *, offset3, count3,
335 				cred_t *, void (*)(vnode_t *, page_t *,
336 				offset3, count3, cred_t *));
337 extern void	nfs_async_inactive(vnode_t *, cred_t *, void (*)(vnode_t *,
338 				cred_t *));
339 extern int	writerp(rnode_t *, caddr_t, int, struct uio *, int);
340 extern int	nfs_putpages(vnode_t *, u_offset_t, size_t, int, cred_t *);
341 extern void	nfs_invalidate_pages(vnode_t *, u_offset_t, cred_t *);
342 extern int	rfs2call(struct mntinfo *, rpcproc_t, xdrproc_t, caddr_t,
343 			xdrproc_t, caddr_t, cred_t *, int *, enum nfsstat *,
344 			int, struct failinfo *);
345 extern int	rfs3call(struct mntinfo *, rpcproc_t, xdrproc_t, caddr_t,
346 			xdrproc_t, caddr_t, cred_t *, int *, nfsstat3 *,
347 			int, struct failinfo *);
348 extern void	nfs_setswaplike(vnode_t *, vattr_t *);
349 extern vnode_t	*makenfsnode(fhandle_t *, struct nfsfattr *, struct vfs *,
350 			hrtime_t, cred_t *, char *, char *);
351 extern vnode_t	*makenfs3node_va(nfs_fh3 *, vattr_t *, struct vfs *, hrtime_t,
352 			cred_t *, char *, char *);
353 extern vnode_t	*makenfs3node(nfs_fh3 *, fattr3 *, struct vfs *, hrtime_t,
354 			cred_t *, char *, char *);
355 extern void	rp_addfree(rnode_t *, cred_t *);
356 extern void	rp_rmhash(rnode_t *);
357 extern int	check_rtable(struct vfs *);
358 extern void	destroy_rtable(struct vfs *, cred_t *);
359 extern void	rflush(struct vfs *, cred_t *);
360 extern nfs_access_type_t nfs_access_check(rnode_t *, uint32_t, cred_t *);
361 extern void	nfs_access_cache(rnode_t *rp, uint32_t, uint32_t, cred_t *);
362 extern int	nfs_access_purge_rp(rnode_t *);
363 extern int	nfs_putapage(vnode_t *, page_t *, u_offset_t *, size_t *,
364 			int, cred_t *);
365 extern int	nfs3_putapage(vnode_t *, page_t *, u_offset_t *, size_t *,
366 			int, cred_t *);
367 extern void	nfs_printfhandle(nfs_fhandle *);
368 extern void	nfs_write_error(vnode_t *, int, cred_t *);
369 extern rddir_cache	*rddir_cache_alloc(int);
370 extern void		rddir_cache_hold(rddir_cache *);
371 extern void		rddir_cache_rele(rddir_cache *);
372 #ifdef DEBUG
373 extern char		*rddir_cache_buf_alloc(size_t, int);
374 extern void		rddir_cache_buf_free(void *, size_t);
375 #endif
376 extern int	nfs_rw_enter_sig(nfs_rwlock_t *, krw_t, int);
377 extern int	nfs_rw_tryenter(nfs_rwlock_t *, krw_t);
378 extern void	nfs_rw_exit(nfs_rwlock_t *);
379 extern int	nfs_rw_lock_held(nfs_rwlock_t *, krw_t);
380 extern void	nfs_rw_init(nfs_rwlock_t *, char *, krw_type_t, void *);
381 extern void	nfs_rw_destroy(nfs_rwlock_t *);
382 extern int	nfs_directio(vnode_t *, int, cred_t *);
383 extern int	nfs3_rddir_compar(const void *, const void *);
384 extern int	nfs_rddir_compar(const void *, const void *);
385 
386 #endif
387 
388 #ifdef	__cplusplus
389 }
390 #endif
391 
392 #endif	/* _NFS_RNODE_H */
393