1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)vfs_vnops.c 7.32 (Berkeley) 06/21/91 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "kernel.h" 13 #include "file.h" 14 #include "stat.h" 15 #include "buf.h" 16 #include "proc.h" 17 #include "mount.h" 18 #include "namei.h" 19 #include "vnode.h" 20 #include "ioctl.h" 21 #include "tty.h" 22 23 struct fileops vnops = 24 { vn_read, vn_write, vn_ioctl, vn_select, vn_close }; 25 26 /* 27 * Common code for vnode open operations. 28 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine. 29 */ 30 vn_open(ndp, p, fmode, cmode) 31 register struct nameidata *ndp; 32 struct proc *p; 33 int fmode, cmode; 34 { 35 register struct vnode *vp; 36 register struct ucred *cred = p->p_ucred; 37 struct vattr vat; 38 struct vattr *vap = &vat; 39 int error; 40 41 if (fmode & O_CREAT) { 42 ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF; 43 if ((fmode & O_EXCL) == 0) 44 ndp->ni_nameiop |= FOLLOW; 45 if (error = namei(ndp, p)) 46 return (error); 47 if (ndp->ni_vp == NULL) { 48 VATTR_NULL(vap); 49 vap->va_type = VREG; 50 vap->va_mode = cmode; 51 if (error = VOP_CREATE(ndp, vap, p)) 52 return (error); 53 fmode &= ~O_TRUNC; 54 vp = ndp->ni_vp; 55 } else { 56 VOP_ABORTOP(ndp); 57 if (ndp->ni_dvp == ndp->ni_vp) 58 vrele(ndp->ni_dvp); 59 else 60 vput(ndp->ni_dvp); 61 ndp->ni_dvp = NULL; 62 vp = ndp->ni_vp; 63 if (fmode & O_EXCL) { 64 error = EEXIST; 65 goto bad; 66 } 67 fmode &= ~O_CREAT; 68 } 69 } else { 70 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 71 if (error = namei(ndp, p)) 72 return (error); 73 vp = ndp->ni_vp; 74 } 75 if (vp->v_type == VSOCK) { 76 error = EOPNOTSUPP; 77 goto bad; 78 } 79 if ((fmode & O_CREAT) == 0) { 80 if (fmode & FREAD) { 81 if (error = VOP_ACCESS(vp, VREAD, cred, p)) 82 goto bad; 83 } 84 if (fmode & (FWRITE | O_TRUNC)) { 85 if (vp->v_type == VDIR) { 86 error = EISDIR; 87 goto bad; 88 } 89 if ((error = vn_writechk(vp)) || 90 (error = VOP_ACCESS(vp, VWRITE, cred, p))) 91 goto bad; 92 } 93 } 94 if (fmode & O_TRUNC) { 95 VATTR_NULL(vap); 96 vap->va_size = 0; 97 if (error = VOP_SETATTR(vp, vap, cred, p)) 98 goto bad; 99 } 100 if (error = VOP_OPEN(vp, fmode, cred, p)) 101 goto bad; 102 if (fmode & FWRITE) 103 vp->v_writecount++; 104 return (0); 105 bad: 106 vput(vp); 107 return (error); 108 } 109 110 /* 111 * Check for write permissions on the specified vnode. 112 * The read-only status of the file system is checked. 113 * Also, prototype text segments cannot be written. 114 */ 115 vn_writechk(vp) 116 register struct vnode *vp; 117 { 118 119 /* 120 * Disallow write attempts on read-only file systems; 121 * unless the file is a socket or a block or character 122 * device resident on the file system. 123 */ 124 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 125 switch (vp->v_type) { 126 case VREG: case VDIR: case VLNK: 127 return (EROFS); 128 } 129 } 130 /* 131 * If there's shared text associated with 132 * the vnode, try to free it up once. If 133 * we fail, we can't allow writing. 134 */ 135 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp)) 136 return (ETXTBSY); 137 return (0); 138 } 139 140 /* 141 * Vnode version of rdwri() for calls on file systems. 142 */ 143 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 144 enum uio_rw rw; 145 struct vnode *vp; 146 caddr_t base; 147 int len; 148 off_t offset; 149 enum uio_seg segflg; 150 int ioflg; 151 struct ucred *cred; 152 int *aresid; 153 struct proc *p; 154 { 155 struct uio auio; 156 struct iovec aiov; 157 int error; 158 159 if ((ioflg & IO_NODELOCKED) == 0) 160 VOP_LOCK(vp); 161 auio.uio_iov = &aiov; 162 auio.uio_iovcnt = 1; 163 aiov.iov_base = base; 164 aiov.iov_len = len; 165 auio.uio_resid = len; 166 auio.uio_offset = offset; 167 auio.uio_segflg = segflg; 168 auio.uio_rw = rw; 169 auio.uio_procp = p; 170 if (rw == UIO_READ) 171 error = VOP_READ(vp, &auio, ioflg, cred); 172 else 173 error = VOP_WRITE(vp, &auio, ioflg, cred); 174 if (aresid) 175 *aresid = auio.uio_resid; 176 else 177 if (auio.uio_resid && error == 0) 178 error = EIO; 179 if ((ioflg & IO_NODELOCKED) == 0) 180 VOP_UNLOCK(vp); 181 return (error); 182 } 183 184 vn_read(fp, uio, cred) 185 struct file *fp; 186 struct uio *uio; 187 struct ucred *cred; 188 { 189 register struct vnode *vp = (struct vnode *)fp->f_data; 190 int count, error; 191 192 VOP_LOCK(vp); 193 uio->uio_offset = fp->f_offset; 194 count = uio->uio_resid; 195 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, 196 cred); 197 fp->f_offset += count - uio->uio_resid; 198 VOP_UNLOCK(vp); 199 return (error); 200 } 201 202 vn_write(fp, uio, cred) 203 struct file *fp; 204 struct uio *uio; 205 struct ucred *cred; 206 { 207 register struct vnode *vp = (struct vnode *)fp->f_data; 208 int count, error, ioflag = 0; 209 210 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 211 ioflag |= IO_APPEND; 212 if (fp->f_flag & FNONBLOCK) 213 ioflag |= IO_NDELAY; 214 VOP_LOCK(vp); 215 uio->uio_offset = fp->f_offset; 216 count = uio->uio_resid; 217 error = VOP_WRITE(vp, uio, ioflag, cred); 218 if (ioflag & IO_APPEND) 219 fp->f_offset = uio->uio_offset; 220 else 221 fp->f_offset += count - uio->uio_resid; 222 VOP_UNLOCK(vp); 223 return (error); 224 } 225 226 /* 227 * Get stat info for a vnode. 228 */ 229 vn_stat(vp, sb, p) 230 struct vnode *vp; 231 register struct stat *sb; 232 struct proc *p; 233 { 234 struct vattr vattr; 235 register struct vattr *vap; 236 int error; 237 u_short mode; 238 239 vap = &vattr; 240 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 241 if (error) 242 return (error); 243 /* 244 * Copy from vattr table 245 */ 246 sb->st_dev = vap->va_fsid; 247 sb->st_ino = vap->va_fileid; 248 mode = vap->va_mode; 249 switch (vp->v_type) { 250 case VREG: 251 mode |= S_IFREG; 252 break; 253 case VDIR: 254 mode |= S_IFDIR; 255 break; 256 case VBLK: 257 mode |= S_IFBLK; 258 break; 259 case VCHR: 260 mode |= S_IFCHR; 261 break; 262 case VLNK: 263 mode |= S_IFLNK; 264 break; 265 case VSOCK: 266 mode |= S_IFSOCK; 267 break; 268 case VFIFO: 269 mode |= S_IFIFO; 270 break; 271 default: 272 return (EBADF); 273 }; 274 sb->st_mode = mode; 275 sb->st_nlink = vap->va_nlink; 276 sb->st_uid = vap->va_uid; 277 sb->st_gid = vap->va_gid; 278 sb->st_rdev = vap->va_rdev; 279 sb->st_size = vap->va_size; 280 sb->st_atime = vap->va_atime.tv_sec; 281 sb->st_spare1 = 0; 282 sb->st_mtime = vap->va_mtime.tv_sec; 283 sb->st_spare2 = 0; 284 sb->st_ctime = vap->va_ctime.tv_sec; 285 sb->st_spare3 = 0; 286 sb->st_blksize = vap->va_blocksize; 287 sb->st_flags = vap->va_flags; 288 sb->st_gen = vap->va_gen; 289 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 290 return (0); 291 } 292 293 /* 294 * Vnode ioctl call 295 */ 296 vn_ioctl(fp, com, data, p) 297 struct file *fp; 298 int com; 299 caddr_t data; 300 struct proc *p; 301 { 302 register struct vnode *vp = ((struct vnode *)fp->f_data); 303 struct vattr vattr; 304 int error; 305 306 switch (vp->v_type) { 307 308 case VREG: 309 case VDIR: 310 if (com == FIONREAD) { 311 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) 312 return (error); 313 *(off_t *)data = vattr.va_size - fp->f_offset; 314 return (0); 315 } 316 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 317 return (0); /* XXX */ 318 /* fall into ... */ 319 320 default: 321 return (ENOTTY); 322 323 case VFIFO: 324 case VCHR: 325 case VBLK: 326 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 327 if (error == 0 && com == TIOCSCTTY) { 328 p->p_session->s_ttyvp = vp; 329 VREF(vp); 330 } 331 return (error); 332 } 333 } 334 335 /* 336 * Vnode select call 337 */ 338 vn_select(fp, which, p) 339 struct file *fp; 340 int which; 341 struct proc *p; 342 { 343 344 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag, 345 p->p_ucred, p)); 346 } 347 348 /* 349 * Vnode close call 350 */ 351 vn_close(vp, flags, cred, p) 352 register struct vnode *vp; 353 int flags; 354 struct ucred *cred; 355 struct proc *p; 356 { 357 int error; 358 359 if (flags & FWRITE) 360 vp->v_writecount--; 361 error = VOP_CLOSE(vp, flags, cred, p); 362 vrele(vp); 363 return (error); 364 } 365 366 /* 367 * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked) 368 * - look up fsid in mount list (if not found ret error) 369 * - get vp by calling VFS_FHTOVP() macro 370 * - if lockflag lock it with VOP_LOCK() 371 */ 372 vn_fhtovp(fhp, lockflag, vpp) 373 fhandle_t *fhp; 374 int lockflag; 375 struct vnode **vpp; 376 { 377 register struct mount *mp; 378 379 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 380 return (ESTALE); 381 if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) 382 return (ESTALE); 383 if (!lockflag) 384 VOP_UNLOCK(*vpp); 385 return (0); 386 } 387