]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsnode.h
xnu-1504.3.12.tar.gz
[apple/xnu.git] / bsd / nfs / nfsnode.h
1 /*
2 * Copyright (c) 2000-2009 Apple 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_vnop_inactive()
82 */
83 struct nfs_sillyrename {
84 kauth_cred_t nsr_cred;
85 struct nfsnode *nsr_dnp;
86 int nsr_namlen;
87 char nsr_name[20];
88 };
89
90 /*
91 * The nfsbuf is the nfs equivalent to a struct buf.
92 */
93 struct nfsbuf {
94 LIST_ENTRY(nfsbuf) nb_hash; /* hash chain */
95 LIST_ENTRY(nfsbuf) nb_vnbufs; /* nfsnode's nfsbuf chain */
96 TAILQ_ENTRY(nfsbuf) nb_free; /* free list position if not active. */
97 volatile uint32_t nb_flags; /* NB_* flags. */
98 volatile uint32_t nb_lflags; /* NBL_* flags. */
99 volatile uint32_t nb_refs; /* outstanding references. */
100 uint32_t nb_bufsize; /* buffer size */
101 daddr64_t nb_lblkno; /* logical block number. */
102 uint64_t nb_verf; /* V3 write verifier */
103 int nb_commitlevel; /* lowest write commit level */
104 time_t nb_timestamp; /* buffer timestamp */
105 int nb_error; /* errno value. */
106 u_int32_t nb_valid; /* valid pages in buf */
107 u_int32_t nb_dirty; /* dirty pages in buf */
108 int nb_validoff; /* offset in buffer of valid region. */
109 int nb_validend; /* offset of end of valid region. */
110 int nb_dirtyoff; /* offset in buffer of dirty region. */
111 int nb_dirtyend; /* offset of end of dirty region. */
112 int nb_offio; /* offset in buffer of I/O region. */
113 int nb_endio; /* offset of end of I/O region. */
114 int nb_rpcs; /* Count of RPCs remaining for this buffer. */
115 caddr_t nb_data; /* mapped buffer */
116 nfsnode_t nb_np; /* nfsnode buffer belongs to */
117 kauth_cred_t nb_rcred; /* read credentials reference */
118 kauth_cred_t nb_wcred; /* write credentials reference */
119 void * nb_pagelist; /* upl */
120 };
121
122 #define NFS_MAXBSIZE (32 * PAGE_SIZE) /* valid/dirty page masks limit buffer size */
123
124 #define NFS_A_LOT_OF_NEEDCOMMITS 256 /* max# uncommitted buffers for a node */
125 #define NFS_A_LOT_OF_DELAYED_WRITES MAX(nfsbufcnt/8,512) /* max# "delwri" buffers in system */
126
127 /*
128 * These flags are kept in b_lflags...
129 * nfs_buf_mutex must be held before examining/updating
130 */
131 #define NBL_BUSY 0x00000001 /* I/O in progress. */
132 #define NBL_WANTED 0x00000002 /* Process wants this buffer. */
133
134 /*
135 * These flags are kept in nb_flags and they're (purposefully)
136 * very similar to the B_* flags for struct buf.
137 * nfs_buf_mutex is not needed to examine/update these.
138 */
139 #define NB_STALEWVERF 0x00000001 /* write verifier changed on us */
140 #define NB_NEEDCOMMIT 0x00000002 /* buffer needs to be committed */
141 #define NB_ASYNC 0x00000004 /* Start I/O, do not wait. */
142 #define NB_CACHE 0x00000020 /* buffer data found in the cache */
143 #define NB_STABLE 0x00000040 /* write FILESYNC not UNSTABLE */
144 #define NB_DELWRI 0x00000080 /* delayed write: dirty range needs to be written */
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_INVAL 0x00002000 /* Does not contain valid info. */
149 #define NB_NCRDAHEAD 0x00004000 /* "nocache readahead" data */
150 #define NB_NOCACHE 0x00008000 /* Do not cache block after use. */
151 #define NB_WRITE 0x00000000 /* Write buffer (pseudo flag). */
152 #define NB_READ 0x00100000 /* Read buffer. */
153 #define NB_MULTASYNCRPC 0x00200000 /* multiple async RPCs issued for buffer */
154 #define NB_PAGELIST 0x00400000 /* Buffer describes pagelist I/O. */
155 #define NB_WRITEINPROG 0x01000000 /* Write in progress. */
156 #define NB_META 0x40000000 /* buffer contains meta-data. */
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
194 LIST_HEAD(nfsbuflists, nfsbuf);
195 TAILQ_HEAD(nfsbuffreehead, nfsbuf);
196
197 #define NFSNOLIST ((void*)0xdeadbeef)
198
199 __private_extern__ lck_mtx_t *nfs_buf_mutex;
200 __private_extern__ int nfsbufcnt, nfsbufmin, nfsbufmax, nfsbufmetacnt, nfsbufmetamax;
201 __private_extern__ int nfsbuffreecnt, nfsbuffreemetacnt, nfsbufdelwricnt, nfsneedbuffer;
202 __private_extern__ int nfs_nbdwrite;
203 __private_extern__ struct nfsbuffreehead nfsbuffree, nfsbufdelwri;
204
205 #ifdef NFSBUFDEBUG
206 #define NFSBUFCNTCHK() \
207 do { \
208 if ( (nfsbufcnt < 0) || \
209 (nfsbufcnt > nfsbufmax) || \
210 (nfsbufmetacnt < 0) || \
211 (nfsbufmetacnt > nfsbufmetamax) || \
212 (nfsbufmetacnt > nfsbufcnt) || \
213 (nfsbuffreecnt < 0) || \
214 (nfsbuffreecnt > nfsbufmax) || \
215 (nfsbuffreecnt > nfsbufcnt) || \
216 (nfsbuffreemetacnt < 0) || \
217 (nfsbuffreemetacnt > nfsbufmax) || \
218 (nfsbuffreemetacnt > nfsbufcnt) || \
219 (nfsbuffreemetacnt > nfsbufmetamax) || \
220 (nfsbuffreemetacnt > nfsbufmetacnt) || \
221 (nfsbufdelwricnt < 0) || \
222 (nfsbufdelwricnt > nfsbufmax) || \
223 (nfsbufdelwricnt > nfsbufcnt) || \
224 (nfs_nbdwrite < 0) || \
225 (nfs_nbdwrite > nfsbufcnt) || \
226 0) \
227 panic("nfsbuf count error: max %d meta %d cnt %d meta %d free %d meta %d delwr %d bdw %d\n", \
228 nfsbufmax, nfsbufmetamax, nfsbufcnt, nfsbufmetacnt, nfsbuffreecnt, nfsbuffreemetacnt, \
229 nfsbufdelwricnt, nfs_nbdwrite); \
230 } while (0)
231 #else
232 #define NFSBUFCNTCHK()
233 #endif
234
235 /*
236 * NFS directory buffer
237 *
238 * Each buffer for a directory consists of:
239 *
240 * - a small header
241 * - a packed list of direntry structures
242 * (if RDIRPLUS is enabled, a file handle and attrstamp are
243 * packed after the direntry name.)
244 * - free/unused space
245 * - if RDIRPLUS is enabled, an array of attributes
246 * that is indexed backwards from the end of the buffer.
247 */
248 struct nfs_dir_buf_header {
249 uint16_t ndbh_flags; /* flags (see below) */
250 uint16_t ndbh_count; /* # of entries */
251 uint32_t ndbh_entry_end; /* end offset of direntry data */
252 uint32_t ndbh_ncgen; /* name cache generation# */
253 uint32_t ndbh_pad; /* reserved */
254 };
255 /* ndbh_flags */
256 #define NDB_FULL 0x0001 /* buffer has been filled */
257 #define NDB_EOF 0x0002 /* buffer contains EOF */
258
259 #define NFS_DIR_BUF_FIRST_DIRENTRY(BP) \
260 ((struct direntry*)((char*)((BP)->nb_data) + sizeof(*ndbhp)))
261 #define NFS_DIR_BUF_NVATTR(BP, IDX) \
262 (&((struct nfs_vattr*)((char*)((BP)->nb_data) + (BP)->nb_bufsize))[-((IDX)+1)])
263 #define NFS_DIRENTRY_LEN(namlen) \
264 ((sizeof(struct direntry) + (namlen) - (MAXPATHLEN-1) + 7) & ~7)
265 #define NFS_DIRENT_LEN(namlen) \
266 ((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen) + 1 + 3) &~ 3))
267 #define NFS_DIRENTRY_NEXT(DP) \
268 ((struct direntry*)((char*)(DP) + (DP)->d_reclen))
269 #define NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(C) \
270 ((C) && ((((C) >> 32) == 0) || (((C) & 0x80000000ULL) && (((C) >> 32) == 0xffffffff))))
271 #define NFS_DIR_COOKIE_SAME32(C1, C2) \
272 (((C1) & 0xffffffffULL) == ((C2) & 0xffffffffULL))
273
274 /*
275 * NFS directory cookie cache
276 *
277 * This structure is used to cache cookie-to-buffer mappings for
278 * cookies recently returned from READDIR. The entries are kept in an
279 * array. The most-recently-used (MRU) list is headed by the entry at
280 * index "mru". The index of the next entry in the list is kept in the
281 * "next" array. (An index value of -1 marks an invalid entry.)
282 */
283 #define NFSNUMCOOKIES 14
284 struct nfsdmap {
285 int8_t free; /* next unused slot */
286 int8_t mru; /* head of MRU list */
287 int8_t next[NFSNUMCOOKIES]; /* MRU list links */
288 struct {
289 uint64_t key; /* cookie */
290 uint64_t lbn; /* lbn of buffer */
291 } cookies[NFSNUMCOOKIES]; /* MRU list entries */
292 };
293
294 /*
295 * NFS vnode attribute structure
296 */
297 #define NFSTIME_ACCESS 0 /* time of last access */
298 #define NFSTIME_MODIFY 1 /* time of last modification */
299 #define NFSTIME_CHANGE 2 /* time file changed */
300 #define NFSTIME_CREATE 3 /* time file created */
301 #define NFSTIME_BACKUP 4 /* time of last backup */
302 #define NFSTIME_COUNT 5
303
304 #define NFS_COMPARE_MTIME(TVP, NVAP, CMP) \
305 (((TVP)->tv_sec == (NVAP)->nva_timesec[NFSTIME_MODIFY]) ? \
306 ((TVP)->tv_nsec CMP (NVAP)->nva_timensec[NFSTIME_MODIFY]) : \
307 ((TVP)->tv_sec CMP (NVAP)->nva_timesec[NFSTIME_MODIFY]))
308 #define NFS_COPY_TIME(TVP, NVAP, WHICH) \
309 do { \
310 (TVP)->tv_sec = (NVAP)->nva_timesec[NFSTIME_##WHICH]; \
311 (TVP)->tv_nsec = (NVAP)->nva_timensec[NFSTIME_##WHICH]; \
312 } while (0)
313
314 struct nfs_vattr {
315 enum vtype nva_type; /* vnode type (for create) */
316 uint32_t nva_mode; /* files access mode (and type) */
317 uid_t nva_uid; /* owner user id */
318 gid_t nva_gid; /* owner group id */
319 nfs_specdata nva_rawdev; /* device the special file represents */
320 uint32_t nva_flags; /* file flags */
321 uint32_t nva_maxlink; /* maximum # of links (v4) */
322 uint64_t nva_nlink; /* number of references to file */
323 uint64_t nva_fileid; /* file id */
324 nfs_fsid nva_fsid; /* file system id */
325 uint64_t nva_size; /* file size in bytes */
326 uint64_t nva_bytes; /* bytes of disk space held by file */
327 uint64_t nva_change; /* change attribute */
328 int64_t nva_timesec[NFSTIME_COUNT];
329 int32_t nva_timensec[NFSTIME_COUNT];
330 uint32_t nva_bitmap[NFS_ATTR_BITMAP_LEN]; /* attributes that are valid */
331 };
332
333 #define NFS_FFLAG_ARCHIVED 0x0001
334 #define NFS_FFLAG_HIDDEN 0x0002
335 #define NFS_FFLAG_NAMED_ATTR 0x0004 /* file has named attributes */
336
337 /* flags for nfs_getattr() */
338 #define NGA_CACHED 0
339 #define NGA_UNCACHED 1
340
341 /*
342 * macros for detecting node changes
343 *
344 * These macros help us determine if a file has been changed on the server and
345 * thus whether or not we need to invalidate any cached data.
346 *
347 * For NFSv2/v3, the modification time is used.
348 * For NFSv4, the change attribute is used.
349 */
350 #define NFS_CHANGED(VERS, NP, NVAP) \
351 (((VERS) >= NFS_VER4) ? \
352 ((NP)->n_change != (NVAP)->nva_change) : \
353 NFS_COMPARE_MTIME(&(NP)->n_mtime, (NVAP), !=))
354 #define NFS_CHANGED_NC(VERS, NP, NVAP) \
355 (((VERS) >= NFS_VER4) ? \
356 ((NP)->n_ncchange != (NVAP)->nva_change) : \
357 NFS_COMPARE_MTIME(&(NP)->n_ncmtime, (NVAP), !=))
358 #define NFS_CHANGED_UPDATE(VERS, NP, NVAP) \
359 do { \
360 if ((VERS) >= NFS_VER4) \
361 (NP)->n_change = (NVAP)->nva_change; \
362 else \
363 NFS_COPY_TIME(&(NP)->n_mtime, (NVAP), MODIFY); \
364 } while (0)
365 #define NFS_CHANGED_UPDATE_NC(VERS, NP, NVAP) \
366 do { \
367 if ((VERS) >= NFS_VER4) \
368 (NP)->n_ncchange = (NVAP)->nva_change; \
369 else \
370 NFS_COPY_TIME(&(NP)->n_ncmtime, (NVAP), MODIFY); \
371 } while (0)
372
373
374 __private_extern__ lck_grp_t *nfs_open_grp;
375 __private_extern__ uint32_t nfs_open_owner_seqnum, nfs_lock_owner_seqnum;
376
377 /*
378 * NFSv4 open owner structure - one per cred per mount
379 */
380 struct nfs_open_owner {
381 TAILQ_ENTRY(nfs_open_owner) noo_link; /* List of open owners (on mount) */
382 lck_mtx_t noo_lock; /* owner mutex */
383 struct nfsmount * noo_mount; /* NFS mount */
384 uint32_t noo_refcnt; /* # outstanding references */
385 uint32_t noo_flags; /* see below */
386 kauth_cred_t noo_cred; /* credentials of open owner */
387 uint32_t noo_name; /* unique name used otw */
388 uint32_t noo_seqid; /* client-side sequence ID */
389 TAILQ_HEAD(,nfs_open_file) noo_opens; /* list of open files */
390 };
391 /* noo_flags */
392 #define NFS_OPEN_OWNER_LINK 0x1 /* linked into mount's open owner list */
393 #define NFS_OPEN_OWNER_BUSY 0x2 /* open state-modifying operation in progress */
394 #define NFS_OPEN_OWNER_WANT 0x4 /* someone else wants to mark busy */
395
396 /*
397 * NFS open file structure - one per open owner per nfsnode
398 */
399 struct nfs_open_file {
400 lck_mtx_t nof_lock; /* open file mutex */
401 TAILQ_ENTRY(nfs_open_file) nof_link; /* list of open files */
402 TAILQ_ENTRY(nfs_open_file) nof_oolink; /* list of open owner's open files */
403 struct nfs_open_owner * nof_owner; /* open owner */
404 nfsnode_t nof_np; /* nfsnode this open is for */
405 nfs_stateid nof_stateid; /* open stateid */
406 thread_t nof_creator; /* thread that created file */
407 uint32_t nof_opencnt; /* open file count */
408 uint16_t nof_flags; /* see below */
409 uint8_t nof_access:4; /* access mode for this open */
410 uint8_t nof_deny:4; /* deny mode for this open */
411 uint8_t nof_mmap_access:4; /* mmap open access mode */
412 uint8_t nof_mmap_deny:4; /* mmap open deny mode */
413 /* counts of access/deny mode open combinations */
414 uint32_t nof_r; /* read opens (deny none) */
415 uint32_t nof_w; /* write opens (deny none) */
416 uint32_t nof_rw; /* read/write opens (deny none) */
417 uint32_t nof_r_dw; /* read deny-write opens */
418 /* the rest of the counts have a max of 2 (1 for open + 1 for mmap) */
419 uint32_t nof_w_dw:4; /* write deny-write opens (max 2) */
420 uint32_t nof_rw_dw:4; /* read/write deny-write opens (max 2) */
421 uint32_t nof_r_drw:4; /* read deny-read/write opens (max 2) */
422 uint32_t nof_w_drw:4; /* write deny-read/write opens (max 2) */
423 uint32_t nof_rw_drw:4; /* read/write deny-read/write opens (max 2) */
424 };
425 /* nof_flags */
426 #define NFS_OPEN_FILE_BUSY 0x0001 /* open state-modifying operation in progress */
427 #define NFS_OPEN_FILE_WANT 0x0002 /* someone else wants to mark busy */
428 #define NFS_OPEN_FILE_CREATE 0x0004 /* has an open(RW) from a VNOP_CREATE call */
429 #define NFS_OPEN_FILE_NEEDCLOSE 0x0008 /* has an open(R) from an (unopen) VNOP_READ call */
430 #define NFS_OPEN_FILE_SETATTR 0x0020 /* has an open(W) to perform a SETATTR(size) */
431 #define NFS_OPEN_FILE_POSIXLOCK 0x0040 /* server supports POSIX locking semantics */
432 #define NFS_OPEN_FILE_LOST 0x0080 /* open state has been lost */
433 #define NFS_OPEN_FILE_REOPEN 0x0100 /* file needs to be reopened */
434 #define NFS_OPEN_FILE_REOPENING 0x0200 /* file is being reopened */
435
436 struct nfs_lock_owner;
437 /*
438 * NFS file lock
439 *
440 * Each lock request (pending or granted) has an
441 * nfs_file_lock structure representing its state.
442 */
443 struct nfs_file_lock {
444 TAILQ_ENTRY(nfs_file_lock) nfl_link; /* List of locks on nfsnode */
445 TAILQ_ENTRY(nfs_file_lock) nfl_lolink; /* List of locks held by locker */
446 struct nfs_lock_owner * nfl_owner; /* lock owner that holds this lock */
447 uint64_t nfl_start; /* starting offset */
448 uint64_t nfl_end; /* ending offset (inclusive) */
449 uint32_t nfl_blockcnt; /* # locks blocked on this lock */
450 uint16_t nfl_flags; /* see below */
451 uint8_t nfl_type; /* lock type: read/write */
452 };
453 /* nfl_flags */
454 #define NFS_FILE_LOCK_ALLOC 0x01 /* lock was allocated */
455 #define NFS_FILE_LOCK_STYLE_POSIX 0x02 /* POSIX-style fcntl() lock */
456 #define NFS_FILE_LOCK_STYLE_FLOCK 0x04 /* flock(2)-style lock */
457 #define NFS_FILE_LOCK_STYLE_MASK 0x06 /* lock style mask */
458 #define NFS_FILE_LOCK_WAIT 0x08 /* may block on conflicting locks */
459 #define NFS_FILE_LOCK_BLOCKED 0x10 /* request is blocked */
460 #define NFS_FILE_LOCK_DEAD 0x20 /* lock (request) no longer exists */
461
462 TAILQ_HEAD(nfs_file_lock_queue, nfs_file_lock);
463
464 /*
465 * Calculate length of lock range given the endpoints.
466 * Note that struct flock has "to EOF" reported as 0 but
467 * the NFSv4 protocol has "to EOF" reported as UINT64_MAX.
468 */
469 #define NFS_FLOCK_LENGTH(S, E) (((E) == UINT64_MAX) ? 0 : ((E) - (S) + 1))
470 #define NFS_LOCK_LENGTH(S, E) (((E) == UINT64_MAX) ? UINT64_MAX : ((E) - (S) + 1))
471
472 /*
473 * NFSv4 lock owner structure - per open owner per process per nfsnode
474 *
475 * A lock owner is a process + an nfsnode.
476 *
477 * Note that flock(2) locks technically should have the lock owner be
478 * an fglob pointer instead of a process. However, implementing that
479 * correctly would not be trivial. So, for now, flock(2) locks are
480 * essentially treated like whole-file POSIX locks.
481 */
482 struct nfs_lock_owner {
483 lck_mtx_t nlo_lock; /* owner mutex */
484 TAILQ_ENTRY(nfs_lock_owner) nlo_link; /* List of lock owners (on nfsnode) */
485 struct nfs_open_owner * nlo_open_owner; /* corresponding open owner */
486 struct nfs_file_lock_queue nlo_locks; /* list of locks held */
487 struct nfs_file_lock nlo_alock; /* most lockers will only ever have one */
488 struct timeval nlo_pid_start; /* Start time of process id */
489 pid_t nlo_pid; /* lock-owning process ID */
490 uint32_t nlo_refcnt; /* # outstanding references */
491 uint32_t nlo_flags; /* see below */
492 uint32_t nlo_name; /* unique name used otw */
493 uint32_t nlo_seqid; /* client-side sequence ID */
494 uint32_t nlo_stategenid; /* mount state generation ID */
495 nfs_stateid nlo_stateid; /* lock stateid */
496 };
497 /* nlo_flags */
498 #define NFS_LOCK_OWNER_LINK 0x1 /* linked into mount's lock owner list */
499 #define NFS_LOCK_OWNER_BUSY 0x2 /* lock state-modifying operation in progress */
500 #define NFS_LOCK_OWNER_WANT 0x4 /* someone else wants to mark busy */
501
502 /*
503 * The nfsnode is the NFS equivalent of an inode.
504 * There is a unique nfsnode for each NFS vnode.
505 * An nfsnode is 'named' by its file handle. (nget/nfs_node.c)
506 * NB: Hopefully the current order of the fields is such that everything will
507 * be well aligned and, therefore, tightly packed.
508 */
509
510 #define NFS_ACCESS_CACHE_SIZE 3
511
512 struct nfsnode {
513 lck_mtx_t n_lock; /* nfs node lock */
514 lck_rw_t n_datalock; /* nfs node data lock */
515 void *n_datalockowner;/* nfs node data lock owner (exclusive) */
516 LIST_ENTRY(nfsnode) n_hash; /* Hash chain */
517 u_quad_t n_size; /* Current size of file */
518 u_quad_t n_newsize; /* new size of file (pending update) */
519 u_int64_t n_xid; /* last xid to loadattr */
520 struct nfs_vattr n_vattr; /* Vnode attribute cache */
521 time_t n_attrstamp; /* Attr. cache timestamp */
522 u_int8_t n_mode[NFS_ACCESS_CACHE_SIZE+1]; /* ACCESS mode cache */
523 uid_t n_modeuid[NFS_ACCESS_CACHE_SIZE]; /* credentials having mode */
524 time_t n_modestamp[NFS_ACCESS_CACHE_SIZE]; /* mode cache timestamp */
525 union {
526 struct {
527 struct timespec n3_mtime; /* Prev modify time. */
528 struct timespec n3_ncmtime; /* namecache modify time. */
529 } v3;
530 struct {
531 uint64_t n4_change; /* prev change attribute */
532 uint64_t n4_ncchange; /* namecache change attribute */
533 } v4;
534 } n_un4;
535 vnode_t n_parent; /* this node's parent */
536 u_char *n_fhp; /* NFS File Handle */
537 vnode_t n_vnode; /* associated vnode */
538 mount_t n_mount; /* associated mount (NHINIT) */
539 int n_error; /* Save write error value */
540 union {
541 struct timespec ns_atim; /* Special file times */
542 daddr64_t nf_lastread; /* last block# read from (for readahead) */
543 uint64_t nd_cookieverf; /* Cookie verifier (dir only) */
544 } n_un1;
545 union {
546 struct timespec ns_mtim; /* Special file times */
547 daddr64_t nf_lastrahead; /* last block# read ahead */
548 uint64_t nd_eofcookie; /* Dir. EOF cookie cache */
549 } n_un2;
550 union {
551 struct nfs_sillyrename *nf_silly;/* Ptr to silly rename struct */
552 struct nfsdmap *nd_cookiecache; /* dir cookie cache */
553 } n_un3;
554 u_short n_fhsize; /* size in bytes, of fh */
555 u_short n_flag; /* node flags */
556 u_short n_hflag; /* node hash flags */
557 u_short n_bflag; /* node buffer flags */
558 u_char n_fh[NFS_SMALLFH];/* Small File Handle */
559 struct nfsbuflists n_cleanblkhd; /* clean blocklist head */
560 struct nfsbuflists n_dirtyblkhd; /* dirty blocklist head */
561 union {
562 int nf_wrbusy; /* # threads in write/fsync */
563 uint32_t nd_ncgen; /* dir name cache generation# */
564 } n_un5;
565 union {
566 int nf_needcommitcnt;/* # bufs that need committing */
567 daddr64_t nd_lastdbl; /* last dir buf lookup block# */
568 } n_un6;
569 int n_bufiterflags; /* buf iterator flags */
570 int n_numoutput; /* I/O in progress */
571 /* open state */
572 lck_mtx_t n_openlock; /* nfs node open lock */
573 uint32_t n_openflags; /* open state flags */
574 uint32_t n_openrefcnt; /* # non-file opens */
575 TAILQ_HEAD(,nfs_open_file) n_opens; /* list of open files */
576 /* lock state */
577 TAILQ_HEAD(, nfs_lock_owner) n_lock_owners; /* list of lock owners */
578 struct nfs_file_lock_queue n_locks; /* list of locks */
579 /* delegation state */
580 nfs_stateid n_dstateid; /* delegation stateid */
581 TAILQ_ENTRY(nfsnode) n_dlink; /* delegation recall list link */
582 };
583
584 #define NFS_DATA_LOCK_SHARED 1
585 #define NFS_DATA_LOCK_EXCLUSIVE 2
586
587 #define nfstimespeccmp(tvp, uvp, cmp) \
588 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
589 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
590 ((tvp)->tv_sec cmp (uvp)->tv_sec))
591
592 #define CHECK_NEEDCOMMITCNT(np) \
593 do { \
594 if ((np)->n_needcommitcnt < 0) { \
595 printf("nfs: n_needcommitcnt negative\n"); \
596 (np)->n_needcommitcnt = 0; \
597 } \
598 } while (0)
599
600 #define n_atim n_un1.ns_atim
601 #define n_mtim n_un2.ns_mtim
602 #define n_lastread n_un1.nf_lastread
603 #define n_lastrahead n_un2.nf_lastrahead
604 #define n_sillyrename n_un3.nf_silly
605 #define n_wrbusy n_un5.nf_wrbusy
606 #define n_needcommitcnt n_un6.nf_needcommitcnt
607 #define n_cookieverf n_un1.nd_cookieverf
608 #define n_eofcookie n_un2.nd_eofcookie
609 #define n_cookiecache n_un3.nd_cookiecache
610 #define n_ncgen n_un5.nd_ncgen
611 #define n_lastdbl n_un6.nd_lastdbl
612 #define n_mtime n_un4.v3.n3_mtime
613 #define n_ncmtime n_un4.v3.n3_ncmtime
614 #define n_change n_un4.v4.n4_change
615 #define n_ncchange n_un4.v4.n4_ncchange
616
617 /*
618 * Flags for n_flag
619 */
620 #define NUPDATESIZE 0x0001 /* size of file needs updating */
621 #define NMODIFIED 0x0004 /* Might have a modified buffer in bio */
622 #define NWRITEERR 0x0008 /* Flag write errors so close will know */
623 #define NNEEDINVALIDATE 0x0010 /* need to call vinvalbuf() */
624 #define NGETATTRINPROG 0x0020 /* GETATTR RPC in progress */
625 #define NGETATTRWANT 0x0040 /* waiting for GETATTR RPC */
626 #define NACC 0x0100 /* Special file accessed */
627 #define NUPD 0x0200 /* Special file updated */
628 #define NCHG 0x0400 /* Special file times changed */
629 #define NNEGNCENTRIES 0x0800 /* directory has negative name cache entries */
630 #define NBUSY 0x1000 /* node is busy */
631 #define NBUSYWANT 0x2000 /* waiting on busy node */
632
633 /*
634 * Flags for n_hflag
635 * Note: protected by nfs_node_hash_mutex
636 */
637 #define NHHASHED 0x0001 /* node is in hash table */
638 #define NHINIT 0x0002 /* node is being initialized */
639 #define NHLOCKED 0x0004 /* node is locked (initting or deleting) */
640 #define NHLOCKWANT 0x0008 /* someone wants to lock */
641
642 /*
643 * Flags for n_bflag
644 * Note: protected by nfs_buf_mutex
645 */
646 #define NBFLUSHINPROG 0x0001 /* Avoid multiple calls to nfs_flush() */
647 #define NBFLUSHWANT 0x0002 /* waiting for nfs_flush() to complete */
648 #define NBINVALINPROG 0x0004 /* Avoid multiple calls to nfs_vinvalbuf() */
649 #define NBINVALWANT 0x0008 /* waiting for nfs_vinvalbuf() to complete */
650
651 /*
652 * n_openflags
653 * Note: protected by n_openlock
654 */
655 #define N_OPENBUSY 0x0001 /* open state is busy - being updated */
656 #define N_OPENWANT 0x0002 /* someone wants to mark busy */
657 #define N_DELEG_READ 0x0004 /* we have a read delegation */
658 #define N_DELEG_WRITE 0x0008 /* we have a write delegation */
659 #define N_DELEG_MASK 0x000c /* delegation mask */
660
661 /* attr/mode timestamp macros */
662 #define NATTRVALID(np) ((np)->n_attrstamp != ~0)
663 #define NATTRINVALIDATE(np) ((np)->n_attrstamp = ~0)
664 #define NMODEVALID(np, slot) (((slot) >= 0) && ((slot) < 3) && ((np)->n_modestamp[(slot)] != ~0))
665 #define NMODEINVALIDATE(np) \
666 do { \
667 (np)->n_modestamp[0] = ~0; \
668 (np)->n_modestamp[1] = ~0; \
669 (np)->n_modestamp[2] = ~0; \
670 (np)->n_mode[3] = 0; \
671 } while (0)
672
673 /*
674 * NFS-specific flags for nfs_vinvalbuf/nfs_flush
675 */
676 #define V_IGNORE_WRITEERR 0x8000
677
678 /*
679 * Flags for nfs_nget()
680 */
681 #define NG_MARKROOT 0x0001 /* mark vnode as root of FS */
682 #define NG_MAKEENTRY 0x0002 /* add name cache entry for vnode */
683 #define NG_NOCREATE 0x0004 /* don't create a new node, return existing one */
684
685 /*
686 * Convert between nfsnode pointers and vnode pointers
687 */
688 #define VTONFS(vp) ((nfsnode_t)vnode_fsnode(vp))
689 #define NFSTOV(np) ((np)->n_vnode)
690
691 /* nfsnode hash table mutex */
692 __private_extern__ lck_mtx_t *nfs_node_hash_mutex;
693
694 /*
695 * nfsiod structures
696 */
697 struct nfsiod {
698 TAILQ_ENTRY(nfsiod) niod_link; /* List of nfsiods */
699 struct nfsmount * niod_nmp; /* mount point for this nfsiod */
700 };
701 TAILQ_HEAD(nfsiodlist, nfsiod);
702 TAILQ_HEAD(nfsiodmountlist, nfsmount);
703 __private_extern__ struct nfsiodlist nfsiodfree, nfsiodwork;
704 __private_extern__ struct nfsiodmountlist nfsiodmounts;
705 __private_extern__ lck_mtx_t *nfsiod_mutex;
706
707 #if defined(KERNEL)
708
709 typedef int vnop_t(void *);
710 extern vnop_t **fifo_nfsv2nodeop_p;
711 extern vnop_t **nfsv2_vnodeop_p;
712 extern vnop_t **spec_nfsv2nodeop_p;
713 extern vnop_t **fifo_nfsv4nodeop_p;
714 extern vnop_t **nfsv4_vnodeop_p;
715 extern vnop_t **spec_nfsv4nodeop_p;
716
717 /*
718 * Prototypes for NFS vnode operations
719 */
720 #define nfs_vnop_revoke nop_revoke
721 int nfs_vnop_inactive(struct vnop_inactive_args *);
722 int nfs_vnop_reclaim(struct vnop_reclaim_args *);
723
724 int nfs_node_lock(nfsnode_t);
725 int nfs_node_lock_internal(nfsnode_t, int);
726 void nfs_node_lock_force(nfsnode_t);
727 void nfs_node_unlock(nfsnode_t);
728 int nfs_node_lock2(nfsnode_t, nfsnode_t);
729 void nfs_node_unlock2(nfsnode_t, nfsnode_t);
730 int nfs_node_set_busy(nfsnode_t, thread_t);
731 int nfs_node_set_busy2(nfsnode_t, nfsnode_t, thread_t);
732 int nfs_node_set_busy4(nfsnode_t, nfsnode_t, nfsnode_t, nfsnode_t, thread_t);
733 void nfs_node_clear_busy(nfsnode_t);
734 void nfs_node_clear_busy2(nfsnode_t, nfsnode_t);
735 void nfs_node_clear_busy4(nfsnode_t, nfsnode_t, nfsnode_t, nfsnode_t);
736 void nfs_data_lock(nfsnode_t, int);
737 void nfs_data_lock_noupdate(nfsnode_t, int);
738 void nfs_data_lock_internal(nfsnode_t, int, int);
739 void nfs_data_unlock(nfsnode_t);
740 void nfs_data_unlock_noupdate(nfsnode_t);
741 void nfs_data_unlock_internal(nfsnode_t, int);
742 void nfs_data_update_size(nfsnode_t, int);
743
744 /* other stuff */
745 int nfs_removeit(struct nfs_sillyrename *);
746 int nfs_nget(mount_t,nfsnode_t,struct componentname *,u_char *,int,struct nfs_vattr *,u_int64_t *,int,nfsnode_t*);
747 void nfs_dir_cookie_cache(nfsnode_t, uint64_t, uint64_t);
748 int nfs_dir_cookie_to_lbn(nfsnode_t, uint64_t, int *, uint64_t *);
749 void nfs_invaldir(nfsnode_t);
750 uint32_t nfs_dir_buf_freespace(struct nfsbuf *, int);
751
752 /* nfsbuf functions */
753 void nfs_nbinit(void);
754 void nfs_buf_timer(void *, void *);
755 void nfs_buf_remfree(struct nfsbuf *);
756 boolean_t nfs_buf_is_incore(nfsnode_t, daddr64_t);
757 struct nfsbuf * nfs_buf_incore(nfsnode_t, daddr64_t);
758 int nfs_buf_get(nfsnode_t, daddr64_t, uint32_t, thread_t, int, struct nfsbuf **);
759 int nfs_buf_upl_setup(struct nfsbuf *bp);
760 void nfs_buf_upl_check(struct nfsbuf *bp);
761 void nfs_buf_normalize_valid_range(nfsnode_t, struct nfsbuf *);
762 int nfs_buf_map(struct nfsbuf *);
763 void nfs_buf_release(struct nfsbuf *, int);
764 int nfs_buf_iowait(struct nfsbuf *);
765 void nfs_buf_iodone(struct nfsbuf *);
766 void nfs_buf_write_delayed(struct nfsbuf *);
767 void nfs_buf_check_write_verifier(nfsnode_t, struct nfsbuf *);
768 void nfs_buf_freeup(int);
769 void nfs_buf_refget(struct nfsbuf *bp);
770 void nfs_buf_refrele(struct nfsbuf *bp);
771 void nfs_buf_drop(struct nfsbuf *);
772 errno_t nfs_buf_acquire(struct nfsbuf *, int, int, int);
773 int nfs_buf_iterprepare(nfsnode_t, struct nfsbuflists *, int);
774 void nfs_buf_itercomplete(nfsnode_t, struct nfsbuflists *, int);
775
776 int nfs_bioread(nfsnode_t, uio_t, int, vfs_context_t);
777 int nfs_buf_readahead(nfsnode_t, int, daddr64_t *, daddr64_t, thread_t, kauth_cred_t);
778 int nfs_buf_readdir(struct nfsbuf *, vfs_context_t);
779 int nfs_buf_read(struct nfsbuf *);
780 void nfs_buf_read_finish(struct nfsbuf *);
781 int nfs_buf_read_rpc(struct nfsbuf *, thread_t, kauth_cred_t);
782 void nfs_buf_read_rpc_finish(struct nfsreq *);
783 int nfs_buf_write(struct nfsbuf *);
784 void nfs_buf_write_finish(struct nfsbuf *, thread_t, kauth_cred_t);
785 int nfs_buf_write_rpc(struct nfsbuf *, int, thread_t, kauth_cred_t);
786 void nfs_buf_write_rpc_finish(struct nfsreq *);
787 int nfs_buf_write_dirty_pages(struct nfsbuf *, thread_t, kauth_cred_t);
788
789 int nfs_flushcommits(nfsnode_t, int);
790 int nfs_flush(nfsnode_t, int, thread_t, int);
791 void nfs_buf_delwri_push(int);
792 void nfs_buf_delwri_service(void);
793 void nfs_buf_delwri_thread(void *, wait_result_t);;
794
795 int nfsiod_start(void);
796 void nfsiod_terminate(struct nfsiod *);
797 void nfsiod_thread(void);
798 int nfsiod_continue(int);
799 void nfs_asyncio_finish(struct nfsreq *);
800 void nfs_asyncio_resend(struct nfsreq *);
801 int nfs_async_write_start(struct nfsmount *);
802 void nfs_async_write_done(struct nfsmount *);
803
804 #endif /* KERNEL */
805
806 #endif /* __APPLE_API_PRIVATE */
807 #endif /* _NFS_NFSNODE_H_ */