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