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