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