1/*-
  2 * SPDX-License-Identifier: BSD-3-Clause
  3 *
  4 * Copyright (c) 1989, 1993
  5 *	The Regents of the University of California.  All rights reserved.
  6 *
  7 * This code is derived from software contributed to Berkeley by
  8 * Rick Macklem at The University of Guelph.
  9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 * 1. Redistributions of source code must retain the above copyright
 14 *    notice, this list of conditions and the following disclaimer.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 *    notice, this list of conditions and the following disclaimer in the
 17 *    documentation and/or other materials provided with the distribution.
 18 * 3. Neither the name of the University nor the names of its contributors
 19 *    may be used to endorse or promote products derived from this software
 20 *    without specific prior written permission.
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 32 * SUCH DAMAGE.
 33 *
 34 *	@(#)nfsnode.h	8.9 (Berkeley) 5/14/95
 35 */
 36
 37#ifndef _NFSCLIENT_NFSNODE_H_
 38#define _NFSCLIENT_NFSNODE_H_
 39
 40#include <sys/_task.h>
 41#if !defined(_NFSCLIENT_NFS_H_) && !defined(_KERNEL)
 42#include <nfs/nfs.h>
 43#endif
 44
 45/*
 46 * Silly rename structure that hangs off the nfsnode until the name
 47 * can be removed by nfs_inactive()
 48 */
 49struct sillyrename {
 50	struct	task s_task;
 51	struct	ucred *s_cred;
 52	struct	vnode *s_dvp;
 53	int	(*s_removeit)(struct sillyrename *sp);
 54	long	s_namlen;
 55	char	s_name[32];
 56};
 57
 58/*
 59 * This structure is used to save the logical directory offset to
 60 * NFS cookie mappings.
 61 * The mappings are stored in a list headed
 62 * by n_cookies, as required.
 63 * There is one mapping for each NFS_DIRBLKSIZ bytes of directory information
 64 * stored in increasing logical offset byte order.
 65 */
 66#define NFSNUMCOOKIES		31
 67
 68struct nfsdmap {
 69	LIST_ENTRY(nfsdmap)	ndm_list;
 70	int			ndm_eocookie;
 71	union {
 72		nfsuint64	ndmu3_cookies[NFSNUMCOOKIES];
 73		uint64_t	ndmu4_cookies[NFSNUMCOOKIES];
 74	} ndm_un1;
 75};
 76
 77#define ndm_cookies	ndm_un1.ndmu3_cookies
 78#define ndm4_cookies	ndm_un1.ndmu4_cookies
 79
 80struct nfs_accesscache {
 81	u_int32_t		mode;		/* ACCESS mode cache */
 82	uid_t			uid;		/* credentials having mode */
 83	time_t			stamp;		/* mode cache timestamp */
 84};
 85
 86/*
 87 * The nfsnode is the nfs equivalent to ufs's inode. Any similarity
 88 * is purely coincidental.
 89 * There is a unique nfsnode allocated for each active file,
 90 * each current directory, each mounted-on file, text file, and the root.
 91 * An nfsnode is 'named' by its file handle. (nget/nfs_node.c)
 92 * If this structure exceeds 256 bytes (it is currently 256 using 4.4BSD-Lite
 93 * type definitions), file handles of > 32 bytes should probably be split out
 94 * into a separate malloc()'d data structure. (Reduce the size of nfsfh_t by
 95 * changing the definition in nfsproto.h of NFS_SMALLFH.)
 96 * NB: Hopefully the current order of the fields is such that everything will
 97 *     be well aligned and, therefore, tightly packed.
 98 */
 99struct nfsnode {
100	struct mtx 		n_mtx;		/* Protects all of these members */
101	u_quad_t		n_size;		/* Current size of file */
102	u_quad_t		n_brev;		/* Modify rev when cached */
103	u_quad_t		n_lrev;		/* Modify rev for lease */
104	struct vattr		n_vattr;	/* Vnode attribute cache */
105	time_t			n_attrstamp;	/* Attr. cache timestamp */
106	struct nfs_accesscache	n_accesscache[NFS_ACCESSCACHESIZE];
107	struct timespec		n_mtime;	/* Prev modify time. */
108	nfsfh_t			*n_fhp;		/* NFS File Handle */
109	struct vnode		*n_vnode;	/* associated vnode */
110	struct vnode		*n_dvp;		/* parent vnode */
111	int			n_error;	/* Save write error value */
112	union {
113		struct timespec	nf_atim;	/* Special file times */
114		nfsuint64	nd_cookieverf;	/* Cookie verifier (dir only) */
115		u_char		nd4_cookieverf[NFSX_V4VERF];
116	} n_un1;
117	union {
118		struct timespec	nf_mtim;
119		off_t		nd_direof;	/* Dir. EOF offset cache */
120	} n_un2;
121	union {
122		struct sillyrename *nf_silly;	/* Ptr to silly rename struct */
123		LIST_HEAD(, nfsdmap) nd_cook;	/* cookies */
124	} n_un3;
125	short			n_fhsize;	/* size in bytes, of fh */
126	short			n_flag;		/* Flag for locking.. */
127	nfsfh_t			n_fh;		/* Small File Handle */
128	u_char			*n_name;	/* leaf name, for v4 OPEN op */
129	uint32_t		n_namelen;
130	int			n_directio_opens;
131	int                     n_directio_asyncwr;
132	struct ucred		*n_writecred;	/* Cred. for putpages */
133};
134
135#define n_atim		n_un1.nf_atim
136#define n_mtim		n_un2.nf_mtim
137#define n_sillyrename	n_un3.nf_silly
138#define n_cookieverf	n_un1.nd_cookieverf
139#define n4_cookieverf	n_un1.nd4_cookieverf
140#define n_direofoffset	n_un2.nd_direof
141#define n_cookies	n_un3.nd_cook
142
143/*
144 * Flags for n_flag
145 */
146#define NFSYNCWAIT      0x0002  /* fsync waiting for all directio async writes
147				  to drain */
148#define	NMODIFIED	0x0004	/* Might have a modified buffer in bio */
149#define	NWRITEERR	0x0008	/* Flag write errors so close will know */
150/* 0x20, 0x40, 0x80 free */
151#define	NACC		0x0100	/* Special file accessed */
152#define	NUPD		0x0200	/* Special file updated */
153#define	NCHG		0x0400	/* Special file times changed */
154#define	NCREATED	0x0800	/* Opened by nfs_create() */
155#define	NTRUNCATE	0x1000	/* Opened by nfs_setattr() */
156#define	NSIZECHANGED	0x2000  /* File size has changed: need cache inval */
157#define NNONCACHE	0x4000  /* Node marked as noncacheable */
158#define NDIRCOOKIELK	0x8000	/* Lock to serialize access to directory cookies */
159
160/*
161 * Convert between nfsnode pointers and vnode pointers
162 */
163#define VTONFS(vp)	((struct nfsnode *)(vp)->v_data)
164#define NFSTOV(np)	((struct vnode *)(np)->n_vnode)
165
166#define NFS_TIMESPEC_COMPARE(T1, T2)	(((T1)->tv_sec != (T2)->tv_sec) || ((T1)->tv_nsec != (T2)->tv_nsec))
167
168/*
169 * NFS iod threads can be in one of these two states once spawned.
170 * NFSIOD_NOT_AVAILABLE - Cannot be assigned an I/O operation at this time.
171 * NFSIOD_AVAILABLE - Available to be assigned an I/O operation.
172 */
173enum nfsiod_state {
174	NFSIOD_NOT_AVAILABLE = 0,
175	NFSIOD_AVAILABLE = 1,
176};
177
178/*
179 * Queue head for nfsiod's
180 */
181extern TAILQ_HEAD(nfs_bufq, buf) nfs_bufq;
182extern enum nfsiod_state nfs_iodwant[NFS_MAXASYNCDAEMON];
183extern struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
184
185#if defined(_KERNEL)
186
187extern	struct vop_vector	nfs_fifoops;
188extern	struct vop_vector	nfs_vnodeops;
189extern struct buf_ops buf_ops_nfs;
190
191/*
192 * Prototypes for NFS vnode operations
193 */
194int	nfs_getpages(struct vop_getpages_args *);
195int	nfs_putpages(struct vop_putpages_args *);
196int	nfs_write(struct vop_write_args *);
197int	nfs_inactive(struct vop_inactive_args *);
198int	nfs_reclaim(struct vop_reclaim_args *);
199
200/* other stuff */
201int	nfs_removeit(struct sillyrename *);
202int	nfs_nget(struct mount *, nfsfh_t *, int, struct nfsnode **, int flags);
203nfsuint64 *nfs_getcookie(struct nfsnode *, off_t, int);
204void	nfs_invaldir(struct vnode *);
205int	nfs_upgrade_vnlock(struct vnode *vp);
206void	nfs_downgrade_vnlock(struct vnode *vp, int old_lock);
207void	nfs_printf(const char *fmt, ...);
208
209void nfs_dircookie_lock(struct nfsnode *np);
210void nfs_dircookie_unlock(struct nfsnode *np);
211
212#endif /* _KERNEL */
213
214#endif