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 <sys/param.h>
71 #include <sys/systm.h>
74 #include <sys/extattr.h>
75 #include <sys/kernel.h>
76 #include <sys/limits.h>
77 #include <sys/malloc.h>
78 #include <sys/mount.h>
80 #include <sys/rwlock.h>
82 #include <sys/sysctl.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vnode.h>
87 #include <vm/vm_param.h>
88 #include <vm/vm_extern.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vnode_pager.h>
94 #include <ufs/ufs/extattr.h>
95 #include <ufs/ufs/quota.h>
96 #include <ufs/ufs/inode.h>
97 #include <ufs/ufs/ufs_extern.h>
98 #include <ufs/ufs/ufsmount.h>
100 #include <ufs/ffs/fs.h>
101 #include <ufs/ffs/ffs_extern.h>
102 #include "opt_directio.h"
105 #define ALIGNED_TO(ptr, s) \
106 (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
109 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
111 static vop_fdatasync_t ffs_fdatasync;
112 static vop_fsync_t ffs_fsync;
113 static vop_getpages_t ffs_getpages;
114 static vop_getpages_async_t ffs_getpages_async;
115 static vop_lock1_t ffs_lock;
117 static vop_unlock_t ffs_unlock_debug;
119 static vop_read_t ffs_read;
120 static vop_write_t ffs_write;
121 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
122 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
124 static vop_strategy_t ffsext_strategy;
125 static vop_closeextattr_t ffs_closeextattr;
126 static vop_deleteextattr_t ffs_deleteextattr;
127 static vop_getextattr_t ffs_getextattr;
128 static vop_listextattr_t ffs_listextattr;
129 static vop_openextattr_t ffs_openextattr;
130 static vop_setextattr_t ffs_setextattr;
131 static vop_vptofh_t ffs_vptofh;
133 /* Global vfs data structures for ufs. */
134 struct vop_vector ffs_vnodeops1 = {
135 .vop_default = &ufs_vnodeops,
136 .vop_fsync = ffs_fsync,
137 .vop_fdatasync = ffs_fdatasync,
138 .vop_getpages = ffs_getpages,
139 .vop_getpages_async = ffs_getpages_async,
140 .vop_lock1 = ffs_lock,
142 .vop_unlock = ffs_unlock_debug,
144 .vop_read = ffs_read,
145 .vop_reallocblks = ffs_reallocblks,
146 .vop_write = ffs_write,
147 .vop_vptofh = ffs_vptofh,
149 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
151 struct vop_vector ffs_fifoops1 = {
152 .vop_default = &ufs_fifoops,
153 .vop_fsync = ffs_fsync,
154 .vop_fdatasync = ffs_fdatasync,
155 .vop_lock1 = ffs_lock,
157 .vop_unlock = ffs_unlock_debug,
159 .vop_vptofh = ffs_vptofh,
161 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
163 /* Global vfs data structures for ufs. */
164 struct vop_vector ffs_vnodeops2 = {
165 .vop_default = &ufs_vnodeops,
166 .vop_fsync = ffs_fsync,
167 .vop_fdatasync = ffs_fdatasync,
168 .vop_getpages = ffs_getpages,
169 .vop_getpages_async = ffs_getpages_async,
170 .vop_lock1 = ffs_lock,
172 .vop_unlock = ffs_unlock_debug,
174 .vop_read = ffs_read,
175 .vop_reallocblks = ffs_reallocblks,
176 .vop_write = ffs_write,
177 .vop_closeextattr = ffs_closeextattr,
178 .vop_deleteextattr = ffs_deleteextattr,
179 .vop_getextattr = ffs_getextattr,
180 .vop_listextattr = ffs_listextattr,
181 .vop_openextattr = ffs_openextattr,
182 .vop_setextattr = ffs_setextattr,
183 .vop_vptofh = ffs_vptofh,
185 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
187 struct vop_vector ffs_fifoops2 = {
188 .vop_default = &ufs_fifoops,
189 .vop_fsync = ffs_fsync,
190 .vop_fdatasync = ffs_fdatasync,
191 .vop_lock1 = ffs_lock,
193 .vop_unlock = ffs_unlock_debug,
195 .vop_reallocblks = ffs_reallocblks,
196 .vop_strategy = ffsext_strategy,
197 .vop_closeextattr = ffs_closeextattr,
198 .vop_deleteextattr = ffs_deleteextattr,
199 .vop_getextattr = ffs_getextattr,
200 .vop_listextattr = ffs_listextattr,
201 .vop_openextattr = ffs_openextattr,
202 .vop_setextattr = ffs_setextattr,
203 .vop_vptofh = ffs_vptofh,
205 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
208 * Synch an open file.
212 ffs_fsync(struct vop_fsync_args *ap)
221 error = ffs_syncvnode(vp, ap->a_waitfor, 0);
224 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
225 error = softdep_fsync(vp);
230 * The softdep_fsync() function may drop vp lock,
231 * allowing for dirty buffers to reappear on the
232 * bo_dirty list. Recheck and resync as needed.
235 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
236 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
242 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
248 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
252 struct ufsmount *ump;
253 struct buf *bp, *nbp;
256 bool still_dirty, unlocked, wait;
259 ip->i_flag &= ~IN_NEEDSYNC;
261 ump = VFSTOUFS(vp->v_mount);
264 * When doing MNT_WAIT we must first flush all dependencies
267 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
268 (error = softdep_sync_metadata(vp)) != 0) {
269 if (ffs_fsfail_cleanup(ump, error))
275 * Flush all dirty buffers associated with a vnode.
279 wait = false; /* Always do an async pass first. */
281 lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
284 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
285 bp->b_vflags &= ~BV_SCANNED;
286 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
288 * Reasons to skip this buffer: it has already been considered
289 * on this pass, the buffer has dependencies that will cause
290 * it to be redirtied and it has not already been deferred,
291 * or it is already being written.
293 if ((bp->b_vflags & BV_SCANNED) != 0)
295 bp->b_vflags |= BV_SCANNED;
297 * Flush indirects in order, if requested.
299 * Note that if only datasync is requested, we can
300 * skip indirect blocks when softupdates are not
301 * active. Otherwise we must flush them with data,
302 * since dependencies prevent data block writes.
304 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
305 (lbn_level(bp->b_lblkno) >= passes ||
306 ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
308 if (bp->b_lblkno > lbn)
309 panic("ffs_syncvnode: syncing truncated data.");
310 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
314 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
315 BO_LOCKPTR(bo)) != 0) {
316 bp->b_vflags &= ~BV_SCANNED;
321 if ((bp->b_flags & B_DELWRI) == 0)
322 panic("ffs_fsync: not dirty");
324 * Check for dependencies and potentially complete them.
326 if (!LIST_EMPTY(&bp->b_dep) &&
327 (error = softdep_sync_buf(vp, bp,
328 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
330 * Lock order conflict, buffer was already unlocked,
331 * and vnode possibly unlocked.
333 if (error == ERELOOKUP) {
334 if (vp->v_data == NULL)
337 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
338 (error = softdep_sync_metadata(vp)) != 0) {
339 if (ffs_fsfail_cleanup(ump, error))
341 return (unlocked && error == 0 ?
344 /* Re-evaluate inode size */
345 lbn = lblkno(ITOFS(ip), (ip->i_size +
346 ITOFS(ip)->fs_bsize - 1));
350 if (error != EBUSY) {
354 /* If we deferred once, don't defer again. */
355 if ((bp->b_flags & B_DEFERRED) == 0) {
356 bp->b_flags |= B_DEFERRED;
364 if (ffs_fsfail_cleanup(ump, error))
368 } else if ((bp->b_flags & B_CLUSTEROK)) {
369 (void) vfs_bio_awrite(bp);
376 * Since we may have slept during the I/O, we need
377 * to start from a known point.
380 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
382 if (waitfor != MNT_WAIT) {
384 if ((flags & NO_INO_UPDT) != 0)
385 return (unlocked ? ERELOOKUP : 0);
386 error = ffs_update(vp, 0);
387 if (error == 0 && unlocked)
391 /* Drain IO to see if we're done. */
392 bufobj_wwait(bo, 0, 0);
394 * Block devices associated with filesystems may have new I/O
395 * requests posted for them even if the vnode is locked, so no
396 * amount of trying will get them clean. We make several passes
399 * Regular files may need multiple passes to flush all dependency
400 * work as it is possible that we must write once per indirect
401 * level, once for the leaf, and once for the inode and each of
402 * these will be done with one sync and one async pass.
404 if (bo->bo_dirty.bv_cnt > 0) {
405 if ((flags & DATA_ONLY) == 0) {
409 * For data-only sync, dirty indirect buffers
413 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
414 if (bp->b_lblkno > -UFS_NDADDR) {
422 /* Write the inode after sync passes to flush deps. */
423 if (wait && DOINGSOFTDEP(vp) &&
424 (flags & NO_INO_UPDT) == 0) {
429 /* switch between sync/async. */
431 if (wait || ++passes < UFS_NIADDR + 2)
437 if ((flags & DATA_ONLY) == 0) {
438 if ((flags & NO_INO_UPDT) == 0)
439 error = ffs_update(vp, 1);
441 softdep_journal_fsync(VTOI(vp));
442 } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
443 error = ffs_update(vp, 1);
445 if (error == 0 && unlocked)
451 ffs_fdatasync(struct vop_fdatasync_args *ap)
454 return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
459 struct vop_lock1_args /* {
466 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
467 struct vnode *vp = ap->a_vp;
468 #endif /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
471 #endif /* DIAGNOSTIC */
473 #ifndef NO_FFS_SNAPSHOT
478 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
479 * and only use it when LK_NODDLKTREAT is set. Currently this means it
480 * is only used during path lookup.
482 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
483 ap->a_flags |= LK_ADAPTIVE;
484 switch (ap->a_flags & LK_TYPE_MASK) {
490 #ifdef DEBUG_VFS_LOCKS
491 VNPASS(vp->v_holdcnt != 0, vp);
492 #endif /* DEBUG_VFS_LOCKS */
494 result = lockmgr_lock_flags(lkp, flags,
495 &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
496 if (lkp == vp->v_vnlock || result != 0)
499 * Apparent success, except that the vnode
500 * mutated between snapshot file vnode and
501 * regular file vnode while this process
502 * slept. The lock currently held is not the
503 * right lock. Release it, and try to get the
507 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
508 (LK_INTERLOCK | LK_NOWAIT))
510 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
511 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
512 flags &= ~LK_INTERLOCK;
515 switch (ap->a_flags & LK_TYPE_MASK) {
518 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
524 #endif /* DIAGNOSTIC */
528 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
531 ufs_unlock_tracker(ip);
533 #endif /* DIAGNOSTIC */
534 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
537 #else /* NO_FFS_SNAPSHOT */
539 * See above for an explanation.
541 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
542 ap->a_flags |= LK_ADAPTIVE;
544 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
547 ufs_unlock_tracker(ip);
549 #endif /* DIAGNOSTIC */
550 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
551 #endif /* NO_FFS_SNAPSHOT */
553 switch (ap->a_flags & LK_TYPE_MASK) {
556 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
562 #endif /* DIAGNOSTIC */
568 ffs_unlock_debug(struct vop_unlock_args *ap)
575 if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
576 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
578 VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
579 ("%s: modified vnode (%x) not on lazy list",
580 __func__, ip->i_flag));
585 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
586 vp->v_vnlock->lk_recurse == 0)
587 ufs_unlock_tracker(ip);
589 return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
594 ffs_read_hole(struct uio *uio, long xfersize, long *size)
596 ssize_t saved_resid, tlen;
599 while (xfersize > 0) {
600 tlen = min(xfersize, ZERO_REGION_SIZE);
601 saved_resid = uio->uio_resid;
602 error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
606 tlen = saved_resid - uio->uio_resid;
614 * Vnode op for reading.
618 struct vop_read_args /* {
622 struct ucred *a_cred;
630 ufs_lbn_t lbn, nextlbn;
632 long size, xfersize, blkoffset;
634 int bflag, error, ioflag, seqcount;
638 ioflag = ap->a_ioflag;
639 if (ap->a_ioflag & IO_EXT)
641 return (ffs_extread(vp, uio, ioflag));
643 panic("ffs_read+IO_EXT");
646 if ((ioflag & IO_DIRECT) != 0) {
649 error = ffs_rawread(vp, uio, &workdone);
650 if (error != 0 || workdone != 0)
655 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
659 if (uio->uio_rw != UIO_READ)
660 panic("ffs_read: mode");
662 if (vp->v_type == VLNK) {
663 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
664 panic("ffs_read: short symlink");
665 } else if (vp->v_type != VREG && vp->v_type != VDIR)
666 panic("ffs_read: type %d", vp->v_type);
668 orig_resid = uio->uio_resid;
669 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
672 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
674 if (uio->uio_offset < ip->i_size &&
675 uio->uio_offset >= fs->fs_maxfilesize)
678 bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
679 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
680 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
682 lbn = lblkno(fs, uio->uio_offset);
686 * size of buffer. The buffer representing the
687 * end of the file is rounded up to the size of
688 * the block type ( fragment or full block,
691 size = blksize(fs, ip, lbn);
692 blkoffset = blkoff(fs, uio->uio_offset);
695 * The amount we want to transfer in this iteration is
696 * one FS block less the amount of the data before
697 * our startpoint (duh!)
699 xfersize = fs->fs_bsize - blkoffset;
702 * But if we actually want less than the block,
703 * or the file doesn't have a whole block more of data,
704 * then use the lesser number.
706 if (uio->uio_resid < xfersize)
707 xfersize = uio->uio_resid;
708 if (bytesinfile < xfersize)
709 xfersize = bytesinfile;
711 if (lblktosize(fs, nextlbn) >= ip->i_size) {
713 * Don't do readahead if this is the end of the file.
715 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
716 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
718 * Otherwise if we are allowed to cluster,
719 * grab as much as we can.
721 * XXX This may not be a win if we are not
722 * doing sequential access.
724 error = cluster_read(vp, ip->i_size, lbn,
725 size, NOCRED, blkoffset + uio->uio_resid,
726 seqcount, bflag, &bp);
727 } else if (seqcount > 1) {
729 * If we are NOT allowed to cluster, then
730 * if we appear to be acting sequentially,
731 * fire off a request for a readahead
732 * as well as a read. Note that the 4th and 5th
733 * arguments point to arrays of the size specified in
736 u_int nextsize = blksize(fs, ip, nextlbn);
737 error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
738 &nextsize, 1, NOCRED, bflag, NULL, &bp);
741 * Failing all of the above, just read what the
742 * user asked for. Interestingly, the same as
743 * the first option above.
745 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
747 if (error == EJUSTRETURN) {
748 error = ffs_read_hole(uio, xfersize, &size);
759 * We should only get non-zero b_resid when an I/O error
760 * has occurred, which should cause us to break above.
761 * However, if the short read did not cause an error,
762 * then we want to ensure that we do not uiomove bad
763 * or uninitialized data.
766 if (size < xfersize) {
772 if (buf_mapped(bp)) {
773 error = vn_io_fault_uiomove((char *)bp->b_data +
774 blkoffset, (int)xfersize, uio);
776 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
782 vfs_bio_brelse(bp, ioflag);
786 * This can only happen in the case of an error
787 * because the loop above resets bp to NULL on each iteration
788 * and on normal completion has not set a new value into it.
789 * so it must have come from a 'break' statement
792 vfs_bio_brelse(bp, ioflag);
794 if ((error == 0 || uio->uio_resid != orig_resid) &&
795 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
796 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
801 * Vnode op for writing.
805 struct vop_write_args /* {
809 struct ucred *a_cred;
821 int blkoffset, error, flags, ioflag, size, xfersize;
825 ioflag = ap->a_ioflag;
826 if (ap->a_ioflag & IO_EXT)
828 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
830 panic("ffs_write+IO_EXT");
833 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
837 if (uio->uio_rw != UIO_WRITE)
838 panic("ffs_write: mode");
841 switch (vp->v_type) {
843 if (ioflag & IO_APPEND)
844 uio->uio_offset = ip->i_size;
845 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
851 panic("ffs_write: dir write");
854 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
855 (int)uio->uio_offset,
860 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
861 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
863 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
866 * Maybe this should be above the vnode op call, but so long as
867 * file servers have no limits, I don't think it matters.
869 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
872 resid = uio->uio_resid;
874 if (seqcount > BA_SEQMAX)
875 flags = BA_SEQMAX << BA_SEQSHIFT;
877 flags = seqcount << BA_SEQSHIFT;
878 if (ioflag & IO_SYNC)
880 flags |= BA_UNMAPPED;
882 for (error = 0; uio->uio_resid > 0;) {
883 lbn = lblkno(fs, uio->uio_offset);
884 blkoffset = blkoff(fs, uio->uio_offset);
885 xfersize = fs->fs_bsize - blkoffset;
886 if (uio->uio_resid < xfersize)
887 xfersize = uio->uio_resid;
888 if (uio->uio_offset + xfersize > ip->i_size)
889 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
892 * We must perform a read-before-write if the transfer size
893 * does not cover the entire buffer.
895 if (fs->fs_bsize > xfersize)
899 /* XXX is uio->uio_offset the right thing here? */
900 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
901 ap->a_cred, flags, &bp);
903 vnode_pager_setsize(vp, ip->i_size);
906 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
907 bp->b_flags |= B_NOCACHE;
909 if (uio->uio_offset + xfersize > ip->i_size) {
910 ip->i_size = uio->uio_offset + xfersize;
911 DIP_SET(ip, i_size, ip->i_size);
912 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
915 size = blksize(fs, ip, lbn) - bp->b_resid;
919 if (buf_mapped(bp)) {
920 error = vn_io_fault_uiomove((char *)bp->b_data +
921 blkoffset, (int)xfersize, uio);
923 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
927 * If the buffer is not already filled and we encounter an
928 * error while trying to fill it, we have to clear out any
929 * garbage data from the pages instantiated for the buffer.
930 * If we do not, a failed uiomove() during a write can leave
931 * the prior contents of the pages exposed to a userland mmap.
933 * Note that we need only clear buffers with a transfer size
934 * equal to the block size because buffers with a shorter
935 * transfer size were cleared above by the call to UFS_BALLOC()
936 * with the BA_CLRBUF flag set.
938 * If the source region for uiomove identically mmaps the
939 * buffer, uiomove() performed the NOP copy, and the buffer
940 * content remains valid because the page fault handler
941 * validated the pages.
943 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
944 fs->fs_bsize == xfersize)
947 vfs_bio_set_flags(bp, ioflag);
950 * If IO_SYNC each buffer is written synchronously. Otherwise
951 * if we have a severe page deficiency write the buffer
952 * asynchronously. Otherwise try to cluster, and if that
953 * doesn't do it then either do an async write (if O_DIRECT),
954 * or a delayed write (if not).
956 if (ioflag & IO_SYNC) {
958 } else if (vm_page_count_severe() ||
959 buf_dirty_count_severe() ||
960 (ioflag & IO_ASYNC)) {
961 bp->b_flags |= B_CLUSTEROK;
963 } else if (xfersize + blkoffset == fs->fs_bsize) {
964 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
965 bp->b_flags |= B_CLUSTEROK;
966 cluster_write(vp, bp, ip->i_size, seqcount,
971 } else if (ioflag & IO_DIRECT) {
972 bp->b_flags |= B_CLUSTEROK;
975 bp->b_flags |= B_CLUSTEROK;
978 if (error || xfersize == 0)
980 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
983 * If we successfully wrote any data, and we are not the superuser
984 * we clear the setuid and setgid bits as a precaution against
987 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
989 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
990 vn_seqc_write_begin(vp);
991 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
992 DIP_SET(ip, i_mode, ip->i_mode);
993 vn_seqc_write_end(vp);
997 if (ioflag & IO_UNIT) {
998 (void)ffs_truncate(vp, osize,
999 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1000 uio->uio_offset -= resid - uio->uio_resid;
1001 uio->uio_resid = resid;
1003 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1004 if (!(ioflag & IO_DATASYNC) ||
1005 (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1006 error = ffs_update(vp, 1);
1007 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1014 * Extended attribute area reading.
1017 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1020 struct ufs2_dinode *dp;
1023 ufs_lbn_t lbn, nextlbn;
1025 long size, xfersize, blkoffset;
1034 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1035 panic("ffs_extread: mode");
1038 orig_resid = uio->uio_resid;
1039 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1040 if (orig_resid == 0)
1042 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1044 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1045 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1047 lbn = lblkno(fs, uio->uio_offset);
1051 * size of buffer. The buffer representing the
1052 * end of the file is rounded up to the size of
1053 * the block type ( fragment or full block,
1056 size = sblksize(fs, dp->di_extsize, lbn);
1057 blkoffset = blkoff(fs, uio->uio_offset);
1060 * The amount we want to transfer in this iteration is
1061 * one FS block less the amount of the data before
1062 * our startpoint (duh!)
1064 xfersize = fs->fs_bsize - blkoffset;
1067 * But if we actually want less than the block,
1068 * or the file doesn't have a whole block more of data,
1069 * then use the lesser number.
1071 if (uio->uio_resid < xfersize)
1072 xfersize = uio->uio_resid;
1073 if (bytesinfile < xfersize)
1074 xfersize = bytesinfile;
1076 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1078 * Don't do readahead if this is the end of the info.
1080 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1083 * If we have a second block, then
1084 * fire off a request for a readahead
1085 * as well as a read. Note that the 4th and 5th
1086 * arguments point to arrays of the size specified in
1089 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
1091 nextlbn = -1 - nextlbn;
1092 error = breadn(vp, -1 - lbn,
1093 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1102 * We should only get non-zero b_resid when an I/O error
1103 * has occurred, which should cause us to break above.
1104 * However, if the short read did not cause an error,
1105 * then we want to ensure that we do not uiomove bad
1106 * or uninitialized data.
1108 size -= bp->b_resid;
1109 if (size < xfersize) {
1115 error = uiomove((char *)bp->b_data + blkoffset,
1116 (int)xfersize, uio);
1119 vfs_bio_brelse(bp, ioflag);
1123 * This can only happen in the case of an error
1124 * because the loop above resets bp to NULL on each iteration
1125 * and on normal completion has not set a new value into it.
1126 * so it must have come from a 'break' statement
1129 vfs_bio_brelse(bp, ioflag);
1134 * Extended attribute area writing.
1137 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1140 struct ufs2_dinode *dp;
1146 int blkoffset, error, flags, size, xfersize;
1153 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1154 panic("ffs_extwrite: mode");
1157 if (ioflag & IO_APPEND)
1158 uio->uio_offset = dp->di_extsize;
1159 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1160 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1161 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1162 UFS_NXADDR * fs->fs_bsize)
1165 resid = uio->uio_resid;
1166 osize = dp->di_extsize;
1168 if (ioflag & IO_SYNC)
1171 for (error = 0; uio->uio_resid > 0;) {
1172 lbn = lblkno(fs, uio->uio_offset);
1173 blkoffset = blkoff(fs, uio->uio_offset);
1174 xfersize = fs->fs_bsize - blkoffset;
1175 if (uio->uio_resid < xfersize)
1176 xfersize = uio->uio_resid;
1179 * We must perform a read-before-write if the transfer size
1180 * does not cover the entire buffer.
1182 if (fs->fs_bsize > xfersize)
1185 flags &= ~BA_CLRBUF;
1186 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1191 * If the buffer is not valid we have to clear out any
1192 * garbage data from the pages instantiated for the buffer.
1193 * If we do not, a failed uiomove() during a write can leave
1194 * the prior contents of the pages exposed to a userland
1195 * mmap(). XXX deal with uiomove() errors a better way.
1197 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1200 if (uio->uio_offset + xfersize > dp->di_extsize) {
1201 dp->di_extsize = uio->uio_offset + xfersize;
1202 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
1205 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1206 if (size < xfersize)
1210 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1212 vfs_bio_set_flags(bp, ioflag);
1215 * If IO_SYNC each buffer is written synchronously. Otherwise
1216 * if we have a severe page deficiency write the buffer
1217 * asynchronously. Otherwise try to cluster, and if that
1218 * doesn't do it then either do an async write (if O_DIRECT),
1219 * or a delayed write (if not).
1221 if (ioflag & IO_SYNC) {
1223 } else if (vm_page_count_severe() ||
1224 buf_dirty_count_severe() ||
1225 xfersize + blkoffset == fs->fs_bsize ||
1226 (ioflag & (IO_ASYNC | IO_DIRECT)))
1230 if (error || xfersize == 0)
1232 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1235 * If we successfully wrote any data, and we are not the superuser
1236 * we clear the setuid and setgid bits as a precaution against
1239 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1240 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) {
1241 vn_seqc_write_begin(vp);
1242 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1243 dp->di_mode = ip->i_mode;
1244 vn_seqc_write_end(vp);
1248 if (ioflag & IO_UNIT) {
1249 (void)ffs_truncate(vp, osize,
1250 IO_EXT | (ioflag&IO_SYNC), ucred);
1251 uio->uio_offset -= resid - uio->uio_resid;
1252 uio->uio_resid = resid;
1254 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1255 error = ffs_update(vp, 1);
1260 * Vnode operating to retrieve a named extended attribute.
1262 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1263 * the length of the EA, and possibly the pointer to the entry and to the data.
1266 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name,
1267 struct extattr **eapp, u_char **eac)
1269 struct extattr *eap, *eaend;
1272 nlen = strlen(name);
1273 KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned"));
1274 eap = (struct extattr *)ptr;
1275 eaend = (struct extattr *)(ptr + length);
1276 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
1277 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1278 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1279 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
1280 || memcmp(eap->ea_name, name, nlen) != 0)
1285 *eac = EXTATTR_CONTENT(eap);
1286 return (EXTATTR_CONTENT_SIZE(eap));
1292 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td)
1294 const struct extattr *eap, *eaend, *eapnext;
1296 struct ufs2_dinode *dp;
1299 struct iovec liovec;
1307 easize = dp->di_extsize;
1308 if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize)
1311 eae = malloc(easize, M_TEMP, M_WAITOK);
1313 liovec.iov_base = eae;
1314 liovec.iov_len = easize;
1315 luio.uio_iov = &liovec;
1316 luio.uio_iovcnt = 1;
1317 luio.uio_offset = 0;
1318 luio.uio_resid = easize;
1319 luio.uio_segflg = UIO_SYSSPACE;
1320 luio.uio_rw = UIO_READ;
1323 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1328 /* Validate disk xattrfile contents. */
1329 for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend;
1331 eapnext = EXTATTR_NEXT(eap);
1332 /* Bogusly short entry or bogusly long entry. */
1333 if (eap->ea_length < sizeof(*eap) || eapnext > eaend) {
1335 return (EINTEGRITY);
1343 ffs_lock_ea(struct vnode *vp)
1349 while (ip->i_flag & IN_EA_LOCKED) {
1350 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT);
1351 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1354 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED);
1359 ffs_unlock_ea(struct vnode *vp)
1365 if (ip->i_flag & IN_EA_LOCKWAIT)
1366 wakeup(&ip->i_ea_refs);
1367 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1372 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1375 struct ufs2_dinode *dp;
1381 if (ip->i_ea_area != NULL) {
1387 error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1392 ip->i_ea_len = dp->di_extsize;
1400 * Vnode extattr transaction commit/abort
1403 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1407 struct iovec liovec;
1409 struct ufs2_dinode *dp;
1414 if (ip->i_ea_area == NULL) {
1419 error = ip->i_ea_error;
1420 if (commit && error == 0) {
1421 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1423 cred = vp->v_mount->mnt_cred;
1424 liovec.iov_base = ip->i_ea_area;
1425 liovec.iov_len = ip->i_ea_len;
1426 luio.uio_iov = &liovec;
1427 luio.uio_iovcnt = 1;
1428 luio.uio_offset = 0;
1429 luio.uio_resid = ip->i_ea_len;
1430 luio.uio_segflg = UIO_SYSSPACE;
1431 luio.uio_rw = UIO_WRITE;
1433 /* XXX: I'm not happy about truncating to zero size */
1434 if (ip->i_ea_len < dp->di_extsize)
1435 error = ffs_truncate(vp, 0, IO_EXT, cred);
1436 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1438 if (--ip->i_ea_refs == 0) {
1439 free(ip->i_ea_area, M_TEMP);
1440 ip->i_ea_area = NULL;
1449 * Vnode extattr strategy routine for fifos.
1451 * We need to check for a read or write of the external attributes.
1452 * Otherwise we just fall through and do the usual thing.
1455 ffsext_strategy(struct vop_strategy_args *ap)
1457 struct vop_strategy_args {
1458 struct vnodeop_desc *a_desc;
1468 lbn = ap->a_bp->b_lblkno;
1469 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1470 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1471 if (vp->v_type == VFIFO)
1472 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1473 panic("spec nodes went here");
1477 * Vnode extattr transaction commit/abort
1480 ffs_openextattr(struct vop_openextattr_args *ap)
1482 struct vop_openextattr_args {
1483 struct vnodeop_desc *a_desc;
1485 IN struct ucred *a_cred;
1486 IN struct thread *a_td;
1491 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1492 return (EOPNOTSUPP);
1494 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1498 * Vnode extattr transaction commit/abort
1501 ffs_closeextattr(struct vop_closeextattr_args *ap)
1503 struct vop_closeextattr_args {
1504 struct vnodeop_desc *a_desc;
1507 IN struct ucred *a_cred;
1508 IN struct thread *a_td;
1513 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1514 return (EOPNOTSUPP);
1516 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1519 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1523 * Vnode operation to remove a named attribute.
1526 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1529 IN struct vnode *a_vp;
1530 IN int a_attrnamespace;
1531 IN const char *a_name;
1532 IN struct ucred *a_cred;
1533 IN struct thread *a_td;
1538 struct extattr *eap;
1540 int olen, error, i, easize;
1544 ip = VTOI(ap->a_vp);
1546 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1547 return (EOPNOTSUPP);
1549 if (strlen(ap->a_name) == 0)
1552 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1555 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1556 ap->a_cred, ap->a_td, VWRITE);
1559 * ffs_lock_ea is not needed there, because the vnode
1560 * must be exclusively locked.
1562 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1563 ip->i_ea_error = error;
1567 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1571 /* CEM: delete could be done in-place instead */
1572 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1573 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1574 easize = ip->i_ea_len;
1576 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1579 /* delete but nonexistent */
1581 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1584 ul = eap->ea_length;
1585 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1586 bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1589 tmp = ip->i_ea_area;
1590 ip->i_ea_area = eae;
1591 ip->i_ea_len = easize;
1593 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1598 * Vnode operation to retrieve a named extended attribute.
1601 ffs_getextattr(struct vop_getextattr_args *ap)
1604 IN struct vnode *a_vp;
1605 IN int a_attrnamespace;
1606 IN const char *a_name;
1607 INOUT struct uio *a_uio;
1609 IN struct ucred *a_cred;
1610 IN struct thread *a_td;
1619 ip = VTOI(ap->a_vp);
1621 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1622 return (EOPNOTSUPP);
1624 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1625 ap->a_cred, ap->a_td, VREAD);
1629 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1633 eae = ip->i_ea_area;
1634 easize = ip->i_ea_len;
1636 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1640 if (ap->a_size != NULL)
1641 *ap->a_size = ealen;
1642 else if (ap->a_uio != NULL)
1643 error = uiomove(p, ealen, ap->a_uio);
1647 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1652 * Vnode operation to retrieve extended attributes on a vnode.
1655 ffs_listextattr(struct vop_listextattr_args *ap)
1658 IN struct vnode *a_vp;
1659 IN int a_attrnamespace;
1660 INOUT struct uio *a_uio;
1662 IN struct ucred *a_cred;
1663 IN struct thread *a_td;
1668 struct extattr *eap, *eaend;
1671 ip = VTOI(ap->a_vp);
1673 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1674 return (EOPNOTSUPP);
1676 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1677 ap->a_cred, ap->a_td, VREAD);
1681 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1686 if (ap->a_size != NULL)
1689 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1690 eap = (struct extattr *)ip->i_ea_area;
1691 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1692 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1693 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1694 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1695 if (eap->ea_namespace != ap->a_attrnamespace)
1698 ealen = eap->ea_namelength;
1699 if (ap->a_size != NULL)
1700 *ap->a_size += ealen + 1;
1701 else if (ap->a_uio != NULL)
1702 error = uiomove(&eap->ea_namelength, ealen + 1,
1706 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1711 * Vnode operation to set a named attribute.
1714 ffs_setextattr(struct vop_setextattr_args *ap)
1717 IN struct vnode *a_vp;
1718 IN int a_attrnamespace;
1719 IN const char *a_name;
1720 INOUT struct uio *a_uio;
1721 IN struct ucred *a_cred;
1722 IN struct thread *a_td;
1728 struct extattr *eap;
1729 uint32_t ealength, ul;
1731 int olen, eapad1, eapad2, error, i, easize;
1735 ip = VTOI(ap->a_vp);
1738 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1739 return (EOPNOTSUPP);
1741 if (strlen(ap->a_name) == 0)
1744 /* XXX Now unsupported API to delete EAs using NULL uio. */
1745 if (ap->a_uio == NULL)
1746 return (EOPNOTSUPP);
1748 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1751 ealen = ap->a_uio->uio_resid;
1752 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1755 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1756 ap->a_cred, ap->a_td, VWRITE);
1759 * ffs_lock_ea is not needed there, because the vnode
1760 * must be exclusively locked.
1762 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1763 ip->i_ea_error = error;
1767 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1771 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1772 eapad1 = roundup2(ealength, 8) - ealength;
1773 eapad2 = roundup2(ealen, 8) - ealen;
1774 ealength += eapad1 + ealen + eapad2;
1777 * CEM: rewrites of the same size or smaller could be done in-place
1778 * instead. (We don't acquire any fine-grained locks in here either,
1779 * so we could also do bigger writes in-place.)
1781 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1782 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1783 easize = ip->i_ea_len;
1785 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1788 /* new, append at end */
1789 KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1791 eap = (struct extattr *)(eae + easize);
1794 ul = eap->ea_length;
1795 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1796 if (ul != ealength) {
1797 bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
1799 easize += (ealength - ul);
1802 if (easize > lblktosize(fs, UFS_NXADDR)) {
1804 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1805 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1806 ip->i_ea_error = ENOSPC;
1809 eap->ea_length = ealength;
1810 eap->ea_namespace = ap->a_attrnamespace;
1811 eap->ea_contentpadlen = eapad2;
1812 eap->ea_namelength = strlen(ap->a_name);
1813 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1814 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1815 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1818 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1819 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1820 ip->i_ea_error = error;
1823 bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1825 tmp = ip->i_ea_area;
1826 ip->i_ea_area = eae;
1827 ip->i_ea_len = easize;
1829 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1834 * Vnode pointer to File handle
1837 ffs_vptofh(struct vop_vptofh_args *ap)
1840 IN struct vnode *a_vp;
1841 IN struct fid *a_fhp;
1848 ip = VTOI(ap->a_vp);
1849 ufhp = (struct ufid *)ap->a_fhp;
1850 ufhp->ufid_len = sizeof(struct ufid);
1851 ufhp->ufid_ino = ip->i_number;
1852 ufhp->ufid_gen = ip->i_gen;
1856 SYSCTL_DECL(_vfs_ffs);
1857 static int use_buf_pager = 1;
1858 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1859 "Always use buffer pager instead of bmap");
1862 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1865 return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1869 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1872 return (blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn));
1876 ffs_getpages(struct vop_getpages_args *ap)
1879 struct ufsmount *um;
1882 um = VFSTOUFS(vp->v_mount);
1884 if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1885 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1886 ap->a_rbehind, ap->a_rahead, NULL, NULL));
1887 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1888 ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1892 ffs_getpages_async(struct vop_getpages_async_args *ap)
1895 struct ufsmount *um;
1900 um = VFSTOUFS(vp->v_mount);
1903 if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1904 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1905 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1909 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1910 ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1913 if (do_iodone && ap->a_iodone != NULL)
1914 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);