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