1 /*
2  * Copyright (c) 2000-2001, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: smbfs_node.h,v 1.31.52.1 2005/05/27 02:35:28 lindak Exp $
33  */
34 
35 /*
36  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  *
39  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
40  */
41 
42 #ifndef _FS_SMBFS_NODE_H_
43 #define	_FS_SMBFS_NODE_H_
44 
45 /*
46  * Much code copied into here from Sun NFS.
47  * Compare with nfs_clnt.h
48  */
49 
50 #include <sys/avl.h>
51 #include <sys/list.h>
52 #include <netsmb/smb_subr.h>
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 /*
59  * Cache whole directories (not yet)
60  */
61 typedef struct rddir_cache {
62 	lloff_t _cookie;	/* cookie used to find this cache entry */
63 	lloff_t _ncookie;	/* cookie used to find the next cache entry */
64 	char *entries;		/* buffer containing dirent entries */
65 	int eof;		/* EOF reached after this request */
66 	int entlen;		/* size of dirent entries in buf */
67 	int buflen;		/* size of the buffer used to store entries */
68 	int flags;		/* control flags, see below */
69 	kcondvar_t cv;		/* cv for blocking */
70 	int error;		/* error from RPC operation */
71 	kmutex_t lock;
72 	uint_t count;		/* reference count */
73 	avl_node_t tree;	/* AVL tree links */
74 } rddir_cache;
75 
76 #define	smbfs_cookie	_cookie._p._l
77 #define	smbfs_ncookie	_ncookie._p._l
78 #define	smbfs3_cookie	_cookie._f
79 #define	smbfs3_ncookie	_ncookie._f
80 
81 #define	RDDIR		0x1	/* readdir operation in progress */
82 #define	RDDIRWAIT	0x2	/* waiting on readdir in progress */
83 #define	RDDIRREQ	0x4	/* a new readdir is required */
84 #define	RDDIRCACHED	0x8	/* entry is in the cache */
85 
86 #define	HAVE_RDDIR_CACHE(rp)	(avl_numnodes(&(rp)->r_dir) > 0)
87 
88 /*
89  * A homegrown reader/writer lock implementation.  It addresses
90  * two requirements not addressed by the system primitives.  They
91  * are that the `enter" operation is optionally interruptible and
92  * that that they can be re`enter'ed by writers without deadlock.
93  */
94 typedef struct smbfs_rwlock {
95 	int count;
96 	int waiters;
97 	kthread_t *owner;
98 	kmutex_t lock;
99 	kcondvar_t cv;
100 } smbfs_rwlock_t;
101 
102 /*
103  * The format of the smbfs node header, which contains the
104  * fields used to link nodes in the AVL tree, and those
105  * fields needed by the AVL node comparison functions.
106  * It's a separate struct so we can call avl_find with
107  * this relatively small struct as a stack local.
108  *
109  * The AVL tree is mntinfo.smi_hash_avl,
110  * and its lock is mntinfo.smi_hash_lk.
111  */
112 typedef struct smbfs_node_hdr {
113 	/*
114 	 * Our linkage in the node cache AVL tree.
115 	 */
116 	avl_node_t	hdr_avl_node;
117 
118 	/*
119 	 * Identity of this node:  The full path name,
120 	 * in server form, relative to the share root.
121 	 */
122 	char		*hdr_n_rpath;
123 	int		hdr_n_rplen;
124 } smbfs_node_hdr_t;
125 
126 /*
127  * Below is the SMBFS-specific representation of a "node".
128  * This struct is a mixture of Sun NFS and Darwin code.
129  * Fields starting with "r_" came from NFS struct "rnode"
130  * and fields starting with "n_" came from Darwin, or
131  * were added during the Solaris port.  We have avoided
132  * renaming fields so we would not cause excessive
133  * changes in the code using this struct.
134  *
135  * Now using an AVL tree instead of hash lists, but kept the
136  * "hash" in some member names and functions to reduce churn.
137  * One AVL tree per mount replaces the global hash buckets.
138  *
139  * Notes carried over from the NFS code:
140  *
141  * The smbnode is the "inode" for remote files.  It contains all the
142  * information necessary to handle remote file on the client side.
143  *
144  * Note on file sizes:  we keep two file sizes in the smbnode: the size
145  * according to the client (r_size) and the size according to the server
146  * (r_attr.fa_size).  They can differ because we modify r_size during a
147  * write system call (smbfs_rdwr), before the write request goes over the
148  * wire (before the file is actually modified on the server).  If an OTW
149  * request occurs before the cached data is written to the server the file
150  * size returned from the server (r_attr.fa_size) may not match r_size.
151  * r_size is the one we use, in general.  r_attr.fa_size is only used to
152  * determine whether or not our cached data is valid.
153  *
154  * Each smbnode has 3 locks associated with it (not including the smbnode
155  * "hash" AVL tree and free list locks):
156  *
157  *	r_rwlock:	Serializes smbfs_write and smbfs_setattr requests
158  *			and allows smbfs_read requests to proceed in parallel.
159  *			Serializes reads/updates to directories.
160  *
161  *	r_lkserlock:	Serializes lock requests with map, write, and
162  *			readahead operations.
163  *
164  *	r_statelock:	Protects all fields in the smbnode except for
165  *			those listed below.  This lock is intented
166  *			to be held for relatively short periods of
167  *			time (not accross entire putpage operations,
168  *			for example).
169  *
170  * The following members are protected by the mutex smbfreelist_lock:
171  *	r_freef
172  *	r_freeb
173  *
174  * The following members are protected by the AVL tree rwlock:
175  *	r_avl_node	(r__hdr.hdr_avl_node)
176  *
177  * Note: r_modaddr is only accessed when the r_statelock mutex is held.
178  *	Its value is also controlled via r_rwlock.  It is assumed that
179  *	there will be only 1 writer active at a time, so it safe to
180  *	set r_modaddr and release r_statelock as long as the r_rwlock
181  *	writer lock is held.
182  *
183  * 64-bit offsets: the code formerly assumed that atomic reads of
184  * r_size were safe and reliable; on 32-bit architectures, this is
185  * not true since an intervening bus cycle from another processor
186  * could update half of the size field.  The r_statelock must now
187  * be held whenever any kind of access of r_size is made.
188  *
189  * Lock ordering:
190  *	r_rwlock > r_lkserlock > r_statelock
191  */
192 
193 typedef struct smbnode {
194 	/* Our linkage in the node cache AVL tree (see above). */
195 	smbfs_node_hdr_t	r__hdr;
196 
197 	/* short-hand names for r__hdr members */
198 #define	r_avl_node	r__hdr.hdr_avl_node
199 #define	n_rpath		r__hdr.hdr_n_rpath
200 #define	n_rplen		r__hdr.hdr_n_rplen
201 
202 	smbmntinfo_t	*n_mount;	/* VFS data */
203 	vnode_t		*r_vnode;	/* associated vnode */
204 
205 	/*
206 	 * Linkage in smbfreelist, for reclaiming nodes.
207 	 * Lock for the free list is: smbfreelist_lock
208 	 */
209 	struct smbnode	*r_freef;	/* free list forward pointer */
210 	struct smbnode	*r_freeb;	/* free list back pointer */
211 
212 	smbfs_rwlock_t	r_rwlock;	/* serialize write/setattr requests */
213 	smbfs_rwlock_t	r_lkserlock;	/* serialize lock with other ops */
214 	kmutex_t	r_statelock;	/* protect (most) smbnode fields */
215 
216 	/*
217 	 * File handle, directory search handle,
218 	 * and reference counts for them, etc.
219 	 * Lock for these is: r_lkserlock
220 	 */
221 	int		n_dirrefs;
222 	struct smbfs_fctx	*n_dirseq;	/* ff context */
223 	int		n_dirofs;	/* last ff offset */
224 	int		n_fidrefs;
225 	smb_fh_t	*n_fid;		/* file handle */
226 	enum vtype	n_ovtype;	/* vnode type opened */
227 
228 	/*
229 	 * Misc. bookkeeping
230 	 */
231 	cred_t		*r_cred;	/* current credentials */
232 	u_offset_t	r_nextr;	/* next read offset (read-ahead) */
233 	long		r_mapcnt;	/* count of mmapped pages */
234 	uint_t		r_inmap;	/* to serialize read/write and mmap */
235 	uint_t		r_count;	/* # of refs not reflect in v_count */
236 	uint_t		r_awcount;	/* # of outstanding async write */
237 	uint_t		r_gcount;	/* getattrs waiting to flush pages */
238 	uint_t		r_flags;	/* flags, see below */
239 	uint32_t	n_flag;		/* N--- flags below */
240 	uint_t		r_error;	/* async write error */
241 	kcondvar_t	r_cv;		/* condvar for blocked threads */
242 	avl_tree_t	r_dir;		/* cache of readdir responses */
243 	rddir_cache	*r_direof;	/* pointer to the EOF entry */
244 	u_offset_t	r_modaddr;	/* address for page in writenp */
245 	kthread_t	*r_serial;	/* id of purging thread */
246 	list_t		r_indelmap;	/* list of delmap callers */
247 
248 	/*
249 	 * Attributes: local, and as last seen on the server.
250 	 * See notes above re: r_size vs r_attr.fa_size, etc.
251 	 */
252 	smbfattr_t	r_attr;		/* attributes from the server */
253 	hrtime_t	r_attrtime;	/* time attributes become invalid */
254 	hrtime_t	r_mtime;	/* client time file last modified */
255 	len_t		r_size;		/* client's view of file size */
256 
257 	/*
258 	 * Security attributes.
259 	 */
260 	vsecattr_t	r_secattr;
261 	hrtime_t	r_sectime;
262 
263 	/*
264 	 * Other attributes, not carried in smbfattr_t
265 	 */
266 	u_longlong_t	n_ino;
267 	uid_t		n_uid;
268 	gid_t		n_gid;
269 	mode_t		n_mode;
270 } smbnode_t;
271 
272 /*
273  * Flag bits in: smbnode_t .n_flag
274  */
275 #define	NFLUSHINPROG	0x00001
276 #define	NFLUSHWANT	0x00002 /* they should gone ... */
277 #define	NMODIFIED	0x00004 /* bogus, until async IO implemented */
278 #define	NREFPARENT	0x00010 /* node holds parent from recycling */
279 #define	NGOTIDS		0x00020
280 #define	NRDIRSERIAL	0x00080	/* serialize readdir operation */
281 #define	NISMAPPED	0x00800
282 #define	NFLUSHWIRE	0x01000
283 #define	NATTRCHANGED	0x02000 /* kill cached attributes at close */
284 #define	NALLOC		0x04000 /* being created */
285 #define	NWALLOC		0x08000 /* awaiting creation */
286 #define	N_XATTR		0x10000 /* extended attribute (dir or file) */
287 
288 /*
289  * Flag bits in: smbnode_t .r_flags
290  */
291 #define	RREADDIRPLUS	0x1	/* issue a READDIRPLUS instead of READDIR */
292 #define	RDIRTY		0x2	/* dirty pages from write operation */
293 #define	RSTALE		0x4	/* file handle is stale */
294 #define	RMODINPROGRESS	0x8	/* page modification happening */
295 #define	RTRUNCATE	0x10	/* truncating, don't commit */
296 #define	RHAVEVERF	0x20	/* have a write verifier to compare against */
297 #define	RCOMMIT		0x40	/* commit in progress */
298 #define	RCOMMITWAIT	0x80	/* someone is waiting to do a commit */
299 #define	RHASHED		0x100	/* smbnode is in the "hash" AVL tree */
300 #define	ROUTOFSPACE	0x200	/* an out of space error has happened */
301 #define	RDIRECTIO	0x400	/* bypass the buffer cache */
302 #define	RLOOKUP		0x800	/* a lookup has been performed */
303 #define	RWRITEATTR	0x1000	/* attributes came from WRITE */
304 #define	RINDNLCPURGE	0x2000	/* in the process of purging DNLC references */
305 #define	RDELMAPLIST	0x4000	/* delmap callers tracking for as callback */
306 
307 /*
308  * Convert between vnode and smbnode
309  */
310 #define	VTOSMB(vp)	((smbnode_t *)((vp)->v_data))
311 #define	SMBTOV(np)	((np)->r_vnode)
312 
313 /*
314  * A macro to compute the separator that should be used for
315  * names under some directory.  See smbfs_fullpath().
316  */
317 #define	SMBFS_DNP_SEP(dnp) \
318 	(((dnp->n_flag & N_XATTR) == 0 && dnp->n_rplen > 1) ? '\\' : '\0')
319 
320 #ifdef __cplusplus
321 }
322 #endif
323 
324 #endif /* _FS_SMBFS_NODE_H_ */
325