Deleted Added
full compact
vfs_vnops.c (12767) vfs_vnops.c (12913)
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 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
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 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
39 * $Id: vfs_vnops.c,v 1.20 1995/12/07 12:47:07 davidg Exp $
39 * $Id: vfs_vnops.c,v 1.21 1995/12/11 04:56:13 dyson Exp $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/file.h>
46#include <sys/stat.h>
47#include <sys/buf.h>
48#include <sys/proc.h>
49#include <sys/mount.h>
50#include <sys/namei.h>
51#include <sys/vnode.h>
52#include <sys/ioctl.h>
53
54#include <vm/vm.h>
55#include <vm/vm_param.h>
56#include <vm/vm_object.h>
57#include <vm/vnode_pager.h>
58
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/file.h>
46#include <sys/stat.h>
47#include <sys/buf.h>
48#include <sys/proc.h>
49#include <sys/mount.h>
50#include <sys/namei.h>
51#include <sys/vnode.h>
52#include <sys/ioctl.h>
53
54#include <vm/vm.h>
55#include <vm/vm_param.h>
56#include <vm/vm_object.h>
57#include <vm/vnode_pager.h>
58
59static int vn_closefile __P((struct file *fp, struct proc *p));
60static int vn_ioctl __P((struct file *fp, int com, caddr_t data,
61 struct proc *p));
62static int vn_read __P((struct file *fp, struct uio *uio,
63 struct ucred *cred));
64static int vn_select __P((struct file *fp, int which, struct proc *p));
65static int vn_vmio_open __P((struct vnode *vp, struct proc *p,
66 struct ucred *cred));
67static int vn_write __P((struct file *fp, struct uio *uio,
68 struct ucred *cred));
69
59struct fileops vnops =
60 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
61
62/*
63 * Common code for vnode open operations.
64 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
65 */
66int
67vn_open(ndp, fmode, cmode)
68 register struct nameidata *ndp;
69 int fmode, cmode;
70{
71 register struct vnode *vp;
72 register struct proc *p = ndp->ni_cnd.cn_proc;
73 register struct ucred *cred = p->p_ucred;
74 struct vattr vat;
75 struct vattr *vap = &vat;
76 int error;
77
78 if (fmode & O_CREAT) {
79 ndp->ni_cnd.cn_nameiop = CREATE;
80 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
81 if ((fmode & O_EXCL) == 0)
82 ndp->ni_cnd.cn_flags |= FOLLOW;
83 error = namei(ndp);
84 if (error)
85 return (error);
86 if (ndp->ni_vp == NULL) {
87 VATTR_NULL(vap);
88 vap->va_type = VREG;
89 vap->va_mode = cmode;
90 LEASE_CHECK(ndp->ni_dvp, p, cred, LEASE_WRITE);
91 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
92 &ndp->ni_cnd, vap);
93 if (error)
94 return (error);
95 fmode &= ~O_TRUNC;
96 vp = ndp->ni_vp;
97 } else {
98 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
99 if (ndp->ni_dvp == ndp->ni_vp)
100 vrele(ndp->ni_dvp);
101 else
102 vput(ndp->ni_dvp);
103 ndp->ni_dvp = NULL;
104 vp = ndp->ni_vp;
105 if (fmode & O_EXCL) {
106 error = EEXIST;
107 goto bad;
108 }
109 fmode &= ~O_CREAT;
110 }
111 } else {
112 ndp->ni_cnd.cn_nameiop = LOOKUP;
113 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
114 error = namei(ndp);
115 if (error)
116 return (error);
117 vp = ndp->ni_vp;
118 }
119 if (vp->v_type == VSOCK) {
120 error = EOPNOTSUPP;
121 goto bad;
122 }
123 if ((fmode & O_CREAT) == 0) {
124 if (fmode & FREAD) {
125 error = VOP_ACCESS(vp, VREAD, cred, p);
126 if (error)
127 goto bad;
128 }
129 if (fmode & (FWRITE | O_TRUNC)) {
130 if (vp->v_type == VDIR) {
131 error = EISDIR;
132 goto bad;
133 }
134 error = vn_writechk(vp);
135 if (error)
136 goto bad;
137 error = VOP_ACCESS(vp, VWRITE, cred, p);
138 if (error)
139 goto bad;
140 }
141 }
142 if (fmode & O_TRUNC) {
143 VOP_UNLOCK(vp); /* XXX */
144 LEASE_CHECK(vp, p, cred, LEASE_WRITE);
145 VOP_LOCK(vp); /* XXX */
146 VATTR_NULL(vap);
147 vap->va_size = 0;
148 error = VOP_SETATTR(vp, vap, cred, p);
149 if (error)
150 goto bad;
151 }
152 error = VOP_OPEN(vp, fmode, cred, p);
153 if (error)
154 goto bad;
155 /*
156 * this is here for VMIO support
157 */
158 if (vp->v_type == VREG) {
159 if ((error = vn_vmio_open(vp, p, cred)) != 0)
160 goto bad;
161 }
162 if (fmode & FWRITE)
163 vp->v_writecount++;
164 return (0);
165bad:
166 vput(vp);
167 return (error);
168}
169
170/*
171 * Check for write permissions on the specified vnode.
172 * The read-only status of the file system is checked.
173 * Also, prototype text segments cannot be written.
174 */
175int
176vn_writechk(vp)
177 register struct vnode *vp;
178{
179
180 /*
181 * If there's shared text associated with
182 * the vnode, try to free it up once. If
183 * we fail, we can't allow writing.
184 */
185 if (vp->v_flag & VTEXT)
186 return (ETXTBSY);
187 return (0);
188}
189
190/*
191 * Vnode close call
192 */
193int
194vn_close(vp, flags, cred, p)
195 register struct vnode *vp;
196 int flags;
197 struct ucred *cred;
198 struct proc *p;
199{
200 int error;
201
202 if (flags & FWRITE)
203 vp->v_writecount--;
204 error = VOP_CLOSE(vp, flags, cred, p);
205 vn_vmio_close(vp);
206 return (error);
207}
208
209/*
210 * Package up an I/O request on a vnode into a uio and do it.
211 */
212int
213vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
214 enum uio_rw rw;
215 struct vnode *vp;
216 caddr_t base;
217 int len;
218 off_t offset;
219 enum uio_seg segflg;
220 int ioflg;
221 struct ucred *cred;
222 int *aresid;
223 struct proc *p;
224{
225 struct uio auio;
226 struct iovec aiov;
227 int error;
228
229 if ((ioflg & IO_NODELOCKED) == 0)
230 VOP_LOCK(vp);
231 auio.uio_iov = &aiov;
232 auio.uio_iovcnt = 1;
233 aiov.iov_base = base;
234 aiov.iov_len = len;
235 auio.uio_resid = len;
236 auio.uio_offset = offset;
237 auio.uio_segflg = segflg;
238 auio.uio_rw = rw;
239 auio.uio_procp = p;
240 if (rw == UIO_READ) {
241 error = VOP_READ(vp, &auio, ioflg, cred);
242 } else {
243 error = VOP_WRITE(vp, &auio, ioflg, cred);
244 }
245 if (aresid)
246 *aresid = auio.uio_resid;
247 else
248 if (auio.uio_resid && error == 0)
249 error = EIO;
250 if ((ioflg & IO_NODELOCKED) == 0)
251 VOP_UNLOCK(vp);
252 return (error);
253}
254
255/*
256 * File table vnode read routine.
257 */
70struct fileops vnops =
71 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile };
72
73/*
74 * Common code for vnode open operations.
75 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
76 */
77int
78vn_open(ndp, fmode, cmode)
79 register struct nameidata *ndp;
80 int fmode, cmode;
81{
82 register struct vnode *vp;
83 register struct proc *p = ndp->ni_cnd.cn_proc;
84 register struct ucred *cred = p->p_ucred;
85 struct vattr vat;
86 struct vattr *vap = &vat;
87 int error;
88
89 if (fmode & O_CREAT) {
90 ndp->ni_cnd.cn_nameiop = CREATE;
91 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
92 if ((fmode & O_EXCL) == 0)
93 ndp->ni_cnd.cn_flags |= FOLLOW;
94 error = namei(ndp);
95 if (error)
96 return (error);
97 if (ndp->ni_vp == NULL) {
98 VATTR_NULL(vap);
99 vap->va_type = VREG;
100 vap->va_mode = cmode;
101 LEASE_CHECK(ndp->ni_dvp, p, cred, LEASE_WRITE);
102 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
103 &ndp->ni_cnd, vap);
104 if (error)
105 return (error);
106 fmode &= ~O_TRUNC;
107 vp = ndp->ni_vp;
108 } else {
109 VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
110 if (ndp->ni_dvp == ndp->ni_vp)
111 vrele(ndp->ni_dvp);
112 else
113 vput(ndp->ni_dvp);
114 ndp->ni_dvp = NULL;
115 vp = ndp->ni_vp;
116 if (fmode & O_EXCL) {
117 error = EEXIST;
118 goto bad;
119 }
120 fmode &= ~O_CREAT;
121 }
122 } else {
123 ndp->ni_cnd.cn_nameiop = LOOKUP;
124 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF;
125 error = namei(ndp);
126 if (error)
127 return (error);
128 vp = ndp->ni_vp;
129 }
130 if (vp->v_type == VSOCK) {
131 error = EOPNOTSUPP;
132 goto bad;
133 }
134 if ((fmode & O_CREAT) == 0) {
135 if (fmode & FREAD) {
136 error = VOP_ACCESS(vp, VREAD, cred, p);
137 if (error)
138 goto bad;
139 }
140 if (fmode & (FWRITE | O_TRUNC)) {
141 if (vp->v_type == VDIR) {
142 error = EISDIR;
143 goto bad;
144 }
145 error = vn_writechk(vp);
146 if (error)
147 goto bad;
148 error = VOP_ACCESS(vp, VWRITE, cred, p);
149 if (error)
150 goto bad;
151 }
152 }
153 if (fmode & O_TRUNC) {
154 VOP_UNLOCK(vp); /* XXX */
155 LEASE_CHECK(vp, p, cred, LEASE_WRITE);
156 VOP_LOCK(vp); /* XXX */
157 VATTR_NULL(vap);
158 vap->va_size = 0;
159 error = VOP_SETATTR(vp, vap, cred, p);
160 if (error)
161 goto bad;
162 }
163 error = VOP_OPEN(vp, fmode, cred, p);
164 if (error)
165 goto bad;
166 /*
167 * this is here for VMIO support
168 */
169 if (vp->v_type == VREG) {
170 if ((error = vn_vmio_open(vp, p, cred)) != 0)
171 goto bad;
172 }
173 if (fmode & FWRITE)
174 vp->v_writecount++;
175 return (0);
176bad:
177 vput(vp);
178 return (error);
179}
180
181/*
182 * Check for write permissions on the specified vnode.
183 * The read-only status of the file system is checked.
184 * Also, prototype text segments cannot be written.
185 */
186int
187vn_writechk(vp)
188 register struct vnode *vp;
189{
190
191 /*
192 * If there's shared text associated with
193 * the vnode, try to free it up once. If
194 * we fail, we can't allow writing.
195 */
196 if (vp->v_flag & VTEXT)
197 return (ETXTBSY);
198 return (0);
199}
200
201/*
202 * Vnode close call
203 */
204int
205vn_close(vp, flags, cred, p)
206 register struct vnode *vp;
207 int flags;
208 struct ucred *cred;
209 struct proc *p;
210{
211 int error;
212
213 if (flags & FWRITE)
214 vp->v_writecount--;
215 error = VOP_CLOSE(vp, flags, cred, p);
216 vn_vmio_close(vp);
217 return (error);
218}
219
220/*
221 * Package up an I/O request on a vnode into a uio and do it.
222 */
223int
224vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
225 enum uio_rw rw;
226 struct vnode *vp;
227 caddr_t base;
228 int len;
229 off_t offset;
230 enum uio_seg segflg;
231 int ioflg;
232 struct ucred *cred;
233 int *aresid;
234 struct proc *p;
235{
236 struct uio auio;
237 struct iovec aiov;
238 int error;
239
240 if ((ioflg & IO_NODELOCKED) == 0)
241 VOP_LOCK(vp);
242 auio.uio_iov = &aiov;
243 auio.uio_iovcnt = 1;
244 aiov.iov_base = base;
245 aiov.iov_len = len;
246 auio.uio_resid = len;
247 auio.uio_offset = offset;
248 auio.uio_segflg = segflg;
249 auio.uio_rw = rw;
250 auio.uio_procp = p;
251 if (rw == UIO_READ) {
252 error = VOP_READ(vp, &auio, ioflg, cred);
253 } else {
254 error = VOP_WRITE(vp, &auio, ioflg, cred);
255 }
256 if (aresid)
257 *aresid = auio.uio_resid;
258 else
259 if (auio.uio_resid && error == 0)
260 error = EIO;
261 if ((ioflg & IO_NODELOCKED) == 0)
262 VOP_UNLOCK(vp);
263 return (error);
264}
265
266/*
267 * File table vnode read routine.
268 */
258int
269static int
259vn_read(fp, uio, cred)
260 struct file *fp;
261 struct uio *uio;
262 struct ucred *cred;
263{
264 register struct vnode *vp = (struct vnode *)fp->f_data;
265 int count, error;
266
267 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
268 VOP_LOCK(vp);
269 uio->uio_offset = fp->f_offset;
270 count = uio->uio_resid;
271 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
272 cred);
273 fp->f_offset += count - uio->uio_resid;
274 VOP_UNLOCK(vp);
275 return (error);
276}
277
278/*
279 * File table vnode write routine.
280 */
270vn_read(fp, uio, cred)
271 struct file *fp;
272 struct uio *uio;
273 struct ucred *cred;
274{
275 register struct vnode *vp = (struct vnode *)fp->f_data;
276 int count, error;
277
278 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_READ);
279 VOP_LOCK(vp);
280 uio->uio_offset = fp->f_offset;
281 count = uio->uio_resid;
282 error = VOP_READ(vp, uio, (fp->f_flag & FNONBLOCK) ? IO_NDELAY : 0,
283 cred);
284 fp->f_offset += count - uio->uio_resid;
285 VOP_UNLOCK(vp);
286 return (error);
287}
288
289/*
290 * File table vnode write routine.
291 */
281int
292static int
282vn_write(fp, uio, cred)
283 struct file *fp;
284 struct uio *uio;
285 struct ucred *cred;
286{
287 register struct vnode *vp = (struct vnode *)fp->f_data;
288 int count, error, ioflag = 0;
289
290 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
291 ioflag |= IO_APPEND;
292 if (fp->f_flag & FNONBLOCK)
293 ioflag |= IO_NDELAY;
294 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
295 VOP_LOCK(vp);
296 uio->uio_offset = fp->f_offset;
297 count = uio->uio_resid;
298 error = VOP_WRITE(vp, uio, ioflag, cred);
299 if (ioflag & IO_APPEND)
300 fp->f_offset = uio->uio_offset;
301 else
302 fp->f_offset += count - uio->uio_resid;
303 VOP_UNLOCK(vp);
304 return (error);
305}
306
307/*
308 * File table vnode stat routine.
309 */
310int
311vn_stat(vp, sb, p)
312 struct vnode *vp;
313 register struct stat *sb;
314 struct proc *p;
315{
316 struct vattr vattr;
317 register struct vattr *vap;
318 int error;
319 u_short mode;
320
321 vap = &vattr;
322 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
323 if (error)
324 return (error);
325 /*
326 * Copy from vattr table
327 */
328 sb->st_dev = vap->va_fsid;
329 sb->st_ino = vap->va_fileid;
330 mode = vap->va_mode;
331 switch (vp->v_type) {
332 case VREG:
333 mode |= S_IFREG;
334 break;
335 case VDIR:
336 mode |= S_IFDIR;
337 break;
338 case VBLK:
339 mode |= S_IFBLK;
340 break;
341 case VCHR:
342 mode |= S_IFCHR;
343 break;
344 case VLNK:
345 mode |= S_IFLNK;
346 break;
347 case VSOCK:
348 mode |= S_IFSOCK;
349 break;
350 case VFIFO:
351 mode |= S_IFIFO;
352 break;
353 default:
354 return (EBADF);
355 };
356 sb->st_mode = mode;
357 sb->st_nlink = vap->va_nlink;
358 sb->st_uid = vap->va_uid;
359 sb->st_gid = vap->va_gid;
360 sb->st_rdev = vap->va_rdev;
361 sb->st_size = vap->va_size;
362 sb->st_atimespec = vap->va_atime;
363 sb->st_mtimespec= vap->va_mtime;
364 sb->st_ctimespec = vap->va_ctime;
365 sb->st_blksize = vap->va_blocksize;
366 sb->st_flags = vap->va_flags;
367 sb->st_gen = vap->va_gen;
368#if (S_BLKSIZE == 512)
369 /* Optimize this case */
370 sb->st_blocks = vap->va_bytes >> 9;
371#else
372 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
373#endif
374 return (0);
375}
376
377/*
378 * File table vnode ioctl routine.
379 */
293vn_write(fp, uio, cred)
294 struct file *fp;
295 struct uio *uio;
296 struct ucred *cred;
297{
298 register struct vnode *vp = (struct vnode *)fp->f_data;
299 int count, error, ioflag = 0;
300
301 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
302 ioflag |= IO_APPEND;
303 if (fp->f_flag & FNONBLOCK)
304 ioflag |= IO_NDELAY;
305 LEASE_CHECK(vp, uio->uio_procp, cred, LEASE_WRITE);
306 VOP_LOCK(vp);
307 uio->uio_offset = fp->f_offset;
308 count = uio->uio_resid;
309 error = VOP_WRITE(vp, uio, ioflag, cred);
310 if (ioflag & IO_APPEND)
311 fp->f_offset = uio->uio_offset;
312 else
313 fp->f_offset += count - uio->uio_resid;
314 VOP_UNLOCK(vp);
315 return (error);
316}
317
318/*
319 * File table vnode stat routine.
320 */
321int
322vn_stat(vp, sb, p)
323 struct vnode *vp;
324 register struct stat *sb;
325 struct proc *p;
326{
327 struct vattr vattr;
328 register struct vattr *vap;
329 int error;
330 u_short mode;
331
332 vap = &vattr;
333 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
334 if (error)
335 return (error);
336 /*
337 * Copy from vattr table
338 */
339 sb->st_dev = vap->va_fsid;
340 sb->st_ino = vap->va_fileid;
341 mode = vap->va_mode;
342 switch (vp->v_type) {
343 case VREG:
344 mode |= S_IFREG;
345 break;
346 case VDIR:
347 mode |= S_IFDIR;
348 break;
349 case VBLK:
350 mode |= S_IFBLK;
351 break;
352 case VCHR:
353 mode |= S_IFCHR;
354 break;
355 case VLNK:
356 mode |= S_IFLNK;
357 break;
358 case VSOCK:
359 mode |= S_IFSOCK;
360 break;
361 case VFIFO:
362 mode |= S_IFIFO;
363 break;
364 default:
365 return (EBADF);
366 };
367 sb->st_mode = mode;
368 sb->st_nlink = vap->va_nlink;
369 sb->st_uid = vap->va_uid;
370 sb->st_gid = vap->va_gid;
371 sb->st_rdev = vap->va_rdev;
372 sb->st_size = vap->va_size;
373 sb->st_atimespec = vap->va_atime;
374 sb->st_mtimespec= vap->va_mtime;
375 sb->st_ctimespec = vap->va_ctime;
376 sb->st_blksize = vap->va_blocksize;
377 sb->st_flags = vap->va_flags;
378 sb->st_gen = vap->va_gen;
379#if (S_BLKSIZE == 512)
380 /* Optimize this case */
381 sb->st_blocks = vap->va_bytes >> 9;
382#else
383 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
384#endif
385 return (0);
386}
387
388/*
389 * File table vnode ioctl routine.
390 */
380int
391static int
381vn_ioctl(fp, com, data, p)
382 struct file *fp;
383 int com;
384 caddr_t data;
385 struct proc *p;
386{
387 register struct vnode *vp = ((struct vnode *)fp->f_data);
388 struct vattr vattr;
389 int error;
390
391 switch (vp->v_type) {
392
393 case VREG:
394 case VDIR:
395 if (com == FIONREAD) {
396 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
397 if (error)
398 return (error);
399 *(int *)data = vattr.va_size - fp->f_offset;
400 return (0);
401 }
402 if (com == FIONBIO || com == FIOASYNC) /* XXX */
403 return (0); /* XXX */
404 /* fall into ... */
405
406 default:
407 return (ENOTTY);
408
409 case VFIFO:
410 case VCHR:
411 case VBLK:
412 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
413 if (error == 0 && com == TIOCSCTTY) {
414
415 /* Do nothing if reassigning same control tty */
416 if (p->p_session->s_ttyvp == vp)
417 return (0);
418
419 /* Get rid of reference to old control tty */
420 if (p->p_session->s_ttyvp)
421 vrele(p->p_session->s_ttyvp);
422
423 p->p_session->s_ttyvp = vp;
424 VREF(vp);
425 }
426 return (error);
427 }
428}
429
430/*
431 * File table vnode select routine.
432 */
392vn_ioctl(fp, com, data, p)
393 struct file *fp;
394 int com;
395 caddr_t data;
396 struct proc *p;
397{
398 register struct vnode *vp = ((struct vnode *)fp->f_data);
399 struct vattr vattr;
400 int error;
401
402 switch (vp->v_type) {
403
404 case VREG:
405 case VDIR:
406 if (com == FIONREAD) {
407 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
408 if (error)
409 return (error);
410 *(int *)data = vattr.va_size - fp->f_offset;
411 return (0);
412 }
413 if (com == FIONBIO || com == FIOASYNC) /* XXX */
414 return (0); /* XXX */
415 /* fall into ... */
416
417 default:
418 return (ENOTTY);
419
420 case VFIFO:
421 case VCHR:
422 case VBLK:
423 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
424 if (error == 0 && com == TIOCSCTTY) {
425
426 /* Do nothing if reassigning same control tty */
427 if (p->p_session->s_ttyvp == vp)
428 return (0);
429
430 /* Get rid of reference to old control tty */
431 if (p->p_session->s_ttyvp)
432 vrele(p->p_session->s_ttyvp);
433
434 p->p_session->s_ttyvp = vp;
435 VREF(vp);
436 }
437 return (error);
438 }
439}
440
441/*
442 * File table vnode select routine.
443 */
433int
444static int
434vn_select(fp, which, p)
435 struct file *fp;
436 int which;
437 struct proc *p;
438{
439
440 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
441 fp->f_cred, p));
442}
443
444/*
445 * File table vnode close routine.
446 */
445vn_select(fp, which, p)
446 struct file *fp;
447 int which;
448 struct proc *p;
449{
450
451 return (VOP_SELECT(((struct vnode *)fp->f_data), which, fp->f_flag,
452 fp->f_cred, p));
453}
454
455/*
456 * File table vnode close routine.
457 */
447int
458static int
448vn_closefile(fp, p)
449 struct file *fp;
450 struct proc *p;
451{
452
453 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
454 fp->f_cred, p));
455}
456
459vn_closefile(fp, p)
460 struct file *fp;
461 struct proc *p;
462{
463
464 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
465 fp->f_cred, p));
466}
467
457int
468static int
458vn_vmio_open(vp, p, cred)
459 struct vnode *vp;
460 struct proc *p;
461 struct ucred *cred;
462{
463 struct vattr vat;
464 int error;
465 /*
466 * this is here for VMIO support
467 */
468 if (vp->v_type == VREG || vp->v_type == VBLK) {
469retry:
470 if ((vp->v_flag & VVMIO) == 0) {
471 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
472 return error;
473 (void) vnode_pager_alloc(vp, vat.va_size, 0, 0);
474 vp->v_flag |= VVMIO;
475 } else {
476 vm_object_t object;
477 if ((object = vp->v_object) &&
478 (object->flags & OBJ_DEAD)) {
479 VOP_UNLOCK(vp);
480 tsleep(object, PVM, "vodead", 0);
481 VOP_LOCK(vp);
482 goto retry;
483 }
484 if (!object)
485 panic("vn_open: VMIO object missing");
486 vm_object_reference(object);
487 }
488 }
489 return 0;
490}
491
492void
493vn_vmio_close(vp)
494 struct vnode *vp;
495{
496 /*
497 * this code is here for VMIO support, will eventually
498 * be in vfs code.
499 */
500 if (vp->v_flag & VVMIO) {
501 vrele(vp);
502 if (vp->v_object == NULL)
503 panic("vn_close: VMIO object missing");
504 vm_object_deallocate(vp->v_object);
505 } else
506 vrele(vp);
507}
469vn_vmio_open(vp, p, cred)
470 struct vnode *vp;
471 struct proc *p;
472 struct ucred *cred;
473{
474 struct vattr vat;
475 int error;
476 /*
477 * this is here for VMIO support
478 */
479 if (vp->v_type == VREG || vp->v_type == VBLK) {
480retry:
481 if ((vp->v_flag & VVMIO) == 0) {
482 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
483 return error;
484 (void) vnode_pager_alloc(vp, vat.va_size, 0, 0);
485 vp->v_flag |= VVMIO;
486 } else {
487 vm_object_t object;
488 if ((object = vp->v_object) &&
489 (object->flags & OBJ_DEAD)) {
490 VOP_UNLOCK(vp);
491 tsleep(object, PVM, "vodead", 0);
492 VOP_LOCK(vp);
493 goto retry;
494 }
495 if (!object)
496 panic("vn_open: VMIO object missing");
497 vm_object_reference(object);
498 }
499 }
500 return 0;
501}
502
503void
504vn_vmio_close(vp)
505 struct vnode *vp;
506{
507 /*
508 * this code is here for VMIO support, will eventually
509 * be in vfs code.
510 */
511 if (vp->v_flag & VVMIO) {
512 vrele(vp);
513 if (vp->v_object == NULL)
514 panic("vn_close: VMIO object missing");
515 vm_object_deallocate(vp->v_object);
516 } else
517 vrele(vp);
518}