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