Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
/*
 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Rick Macklem at The University of Guelph.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)nfsnode.h	8.9 (Berkeley) 5/14/95
 * FreeBSD-Id: nfsnode.h,v 1.24 1997/10/28 14:06:25 bde Exp $
 */


#ifndef _NFS_NFSNODE_H_
#define _NFS_NFSNODE_H_

#include <sys/appleapiopts.h>

#ifdef __APPLE_API_PRIVATE
#ifndef _NFS_NFS_H_
#include <nfs/nfs.h>
#endif
#include <sys/kauth.h>
#include <os/refcnt.h>

/*
 * Silly rename structure that hangs off the nfsnode until the name
 * can be removed by nfs_vnop_inactive()
 */
struct nfs_sillyrename {
	kauth_cred_t    nsr_cred;
	struct nfsnode  *nsr_dnp;
	int             nsr_namlen;
	char            nsr_name[20];
};

/*
 * NFS buf pages struct
 */
typedef struct {
	uint64_t               pages[8];
} nfsbufpgs;

/*
 * The nfsbuf is the nfs equivalent to a struct buf.
 */
struct nfsbuf {
	LIST_ENTRY(nfsbuf)      nb_hash;        /* hash chain */
	LIST_ENTRY(nfsbuf)      nb_vnbufs;      /* nfsnode's nfsbuf chain */
	TAILQ_ENTRY(nfsbuf)     nb_free;        /* free list position if not active. */
	os_refcnt_t             nb_refs;        /* outstanding references. */
	daddr64_t               nb_lblkno;      /* logical block number. */
	uint64_t                nb_verf;        /* V3 write verifier */
	time_t                  nb_timestamp;   /* buffer timestamp */
	nfsbufpgs               nb_valid;       /* valid pages in buf */
	nfsbufpgs               nb_dirty;       /* dirty pages in buf */
	caddr_t                 nb_data;        /* mapped buffer */
	nfsnode_t               nb_np;          /* nfsnode buffer belongs to */
	kauth_cred_t            nb_rcred;       /* read credentials reference */
	kauth_cred_t            nb_wcred;       /* write credentials reference */
	void *                  nb_pagelist;    /* upl */
	volatile uint32_t       nb_flags;       /* NB_* flags. */
	volatile uint32_t       nb_lflags;      /* NBL_* flags. */
	uint32_t                nb_bufsize;     /* buffer size */
	int                     nb_error;       /* errno value. */
	int                     nb_commitlevel; /* lowest write commit level */
	off_t                   nb_validoff;    /* offset in buffer of valid region. */
	off_t                   nb_validend;    /* offset of end of valid region. */
	off_t                   nb_dirtyoff;    /* offset in buffer of dirty region. */
	off_t                   nb_dirtyend;    /* offset of end of dirty region. */
	off_t                   nb_offio;       /* offset in buffer of I/O region. */
	off_t                   nb_endio;       /* offset of end of I/O region. */
	uint64_t                nb_rpcs;        /* Count of RPCs remaining for this buffer. */
};

#define NFS_MAXBSIZE    (8 * 64 * PAGE_SIZE)    /* valid/dirty page masks limit buffer size */

#define NFS_A_LOT_OF_NEEDCOMMITS        256                     /* max# uncommitted buffers for a node */
#define NFS_A_LOT_OF_DELAYED_WRITES     MAX(nfsbufcnt/8,512)    /* max# "delwri" buffers in system */

/*
 * These flags are kept in b_lflags...
 * nfs_buf_mutex must be held before examining/updating
 */
#define NBL_BUSY        0x00000001      /* I/O in progress. */
#define NBL_WANTED      0x00000002      /* Process wants this buffer. */

/*
 * These flags are kept in nb_flags and they're (purposefully)
 * very similar to the B_* flags for struct buf.
 * nfs_buf_mutex is not needed to examine/update these.
 */
#define NB_STALEWVERF   0x00000001      /* write verifier changed on us */
#define NB_NEEDCOMMIT   0x00000002      /* buffer needs to be committed */
#define NB_ASYNC        0x00000004      /* Start I/O, do not wait. */
#define NB_CACHE        0x00000020      /* buffer data found in the cache */
#define NB_STABLE       0x00000040      /* write FILESYNC not UNSTABLE */
#define NB_DELWRI       0x00000080      /* delayed write: dirty range needs to be written */
#define NB_DONE         0x00000200      /* I/O completed. */
#define NB_EINTR        0x00000400      /* I/O was interrupted */
#define NB_ERROR        0x00000800      /* I/O error occurred. */
#define NB_INVAL        0x00002000      /* Does not contain valid info. */
#define NB_NCRDAHEAD    0x00004000      /* "nocache readahead" data */
#define NB_NOCACHE      0x00008000      /* Do not cache block after use. */
#define NB_WRITE        0x00000000      /* Write buffer (pseudo flag). */
#define NB_READ         0x00100000      /* Read buffer. */
#define NB_MULTASYNCRPC 0x00200000      /* multiple async RPCs issued for buffer */
#define NB_PAGELIST     0x00400000      /* Buffer describes pagelist I/O. */
#define NB_WRITEINPROG  0x01000000      /* Write in progress. */
#define NB_META         0x40000000      /* buffer contains meta-data. */

/* Flags for operation type in nfs_buf_get() */
#define NBLK_READ       0x00000001      /* buffer for read */
#define NBLK_WRITE      0x00000002      /* buffer for write */
#define NBLK_META       0x00000004      /* buffer for metadata */
#define NBLK_OPMASK     0x00000007      /* operation mask */
/* modifiers for above flags... */
#define NBLK_NOWAIT     0x40000000      /* don't wait on busy buffer */
#define NBLK_ONLYVALID  0x80000000      /* only return cached buffer */

/* These flags are used for nfsbuf iterating */
#define NBI_ITER                0x01    /* iteration in progress */
#define NBI_ITERWANT            0x02    /* waiting to iterate */
#define NBI_CLEAN               0x04    /* requesting clean buffers */
#define NBI_DIRTY               0x08    /* requesting dirty buffers */
#define NBI_NOWAIT              0x10    /* don't block on NBI_ITER */

