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_lock_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_lock = 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_lock = 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_lock = 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);
200 int s, error, wait, passes, skipmeta;
203 wait = (waitfor == MNT_WAIT);
204 lbn = lblkno(ip->i_fs, (ip->i_size + ip->i_fs->fs_bsize - 1));
207 * Flush all dirty buffers associated with a vnode.
216 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs)
217 bp->b_vflags &= ~BV_SCANNED;
218 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
220 * Reasons to skip this buffer: it has already been considered
221 * on this pass, this pass is the first time through on a
222 * synchronous flush request and the buffer being considered
223 * is metadata, the buffer has dependencies that will cause
224 * it to be redirtied and it has not already been deferred,
225 * or it is already being written.
227 if ((bp->b_vflags & BV_SCANNED) != 0)
229 bp->b_vflags |= BV_SCANNED;
230 if ((skipmeta == 1 && bp->b_lblkno < 0))
232 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
235 if (!wait && LIST_FIRST(&bp->b_dep) != NULL &&
236 (bp->b_flags & B_DEFERRED) == 0 &&
237 buf_countdeps(bp, 0)) {
238 bp->b_flags |= B_DEFERRED;
243 if ((bp->b_flags & B_DELWRI) == 0)
244 panic("ffs_fsync: not dirty");
246 * If this is a synchronous flush request, or it is not a
247 * file or device, start the write on this buffer immediatly.
249 if (wait || (vp->v_type != VREG && vp->v_type != VBLK)) {
252 * On our final pass through, do all I/O synchronously
253 * so that we can find out if our flush is failing
254 * because of write errors.
256 if (passes > 0 || !wait) {
257 if ((bp->b_flags & B_CLUSTEROK) && !wait) {
258 (void) vfs_bio_awrite(bp);
268 if ((error = bwrite(bp)) != 0)
272 } else if ((vp->v_type == VREG) && (bp->b_lblkno >= lbn)) {
274 * If the buffer is for data that has been truncated
275 * off the file, then throw it away.
278 bp->b_flags |= B_INVAL | B_NOCACHE;
286 * Since we may have slept during the I/O, we need
287 * to start from a known point.
290 nbp = TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd);
293 * If we were asked to do this synchronously, then go back for
294 * another pass, this time doing the metadata.
302 bufobj_wwait(&vp->v_bufobj, 3, 0);
306 * Ensure that any filesystem metatdata associated
307 * with the vnode has been written.
310 if ((error = softdep_sync_metadata(vp)) != 0)
315 if (vp->v_bufobj.bo_dirty.bv_cnt > 0) {
317 * Block devices associated with filesystems may
318 * have new I/O requests posted for them even if
319 * the vnode is locked, so no amount of trying will
320 * get them clean. Thus we give block devices a
321 * good effort, then just give up. For all other file
322 * types, go around and try again until it is clean.
329 if (!vn_isdisk(vp, NULL))
330 vprint("ffs_fsync: dirty", vp);
336 return (ffs_update(vp, wait));
341 struct _vop_lock_args /* {
349 #ifndef NO_FFS_SNAPSHOT
355 switch (ap->a_flags & LK_TYPE_MASK) {
363 * vnode interlock must be held to ensure that
364 * the possibly external lock isn't freed,
365 * e.g. when mutating from snapshot file vnode
366 * to regular file vnode.
368 if ((flags & LK_INTERLOCK) == 0) {
370 flags |= LK_INTERLOCK;
373 result = _lockmgr(lkp, flags, VI_MTX(vp), ap->a_td, ap->a_file, ap->a_line);
374 if (lkp == vp->v_vnlock || result != 0)
377 * Apparent success, except that the vnode
378 * mutated between snapshot file vnode and
379 * regular file vnode while this process
380 * slept. The lock currently held is not the
381 * right lock. Release it, and try to get the
384 (void) _lockmgr(lkp, LK_RELEASE, VI_MTX(vp), ap->a_td, ap->a_file, ap->a_line);
385 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
386 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
387 flags &= ~LK_INTERLOCK;
391 result = _VOP_LOCK_APV(&ufs_vnodeops, ap);
395 return (_VOP_LOCK_APV(&ufs_vnodeops, ap));
400 * Vnode op for reading.
405 struct vop_read_args /* {
409 struct ucred *a_cred;
417 ufs_lbn_t lbn, nextlbn;
419 long size, xfersize, blkoffset;
420 int error, orig_resid;
426 ioflag = ap->a_ioflag;
427 if (ap->a_ioflag & IO_EXT)
429 return (ffs_extread(vp, uio, ioflag));
431 panic("ffs_read+IO_EXT");
434 if ((ioflag & IO_DIRECT) != 0) {
437 error = ffs_rawread(vp, uio, &workdone);
438 if (error != 0 || workdone != 0)
443 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
447 if (uio->uio_rw != UIO_READ)
448 panic("ffs_read: mode");
450 if (vp->v_type == VLNK) {
451 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
452 panic("ffs_read: short symlink");
453 } else if (vp->v_type != VREG && vp->v_type != VDIR)
454 panic("ffs_read: type %d", vp->v_type);
456 orig_resid = uio->uio_resid;
457 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
460 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
462 if (uio->uio_offset < ip->i_size &&
463 uio->uio_offset >= fs->fs_maxfilesize)
466 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
467 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
469 lbn = lblkno(fs, uio->uio_offset);
473 * size of buffer. The buffer representing the
474 * end of the file is rounded up to the size of
475 * the block type ( fragment or full block,
478 size = blksize(fs, ip, lbn);
479 blkoffset = blkoff(fs, uio->uio_offset);
482 * The amount we want to transfer in this iteration is
483 * one FS block less the amount of the data before
484 * our startpoint (duh!)
486 xfersize = fs->fs_bsize - blkoffset;
489 * But if we actually want less than the block,
490 * or the file doesn't have a whole block more of data,
491 * then use the lesser number.
493 if (uio->uio_resid < xfersize)
494 xfersize = uio->uio_resid;
495 if (bytesinfile < xfersize)
496 xfersize = bytesinfile;
498 if (lblktosize(fs, nextlbn) >= ip->i_size) {
500 * Don't do readahead if this is the end of the file.
502 error = bread(vp, lbn, size, NOCRED, &bp);
503 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
505 * Otherwise if we are allowed to cluster,
506 * grab as much as we can.
508 * XXX This may not be a win if we are not
509 * doing sequential access.
511 error = cluster_read(vp, ip->i_size, lbn,
512 size, NOCRED, blkoffset + uio->uio_resid, seqcount, &bp);
513 } else if (seqcount > 1) {
515 * If we are NOT allowed to cluster, then
516 * if we appear to be acting sequentially,
517 * fire off a request for a readahead
518 * as well as a read. Note that the 4th and 5th
519 * arguments point to arrays of the size specified in
522 int nextsize = blksize(fs, ip, nextlbn);
523 error = breadn(vp, lbn,
524 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
527 * Failing all of the above, just read what the
528 * user asked for. Interestingly, the same as
529 * the first option above.
531 error = bread(vp, lbn, size, NOCRED, &bp);
540 * If IO_DIRECT then set B_DIRECT for the buffer. This
541 * will cause us to attempt to release the buffer later on
542 * and will cause the buffer cache to attempt to free the
545 if (ioflag & IO_DIRECT)
546 bp->b_flags |= B_DIRECT;
549 * We should only get non-zero b_resid when an I/O error
550 * has occurred, which should cause us to break above.
551 * However, if the short read did not cause an error,
552 * then we want to ensure that we do not uiomove bad
553 * or uninitialized data.
556 if (size < xfersize) {
562 error = uiomove((char *)bp->b_data + blkoffset,
567 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
568 (LIST_FIRST(&bp->b_dep) == NULL)) {
570 * If there are no dependencies, and it's VMIO,
571 * then we don't need the buf, mark it available
572 * for freeing. The VM has the data.
574 bp->b_flags |= B_RELBUF;
578 * Otherwise let whoever
579 * made the request take care of
580 * freeing it. We just queue
581 * it onto another list.
588 * This can only happen in the case of an error
589 * because the loop above resets bp to NULL on each iteration
590 * and on normal completion has not set a new value into it.
591 * so it must have come from a 'break' statement
594 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
595 (LIST_FIRST(&bp->b_dep) == NULL)) {
596 bp->b_flags |= B_RELBUF;
603 if ((error == 0 || uio->uio_resid != orig_resid) &&
604 (vp->v_mount->mnt_flag & MNT_NOATIME) == 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_FIRST(&bp->b_dep) == NULL)) {
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,
795 ip->i_mode &= ~(ISUID | ISGID);
796 DIP_SET(ip, i_mode, ip->i_mode);
800 if (ioflag & IO_UNIT) {
801 (void)ffs_truncate(vp, osize,
802 IO_NORMAL | (ioflag & IO_SYNC),
803 ap->a_cred, uio->uio_td);
804 uio->uio_offset -= resid - uio->uio_resid;
805 uio->uio_resid = resid;
807 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
808 error = ffs_update(vp, 1);
817 struct vop_getpages_args *ap;
823 pcount = round_page(ap->a_count) / PAGE_SIZE;
824 mreq = ap->a_m[ap->a_reqpage];
827 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
828 * then the entire page is valid. Since the page may be mapped,
829 * user programs might reference data beyond the actual end of file
830 * occuring within the page. We have to zero that data.
832 VM_OBJECT_LOCK(mreq->object);
834 if (mreq->valid != VM_PAGE_BITS_ALL)
835 vm_page_zero_invalid(mreq, TRUE);
836 vm_page_lock_queues();
837 for (i = 0; i < pcount; i++) {
838 if (i != ap->a_reqpage) {
839 vm_page_free(ap->a_m[i]);
842 vm_page_unlock_queues();
843 VM_OBJECT_UNLOCK(mreq->object);
846 VM_OBJECT_UNLOCK(mreq->object);
848 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
855 * Extended attribute area reading.
858 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
861 struct ufs2_dinode *dp;
864 ufs_lbn_t lbn, nextlbn;
866 long size, xfersize, blkoffset;
867 int error, orig_resid;
874 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
875 panic("ffs_extread: mode");
878 orig_resid = uio->uio_resid;
879 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
882 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
884 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
885 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
887 lbn = lblkno(fs, uio->uio_offset);
891 * size of buffer. The buffer representing the
892 * end of the file is rounded up to the size of
893 * the block type ( fragment or full block,
896 size = sblksize(fs, dp->di_extsize, lbn);
897 blkoffset = blkoff(fs, uio->uio_offset);
900 * The amount we want to transfer in this iteration is
901 * one FS block less the amount of the data before
902 * our startpoint (duh!)
904 xfersize = fs->fs_bsize - blkoffset;
907 * But if we actually want less than the block,
908 * or the file doesn't have a whole block more of data,
909 * then use the lesser number.
911 if (uio->uio_resid < xfersize)
912 xfersize = uio->uio_resid;
913 if (bytesinfile < xfersize)
914 xfersize = bytesinfile;
916 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
918 * Don't do readahead if this is the end of the info.
920 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
923 * If we have a second block, then
924 * fire off a request for a readahead
925 * as well as a read. Note that the 4th and 5th
926 * arguments point to arrays of the size specified in
929 int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
931 nextlbn = -1 - nextlbn;
932 error = breadn(vp, -1 - lbn,
933 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
942 * If IO_DIRECT then set B_DIRECT for the buffer. This
943 * will cause us to attempt to release the buffer later on
944 * and will cause the buffer cache to attempt to free the
947 if (ioflag & IO_DIRECT)
948 bp->b_flags |= B_DIRECT;
951 * We should only get non-zero b_resid when an I/O error
952 * has occurred, which should cause us to break above.
953 * However, if the short read did not cause an error,
954 * then we want to ensure that we do not uiomove bad
955 * or uninitialized data.
958 if (size < xfersize) {
964 error = uiomove((char *)bp->b_data + blkoffset,
969 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
970 (LIST_FIRST(&bp->b_dep) == NULL)) {
972 * If there are no dependencies, and it's VMIO,
973 * then we don't need the buf, mark it available
974 * for freeing. The VM has the data.
976 bp->b_flags |= B_RELBUF;
980 * Otherwise let whoever
981 * made the request take care of
982 * freeing it. We just queue
983 * it onto another list.
990 * This can only happen in the case of an error
991 * because the loop above resets bp to NULL on each iteration
992 * and on normal completion has not set a new value into it.
993 * so it must have come from a 'break' statement
996 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
997 (LIST_FIRST(&bp->b_dep) == NULL)) {
998 bp->b_flags |= B_RELBUF;
1005 if ((error == 0 || uio->uio_resid != orig_resid) &&
1006 (vp->v_mount->mnt_flag & MNT_NOATIME) == 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;
1033 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1034 panic("ffs_extwrite: mode");
1037 if (ioflag & IO_APPEND)
1038 uio->uio_offset = dp->di_extsize;
1039 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1040 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1041 if ((uoff_t)uio->uio_offset + uio->uio_resid > NXADDR * fs->fs_bsize)
1044 resid = uio->uio_resid;
1045 osize = dp->di_extsize;
1047 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1050 for (error = 0; uio->uio_resid > 0;) {
1051 lbn = lblkno(fs, uio->uio_offset);
1052 blkoffset = blkoff(fs, uio->uio_offset);
1053 xfersize = fs->fs_bsize - blkoffset;
1054 if (uio->uio_resid < xfersize)
1055 xfersize = uio->uio_resid;
1058 * We must perform a read-before-write if the transfer size
1059 * does not cover the entire buffer.
1061 if (fs->fs_bsize > xfersize)
1064 flags &= ~BA_CLRBUF;
1065 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1070 * If the buffer is not valid we have to clear out any
1071 * garbage data from the pages instantiated for the buffer.
1072 * If we do not, a failed uiomove() during a write can leave
1073 * the prior contents of the pages exposed to a userland
1074 * mmap(). XXX deal with uiomove() errors a better way.
1076 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1078 if (ioflag & IO_DIRECT)
1079 bp->b_flags |= B_DIRECT;
1081 if (uio->uio_offset + xfersize > dp->di_extsize)
1082 dp->di_extsize = uio->uio_offset + xfersize;
1084 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1085 if (size < xfersize)
1089 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1090 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1091 (LIST_FIRST(&bp->b_dep) == NULL)) {
1092 bp->b_flags |= B_RELBUF;
1096 * If IO_SYNC each buffer is written synchronously. Otherwise
1097 * if we have a severe page deficiency write the buffer
1098 * asynchronously. Otherwise try to cluster, and if that
1099 * doesn't do it then either do an async write (if O_DIRECT),
1100 * or a delayed write (if not).
1102 if (ioflag & IO_SYNC) {
1104 } else if (vm_page_count_severe() ||
1105 buf_dirty_count_severe() ||
1106 xfersize + blkoffset == fs->fs_bsize ||
1107 (ioflag & (IO_ASYNC | IO_DIRECT)))
1111 if (error || xfersize == 0)
1113 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1116 * If we successfully wrote any data, and we are not the superuser
1117 * we clear the setuid and setgid bits as a precaution against
1120 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1121 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID,
1123 ip->i_mode &= ~(ISUID | ISGID);
1124 dp->di_mode = ip->i_mode;
1128 if (ioflag & IO_UNIT) {
1129 (void)ffs_truncate(vp, osize,
1130 IO_EXT | (ioflag&IO_SYNC), ucred, uio->uio_td);
1131 uio->uio_offset -= resid - uio->uio_resid;
1132 uio->uio_resid = resid;
1134 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1135 error = ffs_update(vp, 1);
1141 * Vnode operating to retrieve a named extended attribute.
1143 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1144 * the length of the EA, and possibly the pointer to the entry and to the data.
1147 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name, u_char **eap, u_char **eac)
1149 u_char *p, *pe, *pn, *p0;
1150 int eapad1, eapad2, ealength, ealen, nlen;
1154 nlen = strlen(name);
1156 for (p = ptr; p < pe; p = pn) {
1158 bcopy(p, &ul, sizeof(ul));
1160 /* make sure this entry is complete */
1163 p += sizeof(uint32_t);
1171 if (bcmp(p, name, nlen))
1173 ealength = sizeof(uint32_t) + 3 + nlen;
1174 eapad1 = 8 - (ealength % 8);
1178 ealen = ul - ealength - eapad2;
1190 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td, int extra)
1193 struct ufs2_dinode *dp;
1195 struct iovec liovec;
1201 easize = dp->di_extsize;
1203 eae = malloc(easize + extra, M_TEMP, M_WAITOK);
1205 liovec.iov_base = eae;
1206 liovec.iov_len = easize;
1207 luio.uio_iov = &liovec;
1208 luio.uio_iovcnt = 1;
1209 luio.uio_offset = 0;
1210 luio.uio_resid = easize;
1211 luio.uio_segflg = UIO_SYSSPACE;
1212 luio.uio_rw = UIO_READ;
1215 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1225 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1228 struct ufs2_dinode *dp;
1233 if (ip->i_ea_area != NULL)
1236 error = ffs_rdextattr(&ip->i_ea_area, vp, td, 0);
1239 ip->i_ea_len = dp->di_extsize;
1245 * Vnode extattr transaction commit/abort
1248 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1252 struct iovec liovec;
1254 struct ufs2_dinode *dp;
1257 if (ip->i_ea_area == NULL)
1260 error = ip->i_ea_error;
1261 if (commit && error == 0) {
1263 cred = vp->v_mount->mnt_cred;
1264 liovec.iov_base = ip->i_ea_area;
1265 liovec.iov_len = ip->i_ea_len;
1266 luio.uio_iov = &liovec;
1267 luio.uio_iovcnt = 1;
1268 luio.uio_offset = 0;
1269 luio.uio_resid = ip->i_ea_len;
1270 luio.uio_segflg = UIO_SYSSPACE;
1271 luio.uio_rw = UIO_WRITE;
1273 /* XXX: I'm not happy about truncating to zero size */
1274 if (ip->i_ea_len < dp->di_extsize)
1275 error = ffs_truncate(vp, 0, IO_EXT, cred, td);
1276 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1278 free(ip->i_ea_area, M_TEMP);
1279 ip->i_ea_area = NULL;
1286 * Vnode extattr strategy routine for fifos.
1288 * We need to check for a read or write of the external attributes.
1289 * Otherwise we just fall through and do the usual thing.
1292 ffsext_strategy(struct vop_strategy_args *ap)
1294 struct vop_strategy_args {
1295 struct vnodeop_desc *a_desc;
1305 lbn = ap->a_bp->b_lblkno;
1306 if (VTOI(vp)->i_fs->fs_magic == FS_UFS2_MAGIC &&
1307 lbn < 0 && lbn >= -NXADDR)
1308 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1309 if (vp->v_type == VFIFO)
1310 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1311 panic("spec nodes went here");
1315 * Vnode extattr transaction commit/abort
1318 ffs_openextattr(struct vop_openextattr_args *ap)
1320 struct vop_openextattr_args {
1321 struct vnodeop_desc *a_desc;
1323 IN struct ucred *a_cred;
1324 IN struct thread *a_td;
1331 ip = VTOI(ap->a_vp);
1334 if (ap->a_vp->v_type == VCHR)
1335 return (EOPNOTSUPP);
1337 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1342 * Vnode extattr transaction commit/abort
1345 ffs_closeextattr(struct vop_closeextattr_args *ap)
1347 struct vop_closeextattr_args {
1348 struct vnodeop_desc *a_desc;
1351 IN struct ucred *a_cred;
1352 IN struct thread *a_td;
1359 ip = VTOI(ap->a_vp);
1362 if (ap->a_vp->v_type == VCHR)
1363 return (EOPNOTSUPP);
1365 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1368 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1372 * Vnode operation to remove a named attribute.
1375 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1378 IN struct vnode *a_vp;
1379 IN int a_attrnamespace;
1380 IN const char *a_name;
1381 IN struct ucred *a_cred;
1382 IN struct thread *a_td;
1388 uint32_t ealength, ul;
1389 int ealen, olen, eapad1, eapad2, error, i, easize;
1393 ip = VTOI(ap->a_vp);
1396 if (ap->a_vp->v_type == VCHR)
1397 return (EOPNOTSUPP);
1399 if (strlen(ap->a_name) == 0)
1402 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1405 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1406 ap->a_cred, ap->a_td, IWRITE);
1408 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1409 ip->i_ea_error = error;
1413 if (ip->i_ea_area == NULL) {
1414 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1422 ealength = eapad1 = ealen = eapad2 = 0;
1424 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1425 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1426 easize = ip->i_ea_len;
1428 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1431 /* delete but nonexistent */
1434 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1437 bcopy(p, &ul, sizeof ul);
1439 if (ul != ealength) {
1440 bcopy(p + ul, p + ealength, easize - i);
1441 easize += (ealength - ul);
1443 if (easize > NXADDR * fs->fs_bsize) {
1446 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1447 else if (ip->i_ea_error == 0)
1448 ip->i_ea_error = ENOSPC;
1452 ip->i_ea_area = eae;
1453 ip->i_ea_len = easize;
1456 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1461 * Vnode operation to retrieve a named extended attribute.
1464 ffs_getextattr(struct vop_getextattr_args *ap)
1467 IN struct vnode *a_vp;
1468 IN int a_attrnamespace;
1469 IN const char *a_name;
1470 INOUT struct uio *a_uio;
1472 IN struct ucred *a_cred;
1473 IN struct thread *a_td;
1481 int error, ealen, stand_alone;
1483 ip = VTOI(ap->a_vp);
1486 if (ap->a_vp->v_type == VCHR)
1487 return (EOPNOTSUPP);
1489 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1490 ap->a_cred, ap->a_td, IREAD);
1494 if (ip->i_ea_area == NULL) {
1495 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1502 eae = ip->i_ea_area;
1503 easize = ip->i_ea_len;
1505 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1509 if (ap->a_size != NULL)
1510 *ap->a_size = ealen;
1511 else if (ap->a_uio != NULL)
1512 error = uiomove(p, ealen, ap->a_uio);
1516 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1521 * Vnode operation to retrieve extended attributes on a vnode.
1524 ffs_listextattr(struct vop_listextattr_args *ap)
1527 IN struct vnode *a_vp;
1528 IN int a_attrnamespace;
1529 INOUT struct uio *a_uio;
1531 IN struct ucred *a_cred;
1532 IN struct thread *a_td;
1538 u_char *eae, *p, *pe, *pn;
1541 int error, ealen, stand_alone;
1543 ip = VTOI(ap->a_vp);
1546 if (ap->a_vp->v_type == VCHR)
1547 return (EOPNOTSUPP);
1549 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1550 ap->a_cred, ap->a_td, IREAD);
1554 if (ip->i_ea_area == NULL) {
1555 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1562 eae = ip->i_ea_area;
1563 easize = ip->i_ea_len;
1566 if (ap->a_size != NULL)
1569 for(p = eae; error == 0 && p < pe; p = pn) {
1570 bcopy(p, &ul, sizeof(ul));
1575 if (*p++ != ap->a_attrnamespace)
1579 if (ap->a_size != NULL) {
1580 *ap->a_size += ealen + 1;
1581 } else if (ap->a_uio != NULL) {
1582 error = uiomove(p, ealen + 1, ap->a_uio);
1586 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1591 * Vnode operation to set a named attribute.
1594 ffs_setextattr(struct vop_setextattr_args *ap)
1597 IN struct vnode *a_vp;
1598 IN int a_attrnamespace;
1599 IN const char *a_name;
1600 INOUT struct uio *a_uio;
1601 IN struct ucred *a_cred;
1602 IN struct thread *a_td;
1608 uint32_t ealength, ul;
1609 int ealen, olen, eapad1, eapad2, error, i, easize;
1613 ip = VTOI(ap->a_vp);
1616 if (ap->a_vp->v_type == VCHR)
1617 return (EOPNOTSUPP);
1619 if (strlen(ap->a_name) == 0)
1622 /* XXX Now unsupported API to delete EAs using NULL uio. */
1623 if (ap->a_uio == NULL)
1624 return (EOPNOTSUPP);
1626 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1629 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1630 ap->a_cred, ap->a_td, IWRITE);
1632 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1633 ip->i_ea_error = error;
1637 if (ip->i_ea_area == NULL) {
1638 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1646 ealen = ap->a_uio->uio_resid;
1647 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1648 eapad1 = 8 - (ealength % 8);
1651 eapad2 = 8 - (ealen % 8);
1654 ealength += eapad1 + ealen + eapad2;
1656 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1657 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1658 easize = ip->i_ea_len;
1660 olen = ffs_findextattr(eae, easize,
1661 ap->a_attrnamespace, ap->a_name, &p, NULL);
1663 /* new, append at end */
1667 bcopy(p, &ul, sizeof ul);
1669 if (ul != ealength) {
1670 bcopy(p + ul, p + ealength, easize - i);
1671 easize += (ealength - ul);
1674 if (easize > NXADDR * fs->fs_bsize) {
1677 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1678 else if (ip->i_ea_error == 0)
1679 ip->i_ea_error = ENOSPC;
1682 bcopy(&ealength, p, sizeof(ealength));
1683 p += sizeof(ealength);
1684 *p++ = ap->a_attrnamespace;
1686 *p++ = strlen(ap->a_name);
1687 strcpy(p, ap->a_name);
1688 p += strlen(ap->a_name);
1691 error = uiomove(p, ealen, ap->a_uio);
1695 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1696 else if (ip->i_ea_error == 0)
1697 ip->i_ea_error = error;
1704 ip->i_ea_area = eae;
1705 ip->i_ea_len = easize;
1708 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1713 * Vnode pointer to File handle
1716 ffs_vptofh(struct vop_vptofh_args *ap)
1719 IN struct vnode *a_vp;
1720 IN struct fid *a_fhp;
1727 ip = VTOI(ap->a_vp);
1728 ufhp = (struct ufid *)ap->a_fhp;
1729 ufhp->ufid_len = sizeof(struct ufid);
1730 ufhp->ufid_ino = ip->i_number;
1731 ufhp->ufid_gen = ip->i_gen;