consistency with tty.c about value for "no pgrp"
[unix-history] / usr / src / sys / kern / vfs_vnops.c
CommitLineData
da7c5cc6 1/*
eb668bc6
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
dbf0c423 5 * %sccs.include.redist.c%
eb668bc6 6 *
63992c37 7 * @(#)vfs_vnops.c 7.31 (Berkeley) %G%
da7c5cc6 8 */
961945a8 9
94368568
JB
10#include "param.h"
11#include "systm.h"
eb668bc6 12#include "kernel.h"
94368568 13#include "file.h"
eb668bc6
KM
14#include "stat.h"
15#include "buf.h"
16#include "proc.h"
eb668bc6 17#include "mount.h"
a9194d51 18#include "namei.h"
eb668bc6 19#include "vnode.h"
eb668bc6
KM
20#include "ioctl.h"
21#include "tty.h"
fc96c615 22
eb668bc6
KM
23struct fileops vnops =
24 { vn_read, vn_write, vn_ioctl, vn_select, vn_close };
d301d150 25
fc96c615 26/*
eb668bc6
KM
27 * Common code for vnode open operations.
28 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
fc96c615 29 */
8429d022 30vn_open(ndp, p, fmode, cmode)
eb668bc6 31 register struct nameidata *ndp;
8429d022 32 struct proc *p;
eb668bc6
KM
33 int fmode, cmode;
34{
35 register struct vnode *vp;
8429d022 36 register struct ucred *cred = p->p_ucred;
eb668bc6
KM
37 struct vattr vat;
38 struct vattr *vap = &vat;
39 int error;
40
63992c37 41 if (fmode & O_CREAT) {
eb668bc6 42 ndp->ni_nameiop = CREATE | LOCKPARENT | LOCKLEAF;
63992c37 43 if ((fmode & O_EXCL) == 0)
eb668bc6 44 ndp->ni_nameiop |= FOLLOW;
8429d022 45 if (error = namei(ndp, p))
eb668bc6
KM
46 return (error);
47 if (ndp->ni_vp == NULL) {
3ee1461b 48 VATTR_NULL(vap);
eb668bc6
KM
49 vap->va_type = VREG;
50 vap->va_mode = cmode;
a9194d51 51 if (error = VOP_CREATE(ndp, vap, p))
eb668bc6 52 return (error);
63992c37 53 fmode &= ~O_TRUNC;
eb668bc6
KM
54 vp = ndp->ni_vp;
55 } else {
e4a57ff3 56 VOP_ABORTOP(ndp);
3f530a7b
KM
57 if (ndp->ni_dvp == ndp->ni_vp)
58 vrele(ndp->ni_dvp);
7341981c 59 else
3f530a7b
KM
60 vput(ndp->ni_dvp);
61 ndp->ni_dvp = NULL;
eb668bc6 62 vp = ndp->ni_vp;
63992c37 63 if (fmode & O_EXCL) {
eb668bc6
KM
64 error = EEXIST;
65 goto bad;
66 }
63992c37 67 fmode &= ~O_CREAT;
eb668bc6
KM
68 }
69 } else {
70 ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
8429d022 71 if (error = namei(ndp, p))
eb668bc6
KM
72 return (error);
73 vp = ndp->ni_vp;
74 }
75 if (vp->v_type == VSOCK) {
76 error = EOPNOTSUPP;
77 goto bad;
78 }
63992c37 79 if ((fmode & O_CREAT) == 0) {
eb668bc6 80 if (fmode & FREAD) {
a9194d51 81 if (error = VOP_ACCESS(vp, VREAD, cred, p))
eb668bc6
KM
82 goto bad;
83 }
63992c37 84 if (fmode & (FWRITE | O_TRUNC)) {
eb668bc6
KM
85 if (vp->v_type == VDIR) {
86 error = EISDIR;
87 goto bad;
88 }
cd811e9a 89 if ((error = vn_writechk(vp)) ||
a9194d51 90 (error = VOP_ACCESS(vp, VWRITE, cred, p)))
cd811e9a 91 goto bad;
eb668bc6
KM
92 }
93 }
63992c37 94 if (fmode & O_TRUNC) {
3ee1461b 95 VATTR_NULL(vap);
eb668bc6 96 vap->va_size = 0;
a9194d51 97 if (error = VOP_SETATTR(vp, vap, cred, p))
eb668bc6
KM
98 goto bad;
99 }
63992c37
KM
100 if ((error = VOP_OPEN(vp, fmode, cred, p)) == 0)
101 return (0);
eb668bc6
KM
102bad:
103 vput(vp);
ccee3c59 104 return (error);
eb668bc6
KM
105}
106
107/*
cd811e9a
KM
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.
eb668bc6 111 */
cd811e9a 112vn_writechk(vp)
eb668bc6 113 register struct vnode *vp;
fc96c615 114{
fc96c615 115
cd811e9a
KM
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 */
5216fbe4
KM
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 }
cd811e9a
KM
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 */
9db58063 132 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
cd811e9a
KM
133 return (ETXTBSY);
134 return (0);
eb668bc6
KM
135}
136
137/*
138 * Vnode version of rdwri() for calls on file systems.
139 */
a9194d51 140vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
eb668bc6
KM
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;
a9194d51 150 struct proc *p;
eb668bc6
KM
151{
152 struct uio auio;
153 struct iovec aiov;
154 int error;
155
cde34f67
KM
156 if ((ioflg & IO_NODELOCKED) == 0)
157 VOP_LOCK(vp);
eb668bc6
KM
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;
a9194d51 166 auio.uio_procp = p;
eb668bc6 167 if (rw == UIO_READ)
cde34f67 168 error = VOP_READ(vp, &auio, ioflg, cred);
eb668bc6 169 else
cde34f67 170 error = VOP_WRITE(vp, &auio, ioflg, cred);
eb668bc6
KM
171 if (aresid)
172 *aresid = auio.uio_resid;
173 else
174 if (auio.uio_resid && error == 0)
175 error = EIO;
cde34f67
KM
176 if ((ioflg & IO_NODELOCKED) == 0)
177 VOP_UNLOCK(vp);
eb668bc6
KM
178 return (error);
179}
180
181vn_read(fp, uio, cred)
182 struct file *fp;
183 struct uio *uio;
184 struct ucred *cred;
185{
cde34f67
KM
186 register struct vnode *vp = (struct vnode *)fp->f_data;
187 int count, error;
eb668bc6 188
cde34f67
KM
189 VOP_LOCK(vp);
190 uio->uio_offset = fp->f_offset;
191 count = uio->uio_resid;
63992c37
KM
192 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
193 cred);
cde34f67
KM
194 fp->f_offset += count - uio->uio_resid;
195 VOP_UNLOCK(vp);
196 return (error);
eb668bc6
KM
197}
198
199vn_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;
cde34f67 205 int count, error, ioflag = 0;
eb668bc6 206
63992c37 207 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
eb668bc6 208 ioflag |= IO_APPEND;
63992c37 209 if (fp->f_flag & FNONBLOCK)
eb668bc6 210 ioflag |= IO_NDELAY;
cde34f67
KM
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);
eb668bc6
KM
221}
222
223/*
224 * Get stat info for a vnode.
225 */
a9194d51 226vn_stat(vp, sb, p)
eb668bc6
KM
227 struct vnode *vp;
228 register struct stat *sb;
a9194d51 229 struct proc *p;
eb668bc6
KM
230{
231 struct vattr vattr;
232 register struct vattr *vap;
233 int error;
234 u_short mode;
235
236 vap = &vattr;
a9194d51 237 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
eb668bc6
KM
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:
2e3baa4b 248 mode |= S_IFREG;
eb668bc6
KM
249 break;
250 case VDIR:
2e3baa4b 251 mode |= S_IFDIR;
eb668bc6
KM
252 break;
253 case VBLK:
2e3baa4b 254 mode |= S_IFBLK;
eb668bc6
KM
255 break;
256 case VCHR:
2e3baa4b 257 mode |= S_IFCHR;
eb668bc6
KM
258 break;
259 case VLNK:
2e3baa4b 260 mode |= S_IFLNK;
eb668bc6
KM
261 break;
262 case VSOCK:
2e3baa4b 263 mode |= S_IFSOCK;
eb668bc6 264 break;
0099f400
KM
265 case VFIFO:
266 mode |= S_IFIFO;
267 break;
eb668bc6
KM
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;
2ff0b8da
KM
284 sb->st_flags = vap->va_flags;
285 sb->st_gen = vap->va_gen;
366f2ef0 286 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
eb668bc6
KM
287 return (0);
288}
289
290/*
291 * Vnode ioctl call
292 */
a9194d51 293vn_ioctl(fp, com, data, p)
eb668bc6
KM
294 struct file *fp;
295 int com;
296 caddr_t data;
a9194d51 297 struct proc *p;
eb668bc6
KM
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) {
a9194d51 308 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
eb668bc6
KM
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
0099f400 320 case VFIFO:
eb668bc6
KM
321 case VCHR:
322 case VBLK:
a9194d51 323 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
1665da5a 324 if (error == 0 && com == TIOCSCTTY) {
8429d022 325 p->p_session->s_ttyvp = vp;
1665da5a
MT
326 VREF(vp);
327 }
328 return (error);
fc96c615 329 }
eb668bc6
KM
330}
331
332/*
333 * Vnode select call
334 */
a9194d51 335vn_select(fp, which, p)
eb668bc6
KM
336 struct file *fp;
337 int which;
a9194d51 338 struct proc *p;
eb668bc6 339{
8429d022 340
ccee3c59 341 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
a9194d51 342 p->p_ucred, p));
eb668bc6
KM
343}
344
345/*
346 * Vnode close call
347 */
a9194d51 348vn_close(fp, p)
eb668bc6 349 register struct file *fp;
a9194d51 350 struct proc *p;
eb668bc6
KM
351{
352 struct vnode *vp = ((struct vnode *)fp->f_data);
353 int error;
354
40123f2c 355 /*
eb668bc6
KM
356 * Must delete vnode reference from this file entry
357 * before VOP_CLOSE, so that only other references
358 * will prevent close.
40123f2c 359 */
eb668bc6 360 fp->f_data = (caddr_t) 0;
a9194d51 361 error = VOP_CLOSE(vp, fp->f_flag, fp->f_cred, p);
eb668bc6
KM
362 vrele(vp);
363 return (error);
364}
365
eb668bc6
KM
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 */
372vn_fhtovp(fhp, lockflag, vpp)
373 fhandle_t *fhp;
374 int lockflag;
375 struct vnode **vpp;
376{
377 register struct mount *mp;
eb668bc6
KM
378
379 if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
380 return (ESTALE);
a2f658f9
KM
381 if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
382 return (ESTALE);
383 if (!lockflag)
384 VOP_UNLOCK(*vpp);
eb668bc6
KM
385 return (0);
386}