/* Flags for nfs_buf_acquire */
#define NBAC_NOWAIT             0x01    /* Don't wait if buffer is busy */
#define NBAC_REMOVE             0x02    /* Remove from free list once buffer is acquired */

/* macros for nfsbufpgs */
#define NBPGS_STRUCT_SIZE               sizeof(nfsbufpgs)
#define NBPGS_ELEMENT_SIZE              sizeof(((nfsbufpgs *)0)->pages[0])
#define NBPGS_ELEMENT_PAGES             (8 * NBPGS_ELEMENT_SIZE)
#define NBPGS_ELEMENTS                  (NBPGS_STRUCT_SIZE / NBPGS_ELEMENT_SIZE)
#define NBPGS_ERASE(NFSBP)              bzero((NFSBP), NBPGS_STRUCT_SIZE)
#define NBPGS_COPY(NFSBPDST, NFSBPSRC)  memcpy((NFSBPDST), (NFSBPSRC), NBPGS_STRUCT_SIZE)
#define NBPGS_IS_EQUAL(NFSBP1, NFSBP2)  (memcmp((NFSBP1), (NFSBP2), NBPGS_STRUCT_SIZE) == 0)
#define NBPGS_GET(NFSBP, P)             ((NFSBP)->pages[((P)/NBPGS_ELEMENT_PAGES)] & (1LLU << ((P) % NBPGS_ELEMENT_PAGES)))
#define NBPGS_SET(NFSBP, P)             ((NFSBP)->pages[((P)/NBPGS_ELEMENT_PAGES)] |= (1LLU << ((P) % NBPGS_ELEMENT_PAGES)))
#define NBPGS_UNSET(NFSBP, P)           ((NFSBP)->pages[((P)/NBPGS_ELEMENT_PAGES)] &= ~(1LLU << ((P) % NBPGS_ELEMENT_PAGES)))

/* some convenience macros...  */
#define NBOFF(BP)                       ((off_t)(BP)->nb_lblkno * (off_t)(BP)->nb_bufsize)
#define NBPGVALID(BP, P)                NBPGS_GET(&((BP)->nb_valid), (P))
#define NBPGDIRTY(BP, P)                NBPGS_GET(&((BP)->nb_dirty), (P))
#define NBPGVALID_SET(BP, P)            NBPGS_SET(&((BP)->nb_valid), (P))
#define NBPGDIRTY_SET(BP, P)            NBPGS_SET(&((BP)->nb_dirty), (P))

#define NBUFSTAMPVALID(BP)              ((BP)->nb_timestamp != ~0)
#define NBUFSTAMPINVALIDATE(BP)         ((BP)->nb_timestamp = ~0)

#define NFS_BUF_MAP(BP) \
	do { \
	        if (!(BP)->nb_data && nfs_buf_map(BP)) \
	                panic("nfs_buf_map failed"); \
	} while (0)

LIST_HEAD(nfsbuflists, nfsbuf);
TAILQ_HEAD(nfsbuffreehead, nfsbuf);

extern lck_mtx_t nfs_buf_mutex;
extern int nfsbufcnt, nfsbufmin, nfsbufmax, nfsbufmetacnt, nfsbufmetamax;
extern int nfsbuffreecnt, nfsbuffreemetacnt, nfsbufdelwricnt, nfsneedbuffer;
extern int nfs_nbdwrite;
extern struct nfsbuffreehead nfsbuffree, nfsbufdelwri;

#ifdef NFSBUFDEBUG
#define NFSBUFCNTCHK() \
	do { \
	if (	(nfsbufcnt < 0) || \
	        (nfsbufcnt > nfsbufmax) || \
	        (nfsbufmetacnt < 0) || \
	        (nfsbufmetacnt > nfsbufmetamax) || \
	        (nfsbufmetacnt > nfsbufcnt) || \
	        (nfsbuffreecnt < 0) || \
	        (nfsbuffreecnt > nfsbufmax) || \
	        (nfsbuffreecnt > nfsbufcnt) || \
	        (nfsbuffreemetacnt < 0) || \
	        (nfsbuffreemetacnt > nfsbufmax) || \
	        (nfsbuffreemetacnt > nfsbufcnt) || \
	        (nfsbuffreemetacnt > nfsbufmetamax) || \
	        (nfsbuffreemetacnt > nfsbufmetacnt) || \
	        (nfsbufdelwricnt < 0) || \
	        (nfsbufdelwricnt > nfsbufmax) || \
	        (nfsbufdelwricnt > nfsbufcnt) || \
	        (nfs_nbdwrite < 0) || \
	        (nfs_nbdwrite > nfsbufcnt) || \
	        0) \
	        panic("nfsbuf count error: max %d meta %d cnt %d meta %d free %d meta %d delwr %d bdw %d\n", \
	                nfsbufmax, nfsbufmetamax, nfsbufcnt, nfsbufmetacnt, nfsbuffreecnt, nfsbuffreemetacnt, \
	                nfsbufdelwricnt, nfs_nbdwrite); \
	} while (0)
#else
#define NFSBUFCNTCHK()
#endif

/*
 * NFS directory buffer
 *
 * Each buffer for a directory consists of:
 *
 * - a small header
 * - a packed list of direntry structures
 *   (if RDIRPLUS is enabled, a file handle and attrstamp are
 *   packed after the direntry name.)
 * - free/unused space
 * - if RDIRPLUS is enabled, an array of attributes
 *   that is indexed backwards from the end of the buffer.
 */
struct nfs_dir_buf_header {
	uint16_t        ndbh_flags;     /* flags (see below) */
	uint16_t        ndbh_count;     /* # of entries */
	off_t           ndbh_entry_end; /* end offset of direntry data */
	uint32_t        ndbh_ncgen;     /* name cache generation# */
	uint32_t        ndbh_pad;       /* reserved */
};
/* ndbh_flags */
#define NDB_FULL        0x0001  /* buffer has been filled */
#define NDB_EOF         0x0002  /* buffer contains EOF */
#define NDB_PLUS        0x0004  /* buffer contains RDIRPLUS data */

