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