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