#define NFS_DIR_BUF_FIRST_DIRENTRY(BP) \
	((struct direntry*)((char*)((BP)->nb_data) + sizeof(*ndbhp)))
#define NFS_DIR_BUF_NVATTR(BP, IDX) \
	(&((struct nfs_vattr*)((char*)((BP)->nb_data) + (BP)->nb_bufsize))[-((IDX)+1)])
#define NFS_DIRENTRY_LEN(namlen) \
	((sizeof(struct direntry) + (namlen) - (MAXPATHLEN-1) + 7) & ~7)
#define NFS_DIRENTRY_LEN_16(namlen) \
	        (uint16_t)(NFS_DIRENTRY_LEN(namlen)); assert((namlen) <= UINT16_MAX);
#define NFS_DIRENT_LEN(namlen) \
	((sizeof(struct dirent) - (NAME_MAX+1)) + (((namlen) + 1 + 3) &~ 3))
#define NFS_DIRENTRY_NEXT(DP) \
	((struct direntry*)((char*)(DP) + (DP)->d_reclen))
#define NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(C) \
	((C) && ((((C) >> 32) == 0) || (((C) & 0x80000000ULL) && (((C) >> 32) == 0xffffffff))))
#define NFS_DIR_COOKIE_SAME32(C1, C2) \
	(((C1) & 0xffffffffULL) == ((C2) & 0xffffffffULL))

/*
 * NFS directory cookie cache
 *
 * This structure is used to cache cookie-to-buffer mappings for
 * cookies recently returned from READDIR.  The entries are kept in an
 * array.  The most-recently-used (MRU) list is headed by the entry at
 * index "mru".  The index of the next entry in the list is kept in the
 * "next" array.  (An index value of -1 marks an invalid entry.)
 */
#define NFSNUMCOOKIES           14
struct nfsdmap {
	int8_t          free;                   /* next unused slot */
	int8_t          mru;                    /* head of MRU list */
	int8_t          next[NFSNUMCOOKIES];    /* MRU list links */
	struct {
		uint64_t    key;                /* cookie */
		uint64_t    lbn;                /* lbn of buffer */
	} cookies[NFSNUMCOOKIES];               /* MRU list entries */
};

/*
 * NFS vnode attribute structure
 */
#define NFSTIME_ACCESS  0       /* time of last access */
#define NFSTIME_MODIFY  1       /* time of last modification */
#define NFSTIME_CHANGE  2       /* time file changed */
#define NFSTIME_CREATE  3       /* time file created */
#define NFSTIME_BACKUP  4       /* time of last backup */
#define NFSTIME_COUNT   6

#define NFS_COMPARE_MTIME(TVP, NVAP, CMP) \
	(((TVP)->tv_sec == (NVAP)->nva_timesec[NFSTIME_MODIFY]) ?       \
	 ((TVP)->tv_nsec CMP (NVAP)->nva_timensec[NFSTIME_MODIFY]) :    \
	 ((TVP)->tv_sec CMP (NVAP)->nva_timesec[NFSTIME_MODIFY]))
