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);
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_EMPTY(&bp->b_dep) &&
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_lock1_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;
372 #ifdef DEBUG_VFS_LOCKS
373 KASSERT(vp->v_holdcnt != 0,
374 ("ffs_lock %p: zero hold count", vp));
377 result = _lockmgr(lkp, flags, VI_MTX(vp), ap->a_td, ap->a_file, ap->a_line);
378 if (lkp == vp->v_vnlock || result != 0)
381 * Apparent success, except that the vnode
382 * mutated between snapshot file vnode and
383 * regular file vnode while this process
384 * slept. The lock currently held is not the
385 * right lock. Release it, and try to get the
388 (void) _lockmgr(lkp, LK_RELEASE, VI_MTX(vp), ap->a_td, ap->a_file, ap->a_line);
389 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
390 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
391 flags &= ~LK_INTERLOCK;
395 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
399 return (VOP_LOCK1_APV(&ufs_vnodeops, ap));
404 * Vnode op for reading.
409 struct vop_read_args /* {
413 struct ucred *a_cred;
421 ufs_lbn_t lbn, nextlbn;
423 long size, xfersize, blkoffset;
424 int error, orig_resid;
430 ioflag = ap->a_ioflag;
431 if (ap->a_ioflag & IO_EXT)
433 return (ffs_extread(vp, uio, ioflag));
435 panic("ffs_read+IO_EXT");
438 if ((ioflag & IO_DIRECT) != 0) {
441 error = ffs_rawread(vp, uio, &workdone);
442 if (error != 0 || workdone != 0)
447 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
451 if (uio->uio_rw != UIO_READ)
452 panic("ffs_read: mode");
454 if (vp->v_type == VLNK) {
455 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
456 panic("ffs_read: short symlink");
457 } else if (vp->v_type != VREG && vp->v_type != VDIR)
458 panic("ffs_read: type %d", vp->v_type);
460 orig_resid = uio->uio_resid;
461 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
464 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
466 if (uio->uio_offset < ip->i_size &&
467 uio->uio_offset >= fs->fs_maxfilesize)
470 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
471 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
473 lbn = lblkno(fs, uio->uio_offset);
477 * size of buffer. The buffer representing the
478 * end of the file is rounded up to the size of
479 * the block type ( fragment or full block,
482 size = blksize(fs, ip, lbn);
483 blkoffset = blkoff(fs, uio->uio_offset);
486 * The amount we want to transfer in this iteration is
487 * one FS block less the amount of the data before
488 * our startpoint (duh!)
490 xfersize = fs->fs_bsize - blkoffset;
493 * But if we actually want less than the block,
494 * or the file doesn't have a whole block more of data,
495 * then use the lesser number.
497 if (uio->uio_resid < xfersize)
498 xfersize = uio->uio_resid;
499 if (bytesinfile < xfersize)
500 xfersize = bytesinfile;
502 if (lblktosize(fs, nextlbn) >= ip->i_size) {
504 * Don't do readahead if this is the end of the file.
506 error = bread(vp, lbn, size, NOCRED, &bp);
507 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
509 * Otherwise if we are allowed to cluster,
510 * grab as much as we can.
512 * XXX This may not be a win if we are not
513 * doing sequential access.
515 error = cluster_read(vp, ip->i_size, lbn,
516 size, NOCRED, blkoffset + uio->uio_resid, seqcount, &bp);
517 } else if (seqcount > 1) {
519 * If we are NOT allowed to cluster, then
520 * if we appear to be acting sequentially,
521 * fire off a request for a readahead
522 * as well as a read. Note that the 4th and 5th
523 * arguments point to arrays of the size specified in
526 int nextsize = blksize(fs, ip, nextlbn);
527 error = breadn(vp, lbn,
528 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
531 * Failing all of the above, just read what the
532 * user asked for. Interestingly, the same as
533 * the first option above.
535 error = bread(vp, lbn, size, NOCRED, &bp);
544 * If IO_DIRECT then set B_DIRECT for the buffer. This
545 * will cause us to attempt to release the buffer later on
546 * and will cause the buffer cache to attempt to free the
549 if (ioflag & IO_DIRECT)
550 bp->b_flags |= B_DIRECT;
553 * We should only get non-zero b_resid when an I/O error
554 * has occurred, which should cause us to break above.
555 * However, if the short read did not cause an error,
556 * then we want to ensure that we do not uiomove bad
557 * or uninitialized data.
560 if (size < xfersize) {
566 error = uiomove((char *)bp->b_data + blkoffset,
571 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
572 (LIST_EMPTY(&bp->b_dep))) {
574 * If there are no dependencies, and it's VMIO,
575 * then we don't need the buf, mark it available
576 * for freeing. The VM has the data.
578 bp->b_flags |= B_RELBUF;
582 * Otherwise let whoever
583 * made the request take care of
584 * freeing it. We just queue
585 * it onto another list.
592 * This can only happen in the case of an error
593 * because the loop above resets bp to NULL on each iteration
594 * and on normal completion has not set a new value into it.
595 * so it must have come from a 'break' statement
598 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
599 (LIST_EMPTY(&bp->b_dep))) {
600 bp->b_flags |= B_RELBUF;
607 if ((error == 0 || uio->uio_resid != orig_resid) &&
608 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
610 ip->i_flag |= IN_ACCESS;
617 * Vnode op for writing.
621 struct vop_write_args /* {
625 struct ucred *a_cred;
637 int blkoffset, error, flags, ioflag, resid, size, xfersize;
641 ioflag = ap->a_ioflag;
642 if (ap->a_ioflag & IO_EXT)
644 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
646 panic("ffs_write+IO_EXT");
649 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
653 if (uio->uio_rw != UIO_WRITE)
654 panic("ffs_write: mode");
657 switch (vp->v_type) {
659 if (ioflag & IO_APPEND)
660 uio->uio_offset = ip->i_size;
661 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
667 panic("ffs_write: dir write");
670 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
671 (int)uio->uio_offset,
676 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
677 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
679 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
682 * Maybe this should be above the vnode op call, but so long as
683 * file servers have no limits, I don't think it matters.
686 if (vp->v_type == VREG && td != NULL) {
687 PROC_LOCK(td->td_proc);
688 if (uio->uio_offset + uio->uio_resid >
689 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
690 psignal(td->td_proc, SIGXFSZ);
691 PROC_UNLOCK(td->td_proc);
694 PROC_UNLOCK(td->td_proc);
697 resid = uio->uio_resid;
699 if (seqcount > BA_SEQMAX)
700 flags = BA_SEQMAX << BA_SEQSHIFT;
702 flags = seqcount << BA_SEQSHIFT;
703 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
706 for (error = 0; uio->uio_resid > 0;) {
707 lbn = lblkno(fs, uio->uio_offset);
708 blkoffset = blkoff(fs, uio->uio_offset);
709 xfersize = fs->fs_bsize - blkoffset;
710 if (uio->uio_resid < xfersize)
711 xfersize = uio->uio_resid;
712 if (uio->uio_offset + xfersize > ip->i_size)
713 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
716 * We must perform a read-before-write if the transfer size
717 * does not cover the entire buffer.
719 if (fs->fs_bsize > xfersize)
723 /* XXX is uio->uio_offset the right thing here? */
724 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
725 ap->a_cred, flags, &bp);
727 vnode_pager_setsize(vp, ip->i_size);
731 * If the buffer is not valid we have to clear out any
732 * garbage data from the pages instantiated for the buffer.
733 * If we do not, a failed uiomove() during a write can leave
734 * the prior contents of the pages exposed to a userland
735 * mmap(). XXX deal with uiomove() errors a better way.
737 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
739 if (ioflag & IO_DIRECT)
740 bp->b_flags |= B_DIRECT;
741 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
742 bp->b_flags |= B_NOCACHE;
744 if (uio->uio_offset + xfersize > ip->i_size) {
745 ip->i_size = uio->uio_offset + xfersize;
746 DIP_SET(ip, i_size, ip->i_size);
749 size = blksize(fs, ip, lbn) - bp->b_resid;
754 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
755 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
756 (LIST_EMPTY(&bp->b_dep))) {
757 bp->b_flags |= B_RELBUF;
761 * If IO_SYNC each buffer is written synchronously. Otherwise
762 * if we have a severe page deficiency write the buffer
763 * asynchronously. Otherwise try to cluster, and if that
764 * doesn't do it then either do an async write (if O_DIRECT),
765 * or a delayed write (if not).
767 if (ioflag & IO_SYNC) {
769 } else if (vm_page_count_severe() ||
770 buf_dirty_count_severe() ||
771 (ioflag & IO_ASYNC)) {
772 bp->b_flags |= B_CLUSTEROK;
774 } else if (xfersize + blkoffset == fs->fs_bsize) {
775 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
776 bp->b_flags |= B_CLUSTEROK;
777 cluster_write(vp, bp, ip->i_size, seqcount);
781 } else if (ioflag & IO_DIRECT) {
782 bp->b_flags |= B_CLUSTEROK;
785 bp->b_flags |= B_CLUSTEROK;
788 if (error || xfersize == 0)
790 ip->i_flag |= IN_CHANGE | IN_UPDATE;
793 * If we successfully wrote any data, and we are not the superuser
794 * we clear the setuid and setgid bits as a precaution against
797 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
799 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0)) {
800 ip->i_mode &= ~(ISUID | ISGID);
801 DIP_SET(ip, i_mode, ip->i_mode);
805 if (ioflag & IO_UNIT) {
806 (void)ffs_truncate(vp, osize,
807 IO_NORMAL | (ioflag & IO_SYNC),
808 ap->a_cred, uio->uio_td);
809 uio->uio_offset -= resid - uio->uio_resid;
810 uio->uio_resid = resid;
812 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
813 error = ffs_update(vp, 1);
822 struct vop_getpages_args *ap;
828 pcount = round_page(ap->a_count) / PAGE_SIZE;
829 mreq = ap->a_m[ap->a_reqpage];
832 * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
833 * then the entire page is valid. Since the page may be mapped,
834 * user programs might reference data beyond the actual end of file
835 * occuring within the page. We have to zero that data.
837 VM_OBJECT_LOCK(mreq->object);
839 if (mreq->valid != VM_PAGE_BITS_ALL)
840 vm_page_zero_invalid(mreq, TRUE);
841 vm_page_lock_queues();
842 for (i = 0; i < pcount; i++) {
843 if (i != ap->a_reqpage) {
844 vm_page_free(ap->a_m[i]);
847 vm_page_unlock_queues();
848 VM_OBJECT_UNLOCK(mreq->object);
851 VM_OBJECT_UNLOCK(mreq->object);
853 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
860 * Extended attribute area reading.
863 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
866 struct ufs2_dinode *dp;
869 ufs_lbn_t lbn, nextlbn;
871 long size, xfersize, blkoffset;
872 int error, orig_resid;
879 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
880 panic("ffs_extread: mode");
883 orig_resid = uio->uio_resid;
884 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
887 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
889 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
890 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
892 lbn = lblkno(fs, uio->uio_offset);
896 * size of buffer. The buffer representing the
897 * end of the file is rounded up to the size of
898 * the block type ( fragment or full block,
901 size = sblksize(fs, dp->di_extsize, lbn);
902 blkoffset = blkoff(fs, uio->uio_offset);
905 * The amount we want to transfer in this iteration is
906 * one FS block less the amount of the data before
907 * our startpoint (duh!)
909 xfersize = fs->fs_bsize - blkoffset;
912 * But if we actually want less than the block,
913 * or the file doesn't have a whole block more of data,
914 * then use the lesser number.
916 if (uio->uio_resid < xfersize)
917 xfersize = uio->uio_resid;
918 if (bytesinfile < xfersize)
919 xfersize = bytesinfile;
921 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
923 * Don't do readahead if this is the end of the info.
925 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
928 * If we have a second block, then
929 * fire off a request for a readahead
930 * as well as a read. Note that the 4th and 5th
931 * arguments point to arrays of the size specified in
934 int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
936 nextlbn = -1 - nextlbn;
937 error = breadn(vp, -1 - lbn,
938 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
947 * If IO_DIRECT then set B_DIRECT for the buffer. This
948 * will cause us to attempt to release the buffer later on
949 * and will cause the buffer cache to attempt to free the
952 if (ioflag & IO_DIRECT)
953 bp->b_flags |= B_DIRECT;
956 * We should only get non-zero b_resid when an I/O error
957 * has occurred, which should cause us to break above.
958 * However, if the short read did not cause an error,
959 * then we want to ensure that we do not uiomove bad
960 * or uninitialized data.
963 if (size < xfersize) {
969 error = uiomove((char *)bp->b_data + blkoffset,
974 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
975 (LIST_EMPTY(&bp->b_dep))) {
977 * If there are no dependencies, and it's VMIO,
978 * then we don't need the buf, mark it available
979 * for freeing. The VM has the data.
981 bp->b_flags |= B_RELBUF;
985 * Otherwise let whoever
986 * made the request take care of
987 * freeing it. We just queue
988 * it onto another list.
995 * This can only happen in the case of an error
996 * because the loop above resets bp to NULL on each iteration
997 * and on normal completion has not set a new value into it.
998 * so it must have come from a 'break' statement
1001 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1002 (LIST_EMPTY(&bp->b_dep))) {
1003 bp->b_flags |= B_RELBUF;
1013 * Extended attribute area writing.
1016 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1019 struct ufs2_dinode *dp;
1024 int blkoffset, error, flags, resid, size, xfersize;
1030 KASSERT(!(ip->i_flag & IN_SPACECOUNTED), ("inode %u: inode is dead",
1034 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1035 panic("ffs_extwrite: mode");
1038 if (ioflag & IO_APPEND)
1039 uio->uio_offset = dp->di_extsize;
1040 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1041 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1042 if ((uoff_t)uio->uio_offset + uio->uio_resid > NXADDR * fs->fs_bsize)
1045 resid = uio->uio_resid;
1046 osize = dp->di_extsize;
1048 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
1051 for (error = 0; uio->uio_resid > 0;) {
1052 lbn = lblkno(fs, uio->uio_offset);
1053 blkoffset = blkoff(fs, uio->uio_offset);
1054 xfersize = fs->fs_bsize - blkoffset;
1055 if (uio->uio_resid < xfersize)
1056 xfersize = uio->uio_resid;
1059 * We must perform a read-before-write if the transfer size
1060 * does not cover the entire buffer.
1062 if (fs->fs_bsize > xfersize)
1065 flags &= ~BA_CLRBUF;
1066 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1071 * If the buffer is not valid we have to clear out any
1072 * garbage data from the pages instantiated for the buffer.
1073 * If we do not, a failed uiomove() during a write can leave
1074 * the prior contents of the pages exposed to a userland
1075 * mmap(). XXX deal with uiomove() errors a better way.
1077 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1079 if (ioflag & IO_DIRECT)
1080 bp->b_flags |= B_DIRECT;
1082 if (uio->uio_offset + xfersize > dp->di_extsize)
1083 dp->di_extsize = uio->uio_offset + xfersize;
1085 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1086 if (size < xfersize)
1090 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1091 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
1092 (LIST_EMPTY(&bp->b_dep))) {
1093 bp->b_flags |= B_RELBUF;
1097 * If IO_SYNC each buffer is written synchronously. Otherwise
1098 * if we have a severe page deficiency write the buffer
1099 * asynchronously. Otherwise try to cluster, and if that
1100 * doesn't do it then either do an async write (if O_DIRECT),
1101 * or a delayed write (if not).
1103 if (ioflag & IO_SYNC) {
1105 } else if (vm_page_count_severe() ||
1106 buf_dirty_count_severe() ||
1107 xfersize + blkoffset == fs->fs_bsize ||
1108 (ioflag & (IO_ASYNC | IO_DIRECT)))
1112 if (error || xfersize == 0)
1114 ip->i_flag |= IN_CHANGE;
1117 * If we successfully wrote any data, and we are not the superuser
1118 * we clear the setuid and setgid bits as a precaution against
1121 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1122 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID, 0)) {
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;
1196 struct iovec liovec;
1203 easize = dp->di_extsize;
1204 if ((uoff_t)easize + extra > NXADDR * fs->fs_bsize)
1207 eae = malloc(easize + extra, M_TEMP, M_WAITOK);
1209 liovec.iov_base = eae;
1210 liovec.iov_len = easize;
1211 luio.uio_iov = &liovec;
1212 luio.uio_iovcnt = 1;
1213 luio.uio_offset = 0;
1214 luio.uio_resid = easize;
1215 luio.uio_segflg = UIO_SYSSPACE;
1216 luio.uio_rw = UIO_READ;
1219 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1229 ffs_lock_ea(struct vnode *vp)
1235 while (ip->i_flag & IN_EA_LOCKED) {
1236 ip->i_flag |= IN_EA_LOCKWAIT;
1237 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1240 ip->i_flag |= IN_EA_LOCKED;
1245 ffs_unlock_ea(struct vnode *vp)
1251 if (ip->i_flag & IN_EA_LOCKWAIT)
1252 wakeup(&ip->i_ea_refs);
1253 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1258 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1261 struct ufs2_dinode *dp;
1267 if (ip->i_ea_area != NULL) {
1273 error = ffs_rdextattr(&ip->i_ea_area, vp, td, 0);
1278 ip->i_ea_len = dp->di_extsize;
1286 * Vnode extattr transaction commit/abort
1289 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1293 struct iovec liovec;
1295 struct ufs2_dinode *dp;
1300 if (ip->i_ea_area == NULL) {
1305 error = ip->i_ea_error;
1306 if (commit && error == 0) {
1307 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1309 cred = vp->v_mount->mnt_cred;
1310 liovec.iov_base = ip->i_ea_area;
1311 liovec.iov_len = ip->i_ea_len;
1312 luio.uio_iov = &liovec;
1313 luio.uio_iovcnt = 1;
1314 luio.uio_offset = 0;
1315 luio.uio_resid = ip->i_ea_len;
1316 luio.uio_segflg = UIO_SYSSPACE;
1317 luio.uio_rw = UIO_WRITE;
1319 /* XXX: I'm not happy about truncating to zero size */
1320 if (ip->i_ea_len < dp->di_extsize)
1321 error = ffs_truncate(vp, 0, IO_EXT, cred, td);
1322 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1324 if (--ip->i_ea_refs == 0) {
1325 free(ip->i_ea_area, M_TEMP);
1326 ip->i_ea_area = NULL;
1335 * Vnode extattr strategy routine for fifos.
1337 * We need to check for a read or write of the external attributes.
1338 * Otherwise we just fall through and do the usual thing.
1341 ffsext_strategy(struct vop_strategy_args *ap)
1343 struct vop_strategy_args {
1344 struct vnodeop_desc *a_desc;
1354 lbn = ap->a_bp->b_lblkno;
1355 if (VTOI(vp)->i_fs->fs_magic == FS_UFS2_MAGIC &&
1356 lbn < 0 && lbn >= -NXADDR)
1357 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1358 if (vp->v_type == VFIFO)
1359 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1360 panic("spec nodes went here");
1364 * Vnode extattr transaction commit/abort
1367 ffs_openextattr(struct vop_openextattr_args *ap)
1369 struct vop_openextattr_args {
1370 struct vnodeop_desc *a_desc;
1372 IN struct ucred *a_cred;
1373 IN struct thread *a_td;
1380 ip = VTOI(ap->a_vp);
1383 if (ap->a_vp->v_type == VCHR)
1384 return (EOPNOTSUPP);
1386 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1391 * Vnode extattr transaction commit/abort
1394 ffs_closeextattr(struct vop_closeextattr_args *ap)
1396 struct vop_closeextattr_args {
1397 struct vnodeop_desc *a_desc;
1400 IN struct ucred *a_cred;
1401 IN struct thread *a_td;
1408 ip = VTOI(ap->a_vp);
1411 if (ap->a_vp->v_type == VCHR)
1412 return (EOPNOTSUPP);
1414 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
1417 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred, ap->a_td));
1421 * Vnode operation to remove a named attribute.
1424 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1427 IN struct vnode *a_vp;
1428 IN int a_attrnamespace;
1429 IN const char *a_name;
1430 IN struct ucred *a_cred;
1431 IN struct thread *a_td;
1437 uint32_t ealength, ul;
1438 int ealen, olen, eapad1, eapad2, error, i, easize;
1441 ip = VTOI(ap->a_vp);
1444 if (ap->a_vp->v_type == VCHR)
1445 return (EOPNOTSUPP);
1447 if (strlen(ap->a_name) == 0)
1450 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1453 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1454 ap->a_cred, ap->a_td, IWRITE);
1458 * ffs_lock_ea is not needed there, because the vnode
1459 * must be exclusively locked.
1461 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1462 ip->i_ea_error = error;
1466 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1470 ealength = eapad1 = ealen = eapad2 = 0;
1472 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1473 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1474 easize = ip->i_ea_len;
1476 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1479 /* delete but nonexistent */
1481 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1484 bcopy(p, &ul, sizeof ul);
1486 if (ul != ealength) {
1487 bcopy(p + ul, p + ealength, easize - i);
1488 easize += (ealength - ul);
1490 if (easize > NXADDR * fs->fs_bsize) {
1492 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1493 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1494 ip->i_ea_error = ENOSPC;
1498 ip->i_ea_area = eae;
1499 ip->i_ea_len = easize;
1501 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1506 * Vnode operation to retrieve a named extended attribute.
1509 ffs_getextattr(struct vop_getextattr_args *ap)
1512 IN struct vnode *a_vp;
1513 IN int a_attrnamespace;
1514 IN const char *a_name;
1515 INOUT struct uio *a_uio;
1517 IN struct ucred *a_cred;
1518 IN struct thread *a_td;
1528 ip = VTOI(ap->a_vp);
1531 if (ap->a_vp->v_type == VCHR)
1532 return (EOPNOTSUPP);
1534 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1535 ap->a_cred, ap->a_td, IREAD);
1539 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1543 eae = ip->i_ea_area;
1544 easize = ip->i_ea_len;
1546 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1550 if (ap->a_size != NULL)
1551 *ap->a_size = ealen;
1552 else if (ap->a_uio != NULL)
1553 error = uiomove(p, ealen, ap->a_uio);
1557 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1562 * Vnode operation to retrieve extended attributes on a vnode.
1565 ffs_listextattr(struct vop_listextattr_args *ap)
1568 IN struct vnode *a_vp;
1569 IN int a_attrnamespace;
1570 INOUT struct uio *a_uio;
1572 IN struct ucred *a_cred;
1573 IN struct thread *a_td;
1579 u_char *eae, *p, *pe, *pn;
1584 ip = VTOI(ap->a_vp);
1587 if (ap->a_vp->v_type == VCHR)
1588 return (EOPNOTSUPP);
1590 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1591 ap->a_cred, ap->a_td, IREAD);
1595 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1598 eae = ip->i_ea_area;
1599 easize = ip->i_ea_len;
1602 if (ap->a_size != NULL)
1605 for(p = eae; error == 0 && p < pe; p = pn) {
1606 bcopy(p, &ul, sizeof(ul));
1611 if (*p++ != ap->a_attrnamespace)
1615 if (ap->a_size != NULL) {
1616 *ap->a_size += ealen + 1;
1617 } else if (ap->a_uio != NULL) {
1618 error = uiomove(p, ealen + 1, ap->a_uio);
1621 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1626 * Vnode operation to set a named attribute.
1629 ffs_setextattr(struct vop_setextattr_args *ap)
1632 IN struct vnode *a_vp;
1633 IN int a_attrnamespace;
1634 IN const char *a_name;
1635 INOUT struct uio *a_uio;
1636 IN struct ucred *a_cred;
1637 IN struct thread *a_td;
1643 uint32_t ealength, ul;
1644 int ealen, olen, eapad1, eapad2, error, i, easize;
1647 ip = VTOI(ap->a_vp);
1650 if (ap->a_vp->v_type == VCHR)
1651 return (EOPNOTSUPP);
1653 if (strlen(ap->a_name) == 0)
1656 /* XXX Now unsupported API to delete EAs using NULL uio. */
1657 if (ap->a_uio == NULL)
1658 return (EOPNOTSUPP);
1660 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1663 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1664 ap->a_cred, ap->a_td, IWRITE);
1668 * ffs_lock_ea is not needed there, because the vnode
1669 * must be exclusively locked.
1671 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1672 ip->i_ea_error = error;
1676 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1680 ealen = ap->a_uio->uio_resid;
1681 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1682 eapad1 = 8 - (ealength % 8);
1685 eapad2 = 8 - (ealen % 8);
1688 ealength += eapad1 + ealen + eapad2;
1690 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1691 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1692 easize = ip->i_ea_len;
1694 olen = ffs_findextattr(eae, easize,
1695 ap->a_attrnamespace, ap->a_name, &p, NULL);
1697 /* new, append at end */
1701 bcopy(p, &ul, sizeof ul);
1703 if (ul != ealength) {
1704 bcopy(p + ul, p + ealength, easize - i);
1705 easize += (ealength - ul);
1708 if (easize > NXADDR * fs->fs_bsize) {
1710 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1711 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1712 ip->i_ea_error = ENOSPC;
1715 bcopy(&ealength, p, sizeof(ealength));
1716 p += sizeof(ealength);
1717 *p++ = ap->a_attrnamespace;
1719 *p++ = strlen(ap->a_name);
1720 strcpy(p, ap->a_name);
1721 p += strlen(ap->a_name);
1724 error = uiomove(p, ealen, ap->a_uio);
1727 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1728 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1729 ip->i_ea_error = error;
1736 ip->i_ea_area = eae;
1737 ip->i_ea_len = easize;
1739 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred, ap->a_td);
1744 * Vnode pointer to File handle
1747 ffs_vptofh(struct vop_vptofh_args *ap)
1750 IN struct vnode *a_vp;
1751 IN struct fid *a_fhp;
1758 ip = VTOI(ap->a_vp);
1759 ufhp = (struct ufid *)ap->a_fhp;
1760 ufhp->ufid_len = sizeof(struct ufid);
1761 ufhp->ufid_ino = ip->i_number;
1762 ufhp->ufid_gen = ip->i_gen;