Commit | Line | Data |
---|---|---|
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 | * |
97f761d2 | 7 | * @(#)vfs_vnops.c 7.33 (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 | 23 | struct fileops vnops = |
97f761d2 | 24 | { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile }; |
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 | 30 | vn_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 | } | |
cb3e538f KM |
100 | if (error = VOP_OPEN(vp, fmode, cred, p)) |
101 | goto bad; | |
102 | if (fmode & FWRITE) | |
103 | vp->v_writecount++; | |
104 | return (0); | |
eb668bc6 KM |
105 | bad: |
106 | vput(vp); | |
ccee3c59 | 107 | return (error); |
eb668bc6 KM |
108 | } |
109 | ||
110 | /* | |
cd811e9a KM |
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. | |
eb668bc6 | 114 | */ |
cd811e9a | 115 | vn_writechk(vp) |
eb668bc6 | 116 | register struct vnode *vp; |
fc96c615 | 117 | { |
fc96c615 | 118 | |
cd811e9a KM |
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 | */ | |
5216fbe4 KM |
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 | } | |
cd811e9a KM |
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 | */ | |
9db58063 | 135 | if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp)) |
cd811e9a KM |
136 | return (ETXTBSY); |
137 | return (0); | |
eb668bc6 KM |
138 | } |
139 | ||
140 | /* | |
97f761d2 KM |
141 | * Vnode close call |
142 | */ | |
143 | vn_close(vp, flags, cred, p) | |
144 | register struct vnode *vp; | |
145 | int flags; | |
146 | struct ucred *cred; | |
147 | struct proc *p; | |
148 | { | |
149 | int error; | |
150 | ||
151 | if (flags & FWRITE) | |
152 | vp->v_writecount--; | |
153 | error = VOP_CLOSE(vp, flags, cred, p); | |
154 | vrele(vp); | |
155 | return (error); | |
156 | } | |
157 | ||
158 | /* | |
159 | * Package up an I/O request on a vnode into a uio and do it. | |
eb668bc6 | 160 | */ |
a9194d51 | 161 | vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p) |
eb668bc6 KM |
162 | enum uio_rw rw; |
163 | struct vnode *vp; | |
164 | caddr_t base; | |
165 | int len; | |
166 | off_t offset; | |
167 | enum uio_seg segflg; | |
168 | int ioflg; | |
169 | struct ucred *cred; | |
170 | int *aresid; | |
a9194d51 | 171 | struct proc *p; |
eb668bc6 KM |
172 | { |
173 | struct uio auio; | |
174 | struct iovec aiov; | |
175 | int error; | |
176 | ||
cde34f67 KM |
177 | if ((ioflg & IO_NODELOCKED) == 0) |
178 | VOP_LOCK(vp); | |
eb668bc6 KM |
179 | auio.uio_iov = &aiov; |
180 | auio.uio_iovcnt = 1; | |
181 | aiov.iov_base = base; | |
182 | aiov.iov_len = len; | |
183 | auio.uio_resid = len; | |
184 | auio.uio_offset = offset; | |
185 | auio.uio_segflg = segflg; | |
186 | auio.uio_rw = rw; | |
a9194d51 | 187 | auio.uio_procp = p; |
eb668bc6 | 188 | if (rw == UIO_READ) |
cde34f67 | 189 | error = VOP_READ(vp, &auio, ioflg, cred); |
eb668bc6 | 190 | else |
cde34f67 | 191 | error = VOP_WRITE(vp, &auio, ioflg, cred); |
eb668bc6 KM |
192 | if (aresid) |
193 | *aresid = auio.uio_resid; | |
194 | else | |
195 | if (auio.uio_resid && error == 0) | |
196 | error = EIO; | |
cde34f67 KM |
197 | if ((ioflg & IO_NODELOCKED) == 0) |
198 | VOP_UNLOCK(vp); | |
eb668bc6 KM |
199 | return (error); |
200 | } | |
201 | ||
97f761d2 KM |
202 | /* |
203 | * File table vnode read routine. | |
204 | */ | |
eb668bc6 KM |
205 | vn_read(fp, uio, cred) |
206 | struct file *fp; | |
207 | struct uio *uio; | |
208 | struct ucred *cred; | |
209 | { | |
cde34f67 KM |
210 | register struct vnode *vp = (struct vnode *)fp->f_data; |
211 | int count, error; | |
eb668bc6 | 212 | |
cde34f67 KM |
213 | VOP_LOCK(vp); |
214 | uio->uio_offset = fp->f_offset; | |
215 | count = uio->uio_resid; | |
63992c37 KM |
216 | error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0, |
217 | cred); | |
cde34f67 KM |
218 | fp->f_offset += count - uio->uio_resid; |
219 | VOP_UNLOCK(vp); | |
220 | return (error); | |
eb668bc6 KM |
221 | } |
222 | ||
97f761d2 KM |
223 | /* |
224 | * File table vnode write routine. | |
225 | */ | |
eb668bc6 KM |
226 | vn_write(fp, uio, cred) |
227 | struct file *fp; | |
228 | struct uio *uio; | |
229 | struct ucred *cred; | |
230 | { | |
231 | register struct vnode *vp = (struct vnode *)fp->f_data; | |
cde34f67 | 232 | int count, error, ioflag = 0; |
eb668bc6 | 233 | |
63992c37 | 234 | if (vp->v_type == VREG && (fp->f_flag & O_APPEND)) |
eb668bc6 | 235 | ioflag |= IO_APPEND; |
63992c37 | 236 | if (fp->f_flag & FNONBLOCK) |
eb668bc6 | 237 | ioflag |= IO_NDELAY; |
cde34f67 KM |
238 | VOP_LOCK(vp); |
239 | uio->uio_offset = fp->f_offset; | |
240 | count = uio->uio_resid; | |
241 | error = VOP_WRITE(vp, uio, ioflag, cred); | |
242 | if (ioflag & IO_APPEND) | |
243 | fp->f_offset = uio->uio_offset; | |
244 | else | |
245 | fp->f_offset += count - uio->uio_resid; | |
246 | VOP_UNLOCK(vp); | |
247 | return (error); | |
eb668bc6 KM |
248 | } |
249 | ||
250 | /* | |
97f761d2 | 251 | * File table vnode stat routine. |
eb668bc6 | 252 | */ |
a9194d51 | 253 | vn_stat(vp, sb, p) |
eb668bc6 KM |
254 | struct vnode *vp; |
255 | register struct stat *sb; | |
a9194d51 | 256 | struct proc *p; |
eb668bc6 KM |
257 | { |
258 | struct vattr vattr; | |
259 | register struct vattr *vap; | |
260 | int error; | |
261 | u_short mode; | |
262 | ||
263 | vap = &vattr; | |
a9194d51 | 264 | error = VOP_GETATTR(vp, vap, p->p_ucred, p); |
eb668bc6 KM |
265 | if (error) |
266 | return (error); | |
267 | /* | |
268 | * Copy from vattr table | |
269 | */ | |
270 | sb->st_dev = vap->va_fsid; | |
271 | sb->st_ino = vap->va_fileid; | |
272 | mode = vap->va_mode; | |
273 | switch (vp->v_type) { | |
274 | case VREG: | |
2e3baa4b | 275 | mode |= S_IFREG; |
eb668bc6 KM |
276 | break; |
277 | case VDIR: | |
2e3baa4b | 278 | mode |= S_IFDIR; |
eb668bc6 KM |
279 | break; |
280 | case VBLK: | |
2e3baa4b | 281 | mode |= S_IFBLK; |
eb668bc6 KM |
282 | break; |
283 | case VCHR: | |
2e3baa4b | 284 | mode |= S_IFCHR; |
eb668bc6 KM |
285 | break; |
286 | case VLNK: | |
2e3baa4b | 287 | mode |= S_IFLNK; |
eb668bc6 KM |
288 | break; |
289 | case VSOCK: | |
2e3baa4b | 290 | mode |= S_IFSOCK; |
eb668bc6 | 291 | break; |
0099f400 KM |
292 | case VFIFO: |
293 | mode |= S_IFIFO; | |
294 | break; | |
eb668bc6 KM |
295 | default: |
296 | return (EBADF); | |
297 | }; | |
298 | sb->st_mode = mode; | |
299 | sb->st_nlink = vap->va_nlink; | |
300 | sb->st_uid = vap->va_uid; | |
301 | sb->st_gid = vap->va_gid; | |
302 | sb->st_rdev = vap->va_rdev; | |
303 | sb->st_size = vap->va_size; | |
304 | sb->st_atime = vap->va_atime.tv_sec; | |
305 | sb->st_spare1 = 0; | |
306 | sb->st_mtime = vap->va_mtime.tv_sec; | |
307 | sb->st_spare2 = 0; | |
308 | sb->st_ctime = vap->va_ctime.tv_sec; | |
309 | sb->st_spare3 = 0; | |
310 | sb->st_blksize = vap->va_blocksize; | |
2ff0b8da KM |
311 | sb->st_flags = vap->va_flags; |
312 | sb->st_gen = vap->va_gen; | |
366f2ef0 | 313 | sb->st_blocks = vap->va_bytes / S_BLKSIZE; |
eb668bc6 KM |
314 | return (0); |
315 | } | |
316 | ||
317 | /* | |
97f761d2 | 318 | * File table vnode ioctl routine. |
eb668bc6 | 319 | */ |
a9194d51 | 320 | vn_ioctl(fp, com, data, p) |
eb668bc6 KM |
321 | struct file *fp; |
322 | int com; | |
323 | caddr_t data; | |
a9194d51 | 324 | struct proc *p; |
eb668bc6 KM |
325 | { |
326 | register struct vnode *vp = ((struct vnode *)fp->f_data); | |
327 | struct vattr vattr; | |
328 | int error; | |
329 | ||
330 | switch (vp->v_type) { | |
331 | ||
332 | case VREG: | |
333 | case VDIR: | |
334 | if (com == FIONREAD) { | |
a9194d51 | 335 | if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) |
eb668bc6 KM |
336 | return (error); |
337 | *(off_t *)data = vattr.va_size - fp->f_offset; | |
338 | return (0); | |
339 | } | |
340 | if (com == FIONBIO || com == FIOASYNC) /* XXX */ | |
341 | return (0); /* XXX */ | |
342 | /* fall into ... */ | |
343 | ||
344 | default: | |
345 | return (ENOTTY); | |
346 | ||
0099f400 | 347 | case VFIFO: |
eb668bc6 KM |
348 | case VCHR: |
349 | case VBLK: | |
a9194d51 | 350 | error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p); |
1665da5a | 351 | if (error == 0 && com == TIOCSCTTY) { |
8429d022 | 352 | p->p_session->s_ttyvp = vp; |
1665da5a MT |
353 | VREF(vp); |
354 | } | |
355 | return (error); | |
fc96c615 | 356 | } |
eb668bc6 KM |
357 | } |
358 | ||
359 | /* | |
97f761d2 | 360 | * File table vnode select routine. |
eb668bc6 | 361 | */ |
a9194d51 | 362 | vn_select(fp, which, p) |
eb668bc6 KM |
363 | struct file *fp; |
364 | int which; | |
a9194d51 | 365 | struct proc *p; |
eb668bc6 | 366 | { |
8429d022 | 367 | |
ccee3c59 | 368 | return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag, |
97f761d2 | 369 | fp->f_cred, p)); |
eb668bc6 KM |
370 | } |
371 | ||
372 | /* | |
97f761d2 | 373 | * File table vnode close routine. |
eb668bc6 | 374 | */ |
97f761d2 KM |
375 | vn_closefile(fp, p) |
376 | struct file *fp; | |
a9194d51 | 377 | struct proc *p; |
eb668bc6 | 378 | { |
eb668bc6 | 379 | |
97f761d2 KM |
380 | return (vn_close(((struct vnode *)fp->f_data), fp->f_flag, |
381 | fp->f_cred, p)); | |
eb668bc6 KM |
382 | } |
383 | ||
eb668bc6 KM |
384 | /* |
385 | * vn_fhtovp() - convert a fh to a vnode ptr (optionally locked) | |
386 | * - look up fsid in mount list (if not found ret error) | |
387 | * - get vp by calling VFS_FHTOVP() macro | |
388 | * - if lockflag lock it with VOP_LOCK() | |
389 | */ | |
390 | vn_fhtovp(fhp, lockflag, vpp) | |
391 | fhandle_t *fhp; | |
392 | int lockflag; | |
393 | struct vnode **vpp; | |
394 | { | |
395 | register struct mount *mp; | |
eb668bc6 KM |
396 | |
397 | if ((mp = getvfs(&fhp->fh_fsid)) == NULL) | |
398 | return (ESTALE); | |
a2f658f9 KM |
399 | if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp)) |
400 | return (ESTALE); | |
401 | if (!lockflag) | |
402 | VOP_UNLOCK(*vpp); | |
eb668bc6 KM |
403 | return (0); |
404 | } |