xref: /dragonfly/sys/vfs/nfs/nfsnode.h (revision 7adf09fa)
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. Neither the name of the University nor the names of its 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 REGENTS 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 REGENTS 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  *	@(#)nfsnode.h	8.9 (Berkeley) 5/14/95
33  * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfsnode.h,v 1.43 2004/04/14 23:23:55 peadar Exp $
34  * $DragonFly: src/sys/vfs/nfs/nfsnode.h,v 1.18 2006/05/06 16:01:21 dillon Exp $
35  */
36 
37 
38 #ifndef _NFS_NFSNODE_H_
39 #define _NFS_NFSNODE_H_
40 
41 #if !defined(_NFS_NFS_H_) && !defined(_KERNEL)
42 #include "nfs.h"
43 #endif
44 
45 #include <sys/lockf.h>
46 
47 /*
48  * Silly rename structure that hangs off the nfsnode until the name
49  * can be removed by nfs_inactive()
50  */
51 struct sillyrename {
52 	struct	ucred *s_cred;
53 	struct	vnode *s_dvp;
54 	long	s_namlen;
55 	char	s_name[20];
56 };
57 
58 /*
59  * Entries of directories in the buffer cache.
60  */
61 struct nfs_dirent {
62 	ino_t		nfs_ino;	/* file number of entry */
63 	uint16_t	nfs_namlen;	/* strlen(d_name) */
64 	uint16_t	nfs_reclen;	/* total record length */
65 	uint8_t		nfs_type;	/* file type */
66 	uint8_t		nfs_padding1;
67 	uint8_t		nfs_padding2;
68 	uint8_t		nfs_padding3;
69 	char		nfs_name[];	/* name, NUL-terminated */
70 };
71 
72 /*
73  * This structure is used to save the logical directory offset to
74  * NFS cookie mappings.
75  * The mappings are stored in a list headed
76  * by n_cookies, as required.
77  * There is one mapping for each NFS_DIRBLKSIZ bytes of directory information
78  * stored in increasing logical offset byte order.
79  */
80 #define NFSNUMCOOKIES		31
81 
82 struct nfsdmap {
83 	LIST_ENTRY(nfsdmap)	ndm_list;
84 	int			ndm_eocookie;
85 	nfsuint64		ndm_cookies[NFSNUMCOOKIES];
86 };
87 
88 /*
89  * The nfsnode is the nfs equivalent to ufs's inode. Any similarity
90  * is purely coincidental.  There is a unique nfsnode allocated for
91  * each active file, each current directory, each mounted-on file,
92  * text file, and the root.
93  *
94  * An nfsnode is 'named' by its file handle. (nget/nfs_node.c)
95  *
96  * File handles are accessed via n_fhp, which will point to n_fh if the
97  * file handle is small enough (<= NFS_SMALLFH).  Otherwise the file handle
98  * will be allocated.
99  *
100  * DragonFly does not pass ucreds to read and write operations, since such
101  * operations are not possible unless the ucred has already been validated.
102  * Validating ucreds are stored in nfsnode to pass on to NFS read/write RPCs.
103  */
104 struct nfsnode {
105 	LIST_ENTRY(nfsnode)	n_hash;		/* Hash chain */
106 	u_quad_t		n_size;		/* Current size of file */
107 	u_quad_t		n_brev;		/* Modify rev when cached */
108 	struct vattr		n_vattr;	/* Vnode attribute cache */
109 	time_t			n_attrstamp;	/* Attr. cache timestamp */
110 	u_int32_t		n_mode;		/* ACCESS mode cache */
111 	uid_t			n_modeuid;	/* credentials having mode */
112 	time_t			n_modestamp;	/* mode cache timestamp */
113 	time_t			n_mtime;	/* Last known modified time */
114 	time_t			n_ctime;	/* Prev create time. */
115 	time_t			n_expiry;	/* Lease expiry time */
116 	nfsfh_t			*n_fhp;		/* NFS File Handle */
117 	struct ucred		*n_rucred;
118 	struct ucred		*n_wucred;
119 	struct vnode		*n_vnode;	/* associated vnode */
120 	struct lockf		n_lockf;	/* Locking record of file */
121 	int			n_error;	/* Save write error value */
122 	union {
123 		struct timespec	nf_atim;	/* Special file times */
124 		nfsuint64	nd_cookieverf;	/* Cookie verifier (dir only) */
125 	} n_un1;
126 	union {
127 		struct timespec	nf_mtim;
128 		off_t		nd_direof;	/* Dir. EOF offset cache */
129 	} n_un2;
130 	union {
131 		struct sillyrename *nf_silly;	/* Ptr to silly rename struct */
132 		LIST_HEAD(, nfsdmap) nd_cook;	/* cookies */
133 	} n_un3;
134 	short			n_fhsize;	/* size in bytes, of fh */
135 	short			n_flag;		/* Flag for locking.. */
136 	nfsfh_t			n_fh;		/* Small File Handle */
137 	struct lock		n_rslock;
138 };
139 
140 #define n_atim		n_un1.nf_atim
141 #define n_mtim		n_un2.nf_mtim
142 #define n_sillyrename	n_un3.nf_silly
143 #define n_cookieverf	n_un1.nd_cookieverf
144 #define n_direofoffset	n_un2.nd_direof
145 #define n_cookies	n_un3.nd_cook
146 
147 /*
148  * Flags for n_flag
149  */
150 #define	NFLUSHWANT	0x0001	/* Want wakeup from a flush in prog. */
151 #define	NFLUSHINPROG	0x0002	/* Avoid multiple calls to vinvalbuf() */
152 #define	NLMODIFIED	0x0004	/* Client has pending modifications */
153 #define	NWRITEERR	0x0008	/* Flag write errors so close will know */
154 #define	NREMOVED	0x0020	/* Flag possible removal of file */
155 #define	NUNUSED040	0x0040
156 #define	NQNFSEVICTED	0x0080	/* Has been evicted */
157 #define	NACC		0x0100	/* Special file accessed */
158 #define	NUPD		0x0200	/* Special file updated */
159 #define	NCHG		0x0400	/* Special file times changed */
160 #define	NLOCKED		0x0800  /* node is locked */
161 #define	NWANTED		0x0100  /* someone wants to lock */
162 #define	NRMODIFIED	0x2000  /* Server has unsynchronized modifications */
163 
164 /*
165  * Convert between nfsnode pointers and vnode pointers
166  */
167 #define	VTONFS(vp)	((struct nfsnode *)(vp)->v_data)
168 #define	NFSTOV(np)	((struct vnode *)(np)->n_vnode)
169 
170 /*
171  * Queue head for nfsiod's
172  */
173 extern TAILQ_HEAD(nfs_bufq, buf) nfs_bufq;
174 
175 #if defined(_KERNEL)
176 
177 /*
178  *	nfs_rslock -	Attempt to obtain lock on nfsnode
179  *
180  *	Attempt to obtain a lock on the passed nfsnode, returning ENOLCK
181  *	if the lock could not be obtained due to our having to sleep.  This
182  *	function is generally used to lock around code that modifies an
183  *	NFS file's size.  In order to avoid deadlocks the lock
184  *	should not be obtained while other locks are being held.
185  */
186 
187 static __inline
188 int
nfs_rslock(struct nfsnode * np)189 nfs_rslock(struct nfsnode *np)
190 {
191         return(lockmgr(&np->n_rslock, LK_EXCLUSIVE | LK_CANRECURSE |
192 		       LK_SLEEPFAIL));
193 }
194 
195 static __inline
196 void
nfs_rsunlock(struct nfsnode * np)197 nfs_rsunlock(struct nfsnode *np)
198 {
199 	lockmgr(&np->n_rslock, LK_RELEASE);
200 }
201 
202 static __inline
203 struct ucred *
nfs_vpcred(struct vnode * vp,int ndflag)204 nfs_vpcred(struct vnode *vp, int ndflag)
205 {
206 	struct nfsnode *np = VTONFS(vp);
207 
208 	if (np && (ndflag & ND_WRITE) && np->n_wucred)
209 		return(np->n_wucred);
210 	if (np && (ndflag & ND_READ) && np->n_rucred)
211 		return(np->n_rucred);
212 	return(VFSTONFS((vp)->v_mount)->nm_cred);
213 }
214 
215 /*
216  * Prototypes for NFS vnode operations
217  */
218 int	nfs_write (struct vop_write_args *);
219 int	nfs_inactive (struct vop_inactive_args *);
220 int	nfs_reclaim (struct vop_reclaim_args *);
221 int	nfs_flush (struct vnode *, int, struct thread *, int);
222 
223 /* other stuff */
224 int	nfs_removeit (struct sillyrename *);
225 int	nfs_nget (struct mount *, nfsfh_t *, int,
226 		struct nfsnode **, struct vnode *);
227 int	nfs_nget_nonblock (struct mount *, nfsfh_t *, int,
228 		struct nfsnode **, struct vnode *);
229 nfsuint64 *nfs_getcookie (struct nfsnode *, off_t, int);
230 void	nfs_invaldir (struct vnode *);
231 
232 #endif /* _KERNEL */
233 
234 #endif
235