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