2 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-3-Clause)
4 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * from: @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
62 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
63 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
69 #include "opt_directio.h"
72 #include <sys/param.h>
74 #include <sys/systm.h>
77 #include <sys/extattr.h>
78 #include <sys/kernel.h>
79 #include <sys/limits.h>
80 #include <sys/malloc.h>
81 #include <sys/mount.h>
83 #include <sys/rwlock.h>
85 #include <sys/sysctl.h>
86 #include <sys/vmmeter.h>
87 #include <sys/vnode.h>
90 #include <vm/vm_param.h>
91 #include <vm/vm_extern.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vnode_pager.h>
97 #include <ufs/ufs/extattr.h>
98 #include <ufs/ufs/quota.h>
99 #include <ufs/ufs/inode.h>
100 #include <ufs/ufs/ufs_extern.h>
101 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ffs/fs.h>
104 #include <ufs/ffs/ffs_extern.h>
106 #define ALIGNED_TO(ptr, s) \
107 (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
110 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
112 static vop_fdatasync_t ffs_fdatasync;
113 static vop_fsync_t ffs_fsync;
114 static vop_getpages_t ffs_getpages;
115 static vop_getpages_async_t ffs_getpages_async;
116 static vop_lock1_t ffs_lock;
118 static vop_unlock_t ffs_unlock_debug;
120 static vop_read_t ffs_read;
121 static vop_write_t ffs_write;
122 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
123 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
125 static vop_strategy_t ffsext_strategy;
126 static vop_closeextattr_t ffs_closeextattr;
127 static vop_deleteextattr_t ffs_deleteextattr;
128 static vop_getextattr_t ffs_getextattr;
129 static vop_listextattr_t ffs_listextattr;
130 static vop_openextattr_t ffs_openextattr;
131 static vop_setextattr_t ffs_setextattr;
132 static vop_vptofh_t ffs_vptofh;
133 static vop_vput_pair_t ffs_vput_pair;
135 /* Global vfs data structures for ufs. */
136 struct vop_vector ffs_vnodeops1 = {
137 .vop_default = &ufs_vnodeops,
138 .vop_fsync = ffs_fsync,
139 .vop_fdatasync = ffs_fdatasync,
140 .vop_getpages = ffs_getpages,
141 .vop_getpages_async = ffs_getpages_async,
142 .vop_lock1 = ffs_lock,
144 .vop_unlock = ffs_unlock_debug,
146 .vop_read = ffs_read,
147 .vop_reallocblks = ffs_reallocblks,
148 .vop_write = ffs_write,
149 .vop_vptofh = ffs_vptofh,
150 .vop_vput_pair = ffs_vput_pair,
152 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
154 struct vop_vector ffs_fifoops1 = {
155 .vop_default = &ufs_fifoops,
156 .vop_fsync = ffs_fsync,
157 .vop_fdatasync = ffs_fdatasync,
158 .vop_lock1 = ffs_lock,
160 .vop_unlock = ffs_unlock_debug,
162 .vop_vptofh = ffs_vptofh,
164 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
166 /* Global vfs data structures for ufs. */
167 struct vop_vector ffs_vnodeops2 = {
168 .vop_default = &ufs_vnodeops,
169 .vop_fsync = ffs_fsync,
170 .vop_fdatasync = ffs_fdatasync,
171 .vop_getpages = ffs_getpages,
172 .vop_getpages_async = ffs_getpages_async,
173 .vop_lock1 = ffs_lock,
175 .vop_unlock = ffs_unlock_debug,
177 .vop_read = ffs_read,
178 .vop_reallocblks = ffs_reallocblks,
179 .vop_write = ffs_write,
180 .vop_closeextattr = ffs_closeextattr,
181 .vop_deleteextattr = ffs_deleteextattr,
182 .vop_getextattr = ffs_getextattr,
183 .vop_listextattr = ffs_listextattr,
184 .vop_openextattr = ffs_openextattr,
185 .vop_setextattr = ffs_setextattr,
186 .vop_vptofh = ffs_vptofh,
187 .vop_vput_pair = ffs_vput_pair,
189 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
191 struct vop_vector ffs_fifoops2 = {
192 .vop_default = &ufs_fifoops,
193 .vop_fsync = ffs_fsync,
194 .vop_fdatasync = ffs_fdatasync,
195 .vop_lock1 = ffs_lock,
197 .vop_unlock = ffs_unlock_debug,
199 .vop_reallocblks = ffs_reallocblks,
200 .vop_strategy = ffsext_strategy,
201 .vop_closeextattr = ffs_closeextattr,
202 .vop_deleteextattr = ffs_deleteextattr,
203 .vop_getextattr = ffs_getextattr,
204 .vop_listextattr = ffs_listextattr,
205 .vop_openextattr = ffs_openextattr,
206 .vop_setextattr = ffs_setextattr,
207 .vop_vptofh = ffs_vptofh,
209 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
212 * Synch an open file.
216 ffs_fsync(struct vop_fsync_args *ap)
225 error = ffs_syncvnode(vp, ap->a_waitfor, 0);
228 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
229 error = softdep_fsync(vp);
234 * The softdep_fsync() function may drop vp lock,
235 * allowing for dirty buffers to reappear on the
236 * bo_dirty list. Recheck and resync as needed.
239 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
240 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
246 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
252 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
256 struct ufsmount *ump;
257 struct buf *bp, *nbp;
260 bool still_dirty, unlocked, wait;
264 ump = VFSTOUFS(vp->v_mount);
267 * When doing MNT_WAIT we must first flush all dependencies
270 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
271 (error = softdep_sync_metadata(vp)) != 0) {
272 if (ffs_fsfail_cleanup(ump, error))
278 * Flush all dirty buffers associated with a vnode.
282 wait = false; /* Always do an async pass first. */
284 lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
287 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
288 bp->b_vflags &= ~BV_SCANNED;
289 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
291 * Reasons to skip this buffer: it has already been considered
292 * on this pass, the buffer has dependencies that will cause
293 * it to be redirtied and it has not already been deferred,
294 * or it is already being written.
296 if ((bp->b_vflags & BV_SCANNED) != 0)
298 bp->b_vflags |= BV_SCANNED;
300 * Flush indirects in order, if requested.
302 * Note that if only datasync is requested, we can
303 * skip indirect blocks when softupdates are not
304 * active. Otherwise we must flush them with data,
305 * since dependencies prevent data block writes.
307 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
308 (lbn_level(bp->b_lblkno) >= passes ||
309 ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
311 if (bp->b_lblkno > lbn)
312 panic("ffs_syncvnode: syncing truncated data.");
313 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
317 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
318 BO_LOCKPTR(bo)) != 0) {
319 bp->b_vflags &= ~BV_SCANNED;
324 if ((bp->b_flags & B_DELWRI) == 0)
325 panic("ffs_fsync: not dirty");
327 * Check for dependencies and potentially complete them.
329 if (!LIST_EMPTY(&bp->b_dep) &&
330 (error = softdep_sync_buf(vp, bp,
331 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
333 * Lock order conflict, buffer was already unlocked,
334 * and vnode possibly unlocked.
336 if (error == ERELOOKUP) {
337 if (vp->v_data == NULL)
340 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
341 (error = softdep_sync_metadata(vp)) != 0) {
342 if (ffs_fsfail_cleanup(ump, error))
344 return (unlocked && error == 0 ?
347 /* Re-evaluate inode size */
348 lbn = lblkno(ITOFS(ip), (ip->i_size +
349 ITOFS(ip)->fs_bsize - 1));
353 if (error != EBUSY) {
357 /* If we deferred once, don't defer again. */
358 if ((bp->b_flags & B_DEFERRED) == 0) {
359 bp->b_flags |= B_DEFERRED;
367 if (ffs_fsfail_cleanup(ump, error))
371 } else if ((bp->b_flags & B_CLUSTEROK)) {
372 (void) vfs_bio_awrite(bp);
379 * Since we may have slept during the I/O, we need
380 * to start from a known point.
383 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
385 if (waitfor != MNT_WAIT) {
387 if ((flags & NO_INO_UPDT) != 0)
388 return (unlocked ? ERELOOKUP : 0);
389 error = ffs_update(vp, 0);
390 if (error == 0 && unlocked)
394 /* Drain IO to see if we're done. */
395 bufobj_wwait(bo, 0, 0);
397 * Block devices associated with filesystems may have new I/O
398 * requests posted for them even if the vnode is locked, so no
399 * amount of trying will get them clean. We make several passes
402 * Regular files may need multiple passes to flush all dependency
403 * work as it is possible that we must write once per indirect
404 * level, once for the leaf, and once for the inode and each of
405 * these will be done with one sync and one async pass.
407 if (bo->bo_dirty.bv_cnt > 0) {
408 if ((flags & DATA_ONLY) == 0) {
412 * For data-only sync, dirty indirect buffers
416 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
417 if (bp->b_lblkno > -UFS_NDADDR) {
425 /* Write the inode after sync passes to flush deps. */
426 if (wait && DOINGSOFTDEP(vp) &&
427 (flags & NO_INO_UPDT) == 0) {
432 /* switch between sync/async. */
434 if (wait || ++passes < UFS_NIADDR + 2)
440 if ((flags & DATA_ONLY) == 0) {
441 if ((flags & NO_INO_UPDT) == 0)
442 error = ffs_update(vp, 1);
444 softdep_journal_fsync(VTOI(vp));
445 } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
446 error = ffs_update(vp, 1);
448 if (error == 0 && unlocked)
451 ip->i_flag &= ~IN_NEEDSYNC;
456 ffs_fdatasync(struct vop_fdatasync_args *ap)
459 return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
464 struct vop_lock1_args /* {
471 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
472 struct vnode *vp = ap->a_vp;
473 #endif /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
476 #endif /* DIAGNOSTIC */
478 #ifndef NO_FFS_SNAPSHOT
483 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
484 * and only use it when LK_NODDLKTREAT is set. Currently this means it
485 * is only used during path lookup.
487 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
488 ap->a_flags |= LK_ADAPTIVE;
489 switch (ap->a_flags & LK_TYPE_MASK) {
495 #ifdef DEBUG_VFS_LOCKS
496 VNPASS(vp->v_holdcnt != 0, vp);
497 #endif /* DEBUG_VFS_LOCKS */
499 result = lockmgr_lock_flags(lkp, flags,
500 &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
501 if (lkp == vp->v_vnlock || result != 0)
504 * Apparent success, except that the vnode
505 * mutated between snapshot file vnode and
506 * regular file vnode while this process
507 * slept. The lock currently held is not the
508 * right lock. Release it, and try to get the
512 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
513 (LK_INTERLOCK | LK_NOWAIT))
515 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
516 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
517 flags &= ~LK_INTERLOCK;
520 switch (ap->a_flags & LK_TYPE_MASK) {
523 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
529 #endif /* DIAGNOSTIC */
533 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
536 ufs_unlock_tracker(ip);
538 #endif /* DIAGNOSTIC */
539 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
542 #else /* NO_FFS_SNAPSHOT */
544 * See above for an explanation.
546 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
547 ap->a_flags |= LK_ADAPTIVE;
549 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
552 ufs_unlock_tracker(ip);
554 #endif /* DIAGNOSTIC */
555 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
556 #endif /* NO_FFS_SNAPSHOT */
558 switch (ap->a_flags & LK_TYPE_MASK) {
561 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
567 #endif /* DIAGNOSTIC */
573 ffs_unlock_debug(struct vop_unlock_args *ap)
580 if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
581 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
583 VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
584 ("%s: modified vnode (%x) not on lazy list",
585 __func__, ip->i_flag));
590 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
591 vp->v_vnlock->lk_recurse == 0)
592 ufs_unlock_tracker(ip);
594 return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
599 ffs_read_hole(struct uio *uio, long xfersize, long *size)
601 ssize_t saved_resid, tlen;
604 while (xfersize > 0) {
605 tlen = min(xfersize, ZERO_REGION_SIZE);
606 saved_resid = uio->uio_resid;
607 error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
611 tlen = saved_resid - uio->uio_resid;
619 * Vnode op for reading.
623 struct vop_read_args /* {
627 struct ucred *a_cred;
635 ufs_lbn_t lbn, nextlbn;
637 long size, xfersize, blkoffset;
639 int bflag, error, ioflag, seqcount;
643 ioflag = ap->a_ioflag;
644 if (ap->a_ioflag & IO_EXT)
646 return (ffs_extread(vp, uio, ioflag));
648 panic("ffs_read+IO_EXT");
651 if ((ioflag & IO_DIRECT) != 0) {
654 error = ffs_rawread(vp, uio, &workdone);
655 if (error != 0 || workdone != 0)
660 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
664 if (uio->uio_rw != UIO_READ)
665 panic("ffs_read: mode");
667 if (vp->v_type == VLNK) {
668 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
669 panic("ffs_read: short symlink");
670 } else if (vp->v_type != VREG && vp->v_type != VDIR)
671 panic("ffs_read: type %d", vp->v_type);
673 orig_resid = uio->uio_resid;
674 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
677 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
679 if (uio->uio_offset < ip->i_size &&
680 uio->uio_offset >= fs->fs_maxfilesize)
683 bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
684 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
685 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
687 lbn = lblkno(fs, uio->uio_offset);
691 * size of buffer. The buffer representing the
692 * end of the file is rounded up to the size of
693 * the block type ( fragment or full block,
696 size = blksize(fs, ip, lbn);
697 blkoffset = blkoff(fs, uio->uio_offset);
700 * The amount we want to transfer in this iteration is
701 * one FS block less the amount of the data before
702 * our startpoint (duh!)
704 xfersize = fs->fs_bsize - blkoffset;
707 * But if we actually want less than the block,
708 * or the file doesn't have a whole block more of data,
709 * then use the lesser number.
711 if (uio->uio_resid < xfersize)
712 xfersize = uio->uio_resid;
713 if (bytesinfile < xfersize)
714 xfersize = bytesinfile;
716 if (lblktosize(fs, nextlbn) >= ip->i_size) {
718 * Don't do readahead if this is the end of the file.
720 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
721 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
723 * Otherwise if we are allowed to cluster,
724 * grab as much as we can.
726 * XXX This may not be a win if we are not
727 * doing sequential access.
729 error = cluster_read(vp, ip->i_size, lbn,
730 size, NOCRED, blkoffset + uio->uio_resid,
731 seqcount, bflag, &bp);
732 } else if (seqcount > 1) {
734 * If we are NOT allowed to cluster, then
735 * if we appear to be acting sequentially,
736 * fire off a request for a readahead
737 * as well as a read. Note that the 4th and 5th
738 * arguments point to arrays of the size specified in
741 u_int nextsize = blksize(fs, ip, nextlbn);
742 error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
743 &nextsize, 1, NOCRED, bflag, NULL, &bp);
746 * Failing all of the above, just read what the
747 * user asked for. Interestingly, the same as
748 * the first option above.
750 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
752 if (error == EJUSTRETURN) {
753 error = ffs_read_hole(uio, xfersize, &size);
764 * We should only get non-zero b_resid when an I/O error
765 * has occurred, which should cause us to break above.
766 * However, if the short read did not cause an error,
767 * then we want to ensure that we do not uiomove bad
768 * or uninitialized data.
771 if (size < xfersize) {
777 if (buf_mapped(bp)) {
778 error = vn_io_fault_uiomove((char *)bp->b_data +
779 blkoffset, (int)xfersize, uio);
781 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
787 vfs_bio_brelse(bp, ioflag);
791 * This can only happen in the case of an error
792 * because the loop above resets bp to NULL on each iteration
793 * and on normal completion has not set a new value into it.
794 * so it must have come from a 'break' statement
797 vfs_bio_brelse(bp, ioflag);
799 if ((error == 0 || uio->uio_resid != orig_resid) &&
800 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
801 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
806 * Vnode op for writing.
810 struct vop_write_args /* {
814 struct ucred *a_cred;
826 int blkoffset, error, flags, ioflag, size, xfersize;
830 ioflag = ap->a_ioflag;
831 if (ap->a_ioflag & IO_EXT)
833 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
835 panic("ffs_write+IO_EXT");
838 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
842 if (uio->uio_rw != UIO_WRITE)
843 panic("ffs_write: mode");
846 switch (vp->v_type) {
848 if (ioflag & IO_APPEND)
849 uio->uio_offset = ip->i_size;
850 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
856 panic("ffs_write: dir write");
859 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
860 (int)uio->uio_offset,
865 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
866 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
868 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
871 * Maybe this should be above the vnode op call, but so long as
872 * file servers have no limits, I don't think it matters.
874 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
877 resid = uio->uio_resid;
879 if (seqcount > BA_SEQMAX)
880 flags = BA_SEQMAX << BA_SEQSHIFT;
882 flags = seqcount << BA_SEQSHIFT;
883 if (ioflag & IO_SYNC)
885 flags |= BA_UNMAPPED;
887 for (error = 0; uio->uio_resid > 0;) {
888 lbn = lblkno(fs, uio->uio_offset);
889 blkoffset = blkoff(fs, uio->uio_offset);
890 xfersize = fs->fs_bsize - blkoffset;
891 if (uio->uio_resid < xfersize)
892 xfersize = uio->uio_resid;
893 if (uio->uio_offset + xfersize > ip->i_size)
894 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
897 * We must perform a read-before-write if the transfer size
898 * does not cover the entire buffer.
900 if (fs->fs_bsize > xfersize)
904 /* XXX is uio->uio_offset the right thing here? */
905 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
906 ap->a_cred, flags, &bp);
908 vnode_pager_setsize(vp, ip->i_size);
911 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
912 bp->b_flags |= B_NOCACHE;
914 if (uio->uio_offset + xfersize > ip->i_size) {
915 ip->i_size = uio->uio_offset + xfersize;
916 DIP_SET(ip, i_size, ip->i_size);
917 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
920 size = blksize(fs, ip, lbn) - bp->b_resid;
924 if (buf_mapped(bp)) {
925 error = vn_io_fault_uiomove((char *)bp->b_data +
926 blkoffset, (int)xfersize, uio);
928 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
932 * If the buffer is not already filled and we encounter an
933 * error while trying to fill it, we have to clear out any
934 * garbage data from the pages instantiated for the buffer.
935 * If we do not, a failed uiomove() during a write can leave
936 * the prior contents of the pages exposed to a userland mmap.
938 * Note that we need only clear buffers with a transfer size
939 * equal to the block size because buffers with a shorter
940 * transfer size were cleared above by the call to UFS_BALLOC()
941 * with the BA_CLRBUF flag set.
943 * If the source region for uiomove identically mmaps the
944 * buffer, uiomove() performed the NOP copy, and the buffer
945 * content remains valid because the page fault handler
946 * validated the pages.
948 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
949 fs->fs_bsize == xfersize)
952 vfs_bio_set_flags(bp, ioflag);
955 * If IO_SYNC each buffer is written synchronously. Otherwise
956 * if we have a severe page deficiency write the buffer
957 * asynchronously. Otherwise try to cluster, and if that
958 * doesn't do it then either do an async write (if O_DIRECT),
959 * or a delayed write (if not).
961 if (ioflag & IO_SYNC) {
963 } else if (vm_page_count_severe() ||
964 buf_dirty_count_severe() ||
965 (ioflag & IO_ASYNC)) {
966 bp->b_flags |= B_CLUSTEROK;
968 } else if (xfersize + blkoffset == fs->fs_bsize) {
969 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
970 bp->b_flags |= B_CLUSTEROK;
971 cluster_write(vp, bp, ip->i_size, seqcount,
976 } else if (ioflag & IO_DIRECT) {
977 bp->b_flags |= B_CLUSTEROK;
980 bp->b_flags |= B_CLUSTEROK;
983 if (error || xfersize == 0)
985 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
988 * If we successfully wrote any data, and we are not the superuser
989 * we clear the setuid and setgid bits as a precaution against
992 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
994 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
995 vn_seqc_write_begin(vp);
996 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
997 DIP_SET(ip, i_mode, ip->i_mode);
998 vn_seqc_write_end(vp);
1002 if (ioflag & IO_UNIT) {
1003 (void)ffs_truncate(vp, osize,
1004 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1005 uio->uio_offset -= resid - uio->uio_resid;
1006 uio->uio_resid = resid;
1008 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1009 if (!(ioflag & IO_DATASYNC) ||
1010 (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1011 error = ffs_update(vp, 1);
1012 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1019 * Extended attribute area reading.
1022 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1025 struct ufs2_dinode *dp;
1028 ufs_lbn_t lbn, nextlbn;
1030 long size, xfersize, blkoffset;
1039 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1040 panic("ffs_extread: mode");
1043 orig_resid = uio->uio_resid;
1044 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1045 if (orig_resid == 0)
1047 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1049 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1050 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1052 lbn = lblkno(fs, uio->uio_offset);
1056 * size of buffer. The buffer representing the
1057 * end of the file is rounded up to the size of
1058 * the block type ( fragment or full block,
1061 size = sblksize(fs, dp->di_extsize, lbn);
1062 blkoffset = blkoff(fs, uio->uio_offset);
1065 * The amount we want to transfer in this iteration is
1066 * one FS block less the amount of the data before
1067 * our startpoint (duh!)
1069 xfersize = fs->fs_bsize - blkoffset;
1072 * But if we actually want less than the block,
1073 * or the file doesn't have a whole block more of data,
1074 * then use the lesser number.
1076 if (uio->uio_resid < xfersize)
1077 xfersize = uio->uio_resid;
1078 if (bytesinfile < xfersize)
1079 xfersize = bytesinfile;
1081 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1083 * Don't do readahead if this is the end of the info.
1085 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1088 * If we have a second block, then
1089 * fire off a request for a readahead
1090 * as well as a read. Note that the 4th and 5th
1091 * arguments point to arrays of the size specified in
1094 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
1096 nextlbn = -1 - nextlbn;
1097 error = breadn(vp, -1 - lbn,
1098 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1107 * We should only get non-zero b_resid when an I/O error
1108 * has occurred, which should cause us to break above.
1109 * However, if the short read did not cause an error,
1110 * then we want to ensure that we do not uiomove bad
1111 * or uninitialized data.
1113 size -= bp->b_resid;
1114 if (size < xfersize) {
1120 error = uiomove((char *)bp->b_data + blkoffset,
1121 (int)xfersize, uio);
1124 vfs_bio_brelse(bp, ioflag);
1128 * This can only happen in the case of an error
1129 * because the loop above resets bp to NULL on each iteration
1130 * and on normal completion has not set a new value into it.
1131 * so it must have come from a 'break' statement
1134 vfs_bio_brelse(bp, ioflag);
1139 * Extended attribute area writing.
1142 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1145 struct ufs2_dinode *dp;
1151 int blkoffset, error, flags, size, xfersize;
1158 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1159 panic("ffs_extwrite: mode");
1162 if (ioflag & IO_APPEND)
1163 uio->uio_offset = dp->di_extsize;
1164 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1165 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1166 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1167 UFS_NXADDR * fs->fs_bsize)
1170 resid = uio->uio_resid;
1171 osize = dp->di_extsize;
1173 if (ioflag & IO_SYNC)
1176 for (error = 0; uio->uio_resid > 0;) {
1177 lbn = lblkno(fs, uio->uio_offset);
1178 blkoffset = blkoff(fs, uio->uio_offset);
1179 xfersize = fs->fs_bsize - blkoffset;
1180 if (uio->uio_resid < xfersize)
1181 xfersize = uio->uio_resid;
1184 * We must perform a read-before-write if the transfer size
1185 * does not cover the entire buffer.
1187 if (fs->fs_bsize > xfersize)
1190 flags &= ~BA_CLRBUF;
1191 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1196 * If the buffer is not valid we have to clear out any
1197 * garbage data from the pages instantiated for the buffer.
1198 * If we do not, a failed uiomove() during a write can leave
1199 * the prior contents of the pages exposed to a userland
1200 * mmap(). XXX deal with uiomove() errors a better way.
1202 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1205 if (uio->uio_offset + xfersize > dp->di_extsize) {
1206 dp->di_extsize = uio->uio_offset + xfersize;
1207 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
1210 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1211 if (size < xfersize)
1215 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1217 vfs_bio_set_flags(bp, ioflag);
1220 * If IO_SYNC each buffer is written synchronously. Otherwise
1221 * if we have a severe page deficiency write the buffer
1222 * asynchronously. Otherwise try to cluster, and if that
1223 * doesn't do it then either do an async write (if O_DIRECT),
1224 * or a delayed write (if not).
1226 if (ioflag & IO_SYNC) {
1228 } else if (vm_page_count_severe() ||
1229 buf_dirty_count_severe() ||
1230 xfersize + blkoffset == fs->fs_bsize ||
1231 (ioflag & (IO_ASYNC | IO_DIRECT)))
1235 if (error || xfersize == 0)
1237 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1240 * If we successfully wrote any data, and we are not the superuser
1241 * we clear the setuid and setgid bits as a precaution against
1244 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1245 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) {
1246 vn_seqc_write_begin(vp);
1247 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1248 dp->di_mode = ip->i_mode;
1249 vn_seqc_write_end(vp);
1253 if (ioflag & IO_UNIT) {
1254 (void)ffs_truncate(vp, osize,
1255 IO_EXT | (ioflag&IO_SYNC), ucred);
1256 uio->uio_offset -= resid - uio->uio_resid;
1257 uio->uio_resid = resid;
1259 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1260 error = ffs_update(vp, 1);
1265 * Vnode operating to retrieve a named extended attribute.
1267 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1268 * the length of the EA, and possibly the pointer to the entry and to the data.
1271 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name,
1272 struct extattr **eapp, u_char **eac)
1274 struct extattr *eap, *eaend;
1277 nlen = strlen(name);
1278 KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned"));
1279 eap = (struct extattr *)ptr;
1280 eaend = (struct extattr *)(ptr + length);
1281 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
1282 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1283 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1284 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
1285 || memcmp(eap->ea_name, name, nlen) != 0)
1290 *eac = EXTATTR_CONTENT(eap);
1291 return (EXTATTR_CONTENT_SIZE(eap));
1297 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td)
1299 const struct extattr *eap, *eaend, *eapnext;
1301 struct ufs2_dinode *dp;
1304 struct iovec liovec;
1312 easize = dp->di_extsize;
1313 if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize)
1316 eae = malloc(easize, M_TEMP, M_WAITOK);
1318 liovec.iov_base = eae;
1319 liovec.iov_len = easize;
1320 luio.uio_iov = &liovec;
1321 luio.uio_iovcnt = 1;
1322 luio.uio_offset = 0;
1323 luio.uio_resid = easize;
1324 luio.uio_segflg = UIO_SYSSPACE;
1325 luio.uio_rw = UIO_READ;
1328 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1333 /* Validate disk xattrfile contents. */
1334 for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend;
1336 eapnext = EXTATTR_NEXT(eap);
1337 /* Bogusly short entry or bogusly long entry. */
1338 if (eap->ea_length < sizeof(*eap) || eapnext > eaend) {
1340 return (EINTEGRITY);
1348 ffs_lock_ea(struct vnode *vp)
1354 while (ip->i_flag & IN_EA_LOCKED) {
1355 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT);
1356 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1359 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED);
1364 ffs_unlock_ea(struct vnode *vp)
1370 if (ip->i_flag & IN_EA_LOCKWAIT)
1371 wakeup(&ip->i_ea_refs);
1372 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1377 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1380 struct ufs2_dinode *dp;
1386 if (ip->i_ea_area != NULL) {
1392 error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1397 ip->i_ea_len = dp->di_extsize;
1405 * Vnode extattr transaction commit/abort
1408 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1412 struct iovec liovec;
1414 struct ufs2_dinode *dp;
1419 if (ip->i_ea_area == NULL) {
1424 error = ip->i_ea_error;
1425 if (commit && error == 0) {
1426 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1428 cred = vp->v_mount->mnt_cred;
1429 liovec.iov_base = ip->i_ea_area;
1430 liovec.iov_len = ip->i_ea_len;
1431 luio.uio_iov = &liovec;
1432 luio.uio_iovcnt = 1;
1433 luio.uio_offset = 0;
1434 luio.uio_resid = ip->i_ea_len;
1435 luio.uio_segflg = UIO_SYSSPACE;
1436 luio.uio_rw = UIO_WRITE;
1438 /* XXX: I'm not happy about truncating to zero size */
1439 if (ip->i_ea_len < dp->di_extsize)
1440 error = ffs_truncate(vp, 0, IO_EXT, cred);
1441 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1443 if (--ip->i_ea_refs == 0) {
1444 free(ip->i_ea_area, M_TEMP);
1445 ip->i_ea_area = NULL;
1454 * Vnode extattr strategy routine for fifos.
1456 * We need to check for a read or write of the external attributes.
1457 * Otherwise we just fall through and do the usual thing.
1460 ffsext_strategy(struct vop_strategy_args *ap)
1462 struct vop_strategy_args {
1463 struct vnodeop_desc *a_desc;
1473 lbn = ap->a_bp->b_lblkno;
1474 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1475 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1476 if (vp->v_type == VFIFO)
1477 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1478 panic("spec nodes went here");
1482 * Vnode extattr transaction commit/abort
1485 ffs_openextattr(struct vop_openextattr_args *ap)
1487 struct vop_openextattr_args {
1488 struct vnodeop_desc *a_desc;
1490 IN struct ucred *a_cred;
1491 IN struct thread *a_td;
1496 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1497 return (EOPNOTSUPP);
1499 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1503 * Vnode extattr transaction commit/abort
1506 ffs_closeextattr(struct vop_closeextattr_args *ap)
1508 struct vop_closeextattr_args {
1509 struct vnodeop_desc *a_desc;
1512 IN struct ucred *a_cred;
1513 IN struct thread *a_td;
1518 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1519 return (EOPNOTSUPP);
1521 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1524 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1528 * Vnode operation to remove a named attribute.
1531 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1534 IN struct vnode *a_vp;
1535 IN int a_attrnamespace;
1536 IN const char *a_name;
1537 IN struct ucred *a_cred;
1538 IN struct thread *a_td;
1543 struct extattr *eap;
1545 int olen, error, i, easize;
1549 ip = VTOI(ap->a_vp);
1551 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1552 return (EOPNOTSUPP);
1554 if (strlen(ap->a_name) == 0)
1557 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1560 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1561 ap->a_cred, ap->a_td, VWRITE);
1564 * ffs_lock_ea is not needed there, because the vnode
1565 * must be exclusively locked.
1567 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1568 ip->i_ea_error = error;
1572 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1576 /* CEM: delete could be done in-place instead */
1577 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1578 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1579 easize = ip->i_ea_len;
1581 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1584 /* delete but nonexistent */
1586 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1589 ul = eap->ea_length;
1590 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1591 bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1594 tmp = ip->i_ea_area;
1595 ip->i_ea_area = eae;
1596 ip->i_ea_len = easize;
1598 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1603 * Vnode operation to retrieve a named extended attribute.
1606 ffs_getextattr(struct vop_getextattr_args *ap)
1609 IN struct vnode *a_vp;
1610 IN int a_attrnamespace;
1611 IN const char *a_name;
1612 INOUT struct uio *a_uio;
1614 IN struct ucred *a_cred;
1615 IN struct thread *a_td;
1624 ip = VTOI(ap->a_vp);
1626 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1627 return (EOPNOTSUPP);
1629 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1630 ap->a_cred, ap->a_td, VREAD);
1634 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1638 eae = ip->i_ea_area;
1639 easize = ip->i_ea_len;
1641 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1645 if (ap->a_size != NULL)
1646 *ap->a_size = ealen;
1647 else if (ap->a_uio != NULL)
1648 error = uiomove(p, ealen, ap->a_uio);
1652 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1657 * Vnode operation to retrieve extended attributes on a vnode.
1660 ffs_listextattr(struct vop_listextattr_args *ap)
1663 IN struct vnode *a_vp;
1664 IN int a_attrnamespace;
1665 INOUT struct uio *a_uio;
1667 IN struct ucred *a_cred;
1668 IN struct thread *a_td;
1673 struct extattr *eap, *eaend;
1676 ip = VTOI(ap->a_vp);
1678 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1679 return (EOPNOTSUPP);
1681 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1682 ap->a_cred, ap->a_td, VREAD);
1686 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1691 if (ap->a_size != NULL)
1694 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1695 eap = (struct extattr *)ip->i_ea_area;
1696 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1697 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1698 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1699 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1700 if (eap->ea_namespace != ap->a_attrnamespace)
1703 ealen = eap->ea_namelength;
1704 if (ap->a_size != NULL)
1705 *ap->a_size += ealen + 1;
1706 else if (ap->a_uio != NULL)
1707 error = uiomove(&eap->ea_namelength, ealen + 1,
1711 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1716 * Vnode operation to set a named attribute.
1719 ffs_setextattr(struct vop_setextattr_args *ap)
1722 IN struct vnode *a_vp;
1723 IN int a_attrnamespace;
1724 IN const char *a_name;
1725 INOUT struct uio *a_uio;
1726 IN struct ucred *a_cred;
1727 IN struct thread *a_td;
1733 struct extattr *eap;
1734 uint32_t ealength, ul;
1736 int olen, eapad1, eapad2, error, i, easize;
1740 ip = VTOI(ap->a_vp);
1743 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1744 return (EOPNOTSUPP);
1746 if (strlen(ap->a_name) == 0)
1749 /* XXX Now unsupported API to delete EAs using NULL uio. */
1750 if (ap->a_uio == NULL)
1751 return (EOPNOTSUPP);
1753 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1756 ealen = ap->a_uio->uio_resid;
1757 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1760 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1761 ap->a_cred, ap->a_td, VWRITE);
1764 * ffs_lock_ea is not needed there, because the vnode
1765 * must be exclusively locked.
1767 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1768 ip->i_ea_error = error;
1772 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1776 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1777 eapad1 = roundup2(ealength, 8) - ealength;
1778 eapad2 = roundup2(ealen, 8) - ealen;
1779 ealength += eapad1 + ealen + eapad2;
1782 * CEM: rewrites of the same size or smaller could be done in-place
1783 * instead. (We don't acquire any fine-grained locks in here either,
1784 * so we could also do bigger writes in-place.)
1786 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1787 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1788 easize = ip->i_ea_len;
1790 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1793 /* new, append at end */
1794 KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1796 eap = (struct extattr *)(eae + easize);
1799 ul = eap->ea_length;
1800 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1801 if (ul != ealength) {
1802 bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
1804 easize += (ealength - ul);
1807 if (easize > lblktosize(fs, UFS_NXADDR)) {
1809 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1810 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1811 ip->i_ea_error = ENOSPC;
1814 eap->ea_length = ealength;
1815 eap->ea_namespace = ap->a_attrnamespace;
1816 eap->ea_contentpadlen = eapad2;
1817 eap->ea_namelength = strlen(ap->a_name);
1818 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1819 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1820 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1823 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1824 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1825 ip->i_ea_error = error;
1828 bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1830 tmp = ip->i_ea_area;
1831 ip->i_ea_area = eae;
1832 ip->i_ea_len = easize;
1834 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1839 * Vnode pointer to File handle
1842 ffs_vptofh(struct vop_vptofh_args *ap)
1845 IN struct vnode *a_vp;
1846 IN struct fid *a_fhp;
1853 ip = VTOI(ap->a_vp);
1854 ufhp = (struct ufid *)ap->a_fhp;
1855 ufhp->ufid_len = sizeof(struct ufid);
1856 ufhp->ufid_ino = ip->i_number;
1857 ufhp->ufid_gen = ip->i_gen;
1861 SYSCTL_DECL(_vfs_ffs);
1862 static int use_buf_pager = 1;
1863 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1864 "Always use buffer pager instead of bmap");
1867 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1870 return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1874 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1877 return (blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn));
1881 ffs_getpages(struct vop_getpages_args *ap)
1884 struct ufsmount *um;
1887 um = VFSTOUFS(vp->v_mount);
1889 if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1890 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1891 ap->a_rbehind, ap->a_rahead, NULL, NULL));
1892 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1893 ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1897 ffs_getpages_async(struct vop_getpages_async_args *ap)
1900 struct ufsmount *um;
1905 um = VFSTOUFS(vp->v_mount);
1908 if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1909 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1910 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1914 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1915 ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1918 if (do_iodone && ap->a_iodone != NULL)
1919 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
1925 ffs_vput_pair(struct vop_vput_pair_args *ap)
1927 struct vnode *dvp, *vp, **vpp;
1929 int error, vp_locked;
1934 vp = vpp != NULL ? *vpp : NULL;
1936 if ((dp->i_flag & IN_NEEDSYNC) == 0) {
1938 if (vp != NULL && ap->a_unlock_vp)
1944 if (ap->a_unlock_vp) {
1947 MPASS(vp->v_type != VNON);
1948 vp_locked = VOP_ISLOCKED(vp);
1954 error = ffs_syncvnode(dvp, MNT_WAIT, 0);
1955 } while (error == ERELOOKUP);
1958 if (vp == NULL || ap->a_unlock_vp)
1962 * It is possible that vp is reclaimed at this point. Only
1963 * routines that call us with a_unlock_vp == false can find
1964 * that their vp has been reclaimed. There are three areas
1965 * that are affected:
1966 * 1) vn_open_cred() - later VOPs could fail, but
1967 * dead_open() returns 0 to simulate successful open.
1968 * 2) ffs_snapshot() - creation of snapshot fails with EBADF.
1969 * 3) NFS server (several places) - code is prepared to detect
1970 * and respond to dead vnodes by returning ESTALE.
1972 VOP_LOCK(vp, vp_locked | LK_RETRY);