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.30 (Berkeley) 05/15/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 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 & FEXCL) { 64 error = EEXIST; 65 goto bad; 66 } 67 fmode &= ~FCREAT; 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 & FCREAT) == 0) { 80 if (fmode & FREAD) { 81 if (error = VOP_ACCESS(vp, VREAD, cred, p)) 82 goto bad; 83 } 84 if (fmode & (FWRITE|FTRUNC)) { 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 & FTRUNC) { 95 VATTR_NULL(vap); 96 vap->va_size = 0; 97 if (error = VOP_SETATTR(vp, vap, cred, p)) 98 goto bad; 99 } 100 VOP_UNLOCK(vp); 101 error = VOP_OPEN(vp, fmode, cred, p); 102 if (error) 103 vrele(vp); 104 return (error); 105 106 bad: 107 vput(vp); 108 return (error); 109 } 110 111 /* 112 * Check for write permissions on the specified vnode. 113 * The read-only status of the file system is checked. 114 * Also, prototype text segments cannot be written. 115 */ 116 vn_writechk(vp) 117 register struct vnode *vp; 118 { 119 120 /* 121 * Disallow write attempts on read-only file systems; 122 * unless the file is a socket or a block or character 123 * device resident on the file system. 124 */ 125 if (vp->v_mount->mnt_flag & MNT_RDONLY) { 126 switch (vp->v_type) { 127 case VREG: case VDIR: case VLNK: 128 return (EROFS); 129 } 130 } 131 /* 132 * If there's shared text associated with 133 * the vnode, try to free it up once. If 134 * we fail, we can't allow writing. 135 */ 136 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp)) 137 return (ETXTBSY); 138 return (0); 139 } 140 141 /* 142 * Vnode version of rdwri() for calls on file systems. 143 */ 144 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) 145 enum uio_rw rw; 146 struct vnode *vp; 147 caddr_t base; 148 int len; 149 off_t offset; 150 enum uio_seg segflg; 151 int ioflg; 152 struct ucred *cred; 153 int *aresid; 154 struct proc *p; 155 { 156 struct uio auio; 157 struct iovec aiov; 158 int error; 159 160 if ((ioflg & IO_NODELOCKED) == 0) 161 VOP_LOCK(vp); 162 auio.uio_iov = &aiov; 163 auio.uio_iovcnt = 1; 164 aiov.iov_base = base; 165 aiov.iov_len = len; 166 auio.uio_resid = len; 167 auio.uio_offset = offset; 168 auio.uio_segflg = segflg; 169 auio.uio_rw = rw; 170 auio.uio_procp = p; 171 if (rw == UIO_READ) 172 error = VOP_READ(vp, &auio, ioflg, cred); 173 else 174 error = VOP_WRITE(vp, &auio, ioflg, cred); 175 if (aresid) 176 *aresid = auio.uio_resid; 177 else 178 if (auio.uio_resid && error == 0) 179 error = EIO; 180 if ((ioflg & IO_NODELOCKED) == 0) 181 VOP_UNLOCK(vp); 182 return (error); 183 } 184 185 vn_read(fp, uio, cred) 186 struct file *fp; 187 struct uio *uio; 188 struct ucred *cred; 189 { 190 register struct vnode *vp = (struct vnode *)fp->f_data; 191 int count, error; 192 193 VOP_LOCK(vp); 194 uio->uio_offset = fp->f_offset; 195 count = uio->uio_resid; 196 error = VOP_READ(vp, uio, (fp->f_flag & FNDELAY) ? IO_NDELAY : 0, 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 & FAPPEND)) 211 ioflag |= IO_APPEND; 212 if (fp->f_flag & FNDELAY) 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(fp, p) 352 register struct file *fp; 353 struct proc *p; 354 { 355 struct vnode *vp = ((struct vnode *)fp->f_data); 356 int error; 357 358 /* 359 * Must delete vnode reference from this file entry 360 * before VOP_CLOSE, so that only other references 361 * will prevent close. 362 */ 363 fp->f_data = (caddr_t) 0; 364 error = VOP_CLOSE(vp, fp->f_flag, fp->f_cred, p); 365 vrele(vp); 366 return (error); 367 } 368 369 /* 370 * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked) 371 * - look up fsid in mount list (if not found ret error) 372 * - get vp by calling VFS_FHTOVP() macro 373 * - if lockflag lock it with VOP_LOCK() 374 */ 375 vn_fhtovp(fhp, lockflag, vpp) 376 fhandle_t *fhp; 377 int lockflag; 378 struct vnode **vpp; 379 { 380 register struct mount *mp; 381 382 if ((mp = getvfs(&fhp->fh_fsid)) == NULL) 383 return (ESTALE); 384 if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) 385 return (ESTALE); 386 if (!lockflag) 387 VOP_UNLOCK(*vpp); 388 return (0); 389 } 390