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