xref: /titanic_41/usr/src/uts/common/nfs/rnode4.h (revision fb9f9b975cb9214fec5dab37d461199adab9b964)
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_RNODE4_H
31 #define	_NFS_RNODE4_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <nfs/rnode.h>		/* for symlink_cache, nfs_rwlock_t, etc. */
40 #include <nfs/nfs4.h>
41 #include <nfs/nfs4_clnt.h>
42 #include <sys/thread.h>
43 #include <sys/sysmacros.h>	/* for offsetof */
44 
45 typedef enum nfs4_access_type {
46 	NFS4_ACCESS_UNKNOWN,
47 	NFS4_ACCESS_ALLOWED,
48 	NFS4_ACCESS_DENIED
49 } nfs4_access_type_t;
50 
51 /*
52  * Access cache
53  */
54 typedef struct acache4_hash {
55 	struct acache4 *next;
56 	struct acache4 *prev;
57 	krwlock_t lock;
58 } acache4_hash_t;
59 
60 typedef struct acache4 {
61 	struct acache4 *next;	/* next and prev must be first */
62 	struct acache4 *prev;
63 	uint32_t known;
64 	uint32_t allowed;
65 	struct rnode4 *rnode;
66 	cred_t *cred;
67 	struct acache4 *list;
68 	struct acache4_hash *hashq;
69 } acache4_t;
70 
71 /*
72  * Note on the different buffer sizes in rddir4_cache:
73  * There seems to be some discrepancy between the intended and actual
74  * use of entlen and buflen, which does not correspond to the comment below.
75  *	entlen - nfsv2/3 used as both alloc'd size of entries buffer and
76  *		as the actual size of the entries (XXX is this correct?).
77  *		nfsv4 will use it only as the alloc'd size.
78  *	buflen - used for calculations of readahead.
79  *	actlen - added for nfsv4 to serve as the size of the useful
80  *		portion of the entries buffer. That is because in
81  *		nfsv4, the otw entries are converted to system entries,
82  *		and may not be the same size - thus buffer may not be full.
83  */
84 typedef struct rddir4_cache {
85 	lloff_t _cookie;	/* cookie used to find this cache entry */
86 	lloff_t _ncookie;	/* cookie used to find the next cache entry */
87 	char *entries;		/* buffer containing dirent entries */
88 	int eof;		/* EOF reached after this request */
89 	int entlen;		/* size of dirent entries in buf */
90 	int buflen;		/* size of the buffer used to store entries */
91 	int actlen;		/* size of the actual entries (nfsv4 only) */
92 	int flags;		/* control flags, see below */
93 	kcondvar_t cv;		/* cv for blocking */
94 	int error;		/* error from RPC operation */
95 	void *data;		/* private data */
96 } rddir4_cache;
97 
98 #define	nfs4_cookie	_cookie._f
99 #define	nfs4_ncookie	_ncookie._f
100 
101 /*
102  * Shadow vnode, v4 only.
103  *
104  * A file's shadow vnode list is protected by its hash bucket lock,
105  * r_hashq->r_lock.
106  *
107  * sv_r_vnode is protected by the appropriate vnode locks.
108  *
109  * sv_dfh, sv_name, sv_dfileid, and sv_dfileid_valid are protected
110  * by rp->r_svlock.
111  */
112 
113 typedef struct insq_link {
114 	void	*forw;
115 	void	*back;
116 } insq_link_t;
117 
118 typedef struct svnode {
119 	insq_link_t	sv_link;	/* must be first for insque */
120 	vnode_t		*sv_r_vnode;	/* vnode for this shadow */
121 	nfs4_fname_t	*sv_name;	/* component name */
122 	nfs4_sharedfh_t	*sv_dfh;	/* directory file handle */
123 } svnode_t;
124 
125 #define	sv_forw			sv_link.forw
126 #define	sv_back			sv_link.back
127 extern svnode_t			*vtosv(vnode_t *);
128 #define	VTOSV(vp)		vtosv(vp)
129 #define	SVTOV(svp)		(((svp)->sv_r_vnode))
130 #define	IS_SHADOW(vp, rp)	((vp) != (rp)->r_vnode)
131 
132 /*
133  * The format of the hash bucket used to lookup rnodes from a file handle.
134  */
135 typedef struct r4hashq {
136 	struct rnode4 *r_hashf;
137 	struct rnode4 *r_hashb;
138 	krwlock_t r_lock;
139 } r4hashq_t;
140 
141 /*
142  * Remote file information structure.
143  *
144  * The rnode is the "inode" for remote files.  It contains all the
145  * information necessary to handle remote file on the client side.
146  *
147  * Note on file sizes:  we keep two file sizes in the rnode: the size
148  * according to the client (r_size) and the size according to the server
149  * (r_attr.va_size).  They can differ because we modify r_size during a
150  * write system call (nfs_rdwr), before the write request goes over the
151  * wire (before the file is actually modified on the server).  If an OTW
152  * request occurs before the cached data is written to the server the file
153  * size returned from the server (r_attr.va_size) may not match r_size.
154  * r_size is the one we use, in general.  r_attr.va_size is only used to
155  * determine whether or not our cached data is valid.
156  *
157  * Each rnode has 5 locks associated with it (not including the rnode
158  * hash table and free list locks):
159  *
160  *	r_rwlock:	Serializes nfs_write and nfs_setattr requests
161  *			and allows nfs_read requests to proceed in parallel.
162  *			Serializes reads/updates to directories.
163  *
164  *	r_lkserlock:	Serializes lock requests with map, write, and
165  *			readahead operations.
166  *
167  *	r_statelock:	Protects all fields in the rnode except for
168  *			those listed below.  This lock is intented
169  *			to be held for relatively short periods of
170  *			time (not accross entire putpage operations,
171  *			for example).
172  *
173  *	r_statev4_lock:	Protects the created_v4 flag, the lock_owners list,
174  *			and all the delegation fields except r_deleg_list.
175  *
176  *	r_os_lock:	Protects r_open_streams.
177  *
178  *
179  * The following members are protected by the mutex rp4freelist_lock:
180  *	r_freef
181  *	r_freeb
182  *
183  * The following members are protected by the hash bucket rwlock:
184  *	r_hashf
185  *	r_hashb
186  *
187  * r_fh is read-only except when an rnode is created (or recycled from the
188  * free list).
189  *
190  * The following members are protected by nfs4_server_t::s_lock:
191  *	r_deleg_list
192  *
193  * Note: r_modaddr is only accessed when the r_statelock mutex is held.
194  *	Its value is also controlled via r_rwlock.  It is assumed that
195  *	there will be only 1 writer active at a time, so it safe to
196  *	set r_modaddr and release r_statelock as long as the r_rwlock
197  *	writer lock is held.
198  *
199  * 64-bit offsets: the code formerly assumed that atomic reads of
200  * r_size were safe and reliable; on 32-bit architectures, this is
201  * not true since an intervening bus cycle from another processor
202  * could update half of the size field.  The r_statelock must now
203  * be held whenever any kind of access of r_size is made.
204  *
205  * Lock ordering:
206  * 	r_rwlock > r_lkserlock > r_os_lock > r_statelock > r_statev4_lock
207  *	vnode_t::v_lock > r_os_lock
208  */
209 struct exportinfo;	/* defined in nfs/export.h */
210 struct servinfo4;	/* defined in nfs/nfs4_clnt.h */
211 struct failinfo;	/* defined in nfs/nfs_clnt.h */
212 struct mntinfo4;	/* defined in nfs/nfs4_clnt.h */
213 
214 typedef struct rnode4 {
215 	/* the hash fields must be first to match the rhashq_t */
216 	struct rnode4	*r_hashf;	/* hash queue forward pointer */
217 	struct rnode4	*r_hashb;	/* hash queue back pointer */
218 	struct rnode4	*r_freef;	/* free list forward pointer */
219 	struct rnode4	*r_freeb;	/* free list back pointer */
220 	r4hashq_t	*r_hashq;	/* pointer to the hash bucket */
221 
222 	svnode_t	r_svnode;	/* "master" shadow vnode for file */
223 	kmutex_t	r_svlock;	/* serializes access to svnode list */
224 	nfs_rwlock_t	r_rwlock;	/* serializes write/setattr requests */
225 	nfs_rwlock_t	r_lkserlock;	/* serialize lock with other ops */
226 	kmutex_t	r_statelock;	/* protects (most of) rnode contents */
227 	nfs4_sharedfh_t	*r_fh;		/* file handle */
228 	struct servinfo4
229 			*r_server;	/* current server */
230 	u_offset_t	r_nextr;	/* next byte read offset (read-ahead) */
231 	uint_t		r_flags;	/* flags, see below */
232 	short		r_error;	/* async write error */
233 	cred_t		*r_unlcred;	/* unlinked credentials */
234 	char		*r_unlname;	/* unlinked file name */
235 	vnode_t		*r_unldvp;	/* parent dir of unlinked file */
236 	vnode_t		*r_xattr_dir;	/* cached xattr dir vnode */
237 	len_t		r_size;		/* client's view of file size */
238 	vattr_t		r_attr;		/* cached vnode attributes */
239 	hrtime_t	r_time_attr_saved; /* time attributes were cached */
240 	hrtime_t	r_time_attr_inval; /* time attributes become invalid */
241 	hrtime_t	r_time_cache_inval; /* time caches become invalid */
242 	time_t		r_delay_wait;	/* future time for DELAY handling */
243 	int		r_delay_interval; /* Number of Secs of last DELAY */
244 	time_t		r_last_recov;	/* time of last recovery operation */
245 	nfs4_recov_t	r_recov_act;	/* action from last recovery op */
246 	long		r_mapcnt;	/* count of mmapped pages */
247 	uint_t		r_count;	/* # of refs not reflect in v_count */
248 	uint_t		r_awcount;	/* # of outstanding async write */
249 	uint_t		r_gcount;	/* getattrs waiting to flush pages */
250 	kcondvar_t	r_cv;		/* condvar for blocked threads */
251 	int		(*r_putapage)	/* address of putapage routine */
252 		(vnode_t *, page_t *, u_offset_t *, size_t *, int, cred_t *);
253 	void		*r_dir;		/* cache of readdir responses */
254 	rddir4_cache	*r_direof;	/* pointer to the EOF entry */
255 	symlink_cache	r_symlink;	/* cached readlink response */
256 	verifier4	r_writeverf;	/* file data write verifier */
257 	u_offset_t	r_modaddr;	/* address for page in writerp */
258 	commit_t	r_commit;	/* commit information */
259 	u_offset_t	r_truncaddr;	/* base for truncate operation */
260 	vsecattr_t	*r_secattr;	/* cached security attributes (acls) */
261 	verifier4	r_cookieverf4;	/* version 4 readdir cookie verifier */
262 	nfs4_pathconf_info_t r_pathconf; /* cached pathconf info */
263 	acache4_t	*r_acache;	/* list of access cache entries */
264 	list_t		r_open_streams;	/* open streams list */
265 	kmutex_t	r_os_lock;	/* protects r_open_streams */
266 	nfs4_lock_owner_t
267 			r_lo_head;	/* lock owners list head */
268 	int		created_v4;	/* 1 if file has been created in v4 */
269 	kmutex_t	r_statev4_lock;	/* protects created_v4, state4ptr */
270 
271 	list_node_t	r_deleg_link;	/* linkage into list of */
272 					/* delegated rnodes for this server */
273 	open_delegation_type4
274 			r_deleg_type;	/* type of delegation granted */
275 	stateid4	r_deleg_stateid;
276 					/* delegation state id */
277 	nfs_space_limit4
278 			r_deleg_limit;	/* file limits returned from */
279 					/* server on delegated open */
280 	nfsace4		r_deleg_perms;	/* file permissions returned from */
281 					/* server on delegated open */
282 	fattr4_change	r_deleg_change;	/* current deleg change attr */
283 	fattr4_change	r_deleg_change_grant;
284 					/* change @ write deleg grant */
285 	cred_t		*r_deleg_cred;	/* credential in force when the */
286 					/* delegation was granted */
287 	open_delegation_type4
288 			r_deleg_needs_recovery;
289 					/* delegation needs recovery */
290 					/* This contains the delegation type */
291 					/* for use with CLAIM_PREVIOUS. */
292 					/* OPEN_DELEGATE_NONE means recovery */
293 					/* is not needed. */
294 	unsigned	r_deleg_needs_recall:1;
295 					/* delegation has been recalled by */
296 					/* the server during open with */
297 					/* CLAIM_PREVIOUS */
298 	unsigned 	r_deleg_return_pending:1;
299 					/* delegreturn is pending, don't use */
300 					/* the delegation stateid, set in */
301 					/* nfs4_dlistadd */
302 	unsigned 	r_deleg_return_inprog:1;
303 					/* delegreturn is in progress, may */
304 					/* only be set by nfs4delegreturn. */
305 	nfs_rwlock_t    r_deleg_recall_lock;
306 					/* lock for synchronizing delegreturn */
307 					/* with in other operations, acquired */
308 					/* in read mode by nfs4_start_fop, */
309 					/* acquired in write mode in */
310 					/* nfs4delegreturn */
311 	fattr4_change	r_change;	/* GETATTR4 change attr;  client  */
312 					/* should always request change   */
313 					/* when c/mtime requested to keep */
314 					/* change and c/mtime in sync	  */
315 	fattr4_fileid	r_mntd_fid;	/* mounted on fileid attr	  */
316 	kthread_t	*r_serial;	/* attrcache validation thread */
317 	kthread_t	*r_pgflush;	/* thread flushing page cache */
318 	list_t		r_indelmap;	/* list of delmap callers */
319 	fattr4_fsid	r_srv_fsid;	/* fsid of srv fs containing object */
320 					/* when rnode created; compare with */
321 					/* sv_fsid (servinfo4_t) to see why */
322 					/* R4SRVSTUB was set		    */
323 } rnode4_t;
324 
325 #define	r_vnode	r_svnode.sv_r_vnode
326 
327 /*
328  * Flags
329  */
330 #define	R4READDIRWATTR	0x1	/* Use READDIR with attributes */
331 #define	R4DIRTY		0x2	/* dirty pages from write operation */
332 #define	R4STALE		0x4	/* stale, don't even attempt to write */
333 #define	R4MODINPROGRESS	0x8	/* page modification happening */
334 #define	R4TRUNCATE	0x10	/* truncating, don't commit */
335 #define	R4HAVEVERF	0x20	/* have a write verifier to compare against */
336 #define	R4COMMIT	0x40	/* commit in progress */
337 #define	R4COMMITWAIT	0x80	/* someone is waiting to do a commit */
338 #define	R4HASHED	0x100	/* rnode is in hash queues */
339 #define	R4OUTOFSPACE	0x200	/* an out of space error has happened */
340 #define	R4LODANGLERS	0x400	/* rnode has dangling lock_owners to cleanup */
341 #define	R4WRITEMODIFIED	0x800	/* file data has been modified by write */
342 #define	R4DIRECTIO	0x1000	/* bypass the buffer cache */
343 #define	R4RECOVERR	0x2000	/* couldn't recover */
344 #define	R4RECEXPFH	0x4000	/* recovering expired filehandle */
345 #define	R4RECOVERRP	0x8000	/* R4RECOVERR pending, but not set (yet) */
346 #define	R4SRVSTUB	0x10000	/* server stub / fs mntpnt */
347 #define	R4ISXATTR	0x20000	/* rnode is a named attribute */
348 #define	R4DELMAPLIST	0x40000	/* delmap callers tracked for as callback */
349 #define	R4PGFLUSH	0x80000	/* page flush thread active */
350 #define	R4LOOKUP	0x200000 /* a lookup has been done in the directory */
351 /*
352  * Convert between vnode and rnode
353  */
354 #define	RTOV4(rp)	((rp)->r_vnode)
355 #define	VTOR4(vp)	((rnode4_t *)((vp)->v_data))
356 
357 /*
358  * Open file instances.
359  */
360 
361 typedef struct nfs4_opinst {
362 	struct nfs4_opinst	*re_next; /* next in list */
363 	vnode_t			*re_vp;	/* held reference */
364 	uint32_t		re_numosp; /* number of valid open streams */
365 	nfs4_open_stream_t	**re_osp; /* held reference */
366 } nfs4_opinst_t;
367 
368 #ifdef _KERNEL
369 
370 extern long nrnode;
371 
372 /* Used for r_delay_interval */
373 #define	NFS4_INITIAL_DELAY_INTERVAL	 1
374 #define	NFS4_MAX_DELAY_INTERVAL		20
375 
376 extern rnode4_t	*r4find(r4hashq_t *, nfs4_sharedfh_t *, struct vfs *);
377 extern rnode4_t	*r4find_unlocked(nfs4_sharedfh_t *, struct vfs *);
378 extern void	r4flush(struct vfs *, cred_t *);
379 extern void	destroy_rtable4(struct vfs *, cred_t *);
380 extern int	check_rtable4(struct vfs *);
381 extern void	rp4_addfree(rnode4_t *, cred_t *);
382 extern void	rp4_addhash(rnode4_t *);
383 extern void	rp4_rmhash(rnode4_t *);
384 extern void	rp4_rmhash_locked(rnode4_t *);
385 extern int	rtable4hash(nfs4_sharedfh_t *);
386 
387 extern vnode_t *makenfs4node(nfs4_sharedfh_t *, nfs4_ga_res_t *, struct vfs *,
388 				hrtime_t, cred_t *, vnode_t *, nfs4_fname_t *);
389 extern vnode_t *makenfs4node_by_fh(nfs4_sharedfh_t *, nfs4_sharedfh_t *,
390     nfs4_fname_t **, nfs4_ga_res_t *, mntinfo4_t *, cred_t *, hrtime_t);
391 
392 extern nfs4_opinst_t *r4mkopenlist(struct mntinfo4 *);
393 extern void	r4releopenlist(nfs4_opinst_t *);
394 
395 /* Access cache calls */
396 extern nfs4_access_type_t nfs4_access_check(rnode4_t *, uint32_t, cred_t *);
397 extern void	nfs4_access_cache(rnode4_t *rp, uint32_t, uint32_t, cred_t *);
398 extern int	nfs4_access_purge_rp(rnode4_t *);
399 
400 extern int	nfs4_free_data_reclaim(rnode4_t *);
401 extern void	nfs4_rnode_invalidate(struct vfs *);
402 
403 extern time_t	r2lease_time(rnode4_t *);
404 extern int	nfs4_directio(vnode_t *, int, cred_t *);
405 
406 /* shadow vnode functions */
407 extern void	sv_activate(vnode_t **, vnode_t *, nfs4_fname_t **, int);
408 extern vnode_t	*sv_find(vnode_t *, vnode_t *, nfs4_fname_t **);
409 extern void	sv_update_path(vnode_t *, char *, char *);
410 extern void	sv_inactive(vnode_t *);
411 extern void	sv_exchange(vnode_t **);
412 extern void	sv_uninit(svnode_t *);
413 extern void	nfs4_clear_open_streams(rnode4_t *);
414 
415 /*
416  * Mark cached attributes as timed out
417  *
418  * The caller must not be holding the rnode r_statelock mutex.
419  */
420 #define	PURGE_ATTRCACHE4_LOCKED(rp)				\
421 	rp->r_time_attr_inval = gethrtime();			\
422 	rp->r_time_attr_saved = rp->r_time_attr_inval;		\
423 	rp->r_pathconf.pc4_xattr_valid = 0;			\
424 	rp->r_pathconf.pc4_cache_valid = 0;
425 
426 #define	PURGE_ATTRCACHE4(vp)	{				\
427 	rnode4_t *rp = VTOR4(vp);				\
428 	mutex_enter(&rp->r_statelock);				\
429 	PURGE_ATTRCACHE4_LOCKED(rp);				\
430 	mutex_exit(&rp->r_statelock);				\
431 }
432 
433 
434 extern void	nfs4_async_readdir(vnode_t *, rddir4_cache *,
435 			cred_t *, int (*)(vnode_t *, rddir4_cache *, cred_t *));
436 extern char	*rnode4info(rnode4_t *rp);
437 
438 extern int	writerp4(rnode4_t *, caddr_t, int, struct uio *, int);
439 extern void	nfs4_set_nonvattrs(rnode4_t *, struct nfs4attr_to_vattr *);
440 extern void	nfs4delegabandon(rnode4_t *);
441 extern stateid4 nfs4_get_w_stateid(cred_t *, rnode4_t *, pid_t, mntinfo4_t *,
442 			nfs_opnum4, nfs4_stateid_types_t *);
443 extern stateid4 nfs4_get_stateid(cred_t *, rnode4_t *, pid_t, mntinfo4_t *,
444 			nfs_opnum4, nfs4_stateid_types_t *, bool_t);
445 extern nfsstat4 nfs4_find_or_create_lock_owner(pid_t, rnode4_t *, cred_t *,
446 			nfs4_open_owner_t **, nfs4_open_stream_t **,
447 			nfs4_lock_owner_t **);
448 extern cred_t   *nfs4_get_otw_cred_by_osp(rnode4_t *, cred_t *,
449 			nfs4_open_stream_t **, bool_t *, bool_t *);
450 
451 
452 /*
453  * Defines for the flag argument of nfs4delegreturn
454  */
455 #define	NFS4_DR_FORCE	0x1	/* discard even if start_op fails */
456 #define	NFS4_DR_PUSH	0x2	/* push modified data back to the server */
457 #define	NFS4_DR_DISCARD	0x4	/* discard the delegation w/o delegreturn */
458 #define	NFS4_DR_DID_OP	0x8	/* calling function did nfs4_start_op */
459 #define	NFS4_DR_RECALL	0x10	/* delegreturn done in response to CB_RECALL */
460 #define	NFS4_DR_REOPEN	0x20	/* perform file reopens, if applicable */
461 
462 extern int nfs4delegreturn(rnode4_t *, int);
463 extern void	nfs4_delegreturn_all(nfs4_server_t *);
464 extern void	nfs4delegreturn_cleanup(rnode4_t *, nfs4_server_t *);
465 extern void nfs4_delegation_accept(rnode4_t *, open_claim_type4, OPEN4res *,
466 		nfs4_ga_res_t *, cred_t *);
467 
468 extern void	nfs4_dlistclean(void);
469 extern void	nfs4_deleg_discard(mntinfo4_t *, nfs4_server_t *);
470 
471 extern void	rddir4_cache_create(rnode4_t *);
472 extern void	rddir4_cache_purge(rnode4_t *);
473 extern void	rddir4_cache_destroy(rnode4_t *);
474 extern rddir4_cache *rddir4_cache_lookup(rnode4_t *, offset_t, int);
475 extern void	rddir4_cache_rele(rnode4_t *, rddir4_cache *);
476 
477 #ifdef DEBUG
478 extern char	*rddir4_cache_buf_alloc(size_t, int);
479 extern void	rddir4_cache_buf_free(void *, size_t);
480 #endif
481 
482 
483 
484 #endif /* _KERNEL */
485 
486 #ifdef	__cplusplus
487 }
488 #endif
489 
490 #endif	/* _NFS_RNODE4_H */
491