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"
73 #include <sys/param.h>
75 #include <sys/systm.h>
78 #include <sys/extattr.h>
79 #include <sys/kernel.h>
80 #include <sys/limits.h>
81 #include <sys/malloc.h>
82 #include <sys/mount.h>
84 #include <sys/rwlock.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
88 #include <sys/vnode.h>
91 #include <vm/vm_param.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vnode_pager.h>
98 #include <ufs/ufs/extattr.h>
99 #include <ufs/ufs/quota.h>
100 #include <ufs/ufs/inode.h>
101 #include <ufs/ufs/ufs_extern.h>
102 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ufs/dir.h>
105 #include <ufs/ufs/dirhash.h>
108 #include <ufs/ffs/fs.h>
109 #include <ufs/ffs/ffs_extern.h>
111 #define ALIGNED_TO(ptr, s) \
112 (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
115 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
117 static vop_fdatasync_t ffs_fdatasync;
118 static vop_fsync_t ffs_fsync;
119 static vop_getpages_t ffs_getpages;
120 static vop_getpages_async_t ffs_getpages_async;
121 static vop_lock1_t ffs_lock;
123 static vop_unlock_t ffs_unlock_debug;
125 static vop_read_t ffs_read;
126 static vop_write_t ffs_write;
127 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
128 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
130 static vop_strategy_t ffsext_strategy;
131 static vop_closeextattr_t ffs_closeextattr;
132 static vop_deleteextattr_t ffs_deleteextattr;
133 static vop_getextattr_t ffs_getextattr;
134 static vop_listextattr_t ffs_listextattr;
135 static vop_openextattr_t ffs_openextattr;
136 static vop_setextattr_t ffs_setextattr;
137 static vop_vptofh_t ffs_vptofh;
138 static vop_vput_pair_t ffs_vput_pair;
140 /* Global vfs data structures for ufs. */
141 struct vop_vector ffs_vnodeops1 = {
142 .vop_default = &ufs_vnodeops,
143 .vop_fsync = ffs_fsync,
144 .vop_fdatasync = ffs_fdatasync,
145 .vop_getpages = ffs_getpages,
146 .vop_getpages_async = ffs_getpages_async,
147 .vop_lock1 = ffs_lock,
149 .vop_unlock = ffs_unlock_debug,
151 .vop_read = ffs_read,
152 .vop_reallocblks = ffs_reallocblks,
153 .vop_write = ffs_write,
154 .vop_vptofh = ffs_vptofh,
155 .vop_vput_pair = ffs_vput_pair,
157 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
159 struct vop_vector ffs_fifoops1 = {
160 .vop_default = &ufs_fifoops,
161 .vop_fsync = ffs_fsync,
162 .vop_fdatasync = ffs_fdatasync,
163 .vop_lock1 = ffs_lock,
165 .vop_unlock = ffs_unlock_debug,
167 .vop_vptofh = ffs_vptofh,
169 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
171 /* Global vfs data structures for ufs. */
172 struct vop_vector ffs_vnodeops2 = {
173 .vop_default = &ufs_vnodeops,
174 .vop_fsync = ffs_fsync,
175 .vop_fdatasync = ffs_fdatasync,
176 .vop_getpages = ffs_getpages,
177 .vop_getpages_async = ffs_getpages_async,
178 .vop_lock1 = ffs_lock,
180 .vop_unlock = ffs_unlock_debug,
182 .vop_read = ffs_read,
183 .vop_reallocblks = ffs_reallocblks,
184 .vop_write = ffs_write,
185 .vop_closeextattr = ffs_closeextattr,
186 .vop_deleteextattr = ffs_deleteextattr,
187 .vop_getextattr = ffs_getextattr,
188 .vop_listextattr = ffs_listextattr,
189 .vop_openextattr = ffs_openextattr,
190 .vop_setextattr = ffs_setextattr,
191 .vop_vptofh = ffs_vptofh,
192 .vop_vput_pair = ffs_vput_pair,
194 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
196 struct vop_vector ffs_fifoops2 = {
197 .vop_default = &ufs_fifoops,
198 .vop_fsync = ffs_fsync,
199 .vop_fdatasync = ffs_fdatasync,
200 .vop_lock1 = ffs_lock,
202 .vop_unlock = ffs_unlock_debug,
204 .vop_reallocblks = ffs_reallocblks,
205 .vop_strategy = ffsext_strategy,
206 .vop_closeextattr = ffs_closeextattr,
207 .vop_deleteextattr = ffs_deleteextattr,
208 .vop_getextattr = ffs_getextattr,
209 .vop_listextattr = ffs_listextattr,
210 .vop_openextattr = ffs_openextattr,
211 .vop_setextattr = ffs_setextattr,
212 .vop_vptofh = ffs_vptofh,
214 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
217 * Synch an open file.
221 ffs_fsync(struct vop_fsync_args *ap)
230 error = ffs_syncvnode(vp, ap->a_waitfor, 0);
233 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
234 error = softdep_fsync(vp);
239 * The softdep_fsync() function may drop vp lock,
240 * allowing for dirty buffers to reappear on the
241 * bo_dirty list. Recheck and resync as needed.
244 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
245 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
251 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
257 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
261 struct ufsmount *ump;
262 struct buf *bp, *nbp;
265 bool still_dirty, unlocked, wait;
269 ump = VFSTOUFS(vp->v_mount);
272 * When doing MNT_WAIT we must first flush all dependencies
275 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
276 (error = softdep_sync_metadata(vp)) != 0) {
277 if (ffs_fsfail_cleanup(ump, error))
283 * Flush all dirty buffers associated with a vnode.
287 wait = false; /* Always do an async pass first. */
289 lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
292 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
293 bp->b_vflags &= ~BV_SCANNED;
294 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
296 * Reasons to skip this buffer: it has already been considered
297 * on this pass, the buffer has dependencies that will cause
298 * it to be redirtied and it has not already been deferred,
299 * or it is already being written.
301 if ((bp->b_vflags & BV_SCANNED) != 0)
303 bp->b_vflags |= BV_SCANNED;
305 * Flush indirects in order, if requested.
307 * Note that if only datasync is requested, we can
308 * skip indirect blocks when softupdates are not
309 * active. Otherwise we must flush them with data,
310 * since dependencies prevent data block writes.
312 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
313 (lbn_level(bp->b_lblkno) >= passes ||
314 ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
316 if (bp->b_lblkno > lbn)
317 panic("ffs_syncvnode: syncing truncated data.");
318 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
322 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
323 BO_LOCKPTR(bo)) != 0) {
325 bp->b_vflags &= ~BV_SCANNED;
330 if ((bp->b_flags & B_DELWRI) == 0)
331 panic("ffs_fsync: not dirty");
333 * Check for dependencies and potentially complete them.
335 if (!LIST_EMPTY(&bp->b_dep) &&
336 (error = softdep_sync_buf(vp, bp,
337 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
339 * Lock order conflict, buffer was already unlocked,
340 * and vnode possibly unlocked.
342 if (error == ERELOOKUP) {
343 if (vp->v_data == NULL)
346 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
347 (error = softdep_sync_metadata(vp)) != 0) {
348 if (ffs_fsfail_cleanup(ump, error))
350 return (unlocked && error == 0 ?
353 /* Re-evaluate inode size */
354 lbn = lblkno(ITOFS(ip), (ip->i_size +
355 ITOFS(ip)->fs_bsize - 1));
359 if (error != EBUSY) {
363 /* If we deferred once, don't defer again. */
364 if ((bp->b_flags & B_DEFERRED) == 0) {
365 bp->b_flags |= B_DEFERRED;
373 if (ffs_fsfail_cleanup(ump, error))
377 } else if ((bp->b_flags & B_CLUSTEROK)) {
378 (void) vfs_bio_awrite(bp);
385 * Since we may have slept during the I/O, we need
386 * to start from a known point.
390 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
392 if (waitfor != MNT_WAIT) {
394 if ((flags & NO_INO_UPDT) != 0)
395 return (unlocked ? ERELOOKUP : 0);
396 error = ffs_update(vp, 0);
397 if (error == 0 && unlocked)
401 /* Drain IO to see if we're done. */
402 bufobj_wwait(bo, 0, 0);
404 * Block devices associated with filesystems may have new I/O
405 * requests posted for them even if the vnode is locked, so no
406 * amount of trying will get them clean. We make several passes
409 * Regular files may need multiple passes to flush all dependency
410 * work as it is possible that we must write once per indirect
411 * level, once for the leaf, and once for the inode and each of
412 * these will be done with one sync and one async pass.
414 if (bo->bo_dirty.bv_cnt > 0) {
415 if ((flags & DATA_ONLY) == 0) {
419 * For data-only sync, dirty indirect buffers
423 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
424 if (bp->b_lblkno > -UFS_NDADDR) {
432 /* Write the inode after sync passes to flush deps. */
433 if (wait && DOINGSOFTDEP(vp) &&
434 (flags & NO_INO_UPDT) == 0) {
439 /* switch between sync/async. */
441 if (wait || ++passes < UFS_NIADDR + 2)
447 if ((flags & DATA_ONLY) == 0) {
448 if ((flags & NO_INO_UPDT) == 0)
449 error = ffs_update(vp, 1);
451 softdep_journal_fsync(VTOI(vp));
452 } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
453 error = ffs_update(vp, 1);
455 if (error == 0 && unlocked)
458 ip->i_flag &= ~IN_NEEDSYNC;
463 ffs_fdatasync(struct vop_fdatasync_args *ap)
466 return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
471 struct vop_lock1_args /* {
478 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
479 struct vnode *vp = ap->a_vp;
480 #endif /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
483 #endif /* DIAGNOSTIC */
485 #ifndef NO_FFS_SNAPSHOT
490 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
491 * and only use it when LK_NODDLKTREAT is set. Currently this means it
492 * is only used during path lookup.
494 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
495 ap->a_flags |= LK_ADAPTIVE;
496 switch (ap->a_flags & LK_TYPE_MASK) {
502 #ifdef DEBUG_VFS_LOCKS
503 VNPASS(vp->v_holdcnt != 0, vp);
504 #endif /* DEBUG_VFS_LOCKS */
506 result = lockmgr_lock_flags(lkp, flags,
507 &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
508 if (lkp == vp->v_vnlock || result != 0)
511 * Apparent success, except that the vnode
512 * mutated between snapshot file vnode and
513 * regular file vnode while this process
514 * slept. The lock currently held is not the
515 * right lock. Release it, and try to get the
519 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
520 (LK_INTERLOCK | LK_NOWAIT))
522 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
523 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
524 flags &= ~LK_INTERLOCK;
527 switch (ap->a_flags & LK_TYPE_MASK) {
530 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
536 #endif /* DIAGNOSTIC */
540 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
543 ufs_unlock_tracker(ip);
545 #endif /* DIAGNOSTIC */
546 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
549 #else /* NO_FFS_SNAPSHOT */
551 * See above for an explanation.
553 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
554 ap->a_flags |= LK_ADAPTIVE;
556 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
559 ufs_unlock_tracker(ip);
561 #endif /* DIAGNOSTIC */
562 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
563 #endif /* NO_FFS_SNAPSHOT */
565 switch (ap->a_flags & LK_TYPE_MASK) {
568 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
574 #endif /* DIAGNOSTIC */
580 ffs_unlock_debug(struct vop_unlock_args *ap)
587 if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
588 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
590 VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
591 ("%s: modified vnode (%x) not on lazy list",
592 __func__, ip->i_flag));
596 KASSERT(vp->v_type != VDIR || vp->v_vnlock->lk_recurse != 0 ||
597 (ip->i_flag & IN_ENDOFF) == 0,
598 ("ufs dir vp %p ip %p flags %#x", vp, ip, ip->i_flag));
600 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
601 vp->v_vnlock->lk_recurse == 0)
602 ufs_unlock_tracker(ip);
604 return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
609 ffs_read_hole(struct uio *uio, long xfersize, long *size)
611 ssize_t saved_resid, tlen;
614 while (xfersize > 0) {
615 tlen = min(xfersize, ZERO_REGION_SIZE);
616 saved_resid = uio->uio_resid;
617 error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
621 tlen = saved_resid - uio->uio_resid;
629 * Vnode op for reading.
633 struct vop_read_args /* {
637 struct ucred *a_cred;
645 ufs_lbn_t lbn, nextlbn;
647 long size, xfersize, blkoffset;
649 int bflag, error, ioflag, seqcount;
653 ioflag = ap->a_ioflag;
654 if (ap->a_ioflag & IO_EXT)
656 return (ffs_extread(vp, uio, ioflag));
658 panic("ffs_read+IO_EXT");
661 if ((ioflag & IO_DIRECT) != 0) {
664 error = ffs_rawread(vp, uio, &workdone);
665 if (error != 0 || workdone != 0)
670 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
674 if (uio->uio_rw != UIO_READ)
675 panic("ffs_read: mode");
677 if (vp->v_type == VLNK) {
678 if ((int)ip->i_size < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
679 panic("ffs_read: short symlink");
680 } else if (vp->v_type != VREG && vp->v_type != VDIR)
681 panic("ffs_read: type %d", vp->v_type);
683 orig_resid = uio->uio_resid;
684 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
687 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
689 if (uio->uio_offset < ip->i_size &&
690 uio->uio_offset >= fs->fs_maxfilesize)
693 bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
694 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
695 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
697 lbn = lblkno(fs, uio->uio_offset);
701 * size of buffer. The buffer representing the
702 * end of the file is rounded up to the size of
703 * the block type ( fragment or full block,
706 size = blksize(fs, ip, lbn);
707 blkoffset = blkoff(fs, uio->uio_offset);
710 * The amount we want to transfer in this iteration is
711 * one FS block less the amount of the data before
712 * our startpoint (duh!)
714 xfersize = fs->fs_bsize - blkoffset;
717 * But if we actually want less than the block,
718 * or the file doesn't have a whole block more of data,
719 * then use the lesser number.
721 if (uio->uio_resid < xfersize)
722 xfersize = uio->uio_resid;
723 if (bytesinfile < xfersize)
724 xfersize = bytesinfile;
726 if (lblktosize(fs, nextlbn) >= ip->i_size) {
728 * Don't do readahead if this is the end of the file.
730 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
731 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
733 * Otherwise if we are allowed to cluster,
734 * grab as much as we can.
736 * XXX This may not be a win if we are not
737 * doing sequential access.
739 error = cluster_read(vp, ip->i_size, lbn,
740 size, NOCRED, blkoffset + uio->uio_resid,
741 seqcount, bflag, &bp);
742 } else if (seqcount > 1) {
744 * If we are NOT allowed to cluster, then
745 * if we appear to be acting sequentially,
746 * fire off a request for a readahead
747 * as well as a read. Note that the 4th and 5th
748 * arguments point to arrays of the size specified in
751 u_int nextsize = blksize(fs, ip, nextlbn);
752 error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
753 &nextsize, 1, NOCRED, bflag, NULL, &bp);
756 * Failing all of the above, just read what the
757 * user asked for. Interestingly, the same as
758 * the first option above.
760 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
762 if (error == EJUSTRETURN) {
763 error = ffs_read_hole(uio, xfersize, &size);
774 * We should only get non-zero b_resid when an I/O error
775 * has occurred, which should cause us to break above.
776 * However, if the short read did not cause an error,
777 * then we want to ensure that we do not uiomove bad
778 * or uninitialized data.
781 if (size < xfersize) {
787 if (buf_mapped(bp)) {
788 error = vn_io_fault_uiomove((char *)bp->b_data +
789 blkoffset, (int)xfersize, uio);
791 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
797 vfs_bio_brelse(bp, ioflag);
801 * This can only happen in the case of an error
802 * because the loop above resets bp to NULL on each iteration
803 * and on normal completion has not set a new value into it.
804 * so it must have come from a 'break' statement
807 vfs_bio_brelse(bp, ioflag);
809 if ((error == 0 || uio->uio_resid != orig_resid) &&
810 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
811 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
816 * Vnode op for writing.
820 struct vop_write_args /* {
824 struct ucred *a_cred;
836 int blkoffset, error, flags, ioflag, size, xfersize;
840 softdep_prealloc(vp, MNT_WAIT);
841 if (vp->v_data == NULL)
845 ioflag = ap->a_ioflag;
846 if (ap->a_ioflag & IO_EXT)
848 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
850 panic("ffs_write+IO_EXT");
853 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
857 if (uio->uio_rw != UIO_WRITE)
858 panic("ffs_write: mode");
861 switch (vp->v_type) {
863 if (ioflag & IO_APPEND)
864 uio->uio_offset = ip->i_size;
865 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
871 panic("ffs_write: dir write");
874 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
875 (int)uio->uio_offset,
880 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
881 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
883 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
886 * Maybe this should be above the vnode op call, but so long as
887 * file servers have no limits, I don't think it matters.
889 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
892 resid = uio->uio_resid;
894 if (seqcount > BA_SEQMAX)
895 flags = BA_SEQMAX << BA_SEQSHIFT;
897 flags = seqcount << BA_SEQSHIFT;
898 if (ioflag & IO_SYNC)
900 flags |= BA_UNMAPPED;
902 for (error = 0; uio->uio_resid > 0;) {
903 lbn = lblkno(fs, uio->uio_offset);
904 blkoffset = blkoff(fs, uio->uio_offset);
905 xfersize = fs->fs_bsize - blkoffset;
906 if (uio->uio_resid < xfersize)
907 xfersize = uio->uio_resid;
908 if (uio->uio_offset + xfersize > ip->i_size)
909 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
912 * We must perform a read-before-write if the transfer size
913 * does not cover the entire buffer.
915 if (fs->fs_bsize > xfersize)
919 /* XXX is uio->uio_offset the right thing here? */
920 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
921 ap->a_cred, flags, &bp);
923 vnode_pager_setsize(vp, ip->i_size);
926 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
927 bp->b_flags |= B_NOCACHE;
929 if (uio->uio_offset + xfersize > ip->i_size) {
930 ip->i_size = uio->uio_offset + xfersize;
931 DIP_SET(ip, i_size, ip->i_size);
932 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
935 size = blksize(fs, ip, lbn) - bp->b_resid;
939 if (buf_mapped(bp)) {
940 error = vn_io_fault_uiomove((char *)bp->b_data +
941 blkoffset, (int)xfersize, uio);
943 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
947 * If the buffer is not already filled and we encounter an
948 * error while trying to fill it, we have to clear out any
949 * garbage data from the pages instantiated for the buffer.
950 * If we do not, a failed uiomove() during a write can leave
951 * the prior contents of the pages exposed to a userland mmap.
953 * Note that we need only clear buffers with a transfer size
954 * equal to the block size because buffers with a shorter
955 * transfer size were cleared above by the call to UFS_BALLOC()
956 * with the BA_CLRBUF flag set.
958 * If the source region for uiomove identically mmaps the
959 * buffer, uiomove() performed the NOP copy, and the buffer
960 * content remains valid because the page fault handler
961 * validated the pages.
963 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
964 fs->fs_bsize == xfersize)
967 vfs_bio_set_flags(bp, ioflag);
970 * If IO_SYNC each buffer is written synchronously. Otherwise
971 * if we have a severe page deficiency write the buffer
972 * asynchronously. Otherwise try to cluster, and if that
973 * doesn't do it then either do an async write (if O_DIRECT),
974 * or a delayed write (if not).
976 if (ioflag & IO_SYNC) {
978 } else if (vm_page_count_severe() ||
979 buf_dirty_count_severe() ||
980 (ioflag & IO_ASYNC)) {
981 bp->b_flags |= B_CLUSTEROK;
983 } else if (xfersize + blkoffset == fs->fs_bsize) {
984 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
985 bp->b_flags |= B_CLUSTEROK;
986 cluster_write(vp, &ip->i_clusterw, bp,
987 ip->i_size, seqcount, GB_UNMAPPED);
991 } else if (ioflag & IO_DIRECT) {
992 bp->b_flags |= B_CLUSTEROK;
995 bp->b_flags |= B_CLUSTEROK;
998 if (error || xfersize == 0)
1000 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1003 * If we successfully wrote any data, and we are not the superuser
1004 * we clear the setuid and setgid bits as a precaution against
1007 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
1009 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
1010 vn_seqc_write_begin(vp);
1011 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1012 DIP_SET(ip, i_mode, ip->i_mode);
1013 vn_seqc_write_end(vp);
1017 if (ioflag & IO_UNIT) {
1018 (void)ffs_truncate(vp, osize,
1019 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1020 uio->uio_offset -= resid - uio->uio_resid;
1021 uio->uio_resid = resid;
1023 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1024 if (!(ioflag & IO_DATASYNC) ||
1025 (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1026 error = ffs_update(vp, 1);
1027 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1034 * Extended attribute area reading.
1037 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1040 struct ufs2_dinode *dp;
1043 ufs_lbn_t lbn, nextlbn;
1045 long size, xfersize, blkoffset;
1054 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1055 panic("ffs_extread: mode");
1058 orig_resid = uio->uio_resid;
1059 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1060 if (orig_resid == 0)
1062 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1064 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1065 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1067 lbn = lblkno(fs, uio->uio_offset);
1071 * size of buffer. The buffer representing the
1072 * end of the file is rounded up to the size of
1073 * the block type ( fragment or full block,
1076 size = sblksize(fs, dp->di_extsize, lbn);
1077 blkoffset = blkoff(fs, uio->uio_offset);
1080 * The amount we want to transfer in this iteration is
1081 * one FS block less the amount of the data before
1082 * our startpoint (duh!)
1084 xfersize = fs->fs_bsize - blkoffset;
1087 * But if we actually want less than the block,
1088 * or the file doesn't have a whole block more of data,
1089 * then use the lesser number.
1091 if (uio->uio_resid < xfersize)
1092 xfersize = uio->uio_resid;
1093 if (bytesinfile < xfersize)
1094 xfersize = bytesinfile;
1096 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1098 * Don't do readahead if this is the end of the info.
1100 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1103 * If we have a second block, then
1104 * fire off a request for a readahead
1105 * as well as a read. Note that the 4th and 5th
1106 * arguments point to arrays of the size specified in
1109 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
1111 nextlbn = -1 - nextlbn;
1112 error = breadn(vp, -1 - lbn,
1113 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1122 * We should only get non-zero b_resid when an I/O error
1123 * has occurred, which should cause us to break above.
1124 * However, if the short read did not cause an error,
1125 * then we want to ensure that we do not uiomove bad
1126 * or uninitialized data.
1128 size -= bp->b_resid;
1129 if (size < xfersize) {
1135 error = uiomove((char *)bp->b_data + blkoffset,
1136 (int)xfersize, uio);
1139 vfs_bio_brelse(bp, ioflag);
1143 * This can only happen in the case of an error
1144 * because the loop above resets bp to NULL on each iteration
1145 * and on normal completion has not set a new value into it.
1146 * so it must have come from a 'break' statement
1149 vfs_bio_brelse(bp, ioflag);
1154 * Extended attribute area writing.
1157 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1160 struct ufs2_dinode *dp;
1166 int blkoffset, error, flags, size, xfersize;
1173 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1174 panic("ffs_extwrite: mode");
1177 if (ioflag & IO_APPEND)
1178 uio->uio_offset = dp->di_extsize;
1179 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1180 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1181 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1182 UFS_NXADDR * fs->fs_bsize)
1185 resid = uio->uio_resid;
1186 osize = dp->di_extsize;
1188 if (ioflag & IO_SYNC)
1191 for (error = 0; uio->uio_resid > 0;) {
1192 lbn = lblkno(fs, uio->uio_offset);
1193 blkoffset = blkoff(fs, uio->uio_offset);
1194 xfersize = fs->fs_bsize - blkoffset;
1195 if (uio->uio_resid < xfersize)
1196 xfersize = uio->uio_resid;
1199 * We must perform a read-before-write if the transfer size
1200 * does not cover the entire buffer.
1202 if (fs->fs_bsize > xfersize)
1205 flags &= ~BA_CLRBUF;
1206 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1211 * If the buffer is not valid we have to clear out any
1212 * garbage data from the pages instantiated for the buffer.
1213 * If we do not, a failed uiomove() during a write can leave
1214 * the prior contents of the pages exposed to a userland
1215 * mmap(). XXX deal with uiomove() errors a better way.
1217 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1220 if (uio->uio_offset + xfersize > dp->di_extsize) {
1221 dp->di_extsize = uio->uio_offset + xfersize;
1222 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
1225 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1226 if (size < xfersize)
1230 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1232 vfs_bio_set_flags(bp, ioflag);
1235 * If IO_SYNC each buffer is written synchronously. Otherwise
1236 * if we have a severe page deficiency write the buffer
1237 * asynchronously. Otherwise try to cluster, and if that
1238 * doesn't do it then either do an async write (if O_DIRECT),
1239 * or a delayed write (if not).
1241 if (ioflag & IO_SYNC) {
1243 } else if (vm_page_count_severe() ||
1244 buf_dirty_count_severe() ||
1245 xfersize + blkoffset == fs->fs_bsize ||
1246 (ioflag & (IO_ASYNC | IO_DIRECT)))
1250 if (error || xfersize == 0)
1252 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1255 * If we successfully wrote any data, and we are not the superuser
1256 * we clear the setuid and setgid bits as a precaution against
1259 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1260 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) {
1261 vn_seqc_write_begin(vp);
1262 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1263 dp->di_mode = ip->i_mode;
1264 vn_seqc_write_end(vp);
1268 if (ioflag & IO_UNIT) {
1269 (void)ffs_truncate(vp, osize,
1270 IO_EXT | (ioflag&IO_SYNC), ucred);
1271 uio->uio_offset -= resid - uio->uio_resid;
1272 uio->uio_resid = resid;
1274 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1275 error = ffs_update(vp, 1);
1280 * Vnode operating to retrieve a named extended attribute.
1282 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1283 * the length of the EA, and possibly the pointer to the entry and to the data.
1286 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name,
1287 struct extattr **eapp, u_char **eac)
1289 struct extattr *eap, *eaend;
1292 nlen = strlen(name);
1293 KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned"));
1294 eap = (struct extattr *)ptr;
1295 eaend = (struct extattr *)(ptr + length);
1296 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
1297 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1298 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1299 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
1300 || memcmp(eap->ea_name, name, nlen) != 0)
1305 *eac = EXTATTR_CONTENT(eap);
1306 return (EXTATTR_CONTENT_SIZE(eap));
1312 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td)
1314 const struct extattr *eap, *eaend, *eapnext;
1316 struct ufs2_dinode *dp;
1319 struct iovec liovec;
1327 easize = dp->di_extsize;
1328 if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize)
1331 eae = malloc(easize, M_TEMP, M_WAITOK);
1333 liovec.iov_base = eae;
1334 liovec.iov_len = easize;
1335 luio.uio_iov = &liovec;
1336 luio.uio_iovcnt = 1;
1337 luio.uio_offset = 0;
1338 luio.uio_resid = easize;
1339 luio.uio_segflg = UIO_SYSSPACE;
1340 luio.uio_rw = UIO_READ;
1343 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1348 /* Validate disk xattrfile contents. */
1349 for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend;
1351 /* Detect zeroed out tail */
1352 if (eap->ea_length < sizeof(*eap) || eap->ea_length == 0) {
1353 easize = (const u_char *)eap - eae;
1357 eapnext = EXTATTR_NEXT(eap);
1358 /* Bogusly long entry. */
1359 if (eapnext > eaend) {
1361 return (EINTEGRITY);
1364 ip->i_ea_len = easize;
1370 ffs_lock_ea(struct vnode *vp)
1376 while (ip->i_flag & IN_EA_LOCKED) {
1377 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT);
1378 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1381 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED);
1386 ffs_unlock_ea(struct vnode *vp)
1392 if (ip->i_flag & IN_EA_LOCKWAIT)
1393 wakeup(&ip->i_ea_refs);
1394 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1399 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1402 struct ufs2_dinode *dp;
1408 if (ip->i_ea_area != NULL) {
1414 error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1426 * Vnode extattr transaction commit/abort
1429 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1433 struct iovec *liovec;
1434 struct ufs2_dinode *dp;
1435 size_t ea_len, tlen;
1442 if (ip->i_ea_area == NULL) {
1447 error = ip->i_ea_error;
1449 if (commit && error == 0) {
1450 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1452 cred = vp->v_mount->mnt_cred;
1454 ea_len = MAX(ip->i_ea_len, dp->di_extsize);
1455 for (lcnt = 1, tlen = ea_len - ip->i_ea_len; tlen > 0;) {
1456 tlen -= MIN(ZERO_REGION_SIZE, tlen);
1460 liovec = __builtin_alloca(lcnt * sizeof(struct iovec));
1461 luio.uio_iovcnt = lcnt;
1463 liovec[0].iov_base = ip->i_ea_area;
1464 liovec[0].iov_len = ip->i_ea_len;
1465 for (i = 1, tlen = ea_len - ip->i_ea_len; i < lcnt; i++) {
1466 liovec[i].iov_base = __DECONST(void *, zero_region);
1467 liovec[i].iov_len = MIN(ZERO_REGION_SIZE, tlen);
1468 tlen -= liovec[i].iov_len;
1472 luio.uio_iov = liovec;
1473 luio.uio_offset = 0;
1474 luio.uio_resid = ea_len;
1475 luio.uio_segflg = UIO_SYSSPACE;
1476 luio.uio_rw = UIO_WRITE;
1478 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1479 if (error == 0 && ip->i_ea_len == 0)
1482 if (--ip->i_ea_refs == 0) {
1483 free(ip->i_ea_area, M_TEMP);
1484 ip->i_ea_area = NULL;
1491 ffs_truncate(vp, 0, IO_EXT, cred);
1496 * Vnode extattr strategy routine for fifos.
1498 * We need to check for a read or write of the external attributes.
1499 * Otherwise we just fall through and do the usual thing.
1502 ffsext_strategy(struct vop_strategy_args *ap)
1504 struct vop_strategy_args {
1505 struct vnodeop_desc *a_desc;
1515 lbn = ap->a_bp->b_lblkno;
1516 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1517 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1518 if (vp->v_type == VFIFO)
1519 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1520 panic("spec nodes went here");
1524 * Vnode extattr transaction commit/abort
1527 ffs_openextattr(struct vop_openextattr_args *ap)
1529 struct vop_openextattr_args {
1530 struct vnodeop_desc *a_desc;
1532 IN struct ucred *a_cred;
1533 IN struct thread *a_td;
1538 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1539 return (EOPNOTSUPP);
1541 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1545 * Vnode extattr transaction commit/abort
1548 ffs_closeextattr(struct vop_closeextattr_args *ap)
1550 struct vop_closeextattr_args {
1551 struct vnodeop_desc *a_desc;
1554 IN struct ucred *a_cred;
1555 IN struct thread *a_td;
1562 if (vp->v_type == VCHR || vp->v_type == VBLK)
1563 return (EOPNOTSUPP);
1564 if (ap->a_commit && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
1567 if (ap->a_commit && DOINGSUJ(vp)) {
1568 ASSERT_VOP_ELOCKED(vp, "ffs_closeextattr commit");
1569 softdep_prealloc(vp, MNT_WAIT);
1570 if (vp->v_data == NULL)
1573 return (ffs_close_ea(vp, ap->a_commit, ap->a_cred, ap->a_td));
1577 * Vnode operation to remove a named attribute.
1580 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1583 IN struct vnode *a_vp;
1584 IN int a_attrnamespace;
1585 IN const char *a_name;
1586 IN struct ucred *a_cred;
1587 IN struct thread *a_td;
1593 struct extattr *eap;
1595 int olen, error, i, easize;
1602 if (vp->v_type == VCHR || vp->v_type == VBLK)
1603 return (EOPNOTSUPP);
1604 if (strlen(ap->a_name) == 0)
1606 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1609 error = extattr_check_cred(vp, ap->a_attrnamespace,
1610 ap->a_cred, ap->a_td, VWRITE);
1613 * ffs_lock_ea is not needed there, because the vnode
1614 * must be exclusively locked.
1616 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1617 ip->i_ea_error = error;
1622 ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1623 softdep_prealloc(vp, MNT_WAIT);
1624 if (vp->v_data == NULL)
1628 error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1632 /* CEM: delete could be done in-place instead */
1633 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1634 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1635 easize = ip->i_ea_len;
1637 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1640 /* delete but nonexistent */
1642 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1645 ul = eap->ea_length;
1646 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1647 bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1650 tmp = ip->i_ea_area;
1651 ip->i_ea_area = eae;
1652 ip->i_ea_len = easize;
1654 error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1659 * Vnode operation to retrieve a named extended attribute.
1662 ffs_getextattr(struct vop_getextattr_args *ap)
1665 IN struct vnode *a_vp;
1666 IN int a_attrnamespace;
1667 IN const char *a_name;
1668 INOUT struct uio *a_uio;
1670 IN struct ucred *a_cred;
1671 IN struct thread *a_td;
1680 ip = VTOI(ap->a_vp);
1682 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1683 return (EOPNOTSUPP);
1685 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1686 ap->a_cred, ap->a_td, VREAD);
1690 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1694 eae = ip->i_ea_area;
1695 easize = ip->i_ea_len;
1697 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1701 if (ap->a_size != NULL)
1702 *ap->a_size = ealen;
1703 else if (ap->a_uio != NULL)
1704 error = uiomove(p, ealen, ap->a_uio);
1708 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1713 * Vnode operation to retrieve extended attributes on a vnode.
1716 ffs_listextattr(struct vop_listextattr_args *ap)
1719 IN struct vnode *a_vp;
1720 IN int a_attrnamespace;
1721 INOUT struct uio *a_uio;
1723 IN struct ucred *a_cred;
1724 IN struct thread *a_td;
1729 struct extattr *eap, *eaend;
1732 ip = VTOI(ap->a_vp);
1734 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1735 return (EOPNOTSUPP);
1737 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1738 ap->a_cred, ap->a_td, VREAD);
1742 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1747 if (ap->a_size != NULL)
1750 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1751 eap = (struct extattr *)ip->i_ea_area;
1752 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1753 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1754 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1755 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1756 if (eap->ea_namespace != ap->a_attrnamespace)
1759 ealen = eap->ea_namelength;
1760 if (ap->a_size != NULL)
1761 *ap->a_size += ealen + 1;
1762 else if (ap->a_uio != NULL)
1763 error = uiomove(&eap->ea_namelength, ealen + 1,
1767 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1772 * Vnode operation to set a named attribute.
1775 ffs_setextattr(struct vop_setextattr_args *ap)
1778 IN struct vnode *a_vp;
1779 IN int a_attrnamespace;
1780 IN const char *a_name;
1781 INOUT struct uio *a_uio;
1782 IN struct ucred *a_cred;
1783 IN struct thread *a_td;
1790 struct extattr *eap;
1791 uint32_t ealength, ul;
1793 int olen, eapad1, eapad2, error, i, easize;
1801 if (vp->v_type == VCHR || vp->v_type == VBLK)
1802 return (EOPNOTSUPP);
1803 if (strlen(ap->a_name) == 0)
1806 /* XXX Now unsupported API to delete EAs using NULL uio. */
1807 if (ap->a_uio == NULL)
1808 return (EOPNOTSUPP);
1810 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1813 ealen = ap->a_uio->uio_resid;
1814 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1817 error = extattr_check_cred(vp, ap->a_attrnamespace,
1818 ap->a_cred, ap->a_td, VWRITE);
1821 * ffs_lock_ea is not needed there, because the vnode
1822 * must be exclusively locked.
1824 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1825 ip->i_ea_error = error;
1830 ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1831 softdep_prealloc(vp, MNT_WAIT);
1832 if (vp->v_data == NULL)
1836 error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1840 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1841 eapad1 = roundup2(ealength, 8) - ealength;
1842 eapad2 = roundup2(ealen, 8) - ealen;
1843 ealength += eapad1 + ealen + eapad2;
1846 * CEM: rewrites of the same size or smaller could be done in-place
1847 * instead. (We don't acquire any fine-grained locks in here either,
1848 * so we could also do bigger writes in-place.)
1850 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1851 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1852 easize = ip->i_ea_len;
1854 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1857 /* new, append at end */
1858 KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1860 eap = (struct extattr *)(eae + easize);
1863 ul = eap->ea_length;
1864 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1865 if (ul != ealength) {
1866 bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
1868 easize += (ealength - ul);
1871 if (easize > lblktosize(fs, UFS_NXADDR)) {
1873 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1874 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1875 ip->i_ea_error = ENOSPC;
1878 eap->ea_length = ealength;
1879 eap->ea_namespace = ap->a_attrnamespace;
1880 eap->ea_contentpadlen = eapad2;
1881 eap->ea_namelength = strlen(ap->a_name);
1882 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1883 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1884 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1887 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1888 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1889 ip->i_ea_error = error;
1892 bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1894 tmp = ip->i_ea_area;
1895 ip->i_ea_area = eae;
1896 ip->i_ea_len = easize;
1898 error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1903 * Vnode pointer to File handle
1906 ffs_vptofh(struct vop_vptofh_args *ap)
1909 IN struct vnode *a_vp;
1910 IN struct fid *a_fhp;
1917 ip = VTOI(ap->a_vp);
1918 ufhp = (struct ufid *)ap->a_fhp;
1919 ufhp->ufid_len = sizeof(struct ufid);
1920 ufhp->ufid_ino = ip->i_number;
1921 ufhp->ufid_gen = ip->i_gen;
1925 SYSCTL_DECL(_vfs_ffs);
1926 static int use_buf_pager = 1;
1927 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1928 "Always use buffer pager instead of bmap");
1931 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1934 return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1938 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1941 return (blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn));
1945 ffs_getpages(struct vop_getpages_args *ap)
1948 struct ufsmount *um;
1951 um = VFSTOUFS(vp->v_mount);
1953 if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1954 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1955 ap->a_rbehind, ap->a_rahead, NULL, NULL));
1956 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1957 ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1961 ffs_getpages_async(struct vop_getpages_async_args *ap)
1964 struct ufsmount *um;
1969 um = VFSTOUFS(vp->v_mount);
1972 if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1973 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1974 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1978 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1979 ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1982 if (do_iodone && ap->a_iodone != NULL)
1983 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
1989 ffs_vput_pair(struct vop_vput_pair_args *ap)
1992 struct vnode *dvp, *vp, *vp1, **vpp;
1993 struct inode *dp, *ip;
1997 int error, vp_locked;
2002 vp = vpp != NULL ? *vpp : NULL;
2004 if ((dp->i_flag & (IN_NEEDSYNC | IN_ENDOFF)) == 0) {
2006 if (vp != NULL && ap->a_unlock_vp)
2013 if (ap->a_unlock_vp) {
2016 MPASS(vp->v_type != VNON);
2017 vp_locked = VOP_ISLOCKED(vp);
2019 ip_ino = ip->i_number;
2026 * If compaction or fsync was requested do it in ffs_vput_pair()
2027 * now that other locks are no longer held.
2029 if ((dp->i_flag & IN_ENDOFF) != 0) {
2030 VNASSERT(I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size, dvp,
2031 ("IN_ENDOFF set but I_ENDOFF() is not"));
2032 dp->i_flag &= ~IN_ENDOFF;
2033 old_size = dp->i_size;
2034 error = UFS_TRUNCATE(dvp, (off_t)I_ENDOFF(dp), IO_NORMAL |
2035 (DOINGASYNC(dvp) ? 0 : IO_SYNC), curthread->td_ucred);
2036 if (error != 0 && error != ERELOOKUP) {
2037 if (!ffs_fsfail_cleanup(VFSTOUFS(mp), error)) {
2039 "IN_ENDOFF: failed to truncate, "
2040 "error %d\n", error);
2043 ufsdirhash_free(dp);
2046 SET_I_ENDOFF(dp, 0);
2048 if ((dp->i_flag & IN_NEEDSYNC) != 0) {
2050 error = ffs_syncvnode(dvp, MNT_WAIT, 0);
2051 } while (error == ERELOOKUP);
2056 if (vp == NULL || ap->a_unlock_vp)
2061 * It is possible that vp is reclaimed at this point. Only
2062 * routines that call us with a_unlock_vp == false can find
2063 * that their vp has been reclaimed. There are three areas
2064 * that are affected:
2065 * 1) vn_open_cred() - later VOPs could fail, but
2066 * dead_open() returns 0 to simulate successful open.
2067 * 2) ffs_snapshot() - creation of snapshot fails with EBADF.
2068 * 3) NFS server (several places) - code is prepared to detect
2069 * and respond to dead vnodes by returning ESTALE.
2071 VOP_LOCK(vp, vp_locked | LK_RETRY);
2072 if (!VN_IS_DOOMED(vp))
2076 * Try harder to recover from reclaimed vp if reclaim was not
2077 * because underlying inode was cleared. We saved inode
2078 * number and inode generation, so we can try to reinstantiate
2079 * exactly same version of inode. If this fails, return
2080 * original doomed vnode and let caller to handle
2083 * Note that callers must keep write started around
2084 * VOP_VPUT_PAIR() calls, so it is safe to use mp without
2088 error = ffs_inotovp(mp, ip_ino, ip_gen, LK_EXCLUSIVE, &vp1,
2089 FFSV_REPLACE_DOOMED);
2091 VOP_LOCK(vp, vp_locked | LK_RETRY);