2 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * from: @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
60 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
61 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
67 #include <sys/param.h>
69 #include <sys/systm.h>
72 #include <sys/extattr.h>
73 #include <sys/kernel.h>
74 #include <sys/limits.h>
75 #include <sys/malloc.h>
76 #include <sys/mount.h>
79 #include <sys/resourcevar.h>
80 #include <sys/signalvar.h>
82 #include <sys/vmmeter.h>
83 #include <sys/vnode.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_pager.h>
90 #include <vm/vnode_pager.h>
92 #include <ufs/ufs/extattr.h>
93 #include <ufs/ufs/quota.h>
94 #include <ufs/ufs/inode.h>
95 #include <ufs/ufs/ufs_extern.h>
96 #include <ufs/ufs/ufsmount.h>
98 #include <ufs/ffs/fs.h>
99 #include <ufs/ffs/ffs_extern.h>
100 #include "opt_directio.h"
104 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
106 static vop_fsync_t ffs_fsync;
107 static vop_lock1_t ffs_lock;
108 static vop_getpages_t ffs_getpages;
109 static vop_read_t ffs_read;
110 static vop_write_t ffs_write;
111 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
112 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
114 static vop_strategy_t ffsext_strategy;
115 static vop_closeextattr_t ffs_closeextattr;
116 static vop_deleteextattr_t ffs_deleteextattr;
117 static vop_getextattr_t ffs_getextattr;
118 static vop_listextattr_t ffs_listextattr;
119 static vop_openextattr_t ffs_openextattr;
120 static vop_setextattr_t ffs_setextattr;
121 static vop_vptofh_t ffs_vptofh;
124 /* Global vfs data structures for ufs. */
125 struct vop_vector ffs_vnodeops1 = {
126 .vop_default = &ufs_vnodeops,
127 .vop_fsync = ffs_fsync,
128 .vop_getpages = ffs_getpages,
129 .vop_lock1 = ffs_lock,
130 .vop_read = ffs_read,
131 .vop_reallocblks = ffs_reallocblks,
132 .vop_write = ffs_write,
133 .vop_vptofh = ffs_vptofh,
136 struct vop_vector ffs_fifoops1 = {
137 .vop_default = &ufs_fifoops,
138 .vop_fsync = ffs_fsync,
139 .vop_reallocblks = ffs_reallocblks, /* XXX: really ??? */
140 .vop_vptofh = ffs_vptofh,
143 /* Global vfs data structures for ufs. */
144 struct vop_vector ffs_vnodeops2 = {
145 .vop_default = &ufs_vnodeops,
146 .vop_fsync = ffs_fsync,
147 .vop_getpages = ffs_getpages,
148 .vop_lock1 = ffs_lock,
149 .vop_read = ffs_read,
150 .vop_reallocblks = ffs_reallocblks,
151 .vop_write = ffs_write,
152 .vop_closeextattr = ffs_closeextattr,
153 .vop_deleteextattr = ffs_deleteextattr,
154 .vop_getextattr = ffs_getextattr,
155 .vop_listextattr = ffs_listextattr,
156 .vop_openextattr = ffs_openextattr,
157 .vop_setextattr = ffs_setextattr,
158 .vop_vptofh = ffs_vptofh,
161 struct vop_vector ffs_fifoops2 = {
162 .vop_default = &ufs_fifoops,
163 .vop_fsync = ffs_fsync,
164 .vop_lock1 = ffs_lock,
165 .vop_reallocblks = ffs_reallocblks,
166 .vop_strategy = ffsext_strategy,
167 .vop_closeextattr = ffs_closeextattr,
168 .vop_deleteextattr = ffs_deleteextattr,
169 .vop_getextattr = ffs_getextattr,
170 .vop_listextattr = ffs_listextattr,
171 .vop_openextattr = ffs_openextattr,
172 .vop_setextattr = ffs_setextattr,
173 .vop_vptofh = ffs_vptofh,
177 * Synch an open file.
181 ffs_fsync(struct vop_fsync_args *ap)
185 error = ffs_syncvnode(ap->a_vp, ap->a_waitfor);
188 if (ap->a_waitfor == MNT_WAIT &&
189 (ap->a_vp->v_mount->mnt_flag & MNT_SOFTDEP))
190 error = softdep_fsync(ap->a_vp);
195 ffs_syncvnode(struct vnode *vp, int waitfor)
197 struct inode *ip = VTOI(vp);
201 int s, error, wait, passes, skipmeta;
204 wait = (waitfor == MNT_WAIT);
205 lbn = lblkno(ip->i_fs, (ip->i_size + ip->i_fs->fs_bsize - 1));
209 * Flush all dirty buffers associated with a vnode.
218 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
219 bp->b_vflags &= ~BV_SCANNED;
220 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
222 * Reasons to skip this buffer: it has already been considered
223 * on this pass, this pass is the first time through on a
224 * synchronous flush request and the buffer being considered
225 * is metadata, the buffer has dependencies that will cause
226 * it to be redirtied and it has not already been deferred,
227 * or it is already being written.
229 if ((bp->b_vflags & BV_SCANNED) != 0)
231 bp->b_vflags |= BV_SCANNED;
232 if ((skipmeta == 1 && bp->b_lblkno < 0))
234 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
237 if (!wait && !LIST_EMPTY(&bp->b_dep) &&
238 (bp->b_flags & B_DEFERRED) == 0 &&
239 buf_countdeps(bp, 0)) {
240 bp->b_flags |= B_DEFERRED;
245 if ((bp->b_flags & B_DELWRI) == 0)
246 panic("ffs_fsync: not dirty");
248 * If this is a synchronous flush request, or it is not a
249 * file or device, start the write on this buffer immediately.
251 if (wait || (vp->v_type != VREG && vp->v_type != VBLK)) {
254 * On our final pass through, do all I/O synchronously
255 * so that we can find out if our flush is failing
256 * because of write errors.
258 if (passes > 0 || !wait) {
259 if ((bp->b_flags & B_CLUSTEROK) && !wait) {
260 (void) vfs_bio_awrite(bp);
270 if ((error = bwrite(bp)) != 0)
274 } else if ((vp->v_type == VREG) && (bp->b_lblkno >= lbn)) {
276 * If the buffer is for data that has been truncated
277 * off the file, then throw it away.
280 bp->b_flags |= B_INVAL | B_NOCACHE;
288 * Since we may have slept during the I/O, we need
289 * to start from a known point.
292 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
295 * If we were asked to do this synchronously, then go back for
296 * another pass, this time doing the metadata.
304 bufobj_wwait(bo, 3, 0);
308 * Ensure that any filesystem metatdata associated
309 * with the vnode has been written.
312 if ((error = softdep_sync_metadata(vp)) != 0)
317 if (bo->bo_dirty.bv_cnt > 0) {
319 * Block devices associated with filesystems may
320 * have new I/O requests posted for them even if
321 * the vnode is locked, so no amount of trying will
322 * get them clean. Thus we give block devices a
323 * good effort, then just give up. For all other file
324 * types, go around and try again until it is clean.
331 if (!vn_isdisk(vp, NULL))
332 vprint("ffs_fsync: dirty", vp);
338 return (ffs_update(vp, wait));
343 struct vop_lock1_args /* {
351 #ifndef NO_FFS_SNAPSHOT
357 switch (ap->a_flags & LK_TYPE_MASK) {
365 result = _lockmgr_args(lkp, flags, VI_MTX(vp),
366 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
367 ap->a_file, ap->a_line);
368 if (lkp == vp->v_vnlock || result != 0)
371 * Apparent success, except that the vnode
372 * mutated between snapshot file vnode and
373 * regular file vnode while this process
374 * slept. The lock currently held is not the
375 * right lock. Release it, and try to get the
378 (void) _lockmgr_args(lkp, LK_RELEASE, NULL,
379 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
380 ap->a_file, ap->a_line);
381 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
382 (LK_INTERLOCK | LK_NOWAIT))
384 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
385 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
386 flags &= ~LK_INTERLOCK;
390 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
394 return (VOP_LOCK1_APV(&ufs_vnodeops, ap));
399 * Vnode op for reading.
404 struct vop_read_args /* {
408 struct ucred *a_cred;
416 ufs_lbn_t lbn, nextlbn;
418 long size, xfersize, blkoffset;
419 int error, orig_resid;
425 ioflag = ap->a_ioflag;
426 if (ap->a_ioflag & IO_EXT)
428 return (ffs_extread(vp, uio, ioflag));
430 panic("ffs_read+IO_EXT");
433 if ((ioflag & IO_DIRECT) != 0) {
436 error = ffs_rawread(vp, uio, &workdone);
437 if (error != 0 || workdone != 0)
442 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
446 if (uio->uio_rw != UIO_READ)
447 panic("ffs_read: mode");
449 if (vp->v_type == VLNK) {
450 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
451 panic("ffs_read: short symlink");
452 } else if (vp->v_type != VREG && vp->v_type != VDIR)
453 panic("ffs_read: type %d", vp->v_type);
455 orig_resid = uio->uio_resid;
456 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
459 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
461 if (uio->uio_offset < ip->i_size &&
462 uio->uio_offset >= fs->fs_maxfilesize)
465 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
466 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
468 lbn = lblkno(fs, uio->uio_offset);
472 * size of buffer. The buffer representing the
473 * end of the file is rounded up to the size of
474 * the block type ( fragment or full block,
477 size = blksize(fs, ip, lbn);
478 blkoffset = blkoff(fs, uio->uio_offset);
481 * The amount we want to transfer in this iteration is
482 * one FS block less the amount of the data before
483 * our startpoint (duh!)
485 xfersize = fs->fs_bsize - blkoffset;
488 * But if we actually want less than the block,
489 * or the file doesn't have a whole block more of data,
490 * then use the lesser number.
492 if (uio->uio_resid < xfersize)
493 xfersize = uio->uio_resid;
494 if (bytesinfile < xfersize)
495 xfersize = bytesinfile;
497 if (lblktosize(fs, nextlbn) >= ip->i_size) {
499 * Don't do readahead if this is the end of the file.
501 error = bread(vp, lbn, size, NOCRED, &bp);
502 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
504 * Otherwise if we are allowed to cluster,
505 * grab as much as we can.
507 * XXX This may not be a win if we are not
508 * doing sequential access.
510 error = cluster_read(vp, ip->i_size, lbn,
511 size, NOCRED, blkoffset + uio->uio_resid, seqcount, &bp);
512 } else if (seqcount > 1) {
514 * If we are NOT allowed to cluster, then
515 * if we appear to be acting sequentially,
516 * fire off a request for a readahead
517 * as well as a read. Note that the 4th and 5th
518 * arguments point to arrays of the size specified in
521 int nextsize = blksize(fs, ip, nextlbn);
522 error = breadn(vp, lbn,
523 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
526 * Failing all of the above, just read what the
527 * user asked for. Interestingly, the same as
528 * the first option above.
530 error = bread(vp, lbn, size, NOCRED, &bp);
539 * If IO_DIRECT then set B_DIRECT for the buffer. This
540 * will cause us to attempt to release the buffer later on
541 * and will cause the buffer cache to attempt to free the
544 if (ioflag & IO_DIRECT)
545 bp->b_flags |= B_DIRECT;
548 * We should only get non-zero b_resid when an I/O error
549 * has occurred, which should cause us to break above.
550 * However, if the short read did not cause an error,
551 * then we want to ensure that we do not uiomove bad
552 * or uninitialized data.
555 if (size < xfersize) {
561 error = uiomove((char *)bp->b_data + blkoffset,
566 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
567 (LIST_EMPTY(&bp->b_dep))) {
569 * If there are no dependencies, and it's VMIO,
570 * then we don't need the buf, mark it available
571 * for freeing. The VM has the data.
573 bp->b_flags |= B_RELBUF;
577 * Otherwise let whoever
578 * made the request take care of
579 * freeing it. We just queue
580 * it onto another list.
587 * This can only happen in the case of an error
588 * because the loop above resets bp to NULL on each iteration
589 * and on normal completion has not set a new value into it.
590 * so it must have come from a 'break' statement
593 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
594 (LIST_EMPTY(&bp->b_dep))) {
595 bp->b_flags |= B_RELBUF;
602 if ((error == 0 || uio->uio_resid != orig_resid) &&
603 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0 &&
604 (ip->i_flag & IN_ACCESS) == 0) {
606 ip->i_flag |= IN_ACCESS;
613 * Vnode op for writing.
617 struct vop_write_args /* {
621 struct ucred *a_cred;
633 int blkoffset, error, flags, ioflag, resid, size, xfersize;
637 ioflag = ap->a_ioflag;
638 if (ap->a_ioflag & IO_EXT)
640 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
642 panic("ffs_write+IO_EXT");
645 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
649 if (uio->uio_rw != UIO_WRITE)
650 panic("ffs_write: mode");
653 switch (vp->v_type) {
655 if (ioflag & IO_APPEND)
656 uio->uio_offset = ip->i_size;
657 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
663 panic("ffs_write: dir write");
666 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
667 (int)uio->uio_offset,
672 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
673 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
675 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
678 * Maybe this should be above the vnode op call, but so long as
679 * file servers have no limits, I don't think it matters.
682 if (vp->v_type == VREG && td != NULL) {
683 PROC_LOCK(td->td_proc);
684 if (uio->uio_offset + uio->uio_resid >
685 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
686 psignal(td->td_proc, SIGXFSZ);
687 PROC_UNLOCK(td->td_proc);
690 PROC_UNLOCK(td->td_proc);
693 resid = uio->uio_resid;
695 if (seqcount > BA_SEQMAX)
696 flags = BA_SEQMAX << BA_SEQSHIFT;
698 flags = seqcount << BA_SEQSHIFT;
699 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
702 for (error = 0; uio->uio_resid > 0;) {
703 lbn = lblkno(fs, uio->uio_offset);
704 blkoffset = blkoff(fs, uio->uio_offset);
705 xfersize = fs->fs_bsize - blkoffset;
706 if (uio->uio_resid < xfersize)
707 xfersize = uio->uio_resid;
708 if (uio->uio_offset + xfersize > ip->i_size)
709 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
712 * We must perform a read-before-write if the transfer size
713 * does not cover the entire buffer.
715 if (fs->fs_bsize > xfersize)
719 /* XXX is uio->uio_offset the right thing here? */
720 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
721 ap->a_cred, flags, &bp);
725 * If the buffer is not valid we have to clear out any
726 * garbage data from the pages instantiated for the buffer.
727 * If we do not, a failed uiomove() during a write can leave
728 * the prior contents of the pages exposed to a userland
729 * mmap(). XXX deal with uiomove() errors a better way.
731 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
733 if (ioflag & IO_DIRECT)
734 bp->b_flags |= B_DIRECT;
735 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
736 bp->b_flags |= B_NOCACHE;
738 if (uio->uio_offset + xfersize > ip->i_size) {
739 ip->i_size = uio->uio_offset + xfersize;
740 DIP_SET(ip, i_size, ip->i_size);
743 size = blksize(fs, ip, lbn) - bp->b_resid;
748 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
749 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
750 (LIST_EMPTY(&bp->b_dep))) {
751 bp->b_flags |= B_RELBUF;
755 * If IO_SYNC each buffer is written synchronously. Otherwise
756 * if we have a severe page deficiency write the buffer
757 * asynchronously. Otherwise try to cluster, and if that
758 * doesn't do it then either do an async write (if O_DIRECT),
759 * or a delayed write (if not).
761 if (ioflag & IO_SYNC) {
763 } else if (vm_page_count_severe() ||
764 buf_dirty_count_severe() ||
765 (ioflag & IO_ASYNC)) {
766 bp->b_flags |= B_CLUSTEROK;
768 } else if (xfersize + blkoffset == fs->fs_bsize) {
769 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
770 bp->b_flags |= B_CLUSTEROK;
771 cluster_write(vp, bp, ip->i_size, seqcount);
775 } else if (ioflag & IO_DIRECT) {
776 bp->b_flags |= B_CLUSTEROK;
779 bp->b_flags |= B_CLUSTEROK;
782 if (error || xfersize == 0)
784 ip->i_flag |= IN_CHANGE | IN_UPDATE;
787 * If we successfully wrote any data, and we are not the superuser
788 * we clear the setuid and setgid bits as a precaution against
791 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
793 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0)) {
794 ip->i_mode &= ~(ISUID | ISGID);
795 DIP_SET(ip, i_mode, ip->i_mode);
799 if (ioflag & IO_UNIT) {
800 (void)ffs_truncate(vp, osize,
801 IO_NORMAL | (ioflag & IO_SYNC),
802 ap->a_cred, uio->uio_td);
803 uio->uio_offset -= resid - uio->uio_resid;
804 uio->uio_resid = resid;
806 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
807 error = ffs_update(vp, 1);
816 struct vop_getpages_args *ap;
822 pcount = round_page(ap->a_count) / PAGE_SIZE;
823 mreq = ap->a_m[ap->a_reqpage];
826 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
827 * then the entire page is valid. Since the page may be mapped,
828 * user programs might reference data beyond the actual end of file
829 * occuring within the page. We have to zero that data.
831 VM_OBJECT_LOCK(mreq->object);
833 if (mreq->valid != VM_PAGE_BITS_ALL)
834 vm_page_zero_invalid(mreq, TRUE);
835 vm_page_lock_queues();
836 for (i = 0; i < pcount; i++) {
837 if (i != ap->a_reqpage) {
838 vm_page_free(ap->a_m[i]);
841 vm_page_unlock_queues();
842 VM_OBJECT_UNLOCK(mreq->object);
845 VM_OBJECT_UNLOCK(mreq->object);
847 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
854 * Extended attribute area reading.
857 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
860 struct ufs2_dinode *dp;
863 ufs_lbn_t lbn, nextlbn;
865 long size, xfersize, blkoffset;
866 int error, orig_resid;
873 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
874 panic("ffs_extread: mode");
877 orig_resid = uio->uio_resid;
878 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
881 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
883 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
884 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
886 lbn = lblkno(fs, uio->uio_offset);
890 * size of buffer. The buffer representing the
891 * end of the file is rounded up to the size of
892 * the block type ( fragment or full block,
895 size = sblksize(fs, dp->di_extsize, lbn);
896 blkoffset = blkoff(fs, uio->uio_offset);
899 * The amount we want to transfer in this iteration is
900 * one FS block less the amount of the data before
901 * our startpoint (duh!)
903 xfersize = fs->fs_bsize - blkoffset;
906 * But if we actually want less than the block,
907 * or the file doesn't have a whole block more of data,
908 * then use the lesser number.
910 if (uio->uio_resid < xfersize)
911 xfersize = uio->uio_resid;
912 if (bytesinfile < xfersize)
913 xfersize = bytesinfile;
915 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
917 * Don't do readahead if this is the end of the info.
919 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
922 * If we have a second block, then
923 * fire off a request for a readahead
924 * as well as a read. Note that the 4th and 5th
925 * arguments point to arrays of the size specified in
928 int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
930 nextlbn = -1 - nextlbn;
931 error = breadn(vp, -1 - lbn,
932 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
941 * If IO_DIRECT then set B_DIRECT for the buffer. This
942 * will cause us to attempt to release the buffer later on
943 * and will cause the buffer cache to attempt to free the
946 if (ioflag & IO_DIRECT)
947 bp->b_flags |= B_DIRECT;
950 * We should only get non-zero b_resid when an I/O error
951 * has occurred, which should cause us to break above.
952 * However, if the short read did not cause an error,
953 * then we want to ensure that we do not uiomove bad
954 * or uninitialized data.
957 if (size < xfersize) {
963 error = uiomove((char *)bp->b_data + blkoffset,
968 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
969 (LIST_EMPTY(&bp->b_dep))) {
971 * If there are no dependencies, and it's VMIO,
972 * then we don't need the buf, mark it available
973 * for freeing. The VM has the data.
975 bp->b_flags |= B_RELBUF;
979 * Otherwise let whoever
980 * made the request take care of
981 * freeing it. We just queue
982 * it onto another list.
989 * This can only happen in the case of an error
990 * because the loop above resets bp to NULL on each iteration
991 * and on normal completion has not set a new value into it.
992 * so it must have come from a 'break' statement
995 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
996 (LIST_EMPTY(&bp->b_dep))) {
997 bp->b_flags |= B_RELBUF;
1004 if ((error == 0 || uio->uio_resid != orig_resid) &&
1005 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0 &&
1006 (ip->i_flag & IN_ACCESS) == 0) {
1008 ip->i_flag |= IN_ACCESS;
1015 * Extended attribute area writing.
1018 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1021 struct ufs2_dinode *dp;
1026 int blkoffset, error, flags, resid, size, xfersize;
1032 KASSERT(!(ip->i_flag & IN_SPACECOUNTED), ("inode %u: inode is dead",
1036 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1037 panic("ffs_extwrite: mode");
1040 if (ioflag & IO_APPEND)
1041 uio->uio_offset = dp->di_extsize;
1042 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1043 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1044 if ((uoff_t)uio->uio_offset + uio->uio_resid > NXADDR * fs->fs_bsize)
1047 resid = uio->uio_resid;
1048 osize = dp->di_extsize;
1050 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1053 for (error = 0; uio->uio_resid > 0;) {
1054 lbn = lblkno(fs, uio->uio_offset);
1055 blkoffset = blkoff(fs, uio->uio_offset);
1056 xfersize = fs->fs_bsize - blkoffset;
1057 if (uio->uio_resid < xfersize)
1058 xfersize = uio->uio_resid;
1061 * We must perform a read-before-write if the transfer size
1062 * does not cover the entire buffer.
1064 if (fs->fs_bsize > xfersize)
1067 flags &= ~BA_CLRBUF;
1068 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1073 * If the buffer is not valid we have to clear out any
1074 * garbage data from the pages instantiated for the buffer.
1075 * If we do not, a failed uiomove() during a write can leave
1076 * the prior contents of the pages exposed to a userland
1077 * mmap(). XXX deal with uiomove() errors a better way.
1079 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1081 if (ioflag & IO_DIRECT)
1082 bp->b_flags |= B_DIRECT;
1084 if (uio->uio_offset + xfersize > dp->di_extsize)
1085 dp->di_extsize = uio->uio_offset + xfersize;
1087 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1088 if (size < xfersize)
1092 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1093 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1094 (LIST_EMPTY(&bp->b_dep))) {
1095 bp->b_flags |= B_RELBUF;
1099 * If IO_SYNC each buffer is written synchronously. Otherwise
1100 * if we have a severe page deficiency write the buffer
1101 * asynchronously. Otherwise try to cluster, and if that
1102 * doesn't do it then either do an async write (if O_DIRECT),
1103 * or a delayed write (if not).
1105 if (ioflag & IO_SYNC) {
1107 } else if (vm_page_count_severe() ||
1108 buf_dirty_count_severe() ||
1109 xfersize + blkoffset == fs->fs_bsize ||
1110 (ioflag & (IO_ASYNC | IO_DIRECT)))
1114 if (error || xfersize == 0)
1116 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1119 * If we successfully wrote any data, and we are not the superuser
1120 * we clear the setuid and setgid bits as a precaution against
1123 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1124 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID, 0)) {
1125 ip->i_mode &= ~(ISUID | ISGID);
1126 dp->di_mode = ip->i_mode;
1130 if (ioflag & IO_UNIT) {
1131 (void)ffs_truncate(vp, osize,
1132 IO_EXT | (ioflag&IO_SYNC), ucred, uio->uio_td);
1133 uio->uio_offset -= resid - uio->uio_resid;
1134 uio->uio_resid = resid;
1136 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1137 error = ffs_update(vp, 1);
1143 * Vnode operating to retrieve a named extended attribute.
1145 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1146 * the length of the EA, and possibly the pointer to the entry and to the data.
1149 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name, u_char **eap, u_char **eac)
1151 u_char *p, *pe, *pn, *p0;
1152 int eapad1, eapad2, ealength, ealen, nlen;
1156 nlen = strlen(name);
1158 for (p = ptr; p < pe; p = pn) {
1160 bcopy(p, &ul, sizeof(ul));
1162 /* make sure this entry is complete */
1165 p += sizeof(uint32_t);
1173 if (bcmp(p, name, nlen))
1175 ealength = sizeof(uint32_t) + 3 + nlen;
1176 eapad1 = 8 - (ealength % 8);
1180 ealen = ul - ealength - eapad2;
1192 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td, int extra)
1195 struct ufs2_dinode *dp;
1198 struct iovec liovec;
1205 easize = dp->di_extsize;
1206 if ((uoff_t)easize + extra > NXADDR * fs->fs_bsize)
1209 eae = malloc(easize + extra, M_TEMP, M_WAITOK);
1211 liovec.iov_base = eae;
1212 liovec.iov_len = easize;
1213 luio.uio_iov = &liovec;
1214 luio.uio_iovcnt = 1;
1215 luio.uio_offset = 0;
1216 luio.uio_resid = easize;
1217 luio.uio_segflg = UIO_SYSSPACE;
1218 luio.uio_rw = UIO_READ;
1221 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1231 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1234 struct ufs2_dinode *dp;
1239 if (ip->i_ea_area != NULL)
1242 error = ffs_rdextattr(&ip->i_ea_area, vp, td, 0);
1245 ip->i_ea_len = dp->di_extsize;
1251 * Vnode extattr transaction commit/abort
1254 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1258 struct iovec liovec;
1260 struct ufs2_dinode *dp;
1263 if (ip->i_ea_area == NULL)
1266 error = ip->i_ea_error;
1267 if (commit && error == 0) {
1269 cred = vp->v_mount->mnt_cred;
1270 liovec.iov_base = ip->i_ea_area;
1271 liovec.iov_len = ip->i_ea_len;
1272 luio.uio_iov = &liovec;
1273 luio.uio_iovcnt = 1;
1274 luio.uio_offset = 0;
1275 luio.uio_resid = ip->i_ea_len;
1276 luio.uio_segflg = UIO_SYSSPACE;
1277 luio.uio_rw = UIO_WRITE;
1279 /* XXX: I'm not happy about truncating to zero size */
1280 if (ip->i_ea_len < dp->di_extsize)
1281 error = ffs_truncate(vp, 0, IO_EXT, cred, td);
1282 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1284 free(ip->i_ea_area, M_TEMP);
1285 ip->i_ea_area = NULL;
1292 * Vnode extattr strategy routine for fifos.
1294 * We need to check for a read or write of the external attributes.
1295 * Otherwise we just fall through and do the usual thing.
1298 ffsext_strategy(struct vop_strategy_args *ap)
1300 struct vop_strategy_args {
1301 struct vnodeop_desc *a_desc;
1311 lbn = ap->a_bp->b_lblkno;
1312 if (VTOI(vp)->i_fs->fs_magic == FS_UFS2_MAGIC &&
1313 lbn < 0 && lbn >= -NXADDR)
1314 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1315 if (vp->v_type == VFIFO)
1316 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1317 panic("spec nodes went here");
1321 * Vnode extattr transaction commit/abort
1324 ffs_openextattr(struct vop_openextattr_args *ap)
1326 struct vop_openextattr_args {
1327 struct vnodeop_desc *a_desc;
1329 IN struct ucred *a_cred;
1330 IN struct thread *a_td;
1337 ip = VTOI(ap->a_vp);
1340 if (ap->a_vp->v_type == VCHR)
1341 return (EOPNOTSUPP);
1343 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1348 * Vnode extattr transaction commit/abort
1351 ffs_closeextattr(struct vop_closeextattr_args *ap)
1353 struct vop_closeextattr_args {
1354 struct vnodeop_desc *a_desc;
1357 IN struct ucred *a_cred;
1358 IN struct thread *a_td;
1365 ip = VTOI(ap->a_vp);
1368 if (ap->a_vp->v_type == VCHR)
1369 return (EOPNOTSUPP);
1371 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1374 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1378 * Vnode operation to remove a named attribute.
1381 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1384 IN struct vnode *a_vp;
1385 IN int a_attrnamespace;
1386 IN const char *a_name;
1387 IN struct ucred *a_cred;
1388 IN struct thread *a_td;
1394 uint32_t ealength, ul;
1395 int ealen, olen, eapad1, eapad2, error, i, easize;
1399 ip = VTOI(ap->a_vp);
1402 if (ap->a_vp->v_type == VCHR)
1403 return (EOPNOTSUPP);
1405 if (strlen(ap->a_name) == 0)
1408 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1411 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1412 ap->a_cred, ap->a_td, VWRITE);
1414 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1415 ip->i_ea_error = error;
1419 if (ip->i_ea_area == NULL) {
1420 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1428 ealength = eapad1 = ealen = eapad2 = 0;
1430 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1431 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1432 easize = ip->i_ea_len;
1434 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1437 /* delete but nonexistent */
1440 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1443 bcopy(p, &ul, sizeof ul);
1445 if (ul != ealength) {
1446 bcopy(p + ul, p + ealength, easize - i);
1447 easize += (ealength - ul);
1449 if (easize > NXADDR * fs->fs_bsize) {
1452 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1453 else if (ip->i_ea_error == 0)
1454 ip->i_ea_error = ENOSPC;
1458 ip->i_ea_area = eae;
1459 ip->i_ea_len = easize;
1462 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1467 * Vnode operation to retrieve a named extended attribute.
1470 ffs_getextattr(struct vop_getextattr_args *ap)
1473 IN struct vnode *a_vp;
1474 IN int a_attrnamespace;
1475 IN const char *a_name;
1476 INOUT struct uio *a_uio;
1478 IN struct ucred *a_cred;
1479 IN struct thread *a_td;
1487 int error, ealen, stand_alone;
1489 ip = VTOI(ap->a_vp);
1492 if (ap->a_vp->v_type == VCHR)
1493 return (EOPNOTSUPP);
1495 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1496 ap->a_cred, ap->a_td, VREAD);
1500 if (ip->i_ea_area == NULL) {
1501 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1508 eae = ip->i_ea_area;
1509 easize = ip->i_ea_len;
1511 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1515 if (ap->a_size != NULL)
1516 *ap->a_size = ealen;
1517 else if (ap->a_uio != NULL)
1518 error = uiomove(p, ealen, ap->a_uio);
1522 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1527 * Vnode operation to retrieve extended attributes on a vnode.
1530 ffs_listextattr(struct vop_listextattr_args *ap)
1533 IN struct vnode *a_vp;
1534 IN int a_attrnamespace;
1535 INOUT struct uio *a_uio;
1537 IN struct ucred *a_cred;
1538 IN struct thread *a_td;
1544 u_char *eae, *p, *pe, *pn;
1547 int error, ealen, stand_alone;
1549 ip = VTOI(ap->a_vp);
1552 if (ap->a_vp->v_type == VCHR)
1553 return (EOPNOTSUPP);
1555 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1556 ap->a_cred, ap->a_td, VREAD);
1560 if (ip->i_ea_area == NULL) {
1561 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1568 eae = ip->i_ea_area;
1569 easize = ip->i_ea_len;
1572 if (ap->a_size != NULL)
1575 for(p = eae; error == 0 && p < pe; p = pn) {
1576 bcopy(p, &ul, sizeof(ul));
1581 if (*p++ != ap->a_attrnamespace)
1585 if (ap->a_size != NULL) {
1586 *ap->a_size += ealen + 1;
1587 } else if (ap->a_uio != NULL) {
1588 error = uiomove(p, ealen + 1, ap->a_uio);
1592 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1597 * Vnode operation to set a named attribute.
1600 ffs_setextattr(struct vop_setextattr_args *ap)
1603 IN struct vnode *a_vp;
1604 IN int a_attrnamespace;
1605 IN const char *a_name;
1606 INOUT struct uio *a_uio;
1607 IN struct ucred *a_cred;
1608 IN struct thread *a_td;
1614 uint32_t ealength, ul;
1615 int ealen, olen, eapad1, eapad2, error, i, easize;
1619 ip = VTOI(ap->a_vp);
1622 if (ap->a_vp->v_type == VCHR)
1623 return (EOPNOTSUPP);
1625 if (strlen(ap->a_name) == 0)
1628 /* XXX Now unsupported API to delete EAs using NULL uio. */
1629 if (ap->a_uio == NULL)
1630 return (EOPNOTSUPP);
1632 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1635 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1636 ap->a_cred, ap->a_td, VWRITE);
1638 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1639 ip->i_ea_error = error;
1643 if (ip->i_ea_area == NULL) {
1644 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1652 ealen = ap->a_uio->uio_resid;
1653 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1654 eapad1 = 8 - (ealength % 8);
1657 eapad2 = 8 - (ealen % 8);
1660 ealength += eapad1 + ealen + eapad2;
1662 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1663 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1664 easize = ip->i_ea_len;
1666 olen = ffs_findextattr(eae, easize,
1667 ap->a_attrnamespace, ap->a_name, &p, NULL);
1669 /* new, append at end */
1673 bcopy(p, &ul, sizeof ul);
1675 if (ul != ealength) {
1676 bcopy(p + ul, p + ealength, easize - i);
1677 easize += (ealength - ul);
1680 if (easize > NXADDR * fs->fs_bsize) {
1683 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1684 else if (ip->i_ea_error == 0)
1685 ip->i_ea_error = ENOSPC;
1688 bcopy(&ealength, p, sizeof(ealength));
1689 p += sizeof(ealength);
1690 *p++ = ap->a_attrnamespace;
1692 *p++ = strlen(ap->a_name);
1693 strcpy(p, ap->a_name);
1694 p += strlen(ap->a_name);
1697 error = uiomove(p, ealen, ap->a_uio);
1701 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1702 else if (ip->i_ea_error == 0)
1703 ip->i_ea_error = error;
1710 ip->i_ea_area = eae;
1711 ip->i_ea_len = easize;
1714 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1719 * Vnode pointer to File handle
1722 ffs_vptofh(struct vop_vptofh_args *ap)
1725 IN struct vnode *a_vp;
1726 IN struct fid *a_fhp;
1733 ip = VTOI(ap->a_vp);
1734 ufhp = (struct ufid *)ap->a_fhp;
1735 ufhp->ufid_len = sizeof(struct ufid);
1736 ufhp->ufid_ino = ip->i_number;
1737 ufhp->ufid_gen = ip->i_gen;