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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)vfs_vnops.c 8.2 (Berkeley) 1/21/94
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/7.4/sys/kern/vfs_vnops.c 206754 2010-04-17 11:38:18Z avg $");
39
40 #include "opt_mac.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/kdb.h>
47 #include <sys/stat.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/limits.h>
51 #include <sys/lock.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/bio.h>
57 #include <sys/buf.h>
58 #include <sys/filio.h>
59 #include <sys/sx.h>
60 #include <sys/ttycom.h>
61 #include <sys/conf.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
64
65 #include <security/mac/mac_framework.h>
66
67 static fo_rdwr_t vn_read;
68 static fo_rdwr_t vn_write;
69 static fo_ioctl_t vn_ioctl;
70 static fo_poll_t vn_poll;
71 static fo_kqfilter_t vn_kqfilter;
72 static fo_stat_t vn_statfile;
73 static fo_close_t vn_closefile;
74
75 struct fileops vnops = {
76 .fo_read = vn_read,
77 .fo_write = vn_write,
78 .fo_ioctl = vn_ioctl,
79 .fo_poll = vn_poll,
80 .fo_kqfilter = vn_kqfilter,
81 .fo_stat = vn_statfile,
82 .fo_close = vn_closefile,
83 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
84 };
85
86 int
87 vn_open(ndp, flagp, cmode, fp)
88 struct nameidata *ndp;
89 int *flagp, cmode;
90 struct file *fp;
91 {
92 struct thread *td = ndp->ni_cnd.cn_thread;
93
94 return (vn_open_cred(ndp, flagp, cmode, td->td_ucred, fp));
95 }
96
97 /*
98 * Common code for vnode open operations.
99 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
100 *
101 * Note that this does NOT free nameidata for the successful case,
102 * due to the NDINIT being done elsewhere.
103 */
104 int
105 vn_open_cred(ndp, flagp, cmode, cred, fp)
106 struct nameidata *ndp;
107 int *flagp, cmode;
108 struct ucred *cred;
109 struct file *fp;
110 {
111 struct vnode *vp;
112 struct mount *mp;
113 struct thread *td = ndp->ni_cnd.cn_thread;
114 struct vattr vat;
115 struct vattr *vap = &vat;
116 int mode, fmode, error;
117 int vfslocked, mpsafe;
118
119 mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
120 restart:
121 vfslocked = 0;
122 fmode = *flagp;
123 if (fmode & O_CREAT) {
124 ndp->ni_cnd.cn_nameiop = CREATE;
125 ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
126 MPSAFE | AUDITVNODE1;
127 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
128 ndp->ni_cnd.cn_flags |= FOLLOW;
129 bwillwrite();
130 if ((error = namei(ndp)) != 0)
131 return (error);
132 vfslocked = NDHASGIANT(ndp);
133 if (!mpsafe)
134 ndp->ni_cnd.cn_flags &= ~MPSAFE;
135 if (ndp->ni_vp == NULL) {
136 VATTR_NULL(vap);
137 vap->va_type = VREG;
138 vap->va_mode = cmode;
139 if (fmode & O_EXCL)
140 vap->va_vaflags |= VA_EXCLUSIVE;
141 if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
142 NDFREE(ndp, NDF_ONLY_PNBUF);
143 vput(ndp->ni_dvp);
144 VFS_UNLOCK_GIANT(vfslocked);
145 if ((error = vn_start_write(NULL, &mp,
146 V_XSLEEP | PCATCH)) != 0)
147 return (error);
148 goto restart;
149 }
150 #ifdef MAC
151 error = mac_check_vnode_create(cred, ndp->ni_dvp,
152 &ndp->ni_cnd, vap);
153 if (error == 0) {
154 #endif
155 VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
156 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
157 &ndp->ni_cnd, vap);
158 #ifdef MAC
159 }
160 #endif
161 vput(ndp->ni_dvp);
162 vn_finished_write(mp);
163 if (error) {
164 VFS_UNLOCK_GIANT(vfslocked);
165 NDFREE(ndp, NDF_ONLY_PNBUF);
166 return (error);
167 }
168 fmode &= ~O_TRUNC;
169 vp = ndp->ni_vp;
170 } else {
171 if (ndp->ni_dvp == ndp->ni_vp)
172 vrele(ndp->ni_dvp);
173 else
174 vput(ndp->ni_dvp);
175 ndp->ni_dvp = NULL;
176 vp = ndp->ni_vp;
177 if (fmode & O_EXCL) {
178 error = EEXIST;
179 goto bad;
180 }
181 fmode &= ~O_CREAT;
182 }
183 } else {
184 ndp->ni_cnd.cn_nameiop = LOOKUP;
185 ndp->ni_cnd.cn_flags = ISOPEN |
186 ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
187 LOCKLEAF | MPSAFE | AUDITVNODE1;
188 if (!(fmode & FWRITE))
189 ndp->ni_cnd.cn_flags |= LOCKSHARED;
190 if ((error = namei(ndp)) != 0)
191 return (error);
192 if (!mpsafe)
193 ndp->ni_cnd.cn_flags &= ~MPSAFE;
194 vfslocked = NDHASGIANT(ndp);
195 vp = ndp->ni_vp;
196 }
197 if (vp->v_type == VLNK) {
198 error = EMLINK;
199 goto bad;
200 }
201 if (vp->v_type == VSOCK) {
202 error = EOPNOTSUPP;
203 goto bad;
204 }
205 mode = 0;
206 if (fmode & (FWRITE | O_TRUNC)) {
207 if (vp->v_type == VDIR) {
208 error = EISDIR;
209 goto bad;
210 }
211 mode |= VWRITE;
212 }
213 if (fmode & FREAD)
214 mode |= VREAD;
215 if (fmode & O_APPEND)
216 mode |= VAPPEND;
217 #ifdef MAC
218 error = mac_check_vnode_open(cred, vp, mode);
219 if (error)
220 goto bad;
221 #endif
222 if ((fmode & O_CREAT) == 0) {
223 if (mode & VWRITE) {
224 error = vn_writechk(vp);
225 if (error)
226 goto bad;
227 }
228 if (mode) {
229 error = VOP_ACCESS(vp, mode, cred, td);
230 if (error)
231 goto bad;
232 }
233 }
234 if ((error = VOP_OPEN(vp, fmode, cred, td, fp)) != 0)
235 goto bad;
236
237 if (fmode & FWRITE)
238 vp->v_writecount++;
239 *flagp = fmode;
240 ASSERT_VOP_LOCKED(vp, "vn_open_cred");
241 if (!mpsafe)
242 VFS_UNLOCK_GIANT(vfslocked);
243 return (0);
244 bad:
245 NDFREE(ndp, NDF_ONLY_PNBUF);
246 vput(vp);
247 VFS_UNLOCK_GIANT(vfslocked);
248 *flagp = fmode;
249 ndp->ni_vp = NULL;
250 return (error);
251 }
252
253 /*
254 * Check for write permissions on the specified vnode.
255 * Prototype text segments cannot be written.
256 */
257 int
258 vn_writechk(vp)
259 register struct vnode *vp;
260 {
261
262 ASSERT_VOP_LOCKED(vp, "vn_writechk");
263 /*
264 * If there's shared text associated with
265 * the vnode, try to free it up once. If
266 * we fail, we can't allow writing.
267 */
268 if (vp->v_vflag & VV_TEXT)
269 return (ETXTBSY);
270
271 return (0);
272 }
273
274 /*
275 * Vnode close call
276 */
277 int
278 vn_close(vp, flags, file_cred, td)
279 register struct vnode *vp;
280 int flags;
281 struct ucred *file_cred;
282 struct thread *td;
283 {
284 struct mount *mp;
285 int error, lock_flags;
286
287 if (!(flags & FWRITE) && vp->v_mount != NULL &&
288 vp->v_mount->mnt_kern_flag & MNTK_EXTENDED_SHARED)
289 lock_flags = LK_SHARED;
290 else
291 lock_flags = LK_EXCLUSIVE;
292
293 VFS_ASSERT_GIANT(vp->v_mount);
294
295 vn_start_write(vp, &mp, V_WAIT);
296 vn_lock(vp, lock_flags | LK_RETRY, td);
297 if (flags & FWRITE) {
298 VNASSERT(vp->v_writecount > 0, vp,
299 ("vn_close: negative writecount"));
300 vp->v_writecount--;
301 }
302 error = VOP_CLOSE(vp, flags, file_cred, td);
303 vput(vp);
304 vn_finished_write(mp);
305 return (error);
306 }
307
308 /*
309 * Sequential heuristic - detect sequential operation
310 */
311 static __inline
312 int
313 sequential_heuristic(struct uio *uio, struct file *fp)
314 {
315
316 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
317 uio->uio_offset == fp->f_nextoff) {
318 /*
319 * XXX we assume that the filesystem block size is
320 * the default. Not true, but still gives us a pretty
321 * good indicator of how sequential the read operations
322 * are.
323 */
324 fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
325 if (fp->f_seqcount > IO_SEQMAX)
326 fp->f_seqcount = IO_SEQMAX;
327 return(fp->f_seqcount << IO_SEQSHIFT);
328 }
329
330 /*
331 * Not sequential, quick draw-down of seqcount
332 */
333 if (fp->f_seqcount > 1)
334 fp->f_seqcount = 1;
335 else
336 fp->f_seqcount = 0;
337 return(0);
338 }
339
340 /*
341 * Package up an I/O request on a vnode into a uio and do it.
342 */
343 int
344 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
345 aresid, td)
346 enum uio_rw rw;
347 struct vnode *vp;
348 void *base;
349 int len;
350 off_t offset;
351 enum uio_seg segflg;
352 int ioflg;
353 struct ucred *active_cred;
354 struct ucred *file_cred;
355 int *aresid;
356 struct thread *td;
357 {
358 struct uio auio;
359 struct iovec aiov;
360 struct mount *mp;
361 struct ucred *cred;
362 int error, lock_flags;
363
364 VFS_ASSERT_GIANT(vp->v_mount);
365
366 if ((ioflg & IO_NODELOCKED) == 0) {
367 mp = NULL;
368 if (rw == UIO_WRITE) {
369 if (vp->v_type != VCHR &&
370 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
371 != 0)
372 return (error);
373 if (MNT_SHARED_WRITES(mp) ||
374 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) {
375 lock_flags = LK_SHARED;
376 } else {
377 lock_flags = LK_EXCLUSIVE;
378 }
379 vn_lock(vp, lock_flags | LK_RETRY, td);
380 } else
381 vn_lock(vp, LK_SHARED | LK_RETRY, td);
382
383 }
384 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
385 auio.uio_iov = &aiov;
386 auio.uio_iovcnt = 1;
387 aiov.iov_base = base;
388 aiov.iov_len = len;
389 auio.uio_resid = len;
390 auio.uio_offset = offset;
391 auio.uio_segflg = segflg;
392 auio.uio_rw = rw;
393 auio.uio_td = td;
394 error = 0;
395 #ifdef MAC
396 if ((ioflg & IO_NOMACCHECK) == 0) {
397 if (rw == UIO_READ)
398 error = mac_check_vnode_read(active_cred, file_cred,
399 vp);
400 else
401 error = mac_check_vnode_write(active_cred, file_cred,
402 vp);
403 }
404 #endif
405 if (error == 0) {
406 if (file_cred)
407 cred = file_cred;
408 else
409 cred = active_cred;
410 if (rw == UIO_READ)
411 error = VOP_READ(vp, &auio, ioflg, cred);
412 else
413 error = VOP_WRITE(vp, &auio, ioflg, cred);
414 }
415 if (aresid)
416 *aresid = auio.uio_resid;
417 else
418 if (auio.uio_resid && error == 0)
419 error = EIO;
420 if ((ioflg & IO_NODELOCKED) == 0) {
421 if (rw == UIO_WRITE && vp->v_type != VCHR)
422 vn_finished_write(mp);
423 VOP_UNLOCK(vp, 0, td);
424 }
425 return (error);
426 }
427
428 /*
429 * Package up an I/O request on a vnode into a uio and do it. The I/O
430 * request is split up into smaller chunks and we try to avoid saturating
431 * the buffer cache while potentially holding a vnode locked, so we
432 * check bwillwrite() before calling vn_rdwr(). We also call uio_yield()
433 * to give other processes a chance to lock the vnode (either other processes
434 * core'ing the same binary, or unrelated processes scanning the directory).
435 */
436 int
437 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
438 file_cred, aresid, td)
439 enum uio_rw rw;
440 struct vnode *vp;
441 void *base;
442 size_t len;
443 off_t offset;
444 enum uio_seg segflg;
445 int ioflg;
446 struct ucred *active_cred;
447 struct ucred *file_cred;
448 size_t *aresid;
449 struct thread *td;
450 {
451 int error = 0;
452 int iaresid;
453
454 VFS_ASSERT_GIANT(vp->v_mount);
455
456 do {
457 int chunk;
458
459 /*
460 * Force `offset' to a multiple of MAXBSIZE except possibly
461 * for the first chunk, so that filesystems only need to
462 * write full blocks except possibly for the first and last
463 * chunks.
464 */
465 chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
466
467 if (chunk > len)
468 chunk = len;
469 if (rw != UIO_READ && vp->v_type == VREG)
470 bwillwrite();
471 iaresid = 0;
472 error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
473 ioflg, active_cred, file_cred, &iaresid, td);
474 len -= chunk; /* aresid calc already includes length */
475 if (error)
476 break;
477 offset += chunk;
478 base = (char *)base + chunk;
479 uio_yield();
480 } while (len);
481 if (aresid)
482 *aresid = len + iaresid;
483 return (error);
484 }
485
486 /*
487 * File table vnode read routine.
488 */
489 static int
490 vn_read(fp, uio, active_cred, flags, td)
491 struct file *fp;
492 struct uio *uio;
493 struct ucred *active_cred;
494 struct thread *td;
495 int flags;
496 {
497 struct vnode *vp;
498 int error, ioflag;
499 int vfslocked;
500
501 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
502 uio->uio_td, td));
503 vp = fp->f_vnode;
504 ioflag = 0;
505 if (fp->f_flag & FNONBLOCK)
506 ioflag |= IO_NDELAY;
507 if (fp->f_flag & O_DIRECT)
508 ioflag |= IO_DIRECT;
509 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
510 VOP_LEASE(vp, td, fp->f_cred, LEASE_READ);
511 /*
512 * According to McKusick the vn lock was protecting f_offset here.
513 * It is now protected by the FOFFSET_LOCKED flag.
514 */
515 if ((flags & FOF_OFFSET) == 0) {
516 FILE_LOCK(fp);
517 while(fp->f_vnread_flags & FOFFSET_LOCKED) {
518 fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
519 msleep(&fp->f_vnread_flags,fp->f_mtxp,PUSER -1,"vnread offlock",0);
520 }
521 fp->f_vnread_flags |= FOFFSET_LOCKED;
522 FILE_UNLOCK(fp);
523 vn_lock(vp, LK_SHARED | LK_RETRY, td);
524 uio->uio_offset = fp->f_offset;
525 } else
526 vn_lock(vp, LK_SHARED | LK_RETRY, td);
527
528 ioflag |= sequential_heuristic(uio, fp);
529
530 #ifdef MAC
531 error = mac_check_vnode_read(active_cred, fp->f_cred, vp);
532 if (error == 0)
533 #endif
534 error = VOP_READ(vp, uio, ioflag, fp->f_cred);
535 if ((flags & FOF_OFFSET) == 0) {
536 fp->f_offset = uio->uio_offset;
537 FILE_LOCK(fp);
538 if (fp->f_vnread_flags & FOFFSET_LOCK_WAITING)
539 wakeup(&fp->f_vnread_flags);
540 fp->f_vnread_flags = 0;
541 FILE_UNLOCK(fp);
542 }
543 fp->f_nextoff = uio->uio_offset;
544 VOP_UNLOCK(vp, 0, td);
545 VFS_UNLOCK_GIANT(vfslocked);
546 return (error);
547 }
548
549 /*
550 * File table vnode write routine.
551 */
552 static int
553 vn_write(fp, uio, active_cred, flags, td)
554 struct file *fp;
555 struct uio *uio;
556 struct ucred *active_cred;
557 struct thread *td;
558 int flags;
559 {
560 struct vnode *vp;
561 struct mount *mp;
562 int error, ioflag, lock_flags;
563 int vfslocked;
564
565 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
566 uio->uio_td, td));
567 vp = fp->f_vnode;
568 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
569 if (vp->v_type == VREG)
570 bwillwrite();
571 ioflag = IO_UNIT;
572 if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
573 ioflag |= IO_APPEND;
574 if (fp->f_flag & FNONBLOCK)
575 ioflag |= IO_NDELAY;
576 if (fp->f_flag & O_DIRECT)
577 ioflag |= IO_DIRECT;
578 if ((fp->f_flag & O_FSYNC) ||
579 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
580 ioflag |= IO_SYNC;
581 mp = NULL;
582 if (vp->v_type != VCHR &&
583 (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
584 goto unlock;
585
586 if ((MNT_SHARED_WRITES(mp) ||
587 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) &&
588 (flags & FOF_OFFSET) != 0) {
589 lock_flags = LK_SHARED;
590 } else {
591 lock_flags = LK_EXCLUSIVE;
592 }
593
594 vn_lock(vp, lock_flags | LK_RETRY, td);
595 if ((flags & FOF_OFFSET) == 0)
596 uio->uio_offset = fp->f_offset;
597 ioflag |= sequential_heuristic(uio, fp);
598 #ifdef MAC
599 error = mac_check_vnode_write(active_cred, fp->f_cred, vp);
600 if (error == 0)
601 #endif
602 error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
603 if ((flags & FOF_OFFSET) == 0)
604 fp->f_offset = uio->uio_offset;
605 fp->f_nextoff = uio->uio_offset;
606 VOP_UNLOCK(vp, 0, td);
607 if (vp->v_type != VCHR)
608 vn_finished_write(mp);
609 unlock:
610 VFS_UNLOCK_GIANT(vfslocked);
611 return (error);
612 }
613
614 /*
615 * File table vnode stat routine.
616 */
617 static int
618 vn_statfile(fp, sb, active_cred, td)
619 struct file *fp;
620 struct stat *sb;
621 struct ucred *active_cred;
622 struct thread *td;
623 {
624 struct vnode *vp = fp->f_vnode;
625 int vfslocked;
626 int error;
627
628 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
629 vn_lock(vp, LK_SHARED | LK_RETRY, td);
630 error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
631 VOP_UNLOCK(vp, 0, td);
632 VFS_UNLOCK_GIANT(vfslocked);
633
634 return (error);
635 }
636
637 /*
638 * Stat a vnode; implementation for the stat syscall
639 */
640 int
641 vn_stat(vp, sb, active_cred, file_cred, td)
642 struct vnode *vp;
643 register struct stat *sb;
644 struct ucred *active_cred;
645 struct ucred *file_cred;
646 struct thread *td;
647 {
648 struct vattr vattr;
649 register struct vattr *vap;
650 int error;
651 u_short mode;
652
653 #ifdef MAC
654 error = mac_check_vnode_stat(active_cred, file_cred, vp);
655 if (error)
656 return (error);
657 #endif
658
659 vap = &vattr;
660
661 /*
662 * Initialize defaults for new and unusual fields, so that file
663 * systems which don't support these fields don't need to know
664 * about them.
665 */
666 vap->va_birthtime.tv_sec = -1;
667 vap->va_birthtime.tv_nsec = 0;
668 vap->va_fsid = VNOVAL;
669 vap->va_rdev = NODEV;
670
671 error = VOP_GETATTR(vp, vap, active_cred, td);
672 if (error)
673 return (error);
674
675 /*
676 * Zero the spare stat fields
677 */
678 bzero(sb, sizeof *sb);
679
680 /*
681 * Copy from vattr table
682 */
683 if (vap->va_fsid != VNOVAL)
684 sb->st_dev = vap->va_fsid;
685 else
686 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
687 sb->st_ino = vap->va_fileid;
688 mode = vap->va_mode;
689 switch (vap->va_type) {
690 case VREG:
691 mode |= S_IFREG;
692 break;
693 case VDIR:
694 mode |= S_IFDIR;
695 break;
696 case VBLK:
697 mode |= S_IFBLK;
698 break;
699 case VCHR:
700 mode |= S_IFCHR;
701 break;
702 case VLNK:
703 mode |= S_IFLNK;
704 break;
705 case VSOCK:
706 mode |= S_IFSOCK;
707 break;
708 case VFIFO:
709 mode |= S_IFIFO;
710 break;
711 default:
712 return (EBADF);
713 };
714 sb->st_mode = mode;
715 sb->st_nlink = vap->va_nlink;
716 sb->st_uid = vap->va_uid;
717 sb->st_gid = vap->va_gid;
718 sb->st_rdev = vap->va_rdev;
719 if (vap->va_size > OFF_MAX)
720 return (EOVERFLOW);
721 sb->st_size = vap->va_size;
722 sb->st_atimespec = vap->va_atime;
723 sb->st_mtimespec = vap->va_mtime;
724 sb->st_ctimespec = vap->va_ctime;
725 sb->st_birthtimespec = vap->va_birthtime;
726
727 /*
728 * According to www.opengroup.org, the meaning of st_blksize is
729 * "a filesystem-specific preferred I/O block size for this
730 * object. In some filesystem types, this may vary from file
731 * to file"
732 * Use miminum/default of PAGE_SIZE (e.g. for VCHR).
733 */
734
735 sb->st_blksize = max(PAGE_SIZE, vap->va_blocksize);
736
737 sb->st_flags = vap->va_flags;
738 if (priv_check(td, PRIV_VFS_GENERATION))
739 sb->st_gen = 0;
740 else
741 sb->st_gen = vap->va_gen;
742
743 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
744 return (0);
745 }
746
747 /*
748 * File table vnode ioctl routine.
749 */
750 static int
751 vn_ioctl(fp, com, data, active_cred, td)
752 struct file *fp;
753 u_long com;
754 void *data;
755 struct ucred *active_cred;
756 struct thread *td;
757 {
758 struct vnode *vp = fp->f_vnode;
759 struct vattr vattr;
760 int vfslocked;
761 int error;
762
763 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
764 error = ENOTTY;
765 switch (vp->v_type) {
766 case VREG:
767 case VDIR:
768 if (com == FIONREAD) {
769 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
770 error = VOP_GETATTR(vp, &vattr, active_cred, td);
771 VOP_UNLOCK(vp, 0, td);
772 if (!error)
773 *(int *)data = vattr.va_size - fp->f_offset;
774 }
775 if (com == FIONBIO || com == FIOASYNC) /* XXX */
776 error = 0;
777 else
778 error = VOP_IOCTL(vp, com, data, fp->f_flag,
779 active_cred, td);
780 break;
781
782 default:
783 break;
784 }
785 VFS_UNLOCK_GIANT(vfslocked);
786 return (error);
787 }
788
789 /*
790 * File table vnode poll routine.
791 */
792 static int
793 vn_poll(fp, events, active_cred, td)
794 struct file *fp;
795 int events;
796 struct ucred *active_cred;
797 struct thread *td;
798 {
799 struct vnode *vp;
800 int vfslocked;
801 int error;
802
803 vp = fp->f_vnode;
804 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
805 #ifdef MAC
806 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
807 error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
808 VOP_UNLOCK(vp, 0, td);
809 if (!error)
810 #endif
811
812 error = VOP_POLL(vp, events, fp->f_cred, td);
813 VFS_UNLOCK_GIANT(vfslocked);
814 return (error);
815 }
816
817 /*
818 * Check that the vnode is still valid, and if so
819 * acquire requested lock.
820 */
821 int
822 _vn_lock(struct vnode *vp, int flags, struct thread *td, char *file, int line)
823 {
824 int error;
825
826 do {
827 if ((flags & LK_INTERLOCK) == 0)
828 VI_LOCK(vp);
829 #ifdef DEBUG_VFS_LOCKS
830 KASSERT(vp->v_holdcnt != 0,
831 ("vn_lock %p: zero hold count", vp));
832 #endif
833 if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
834 vp->v_iflag & VI_DOOMED) {
835 VI_UNLOCK(vp);
836 return (ENOENT);
837 }
838 /*
839 * Just polling to check validity.
840 */
841 if ((flags & LK_TYPE_MASK) == 0) {
842 VI_UNLOCK(vp);
843 return (0);
844 }
845 /*
846 * lockmgr drops interlock before it will return for
847 * any reason. So force the code above to relock it.
848 */
849 error = VOP_LOCK1(vp, flags | LK_INTERLOCK, td, file, line);
850 flags &= ~LK_INTERLOCK;
851 KASSERT((flags & LK_RETRY) == 0 || error == 0,
852 ("LK_RETRY set with incompatible flags %d\n", flags));
853 /*
854 * Callers specify LK_RETRY if they wish to get dead vnodes.
855 * If RETRY is not set, we return ENOENT instead.
856 */
857 if (error == 0 && vp->v_iflag & VI_DOOMED &&
858 (flags & LK_RETRY) == 0) {
859 VOP_UNLOCK(vp, 0, td);
860 error = ENOENT;
861 break;
862 }
863 } while (flags & LK_RETRY && error != 0);
864 return (error);
865 }
866
867 /*
868 * File table vnode close routine.
869 */
870 static int
871 vn_closefile(fp, td)
872 struct file *fp;
873 struct thread *td;
874 {
875 struct vnode *vp;
876 struct flock lf;
877 int vfslocked;
878 int error;
879
880 vp = fp->f_vnode;
881
882 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
883 if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
884 lf.l_whence = SEEK_SET;
885 lf.l_start = 0;
886 lf.l_len = 0;
887 lf.l_type = F_UNLCK;
888 (void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
889 }
890
891 fp->f_ops = &badfileops;
892
893 error = vn_close(vp, fp->f_flag, fp->f_cred, td);
894 VFS_UNLOCK_GIANT(vfslocked);
895 return (error);
896 }
897
898 /*
899 * Preparing to start a filesystem write operation. If the operation is
900 * permitted, then we bump the count of operations in progress and
901 * proceed. If a suspend request is in progress, we wait until the
902 * suspension is over, and then proceed.
903 */
904 int
905 vn_start_write(vp, mpp, flags)
906 struct vnode *vp;
907 struct mount **mpp;
908 int flags;
909 {
910 struct mount *mp;
911 int error;
912
913 error = 0;
914 /*
915 * If a vnode is provided, get and return the mount point that
916 * to which it will write.
917 */
918 if (vp != NULL) {
919 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
920 *mpp = NULL;
921 if (error != EOPNOTSUPP)
922 return (error);
923 return (0);
924 }
925 }
926 if ((mp = *mpp) == NULL)
927 return (0);
928 MNT_ILOCK(mp);
929 if (vp == NULL)
930 MNT_REF(mp);
931 /*
932 * Check on status of suspension.
933 */
934 if ((curthread->td_pflags & TDP_IGNSUSP) == 0 ||
935 mp->mnt_susp_owner != curthread) {
936 while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
937 if (flags & V_NOWAIT) {
938 error = EWOULDBLOCK;
939 goto unlock;
940 }
941 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
942 (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
943 if (error)
944 goto unlock;
945 }
946 }
947 if (flags & V_XSLEEP)
948 goto unlock;
949 mp->mnt_writeopcount++;
950 unlock:
951 MNT_REL(mp);
952 MNT_IUNLOCK(mp);
953 return (error);
954 }
955
956 /*
957 * Secondary suspension. Used by operations such as vop_inactive
958 * routines that are needed by the higher level functions. These
959 * are allowed to proceed until all the higher level functions have
960 * completed (indicated by mnt_writeopcount dropping to zero). At that
961 * time, these operations are halted until the suspension is over.
962 */
963 int
964 vn_start_secondary_write(vp, mpp, flags)
965 struct vnode *vp;
966 struct mount **mpp;
967 int flags;
968 {
969 struct mount *mp;
970 int error;
971
972 retry:
973 if (vp != NULL) {
974 if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
975 *mpp = NULL;
976 if (error != EOPNOTSUPP)
977 return (error);
978 return (0);
979 }
980 }
981 /*
982 * If we are not suspended or have not yet reached suspended
983 * mode, then let the operation proceed.
984 */
985 if ((mp = *mpp) == NULL)
986 return (0);
987 MNT_ILOCK(mp);
988 if (vp == NULL)
989 MNT_REF(mp);
990 if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
991 mp->mnt_secondary_writes++;
992 mp->mnt_secondary_accwrites++;
993 MNT_REL(mp);
994 MNT_IUNLOCK(mp);
995 return (0);
996 }
997 if (flags & V_NOWAIT) {
998 MNT_REL(mp);
999 MNT_IUNLOCK(mp);
1000 return (EWOULDBLOCK);
1001 }
1002 /*
1003 * Wait for the suspension to finish.
1004 */
1005 error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1006 (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1007 vfs_rel(mp);
1008 if (error == 0)
1009 goto retry;
1010 return (error);
1011 }
1012
1013 /*
1014 * Filesystem write operation has completed. If we are suspending and this
1015 * operation is the last one, notify the suspender that the suspension is
1016 * now in effect.
1017 */
1018 void
1019 vn_finished_write(mp)
1020 struct mount *mp;
1021 {
1022 if (mp == NULL)
1023 return;
1024 MNT_ILOCK(mp);
1025 mp->mnt_writeopcount--;
1026 if (mp->mnt_writeopcount < 0)
1027 panic("vn_finished_write: neg cnt");
1028 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1029 mp->mnt_writeopcount <= 0)
1030 wakeup(&mp->mnt_writeopcount);
1031 MNT_IUNLOCK(mp);
1032 }
1033
1034
1035 /*
1036 * Filesystem secondary write operation has completed. If we are
1037 * suspending and this operation is the last one, notify the suspender
1038 * that the suspension is now in effect.
1039 */
1040 void
1041 vn_finished_secondary_write(mp)
1042 struct mount *mp;
1043 {
1044 if (mp == NULL)
1045 return;
1046 MNT_ILOCK(mp);
1047 mp->mnt_secondary_writes--;
1048 if (mp->mnt_secondary_writes < 0)
1049 panic("vn_finished_secondary_write: neg cnt");
1050 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1051 mp->mnt_secondary_writes <= 0)
1052 wakeup(&mp->mnt_secondary_writes);
1053 MNT_IUNLOCK(mp);
1054 }
1055
1056
1057
1058 /*
1059 * Request a filesystem to suspend write operations.
1060 */
1061 int
1062 vfs_write_suspend(mp)
1063 struct mount *mp;
1064 {
1065 struct thread *td = curthread;
1066 int error;
1067
1068 MNT_ILOCK(mp);
1069 if (mp->mnt_susp_owner == curthread) {
1070 MNT_IUNLOCK(mp);
1071 return (EALREADY);
1072 }
1073 while (mp->mnt_kern_flag & MNTK_SUSPEND)
1074 msleep(&mp->mnt_flag, MNT_MTX(mp), PUSER - 1, "wsuspfs", 0);
1075 mp->mnt_kern_flag |= MNTK_SUSPEND;
1076 mp->mnt_susp_owner = curthread;
1077 if (mp->mnt_writeopcount > 0)
1078 (void) msleep(&mp->mnt_writeopcount,
1079 MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1080 else
1081 MNT_IUNLOCK(mp);
1082 if ((error = VFS_SYNC(mp, MNT_SUSPEND, td)) != 0)
1083 vfs_write_resume(mp);
1084 return (error);
1085 }
1086
1087 /*
1088 * Request a filesystem to resume write operations.
1089 */
1090 void
1091 vfs_write_resume(mp)
1092 struct mount *mp;
1093 {
1094
1095 MNT_ILOCK(mp);
1096 if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1097 KASSERT(mp->mnt_susp_owner == curthread, ("mnt_susp_owner"));
1098 mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1099 MNTK_SUSPENDED);
1100 mp->mnt_susp_owner = NULL;
1101 wakeup(&mp->mnt_writeopcount);
1102 wakeup(&mp->mnt_flag);
1103 curthread->td_pflags &= ~TDP_IGNSUSP;
1104 MNT_IUNLOCK(mp);
1105 VFS_SUSP_CLEAN(mp);
1106 } else
1107 MNT_IUNLOCK(mp);
1108 }
1109
1110 /*
1111 * Implement kqueues for files by translating it to vnode operation.
1112 */
1113 static int
1114 vn_kqfilter(struct file *fp, struct knote *kn)
1115 {
1116 int vfslocked;
1117 int error;
1118
1119 vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1120 error = VOP_KQFILTER(fp->f_vnode, kn);
1121 VFS_UNLOCK_GIANT(vfslocked);
1122
1123 return error;
1124 }
1125
1126 /*
1127 * Simplified in-kernel wrapper calls for extended attribute access.
1128 * Both calls pass in a NULL credential, authorizing as "kernel" access.
1129 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1130 */
1131 int
1132 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1133 const char *attrname, int *buflen, char *buf, struct thread *td)
1134 {
1135 struct uio auio;
1136 struct iovec iov;
1137 int error;
1138
1139 iov.iov_len = *buflen;
1140 iov.iov_base = buf;
1141
1142 auio.uio_iov = &iov;
1143 auio.uio_iovcnt = 1;
1144 auio.uio_rw = UIO_READ;
1145 auio.uio_segflg = UIO_SYSSPACE;
1146 auio.uio_td = td;
1147 auio.uio_offset = 0;
1148 auio.uio_resid = *buflen;
1149
1150 if ((ioflg & IO_NODELOCKED) == 0)
1151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1152
1153 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1154
1155 /* authorize attribute retrieval as kernel */
1156 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1157 td);
1158
1159 if ((ioflg & IO_NODELOCKED) == 0)
1160 VOP_UNLOCK(vp, 0, td);
1161
1162 if (error == 0) {
1163 *buflen = *buflen - auio.uio_resid;
1164 }
1165
1166 return (error);
1167 }
1168
1169 /*
1170 * XXX failure mode if partially written?
1171 */
1172 int
1173 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1174 const char *attrname, int buflen, char *buf, struct thread *td)
1175 {
1176 struct uio auio;
1177 struct iovec iov;
1178 struct mount *mp;
1179 int error;
1180
1181 iov.iov_len = buflen;
1182 iov.iov_base = buf;
1183
1184 auio.uio_iov = &iov;
1185 auio.uio_iovcnt = 1;
1186 auio.uio_rw = UIO_WRITE;
1187 auio.uio_segflg = UIO_SYSSPACE;
1188 auio.uio_td = td;
1189 auio.uio_offset = 0;
1190 auio.uio_resid = buflen;
1191
1192 if ((ioflg & IO_NODELOCKED) == 0) {
1193 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1194 return (error);
1195 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1196 }
1197
1198 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1199
1200 /* authorize attribute setting as kernel */
1201 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1202
1203 if ((ioflg & IO_NODELOCKED) == 0) {
1204 vn_finished_write(mp);
1205 VOP_UNLOCK(vp, 0, td);
1206 }
1207
1208 return (error);
1209 }
1210
1211 int
1212 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1213 const char *attrname, struct thread *td)
1214 {
1215 struct mount *mp;
1216 int error;
1217
1218 if ((ioflg & IO_NODELOCKED) == 0) {
1219 if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1220 return (error);
1221 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1222 }
1223
1224 ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1225
1226 /* authorize attribute removal as kernel */
1227 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1228 if (error == EOPNOTSUPP)
1229 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1230 NULL, td);
1231
1232 if ((ioflg & IO_NODELOCKED) == 0) {
1233 vn_finished_write(mp);
1234 VOP_UNLOCK(vp, 0, td);
1235 }
1236
1237 return (error);
1238 }
1239
1240 int
1241 vn_vget_ino(struct vnode *vp, ino_t ino, int lkflags, struct vnode **rvp)
1242 {
1243 struct mount *mp;
1244 struct thread *td;
1245 int ltype, error;
1246
1247 mp = vp->v_mount;
1248 td = curthread;
1249 ltype = VOP_ISLOCKED(vp, td);
1250 KASSERT(ltype == LK_EXCLUSIVE || ltype == LK_SHARED,
1251 ("vn_vget_ino: vp not locked"));
1252 error = vfs_busy(mp, LK_NOWAIT, NULL, td);
1253 if (error != 0) {
1254 VOP_UNLOCK(vp, 0, td);
1255 error = vfs_busy(mp, 0, NULL, td);
1256 vn_lock(vp, ltype | LK_RETRY, td);
1257 if (error != 0)
1258 return (ENOENT);
1259 if (vp->v_iflag & VI_DOOMED) {
1260 vfs_unbusy(mp, td);
1261 return (ENOENT);
1262 }
1263 }
1264 VOP_UNLOCK(vp, 0, td);
1265 error = VFS_VGET(mp, ino, lkflags, rvp);
1266 vfs_unbusy(mp, td);
1267 vn_lock(vp, ltype | LK_RETRY, td);
1268 if (vp->v_iflag & VI_DOOMED) {
1269 if (error == 0)
1270 vput(*rvp);
1271 error = ENOENT;
1272 }
1273 return (error);
1274 }
Cache object: 8ea93462102200bfbce171723fc7e91e
|