1 /* 2 * Copyright (c) 1989, 1993 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_subs.c 8.8 (Berkeley) 5/22/95 37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_subs.c,v 1.128 2004/04/14 23:23:55 peadar Exp $ 38 * $DragonFly: src/sys/vfs/nfs/nfs_subs.c,v 1.48 2008/09/17 21:44:24 dillon Exp $ 39 */ 40 41 /* 42 * These functions support the macros and help fiddle mbuf chains for 43 * the nfs op functions. They do things like create the rpc header and 44 * copy data between mbuf chains and uio lists. 45 */ 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/buf.h> 50 #include <sys/proc.h> 51 #include <sys/mount.h> 52 #include <sys/vnode.h> 53 #include <sys/nlookup.h> 54 #include <sys/namei.h> 55 #include <sys/mbuf.h> 56 #include <sys/socket.h> 57 #include <sys/stat.h> 58 #include <sys/malloc.h> 59 #include <sys/sysent.h> 60 #include <sys/syscall.h> 61 #include <sys/conf.h> 62 #include <sys/objcache.h> 63 64 #include <vm/vm.h> 65 #include <vm/vm_object.h> 66 #include <vm/vm_extern.h> 67 68 #include <sys/buf2.h> 69 70 #include "rpcv2.h" 71 #include "nfsproto.h" 72 #include "nfs.h" 73 #include "nfsmount.h" 74 #include "nfsnode.h" 75 #include "xdr_subs.h" 76 #include "nfsm_subs.h" 77 #include "nfsrtt.h" 78 79 #include <netinet/in.h> 80 81 MALLOC_DEFINE(M_NFSMOUNT, "NFS mount", "NFS mount"); 82 83 /* 84 * Data items converted to xdr at startup, since they are constant 85 * This is kinda hokey, but may save a little time doing byte swaps 86 */ 87 u_int32_t nfs_xdrneg1; 88 u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers; 89 u_int32_t rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr; 90 u_int32_t rpc_auth_kerb; 91 u_int32_t nfs_prog, nfs_true, nfs_false; 92 93 /* And other global data */ 94 static enum vtype nv2tov_type[8]= { 95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON 96 }; 97 enum vtype nv3tov_type[8]= { 98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO 99 }; 100 101 int nfs_ticks; 102 103 /* 104 * Protect master lists only. Primary protection uses the per-mount 105 * and per nfssvc_sock tokens. 106 */ 107 struct lwkt_token nfs_token = LWKT_TOKEN_INITIALIZER(unp_token); 108 109 static int nfs_pbuf_freecnt = -1; /* start out unlimited */ 110 111 struct nfsmount_head nfs_mountq = TAILQ_HEAD_INITIALIZER(nfs_mountq); 112 struct nfssvc_sockhead nfssvc_sockhead; 113 int nfssvc_sockhead_flag; 114 struct nfsd_head nfsd_head; 115 int nfsd_head_flag; 116 struct nfs_bufq nfs_bufq; 117 struct nqfhhashhead *nqfhhashtbl; 118 u_long nqfhhash; 119 120 static int nfs_prev_nfssvc_sy_narg; 121 static sy_call_t *nfs_prev_nfssvc_sy_call; 122 123 #ifndef NFS_NOSERVER 124 125 /* 126 * Mapping of old NFS Version 2 RPC numbers to generic numbers. 127 */ 128 int nfsv3_procid[NFS_NPROCS] = { 129 NFSPROC_NULL, 130 NFSPROC_GETATTR, 131 NFSPROC_SETATTR, 132 NFSPROC_NOOP, 133 NFSPROC_LOOKUP, 134 NFSPROC_READLINK, 135 NFSPROC_READ, 136 NFSPROC_NOOP, 137 NFSPROC_WRITE, 138 NFSPROC_CREATE, 139 NFSPROC_REMOVE, 140 NFSPROC_RENAME, 141 NFSPROC_LINK, 142 NFSPROC_SYMLINK, 143 NFSPROC_MKDIR, 144 NFSPROC_RMDIR, 145 NFSPROC_READDIR, 146 NFSPROC_FSSTAT, 147 NFSPROC_NOOP, 148 NFSPROC_NOOP, 149 NFSPROC_NOOP, 150 NFSPROC_NOOP, 151 NFSPROC_NOOP, 152 NFSPROC_NOOP, 153 NFSPROC_NOOP, 154 NFSPROC_NOOP 155 }; 156 157 #endif /* NFS_NOSERVER */ 158 /* 159 * and the reverse mapping from generic to Version 2 procedure numbers 160 */ 161 int nfsv2_procid[NFS_NPROCS] = { 162 NFSV2PROC_NULL, 163 NFSV2PROC_GETATTR, 164 NFSV2PROC_SETATTR, 165 NFSV2PROC_LOOKUP, 166 NFSV2PROC_NOOP, 167 NFSV2PROC_READLINK, 168 NFSV2PROC_READ, 169 NFSV2PROC_WRITE, 170 NFSV2PROC_CREATE, 171 NFSV2PROC_MKDIR, 172 NFSV2PROC_SYMLINK, 173 NFSV2PROC_CREATE, 174 NFSV2PROC_REMOVE, 175 NFSV2PROC_RMDIR, 176 NFSV2PROC_RENAME, 177 NFSV2PROC_LINK, 178 NFSV2PROC_READDIR, 179 NFSV2PROC_NOOP, 180 NFSV2PROC_STATFS, 181 NFSV2PROC_NOOP, 182 NFSV2PROC_NOOP, 183 NFSV2PROC_NOOP, 184 NFSV2PROC_NOOP, 185 NFSV2PROC_NOOP, 186 NFSV2PROC_NOOP, 187 NFSV2PROC_NOOP, 188 }; 189 190 #ifndef NFS_NOSERVER 191 /* 192 * Maps errno values to nfs error numbers. 193 * Use NFSERR_IO as the catch all for ones not specifically defined in 194 * RFC 1094. 195 */ 196 static u_char nfsrv_v2errmap[ELAST] = { 197 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 198 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 199 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 200 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 201 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 202 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 203 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 204 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 205 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 206 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 207 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 208 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 209 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 210 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 211 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 212 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 213 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 214 NFSERR_IO /* << Last is 86 */ 215 }; 216 217 /* 218 * Maps errno values to nfs error numbers. 219 * Although it is not obvious whether or not NFS clients really care if 220 * a returned error value is in the specified list for the procedure, the 221 * safest thing to do is filter them appropriately. For Version 2, the 222 * X/Open XNFS document is the only specification that defines error values 223 * for each RPC (The RFC simply lists all possible error values for all RPCs), 224 * so I have decided to not do this for Version 2. 225 * The first entry is the default error return and the rest are the valid 226 * errors for that RPC in increasing numeric order. 227 */ 228 static short nfsv3err_null[] = { 229 0, 230 0, 231 }; 232 233 static short nfsv3err_getattr[] = { 234 NFSERR_IO, 235 NFSERR_IO, 236 NFSERR_STALE, 237 NFSERR_BADHANDLE, 238 NFSERR_SERVERFAULT, 239 0, 240 }; 241 242 static short nfsv3err_setattr[] = { 243 NFSERR_IO, 244 NFSERR_PERM, 245 NFSERR_IO, 246 NFSERR_ACCES, 247 NFSERR_INVAL, 248 NFSERR_NOSPC, 249 NFSERR_ROFS, 250 NFSERR_DQUOT, 251 NFSERR_STALE, 252 NFSERR_BADHANDLE, 253 NFSERR_NOT_SYNC, 254 NFSERR_SERVERFAULT, 255 0, 256 }; 257 258 static short nfsv3err_lookup[] = { 259 NFSERR_IO, 260 NFSERR_NOENT, 261 NFSERR_IO, 262 NFSERR_ACCES, 263 NFSERR_NOTDIR, 264 NFSERR_NAMETOL, 265 NFSERR_STALE, 266 NFSERR_BADHANDLE, 267 NFSERR_SERVERFAULT, 268 0, 269 }; 270 271 static short nfsv3err_access[] = { 272 NFSERR_IO, 273 NFSERR_IO, 274 NFSERR_STALE, 275 NFSERR_BADHANDLE, 276 NFSERR_SERVERFAULT, 277 0, 278 }; 279 280 static short nfsv3err_readlink[] = { 281 NFSERR_IO, 282 NFSERR_IO, 283 NFSERR_ACCES, 284 NFSERR_INVAL, 285 NFSERR_STALE, 286 NFSERR_BADHANDLE, 287 NFSERR_NOTSUPP, 288 NFSERR_SERVERFAULT, 289 0, 290 }; 291 292 static short nfsv3err_read[] = { 293 NFSERR_IO, 294 NFSERR_IO, 295 NFSERR_NXIO, 296 NFSERR_ACCES, 297 NFSERR_INVAL, 298 NFSERR_STALE, 299 NFSERR_BADHANDLE, 300 NFSERR_SERVERFAULT, 301 0, 302 }; 303 304 static short nfsv3err_write[] = { 305 NFSERR_IO, 306 NFSERR_IO, 307 NFSERR_ACCES, 308 NFSERR_INVAL, 309 NFSERR_FBIG, 310 NFSERR_NOSPC, 311 NFSERR_ROFS, 312 NFSERR_DQUOT, 313 NFSERR_STALE, 314 NFSERR_BADHANDLE, 315 NFSERR_SERVERFAULT, 316 0, 317 }; 318 319 static short nfsv3err_create[] = { 320 NFSERR_IO, 321 NFSERR_IO, 322 NFSERR_ACCES, 323 NFSERR_EXIST, 324 NFSERR_NOTDIR, 325 NFSERR_NOSPC, 326 NFSERR_ROFS, 327 NFSERR_NAMETOL, 328 NFSERR_DQUOT, 329 NFSERR_STALE, 330 NFSERR_BADHANDLE, 331 NFSERR_NOTSUPP, 332 NFSERR_SERVERFAULT, 333 0, 334 }; 335 336 static short nfsv3err_mkdir[] = { 337 NFSERR_IO, 338 NFSERR_IO, 339 NFSERR_ACCES, 340 NFSERR_EXIST, 341 NFSERR_NOTDIR, 342 NFSERR_NOSPC, 343 NFSERR_ROFS, 344 NFSERR_NAMETOL, 345 NFSERR_DQUOT, 346 NFSERR_STALE, 347 NFSERR_BADHANDLE, 348 NFSERR_NOTSUPP, 349 NFSERR_SERVERFAULT, 350 0, 351 }; 352 353 static short nfsv3err_symlink[] = { 354 NFSERR_IO, 355 NFSERR_IO, 356 NFSERR_ACCES, 357 NFSERR_EXIST, 358 NFSERR_NOTDIR, 359 NFSERR_NOSPC, 360 NFSERR_ROFS, 361 NFSERR_NAMETOL, 362 NFSERR_DQUOT, 363 NFSERR_STALE, 364 NFSERR_BADHANDLE, 365 NFSERR_NOTSUPP, 366 NFSERR_SERVERFAULT, 367 0, 368 }; 369 370 static short nfsv3err_mknod[] = { 371 NFSERR_IO, 372 NFSERR_IO, 373 NFSERR_ACCES, 374 NFSERR_EXIST, 375 NFSERR_NOTDIR, 376 NFSERR_NOSPC, 377 NFSERR_ROFS, 378 NFSERR_NAMETOL, 379 NFSERR_DQUOT, 380 NFSERR_STALE, 381 NFSERR_BADHANDLE, 382 NFSERR_NOTSUPP, 383 NFSERR_SERVERFAULT, 384 NFSERR_BADTYPE, 385 0, 386 }; 387 388 static short nfsv3err_remove[] = { 389 NFSERR_IO, 390 NFSERR_NOENT, 391 NFSERR_IO, 392 NFSERR_ACCES, 393 NFSERR_NOTDIR, 394 NFSERR_ROFS, 395 NFSERR_NAMETOL, 396 NFSERR_STALE, 397 NFSERR_BADHANDLE, 398 NFSERR_SERVERFAULT, 399 0, 400 }; 401 402 static short nfsv3err_rmdir[] = { 403 NFSERR_IO, 404 NFSERR_NOENT, 405 NFSERR_IO, 406 NFSERR_ACCES, 407 NFSERR_EXIST, 408 NFSERR_NOTDIR, 409 NFSERR_INVAL, 410 NFSERR_ROFS, 411 NFSERR_NAMETOL, 412 NFSERR_NOTEMPTY, 413 NFSERR_STALE, 414 NFSERR_BADHANDLE, 415 NFSERR_NOTSUPP, 416 NFSERR_SERVERFAULT, 417 0, 418 }; 419 420 static short nfsv3err_rename[] = { 421 NFSERR_IO, 422 NFSERR_NOENT, 423 NFSERR_IO, 424 NFSERR_ACCES, 425 NFSERR_EXIST, 426 NFSERR_XDEV, 427 NFSERR_NOTDIR, 428 NFSERR_ISDIR, 429 NFSERR_INVAL, 430 NFSERR_NOSPC, 431 NFSERR_ROFS, 432 NFSERR_MLINK, 433 NFSERR_NAMETOL, 434 NFSERR_NOTEMPTY, 435 NFSERR_DQUOT, 436 NFSERR_STALE, 437 NFSERR_BADHANDLE, 438 NFSERR_NOTSUPP, 439 NFSERR_SERVERFAULT, 440 0, 441 }; 442 443 static short nfsv3err_link[] = { 444 NFSERR_IO, 445 NFSERR_IO, 446 NFSERR_ACCES, 447 NFSERR_EXIST, 448 NFSERR_XDEV, 449 NFSERR_NOTDIR, 450 NFSERR_INVAL, 451 NFSERR_NOSPC, 452 NFSERR_ROFS, 453 NFSERR_MLINK, 454 NFSERR_NAMETOL, 455 NFSERR_DQUOT, 456 NFSERR_STALE, 457 NFSERR_BADHANDLE, 458 NFSERR_NOTSUPP, 459 NFSERR_SERVERFAULT, 460 0, 461 }; 462 463 static short nfsv3err_readdir[] = { 464 NFSERR_IO, 465 NFSERR_IO, 466 NFSERR_ACCES, 467 NFSERR_NOTDIR, 468 NFSERR_STALE, 469 NFSERR_BADHANDLE, 470 NFSERR_BAD_COOKIE, 471 NFSERR_TOOSMALL, 472 NFSERR_SERVERFAULT, 473 0, 474 }; 475 476 static short nfsv3err_readdirplus[] = { 477 NFSERR_IO, 478 NFSERR_IO, 479 NFSERR_ACCES, 480 NFSERR_NOTDIR, 481 NFSERR_STALE, 482 NFSERR_BADHANDLE, 483 NFSERR_BAD_COOKIE, 484 NFSERR_NOTSUPP, 485 NFSERR_TOOSMALL, 486 NFSERR_SERVERFAULT, 487 0, 488 }; 489 490 static short nfsv3err_fsstat[] = { 491 NFSERR_IO, 492 NFSERR_IO, 493 NFSERR_STALE, 494 NFSERR_BADHANDLE, 495 NFSERR_SERVERFAULT, 496 0, 497 }; 498 499 static short nfsv3err_fsinfo[] = { 500 NFSERR_STALE, 501 NFSERR_STALE, 502 NFSERR_BADHANDLE, 503 NFSERR_SERVERFAULT, 504 0, 505 }; 506 507 static short nfsv3err_pathconf[] = { 508 NFSERR_STALE, 509 NFSERR_STALE, 510 NFSERR_BADHANDLE, 511 NFSERR_SERVERFAULT, 512 0, 513 }; 514 515 static short nfsv3err_commit[] = { 516 NFSERR_IO, 517 NFSERR_IO, 518 NFSERR_STALE, 519 NFSERR_BADHANDLE, 520 NFSERR_SERVERFAULT, 521 0, 522 }; 523 524 static short *nfsrv_v3errmap[] = { 525 nfsv3err_null, 526 nfsv3err_getattr, 527 nfsv3err_setattr, 528 nfsv3err_lookup, 529 nfsv3err_access, 530 nfsv3err_readlink, 531 nfsv3err_read, 532 nfsv3err_write, 533 nfsv3err_create, 534 nfsv3err_mkdir, 535 nfsv3err_symlink, 536 nfsv3err_mknod, 537 nfsv3err_remove, 538 nfsv3err_rmdir, 539 nfsv3err_rename, 540 nfsv3err_link, 541 nfsv3err_readdir, 542 nfsv3err_readdirplus, 543 nfsv3err_fsstat, 544 nfsv3err_fsinfo, 545 nfsv3err_pathconf, 546 nfsv3err_commit, 547 }; 548 549 #endif /* NFS_NOSERVER */ 550 551 struct nfssvc_args; 552 extern int sys_nfssvc(struct proc *, struct nfssvc_args *, int *); 553 554 /* 555 * This needs to return a monotonically increasing or close to monotonically 556 * increasing result, otherwise the write gathering queues won't work 557 * properly. 558 */ 559 u_quad_t 560 nfs_curusec(void) 561 { 562 struct timeval tv; 563 564 getmicrouptime(&tv); 565 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec); 566 } 567 568 /* 569 * Called once to initialize data structures... 570 */ 571 int 572 nfs_init(struct vfsconf *vfsp) 573 { 574 callout_init(&nfs_timer_handle); 575 nfsmount_objcache = objcache_create_simple(M_NFSMOUNT, sizeof(struct nfsmount)); 576 577 nfs_mount_type = vfsp->vfc_typenum; 578 nfsrtt.pos = 0; 579 rpc_vers = txdr_unsigned(RPC_VER2); 580 rpc_call = txdr_unsigned(RPC_CALL); 581 rpc_reply = txdr_unsigned(RPC_REPLY); 582 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 583 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 584 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 585 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 586 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 587 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 588 nfs_prog = txdr_unsigned(NFS_PROG); 589 nfs_true = txdr_unsigned(TRUE); 590 nfs_false = txdr_unsigned(FALSE); 591 nfs_xdrneg1 = txdr_unsigned(-1); 592 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 593 if (nfs_ticks < 1) 594 nfs_ticks = 1; 595 nfs_nhinit(); /* Init the nfsnode table */ 596 #ifndef NFS_NOSERVER 597 nfsrv_init(0); /* Init server data structures */ 598 nfsrv_initcache(); /* Init the server request cache */ 599 #endif 600 601 /* 602 * Mainly for vkernel operation. If memory is severely limited 603 */ 604 if (nfs_maxasyncbio > nmbclusters * MCLBYTES / NFS_MAXDATA / 3) 605 nfs_maxasyncbio = nmbclusters * MCLBYTES / NFS_MAXDATA / 3; 606 if (nfs_maxasyncbio < 4) 607 nfs_maxasyncbio = 4; 608 609 /* 610 * Initialize reply list and start timer 611 */ 612 nfs_timer_callout(0); 613 614 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg; 615 sysent[SYS_nfssvc].sy_narg = 2; 616 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call; 617 sysent[SYS_nfssvc].sy_call = (sy_call_t *)sys_nfssvc; 618 619 nfs_pbuf_freecnt = nswbuf / 2 + 1; 620 621 return (0); 622 } 623 624 int 625 nfs_uninit(struct vfsconf *vfsp) 626 { 627 callout_stop(&nfs_timer_handle); 628 nfs_mount_type = -1; 629 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg; 630 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call; 631 return (0); 632 } 633 634 /* 635 * Attribute cache routines. 636 * nfs_loadattrcache() - loads or updates the cache contents from attributes 637 * that are on the mbuf list 638 * nfs_getattrcache() - returns valid attributes if found in cache, returns 639 * error otherwise 640 */ 641 642 /* 643 * Load the attribute cache (that lives in the nfsnode entry) with 644 * the values on the mbuf list. Load *vaper with the attributes. vaper 645 * may be NULL. 646 * 647 * As a side effect n_mtime, which we use to determine if the file was 648 * modified by some other host, is set to the attribute timestamp and 649 * NRMODIFIED is set if the two values differ. 650 * 651 * WARNING: the mtime loaded into vaper does not necessarily represent 652 * n_mtime or n_attr.mtime due to NACC and NUPD. 653 */ 654 int 655 nfs_loadattrcache(struct vnode *vp, struct mbuf **mdp, caddr_t *dposp, 656 struct vattr *vaper, int lattr_flags) 657 { 658 struct vattr *vap; 659 struct nfs_fattr *fp; 660 struct nfsnode *np; 661 int32_t t1; 662 caddr_t cp2; 663 int error = 0; 664 int rmajor, rminor; 665 udev_t rdev; 666 struct mbuf *md; 667 enum vtype vtyp; 668 u_short vmode; 669 struct timespec mtime; 670 int v3 = NFS_ISV3(vp); 671 672 md = *mdp; 673 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 674 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0) 675 return (error); 676 fp = (struct nfs_fattr *)cp2; 677 if (v3) { 678 vtyp = nfsv3tov_type(fp->fa_type); 679 vmode = fxdr_unsigned(u_short, fp->fa_mode); 680 rmajor = (int)fxdr_unsigned(int, fp->fa3_rdev.specdata1); 681 rminor = (int)fxdr_unsigned(int, fp->fa3_rdev.specdata2); 682 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 683 } else { 684 vtyp = nfsv2tov_type(fp->fa_type); 685 vmode = fxdr_unsigned(u_short, fp->fa_mode); 686 /* 687 * XXX 688 * 689 * The duplicate information returned in fa_type and fa_mode 690 * is an ambiguity in the NFS version 2 protocol. 691 * 692 * VREG should be taken literally as a regular file. If a 693 * server intents to return some type information differently 694 * in the upper bits of the mode field (e.g. for sockets, or 695 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 696 * leave the examination of the mode bits even in the VREG 697 * case to avoid breakage for bogus servers, but we make sure 698 * that there are actually type bits set in the upper part of 699 * fa_mode (and failing that, trust the va_type field). 700 * 701 * NFSv3 cleared the issue, and requires fa_mode to not 702 * contain any type information (while also introduing sockets 703 * and FIFOs for fa_type). 704 */ 705 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 706 vtyp = IFTOVT(vmode); 707 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 708 rmajor = umajor(rdev); 709 rminor = uminor(rdev); 710 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 711 712 /* 713 * Really ugly NFSv2 kludge. 714 */ 715 if (vtyp == VCHR && rdev == (udev_t)0xffffffff) 716 vtyp = VFIFO; 717 } 718 719 /* 720 * If v_type == VNON it is a new node, so fill in the v_type, 721 * n_mtime fields. Check to see if it represents a special 722 * device, and if so, check for a possible alias. Once the 723 * correct vnode has been obtained, fill in the rest of the 724 * information. 725 */ 726 np = VTONFS(vp); 727 if (vp->v_type != vtyp) { 728 nfs_setvtype(vp, vtyp); 729 if (vp->v_type == VFIFO) { 730 vp->v_ops = &vp->v_mount->mnt_vn_fifo_ops; 731 } else if (vp->v_type == VCHR || vp->v_type == VBLK) { 732 vp->v_ops = &vp->v_mount->mnt_vn_spec_ops; 733 addaliasu(vp, rmajor, rminor); 734 } else { 735 vp->v_ops = &vp->v_mount->mnt_vn_use_ops; 736 } 737 np->n_mtime = mtime.tv_sec; 738 } else if (np->n_mtime != mtime.tv_sec) { 739 /* 740 * If we haven't modified the file locally and the server 741 * timestamp does not match, then the server probably 742 * modified the file. We must flag this condition so 743 * the proper syncnronization can be done. We do not 744 * try to synchronize the state here because that 745 * could lead to an endless recursion. 746 * 747 * XXX loadattrcache can be set during the reply to a write, 748 * before the write timestamp is properly processed. To 749 * avoid unconditionally setting the rmodified bit (which 750 * has the effect of flushing the cache), we only do this 751 * check if the lmodified bit is not set. 752 */ 753 np->n_mtime = mtime.tv_sec; 754 if ((lattr_flags & NFS_LATTR_NOMTIMECHECK) == 0) 755 np->n_flag |= NRMODIFIED; 756 } 757 vap = &np->n_vattr; 758 vap->va_type = vtyp; 759 vap->va_mode = (vmode & 07777); 760 vap->va_rmajor = rmajor; 761 vap->va_rminor = rminor; 762 vap->va_mtime = mtime; 763 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 764 if (v3) { 765 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 766 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 767 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 768 vap->va_size = fxdr_hyper(&fp->fa3_size); 769 vap->va_blocksize = NFS_FABLKSIZE; 770 vap->va_bytes = fxdr_hyper(&fp->fa3_used); 771 vap->va_fileid = fxdr_hyper(&fp->fa3_fileid); 772 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 773 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 774 vap->va_flags = 0; 775 vap->va_filerev = 0; 776 } else { 777 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 778 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 779 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 780 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 781 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 782 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) 783 * NFS_FABLKSIZE; 784 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 785 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 786 vap->va_flags = 0; 787 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 788 fp->fa2_ctime.nfsv2_sec); 789 vap->va_ctime.tv_nsec = 0; 790 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 791 vap->va_filerev = 0; 792 } 793 np->n_attrstamp = time_second; 794 if (vap->va_size != np->n_size) { 795 if (vap->va_type == VREG) { 796 /* 797 * Get rid of all the junk we had before and just 798 * set NRMODIFIED if NLMODIFIED is 0. Depend on 799 * occassionally flushing our dirty buffers to 800 * clear both the NLMODIFIED and NRMODIFIED flags. 801 */ 802 if ((np->n_flag & NLMODIFIED) == 0) 803 np->n_flag |= NRMODIFIED; 804 #if 0 805 if ((lattr_flags & NFS_LATTR_NOSHRINK) && 806 vap->va_size < np->n_size) { 807 /* 808 * We've been told not to shrink the file; 809 * zero np->n_attrstamp to indicate that 810 * the attributes are stale. 811 * 812 * This occurs primarily due to recursive 813 * NFS ops that are executed during periods 814 * where we cannot safely reduce the size of 815 * the file. 816 * 817 * Additionally, write rpcs are broken down 818 * into buffers and np->n_size is 819 * pre-extended. Setting NRMODIFIED here 820 * can result in n_size getting reset to a 821 * lower value, which is NOT what we want. 822 * XXX this needs to be cleaned up a lot 823 * more. 824 */ 825 vap->va_size = np->n_size; 826 np->n_attrstamp = 0; 827 if ((np->n_flag & NLMODIFIED) == 0) 828 np->n_flag |= NRMODIFIED; 829 } else if (np->n_flag & NLMODIFIED) { 830 /* 831 * We've modified the file: Use the larger 832 * of our size, and the server's size. At 833 * this point the cache coherency is all 834 * shot to hell. To try to handle multiple 835 * clients appending to the file at the same 836 * time mark that the server has changed 837 * the file if the server's notion of the 838 * file size is larger then our notion. 839 * 840 * XXX this needs work. 841 */ 842 if (vap->va_size < np->n_size) { 843 vap->va_size = np->n_size; 844 } else { 845 np->n_size = vap->va_size; 846 np->n_flag |= NRMODIFIED; 847 } 848 } else { 849 /* 850 * Someone changed the file's size on the 851 * server and there are no local changes 852 * to get in the way, set the size and mark 853 * it. 854 */ 855 np->n_size = vap->va_size; 856 np->n_flag |= NRMODIFIED; 857 } 858 nvnode_pager_setsize(vp, np->n_size, XXX); 859 #endif 860 } else { 861 np->n_size = vap->va_size; 862 } 863 } 864 if (vaper != NULL) { 865 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 866 if (np->n_flag & NCHG) { 867 if (np->n_flag & NACC) 868 vaper->va_atime = np->n_atim; 869 if (np->n_flag & NUPD) 870 vaper->va_mtime = np->n_mtim; 871 } 872 } 873 return (0); 874 } 875 876 #ifdef NFS_ACDEBUG 877 #include <sys/sysctl.h> 878 SYSCTL_DECL(_vfs_nfs); 879 static int nfs_acdebug; 880 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 881 #endif 882 883 /* 884 * Check the time stamp 885 * If the cache is valid, copy contents to *vap and return 0 886 * otherwise return an error 887 */ 888 int 889 nfs_getattrcache(struct vnode *vp, struct vattr *vaper) 890 { 891 struct nfsnode *np; 892 struct vattr *vap; 893 struct nfsmount *nmp; 894 int timeo; 895 896 np = VTONFS(vp); 897 vap = &np->n_vattr; 898 nmp = VFSTONFS(vp->v_mount); 899 900 /* 901 * Dynamic timeout based on how recently the file was modified. 902 * n_mtime is always valid. 903 */ 904 timeo = (get_approximate_time_t() - np->n_mtime) / 60; 905 906 #ifdef NFS_ACDEBUG 907 if (nfs_acdebug>1) 908 kprintf("nfs_getattrcache: initial timeo = %d\n", timeo); 909 #endif 910 911 if (vap->va_type == VDIR) { 912 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acdirmin) 913 timeo = nmp->nm_acdirmin; 914 else if (timeo > nmp->nm_acdirmax) 915 timeo = nmp->nm_acdirmax; 916 } else { 917 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acregmin) 918 timeo = nmp->nm_acregmin; 919 else if (timeo > nmp->nm_acregmax) 920 timeo = nmp->nm_acregmax; 921 } 922 923 #ifdef NFS_ACDEBUG 924 if (nfs_acdebug > 2) 925 kprintf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 926 nmp->nm_acregmin, nmp->nm_acregmax, 927 nmp->nm_acdirmin, nmp->nm_acdirmax); 928 929 if (nfs_acdebug) 930 kprintf("nfs_getattrcache: age = %d; final timeo = %d\n", 931 (int)(time_second - np->n_attrstamp), timeo); 932 #endif 933 934 if (np->n_attrstamp == 0 || (time_second - np->n_attrstamp) >= timeo) { 935 nfsstats.attrcache_misses++; 936 return (ENOENT); 937 } 938 nfsstats.attrcache_hits++; 939 940 /* 941 * Our attribute cache can be stale due to modifications made on 942 * this host. XXX this is a bad hack. We need a more deterministic 943 * means of finding out which np fields are valid verses attr cache 944 * fields. We really should update the vattr info on the fly when 945 * making local changes. 946 */ 947 if (vap->va_size != np->n_size) { 948 if (vap->va_type == VREG) { 949 if (np->n_flag & NLMODIFIED) 950 vap->va_size = np->n_size; 951 nfs_meta_setsize(vp, curthread, vap->va_size, 0); 952 } else { 953 np->n_size = vap->va_size; 954 } 955 } 956 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 957 if (np->n_flag & NCHG) { 958 if (np->n_flag & NACC) 959 vaper->va_atime = np->n_atim; 960 if (np->n_flag & NUPD) 961 vaper->va_mtime = np->n_mtim; 962 } 963 return (0); 964 } 965 966 #ifndef NFS_NOSERVER 967 968 /* 969 * Set up nameidata for a lookup() call and do it. 970 * 971 * If pubflag is set, this call is done for a lookup operation on the 972 * public filehandle. In that case we allow crossing mountpoints and 973 * absolute pathnames. However, the caller is expected to check that 974 * the lookup result is within the public fs, and deny access if 975 * it is not. 976 * 977 * dirp may be set whether an error is returned or not, and must be 978 * released by the caller. 979 * 980 * On return nd->nl_nch usually points to the target ncp, which may represent 981 * a negative hit. 982 * 983 * NOTE: the caller must call nlookup_done(nd) unconditionally on return 984 * to cleanup. 985 */ 986 int 987 nfs_namei(struct nlookupdata *nd, struct ucred *cred, int nflags, 988 struct vnode **dvpp, struct vnode **vpp, 989 fhandle_t *fhp, int len, 990 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp, 991 caddr_t *dposp, struct vnode **dirpp, struct thread *td, 992 int kerbflag, int pubflag) 993 { 994 int i, rem; 995 struct mbuf *md; 996 char *fromcp, *tocp, *cp; 997 char *namebuf; 998 struct nchandle nch; 999 struct vnode *dp; 1000 struct mount *mp; 1001 int error, rdonly; 1002 1003 namebuf = objcache_get(namei_oc, M_WAITOK); 1004 *dirpp = NULL; 1005 1006 /* 1007 * Copy the name from the mbuf list to namebuf. 1008 */ 1009 fromcp = *dposp; 1010 tocp = namebuf; 1011 md = *mdp; 1012 rem = mtod(md, caddr_t) + md->m_len - fromcp; 1013 for (i = 0; i < len; i++) { 1014 while (rem == 0) { 1015 md = md->m_next; 1016 if (md == NULL) { 1017 error = EBADRPC; 1018 goto out; 1019 } 1020 fromcp = mtod(md, caddr_t); 1021 rem = md->m_len; 1022 } 1023 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 1024 error = EACCES; 1025 goto out; 1026 } 1027 *tocp++ = *fromcp++; 1028 rem--; 1029 } 1030 *tocp = '\0'; 1031 *mdp = md; 1032 *dposp = fromcp; 1033 len = nfsm_rndup(len)-len; 1034 if (len > 0) { 1035 if (rem >= len) 1036 *dposp += len; 1037 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 1038 goto out; 1039 } 1040 1041 /* 1042 * Extract and set starting directory. The returned dp is refd 1043 * but not locked. 1044 */ 1045 error = nfsrv_fhtovp(fhp, FALSE, &mp, &dp, cred, slp, 1046 nam, &rdonly, kerbflag, pubflag); 1047 if (error) 1048 goto out; 1049 if (dp->v_type != VDIR) { 1050 vrele(dp); 1051 error = ENOTDIR; 1052 goto out; 1053 } 1054 1055 /* 1056 * Set return directory. Reference to dp is implicitly transfered 1057 * to the returned pointer. This must be set before we potentially 1058 * goto out below. 1059 */ 1060 *dirpp = dp; 1061 1062 /* 1063 * read-only - NLC_DELETE, NLC_RENAME_DST are disallowed. NLC_CREATE 1064 * is passed through to nlookup() and will be disallowed 1065 * if the file does not already exist. 1066 */ 1067 if (rdonly) { 1068 nflags |= NLC_NFS_RDONLY; 1069 if (nflags & (NLC_DELETE | NLC_RENAME_DST)) { 1070 error = EROFS; 1071 goto out; 1072 } 1073 } 1074 1075 /* 1076 * Oh joy. For WebNFS, handle those pesky '%' escapes, 1077 * and the 'native path' indicator. 1078 */ 1079 if (pubflag) { 1080 cp = objcache_get(namei_oc, M_WAITOK); 1081 fromcp = namebuf; 1082 tocp = cp; 1083 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 1084 switch ((unsigned char)*fromcp) { 1085 case WEBNFS_NATIVE_CHAR: 1086 /* 1087 * 'Native' path for us is the same 1088 * as a path according to the NFS spec, 1089 * just skip the escape char. 1090 */ 1091 fromcp++; 1092 break; 1093 /* 1094 * More may be added in the future, range 0x80-0xff 1095 */ 1096 default: 1097 error = EIO; 1098 objcache_put(namei_oc, cp); 1099 goto out; 1100 } 1101 } 1102 /* 1103 * Translate the '%' escapes, URL-style. 1104 */ 1105 while (*fromcp != '\0') { 1106 if (*fromcp == WEBNFS_ESC_CHAR) { 1107 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 1108 fromcp++; 1109 *tocp++ = HEXSTRTOI(fromcp); 1110 fromcp += 2; 1111 continue; 1112 } else { 1113 error = ENOENT; 1114 objcache_put(namei_oc, cp); 1115 goto out; 1116 } 1117 } else 1118 *tocp++ = *fromcp++; 1119 } 1120 *tocp = '\0'; 1121 objcache_put(namei_oc, namebuf); 1122 namebuf = cp; 1123 } 1124 1125 /* 1126 * Setup for search. We need to get a start directory from dp. Note 1127 * that dp is ref'd, but we no longer 'own' the ref (*dirpp owns it). 1128 */ 1129 if (pubflag == 0) { 1130 nflags |= NLC_NFS_NOSOFTLINKTRAV; 1131 nflags |= NLC_NOCROSSMOUNT; 1132 } 1133 1134 /* 1135 * We need a starting ncp from the directory vnode dp. dp must not 1136 * be locked. The returned ncp will be refd but not locked. 1137 * 1138 * If no suitable ncp is found we instruct cache_fromdvp() to create 1139 * one. If this fails the directory has probably been removed while 1140 * the target was chdir'd into it and any further lookup will fail. 1141 */ 1142 if ((error = cache_fromdvp(dp, cred, 1, &nch)) != 0) 1143 goto out; 1144 nlookup_init_raw(nd, namebuf, UIO_SYSSPACE, nflags, cred, &nch); 1145 cache_drop(&nch); 1146 1147 /* 1148 * Ok, do the lookup. 1149 */ 1150 error = nlookup(nd); 1151 1152 /* 1153 * If no error occured return the requested dvpp and vpp. If 1154 * NLC_CREATE was specified nd->nl_nch may represent a negative 1155 * cache hit in which case we do not attempt to obtain the vp. 1156 */ 1157 if (error == 0) { 1158 if (dvpp) { 1159 if (nd->nl_nch.ncp->nc_parent) { 1160 nch = nd->nl_nch; 1161 nch.ncp = nch.ncp->nc_parent; 1162 cache_hold(&nch); 1163 cache_lock(&nch); 1164 error = cache_vget(&nch, nd->nl_cred, 1165 LK_EXCLUSIVE, dvpp); 1166 cache_put(&nch); 1167 } else { 1168 error = ENXIO; 1169 } 1170 } 1171 if (vpp && nd->nl_nch.ncp->nc_vp) { 1172 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, vpp); 1173 } 1174 if (error) { 1175 if (dvpp && *dvpp) { 1176 vput(*dvpp); 1177 *dvpp = NULL; 1178 } 1179 if (vpp && *vpp) { 1180 vput(*vpp); 1181 *vpp = NULL; 1182 } 1183 } 1184 } 1185 1186 /* 1187 * Finish up. 1188 */ 1189 out: 1190 objcache_put(namei_oc, namebuf); 1191 return (error); 1192 } 1193 1194 /* 1195 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1196 * - look up fsid in mount list (if not found ret error) 1197 * - get vp and export rights by calling VFS_FHTOVP() 1198 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1199 * - if not lockflag unlock it with vn_unlock() 1200 */ 1201 int 1202 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, 1203 struct mount **mpp, struct vnode **vpp, 1204 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam, 1205 int *rdonlyp, int kerbflag, int pubflag) 1206 { 1207 struct mount *mp; 1208 int i; 1209 struct ucred *credanon; 1210 int error, exflags; 1211 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */ 1212 struct sockaddr_int *saddr; 1213 #endif 1214 1215 *vpp = NULL; 1216 *mpp = NULL; 1217 1218 if (nfs_ispublicfh(fhp)) { 1219 if (!pubflag || !nfs_pub.np_valid) 1220 return (ESTALE); 1221 fhp = &nfs_pub.np_handle; 1222 } 1223 1224 mp = *mpp = vfs_getvfs(&fhp->fh_fsid); 1225 if (mp == NULL) 1226 return (ESTALE); 1227 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon); 1228 if (error) 1229 return (error); 1230 error = VFS_FHTOVP(mp, NULL, &fhp->fh_fid, vpp); 1231 if (error) 1232 return (error); 1233 #ifdef MNT_EXNORESPORT 1234 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 1235 saddr = (struct sockaddr_in *)nam; 1236 if (saddr->sin_family == AF_INET && 1237 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 1238 vput(*vpp); 1239 *vpp = NULL; 1240 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1241 } 1242 } 1243 #endif 1244 /* 1245 * Check/setup credentials. 1246 */ 1247 if (exflags & MNT_EXKERB) { 1248 if (!kerbflag) { 1249 vput(*vpp); 1250 *vpp = NULL; 1251 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1252 } 1253 } else if (kerbflag) { 1254 vput(*vpp); 1255 *vpp = NULL; 1256 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1257 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1258 cred->cr_uid = credanon->cr_uid; 1259 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1260 cred->cr_groups[i] = credanon->cr_groups[i]; 1261 cred->cr_ngroups = i; 1262 } 1263 if (exflags & MNT_EXRDONLY) 1264 *rdonlyp = 1; 1265 else 1266 *rdonlyp = 0; 1267 1268 if (!lockflag) 1269 vn_unlock(*vpp); 1270 return (0); 1271 } 1272 1273 /* 1274 * WebNFS: check if a filehandle is a public filehandle. For v3, this 1275 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 1276 * transformed this to all zeroes in both cases, so check for it. 1277 */ 1278 int 1279 nfs_ispublicfh(fhandle_t *fhp) 1280 { 1281 char *cp = (char *)fhp; 1282 int i; 1283 1284 for (i = 0; i < NFSX_V3FH; i++) 1285 if (*cp++ != 0) 1286 return (FALSE); 1287 return (TRUE); 1288 } 1289 1290 #endif /* NFS_NOSERVER */ 1291 /* 1292 * This function compares two net addresses by family and returns TRUE 1293 * if they are the same host. 1294 * If there is any doubt, return FALSE. 1295 * The AF_INET family is handled as a special case so that address mbufs 1296 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1297 */ 1298 int 1299 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam) 1300 { 1301 struct sockaddr_in *inetaddr; 1302 1303 switch (family) { 1304 case AF_INET: 1305 inetaddr = (struct sockaddr_in *)nam; 1306 if (inetaddr->sin_family == AF_INET && 1307 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 1308 return (1); 1309 break; 1310 default: 1311 break; 1312 }; 1313 return (0); 1314 } 1315 1316 static nfsuint64 nfs_nullcookie = { { 0, 0 } }; 1317 /* 1318 * This function finds the directory cookie that corresponds to the 1319 * logical byte offset given. 1320 */ 1321 nfsuint64 * 1322 nfs_getcookie(struct nfsnode *np, off_t off, int add) 1323 { 1324 struct nfsdmap *dp, *dp2; 1325 int pos; 1326 1327 pos = (uoff_t)off / NFS_DIRBLKSIZ; 1328 if (pos == 0 || off < 0) { 1329 #ifdef DIAGNOSTIC 1330 if (add) 1331 panic("nfs getcookie add at <= 0"); 1332 #endif 1333 return (&nfs_nullcookie); 1334 } 1335 pos--; 1336 dp = np->n_cookies.lh_first; 1337 if (!dp) { 1338 if (add) { 1339 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 1340 M_NFSDIROFF, M_WAITOK); 1341 dp->ndm_eocookie = 0; 1342 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 1343 } else 1344 return (NULL); 1345 } 1346 while (pos >= NFSNUMCOOKIES) { 1347 pos -= NFSNUMCOOKIES; 1348 if (dp->ndm_list.le_next) { 1349 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 1350 pos >= dp->ndm_eocookie) 1351 return (NULL); 1352 dp = dp->ndm_list.le_next; 1353 } else if (add) { 1354 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 1355 M_NFSDIROFF, M_WAITOK); 1356 dp2->ndm_eocookie = 0; 1357 LIST_INSERT_AFTER(dp, dp2, ndm_list); 1358 dp = dp2; 1359 } else 1360 return (NULL); 1361 } 1362 if (pos >= dp->ndm_eocookie) { 1363 if (add) 1364 dp->ndm_eocookie = pos + 1; 1365 else 1366 return (NULL); 1367 } 1368 return (&dp->ndm_cookies[pos]); 1369 } 1370 1371 /* 1372 * Invalidate cached directory information, except for the actual directory 1373 * blocks (which are invalidated separately). 1374 * Done mainly to avoid the use of stale offset cookies. 1375 */ 1376 void 1377 nfs_invaldir(struct vnode *vp) 1378 { 1379 struct nfsnode *np = VTONFS(vp); 1380 1381 #ifdef DIAGNOSTIC 1382 if (vp->v_type != VDIR) 1383 panic("nfs: invaldir not dir"); 1384 #endif 1385 np->n_direofoffset = 0; 1386 np->n_cookieverf.nfsuquad[0] = 0; 1387 np->n_cookieverf.nfsuquad[1] = 0; 1388 if (np->n_cookies.lh_first) 1389 np->n_cookies.lh_first->ndm_eocookie = 0; 1390 } 1391 1392 /* 1393 * Set the v_type field for an NFS client's vnode and initialize for 1394 * buffer cache operations if necessary. 1395 */ 1396 void 1397 nfs_setvtype(struct vnode *vp, enum vtype vtyp) 1398 { 1399 vp->v_type = vtyp; 1400 1401 switch(vtyp) { 1402 case VREG: 1403 case VDIR: 1404 case VLNK: 1405 /* 1406 * Needs VMIO, size not yet known, and blocksize 1407 * is not really relevant if we are passing a 1408 * filesize of 0. 1409 */ 1410 vinitvmio(vp, 0, PAGE_SIZE, -1); 1411 break; 1412 default: 1413 break; 1414 } 1415 } 1416 1417 /* 1418 * The write verifier has changed (probably due to a server reboot), so all 1419 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 1420 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 1421 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 1422 * mount point. 1423 * 1424 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 1425 * writes are not clusterable. 1426 */ 1427 1428 static int nfs_clearcommit_bp(struct buf *bp, void *data __unused); 1429 static int nfs_clearcommit_callback(struct mount *mp, struct vnode *vp, 1430 void *data __unused); 1431 1432 void 1433 nfs_clearcommit(struct mount *mp) 1434 { 1435 vmntvnodescan(mp, VMSC_NOWAIT, nfs_clearcommit_callback, NULL, NULL); 1436 } 1437 1438 static int 1439 nfs_clearcommit_callback(struct mount *mp, struct vnode *vp, 1440 void *data __unused) 1441 { 1442 vhold(vp); 1443 lwkt_gettoken(&vp->v_token); 1444 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 1445 nfs_clearcommit_bp, NULL); 1446 lwkt_reltoken(&vp->v_token); 1447 vdrop(vp); 1448 return(0); 1449 } 1450 1451 static int 1452 nfs_clearcommit_bp(struct buf *bp, void *data __unused) 1453 { 1454 if (BUF_REFCNT(bp) == 0 && 1455 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 1456 == (B_DELWRI | B_NEEDCOMMIT)) { 1457 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 1458 } 1459 return(0); 1460 } 1461 1462 #ifndef NFS_NOSERVER 1463 /* 1464 * Map errnos to NFS error numbers. For Version 3 also filter out error 1465 * numbers not specified for the associated procedure. 1466 */ 1467 int 1468 nfsrv_errmap(struct nfsrv_descript *nd, int err) 1469 { 1470 short *defaulterrp, *errp; 1471 1472 if (nd->nd_flag & ND_NFSV3) { 1473 if (nd->nd_procnum <= NFSPROC_COMMIT) { 1474 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 1475 while (*++errp) { 1476 if (*errp == err) 1477 return (err); 1478 else if (*errp > err) 1479 break; 1480 } 1481 return ((int)*defaulterrp); 1482 } else 1483 return (err & 0xffff); 1484 } 1485 if (err <= ELAST) 1486 return ((int)nfsrv_v2errmap[err - 1]); 1487 return (NFSERR_IO); 1488 } 1489 1490 /* 1491 * Sort the group list in increasing numerical order. 1492 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 1493 * that used to be here.) 1494 */ 1495 void 1496 nfsrvw_sort(gid_t *list, int num) 1497 { 1498 int i, j; 1499 gid_t v; 1500 1501 /* Insertion sort. */ 1502 for (i = 1; i < num; i++) { 1503 v = list[i]; 1504 /* find correct slot for value v, moving others up */ 1505 for (j = i; --j >= 0 && v < list[j];) 1506 list[j + 1] = list[j]; 1507 list[j + 1] = v; 1508 } 1509 } 1510 1511 /* 1512 * copy credentials making sure that the result can be compared with bcmp(). 1513 */ 1514 void 1515 nfsrv_setcred(struct ucred *incred, struct ucred *outcred) 1516 { 1517 int i; 1518 1519 bzero((caddr_t)outcred, sizeof (struct ucred)); 1520 outcred->cr_ref = 1; 1521 outcred->cr_uid = incred->cr_uid; 1522 outcred->cr_ngroups = incred->cr_ngroups; 1523 for (i = 0; i < incred->cr_ngroups; i++) 1524 outcred->cr_groups[i] = incred->cr_groups[i]; 1525 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 1526 } 1527 #endif /* NFS_NOSERVER */ 1528