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