1 /* $NetBSD: nfs_vnops.c,v 1.140 2001/09/22 22:35:19 sommerfeld Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)nfs_vnops.c 8.19 (Berkeley) 7/31/95 39 */ 40 41 /* 42 * vnode op calls for Sun NFS version 2 and 3 43 */ 44 45 #include "opt_nfs.h" 46 #include "opt_uvmhist.h" 47 48 #include <sys/param.h> 49 #include <sys/proc.h> 50 #include <sys/kernel.h> 51 #include <sys/systm.h> 52 #include <sys/resourcevar.h> 53 #include <sys/proc.h> 54 #include <sys/mount.h> 55 #include <sys/buf.h> 56 #include <sys/malloc.h> 57 #include <sys/mbuf.h> 58 #include <sys/namei.h> 59 #include <sys/vnode.h> 60 #include <sys/dirent.h> 61 #include <sys/fcntl.h> 62 #include <sys/lockf.h> 63 #include <sys/stat.h> 64 #include <sys/unistd.h> 65 66 #include <uvm/uvm_extern.h> 67 #include <uvm/uvm.h> 68 69 #include <miscfs/fifofs/fifo.h> 70 #include <miscfs/genfs/genfs.h> 71 #include <miscfs/specfs/specdev.h> 72 73 #include <nfs/rpcv2.h> 74 #include <nfs/nfsproto.h> 75 #include <nfs/nfs.h> 76 #include <nfs/nfsnode.h> 77 #include <nfs/nfsmount.h> 78 #include <nfs/xdr_subs.h> 79 #include <nfs/nfsm_subs.h> 80 #include <nfs/nqnfs.h> 81 #include <nfs/nfs_var.h> 82 83 #include <net/if.h> 84 #include <netinet/in.h> 85 #include <netinet/in_var.h> 86 87 /* Defs */ 88 #define TRUE 1 89 #define FALSE 0 90 91 /* 92 * Global vfs data structures for nfs 93 */ 94 int (**nfsv2_vnodeop_p) __P((void *)); 95 const struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = { 96 { &vop_default_desc, vn_default_error }, 97 { &vop_lookup_desc, nfs_lookup }, /* lookup */ 98 { &vop_create_desc, nfs_create }, /* create */ 99 { &vop_mknod_desc, nfs_mknod }, /* mknod */ 100 { &vop_open_desc, nfs_open }, /* open */ 101 { &vop_close_desc, nfs_close }, /* close */ 102 { &vop_access_desc, nfs_access }, /* access */ 103 { &vop_getattr_desc, nfs_getattr }, /* getattr */ 104 { &vop_setattr_desc, nfs_setattr }, /* setattr */ 105 { &vop_read_desc, nfs_read }, /* read */ 106 { &vop_write_desc, nfs_write }, /* write */ 107 { &vop_lease_desc, nfs_lease_check }, /* lease */ 108 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ 109 { &vop_ioctl_desc, nfs_ioctl }, /* ioctl */ 110 { &vop_poll_desc, nfs_poll }, /* poll */ 111 { &vop_revoke_desc, nfs_revoke }, /* revoke */ 112 { &vop_mmap_desc, nfs_mmap }, /* mmap */ 113 { &vop_fsync_desc, nfs_fsync }, /* fsync */ 114 { &vop_seek_desc, nfs_seek }, /* seek */ 115 { &vop_remove_desc, nfs_remove }, /* remove */ 116 { &vop_link_desc, nfs_link }, /* link */ 117 { &vop_rename_desc, nfs_rename }, /* rename */ 118 { &vop_mkdir_desc, nfs_mkdir }, /* mkdir */ 119 { &vop_rmdir_desc, nfs_rmdir }, /* rmdir */ 120 { &vop_symlink_desc, nfs_symlink }, /* symlink */ 121 { &vop_readdir_desc, nfs_readdir }, /* readdir */ 122 { &vop_readlink_desc, nfs_readlink }, /* readlink */ 123 { &vop_abortop_desc, nfs_abortop }, /* abortop */ 124 { &vop_inactive_desc, nfs_inactive }, /* inactive */ 125 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */ 126 { &vop_lock_desc, nfs_lock }, /* lock */ 127 { &vop_unlock_desc, nfs_unlock }, /* unlock */ 128 { &vop_bmap_desc, nfs_bmap }, /* bmap */ 129 { &vop_strategy_desc, nfs_strategy }, /* strategy */ 130 { &vop_print_desc, nfs_print }, /* print */ 131 { &vop_islocked_desc, nfs_islocked }, /* islocked */ 132 { &vop_pathconf_desc, nfs_pathconf }, /* pathconf */ 133 { &vop_advlock_desc, nfs_advlock }, /* advlock */ 134 { &vop_blkatoff_desc, nfs_blkatoff }, /* blkatoff */ 135 { &vop_valloc_desc, nfs_valloc }, /* valloc */ 136 { &vop_reallocblks_desc, nfs_reallocblks }, /* reallocblks */ 137 { &vop_vfree_desc, nfs_vfree }, /* vfree */ 138 { &vop_truncate_desc, nfs_truncate }, /* truncate */ 139 { &vop_update_desc, nfs_update }, /* update */ 140 { &vop_bwrite_desc, nfs_bwrite }, /* bwrite */ 141 { &vop_getpages_desc, nfs_getpages }, /* getpages */ 142 { &vop_putpages_desc, genfs_putpages }, /* putpages */ 143 { NULL, NULL } 144 }; 145 const struct vnodeopv_desc nfsv2_vnodeop_opv_desc = 146 { &nfsv2_vnodeop_p, nfsv2_vnodeop_entries }; 147 148 /* 149 * Special device vnode ops 150 */ 151 int (**spec_nfsv2nodeop_p) __P((void *)); 152 const struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = { 153 { &vop_default_desc, vn_default_error }, 154 { &vop_lookup_desc, spec_lookup }, /* lookup */ 155 { &vop_create_desc, spec_create }, /* create */ 156 { &vop_mknod_desc, spec_mknod }, /* mknod */ 157 { &vop_open_desc, spec_open }, /* open */ 158 { &vop_close_desc, nfsspec_close }, /* close */ 159 { &vop_access_desc, nfsspec_access }, /* access */ 160 { &vop_getattr_desc, nfs_getattr }, /* getattr */ 161 { &vop_setattr_desc, nfs_setattr }, /* setattr */ 162 { &vop_read_desc, nfsspec_read }, /* read */ 163 { &vop_write_desc, nfsspec_write }, /* write */ 164 { &vop_lease_desc, spec_lease_check }, /* lease */ 165 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ 166 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */ 167 { &vop_poll_desc, spec_poll }, /* poll */ 168 { &vop_revoke_desc, spec_revoke }, /* revoke */ 169 { &vop_mmap_desc, spec_mmap }, /* mmap */ 170 { &vop_fsync_desc, spec_fsync }, /* fsync */ 171 { &vop_seek_desc, spec_seek }, /* seek */ 172 { &vop_remove_desc, spec_remove }, /* remove */ 173 { &vop_link_desc, spec_link }, /* link */ 174 { &vop_rename_desc, spec_rename }, /* rename */ 175 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */ 176 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */ 177 { &vop_symlink_desc, spec_symlink }, /* symlink */ 178 { &vop_readdir_desc, spec_readdir }, /* readdir */ 179 { &vop_readlink_desc, spec_readlink }, /* readlink */ 180 { &vop_abortop_desc, spec_abortop }, /* abortop */ 181 { &vop_inactive_desc, nfs_inactive }, /* inactive */ 182 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */ 183 { &vop_lock_desc, nfs_lock }, /* lock */ 184 { &vop_unlock_desc, nfs_unlock }, /* unlock */ 185 { &vop_bmap_desc, spec_bmap }, /* bmap */ 186 { &vop_strategy_desc, spec_strategy }, /* strategy */ 187 { &vop_print_desc, nfs_print }, /* print */ 188 { &vop_islocked_desc, nfs_islocked }, /* islocked */ 189 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */ 190 { &vop_advlock_desc, spec_advlock }, /* advlock */ 191 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */ 192 { &vop_valloc_desc, spec_valloc }, /* valloc */ 193 { &vop_reallocblks_desc, spec_reallocblks }, /* reallocblks */ 194 { &vop_vfree_desc, spec_vfree }, /* vfree */ 195 { &vop_truncate_desc, spec_truncate }, /* truncate */ 196 { &vop_update_desc, nfs_update }, /* update */ 197 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ 198 { &vop_getpages_desc, spec_getpages }, /* getpages */ 199 { &vop_putpages_desc, spec_putpages }, /* putpages */ 200 { NULL, NULL } 201 }; 202 const struct vnodeopv_desc spec_nfsv2nodeop_opv_desc = 203 { &spec_nfsv2nodeop_p, spec_nfsv2nodeop_entries }; 204 205 int (**fifo_nfsv2nodeop_p) __P((void *)); 206 const struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = { 207 { &vop_default_desc, vn_default_error }, 208 { &vop_lookup_desc, fifo_lookup }, /* lookup */ 209 { &vop_create_desc, fifo_create }, /* create */ 210 { &vop_mknod_desc, fifo_mknod }, /* mknod */ 211 { &vop_open_desc, fifo_open }, /* open */ 212 { &vop_close_desc, nfsfifo_close }, /* close */ 213 { &vop_access_desc, nfsspec_access }, /* access */ 214 { &vop_getattr_desc, nfs_getattr }, /* getattr */ 215 { &vop_setattr_desc, nfs_setattr }, /* setattr */ 216 { &vop_read_desc, nfsfifo_read }, /* read */ 217 { &vop_write_desc, nfsfifo_write }, /* write */ 218 { &vop_lease_desc, fifo_lease_check }, /* lease */ 219 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ 220 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */ 221 { &vop_poll_desc, fifo_poll }, /* poll */ 222 { &vop_revoke_desc, fifo_revoke }, /* revoke */ 223 { &vop_mmap_desc, fifo_mmap }, /* mmap */ 224 { &vop_fsync_desc, nfs_fsync }, /* fsync */ 225 { &vop_seek_desc, fifo_seek }, /* seek */ 226 { &vop_remove_desc, fifo_remove }, /* remove */ 227 { &vop_link_desc, fifo_link }, /* link */ 228 { &vop_rename_desc, fifo_rename }, /* rename */ 229 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */ 230 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */ 231 { &vop_symlink_desc, fifo_symlink }, /* symlink */ 232 { &vop_readdir_desc, fifo_readdir }, /* readdir */ 233 { &vop_readlink_desc, fifo_readlink }, /* readlink */ 234 { &vop_abortop_desc, fifo_abortop }, /* abortop */ 235 { &vop_inactive_desc, nfs_inactive }, /* inactive */ 236 { &vop_reclaim_desc, nfs_reclaim }, /* reclaim */ 237 { &vop_lock_desc, nfs_lock }, /* lock */ 238 { &vop_unlock_desc, nfs_unlock }, /* unlock */ 239 { &vop_bmap_desc, fifo_bmap }, /* bmap */ 240 { &vop_strategy_desc, genfs_badop }, /* strategy */ 241 { &vop_print_desc, nfs_print }, /* print */ 242 { &vop_islocked_desc, nfs_islocked }, /* islocked */ 243 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */ 244 { &vop_advlock_desc, fifo_advlock }, /* advlock */ 245 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */ 246 { &vop_valloc_desc, fifo_valloc }, /* valloc */ 247 { &vop_reallocblks_desc, fifo_reallocblks }, /* reallocblks */ 248 { &vop_vfree_desc, fifo_vfree }, /* vfree */ 249 { &vop_truncate_desc, fifo_truncate }, /* truncate */ 250 { &vop_update_desc, nfs_update }, /* update */ 251 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ 252 { &vop_putpages_desc, fifo_putpages }, /* putpages */ 253 { NULL, NULL } 254 }; 255 const struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc = 256 { &fifo_nfsv2nodeop_p, fifo_nfsv2nodeop_entries }; 257 258 /* 259 * Global variables 260 */ 261 extern u_int32_t nfs_true, nfs_false; 262 extern u_int32_t nfs_xdrneg1; 263 extern struct nfsstats nfsstats; 264 extern nfstype nfsv3_type[9]; 265 struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON]; 266 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON]; 267 int nfs_numasync = 0; 268 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1)) 269 270 /* 271 * nfs null call from vfs. 272 */ 273 int 274 nfs_null(vp, cred, procp) 275 struct vnode *vp; 276 struct ucred *cred; 277 struct proc *procp; 278 { 279 caddr_t bpos, dpos; 280 int error = 0; 281 struct mbuf *mreq, *mrep, *md, *mb; 282 283 nfsm_reqhead(vp, NFSPROC_NULL, 0); 284 nfsm_request(vp, NFSPROC_NULL, procp, cred); 285 nfsm_reqdone; 286 return (error); 287 } 288 289 /* 290 * nfs access vnode op. 291 * For nfs version 2, just return ok. File accesses may fail later. 292 * For nfs version 3, use the access rpc to check accessibility. If file modes 293 * are changed on the server, accesses might still fail later. 294 */ 295 int 296 nfs_access(v) 297 void *v; 298 { 299 struct vop_access_args /* { 300 struct vnode *a_vp; 301 int a_mode; 302 struct ucred *a_cred; 303 struct proc *a_p; 304 } */ *ap = v; 305 struct vnode *vp = ap->a_vp; 306 u_int32_t *tl; 307 caddr_t cp; 308 int32_t t1, t2; 309 caddr_t bpos, dpos, cp2; 310 int error = 0, attrflag, cachevalid; 311 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 312 u_int32_t mode, rmode; 313 const int v3 = NFS_ISV3(vp); 314 struct nfsnode *np = VTONFS(vp); 315 316 cachevalid = (np->n_accstamp != -1 && 317 (time.tv_sec - np->n_accstamp) < NFS_ATTRTIMEO(np) && 318 np->n_accuid == ap->a_cred->cr_uid); 319 320 /* 321 * Check access cache first. If this request has been made for this 322 * uid shortly before, use the cached result. 323 */ 324 if (cachevalid) { 325 if (!np->n_accerror) { 326 if ((np->n_accmode & ap->a_mode) == ap->a_mode) 327 return np->n_accerror; 328 } else if ((np->n_accmode & ap->a_mode) == np->n_accmode) 329 return np->n_accerror; 330 } 331 332 /* 333 * For nfs v3, do an access rpc, otherwise you are stuck emulating 334 * ufs_access() locally using the vattr. This may not be correct, 335 * since the server may apply other access criteria such as 336 * client uid-->server uid mapping that we do not know about, but 337 * this is better than just returning anything that is lying about 338 * in the cache. 339 */ 340 if (v3) { 341 nfsstats.rpccnt[NFSPROC_ACCESS]++; 342 nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED); 343 nfsm_fhtom(vp, v3); 344 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 345 if (ap->a_mode & VREAD) 346 mode = NFSV3ACCESS_READ; 347 else 348 mode = 0; 349 if (vp->v_type != VDIR) { 350 if (ap->a_mode & VWRITE) 351 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND); 352 if (ap->a_mode & VEXEC) 353 mode |= NFSV3ACCESS_EXECUTE; 354 } else { 355 if (ap->a_mode & VWRITE) 356 mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND | 357 NFSV3ACCESS_DELETE); 358 if (ap->a_mode & VEXEC) 359 mode |= NFSV3ACCESS_LOOKUP; 360 } 361 *tl = txdr_unsigned(mode); 362 nfsm_request(vp, NFSPROC_ACCESS, ap->a_p, ap->a_cred); 363 nfsm_postop_attr(vp, attrflag); 364 if (!error) { 365 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 366 rmode = fxdr_unsigned(u_int32_t, *tl); 367 /* 368 * The NFS V3 spec does not clarify whether or not 369 * the returned access bits can be a superset of 370 * the ones requested, so... 371 */ 372 if ((rmode & mode) != mode) 373 error = EACCES; 374 } 375 nfsm_reqdone; 376 } else 377 return (nfsspec_access(ap)); 378 /* 379 * Disallow write attempts on filesystems mounted read-only; 380 * unless the file is a socket, fifo, or a block or character 381 * device resident on the filesystem. 382 */ 383 if (!error && (ap->a_mode & VWRITE) && 384 (vp->v_mount->mnt_flag & MNT_RDONLY)) { 385 switch (vp->v_type) { 386 case VREG: 387 case VDIR: 388 case VLNK: 389 error = EROFS; 390 default: 391 break; 392 } 393 } 394 395 if (!error || error == EACCES) { 396 /* 397 * If we got the same result as for a previous, 398 * different request, OR it in. Don't update 399 * the timestamp in that case. 400 */ 401 if (cachevalid && error == np->n_accerror) { 402 if (!error) 403 np->n_accmode |= ap->a_mode; 404 else if ((np->n_accmode & ap->a_mode) == ap->a_mode) 405 np->n_accmode = ap->a_mode; 406 } else { 407 np->n_accstamp = time.tv_sec; 408 np->n_accuid = ap->a_cred->cr_uid; 409 np->n_accmode = ap->a_mode; 410 np->n_accerror = error; 411 } 412 } 413 414 return (error); 415 } 416 417 /* 418 * nfs open vnode op 419 * Check to see if the type is ok 420 * and that deletion is not in progress. 421 * For paged in text files, you will need to flush the page cache 422 * if consistency is lost. 423 */ 424 /* ARGSUSED */ 425 int 426 nfs_open(v) 427 void *v; 428 { 429 struct vop_open_args /* { 430 struct vnode *a_vp; 431 int a_mode; 432 struct ucred *a_cred; 433 struct proc *a_p; 434 } */ *ap = v; 435 struct vnode *vp = ap->a_vp; 436 struct nfsnode *np = VTONFS(vp); 437 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 438 struct vattr vattr; 439 int error; 440 441 if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) { 442 return (EACCES); 443 } 444 445 /* 446 * Initialize read and write creds here, for swapfiles 447 * and other paths that don't set the creds themselves. 448 */ 449 450 if (ap->a_mode & FREAD) { 451 if (np->n_rcred) { 452 crfree(np->n_rcred); 453 } 454 np->n_rcred = ap->a_cred; 455 crhold(np->n_rcred); 456 } 457 if (ap->a_mode & FWRITE) { 458 if (np->n_wcred) { 459 crfree(np->n_wcred); 460 } 461 np->n_wcred = ap->a_cred; 462 crhold(np->n_wcred); 463 } 464 465 #ifndef NFS_V2_ONLY 466 /* 467 * Get a valid lease. If cached data is stale, flush it. 468 */ 469 if (nmp->nm_flag & NFSMNT_NQNFS) { 470 if (NQNFS_CKINVALID(vp, np, ND_READ)) { 471 do { 472 error = nqnfs_getlease(vp, ND_READ, ap->a_cred, 473 ap->a_p); 474 } while (error == NQNFS_EXPIRED); 475 if (error) 476 return (error); 477 if (np->n_lrev != np->n_brev || 478 (np->n_flag & NQNFSNONCACHE)) { 479 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, 480 ap->a_p, 1)) == EINTR) 481 return (error); 482 np->n_brev = np->n_lrev; 483 } 484 } 485 } else 486 #endif 487 { 488 if (np->n_flag & NMODIFIED) { 489 if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, 490 ap->a_p, 1)) == EINTR) 491 return (error); 492 np->n_attrstamp = 0; 493 if (vp->v_type == VDIR) { 494 nfs_invaldircache(vp, 0); 495 np->n_direofoffset = 0; 496 } 497 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p); 498 if (error) 499 return (error); 500 np->n_mtime = vattr.va_mtime.tv_sec; 501 } else { 502 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_p); 503 if (error) 504 return (error); 505 if (np->n_mtime != vattr.va_mtime.tv_sec) { 506 if (vp->v_type == VDIR) { 507 nfs_invaldircache(vp, 0); 508 np->n_direofoffset = 0; 509 } 510 if ((error = nfs_vinvalbuf(vp, V_SAVE, 511 ap->a_cred, ap->a_p, 1)) == EINTR) 512 return (error); 513 np->n_mtime = vattr.va_mtime.tv_sec; 514 } 515 } 516 } 517 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) 518 np->n_attrstamp = 0; /* For Open/Close consistency */ 519 return (0); 520 } 521 522 /* 523 * nfs close vnode op 524 * What an NFS client should do upon close after writing is a debatable issue. 525 * Most NFS clients push delayed writes to the server upon close, basically for 526 * two reasons: 527 * 1 - So that any write errors may be reported back to the client process 528 * doing the close system call. By far the two most likely errors are 529 * NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure. 530 * 2 - To put a worst case upper bound on cache inconsistency between 531 * multiple clients for the file. 532 * There is also a consistency problem for Version 2 of the protocol w.r.t. 533 * not being able to tell if other clients are writing a file concurrently, 534 * since there is no way of knowing if the changed modify time in the reply 535 * is only due to the write for this client. 536 * (NFS Version 3 provides weak cache consistency data in the reply that 537 * should be sufficient to detect and handle this case.) 538 * 539 * The current code does the following: 540 * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers 541 * for NFS Version 3 - flush dirty buffers to the server but don't invalidate 542 * or commit them (this satisfies 1 and 2 except for the 543 * case where the server crashes after this close but 544 * before the commit RPC, which is felt to be "good 545 * enough". Changing the last argument to nfs_flush() to 546 * a 1 would force a commit operation, if it is felt a 547 * commit is necessary now. 548 * for NQNFS - do nothing now, since 2 is dealt with via leases and 549 * 1 should be dealt with via an fsync() system call for 550 * cases where write errors are important. 551 */ 552 /* ARGSUSED */ 553 int 554 nfs_close(v) 555 void *v; 556 { 557 struct vop_close_args /* { 558 struct vnodeop_desc *a_desc; 559 struct vnode *a_vp; 560 int a_fflag; 561 struct ucred *a_cred; 562 struct proc *a_p; 563 } */ *ap = v; 564 struct vnode *vp = ap->a_vp; 565 struct nfsnode *np = VTONFS(vp); 566 int error = 0; 567 UVMHIST_FUNC("nfs_close"); UVMHIST_CALLED(ubchist); 568 569 if (vp->v_type == VREG) { 570 if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 && 571 (np->n_flag & NMODIFIED)) { 572 if (NFS_ISV3(vp)) { 573 error = nfs_flush(vp, ap->a_cred, MNT_WAIT, ap->a_p, 0); 574 np->n_flag &= ~NMODIFIED; 575 } else 576 error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 1); 577 np->n_attrstamp = 0; 578 } 579 if (np->n_flag & NWRITEERR) { 580 np->n_flag &= ~NWRITEERR; 581 error = np->n_error; 582 } 583 } 584 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0); 585 return (error); 586 } 587 588 /* 589 * nfs getattr call from vfs. 590 */ 591 int 592 nfs_getattr(v) 593 void *v; 594 { 595 struct vop_getattr_args /* { 596 struct vnode *a_vp; 597 struct vattr *a_vap; 598 struct ucred *a_cred; 599 struct proc *a_p; 600 } */ *ap = v; 601 struct vnode *vp = ap->a_vp; 602 struct nfsnode *np = VTONFS(vp); 603 caddr_t cp; 604 u_int32_t *tl; 605 int32_t t1, t2; 606 caddr_t bpos, dpos; 607 int error = 0; 608 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 609 const int v3 = NFS_ISV3(vp); 610 611 /* 612 * Update local times for special files. 613 */ 614 if (np->n_flag & (NACC | NUPD)) 615 np->n_flag |= NCHG; 616 /* 617 * First look in the cache. 618 */ 619 if (nfs_getattrcache(vp, ap->a_vap) == 0) 620 return (0); 621 nfsstats.rpccnt[NFSPROC_GETATTR]++; 622 nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3)); 623 nfsm_fhtom(vp, v3); 624 nfsm_request(vp, NFSPROC_GETATTR, ap->a_p, ap->a_cred); 625 if (!error) { 626 nfsm_loadattr(vp, ap->a_vap); 627 if (vp->v_type == VDIR && 628 ap->a_vap->va_blocksize < NFS_DIRFRAGSIZ) 629 ap->a_vap->va_blocksize = NFS_DIRFRAGSIZ; 630 } 631 nfsm_reqdone; 632 return (error); 633 } 634 635 /* 636 * nfs setattr call. 637 */ 638 int 639 nfs_setattr(v) 640 void *v; 641 { 642 struct vop_setattr_args /* { 643 struct vnodeop_desc *a_desc; 644 struct vnode *a_vp; 645 struct vattr *a_vap; 646 struct ucred *a_cred; 647 struct proc *a_p; 648 } */ *ap = v; 649 struct vnode *vp = ap->a_vp; 650 struct nfsnode *np = VTONFS(vp); 651 struct vattr *vap = ap->a_vap; 652 int error = 0; 653 u_quad_t tsize = 0; 654 655 /* 656 * Setting of flags is not supported. 657 */ 658 if (vap->va_flags != VNOVAL) 659 return (EOPNOTSUPP); 660 661 /* 662 * Disallow write attempts if the filesystem is mounted read-only. 663 */ 664 if ((vap->va_uid != (uid_t)VNOVAL || 665 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 666 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) && 667 (vp->v_mount->mnt_flag & MNT_RDONLY)) 668 return (EROFS); 669 if (vap->va_size != VNOVAL) { 670 switch (vp->v_type) { 671 case VDIR: 672 return (EISDIR); 673 case VCHR: 674 case VBLK: 675 case VSOCK: 676 case VFIFO: 677 if (vap->va_mtime.tv_sec == VNOVAL && 678 vap->va_atime.tv_sec == VNOVAL && 679 vap->va_mode == (mode_t)VNOVAL && 680 vap->va_uid == (uid_t)VNOVAL && 681 vap->va_gid == (gid_t)VNOVAL) 682 return (0); 683 vap->va_size = VNOVAL; 684 break; 685 default: 686 /* 687 * Disallow write attempts if the filesystem is 688 * mounted read-only. 689 */ 690 if (vp->v_mount->mnt_flag & MNT_RDONLY) 691 return (EROFS); 692 uvm_vnp_setsize(vp, vap->va_size); 693 if (vap->va_size == 0) 694 error = nfs_vinvalbuf(vp, 0, 695 ap->a_cred, ap->a_p, 1); 696 else 697 error = nfs_vinvalbuf(vp, V_SAVE, 698 ap->a_cred, ap->a_p, 1); 699 if (error) { 700 uvm_vnp_setsize(vp, np->n_size); 701 return (error); 702 } 703 tsize = np->n_size; 704 np->n_size = np->n_vattr->va_size = vap->va_size; 705 } 706 } else if ((vap->va_mtime.tv_sec != VNOVAL || 707 vap->va_atime.tv_sec != VNOVAL) && 708 vp->v_type == VREG && 709 (error = nfs_vinvalbuf(vp, V_SAVE, ap->a_cred, 710 ap->a_p, 1)) == EINTR) 711 return (error); 712 error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_p); 713 if (error && vap->va_size != VNOVAL) { 714 np->n_size = np->n_vattr->va_size = tsize; 715 uvm_vnp_setsize(vp, np->n_size); 716 } 717 return (error); 718 } 719 720 /* 721 * Do an nfs setattr rpc. 722 */ 723 int 724 nfs_setattrrpc(vp, vap, cred, procp) 725 struct vnode *vp; 726 struct vattr *vap; 727 struct ucred *cred; 728 struct proc *procp; 729 { 730 struct nfsv2_sattr *sp; 731 caddr_t cp; 732 int32_t t1, t2; 733 caddr_t bpos, dpos, cp2; 734 u_int32_t *tl; 735 int error = 0, wccflag = NFSV3_WCCRATTR; 736 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 737 const int v3 = NFS_ISV3(vp); 738 739 nfsstats.rpccnt[NFSPROC_SETATTR]++; 740 nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3)); 741 nfsm_fhtom(vp, v3); 742 if (v3) { 743 nfsm_v3attrbuild(vap, TRUE); 744 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 745 *tl = nfs_false; 746 } else { 747 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 748 if (vap->va_mode == (mode_t)VNOVAL) 749 sp->sa_mode = nfs_xdrneg1; 750 else 751 sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode); 752 if (vap->va_uid == (uid_t)VNOVAL) 753 sp->sa_uid = nfs_xdrneg1; 754 else 755 sp->sa_uid = txdr_unsigned(vap->va_uid); 756 if (vap->va_gid == (gid_t)VNOVAL) 757 sp->sa_gid = nfs_xdrneg1; 758 else 759 sp->sa_gid = txdr_unsigned(vap->va_gid); 760 sp->sa_size = txdr_unsigned(vap->va_size); 761 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 762 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 763 } 764 nfsm_request(vp, NFSPROC_SETATTR, procp, cred); 765 if (v3) { 766 nfsm_wcc_data(vp, wccflag); 767 } else 768 nfsm_loadattr(vp, (struct vattr *)0); 769 nfsm_reqdone; 770 return (error); 771 } 772 773 /* 774 * nfs lookup call, one step at a time... 775 * First look in cache 776 * If not found, unlock the directory nfsnode and do the rpc 777 * 778 * This code is full of lock/unlock statements and checks, because 779 * we continue after cache_lookup has finished (we need to check 780 * with the attr cache and do an rpc if it has timed out). This means 781 * that the locking effects of cache_lookup have to be taken into 782 * account. 783 */ 784 int 785 nfs_lookup(v) 786 void *v; 787 { 788 struct vop_lookup_args /* { 789 struct vnodeop_desc *a_desc; 790 struct vnode *a_dvp; 791 struct vnode **a_vpp; 792 struct componentname *a_cnp; 793 } */ *ap = v; 794 struct componentname *cnp = ap->a_cnp; 795 struct vnode *dvp = ap->a_dvp; 796 struct vnode **vpp = ap->a_vpp; 797 int flags; 798 struct vnode *newvp; 799 u_int32_t *tl; 800 caddr_t cp; 801 int32_t t1, t2; 802 struct nfsmount *nmp; 803 caddr_t bpos, dpos, cp2; 804 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 805 long len; 806 nfsfh_t *fhp; 807 struct nfsnode *np; 808 int lockparent, wantparent, error = 0, attrflag, fhsize; 809 const int v3 = NFS_ISV3(dvp); 810 811 cnp->cn_flags &= ~PDIRUNLOCK; 812 flags = cnp->cn_flags; 813 814 *vpp = NULLVP; 815 newvp = NULLVP; 816 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 817 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 818 return (EROFS); 819 if (dvp->v_type != VDIR) 820 return (ENOTDIR); 821 822 lockparent = flags & LOCKPARENT; 823 wantparent = flags & (LOCKPARENT|WANTPARENT); 824 nmp = VFSTONFS(dvp->v_mount); 825 np = VTONFS(dvp); 826 827 /* 828 * Before tediously performing a linear scan of the directory, 829 * check the name cache to see if the directory/name pair 830 * we are looking for is known already. 831 * If the directory/name pair is found in the name cache, 832 * we have to ensure the directory has not changed from 833 * the time the cache entry has been created. If it has, 834 * the cache entry has to be ignored 835 */ 836 if ((error = cache_lookup(dvp, vpp, cnp)) >= 0) { 837 struct vattr vattr; 838 int err2; 839 840 if (error && error != ENOENT) { 841 *vpp = NULLVP; 842 return error; 843 } 844 845 if (cnp->cn_flags & PDIRUNLOCK) { 846 err2 = vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 847 if (err2 != 0) { 848 *vpp = NULLVP; 849 return err2; 850 } 851 cnp->cn_flags &= ~PDIRUNLOCK; 852 } 853 854 err2 = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_proc); 855 if (err2 != 0) { 856 if (error == 0) { 857 if (*vpp != dvp) 858 vput(*vpp); 859 else 860 vrele(*vpp); 861 } 862 *vpp = NULLVP; 863 return err2; 864 } 865 866 if (error == ENOENT) { 867 if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred, 868 cnp->cn_proc) && vattr.va_mtime.tv_sec == 869 VTONFS(dvp)->n_nctime) 870 return ENOENT; 871 cache_purge(dvp); 872 np->n_nctime = 0; 873 goto dorpc; 874 } 875 876 newvp = *vpp; 877 if (!VOP_GETATTR(newvp, &vattr, cnp->cn_cred, cnp->cn_proc) 878 && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) 879 { 880 nfsstats.lookupcache_hits++; 881 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 882 cnp->cn_flags |= SAVENAME; 883 if ((!lockparent || !(flags & ISLASTCN)) && 884 newvp != dvp) 885 VOP_UNLOCK(dvp, 0); 886 return (0); 887 } 888 cache_purge(newvp); 889 if (newvp != dvp) 890 vput(newvp); 891 else 892 vrele(newvp); 893 *vpp = NULLVP; 894 } 895 dorpc: 896 error = 0; 897 newvp = NULLVP; 898 nfsstats.lookupcache_misses++; 899 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 900 len = cnp->cn_namelen; 901 nfsm_reqhead(dvp, NFSPROC_LOOKUP, 902 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 903 nfsm_fhtom(dvp, v3); 904 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN); 905 nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_proc, cnp->cn_cred); 906 if (error) { 907 nfsm_postop_attr(dvp, attrflag); 908 m_freem(mrep); 909 goto nfsmout; 910 } 911 nfsm_getfh(fhp, fhsize, v3); 912 913 /* 914 * Handle RENAME case... 915 */ 916 if (cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN)) { 917 if (NFS_CMPFH(np, fhp, fhsize)) { 918 m_freem(mrep); 919 return (EISDIR); 920 } 921 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 922 if (error) { 923 m_freem(mrep); 924 return error; 925 } 926 newvp = NFSTOV(np); 927 if (v3) { 928 nfsm_postop_attr(newvp, attrflag); 929 nfsm_postop_attr(dvp, attrflag); 930 } else 931 nfsm_loadattr(newvp, (struct vattr *)0); 932 *vpp = newvp; 933 m_freem(mrep); 934 cnp->cn_flags |= SAVENAME; 935 if (!lockparent) { 936 VOP_UNLOCK(dvp, 0); 937 cnp->cn_flags |= PDIRUNLOCK; 938 } 939 return (0); 940 } 941 942 /* 943 * The postop attr handling is duplicated for each if case, 944 * because it should be done while dvp is locked (unlocking 945 * dvp is different for each case). 946 */ 947 948 if (NFS_CMPFH(np, fhp, fhsize)) { 949 /* 950 * "." lookup 951 */ 952 VREF(dvp); 953 newvp = dvp; 954 if (v3) { 955 nfsm_postop_attr(newvp, attrflag); 956 nfsm_postop_attr(dvp, attrflag); 957 } else 958 nfsm_loadattr(newvp, (struct vattr *)0); 959 } else if (flags & ISDOTDOT) { 960 /* 961 * ".." lookup 962 */ 963 VOP_UNLOCK(dvp, 0); 964 cnp->cn_flags |= PDIRUNLOCK; 965 966 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 967 if (error) { 968 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0) 969 cnp->cn_flags &= ~PDIRUNLOCK; 970 m_freem(mrep); 971 return error; 972 } 973 newvp = NFSTOV(np); 974 975 if (v3) { 976 nfsm_postop_attr(newvp, attrflag); 977 nfsm_postop_attr(dvp, attrflag); 978 } else 979 nfsm_loadattr(newvp, (struct vattr *)0); 980 981 if (lockparent && (flags & ISLASTCN)) { 982 if ((error = vn_lock(dvp, LK_EXCLUSIVE))) { 983 m_freem(mrep); 984 vput(newvp); 985 return error; 986 } 987 cnp->cn_flags &= ~PDIRUNLOCK; 988 } 989 } else { 990 /* 991 * Other lookups. 992 */ 993 error = nfs_nget(dvp->v_mount, fhp, fhsize, &np); 994 if (error) { 995 m_freem(mrep); 996 return error; 997 } 998 newvp = NFSTOV(np); 999 if (v3) { 1000 nfsm_postop_attr(newvp, attrflag); 1001 nfsm_postop_attr(dvp, attrflag); 1002 } else 1003 nfsm_loadattr(newvp, (struct vattr *)0); 1004 if (!lockparent || !(flags & ISLASTCN)) { 1005 VOP_UNLOCK(dvp, 0); 1006 cnp->cn_flags |= PDIRUNLOCK; 1007 } 1008 } 1009 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 1010 cnp->cn_flags |= SAVENAME; 1011 if ((cnp->cn_flags & MAKEENTRY) && 1012 (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN))) { 1013 np->n_ctime = np->n_vattr->va_ctime.tv_sec; 1014 cache_enter(dvp, newvp, cnp); 1015 } 1016 *vpp = newvp; 1017 nfsm_reqdone; 1018 if (error) { 1019 /* 1020 * We get here only because of errors returned by 1021 * the RPC. Otherwise we'll have returned above 1022 * (the nfsm_* macros will jump to nfsm_reqdone 1023 * on error). 1024 */ 1025 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && 1026 cnp->cn_nameiop != CREATE) { 1027 if (VTONFS(dvp)->n_nctime == 0) 1028 VTONFS(dvp)->n_nctime = 1029 VTONFS(dvp)->n_vattr->va_mtime.tv_sec; 1030 cache_enter(dvp, NULL, cnp); 1031 } 1032 if (newvp != NULLVP) { 1033 vrele(newvp); 1034 if (newvp != dvp) 1035 VOP_UNLOCK(newvp, 0); 1036 } 1037 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) && 1038 (flags & ISLASTCN) && error == ENOENT) { 1039 if (dvp->v_mount->mnt_flag & MNT_RDONLY) 1040 error = EROFS; 1041 else 1042 error = EJUSTRETURN; 1043 } 1044 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)) 1045 cnp->cn_flags |= SAVENAME; 1046 *vpp = NULL; 1047 } 1048 1049 return error; 1050 } 1051 1052 /* 1053 * nfs read call. 1054 * Just call nfs_bioread() to do the work. 1055 */ 1056 int 1057 nfs_read(v) 1058 void *v; 1059 { 1060 struct vop_read_args /* { 1061 struct vnode *a_vp; 1062 struct uio *a_uio; 1063 int a_ioflag; 1064 struct ucred *a_cred; 1065 } */ *ap = v; 1066 struct vnode *vp = ap->a_vp; 1067 1068 if (vp->v_type != VREG) 1069 return (EPERM); 1070 return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred, 0)); 1071 } 1072 1073 /* 1074 * nfs readlink call 1075 */ 1076 int 1077 nfs_readlink(v) 1078 void *v; 1079 { 1080 struct vop_readlink_args /* { 1081 struct vnode *a_vp; 1082 struct uio *a_uio; 1083 struct ucred *a_cred; 1084 } */ *ap = v; 1085 struct vnode *vp = ap->a_vp; 1086 1087 if (vp->v_type != VLNK) 1088 return (EPERM); 1089 return (nfs_bioread(vp, ap->a_uio, 0, ap->a_cred, 0)); 1090 } 1091 1092 /* 1093 * Do a readlink rpc. 1094 * Called by nfs_doio() from below the buffer cache. 1095 */ 1096 int 1097 nfs_readlinkrpc(vp, uiop, cred) 1098 struct vnode *vp; 1099 struct uio *uiop; 1100 struct ucred *cred; 1101 { 1102 u_int32_t *tl; 1103 caddr_t cp; 1104 int32_t t1, t2; 1105 caddr_t bpos, dpos, cp2; 1106 int error = 0, len, attrflag; 1107 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1108 const int v3 = NFS_ISV3(vp); 1109 1110 nfsstats.rpccnt[NFSPROC_READLINK]++; 1111 nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3)); 1112 nfsm_fhtom(vp, v3); 1113 nfsm_request(vp, NFSPROC_READLINK, uiop->uio_procp, cred); 1114 if (v3) 1115 nfsm_postop_attr(vp, attrflag); 1116 if (!error) { 1117 nfsm_strsiz(len, NFS_MAXPATHLEN); 1118 nfsm_mtouio(uiop, len); 1119 } 1120 nfsm_reqdone; 1121 return (error); 1122 } 1123 1124 /* 1125 * nfs read rpc call 1126 * Ditto above 1127 */ 1128 int 1129 nfs_readrpc(vp, uiop) 1130 struct vnode *vp; 1131 struct uio *uiop; 1132 { 1133 u_int32_t *tl; 1134 caddr_t cp; 1135 int32_t t1, t2; 1136 caddr_t bpos, dpos, cp2; 1137 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1138 struct nfsmount *nmp; 1139 int error = 0, len, retlen, tsiz, eof, attrflag; 1140 const int v3 = NFS_ISV3(vp); 1141 1142 #ifndef nolint 1143 eof = 0; 1144 #endif 1145 nmp = VFSTONFS(vp->v_mount); 1146 tsiz = uiop->uio_resid; 1147 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 1148 return (EFBIG); 1149 while (tsiz > 0) { 1150 nfsstats.rpccnt[NFSPROC_READ]++; 1151 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; 1152 nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3); 1153 nfsm_fhtom(vp, v3); 1154 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3); 1155 if (v3) { 1156 txdr_hyper(uiop->uio_offset, tl); 1157 *(tl + 2) = txdr_unsigned(len); 1158 } else { 1159 *tl++ = txdr_unsigned(uiop->uio_offset); 1160 *tl++ = txdr_unsigned(len); 1161 *tl = 0; 1162 } 1163 nfsm_request(vp, NFSPROC_READ, uiop->uio_procp, 1164 VTONFS(vp)->n_rcred); 1165 if (v3) { 1166 nfsm_postop_attr(vp, attrflag); 1167 if (error) { 1168 m_freem(mrep); 1169 goto nfsmout; 1170 } 1171 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1172 eof = fxdr_unsigned(int, *(tl + 1)); 1173 } else 1174 nfsm_loadattr(vp, (struct vattr *)0); 1175 nfsm_strsiz(retlen, nmp->nm_rsize); 1176 nfsm_mtouio(uiop, retlen); 1177 m_freem(mrep); 1178 tsiz -= retlen; 1179 if (v3) { 1180 if (eof || retlen == 0) 1181 tsiz = 0; 1182 } else if (retlen < len) 1183 tsiz = 0; 1184 } 1185 nfsmout: 1186 return (error); 1187 } 1188 1189 /* 1190 * nfs write call 1191 */ 1192 int 1193 nfs_writerpc(vp, uiop, iomode, must_commit) 1194 struct vnode *vp; 1195 struct uio *uiop; 1196 int *iomode, *must_commit; 1197 { 1198 u_int32_t *tl; 1199 caddr_t cp; 1200 int32_t t1, t2, backup; 1201 caddr_t bpos, dpos, cp2; 1202 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1203 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 1204 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit; 1205 const int v3 = NFS_ISV3(vp); 1206 int committed = NFSV3WRITE_FILESYNC; 1207 1208 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 1209 panic("writerpc readonly vp %p", vp); 1210 } 1211 1212 #ifndef DIAGNOSTIC 1213 if (uiop->uio_iovcnt != 1) 1214 panic("nfs: writerpc iovcnt > 1"); 1215 #endif 1216 *must_commit = 0; 1217 tsiz = uiop->uio_resid; 1218 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize) 1219 return (EFBIG); 1220 while (tsiz > 0) { 1221 nfsstats.rpccnt[NFSPROC_WRITE]++; 1222 len = min(tsiz, nmp->nm_wsize); 1223 nfsm_reqhead(vp, NFSPROC_WRITE, 1224 NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len)); 1225 nfsm_fhtom(vp, v3); 1226 if (v3) { 1227 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1228 txdr_hyper(uiop->uio_offset, tl); 1229 tl += 2; 1230 *tl++ = txdr_unsigned(len); 1231 *tl++ = txdr_unsigned(*iomode); 1232 *tl = txdr_unsigned(len); 1233 } else { 1234 u_int32_t x; 1235 1236 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1237 /* Set both "begin" and "current" to non-garbage. */ 1238 x = txdr_unsigned((u_int32_t)uiop->uio_offset); 1239 *tl++ = x; /* "begin offset" */ 1240 *tl++ = x; /* "current offset" */ 1241 x = txdr_unsigned(len); 1242 *tl++ = x; /* total to this offset */ 1243 *tl = x; /* size of this write */ 1244 1245 } 1246 nfsm_uiotom(uiop, len); 1247 nfsm_request(vp, NFSPROC_WRITE, uiop->uio_procp, 1248 VTONFS(vp)->n_wcred); 1249 if (v3) { 1250 wccflag = NFSV3_WCCCHK; 1251 nfsm_wcc_data(vp, wccflag); 1252 if (!error) { 1253 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED 1254 + NFSX_V3WRITEVERF); 1255 rlen = fxdr_unsigned(int, *tl++); 1256 if (rlen == 0) { 1257 error = NFSERR_IO; 1258 m_freem(mrep); 1259 break; 1260 } else if (rlen < len) { 1261 backup = len - rlen; 1262 uiop->uio_iov->iov_base = 1263 (caddr_t)uiop->uio_iov->iov_base - 1264 backup; 1265 uiop->uio_iov->iov_len += backup; 1266 uiop->uio_offset -= backup; 1267 uiop->uio_resid += backup; 1268 len = rlen; 1269 } 1270 commit = fxdr_unsigned(int, *tl++); 1271 1272 /* 1273 * Return the lowest committment level 1274 * obtained by any of the RPCs. 1275 */ 1276 if (committed == NFSV3WRITE_FILESYNC) 1277 committed = commit; 1278 else if (committed == NFSV3WRITE_DATASYNC && 1279 commit == NFSV3WRITE_UNSTABLE) 1280 committed = commit; 1281 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0){ 1282 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl, 1283 NFSX_V3WRITEVERF); 1284 nmp->nm_iflag |= NFSMNT_HASWRITEVERF; 1285 } else if (memcmp((caddr_t)tl, 1286 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) { 1287 *must_commit = 1; 1288 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl, 1289 NFSX_V3WRITEVERF); 1290 } 1291 } 1292 } else 1293 nfsm_loadattr(vp, (struct vattr *)0); 1294 if (wccflag) 1295 VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr->va_mtime.tv_sec; 1296 m_freem(mrep); 1297 if (error) 1298 break; 1299 tsiz -= len; 1300 } 1301 nfsmout: 1302 *iomode = committed; 1303 if (error) 1304 uiop->uio_resid = tsiz; 1305 return (error); 1306 } 1307 1308 /* 1309 * nfs mknod rpc 1310 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1311 * mode set to specify the file type and the size field for rdev. 1312 */ 1313 int 1314 nfs_mknodrpc(dvp, vpp, cnp, vap) 1315 struct vnode *dvp; 1316 struct vnode **vpp; 1317 struct componentname *cnp; 1318 struct vattr *vap; 1319 { 1320 struct nfsv2_sattr *sp; 1321 u_int32_t *tl; 1322 caddr_t cp; 1323 int32_t t1, t2; 1324 struct vnode *newvp = (struct vnode *)0; 1325 struct nfsnode *np; 1326 char *cp2; 1327 caddr_t bpos, dpos; 1328 int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0; 1329 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1330 u_int32_t rdev; 1331 const int v3 = NFS_ISV3(dvp); 1332 1333 if (vap->va_type == VCHR || vap->va_type == VBLK) 1334 rdev = txdr_unsigned(vap->va_rdev); 1335 else if (vap->va_type == VFIFO || vap->va_type == VSOCK) 1336 rdev = nfs_xdrneg1; 1337 else { 1338 VOP_ABORTOP(dvp, cnp); 1339 vput(dvp); 1340 return (EOPNOTSUPP); 1341 } 1342 nfsstats.rpccnt[NFSPROC_MKNOD]++; 1343 nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED + 1344 + nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3)); 1345 nfsm_fhtom(dvp, v3); 1346 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1347 if (v3) { 1348 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1349 *tl++ = vtonfsv3_type(vap->va_type); 1350 nfsm_v3attrbuild(vap, FALSE); 1351 if (vap->va_type == VCHR || vap->va_type == VBLK) { 1352 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1353 *tl++ = txdr_unsigned(major(vap->va_rdev)); 1354 *tl = txdr_unsigned(minor(vap->va_rdev)); 1355 } 1356 } else { 1357 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1358 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1359 sp->sa_uid = nfs_xdrneg1; 1360 sp->sa_gid = nfs_xdrneg1; 1361 sp->sa_size = rdev; 1362 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1363 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1364 } 1365 nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_proc, cnp->cn_cred); 1366 if (!error) { 1367 nfsm_mtofh(dvp, newvp, v3, gotvp); 1368 if (!gotvp) { 1369 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1370 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np); 1371 if (!error) 1372 newvp = NFSTOV(np); 1373 } 1374 } 1375 if (v3) 1376 nfsm_wcc_data(dvp, wccflag); 1377 nfsm_reqdone; 1378 if (error) { 1379 if (newvp) 1380 vput(newvp); 1381 } else { 1382 if (cnp->cn_flags & MAKEENTRY) 1383 cache_enter(dvp, newvp, cnp); 1384 *vpp = newvp; 1385 } 1386 PNBUF_PUT(cnp->cn_pnbuf); 1387 VTONFS(dvp)->n_flag |= NMODIFIED; 1388 if (!wccflag) 1389 VTONFS(dvp)->n_attrstamp = 0; 1390 vput(dvp); 1391 return (error); 1392 } 1393 1394 /* 1395 * nfs mknod vop 1396 * just call nfs_mknodrpc() to do the work. 1397 */ 1398 /* ARGSUSED */ 1399 int 1400 nfs_mknod(v) 1401 void *v; 1402 { 1403 struct vop_mknod_args /* { 1404 struct vnode *a_dvp; 1405 struct vnode **a_vpp; 1406 struct componentname *a_cnp; 1407 struct vattr *a_vap; 1408 } */ *ap = v; 1409 int error; 1410 1411 error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap); 1412 return (error); 1413 } 1414 1415 static u_long create_verf; 1416 /* 1417 * nfs file create call 1418 */ 1419 int 1420 nfs_create(v) 1421 void *v; 1422 { 1423 struct vop_create_args /* { 1424 struct vnode *a_dvp; 1425 struct vnode **a_vpp; 1426 struct componentname *a_cnp; 1427 struct vattr *a_vap; 1428 } */ *ap = v; 1429 struct vnode *dvp = ap->a_dvp; 1430 struct vattr *vap = ap->a_vap; 1431 struct componentname *cnp = ap->a_cnp; 1432 struct nfsv2_sattr *sp; 1433 u_int32_t *tl; 1434 caddr_t cp; 1435 int32_t t1, t2; 1436 struct nfsnode *np = (struct nfsnode *)0; 1437 struct vnode *newvp = (struct vnode *)0; 1438 caddr_t bpos, dpos, cp2; 1439 int error, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0; 1440 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1441 const int v3 = NFS_ISV3(dvp); 1442 1443 /* 1444 * Oops, not for me.. 1445 */ 1446 if (vap->va_type == VSOCK) 1447 return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap)); 1448 1449 #ifdef VA_EXCLUSIVE 1450 if (vap->va_vaflags & VA_EXCLUSIVE) 1451 fmode |= O_EXCL; 1452 #endif 1453 again: 1454 error = 0; 1455 nfsstats.rpccnt[NFSPROC_CREATE]++; 1456 nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED + 1457 nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3)); 1458 nfsm_fhtom(dvp, v3); 1459 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1460 if (v3) { 1461 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1462 if (fmode & O_EXCL) { 1463 *tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE); 1464 nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF); 1465 #ifdef INET 1466 if (in_ifaddr.tqh_first) 1467 *tl++ = in_ifaddr.tqh_first->ia_addr.sin_addr.s_addr; 1468 else 1469 *tl++ = create_verf; 1470 #else 1471 *tl++ = create_verf; 1472 #endif 1473 *tl = ++create_verf; 1474 } else { 1475 *tl = txdr_unsigned(NFSV3CREATE_UNCHECKED); 1476 nfsm_v3attrbuild(vap, FALSE); 1477 } 1478 } else { 1479 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1480 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 1481 sp->sa_uid = nfs_xdrneg1; 1482 sp->sa_gid = nfs_xdrneg1; 1483 sp->sa_size = 0; 1484 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1485 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1486 } 1487 nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_proc, cnp->cn_cred); 1488 if (!error) { 1489 nfsm_mtofh(dvp, newvp, v3, gotvp); 1490 if (!gotvp) { 1491 error = nfs_lookitup(dvp, cnp->cn_nameptr, 1492 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc, &np); 1493 if (!error) 1494 newvp = NFSTOV(np); 1495 } 1496 } 1497 if (v3) 1498 nfsm_wcc_data(dvp, wccflag); 1499 nfsm_reqdone; 1500 if (error) { 1501 if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) { 1502 fmode &= ~O_EXCL; 1503 goto again; 1504 } 1505 if (newvp) 1506 vput(newvp); 1507 } else if (v3 && (fmode & O_EXCL)) 1508 error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_proc); 1509 if (!error) { 1510 if (cnp->cn_flags & MAKEENTRY) 1511 cache_enter(dvp, newvp, cnp); 1512 *ap->a_vpp = newvp; 1513 } 1514 PNBUF_PUT(cnp->cn_pnbuf); 1515 VTONFS(dvp)->n_flag |= NMODIFIED; 1516 if (!wccflag) 1517 VTONFS(dvp)->n_attrstamp = 0; 1518 vput(dvp); 1519 return (error); 1520 } 1521 1522 /* 1523 * nfs file remove call 1524 * To try and make nfs semantics closer to ufs semantics, a file that has 1525 * other processes using the vnode is renamed instead of removed and then 1526 * removed later on the last close. 1527 * - If v_usecount > 1 1528 * If a rename is not already in the works 1529 * call nfs_sillyrename() to set it up 1530 * else 1531 * do the remove rpc 1532 */ 1533 int 1534 nfs_remove(v) 1535 void *v; 1536 { 1537 struct vop_remove_args /* { 1538 struct vnodeop_desc *a_desc; 1539 struct vnode * a_dvp; 1540 struct vnode * a_vp; 1541 struct componentname * a_cnp; 1542 } */ *ap = v; 1543 struct vnode *vp = ap->a_vp; 1544 struct vnode *dvp = ap->a_dvp; 1545 struct componentname *cnp = ap->a_cnp; 1546 struct nfsnode *np = VTONFS(vp); 1547 int error = 0; 1548 struct vattr vattr; 1549 1550 #ifndef DIAGNOSTIC 1551 if ((cnp->cn_flags & HASBUF) == 0) 1552 panic("nfs_remove: no name"); 1553 if (vp->v_usecount < 1) 1554 panic("nfs_remove: bad v_usecount"); 1555 #endif 1556 if (vp->v_type == VDIR) 1557 error = EPERM; 1558 else if (vp->v_usecount == 1 || (np->n_sillyrename && 1559 VOP_GETATTR(vp, &vattr, cnp->cn_cred, cnp->cn_proc) == 0 && 1560 vattr.va_nlink > 1)) { 1561 /* 1562 * Purge the name cache so that the chance of a lookup for 1563 * the name succeeding while the remove is in progress is 1564 * minimized. Without node locking it can still happen, such 1565 * that an I/O op returns ESTALE, but since you get this if 1566 * another host removes the file.. 1567 */ 1568 cache_purge(vp); 1569 /* 1570 * throw away biocache buffers, mainly to avoid 1571 * unnecessary delayed writes later. 1572 */ 1573 error = nfs_vinvalbuf(vp, 0, cnp->cn_cred, cnp->cn_proc, 1); 1574 /* Do the rpc */ 1575 if (error != EINTR) 1576 error = nfs_removerpc(dvp, cnp->cn_nameptr, 1577 cnp->cn_namelen, cnp->cn_cred, cnp->cn_proc); 1578 /* 1579 * Kludge City: If the first reply to the remove rpc is lost.. 1580 * the reply to the retransmitted request will be ENOENT 1581 * since the file was in fact removed 1582 * Therefore, we cheat and return success. 1583 */ 1584 if (error == ENOENT) 1585 error = 0; 1586 } else if (!np->n_sillyrename) 1587 error = nfs_sillyrename(dvp, vp, cnp); 1588 PNBUF_PUT(cnp->cn_pnbuf); 1589 np->n_attrstamp = 0; 1590 vput(dvp); 1591 vput(vp); 1592 return (error); 1593 } 1594 1595 /* 1596 * nfs file remove rpc called from nfs_inactive 1597 */ 1598 int 1599 nfs_removeit(sp) 1600 struct sillyrename *sp; 1601 { 1602 1603 return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen, sp->s_cred, 1604 (struct proc *)0)); 1605 } 1606 1607 /* 1608 * Nfs remove rpc, called from nfs_remove() and nfs_removeit(). 1609 */ 1610 int 1611 nfs_removerpc(dvp, name, namelen, cred, proc) 1612 struct vnode *dvp; 1613 const char *name; 1614 int namelen; 1615 struct ucred *cred; 1616 struct proc *proc; 1617 { 1618 u_int32_t *tl; 1619 caddr_t cp; 1620 int32_t t1, t2; 1621 caddr_t bpos, dpos, cp2; 1622 int error = 0, wccflag = NFSV3_WCCRATTR; 1623 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1624 const int v3 = NFS_ISV3(dvp); 1625 1626 nfsstats.rpccnt[NFSPROC_REMOVE]++; 1627 nfsm_reqhead(dvp, NFSPROC_REMOVE, 1628 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen)); 1629 nfsm_fhtom(dvp, v3); 1630 nfsm_strtom(name, namelen, NFS_MAXNAMLEN); 1631 nfsm_request(dvp, NFSPROC_REMOVE, proc, cred); 1632 if (v3) 1633 nfsm_wcc_data(dvp, wccflag); 1634 nfsm_reqdone; 1635 VTONFS(dvp)->n_flag |= NMODIFIED; 1636 if (!wccflag) 1637 VTONFS(dvp)->n_attrstamp = 0; 1638 return (error); 1639 } 1640 1641 /* 1642 * nfs file rename call 1643 */ 1644 int 1645 nfs_rename(v) 1646 void *v; 1647 { 1648 struct vop_rename_args /* { 1649 struct vnode *a_fdvp; 1650 struct vnode *a_fvp; 1651 struct componentname *a_fcnp; 1652 struct vnode *a_tdvp; 1653 struct vnode *a_tvp; 1654 struct componentname *a_tcnp; 1655 } */ *ap = v; 1656 struct vnode *fvp = ap->a_fvp; 1657 struct vnode *tvp = ap->a_tvp; 1658 struct vnode *fdvp = ap->a_fdvp; 1659 struct vnode *tdvp = ap->a_tdvp; 1660 struct componentname *tcnp = ap->a_tcnp; 1661 struct componentname *fcnp = ap->a_fcnp; 1662 int error; 1663 1664 #ifndef DIAGNOSTIC 1665 if ((tcnp->cn_flags & HASBUF) == 0 || 1666 (fcnp->cn_flags & HASBUF) == 0) 1667 panic("nfs_rename: no name"); 1668 #endif 1669 /* Check for cross-device rename */ 1670 if ((fvp->v_mount != tdvp->v_mount) || 1671 (tvp && (fvp->v_mount != tvp->v_mount))) { 1672 error = EXDEV; 1673 goto out; 1674 } 1675 1676 /* 1677 * If the tvp exists and is in use, sillyrename it before doing the 1678 * rename of the new file over it. 1679 */ 1680 if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename && 1681 tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) { 1682 vput(tvp); 1683 tvp = NULL; 1684 } 1685 1686 error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen, 1687 tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred, 1688 tcnp->cn_proc); 1689 1690 if (fvp->v_type == VDIR) { 1691 if (tvp != NULL && tvp->v_type == VDIR) 1692 cache_purge(tdvp); 1693 cache_purge(fdvp); 1694 } 1695 out: 1696 if (tdvp == tvp) 1697 vrele(tdvp); 1698 else 1699 vput(tdvp); 1700 if (tvp) 1701 vput(tvp); 1702 vrele(fdvp); 1703 vrele(fvp); 1704 /* 1705 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. 1706 */ 1707 if (error == ENOENT) 1708 error = 0; 1709 return (error); 1710 } 1711 1712 /* 1713 * nfs file rename rpc called from nfs_remove() above 1714 */ 1715 int 1716 nfs_renameit(sdvp, scnp, sp) 1717 struct vnode *sdvp; 1718 struct componentname *scnp; 1719 struct sillyrename *sp; 1720 { 1721 return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen, 1722 sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_proc)); 1723 } 1724 1725 /* 1726 * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit(). 1727 */ 1728 int 1729 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, proc) 1730 struct vnode *fdvp; 1731 const char *fnameptr; 1732 int fnamelen; 1733 struct vnode *tdvp; 1734 const char *tnameptr; 1735 int tnamelen; 1736 struct ucred *cred; 1737 struct proc *proc; 1738 { 1739 u_int32_t *tl; 1740 caddr_t cp; 1741 int32_t t1, t2; 1742 caddr_t bpos, dpos, cp2; 1743 int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR; 1744 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1745 const int v3 = NFS_ISV3(fdvp); 1746 1747 nfsstats.rpccnt[NFSPROC_RENAME]++; 1748 nfsm_reqhead(fdvp, NFSPROC_RENAME, 1749 (NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) + 1750 nfsm_rndup(tnamelen)); 1751 nfsm_fhtom(fdvp, v3); 1752 nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN); 1753 nfsm_fhtom(tdvp, v3); 1754 nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN); 1755 nfsm_request(fdvp, NFSPROC_RENAME, proc, cred); 1756 if (v3) { 1757 nfsm_wcc_data(fdvp, fwccflag); 1758 nfsm_wcc_data(tdvp, twccflag); 1759 } 1760 nfsm_reqdone; 1761 VTONFS(fdvp)->n_flag |= NMODIFIED; 1762 VTONFS(tdvp)->n_flag |= NMODIFIED; 1763 if (!fwccflag) 1764 VTONFS(fdvp)->n_attrstamp = 0; 1765 if (!twccflag) 1766 VTONFS(tdvp)->n_attrstamp = 0; 1767 return (error); 1768 } 1769 1770 /* 1771 * nfs hard link create call 1772 */ 1773 int 1774 nfs_link(v) 1775 void *v; 1776 { 1777 struct vop_link_args /* { 1778 struct vnode *a_dvp; 1779 struct vnode *a_vp; 1780 struct componentname *a_cnp; 1781 } */ *ap = v; 1782 struct vnode *vp = ap->a_vp; 1783 struct vnode *dvp = ap->a_dvp; 1784 struct componentname *cnp = ap->a_cnp; 1785 u_int32_t *tl; 1786 caddr_t cp; 1787 int32_t t1, t2; 1788 caddr_t bpos, dpos, cp2; 1789 int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0; 1790 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1791 /* XXX Should be const and initialised? */ 1792 int v3; 1793 1794 if (dvp->v_mount != vp->v_mount) { 1795 VOP_ABORTOP(vp, cnp); 1796 vput(dvp); 1797 return (EXDEV); 1798 } 1799 if (dvp != vp) { 1800 error = vn_lock(vp, LK_EXCLUSIVE); 1801 if (error != 0) { 1802 VOP_ABORTOP(dvp, cnp); 1803 vput(dvp); 1804 return error; 1805 } 1806 } 1807 1808 /* 1809 * Push all writes to the server, so that the attribute cache 1810 * doesn't get "out of sync" with the server. 1811 * XXX There should be a better way! 1812 */ 1813 VOP_FSYNC(vp, cnp->cn_cred, FSYNC_WAIT, 0, 0, cnp->cn_proc); 1814 1815 v3 = NFS_ISV3(vp); 1816 nfsstats.rpccnt[NFSPROC_LINK]++; 1817 nfsm_reqhead(vp, NFSPROC_LINK, 1818 NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); 1819 nfsm_fhtom(vp, v3); 1820 nfsm_fhtom(dvp, v3); 1821 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1822 nfsm_request(vp, NFSPROC_LINK, cnp->cn_proc, cnp->cn_cred); 1823 if (v3) { 1824 nfsm_postop_attr(vp, attrflag); 1825 nfsm_wcc_data(dvp, wccflag); 1826 } 1827 nfsm_reqdone; 1828 PNBUF_PUT(cnp->cn_pnbuf); 1829 VTONFS(dvp)->n_flag |= NMODIFIED; 1830 if (!attrflag) 1831 VTONFS(vp)->n_attrstamp = 0; 1832 if (!wccflag) 1833 VTONFS(dvp)->n_attrstamp = 0; 1834 if (dvp != vp) 1835 VOP_UNLOCK(vp, 0); 1836 vput(dvp); 1837 /* 1838 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 1839 */ 1840 if (error == EEXIST) 1841 error = 0; 1842 return (error); 1843 } 1844 1845 /* 1846 * nfs symbolic link create call 1847 */ 1848 int 1849 nfs_symlink(v) 1850 void *v; 1851 { 1852 struct vop_symlink_args /* { 1853 struct vnode *a_dvp; 1854 struct vnode **a_vpp; 1855 struct componentname *a_cnp; 1856 struct vattr *a_vap; 1857 char *a_target; 1858 } */ *ap = v; 1859 struct vnode *dvp = ap->a_dvp; 1860 struct vattr *vap = ap->a_vap; 1861 struct componentname *cnp = ap->a_cnp; 1862 struct nfsv2_sattr *sp; 1863 u_int32_t *tl; 1864 caddr_t cp; 1865 int32_t t1, t2; 1866 caddr_t bpos, dpos, cp2; 1867 int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp; 1868 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1869 struct vnode *newvp = (struct vnode *)0; 1870 const int v3 = NFS_ISV3(dvp); 1871 1872 *ap->a_vpp = NULL; 1873 nfsstats.rpccnt[NFSPROC_SYMLINK]++; 1874 slen = strlen(ap->a_target); 1875 nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED + 1876 nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3)); 1877 nfsm_fhtom(dvp, v3); 1878 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 1879 if (v3) 1880 nfsm_v3attrbuild(vap, FALSE); 1881 nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN); 1882 if (!v3) { 1883 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1884 sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode); 1885 sp->sa_uid = nfs_xdrneg1; 1886 sp->sa_gid = nfs_xdrneg1; 1887 sp->sa_size = nfs_xdrneg1; 1888 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1889 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1890 } 1891 nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_proc, cnp->cn_cred); 1892 if (v3) { 1893 if (!error) 1894 nfsm_mtofh(dvp, newvp, v3, gotvp); 1895 nfsm_wcc_data(dvp, wccflag); 1896 } 1897 nfsm_reqdone; 1898 /* 1899 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 1900 */ 1901 if (error == EEXIST) 1902 error = 0; 1903 if (error == 0 && newvp == NULL) { 1904 struct nfsnode *np = NULL; 1905 1906 error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen, 1907 cnp->cn_cred, cnp->cn_proc, &np); 1908 if (error == 0) 1909 newvp = NFSTOV(np); 1910 } 1911 if (error) { 1912 if (newvp != NULL) 1913 vput(newvp); 1914 } else { 1915 *ap->a_vpp = newvp; 1916 } 1917 PNBUF_PUT(cnp->cn_pnbuf); 1918 VTONFS(dvp)->n_flag |= NMODIFIED; 1919 if (!wccflag) 1920 VTONFS(dvp)->n_attrstamp = 0; 1921 vput(dvp); 1922 return (error); 1923 } 1924 1925 /* 1926 * nfs make dir call 1927 */ 1928 int 1929 nfs_mkdir(v) 1930 void *v; 1931 { 1932 struct vop_mkdir_args /* { 1933 struct vnode *a_dvp; 1934 struct vnode **a_vpp; 1935 struct componentname *a_cnp; 1936 struct vattr *a_vap; 1937 } */ *ap = v; 1938 struct vnode *dvp = ap->a_dvp; 1939 struct vattr *vap = ap->a_vap; 1940 struct componentname *cnp = ap->a_cnp; 1941 struct nfsv2_sattr *sp; 1942 u_int32_t *tl; 1943 caddr_t cp; 1944 int32_t t1, t2; 1945 int len; 1946 struct nfsnode *np = (struct nfsnode *)0; 1947 struct vnode *newvp = (struct vnode *)0; 1948 caddr_t bpos, dpos, cp2; 1949 int error = 0, wccflag = NFSV3_WCCRATTR; 1950 int gotvp = 0; 1951 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 1952 const int v3 = NFS_ISV3(dvp); 1953 1954 len = cnp->cn_namelen; 1955 nfsstats.rpccnt[NFSPROC_MKDIR]++; 1956 nfsm_reqhead(dvp, NFSPROC_MKDIR, 1957 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3)); 1958 nfsm_fhtom(dvp, v3); 1959 nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN); 1960 if (v3) { 1961 nfsm_v3attrbuild(vap, FALSE); 1962 } else { 1963 nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1964 sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode); 1965 sp->sa_uid = nfs_xdrneg1; 1966 sp->sa_gid = nfs_xdrneg1; 1967 sp->sa_size = nfs_xdrneg1; 1968 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 1969 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 1970 } 1971 nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_proc, cnp->cn_cred); 1972 if (!error) 1973 nfsm_mtofh(dvp, newvp, v3, gotvp); 1974 if (v3) 1975 nfsm_wcc_data(dvp, wccflag); 1976 nfsm_reqdone; 1977 VTONFS(dvp)->n_flag |= NMODIFIED; 1978 if (!wccflag) 1979 VTONFS(dvp)->n_attrstamp = 0; 1980 /* 1981 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry 1982 * if we can succeed in looking up the directory. 1983 */ 1984 if (error == EEXIST || (!error && !gotvp)) { 1985 if (newvp) { 1986 vput(newvp); 1987 newvp = (struct vnode *)0; 1988 } 1989 error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred, 1990 cnp->cn_proc, &np); 1991 if (!error) { 1992 newvp = NFSTOV(np); 1993 if (newvp->v_type != VDIR) 1994 error = EEXIST; 1995 } 1996 } 1997 if (error) { 1998 if (newvp) 1999 vput(newvp); 2000 } else { 2001 if (cnp->cn_flags & MAKEENTRY) 2002 cache_enter(dvp, newvp, cnp); 2003 *ap->a_vpp = newvp; 2004 } 2005 PNBUF_PUT(cnp->cn_pnbuf); 2006 vput(dvp); 2007 return (error); 2008 } 2009 2010 /* 2011 * nfs remove directory call 2012 */ 2013 int 2014 nfs_rmdir(v) 2015 void *v; 2016 { 2017 struct vop_rmdir_args /* { 2018 struct vnode *a_dvp; 2019 struct vnode *a_vp; 2020 struct componentname *a_cnp; 2021 } */ *ap = v; 2022 struct vnode *vp = ap->a_vp; 2023 struct vnode *dvp = ap->a_dvp; 2024 struct componentname *cnp = ap->a_cnp; 2025 u_int32_t *tl; 2026 caddr_t cp; 2027 int32_t t1, t2; 2028 caddr_t bpos, dpos, cp2; 2029 int error = 0, wccflag = NFSV3_WCCRATTR; 2030 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2031 const int v3 = NFS_ISV3(dvp); 2032 2033 if (dvp == vp) { 2034 vrele(dvp); 2035 vput(dvp); 2036 PNBUF_PUT(cnp->cn_pnbuf); 2037 return (EINVAL); 2038 } 2039 nfsstats.rpccnt[NFSPROC_RMDIR]++; 2040 nfsm_reqhead(dvp, NFSPROC_RMDIR, 2041 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen)); 2042 nfsm_fhtom(dvp, v3); 2043 nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN); 2044 nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_proc, cnp->cn_cred); 2045 if (v3) 2046 nfsm_wcc_data(dvp, wccflag); 2047 nfsm_reqdone; 2048 PNBUF_PUT(cnp->cn_pnbuf); 2049 VTONFS(dvp)->n_flag |= NMODIFIED; 2050 if (!wccflag) 2051 VTONFS(dvp)->n_attrstamp = 0; 2052 cache_purge(dvp); 2053 cache_purge(vp); 2054 vput(vp); 2055 vput(dvp); 2056 /* 2057 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 2058 */ 2059 if (error == ENOENT) 2060 error = 0; 2061 return (error); 2062 } 2063 2064 /* 2065 * nfs readdir call 2066 */ 2067 int 2068 nfs_readdir(v) 2069 void *v; 2070 { 2071 struct vop_readdir_args /* { 2072 struct vnode *a_vp; 2073 struct uio *a_uio; 2074 struct ucred *a_cred; 2075 int *a_eofflag; 2076 off_t **a_cookies; 2077 int *a_ncookies; 2078 } */ *ap = v; 2079 struct vnode *vp = ap->a_vp; 2080 struct uio *uio = ap->a_uio; 2081 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2082 char *base = uio->uio_iov->iov_base; 2083 int tresid, error; 2084 size_t count, lost; 2085 struct dirent *dp; 2086 off_t *cookies = NULL; 2087 int ncookies = 0, nc; 2088 2089 if (vp->v_type != VDIR) 2090 return (EPERM); 2091 2092 lost = uio->uio_resid & (NFS_DIRFRAGSIZ - 1); 2093 count = uio->uio_resid - lost; 2094 if (count <= 0) 2095 return (EINVAL); 2096 2097 /* 2098 * Call nfs_bioread() to do the real work. 2099 */ 2100 tresid = uio->uio_resid = count; 2101 error = nfs_bioread(vp, uio, 0, ap->a_cred, 2102 ap->a_cookies ? NFSBIO_CACHECOOKIES : 0); 2103 2104 if (!error && ap->a_cookies) { 2105 ncookies = count / 16; 2106 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK); 2107 *ap->a_cookies = cookies; 2108 } 2109 2110 if (!error && uio->uio_resid == tresid) { 2111 uio->uio_resid += lost; 2112 nfsstats.direofcache_misses++; 2113 if (ap->a_cookies) 2114 *ap->a_ncookies = 0; 2115 *ap->a_eofflag = 1; 2116 return (0); 2117 } 2118 2119 if (!error && ap->a_cookies) { 2120 /* 2121 * Only the NFS server and emulations use cookies, and they 2122 * load the directory block into system space, so we can 2123 * just look at it directly. 2124 */ 2125 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2126 panic("nfs_readdir: lost in space"); 2127 for (nc = 0; ncookies-- && 2128 base < (char *)uio->uio_iov->iov_base; nc++){ 2129 dp = (struct dirent *) base; 2130 if (dp->d_reclen == 0) 2131 break; 2132 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) 2133 *(cookies++) = (off_t)NFS_GETCOOKIE32(dp); 2134 else 2135 *(cookies++) = NFS_GETCOOKIE(dp); 2136 base += dp->d_reclen; 2137 } 2138 uio->uio_resid += 2139 ((caddr_t)uio->uio_iov->iov_base - base); 2140 uio->uio_iov->iov_len += 2141 ((caddr_t)uio->uio_iov->iov_base - base); 2142 uio->uio_iov->iov_base = base; 2143 *ap->a_ncookies = nc; 2144 } 2145 2146 uio->uio_resid += lost; 2147 *ap->a_eofflag = 0; 2148 return (error); 2149 } 2150 2151 /* 2152 * Readdir rpc call. 2153 * Called from below the buffer cache by nfs_doio(). 2154 */ 2155 int 2156 nfs_readdirrpc(vp, uiop, cred) 2157 struct vnode *vp; 2158 struct uio *uiop; 2159 struct ucred *cred; 2160 { 2161 int len, left; 2162 struct dirent *dp = NULL; 2163 u_int32_t *tl; 2164 caddr_t cp; 2165 int32_t t1, t2; 2166 caddr_t bpos, dpos, cp2; 2167 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2168 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2169 struct nfsnode *dnp = VTONFS(vp); 2170 u_quad_t fileno; 2171 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2172 int attrflag, nrpcs = 0, reclen; 2173 const int v3 = NFS_ISV3(vp); 2174 nfsquad_t cookie; 2175 2176 #ifdef DIAGNOSTIC 2177 /* 2178 * Should be called from buffer cache, so only amount of 2179 * NFS_DIRBLKSIZ will be requested. 2180 */ 2181 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1))) 2182 panic("nfs readdirrpc bad uio"); 2183 #endif 2184 2185 /* 2186 * Loop around doing readdir rpc's of size nm_readdirsize 2187 * truncated to a multiple of NFS_DIRFRAGSIZ. 2188 * The stopping criteria is EOF or buffer full. 2189 */ 2190 while (more_dirs && bigenough) { 2191 /* 2192 * Heuristic: don't bother to do another RPC to further 2193 * fill up this block if there is not much room left. (< 50% 2194 * of the readdir RPC size). This wastes some buffer space 2195 * but can save up to 50% in RPC calls. 2196 */ 2197 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) { 2198 bigenough = 0; 2199 break; 2200 } 2201 nfsstats.rpccnt[NFSPROC_READDIR]++; 2202 nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) + 2203 NFSX_READDIR(v3)); 2204 nfsm_fhtom(vp, v3); 2205 if (v3) { 2206 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2207 cookie.qval = uiop->uio_offset; 2208 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) { 2209 txdr_swapcookie3(uiop->uio_offset, tl); 2210 } else { 2211 txdr_cookie3(uiop->uio_offset, tl); 2212 } 2213 tl += 2; 2214 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2215 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2216 } else { 2217 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2218 *tl++ = txdr_unsigned(uiop->uio_offset); 2219 } 2220 *tl = txdr_unsigned(nmp->nm_readdirsize); 2221 nfsm_request(vp, NFSPROC_READDIR, uiop->uio_procp, cred); 2222 nrpcs++; 2223 if (v3) { 2224 nfsm_postop_attr(vp, attrflag); 2225 if (!error) { 2226 nfsm_dissect(tl, u_int32_t *, 2227 2 * NFSX_UNSIGNED); 2228 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2229 dnp->n_cookieverf.nfsuquad[1] = *tl; 2230 } else { 2231 m_freem(mrep); 2232 goto nfsmout; 2233 } 2234 } 2235 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2236 more_dirs = fxdr_unsigned(int, *tl); 2237 2238 /* loop thru the dir entries, doctoring them to 4bsd form */ 2239 while (more_dirs && bigenough) { 2240 if (v3) { 2241 nfsm_dissect(tl, u_int32_t *, 2242 3 * NFSX_UNSIGNED); 2243 fileno = fxdr_hyper(tl); 2244 len = fxdr_unsigned(int, *(tl + 2)); 2245 } else { 2246 nfsm_dissect(tl, u_int32_t *, 2247 2 * NFSX_UNSIGNED); 2248 fileno = fxdr_unsigned(u_quad_t, *tl++); 2249 len = fxdr_unsigned(int, *tl); 2250 } 2251 if (len <= 0 || len > NFS_MAXNAMLEN) { 2252 error = EBADRPC; 2253 m_freem(mrep); 2254 goto nfsmout; 2255 } 2256 tlen = nfsm_rndup(len); 2257 if (tlen == len) 2258 tlen += 4; /* To ensure null termination */ 2259 tlen += sizeof (off_t) + sizeof (int); 2260 reclen = ALIGN(tlen + DIRHDSIZ); 2261 tlen = reclen - DIRHDSIZ; 2262 left = NFS_DIRFRAGSIZ - blksiz; 2263 if (reclen > left) { 2264 dp->d_reclen += left; 2265 uiop->uio_iov->iov_base = 2266 (caddr_t)uiop->uio_iov->iov_base + left; 2267 uiop->uio_iov->iov_len -= left; 2268 uiop->uio_resid -= left; 2269 blksiz = 0; 2270 NFS_STASHCOOKIE(dp, uiop->uio_offset); 2271 } 2272 if (reclen > uiop->uio_resid) 2273 bigenough = 0; 2274 if (bigenough) { 2275 dp = (struct dirent *)uiop->uio_iov->iov_base; 2276 dp->d_fileno = (int)fileno; 2277 dp->d_namlen = len; 2278 dp->d_reclen = reclen; 2279 dp->d_type = DT_UNKNOWN; 2280 blksiz += dp->d_reclen; 2281 if (blksiz == NFS_DIRFRAGSIZ) 2282 blksiz = 0; 2283 uiop->uio_resid -= DIRHDSIZ; 2284 uiop->uio_iov->iov_base = 2285 (caddr_t)uiop->uio_iov->iov_base + DIRHDSIZ; 2286 uiop->uio_iov->iov_len -= DIRHDSIZ; 2287 nfsm_mtouio(uiop, len); 2288 cp = uiop->uio_iov->iov_base; 2289 tlen -= len; 2290 *cp = '\0'; /* null terminate */ 2291 uiop->uio_iov->iov_base = 2292 (caddr_t)uiop->uio_iov->iov_base + tlen; 2293 uiop->uio_iov->iov_len -= tlen; 2294 uiop->uio_resid -= tlen; 2295 } else 2296 nfsm_adv(nfsm_rndup(len)); 2297 if (v3) { 2298 nfsm_dissect(tl, u_int32_t *, 2299 3 * NFSX_UNSIGNED); 2300 } else { 2301 nfsm_dissect(tl, u_int32_t *, 2302 2 * NFSX_UNSIGNED); 2303 } 2304 if (bigenough) { 2305 if (v3) { 2306 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) 2307 uiop->uio_offset = 2308 fxdr_swapcookie3(tl); 2309 else 2310 uiop->uio_offset = 2311 fxdr_cookie3(tl); 2312 } 2313 else { 2314 uiop->uio_offset = 2315 fxdr_unsigned(off_t, *tl); 2316 } 2317 NFS_STASHCOOKIE(dp, uiop->uio_offset); 2318 } 2319 if (v3) 2320 tl += 2; 2321 else 2322 tl++; 2323 more_dirs = fxdr_unsigned(int, *tl); 2324 } 2325 /* 2326 * If at end of rpc data, get the eof boolean 2327 */ 2328 if (!more_dirs) { 2329 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2330 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2331 } 2332 m_freem(mrep); 2333 } 2334 /* 2335 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ 2336 * by increasing d_reclen for the last record. 2337 */ 2338 if (blksiz > 0) { 2339 left = NFS_DIRFRAGSIZ - blksiz; 2340 dp->d_reclen += left; 2341 NFS_STASHCOOKIE(dp, uiop->uio_offset); 2342 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base + 2343 left; 2344 uiop->uio_iov->iov_len -= left; 2345 uiop->uio_resid -= left; 2346 } 2347 2348 /* 2349 * We are now either at the end of the directory or have filled the 2350 * block. 2351 */ 2352 if (bigenough) 2353 dnp->n_direofoffset = uiop->uio_offset; 2354 nfsmout: 2355 return (error); 2356 } 2357 2358 /* 2359 * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc(). 2360 */ 2361 int 2362 nfs_readdirplusrpc(vp, uiop, cred) 2363 struct vnode *vp; 2364 struct uio *uiop; 2365 struct ucred *cred; 2366 { 2367 int len, left; 2368 struct dirent *dp = NULL; 2369 u_int32_t *tl; 2370 caddr_t cp; 2371 int32_t t1, t2; 2372 struct vnode *newvp; 2373 caddr_t bpos, dpos, cp2; 2374 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2375 struct nameidata nami, *ndp = &nami; 2376 struct componentname *cnp = &ndp->ni_cnd; 2377 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2378 struct nfsnode *dnp = VTONFS(vp), *np; 2379 const unsigned char *hcp; 2380 nfsfh_t *fhp; 2381 u_quad_t fileno; 2382 int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i; 2383 int attrflag, fhsize, nrpcs = 0, reclen; 2384 struct nfs_fattr fattr, *fp; 2385 2386 #ifdef DIAGNOSTIC 2387 if (uiop->uio_iovcnt != 1 || (uiop->uio_resid & (NFS_DIRBLKSIZ - 1))) 2388 panic("nfs readdirplusrpc bad uio"); 2389 #endif 2390 ndp->ni_dvp = vp; 2391 newvp = NULLVP; 2392 2393 /* 2394 * Loop around doing readdir rpc's of size nm_readdirsize 2395 * truncated to a multiple of NFS_DIRFRAGSIZ. 2396 * The stopping criteria is EOF or buffer full. 2397 */ 2398 while (more_dirs && bigenough) { 2399 if (nrpcs > 0 && uiop->uio_resid < (nmp->nm_readdirsize / 2)) { 2400 bigenough = 0; 2401 break; 2402 } 2403 nfsstats.rpccnt[NFSPROC_READDIRPLUS]++; 2404 nfsm_reqhead(vp, NFSPROC_READDIRPLUS, 2405 NFSX_FH(1) + 6 * NFSX_UNSIGNED); 2406 nfsm_fhtom(vp, 1); 2407 nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 2408 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) { 2409 txdr_swapcookie3(uiop->uio_offset, tl); 2410 } else { 2411 txdr_cookie3(uiop->uio_offset, tl); 2412 } 2413 tl += 2; 2414 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2415 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2416 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 2417 *tl = txdr_unsigned(nmp->nm_rsize); 2418 nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_procp, cred); 2419 nfsm_postop_attr(vp, attrflag); 2420 if (error) { 2421 m_freem(mrep); 2422 goto nfsmout; 2423 } 2424 nrpcs++; 2425 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2426 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2427 dnp->n_cookieverf.nfsuquad[1] = *tl++; 2428 more_dirs = fxdr_unsigned(int, *tl); 2429 2430 /* loop thru the dir entries, doctoring them to 4bsd form */ 2431 while (more_dirs && bigenough) { 2432 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2433 fileno = fxdr_hyper(tl); 2434 len = fxdr_unsigned(int, *(tl + 2)); 2435 if (len <= 0 || len > NFS_MAXNAMLEN) { 2436 error = EBADRPC; 2437 m_freem(mrep); 2438 goto nfsmout; 2439 } 2440 tlen = nfsm_rndup(len); 2441 if (tlen == len) 2442 tlen += 4; /* To ensure null termination*/ 2443 tlen += sizeof (off_t) + sizeof (int); 2444 reclen = ALIGN(tlen + DIRHDSIZ); 2445 tlen = reclen - DIRHDSIZ; 2446 left = NFS_DIRFRAGSIZ - blksiz; 2447 if (reclen > left) { 2448 /* 2449 * DIRFRAGSIZ is aligned, no need to align 2450 * again here. 2451 */ 2452 dp->d_reclen += left; 2453 uiop->uio_iov->iov_base = 2454 (caddr_t)uiop->uio_iov->iov_base + left; 2455 uiop->uio_iov->iov_len -= left; 2456 uiop->uio_resid -= left; 2457 NFS_STASHCOOKIE(dp, uiop->uio_offset); 2458 blksiz = 0; 2459 } 2460 if (reclen > uiop->uio_resid) 2461 bigenough = 0; 2462 if (bigenough) { 2463 dp = (struct dirent *)uiop->uio_iov->iov_base; 2464 dp->d_fileno = (int)fileno; 2465 dp->d_namlen = len; 2466 dp->d_reclen = reclen; 2467 dp->d_type = DT_UNKNOWN; 2468 blksiz += dp->d_reclen; 2469 if (blksiz == NFS_DIRFRAGSIZ) 2470 blksiz = 0; 2471 uiop->uio_resid -= DIRHDSIZ; 2472 uiop->uio_iov->iov_base = 2473 (caddr_t)uiop->uio_iov->iov_base + 2474 DIRHDSIZ; 2475 uiop->uio_iov->iov_len -= DIRHDSIZ; 2476 cnp->cn_nameptr = uiop->uio_iov->iov_base; 2477 cnp->cn_namelen = len; 2478 nfsm_mtouio(uiop, len); 2479 cp = uiop->uio_iov->iov_base; 2480 tlen -= len; 2481 *cp = '\0'; 2482 uiop->uio_iov->iov_base = 2483 (caddr_t)uiop->uio_iov->iov_base + tlen; 2484 uiop->uio_iov->iov_len -= tlen; 2485 uiop->uio_resid -= tlen; 2486 } else 2487 nfsm_adv(nfsm_rndup(len)); 2488 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2489 if (bigenough) { 2490 if (nmp->nm_iflag & NFSMNT_SWAPCOOKIE) 2491 uiop->uio_offset = 2492 fxdr_swapcookie3(tl); 2493 else 2494 uiop->uio_offset = 2495 fxdr_cookie3(tl); 2496 NFS_STASHCOOKIE(dp, uiop->uio_offset); 2497 } 2498 tl += 2; 2499 2500 /* 2501 * Since the attributes are before the file handle 2502 * (sigh), we must skip over the attributes and then 2503 * come back and get them. 2504 */ 2505 attrflag = fxdr_unsigned(int, *tl); 2506 if (attrflag) { 2507 nfsm_dissect(fp, struct nfs_fattr *, NFSX_V3FATTR); 2508 memcpy(&fattr, fp, NFSX_V3FATTR); 2509 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2510 doit = fxdr_unsigned(int, *tl); 2511 if (doit) { 2512 nfsm_getfh(fhp, fhsize, 1); 2513 if (NFS_CMPFH(dnp, fhp, fhsize)) { 2514 VREF(vp); 2515 newvp = vp; 2516 np = dnp; 2517 } else { 2518 error = nfs_nget(vp->v_mount, fhp, 2519 fhsize, &np); 2520 if (!error) 2521 newvp = NFSTOV(np); 2522 } 2523 if (!error) { 2524 nfs_loadattrcache(&newvp, &fattr, 0); 2525 dp->d_type = 2526 IFTODT(VTTOIF(np->n_vattr->va_type)); 2527 ndp->ni_vp = newvp; 2528 cnp->cn_hash = 0; 2529 for (hcp = cnp->cn_nameptr, i = 1; i <= len; 2530 i++, hcp++) 2531 cnp->cn_hash += *hcp * i; 2532 if (cnp->cn_namelen <= NCHNAMLEN) 2533 cache_enter(ndp->ni_dvp, ndp->ni_vp, 2534 cnp); 2535 } 2536 } 2537 } else { 2538 /* Just skip over the file handle */ 2539 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2540 i = fxdr_unsigned(int, *tl); 2541 nfsm_adv(nfsm_rndup(i)); 2542 } 2543 if (newvp != NULLVP) { 2544 vrele(newvp); 2545 if (newvp != vp) 2546 VOP_UNLOCK(vp, 0); 2547 newvp = NULLVP; 2548 } 2549 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2550 more_dirs = fxdr_unsigned(int, *tl); 2551 } 2552 /* 2553 * If at end of rpc data, get the eof boolean 2554 */ 2555 if (!more_dirs) { 2556 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2557 more_dirs = (fxdr_unsigned(int, *tl) == 0); 2558 } 2559 m_freem(mrep); 2560 } 2561 /* 2562 * Fill last record, iff any, out to a multiple of NFS_DIRFRAGSIZ 2563 * by increasing d_reclen for the last record. 2564 */ 2565 if (blksiz > 0) { 2566 left = NFS_DIRFRAGSIZ - blksiz; 2567 dp->d_reclen += left; 2568 NFS_STASHCOOKIE(dp, uiop->uio_offset); 2569 uiop->uio_iov->iov_base = (caddr_t)uiop->uio_iov->iov_base + 2570 left; 2571 uiop->uio_iov->iov_len -= left; 2572 uiop->uio_resid -= left; 2573 } 2574 2575 /* 2576 * We are now either at the end of the directory or have filled the 2577 * block. 2578 */ 2579 if (bigenough) 2580 dnp->n_direofoffset = uiop->uio_offset; 2581 nfsmout: 2582 if (newvp != NULLVP) { 2583 vrele(newvp); 2584 if (newvp != vp) 2585 VOP_UNLOCK(vp, 0); 2586 } 2587 return (error); 2588 } 2589 static char hextoasc[] = "0123456789abcdef"; 2590 2591 /* 2592 * Silly rename. To make the NFS filesystem that is stateless look a little 2593 * more like the "ufs" a remove of an active vnode is translated to a rename 2594 * to a funny looking filename that is removed by nfs_inactive on the 2595 * nfsnode. There is the potential for another process on a different client 2596 * to create the same funny name between the nfs_lookitup() fails and the 2597 * nfs_rename() completes, but... 2598 */ 2599 int 2600 nfs_sillyrename(dvp, vp, cnp) 2601 struct vnode *dvp, *vp; 2602 struct componentname *cnp; 2603 { 2604 struct sillyrename *sp; 2605 struct nfsnode *np; 2606 int error; 2607 short pid; 2608 2609 cache_purge(dvp); 2610 np = VTONFS(vp); 2611 #ifndef DIAGNOSTIC 2612 if (vp->v_type == VDIR) 2613 panic("nfs: sillyrename dir"); 2614 #endif 2615 MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename), 2616 M_NFSREQ, M_WAITOK); 2617 sp->s_cred = crdup(cnp->cn_cred); 2618 sp->s_dvp = dvp; 2619 VREF(dvp); 2620 2621 /* Fudge together a funny name */ 2622 pid = cnp->cn_proc->p_pid; 2623 memcpy(sp->s_name, ".nfsAxxxx4.4", 13); 2624 sp->s_namlen = 12; 2625 sp->s_name[8] = hextoasc[pid & 0xf]; 2626 sp->s_name[7] = hextoasc[(pid >> 4) & 0xf]; 2627 sp->s_name[6] = hextoasc[(pid >> 8) & 0xf]; 2628 sp->s_name[5] = hextoasc[(pid >> 12) & 0xf]; 2629 2630 /* Try lookitups until we get one that isn't there */ 2631 while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2632 cnp->cn_proc, (struct nfsnode **)0) == 0) { 2633 sp->s_name[4]++; 2634 if (sp->s_name[4] > 'z') { 2635 error = EINVAL; 2636 goto bad; 2637 } 2638 } 2639 error = nfs_renameit(dvp, cnp, sp); 2640 if (error) 2641 goto bad; 2642 error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred, 2643 cnp->cn_proc, &np); 2644 np->n_sillyrename = sp; 2645 return (0); 2646 bad: 2647 vrele(sp->s_dvp); 2648 crfree(sp->s_cred); 2649 free((caddr_t)sp, M_NFSREQ); 2650 return (error); 2651 } 2652 2653 /* 2654 * Look up a file name and optionally either update the file handle or 2655 * allocate an nfsnode, depending on the value of npp. 2656 * npp == NULL --> just do the lookup 2657 * *npp == NULL --> allocate a new nfsnode and make sure attributes are 2658 * handled too 2659 * *npp != NULL --> update the file handle in the vnode 2660 */ 2661 int 2662 nfs_lookitup(dvp, name, len, cred, procp, npp) 2663 struct vnode *dvp; 2664 const char *name; 2665 int len; 2666 struct ucred *cred; 2667 struct proc *procp; 2668 struct nfsnode **npp; 2669 { 2670 u_int32_t *tl; 2671 caddr_t cp; 2672 int32_t t1, t2; 2673 struct vnode *newvp = (struct vnode *)0; 2674 struct nfsnode *np, *dnp = VTONFS(dvp); 2675 caddr_t bpos, dpos, cp2; 2676 int error = 0, fhlen, attrflag; 2677 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2678 nfsfh_t *nfhp; 2679 const int v3 = NFS_ISV3(dvp); 2680 2681 nfsstats.rpccnt[NFSPROC_LOOKUP]++; 2682 nfsm_reqhead(dvp, NFSPROC_LOOKUP, 2683 NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len)); 2684 nfsm_fhtom(dvp, v3); 2685 nfsm_strtom(name, len, NFS_MAXNAMLEN); 2686 nfsm_request(dvp, NFSPROC_LOOKUP, procp, cred); 2687 if (npp && !error) { 2688 nfsm_getfh(nfhp, fhlen, v3); 2689 if (*npp) { 2690 np = *npp; 2691 if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) { 2692 free((caddr_t)np->n_fhp, M_NFSBIGFH); 2693 np->n_fhp = &np->n_fh; 2694 } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH) 2695 np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK); 2696 memcpy((caddr_t)np->n_fhp, (caddr_t)nfhp, fhlen); 2697 np->n_fhsize = fhlen; 2698 newvp = NFSTOV(np); 2699 } else if (NFS_CMPFH(dnp, nfhp, fhlen)) { 2700 VREF(dvp); 2701 newvp = dvp; 2702 } else { 2703 error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np); 2704 if (error) { 2705 m_freem(mrep); 2706 return (error); 2707 } 2708 newvp = NFSTOV(np); 2709 } 2710 if (v3) { 2711 nfsm_postop_attr(newvp, attrflag); 2712 if (!attrflag && *npp == NULL) { 2713 m_freem(mrep); 2714 vput(newvp); 2715 return (ENOENT); 2716 } 2717 } else 2718 nfsm_loadattr(newvp, (struct vattr *)0); 2719 } 2720 nfsm_reqdone; 2721 if (npp && *npp == NULL) { 2722 if (error) { 2723 if (newvp) 2724 vput(newvp); 2725 } else 2726 *npp = np; 2727 } 2728 return (error); 2729 } 2730 2731 /* 2732 * Nfs Version 3 commit rpc 2733 */ 2734 int 2735 nfs_commit(vp, offset, cnt, procp) 2736 struct vnode *vp; 2737 off_t offset; 2738 uint32_t cnt; 2739 struct proc *procp; 2740 { 2741 caddr_t cp; 2742 u_int32_t *tl; 2743 int32_t t1, t2; 2744 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 2745 caddr_t bpos, dpos, cp2; 2746 int error = 0, wccflag = NFSV3_WCCRATTR; 2747 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2748 2749 #ifdef fvdl_debug 2750 printf("commit %lu - %lu\n", (unsigned long)offset, 2751 (unsigned long)(offset + cnt)); 2752 #endif 2753 2754 if ((nmp->nm_iflag & NFSMNT_HASWRITEVERF) == 0) 2755 return (0); 2756 nfsstats.rpccnt[NFSPROC_COMMIT]++; 2757 nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1)); 2758 nfsm_fhtom(vp, 1); 2759 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2760 txdr_hyper(offset, tl); 2761 tl += 2; 2762 *tl = txdr_unsigned(cnt); 2763 nfsm_request(vp, NFSPROC_COMMIT, procp, VTONFS(vp)->n_wcred); 2764 nfsm_wcc_data(vp, wccflag); 2765 if (!error) { 2766 nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF); 2767 if (memcmp((caddr_t)nmp->nm_verf, (caddr_t)tl, 2768 NFSX_V3WRITEVERF)) { 2769 memcpy((caddr_t)nmp->nm_verf, (caddr_t)tl, 2770 NFSX_V3WRITEVERF); 2771 error = NFSERR_STALEWRITEVERF; 2772 } 2773 } 2774 nfsm_reqdone; 2775 return (error); 2776 } 2777 2778 /* 2779 * Kludge City.. 2780 * - make nfs_bmap() essentially a no-op that does no translation 2781 * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc 2782 * (Maybe I could use the process's page mapping, but I was concerned that 2783 * Kernel Write might not be enabled and also figured copyout() would do 2784 * a lot more work than memcpy() and also it currently happens in the 2785 * context of the swapper process (2). 2786 */ 2787 int 2788 nfs_bmap(v) 2789 void *v; 2790 { 2791 struct vop_bmap_args /* { 2792 struct vnode *a_vp; 2793 daddr_t a_bn; 2794 struct vnode **a_vpp; 2795 daddr_t *a_bnp; 2796 int *a_runp; 2797 } */ *ap = v; 2798 struct vnode *vp = ap->a_vp; 2799 int bshift = vp->v_mount->mnt_fs_bshift - vp->v_mount->mnt_dev_bshift; 2800 2801 if (ap->a_vpp != NULL) 2802 *ap->a_vpp = vp; 2803 if (ap->a_bnp != NULL) 2804 *ap->a_bnp = ap->a_bn << bshift; 2805 if (ap->a_runp != NULL) 2806 *ap->a_runp = 1024 * 1024; /* XXX */ 2807 return (0); 2808 } 2809 2810 /* 2811 * Strategy routine. 2812 * For async requests when nfsiod(s) are running, queue the request by 2813 * calling nfs_asyncio(), otherwise just all nfs_doio() to do the 2814 * request. 2815 */ 2816 int 2817 nfs_strategy(v) 2818 void *v; 2819 { 2820 struct vop_strategy_args *ap = v; 2821 struct buf *bp = ap->a_bp; 2822 struct proc *p; 2823 int error = 0; 2824 2825 if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC)) 2826 panic("nfs physio/async"); 2827 if (bp->b_flags & B_ASYNC) 2828 p = NULL; 2829 else 2830 p = curproc; /* XXX */ 2831 2832 /* 2833 * If the op is asynchronous and an i/o daemon is waiting 2834 * queue the request, wake it up and wait for completion 2835 * otherwise just do it ourselves. 2836 */ 2837 2838 if ((bp->b_flags & B_ASYNC) == 0 || 2839 nfs_asyncio(bp)) 2840 error = nfs_doio(bp, p); 2841 return (error); 2842 } 2843 2844 /* 2845 * fsync vnode op. Just call nfs_flush() with commit == 1. 2846 */ 2847 /* ARGSUSED */ 2848 int 2849 nfs_fsync(v) 2850 void *v; 2851 { 2852 struct vop_fsync_args /* { 2853 struct vnodeop_desc *a_desc; 2854 struct vnode * a_vp; 2855 struct ucred * a_cred; 2856 int a_flags; 2857 off_t offlo; 2858 off_t offhi; 2859 struct proc * a_p; 2860 } */ *ap = v; 2861 2862 return (nfs_flush(ap->a_vp, ap->a_cred, 2863 (ap->a_flags & FSYNC_WAIT) != 0 ? MNT_WAIT : 0, ap->a_p, 1)); 2864 } 2865 2866 /* 2867 * Flush all the data associated with a vnode. 2868 */ 2869 int 2870 nfs_flush(vp, cred, waitfor, p, commit) 2871 struct vnode *vp; 2872 struct ucred *cred; 2873 int waitfor; 2874 struct proc *p; 2875 int commit; 2876 { 2877 struct uvm_object *uobj = &vp->v_uobj; 2878 struct nfsnode *np = VTONFS(vp); 2879 int error; 2880 int flushflags = PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO; 2881 UVMHIST_FUNC("nfs_flush"); UVMHIST_CALLED(ubchist); 2882 2883 simple_lock(&uobj->vmobjlock); 2884 error = (uobj->pgops->pgo_put)(uobj, 0, 0, flushflags); 2885 if (np->n_flag & NWRITEERR) { 2886 error = np->n_error; 2887 np->n_flag &= ~NWRITEERR; 2888 } 2889 UVMHIST_LOG(ubchist, "returning %d", error,0,0,0); 2890 return (error); 2891 } 2892 2893 /* 2894 * Return POSIX pathconf information applicable to nfs. 2895 * 2896 * N.B. The NFS V2 protocol doesn't support this RPC. 2897 */ 2898 /* ARGSUSED */ 2899 int 2900 nfs_pathconf(v) 2901 void *v; 2902 { 2903 struct vop_pathconf_args /* { 2904 struct vnode *a_vp; 2905 int a_name; 2906 register_t *a_retval; 2907 } */ *ap = v; 2908 struct nfsv3_pathconf *pcp; 2909 struct vnode *vp = ap->a_vp; 2910 struct nfsmount *nmp; 2911 struct mbuf *mreq, *mrep, *md, *mb, *mb2; 2912 int32_t t1, t2; 2913 u_int32_t *tl; 2914 caddr_t bpos, dpos, cp, cp2; 2915 int error = 0, attrflag; 2916 unsigned int l; 2917 u_int64_t maxsize; 2918 const int v3 = NFS_ISV3(vp); 2919 2920 switch (ap->a_name) { 2921 /* Names that can be resolved locally. */ 2922 case _PC_PIPE_BUF: 2923 *ap->a_retval = PIPE_BUF; 2924 break; 2925 case _PC_SYNC_IO: 2926 *ap->a_retval = 1; 2927 break; 2928 /* Names that cannot be resolved locally; do an RPC, if possible. */ 2929 case _PC_LINK_MAX: 2930 case _PC_NAME_MAX: 2931 case _PC_CHOWN_RESTRICTED: 2932 case _PC_NO_TRUNC: 2933 if (!v3) { 2934 error = EINVAL; 2935 break; 2936 } 2937 nfsstats.rpccnt[NFSPROC_PATHCONF]++; 2938 nfsm_reqhead(vp, NFSPROC_PATHCONF, NFSX_FH(1)); 2939 nfsm_fhtom(vp, 1); 2940 nfsm_request(vp, NFSPROC_PATHCONF, 2941 curproc, curproc->p_ucred); /* XXX */ 2942 nfsm_postop_attr(vp, attrflag); 2943 if (!error) { 2944 nfsm_dissect(pcp, struct nfsv3_pathconf *, 2945 NFSX_V3PATHCONF); 2946 switch (ap->a_name) { 2947 case _PC_LINK_MAX: 2948 *ap->a_retval = 2949 fxdr_unsigned(register_t, pcp->pc_linkmax); 2950 break; 2951 case _PC_NAME_MAX: 2952 *ap->a_retval = 2953 fxdr_unsigned(register_t, pcp->pc_namemax); 2954 break; 2955 case _PC_CHOWN_RESTRICTED: 2956 *ap->a_retval = 2957 (pcp->pc_chownrestricted == nfs_true); 2958 break; 2959 case _PC_NO_TRUNC: 2960 *ap->a_retval = 2961 (pcp->pc_notrunc == nfs_true); 2962 break; 2963 } 2964 } 2965 nfsm_reqdone; 2966 break; 2967 case _PC_FILESIZEBITS: 2968 if (v3) { 2969 nmp = VFSTONFS(vp->v_mount); 2970 if ((nmp->nm_iflag & NFSMNT_GOTFSINFO) == 0) 2971 if ((error = nfs_fsinfo(nmp, vp, 2972 curproc->p_ucred, curproc)) != 0) /* XXX */ 2973 break; 2974 for (l = 0, maxsize = nmp->nm_maxfilesize; 2975 (maxsize >> l) > 0; l++) 2976 ; 2977 *ap->a_retval = l + 1; 2978 } else { 2979 *ap->a_retval = 32; /* NFS V2 limitation */ 2980 } 2981 break; 2982 default: 2983 error = EINVAL; 2984 break; 2985 } 2986 2987 return (error); 2988 } 2989 2990 /* 2991 * NFS advisory byte-level locks. 2992 */ 2993 int 2994 nfs_advlock(v) 2995 void *v; 2996 { 2997 struct vop_advlock_args /* { 2998 struct vnode *a_vp; 2999 caddr_t a_id; 3000 int a_op; 3001 struct flock *a_fl; 3002 int a_flags; 3003 } */ *ap = v; 3004 struct nfsnode *np = VTONFS(ap->a_vp); 3005 3006 return lf_advlock(ap, &np->n_lockf, np->n_size); 3007 } 3008 3009 /* 3010 * Print out the contents of an nfsnode. 3011 */ 3012 int 3013 nfs_print(v) 3014 void *v; 3015 { 3016 struct vop_print_args /* { 3017 struct vnode *a_vp; 3018 } */ *ap = v; 3019 struct vnode *vp = ap->a_vp; 3020 struct nfsnode *np = VTONFS(vp); 3021 3022 printf("tag VT_NFS, fileid %ld fsid 0x%lx", 3023 np->n_vattr->va_fileid, np->n_vattr->va_fsid); 3024 if (vp->v_type == VFIFO) 3025 fifo_printinfo(vp); 3026 printf("\n"); 3027 return (0); 3028 } 3029 3030 /* 3031 * NFS file truncation. 3032 */ 3033 int 3034 nfs_truncate(v) 3035 void *v; 3036 { 3037 #if 0 3038 struct vop_truncate_args /* { 3039 struct vnode *a_vp; 3040 off_t a_length; 3041 int a_flags; 3042 struct ucred *a_cred; 3043 struct proc *a_p; 3044 } */ *ap = v; 3045 #endif 3046 3047 /* Use nfs_setattr */ 3048 return (EOPNOTSUPP); 3049 } 3050 3051 /* 3052 * NFS update. 3053 */ 3054 int 3055 nfs_update(v) 3056 void *v; 3057 #if 0 3058 struct vop_update_args /* { 3059 struct vnode *a_vp; 3060 struct timespec *a_ta; 3061 struct timespec *a_tm; 3062 int a_waitfor; 3063 } */ *ap = v; 3064 #endif 3065 { 3066 3067 /* Use nfs_setattr */ 3068 return (EOPNOTSUPP); 3069 } 3070 3071 /* 3072 * Just call bwrite(). 3073 */ 3074 int 3075 nfs_bwrite(v) 3076 void *v; 3077 { 3078 struct vop_bwrite_args /* { 3079 struct vnode *a_bp; 3080 } */ *ap = v; 3081 3082 return (bwrite(ap->a_bp)); 3083 } 3084 3085 /* 3086 * nfs special file access vnode op. 3087 * Essentially just get vattr and then imitate iaccess() since the device is 3088 * local to the client. 3089 */ 3090 int 3091 nfsspec_access(v) 3092 void *v; 3093 { 3094 struct vop_access_args /* { 3095 struct vnode *a_vp; 3096 int a_mode; 3097 struct ucred *a_cred; 3098 struct proc *a_p; 3099 } */ *ap = v; 3100 struct vattr va; 3101 struct vnode *vp = ap->a_vp; 3102 int error; 3103 3104 error = VOP_GETATTR(vp, &va, ap->a_cred, ap->a_p); 3105 if (error) 3106 return (error); 3107 3108 /* 3109 * Disallow write attempts on filesystems mounted read-only; 3110 * unless the file is a socket, fifo, or a block or character 3111 * device resident on the filesystem. 3112 */ 3113 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 3114 switch (vp->v_type) { 3115 case VREG: 3116 case VDIR: 3117 case VLNK: 3118 return (EROFS); 3119 default: 3120 break; 3121 } 3122 } 3123 3124 return (vaccess(va.va_type, va.va_mode, 3125 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred)); 3126 } 3127 3128 /* 3129 * Read wrapper for special devices. 3130 */ 3131 int 3132 nfsspec_read(v) 3133 void *v; 3134 { 3135 struct vop_read_args /* { 3136 struct vnode *a_vp; 3137 struct uio *a_uio; 3138 int a_ioflag; 3139 struct ucred *a_cred; 3140 } */ *ap = v; 3141 struct nfsnode *np = VTONFS(ap->a_vp); 3142 3143 /* 3144 * Set access flag. 3145 */ 3146 np->n_flag |= NACC; 3147 np->n_atim.tv_sec = time.tv_sec; 3148 np->n_atim.tv_nsec = time.tv_usec * 1000; 3149 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap)); 3150 } 3151 3152 /* 3153 * Write wrapper for special devices. 3154 */ 3155 int 3156 nfsspec_write(v) 3157 void *v; 3158 { 3159 struct vop_write_args /* { 3160 struct vnode *a_vp; 3161 struct uio *a_uio; 3162 int a_ioflag; 3163 struct ucred *a_cred; 3164 } */ *ap = v; 3165 struct nfsnode *np = VTONFS(ap->a_vp); 3166 3167 /* 3168 * Set update flag. 3169 */ 3170 np->n_flag |= NUPD; 3171 np->n_mtim.tv_sec = time.tv_sec; 3172 np->n_mtim.tv_nsec = time.tv_usec * 1000; 3173 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap)); 3174 } 3175 3176 /* 3177 * Close wrapper for special devices. 3178 * 3179 * Update the times on the nfsnode then do device close. 3180 */ 3181 int 3182 nfsspec_close(v) 3183 void *v; 3184 { 3185 struct vop_close_args /* { 3186 struct vnode *a_vp; 3187 int a_fflag; 3188 struct ucred *a_cred; 3189 struct proc *a_p; 3190 } */ *ap = v; 3191 struct vnode *vp = ap->a_vp; 3192 struct nfsnode *np = VTONFS(vp); 3193 struct vattr vattr; 3194 3195 if (np->n_flag & (NACC | NUPD)) { 3196 np->n_flag |= NCHG; 3197 if (vp->v_usecount == 1 && 3198 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 3199 VATTR_NULL(&vattr); 3200 if (np->n_flag & NACC) 3201 vattr.va_atime = np->n_atim; 3202 if (np->n_flag & NUPD) 3203 vattr.va_mtime = np->n_mtim; 3204 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p); 3205 } 3206 } 3207 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap)); 3208 } 3209 3210 /* 3211 * Read wrapper for fifos. 3212 */ 3213 int 3214 nfsfifo_read(v) 3215 void *v; 3216 { 3217 struct vop_read_args /* { 3218 struct vnode *a_vp; 3219 struct uio *a_uio; 3220 int a_ioflag; 3221 struct ucred *a_cred; 3222 } */ *ap = v; 3223 struct nfsnode *np = VTONFS(ap->a_vp); 3224 3225 /* 3226 * Set access flag. 3227 */ 3228 np->n_flag |= NACC; 3229 np->n_atim.tv_sec = time.tv_sec; 3230 np->n_atim.tv_nsec = time.tv_usec * 1000; 3231 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap)); 3232 } 3233 3234 /* 3235 * Write wrapper for fifos. 3236 */ 3237 int 3238 nfsfifo_write(v) 3239 void *v; 3240 { 3241 struct vop_write_args /* { 3242 struct vnode *a_vp; 3243 struct uio *a_uio; 3244 int a_ioflag; 3245 struct ucred *a_cred; 3246 } */ *ap = v; 3247 struct nfsnode *np = VTONFS(ap->a_vp); 3248 3249 /* 3250 * Set update flag. 3251 */ 3252 np->n_flag |= NUPD; 3253 np->n_mtim.tv_sec = time.tv_sec; 3254 np->n_mtim.tv_nsec = time.tv_usec * 1000; 3255 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap)); 3256 } 3257 3258 /* 3259 * Close wrapper for fifos. 3260 * 3261 * Update the times on the nfsnode then do fifo close. 3262 */ 3263 int 3264 nfsfifo_close(v) 3265 void *v; 3266 { 3267 struct vop_close_args /* { 3268 struct vnode *a_vp; 3269 int a_fflag; 3270 struct ucred *a_cred; 3271 struct proc *a_p; 3272 } */ *ap = v; 3273 struct vnode *vp = ap->a_vp; 3274 struct nfsnode *np = VTONFS(vp); 3275 struct vattr vattr; 3276 3277 if (np->n_flag & (NACC | NUPD)) { 3278 if (np->n_flag & NACC) { 3279 np->n_atim.tv_sec = time.tv_sec; 3280 np->n_atim.tv_nsec = time.tv_usec * 1000; 3281 } 3282 if (np->n_flag & NUPD) { 3283 np->n_mtim.tv_sec = time.tv_sec; 3284 np->n_mtim.tv_nsec = time.tv_usec * 1000; 3285 } 3286 np->n_flag |= NCHG; 3287 if (vp->v_usecount == 1 && 3288 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 3289 VATTR_NULL(&vattr); 3290 if (np->n_flag & NACC) 3291 vattr.va_atime = np->n_atim; 3292 if (np->n_flag & NUPD) 3293 vattr.va_mtime = np->n_mtim; 3294 (void)VOP_SETATTR(vp, &vattr, ap->a_cred, ap->a_p); 3295 } 3296 } 3297 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap)); 3298 } 3299