#define NFS_COPY_TIME(TVP, NVAP, WHICH) \
	do { \
	(TVP)->tv_sec = (NVAP)->nva_timesec[NFSTIME_##WHICH]; \
	(TVP)->tv_nsec = (NVAP)->nva_timensec[NFSTIME_##WHICH]; \
	} while (0)

struct nfs_vattr {
	enum vtype      nva_type;       /* vnode type (for create) */
	mode_t          nva_mode;       /* file's access mode (and type) */
	uid_t           nva_uid;        /* owner user id */
	gid_t           nva_gid;        /* owner group id */
	guid_t          nva_uuuid;      /* owner user UUID */
	guid_t          nva_guuid;      /* owner group UUID */
	kauth_acl_t     nva_acl;        /* access control list */
	nfs_specdata    nva_rawdev;     /* device the special file represents */
	uint32_t        nva_flags;      /* file flags (see below) */
	uint32_t        nva_maxlink;    /* maximum # of links (v4) */
	uint64_t        nva_nlink;      /* number of references to file */
	uint64_t        nva_fileid;     /* file id */
	nfs_fsid        nva_fsid;       /* file system id */
	uint64_t        nva_size;       /* file size in bytes */
	uint64_t        nva_bytes;      /* bytes of disk space held by file */
	uint64_t        nva_change;     /* change attribute */
	time_t          nva_timesec[NFSTIME_COUNT];
	long            nva_timensec[NFSTIME_COUNT];
	uint32_t        nva_bitmap[NFS_ATTR_BITMAP_LEN]; /* attributes that are valid */
};

/* nva_flags */
#define NFS_FFLAG_ARCHIVED              0x0001
#define NFS_FFLAG_HIDDEN                0x0002
#define NFS_FFLAG_HAS_NAMED_ATTRS       0x0004  /* file has named attributes */
#define NFS_FFLAG_TRIGGER               0x0008  /* node is a trigger/mirror mount point */
#define NFS_FFLAG_TRIGGER_REFERRAL      0x0010  /* trigger is a referral */
#define NFS_FFLAG_PARTIAL_WRITE         0x0020  /* partial attribute for NFSv4 writes */
#define NFS_FFLAG_FILEID_CONTAINS_XID   0x0040  /* xid might be stashed in nva_fileid is rdirplus is enabled */
#define NFS_FFLAG_IS_ATTR               0x8000  /* file is a named attribute file/directory */

/* flags for nfs_getattr() */
#define NGA_CACHED      0x0001  /* use cached attributes (if still valid) */
#define NGA_UNCACHED    0x0002  /* fetch new attributes */
#define NGA_ACL         0x0004  /* fetch ACL */
#define NGA_MONITOR     0x0008  /* vnode monitor attr update poll */
#define NGA_SOFT        0x0010  /* use cached attributes if ETIMEOUT */

/* macros for initting/cleaning up nfs_vattr structures */
#define NVATTR_INIT(NVAP) \
	do { \
	        NFS_CLEAR_ATTRIBUTES((NVAP)->nva_bitmap); \
	        (NVAP)->nva_flags = 0; \
	        (NVAP)->nva_acl = NULL; \
	} while (0)
#define NVATTR_CLEANUP(NVAP) \
	do { \
	        NFS_CLEAR_ATTRIBUTES((NVAP)->nva_bitmap); \
	        if ((NVAP)->nva_acl) { \
	                kauth_acl_free((NVAP)->nva_acl); \
	                (NVAP)->nva_acl = NULL; \
	        } \
	} while (0)

/*
 * macros for detecting node changes
 *
 * These macros help us determine if a file has been changed on the server and
 * thus whether or not we need to invalidate any cached data.
 *
 * For NFSv2/v3, the modification time is used.
 * For NFSv4, the change attribute is used.
 */
#define NFS_CHANGED(VERS, NP, NVAP) \
	        (((VERS) >= NFS_VER4) ? \
	                ((NP)->n_change != (NVAP)->nva_change) : \
	                NFS_COMPARE_MTIME(&(NP)->n_mtime, (NVAP), !=))
#define NFS_CHANGED_NC(VERS, NP, NVAP) \
	        (((VERS) >= NFS_VER4) ? \
	                ((NP)->n_ncchange != (NVAP)->nva_change) : \
	                NFS_COMPARE_MTIME(&(NP)->n_ncmtime, (NVAP), !=))
#define NFS_CHANGED_UPDATE(VERS, NP, NVAP) \
	do { \
	        if ((VERS) >= NFS_VER4) \
	                (NP)->n_change = (NVAP)->nva_change; \
	        else \
	                NFS_COPY_TIME(&(NP)->n_mtime, (NVAP), MODIFY); \
	} while (0)
#define NFS_CHANGED_UPDATE_NC(VERS, NP, NVAP) \
	do { \
	        if ((VERS) >= NFS_VER4) \
	                (NP)->n_ncchange = (NVAP)->nva_change; \
	        else \
	                NFS_COPY_TIME(&(NP)->n_ncmtime, (NVAP), MODIFY); \
	} while (0)


extern lck_grp_t nfs_open_grp;
extern uint32_t nfs_open_owner_seqnum, nfs_lock_owner_seqnum;

/*
 * NFSv4 open owner structure - one per cred per mount
 */
struct nfs_open_owner {
	TAILQ_ENTRY(nfs_open_owner)     noo_link;       /* List of open owners (on mount) */
	lck_mtx_t                       noo_lock;       /* owner mutex */
	struct nfsmount *               noo_mount;      /* NFS mount */
	os_refcnt_t                     noo_refcnt;     /* # outstanding references */
	uint32_t                        noo_flags;      /* see below */
	kauth_cred_t                    noo_cred;       /* credentials of open owner */
	pid_t                           noo_pid;        /* process pid of this owner */
	uint32_t                        noo_name;       /* unique name used otw */
	uint32_t                        noo_seqid;      /* client-side sequence ID */
	TAILQ_HEAD(, nfs_open_file)     noo_opens;      /* list of open files */
};
/* noo_flags */
#define NFS_OPEN_OWNER_LINK     0x1     /* linked into mount's open owner list */
#define NFS_OPEN_OWNER_BUSY     0x2     /* open state-modifying operation in progress */
#define NFS_OPEN_OWNER_WANT     0x4     /* someone else wants to mark busy */

/*
 * NFS open file structure - one per open owner per nfsnode
 */
struct nfs_open_file {
	lck_mtx_t                       nof_lock;               /* open file mutex */
	TAILQ_ENTRY(nfs_open_file)      nof_link;               /* list of open files */
	TAILQ_ENTRY(nfs_open_file)      nof_oolink;             /* list of open owner's open files */
	struct nfs_open_owner *         nof_owner;              /* open owner */
	nfsnode_t                       nof_np;                 /* nfsnode this open is for */
	nfs_stateid                     nof_stateid;            /* open stateid */
	thread_t                        nof_creator;            /* thread that created file */
	uint32_t                        nof_opencnt;            /* open file count */
	uint16_t                        nof_flags;              /* see below */
	uint8_t                         nof_access:4;           /* access mode for this open */
	uint8_t                         nof_deny:4;             /* deny mode for this open */
	uint8_t                         nof_mmap_access:4;      /* mmap open access mode */
	uint8_t                         nof_mmap_deny:4;        /* mmap open deny mode */
	/* counts of access/deny mode open combinations */
	uint32_t                        nof_r;                  /* read opens (deny none) */
	uint32_t                        nof_w;                  /* write opens (deny none) */
	uint32_t                        nof_rw;                 /* read/write opens (deny none) */
	uint32_t                        nof_r_dw;               /* read deny-write opens */
	/* the rest of the counts have a max of 2 (1 for open + 1 for mmap) */
	uint32_t                        nof_w_dw:2;             /* write deny-write opens (max 2) */
	uint32_t                        nof_rw_dw:2;            /* read/write deny-write opens (max 2) */
	uint32_t                        nof_r_drw:2;            /* read deny-read/write opens (max 2) */
	uint32_t                        nof_w_drw:2;            /* write deny-read/write opens (max 2) */
	uint32_t                        nof_rw_drw:2;           /* read/write deny-read/write opens (max 2) */
	/* counts of DELEGATED access/deny mode open combinations */
	uint32_t                        nof_d_w_dw:2;           /* write deny-write opens (max 2) */
	uint32_t                        nof_d_rw_dw:2;          /* read/write deny-write opens (max 2) */
	uint32_t                        nof_d_r_drw:2;          /* read deny-read/write opens (max 2) */
	uint32_t                        nof_d_w_drw:2;          /* write deny-read/write opens (max 2) */
	uint32_t                        nof_d_rw_drw:2;         /* read/write deny-read/write opens (max 2) */
	uint32_t                        nof_d_r;                /* read opens (deny none) */
	uint32_t                        nof_d_w;                /* write opens (deny none) */
	uint32_t                        nof_d_rw;               /* read/write opens (deny none) */
	uint32_t                        nof_d_r_dw;             /* read deny-write opens */
};
/* nof_flags */
#define NFS_OPEN_FILE_BUSY      0x0001  /* open state-modifying operation in progress */
#define NFS_OPEN_FILE_WANT      0x0002  /* someone else wants to mark busy */
#define NFS_OPEN_FILE_CREATE    0x0004  /* has an open(RW) from a "CREATE" call */
#define NFS_OPEN_FILE_NEEDCLOSE 0x0008  /* has an open(R) from an (unopen) VNOP_READ or VNOP_MMAP call */
#define NFS_OPEN_FILE_SETATTR   0x0020  /* has an open(W) to perform a SETATTR(size) */
#define NFS_OPEN_FILE_POSIXLOCK 0x0040  /* server supports POSIX locking semantics */
#define NFS_OPEN_FILE_LOST      0x0080  /* open state has been lost */
#define NFS_OPEN_FILE_REOPEN    0x0100  /* file needs to be reopened */
#define NFS_OPEN_FILE_REOPENING 0x0200  /* file is being reopened */

struct nfs_lock_owner;
/*
 * NFS file lock
 *
 * Each lock request (pending or granted) has an
 * nfs_file_lock structure representing its state.
 */
struct nfs_file_lock {
	TAILQ_ENTRY(nfs_file_lock)      nfl_link;       /* List of locks on nfsnode */
	TAILQ_ENTRY(nfs_file_lock)      nfl_lolink;     /* List of locks held by locker */
	struct nfs_lock_owner *         nfl_owner;      /* lock owner that holds this lock */
	uint64_t                        nfl_start;      /* starting offset */
	uint64_t                        nfl_end;        /* ending offset (inclusive) */
	uint32_t                        nfl_blockcnt;   /* # locks blocked on this lock */
	uint16_t                        nfl_flags;      /* see below */
	uint16_t                        nfl_type;       /* lock type: read/write */
};
/* nfl_flags */
#define NFS_FILE_LOCK_ALLOC             0x01    /* lock was allocated */
#define NFS_FILE_LOCK_STYLE_POSIX       0x02    /* POSIX-style fcntl() lock */
#define NFS_FILE_LOCK_STYLE_FLOCK       0x04    /* flock(2)-style lock */
#define NFS_FILE_LOCK_STYLE_MASK        0x06    /* lock style mask */
#define NFS_FILE_LOCK_WAIT              0x08    /* may block on conflicting locks */
#define NFS_FILE_LOCK_BLOCKED           0x10    /* request is blocked */
#define NFS_FILE_LOCK_DEAD              0x20    /* lock (request) no longer exists */
#define NFS_FILE_LOCK_DELEGATED         0x40    /* lock acquired via delegation */

TAILQ_HEAD(nfs_file_lock_queue, nfs_file_lock);

/*
 * Calculate length of lock range given the endpoints.
 * Note that struct flock has "to EOF" reported as 0 but
 * the NFSv4 protocol has "to EOF" reported as UINT64_MAX.
 */
#define NFS_FLOCK_LENGTH(S, E)  (((E) == UINT64_MAX) ? 0 : ((E) - (S) + 1))
#define NFS_LOCK_LENGTH(S, E)   (((E) == UINT64_MAX) ? UINT64_MAX : ((E) - (S) + 1))

/* nfs_lock_owner_find flags */
#define NFS_LOCK_OWNER_FIND_ALLOC               0x01
#define NFS_LOCK_OWNER_FIND_DEQUEUE             0x02

/*
 * NFSv4 lock owner structure - per open owner per process per nfsnode
 *
 * A lock owner is a process + an nfsnode.
 *
 * Note that flock(2) locks technically should have the lock owner be
 * an fglob pointer instead of a process.  However, implementing that
 * correctly would not be trivial.  So, for now, flock(2) locks are
 * essentially treated like whole-file POSIX locks.
 */
struct nfs_lock_owner {
	lck_mtx_t                       nlo_lock;       /* owner mutex */
	TAILQ_ENTRY(nfs_lock_owner)     nlo_link;       /* List of lock owners (on nfsnode) */
	struct nfs_open_owner *         nlo_open_owner; /* corresponding open owner */
	struct nfs_file_lock_queue      nlo_locks;      /* list of locks held */
	struct nfs_file_lock            nlo_alock;      /* most lockers will only ever have one */
	struct timeval                  nlo_pid_start;  /* Start time of process id */
	caddr_t                         nlo_lockid;     /* lock identifier received by VNOP_ADVLOCK */
	pid_t                           nlo_pid;        /* lock-owning process ID */
	os_refcnt_t                     nlo_refcnt;     /* # outstanding references */
	uint32_t                        nlo_flags;      /* see below */
	uint32_t                        nlo_name;       /* unique name used otw */
	uint32_t                        nlo_seqid;      /* client-side sequence ID */
	uint32_t                        nlo_stategenid; /* mount state generation ID */
	nfs_stateid                     nlo_stateid;    /* lock stateid */
};
/* nlo_flags */
#define NFS_LOCK_OWNER_LINK     0x1     /* linked into mount's lock owner list */
#define NFS_LOCK_OWNER_BUSY     0x2     /* lock state-modifying operation in progress */
#define NFS_LOCK_OWNER_WANT     0x4     /* someone else wants to mark busy */

/*
 * The nfsnode is the NFS equivalent of an inode.
 * There is a unique nfsnode for each NFS vnode.
 * An nfsnode is 'named' by its file handle. (nget/nfs_node.c)
 * NB: Hopefully the current order of the fields is such that everything will
 *     be well aligned and, therefore, tightly packed.
 */

#define NFS_ACCESS_CACHE_SIZE   3

struct nfsnode {
	lck_mtx_t               n_lock;         /* nfs node lock */
	lck_rw_t                n_datalock;     /* nfs node data lock */
	void                    *n_datalockowner;/* nfs node data lock owner (exclusive) */
	LIST_ENTRY(nfsnode)     n_hash;         /* Hash chain */
	LIST_ENTRY(nfsnode)     n_monlink;      /* list of monitored nodes */
	u_quad_t                n_size;         /* Current size of file */
	u_quad_t                n_newsize;      /* new size of file (pending update) */
	u_int64_t               n_xid;          /* last xid to loadattr */
	struct nfs_vattr        n_vattr;        /* Vnode attribute cache */
	time_t                  n_attrstamp;    /* Attr. cache timestamp */
	time_t                  n_aclstamp;     /* ACL cache timestamp */
	time_t                  n_evtstamp;     /* last vnode event timestamp */
	uint32_t                n_events;       /* pending vnode events */
	uint32_t                n_access[NFS_ACCESS_CACHE_SIZE + 1];      /* ACCESS cache */
	uid_t                   n_accessuid[NFS_ACCESS_CACHE_SIZE];     /* credentials having access */
	time_t                  n_accessstamp[NFS_ACCESS_CACHE_SIZE];   /* access cache timestamp */
	time_t                  n_rdirplusstamp_sof; /* Readdirplus sof timestamp */
	time_t                  n_rdirplusstamp_eof; /* Readdirplus eof timestamp */
	union {
		struct {
			struct timespec n3_mtime; /* Prev modify time. */
			struct timespec n3_ncmtime; /* namecache modify time. */
		} v3;
		struct {
			uint64_t        n4_change;/* prev change attribute */
			uint64_t        n4_ncchange;/* namecache change attribute */
			u_char          *n4_attrdirfh;/* associated attr directory fh */
			struct timeval  n4_lastio;/* time of most recent I/O on attr */
		} v4;
	} n_un4;
	vnode_t                 n_parent;       /* this node's parent */
	u_char                  *n_fhp;         /* NFS File Handle */
	vnode_t                 n_vnode;        /* associated vnode */
	mount_t                 n_mount;        /* associated mount (NHINIT) */
	int                     n_error;        /* Save write error value */
	union {
		struct timespec ns_atim;        /* Special file times */
		struct timespec nl_rltim;       /* Time of last readlink */
		daddr64_t       nf_lastread;    /* last block# read from (for readahead) */
		uint64_t        nd_cookieverf;  /* Cookie verifier (dir only) */
	} n_un1;
	union {
		struct timespec ns_mtim;        /* Special file times */
		daddr64_t       nf_lastrahead;  /* last block# read ahead */
		uint64_t        nd_eofcookie;   /* Dir. EOF cookie cache */
	} n_un2;
	union {
		struct nfs_sillyrename *nf_silly;/* Ptr to silly rename struct */
		struct nfsdmap *nd_cookiecache; /* dir cookie cache */
	} n_un3;
	uint32_t                n_flag;         /* node flags */
	uint32_t                n_fhsize;       /* size in bytes, of fh */
	u_short                 n_hflag;        /* node hash flags */
	u_short                 n_bflag;        /* node buffer flags */
	u_short                 n_mflag;        /* node mount flags */
	u_char                  n_fh[NFS_SMALLFH];/* Small File Handle */
	uint32_t                n_auth;         /* security flavor used for this node */
	struct nfsbuflists      n_cleanblkhd;   /* clean blocklist head */
	struct nfsbuflists      n_dirtyblkhd;   /* dirty blocklist head */
	union {
		int             nf_wrbusy;      /* # threads in write/fsync */
		uint32_t        nd_ncgen;       /* dir name cache generation# */
	} n_un5;
	union {
		int             nf_needcommitcnt;/* # bufs that need committing */
		daddr64_t       nd_lastdbl;     /* last dir buf lookup block# */
	} n_un6;
	int                     n_bufiterflags; /* buf iterator flags */
	union {
		int             nf_numoutput;   /* write I/Os in progress */
		int             nd_trigseq;     /* vnode trigger seq# */
	} n_un7;
	/* open state */
	lck_mtx_t               n_openlock;     /* nfs node open lock */
	uint32_t                n_openflags;    /* open state flags */
	uint32_t                n_openrefcnt;   /* # non-file opens */
	TAILQ_HEAD(, nfs_open_file) n_opens;     /* list of open files */
	/* lock state */
	TAILQ_HEAD(, nfs_lock_owner) n_lock_owners; /* list of lock owners */
	struct nfs_file_lock_queue n_locks;     /* list of locks */
	/* delegation state */
	nfs_stateid             n_dstateid;     /* delegation stateid */
	TAILQ_ENTRY(nfsnode)    n_dlink;        /* delegation list link */
	TAILQ_ENTRY(nfsnode)    n_dreturn;      /* delegation return list link */
	struct kauth_ace        n_dace;         /* delegation ACE */
};

#define NFS_DATA_LOCK_SHARED    1
#define NFS_DATA_LOCK_EXCLUSIVE 2

#define nfstimespeccmp(tvp, uvp, cmp)           \
	(((tvp)->tv_sec == (uvp)->tv_sec) ?     \
	 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :  \
	 ((tvp)->tv_sec cmp (uvp)->tv_sec))

#define CHECK_NEEDCOMMITCNT(np) \
	do { \
	        if ((np)->n_needcommitcnt < 0) { \
	                printf("nfs: n_needcommitcnt negative\n"); \
	                (np)->n_needcommitcnt = 0; \
	        } \
	} while (0)

#define n_atim                  n_un1.ns_atim
#define n_mtim                  n_un2.ns_mtim
#define n_rltim                 n_un1.nl_rltim
#define n_lastread              n_un1.nf_lastread
#define n_lastrahead            n_un2.nf_lastrahead
#define n_sillyrename           n_un3.nf_silly
#define n_wrbusy                n_un5.nf_wrbusy
#define n_needcommitcnt         n_un6.nf_needcommitcnt
#define n_numoutput             n_un7.nf_numoutput
#define n_cookieverf            n_un1.nd_cookieverf
#define n_eofcookie             n_un2.nd_eofcookie
#define n_cookiecache           n_un3.nd_cookiecache
#define n_ncgen                 n_un5.nd_ncgen
#define n_lastdbl               n_un6.nd_lastdbl
#define n_trigseq               n_un7.nd_trigseq
#define n_mtime                 n_un4.v3.n3_mtime
#define n_ncmtime               n_un4.v3.n3_ncmtime
#define n_change                n_un4.v4.n4_change
#define n_ncchange              n_un4.v4.n4_ncchange
#define n_attrdirfh             n_un4.v4.n4_attrdirfh
#define n_lastio                n_un4.v4.n4_lastio

/*
 * Flags for n_flag
 */
#define NUPDATESIZE     0x00001 /* size of file needs updating */
#define NREVOKE         0x00002 /* node revoked */
#define NMODIFIED       0x00004 /* Might have a modified buffer in bio */
#define NWRITEERR       0x00008 /* Flag write errors so close will know */
#define NNEEDINVALIDATE 0x00010 /* need to call vinvalbuf() */
#define NGETATTRINPROG  0x00020 /* GETATTR RPC in progress */
#define NGETATTRWANT    0x00040 /* waiting for GETATTR RPC */
#define NACC            0x00100 /* Special file accessed */
#define NUPD            0x00200 /* Special file updated */
#define NCHG            0x00400 /* Special file times changed */
#define NNEGNCENTRIES   0x00800 /* directory has negative name cache entries */
#define NBUSY           0x01000 /* node is busy */
#define NBUSYWANT       0x02000 /* waiting on busy node */
#define NISDOTZFS       0x04000 /* a ".zfs" directory */
#define NISDOTZFSCHILD  0x08000 /* a child of a ".zfs" directory */
#define NISMAPPED       0x10000 /* node is mmapped   */
#define NREFRESH        0x20000 /* node's fh needs to be refreshed */
#define NREFRESHWANT    0x40000 /* Waiting for fh to be refreshed */
#define NDISARMTRIGGER  0x80000 /* Ignore node's mirror mount trigger */

/*
 * Flags for n_hflag
 * Note: protected by nfs_node_hash_mutex
 */
#define NHHASHED        0x0001  /* node is in hash table */
#define NHINIT          0x0002  /* node is being initialized */
#define NHLOCKED        0x0004  /* node is locked (initting or deleting) */
#define NHLOCKWANT      0x0008  /* someone wants to lock */

/*
 * Flags for n_bflag
 * Note: protected by nfs_buf_mutex
 */
#define NBFLUSHINPROG   0x0001  /* Avoid multiple calls to nfs_flush() */
#define NBFLUSHWANT     0x0002  /* waiting for nfs_flush() to complete */
#define NBINVALINPROG   0x0004  /* Avoid multiple calls to nfs_vinvalbuf() */
#define NBINVALWANT     0x0008  /* waiting for nfs_vinvalbuf() to complete */

/*
 * Flags for n_mflag
 * Note: protected by nfsmount's nm_lock
 */
#define NMMONSCANINPROG 0x0001  /* monitored node is currently updating attributes */
#define NMMONSCANWANT   0x0002  /* waiting for attribute update to complete */

/*
 * n_openflags
 * Note: protected by n_openlock
 */
#define N_OPENBUSY              0x0001  /* open state is busy - being updated */
#define N_OPENWANT              0x0002  /* someone wants to mark busy */
#define N_DELEG_READ            0x0004  /* we have a read delegation */
#define N_DELEG_WRITE           0x0008  /* we have a write delegation */
#define N_DELEG_MASK            0x000c  /* delegation mask */
#define N_DELEG_RETURN          0x0010  /* delegation queued for return */
#define N_DELEG_RETURNING       0x0020  /* delegation being returned */

/* attr/access/ACL cache timestamp macros */
#define NATTRVALID(np)          ((np)->n_attrstamp != ~0)
#define NATTRINVALIDATE(np)     ((np)->n_attrstamp = ~0)
#define NACCESSVALID(np, slot)  (((slot) >= 0) && ((slot) < NFS_ACCESS_CACHE_SIZE) && ((np)->n_accessstamp[(slot)] != ~0))
#define NACCESSINVALIDATE(np) \
	do { \
	        int __i; \
	        for (__i=0; __i < NFS_ACCESS_CACHE_SIZE; __i++) \
	                (np)->n_accessstamp[__i] = ~0; \
	        (np)->n_access[NFS_ACCESS_CACHE_SIZE] = 0; \
	} while (0)
#define NACLVALID(np)           ((np)->n_aclstamp != ~0)
#define NACLINVALIDATE(np)      ((np)->n_aclstamp = ~0)

/*
 * NFS-specific flags for nfs_vinvalbuf/nfs_flush
 */
#define V_IGNORE_WRITEERR       0x8000

/*
 * Flags for nfs_nget()
 */
#define NG_MARKROOT     0x0001  /* mark vnode as root of FS */
#define NG_MAKEENTRY    0x0002  /* add name cache entry for vnode */
#define NG_NOCREATE     0x0004  /* don't create a new node, return existing one */

/*
 * Convert between nfsnode pointers and vnode pointers
 */
#define VTONFS(vp)      ((nfsnode_t)vnode_fsnode(vp))
#define NFSTOV(np)      ((np)->n_vnode)

/* nfsnode hash table mutex */
extern lck_mtx_t nfs_node_hash_mutex;

/*
 * printf-like helper macro that also outputs node name.
 */
#define NP(NP, FMT, ...) \
	do { \
	        const char *__vname = (NP) ? vnode_getname(NFSTOV(NP)) : NULL; \
	        printf(FMT " %s\n", ##__VA_ARGS__, __vname ? __vname : "???"); \
	        if (__vname) vnode_putname(__vname); \
	} while (0)

/*
 * nfsiod structures
 */
struct nfsiod {
	TAILQ_ENTRY(nfsiod)     niod_link;      /* List of nfsiods */
	struct nfsmount *       niod_nmp;       /* mount point for this nfsiod */
};
TAILQ_HEAD(nfsiodlist, nfsiod);
TAILQ_HEAD(nfsiodmountlist, nfsmount);
extern struct nfsiodlist nfsiodfree, nfsiodwork;
extern struct nfsiodmountlist nfsiodmounts;
extern lck_mtx_t nfsiod_mutex;

#if defined(KERNEL)

typedef int     vnop_t(void *);
extern  vnop_t  **fifo_nfsv2nodeop_p;
extern  vnop_t  **nfsv2_vnodeop_p;
extern  vnop_t  **spec_nfsv2nodeop_p;
#if CONFIG_NFS4
extern  vnop_t  **fifo_nfsv4nodeop_p;
extern  vnop_t  **nfsv4_vnodeop_p;
extern  vnop_t  **spec_nfsv4nodeop_p;
#endif
/*
 * Prototypes for NFS vnode operations
 */
#define nfs_vnop_revoke nop_revoke
int     nfs_vnop_inactive(struct vnop_inactive_args *);
int     nfs_vnop_reclaim(struct vnop_reclaim_args *);

int nfs_node_lock(nfsnode_t);
int nfs_node_lock_internal(nfsnode_t, int);
void nfs_node_lock_force(nfsnode_t);
void nfs_node_unlock(nfsnode_t);
int nfs_node_lock2(nfsnode_t, nfsnode_t);
void nfs_node_unlock2(nfsnode_t, nfsnode_t);
int nfs_node_set_busy(nfsnode_t, thread_t);
int nfs_node_set_busy2(nfsnode_t, nfsnode_t, thread_t);
int nfs_node_set_busy4(nfsnode_t, nfsnode_t, nfsnode_t, nfsnode_t, thread_t);
void nfs_node_clear_busy(nfsnode_t);
void nfs_node_clear_busy2(nfsnode_t, nfsnode_t);
void nfs_node_clear_busy4(nfsnode_t, nfsnode_t, nfsnode_t, nfsnode_t);
void nfs_data_lock(nfsnode_t, int);
void nfs_data_lock_noupdate(nfsnode_t, int);
void nfs_data_lock_internal(nfsnode_t, int, int);
void nfs_data_unlock(nfsnode_t);
void nfs_data_unlock_noupdate(nfsnode_t);
void nfs_data_unlock_internal(nfsnode_t, int);
void nfs_data_update_size(nfsnode_t, int);

/* other stuff */
int nfs_removeit(struct nfs_sillyrename *);
int nfs_nget(mount_t, nfsnode_t, struct componentname *, u_char *, uint32_t, struct nfs_vattr *, u_int64_t *, uint32_t, int, nfsnode_t*);
int nfs_mount_is_dirty(mount_t);
void nfs_dir_cookie_cache(nfsnode_t, uint64_t, uint64_t);
int nfs_dir_cookie_to_lbn(nfsnode_t, uint64_t, int *, uint64_t *);
void nfs_invaldir(nfsnode_t);
uint64_t nfs_dir_buf_freespace(struct nfsbuf *, int);

/* nfsbuf functions */
void nfs_nbinit(void);
void nfs_buf_timer(void *, void *);
void nfs_buf_remfree(struct nfsbuf *);
boolean_t nfs_buf_is_incore(nfsnode_t, daddr64_t);
struct nfsbuf * nfs_buf_incore(nfsnode_t, daddr64_t);
int nfs_buf_get(nfsnode_t, daddr64_t, uint32_t, thread_t, int, struct nfsbuf **);
int nfs_buf_upl_setup(struct nfsbuf *bp);
void nfs_buf_upl_check(struct nfsbuf *bp);
void nfs_buf_normalize_valid_range(nfsnode_t, struct nfsbuf *);
int nfs_buf_map(struct nfsbuf *);
void nfs_buf_release(struct nfsbuf *, int);
int nfs_buf_iowait(struct nfsbuf *);
void nfs_buf_iodone(struct nfsbuf *);
void nfs_buf_write_delayed(struct nfsbuf *);
void nfs_buf_check_write_verifier(nfsnode_t, struct nfsbuf *);
void nfs_buf_freeup(int);
void nfs_buf_refget(struct nfsbuf *bp);
void nfs_buf_refrele(struct nfsbuf *bp);
void nfs_buf_drop(struct nfsbuf *);
errno_t nfs_buf_acquire(struct nfsbuf *, int, int, int);
int nfs_buf_iterprepare(nfsnode_t, struct nfsbuflists *, int);
void nfs_buf_itercomplete(nfsnode_t, struct nfsbuflists *, int);

void nfs_buf_pgs_get_page_mask(nfsbufpgs *, off_t);
void nfs_buf_pgs_bit_not(nfsbufpgs *);
void nfs_buf_pgs_bit_and(nfsbufpgs *, nfsbufpgs *, nfsbufpgs *);
void nfs_buf_pgs_set_pages_between(nfsbufpgs *, off_t, off_t);
int nfs_buf_pgs_is_set(nfsbufpgs *);

int nfs_bioread(nfsnode_t, uio_t, int, vfs_context_t);
int nfs_buf_readahead(nfsnode_t, int, daddr64_t *, daddr64_t, thread_t, kauth_cred_t);
int nfs_buf_readdir(struct nfsbuf *, vfs_context_t);
int nfs_buf_read(struct nfsbuf *);
void nfs_buf_read_finish(struct nfsbuf *);
int nfs_buf_read_rpc(struct nfsbuf *, thread_t, kauth_cred_t);
void nfs_buf_read_rpc_finish(struct nfsreq *);
int nfs_buf_write(struct nfsbuf *);
void nfs_buf_write_finish(struct nfsbuf *, thread_t, kauth_cred_t);
int nfs_buf_write_rpc(struct nfsbuf *, int, thread_t, kauth_cred_t);
void nfs_buf_write_rpc_finish(struct nfsreq *);
int nfs_buf_write_dirty_pages(struct nfsbuf *, thread_t, kauth_cred_t);

int nfs_flushcommits(nfsnode_t, int);
int nfs_flush(nfsnode_t, int, thread_t, int);
void nfs_buf_delwri_push(int);
void nfs_buf_delwri_service(void);
void nfs_buf_delwri_thread(void *, wait_result_t);

int nfsiod_start(void);
void nfsiod_terminate(struct nfsiod *);
void nfsiod_thread(void);
int nfsiod_continue(int);
void nfs_asyncio_finish(struct nfsreq *);
void nfs_asyncio_resend(struct nfsreq *);
int nfs_async_write_start(struct nfsmount *);
void nfs_async_write_done(struct nfsmount *);

#endif /* KERNEL */

#endif /* __APPLE_API_PRIVATE */
#endif /* _NFS_NFSNODE_H_ */