1*0a6a1f1dSLionel Sambuc /* $NetBSD: ulfs_readwrite.c,v 1.19 2015/07/24 06:59:32 dholland Exp $ */
284d9c625SLionel Sambuc /* from NetBSD: ufs_readwrite.c,v 1.105 2013/01/22 09:39:18 dholland Exp */
384d9c625SLionel Sambuc
484d9c625SLionel Sambuc /*-
584d9c625SLionel Sambuc * Copyright (c) 1993
684d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
984d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1084d9c625SLionel Sambuc * are met:
1184d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1284d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1384d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1484d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1584d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
1684d9c625SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
1784d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
1884d9c625SLionel Sambuc * without specific prior written permission.
1984d9c625SLionel Sambuc *
2084d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2184d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2284d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2384d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2484d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2584d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2684d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2784d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2884d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2984d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3084d9c625SLionel Sambuc * SUCH DAMAGE.
3184d9c625SLionel Sambuc *
3284d9c625SLionel Sambuc * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
3384d9c625SLionel Sambuc */
3484d9c625SLionel Sambuc
3584d9c625SLionel Sambuc #include <sys/cdefs.h>
36*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.19 2015/07/24 06:59:32 dholland Exp $");
3784d9c625SLionel Sambuc
3884d9c625SLionel Sambuc #ifdef LFS_READWRITE
3984d9c625SLionel Sambuc #define FS struct lfs
4084d9c625SLionel Sambuc #define I_FS i_lfs
4184d9c625SLionel Sambuc #define READ lfs_read
4284d9c625SLionel Sambuc #define READ_S "lfs_read"
4384d9c625SLionel Sambuc #define WRITE lfs_write
4484d9c625SLionel Sambuc #define WRITE_S "lfs_write"
45*0a6a1f1dSLionel Sambuc #define BUFRD lfs_bufrd
46*0a6a1f1dSLionel Sambuc #define BUFWR lfs_bufwr
47*0a6a1f1dSLionel Sambuc #define fs_sb_getbsize(fs) lfs_sb_getbsize(fs)
4884d9c625SLionel Sambuc #define fs_bmask lfs_bmask
4984d9c625SLionel Sambuc #else
5084d9c625SLionel Sambuc #define FS struct fs
5184d9c625SLionel Sambuc #define I_FS i_fs
5284d9c625SLionel Sambuc #define READ ffs_read
5384d9c625SLionel Sambuc #define READ_S "ffs_read"
5484d9c625SLionel Sambuc #define WRITE ffs_write
5584d9c625SLionel Sambuc #define WRITE_S "ffs_write"
56*0a6a1f1dSLionel Sambuc #define BUFRD ffs_bufrd
57*0a6a1f1dSLionel Sambuc #define BUFWR ffs_bufwr
58*0a6a1f1dSLionel Sambuc #define fs_sb_getbsize(fs) (fs)->fs_bsize
5984d9c625SLionel Sambuc #endif
6084d9c625SLionel Sambuc
61*0a6a1f1dSLionel Sambuc static int ulfs_post_read_update(struct vnode *, int, int);
62*0a6a1f1dSLionel Sambuc static int ulfs_post_write_update(struct vnode *, struct uio *, int,
63*0a6a1f1dSLionel Sambuc kauth_cred_t, off_t, int, int, int);
64*0a6a1f1dSLionel Sambuc
6584d9c625SLionel Sambuc /*
6684d9c625SLionel Sambuc * Vnode op for reading.
6784d9c625SLionel Sambuc */
6884d9c625SLionel Sambuc /* ARGSUSED */
6984d9c625SLionel Sambuc int
READ(void * v)7084d9c625SLionel Sambuc READ(void *v)
7184d9c625SLionel Sambuc {
7284d9c625SLionel Sambuc struct vop_read_args /* {
7384d9c625SLionel Sambuc struct vnode *a_vp;
7484d9c625SLionel Sambuc struct uio *a_uio;
7584d9c625SLionel Sambuc int a_ioflag;
7684d9c625SLionel Sambuc kauth_cred_t a_cred;
7784d9c625SLionel Sambuc } */ *ap = v;
7884d9c625SLionel Sambuc struct vnode *vp;
7984d9c625SLionel Sambuc struct inode *ip;
8084d9c625SLionel Sambuc struct uio *uio;
8184d9c625SLionel Sambuc FS *fs;
8284d9c625SLionel Sambuc vsize_t bytelen;
83*0a6a1f1dSLionel Sambuc int error, ioflag, advice;
8484d9c625SLionel Sambuc
8584d9c625SLionel Sambuc vp = ap->a_vp;
8684d9c625SLionel Sambuc ip = VTOI(vp);
8784d9c625SLionel Sambuc fs = ip->I_FS;
8884d9c625SLionel Sambuc uio = ap->a_uio;
8984d9c625SLionel Sambuc ioflag = ap->a_ioflag;
9084d9c625SLionel Sambuc error = 0;
9184d9c625SLionel Sambuc
92*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_READ);
93*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
9484d9c625SLionel Sambuc
95*0a6a1f1dSLionel Sambuc /* XXX Eliminate me by refusing directory reads from userland. */
96*0a6a1f1dSLionel Sambuc if (vp->v_type == VDIR)
97*0a6a1f1dSLionel Sambuc return BUFRD(vp, uio, ioflag, ap->a_cred);
98*0a6a1f1dSLionel Sambuc #ifdef LFS_READWRITE
99*0a6a1f1dSLionel Sambuc /* XXX Eliminate me by using ufs_bufio in lfs. */
100*0a6a1f1dSLionel Sambuc if (vp->v_type == VREG && ip->i_number == LFS_IFILE_INUM)
101*0a6a1f1dSLionel Sambuc return BUFRD(vp, uio, ioflag, ap->a_cred);
10284d9c625SLionel Sambuc #endif
10384d9c625SLionel Sambuc if ((u_int64_t)uio->uio_offset > fs->um_maxfilesize)
10484d9c625SLionel Sambuc return (EFBIG);
10584d9c625SLionel Sambuc if (uio->uio_resid == 0)
10684d9c625SLionel Sambuc return (0);
10784d9c625SLionel Sambuc
10884d9c625SLionel Sambuc #ifndef LFS_READWRITE
10984d9c625SLionel Sambuc if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT)
11084d9c625SLionel Sambuc return ffs_snapshot_read(vp, uio, ioflag);
11184d9c625SLionel Sambuc #endif /* !LFS_READWRITE */
11284d9c625SLionel Sambuc
11384d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
11484d9c625SLionel Sambuc
11584d9c625SLionel Sambuc if (uio->uio_offset >= ip->i_size)
11684d9c625SLionel Sambuc goto out;
11784d9c625SLionel Sambuc
118*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG);
119*0a6a1f1dSLionel Sambuc advice = IO_ADV_DECODE(ap->a_ioflag);
12084d9c625SLionel Sambuc while (uio->uio_resid > 0) {
12184d9c625SLionel Sambuc if (ioflag & IO_DIRECT) {
12284d9c625SLionel Sambuc genfs_directio(vp, uio, ioflag);
12384d9c625SLionel Sambuc }
124*0a6a1f1dSLionel Sambuc bytelen = MIN(ip->i_size - uio->uio_offset, uio->uio_resid);
12584d9c625SLionel Sambuc if (bytelen == 0)
12684d9c625SLionel Sambuc break;
12784d9c625SLionel Sambuc error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
12884d9c625SLionel Sambuc UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
12984d9c625SLionel Sambuc if (error)
13084d9c625SLionel Sambuc break;
13184d9c625SLionel Sambuc }
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc out:
134*0a6a1f1dSLionel Sambuc error = ulfs_post_read_update(vp, ap->a_ioflag, error);
135*0a6a1f1dSLionel Sambuc fstrans_done(vp->v_mount);
136*0a6a1f1dSLionel Sambuc return (error);
13784d9c625SLionel Sambuc }
13884d9c625SLionel Sambuc
139*0a6a1f1dSLionel Sambuc /*
140*0a6a1f1dSLionel Sambuc * UFS op for reading via the buffer cache
141*0a6a1f1dSLionel Sambuc */
142*0a6a1f1dSLionel Sambuc int
BUFRD(struct vnode * vp,struct uio * uio,int ioflag,kauth_cred_t cred)143*0a6a1f1dSLionel Sambuc BUFRD(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
144*0a6a1f1dSLionel Sambuc {
145*0a6a1f1dSLionel Sambuc struct inode *ip;
146*0a6a1f1dSLionel Sambuc FS *fs;
147*0a6a1f1dSLionel Sambuc struct buf *bp;
148*0a6a1f1dSLionel Sambuc daddr_t lbn, nextlbn;
149*0a6a1f1dSLionel Sambuc off_t bytesinfile;
150*0a6a1f1dSLionel Sambuc long size, xfersize, blkoffset;
151*0a6a1f1dSLionel Sambuc int error;
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc KASSERT(VOP_ISLOCKED(vp));
154*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VDIR || vp->v_type == VLNK ||
155*0a6a1f1dSLionel Sambuc vp->v_type == VREG);
156*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_READ);
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc ip = VTOI(vp);
159*0a6a1f1dSLionel Sambuc fs = ip->I_FS;
160*0a6a1f1dSLionel Sambuc error = 0;
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VLNK || ip->i_size < fs->um_maxsymlinklen);
163*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VLNK || fs->um_maxsymlinklen != 0 ||
164*0a6a1f1dSLionel Sambuc DIP(ip, blocks) == 0);
165*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VREG || vp == fs->lfs_ivnode);
166*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM);
167*0a6a1f1dSLionel Sambuc
168*0a6a1f1dSLionel Sambuc if (uio->uio_offset > fs->um_maxfilesize)
169*0a6a1f1dSLionel Sambuc return EFBIG;
170*0a6a1f1dSLionel Sambuc if (uio->uio_resid == 0)
171*0a6a1f1dSLionel Sambuc return 0;
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc #ifndef LFS_READWRITE
174*0a6a1f1dSLionel Sambuc KASSERT(!ISSET(ip->i_flags, (SF_SNAPSHOT | SF_SNAPINVAL)));
175*0a6a1f1dSLionel Sambuc #endif
176*0a6a1f1dSLionel Sambuc
177*0a6a1f1dSLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc if (uio->uio_offset >= ip->i_size)
180*0a6a1f1dSLionel Sambuc goto out;
181*0a6a1f1dSLionel Sambuc
18284d9c625SLionel Sambuc for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
18384d9c625SLionel Sambuc bytesinfile = ip->i_size - uio->uio_offset;
18484d9c625SLionel Sambuc if (bytesinfile <= 0)
18584d9c625SLionel Sambuc break;
18684d9c625SLionel Sambuc lbn = lfs_lblkno(fs, uio->uio_offset);
18784d9c625SLionel Sambuc nextlbn = lbn + 1;
18884d9c625SLionel Sambuc size = lfs_blksize(fs, ip, lbn);
18984d9c625SLionel Sambuc blkoffset = lfs_blkoff(fs, uio->uio_offset);
190*0a6a1f1dSLionel Sambuc xfersize = MIN(MIN(fs_sb_getbsize(fs) - blkoffset, uio->uio_resid),
19184d9c625SLionel Sambuc bytesinfile);
19284d9c625SLionel Sambuc
19384d9c625SLionel Sambuc if (lfs_lblktosize(fs, nextlbn) >= ip->i_size)
194*0a6a1f1dSLionel Sambuc error = bread(vp, lbn, size, 0, &bp);
19584d9c625SLionel Sambuc else {
19684d9c625SLionel Sambuc int nextsize = lfs_blksize(fs, ip, nextlbn);
19784d9c625SLionel Sambuc error = breadn(vp, lbn,
198*0a6a1f1dSLionel Sambuc size, &nextlbn, &nextsize, 1, 0, &bp);
19984d9c625SLionel Sambuc }
20084d9c625SLionel Sambuc if (error)
20184d9c625SLionel Sambuc break;
20284d9c625SLionel Sambuc
20384d9c625SLionel Sambuc /*
20484d9c625SLionel Sambuc * We should only get non-zero b_resid when an I/O error
20584d9c625SLionel Sambuc * has occurred, which should cause us to break above.
20684d9c625SLionel Sambuc * However, if the short read did not cause an error,
20784d9c625SLionel Sambuc * then we want to ensure that we do not uiomove bad
20884d9c625SLionel Sambuc * or uninitialized data.
20984d9c625SLionel Sambuc */
21084d9c625SLionel Sambuc size -= bp->b_resid;
21184d9c625SLionel Sambuc if (size < xfersize) {
21284d9c625SLionel Sambuc if (size == 0)
21384d9c625SLionel Sambuc break;
21484d9c625SLionel Sambuc xfersize = size;
21584d9c625SLionel Sambuc }
21684d9c625SLionel Sambuc error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
21784d9c625SLionel Sambuc if (error)
21884d9c625SLionel Sambuc break;
21984d9c625SLionel Sambuc brelse(bp, 0);
22084d9c625SLionel Sambuc }
22184d9c625SLionel Sambuc if (bp != NULL)
22284d9c625SLionel Sambuc brelse(bp, 0);
22384d9c625SLionel Sambuc
22484d9c625SLionel Sambuc out:
225*0a6a1f1dSLionel Sambuc error = ulfs_post_read_update(vp, ioflag, error);
226*0a6a1f1dSLionel Sambuc fstrans_done(vp->v_mount);
227*0a6a1f1dSLionel Sambuc return (error);
228*0a6a1f1dSLionel Sambuc }
229*0a6a1f1dSLionel Sambuc
230*0a6a1f1dSLionel Sambuc static int
ulfs_post_read_update(struct vnode * vp,int ioflag,int oerror)231*0a6a1f1dSLionel Sambuc ulfs_post_read_update(struct vnode *vp, int ioflag, int oerror)
232*0a6a1f1dSLionel Sambuc {
233*0a6a1f1dSLionel Sambuc struct inode *ip = VTOI(vp);
234*0a6a1f1dSLionel Sambuc int error = oerror;
235*0a6a1f1dSLionel Sambuc
23684d9c625SLionel Sambuc if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
23784d9c625SLionel Sambuc ip->i_flag |= IN_ACCESS;
238*0a6a1f1dSLionel Sambuc if ((ioflag & IO_SYNC) == IO_SYNC) {
23984d9c625SLionel Sambuc error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
24084d9c625SLionel Sambuc }
24184d9c625SLionel Sambuc }
24284d9c625SLionel Sambuc
243*0a6a1f1dSLionel Sambuc /* Read error overrides any inode update error. */
244*0a6a1f1dSLionel Sambuc if (oerror)
245*0a6a1f1dSLionel Sambuc error = oerror;
246*0a6a1f1dSLionel Sambuc return error;
24784d9c625SLionel Sambuc }
24884d9c625SLionel Sambuc
24984d9c625SLionel Sambuc /*
25084d9c625SLionel Sambuc * Vnode op for writing.
25184d9c625SLionel Sambuc */
25284d9c625SLionel Sambuc int
WRITE(void * v)25384d9c625SLionel Sambuc WRITE(void *v)
25484d9c625SLionel Sambuc {
25584d9c625SLionel Sambuc struct vop_write_args /* {
25684d9c625SLionel Sambuc struct vnode *a_vp;
25784d9c625SLionel Sambuc struct uio *a_uio;
25884d9c625SLionel Sambuc int a_ioflag;
25984d9c625SLionel Sambuc kauth_cred_t a_cred;
26084d9c625SLionel Sambuc } */ *ap = v;
26184d9c625SLionel Sambuc struct vnode *vp;
26284d9c625SLionel Sambuc struct uio *uio;
26384d9c625SLionel Sambuc struct inode *ip;
26484d9c625SLionel Sambuc FS *fs;
26584d9c625SLionel Sambuc kauth_cred_t cred;
26684d9c625SLionel Sambuc off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
267*0a6a1f1dSLionel Sambuc int blkoffset, error, flags, ioflag, resid;
26884d9c625SLionel Sambuc int aflag;
26984d9c625SLionel Sambuc int extended=0;
27084d9c625SLionel Sambuc vsize_t bytelen;
27184d9c625SLionel Sambuc bool async;
27284d9c625SLionel Sambuc
27384d9c625SLionel Sambuc cred = ap->a_cred;
27484d9c625SLionel Sambuc ioflag = ap->a_ioflag;
27584d9c625SLionel Sambuc uio = ap->a_uio;
27684d9c625SLionel Sambuc vp = ap->a_vp;
27784d9c625SLionel Sambuc ip = VTOI(vp);
27884d9c625SLionel Sambuc
27984d9c625SLionel Sambuc KASSERT(vp->v_size == ip->i_size);
280*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_WRITE);
281*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG);
28284d9c625SLionel Sambuc
28384d9c625SLionel Sambuc if (ioflag & IO_APPEND)
28484d9c625SLionel Sambuc uio->uio_offset = ip->i_size;
28584d9c625SLionel Sambuc if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
28684d9c625SLionel Sambuc return (EPERM);
28784d9c625SLionel Sambuc
28884d9c625SLionel Sambuc fs = ip->I_FS;
28984d9c625SLionel Sambuc if (uio->uio_offset < 0 ||
29084d9c625SLionel Sambuc (u_int64_t)uio->uio_offset + uio->uio_resid > fs->um_maxfilesize)
29184d9c625SLionel Sambuc return (EFBIG);
29284d9c625SLionel Sambuc #ifdef LFS_READWRITE
29384d9c625SLionel Sambuc /* Disallow writes to the Ifile, even if noschg flag is removed */
29484d9c625SLionel Sambuc /* XXX can this go away when the Ifile is no longer in the namespace? */
29584d9c625SLionel Sambuc if (vp == fs->lfs_ivnode)
29684d9c625SLionel Sambuc return (EPERM);
29784d9c625SLionel Sambuc #endif
29884d9c625SLionel Sambuc if (uio->uio_resid == 0)
29984d9c625SLionel Sambuc return (0);
30084d9c625SLionel Sambuc
30184d9c625SLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
30284d9c625SLionel Sambuc
30384d9c625SLionel Sambuc flags = ioflag & IO_SYNC ? B_SYNC : 0;
30484d9c625SLionel Sambuc async = vp->v_mount->mnt_flag & MNT_ASYNC;
30584d9c625SLionel Sambuc origoff = uio->uio_offset;
30684d9c625SLionel Sambuc resid = uio->uio_resid;
30784d9c625SLionel Sambuc osize = ip->i_size;
30884d9c625SLionel Sambuc error = 0;
30984d9c625SLionel Sambuc
310*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VREG);
31184d9c625SLionel Sambuc
31284d9c625SLionel Sambuc #ifdef LFS_READWRITE
31384d9c625SLionel Sambuc async = true;
31484d9c625SLionel Sambuc lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
31584d9c625SLionel Sambuc lfs_check(vp, LFS_UNUSED_LBN, 0);
31684d9c625SLionel Sambuc #endif /* !LFS_READWRITE */
31784d9c625SLionel Sambuc
31884d9c625SLionel Sambuc preallocoff = round_page(lfs_blkroundup(fs, MAX(osize, uio->uio_offset)));
31984d9c625SLionel Sambuc aflag = ioflag & IO_SYNC ? B_SYNC : 0;
32084d9c625SLionel Sambuc nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
32184d9c625SLionel Sambuc endallocoff = nsize - lfs_blkoff(fs, nsize);
32284d9c625SLionel Sambuc
32384d9c625SLionel Sambuc /*
32484d9c625SLionel Sambuc * if we're increasing the file size, deal with expanding
32584d9c625SLionel Sambuc * the fragment if there is one.
32684d9c625SLionel Sambuc */
32784d9c625SLionel Sambuc
32884d9c625SLionel Sambuc if (nsize > osize && lfs_lblkno(fs, osize) < ULFS_NDADDR &&
32984d9c625SLionel Sambuc lfs_lblkno(fs, osize) != lfs_lblkno(fs, nsize) &&
33084d9c625SLionel Sambuc lfs_blkroundup(fs, osize) != osize) {
33184d9c625SLionel Sambuc off_t eob;
33284d9c625SLionel Sambuc
33384d9c625SLionel Sambuc eob = lfs_blkroundup(fs, osize);
33484d9c625SLionel Sambuc uvm_vnp_setwritesize(vp, eob);
33584d9c625SLionel Sambuc error = ulfs_balloc_range(vp, osize, eob - osize, cred, aflag);
33684d9c625SLionel Sambuc if (error)
33784d9c625SLionel Sambuc goto out;
33884d9c625SLionel Sambuc if (flags & B_SYNC) {
33984d9c625SLionel Sambuc mutex_enter(vp->v_interlock);
340*0a6a1f1dSLionel Sambuc VOP_PUTPAGES(vp, trunc_page(osize & lfs_sb_getbmask(fs)),
34184d9c625SLionel Sambuc round_page(eob),
342*0a6a1f1dSLionel Sambuc PGO_CLEANIT | PGO_SYNCIO);
34384d9c625SLionel Sambuc }
34484d9c625SLionel Sambuc }
34584d9c625SLionel Sambuc
34684d9c625SLionel Sambuc while (uio->uio_resid > 0) {
34784d9c625SLionel Sambuc int ubc_flags = UBC_WRITE;
34884d9c625SLionel Sambuc bool overwrite; /* if we're overwrite a whole block */
34984d9c625SLionel Sambuc off_t newoff;
35084d9c625SLionel Sambuc
35184d9c625SLionel Sambuc if (ioflag & IO_DIRECT) {
352*0a6a1f1dSLionel Sambuc genfs_directio(vp, uio, ioflag);
35384d9c625SLionel Sambuc }
35484d9c625SLionel Sambuc
35584d9c625SLionel Sambuc oldoff = uio->uio_offset;
35684d9c625SLionel Sambuc blkoffset = lfs_blkoff(fs, uio->uio_offset);
357*0a6a1f1dSLionel Sambuc bytelen = MIN(fs_sb_getbsize(fs) - blkoffset, uio->uio_resid);
35884d9c625SLionel Sambuc if (bytelen == 0) {
35984d9c625SLionel Sambuc break;
36084d9c625SLionel Sambuc }
36184d9c625SLionel Sambuc
36284d9c625SLionel Sambuc /*
36384d9c625SLionel Sambuc * if we're filling in a hole, allocate the blocks now and
36484d9c625SLionel Sambuc * initialize the pages first. if we're extending the file,
36584d9c625SLionel Sambuc * we can safely allocate blocks without initializing pages
36684d9c625SLionel Sambuc * since the new blocks will be inaccessible until the write
36784d9c625SLionel Sambuc * is complete.
36884d9c625SLionel Sambuc */
36984d9c625SLionel Sambuc overwrite = uio->uio_offset >= preallocoff &&
37084d9c625SLionel Sambuc uio->uio_offset < endallocoff;
37184d9c625SLionel Sambuc if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
37284d9c625SLionel Sambuc lfs_blkoff(fs, uio->uio_offset) == 0 &&
37384d9c625SLionel Sambuc (uio->uio_offset & PAGE_MASK) == 0) {
37484d9c625SLionel Sambuc vsize_t len;
37584d9c625SLionel Sambuc
37684d9c625SLionel Sambuc len = trunc_page(bytelen);
37784d9c625SLionel Sambuc len -= lfs_blkoff(fs, len);
37884d9c625SLionel Sambuc if (len > 0) {
37984d9c625SLionel Sambuc overwrite = true;
38084d9c625SLionel Sambuc bytelen = len;
38184d9c625SLionel Sambuc }
38284d9c625SLionel Sambuc }
38384d9c625SLionel Sambuc
38484d9c625SLionel Sambuc newoff = oldoff + bytelen;
38584d9c625SLionel Sambuc if (vp->v_size < newoff) {
38684d9c625SLionel Sambuc uvm_vnp_setwritesize(vp, newoff);
38784d9c625SLionel Sambuc }
38884d9c625SLionel Sambuc
38984d9c625SLionel Sambuc if (!overwrite) {
39084d9c625SLionel Sambuc error = ulfs_balloc_range(vp, uio->uio_offset, bytelen,
39184d9c625SLionel Sambuc cred, aflag);
39284d9c625SLionel Sambuc if (error)
39384d9c625SLionel Sambuc break;
39484d9c625SLionel Sambuc } else {
39584d9c625SLionel Sambuc genfs_node_wrlock(vp);
39684d9c625SLionel Sambuc error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
39784d9c625SLionel Sambuc aflag, cred);
39884d9c625SLionel Sambuc genfs_node_unlock(vp);
39984d9c625SLionel Sambuc if (error)
40084d9c625SLionel Sambuc break;
40184d9c625SLionel Sambuc ubc_flags |= UBC_FAULTBUSY;
40284d9c625SLionel Sambuc }
40384d9c625SLionel Sambuc
40484d9c625SLionel Sambuc /*
40584d9c625SLionel Sambuc * copy the data.
40684d9c625SLionel Sambuc */
40784d9c625SLionel Sambuc
40884d9c625SLionel Sambuc error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
40984d9c625SLionel Sambuc IO_ADV_DECODE(ioflag), ubc_flags | UBC_UNMAP_FLAG(vp));
41084d9c625SLionel Sambuc
41184d9c625SLionel Sambuc /*
41284d9c625SLionel Sambuc * update UVM's notion of the size now that we've
41384d9c625SLionel Sambuc * copied the data into the vnode's pages.
41484d9c625SLionel Sambuc *
41584d9c625SLionel Sambuc * we should update the size even when uiomove failed.
41684d9c625SLionel Sambuc */
41784d9c625SLionel Sambuc
41884d9c625SLionel Sambuc if (vp->v_size < newoff) {
41984d9c625SLionel Sambuc uvm_vnp_setsize(vp, newoff);
42084d9c625SLionel Sambuc extended = 1;
42184d9c625SLionel Sambuc }
42284d9c625SLionel Sambuc
42384d9c625SLionel Sambuc if (error)
42484d9c625SLionel Sambuc break;
42584d9c625SLionel Sambuc
42684d9c625SLionel Sambuc /*
42784d9c625SLionel Sambuc * flush what we just wrote if necessary.
42884d9c625SLionel Sambuc * XXXUBC simplistic async flushing.
42984d9c625SLionel Sambuc */
43084d9c625SLionel Sambuc
43184d9c625SLionel Sambuc #ifndef LFS_READWRITE
43284d9c625SLionel Sambuc if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
43384d9c625SLionel Sambuc mutex_enter(vp->v_interlock);
43484d9c625SLionel Sambuc error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
43584d9c625SLionel Sambuc (uio->uio_offset >> 16) << 16,
436*0a6a1f1dSLionel Sambuc PGO_CLEANIT | PGO_LAZY);
43784d9c625SLionel Sambuc if (error)
43884d9c625SLionel Sambuc break;
43984d9c625SLionel Sambuc }
44084d9c625SLionel Sambuc #else
44184d9c625SLionel Sambuc __USE(async);
44284d9c625SLionel Sambuc #endif
44384d9c625SLionel Sambuc }
44484d9c625SLionel Sambuc if (error == 0 && ioflag & IO_SYNC) {
44584d9c625SLionel Sambuc mutex_enter(vp->v_interlock);
446*0a6a1f1dSLionel Sambuc error = VOP_PUTPAGES(vp, trunc_page(origoff & lfs_sb_getbmask(fs)),
44784d9c625SLionel Sambuc round_page(lfs_blkroundup(fs, uio->uio_offset)),
448*0a6a1f1dSLionel Sambuc PGO_CLEANIT | PGO_SYNCIO);
44984d9c625SLionel Sambuc }
45084d9c625SLionel Sambuc
451*0a6a1f1dSLionel Sambuc out:
452*0a6a1f1dSLionel Sambuc error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
453*0a6a1f1dSLionel Sambuc extended, error);
454*0a6a1f1dSLionel Sambuc fstrans_done(vp->v_mount);
455*0a6a1f1dSLionel Sambuc
456*0a6a1f1dSLionel Sambuc return (error);
457*0a6a1f1dSLionel Sambuc }
458*0a6a1f1dSLionel Sambuc
459*0a6a1f1dSLionel Sambuc /*
460*0a6a1f1dSLionel Sambuc * UFS op for writing via the buffer cache
461*0a6a1f1dSLionel Sambuc */
462*0a6a1f1dSLionel Sambuc int
BUFWR(struct vnode * vp,struct uio * uio,int ioflag,kauth_cred_t cred)463*0a6a1f1dSLionel Sambuc BUFWR(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
464*0a6a1f1dSLionel Sambuc {
465*0a6a1f1dSLionel Sambuc struct inode *ip;
466*0a6a1f1dSLionel Sambuc FS *fs;
467*0a6a1f1dSLionel Sambuc int flags;
468*0a6a1f1dSLionel Sambuc struct buf *bp;
469*0a6a1f1dSLionel Sambuc off_t osize;
470*0a6a1f1dSLionel Sambuc int resid, xfersize, size, blkoffset;
471*0a6a1f1dSLionel Sambuc daddr_t lbn;
472*0a6a1f1dSLionel Sambuc int extended=0;
473*0a6a1f1dSLionel Sambuc int error;
474*0a6a1f1dSLionel Sambuc #ifdef LFS_READWRITE
475*0a6a1f1dSLionel Sambuc bool need_unreserve = false;
476*0a6a1f1dSLionel Sambuc #endif
477*0a6a1f1dSLionel Sambuc
478*0a6a1f1dSLionel Sambuc KASSERT(ISSET(ioflag, IO_NODELOCKED));
479*0a6a1f1dSLionel Sambuc KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
480*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
481*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
482*0a6a1f1dSLionel Sambuc KASSERT(uio->uio_rw == UIO_WRITE);
483*0a6a1f1dSLionel Sambuc
484*0a6a1f1dSLionel Sambuc ip = VTOI(vp);
485*0a6a1f1dSLionel Sambuc fs = ip->I_FS;
486*0a6a1f1dSLionel Sambuc
487*0a6a1f1dSLionel Sambuc KASSERT(vp->v_size == ip->i_size);
488*0a6a1f1dSLionel Sambuc
489*0a6a1f1dSLionel Sambuc if (uio->uio_offset < 0 ||
490*0a6a1f1dSLionel Sambuc uio->uio_resid > fs->um_maxfilesize ||
491*0a6a1f1dSLionel Sambuc uio->uio_offset > (fs->um_maxfilesize - uio->uio_resid))
492*0a6a1f1dSLionel Sambuc return EFBIG;
493*0a6a1f1dSLionel Sambuc #ifdef LFS_READWRITE
494*0a6a1f1dSLionel Sambuc KASSERT(vp != fs->lfs_ivnode);
495*0a6a1f1dSLionel Sambuc #endif
496*0a6a1f1dSLionel Sambuc if (uio->uio_resid == 0)
497*0a6a1f1dSLionel Sambuc return 0;
498*0a6a1f1dSLionel Sambuc
499*0a6a1f1dSLionel Sambuc fstrans_start(vp->v_mount, FSTRANS_SHARED);
500*0a6a1f1dSLionel Sambuc
501*0a6a1f1dSLionel Sambuc flags = ioflag & IO_SYNC ? B_SYNC : 0;
502*0a6a1f1dSLionel Sambuc resid = uio->uio_resid;
503*0a6a1f1dSLionel Sambuc osize = ip->i_size;
504*0a6a1f1dSLionel Sambuc error = 0;
505*0a6a1f1dSLionel Sambuc
506*0a6a1f1dSLionel Sambuc KASSERT(vp->v_type != VREG);
507*0a6a1f1dSLionel Sambuc
508*0a6a1f1dSLionel Sambuc #ifdef LFS_READWRITE
509*0a6a1f1dSLionel Sambuc lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
510*0a6a1f1dSLionel Sambuc lfs_check(vp, LFS_UNUSED_LBN, 0);
511*0a6a1f1dSLionel Sambuc #endif /* !LFS_READWRITE */
512*0a6a1f1dSLionel Sambuc
513*0a6a1f1dSLionel Sambuc /* XXX Should never have pages cached here. */
514*0a6a1f1dSLionel Sambuc KASSERT(vp->v_uobj.uo_npages == 0);
51584d9c625SLionel Sambuc while (uio->uio_resid > 0) {
51684d9c625SLionel Sambuc lbn = lfs_lblkno(fs, uio->uio_offset);
51784d9c625SLionel Sambuc blkoffset = lfs_blkoff(fs, uio->uio_offset);
518*0a6a1f1dSLionel Sambuc xfersize = MIN(fs_sb_getbsize(fs) - blkoffset, uio->uio_resid);
519*0a6a1f1dSLionel Sambuc if (fs_sb_getbsize(fs) > xfersize)
52084d9c625SLionel Sambuc flags |= B_CLRBUF;
52184d9c625SLionel Sambuc else
52284d9c625SLionel Sambuc flags &= ~B_CLRBUF;
52384d9c625SLionel Sambuc
52484d9c625SLionel Sambuc #ifdef LFS_READWRITE
52584d9c625SLionel Sambuc error = lfs_reserve(fs, vp, NULL,
526*0a6a1f1dSLionel Sambuc lfs_btofsb(fs, (ULFS_NIADDR + 1) << lfs_sb_getbshift(fs)));
52784d9c625SLionel Sambuc if (error)
52884d9c625SLionel Sambuc break;
52984d9c625SLionel Sambuc need_unreserve = true;
53084d9c625SLionel Sambuc #endif
531*0a6a1f1dSLionel Sambuc error = lfs_balloc(vp, uio->uio_offset, xfersize, cred, flags,
532*0a6a1f1dSLionel Sambuc &bp);
53384d9c625SLionel Sambuc
53484d9c625SLionel Sambuc if (error)
53584d9c625SLionel Sambuc break;
53684d9c625SLionel Sambuc if (uio->uio_offset + xfersize > ip->i_size) {
53784d9c625SLionel Sambuc ip->i_size = uio->uio_offset + xfersize;
53884d9c625SLionel Sambuc DIP_ASSIGN(ip, size, ip->i_size);
53984d9c625SLionel Sambuc uvm_vnp_setsize(vp, ip->i_size);
54084d9c625SLionel Sambuc extended = 1;
54184d9c625SLionel Sambuc }
54284d9c625SLionel Sambuc size = lfs_blksize(fs, ip, lbn) - bp->b_resid;
54384d9c625SLionel Sambuc if (xfersize > size)
54484d9c625SLionel Sambuc xfersize = size;
54584d9c625SLionel Sambuc
54684d9c625SLionel Sambuc error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
54784d9c625SLionel Sambuc
54884d9c625SLionel Sambuc /*
54984d9c625SLionel Sambuc * if we didn't clear the block and the uiomove failed,
55084d9c625SLionel Sambuc * the buf will now contain part of some other file,
55184d9c625SLionel Sambuc * so we need to invalidate it.
55284d9c625SLionel Sambuc */
55384d9c625SLionel Sambuc if (error && (flags & B_CLRBUF) == 0) {
55484d9c625SLionel Sambuc brelse(bp, BC_INVAL);
55584d9c625SLionel Sambuc break;
55684d9c625SLionel Sambuc }
55784d9c625SLionel Sambuc #ifdef LFS_READWRITE
55884d9c625SLionel Sambuc (void)VOP_BWRITE(bp->b_vp, bp);
55984d9c625SLionel Sambuc lfs_reserve(fs, vp, NULL,
560*0a6a1f1dSLionel Sambuc -lfs_btofsb(fs, (ULFS_NIADDR + 1) << lfs_sb_getbshift(fs)));
56184d9c625SLionel Sambuc need_unreserve = false;
56284d9c625SLionel Sambuc #else
56384d9c625SLionel Sambuc if (ioflag & IO_SYNC)
56484d9c625SLionel Sambuc (void)bwrite(bp);
56584d9c625SLionel Sambuc else if (xfersize + blkoffset == fs->fs_bsize)
56684d9c625SLionel Sambuc bawrite(bp);
56784d9c625SLionel Sambuc else
56884d9c625SLionel Sambuc bdwrite(bp);
56984d9c625SLionel Sambuc #endif
57084d9c625SLionel Sambuc if (error || xfersize == 0)
57184d9c625SLionel Sambuc break;
57284d9c625SLionel Sambuc }
57384d9c625SLionel Sambuc #ifdef LFS_READWRITE
57484d9c625SLionel Sambuc if (need_unreserve) {
57584d9c625SLionel Sambuc lfs_reserve(fs, vp, NULL,
576*0a6a1f1dSLionel Sambuc -lfs_btofsb(fs, (ULFS_NIADDR + 1) << lfs_sb_getbshift(fs)));
57784d9c625SLionel Sambuc }
57884d9c625SLionel Sambuc #endif
57984d9c625SLionel Sambuc
580*0a6a1f1dSLionel Sambuc error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
581*0a6a1f1dSLionel Sambuc extended, error);
582*0a6a1f1dSLionel Sambuc fstrans_done(vp->v_mount);
583*0a6a1f1dSLionel Sambuc
584*0a6a1f1dSLionel Sambuc return (error);
585*0a6a1f1dSLionel Sambuc }
586*0a6a1f1dSLionel Sambuc
587*0a6a1f1dSLionel Sambuc static int
ulfs_post_write_update(struct vnode * vp,struct uio * uio,int ioflag,kauth_cred_t cred,off_t osize,int resid,int extended,int oerror)588*0a6a1f1dSLionel Sambuc ulfs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
589*0a6a1f1dSLionel Sambuc kauth_cred_t cred, off_t osize, int resid, int extended, int oerror)
590*0a6a1f1dSLionel Sambuc {
591*0a6a1f1dSLionel Sambuc struct inode *ip = VTOI(vp);
592*0a6a1f1dSLionel Sambuc int error = oerror;
593*0a6a1f1dSLionel Sambuc
594*0a6a1f1dSLionel Sambuc /* Trigger ctime and mtime updates, and atime if MNT_RELATIME. */
59584d9c625SLionel Sambuc ip->i_flag |= IN_CHANGE | IN_UPDATE;
59684d9c625SLionel Sambuc if (vp->v_mount->mnt_flag & MNT_RELATIME)
59784d9c625SLionel Sambuc ip->i_flag |= IN_ACCESS;
598*0a6a1f1dSLionel Sambuc
599*0a6a1f1dSLionel Sambuc /*
600*0a6a1f1dSLionel Sambuc * If we successfully wrote any data and we are not the superuser,
601*0a6a1f1dSLionel Sambuc * we clear the setuid and setgid bits as a precaution against
602*0a6a1f1dSLionel Sambuc * tampering.
603*0a6a1f1dSLionel Sambuc */
604*0a6a1f1dSLionel Sambuc if (resid > uio->uio_resid && cred) {
60584d9c625SLionel Sambuc if (ip->i_mode & ISUID) {
606*0a6a1f1dSLionel Sambuc if (kauth_authorize_vnode(cred,
60784d9c625SLionel Sambuc KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
60884d9c625SLionel Sambuc ip->i_mode &= ~ISUID;
60984d9c625SLionel Sambuc DIP_ASSIGN(ip, mode, ip->i_mode);
61084d9c625SLionel Sambuc }
61184d9c625SLionel Sambuc }
61284d9c625SLionel Sambuc
61384d9c625SLionel Sambuc if (ip->i_mode & ISGID) {
614*0a6a1f1dSLionel Sambuc if (kauth_authorize_vnode(cred,
61584d9c625SLionel Sambuc KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
61684d9c625SLionel Sambuc ip->i_mode &= ~ISGID;
61784d9c625SLionel Sambuc DIP_ASSIGN(ip, mode, ip->i_mode);
61884d9c625SLionel Sambuc }
61984d9c625SLionel Sambuc }
62084d9c625SLionel Sambuc }
621*0a6a1f1dSLionel Sambuc
622*0a6a1f1dSLionel Sambuc /* If we successfully wrote anything, notify kevent listeners. */
62384d9c625SLionel Sambuc if (resid > uio->uio_resid)
62484d9c625SLionel Sambuc VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
625*0a6a1f1dSLionel Sambuc
626*0a6a1f1dSLionel Sambuc /*
627*0a6a1f1dSLionel Sambuc * Update the size on disk: truncate back to original size on
628*0a6a1f1dSLionel Sambuc * error, or reflect the new size on success.
629*0a6a1f1dSLionel Sambuc */
63084d9c625SLionel Sambuc if (error) {
631*0a6a1f1dSLionel Sambuc (void) lfs_truncate(vp, osize, ioflag & IO_SYNC, cred);
63284d9c625SLionel Sambuc uio->uio_offset -= resid - uio->uio_resid;
63384d9c625SLionel Sambuc uio->uio_resid = resid;
63484d9c625SLionel Sambuc } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
63584d9c625SLionel Sambuc error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
63684d9c625SLionel Sambuc } else {
63784d9c625SLionel Sambuc /* nothing */
63884d9c625SLionel Sambuc }
63984d9c625SLionel Sambuc
640*0a6a1f1dSLionel Sambuc /* Make sure the vnode uvm size matches the inode file size. */
641*0a6a1f1dSLionel Sambuc KASSERT(vp->v_size == ip->i_size);
642*0a6a1f1dSLionel Sambuc
643*0a6a1f1dSLionel Sambuc /* Write error overrides any inode update error. */
644*0a6a1f1dSLionel Sambuc if (oerror)
645*0a6a1f1dSLionel Sambuc error = oerror;
646*0a6a1f1dSLionel Sambuc return error;
64784d9c625SLionel Sambuc }
648