2 * SPDX-License-Identifier: (BSD-2-Clause 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
60 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
63 #include <sys/cdefs.h>
64 #include "opt_directio.h"
68 #include <sys/param.h>
70 #include <sys/systm.h>
73 #include <sys/extattr.h>
74 #include <sys/kernel.h>
75 #include <sys/limits.h>
76 #include <sys/malloc.h>
77 #include <sys/mount.h>
79 #include <sys/rwlock.h>
81 #include <sys/sysctl.h>
82 #include <sys/vmmeter.h>
83 #include <sys/vnode.h>
86 #include <vm/vm_param.h>
87 #include <vm/vm_extern.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_pager.h>
91 #include <vm/vnode_pager.h>
93 #include <ufs/ufs/extattr.h>
94 #include <ufs/ufs/quota.h>
95 #include <ufs/ufs/inode.h>
96 #include <ufs/ufs/ufs_extern.h>
97 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ufs/dir.h>
100 #include <ufs/ufs/dirhash.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 vop_fplookup_vexec_t ufs_fplookup_vexec;
137 /* Global vfs data structures for ufs. */
138 struct vop_vector ffs_vnodeops1 = {
139 .vop_default = &ufs_vnodeops,
140 .vop_fsync = ffs_fsync,
141 .vop_fdatasync = ffs_fdatasync,
142 .vop_getpages = ffs_getpages,
143 .vop_getpages_async = ffs_getpages_async,
144 .vop_lock1 = ffs_lock,
146 .vop_unlock = ffs_unlock_debug,
148 .vop_read = ffs_read,
149 .vop_reallocblks = ffs_reallocblks,
150 .vop_write = ffs_write,
151 .vop_vptofh = ffs_vptofh,
152 .vop_vput_pair = ffs_vput_pair,
153 .vop_fplookup_vexec = ufs_fplookup_vexec,
154 .vop_fplookup_symlink = VOP_EAGAIN,
156 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
158 struct vop_vector ffs_fifoops1 = {
159 .vop_default = &ufs_fifoops,
160 .vop_fsync = ffs_fsync,
161 .vop_fdatasync = ffs_fdatasync,
162 .vop_lock1 = ffs_lock,
164 .vop_unlock = ffs_unlock_debug,
166 .vop_vptofh = ffs_vptofh,
167 .vop_fplookup_vexec = VOP_EAGAIN,
168 .vop_fplookup_symlink = VOP_EAGAIN,
170 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
172 /* Global vfs data structures for ufs. */
173 struct vop_vector ffs_vnodeops2 = {
174 .vop_default = &ufs_vnodeops,
175 .vop_fsync = ffs_fsync,
176 .vop_fdatasync = ffs_fdatasync,
177 .vop_getpages = ffs_getpages,
178 .vop_getpages_async = ffs_getpages_async,
179 .vop_lock1 = ffs_lock,
181 .vop_unlock = ffs_unlock_debug,
183 .vop_read = ffs_read,
184 .vop_reallocblks = ffs_reallocblks,
185 .vop_write = ffs_write,
186 .vop_closeextattr = ffs_closeextattr,
187 .vop_deleteextattr = ffs_deleteextattr,
188 .vop_getextattr = ffs_getextattr,
189 .vop_listextattr = ffs_listextattr,
190 .vop_openextattr = ffs_openextattr,
191 .vop_setextattr = ffs_setextattr,
192 .vop_vptofh = ffs_vptofh,
193 .vop_vput_pair = ffs_vput_pair,
194 .vop_fplookup_vexec = ufs_fplookup_vexec,
195 .vop_fplookup_symlink = VOP_EAGAIN,
197 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
199 struct vop_vector ffs_fifoops2 = {
200 .vop_default = &ufs_fifoops,
201 .vop_fsync = ffs_fsync,
202 .vop_fdatasync = ffs_fdatasync,
203 .vop_lock1 = ffs_lock,
205 .vop_unlock = ffs_unlock_debug,
207 .vop_reallocblks = ffs_reallocblks,
208 .vop_strategy = ffsext_strategy,
209 .vop_closeextattr = ffs_closeextattr,
210 .vop_deleteextattr = ffs_deleteextattr,
211 .vop_getextattr = ffs_getextattr,
212 .vop_listextattr = ffs_listextattr,
213 .vop_openextattr = ffs_openextattr,
214 .vop_setextattr = ffs_setextattr,
215 .vop_vptofh = ffs_vptofh,
216 .vop_fplookup_vexec = VOP_EAGAIN,
217 .vop_fplookup_symlink = VOP_EAGAIN,
219 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
222 * Synch an open file.
226 ffs_fsync(struct vop_fsync_args *ap)
235 error = ffs_syncvnode(vp, ap->a_waitfor, 0);
238 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
239 error = softdep_fsync(vp);
244 * The softdep_fsync() function may drop vp lock,
245 * allowing for dirty buffers to reappear on the
246 * bo_dirty list. Recheck and resync as needed.
249 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
250 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
256 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
262 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
266 struct ufsmount *ump;
267 struct buf *bp, *nbp;
269 int error, passes, wflag;
270 bool still_dirty, unlocked, wait;
274 ump = VFSTOUFS(vp->v_mount);
276 wflag = IS_SNAPSHOT(ip) ? LK_NOWITNESS : 0;
282 * When doing MNT_WAIT we must first flush all dependencies
285 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
286 (error = softdep_sync_metadata(vp)) != 0) {
287 if (ffs_fsfail_cleanup(ump, error))
293 * Flush all dirty buffers associated with a vnode.
297 wait = false; /* Always do an async pass first. */
299 lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
302 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
303 bp->b_vflags &= ~BV_SCANNED;
304 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
306 * Reasons to skip this buffer: it has already been considered
307 * on this pass, the buffer has dependencies that will cause
308 * it to be redirtied and it has not already been deferred,
309 * or it is already being written.
311 if ((bp->b_vflags & BV_SCANNED) != 0)
313 bp->b_vflags |= BV_SCANNED;
315 * Flush indirects in order, if requested.
317 * Note that if only datasync is requested, we can
318 * skip indirect blocks when softupdates are not
319 * active. Otherwise we must flush them with data,
320 * since dependencies prevent data block writes.
322 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
323 (lbn_level(bp->b_lblkno) >= passes ||
324 ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
326 if (bp->b_lblkno > lbn)
327 panic("ffs_syncvnode: syncing truncated data.");
328 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
331 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
332 LK_INTERLOCK | wflag, BO_LOCKPTR(bo)) != 0) {
334 bp->b_vflags &= ~BV_SCANNED;
339 if ((bp->b_flags & B_DELWRI) == 0)
340 panic("ffs_fsync: not dirty");
342 * Check for dependencies and potentially complete them.
344 if (!LIST_EMPTY(&bp->b_dep) &&
345 (error = softdep_sync_buf(vp, bp,
346 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
348 * Lock order conflict, buffer was already unlocked,
349 * and vnode possibly unlocked.
351 if (error == ERELOOKUP) {
352 if (vp->v_data == NULL)
355 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
356 (error = softdep_sync_metadata(vp)) != 0) {
357 if (ffs_fsfail_cleanup(ump, error))
359 return (unlocked && error == 0 ?
362 /* Re-evaluate inode size */
363 lbn = lblkno(ITOFS(ip), (ip->i_size +
364 ITOFS(ip)->fs_bsize - 1));
368 if (error != EBUSY) {
372 /* If we deferred once, don't defer again. */
373 if ((bp->b_flags & B_DEFERRED) == 0) {
374 bp->b_flags |= B_DEFERRED;
382 if (ffs_fsfail_cleanup(ump, error))
386 } else if ((bp->b_flags & B_CLUSTEROK)) {
387 (void) vfs_bio_awrite(bp);
394 * Since we may have slept during the I/O, we need
395 * to start from a known point.
399 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
401 if (waitfor != MNT_WAIT) {
403 if ((flags & NO_INO_UPDT) != 0)
404 return (unlocked ? ERELOOKUP : 0);
405 error = ffs_update(vp, 0);
406 if (error == 0 && unlocked)
410 /* Drain IO to see if we're done. */
411 bufobj_wwait(bo, 0, 0);
413 * Block devices associated with filesystems may have new I/O
414 * requests posted for them even if the vnode is locked, so no
415 * amount of trying will get them clean. We make several passes
418 * Regular files may need multiple passes to flush all dependency
419 * work as it is possible that we must write once per indirect
420 * level, once for the leaf, and once for the inode and each of
421 * these will be done with one sync and one async pass.
423 if (bo->bo_dirty.bv_cnt > 0) {
424 if ((flags & DATA_ONLY) == 0) {
428 * For data-only sync, dirty indirect buffers
432 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
433 if (bp->b_lblkno > -UFS_NDADDR) {
441 /* Write the inode after sync passes to flush deps. */
442 if (wait && DOINGSOFTDEP(vp) &&
443 (flags & NO_INO_UPDT) == 0) {
448 /* switch between sync/async. */
450 if (wait || ++passes < UFS_NIADDR + 2)
456 if ((flags & DATA_ONLY) == 0) {
457 if ((flags & NO_INO_UPDT) == 0)
458 error = ffs_update(vp, 1);
460 softdep_journal_fsync(VTOI(vp));
461 } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
462 error = ffs_update(vp, 1);
464 if (error == 0 && unlocked)
467 ip->i_flag &= ~IN_NEEDSYNC;
472 ffs_fdatasync(struct vop_fdatasync_args *ap)
475 return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
480 struct vop_lock1_args /* {
487 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
488 struct vnode *vp = ap->a_vp;
489 #endif /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
492 #endif /* DIAGNOSTIC */
494 #ifndef NO_FFS_SNAPSHOT
499 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
500 * and only use it when LK_NODDLKTREAT is set. Currently this means it
501 * is only used during path lookup.
503 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
504 ap->a_flags |= LK_ADAPTIVE;
505 switch (ap->a_flags & LK_TYPE_MASK) {
511 #ifdef DEBUG_VFS_LOCKS
512 VNPASS(vp->v_holdcnt != 0, vp);
513 #endif /* DEBUG_VFS_LOCKS */
515 result = lockmgr_lock_flags(lkp, flags,
516 &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
517 if (lkp == vp->v_vnlock || result != 0)
520 * Apparent success, except that the vnode
521 * mutated between snapshot file vnode and
522 * regular file vnode while this process
523 * slept. The lock currently held is not the
524 * right lock. Release it, and try to get the
528 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
529 (LK_INTERLOCK | LK_NOWAIT))
531 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
532 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
533 flags &= ~LK_INTERLOCK;
536 switch (ap->a_flags & LK_TYPE_MASK) {
539 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
545 #endif /* DIAGNOSTIC */
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);
558 #else /* NO_FFS_SNAPSHOT */
560 * See above for an explanation.
562 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
563 ap->a_flags |= LK_ADAPTIVE;
565 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
568 ufs_unlock_tracker(ip);
570 #endif /* DIAGNOSTIC */
571 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
572 #endif /* NO_FFS_SNAPSHOT */
574 switch (ap->a_flags & LK_TYPE_MASK) {
577 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
583 #endif /* DIAGNOSTIC */
589 ffs_unlock_debug(struct vop_unlock_args *ap)
596 if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
597 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
599 VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
600 ("%s: modified vnode (%x) not on lazy list",
601 __func__, ip->i_flag));
605 KASSERT(vp->v_type != VDIR || vp->v_vnlock->lk_recurse != 0 ||
606 (ip->i_flag & IN_ENDOFF) == 0,
607 ("ufs dir vp %p ip %p flags %#x", vp, ip, ip->i_flag));
609 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
610 vp->v_vnlock->lk_recurse == 0)
611 ufs_unlock_tracker(ip);
613 return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
618 ffs_read_hole(struct uio *uio, long xfersize, long *size)
620 ssize_t saved_resid, tlen;
623 while (xfersize > 0) {
624 tlen = min(xfersize, ZERO_REGION_SIZE);
625 saved_resid = uio->uio_resid;
626 error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
630 tlen = saved_resid - uio->uio_resid;
638 * Vnode op for reading.
642 struct vop_read_args /* {
646 struct ucred *a_cred;
654 ufs_lbn_t lbn, nextlbn;
656 long size, xfersize, blkoffset;
658 int bflag, error, ioflag, seqcount;
662 ioflag = ap->a_ioflag;
663 if (ap->a_ioflag & IO_EXT)
665 return (ffs_extread(vp, uio, ioflag));
667 panic("ffs_read+IO_EXT");
670 if ((ioflag & IO_DIRECT) != 0) {
673 error = ffs_rawread(vp, uio, &workdone);
674 if (error != 0 || workdone != 0)
679 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
683 if (uio->uio_rw != UIO_READ)
684 panic("ffs_read: mode");
686 if (vp->v_type == VLNK) {
687 if ((int)ip->i_size < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
688 panic("ffs_read: short symlink");
689 } else if (vp->v_type != VREG && vp->v_type != VDIR)
690 panic("ffs_read: type %d", vp->v_type);
692 orig_resid = uio->uio_resid;
693 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
696 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
698 if (uio->uio_offset < ip->i_size &&
699 uio->uio_offset >= fs->fs_maxfilesize)
702 bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
704 bflag |= IS_SNAPSHOT(ip) ? GB_NOWITNESS : 0;
706 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
707 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
709 lbn = lblkno(fs, uio->uio_offset);
713 * size of buffer. The buffer representing the
714 * end of the file is rounded up to the size of
715 * the block type ( fragment or full block,
718 size = blksize(fs, ip, lbn);
719 blkoffset = blkoff(fs, uio->uio_offset);
722 * The amount we want to transfer in this iteration is
723 * one FS block less the amount of the data before
724 * our startpoint (duh!)
726 xfersize = fs->fs_bsize - blkoffset;
729 * But if we actually want less than the block,
730 * or the file doesn't have a whole block more of data,
731 * then use the lesser number.
733 if (uio->uio_resid < xfersize)
734 xfersize = uio->uio_resid;
735 if (bytesinfile < xfersize)
736 xfersize = bytesinfile;
738 if (lblktosize(fs, nextlbn) >= ip->i_size) {
740 * Don't do readahead if this is the end of the file.
742 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
743 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
745 * Otherwise if we are allowed to cluster,
746 * grab as much as we can.
748 * XXX This may not be a win if we are not
749 * doing sequential access.
751 error = cluster_read(vp, ip->i_size, lbn,
752 size, NOCRED, blkoffset + uio->uio_resid,
753 seqcount, bflag, &bp);
754 } else if (seqcount > 1) {
756 * If we are NOT allowed to cluster, then
757 * if we appear to be acting sequentially,
758 * fire off a request for a readahead
759 * as well as a read. Note that the 4th and 5th
760 * arguments point to arrays of the size specified in
763 int nextsize = blksize(fs, ip, nextlbn);
764 error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
765 &nextsize, 1, NOCRED, bflag, NULL, &bp);
768 * Failing all of the above, just read what the
769 * user asked for. Interestingly, the same as
770 * the first option above.
772 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
774 if (error == EJUSTRETURN) {
775 error = ffs_read_hole(uio, xfersize, &size);
786 * We should only get non-zero b_resid when an I/O error
787 * has occurred, which should cause us to break above.
788 * However, if the short read did not cause an error,
789 * then we want to ensure that we do not uiomove bad
790 * or uninitialized data.
793 if (size < xfersize) {
799 if (buf_mapped(bp)) {
800 error = vn_io_fault_uiomove((char *)bp->b_data +
801 blkoffset, (int)xfersize, uio);
803 error = vn_io_fault_pgmove(bp->b_pages,
804 blkoffset + (bp->b_offset & PAGE_MASK),
810 vfs_bio_brelse(bp, ioflag);
814 * This can only happen in the case of an error
815 * because the loop above resets bp to NULL on each iteration
816 * and on normal completion has not set a new value into it.
817 * so it must have come from a 'break' statement
820 vfs_bio_brelse(bp, ioflag);
822 if ((error == 0 || uio->uio_resid != orig_resid) &&
823 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
824 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
829 * Vnode op for writing.
833 struct vop_write_args /* {
837 struct ucred *a_cred;
849 int blkoffset, error, flags, ioflag, size, xfersize;
853 softdep_prealloc(vp, MNT_WAIT);
854 if (vp->v_data == NULL)
858 ioflag = ap->a_ioflag;
859 if (ap->a_ioflag & IO_EXT)
861 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
863 panic("ffs_write+IO_EXT");
866 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
870 if (uio->uio_rw != UIO_WRITE)
871 panic("ffs_write: mode");
874 switch (vp->v_type) {
876 if (ioflag & IO_APPEND)
877 uio->uio_offset = ip->i_size;
878 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
884 panic("ffs_write: dir write");
887 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
888 (int)uio->uio_offset,
893 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
894 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
898 * Maybe this should be above the vnode op call, but so long as
899 * file servers have no limits, I don't think it matters.
901 error = vn_rlimit_fsizex(vp, uio, fs->fs_maxfilesize, &r,
904 vn_rlimit_fsizex_res(uio, r);
908 resid = uio->uio_resid;
910 if (seqcount > BA_SEQMAX)
911 flags = BA_SEQMAX << BA_SEQSHIFT;
913 flags = seqcount << BA_SEQSHIFT;
914 if (ioflag & IO_SYNC)
916 flags |= BA_UNMAPPED;
918 for (error = 0; uio->uio_resid > 0;) {
919 lbn = lblkno(fs, uio->uio_offset);
920 blkoffset = blkoff(fs, uio->uio_offset);
921 xfersize = fs->fs_bsize - blkoffset;
922 if (uio->uio_resid < xfersize)
923 xfersize = uio->uio_resid;
924 if (uio->uio_offset + xfersize > ip->i_size)
925 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
928 * We must perform a read-before-write if the transfer size
929 * does not cover the entire buffer.
931 if (fs->fs_bsize > xfersize)
935 /* XXX is uio->uio_offset the right thing here? */
936 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
937 ap->a_cred, flags, &bp);
939 vnode_pager_setsize(vp, ip->i_size);
942 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
943 bp->b_flags |= B_NOCACHE;
945 if (uio->uio_offset + xfersize > ip->i_size) {
946 ip->i_size = uio->uio_offset + xfersize;
947 DIP_SET(ip, i_size, ip->i_size);
948 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
951 size = blksize(fs, ip, lbn) - bp->b_resid;
955 if (buf_mapped(bp)) {
956 error = vn_io_fault_uiomove((char *)bp->b_data +
957 blkoffset, (int)xfersize, uio);
959 error = vn_io_fault_pgmove(bp->b_pages,
960 blkoffset + (bp->b_offset & PAGE_MASK),
964 * If the buffer is not already filled and we encounter an
965 * error while trying to fill it, we have to clear out any
966 * garbage data from the pages instantiated for the buffer.
967 * If we do not, a failed uiomove() during a write can leave
968 * the prior contents of the pages exposed to a userland mmap.
970 * Note that we need only clear buffers with a transfer size
971 * equal to the block size because buffers with a shorter
972 * transfer size were cleared above by the call to UFS_BALLOC()
973 * with the BA_CLRBUF flag set.
975 * If the source region for uiomove identically mmaps the
976 * buffer, uiomove() performed the NOP copy, and the buffer
977 * content remains valid because the page fault handler
978 * validated the pages.
980 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
981 fs->fs_bsize == xfersize) {
982 if (error == EFAULT && LIST_EMPTY(&bp->b_dep)) {
983 bp->b_flags |= B_INVAL | B_RELBUF | B_NOCACHE;
991 vfs_bio_set_flags(bp, ioflag);
994 * If IO_SYNC each buffer is written synchronously. Otherwise
995 * if we have a severe page deficiency write the buffer
996 * asynchronously. Otherwise try to cluster, and if that
997 * doesn't do it then either do an async write (if O_DIRECT),
998 * or a delayed write (if not).
1000 if (ioflag & IO_SYNC) {
1002 } else if (vm_page_count_severe() ||
1003 buf_dirty_count_severe() ||
1004 (ioflag & IO_ASYNC)) {
1005 bp->b_flags |= B_CLUSTEROK;
1007 } else if (xfersize + blkoffset == fs->fs_bsize) {
1008 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
1009 bp->b_flags |= B_CLUSTEROK;
1010 cluster_write(vp, &ip->i_clusterw, bp,
1011 ip->i_size, seqcount, GB_UNMAPPED);
1015 } else if (ioflag & IO_DIRECT) {
1016 bp->b_flags |= B_CLUSTEROK;
1019 bp->b_flags |= B_CLUSTEROK;
1022 if (error || xfersize == 0)
1024 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1027 * If we successfully wrote any data, and we are not the superuser
1028 * we clear the setuid and setgid bits as a precaution against
1031 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
1033 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
1034 vn_seqc_write_begin(vp);
1035 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1036 DIP_SET(ip, i_mode, ip->i_mode);
1037 vn_seqc_write_end(vp);
1041 if (ioflag & IO_UNIT) {
1042 (void)ffs_truncate(vp, osize,
1043 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1044 uio->uio_offset -= resid - uio->uio_resid;
1045 uio->uio_resid = resid;
1047 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1048 if (!(ioflag & IO_DATASYNC) ||
1049 (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1050 error = ffs_update(vp, 1);
1051 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1054 vn_rlimit_fsizex_res(uio, r);
1059 * Extended attribute area reading.
1062 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1065 struct ufs2_dinode *dp;
1068 ufs_lbn_t lbn, nextlbn;
1070 long size, xfersize, blkoffset;
1079 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1080 panic("ffs_extread: mode");
1083 orig_resid = uio->uio_resid;
1084 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1085 if (orig_resid == 0)
1087 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1089 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1090 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1092 lbn = lblkno(fs, uio->uio_offset);
1096 * size of buffer. The buffer representing the
1097 * end of the file is rounded up to the size of
1098 * the block type ( fragment or full block,
1101 size = sblksize(fs, dp->di_extsize, lbn);
1102 blkoffset = blkoff(fs, uio->uio_offset);
1105 * The amount we want to transfer in this iteration is
1106 * one FS block less the amount of the data before
1107 * our startpoint (duh!)
1109 xfersize = fs->fs_bsize - blkoffset;
1112 * But if we actually want less than the block,
1113 * or the file doesn't have a whole block more of data,
1114 * then use the lesser number.
1116 if (uio->uio_resid < xfersize)
1117 xfersize = uio->uio_resid;
1118 if (bytesinfile < xfersize)
1119 xfersize = bytesinfile;
1121 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1123 * Don't do readahead if this is the end of the info.
1125 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1128 * If we have a second block, then
1129 * fire off a request for a readahead
1130 * as well as a read. Note that the 4th and 5th
1131 * arguments point to arrays of the size specified in
1134 int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
1135 nextlbn = -1 - nextlbn;
1136 error = breadn(vp, -1 - lbn,
1137 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1146 * We should only get non-zero b_resid when an I/O error
1147 * has occurred, which should cause us to break above.
1148 * However, if the short read did not cause an error,
1149 * then we want to ensure that we do not uiomove bad
1150 * or uninitialized data.
1152 size -= bp->b_resid;
1153 if (size < xfersize) {
1159 error = uiomove((char *)bp->b_data + blkoffset,
1160 (int)xfersize, uio);
1163 vfs_bio_brelse(bp, ioflag);
1167 * This can only happen in the case of an error
1168 * because the loop above resets bp to NULL on each iteration
1169 * and on normal completion has not set a new value into it.
1170 * so it must have come from a 'break' statement
1173 vfs_bio_brelse(bp, ioflag);
1178 * Extended attribute area writing.
1181 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1184 struct ufs2_dinode *dp;
1190 int blkoffset, error, flags, size, xfersize;
1197 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1198 panic("ffs_extwrite: mode");
1201 if (ioflag & IO_APPEND)
1202 uio->uio_offset = dp->di_extsize;
1203 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1204 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1205 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1206 UFS_NXADDR * fs->fs_bsize)
1209 resid = uio->uio_resid;
1210 osize = dp->di_extsize;
1212 if (ioflag & IO_SYNC)
1215 for (error = 0; uio->uio_resid > 0;) {
1216 lbn = lblkno(fs, uio->uio_offset);
1217 blkoffset = blkoff(fs, uio->uio_offset);
1218 xfersize = fs->fs_bsize - blkoffset;
1219 if (uio->uio_resid < xfersize)
1220 xfersize = uio->uio_resid;
1223 * We must perform a read-before-write if the transfer size
1224 * does not cover the entire buffer.
1226 if (fs->fs_bsize > xfersize)
1229 flags &= ~BA_CLRBUF;
1230 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1235 * If the buffer is not valid we have to clear out any
1236 * garbage data from the pages instantiated for the buffer.
1237 * If we do not, a failed uiomove() during a write can leave
1238 * the prior contents of the pages exposed to a userland
1239 * mmap(). XXX deal with uiomove() errors a better way.
1241 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1244 if (uio->uio_offset + xfersize > dp->di_extsize) {
1245 dp->di_extsize = uio->uio_offset + xfersize;
1246 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
1249 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1250 if (size < xfersize)
1254 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1256 vfs_bio_set_flags(bp, ioflag);
1259 * If IO_SYNC each buffer is written synchronously. Otherwise
1260 * if we have a severe page deficiency write the buffer
1261 * asynchronously. Otherwise try to cluster, and if that
1262 * doesn't do it then either do an async write (if O_DIRECT),
1263 * or a delayed write (if not).
1265 if (ioflag & IO_SYNC) {
1267 } else if (vm_page_count_severe() ||
1268 buf_dirty_count_severe() ||
1269 xfersize + blkoffset == fs->fs_bsize ||
1270 (ioflag & (IO_ASYNC | IO_DIRECT)))
1274 if (error || xfersize == 0)
1276 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1279 * If we successfully wrote any data, and we are not the superuser
1280 * we clear the setuid and setgid bits as a precaution against
1283 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1284 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) {
1285 vn_seqc_write_begin(vp);
1286 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1287 dp->di_mode = ip->i_mode;
1288 vn_seqc_write_end(vp);
1292 if (ioflag & IO_UNIT) {
1293 (void)ffs_truncate(vp, osize,
1294 IO_EXT | (ioflag&IO_SYNC), ucred);
1295 uio->uio_offset -= resid - uio->uio_resid;
1296 uio->uio_resid = resid;
1298 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1299 error = ffs_update(vp, 1);
1304 * Vnode operating to retrieve a named extended attribute.
1306 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1307 * the length of the EA, and possibly the pointer to the entry and to the data.
1310 ffs_findextattr(uint8_t *ptr, uint64_t length, int nspace, const char *name,
1311 struct extattr **eapp, uint8_t **eac)
1313 struct extattr *eap, *eaend;
1316 nlen = strlen(name);
1317 KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned"));
1318 eap = (struct extattr *)ptr;
1319 eaend = (struct extattr *)(ptr + length);
1320 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
1321 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1322 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1323 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
1324 || memcmp(eap->ea_name, name, nlen) != 0)
1329 *eac = EXTATTR_CONTENT(eap);
1330 return (EXTATTR_CONTENT_SIZE(eap));
1336 ffs_rdextattr(uint8_t **p, struct vnode *vp, struct thread *td)
1338 const struct extattr *eap, *eaend, *eapnext;
1340 struct ufs2_dinode *dp;
1343 struct iovec liovec;
1351 easize = dp->di_extsize;
1352 if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize)
1355 eae = malloc(easize, M_TEMP, M_WAITOK);
1357 liovec.iov_base = eae;
1358 liovec.iov_len = easize;
1359 luio.uio_iov = &liovec;
1360 luio.uio_iovcnt = 1;
1361 luio.uio_offset = 0;
1362 luio.uio_resid = easize;
1363 luio.uio_segflg = UIO_SYSSPACE;
1364 luio.uio_rw = UIO_READ;
1367 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1372 /* Validate disk xattrfile contents. */
1373 for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend;
1375 /* Detect zeroed out tail */
1376 if (eap->ea_length < sizeof(*eap) || eap->ea_length == 0) {
1377 easize = (const uint8_t *)eap - eae;
1381 eapnext = EXTATTR_NEXT(eap);
1382 /* Bogusly long entry. */
1383 if (eapnext > eaend) {
1385 return (EINTEGRITY);
1388 ip->i_ea_len = easize;
1394 ffs_lock_ea(struct vnode *vp)
1400 while (ip->i_flag & IN_EA_LOCKED) {
1401 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT);
1402 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1405 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED);
1410 ffs_unlock_ea(struct vnode *vp)
1416 if (ip->i_flag & IN_EA_LOCKWAIT)
1417 wakeup(&ip->i_ea_refs);
1418 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1423 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1431 if (ip->i_ea_area != NULL) {
1436 error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1448 * Vnode extattr transaction commit/abort
1451 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1455 struct iovec *liovec;
1456 struct ufs2_dinode *dp;
1457 size_t ea_len, tlen;
1464 if (ip->i_ea_area == NULL) {
1469 error = ip->i_ea_error;
1471 if (commit && error == 0) {
1472 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1474 cred = vp->v_mount->mnt_cred;
1476 ea_len = MAX(ip->i_ea_len, dp->di_extsize);
1477 for (lcnt = 1, tlen = ea_len - ip->i_ea_len; tlen > 0;) {
1478 tlen -= MIN(ZERO_REGION_SIZE, tlen);
1482 liovec = __builtin_alloca(lcnt * sizeof(struct iovec));
1483 luio.uio_iovcnt = lcnt;
1485 liovec[0].iov_base = ip->i_ea_area;
1486 liovec[0].iov_len = ip->i_ea_len;
1487 for (i = 1, tlen = ea_len - ip->i_ea_len; i < lcnt; i++) {
1488 liovec[i].iov_base = __DECONST(void *, zero_region);
1489 liovec[i].iov_len = MIN(ZERO_REGION_SIZE, tlen);
1490 tlen -= liovec[i].iov_len;
1494 luio.uio_iov = liovec;
1495 luio.uio_offset = 0;
1496 luio.uio_resid = ea_len;
1497 luio.uio_segflg = UIO_SYSSPACE;
1498 luio.uio_rw = UIO_WRITE;
1500 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1501 if (error == 0 && ip->i_ea_len == 0)
1504 if (--ip->i_ea_refs == 0) {
1505 free(ip->i_ea_area, M_TEMP);
1506 ip->i_ea_area = NULL;
1513 ffs_truncate(vp, 0, IO_EXT, cred);
1518 * Vnode extattr strategy routine for fifos.
1520 * We need to check for a read or write of the external attributes.
1521 * Otherwise we just fall through and do the usual thing.
1525 struct vop_strategy_args /* {
1526 struct vnodeop_desc *a_desc;
1535 lbn = ap->a_bp->b_lblkno;
1536 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1537 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1538 if (vp->v_type == VFIFO)
1539 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1540 panic("spec nodes went here");
1544 * Vnode extattr transaction commit/abort
1548 struct vop_openextattr_args /* {
1549 struct vnodeop_desc *a_desc;
1551 IN struct ucred *a_cred;
1552 IN struct thread *a_td;
1556 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1557 return (EOPNOTSUPP);
1559 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1563 * Vnode extattr transaction commit/abort
1567 struct vop_closeextattr_args /* {
1568 struct vnodeop_desc *a_desc;
1571 IN struct ucred *a_cred;
1572 IN struct thread *a_td;
1578 if (vp->v_type == VCHR || vp->v_type == VBLK)
1579 return (EOPNOTSUPP);
1580 if (ap->a_commit && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
1583 if (ap->a_commit && DOINGSUJ(vp)) {
1584 ASSERT_VOP_ELOCKED(vp, "ffs_closeextattr commit");
1585 softdep_prealloc(vp, MNT_WAIT);
1586 if (vp->v_data == NULL)
1589 return (ffs_close_ea(vp, ap->a_commit, ap->a_cred, ap->a_td));
1593 * Vnode operation to remove a named attribute.
1597 struct vop_deleteextattr_args /* {
1598 IN struct vnode *a_vp;
1599 IN int a_attrnamespace;
1600 IN const char *a_name;
1601 IN struct ucred *a_cred;
1602 IN struct thread *a_td;
1607 struct extattr *eap;
1609 int olen, error, i, easize;
1616 if (vp->v_type == VCHR || vp->v_type == VBLK)
1617 return (EOPNOTSUPP);
1618 if (strlen(ap->a_name) == 0)
1620 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1623 error = extattr_check_cred(vp, ap->a_attrnamespace,
1624 ap->a_cred, ap->a_td, VWRITE);
1627 * ffs_lock_ea is not needed there, because the vnode
1628 * must be exclusively locked.
1630 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1631 ip->i_ea_error = error;
1636 ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1637 softdep_prealloc(vp, MNT_WAIT);
1638 if (vp->v_data == NULL)
1642 error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1646 /* CEM: delete could be done in-place instead */
1647 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1648 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1649 easize = ip->i_ea_len;
1651 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1654 /* delete but nonexistent */
1656 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1659 ul = eap->ea_length;
1660 i = (uint8_t *)EXTATTR_NEXT(eap) - eae;
1661 bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1664 tmp = ip->i_ea_area;
1665 ip->i_ea_area = eae;
1666 ip->i_ea_len = easize;
1668 error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1673 * Vnode operation to retrieve a named extended attribute.
1677 struct vop_getextattr_args /* {
1678 IN struct vnode *a_vp;
1679 IN int a_attrnamespace;
1680 IN const char *a_name;
1681 INOUT struct uio *a_uio;
1683 IN struct ucred *a_cred;
1684 IN struct thread *a_td;
1692 ip = VTOI(ap->a_vp);
1694 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1695 return (EOPNOTSUPP);
1697 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1698 ap->a_cred, ap->a_td, VREAD);
1702 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1706 eae = ip->i_ea_area;
1707 easize = ip->i_ea_len;
1709 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1713 if (ap->a_size != NULL)
1714 *ap->a_size = ealen;
1715 else if (ap->a_uio != NULL)
1716 error = uiomove(p, ealen, ap->a_uio);
1720 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1725 * Vnode operation to retrieve extended attributes on a vnode.
1729 struct vop_listextattr_args /* {
1730 IN struct vnode *a_vp;
1731 IN int a_attrnamespace;
1732 INOUT struct uio *a_uio;
1734 IN struct ucred *a_cred;
1735 IN struct thread *a_td;
1739 struct extattr *eap, *eaend;
1742 ip = VTOI(ap->a_vp);
1744 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1745 return (EOPNOTSUPP);
1747 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1748 ap->a_cred, ap->a_td, VREAD);
1752 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1757 if (ap->a_size != NULL)
1760 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1761 eap = (struct extattr *)ip->i_ea_area;
1762 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1763 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1764 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1765 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1766 if (eap->ea_namespace != ap->a_attrnamespace)
1769 ealen = eap->ea_namelength;
1770 if (ap->a_size != NULL)
1771 *ap->a_size += ealen + 1;
1772 else if (ap->a_uio != NULL)
1773 error = uiomove(&eap->ea_namelength, ealen + 1,
1777 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1782 * Vnode operation to set a named attribute.
1786 struct vop_setextattr_args /* {
1787 IN struct vnode *a_vp;
1788 IN int a_attrnamespace;
1789 IN const char *a_name;
1790 INOUT struct uio *a_uio;
1791 IN struct ucred *a_cred;
1792 IN struct thread *a_td;
1798 struct extattr *eap;
1799 uint32_t ealength, ul;
1801 int olen, eapad1, eapad2, error, i, easize;
1809 if (vp->v_type == VCHR || vp->v_type == VBLK)
1810 return (EOPNOTSUPP);
1811 if (strlen(ap->a_name) == 0)
1814 /* XXX Now unsupported API to delete EAs using NULL uio. */
1815 if (ap->a_uio == NULL)
1816 return (EOPNOTSUPP);
1818 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1821 ealen = ap->a_uio->uio_resid;
1822 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1825 error = extattr_check_cred(vp, ap->a_attrnamespace,
1826 ap->a_cred, ap->a_td, VWRITE);
1829 * ffs_lock_ea is not needed there, because the vnode
1830 * must be exclusively locked.
1832 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1833 ip->i_ea_error = error;
1838 ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1839 softdep_prealloc(vp, MNT_WAIT);
1840 if (vp->v_data == NULL)
1844 error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1848 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1849 eapad1 = roundup2(ealength, 8) - ealength;
1850 eapad2 = roundup2(ealen, 8) - ealen;
1851 ealength += eapad1 + ealen + eapad2;
1854 * CEM: rewrites of the same size or smaller could be done in-place
1855 * instead. (We don't acquire any fine-grained locks in here either,
1856 * so we could also do bigger writes in-place.)
1858 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1859 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1860 easize = ip->i_ea_len;
1862 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1865 /* new, append at end */
1866 KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1868 eap = (struct extattr *)(eae + easize);
1871 ul = eap->ea_length;
1872 i = (uint8_t *)EXTATTR_NEXT(eap) - eae;
1873 if (ul != ealength) {
1874 bcopy(EXTATTR_NEXT(eap), (uint8_t *)eap + ealength,
1876 easize += (ealength - ul);
1879 if (easize > lblktosize(fs, UFS_NXADDR)) {
1881 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1882 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1883 ip->i_ea_error = ENOSPC;
1886 eap->ea_length = ealength;
1887 eap->ea_namespace = ap->a_attrnamespace;
1888 eap->ea_contentpadlen = eapad2;
1889 eap->ea_namelength = strlen(ap->a_name);
1890 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1891 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1892 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1895 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1896 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1897 ip->i_ea_error = error;
1900 bzero((uint8_t *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1902 tmp = ip->i_ea_area;
1903 ip->i_ea_area = eae;
1904 ip->i_ea_len = easize;
1906 error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1911 * Vnode pointer to File handle
1915 struct vop_vptofh_args /* {
1916 IN struct vnode *a_vp;
1917 IN struct fid *a_fhp;
1923 ip = VTOI(ap->a_vp);
1924 ufhp = (struct ufid *)ap->a_fhp;
1925 ufhp->ufid_len = sizeof(struct ufid);
1926 ufhp->ufid_ino = ip->i_number;
1927 ufhp->ufid_gen = ip->i_gen;
1931 SYSCTL_DECL(_vfs_ffs);
1932 static int use_buf_pager = 1;
1933 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1934 "Always use buffer pager instead of bmap");
1937 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1940 return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1944 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
1947 *sz = blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn);
1952 ffs_getpages(struct vop_getpages_args *ap)
1955 struct ufsmount *um;
1958 um = VFSTOUFS(vp->v_mount);
1960 if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1961 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1962 ap->a_rbehind, ap->a_rahead, NULL, NULL));
1963 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1964 ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1968 ffs_getpages_async(struct vop_getpages_async_args *ap)
1971 struct ufsmount *um;
1976 um = VFSTOUFS(vp->v_mount);
1979 if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1980 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1981 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1985 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1986 ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1989 if (do_iodone && ap->a_iodone != NULL)
1990 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
1996 ffs_vput_pair(struct vop_vput_pair_args *ap)
1999 struct vnode *dvp, *vp, *vp1, **vpp;
2000 struct inode *dp, *ip;
2003 int error, vp_locked;
2008 vp = vpp != NULL ? *vpp : NULL;
2010 if ((dp->i_flag & (IN_NEEDSYNC | IN_ENDOFF)) == 0) {
2012 if (vp != NULL && ap->a_unlock_vp)
2019 if (ap->a_unlock_vp) {
2022 MPASS(vp->v_type != VNON);
2023 vp_locked = VOP_ISLOCKED(vp);
2025 ip_ino = ip->i_number;
2032 * If compaction or fsync was requested do it in ffs_vput_pair()
2033 * now that other locks are no longer held.
2035 if ((dp->i_flag & IN_ENDOFF) != 0) {
2036 VNASSERT(I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size, dvp,
2037 ("IN_ENDOFF set but I_ENDOFF() is not"));
2038 dp->i_flag &= ~IN_ENDOFF;
2039 error = UFS_TRUNCATE(dvp, (off_t)I_ENDOFF(dp), IO_NORMAL |
2040 (DOINGASYNC(dvp) ? 0 : IO_SYNC), curthread->td_ucred);
2041 if (error != 0 && error != ERELOOKUP) {
2042 if (!ffs_fsfail_cleanup(VFSTOUFS(mp), error)) {
2044 "IN_ENDOFF: failed to truncate, "
2045 "error %d\n", error);
2048 ufsdirhash_free(dp);
2051 SET_I_ENDOFF(dp, 0);
2053 if ((dp->i_flag & IN_NEEDSYNC) != 0) {
2055 error = ffs_syncvnode(dvp, MNT_WAIT, 0);
2056 } while (error == ERELOOKUP);
2061 if (vp == NULL || ap->a_unlock_vp)
2066 * It is possible that vp is reclaimed at this point. Only
2067 * routines that call us with a_unlock_vp == false can find
2068 * that their vp has been reclaimed. There are three areas
2069 * that are affected:
2070 * 1) vn_open_cred() - later VOPs could fail, but
2071 * dead_open() returns 0 to simulate successful open.
2072 * 2) ffs_snapshot() - creation of snapshot fails with EBADF.
2073 * 3) NFS server (several places) - code is prepared to detect
2074 * and respond to dead vnodes by returning ESTALE.
2076 VOP_LOCK(vp, vp_locked | LK_RETRY);
2081 * Try harder to recover from reclaimed vp if reclaim was not
2082 * because underlying inode was cleared. We saved inode
2083 * number and inode generation, so we can try to reinstantiate
2084 * exactly same version of inode. If this fails, return
2085 * original doomed vnode and let caller to handle
2088 * Note that callers must keep write started around
2089 * VOP_VPUT_PAIR() calls, so it is safe to use mp without
2093 error = ffs_inotovp(mp, ip_ino, ip_gen, LK_EXCLUSIVE, &vp1,
2094 FFSV_REPLACE_DOOMED);
2096 VOP_LOCK(vp, vp_locked | LK_RETRY);