1 /*- 2 * Copyright (c) 1989, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)nfs.h 8.4 (Berkeley) 5/1/95 33 * $FreeBSD$ 34 */ 35 36 #ifndef _NFSSERVER_NFS_H_ 37 #define _NFSSERVER_NFS_H_ 38 39 #ifdef _KERNEL 40 #include "opt_nfs.h" 41 #endif 42 43 /* 44 * Tunable constants for nfs 45 */ 46 47 #define NFS_TICKINTVL 10 /* Desired time for a tick (msec) */ 48 #define NFS_HZ (hz / nfs_ticks) /* Ticks/sec */ 49 #define NFS_TIMEO (1 * NFS_HZ) /* Default timeout = 1 second */ 50 #define NFS_MINTIMEO (1 * NFS_HZ) /* Min timeout to use */ 51 #define NFS_MAXTIMEO (60 * NFS_HZ) /* Max timeout to backoff to */ 52 #define NFS_MINIDEMTIMEO (5 * NFS_HZ) /* Min timeout for non-idempotent ops*/ 53 #define NFS_MAXUIDHASH 64 /* Max. # of hashed uid entries/mp */ 54 #ifndef NFS_GATHERDELAY 55 #define NFS_GATHERDELAY 10 /* Default write gather delay (msec) */ 56 #endif 57 #ifdef _KERNEL 58 #define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */ 59 #endif 60 61 /* 62 * Oddballs 63 */ 64 #define NFS_SRVMAXDATA(n) \ 65 (((n)->nd_flag & ND_NFSV3) ? (((n)->nd_nam2) ? \ 66 NFS_MAXDGRAMDATA : NFS_MAXDATA) : NFS_V2MAXDATA) 67 68 /* 69 * XXX 70 * The B_INVAFTERWRITE flag should be set to whatever is required by the 71 * buffer cache code to say "Invalidate the block after it is written back". 72 */ 73 #define B_INVAFTERWRITE B_NOCACHE 74 75 /* 76 * The IO_METASYNC flag should be implemented for local filesystems. 77 * (Until then, it is nothin at all.) 78 */ 79 #ifndef IO_METASYNC 80 #define IO_METASYNC 0 81 #endif 82 83 84 /* NFS state flags XXX -Wunused */ 85 #define NFSRV_SNDLOCK 0x01000000 /* Send socket lock */ 86 #define NFSRV_WANTSND 0x02000000 /* Want above */ 87 88 /* 89 * Structures for the nfssvc(2) syscall. Not that anyone but nfsd and mount_nfs 90 * should ever try and use it. 91 */ 92 93 /* 94 * Add a socket to monitor for NFS requests. 95 */ 96 struct nfsd_addsock_args { 97 int sock; /* Socket to serve */ 98 caddr_t name; /* Client addr for connection based sockets */ 99 int namelen; /* Length of name */ 100 }; 101 102 /* 103 * Start processing requests. 104 */ 105 struct nfsd_nfsd_args { 106 const char *principal; /* GSS-API service principal name */ 107 int minthreads; /* minimum service thread count */ 108 int maxthreads; /* maximum service thread count */ 109 }; 110 111 /* 112 * XXX to allow amd to include nfs.h without nfsproto.h 113 */ 114 #ifdef NFS_NPROCS 115 #include <nfsserver/nfsrvstats.h> 116 #endif 117 118 /* 119 * Flags for nfssvc() system call. 120 */ 121 #define NFSSVC_OLDNFSD 0x004 122 #define NFSSVC_ADDSOCK 0x008 123 #define NFSSVC_NFSD 0x010 124 125 /* 126 * vfs.nfsrv sysctl(3) identifiers 127 */ 128 #define NFS_NFSRVSTATS 1 /* struct: struct nfsrvstats */ 129 #define NFS_NFSPRIVPORT 2 /* int: prohibit nfs to resvports */ 130 131 #ifdef _KERNEL 132 133 extern struct mtx nfsd_mtx; 134 #define NFSD_LOCK_ASSERT() mtx_assert(&nfsd_mtx, MA_OWNED) 135 #define NFSD_UNLOCK_ASSERT() mtx_assert(&nfsd_mtx, MA_NOTOWNED) 136 #define NFSD_LOCK_DONTCARE() 137 #define NFSD_LOCK() mtx_lock(&nfsd_mtx) 138 #define NFSD_UNLOCK() mtx_unlock(&nfsd_mtx) 139 140 #ifdef MALLOC_DECLARE 141 MALLOC_DECLARE(M_NFSRVDESC); 142 MALLOC_DECLARE(M_NFSD); 143 #endif 144 145 /* Forward declarations */ 146 struct nfssvc_sock; 147 struct nfsrv_descript; 148 struct uio; 149 struct vattr; 150 struct nameidata; 151 152 extern struct callout nfsrv_callout; 153 extern struct nfsrvstats nfsrvstats; 154 155 extern int nfsrv_ticks; 156 extern int nfsrvw_procrastinate; 157 extern int nfsrvw_procrastinate_v3; 158 extern int nfsrv_numnfsd; 159 160 /* Various values converted to XDR form. */ 161 extern u_int32_t nfsrv_nfs_false, nfsrv_nfs_true, nfsrv_nfs_xdrneg1, 162 nfsrv_nfs_prog; 163 extern u_int32_t nfsrv_rpc_reply, nfsrv_rpc_msgdenied, nfsrv_rpc_mismatch, 164 nfsrv_rpc_vers; 165 extern u_int32_t nfsrv_rpc_auth_unix, nfsrv_rpc_msgaccepted, nfsrv_rpc_call, 166 nfsrv_rpc_autherr; 167 168 /* Procedure table data */ 169 extern const int nfsrvv2_procid[NFS_NPROCS]; 170 extern const int nfsrv_nfsv3_procid[NFS_NPROCS]; 171 extern int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd, 172 struct nfssvc_sock *slp, struct mbuf **mreqp); 173 174 /* 175 * A list of nfssvc_sock structures is maintained with all the sockets 176 * that require service by the nfsd. 177 */ 178 #ifndef NFS_WDELAYHASHSIZ 179 #define NFS_WDELAYHASHSIZ 16 /* and with this */ 180 #endif 181 #define NWDELAYHASH(sock, f) \ 182 (&(sock)->ns_wdelayhashtbl[(*((u_int32_t *)(f))) % NFS_WDELAYHASHSIZ]) 183 184 #ifdef NFS_LEGACYRPC 185 /* 186 * Network address hash list element 187 */ 188 union nethostaddr { 189 u_int32_t had_inetaddr; 190 struct sockaddr *had_nam; 191 }; 192 193 struct nfsrv_rec { 194 STAILQ_ENTRY(nfsrv_rec) nr_link; 195 struct sockaddr *nr_address; 196 struct mbuf *nr_packet; 197 }; 198 199 struct nfssvc_sock { 200 TAILQ_ENTRY(nfssvc_sock) ns_chain; /* List of all nfssvc_sock's */ 201 struct file *ns_fp; 202 struct socket *ns_so; 203 struct sockaddr *ns_nam; 204 struct mbuf *ns_raw; 205 struct mbuf *ns_rawend; 206 STAILQ_HEAD(, nfsrv_rec) ns_rec; 207 struct mbuf *ns_frag; 208 int ns_flag; 209 int ns_solock; 210 int ns_cc; 211 int ns_reclen; 212 u_int32_t ns_sref; 213 LIST_HEAD(, nfsrv_descript) ns_tq; /* Write gather lists */ 214 LIST_HEAD(nfsrvw_delayhash, nfsrv_descript) ns_wdelayhashtbl[NFS_WDELAYHASHSIZ]; 215 }; 216 217 /* Bits for "ns_flag" */ 218 #define SLP_VALID 0x01 /* Socket valid for use (XXX) */ 219 #define SLP_DOREC 0x02 /* Socket ready for processing */ 220 #define SLP_NEEDQ 0x04 /* Socket has request queued */ 221 #define SLP_DISCONN 0x08 /* Error received from stream socket */ 222 #define SLP_GETSTREAM 0x10 /* nfsrv_getstream in prog on sock */ 223 #define SLP_LASTFRAG 0x20 /* Socket received end-of-record */ 224 #define SLP_ALLFLAGS 0xff 225 226 extern TAILQ_HEAD(nfssvc_sockhead, nfssvc_sock) nfssvc_sockhead; 227 extern int nfssvc_sockhead_flag; 228 #define SLP_INIT 0x01 229 #define SLP_WANTINIT 0x02 230 231 /* 232 * One of these structures is allocated for each nfsd. 233 */ 234 struct nfsd { 235 TAILQ_ENTRY(nfsd) nfsd_chain; /* List of all nfsd's */ 236 int nfsd_flag; /* NFSD_ flags */ 237 struct nfssvc_sock *nfsd_slp; /* Current socket */ 238 int nfsd_authlen; /* Authenticator len */ 239 u_char nfsd_authstr[RPCAUTH_MAXSIZ]; /* Authenticator data */ 240 int nfsd_verflen; /* and the Verifier */ 241 u_char nfsd_verfstr[RPCVERF_MAXSIZ]; 242 struct nfsrv_descript *nfsd_nd; /* Associated nfsrv_descript */ 243 }; 244 245 /* Bits for "nfsd_flag" */ 246 #define NFSD_WAITING 0x01 247 #define NFSD_REQINPROG 0x02 248 249 /* 250 * This structure is used by the server for describing each request. 251 * Some fields are used only when write request gathering is performed. 252 */ 253 struct nfsrv_descript { 254 u_quad_t nd_time; /* Write deadline (usec) */ 255 off_t nd_off; /* Start byte offset */ 256 off_t nd_eoff; /* and end byte offset */ 257 LIST_ENTRY(nfsrv_descript) nd_hash; /* Hash list */ 258 LIST_ENTRY(nfsrv_descript) nd_tq; /* and timer list */ 259 LIST_HEAD(, nfsrv_descript) nd_coalesce;/* coalesced writes */ 260 struct mbuf *nd_mrep; /* Request mbuf list */ 261 struct mbuf *nd_md; /* Current dissect mbuf */ 262 struct mbuf *nd_mreq; /* Reply mbuf list */ 263 struct sockaddr *nd_nam; /* and socket addr */ 264 struct sockaddr *nd_nam2; /* return socket addr */ 265 caddr_t nd_dpos; /* Current dissect pos */ 266 u_int32_t nd_procnum; /* RPC # */ 267 int nd_stable; /* storage type */ 268 int nd_flag; /* nd_flag */ 269 int nd_len; /* Length of this write */ 270 int nd_repstat; /* Reply status */ 271 u_int32_t nd_retxid; /* Reply xid */ 272 struct timeval nd_starttime; /* Time RPC initiated */ 273 fhandle_t nd_fh; /* File handle */ 274 struct ucred *nd_cr; /* Credentials */ 275 int nd_credflavor; /* Security flavor */ 276 }; 277 278 #else 279 280 /* 281 * This structure is used by the server for describing each request. 282 */ 283 struct nfsrv_descript { 284 struct mbuf *nd_mrep; /* Request mbuf list */ 285 struct mbuf *nd_md; /* Current dissect mbuf */ 286 struct mbuf *nd_mreq; /* Reply mbuf list */ 287 struct sockaddr *nd_nam; /* and socket addr */ 288 struct sockaddr *nd_nam2; /* return socket addr */ 289 caddr_t nd_dpos; /* Current dissect pos */ 290 u_int32_t nd_procnum; /* RPC # */ 291 int nd_stable; /* storage type */ 292 int nd_flag; /* nd_flag */ 293 int nd_repstat; /* Reply status */ 294 fhandle_t nd_fh; /* File handle */ 295 struct ucred *nd_cr; /* Credentials */ 296 int nd_credflavor; /* Security flavor */ 297 }; 298 299 #endif 300 301 /* Bits for "nd_flag" */ 302 #define ND_NFSV3 0x08 303 304 #ifdef NFS_LEGACYRPC 305 306 extern TAILQ_HEAD(nfsd_head, nfsd) nfsd_head; 307 extern int nfsd_head_flag; 308 #define NFSD_CHECKSLP 0x01 309 310 /* 311 * These macros compare nfsrv_descript structures. 312 */ 313 #define NFSW_CONTIG(o, n) \ 314 ((o)->nd_eoff >= (n)->nd_off && \ 315 !bcmp((caddr_t)&(o)->nd_fh, (caddr_t)&(n)->nd_fh, NFSX_V3FH)) 316 317 #endif 318 319 /* 320 * Defines for WebNFS 321 */ 322 323 #define WEBNFS_ESC_CHAR '%' 324 #define WEBNFS_SPECCHAR_START 0x80 325 326 #define WEBNFS_NATIVE_CHAR 0x80 327 /* 328 * .. 329 * Possibly more here in the future. 330 */ 331 332 /* 333 * Macro for converting escape characters in WebNFS pathnames. 334 * Should really be in libkern. 335 */ 336 337 #define HEXTOC(c) \ 338 ((c) >= 'a' ? ((c) - ('a' - 10)) : \ 339 ((c) >= 'A' ? ((c) - ('A' - 10)) : ((c) - '0'))) 340 #define HEXSTRTOI(p) \ 341 ((HEXTOC(p[0]) << 4) + HEXTOC(p[1])) 342 343 #ifdef NFS_DEBUG 344 345 extern int nfs_debug; 346 #define NFS_DEBUG_ASYNCIO 1 /* asynchronous i/o */ 347 #define NFS_DEBUG_WG 2 /* server write gathering */ 348 #define NFS_DEBUG_RC 4 /* server request caching */ 349 350 #define NFS_DPF(cat, args) \ 351 do { \ 352 if (nfs_debug & NFS_DEBUG_##cat) printf args; \ 353 } while (0) 354 355 #else 356 357 #define NFS_DPF(cat, args) 358 359 #endif 360 361 #ifdef NFS_LEGACYRPC 362 int netaddr_match(int, union nethostaddr *, struct sockaddr *); 363 int nfs_getreq(struct nfsrv_descript *, struct nfsd *, int); 364 int nfsrv_send(struct socket *, struct sockaddr *, struct mbuf *); 365 int nfsrv_dorec(struct nfssvc_sock *, struct nfsd *, 366 struct nfsrv_descript **); 367 int nfs_slplock(struct nfssvc_sock *, int); 368 void nfs_slpunlock(struct nfssvc_sock *); 369 void nfsrv_initcache(void); 370 void nfsrv_destroycache(void); 371 void nfsrv_timer(void *); 372 int nfsrv_getcache(struct nfsrv_descript *, struct mbuf **); 373 void nfsrv_updatecache(struct nfsrv_descript *, int, struct mbuf *); 374 void nfsrv_cleancache(void); 375 void nfsrv_rcv(struct socket *so, void *arg, int waitflag); 376 void nfsrv_slpderef(struct nfssvc_sock *slp); 377 void nfsrv_wakenfsd(struct nfssvc_sock *slp); 378 int nfsrv_writegather(struct nfsrv_descript **, struct nfssvc_sock *, 379 struct mbuf **); 380 #endif 381 struct mbuf *nfs_rephead(int, struct nfsrv_descript *, int, struct mbuf **, 382 caddr_t *); 383 void nfsm_srvfattr(struct nfsrv_descript *, struct vattr *, 384 struct nfs_fattr *); 385 void nfsm_srvwcc(struct nfsrv_descript *, int, struct vattr *, int, 386 struct vattr *, struct mbuf **, char **); 387 void nfsm_srvpostopattr(struct nfsrv_descript *, int, struct vattr *, 388 struct mbuf **, char **); 389 int nfs_namei(struct nameidata *, struct nfsrv_descript *, fhandle_t *, 390 int, struct nfssvc_sock *, struct sockaddr *, struct mbuf **, 391 caddr_t *, struct vnode **, int, struct vattr *, int *, int); 392 void nfsm_adj(struct mbuf *, int, int); 393 int nfsm_mbuftouio(struct mbuf **, struct uio *, int, caddr_t *); 394 void nfsrv_init(int); 395 int nfsrv_errmap(struct nfsrv_descript *, int); 396 void nfsrvw_sort(gid_t *, int); 397 398 int nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 399 struct mbuf **mrq); 400 int nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 401 struct mbuf **mrq); 402 int nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 403 struct mbuf **mrq); 404 int nfsrv_fhtovp(fhandle_t *, int, struct vnode **, int *, 405 struct nfsrv_descript *, struct nfssvc_sock *, struct sockaddr *, 406 int *, int); 407 int nfsrv_setpublicfs(struct mount *, struct netexport *, 408 struct export_args *); 409 int nfs_ispublicfh(fhandle_t *); 410 int nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 411 struct mbuf **mrq); 412 int nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 413 struct mbuf **mrq); 414 int nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 415 struct mbuf **mrq); 416 int nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 417 struct mbuf **mrq); 418 int nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 419 struct mbuf **mrq); 420 int nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 421 struct mbuf **mrq); 422 int nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 423 struct mbuf **mrq); 424 int nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 425 struct mbuf **mrq); 426 int nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 427 struct mbuf **mrq); 428 int nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 429 struct mbuf **mrq); 430 int nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 431 struct mbuf **mrq); 432 int nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 433 struct mbuf **mrq); 434 int nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 435 struct mbuf **mrq); 436 int nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 437 struct mbuf **mrq); 438 int nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 439 struct mbuf **mrq); 440 int nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 441 struct mbuf **mrq); 442 int nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 443 struct mbuf **mrq); 444 int nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 445 struct mbuf **mrq); 446 int nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 447 struct mbuf **mrq); 448 int nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 449 struct mbuf **mrq); 450 #endif /* _KERNEL */ 451 452 #endif 453