]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsnode.h
a2da9b5097b9653d80b505ddf89399cedd560c08
[apple/xnu.git] / bsd / nfs / nfsnode.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfsnode.h 8.9 (Berkeley) 5/14/95
65 * FreeBSD-Id: nfsnode.h,v 1.24 1997/10/28 14:06:25 bde Exp $
66 */
67
68
69 #ifndef _NFS_NFSNODE_H_
70 #define _NFS_NFSNODE_H_
71
72 #include <sys/appleapiopts.h>
73
74 #ifdef __APPLE_API_PRIVATE
75 #ifndef _NFS_NFS_H_
76 #include <nfs/nfs.h>
77 #endif
78
79 /*
80 * Silly rename structure that hangs off the nfsnode until the name
81 * can be removed by nfs_inactive()
82 */
83 struct sillyrename {
84 struct ucred *s_cred;
85 vnode_t s_dvp;
86 long s_namlen;
87 char s_name[20];
88 };
89
90 /*
91 * This structure is used to save the logical directory offset to
92 * NFS cookie mappings.
93 * The mappings are stored in a list headed
94 * by n_cookies, as required.
95 * There is one mapping for each NFS_DIRBLKSIZ bytes of directory information
96 * stored in increasing logical offset byte order.
97 */
98 #define NFSNUMCOOKIES 31
99
100 struct nfsdmap {
101 LIST_ENTRY(nfsdmap) ndm_list;
102 int ndm_eocookie;
103 nfsuint64 ndm_cookies[NFSNUMCOOKIES];
104 };
105
106 /*
107 * The nfsbuf is the nfs equivalent to a struct buf.
108 */
109 struct nfsbuf {
110 LIST_ENTRY(nfsbuf) nb_hash; /* hash chain */
111 LIST_ENTRY(nfsbuf) nb_vnbufs; /* vnode's nfsbuf chain */
112 TAILQ_ENTRY(nfsbuf) nb_free; /* free list position if not active. */
113 volatile long nb_flags; /* NB_* flags. */
114 volatile long nb_lflags; /* NBL_* flags. */
115 volatile long nb_refs; /* outstanding references. */
116 long nb_bufsize; /* buffer size */
117 daddr64_t nb_lblkno; /* logical block number. */
118 uint64_t nb_verf; /* V3 write verifier */
119 time_t nb_timestamp; /* buffer timestamp */
120 int nb_error; /* errno value. */
121 u_int32_t nb_valid; /* valid pages in buf */
122 u_int32_t nb_dirty; /* dirty pages in buf */
123 int nb_validoff; /* offset in buffer of valid region. */
124 int nb_validend; /* offset of end of valid region. */
125 int nb_dirtyoff; /* offset in buffer of dirty region. */
126 int nb_dirtyend; /* offset of end of dirty region. */
127 caddr_t nb_data; /* mapped buffer */
128 vnode_t nb_vp; /* device vnode */
129 proc_t nb_proc; /* associated proc; NULL if kernel. */
130 struct ucred * nb_rcred; /* read credentials reference */
131 struct ucred * nb_wcred; /* write credentials reference */
132 void * nb_pagelist; /* upl */
133 };
134
135 #define NFS_MAXBSIZE (32 * PAGE_SIZE) /* valid/dirty page masks limit buffer size */
136
137 /*
138 * These flags are kept in b_lflags...
139 * nfs_buf_mutex must be held before examining/updating
140 */
141 #define NBL_BUSY 0x00000001 /* I/O in progress. */
142 #define NBL_WANTED 0x00000002 /* Process wants this buffer. */
143
144 /*
145 * These flags are kept in nb_flags and they're (purposefully)
146 * very similar to the B_* flags for struct buf.
147 * nfs_buf_mutex is not needed to examine/update these.
148 */
149 #define NB_NEEDCOMMIT 0x00000002 /* Append-write in progress. */
150 #define NB_ASYNC 0x00000004 /* Start I/O, do not wait. */
151 #define NB_CACHE 0x00000020 /* Bread found us in the cache. */
152 #define NB_STABLE 0x00000040 /* write FILESYNC not UNSTABLE. */
153 #define NB_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
154 #define NB_DONE 0x00000200 /* I/O completed. */
155 #define NB_EINTR 0x00000400 /* I/O was interrupted */
156 #define NB_ERROR 0x00000800 /* I/O error occurred. */
157 #define NB_WASDIRTY 0x00001000 /* page was found dirty in the VM cache */
158 #define NB_INVAL 0x00002000 /* Does not contain valid info. */
159 #define NB_NOCACHE 0x00008000 /* Do not cache block after use. */
160 #define NB_READ 0x00100000 /* Read buffer. */
161 #define NB_PAGELIST 0x00400000 /* Buffer describes pagelist I/O. */
162 #define NB_WRITE 0x00000000 /* Write buffer (pseudo flag). */
163 #define NB_WRITEINPROG 0x01000000 /* Write in progress. */
164 #define NB_META 0x40000000 /* buffer contains meta-data. */
165 #define NB_IOD 0x80000000 /* buffer being handled by nfsiod. */
166
167 /* Flags for operation type in nfs_buf_get() */
168 #define NBLK_READ 0x00000001 /* buffer for read */
169 #define NBLK_WRITE 0x00000002 /* buffer for write */
170 #define NBLK_META 0x00000004 /* buffer for metadata */
171 #define NBLK_OPMASK 0x00000007 /* operation mask */
172 /* modifiers for above flags... */
173 #define NBLK_NOWAIT 0x40000000 /* don't wait on busy buffer */
174 #define NBLK_ONLYVALID 0x80000000 /* only return cached buffer */
175
176 /* These flags are used for nfsbuf iterating */
177 #define NBI_ITER 0x01 /* iteration in progress */
178 #define NBI_ITERWANT 0x02 /* waiting to iterate */
179 #define NBI_CLEAN 0x04 /* requesting clean buffers */
180 #define NBI_DIRTY 0x08 /* requesting dirty buffers */
181 #define NBI_NOWAIT 0x10 /* don't block on NBI_ITER */
182
183 /* Flags for nfs_buf_acquire */
184 #define NBAC_NOWAIT 0x01 /* Don't wait if buffer is busy */
185 #define NBAC_REMOVE 0x02 /* Remove from free list once buffer is acquired */
186
187 /* some convenience macros... */
188 #define NBOFF(BP) ((off_t)(BP)->nb_lblkno * (off_t)(BP)->nb_bufsize)
189 #define NBPGVALID(BP,P) (((BP)->nb_valid >> (P)) & 0x1)
190 #define NBPGDIRTY(BP,P) (((BP)->nb_dirty >> (P)) & 0x1)
191 #define NBPGVALID_SET(BP,P) ((BP)->nb_valid |= (1 << (P)))
192 #define NBPGDIRTY_SET(BP,P) ((BP)->nb_dirty |= (1 << (P)))
193
194 #define NBUFSTAMPVALID(BP) ((BP)->nb_timestamp != ~0)
195 #define NBUFSTAMPINVALIDATE(BP) ((BP)->nb_timestamp = ~0)
196
197 #define NFS_BUF_MAP(BP) \
198 do { \
199 if (!(BP)->nb_data && nfs_buf_map(BP)) \
200 panic("nfs_buf_map failed"); \
201 } while (0)
202
203 LIST_HEAD(nfsbuflists, nfsbuf);
204 TAILQ_HEAD(nfsbuffreehead, nfsbuf);
205
206 #define NFSNOLIST ((struct nfsbuf *)0xdeadbeef)
207
208 extern lck_mtx_t *nfs_buf_mutex;
209 extern int nfsbufcnt, nfsbufmin, nfsbufmax, nfsbufmetacnt, nfsbufmetamax;
210 extern int nfsbuffreecnt, nfsbuffreemetacnt, nfsbufdelwricnt, nfsneedbuffer;
211 extern int nfs_nbdwrite;
212 extern struct nfsbuffreehead nfsbuffree, nfsbufdelwri;
213 extern time_t nfsbuffreeuptimestamp;
214
215 #define NFSBUFCNTCHK(locked) \
216 do { \
217 if (!locked) lck_mtx_lock(nfs_buf_mutex); \
218 if ( (nfsbufcnt < 0) || \
219 (nfsbufcnt > nfsbufmax) || \
220 (nfsbufmetacnt < 0) || \
221 (nfsbufmetacnt > nfsbufmetamax) || \
222 (nfsbufmetacnt > nfsbufcnt) || \
223 (nfsbuffreecnt < 0) || \
224 (nfsbuffreecnt > nfsbufmax) || \
225 (nfsbuffreecnt > nfsbufcnt) || \
226 (nfsbuffreemetacnt < 0) || \
227 (nfsbuffreemetacnt > nfsbufmax) || \
228 (nfsbuffreemetacnt > nfsbufcnt) || \
229 (nfsbuffreemetacnt > nfsbufmetamax) || \
230 (nfsbuffreemetacnt > nfsbufmetacnt) || \
231 (nfsbufdelwricnt < 0) || \
232 (nfsbufdelwricnt > nfsbufmax) || \
233 (nfsbufdelwricnt > nfsbufcnt) || \
234 (nfs_nbdwrite < 0) || \
235 (nfs_nbdwrite > nfsbufcnt) || \
236 0) \
237 panic("nfsbuf count error: max %d meta %d cnt %d meta %d free %d meta %d delwr %d bdw %d\n", \
238 nfsbufmax, nfsbufmetamax, nfsbufcnt, nfsbufmetacnt, nfsbuffreecnt, nfsbuffreemetacnt, \
239 nfsbufdelwricnt, nfs_nbdwrite); \
240 if (!locked) lck_mtx_unlock(nfs_buf_mutex); \
241 } while (0)
242
243 struct nfs_vattr {
244 enum vtype nva_type; /* vnode type (for create) */
245 u_short nva_mode; /* files access mode and type */
246 dev_t nva_rdev; /* device the special file represents */
247 uid_t nva_uid; /* owner user id */
248 gid_t nva_gid; /* owner group id */
249 uint32_t nva_fsid; /* file system id (dev for now) */
250 uint64_t nva_nlink; /* number of references to file */
251 uint64_t nva_fileid; /* file id */
252 uint64_t nva_size; /* file size in bytes */
253 uint64_t nva_bytes; /* bytes of disk space held by file */
254 uint32_t nva_blocksize; /* blocksize preferred for i/o */
255 struct timespec nva_atime; /* time of last access */
256 struct timespec nva_mtime; /* time of last modification */
257 struct timespec nva_ctime; /* time file changed */
258 };
259
260 /*
261 * The nfsnode is the nfs equivalent to ufs's inode. Any similarity
262 * is purely coincidental.
263 * There is a unique nfsnode allocated for each active file,
264 * each current directory, each mounted-on file, text file, and the root.
265 * An nfsnode is 'named' by its file handle. (nget/nfs_node.c)
266 * If this structure exceeds 256 bytes (it is currently 256 using 4.4BSD-Lite
267 * type definitions), file handles of > 32 bytes should probably be split out
268 * into a separate MALLOC()'d data structure. (Reduce the size of nfsnode.n_fh
269 * by changing the definition in nfsproto.h of NFS_SMALLFH.)
270 * NB: Hopefully the current order of the fields is such that everything will
271 * be well aligned and, therefore, tightly packed.
272 */
273 struct nfsnode {
274 LIST_ENTRY(nfsnode) n_hash; /* Hash chain */
275 u_quad_t n_size; /* Current size of file */
276 struct nfs_vattr n_vattr; /* Vnode attribute cache */
277 time_t n_attrstamp; /* Attr. cache timestamp */
278 u_int32_t n_mode; /* ACCESS mode cache */
279 uid_t n_modeuid; /* credentials having mode */
280 time_t n_modestamp; /* mode cache timestamp */
281 struct timespec n_mtime; /* Prev modify time. */
282 struct timespec n_ncmtime; /* namecache modify time. */
283 u_char *n_fhp; /* NFS File Handle */
284 union {
285 vnode_t n_vp; /* associated vnode */
286 mount_t n_mp; /* associated mount (NINIT) */
287 } n_un0;
288 struct lockf *n_lockf; /* Locking record of file */
289 int n_error; /* Save write error value */
290 union {
291 struct timespec nf_atim; /* Special file times */
292 nfsuint64 nd_cookieverf; /* Cookie verifier (dir only) */
293 } n_un1;
294 union {
295 struct timespec nf_mtim;
296 off_t nd_direof; /* Dir. EOF offset cache */
297 } n_un2;
298 union {
299 struct sillyrename *nf_silly; /* Ptr to silly rename struct */
300 LIST_HEAD(, nfsdmap) nd_cook; /* cookies */
301 } n_un3;
302 short n_fhsize; /* size in bytes, of fh */
303 short n_flag; /* Flag for locking.. */
304 u_char n_fh[NFS_SMALLFH];/* Small File Handle */
305 u_int64_t n_xid; /* last xid to loadattr */
306 struct nfsbuflists n_cleanblkhd; /* clean blocklist head */
307 struct nfsbuflists n_dirtyblkhd; /* dirty blocklist head */
308 int n_needcommitcnt;/* # bufs that need committing */
309 int n_bufiterflags; /* buf iterator flags */
310 };
311
312 #define nfstimespeccmp(tvp, uvp, cmp) \
313 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
314 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
315 ((tvp)->tv_sec cmp (uvp)->tv_sec))
316
317 #define CHECK_NEEDCOMMITCNT(np) \
318 do { \
319 if ((np)->n_needcommitcnt < 0) { \
320 printf("nfs: n_needcommitcnt negative\n"); \
321 (np)->n_needcommitcnt = 0; \
322 } \
323 } while (0)
324
325 #define n_vnode n_un0.n_vp
326 #define n_mount n_un0.n_mp
327 #define n_atim n_un1.nf_atim
328 #define n_mtim n_un2.nf_mtim
329 #define n_sillyrename n_un3.nf_silly
330 #define n_cookieverf n_un1.nd_cookieverf
331 #define n_direofoffset n_un2.nd_direof
332 #define n_cookies n_un3.nd_cook
333
334 /*
335 * Flags for n_flag
336 */
337 #define NFLUSHWANT 0x0001 /* Want wakeup from a flush in prog. */
338 #define NFLUSHINPROG 0x0002 /* Avoid multiple calls to vinvalbuf() */
339 #define NMODIFIED 0x0004 /* Might have a modified buffer in bio */
340 #define NWRITEERR 0x0008 /* Flag write errors so close will know */
341 #define NNEEDINVALIDATE 0x0010 /* need to call vinvalbuf() */
342 #define NNOCACHE 0x0020 /* all bufs are uncached */
343 #define NWRBUSY 0x0040 /* node in write/fsync */
344 #define NACC 0x0100 /* Special file accessed */
345 #define NUPD 0x0200 /* Special file updated */
346 #define NCHG 0x0400 /* Special file times changed */
347 #define NHASHED 0x1000 /* someone wants to lock */
348 #define NINIT 0x2000 /* node is being initialized */
349 #define NWINIT 0x4000 /* someone waiting for init to complete */
350
351 #define NATTRVALID(np) ((np)->n_attrstamp != ~0)
352 #define NATTRINVALIDATE(np) ((np)->n_attrstamp = ~0)
353 #define NMODEVALID(np) ((np)->n_modestamp != ~0)
354 #define NMODEINVALIDATE(np) ((np)->n_modestamp = ~0)
355
356 #define NVALIDBUFS(np) (!LIST_EMPTY(&(np)->n_dirtyblkhd) || \
357 !LIST_EMPTY(&(np)->n_cleanblkhd))
358
359 /*
360 * NFS-specific flags for nfs_vinvalbuf/nfs_flush
361 */
362 #define V_IGNORE_WRITEERR 0x8000
363
364 /*
365 * Flags for nfs_nget()
366 */
367 #define NG_MARKROOT 0x0001 /* mark vnode as root of FS */
368 #define NG_MAKEENTRY 0x0002 /* add name cache entry for vnode */
369
370 /*
371 * Convert between nfsnode pointers and vnode pointers
372 */
373 #define VTONFS(vp) ((struct nfsnode *)vnode_fsnode(vp))
374 #define NFSTOV(np) ((np)->n_vnode)
375
376 /* nfsnode hash table mutex */
377 extern lck_mtx_t *nfs_node_hash_mutex;
378
379 /*
380 * nfsiod structures
381 */
382 extern proc_t nfs_iodwant[NFS_MAXASYNCDAEMON];
383 extern struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
384 extern lck_grp_t *nfs_iod_lck_grp;
385 extern lck_grp_attr_t *nfs_iod_lck_grp_attr;
386 extern lck_attr_t *nfs_iod_lck_attr;
387 extern lck_mtx_t *nfs_iod_mutex;
388
389 #if defined(KERNEL)
390
391 typedef int vnop_t(void *);
392 extern vnop_t **fifo_nfsv2nodeop_p;
393 extern vnop_t **nfsv2_vnodeop_p;
394 extern vnop_t **spec_nfsv2nodeop_p;
395
396 /*
397 * Prototypes for NFS vnode operations
398 */
399 int nfs_write(struct vnop_write_args *);
400 #define nfs_revoke nop_revoke
401 #define nfs_seek ((int (*)(struct vnop_seek_args *))nullop) //XXXdead?
402 int nfs_inactive(struct vnop_inactive_args *);
403 int nfs_reclaim(struct vnop_reclaim_args *);
404
405
406 /* other stuff */
407 int nfs_removeit(struct sillyrename *);
408 int nfs_nget(mount_t,vnode_t,struct componentname *,u_char *,int,struct nfs_vattr *,u_int64_t *,int,struct nfsnode **);
409 nfsuint64 *nfs_getcookie(struct nfsnode *, off_t, int);
410 void nfs_invaldir(vnode_t);
411
412 /* nfsbuf functions */
413 void nfs_nbinit(void);
414 void nfs_buf_remfree(struct nfsbuf *);
415 boolean_t nfs_buf_is_incore(vnode_t, daddr64_t);
416 struct nfsbuf * nfs_buf_incore(vnode_t, daddr64_t);
417 int nfs_buf_get(vnode_t, daddr64_t, int, proc_t, int, struct nfsbuf **);
418 int nfs_buf_upl_setup(struct nfsbuf *bp);
419 void nfs_buf_upl_check(struct nfsbuf *bp);
420 void nfs_buf_release(struct nfsbuf *, int);
421 int nfs_buf_iowait(struct nfsbuf *);
422 void nfs_buf_iodone(struct nfsbuf *);
423 void nfs_buf_write_delayed(struct nfsbuf *, proc_t);
424 void nfs_buf_check_write_verifier(struct nfsnode *, struct nfsbuf *);
425 void nfs_buf_freeup(int);
426 void nfs_buf_refget(struct nfsbuf *bp);
427 void nfs_buf_refrele(struct nfsbuf *bp);
428 void nfs_buf_drop(struct nfsbuf *);
429 errno_t nfs_buf_acquire(struct nfsbuf *, int, int, int);
430 int nfs_buf_iterprepare(struct nfsnode *, struct nfsbuflists *, int);
431 void nfs_buf_itercomplete(struct nfsnode *, struct nfsbuflists *, int);
432
433 #endif /* KERNEL */
434
435 #endif /* __APPLE_API_PRIVATE */
436 #endif /* _NFS_NFSNODE_H_ */