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