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