2 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * from: @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
60 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
61 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
67 #include <sys/param.h>
69 #include <sys/systm.h>
72 #include <sys/extattr.h>
73 #include <sys/kernel.h>
74 #include <sys/limits.h>
75 #include <sys/malloc.h>
76 #include <sys/mount.h>
78 #include <sys/rwlock.h>
80 #include <sys/vmmeter.h>
81 #include <sys/vnode.h>
84 #include <vm/vm_param.h>
85 #include <vm/vm_extern.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vnode_pager.h>
91 #include <ufs/ufs/extattr.h>
92 #include <ufs/ufs/quota.h>
93 #include <ufs/ufs/inode.h>
94 #include <ufs/ufs/ufs_extern.h>
95 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ffs/fs.h>
98 #include <ufs/ffs/ffs_extern.h>
99 #include "opt_directio.h"
103 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
105 static vop_fsync_t ffs_fsync;
106 static vop_lock1_t ffs_lock;
107 static vop_read_t ffs_read;
108 static vop_write_t ffs_write;
109 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
110 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
112 static vop_strategy_t ffsext_strategy;
113 static vop_closeextattr_t ffs_closeextattr;
114 static vop_deleteextattr_t ffs_deleteextattr;
115 static vop_getextattr_t ffs_getextattr;
116 static vop_listextattr_t ffs_listextattr;
117 static vop_openextattr_t ffs_openextattr;
118 static vop_setextattr_t ffs_setextattr;
119 static vop_vptofh_t ffs_vptofh;
122 /* Global vfs data structures for ufs. */
123 struct vop_vector ffs_vnodeops1 = {
124 .vop_default = &ufs_vnodeops,
125 .vop_fsync = ffs_fsync,
126 .vop_getpages = vnode_pager_local_getpages,
127 .vop_getpages_async = vnode_pager_local_getpages_async,
128 .vop_lock1 = ffs_lock,
129 .vop_read = ffs_read,
130 .vop_reallocblks = ffs_reallocblks,
131 .vop_write = ffs_write,
132 .vop_vptofh = ffs_vptofh,
135 struct vop_vector ffs_fifoops1 = {
136 .vop_default = &ufs_fifoops,
137 .vop_fsync = ffs_fsync,
138 .vop_reallocblks = ffs_reallocblks, /* XXX: really ??? */
139 .vop_vptofh = ffs_vptofh,
142 /* Global vfs data structures for ufs. */
143 struct vop_vector ffs_vnodeops2 = {
144 .vop_default = &ufs_vnodeops,
145 .vop_fsync = ffs_fsync,
146 .vop_getpages = vnode_pager_local_getpages,
147 .vop_getpages_async = vnode_pager_local_getpages_async,
148 .vop_lock1 = ffs_lock,
149 .vop_read = ffs_read,
150 .vop_reallocblks = ffs_reallocblks,
151 .vop_write = ffs_write,
152 .vop_closeextattr = ffs_closeextattr,
153 .vop_deleteextattr = ffs_deleteextattr,
154 .vop_getextattr = ffs_getextattr,
155 .vop_listextattr = ffs_listextattr,
156 .vop_openextattr = ffs_openextattr,
157 .vop_setextattr = ffs_setextattr,
158 .vop_vptofh = ffs_vptofh,
161 struct vop_vector ffs_fifoops2 = {
162 .vop_default = &ufs_fifoops,
163 .vop_fsync = ffs_fsync,
164 .vop_lock1 = ffs_lock,
165 .vop_reallocblks = ffs_reallocblks,
166 .vop_strategy = ffsext_strategy,
167 .vop_closeextattr = ffs_closeextattr,
168 .vop_deleteextattr = ffs_deleteextattr,
169 .vop_getextattr = ffs_getextattr,
170 .vop_listextattr = ffs_listextattr,
171 .vop_openextattr = ffs_openextattr,
172 .vop_setextattr = ffs_setextattr,
173 .vop_vptofh = ffs_vptofh,
177 * Synch an open file.
181 ffs_fsync(struct vop_fsync_args *ap)
190 error = ffs_syncvnode(vp, ap->a_waitfor, 0);
193 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
194 error = softdep_fsync(vp);
199 * The softdep_fsync() function may drop vp lock,
200 * allowing for dirty buffers to reappear on the
201 * bo_dirty list. Recheck and resync as needed.
204 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
205 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
215 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
222 int error, wait, passes;
225 ip->i_flag &= ~IN_NEEDSYNC;
229 * When doing MNT_WAIT we must first flush all dependencies
232 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
233 (error = softdep_sync_metadata(vp)) != 0)
237 * Flush all dirty buffers associated with a vnode.
241 wait = 0; /* Always do an async pass first. */
242 lbn = lblkno(ip->i_fs, (ip->i_size + ip->i_fs->fs_bsize - 1));
245 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
246 bp->b_vflags &= ~BV_SCANNED;
247 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
249 * Reasons to skip this buffer: it has already been considered
250 * on this pass, the buffer has dependencies that will cause
251 * it to be redirtied and it has not already been deferred,
252 * or it is already being written.
254 if ((bp->b_vflags & BV_SCANNED) != 0)
256 bp->b_vflags |= BV_SCANNED;
257 /* Flush indirects in order. */
258 if (waitfor == MNT_WAIT && bp->b_lblkno <= -NDADDR &&
259 lbn_level(bp->b_lblkno) >= passes)
261 if (bp->b_lblkno > lbn)
262 panic("ffs_syncvnode: syncing truncated data.");
263 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
265 } else if (wait != 0) {
267 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
268 BO_LOCKPTR(bo)) != 0) {
269 bp->b_vflags &= ~BV_SCANNED;
274 if ((bp->b_flags & B_DELWRI) == 0)
275 panic("ffs_fsync: not dirty");
277 * Check for dependencies and potentially complete them.
279 if (!LIST_EMPTY(&bp->b_dep) &&
280 (error = softdep_sync_buf(vp, bp,
281 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
283 if (error != EBUSY) {
287 /* If we deferred once, don't defer again. */
288 if ((bp->b_flags & B_DEFERRED) == 0) {
289 bp->b_flags |= B_DEFERRED;
296 if ((error = bwrite(bp)) != 0)
298 } else if ((bp->b_flags & B_CLUSTEROK)) {
299 (void) vfs_bio_awrite(bp);
306 * Since we may have slept during the I/O, we need
307 * to start from a known point.
310 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
312 if (waitfor != MNT_WAIT) {
314 if ((flags & NO_INO_UPDT) != 0)
317 return (ffs_update(vp, 0));
319 /* Drain IO to see if we're done. */
320 bufobj_wwait(bo, 0, 0);
322 * Block devices associated with filesystems may have new I/O
323 * requests posted for them even if the vnode is locked, so no
324 * amount of trying will get them clean. We make several passes
327 * Regular files may need multiple passes to flush all dependency
328 * work as it is possible that we must write once per indirect
329 * level, once for the leaf, and once for the inode and each of
330 * these will be done with one sync and one async pass.
332 if (bo->bo_dirty.bv_cnt > 0) {
333 /* Write the inode after sync passes to flush deps. */
334 if (wait && DOINGSOFTDEP(vp) && (flags & NO_INO_UPDT) == 0) {
339 /* switch between sync/async. */
341 if (wait == 1 || ++passes < NIADDR + 2)
344 if (!vn_isdisk(vp, NULL))
345 vprint("ffs_fsync: dirty", vp);
350 if ((flags & NO_INO_UPDT) == 0)
351 error = ffs_update(vp, 1);
353 softdep_journal_fsync(VTOI(vp));
359 struct vop_lock1_args /* {
367 #ifndef NO_FFS_SNAPSHOT
373 switch (ap->a_flags & LK_TYPE_MASK) {
380 #ifdef DEBUG_VFS_LOCKS
381 KASSERT(vp->v_holdcnt != 0,
382 ("ffs_lock %p: zero hold count", vp));
385 result = _lockmgr_args(lkp, flags, VI_MTX(vp),
386 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
387 ap->a_file, ap->a_line);
388 if (lkp == vp->v_vnlock || result != 0)
391 * Apparent success, except that the vnode
392 * mutated between snapshot file vnode and
393 * regular file vnode while this process
394 * slept. The lock currently held is not the
395 * right lock. Release it, and try to get the
398 (void) _lockmgr_args(lkp, LK_RELEASE, NULL,
399 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
400 ap->a_file, ap->a_line);
401 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
402 (LK_INTERLOCK | LK_NOWAIT))
404 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
405 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
406 flags &= ~LK_INTERLOCK;
410 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
414 return (VOP_LOCK1_APV(&ufs_vnodeops, ap));
419 * Vnode op for reading.
423 struct vop_read_args /* {
427 struct ucred *a_cred;
435 ufs_lbn_t lbn, nextlbn;
437 long size, xfersize, blkoffset;
445 ioflag = ap->a_ioflag;
446 if (ap->a_ioflag & IO_EXT)
448 return (ffs_extread(vp, uio, ioflag));
450 panic("ffs_read+IO_EXT");
453 if ((ioflag & IO_DIRECT) != 0) {
456 error = ffs_rawread(vp, uio, &workdone);
457 if (error != 0 || workdone != 0)
462 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
466 if (uio->uio_rw != UIO_READ)
467 panic("ffs_read: mode");
469 if (vp->v_type == VLNK) {
470 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
471 panic("ffs_read: short symlink");
472 } else if (vp->v_type != VREG && vp->v_type != VDIR)
473 panic("ffs_read: type %d", vp->v_type);
475 orig_resid = uio->uio_resid;
476 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
479 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
481 if (uio->uio_offset < ip->i_size &&
482 uio->uio_offset >= fs->fs_maxfilesize)
485 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
486 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
488 lbn = lblkno(fs, uio->uio_offset);
492 * size of buffer. The buffer representing the
493 * end of the file is rounded up to the size of
494 * the block type ( fragment or full block,
497 size = blksize(fs, ip, lbn);
498 blkoffset = blkoff(fs, uio->uio_offset);
501 * The amount we want to transfer in this iteration is
502 * one FS block less the amount of the data before
503 * our startpoint (duh!)
505 xfersize = fs->fs_bsize - blkoffset;
508 * But if we actually want less than the block,
509 * or the file doesn't have a whole block more of data,
510 * then use the lesser number.
512 if (uio->uio_resid < xfersize)
513 xfersize = uio->uio_resid;
514 if (bytesinfile < xfersize)
515 xfersize = bytesinfile;
517 if (lblktosize(fs, nextlbn) >= ip->i_size) {
519 * Don't do readahead if this is the end of the file.
521 error = bread_gb(vp, lbn, size, NOCRED,
523 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
525 * Otherwise if we are allowed to cluster,
526 * grab as much as we can.
528 * XXX This may not be a win if we are not
529 * doing sequential access.
531 error = cluster_read(vp, ip->i_size, lbn,
532 size, NOCRED, blkoffset + uio->uio_resid,
533 seqcount, GB_UNMAPPED, &bp);
534 } else if (seqcount > 1) {
536 * If we are NOT allowed to cluster, then
537 * if we appear to be acting sequentially,
538 * fire off a request for a readahead
539 * as well as a read. Note that the 4th and 5th
540 * arguments point to arrays of the size specified in
543 u_int nextsize = blksize(fs, ip, nextlbn);
544 error = breadn_flags(vp, lbn, size, &nextlbn,
545 &nextsize, 1, NOCRED, GB_UNMAPPED, &bp);
548 * Failing all of the above, just read what the
549 * user asked for. Interestingly, the same as
550 * the first option above.
552 error = bread_gb(vp, lbn, size, NOCRED,
562 * If IO_DIRECT then set B_DIRECT for the buffer. This
563 * will cause us to attempt to release the buffer later on
564 * and will cause the buffer cache to attempt to free the
567 if (ioflag & IO_DIRECT)
568 bp->b_flags |= B_DIRECT;
571 * We should only get non-zero b_resid when an I/O error
572 * has occurred, which should cause us to break above.
573 * However, if the short read did not cause an error,
574 * then we want to ensure that we do not uiomove bad
575 * or uninitialized data.
578 if (size < xfersize) {
584 if (buf_mapped(bp)) {
585 error = vn_io_fault_uiomove((char *)bp->b_data +
586 blkoffset, (int)xfersize, uio);
588 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
594 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
595 (LIST_EMPTY(&bp->b_dep))) {
597 * If there are no dependencies, and it's VMIO,
598 * then we don't need the buf, mark it available
599 * for freeing. For non-direct VMIO reads, the VM
602 bp->b_flags |= B_RELBUF;
606 * Otherwise let whoever
607 * made the request take care of
608 * freeing it. We just queue
609 * it onto another list.
616 * This can only happen in the case of an error
617 * because the loop above resets bp to NULL on each iteration
618 * and on normal completion has not set a new value into it.
619 * so it must have come from a 'break' statement
622 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
623 (LIST_EMPTY(&bp->b_dep))) {
624 bp->b_flags |= B_RELBUF;
631 if ((error == 0 || uio->uio_resid != orig_resid) &&
632 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0 &&
633 (ip->i_flag & IN_ACCESS) == 0) {
635 ip->i_flag |= IN_ACCESS;
642 * Vnode op for writing.
646 struct vop_write_args /* {
650 struct ucred *a_cred;
662 int blkoffset, error, flags, ioflag, size, xfersize;
666 ioflag = ap->a_ioflag;
667 if (ap->a_ioflag & IO_EXT)
669 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
671 panic("ffs_write+IO_EXT");
674 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
678 if (uio->uio_rw != UIO_WRITE)
679 panic("ffs_write: mode");
682 switch (vp->v_type) {
684 if (ioflag & IO_APPEND)
685 uio->uio_offset = ip->i_size;
686 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
692 panic("ffs_write: dir write");
695 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
696 (int)uio->uio_offset,
701 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
702 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
704 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
707 * Maybe this should be above the vnode op call, but so long as
708 * file servers have no limits, I don't think it matters.
710 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
713 resid = uio->uio_resid;
715 if (seqcount > BA_SEQMAX)
716 flags = BA_SEQMAX << BA_SEQSHIFT;
718 flags = seqcount << BA_SEQSHIFT;
719 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
721 flags |= BA_UNMAPPED;
723 for (error = 0; uio->uio_resid > 0;) {
724 lbn = lblkno(fs, uio->uio_offset);
725 blkoffset = blkoff(fs, uio->uio_offset);
726 xfersize = fs->fs_bsize - blkoffset;
727 if (uio->uio_resid < xfersize)
728 xfersize = uio->uio_resid;
729 if (uio->uio_offset + xfersize > ip->i_size)
730 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
733 * We must perform a read-before-write if the transfer size
734 * does not cover the entire buffer.
736 if (fs->fs_bsize > xfersize)
740 /* XXX is uio->uio_offset the right thing here? */
741 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
742 ap->a_cred, flags, &bp);
744 vnode_pager_setsize(vp, ip->i_size);
747 if (ioflag & IO_DIRECT)
748 bp->b_flags |= B_DIRECT;
749 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
750 bp->b_flags |= B_NOCACHE;
752 if (uio->uio_offset + xfersize > ip->i_size) {
753 ip->i_size = uio->uio_offset + xfersize;
754 DIP_SET(ip, i_size, ip->i_size);
757 size = blksize(fs, ip, lbn) - bp->b_resid;
761 if (buf_mapped(bp)) {
762 error = vn_io_fault_uiomove((char *)bp->b_data +
763 blkoffset, (int)xfersize, uio);
765 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
769 * If the buffer is not already filled and we encounter an
770 * error while trying to fill it, we have to clear out any
771 * garbage data from the pages instantiated for the buffer.
772 * If we do not, a failed uiomove() during a write can leave
773 * the prior contents of the pages exposed to a userland mmap.
775 * Note that we need only clear buffers with a transfer size
776 * equal to the block size because buffers with a shorter
777 * transfer size were cleared above by the call to UFS_BALLOC()
778 * with the BA_CLRBUF flag set.
780 * If the source region for uiomove identically mmaps the
781 * buffer, uiomove() performed the NOP copy, and the buffer
782 * content remains valid because the page fault handler
783 * validated the pages.
785 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
786 fs->fs_bsize == xfersize)
788 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
789 (LIST_EMPTY(&bp->b_dep))) {
790 bp->b_flags |= B_RELBUF;
794 * If IO_SYNC each buffer is written synchronously. Otherwise
795 * if we have a severe page deficiency write the buffer
796 * asynchronously. Otherwise try to cluster, and if that
797 * doesn't do it then either do an async write (if O_DIRECT),
798 * or a delayed write (if not).
800 if (ioflag & IO_SYNC) {
802 } else if (vm_page_count_severe() ||
803 buf_dirty_count_severe() ||
804 (ioflag & IO_ASYNC)) {
805 bp->b_flags |= B_CLUSTEROK;
807 } else if (xfersize + blkoffset == fs->fs_bsize) {
808 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
809 bp->b_flags |= B_CLUSTEROK;
810 cluster_write(vp, bp, ip->i_size, seqcount,
815 } else if (ioflag & IO_DIRECT) {
816 bp->b_flags |= B_CLUSTEROK;
819 bp->b_flags |= B_CLUSTEROK;
822 if (error || xfersize == 0)
824 ip->i_flag |= IN_CHANGE | IN_UPDATE;
827 * If we successfully wrote any data, and we are not the superuser
828 * we clear the setuid and setgid bits as a precaution against
831 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
833 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0)) {
834 ip->i_mode &= ~(ISUID | ISGID);
835 DIP_SET(ip, i_mode, ip->i_mode);
839 if (ioflag & IO_UNIT) {
840 (void)ffs_truncate(vp, osize,
841 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
842 uio->uio_offset -= resid - uio->uio_resid;
843 uio->uio_resid = resid;
845 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
846 error = ffs_update(vp, 1);
851 * Extended attribute area reading.
854 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
857 struct ufs2_dinode *dp;
860 ufs_lbn_t lbn, nextlbn;
862 long size, xfersize, blkoffset;
871 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
872 panic("ffs_extread: mode");
875 orig_resid = uio->uio_resid;
876 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
879 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
881 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
882 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
884 lbn = lblkno(fs, uio->uio_offset);
888 * size of buffer. The buffer representing the
889 * end of the file is rounded up to the size of
890 * the block type ( fragment or full block,
893 size = sblksize(fs, dp->di_extsize, lbn);
894 blkoffset = blkoff(fs, uio->uio_offset);
897 * The amount we want to transfer in this iteration is
898 * one FS block less the amount of the data before
899 * our startpoint (duh!)
901 xfersize = fs->fs_bsize - blkoffset;
904 * But if we actually want less than the block,
905 * or the file doesn't have a whole block more of data,
906 * then use the lesser number.
908 if (uio->uio_resid < xfersize)
909 xfersize = uio->uio_resid;
910 if (bytesinfile < xfersize)
911 xfersize = bytesinfile;
913 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
915 * Don't do readahead if this is the end of the info.
917 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
920 * If we have a second block, then
921 * fire off a request for a readahead
922 * as well as a read. Note that the 4th and 5th
923 * arguments point to arrays of the size specified in
926 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
928 nextlbn = -1 - nextlbn;
929 error = breadn(vp, -1 - lbn,
930 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
939 * If IO_DIRECT then set B_DIRECT for the buffer. This
940 * will cause us to attempt to release the buffer later on
941 * and will cause the buffer cache to attempt to free the
944 if (ioflag & IO_DIRECT)
945 bp->b_flags |= B_DIRECT;
948 * We should only get non-zero b_resid when an I/O error
949 * has occurred, which should cause us to break above.
950 * However, if the short read did not cause an error,
951 * then we want to ensure that we do not uiomove bad
952 * or uninitialized data.
955 if (size < xfersize) {
961 error = uiomove((char *)bp->b_data + blkoffset,
966 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
967 (LIST_EMPTY(&bp->b_dep))) {
969 * If there are no dependencies, and it's VMIO,
970 * then we don't need the buf, mark it available
971 * for freeing. For non-direct VMIO reads, the VM
974 bp->b_flags |= B_RELBUF;
978 * Otherwise let whoever
979 * made the request take care of
980 * freeing it. We just queue
981 * it onto another list.
988 * This can only happen in the case of an error
989 * because the loop above resets bp to NULL on each iteration
990 * and on normal completion has not set a new value into it.
991 * so it must have come from a 'break' statement
994 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
995 (LIST_EMPTY(&bp->b_dep))) {
996 bp->b_flags |= B_RELBUF;
1006 * Extended attribute area writing.
1009 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1012 struct ufs2_dinode *dp;
1018 int blkoffset, error, flags, size, xfersize;
1025 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1026 panic("ffs_extwrite: mode");
1029 if (ioflag & IO_APPEND)
1030 uio->uio_offset = dp->di_extsize;
1031 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1032 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1033 if ((uoff_t)uio->uio_offset + uio->uio_resid > NXADDR * fs->fs_bsize)
1036 resid = uio->uio_resid;
1037 osize = dp->di_extsize;
1039 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1042 for (error = 0; uio->uio_resid > 0;) {
1043 lbn = lblkno(fs, uio->uio_offset);
1044 blkoffset = blkoff(fs, uio->uio_offset);
1045 xfersize = fs->fs_bsize - blkoffset;
1046 if (uio->uio_resid < xfersize)
1047 xfersize = uio->uio_resid;
1050 * We must perform a read-before-write if the transfer size
1051 * does not cover the entire buffer.
1053 if (fs->fs_bsize > xfersize)
1056 flags &= ~BA_CLRBUF;
1057 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1062 * If the buffer is not valid we have to clear out any
1063 * garbage data from the pages instantiated for the buffer.
1064 * If we do not, a failed uiomove() during a write can leave
1065 * the prior contents of the pages exposed to a userland
1066 * mmap(). XXX deal with uiomove() errors a better way.
1068 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1070 if (ioflag & IO_DIRECT)
1071 bp->b_flags |= B_DIRECT;
1073 if (uio->uio_offset + xfersize > dp->di_extsize)
1074 dp->di_extsize = uio->uio_offset + xfersize;
1076 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1077 if (size < xfersize)
1081 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1082 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1083 (LIST_EMPTY(&bp->b_dep))) {
1084 bp->b_flags |= B_RELBUF;
1088 * If IO_SYNC each buffer is written synchronously. Otherwise
1089 * if we have a severe page deficiency write the buffer
1090 * asynchronously. Otherwise try to cluster, and if that
1091 * doesn't do it then either do an async write (if O_DIRECT),
1092 * or a delayed write (if not).
1094 if (ioflag & IO_SYNC) {
1096 } else if (vm_page_count_severe() ||
1097 buf_dirty_count_severe() ||
1098 xfersize + blkoffset == fs->fs_bsize ||
1099 (ioflag & (IO_ASYNC | IO_DIRECT)))
1103 if (error || xfersize == 0)
1105 ip->i_flag |= IN_CHANGE;
1108 * If we successfully wrote any data, and we are not the superuser
1109 * we clear the setuid and setgid bits as a precaution against
1112 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1113 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID, 0)) {
1114 ip->i_mode &= ~(ISUID | ISGID);
1115 dp->di_mode = ip->i_mode;
1119 if (ioflag & IO_UNIT) {
1120 (void)ffs_truncate(vp, osize,
1121 IO_EXT | (ioflag&IO_SYNC), ucred);
1122 uio->uio_offset -= resid - uio->uio_resid;
1123 uio->uio_resid = resid;
1125 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1126 error = ffs_update(vp, 1);
1132 * Vnode operating to retrieve a named extended attribute.
1134 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1135 * the length of the EA, and possibly the pointer to the entry and to the data.
1138 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name, u_char **eap, u_char **eac)
1140 u_char *p, *pe, *pn, *p0;
1141 int eapad1, eapad2, ealength, ealen, nlen;
1145 nlen = strlen(name);
1147 for (p = ptr; p < pe; p = pn) {
1149 bcopy(p, &ul, sizeof(ul));
1151 /* make sure this entry is complete */
1154 p += sizeof(uint32_t);
1162 if (bcmp(p, name, nlen))
1164 ealength = sizeof(uint32_t) + 3 + nlen;
1165 eapad1 = 8 - (ealength % 8);
1169 ealen = ul - ealength - eapad2;
1181 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td, int extra)
1184 struct ufs2_dinode *dp;
1187 struct iovec liovec;
1195 easize = dp->di_extsize;
1196 if ((uoff_t)easize + extra > NXADDR * fs->fs_bsize)
1199 eae = malloc(easize + extra, M_TEMP, M_WAITOK);
1201 liovec.iov_base = eae;
1202 liovec.iov_len = easize;
1203 luio.uio_iov = &liovec;
1204 luio.uio_iovcnt = 1;
1205 luio.uio_offset = 0;
1206 luio.uio_resid = easize;
1207 luio.uio_segflg = UIO_SYSSPACE;
1208 luio.uio_rw = UIO_READ;
1211 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1221 ffs_lock_ea(struct vnode *vp)
1227 while (ip->i_flag & IN_EA_LOCKED) {
1228 ip->i_flag |= IN_EA_LOCKWAIT;
1229 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1232 ip->i_flag |= IN_EA_LOCKED;
1237 ffs_unlock_ea(struct vnode *vp)
1243 if (ip->i_flag & IN_EA_LOCKWAIT)
1244 wakeup(&ip->i_ea_refs);
1245 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1250 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1253 struct ufs2_dinode *dp;
1259 if (ip->i_ea_area != NULL) {
1265 error = ffs_rdextattr(&ip->i_ea_area, vp, td, 0);
1270 ip->i_ea_len = dp->di_extsize;
1278 * Vnode extattr transaction commit/abort
1281 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1285 struct iovec liovec;
1287 struct ufs2_dinode *dp;
1292 if (ip->i_ea_area == NULL) {
1297 error = ip->i_ea_error;
1298 if (commit && error == 0) {
1299 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1301 cred = vp->v_mount->mnt_cred;
1302 liovec.iov_base = ip->i_ea_area;
1303 liovec.iov_len = ip->i_ea_len;
1304 luio.uio_iov = &liovec;
1305 luio.uio_iovcnt = 1;
1306 luio.uio_offset = 0;
1307 luio.uio_resid = ip->i_ea_len;
1308 luio.uio_segflg = UIO_SYSSPACE;
1309 luio.uio_rw = UIO_WRITE;
1311 /* XXX: I'm not happy about truncating to zero size */
1312 if (ip->i_ea_len < dp->di_extsize)
1313 error = ffs_truncate(vp, 0, IO_EXT, cred);
1314 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1316 if (--ip->i_ea_refs == 0) {
1317 free(ip->i_ea_area, M_TEMP);
1318 ip->i_ea_area = NULL;
1327 * Vnode extattr strategy routine for fifos.
1329 * We need to check for a read or write of the external attributes.
1330 * Otherwise we just fall through and do the usual thing.
1333 ffsext_strategy(struct vop_strategy_args *ap)
1335 struct vop_strategy_args {
1336 struct vnodeop_desc *a_desc;
1346 lbn = ap->a_bp->b_lblkno;
1347 if (VTOI(vp)->i_fs->fs_magic == FS_UFS2_MAGIC &&
1348 lbn < 0 && lbn >= -NXADDR)
1349 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1350 if (vp->v_type == VFIFO)
1351 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1352 panic("spec nodes went here");
1356 * Vnode extattr transaction commit/abort
1359 ffs_openextattr(struct vop_openextattr_args *ap)
1361 struct vop_openextattr_args {
1362 struct vnodeop_desc *a_desc;
1364 IN struct ucred *a_cred;
1365 IN struct thread *a_td;
1370 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1371 return (EOPNOTSUPP);
1373 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1378 * Vnode extattr transaction commit/abort
1381 ffs_closeextattr(struct vop_closeextattr_args *ap)
1383 struct vop_closeextattr_args {
1384 struct vnodeop_desc *a_desc;
1387 IN struct ucred *a_cred;
1388 IN struct thread *a_td;
1393 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1394 return (EOPNOTSUPP);
1396 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1399 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1403 * Vnode operation to remove a named attribute.
1406 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1409 IN struct vnode *a_vp;
1410 IN int a_attrnamespace;
1411 IN const char *a_name;
1412 IN struct ucred *a_cred;
1413 IN struct thread *a_td;
1419 uint32_t ealength, ul;
1420 int ealen, olen, eapad1, eapad2, error, i, easize;
1423 ip = VTOI(ap->a_vp);
1426 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1427 return (EOPNOTSUPP);
1429 if (strlen(ap->a_name) == 0)
1432 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1435 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1436 ap->a_cred, ap->a_td, VWRITE);
1440 * ffs_lock_ea is not needed there, because the vnode
1441 * must be exclusively locked.
1443 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1444 ip->i_ea_error = error;
1448 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1452 ealength = eapad1 = ealen = eapad2 = 0;
1454 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1455 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1456 easize = ip->i_ea_len;
1458 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1461 /* delete but nonexistent */
1463 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1466 bcopy(p, &ul, sizeof ul);
1468 if (ul != ealength) {
1469 bcopy(p + ul, p + ealength, easize - i);
1470 easize += (ealength - ul);
1472 if (easize > NXADDR * fs->fs_bsize) {
1474 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1475 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1476 ip->i_ea_error = ENOSPC;
1480 ip->i_ea_area = eae;
1481 ip->i_ea_len = easize;
1483 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1488 * Vnode operation to retrieve a named extended attribute.
1491 ffs_getextattr(struct vop_getextattr_args *ap)
1494 IN struct vnode *a_vp;
1495 IN int a_attrnamespace;
1496 IN const char *a_name;
1497 INOUT struct uio *a_uio;
1499 IN struct ucred *a_cred;
1500 IN struct thread *a_td;
1509 ip = VTOI(ap->a_vp);
1511 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1512 return (EOPNOTSUPP);
1514 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1515 ap->a_cred, ap->a_td, VREAD);
1519 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1523 eae = ip->i_ea_area;
1524 easize = ip->i_ea_len;
1526 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1530 if (ap->a_size != NULL)
1531 *ap->a_size = ealen;
1532 else if (ap->a_uio != NULL)
1533 error = uiomove(p, ealen, ap->a_uio);
1537 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1542 * Vnode operation to retrieve extended attributes on a vnode.
1545 ffs_listextattr(struct vop_listextattr_args *ap)
1548 IN struct vnode *a_vp;
1549 IN int a_attrnamespace;
1550 INOUT struct uio *a_uio;
1552 IN struct ucred *a_cred;
1553 IN struct thread *a_td;
1558 u_char *eae, *p, *pe, *pn;
1563 ip = VTOI(ap->a_vp);
1565 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1566 return (EOPNOTSUPP);
1568 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1569 ap->a_cred, ap->a_td, VREAD);
1573 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1576 eae = ip->i_ea_area;
1577 easize = ip->i_ea_len;
1580 if (ap->a_size != NULL)
1583 for(p = eae; error == 0 && p < pe; p = pn) {
1584 bcopy(p, &ul, sizeof(ul));
1589 if (*p++ != ap->a_attrnamespace)
1593 if (ap->a_size != NULL) {
1594 *ap->a_size += ealen + 1;
1595 } else if (ap->a_uio != NULL) {
1596 error = uiomove(p, ealen + 1, ap->a_uio);
1599 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1604 * Vnode operation to set a named attribute.
1607 ffs_setextattr(struct vop_setextattr_args *ap)
1610 IN struct vnode *a_vp;
1611 IN int a_attrnamespace;
1612 IN const char *a_name;
1613 INOUT struct uio *a_uio;
1614 IN struct ucred *a_cred;
1615 IN struct thread *a_td;
1621 uint32_t ealength, ul;
1623 int olen, eapad1, eapad2, error, i, easize;
1626 ip = VTOI(ap->a_vp);
1629 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1630 return (EOPNOTSUPP);
1632 if (strlen(ap->a_name) == 0)
1635 /* XXX Now unsupported API to delete EAs using NULL uio. */
1636 if (ap->a_uio == NULL)
1637 return (EOPNOTSUPP);
1639 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1642 ealen = ap->a_uio->uio_resid;
1643 if (ealen < 0 || ealen > lblktosize(fs, NXADDR))
1646 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1647 ap->a_cred, ap->a_td, VWRITE);
1651 * ffs_lock_ea is not needed there, because the vnode
1652 * must be exclusively locked.
1654 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1655 ip->i_ea_error = error;
1659 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1663 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1664 eapad1 = 8 - (ealength % 8);
1667 eapad2 = 8 - (ealen % 8);
1670 ealength += eapad1 + ealen + eapad2;
1672 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1673 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1674 easize = ip->i_ea_len;
1676 olen = ffs_findextattr(eae, easize,
1677 ap->a_attrnamespace, ap->a_name, &p, NULL);
1679 /* new, append at end */
1683 bcopy(p, &ul, sizeof ul);
1685 if (ul != ealength) {
1686 bcopy(p + ul, p + ealength, easize - i);
1687 easize += (ealength - ul);
1690 if (easize > lblktosize(fs, NXADDR)) {
1692 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1693 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1694 ip->i_ea_error = ENOSPC;
1697 bcopy(&ealength, p, sizeof(ealength));
1698 p += sizeof(ealength);
1699 *p++ = ap->a_attrnamespace;
1701 *p++ = strlen(ap->a_name);
1702 strcpy(p, ap->a_name);
1703 p += strlen(ap->a_name);
1706 error = uiomove(p, ealen, ap->a_uio);
1709 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1710 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1711 ip->i_ea_error = error;
1718 ip->i_ea_area = eae;
1719 ip->i_ea_len = easize;
1721 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1726 * Vnode pointer to File handle
1729 ffs_vptofh(struct vop_vptofh_args *ap)
1732 IN struct vnode *a_vp;
1733 IN struct fid *a_fhp;
1740 ip = VTOI(ap->a_vp);
1741 ufhp = (struct ufid *)ap->a_fhp;
1742 ufhp->ufid_len = sizeof(struct ufid);
1743 ufhp->ufid_ino = ip->i_number;
1744 ufhp->ufid_gen = ip->i_gen;