xref: /dragonfly/sys/vfs/nfs/nfsnode.h (revision 6fb88001)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfsnode.h	8.9 (Berkeley) 5/14/95
37  * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfsnode.h,v 1.43 2004/04/14 23:23:55 peadar Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfsnode.h,v 1.14 2005/08/27 20:23:06 joerg Exp $
39  */
40 
41 
42 #ifndef _NFS_NFSNODE_H_
43 #define _NFS_NFSNODE_H_
44 
45 #if !defined(_NFS_NFS_H_) && !defined(_KERNEL)
46 #include "nfs.h"
47 #endif
48 
49 #include <sys/lockf.h>
50 
51 /*
52  * Silly rename structure that hangs off the nfsnode until the name
53  * can be removed by nfs_inactive()
54  */
55 struct sillyrename {
56 	struct	ucred *s_cred;
57 	struct	vnode *s_dvp;
58 	long	s_namlen;
59 	char	s_name[20];
60 };
61 
62 /*
63  * Entries of directories in the buffer cache.
64  */
65 struct nfs_dirent {
66 	ino_t		nfs_ino;	/* file number of entry */
67 	uint16_t	nfs_namlen;	/* strlen(d_name) */
68 	uint16_t	nfs_reclen;	/* total record length */
69 	uint8_t		nfs_type;	/* file type */
70 	uint8_t		nfs_padding1;
71 	uint8_t		nfs_padding2;
72 	uint8_t		nfs_padding3;
73 	char		nfs_name[];	/* name, NUL-terminated */
74 };
75 
76 /*
77  * This structure is used to save the logical directory offset to
78  * NFS cookie mappings.
79  * The mappings are stored in a list headed
80  * by n_cookies, as required.
81  * There is one mapping for each NFS_DIRBLKSIZ bytes of directory information
82  * stored in increasing logical offset byte order.
83  */
84 #define NFSNUMCOOKIES		31
85 
86 struct nfsdmap {
87 	LIST_ENTRY(nfsdmap)	ndm_list;
88 	int			ndm_eocookie;
89 	nfsuint64		ndm_cookies[NFSNUMCOOKIES];
90 };
91 
92 /*
93  * The nfsnode is the nfs equivalent to ufs's inode. Any similarity
94  * is purely coincidental.  There is a unique nfsnode allocated for
95  * each active file, each current directory, each mounted-on file,
96  * text file, and the root.
97  *
98  * An nfsnode is 'named' by its file handle. (nget/nfs_node.c)
99  *
100  * File handles are accessed via n_fhp, which will point to n_fh if the
101  * file handle is small enough (<= NFS_SMALLFH).  Otherwise the file handle
102  * will be allocated.
103  *
104  * DragonFly does not pass ucreds to read and write operations, since such
105  * operations are not possible unless the ucred has already been validated.
106  * Validating ucreds are stored in nfsnode to pass on to NFS read/write RPCs.
107  */
108 struct nfsnode {
109 	LIST_ENTRY(nfsnode)	n_hash;		/* Hash chain */
110 	CIRCLEQ_ENTRY(nfsnode)	n_timer;	/* Nqnfs timer chain */
111 	u_quad_t		n_size;		/* Current size of file */
112 	u_quad_t		n_brev;		/* Modify rev when cached */
113 	u_quad_t		n_lrev;		/* Modify rev for lease */
114 	struct vattr		n_vattr;	/* Vnode attribute cache */
115 	time_t			n_attrstamp;	/* Attr. cache timestamp */
116 	u_int32_t		n_mode;		/* ACCESS mode cache */
117 	uid_t			n_modeuid;	/* credentials having mode */
118 	time_t			n_modestamp;	/* mode cache timestamp */
119 	time_t			n_mtime;	/* Last known modified time */
120 	time_t			n_ctime;	/* Prev create time. */
121 	time_t			n_expiry;	/* Lease expiry time */
122 	nfsfh_t			*n_fhp;		/* NFS File Handle */
123 	struct ucred		*n_rucred;
124 	struct ucred		*n_wucred;
125 	struct vnode		*n_vnode;	/* associated vnode */
126 	struct lockf		n_lockf;	/* Locking record of file */
127 	int			n_error;	/* Save write error value */
128 	union {
129 		struct timespec	nf_atim;	/* Special file times */
130 		nfsuint64	nd_cookieverf;	/* Cookie verifier (dir only) */
131 	} n_un1;
132 	union {
133 		struct timespec	nf_mtim;
134 		off_t		nd_direof;	/* Dir. EOF offset cache */
135 	} n_un2;
136 	union {
137 		struct sillyrename *nf_silly;	/* Ptr to silly rename struct */
138 		LIST_HEAD(, nfsdmap) nd_cook;	/* cookies */
139 	} n_un3;
140 	short			n_fhsize;	/* size in bytes, of fh */
141 	short			n_flag;		/* Flag for locking.. */
142 	nfsfh_t			n_fh;		/* Small File Handle */
143 	struct lock		n_rslock;
144 };
145 
146 #define n_atim		n_un1.nf_atim
147 #define n_mtim		n_un2.nf_mtim
148 #define n_sillyrename	n_un3.nf_silly
149 #define n_cookieverf	n_un1.nd_cookieverf
150 #define n_direofoffset	n_un2.nd_direof
151 #define n_cookies	n_un3.nd_cook
152 
153 /*
154  * Flags for n_flag
155  */
156 #define	NFLUSHWANT	0x0001	/* Want wakeup from a flush in prog. */
157 #define	NFLUSHINPROG	0x0002	/* Avoid multiple calls to vinvalbuf() */
158 #define	NLMODIFIED	0x0004	/* Client has pending modifications */
159 #define	NWRITEERR	0x0008	/* Flag write errors so close will know */
160 #define	NQNFSNONCACHE	0x0020	/* Non-cachable lease */
161 #define	NQNFSWRITE	0x0040	/* Write lease */
162 #define	NQNFSEVICTED	0x0080	/* Has been evicted */
163 #define	NACC		0x0100	/* Special file accessed */
164 #define	NUPD		0x0200	/* Special file updated */
165 #define	NCHG		0x0400	/* Special file times changed */
166 #define	NLOCKED		0x0800  /* node is locked */
167 #define	NWANTED		0x0100  /* someone wants to lock */
168 #define	NRMODIFIED	0x2000  /* Server has unsynchronized modifications */
169 
170 /*
171  * Convert between nfsnode pointers and vnode pointers
172  */
173 #define	VTONFS(vp)	((struct nfsnode *)(vp)->v_data)
174 #define	NFSTOV(np)	((struct vnode *)(np)->n_vnode)
175 
176 /*
177  * Queue head for nfsiod's
178  */
179 extern TAILQ_HEAD(nfs_bufq, buf) nfs_bufq;
180 extern struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
181 extern struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
182 
183 #if defined(_KERNEL)
184 
185 /*
186  *	nfs_rslock -	Attempt to obtain lock on nfsnode
187  *
188  *	Attempt to obtain a lock on the passed nfsnode, returning ENOLCK
189  *	if the lock could not be obtained due to our having to sleep.  This
190  *	function is generally used to lock around code that modifies an
191  *	NFS file's size.  In order to avoid deadlocks the lock
192  *	should not be obtained while other locks are being held.
193  */
194 
195 static __inline
196 int
197 nfs_rslock(struct nfsnode *np, struct thread *td)
198 {
199         return(lockmgr(&np->n_rslock, LK_EXCLUSIVE | LK_CANRECURSE |
200 		LK_SLEEPFAIL, NULL, td));
201 }
202 
203 static __inline
204 void
205 nfs_rsunlock(struct nfsnode *np, struct thread *td)
206 {
207 	(void)lockmgr(&np->n_rslock, LK_RELEASE, NULL, td);
208 }
209 
210 static __inline
211 struct ucred *
212 nfs_vpcred(struct vnode *vp, int ndflag)
213 {
214 	struct nfsnode *np = VTONFS(vp);
215 
216 	if (np && (ndflag & ND_WRITE) && np->n_wucred)
217 		return(np->n_wucred);
218 	if (np && (ndflag & ND_READ) && np->n_rucred)
219 		return(np->n_rucred);
220 	return(VFSTONFS((vp)->v_mount)->nm_cred);
221 }
222 
223 /*
224  * Prototypes for NFS vnode operations
225  */
226 int	nfs_getpages (struct vop_getpages_args *);
227 int	nfs_putpages (struct vop_putpages_args *);
228 int	nfs_write (struct vop_write_args *);
229 int	nqnfs_vop_lease_check (struct vop_lease_args *);
230 int	nfs_inactive (struct vop_inactive_args *);
231 int	nfs_reclaim (struct vop_reclaim_args *);
232 int	nfs_flush (struct vnode *, int, struct thread *, int);
233 
234 /* other stuff */
235 int	nfs_removeit (struct sillyrename *);
236 int	nfs_nget (struct mount *,nfsfh_t *,int,struct nfsnode **);
237 nfsuint64 *nfs_getcookie (struct nfsnode *, off_t, int);
238 void	nfs_invaldir (struct vnode *);
239 
240 #define	nqnfs_lease_updatetime	nfs_lease_updatetime
241 
242 #endif /* _KERNEL */
243 
244 #endif
245