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.31 (Berkeley) 05/30/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)) == 0) 101 return (0); 102 bad: 103 vput(vp); 104 return (error); 105 } 106 107 /* 108 * Check for write permissions on the specified vnode. 109 * The read-only status of the file system is checked. 110 * Also, prototype text segments cannot be written. 111 */ 112 vn_writechk(vp) 113 register struct vnode *vp; 114 { 115 116 /* 117 * Disallow write attempts on read-only file systems; 118 * unless the file is a socket or a block or character 119 * device resident on the file system. 120 */ 121 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 122 switch (vp->v_type) { 123 case VREG: case VDIR: case VLNK: 124 return (EROFS); 125 } 126 } 127 /* 128 * If there's shared text associated with 129 * the vnode, try to free it up once. If 130 * we fail, we can't allow writing. 131 */ 132 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp)) 133 return (ETXTBSY); 134 return (0); 135 } 136 137 /* 138 * Vnode version of rdwri() for calls on file systems. 139 */ 140 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 141 enum uio_rw rw; 142 struct vnode *vp; 143 caddr_t base; 144 int len; 145 off_t offset; 146 enum uio_seg segflg; 147 int ioflg; 148 struct ucred *cred; 149 int *aresid; 150 struct proc *p; 151 { 152 struct uio auio; 153 struct iovec aiov; 154 int error; 155 156 if ((ioflg & IO_NODELOCKED) == 0) 157 VOP_LOCK(vp); 158 auio.uio_iov = &aiov; 159 auio.uio_iovcnt = 1; 160 aiov.iov_base = base; 161 aiov.iov_len = len; 162 auio.uio_resid = len; 163 auio.uio_offset = offset; 164 auio.uio_segflg = segflg; 165 auio.uio_rw = rw; 166 auio.uio_procp = p; 167 if (rw == UIO_READ) 168 error = VOP_READ(vp, &auio, ioflg, cred); 169 else 170 error = VOP_WRITE(vp, &auio, ioflg, cred); 171 if (aresid) 172 *aresid = auio.uio_resid; 173 else 174 if (auio.uio_resid && error == 0) 175 error = EIO; 176 if ((ioflg & IO_NODELOCKED) == 0) 177 VOP_UNLOCK(vp); 178 return (error); 179 } 180 181 vn_read(fp, uio, cred) 182 struct file *fp; 183 struct uio *uio; 184 struct ucred *cred; 185 { 186 register struct vnode *vp = (struct vnode *)fp->f_data; 187 int count, error; 188 189 VOP_LOCK(vp); 190 uio->uio_offset = fp->f_offset; 191 count = uio->uio_resid; 192 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, 193 cred); 194 fp->f_offset += count - uio->uio_resid; 195 VOP_UNLOCK(vp); 196 return (error); 197 } 198 199 vn_write(fp, uio, cred) 200 struct file *fp; 201 struct uio *uio; 202 struct ucred *cred; 203 { 204 register struct vnode *vp = (struct vnode *)fp->f_data; 205 int count, error, ioflag = 0; 206 207 if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) 208 ioflag |= IO_APPEND; 209 if (fp->f_flag & FNONBLOCK) 210 ioflag |= IO_NDELAY; 211 VOP_LOCK(vp); 212 uio->uio_offset = fp->f_offset; 213 count = uio->uio_resid; 214 error = VOP_WRITE(vp, uio, ioflag, cred); 215 if (ioflag & IO_APPEND) 216 fp->f_offset = uio->uio_offset; 217 else 218 fp->f_offset += count - uio->uio_resid; 219 VOP_UNLOCK(vp); 220 return (error); 221 } 222 223 /* 224 * Get stat info for a vnode. 225 */ 226 vn_stat(vp, sb, p) 227 struct vnode *vp; 228 register struct stat *sb; 229 struct proc *p; 230 { 231 struct vattr vattr; 232 register struct vattr *vap; 233 int error; 234 u_short mode; 235 236 vap = &vattr; 237 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 238 if (error) 239 return (error); 240 /* 241 * Copy from vattr table 242 */ 243 sb->st_dev = vap->va_fsid; 244 sb->st_ino = vap->va_fileid; 245 mode = vap->va_mode; 246 switch (vp->v_type) { 247 case VREG: 248 mode |= S_IFREG; 249 break; 250 case VDIR: 251 mode |= S_IFDIR; 252 break; 253 case VBLK: 254 mode |= S_IFBLK; 255 break; 256 case VCHR: 257 mode |= S_IFCHR; 258 break; 259 case VLNK: 260 mode |= S_IFLNK; 261 break; 262 case VSOCK: 263 mode |= S_IFSOCK; 264 break; 265 case VFIFO: 266 mode |= S_IFIFO; 267 break; 268 default: 269 return (EBADF); 270 }; 271 sb->st_mode = mode; 272 sb->st_nlink = vap->va_nlink; 273 sb->st_uid = vap->va_uid; 274 sb->st_gid = vap->va_gid; 275 sb->st_rdev = vap->va_rdev; 276 sb->st_size = vap->va_size; 277 sb->st_atime = vap->va_atime.tv_sec; 278 sb->st_spare1 = 0; 279 sb->st_mtime = vap->va_mtime.tv_sec; 280 sb->st_spare2 = 0; 281 sb->st_ctime = vap->va_ctime.tv_sec; 282 sb->st_spare3 = 0; 283 sb->st_blksize = vap->va_blocksize; 284 sb->st_flags = vap->va_flags; 285 sb->st_gen = vap->va_gen; 286 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 287 return (0); 288 } 289 290 /* 291 * Vnode ioctl call 292 */ 293 vn_ioctl(fp, com, data, p) 294 struct file *fp; 295 int com; 296 caddr_t data; 297 struct proc *p; 298 { 299 register struct vnode *vp = ((struct vnode *)fp->f_data); 300 struct vattr vattr; 301 int error; 302 303 switch (vp->v_type) { 304 305 case VREG: 306 case VDIR: 307 if (com == FIONREAD) { 308 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) 309 return (error); 310 *(off_t *)data = vattr.va_size - fp->f_offset; 311 return (0); 312 } 313 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 314 return (0); /* XXX */ 315 /* fall into ... */ 316 317 default: 318 return (ENOTTY); 319 320 case VFIFO: 321 case VCHR: 322 case VBLK: 323 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 324 if (error == 0 && com == TIOCSCTTY) { 325 p->p_session->s_ttyvp = vp; 326 VREF(vp); 327 } 328 return (error); 329 } 330 } 331 332 /* 333 * Vnode select call 334 */ 335 vn_select(fp, which, p) 336 struct file *fp; 337 int which; 338 struct proc *p; 339 { 340 341 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag, 342 p->p_ucred, p)); 343 } 344 345 /* 346 * Vnode close call 347 */ 348 vn_close(fp, p) 349 register struct file *fp; 350 struct proc *p; 351 { 352 struct vnode *vp = ((struct vnode *)fp->f_data); 353 int error; 354 355 /* 356 * Must delete vnode reference from this file entry 357 * before VOP_CLOSE, so that only other references 358 * will prevent close. 359 */ 360 fp->f_data = (caddr_t) 0; 361 error = VOP_CLOSE(vp, fp->f_flag, fp->f_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