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.34 2006/02/21 19:00:19 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 63 #include <vm/vm.h> 64 #include <vm/vm_object.h> 65 #include <vm/vm_extern.h> 66 #include <vm/vm_zone.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 "nqnfs.h" 78 #include "nfsrtt.h" 79 80 #include <netinet/in.h> 81 82 /* 83 * Data items converted to xdr at startup, since they are constant 84 * This is kinda hokey, but may save a little time doing byte swaps 85 */ 86 u_int32_t nfs_xdrneg1; 87 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr, 88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted, 89 rpc_auth_kerb; 90 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false; 91 92 /* And other global data */ 93 static u_int32_t nfs_xid = 0; 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 int nfs_pbuf_freecnt = -1; /* start out unlimited */ 103 104 struct nfs_reqq nfs_reqq; 105 struct nfssvc_sockhead nfssvc_sockhead; 106 int nfssvc_sockhead_flag; 107 struct nfsd_head nfsd_head; 108 int nfsd_head_flag; 109 struct nfs_bufq nfs_bufq; 110 struct nqtimerhead nqtimerhead; 111 struct nqfhhashhead *nqfhhashtbl; 112 u_long nqfhhash; 113 114 static void (*nfs_prev_lease_updatetime) (int); 115 static int nfs_prev_nfssvc_sy_narg; 116 static sy_call_t *nfs_prev_nfssvc_sy_call; 117 118 #ifndef NFS_NOSERVER 119 120 static int (*nfs_prev_vop_lease_check)(struct vop_lease_args *); 121 122 /* 123 * Mapping of old NFS Version 2 RPC numbers to generic numbers. 124 */ 125 int nfsv3_procid[NFS_NPROCS] = { 126 NFSPROC_NULL, 127 NFSPROC_GETATTR, 128 NFSPROC_SETATTR, 129 NFSPROC_NOOP, 130 NFSPROC_LOOKUP, 131 NFSPROC_READLINK, 132 NFSPROC_READ, 133 NFSPROC_NOOP, 134 NFSPROC_WRITE, 135 NFSPROC_CREATE, 136 NFSPROC_REMOVE, 137 NFSPROC_RENAME, 138 NFSPROC_LINK, 139 NFSPROC_SYMLINK, 140 NFSPROC_MKDIR, 141 NFSPROC_RMDIR, 142 NFSPROC_READDIR, 143 NFSPROC_FSSTAT, 144 NFSPROC_NOOP, 145 NFSPROC_NOOP, 146 NFSPROC_NOOP, 147 NFSPROC_NOOP, 148 NFSPROC_NOOP, 149 NFSPROC_NOOP, 150 NFSPROC_NOOP, 151 NFSPROC_NOOP 152 }; 153 154 #endif /* NFS_NOSERVER */ 155 /* 156 * and the reverse mapping from generic to Version 2 procedure numbers 157 */ 158 int nfsv2_procid[NFS_NPROCS] = { 159 NFSV2PROC_NULL, 160 NFSV2PROC_GETATTR, 161 NFSV2PROC_SETATTR, 162 NFSV2PROC_LOOKUP, 163 NFSV2PROC_NOOP, 164 NFSV2PROC_READLINK, 165 NFSV2PROC_READ, 166 NFSV2PROC_WRITE, 167 NFSV2PROC_CREATE, 168 NFSV2PROC_MKDIR, 169 NFSV2PROC_SYMLINK, 170 NFSV2PROC_CREATE, 171 NFSV2PROC_REMOVE, 172 NFSV2PROC_RMDIR, 173 NFSV2PROC_RENAME, 174 NFSV2PROC_LINK, 175 NFSV2PROC_READDIR, 176 NFSV2PROC_NOOP, 177 NFSV2PROC_STATFS, 178 NFSV2PROC_NOOP, 179 NFSV2PROC_NOOP, 180 NFSV2PROC_NOOP, 181 NFSV2PROC_NOOP, 182 NFSV2PROC_NOOP, 183 NFSV2PROC_NOOP, 184 NFSV2PROC_NOOP, 185 }; 186 187 #ifndef NFS_NOSERVER 188 /* 189 * Maps errno values to nfs error numbers. 190 * Use NFSERR_IO as the catch all for ones not specifically defined in 191 * RFC 1094. 192 */ 193 static u_char nfsrv_v2errmap[ELAST] = { 194 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO, 195 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 196 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO, 197 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR, 198 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 199 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS, 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_IO, NFSERR_IO, NFSERR_IO, 206 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO, 207 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE, 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, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, 211 NFSERR_IO /* << Last is 86 */ 212 }; 213 214 /* 215 * Maps errno values to nfs error numbers. 216 * Although it is not obvious whether or not NFS clients really care if 217 * a returned error value is in the specified list for the procedure, the 218 * safest thing to do is filter them appropriately. For Version 2, the 219 * X/Open XNFS document is the only specification that defines error values 220 * for each RPC (The RFC simply lists all possible error values for all RPCs), 221 * so I have decided to not do this for Version 2. 222 * The first entry is the default error return and the rest are the valid 223 * errors for that RPC in increasing numeric order. 224 */ 225 static short nfsv3err_null[] = { 226 0, 227 0, 228 }; 229 230 static short nfsv3err_getattr[] = { 231 NFSERR_IO, 232 NFSERR_IO, 233 NFSERR_STALE, 234 NFSERR_BADHANDLE, 235 NFSERR_SERVERFAULT, 236 0, 237 }; 238 239 static short nfsv3err_setattr[] = { 240 NFSERR_IO, 241 NFSERR_PERM, 242 NFSERR_IO, 243 NFSERR_ACCES, 244 NFSERR_INVAL, 245 NFSERR_NOSPC, 246 NFSERR_ROFS, 247 NFSERR_DQUOT, 248 NFSERR_STALE, 249 NFSERR_BADHANDLE, 250 NFSERR_NOT_SYNC, 251 NFSERR_SERVERFAULT, 252 0, 253 }; 254 255 static short nfsv3err_lookup[] = { 256 NFSERR_IO, 257 NFSERR_NOENT, 258 NFSERR_IO, 259 NFSERR_ACCES, 260 NFSERR_NOTDIR, 261 NFSERR_NAMETOL, 262 NFSERR_STALE, 263 NFSERR_BADHANDLE, 264 NFSERR_SERVERFAULT, 265 0, 266 }; 267 268 static short nfsv3err_access[] = { 269 NFSERR_IO, 270 NFSERR_IO, 271 NFSERR_STALE, 272 NFSERR_BADHANDLE, 273 NFSERR_SERVERFAULT, 274 0, 275 }; 276 277 static short nfsv3err_readlink[] = { 278 NFSERR_IO, 279 NFSERR_IO, 280 NFSERR_ACCES, 281 NFSERR_INVAL, 282 NFSERR_STALE, 283 NFSERR_BADHANDLE, 284 NFSERR_NOTSUPP, 285 NFSERR_SERVERFAULT, 286 0, 287 }; 288 289 static short nfsv3err_read[] = { 290 NFSERR_IO, 291 NFSERR_IO, 292 NFSERR_NXIO, 293 NFSERR_ACCES, 294 NFSERR_INVAL, 295 NFSERR_STALE, 296 NFSERR_BADHANDLE, 297 NFSERR_SERVERFAULT, 298 0, 299 }; 300 301 static short nfsv3err_write[] = { 302 NFSERR_IO, 303 NFSERR_IO, 304 NFSERR_ACCES, 305 NFSERR_INVAL, 306 NFSERR_FBIG, 307 NFSERR_NOSPC, 308 NFSERR_ROFS, 309 NFSERR_DQUOT, 310 NFSERR_STALE, 311 NFSERR_BADHANDLE, 312 NFSERR_SERVERFAULT, 313 0, 314 }; 315 316 static short nfsv3err_create[] = { 317 NFSERR_IO, 318 NFSERR_IO, 319 NFSERR_ACCES, 320 NFSERR_EXIST, 321 NFSERR_NOTDIR, 322 NFSERR_NOSPC, 323 NFSERR_ROFS, 324 NFSERR_NAMETOL, 325 NFSERR_DQUOT, 326 NFSERR_STALE, 327 NFSERR_BADHANDLE, 328 NFSERR_NOTSUPP, 329 NFSERR_SERVERFAULT, 330 0, 331 }; 332 333 static short nfsv3err_mkdir[] = { 334 NFSERR_IO, 335 NFSERR_IO, 336 NFSERR_ACCES, 337 NFSERR_EXIST, 338 NFSERR_NOTDIR, 339 NFSERR_NOSPC, 340 NFSERR_ROFS, 341 NFSERR_NAMETOL, 342 NFSERR_DQUOT, 343 NFSERR_STALE, 344 NFSERR_BADHANDLE, 345 NFSERR_NOTSUPP, 346 NFSERR_SERVERFAULT, 347 0, 348 }; 349 350 static short nfsv3err_symlink[] = { 351 NFSERR_IO, 352 NFSERR_IO, 353 NFSERR_ACCES, 354 NFSERR_EXIST, 355 NFSERR_NOTDIR, 356 NFSERR_NOSPC, 357 NFSERR_ROFS, 358 NFSERR_NAMETOL, 359 NFSERR_DQUOT, 360 NFSERR_STALE, 361 NFSERR_BADHANDLE, 362 NFSERR_NOTSUPP, 363 NFSERR_SERVERFAULT, 364 0, 365 }; 366 367 static short nfsv3err_mknod[] = { 368 NFSERR_IO, 369 NFSERR_IO, 370 NFSERR_ACCES, 371 NFSERR_EXIST, 372 NFSERR_NOTDIR, 373 NFSERR_NOSPC, 374 NFSERR_ROFS, 375 NFSERR_NAMETOL, 376 NFSERR_DQUOT, 377 NFSERR_STALE, 378 NFSERR_BADHANDLE, 379 NFSERR_NOTSUPP, 380 NFSERR_SERVERFAULT, 381 NFSERR_BADTYPE, 382 0, 383 }; 384 385 static short nfsv3err_remove[] = { 386 NFSERR_IO, 387 NFSERR_NOENT, 388 NFSERR_IO, 389 NFSERR_ACCES, 390 NFSERR_NOTDIR, 391 NFSERR_ROFS, 392 NFSERR_NAMETOL, 393 NFSERR_STALE, 394 NFSERR_BADHANDLE, 395 NFSERR_SERVERFAULT, 396 0, 397 }; 398 399 static short nfsv3err_rmdir[] = { 400 NFSERR_IO, 401 NFSERR_NOENT, 402 NFSERR_IO, 403 NFSERR_ACCES, 404 NFSERR_EXIST, 405 NFSERR_NOTDIR, 406 NFSERR_INVAL, 407 NFSERR_ROFS, 408 NFSERR_NAMETOL, 409 NFSERR_NOTEMPTY, 410 NFSERR_STALE, 411 NFSERR_BADHANDLE, 412 NFSERR_NOTSUPP, 413 NFSERR_SERVERFAULT, 414 0, 415 }; 416 417 static short nfsv3err_rename[] = { 418 NFSERR_IO, 419 NFSERR_NOENT, 420 NFSERR_IO, 421 NFSERR_ACCES, 422 NFSERR_EXIST, 423 NFSERR_XDEV, 424 NFSERR_NOTDIR, 425 NFSERR_ISDIR, 426 NFSERR_INVAL, 427 NFSERR_NOSPC, 428 NFSERR_ROFS, 429 NFSERR_MLINK, 430 NFSERR_NAMETOL, 431 NFSERR_NOTEMPTY, 432 NFSERR_DQUOT, 433 NFSERR_STALE, 434 NFSERR_BADHANDLE, 435 NFSERR_NOTSUPP, 436 NFSERR_SERVERFAULT, 437 0, 438 }; 439 440 static short nfsv3err_link[] = { 441 NFSERR_IO, 442 NFSERR_IO, 443 NFSERR_ACCES, 444 NFSERR_EXIST, 445 NFSERR_XDEV, 446 NFSERR_NOTDIR, 447 NFSERR_INVAL, 448 NFSERR_NOSPC, 449 NFSERR_ROFS, 450 NFSERR_MLINK, 451 NFSERR_NAMETOL, 452 NFSERR_DQUOT, 453 NFSERR_STALE, 454 NFSERR_BADHANDLE, 455 NFSERR_NOTSUPP, 456 NFSERR_SERVERFAULT, 457 0, 458 }; 459 460 static short nfsv3err_readdir[] = { 461 NFSERR_IO, 462 NFSERR_IO, 463 NFSERR_ACCES, 464 NFSERR_NOTDIR, 465 NFSERR_STALE, 466 NFSERR_BADHANDLE, 467 NFSERR_BAD_COOKIE, 468 NFSERR_TOOSMALL, 469 NFSERR_SERVERFAULT, 470 0, 471 }; 472 473 static short nfsv3err_readdirplus[] = { 474 NFSERR_IO, 475 NFSERR_IO, 476 NFSERR_ACCES, 477 NFSERR_NOTDIR, 478 NFSERR_STALE, 479 NFSERR_BADHANDLE, 480 NFSERR_BAD_COOKIE, 481 NFSERR_NOTSUPP, 482 NFSERR_TOOSMALL, 483 NFSERR_SERVERFAULT, 484 0, 485 }; 486 487 static short nfsv3err_fsstat[] = { 488 NFSERR_IO, 489 NFSERR_IO, 490 NFSERR_STALE, 491 NFSERR_BADHANDLE, 492 NFSERR_SERVERFAULT, 493 0, 494 }; 495 496 static short nfsv3err_fsinfo[] = { 497 NFSERR_STALE, 498 NFSERR_STALE, 499 NFSERR_BADHANDLE, 500 NFSERR_SERVERFAULT, 501 0, 502 }; 503 504 static short nfsv3err_pathconf[] = { 505 NFSERR_STALE, 506 NFSERR_STALE, 507 NFSERR_BADHANDLE, 508 NFSERR_SERVERFAULT, 509 0, 510 }; 511 512 static short nfsv3err_commit[] = { 513 NFSERR_IO, 514 NFSERR_IO, 515 NFSERR_STALE, 516 NFSERR_BADHANDLE, 517 NFSERR_SERVERFAULT, 518 0, 519 }; 520 521 static short *nfsrv_v3errmap[] = { 522 nfsv3err_null, 523 nfsv3err_getattr, 524 nfsv3err_setattr, 525 nfsv3err_lookup, 526 nfsv3err_access, 527 nfsv3err_readlink, 528 nfsv3err_read, 529 nfsv3err_write, 530 nfsv3err_create, 531 nfsv3err_mkdir, 532 nfsv3err_symlink, 533 nfsv3err_mknod, 534 nfsv3err_remove, 535 nfsv3err_rmdir, 536 nfsv3err_rename, 537 nfsv3err_link, 538 nfsv3err_readdir, 539 nfsv3err_readdirplus, 540 nfsv3err_fsstat, 541 nfsv3err_fsinfo, 542 nfsv3err_pathconf, 543 nfsv3err_commit, 544 }; 545 546 #endif /* NFS_NOSERVER */ 547 548 extern struct nfsrtt nfsrtt; 549 extern time_t nqnfsstarttime; 550 extern int nqsrv_clockskew; 551 extern int nqsrv_writeslack; 552 extern int nqsrv_maxlease; 553 extern struct nfsstats nfsstats; 554 extern int nqnfs_piggy[NFS_NPROCS]; 555 extern nfstype nfsv2_type[9]; 556 extern nfstype nfsv3_type[9]; 557 extern struct nfsnodehashhead *nfsnodehashtbl; 558 extern u_long nfsnodehash; 559 560 struct nfssvc_args; 561 extern int nfssvc(struct proc *, struct nfssvc_args *, int *); 562 563 LIST_HEAD(nfsnodehashhead, nfsnode); 564 565 /* 566 * This needs to return a monotonically increasing or close to monotonically 567 * increasing result, otherwise the write gathering queues won't work 568 * properly. 569 */ 570 u_quad_t 571 nfs_curusec(void) 572 { 573 struct timeval tv; 574 575 getmicrouptime(&tv); 576 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec); 577 } 578 579 /* 580 * Create the header for an rpc request packet 581 * The hsiz is the size of the rest of the nfs request header. 582 * (just used to decide if a cluster is a good idea) 583 */ 584 struct mbuf * 585 nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp) 586 { 587 struct mbuf *mb; 588 u_int32_t *tl; 589 caddr_t bpos; 590 struct mbuf *mb2; 591 struct nfsmount *nmp; 592 int nqflag; 593 594 mb = m_getl(hsiz, MB_WAIT, MT_DATA, 0, NULL); 595 mb->m_len = 0; 596 bpos = mtod(mb, caddr_t); 597 598 /* 599 * For NQNFS, add lease request. 600 */ 601 if (vp) { 602 nmp = VFSTONFS(vp->v_mount); 603 if (nmp->nm_flag & NFSMNT_NQNFS) { 604 nqflag = NQNFS_NEEDLEASE(vp, procid); 605 if (nqflag) { 606 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED); 607 *tl++ = txdr_unsigned(nqflag); 608 *tl = txdr_unsigned(nmp->nm_leaseterm); 609 } else { 610 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 611 *tl = 0; 612 } 613 } 614 } 615 /* Finally, return values */ 616 *bposp = bpos; 617 return (mb); 618 } 619 620 /* 621 * Build the RPC header and fill in the authorization info. 622 * The authorization string argument is only used when the credentials 623 * come from outside of the kernel. 624 * Returns the head of the mbuf list. 625 */ 626 struct mbuf * 627 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type, 628 int auth_len, char *auth_str, int verf_len, char *verf_str, 629 struct mbuf *mrest, int mrest_len, struct mbuf **mbp, 630 u_int32_t *xidp) 631 { 632 struct mbuf *mb; 633 u_int32_t *tl; 634 caddr_t bpos; 635 int i; 636 struct mbuf *mreq, *mb2; 637 int siz, grpsiz, authsiz, dsiz; 638 639 authsiz = nfsm_rndup(auth_len); 640 dsiz = authsiz + 10 * NFSX_UNSIGNED; 641 mb = m_getl(dsiz, MB_WAIT, MT_DATA, M_PKTHDR, NULL); 642 if (dsiz < MINCLSIZE) { 643 if (dsiz < MHLEN) 644 MH_ALIGN(mb, dsiz); 645 else 646 MH_ALIGN(mb, 8 * NFSX_UNSIGNED); 647 } 648 mb->m_len = mb->m_pkthdr.len = 0; 649 mreq = mb; 650 bpos = mtod(mb, caddr_t); 651 652 /* 653 * First the RPC header. 654 */ 655 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 656 657 /* Get a pretty random xid to start with */ 658 if (!nfs_xid) 659 nfs_xid = random(); 660 /* 661 * Skip zero xid if it should ever happen. 662 */ 663 if (++nfs_xid == 0) 664 nfs_xid++; 665 666 *tl++ = *xidp = txdr_unsigned(nfs_xid); 667 *tl++ = rpc_call; 668 *tl++ = rpc_vers; 669 if (nmflag & NFSMNT_NQNFS) { 670 *tl++ = txdr_unsigned(NQNFS_PROG); 671 *tl++ = txdr_unsigned(NQNFS_VER3); 672 } else { 673 *tl++ = txdr_unsigned(NFS_PROG); 674 if (nmflag & NFSMNT_NFSV3) 675 *tl++ = txdr_unsigned(NFS_VER3); 676 else 677 *tl++ = txdr_unsigned(NFS_VER2); 678 } 679 if (nmflag & NFSMNT_NFSV3) 680 *tl++ = txdr_unsigned(procid); 681 else 682 *tl++ = txdr_unsigned(nfsv2_procid[procid]); 683 684 /* 685 * And then the authorization cred. 686 */ 687 *tl++ = txdr_unsigned(auth_type); 688 *tl = txdr_unsigned(authsiz); 689 switch (auth_type) { 690 case RPCAUTH_UNIX: 691 nfsm_build(tl, u_int32_t *, auth_len); 692 *tl++ = 0; /* stamp ?? */ 693 *tl++ = 0; /* NULL hostname */ 694 *tl++ = txdr_unsigned(cr->cr_uid); 695 *tl++ = txdr_unsigned(cr->cr_groups[0]); 696 grpsiz = (auth_len >> 2) - 5; 697 *tl++ = txdr_unsigned(grpsiz); 698 for (i = 1; i <= grpsiz; i++) 699 *tl++ = txdr_unsigned(cr->cr_groups[i]); 700 break; 701 case RPCAUTH_KERB4: 702 siz = auth_len; 703 while (siz > 0) { 704 if (M_TRAILINGSPACE(mb) == 0) { 705 mb2 = m_getl(siz, MB_WAIT, MT_DATA, 0, NULL); 706 mb2->m_len = 0; 707 mb->m_next = mb2; 708 mb = mb2; 709 bpos = mtod(mb, caddr_t); 710 } 711 i = min(siz, M_TRAILINGSPACE(mb)); 712 bcopy(auth_str, bpos, i); 713 mb->m_len += i; 714 auth_str += i; 715 bpos += i; 716 siz -= i; 717 } 718 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) { 719 for (i = 0; i < siz; i++) 720 *bpos++ = '\0'; 721 mb->m_len += siz; 722 } 723 break; 724 }; 725 726 /* 727 * And the verifier... 728 */ 729 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 730 if (verf_str) { 731 *tl++ = txdr_unsigned(RPCAUTH_KERB4); 732 *tl = txdr_unsigned(verf_len); 733 siz = verf_len; 734 while (siz > 0) { 735 if (M_TRAILINGSPACE(mb) == 0) { 736 mb2 = m_getl(siz, MB_WAIT, MT_DATA, 0, NULL); 737 mb2->m_len = 0; 738 mb->m_next = mb2; 739 mb = mb2; 740 bpos = mtod(mb, caddr_t); 741 } 742 i = min(siz, M_TRAILINGSPACE(mb)); 743 bcopy(verf_str, bpos, i); 744 mb->m_len += i; 745 verf_str += i; 746 bpos += i; 747 siz -= i; 748 } 749 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) { 750 for (i = 0; i < siz; i++) 751 *bpos++ = '\0'; 752 mb->m_len += siz; 753 } 754 } else { 755 *tl++ = txdr_unsigned(RPCAUTH_NULL); 756 *tl = 0; 757 } 758 mb->m_next = mrest; 759 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len; 760 mreq->m_pkthdr.rcvif = (struct ifnet *)0; 761 *mbp = mb; 762 return (mreq); 763 } 764 765 /* 766 * copies mbuf chain to the uio scatter/gather list 767 */ 768 int 769 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos) 770 { 771 char *mbufcp, *uiocp; 772 int xfer, left, len; 773 struct mbuf *mp; 774 long uiosiz, rem; 775 int error = 0; 776 777 mp = *mrep; 778 mbufcp = *dpos; 779 len = mtod(mp, caddr_t)+mp->m_len-mbufcp; 780 rem = nfsm_rndup(siz)-siz; 781 while (siz > 0) { 782 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 783 return (EFBIG); 784 left = uiop->uio_iov->iov_len; 785 uiocp = uiop->uio_iov->iov_base; 786 if (left > siz) 787 left = siz; 788 uiosiz = left; 789 while (left > 0) { 790 while (len == 0) { 791 mp = mp->m_next; 792 if (mp == NULL) 793 return (EBADRPC); 794 mbufcp = mtod(mp, caddr_t); 795 len = mp->m_len; 796 } 797 xfer = (left > len) ? len : left; 798 #ifdef notdef 799 /* Not Yet.. */ 800 if (uiop->uio_iov->iov_op != NULL) 801 (*(uiop->uio_iov->iov_op)) 802 (mbufcp, uiocp, xfer); 803 else 804 #endif 805 if (uiop->uio_segflg == UIO_SYSSPACE) 806 bcopy(mbufcp, uiocp, xfer); 807 else 808 copyout(mbufcp, uiocp, xfer); 809 left -= xfer; 810 len -= xfer; 811 mbufcp += xfer; 812 uiocp += xfer; 813 uiop->uio_offset += xfer; 814 uiop->uio_resid -= xfer; 815 } 816 if (uiop->uio_iov->iov_len <= siz) { 817 uiop->uio_iovcnt--; 818 uiop->uio_iov++; 819 } else { 820 uiop->uio_iov->iov_base += uiosiz; 821 uiop->uio_iov->iov_len -= uiosiz; 822 } 823 siz -= uiosiz; 824 } 825 *dpos = mbufcp; 826 *mrep = mp; 827 if (rem > 0) { 828 if (len < rem) 829 error = nfs_adv(mrep, dpos, rem, len); 830 else 831 *dpos += rem; 832 } 833 return (error); 834 } 835 836 /* 837 * copies a uio scatter/gather list to an mbuf chain. 838 * NOTE: can ony handle iovcnt == 1 839 */ 840 int 841 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos) 842 { 843 char *uiocp; 844 struct mbuf *mp, *mp2; 845 int xfer, left, mlen; 846 int uiosiz, rem; 847 boolean_t getcluster; 848 char *cp; 849 850 #ifdef DIAGNOSTIC 851 if (uiop->uio_iovcnt != 1) 852 panic("nfsm_uiotombuf: iovcnt != 1"); 853 #endif 854 855 if (siz >= MINCLSIZE) 856 getcluster = TRUE; 857 else 858 getcluster = FALSE; 859 rem = nfsm_rndup(siz) - siz; 860 mp = mp2 = *mq; 861 while (siz > 0) { 862 left = uiop->uio_iov->iov_len; 863 uiocp = uiop->uio_iov->iov_base; 864 if (left > siz) 865 left = siz; 866 uiosiz = left; 867 while (left > 0) { 868 mlen = M_TRAILINGSPACE(mp); 869 if (mlen == 0) { 870 if (getcluster) 871 mp = m_getcl(MB_WAIT, MT_DATA, 0); 872 else 873 mp = m_get(MB_WAIT, MT_DATA); 874 mp->m_len = 0; 875 mp2->m_next = mp; 876 mp2 = mp; 877 mlen = M_TRAILINGSPACE(mp); 878 } 879 xfer = (left > mlen) ? mlen : left; 880 #ifdef notdef 881 /* Not Yet.. */ 882 if (uiop->uio_iov->iov_op != NULL) 883 (*(uiop->uio_iov->iov_op)) 884 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 885 else 886 #endif 887 if (uiop->uio_segflg == UIO_SYSSPACE) 888 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 889 else 890 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer); 891 mp->m_len += xfer; 892 left -= xfer; 893 uiocp += xfer; 894 uiop->uio_offset += xfer; 895 uiop->uio_resid -= xfer; 896 } 897 uiop->uio_iov->iov_base += uiosiz; 898 uiop->uio_iov->iov_len -= uiosiz; 899 siz -= uiosiz; 900 } 901 if (rem > 0) { 902 if (rem > M_TRAILINGSPACE(mp)) { 903 MGET(mp, MB_WAIT, MT_DATA); 904 mp->m_len = 0; 905 mp2->m_next = mp; 906 } 907 cp = mtod(mp, caddr_t)+mp->m_len; 908 for (left = 0; left < rem; left++) 909 *cp++ = '\0'; 910 mp->m_len += rem; 911 *bpos = cp; 912 } else 913 *bpos = mtod(mp, caddr_t)+mp->m_len; 914 *mq = mp; 915 return (0); 916 } 917 918 /* 919 * Help break down an mbuf chain by setting the first siz bytes contiguous 920 * pointed to by returned val. 921 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough 922 * cases. (The macros use the vars. dpos and dpos2) 923 */ 924 int 925 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2) 926 { 927 struct mbuf *mp, *mp2; 928 int siz2, xfer; 929 caddr_t p; 930 931 mp = *mdp; 932 while (left == 0) { 933 *mdp = mp = mp->m_next; 934 if (mp == NULL) 935 return (EBADRPC); 936 left = mp->m_len; 937 *dposp = mtod(mp, caddr_t); 938 } 939 if (left >= siz) { 940 *cp2 = *dposp; 941 *dposp += siz; 942 } else if (mp->m_next == NULL) { 943 return (EBADRPC); 944 } else if (siz > MHLEN) { 945 panic("nfs S too big"); 946 } else { 947 MGET(mp2, MB_WAIT, MT_DATA); 948 mp2->m_next = mp->m_next; 949 mp->m_next = mp2; 950 mp->m_len -= left; 951 mp = mp2; 952 *cp2 = p = mtod(mp, caddr_t); 953 bcopy(*dposp, p, left); /* Copy what was left */ 954 siz2 = siz-left; 955 p += left; 956 mp2 = mp->m_next; 957 /* Loop around copying up the siz2 bytes */ 958 while (siz2 > 0) { 959 if (mp2 == NULL) 960 return (EBADRPC); 961 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2; 962 if (xfer > 0) { 963 bcopy(mtod(mp2, caddr_t), p, xfer); 964 NFSMADV(mp2, xfer); 965 mp2->m_len -= xfer; 966 p += xfer; 967 siz2 -= xfer; 968 } 969 if (siz2 > 0) 970 mp2 = mp2->m_next; 971 } 972 mp->m_len = siz; 973 *mdp = mp2; 974 *dposp = mtod(mp2, caddr_t); 975 } 976 return (0); 977 } 978 979 /* 980 * Advance the position in the mbuf chain. 981 */ 982 int 983 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left) 984 { 985 struct mbuf *m; 986 int s; 987 988 m = *mdp; 989 s = left; 990 while (s < offs) { 991 offs -= s; 992 m = m->m_next; 993 if (m == NULL) 994 return (EBADRPC); 995 s = m->m_len; 996 } 997 *mdp = m; 998 *dposp = mtod(m, caddr_t)+offs; 999 return (0); 1000 } 1001 1002 /* 1003 * Copy a string into mbufs for the hard cases... 1004 */ 1005 int 1006 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz) 1007 { 1008 struct mbuf *m1 = NULL, *m2; 1009 long left, xfer, len, tlen; 1010 u_int32_t *tl; 1011 int putsize; 1012 1013 putsize = 1; 1014 m2 = *mb; 1015 left = M_TRAILINGSPACE(m2); 1016 if (left > 0) { 1017 tl = ((u_int32_t *)(*bpos)); 1018 *tl++ = txdr_unsigned(siz); 1019 putsize = 0; 1020 left -= NFSX_UNSIGNED; 1021 m2->m_len += NFSX_UNSIGNED; 1022 if (left > 0) { 1023 bcopy(cp, (caddr_t) tl, left); 1024 siz -= left; 1025 cp += left; 1026 m2->m_len += left; 1027 left = 0; 1028 } 1029 } 1030 /* Loop around adding mbufs */ 1031 while (siz > 0) { 1032 int msize; 1033 1034 m1 = m_getl(siz, MB_WAIT, MT_DATA, 0, &msize); 1035 m1->m_len = msize; 1036 m2->m_next = m1; 1037 m2 = m1; 1038 tl = mtod(m1, u_int32_t *); 1039 tlen = 0; 1040 if (putsize) { 1041 *tl++ = txdr_unsigned(siz); 1042 m1->m_len -= NFSX_UNSIGNED; 1043 tlen = NFSX_UNSIGNED; 1044 putsize = 0; 1045 } 1046 if (siz < m1->m_len) { 1047 len = nfsm_rndup(siz); 1048 xfer = siz; 1049 if (xfer < len) 1050 *(tl+(xfer>>2)) = 0; 1051 } else { 1052 xfer = len = m1->m_len; 1053 } 1054 bcopy(cp, (caddr_t) tl, xfer); 1055 m1->m_len = len+tlen; 1056 siz -= xfer; 1057 cp += xfer; 1058 } 1059 *mb = m1; 1060 *bpos = mtod(m1, caddr_t)+m1->m_len; 1061 return (0); 1062 } 1063 1064 /* 1065 * Called once to initialize data structures... 1066 */ 1067 int 1068 nfs_init(struct vfsconf *vfsp) 1069 { 1070 int i; 1071 1072 callout_init(&nfs_timer_handle); 1073 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1); 1074 1075 nfs_mount_type = vfsp->vfc_typenum; 1076 nfsrtt.pos = 0; 1077 rpc_vers = txdr_unsigned(RPC_VER2); 1078 rpc_call = txdr_unsigned(RPC_CALL); 1079 rpc_reply = txdr_unsigned(RPC_REPLY); 1080 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED); 1081 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED); 1082 rpc_mismatch = txdr_unsigned(RPC_MISMATCH); 1083 rpc_autherr = txdr_unsigned(RPC_AUTHERR); 1084 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX); 1085 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4); 1086 nfs_prog = txdr_unsigned(NFS_PROG); 1087 nqnfs_prog = txdr_unsigned(NQNFS_PROG); 1088 nfs_true = txdr_unsigned(TRUE); 1089 nfs_false = txdr_unsigned(FALSE); 1090 nfs_xdrneg1 = txdr_unsigned(-1); 1091 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000; 1092 if (nfs_ticks < 1) 1093 nfs_ticks = 1; 1094 /* Ensure async daemons disabled */ 1095 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) { 1096 nfs_iodwant[i] = NULL; 1097 nfs_iodmount[i] = (struct nfsmount *)0; 1098 } 1099 nfs_nhinit(); /* Init the nfsnode table */ 1100 #ifndef NFS_NOSERVER 1101 nfsrv_init(0); /* Init server data structures */ 1102 nfsrv_initcache(); /* Init the server request cache */ 1103 #endif 1104 1105 /* 1106 * Initialize the nqnfs server stuff. 1107 */ 1108 if (nqnfsstarttime == 0) { 1109 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease 1110 + nqsrv_clockskew + nqsrv_writeslack; 1111 NQLOADNOVRAM(nqnfsstarttime); 1112 CIRCLEQ_INIT(&nqtimerhead); 1113 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash); 1114 } 1115 1116 /* 1117 * Initialize reply list and start timer 1118 */ 1119 TAILQ_INIT(&nfs_reqq); 1120 1121 nfs_timer(0); 1122 1123 /* 1124 * Set up lease_check and lease_updatetime so that other parts 1125 * of the system can call us, if we are loadable. 1126 */ 1127 #ifndef NFS_NOSERVER 1128 nfs_prev_vop_lease_check = default_vnode_vops->vop_lease; 1129 default_vnode_vops->vop_lease = nqnfs_vop_lease_check; 1130 #endif 1131 nfs_prev_lease_updatetime = lease_updatetime; 1132 lease_updatetime = nfs_lease_updatetime; 1133 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg; 1134 sysent[SYS_nfssvc].sy_narg = 2; 1135 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call; 1136 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc; 1137 1138 nfs_pbuf_freecnt = nswbuf / 2 + 1; 1139 1140 return (0); 1141 } 1142 1143 int 1144 nfs_uninit(struct vfsconf *vfsp) 1145 { 1146 callout_stop(&nfs_timer_handle); 1147 nfs_mount_type = -1; 1148 #ifndef NFS_NOSERVER 1149 default_vnode_vops->vop_lease = nfs_prev_vop_lease_check; 1150 #endif 1151 lease_updatetime = nfs_prev_lease_updatetime; 1152 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg; 1153 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call; 1154 return (0); 1155 } 1156 1157 /* 1158 * Attribute cache routines. 1159 * nfs_loadattrcache() - loads or updates the cache contents from attributes 1160 * that are on the mbuf list 1161 * nfs_getattrcache() - returns valid attributes if found in cache, returns 1162 * error otherwise 1163 */ 1164 1165 /* 1166 * Load the attribute cache (that lives in the nfsnode entry) with 1167 * the values on the mbuf list. Load *vaper with the attributes. vaper 1168 * may be NULL. 1169 * 1170 * As a side effect n_mtime, which we use to determine if the file was 1171 * modified by some other host, is set to the attribute timestamp and 1172 * NRMODIFIED is set if the two values differ. 1173 * 1174 * WARNING: the mtime loaded into vaper does not necessarily represent 1175 * n_mtime or n_attr.mtime due to NACC and NUPD. 1176 */ 1177 int 1178 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp, 1179 struct vattr *vaper, int lattr_flags) 1180 { 1181 struct vnode *vp = *vpp; 1182 struct vattr *vap; 1183 struct nfs_fattr *fp; 1184 struct nfsnode *np; 1185 int32_t t1; 1186 caddr_t cp2; 1187 int error = 0; 1188 udev_t rdev; 1189 struct mbuf *md; 1190 enum vtype vtyp; 1191 u_short vmode; 1192 struct timespec mtime; 1193 int v3 = NFS_ISV3(vp); 1194 1195 md = *mdp; 1196 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp; 1197 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0) 1198 return (error); 1199 fp = (struct nfs_fattr *)cp2; 1200 if (v3) { 1201 vtyp = nfsv3tov_type(fp->fa_type); 1202 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1203 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1), 1204 fxdr_unsigned(int, fp->fa3_rdev.specdata2)); 1205 fxdr_nfsv3time(&fp->fa3_mtime, &mtime); 1206 } else { 1207 vtyp = nfsv2tov_type(fp->fa_type); 1208 vmode = fxdr_unsigned(u_short, fp->fa_mode); 1209 /* 1210 * XXX 1211 * 1212 * The duplicate information returned in fa_type and fa_mode 1213 * is an ambiguity in the NFS version 2 protocol. 1214 * 1215 * VREG should be taken literally as a regular file. If a 1216 * server intents to return some type information differently 1217 * in the upper bits of the mode field (e.g. for sockets, or 1218 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we 1219 * leave the examination of the mode bits even in the VREG 1220 * case to avoid breakage for bogus servers, but we make sure 1221 * that there are actually type bits set in the upper part of 1222 * fa_mode (and failing that, trust the va_type field). 1223 * 1224 * NFSv3 cleared the issue, and requires fa_mode to not 1225 * contain any type information (while also introduing sockets 1226 * and FIFOs for fa_type). 1227 */ 1228 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0)) 1229 vtyp = IFTOVT(vmode); 1230 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev); 1231 fxdr_nfsv2time(&fp->fa2_mtime, &mtime); 1232 1233 /* 1234 * Really ugly NFSv2 kludge. 1235 */ 1236 if (vtyp == VCHR && rdev == (udev_t)0xffffffff) 1237 vtyp = VFIFO; 1238 } 1239 1240 /* 1241 * If v_type == VNON it is a new node, so fill in the v_type, 1242 * n_mtime fields. Check to see if it represents a special 1243 * device, and if so, check for a possible alias. Once the 1244 * correct vnode has been obtained, fill in the rest of the 1245 * information. 1246 */ 1247 np = VTONFS(vp); 1248 if (vp->v_type != vtyp) { 1249 vp->v_type = vtyp; 1250 if (vp->v_type == VFIFO) { 1251 vp->v_ops = &vp->v_mount->mnt_vn_fifo_ops; 1252 } else if (vp->v_type == VCHR || vp->v_type == VBLK) { 1253 vp->v_ops = &vp->v_mount->mnt_vn_spec_ops; 1254 addaliasu(vp, rdev); 1255 } else { 1256 vp->v_ops = &vp->v_mount->mnt_vn_use_ops; 1257 } 1258 np->n_mtime = mtime.tv_sec; 1259 } else if (np->n_mtime != mtime.tv_sec) { 1260 /* 1261 * If we haven't modified the file locally and the server 1262 * timestamp does not match, then the server probably 1263 * modified the file. We must flag this condition so 1264 * the proper syncnronization can be done. We do not 1265 * try to synchronize the state here because that 1266 * could lead to an endless recursion. 1267 * 1268 * XXX loadattrcache can be set during the reply to a write, 1269 * before the write timestamp is properly processed. To 1270 * avoid unconditionally setting the rmodified bit (which 1271 * has the effect of flushing the cache), we only do this 1272 * check if the lmodified bit is not set. 1273 */ 1274 np->n_mtime = mtime.tv_sec; 1275 if ((lattr_flags & NFS_LATTR_NOMTIMECHECK) == 0) 1276 np->n_flag |= NRMODIFIED; 1277 } 1278 vap = &np->n_vattr; 1279 vap->va_type = vtyp; 1280 vap->va_mode = (vmode & 07777); 1281 vap->va_rdev = rdev; 1282 vap->va_mtime = mtime; 1283 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 1284 if (v3) { 1285 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1286 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1287 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1288 vap->va_size = fxdr_hyper(&fp->fa3_size); 1289 vap->va_blocksize = NFS_FABLKSIZE; 1290 vap->va_bytes = fxdr_hyper(&fp->fa3_used); 1291 vap->va_fileid = fxdr_unsigned(int32_t, 1292 fp->fa3_fileid.nfsuquad[1]); 1293 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime); 1294 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime); 1295 vap->va_flags = 0; 1296 vap->va_filerev = 0; 1297 } else { 1298 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink); 1299 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid); 1300 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid); 1301 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size); 1302 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize); 1303 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) 1304 * NFS_FABLKSIZE; 1305 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid); 1306 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime); 1307 vap->va_flags = 0; 1308 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t, 1309 fp->fa2_ctime.nfsv2_sec); 1310 vap->va_ctime.tv_nsec = 0; 1311 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec); 1312 vap->va_filerev = 0; 1313 } 1314 np->n_attrstamp = time_second; 1315 if (vap->va_size != np->n_size) { 1316 if (vap->va_type == VREG) { 1317 if ((lattr_flags & NFS_LATTR_NOSHRINK) && 1318 vap->va_size < np->n_size) { 1319 /* 1320 * We've been told not to shrink the file; 1321 * zero np->n_attrstamp to indicate that 1322 * the attributes are stale. 1323 * 1324 * This occurs primarily due to recursive 1325 * NFS ops that are executed during periods 1326 * where we cannot safely reduce the size of 1327 * the file. 1328 * 1329 * Additionally, write rpcs are broken down 1330 * into buffers and np->n_size is 1331 * pre-extended. Setting NRMODIFIED here 1332 * can result in n_size getting reset to a 1333 * lower value, which is NOT what we want. 1334 * XXX this needs to be cleaned up a lot 1335 * more. 1336 */ 1337 vap->va_size = np->n_size; 1338 np->n_attrstamp = 0; 1339 if ((np->n_flag & NLMODIFIED) == 0) 1340 np->n_flag |= NRMODIFIED; 1341 } else if (np->n_flag & NLMODIFIED) { 1342 /* 1343 * We've modified the file: Use the larger 1344 * of our size, and the server's size. At 1345 * this point the cache coherency is all 1346 * shot to hell. To try to handle multiple 1347 * clients appending to the file at the same 1348 * time mark that the server has changed 1349 * the file if the server's notion of the 1350 * file size is larger then our notion. 1351 * 1352 * XXX this needs work. 1353 */ 1354 if (vap->va_size < np->n_size) { 1355 vap->va_size = np->n_size; 1356 } else { 1357 np->n_size = vap->va_size; 1358 np->n_flag |= NRMODIFIED; 1359 } 1360 } else { 1361 /* 1362 * Someone changed the file's size on the 1363 * server and there are no local changes 1364 * to get in the way, set the size and mark 1365 * it. 1366 */ 1367 np->n_size = vap->va_size; 1368 np->n_flag |= NRMODIFIED; 1369 } 1370 vnode_pager_setsize(vp, np->n_size); 1371 } else { 1372 np->n_size = vap->va_size; 1373 } 1374 } 1375 if (vaper != NULL) { 1376 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 1377 if (np->n_flag & NCHG) { 1378 if (np->n_flag & NACC) 1379 vaper->va_atime = np->n_atim; 1380 if (np->n_flag & NUPD) 1381 vaper->va_mtime = np->n_mtim; 1382 } 1383 } 1384 return (0); 1385 } 1386 1387 #ifdef NFS_ACDEBUG 1388 #include <sys/sysctl.h> 1389 SYSCTL_DECL(_vfs_nfs); 1390 static int nfs_acdebug; 1391 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, ""); 1392 #endif 1393 1394 /* 1395 * Check the time stamp 1396 * If the cache is valid, copy contents to *vap and return 0 1397 * otherwise return an error 1398 */ 1399 int 1400 nfs_getattrcache(struct vnode *vp, struct vattr *vaper) 1401 { 1402 struct nfsnode *np; 1403 struct vattr *vap; 1404 struct nfsmount *nmp; 1405 int timeo; 1406 1407 np = VTONFS(vp); 1408 vap = &np->n_vattr; 1409 nmp = VFSTONFS(vp->v_mount); 1410 1411 /* 1412 * Dynamic timeout based on how recently the file was modified. 1413 * n_mtime is always valid. 1414 */ 1415 timeo = (get_approximate_time_t() - np->n_mtime) / 60; 1416 1417 #ifdef NFS_ACDEBUG 1418 if (nfs_acdebug>1) 1419 printf("nfs_getattrcache: initial timeo = %d\n", timeo); 1420 #endif 1421 1422 if (vap->va_type == VDIR) { 1423 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acdirmin) 1424 timeo = nmp->nm_acdirmin; 1425 else if (timeo > nmp->nm_acdirmax) 1426 timeo = nmp->nm_acdirmax; 1427 } else { 1428 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acregmin) 1429 timeo = nmp->nm_acregmin; 1430 else if (timeo > nmp->nm_acregmax) 1431 timeo = nmp->nm_acregmax; 1432 } 1433 1434 #ifdef NFS_ACDEBUG 1435 if (nfs_acdebug > 2) 1436 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n", 1437 nmp->nm_acregmin, nmp->nm_acregmax, 1438 nmp->nm_acdirmin, nmp->nm_acdirmax); 1439 1440 if (nfs_acdebug) 1441 printf("nfs_getattrcache: age = %d; final timeo = %d\n", 1442 (int)(time_second - np->n_attrstamp), timeo); 1443 #endif 1444 1445 if (np->n_attrstamp == 0 || (time_second - np->n_attrstamp) >= timeo) { 1446 nfsstats.attrcache_misses++; 1447 return (ENOENT); 1448 } 1449 nfsstats.attrcache_hits++; 1450 1451 /* 1452 * Our attribute cache can be stale due to modifications made on 1453 * this host. XXX this is a bad hack. We need a more deterministic 1454 * means of finding out which np fields are valid verses attr cache 1455 * fields. We really should update the vattr info on the fly when 1456 * making local changes. 1457 */ 1458 if (vap->va_size != np->n_size) { 1459 if (vap->va_type == VREG) { 1460 if (np->n_flag & NLMODIFIED) { 1461 if (vap->va_size < np->n_size) 1462 vap->va_size = np->n_size; 1463 else 1464 np->n_size = vap->va_size; 1465 } else { 1466 np->n_size = vap->va_size; 1467 } 1468 vnode_pager_setsize(vp, np->n_size); 1469 } else { 1470 np->n_size = vap->va_size; 1471 } 1472 } 1473 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr)); 1474 if (np->n_flag & NCHG) { 1475 if (np->n_flag & NACC) 1476 vaper->va_atime = np->n_atim; 1477 if (np->n_flag & NUPD) 1478 vaper->va_mtime = np->n_mtim; 1479 } 1480 return (0); 1481 } 1482 1483 #ifndef NFS_NOSERVER 1484 1485 /* 1486 * Set up nameidata for a lookup() call and do it. 1487 * 1488 * If pubflag is set, this call is done for a lookup operation on the 1489 * public filehandle. In that case we allow crossing mountpoints and 1490 * absolute pathnames. However, the caller is expected to check that 1491 * the lookup result is within the public fs, and deny access if 1492 * it is not. 1493 * 1494 * dirp may be set whether an error is returned or not, and must be 1495 * released by the caller. 1496 * 1497 * On return nd->nl_ncp usually points to the target ncp, which may represent 1498 * a negative hit. 1499 * 1500 * NOTE: the caller must call nlookup_done(nd) unconditionally on return 1501 * to cleanup. 1502 */ 1503 int 1504 nfs_namei(struct nlookupdata *nd, struct ucred *cred, int nameiop, 1505 struct vnode **dvpp, struct vnode **vpp, 1506 fhandle_t *fhp, int len, 1507 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp, 1508 caddr_t *dposp, struct vnode **dirpp, struct thread *td, 1509 int kerbflag, int pubflag) 1510 { 1511 int i, rem; 1512 int flags; 1513 struct mbuf *md; 1514 char *fromcp, *tocp, *cp; 1515 char *namebuf; 1516 struct namecache *ncp; 1517 struct vnode *dp; 1518 int error, rdonly; 1519 1520 namebuf = zalloc(namei_zone); 1521 flags = 0; 1522 *dirpp = NULL; 1523 1524 /* 1525 * Copy the name from the mbuf list to namebuf. 1526 */ 1527 fromcp = *dposp; 1528 tocp = namebuf; 1529 md = *mdp; 1530 rem = mtod(md, caddr_t) + md->m_len - fromcp; 1531 for (i = 0; i < len; i++) { 1532 while (rem == 0) { 1533 md = md->m_next; 1534 if (md == NULL) { 1535 error = EBADRPC; 1536 goto out; 1537 } 1538 fromcp = mtod(md, caddr_t); 1539 rem = md->m_len; 1540 } 1541 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) { 1542 error = EACCES; 1543 goto out; 1544 } 1545 *tocp++ = *fromcp++; 1546 rem--; 1547 } 1548 *tocp = '\0'; 1549 *mdp = md; 1550 *dposp = fromcp; 1551 len = nfsm_rndup(len)-len; 1552 if (len > 0) { 1553 if (rem >= len) 1554 *dposp += len; 1555 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0) 1556 goto out; 1557 } 1558 1559 /* 1560 * Extract and set starting directory. The returned dp is refd 1561 * but not locked. 1562 */ 1563 error = nfsrv_fhtovp(fhp, FALSE, &dp, cred, slp, 1564 nam, &rdonly, kerbflag, pubflag); 1565 if (error) 1566 goto out; 1567 if (dp->v_type != VDIR) { 1568 vrele(dp); 1569 error = ENOTDIR; 1570 goto out; 1571 } 1572 1573 /* 1574 * Set return directory. Reference to dp is implicitly transfered 1575 * to the returned pointer. This must be set before we potentially 1576 * goto out below. 1577 */ 1578 *dirpp = dp; 1579 1580 if (pubflag) { 1581 /* 1582 * Oh joy. For WebNFS, handle those pesky '%' escapes, 1583 * and the 'native path' indicator. 1584 */ 1585 cp = zalloc(namei_zone); 1586 fromcp = namebuf; 1587 tocp = cp; 1588 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) { 1589 switch ((unsigned char)*fromcp) { 1590 case WEBNFS_NATIVE_CHAR: 1591 /* 1592 * 'Native' path for us is the same 1593 * as a path according to the NFS spec, 1594 * just skip the escape char. 1595 */ 1596 fromcp++; 1597 break; 1598 /* 1599 * More may be added in the future, range 0x80-0xff 1600 */ 1601 default: 1602 error = EIO; 1603 zfree(namei_zone, cp); 1604 goto out; 1605 } 1606 } 1607 /* 1608 * Translate the '%' escapes, URL-style. 1609 */ 1610 while (*fromcp != '\0') { 1611 if (*fromcp == WEBNFS_ESC_CHAR) { 1612 if (fromcp[1] != '\0' && fromcp[2] != '\0') { 1613 fromcp++; 1614 *tocp++ = HEXSTRTOI(fromcp); 1615 fromcp += 2; 1616 continue; 1617 } else { 1618 error = ENOENT; 1619 zfree(namei_zone, cp); 1620 goto out; 1621 } 1622 } else 1623 *tocp++ = *fromcp++; 1624 } 1625 *tocp = '\0'; 1626 zfree(namei_zone, namebuf); 1627 namebuf = cp; 1628 } 1629 1630 /* 1631 * Setup for search. We need to get a start directory from dp. Note 1632 * that dp is ref'd, but we no longer 'own' the ref (*dirpp owns it). 1633 */ 1634 if (pubflag == 0) { 1635 flags |= NLC_NFS_NOSOFTLINKTRAV; 1636 flags |= NLC_NOCROSSMOUNT; 1637 } 1638 if (rdonly) 1639 flags |= NLC_NFS_RDONLY; 1640 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) 1641 flags |= NLC_CREATE; 1642 1643 /* 1644 * We need a starting ncp from the directory vnode dp. dp must not 1645 * be locked. The returned ncp will be refd but not locked. 1646 * 1647 * If no suitable ncp is found we instruct cache_fromdvp() to create 1648 * one. If this fails the directory has probably been removed while 1649 * the target was chdir'd into it and any further lookup will fail. 1650 */ 1651 if ((ncp = cache_fromdvp(dp, cred, 1)) == NULL) { 1652 error = EINVAL; 1653 goto out; 1654 } 1655 nlookup_init_raw(nd, namebuf, UIO_SYSSPACE, flags, cred, ncp); 1656 cache_drop(ncp); 1657 1658 /* 1659 * Ok, do the lookup. 1660 */ 1661 error = nlookup(nd); 1662 1663 /* 1664 * If no error occured return the requested dvpp and vpp. If 1665 * NLC_CREATE was specified nd->nl_ncp may represent a negative 1666 * cache hit in which case we do not attempt to obtain the vp. 1667 */ 1668 if (error == 0) { 1669 ncp = nd->nl_ncp; 1670 if (dvpp) { 1671 if (ncp->nc_parent && 1672 ncp->nc_parent->nc_mount == ncp->nc_mount) { 1673 error = cache_vget(ncp->nc_parent, nd->nl_cred, 1674 LK_EXCLUSIVE, dvpp); 1675 } else { 1676 error = ENXIO; 1677 } 1678 } 1679 if (vpp && ncp->nc_vp) { 1680 error = cache_vget(ncp, nd->nl_cred, LK_EXCLUSIVE, vpp); 1681 } 1682 if (error) { 1683 if (dvpp && *dvpp) { 1684 vput(*dvpp); 1685 *dvpp = NULL; 1686 } 1687 if (vpp && *vpp) { 1688 vput(*vpp); 1689 *vpp = NULL; 1690 } 1691 } 1692 } 1693 1694 /* 1695 * Finish up. 1696 */ 1697 out: 1698 zfree(namei_zone, namebuf); 1699 return (error); 1700 } 1701 1702 /* 1703 * A fiddled version of m_adj() that ensures null fill to a long 1704 * boundary and only trims off the back end 1705 */ 1706 void 1707 nfsm_adj(struct mbuf *mp, int len, int nul) 1708 { 1709 struct mbuf *m; 1710 int count, i; 1711 char *cp; 1712 1713 /* 1714 * Trim from tail. Scan the mbuf chain, 1715 * calculating its length and finding the last mbuf. 1716 * If the adjustment only affects this mbuf, then just 1717 * adjust and return. Otherwise, rescan and truncate 1718 * after the remaining size. 1719 */ 1720 count = 0; 1721 m = mp; 1722 for (;;) { 1723 count += m->m_len; 1724 if (m->m_next == (struct mbuf *)0) 1725 break; 1726 m = m->m_next; 1727 } 1728 if (m->m_len > len) { 1729 m->m_len -= len; 1730 if (nul > 0) { 1731 cp = mtod(m, caddr_t)+m->m_len-nul; 1732 for (i = 0; i < nul; i++) 1733 *cp++ = '\0'; 1734 } 1735 return; 1736 } 1737 count -= len; 1738 if (count < 0) 1739 count = 0; 1740 /* 1741 * Correct length for chain is "count". 1742 * Find the mbuf with last data, adjust its length, 1743 * and toss data from remaining mbufs on chain. 1744 */ 1745 for (m = mp; m; m = m->m_next) { 1746 if (m->m_len >= count) { 1747 m->m_len = count; 1748 if (nul > 0) { 1749 cp = mtod(m, caddr_t)+m->m_len-nul; 1750 for (i = 0; i < nul; i++) 1751 *cp++ = '\0'; 1752 } 1753 break; 1754 } 1755 count -= m->m_len; 1756 } 1757 for (m = m->m_next;m;m = m->m_next) 1758 m->m_len = 0; 1759 } 1760 1761 /* 1762 * Make these functions instead of macros, so that the kernel text size 1763 * doesn't get too big... 1764 */ 1765 void 1766 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret, 1767 struct vattr *before_vap, int after_ret, struct vattr *after_vap, 1768 struct mbuf **mbp, char **bposp) 1769 { 1770 struct mbuf *mb = *mbp, *mb2; 1771 char *bpos = *bposp; 1772 u_int32_t *tl; 1773 1774 /* 1775 * before_ret is 0 if before_vap is valid, non-zero if it isn't. 1776 */ 1777 if (before_ret) { 1778 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1779 *tl = nfs_false; 1780 } else { 1781 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 1782 *tl++ = nfs_true; 1783 txdr_hyper(before_vap->va_size, tl); 1784 tl += 2; 1785 txdr_nfsv3time(&(before_vap->va_mtime), tl); 1786 tl += 2; 1787 txdr_nfsv3time(&(before_vap->va_ctime), tl); 1788 } 1789 *bposp = bpos; 1790 *mbp = mb; 1791 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp); 1792 } 1793 1794 void 1795 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret, 1796 struct vattr *after_vap, struct mbuf **mbp, char **bposp) 1797 { 1798 struct mbuf *mb = *mbp, *mb2; 1799 char *bpos = *bposp; 1800 u_int32_t *tl; 1801 struct nfs_fattr *fp; 1802 1803 if (after_ret) { 1804 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1805 *tl = nfs_false; 1806 } else { 1807 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR); 1808 *tl++ = nfs_true; 1809 fp = (struct nfs_fattr *)tl; 1810 nfsm_srvfattr(nfsd, after_vap, fp); 1811 } 1812 *mbp = mb; 1813 *bposp = bpos; 1814 } 1815 1816 void 1817 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap, 1818 struct nfs_fattr *fp) 1819 { 1820 1821 fp->fa_nlink = txdr_unsigned(vap->va_nlink); 1822 fp->fa_uid = txdr_unsigned(vap->va_uid); 1823 fp->fa_gid = txdr_unsigned(vap->va_gid); 1824 if (nfsd->nd_flag & ND_NFSV3) { 1825 fp->fa_type = vtonfsv3_type(vap->va_type); 1826 fp->fa_mode = vtonfsv3_mode(vap->va_mode); 1827 txdr_hyper(vap->va_size, &fp->fa3_size); 1828 txdr_hyper(vap->va_bytes, &fp->fa3_used); 1829 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev)); 1830 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev)); 1831 fp->fa3_fsid.nfsuquad[0] = 0; 1832 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid); 1833 fp->fa3_fileid.nfsuquad[0] = 0; 1834 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid); 1835 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime); 1836 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime); 1837 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime); 1838 } else { 1839 fp->fa_type = vtonfsv2_type(vap->va_type); 1840 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1841 fp->fa2_size = txdr_unsigned(vap->va_size); 1842 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize); 1843 if (vap->va_type == VFIFO) 1844 fp->fa2_rdev = 0xffffffff; 1845 else 1846 fp->fa2_rdev = txdr_unsigned(vap->va_rdev); 1847 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); 1848 fp->fa2_fsid = txdr_unsigned(vap->va_fsid); 1849 fp->fa2_fileid = txdr_unsigned(vap->va_fileid); 1850 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime); 1851 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime); 1852 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime); 1853 } 1854 } 1855 1856 /* 1857 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked) 1858 * - look up fsid in mount list (if not found ret error) 1859 * - get vp and export rights by calling VFS_FHTOVP() 1860 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 1861 * - if not lockflag unlock it with VOP_UNLOCK() 1862 */ 1863 int 1864 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp, 1865 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam, 1866 int *rdonlyp, int kerbflag, int pubflag) 1867 { 1868 struct thread *td = curthread; /* XXX */ 1869 struct mount *mp; 1870 int i; 1871 struct ucred *credanon; 1872 int error, exflags; 1873 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */ 1874 struct sockaddr_int *saddr; 1875 #endif 1876 1877 *vpp = (struct vnode *)0; 1878 1879 if (nfs_ispublicfh(fhp)) { 1880 if (!pubflag || !nfs_pub.np_valid) 1881 return (ESTALE); 1882 fhp = &nfs_pub.np_handle; 1883 } 1884 1885 mp = vfs_getvfs(&fhp->fh_fsid); 1886 if (!mp) 1887 return (ESTALE); 1888 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon); 1889 if (error) 1890 return (error); 1891 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp); 1892 if (error) 1893 return (error); 1894 #ifdef MNT_EXNORESPORT 1895 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) { 1896 saddr = (struct sockaddr_in *)nam; 1897 if (saddr->sin_family == AF_INET && 1898 ntohs(saddr->sin_port) >= IPPORT_RESERVED) { 1899 vput(*vpp); 1900 *vpp = NULL; 1901 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1902 } 1903 } 1904 #endif 1905 /* 1906 * Check/setup credentials. 1907 */ 1908 if (exflags & MNT_EXKERB) { 1909 if (!kerbflag) { 1910 vput(*vpp); 1911 *vpp = NULL; 1912 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1913 } 1914 } else if (kerbflag) { 1915 vput(*vpp); 1916 *vpp = NULL; 1917 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 1918 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) { 1919 cred->cr_uid = credanon->cr_uid; 1920 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++) 1921 cred->cr_groups[i] = credanon->cr_groups[i]; 1922 cred->cr_ngroups = i; 1923 } 1924 if (exflags & MNT_EXRDONLY) 1925 *rdonlyp = 1; 1926 else 1927 *rdonlyp = 0; 1928 1929 nfsrv_object_create(*vpp); 1930 1931 if (!lockflag) 1932 VOP_UNLOCK(*vpp, 0, td); 1933 return (0); 1934 } 1935 1936 1937 /* 1938 * WebNFS: check if a filehandle is a public filehandle. For v3, this 1939 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has 1940 * transformed this to all zeroes in both cases, so check for it. 1941 */ 1942 int 1943 nfs_ispublicfh(fhandle_t *fhp) 1944 { 1945 char *cp = (char *)fhp; 1946 int i; 1947 1948 for (i = 0; i < NFSX_V3FH; i++) 1949 if (*cp++ != 0) 1950 return (FALSE); 1951 return (TRUE); 1952 } 1953 1954 #endif /* NFS_NOSERVER */ 1955 /* 1956 * This function compares two net addresses by family and returns TRUE 1957 * if they are the same host. 1958 * If there is any doubt, return FALSE. 1959 * The AF_INET family is handled as a special case so that address mbufs 1960 * don't need to be saved to store "struct in_addr", which is only 4 bytes. 1961 */ 1962 int 1963 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam) 1964 { 1965 struct sockaddr_in *inetaddr; 1966 1967 switch (family) { 1968 case AF_INET: 1969 inetaddr = (struct sockaddr_in *)nam; 1970 if (inetaddr->sin_family == AF_INET && 1971 inetaddr->sin_addr.s_addr == haddr->had_inetaddr) 1972 return (1); 1973 break; 1974 default: 1975 break; 1976 }; 1977 return (0); 1978 } 1979 1980 static nfsuint64 nfs_nullcookie = { { 0, 0 } }; 1981 /* 1982 * This function finds the directory cookie that corresponds to the 1983 * logical byte offset given. 1984 */ 1985 nfsuint64 * 1986 nfs_getcookie(struct nfsnode *np, off_t off, int add) 1987 { 1988 struct nfsdmap *dp, *dp2; 1989 int pos; 1990 1991 pos = (uoff_t)off / NFS_DIRBLKSIZ; 1992 if (pos == 0 || off < 0) { 1993 #ifdef DIAGNOSTIC 1994 if (add) 1995 panic("nfs getcookie add at <= 0"); 1996 #endif 1997 return (&nfs_nullcookie); 1998 } 1999 pos--; 2000 dp = np->n_cookies.lh_first; 2001 if (!dp) { 2002 if (add) { 2003 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap), 2004 M_NFSDIROFF, M_WAITOK); 2005 dp->ndm_eocookie = 0; 2006 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list); 2007 } else 2008 return ((nfsuint64 *)0); 2009 } 2010 while (pos >= NFSNUMCOOKIES) { 2011 pos -= NFSNUMCOOKIES; 2012 if (dp->ndm_list.le_next) { 2013 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES && 2014 pos >= dp->ndm_eocookie) 2015 return ((nfsuint64 *)0); 2016 dp = dp->ndm_list.le_next; 2017 } else if (add) { 2018 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap), 2019 M_NFSDIROFF, M_WAITOK); 2020 dp2->ndm_eocookie = 0; 2021 LIST_INSERT_AFTER(dp, dp2, ndm_list); 2022 dp = dp2; 2023 } else 2024 return ((nfsuint64 *)0); 2025 } 2026 if (pos >= dp->ndm_eocookie) { 2027 if (add) 2028 dp->ndm_eocookie = pos + 1; 2029 else 2030 return ((nfsuint64 *)0); 2031 } 2032 return (&dp->ndm_cookies[pos]); 2033 } 2034 2035 /* 2036 * Invalidate cached directory information, except for the actual directory 2037 * blocks (which are invalidated separately). 2038 * Done mainly to avoid the use of stale offset cookies. 2039 */ 2040 void 2041 nfs_invaldir(struct vnode *vp) 2042 { 2043 struct nfsnode *np = VTONFS(vp); 2044 2045 #ifdef DIAGNOSTIC 2046 if (vp->v_type != VDIR) 2047 panic("nfs: invaldir not dir"); 2048 #endif 2049 np->n_direofoffset = 0; 2050 np->n_cookieverf.nfsuquad[0] = 0; 2051 np->n_cookieverf.nfsuquad[1] = 0; 2052 if (np->n_cookies.lh_first) 2053 np->n_cookies.lh_first->ndm_eocookie = 0; 2054 } 2055 2056 /* 2057 * The write verifier has changed (probably due to a server reboot), so all 2058 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the 2059 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT 2060 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the 2061 * mount point. 2062 * 2063 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data 2064 * writes are not clusterable. 2065 */ 2066 2067 static int nfs_clearcommit_bp(struct buf *bp, void *data __unused); 2068 2069 void 2070 nfs_clearcommit(struct mount *mp) 2071 { 2072 struct vnode *vp, *nvp; 2073 lwkt_tokref ilock; 2074 2075 lwkt_gettoken(&ilock, &mntvnode_token); 2076 crit_enter(); 2077 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) { 2078 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */ 2079 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL, 2080 nfs_clearcommit_bp, NULL); 2081 } 2082 crit_exit(); 2083 lwkt_reltoken(&ilock); 2084 } 2085 2086 static int 2087 nfs_clearcommit_bp(struct buf *bp, void *data __unused) 2088 { 2089 if (BUF_REFCNT(bp) == 0 && 2090 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) 2091 == (B_DELWRI | B_NEEDCOMMIT)) { 2092 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 2093 } 2094 return(0); 2095 } 2096 2097 #ifndef NFS_NOSERVER 2098 /* 2099 * Map errnos to NFS error numbers. For Version 3 also filter out error 2100 * numbers not specified for the associated procedure. 2101 */ 2102 int 2103 nfsrv_errmap(struct nfsrv_descript *nd, int err) 2104 { 2105 short *defaulterrp, *errp; 2106 2107 if (nd->nd_flag & ND_NFSV3) { 2108 if (nd->nd_procnum <= NFSPROC_COMMIT) { 2109 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum]; 2110 while (*++errp) { 2111 if (*errp == err) 2112 return (err); 2113 else if (*errp > err) 2114 break; 2115 } 2116 return ((int)*defaulterrp); 2117 } else 2118 return (err & 0xffff); 2119 } 2120 if (err <= ELAST) 2121 return ((int)nfsrv_v2errmap[err - 1]); 2122 return (NFSERR_IO); 2123 } 2124 2125 int 2126 nfsrv_object_create(struct vnode *vp) 2127 { 2128 struct thread *td = curthread; 2129 2130 if (vp == NULL || vp->v_type != VREG) 2131 return (1); 2132 return (vfs_object_create(vp, td)); 2133 } 2134 2135 /* 2136 * Sort the group list in increasing numerical order. 2137 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort 2138 * that used to be here.) 2139 */ 2140 void 2141 nfsrvw_sort(gid_t *list, int num) 2142 { 2143 int i, j; 2144 gid_t v; 2145 2146 /* Insertion sort. */ 2147 for (i = 1; i < num; i++) { 2148 v = list[i]; 2149 /* find correct slot for value v, moving others up */ 2150 for (j = i; --j >= 0 && v < list[j];) 2151 list[j + 1] = list[j]; 2152 list[j + 1] = v; 2153 } 2154 } 2155 2156 /* 2157 * copy credentials making sure that the result can be compared with bcmp(). 2158 */ 2159 void 2160 nfsrv_setcred(struct ucred *incred, struct ucred *outcred) 2161 { 2162 int i; 2163 2164 bzero((caddr_t)outcred, sizeof (struct ucred)); 2165 outcred->cr_ref = 1; 2166 outcred->cr_uid = incred->cr_uid; 2167 outcred->cr_ngroups = incred->cr_ngroups; 2168 for (i = 0; i < incred->cr_ngroups; i++) 2169 outcred->cr_groups[i] = incred->cr_groups[i]; 2170 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups); 2171 } 2172 #endif /* NFS_NOSERVER */ 2173