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.29 (Berkeley) 04/16/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 & FCREAT) { 42 ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF; 43 if ((fmode & FEXCL) == 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 &= ~FTRUNC; 54 vp = ndp->ni_vp; 55 } else { 56 if (ndp->ni_dvp == ndp->ni_vp) 57 vrele(ndp->ni_dvp); 58 else 59 vput(ndp->ni_dvp); 60 ndp->ni_dvp = NULL; 61 vp = ndp->ni_vp; 62 if (fmode & FEXCL) { 63 error = EEXIST; 64 goto bad; 65 } 66 fmode &= ~FCREAT; 67 } 68 } else { 69 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF; 70 if (error = namei(ndp, p)) 71 return (error); 72 vp = ndp->ni_vp; 73 } 74 if (vp->v_type == VSOCK) { 75 error = EOPNOTSUPP; 76 goto bad; 77 } 78 if ((fmode & FCREAT) == 0) { 79 if (fmode & FREAD) { 80 if (error = VOP_ACCESS(vp, VREAD, cred, p)) 81 goto bad; 82 } 83 if (fmode & (FWRITE|FTRUNC)) { 84 if (vp->v_type == VDIR) { 85 error = EISDIR; 86 goto bad; 87 } 88 if ((error = vn_writechk(vp)) || 89 (error = VOP_ACCESS(vp, VWRITE, cred, p))) 90 goto bad; 91 } 92 } 93 if (fmode & FTRUNC) { 94 VATTR_NULL(vap); 95 vap->va_size = 0; 96 if (error = VOP_SETATTR(vp, vap, cred, p)) 97 goto bad; 98 } 99 VOP_UNLOCK(vp); 100 error = VOP_OPEN(vp, fmode, cred, p); 101 if (error) 102 vrele(vp); 103 return (error); 104 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 & FNDELAY) ? IO_NDELAY : 0, cred); 196 fp->f_offset += count - uio->uio_resid; 197 VOP_UNLOCK(vp); 198 return (error); 199 } 200 201 vn_write(fp, uio, cred) 202 struct file *fp; 203 struct uio *uio; 204 struct ucred *cred; 205 { 206 register struct vnode *vp = (struct vnode *)fp->f_data; 207 int count, error, ioflag = 0; 208 209 if (vp->v_type == VREG && (fp->f_flag & FAPPEND)) 210 ioflag |= IO_APPEND; 211 if (fp->f_flag & FNDELAY) 212 ioflag |= IO_NDELAY; 213 VOP_LOCK(vp); 214 uio->uio_offset = fp->f_offset; 215 count = uio->uio_resid; 216 error = VOP_WRITE(vp, uio, ioflag, cred); 217 if (ioflag & IO_APPEND) 218 fp->f_offset = uio->uio_offset; 219 else 220 fp->f_offset += count - uio->uio_resid; 221 VOP_UNLOCK(vp); 222 return (error); 223 } 224 225 /* 226 * Get stat info for a vnode. 227 */ 228 vn_stat(vp, sb, p) 229 struct vnode *vp; 230 register struct stat *sb; 231 struct proc *p; 232 { 233 struct vattr vattr; 234 register struct vattr *vap; 235 int error; 236 u_short mode; 237 238 vap = &vattr; 239 error = VOP_GETATTR(vp, vap, p->p_ucred, p); 240 if (error) 241 return (error); 242 /* 243 * Copy from vattr table 244 */ 245 sb->st_dev = vap->va_fsid; 246 sb->st_ino = vap->va_fileid; 247 mode = vap->va_mode; 248 switch (vp->v_type) { 249 case VREG: 250 mode |= S_IFREG; 251 break; 252 case VDIR: 253 mode |= S_IFDIR; 254 break; 255 case VBLK: 256 mode |= S_IFBLK; 257 break; 258 case VCHR: 259 mode |= S_IFCHR; 260 break; 261 case VLNK: 262 mode |= S_IFLNK; 263 break; 264 case VSOCK: 265 mode |= S_IFSOCK; 266 break; 267 case VFIFO: 268 mode |= S_IFIFO; 269 break; 270 default: 271 return (EBADF); 272 }; 273 sb->st_mode = mode; 274 sb->st_nlink = vap->va_nlink; 275 sb->st_uid = vap->va_uid; 276 sb->st_gid = vap->va_gid; 277 sb->st_rdev = vap->va_rdev; 278 sb->st_size = vap->va_size; 279 sb->st_atime = vap->va_atime.tv_sec; 280 sb->st_spare1 = 0; 281 sb->st_mtime = vap->va_mtime.tv_sec; 282 sb->st_spare2 = 0; 283 sb->st_ctime = vap->va_ctime.tv_sec; 284 sb->st_spare3 = 0; 285 sb->st_blksize = vap->va_blocksize; 286 sb->st_flags = vap->va_flags; 287 sb->st_gen = vap->va_gen; 288 sb->st_blocks = vap->va_bytes / S_BLKSIZE; 289 return (0); 290 } 291 292 /* 293 * Vnode ioctl call 294 */ 295 vn_ioctl(fp, com, data, p) 296 struct file *fp; 297 int com; 298 caddr_t data; 299 struct proc *p; 300 { 301 register struct vnode *vp = ((struct vnode *)fp->f_data); 302 struct vattr vattr; 303 int error; 304 305 switch (vp->v_type) { 306 307 case VREG: 308 case VDIR: 309 if (com == FIONREAD) { 310 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) 311 return (error); 312 *(off_t *)data = vattr.va_size - fp->f_offset; 313 return (0); 314 } 315 if (com == FIONBIO || com == FIOASYNC) /* XXX */ 316 return (0); /* XXX */ 317 /* fall into ... */ 318 319 default: 320 return (ENOTTY); 321 322 case VFIFO: 323 case VCHR: 324 case VBLK: 325 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); 326 if (error == 0 && com == TIOCSCTTY) { 327 p->p_session->s_ttyvp = vp; 328 VREF(vp); 329 } 330 return (error); 331 } 332 } 333 334 /* 335 * Vnode select call 336 */ 337 vn_select(fp, which, p) 338 struct file *fp; 339 int which; 340 struct proc *p; 341 { 342 343 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag, 344 p->p_ucred, p)); 345 } 346 347 /* 348 * Vnode close call 349 */ 350 vn_close(fp, p) 351 register struct file *fp; 352 struct proc *p; 353 { 354 struct vnode *vp = ((struct vnode *)fp->f_data); 355 int error; 356 357 /* 358 * Must delete vnode reference from this file entry 359 * before VOP_CLOSE, so that only other references 360 * will prevent close. 361 */ 362 fp->f_data = (caddr_t) 0; 363 error = VOP_CLOSE(vp, fp->f_flag, fp->f_cred, p); 364 vrele(vp); 365 return (error); 366 } 367 368 /* 369 * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked) 370 * - look up fsid in mount list (if not found ret error) 371 * - get vp by calling VFS_FHTOVP() macro 372 * - if lockflag lock it with VOP_LOCK() 373 */ 374 vn_fhtovp(fhp, lockflag, vpp) 375 fhandle_t *fhp; 376 int lockflag; 377 struct vnode **vpp; 378 { 379 register struct mount *mp; 380 381 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 382 return (ESTALE); 383 if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) 384 return (ESTALE); 385 if (!lockflag) 386 VOP_UNLOCK(*vpp); 387 return (0); 388 } 389