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