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 * $FreeBSD: head/sys/kern/vfs_vnops.c 55696 2000-01-10 00:08:53Z mckusick $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/fcntl.h>
45#include <sys/file.h>
46#include <sys/stat.h>
47#include <sys/proc.h>
48#include <sys/mount.h>
49#include <sys/namei.h>
50#include <sys/vnode.h>
51#include <sys/buf.h>
52#include <sys/filio.h>
53#include <sys/ttycom.h>
54#include <sys/conf.h>
55#include <vm/vm_zone.h>
56
57static int vn_closefile __P((struct file *fp, struct proc *p));
58static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
59 struct proc *p));
60static int vn_read __P((struct file *fp, struct uio *uio,
61 struct ucred *cred, int flags, struct proc *p));
62static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
63 struct proc *p));
64static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
65static int vn_write __P((struct file *fp, struct uio *uio,
66 struct ucred *cred, int flags, struct proc *p));
67
68struct fileops vnops =
69 { vn_read, vn_write, vn_ioctl, vn_poll, vn_statfile, vn_closefile };
70
71/*
72 * Common code for vnode open operations.
73 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
74 *
75 * Note that this do NOT free nameidata for the successful case,
76 * due to the NDINIT being done elsewhere.
77 */
78int
79vn_open(ndp, fmode, cmode)
80 register struct nameidata *ndp;
81 int fmode, cmode;
82{
83 register struct vnode *vp;
84 register struct proc *p = ndp->ni_cnd.cn_proc;
85 register struct ucred *cred = p->p_ucred;
86 struct vattr vat;
87 struct vattr *vap = &vat;
88 int mode, error;
89
90 if (fmode & O_CREAT) {
91 ndp->ni_cnd.cn_nameiop = CREATE;
92 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
93 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
94 ndp->ni_cnd.cn_flags |= FOLLOW;
95 bwillwrite();
96 error = namei(ndp);
97 if (error)
98 return (error);
99 if (ndp->ni_vp == NULL) {
100 VATTR_NULL(vap);
101 vap->va_type = VREG;
102 vap->va_mode = cmode;
103 if (fmode & O_EXCL)
104 vap->va_vaflags |= VA_EXCLUSIVE;
105 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
106 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
107 &ndp->ni_cnd, vap);
108 if (error) {
109 NDFREE(ndp, NDF_ONLY_PNBUF);
110 vput(ndp->ni_dvp);
111 return (error);
112 }
113 vput(ndp->ni_dvp);
114 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
115 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
116 fmode &= ~O_TRUNC;
117 vp = ndp->ni_vp;
118 } else {
119 if (ndp->ni_dvp == ndp->ni_vp)
120 vrele(ndp->ni_dvp);
121 else
122 vput(ndp->ni_dvp);
123 ndp->ni_dvp = NULL;
124 vp = ndp->ni_vp;
125 if (fmode & O_EXCL) {
126 error = EEXIST;
127 goto bad;
128 }
129 fmode &= ~O_CREAT;
130 }
131 } else {
132 ndp->ni_cnd.cn_nameiop = LOOKUP;
133 ndp->ni_cnd.cn_flags =
134 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
135 error = namei(ndp);
136 if (error)
137 return (error);
138 vp = ndp->ni_vp;
139 }
140 if (vp->v_type == VLNK) {
141 error = EMLINK;
142 goto bad;
143 }
144 if (vp->v_type == VSOCK) {
145 error = EOPNOTSUPP;
146 goto bad;
147 }
148 if ((fmode & O_CREAT) == 0) {
149 mode = 0;
150 if (fmode & (FWRITE | O_TRUNC)) {
151 if (vp->v_type == VDIR) {
152 error = EISDIR;
153 goto bad;
154 }
155 error = vn_writechk(vp);
156 if (error)
157 goto bad;
158 mode |= VWRITE;
159 }
160 if (fmode & FREAD)
161 mode |= VREAD;
162 if (mode) {
163 error = VOP_ACCESS(vp, mode, cred, p);
164 if (error)
165 goto bad;
166 }
167 }
168 if (fmode & O_TRUNC) {
169 VOP_UNLOCK(vp, 0, p); /* XXX */
170 VOP_LEASE(vp, p, cred, LEASE_WRITE);
171 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
172 VATTR_NULL(vap);
173 vap->va_size = 0;
174 error = VOP_SETATTR(vp, vap, cred, p);
175 if (error)
176 goto bad;
177 }
178 error = VOP_OPEN(vp, fmode, cred, p);
179 if (error)
180 goto bad;
181 /*
182 * Make sure that a VM object is created for VMIO support.
183 */
184 if (vn_canvmio(vp) == TRUE) {
185 if ((error = vfs_object_create(vp, p, cred)) != 0)
186 goto bad;
187 }
188
189 if (fmode & FWRITE)
190 vp->v_writecount++;
191 return (0);
192bad:
193 NDFREE(ndp, NDF_ONLY_PNBUF);
194 vput(vp);
195 return (error);
196}
197
198/*
199 * Check for write permissions on the specified vnode.
200 * Prototype text segments cannot be written.
201 */
202int
203vn_writechk(vp)
204 register struct vnode *vp;
205{
206
207 /*
208 * If there's shared text associated with
209 * the vnode, try to free it up once. If
210 * we fail, we can't allow writing.
211 */
212 if (vp->v_flag & VTEXT)
213 return (ETXTBSY);
214 return (0);
215}
216
217/*
218 * Vnode close call
219 */
220int
221vn_close(vp, flags, cred, p)
222 register struct vnode *vp;
223 int flags;
224 struct ucred *cred;
225 struct proc *p;
226{
227 int error;
228
229 if (flags & FWRITE)
230 vp->v_writecount--;
231 error = VOP_CLOSE(vp, flags, cred, p);
232 vrele(vp);
233 return (error);
234}
235
236/*
237 * Package up an I/O request on a vnode into a uio and do it.
238 */
239int
240vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
241 enum uio_rw rw;
242 struct vnode *vp;
243 caddr_t base;
244 int len;
245 off_t offset;
246 enum uio_seg segflg;
247 int ioflg;
248 struct ucred *cred;
249 int *aresid;
250 struct proc *p;
251{
252 struct uio auio;
253 struct iovec aiov;
254 int error;
255
256 if ((ioflg & IO_NODELOCKED) == 0)
257 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
258 auio.uio_iov = &aiov;
259 auio.uio_iovcnt = 1;
260 aiov.iov_base = base;
261 aiov.iov_len = len;
262 auio.uio_resid = len;
263 auio.uio_offset = offset;
264 auio.uio_segflg = segflg;
265 auio.uio_rw = rw;
266 auio.uio_procp = p;
267 if (rw == UIO_READ) {
268 error = VOP_READ(vp, &auio, ioflg, cred);
269 } else {
270 error = VOP_WRITE(vp, &auio, ioflg, cred);
271 }
272 if (aresid)
273 *aresid = auio.uio_resid;
274 else
275 if (auio.uio_resid && error == 0)
276 error = EIO;
277 if ((ioflg & IO_NODELOCKED) == 0)
278 VOP_UNLOCK(vp, 0, p);
279 return (error);
280}
281
282/*
283 * File table vnode read routine.
284 */
285static int
286vn_read(fp, uio, cred, flags, p)
287 struct file *fp;
288 struct uio *uio;
289 struct ucred *cred;
290 struct proc *p;
291 int flags;
292{
293 struct vnode *vp;
294 int error, ioflag;
295
296 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
297 uio->uio_procp, p));
298 vp = (struct vnode *)fp->f_data;
299 ioflag = 0;
300 if (fp->f_flag & FNONBLOCK)
301 ioflag |= IO_NDELAY;
302 VOP_LEASE(vp, p, cred, LEASE_READ);
303 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
304 if ((flags & FOF_OFFSET) == 0)
305 uio->uio_offset = fp->f_offset;
306
307 /*
308 * Sequential read heuristic.
309 * If we have been doing sequential input,
310 * a rewind operation doesn't turn off
311 * sequential input mode.
312 */
313 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
314 uio->uio_offset == fp->f_nextread) {
315 int tmpseq = fp->f_seqcount;
316 /*
317 * XXX we assume that the filesystem block size is
318 * the default. Not true, but still gives us a pretty
319 * good indicator of how sequential the read operations
320 * are.
321 */
322 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
323 if (tmpseq >= 127)
324 tmpseq = 127;
325 fp->f_seqcount = tmpseq;
326 ioflag |= fp->f_seqcount << 16;
327 } else {
328 if (fp->f_seqcount > 1)
329 fp->f_seqcount = 1;
330 else
331 fp->f_seqcount = 0;
332 }
333 error = VOP_READ(vp, uio, ioflag, cred);
334 if ((flags & FOF_OFFSET) == 0)
335 fp->f_offset = uio->uio_offset;
336 fp->f_nextread = uio->uio_offset;
337 VOP_UNLOCK(vp, 0, p);
338 return (error);
339}
340
341/*
342 * File table vnode write routine.
343 */
344static int
345vn_write(fp, uio, cred, flags, p)
346 struct file *fp;
347 struct uio *uio;
348 struct ucred *cred;
349 struct proc *p;
350 int flags;
351{
352 struct vnode *vp;
353 int error, ioflag;
354
355 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
356 uio->uio_procp, p));
357 vp = (struct vnode *)fp->f_data;
358 if (vp->v_type == VREG)
359 bwillwrite();
360 vp = (struct vnode *)fp->f_data; /* XXX needed? */
361 ioflag = IO_UNIT;
362 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
363 ioflag |= IO_APPEND;
364 if (fp->f_flag & FNONBLOCK)
365 ioflag |= IO_NDELAY;
366 if ((fp->f_flag & O_FSYNC) ||
367 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
368 ioflag |= IO_SYNC;
369 VOP_LEASE(vp, p, cred, LEASE_WRITE);
370 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
371 if ((flags & FOF_OFFSET) == 0)
372 uio->uio_offset = fp->f_offset;
373 error = VOP_WRITE(vp, uio, ioflag, cred);
374 if ((flags & FOF_OFFSET) == 0)
375 fp->f_offset = uio->uio_offset;
376 VOP_UNLOCK(vp, 0, p);
377 return (error);
378}
379
380/*
381 * File table vnode stat routine.
382 */
383static int
384vn_statfile(fp, sb, p)
385 struct file *fp;
386 struct stat *sb;
387 struct proc *p;
388{
389 struct vnode *vp = (struct vnode *)fp->f_data;
390
391 return vn_stat(vp, sb, p);
392}
393
394int
395vn_stat(vp, sb, p)
396 struct vnode *vp;
397 register struct stat *sb;
398 struct proc *p;
399{
400 struct vattr vattr;
401 register struct vattr *vap;
402 int error;
403 u_short mode;
404
405 vap = &vattr;
406 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
407 if (error)
408 return (error);
409
410 /*
411 * Zero the spare stat fields
412 */
413 sb->st_lspare = 0;
414 sb->st_qspare[0] = 0;
415 sb->st_qspare[1] = 0;
416
417 /*
418 * Copy from vattr table
419 */
420 if (vap->va_fsid != VNOVAL)
421 sb->st_dev = vap->va_fsid;
422 else
423 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
424 sb->st_ino = vap->va_fileid;
425 mode = vap->va_mode;
426 switch (vap->va_type) {
427 case VREG:
428 mode |= S_IFREG;
429 break;
430 case VDIR:
431 mode |= S_IFDIR;
432 break;
433 case VBLK:
434 mode |= S_IFBLK;
435 break;
436 case VCHR:
437 mode |= S_IFCHR;
438 break;
439 case VLNK:
440 mode |= S_IFLNK;
441 /* This is a cosmetic change, symlinks do not have a mode. */
442 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
443 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
444 else
445 sb->st_mode |= ACCESSPERMS; /* 0777 */
446 break;
447 case VSOCK:
448 mode |= S_IFSOCK;
449 break;
450 case VFIFO:
451 mode |= S_IFIFO;
452 break;
453 default:
454 return (EBADF);
455 };
456 sb->st_mode = mode;
457 sb->st_nlink = vap->va_nlink;
458 sb->st_uid = vap->va_uid;
459 sb->st_gid = vap->va_gid;
460 sb->st_rdev = vap->va_rdev;
461 sb->st_size = vap->va_size;
462 sb->st_atimespec = vap->va_atime;
463 sb->st_mtimespec = vap->va_mtime;
464 sb->st_ctimespec = vap->va_ctime;
465
466 /*
467 * According to www.opengroup.org, the meaning of st_blksize is
468 * "a filesystem-specific preferred I/O block size for this
469 * object. In some filesystem types, this may vary from file
470 * to file"
471 * Default to zero to catch bogus uses of this field.
472 */
473
474 if (vap->va_type == VREG) {
475 sb->st_blksize = vap->va_blocksize;
476 } else if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
477 devsw(vp->v_rdev) && (devsw(vp->v_rdev)->d_flags & D_DISK)) {
478 /* XXX use vn_isdisk() above once VCHR is also disk */
479 sb->st_blksize = vp->v_rdev->si_bsize_best;
480 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
481 sb->st_blksize = vp->v_rdev->si_bsize_phys;
482 if (sb->st_blksize < BLKDEV_IOSIZE)
483 sb->st_blksize = BLKDEV_IOSIZE;
484 } else {
485 sb->st_blksize = 0;
486 }
487
488 sb->st_flags = vap->va_flags;
489 if (suser_xxx(p->p_ucred, 0, 0))
490 sb->st_gen = 0;
491 else
492 sb->st_gen = vap->va_gen;
493
494#if (S_BLKSIZE == 512)
495 /* Optimize this case */
496 sb->st_blocks = vap->va_bytes >> 9;
497#else
498 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
499#endif
500 return (0);
501}
502
503/*
504 * File table vnode ioctl routine.
505 */
506static int
507vn_ioctl(fp, com, data, p)
508 struct file *fp;
509 u_long com;
510 caddr_t data;
511 struct proc *p;
512{
513 register struct vnode *vp = ((struct vnode *)fp->f_data);
514 struct vattr vattr;
515 int error;
516
517 switch (vp->v_type) {
518
519 case VREG:
520 case VDIR:
521 if (com == FIONREAD) {
522 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
523 if (error)
524 return (error);
525 *(int *)data = vattr.va_size - fp->f_offset;
526 return (0);
527 }
528 if (com == FIONBIO || com == FIOASYNC) /* XXX */
529 return (0); /* XXX */
530 /* fall into ... */
531
532 default:
533#if 0
534 return (ENOTTY);
535#endif
536 case VFIFO:
537 case VCHR:
538 case VBLK:
539 if (com == FIODTYPE) {
540 if (vp->v_type != VCHR && vp->v_type != VBLK)
541 return (ENOTTY);
542 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
543 return (0);
544 }
545 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
546 if (error == 0 && com == TIOCSCTTY) {
547
548 /* Do nothing if reassigning same control tty */
549 if (p->p_session->s_ttyvp == vp)
550 return (0);
551
552 /* Get rid of reference to old control tty */
553 if (p->p_session->s_ttyvp)
554 vrele(p->p_session->s_ttyvp);
555
556 p->p_session->s_ttyvp = vp;
557 VREF(vp);
558 }
559 return (error);
560 }
561}
562
563/*
564 * File table vnode poll routine.
565 */
566static int
567vn_poll(fp, events, cred, p)
568 struct file *fp;
569 int events;
570 struct ucred *cred;
571 struct proc *p;
572{
573
574 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p));
575}
576
577/*
578 * Check that the vnode is still valid, and if so
579 * acquire requested lock.
580 */
581int
582#ifndef DEBUG_LOCKS
583vn_lock(vp, flags, p)
584#else
585debug_vn_lock(vp, flags, p, filename, line)
586#endif
587 struct vnode *vp;
588 int flags;
589 struct proc *p;
590#ifdef DEBUG_LOCKS
591 const char *filename;
592 int line;
593#endif
594{
595 int error;
596
597 do {
598 if ((flags & LK_INTERLOCK) == 0)
599 simple_lock(&vp->v_interlock);
600 if (vp->v_flag & VXLOCK) {
601 vp->v_flag |= VXWANT;
602 simple_unlock(&vp->v_interlock);
603 tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
604 error = ENOENT;
605 } else {
606#ifdef DEBUG_LOCKS
607 vp->filename = filename;
608 vp->line = line;
609#endif
610 error = VOP_LOCK(vp,
611 flags | LK_NOPAUSE | LK_INTERLOCK, p);
612 if (error == 0)
613 return (error);
614 }
615 flags &= ~LK_INTERLOCK;
616 } while (flags & LK_RETRY);
617 return (error);
618}
619
620/*
621 * File table vnode close routine.
622 */
623static int
624vn_closefile(fp, p)
625 struct file *fp;
626 struct proc *p;
627{
628
629 fp->f_ops = &badfileops;
630 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
631 fp->f_cred, p));
632}
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 * $FreeBSD: head/sys/kern/vfs_vnops.c 55696 2000-01-10 00:08:53Z mckusick $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/fcntl.h>
45#include <sys/file.h>
46#include <sys/stat.h>
47#include <sys/proc.h>
48#include <sys/mount.h>
49#include <sys/namei.h>
50#include <sys/vnode.h>
51#include <sys/buf.h>
52#include <sys/filio.h>
53#include <sys/ttycom.h>
54#include <sys/conf.h>
55#include <vm/vm_zone.h>
56
57static int vn_closefile __P((struct file *fp, struct proc *p));
58static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
59 struct proc *p));
60static int vn_read __P((struct file *fp, struct uio *uio,
61 struct ucred *cred, int flags, struct proc *p));
62static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
63 struct proc *p));
64static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
65static int vn_write __P((struct file *fp, struct uio *uio,
66 struct ucred *cred, int flags, struct proc *p));
67
68struct fileops vnops =
69 { vn_read, vn_write, vn_ioctl, vn_poll, vn_statfile, vn_closefile };
70
71/*
72 * Common code for vnode open operations.
73 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
74 *
75 * Note that this do NOT free nameidata for the successful case,
76 * due to the NDINIT being done elsewhere.
77 */
78int
79vn_open(ndp, fmode, cmode)
80 register struct nameidata *ndp;
81 int fmode, cmode;
82{
83 register struct vnode *vp;
84 register struct proc *p = ndp->ni_cnd.cn_proc;
85 register struct ucred *cred = p->p_ucred;
86 struct vattr vat;
87 struct vattr *vap = &vat;
88 int mode, error;
89
90 if (fmode & O_CREAT) {
91 ndp->ni_cnd.cn_nameiop = CREATE;
92 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
93 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
94 ndp->ni_cnd.cn_flags |= FOLLOW;
95 bwillwrite();
96 error = namei(ndp);
97 if (error)
98 return (error);
99 if (ndp->ni_vp == NULL) {
100 VATTR_NULL(vap);
101 vap->va_type = VREG;
102 vap->va_mode = cmode;
103 if (fmode & O_EXCL)
104 vap->va_vaflags |= VA_EXCLUSIVE;
105 VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
106 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
107 &ndp->ni_cnd, vap);
108 if (error) {
109 NDFREE(ndp, NDF_ONLY_PNBUF);
110 vput(ndp->ni_dvp);
111 return (error);
112 }
113 vput(ndp->ni_dvp);
114 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
115 ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
116 fmode &= ~O_TRUNC;
117 vp = ndp->ni_vp;
118 } else {
119 if (ndp->ni_dvp == ndp->ni_vp)
120 vrele(ndp->ni_dvp);
121 else
122 vput(ndp->ni_dvp);
123 ndp->ni_dvp = NULL;
124 vp = ndp->ni_vp;
125 if (fmode & O_EXCL) {
126 error = EEXIST;
127 goto bad;
128 }
129 fmode &= ~O_CREAT;
130 }
131 } else {
132 ndp->ni_cnd.cn_nameiop = LOOKUP;
133 ndp->ni_cnd.cn_flags =
134 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
135 error = namei(ndp);
136 if (error)
137 return (error);
138 vp = ndp->ni_vp;
139 }
140 if (vp->v_type == VLNK) {
141 error = EMLINK;
142 goto bad;
143 }
144 if (vp->v_type == VSOCK) {
145 error = EOPNOTSUPP;
146 goto bad;
147 }
148 if ((fmode & O_CREAT) == 0) {
149 mode = 0;
150 if (fmode & (FWRITE | O_TRUNC)) {
151 if (vp->v_type == VDIR) {
152 error = EISDIR;
153 goto bad;
154 }
155 error = vn_writechk(vp);
156 if (error)
157 goto bad;
158 mode |= VWRITE;
159 }
160 if (fmode & FREAD)
161 mode |= VREAD;
162 if (mode) {
163 error = VOP_ACCESS(vp, mode, cred, p);
164 if (error)
165 goto bad;
166 }
167 }
168 if (fmode & O_TRUNC) {
169 VOP_UNLOCK(vp, 0, p); /* XXX */
170 VOP_LEASE(vp, p, cred, LEASE_WRITE);
171 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
172 VATTR_NULL(vap);
173 vap->va_size = 0;
174 error = VOP_SETATTR(vp, vap, cred, p);
175 if (error)
176 goto bad;
177 }
178 error = VOP_OPEN(vp, fmode, cred, p);
179 if (error)
180 goto bad;
181 /*
182 * Make sure that a VM object is created for VMIO support.
183 */
184 if (vn_canvmio(vp) == TRUE) {
185 if ((error = vfs_object_create(vp, p, cred)) != 0)
186 goto bad;
187 }
188
189 if (fmode & FWRITE)
190 vp->v_writecount++;
191 return (0);
192bad:
193 NDFREE(ndp, NDF_ONLY_PNBUF);
194 vput(vp);
195 return (error);
196}
197
198/*
199 * Check for write permissions on the specified vnode.
200 * Prototype text segments cannot be written.
201 */
202int
203vn_writechk(vp)
204 register struct vnode *vp;
205{
206
207 /*
208 * If there's shared text associated with
209 * the vnode, try to free it up once. If
210 * we fail, we can't allow writing.
211 */
212 if (vp->v_flag & VTEXT)
213 return (ETXTBSY);
214 return (0);
215}
216
217/*
218 * Vnode close call
219 */
220int
221vn_close(vp, flags, cred, p)
222 register struct vnode *vp;
223 int flags;
224 struct ucred *cred;
225 struct proc *p;
226{
227 int error;
228
229 if (flags & FWRITE)
230 vp->v_writecount--;
231 error = VOP_CLOSE(vp, flags, cred, p);
232 vrele(vp);
233 return (error);
234}
235
236/*
237 * Package up an I/O request on a vnode into a uio and do it.
238 */
239int
240vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
241 enum uio_rw rw;
242 struct vnode *vp;
243 caddr_t base;
244 int len;
245 off_t offset;
246 enum uio_seg segflg;
247 int ioflg;
248 struct ucred *cred;
249 int *aresid;
250 struct proc *p;
251{
252 struct uio auio;
253 struct iovec aiov;
254 int error;
255
256 if ((ioflg & IO_NODELOCKED) == 0)
257 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
258 auio.uio_iov = &aiov;
259 auio.uio_iovcnt = 1;
260 aiov.iov_base = base;
261 aiov.iov_len = len;
262 auio.uio_resid = len;
263 auio.uio_offset = offset;
264 auio.uio_segflg = segflg;
265 auio.uio_rw = rw;
266 auio.uio_procp = p;
267 if (rw == UIO_READ) {
268 error = VOP_READ(vp, &auio, ioflg, cred);
269 } else {
270 error = VOP_WRITE(vp, &auio, ioflg, cred);
271 }
272 if (aresid)
273 *aresid = auio.uio_resid;
274 else
275 if (auio.uio_resid && error == 0)
276 error = EIO;
277 if ((ioflg & IO_NODELOCKED) == 0)
278 VOP_UNLOCK(vp, 0, p);
279 return (error);
280}
281
282/*
283 * File table vnode read routine.
284 */
285static int
286vn_read(fp, uio, cred, flags, p)
287 struct file *fp;
288 struct uio *uio;
289 struct ucred *cred;
290 struct proc *p;
291 int flags;
292{
293 struct vnode *vp;
294 int error, ioflag;
295
296 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
297 uio->uio_procp, p));
298 vp = (struct vnode *)fp->f_data;
299 ioflag = 0;
300 if (fp->f_flag & FNONBLOCK)
301 ioflag |= IO_NDELAY;
302 VOP_LEASE(vp, p, cred, LEASE_READ);
303 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
304 if ((flags & FOF_OFFSET) == 0)
305 uio->uio_offset = fp->f_offset;
306
307 /*
308 * Sequential read heuristic.
309 * If we have been doing sequential input,
310 * a rewind operation doesn't turn off
311 * sequential input mode.
312 */
313 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
314 uio->uio_offset == fp->f_nextread) {
315 int tmpseq = fp->f_seqcount;
316 /*
317 * XXX we assume that the filesystem block size is
318 * the default. Not true, but still gives us a pretty
319 * good indicator of how sequential the read operations
320 * are.
321 */
322 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
323 if (tmpseq >= 127)
324 tmpseq = 127;
325 fp->f_seqcount = tmpseq;
326 ioflag |= fp->f_seqcount << 16;
327 } else {
328 if (fp->f_seqcount > 1)
329 fp->f_seqcount = 1;
330 else
331 fp->f_seqcount = 0;
332 }
333 error = VOP_READ(vp, uio, ioflag, cred);
334 if ((flags & FOF_OFFSET) == 0)
335 fp->f_offset = uio->uio_offset;
336 fp->f_nextread = uio->uio_offset;
337 VOP_UNLOCK(vp, 0, p);
338 return (error);
339}
340
341/*
342 * File table vnode write routine.
343 */
344static int
345vn_write(fp, uio, cred, flags, p)
346 struct file *fp;
347 struct uio *uio;
348 struct ucred *cred;
349 struct proc *p;
350 int flags;
351{
352 struct vnode *vp;
353 int error, ioflag;
354
355 KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
356 uio->uio_procp, p));
357 vp = (struct vnode *)fp->f_data;
358 if (vp->v_type == VREG)
359 bwillwrite();
360 vp = (struct vnode *)fp->f_data; /* XXX needed? */
361 ioflag = IO_UNIT;
362 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
363 ioflag |= IO_APPEND;
364 if (fp->f_flag & FNONBLOCK)
365 ioflag |= IO_NDELAY;
366 if ((fp->f_flag & O_FSYNC) ||
367 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
368 ioflag |= IO_SYNC;
369 VOP_LEASE(vp, p, cred, LEASE_WRITE);
370 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
371 if ((flags & FOF_OFFSET) == 0)
372 uio->uio_offset = fp->f_offset;
373 error = VOP_WRITE(vp, uio, ioflag, cred);
374 if ((flags & FOF_OFFSET) == 0)
375 fp->f_offset = uio->uio_offset;
376 VOP_UNLOCK(vp, 0, p);
377 return (error);
378}
379
380/*
381 * File table vnode stat routine.
382 */
383static int
384vn_statfile(fp, sb, p)
385 struct file *fp;
386 struct stat *sb;
387 struct proc *p;
388{
389 struct vnode *vp = (struct vnode *)fp->f_data;
390
391 return vn_stat(vp, sb, p);
392}
393
394int
395vn_stat(vp, sb, p)
396 struct vnode *vp;
397 register struct stat *sb;
398 struct proc *p;
399{
400 struct vattr vattr;
401 register struct vattr *vap;
402 int error;
403 u_short mode;
404
405 vap = &vattr;
406 error = VOP_GETATTR(vp, vap, p->p_ucred, p);
407 if (error)
408 return (error);
409
410 /*
411 * Zero the spare stat fields
412 */
413 sb->st_lspare = 0;
414 sb->st_qspare[0] = 0;
415 sb->st_qspare[1] = 0;
416
417 /*
418 * Copy from vattr table
419 */
420 if (vap->va_fsid != VNOVAL)
421 sb->st_dev = vap->va_fsid;
422 else
423 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
424 sb->st_ino = vap->va_fileid;
425 mode = vap->va_mode;
426 switch (vap->va_type) {
427 case VREG:
428 mode |= S_IFREG;
429 break;
430 case VDIR:
431 mode |= S_IFDIR;
432 break;
433 case VBLK:
434 mode |= S_IFBLK;
435 break;
436 case VCHR:
437 mode |= S_IFCHR;
438 break;
439 case VLNK:
440 mode |= S_IFLNK;
441 /* This is a cosmetic change, symlinks do not have a mode. */
442 if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
443 sb->st_mode &= ~ACCESSPERMS; /* 0000 */
444 else
445 sb->st_mode |= ACCESSPERMS; /* 0777 */
446 break;
447 case VSOCK:
448 mode |= S_IFSOCK;
449 break;
450 case VFIFO:
451 mode |= S_IFIFO;
452 break;
453 default:
454 return (EBADF);
455 };
456 sb->st_mode = mode;
457 sb->st_nlink = vap->va_nlink;
458 sb->st_uid = vap->va_uid;
459 sb->st_gid = vap->va_gid;
460 sb->st_rdev = vap->va_rdev;
461 sb->st_size = vap->va_size;
462 sb->st_atimespec = vap->va_atime;
463 sb->st_mtimespec = vap->va_mtime;
464 sb->st_ctimespec = vap->va_ctime;
465
466 /*
467 * According to www.opengroup.org, the meaning of st_blksize is
468 * "a filesystem-specific preferred I/O block size for this
469 * object. In some filesystem types, this may vary from file
470 * to file"
471 * Default to zero to catch bogus uses of this field.
472 */
473
474 if (vap->va_type == VREG) {
475 sb->st_blksize = vap->va_blocksize;
476 } else if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
477 devsw(vp->v_rdev) && (devsw(vp->v_rdev)->d_flags & D_DISK)) {
478 /* XXX use vn_isdisk() above once VCHR is also disk */
479 sb->st_blksize = vp->v_rdev->si_bsize_best;
480 if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
481 sb->st_blksize = vp->v_rdev->si_bsize_phys;
482 if (sb->st_blksize < BLKDEV_IOSIZE)
483 sb->st_blksize = BLKDEV_IOSIZE;
484 } else {
485 sb->st_blksize = 0;
486 }
487
488 sb->st_flags = vap->va_flags;
489 if (suser_xxx(p->p_ucred, 0, 0))
490 sb->st_gen = 0;
491 else
492 sb->st_gen = vap->va_gen;
493
494#if (S_BLKSIZE == 512)
495 /* Optimize this case */
496 sb->st_blocks = vap->va_bytes >> 9;
497#else
498 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
499#endif
500 return (0);
501}
502
503/*
504 * File table vnode ioctl routine.
505 */
506static int
507vn_ioctl(fp, com, data, p)
508 struct file *fp;
509 u_long com;
510 caddr_t data;
511 struct proc *p;
512{
513 register struct vnode *vp = ((struct vnode *)fp->f_data);
514 struct vattr vattr;
515 int error;
516
517 switch (vp->v_type) {
518
519 case VREG:
520 case VDIR:
521 if (com == FIONREAD) {
522 error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
523 if (error)
524 return (error);
525 *(int *)data = vattr.va_size - fp->f_offset;
526 return (0);
527 }
528 if (com == FIONBIO || com == FIOASYNC) /* XXX */
529 return (0); /* XXX */
530 /* fall into ... */
531
532 default:
533#if 0
534 return (ENOTTY);
535#endif
536 case VFIFO:
537 case VCHR:
538 case VBLK:
539 if (com == FIODTYPE) {
540 if (vp->v_type != VCHR && vp->v_type != VBLK)
541 return (ENOTTY);
542 *(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
543 return (0);
544 }
545 error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
546 if (error == 0 && com == TIOCSCTTY) {
547
548 /* Do nothing if reassigning same control tty */
549 if (p->p_session->s_ttyvp == vp)
550 return (0);
551
552 /* Get rid of reference to old control tty */
553 if (p->p_session->s_ttyvp)
554 vrele(p->p_session->s_ttyvp);
555
556 p->p_session->s_ttyvp = vp;
557 VREF(vp);
558 }
559 return (error);
560 }
561}
562
563/*
564 * File table vnode poll routine.
565 */
566static int
567vn_poll(fp, events, cred, p)
568 struct file *fp;
569 int events;
570 struct ucred *cred;
571 struct proc *p;
572{
573
574 return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p));
575}
576
577/*
578 * Check that the vnode is still valid, and if so
579 * acquire requested lock.
580 */
581int
582#ifndef DEBUG_LOCKS
583vn_lock(vp, flags, p)
584#else
585debug_vn_lock(vp, flags, p, filename, line)
586#endif
587 struct vnode *vp;
588 int flags;
589 struct proc *p;
590#ifdef DEBUG_LOCKS
591 const char *filename;
592 int line;
593#endif
594{
595 int error;
596
597 do {
598 if ((flags & LK_INTERLOCK) == 0)
599 simple_lock(&vp->v_interlock);
600 if (vp->v_flag & VXLOCK) {
601 vp->v_flag |= VXWANT;
602 simple_unlock(&vp->v_interlock);
603 tsleep((caddr_t)vp, PINOD, "vn_lock", 0);
604 error = ENOENT;
605 } else {
606#ifdef DEBUG_LOCKS
607 vp->filename = filename;
608 vp->line = line;
609#endif
610 error = VOP_LOCK(vp,
611 flags | LK_NOPAUSE | LK_INTERLOCK, p);
612 if (error == 0)
613 return (error);
614 }
615 flags &= ~LK_INTERLOCK;
616 } while (flags & LK_RETRY);
617 return (error);
618}
619
620/*
621 * File table vnode close routine.
622 */
623static int
624vn_closefile(fp, p)
625 struct file *fp;
626 struct proc *p;
627{
628
629 fp->f_ops = &badfileops;
630 return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
631 fp->f_cred, p));
632}